Skip to content

fix(acp): clean up agents before worker exit - #5374

Merged
SawyerHood merged 2 commits into
mainfrom
slopcop/issue-5371
Oct 13, 2026
Merged

SawyerHood merged 2 commits into
mainfrom
slopcop/issue-5371

Conversation

@bb-slop-cop

@bb-slop-cop bb-slop-cop Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

🚨 SLOP COP 🚨 · new-issue-autopilot

Human comments

What was wrong

The ACP worker exported no signal-cleanup hooks, so SIGTERM and SIGINT bypassed agent termination. That is the path the runtime itself uses to stop a provider worker: it sends SIGTERM and only kills the worker's own process group, and ACP agents run in their own groups, so they were left running with parent PID 1. The normal-close cleanup enumerated only registered thread sessions and missed model-discovery connections. Reproduced twice from clean trusted main checkouts: root-cause report.

What changed

  • Track every ACP connection (thread sessions, sessions still starting, model discovery) and remove it on exit; reject connection creation once shutdown begins.
  • Share idempotent, awaited cleanup across stdin close, SIGTERM and SIGINT. Release sessions and terminate all tracked agents (SIGTERM, then SIGKILL of the process group after 1 s) with all-settled failure reporting, then exit 0.
  • On stdin close, sessions are now released instead of stopped: an active turn is no longer sent session/cancel and given up to 4 s to settle first. This keeps shutdown inside the runtime's 5 s escalation window.
  • Add real-worker lifecycle cases covering registered sessions and discovery, termination escalation, and late-start rejection: six on macOS and Linux, two on Windows. Windows cannot deliver SIGTERM or SIGINT to a handler and its agents are not detached, so only the stdin cases run there.
  • Two files in the existing ACP subsystem: 247 additions + 11 deletions. No dependencies, generated artifacts, stored data or public protocol/schema changes. The branch is rebased onto current main so the bundled bridge ships under the unpublished plugin SDK version; no version bump of its own is needed.

Not covered here:

  • The opt-in rebuilt ACP adapter (packages/provider-bridge-acp-next) has the same gap and still needs this change.
  • A second SIGTERM while cleanup is running ends the worker at once, so agents that ignore SIGTERM can still be left behind in that case. The runtime sends one.
  • A worker that is killed with SIGKILL or crashes still leaves its agents running.

How you verified

  • Before the production change, the focused test fails in five cases with "agent remains alive after bridge exits" and passes its existing session/stdin control. After the fix, all six pass.
  • Each part of the fix is pinned by the test. Removing the shutting-down rejection fails the four signal cases ("shutdown admitted a new agent"); stopping only registered sessions fails the three discovery cases; exiting without awaiting cleanup fails all six; dropping the SIGINT hook fails the two SIGINT cases.
  • A separate probe drives the real worker the way the runtime stops it, with three sessions (one agent ignores SIGTERM, one has a child that ignores it), one session hung in initialize and one hung model discovery. Before: the worker dies in 15 ms and all six processes survive. After: the worker exits 0 in about 1 s and none survive.
  • pnpm exec turbo run test lint typecheck --continue --filter=@bb/provider-bridge-acp --filter=@bb/agent-runtime --filter=bb-plugin-provider-acp on this head: 16 of 16 tasks pass; 391 ACP tests pass with four skipped, 309 runtime tests and 68 ACP plugin tests pass.
  • node packages/plugin-sdk/scripts/check-npm-version-guard.mjs passes on this head.
  • Local runs are on Linux with an authored Node agent, not the proprietary provider on macOS. Windows is covered by CI only.

Fixes #5371

AGENT GENERATED

SawyerHood and others added 2 commits October 13, 2026 22:41
Windows cannot deliver SIGTERM or SIGINT to a handler, so kill ends the
worker outright and those cases can never observe a clean exit. Agents
there are not detached and end with the worker. The stdin cases still
run on every platform.

Co-Authored-By: Claude Code <[email protected]>
@SawyerHood
SawyerHood merged commit 995fe31 into main Oct 13, 2026
85 of 87 checks passed
@SawyerHood
SawyerHood deleted the slopcop/issue-5371 branch October 13, 2026 22:56
SawyerHood added a commit that referenced this pull request Oct 20, 2026
## Human comments

## What was wrong

bb shipped two ACP adapters side by side: the original
(`packages/provider-bridge-acp`, plugin `provider-acp`, on by default)
and the rebuilt one from #5266 (`packages/provider-bridge-acp-next`,
plugin `bb--provider-acp-next`, opt-in and mutually exclusive through
`BUNDLED_PLUGIN_REPLACEMENTS`). Fixes had to be made twice, and some
weren't: #5374's shutdown cleanup only reached the original bridge.
Deleting the original plugin directly would have caused data loss. The
orphaned-builtin cleanup (`removeUnbundledBuiltins`) deletes a removed
built-in's settings, so every default user's `customAgents` would have
been wiped. Users who had opted in would have been left with
`provider-acp` disabled and no ACP providers.

## What changed

- **In-place swap.** The rebuilt bridge and plugin now live at
`packages/provider-bridge-acp` and `plugins/provider-acp`, under the
original package names and the `provider-acp` plugin id. Default users'
plugin rows, `customAgents`, and threads carry over unchanged, since
provider ids are the same (`acp-cursor`, …). The `-next` directories are
deleted. So are the opt-in switch (`BUNDLED_PLUGIN_REPLACEMENTS`, the
`standsAside` catalog filter and the paired enable/disable in
`setEnabled`) and their tests. Hardcoded `bb--provider-acp-next` ids in
the plugin (`known-agents.ts`, `registry-icons.ts`, `usage-source.ts`)
are now `provider-acp`. Registry icon refs resolve as
`provider-acp/registry-*`.
- **Handover for opted-in users.** `RETIRED_BUNDLED_PLUGINS` in
`builtin-registry.ts` declares `bb--provider-acp-next` → `provider-acp`.
At startup, `handOverRetiredBuiltins()` runs before
`removeUnbundledBuiltins()`. If the retired row was enabled, it copies
`customAgents` to `provider-acp` and turns `provider-acp` on. Then it
deletes the retired row's settings, secrets and schedules. It does not
clear the user's disabled-provider list, which normal orphan cleanup
would, because both plugins own the same provider ids.
- **Plugin SDK.** `@get-bb/plugin-sdk/provider-bridge/acp` now exports
the rebuilt bridge. The export names and type shapes are the same as
before, plus `experimental_registerAcpDialect`.
`provider-bridge/acp-next` stays as a deprecated alias re-exporting
`/acp`. `docs/api_to_audit.md` merges the two entries. No wire change;
`HostDaemonAcpLaunchSpec` is identical.
- **#5374 ported to the rebuilt bridge.** Every agent connection (thread
sessions, model discovery, session-option discovery) is tracked and
removed on exit. New connections are refused once shutdown starts. stdin
close, SIGTERM and SIGINT share one awaited cleanup that releases
sessions and kills tracked agents. Includes `shutdown.test.ts` from
#5374.
- **References.** Updated `bundled-plugins`, `bb-official.json`,
`first-party-plugins.json`, `forkable-plugins.json`, the web
bundled-marketplace list, `turbo.json` (the -next task config moved to
`bb-plugin-provider-acp`), the provider-literal ratchet, plugin-build
tests, `docs/configuration.md`, and the bb-guide plugin-authoring
references.

## How you verified

- `pnpm exec turbo run typecheck lint` for `@bb/provider-bridge-acp`,
`bb-plugin-provider-acp`, `@get-bb/plugin-sdk`, `@bb/server`, `@bb/web`,
`@bb/domain`, and `@bb/plugin-build`. All passed.
- `turbo run test` passed for the bridge (505 tests, including the 6
ported shutdown cases), the SDK (376), plugin-build (211) and domain
(248).
- The plugin package had 81 of 82 passing. The one failure is
`native-roots.test.ts` omp, an environment-only failure because
`/tmp/.agents` exists on the machine (per `.bb/skills/test-audit`). It
passes with `TMPDIR=/var/tmp/...`.
- Server: `vitest run` over `test/services/plugins`,
`test/services/plugin-catalog`, `test/system/provider-management`,
`test/system/execution-options`,
`test/public/public-provider-installations` and
`test/providers/provider-registry`: 65 files and 765 tests passed. That
includes the new `retired-bundled-plugins.test.ts`, which covers three
cases: an enabled retired plugin hands off settings and turns its
successor on; a disabled one leaves the successor alone and keeps
disabled providers; and a missing successor.
- `packages/agent-runtime` `runtime.acp-topology.test.ts` passed.
- `node scripts/check-plugin-forks.mjs plugins/provider-acp` passed
(with a clean TMPDIR). The provider-literal ratchet passed. The plugin
SDK npm version guard passed (0.6.56 is unpublished, so no bump).
- Not run locally: the full server suite, tarball smoke, Windows, and a
live Cursor/opencode thread on the merged build.

Follow-up: #5692 and #5055 target only the removed original bridge and
need re-targeting; #5694 touches both.

> AGENT GENERATED

---------

Co-authored-by: Sawyer <[email protected]>
SawyerHood added a commit that referenced this pull request Oct 20, 2026
## Human comments

## What was wrong

When bb shut a provider worker down (stdin close, SIGTERM or SIGINT),
the ACP bridge released every session and killed every agent at once.
ACP agents run their tool commands in process groups of their own, so a
command running at that moment outlived bb, re-parented to init. A
normal `thread stop` doesn't leave them, because it sends
`session/cancel` first and the agent ends its own commands. #5374 added
the shutdown hooks but deliberately skipped the cancel, to stay inside
the runtime's 5 s escalation window.

Found while live-testing #5775: Ctrl-C of bb mid-turn left Cursor's and
OpenCode's `sleep 120` running under systemd.

## What changed

- `packages/provider-bridge-acp/src/bridge/bridge.ts`: shutdown stops
each session through `stopSession` with a 1.5 s cancel window
(`SHUTDOWN_CANCEL_TIMEOUT_MS`). All sessions are stopped in parallel, so
total shutdown stays well inside 5 s. Connections owned by no session
(model/option discovery, sessions still starting) are killed straight
away, and anything left is killed afterwards. Previously one
`allSettled` killed every connection in parallel with releasing
sessions, so the agent died before it could handle a cancel.
- `shutdown.test.ts`: new case per shutdown path. A fake agent starts a
detached child on `session/prompt` and kills it on `session/cancel`; the
test asserts the child is gone once the bridge exits.

## How you verified

- The three new cases (SIGTERM, SIGINT, stdin) fail before the change
with "the agent's command outlived the bridge". They pass after it, as
do the six existing reap cases.
- `pnpm exec turbo run test lint typecheck
--filter=@bb/provider-bridge-acp`: 508 passed, 4 skipped.
- Live, on an isolated `start:worktree` build with real cursor-agent
2026.08.11 and opencode 1.18.35: both threads ran `sleep 120`, then bb
got Ctrl-C (SIGINT to its process group). Before the fix, both sleeps
survived. After it, ports are free and no `sleep`, `cursor-agent acp` or
`opencode acp` process is left.

> AGENT GENERATED

Co-authored-by: Sawyer <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ACP bridge leaves cursor-agent acp processes orphaned (PPID 1) when terminated by SIGTERM/SIGINT

1 participant