S06-11 - Reconnect, Loss and Recovery
Purpose: What happens when the host-guest link drops, when a session is lost, and how a lost session’s work is recovered or discarded.
Design
Section titled “Design”Reconnect
Section titled “Reconnect”When the host-guest link drops:
wb-guestdstops the project processes (S13-01-process-hardeningS13-01-process-hardening: Layer 1: Process Hardening (v1)How wb-guestd hardens every process it starts for a project user, and how it stops and kills a project user's processes.);wb-hostdconnects to it again (S04-architectureS04-architecture: ArchitectureName the processes, the boundaries between them, and which requirement each boundary serves.).
wb-hostd waits 0.1 s before the first try, and doubles the wait
after each failed one, up to 5 s. It opens at most 20 connections to
one VM in a minute (SEC13-bounded-resourcesSEC13-bounded-resources: Bounded resourcesCPU, memory and disk of each VM are capped.).
A guest that answers the hello and then stops answering also counts as
a drop. wb-hostd drops the link with guest-time-unanswered when a
time call (S06-06-devices-and-warm-startS06-06-devices-and-warm-start: Devices, Warm Start and TimeA VM's devices and resources, saving and restoring its state, and how the guest clock follows the host.) is still unanswered at the
link’s call deadline, or fails because the connection failed. The
call’s status code is guest input: the host can’t tell a failed
transport from an Unavailable that the guest sent, and both lead to
the same reconnect, which the waits and the cap above bound. So the
link is never shown as up for more than a minute plus that deadline
after the guest’s last answer.
The list
Section titled “The list”After it connects, wb-guestd lists what wb-hostd hasn’t
acknowledged:
- the sessions it holds;
- the reports of finished sessions (S06-09-session-lifecycleS06-09-session-lifecycle: Session LifecycleA session's states from wb claude to its summary, how it starts and ends, its deadlines, and when its VM may be saved., “Ending”);
- the cleanup records, each with its kill ID (S06-10-leftover-processesS06-10-leftover-processes: Leftover ProcessesThe results of the kill of a project user, how wb-hostd accepts them, and what it does when processes are left.).
The list is guest input (SEC11-root-gains-nothingSEC11-root-gains-nothing: Guest root is not a privilegeFull control of the guest grants no control over enforcement, secrets, or policy.). wb-hostd refuses
the whole list when:
- it has more than 64 entries, held sessions and reports counted together;
- it has more cleanup records than project users in the VM;
- it names one ID twice, as a held session and as a report, or twice as either;
- it has two records with one kill ID.
It logs the refusal and closes the connection, which counts as a failed try.
wb-hostd runs at most 64 sessions in one VM.
Answers to sessions and reports
Section titled “Answers to sessions and reports”For each entry, wb-hostd first checks the session ID format of
S05-04-namingS05-04-naming: NamingHow wb identifies a project, a session and its branch, and how projects are registered, checked, moved and removed.. Then it looks for a session of this VM and this VM
generation only, and answers:
- “resume” for a running or paused session whose
wbis still connected. The session goes back to running. - “finish” for a running or paused session whose
wbhas gone, and for an ending session. The session goes to ending. - “acknowledged” for a report of a session of this VM and this VM
generation that
wb-hostdhas as running, paused, ending, or lost. The session ends with that report as at a normal end. The checks of S06-09-session-lifecycleS06-09-session-lifecycle: Session LifecycleA session's states from wb claude to its summary, how it starts and ends, its deadlines, and when its VM may be saved. (S06-09-session-lifecycleS06-09-session-lifecycle: Session LifecycleA session's states from wb claude to its summary, how it starts and ends, its deadlines, and when its VM may be saved., “Ending”, “Returned work comes from the host”) apply. - “acknowledged” again for a report of a session that
wb-hostdalready has as ended. It logs that, and doesn’t change the session. - “end” for every other entry, a report included.
A lost session of this VM gets “finish” from recovery (“Recovery” below).
On “finish”, wb-guestd:
- hangs up as in S06-09-session-lifecycleS06-09-session-lifecycle: Session LifecycleA session's states from wb claude to its summary, how it starts and ends, its deadlines, and when its VM may be saved., “Closing the terminal ends the session”, if
claudestill runs; - kills every process left in the session, a stopped WIP commit or push included;
- removes the stale
index.lockthat a killed commit left (S06-09-session-lifecycleS06-09-session-lifecycle: Session LifecycleA session's states from wb claude to its summary, how it starts and ends, its deadlines, and when its VM may be saved., “As the project user”); - commits and pushes WIP again.
“end” goes to these entries:
- a starting session, whose start has failed;
- a session of another VM;
- a running, paused or ending session of another VM generation;
- an ended session;
- a malformed ID;
- an ID that
wb-hostdhas no record of.
On “end”, wb-guestd kills every process left in that session, a
stopped WIP commit or push included. It doesn’t commit or push. It
deletes the entry’s report and recorded POSIX session ID. For a
session whose start failed, or that was discarded, it also removes
the worktree and its history link. wb-hostd logs each such entry
with the rule.
Answers to cleanup records
Section titled “Answers to cleanup records”A cleanup record gets “acknowledged” when it passes the checks of S06-10-leftover-processesS06-10-leftover-processes: Leftover ProcessesThe results of the kill of a project user, how wb-hostd accepts them, and what it does when processes are left., or was already acknowledged. Otherwise it gets “end”, which deletes it.
Every entry gets one of these answers, so every report and record is cleared.
Sessions the list doesn’t hold
Section titled “Sessions the list doesn’t hold”wb-guestd resumes a session only on “resume”.
A session of this VM that wb-hostd has as running, paused or ending
is lost when the list doesn’t hold it, as a held session or as a
report. A starting session isn’t in that rule, because a link drop
already failed its start.
A false list can only stop, keep, or end sessions in that guest.
wb doesn’t print anything while a session is paused.
Pause limit
Section titled “Pause limit”A session that is paused for 30 seconds of host time awake is lost. Time the host spends asleep doesn’t count.
A restore reconnects in about 0.2 s (X18-vsock-handoffX18-vsock-handoff: Host-guest socket and descriptor hand-offCan wb-guestd, written in Go, open AFVSOCK sockets in a macOS guest?). A
wb-guestd restart loses the session anyway (“Lost” below). So the
limit only has to outlast a slow reconnect.
A session is lost:
- when
wb-vmdreports its VM stopped; - when its
wb-guestdexits. The reconnectedwb-guestddoesn’t list it, and a newwb-guestdprocess never takes over an earlier one’s sessions; - when
wb-hostdexits, which stops every VM; - at the pause limit.
A wb-guestd exit closes the PTYs it holds. So claude gets a hangup
from the guest kernel.
wb prints a wraith box: line that names the session, and says its
WIP returns at the next connection to the VM. Then it exits with 255
(S05-cliS05-cli: Command Line InterfaceSpecify wb, the single user-facing command.).
When wb-hostd starts, it marks as lost every session it has as
starting, running, paused or ending.
What a lost session counts as
Section titled “What a lost session counts as”A lost session counts as ended for these:
- the effective policy of S07-01-per-vm-policyS07-01-per-vm-policy: Enforced per VMWhy the VM is the unit the gateway enforces, how the policies of its projects combine, and what happens when they conflict or change.. The project’s grants leave the VM when no other session of the project is starting, running, paused or ending there;
wb vm stopandwb vm suspend.
A lost session still counts as a session for the lock and the kill of
a project’s last session (S13-01-process-hardeningS13-01-process-hardening: Layer 1: Process Hardening (v1)How wb-guestd hardens every process it starts for a project user, and how it stops and kills a project user's processes.). So the
project user is locked only after its lost sessions have ended. They
end by recovery, wb discard, wb project rm or the deadline.
Recovery
Section titled “Recovery”A lost session’s worktree stays on the data disk.
At the next connection to a wb-guestd in that VM, wb-hostd sends
“finish” for each lost session of the VM that it has a record of,
whatever else the list holds. That connection is after a restart, or
at the next VM start. Recovery skips a lost session whose report the
list holds. “acknowledged” ends that one instead (“Reconnect”).
On “finish”, wb-guestd kills what is left of the session’s
processes. Then it commits and pushes WIP, as at a normal end.
During recovery, wb-hostd:
- accepts a push for the lost session only to its own branch (S08-03-pushesS08-03-pushes: PushesWhere the guest's pushes land, the settings receive-pack runs with, which refs a push may write, and the push at session end., “Ref restriction”);
- doesn’t give network grants on account of the lost session.
The session is then ended, with “lost” as its exit status in wb sessions. When this was the project’s last session in the VM, the
project user is then locked and its processes killed
(S13-01-process-hardeningS13-01-process-hardening: Layer 1: Process Hardening (v1)How wb-guestd hardens every process it starts for a project user, and how it stops and kills a project user's processes.).
A lost session stays live for export.git until it ends
(S08-02-export-repositoryS08-02-export-repository: Export RepositoryWhich refs and objects the guest can fetch from export.git, and how wb, its one writer, keeps it safe to run git in.). So its WIP push still has its base.
Discarding a lost session
Section titled “Discarding a lost session”wb discard on a lost session ends it with no recovery push. So does
wb project rm, for each lost session of the project. wb-hostd:
- takes the project’s landing lock (S08-05-push-limitsS08-05-push-limits: Push LimitsCleanup after a push that failed, the size cap of landing.git, and the time and memory bounds of the git gateway., “Landing lock”);
- records the session as ended and discarded;
- only then deletes its refs.
A recovery push that already holds the lock finishes first. Its refs are deleted after it. A later one is refused, because the session is no longer live.
At the next connection to the VM, wb-guestd gets “end” for the
session. It removes the worktree and its history link.
Decisions
Section titled “Decisions”A wb-hostd exit stops every VM
Section titled “A wb-hostd exit stops every VM”Status: Draft