Skip to content

Retire provider bridges after their final thread ends - #2773

Merged
SawyerHood merged 1 commit into
mainfrom
bb/retire-idle-provider-bridges-thr_h4bt2ntqqv
Aug 31, 2026
Merged

SawyerHood merged 1 commit into
mainfrom
bb/retire-idle-provider-bridges-thr_h4bt2ntqqv

Conversation

@SawyerHood

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

The runtime retired only a superseded provider bridge after its final thread ended. It kept the current bridge active with no threads.

What changed

The runtime now stops each provider bridge after its final thread ends. It keeps a shared bridge active while another thread uses it. A later request starts a new bridge and restores the session. This change also removes the superseded-only retirement method. The stale bridge check remains for bridges that never owned a thread. This change does not alter the host daemon protocol.

How you verified

The lifecycle tests now cover final-thread stop, failed session creation, idle session release, ACP, Codex, and replacement bridge resume. These checks failed before the change because the current bridge stayed active.

  • pnpm exec turbo run test --filter=@bb/agent-runtime --force (316 tests passed)
  • pnpm exec turbo run typecheck --filter=@bb/agent-runtime
  • git diff --check

Fixes #2308

AGENT GENERATED

@SawyerHood
SawyerHood merged commit 59289a6 into main Aug 31, 2026
15 checks passed
@SawyerHood
SawyerHood deleted the bb/retire-idle-provider-bridges-thr_h4bt2ntqqv branch August 31, 2026 20:14
ymichael added a commit that referenced this pull request Sep 1, 2026
## Human comments

## What was wrong

PR #2773 correctly retires a provider bridge after its final thread
ends, but the shared process manager did not record that retirement
while it was in progress. A same-key `ensureProvider` could therefore
reuse the bridge being terminated; the new thread would reject
immediately or briefly start and then lose its provider when the old
bridge exited.

Review exposed two full-shutdown ordering cases: a retirement-blocked
start could spawn after the shutdown sweep, and waiting for that
replacement to finish initialization could exceed the daemon's 15-second
exit grace because provider RPC initialization waits up to 30 seconds.

## What changed

The provider process manager now memoizes same-key retirement promises.
New starts wait for retirement before selecting or spawning a bridge,
and concurrent shutdown callers share the active retirement. Full
runtime shutdown becomes terminal before any lifecycle wait:
retirement-blocked or later ensures fulfill without spawning, while
already-initializing starts treat shutdown as superseding their internal
RPC wait. Shutdown then terminates every process already visible to its
sweep.

Final-thread retirement, bridge reuse while another thread remains, and
later cold resume remain unchanged. The change is internal to
`@bb/agent-runtime`; no daemon wire contract, protocol version, public
API, CLI, or configuration changed.

## How you verified

- Added a regression that overlaps same-key provider shutdown and
ensure. It failed before the production change with `Provider "fake" is
not running` and passes afterward with a distinct surviving replacement
process.
- Added a regression that overlaps retirement, a same-key ensure with a
three-second delayed initialization response, and full shutdown. It was
red because shutdown took the full initialization delay; it is green
within the one-second bound, all calls fulfill, and no provider remains.
A post-shutdown ensure also fulfills without recreating a provider.
- `pnpm exec turbo run test typecheck --filter=@bb/agent-runtime
--force` — 22 files / 318 tests passed; typecheck passed.
- GitHub CI run `33459198901` — all non-skipped jobs passed, including
Linux AppImage lifecycle smoke.
- `git diff --check origin/main...HEAD` — passed.
- Rebased and reran validation on `origin/main` at
`fb05e93ff5ad4f58831e804b973f2de790557afa`.

Completes the lifecycle coordination missed by #2773.

Tagging @slopcop for re-review of head
`0ab3801d24d4d8c04e9cc1bff808357cb0f07d70`.

> AGENT GENERATED
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.

Idle provider bridge processes are never retired for non-Codex providers

1 participant