Contain harness executions after an abrupt bridge restart
Problem
The supervisor restarts the agent bridge after it exits (Supervisor._handle_bridge_exit), and its stop path (AgentBridgeProcess.stop) signals only the bridge's own PID. The vendor processes a bridge starts are reaped only by that bridge's own cleanup (harness.close() in AgentBridge.run's finally). When a bridge dies without running it (an OOM kill, SIGKILL, a hard crash, a close() that fails and is only logged), nothing stops its children:
- Claude: the CLI subprocess the Agent SDK transport spawns for
ClaudeHarness. - Codex: the app-server, which runs under the subreaper in
harness/codex/process.py. The subreaper freezes and reaps its tree onSIGTERM/SIGINTor when its child exits, but nothing signals it when its parent dies (no parent-death signal). Native exec tools can callsetsid(), so the process group is not an ownership domain either. - OpenCode: the server outlives the bridge by design. On
main,OpencodeHarness.resume_sessionadopts the persisted session (session_exists) without stopping what the server may still be running for the previous bridge.
A restarted Claude or Codex bridge starts a fresh vendor child and knows nothing of the old one; a restarted OpenCode bridge reattaches to a server that may still be running the interrupted turn. Either way the old turn may still be editing the worktree, and the restarted bridge cannot prove otherwise.
Current state on main
#2462 (closed without merging) proposed reconciling a processing prompt both when the sandbox is replaced and when the bridge restarts. Its bridge-restart half is not on main: the ready.activeMessageId report, the contains_inherited_execution harness declaration, and OpenCode's _stop_inherited_execution in resume_session.
- Sandbox replaced (fresh spawn or snapshot restore): handled by #2615. A spawn or restore reserves a new provider instance, revokes the old generation's credentials and socket authority, and retires the prior provider object, so the next prompt never shares a worktree with the old instance. #2615 fails the prompt the old instance was running when the reservation is published, and the queue continues on the replacement. This case is out of scope here.
- Bridge restarted inside a live sandbox (every harness, including OpenCode): still open. After the restarted bridge reconnects and reports
ready, the control plane keeps the promptprocessinguntil the execution timeout (DEFAULT_SANDBOX_TIMEOUT_SECONDS) or a Stop. The bridge heartbeat keeps renewing the sandbox's activity for it in the meantime, so the inactivity stop does not fire, and the prompts queued behind it wait. Releasing the queue is safe only once the restarted bridge can prove its predecessor's turn is contained, and no harness can do that yet.
What a fix needs
Containment first, for each harness, using one of:
- Supervisor-owned containment. The supervisor owns the process domain of each harness's vendor process and terminates the whole domain, with confirmation, whenever the bridge exits and before it starts a new one. Since tools can
setsid(), a process group is not enough: it needs subreaper- or cgroup-style ownership. The Codex subreaper is a starting point if it is launched outside the bridge, or given a parent-death signal plus a way for the next bridge to confirm the tree is gone. - Harness-level containment before
ready. On attach, the harness finds and stops whatever a predecessor's vendor process still runs (for example through a handle the previous bridge persisted), within a bounded budget, and raisesHarnessStartErrorwhen it cannot confirm containment. For OpenCode this means stopping what the server still runs for an earlier bridge and waiting for its idle confirmation beforeready; #2462'sOpencodeHarness._stop_inherited_executionis a starting point.
Then reconciliation: a contained bridge's ready reports the prompt it still owes a result for (as #2462's activeMessageId did), and the control plane fails a processing prompt that was dispatched before the current socket was accepted and that the bridge does not hold, then wakes the queue. Only harnesses that declare containment should send the report; a bridge that sends none keeps today's behaviour. Tests should show that a restarted bridge has contained its predecessor's execution before it reports ready.