Skip to content

Agents die with the daemon: run bridge workers over a reconnectable transport so updates don't interrupt in-flight work #3143

Open

Description

@albrand

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() calls spawnPortablePipedProcess({ ..., detached: supportsProcessGroups() })
  • packages/process-utils/src/index.ts → spawnPortablePipedProcess() forces stdio: ["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:

  1. Reconnectable transport. Give the bridge worker a unix domain socket (named pipe on Windows) instead of inherited stdio. The worker listens; the daemon dials.
  2. Worker registry. Persist { processKey, pid, socketPath, providerId, bridgeProtocolVersion } under the data dir when a worker starts.
  3. Adopt on startup. On daemon start, dial each registered socket and adopt live workers instead of respawning. Reap entries that do not answer.
  4. 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

  1. added theissue type on Sep 5, 2026
  2. added
    host
    confirmed-repro
    on Sep 5, 2026
  3. bb-slop-cop commented on Sep 5, 2026

    @bb-slop-cop
    Contributor

    🚨 SLOP COP 🚨 · new-issue-autopilot

    • Classification: Bug · Priority High · Effort High · label host
    • Report: https://get-bb.github.io/reports/issues/3143.html
    • Verdict: REPRODUCED · confidence high · reproduction label confirmed-repro
    • Root cause: a successful protocol self-update requests daemon shutdown without gating on active threads, and shutdown explicitly terminates every provider process group.
    • No pull request: the durable fix requires a cross-subsystem transport, worker-lifetime, and adoption design, so it does not meet the simple-fix safety gate.
  4. 154 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-reprohost

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions