S06-09 - Session Lifecycle
Purpose: A session’s states from wb claude to its summary, how it
starts and ends, its deadlines, and when its VM may be saved.
Design
Section titled “Design”A session runs from wb claude to its summary.
wb-hostdkeeps each session’s record and state instate.db(S04-architectureS04-architecture: ArchitectureName the processes, the boundaries between them, and which requirement each boundary serves., “Host state”). The record has the VM and the VM generation the session runs in. The debug shells of V1-M2-walking-skeletonV1-M2-walking-skeleton: Walking skeletonwb-vmd boots a fixture VM bundle in its confined process, wb-guestd answers over vsock, a data disk made on the host holds the homes, and wb shell opens a PTY as a project user, with its output through the terminal fi… are kept in memory instead (S06-08-projects-and-sessionsS06-08-projects-and-sessions: Projects and Sessions Inside a VMHow projects map onto guest users and sessions onto worktrees, and how Claude Code runs in the guest., “Debug shells in V1-M2-walking-skeletonV1-M2-walking-skeleton: Walking skeletonwb-vmd boots a fixture VM bundle in its confined process, wb-guestd answers over vsock, a data disk made on the host holds the homes, and wb shell opens a PTY as a project user, with its output through the terminal fi…”).wb-guestdholds the session’s processes. In interactive use it also holds its terminal device (PTY).
Closing the terminal ends the session. V1V1-initial: Initial versionThe first version: what it delivers, what it leaves out, and the milestones that get there. has no detach and no wb attach. A session has one client, the wb process that started it.
Session states
Section titled “Session states”| State | What holds | Link drops | VM or daemon stops | Other exits |
|---|---|---|---|---|
| starting | wb-hostd admits the session, starts or restores the VM, and wb-guestd creates the worktree and applies the carry-in (S08-workspace-and-gitS08-workspace-and-git: Workspace and Git Round TripHow code gets into the guest and how the agent's work gets back out, without sharing the host filesystem.) |
ended, as a failed start | lost | running when claude starts. ended, as a failed start, on a refusal, a failure or the deadline |
| running | claude runs, and wb relays its terminal or its streams (S05-cliS05-cli: Command Line InterfaceSpecify wb, the single user-facing command.) |
paused | lost | ending when claude exits or the client goes |
| paused | the link is down, and the guest’s project processes are stopped (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.) | stays paused | lost | running or ending when the link returns. lost at the pause limit |
| ending | claude has exited or was killed, and wb-guestd commits WIP and pushes (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., “Session end”) |
stays ending, and finishes again when the link returns | lost | ended when done, or as a failure at the deadline |
| lost | the session’s VM, its wb-guestd, or wb-hostd stopped under it |
ended by recovery, wb discard, wb project rm, or the deadline |
||
| ended | the exit status and the summary are in wb sessions, and the returned work stays until wb land or wb discard |
wb-hostd logs these in the audit log, with the rule that made each
one (SEC10-auditSEC10-audit: AuditEvery egress decision, approval, and returned change is recorded on the host in a persisted log the guest cannot alter.):
- every transition to paused, lost and ended;
- each answer it gives
wb-guestd(S06-11-reconnect-and-recoveryS06-11-reconnect-and-recovery: Reconnect, Loss and RecoveryWhat happens when the host-guest link drops, when a session is lost, and how a lost session's work is recovered or discarded.).
Opening a session
Section titled “Opening a session”wb-hostd refuses an open out of its form with session-open-form:
an agent other than claude, over 4096 arguments or 1 MiB of them, or
a NUL byte. It refuses a flag not built yet with
session-flag-unavailable, naming when it comes. Admission checks
processes left (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.) first. The cap of 64
sessions and shells per VM comes next, then the isolated VM’s one
project. The VM’s state, the mount, and the unlock come last. Once the
record exists, a refusal ends the session as a failed start. wb project place is refused with project-place-has-session, naming the
live session.
As the project user
Section titled “As the project user”Every operation under a project user’s home runs in a child process as that user, with the session’s environment allowlist (S09-policy-credentials-auditS09-policy-credentials-audit: Policy, Credentials, Certificates and AuditWhere policy comes from, how secrets are held, how TLS inspection is trusted, and what is recorded.). These operations are:
- creating and removing the worktree;
- the carry-in;
- the history link of S06-08-projects-and-sessionsS06-08-projects-and-sessions: Projects and Sessions Inside a VMHow projects map onto guest users and sessions onto worktrees, and how Claude Code runs in the guest.;
- the removal of a stale
index.lockthat a killed WIP commit left. It goes by that fixed file name, never by a pattern; - the WIP commit and the push.
wb-guestd, running as root, never opens a path under a project
user’s home. So a project user’s .git/config or hooks never run as
root.
Session helper
Section titled “Session helper”wb-guestd starts each session through a small helper that runs as
the project user. The helper:
- starts a new POSIX session (
setsid); - in interactive use, makes the PTY its controlling terminal;
- starts
claudein a process group of its own, as the terminal’s foreground group, as a shell does for a job; - waits with
WUNTRACED, so it seesclaudestop; - reports that, and
claude’s exit status, towb-guestd.
It ignores the SIGHUP it gets when the PTY’s other end closes. So it
can still report claude’s exit after a hangup.
wb shell uses the same helper, with a login zsh (zsh -l) in
place of claude.
The helper’s form
Section titled “The helper’s form”The helper is wb-guestd’s own executable, started with the one
argument session-helper. wb-guestd checks that argument before any
other start-up code, so a helper never updates, listens or stops
processes. It starts as the project user, with the user’s ID, primary
group, and no other group. It refuses to run as root. Its program
starts in the user’s home directory.
Before it starts the helper, wb-guestd checks the data mount
(S06-05-disksS06-05-disks: DisksA VM's system disk and data disk: how the data disk keeps what the guest flushes, and how the guest mounts it., “Before every session start”) and that the user is a project user
it knows, not root and not in admin or wheel
(SEC12-least-privilegeSEC12-least-privilege: Least privilege on the hostNo root or administrator rights at run time and no extra host user accounts.), and not locked.
The helper reports to wb-guestd, and takes its commands, in the
records of S06-12-guest-agentS06-12-guest-agent: Guest AgentWhat wb-guestd, the service in the guest, does for the host, and how it updates itself. (“Session helper records”).
The POSIX session
Section titled “The POSIX session”The session’s processes are the processes in the helper’s POSIX
session. wb-guestd records that POSIX session ID in a root-owned
directory on the data disk, outside every project user’s home. It uses
a recorded ID only for processes whose user ID is this session’s
project user. A process that leaves the POSIX session with setsid is
out of the session (S13-02-filesystem-profileS13-02-filesystem-profile: Layer 2: Seatbelt Filesystem Profile (v1)The Seatbelt profile each session's process tree runs under, where it comes from, its exceptions for Xcode builds, and its known gaps., “Known gaps”).
V1-M2-walking-skeletonV1-M2-walking-skeleton: Walking skeletonwb-vmd boots a fixture VM bundle in its confined process, wb-guestd answers over vsock, a data disk made on the host holds the homes, and wb shell opens a PTY as a project user, with its output through the terminal fi… keeps the ID in memory only. The record on the data disk comes
with the reports kept in the guest (below). Until then, a wb-guestd
that starts again stops the project users’ processes at start
(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., “When the link drops”), and knows no
session’s ID.
Closing the terminal ends the session
Section titled “Closing the terminal ends the session”wb-hostd has wb-guestd hang up the session when:
wbgetsSIGHUP;wb’s connection towb-hostdcloses for any reason, as whenwbwas killed or crashed.
wb-hostd detects this by the closed connection, never by a missed
heartbeat. So a wb stopped with job control keeps its session.
The hangup works like this:
- In interactive use,
wb-guestdcloses its end of the session’s PTY. The guest kernel sendsSIGHUP, as it does for a closed terminal. - Otherwise
wb-guestdclosesclaude’s standard input. - Either way,
wb-guestdsends the helper the hangup command, and the helper sendsSIGHUPtoclaude’s process group. - A stopped group also gets
SIGCONT, so the hangup is delivered.
When claude hasn’t exited 10 seconds later, wb-guestd kills the
session’s processes. It kills the project user’s other processes as
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. says.
Either way, the session goes on to ending, as on a normal exit.
wb claude --continue then starts a new session at the host’s HEAD.
It resumes the conversation (S06-08-projects-and-sessionsS06-08-projects-and-sessions: Projects and Sessions Inside a VMHow projects map onto guest users and sessions onto worktrees, and how Claude Code runs in the guest.,
“Conversation history”). The files the old session left uncommitted
are only in its WIP commit, on its session branch.
Ending
Section titled “Ending”The session ends when claude exits, or after a hangup. wb-guestd:
- commits and pushes WIP (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., “Session end”);
- reports
claude’s exit status: an exit code, or the signal that ended it.
The report 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
accepts an exit code from 0 to 255, or a signal number from 1 to 64.
Anything else is recorded as a failure with the rule. wb then exits
as for a Wraith Box failure (S05-02-exit-status-and-signalsS05-02-exit-status-and-signals: Exit Status and SignalsWhat wb claude exits with, and what it does with each signal.).
When wb is still connected, it prints the summary and exits.
Reports kept in the guest
Section titled “Reports kept in the guest”wb-guestd keeps each finished session’s report in its root-owned
directory, until wb-hostd acknowledges it. The report has the exit
status, and whether the WIP commit and push succeeded. wb-guestd
sends it again after a reconnect. So a report lost in a dropped link
still ends the session normally, not as lost.
wb-guestd deletes:
- a report, when it is acknowledged;
- a report or a recorded POSIX session ID, when it gets “end” for that session (S06-11-reconnect-and-recoveryS06-11-reconnect-and-recovery: Reconnect, Loss and RecoveryWhat happens when the host-guest link drops, when a session is lost, and how a lost session's work is recovered or discarded., “Reconnect”).
So the directory holds at most the 64 sessions of one VM.
The session’s client can go when no session of its project user is left. The report then also has a cleanup field: the result of the kill loop that followed (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.). That is clean, or the count of processes left, capped at 1001.
Returned work comes from the host
Section titled “Returned work comes from the host”The summary’s commit count and its ”(+ WIP)” mark come from
landing.git, not from the report.
- A report can claim a push that
wb-hostd’sreceive-packdidn’t accept for that session’s branch. It is recorded as a failed push.wbexits with 255, and the rule is logged. - A report of nothing to commit is taken as reported.
Changes left in the guest
Section titled “Changes left in the guest”A session’s changes stay in the guest worktree when it ends at a
deadline, or when its WIP commit or push failed. The summary and wb sessions then (NFR06-explained-refusalsNFR06-explained-refusals: OperabilityEvery refusal names the rule that caused it and how to change it.):
- give the worktree’s path in the guest;
- give
wb shell --project Pto reach it; - say that
wb discarddeletes it.
Deadlines
Section titled “Deadlines”Each state that waits on the guest has a deadline on the host, counted in host time awake:
- starting has 10 minutes from admission. That includes the toolchain reconcile (FR07-toolchain-manifestFR07-toolchain-manifest: Declared toolchainA project's toolchain is declared host-side (on macOS guests, a Brewfile) and reconciled automatically before a session starts.);
- ending and recovery have 7 minutes each. That holds the 10 seconds of the hangup, the WIP commit and the 5-minute push watchdog (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., “Bounds”).
Past its deadline, wb-hostd:
- has
wb-guestdend the session (S06-11-reconnect-and-recoveryS06-11-reconnect-and-recovery: Reconnect, Loss and RecoveryWhat happens when the host-guest link drops, when a session is lost, and how a lost session's work is recovered or discarded., “Reconnect”); - records it as ended with a Wraith Box failure.
wbexits with 255.
The worktree keeps its changes until wb discard. So a guest that
never reports can’t hold a session, or its VM, past the deadline
(SEC13-bounded-resourcesSEC13-bounded-resources: Bounded resourcesCPU, memory and disk of each VM are capped.).
A kill of a project user (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.) has 7 minutes. They count from the kill, or from the start of ending for a kill that follows a client that went. Past that deadline, without an accepted clean result, the kill counts as processes left (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., “Failing closed”).
Host sleep
Section titled “Host sleep”The VM sleeps with the host. On wake, wb-hostd updates the guest
clock (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., “Time and sleep”).
The session runs on as long as wb still has its terminal. When the
host-guest link drops across the sleep, the session pauses and resumes
as S06-11-reconnect-and-recoveryS06-11-reconnect-and-recovery: Reconnect, Loss and RecoveryWhat happens when the host-guest link drops, when a session is lost, and how a lost session's work is recovered or discarded. (S06-11-reconnect-and-recoveryS06-11-reconnect-and-recovery: Reconnect, Loss and RecoveryWhat happens when the host-guest link drops, when a session is lost, and how a lost session's work is recovered or discarded., “Reconnect”) says.
wb over SSH gets SIGHUP when the SSH connection drops, and that
ends the session.
Idle suspend
Section titled “Idle suspend”The idle period of 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., “Warm start”, runs
only while the VM has no session that is starting, running, paused or
ending, debug shells included. A lost or ended session doesn’t count.
An open wb vm admin connection stops the idle period too
(S15-05-admin-pathS15-05-admin-path: The Admin PathHow the person at the host's terminal gets a root shell in their own guest, on a path that no agent, session, or guest process can start or reach., “Marked”).
So a VM is never saved for idleness with such a session in it, and no
restore has a session to resume.
While a kill of a project user in the VM is pending, and after a
result of processes left, the idle period doesn’t run.
wb vm suspend is then refused with the reason
(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.).
wb vm suspend and wb vm stop are refused while the VM has a
starting, running, paused or ending session. The refusal names each
such session with (NFR06-explained-refusalsNFR06-explained-refusals: OperabilityEvery refusal names the rule that caused it and how to change it.):
- its state;
- the process ID of its
wb; - how it ends: close its terminal for a running or paused session, or wait for its deadline for a starting or ending one.
wb sessions lists them.
A terminal left open on a session keeps its VM running (NFR03-footprintNFR03-footprint: FootprintIdle VMs suspend after a configurable period.). It also holds the VM’s slot (NFR05-two-macos-vmsNFR05-two-macos-vms: Platform limitmacOS permits at most two concurrently running macOS guests per host.).
Parallel sessions
Section titled “Parallel sessions”Each session has its own ID, worktree, client, exit status, and summary (FR05-parallel-sessionsFR05-parallel-sessions: Parallel sessionsSeveral sessions can run at once, in the same project and across projects.). Ending one session doesn’t touch the others.
The one exception is the end of its project’s last session. Then all the project user’s processes are killed, and the user is locked (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.).
Decisions
Section titled “Decisions”No detach in V1
Section titled “No detach in V1”Closing the terminal ends the session, and V1V1-initial: Initial versionThe first version: what it delivers, what it leaves out, and the milestones that get there. has no wb attach.
- Decided: I46I46 (closed): S05-cli, S06-vm-lifecycle: session lifecycle, detach, sleep and failureS05-cli and S06-vm-lifecycle describe starting a session and its summary at the end, but not what happens in between, or when things go wrong: (B46-session-lifecycleB46-session-lifecycle: What happens to a running session when its terminal closes or its VM stops?Recommended that closing the terminal detaches an interactive session, which `wb attach` resumes, that a session ends only when claude exits, and that `wb` exits 255 when it loses a session.).
Idle suspend only without live sessions
Section titled “Idle suspend only without live sessions”- Rejected: saving a VM whose sessions have been quiet. That needs a restore on the next keystroke, and turns every restore into a pause.
Status: Draft