Skip to content

Deduplicate agent-runtime provider adapters and bridges - #1374

Merged
ymichael merged 8 commits into
mainfrom
bb/agent-runtime-adapter-dedup
Aug 12, 2026
Merged

ymichael merged 8 commits into
mainfrom
bb/agent-runtime-adapter-dedup

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

Motivation

The four provider adapters (claude-code, codex, pi, acp) and three bridges carry near-verbatim copies of the same machinery: stdio harness, session registry, adapter identity/command/translation boilerplate, turn lifecycle, item-id scoping, and tool item translation. That duplication has a measurable history of making cross-provider work slow and lossy:

  • Manual compaction (Add manual context compaction across providers #1152) landed through 19 commits, more than 11 of them iterating the same feature provider-by-provider ("Make OpenCode ACP compaction reliable", "Avoid false Pi compaction success", "Show ACP compaction lifecycle", "Fix compaction lifecycle races", ...). One feature, re-fought per adapter.
  • Rate-limit normalization (Handle provider subscription rate-limit retries #1027) only ever shipped to 2 of 4 providers. On main today, ProviderRateLimitState is referenced by claude-code and codex translation only — Pi and ACP have zero references. Duplication didn't just slow the change down; it silently dropped half the matrix.
  • classifyExecutionSettingsChange (Apply Claude turn settings without replacing live sessions #1236) had to touch all four adapters to change one provider's behavior, because the member wiring is copied everywhere.

After this PR, the shared machinery lives in packages/agent-runtime/src/shared/ and a cross-provider change of this kind is a one-place change.

Honest accounting

Repo-wide this is roughly −300 lines net (2,204 insertions, 2,512 deletions, all inside packages/agent-runtime). The ~1,640-line figure from the de-slop audit counts duplicated lines, not deletable ones — extracting a shared implementation still costs its one copy plus parameterization. The value of this PR is one-place-change for cross-provider behavior, not raw deletion.

Commit-by-commit review guide

The 8 commits are logically staged; each is independently revertable and behavior-preserving:

  1. fff1c494c share bridge stdio harness — the three bridges' identical stdin/stdout JSON-RPC loop (readline wiring, dispatch, error replies) moves to shared/bridge-harness.ts.
  2. e1e7615ba share bridge session registry — the per-bridge Map-of-sessions bookkeeping (create/get/stop/dispose) moves to shared/bridge-session-registry.ts.
  3. 66fee1a30 share bridge environment helpers — env-override construction/parsing dedup in shared/adapter-utils.ts (round-trip covered by a new test), plus shared/mime-types.ts.
  4. 6e6e9fbc1 share standard adapter members — the biggest one. Each adapter's copied identity/launch members, initialize plan, unsupported-command fallbacks, accepted-command translation, model-list parsing, and tool-call decoding collapse into createStandardAdapterMembers() (shared/standard-adapter-members.ts); adapters keep only their provider-specific buildProviderCommandPlan. Review focus: confirm each adapter's per-command plans are unchanged (they are moved, not edited).
  5. 4c6a3c65b centralize provider turn hooks — per-adapter ensureXTurnStarted wrappers and the copied error-envelope-to-events helper become onTurnStart/onTurnFinish hooks and buildErrorEvents on the shared turn-state registry; shared/provider-error-events.ts is deleted.
  6. 276516e32 share scoped item id factories — the copied open/completed scoped-item-id resolution moves to shared/scoped-item-ids.ts.
  7. fbd671c2a share tool item translation — the copied toolUse/toolResult item builders move to shared/tool-item-translation.ts.
  8. 86a313d7b remove dead exports and helpers — deletes exports and helpers made unreferenced by the extractions (verified by repo-wide typecheck).

Wire compatibility (server ↔ host daemon)

No HOST_DAEMON_PROTOCOL_VERSION bump needed, and none is included. The diff is confined to packages/agent-runtime; packages/host-daemon-contract (session payloads, WS messages, host RPC commands/results) is untouched and the protocol version stays at 106 on both sides. The refactor changes where adapter/bridge code lives, not what crosses the wire: the 900+ adapter tests that assert exact translated ThreadEvent output (ids, scopes, payloads) pass without any expectation changes, so the event stream the daemon posts to the server is byte-identical.

Rebase provenance

This branch originated in archived thread thr_jyia7bkdqn and was rebased onto current main (original authorship preserved in the commits). The rebase crossed #1280 (sent-message editing) and #1321 (provider-unhandled-event fix), both of which touched the same adapters; conflicts were resolved by preserving main's shipped behavior and re-grafting it into the extracted structure:

  • thread/discard command plans re-added per provider (acp: noop, claude-code: thread/stop, pi: thread/discard) inside each buildProviderCommandPlan.
  • Claude Code's latestProviderCheckpointId reset moved into the shared onTurnStart hook (fires only for newly opened turns, same guard as the removed wrapper).
  • sourceProviderCheckpointId forwarding re-added to the claude-code and pi thread/fork plans.

The rebase delta vs the original branch inside packages/agent-runtime is exactly the sum of #1280 + #1321 (+1,124/−36), i.e. nothing from main was lost and nothing beyond the refactor was introduced.

Verification

  • pnpm exec turbo run typecheck — whole repo, 58/58 tasks pass.
  • pnpm exec turbo run test --filter=@bb/agent-runtime — 907 passed, 1 failed: runtime.process-lifecycle.test.ts > bounds provider stderr while data arrives without a newline, which fails identically on unmodified origin/main (reproduced during this work; also documented as pre-existing in fix(host-daemon,db): stop one undeliverable event from wedging every thread #1321). The lifecycle suites (runtime.lifecycle.test.ts 28/28, runtime.process-lifecycle.test.ts 21/21 excluding the pre-existing failure) covering the Codex recovery paths pass.
  • pnpm exec turbo run test --filter=@bb/host-daemon (the only dependent of @bb/agent-runtime) — 541/541 pass.

AGENT GENERATED: by Claude Fable 5

🤖 Generated with Claude Code

@SawyerHood

Copy link
Copy Markdown
Collaborator

🚨 SLOP COP 🚨 · review

I am the Slop Cop. I am now reviewing this pull request.

I will check security, code quality, performance, architecture, and practical test coverage.

@SawyerHood SawyerHood left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 SLOP COP 🚨 · review

ELI5: This PR puts repeated provider code into shared helpers. Future provider changes should need fewer edits and have fewer chances for drift.

I found no blocking issue.

The security review found no new command injection, permission bypass, cross-session response use, or secret transfer path.

The quality review found that the refactor preserves the reviewed provider behavior. It found no stale helper names or duplicate adapter implementations.

The performance review found no regression. The shared tool-response path removes one repeated session scan.

One small follow-up remains. ToolCallForwarder still has matching definitions in the shared registry, Claude proxy, and Pi proxy. A later cleanup can export one shared result contract.

I checked the full 31-file diff and relevant surrounding code. I also searched the repository for stale names and duplicate implementations.

The @bb/agent-runtime type check passed. The local package run passed 907 of 908 tests.

The unchanged stderr-tail test failed twice. This pull request does not change that test or its process lifecycle code.

GitHub CI passed all package, server, app, integration, and package-smoke checks.

I did not start the app or use a browser. This change has no user interface route, so the bridge and integration suites provide the relevant product coverage.

@ymichael
ymichael merged commit 8482401 into main Aug 12, 2026
10 checks passed
@ymichael
ymichael deleted the bb/agent-runtime-adapter-dedup branch August 12, 2026 16:32
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.

2 participants