Repository navigation
Description
Problem
A daemon restart kills every agent that daemon owns. Because protocol bumps force a daemon self-update, and a self-update ends in a restart, routinely updating bb terminates in-flight agent work.
Observed on one host between Aug 31 and Sep 5: daemon protocol went 135 → 170 → 176 → 180. Each bump is a forced update + restart. In our case a restart also terminated the hermes acp child processes under the daemon's cgroup, and any turn in flight died with them.
The daemon clearly already wants to avoid disruption — it logs:
Daemon self-update is backing off; keeping the current daemon running.
but the only alternatives it has today are "restart and interrupt work" or "stay on an incompatible protocol". Staying incompatible means the host shows disconnected while being perfectly healthy, and every turn.submit fails. Ours sat that way for hours.
This gets worse as protocol churn continues: the more often bb ships, the more often running agents are killed.
Why it happens
Bridge workers are already in their own process group, but their only transport is inherited stdio pipes, so they cannot outlive the parent:
packages/agent-runtime/src/runtime-provider-process.ts→spawnProvider()callsspawnPortablePipedProcess({ ..., detached: supportsProcessGroups() })packages/process-utils/src/index.ts→spawnPortablePipedProcess()forcesstdio: ["pipe", "pipe", "pipe"]
detached here is not for survival — it exists so killProcessGroup() can process.kill(-pid, signal) and take the whole group down. The pipes are owned by the daemon, so when the daemon exits the transport dies with it and the worker is unreachable even if the process lingers.
So agent isolation is blocked by the transport, not by supervision.
Proposal
Let agents outlive the daemon and the IDE:
- Reconnectable transport. Give the bridge worker a unix domain socket (named pipe on Windows) instead of inherited stdio. The worker listens; the daemon dials.
- Worker registry. Persist
{ processKey, pid, socketPath, providerId, bridgeProtocolVersion }under the data dir when a worker starts. - Adopt on startup. On daemon start, dial each registered socket and adopt live workers instead of respawning. Reap entries that do not answer.
- Version-gated adoption. A worker advertises its bridge protocol version; the daemon adopts only compatible workers and drains/replaces the rest, so this stays safe across the same protocol bumps that trigger updates.
With that, a daemon restart — including a self-update — becomes transparent to a running agent, and the same mechanism covers IDE restarts and crash recovery.
Happy to split this into reviewable pieces (transport first behind a flagless default of the current behaviour, then registry, then adoption) if that suits the review process better.
Interim patch: don't restart while turns are in flight
The architecture above is a real change. In the meantime, the daemon already knows its active threads (getActiveThreads, wired in apps/host-daemon/src/app.ts), so it can simply avoid restarting into running work. This does not isolate agents, but it removes the common case where an update interrupts them.
The deadline is deliberate: a stuck turn must not be able to pin the daemon on an incompatible protocol forever, which would reproduce the disconnected-host failure above.
diff --git a/apps/host-daemon/src/server-connection.ts b/apps/host-daemon/src/server-connection.ts
index f1adf9a..aa22d8f 100644
--- a/apps/host-daemon/src/server-connection.ts
+++ b/apps/host-daemon/src/server-connection.ts
@@ -33,6 +33,11 @@ export type {
ServerConnectionOptions,
} from "./server-connection-support.js";
+/** How long a protocol self-update waits for in-flight agent turns to finish. */
+export const SELF_UPDATE_DRAIN_TIMEOUT_MS = 10 * 60 * 1000;
+/** How often the drain gate re-checks for active agent turns. */
+export const SELF_UPDATE_DRAIN_POLL_INTERVAL_MS = 2_000;
+
interface InvalidServerMessageArgs {
data: unknown;
error: Error;
@@ -362,6 +367,7 @@ export class ServerConnection {
force: error.protocolUpdateRetryRequested,
});
if (result === "updated") {
+ await this.waitForActiveTurnsToDrain();
await this.options.onSelfUpdateInstalled?.();
}
}
@@ -377,6 +383,42 @@ export class ServerConnection {
}
}
+ /**
+ * A protocol self-update restarts the daemon, and restarting kills every
+ * provider bridge worker it owns, interrupting any agent turn in flight.
+ * Wait for turns to finish first; past the deadline, restart anyway so a
+ * stuck turn cannot pin the daemon on an incompatible protocol forever.
+ */
+ private async waitForActiveTurnsToDrain(): Promise<void> {
+ if (this.options.getActiveThreads === undefined) return;
+ const deadline = Date.now() + SELF_UPDATE_DRAIN_TIMEOUT_MS;
+ let announced = false;
+ for (;;) {
+ const activeThreads = await this.options.getActiveThreads();
+ if (activeThreads.length === 0) return;
+ if (Date.now() >= deadline) {
+ this.options.logger.warn(
+ {
+ activeThreadCount: activeThreads.length,
+ drainTimeoutMs: SELF_UPDATE_DRAIN_TIMEOUT_MS,
+ },
+ "Restarting for a protocol self-update while agent turns are still active; those turns will be interrupted.",
+ );
+ return;
+ }
+ if (!announced) {
+ announced = true;
+ this.options.logger.info(
+ { activeThreadCount: activeThreads.length },
+ "Protocol self-update installed; waiting for active agent turns to finish before restarting the daemon.",
+ );
+ }
+ await new Promise<void>((resolve) => {
+ setTimeout(resolve, SELF_UPDATE_DRAIN_POLL_INTERVAL_MS);
+ });
+ }
+ }
+
private logFatalConnectError(error: ServerResponseError): void {
const base = {
hostId: this.options.hostId,Notes on the patch: it applies to main and passes a syntax check, but I have not run the repo's build or test suite, so treat it as a starting point rather than a finished PR. Two follow-ups a maintainer may prefer to decide: whether SELF_UPDATE_DRAIN_TIMEOUT_MS should be configurable, and whether force: true (i.e. protocolUpdateRetryRequested) should skip the drain and restart immediately.
Activity
bb-slop-cop commentedon Sep 5, 2026
🚨 SLOP COP 🚨 ·
new-issue-autopilothostconfirmed-repropatch: bridge workers talk to the daemon over a socket that outlives …
patch: a registry of running bridge workers (get-bb#3143 phase 2)
patch: a daemon exit detaches from bridge workers instead of killing …
patch: bridge workers number their output and replay what the daemon …
patch: acknowledge a worker line only once its events are stored, and…
patch: acknowledge a worker line only once its events are stored, and…
patch: a restarted daemon adopts running bridge workers (get-bb#3143 …
patch: the server keeps adopted threads running across a daemon resta…
patch: quitting bb lets the host daemon detach before the server stops (
patch: note why replay keys need no protocol bump (get-bb#3143)
patch: continue exactly the provider processes the worker stopped, an…
154 remaining items
patch: two registry entries for one provider process keep only the ne…
patch: a registry format version, and an unparseable entry is still r…
patch: a reconnecting daemon resumes from what the server accepted, n…
patch: a worker that never opened its socket is asked to stop before …
patch: an answer lost with its socket leaves the request replayable (g…
patch: an adopted thread whose turn has not arrived is interrupted af…
patch: a worker stopped for never listening still reports why, and a …
patch: an adopted environment runtime is not evicted while its provid…
patch: an adopted runtime is held from eviction for a flat window (ge…
patch: the detach notice and the worker release come before the unbou…
patch: a kept frame on disk no longer sends every later frame to disk (…
patch: an answer counts as delivered only once the worker is seen rea…
patch: a daemon whose socket keeps being taken backs off before redia…
docs: the fork's deliberate behaviour when agents outlive the daemon (g…
patch: spend is recorded where the pruner cannot reach it