Repository navigation
Fix Codex archive undo synchronization - #3025
Merged
Merged
Conversation
ymichael
added a commit
that referenced
this pull request
Sep 8, 2026
## Human comments ## What was wrong The recurring [packages job failure](https://github.com/get-bb/bb/actions/runs/34275422905/job/102227127822) was triggered by package-shard CPU contention but rooted in a lifecycle mismatch in the test. The runtime started its 200 ms `thread/start` request deadline after provider initialization but before the bridge necessarily handled the request and spawned the app-server child. The test then required `spawned === 1 && exited === 1`; when cancellation retired the bridge before its request handler ran, no child could ever publish either marker. An instrumented 24-run stress reproduced 22 exact failures, all with `spawned=0`, `exited=0`, no child PID, and an expected bridge exit. The predicate-before-deadline polling fix from #2352 remains present; this was an impossible lifecycle state rather than a missed final poll. The audit of closed PR #2506 also confirmed that its adjacent child-stdin finding remains real on current `main`: a live app-server child that closes fd 0 produced an unhandled `EPIPE` and left its request pending. Current `main` has since made `kill()` awaitable in #3025, so the old patch could not be reused unchanged. The package shard's nested concurrency is systemic: CI runs four Turbo package tasks on a 4-vCPU runner, and each package's Vitest process independently schedules worker pools. That explains the reproduction condition, but it is not the root cause addressed here. The independently verified merge base is `06047280e04e6ce57554052ec788a1c5defe7d1e`. ## What changed - Pre-start the shared Codex bridge outside the construction deadline scenario, stall the fake `thread/start`, hold only the runtime's request clock until the real child publishes its spawn marker, then advance the unchanged 200 ms deadline. The test still requires the child to record its SIGTERM exit and verifies its PID is gone. - Publish the fake child's spawn marker only after its SIGTERM handler is installed, and replace the elapsed 800 ms response delay with a construction that remains pending until release. - Treat child-stdin `EPIPE` and `ERR_STREAM_DESTROYED` as terminal connection failures. New work is rejected immediately, the unusable child is force-stopped, and pending work still finalizes on the observable child exit/close boundary so natural exit status, stderr, and final stdout are preserved. The current awaitable `kill()` contract remains intact. - Add real-child regressions for post-exit stdout draining, EPIPE immediately before a natural exit, and containment of a live child with closed stdin. No request, polling, release, or test timeout was increased. There is no server/daemon wire change, so `HOST_DAEMON_PROTOCOL_VERSION` is unchanged. There are no CLI, SDK, guide, or configuration-surface changes. ## How you verified - Original red-capable exact stress on the enrolled 8-logical-CPU Intel host, 64 runs at 32-way parallelism: - Before: 5/64 passed; 59/64 failed with the exact `Timed out after 10000ms waiting for the late-constructed child was released` signature. - Instrumented classification: 22/24 exact failures; every failure recorded `spawned=0`, `exited=0`, no child PID, and `bridgeExits=[{expected:true}]`. - Final: 64/64 passed with no timeout, EPIPE, or survivor; test-body p50/p95/max were 21.48 s / 24.16 s / 24.51 s under deliberate 4x oversubscription. - Closed-stdin regression before the connection change: failed deterministically because the request remained pending past 500 ms and Vitest captured an unhandled `write EPIPE`. After: the three-test connection file passed, then passed 32/32 parallel stress runs with no unhandled error or survivor. - `pnpm exec turbo run test --filter=@bb/agent-runtime -- --run src/runtime.codex-topology.test.ts` — 1 file / 4 tests passed. - `pnpm exec turbo run test --filter=bb-plugin-provider-codex -- --run src/bridge/app-server-connection.test.ts` — 1 file / 3 tests passed. - `pnpm exec turbo run test --filter=@bb/agent-runtime --force` — 22 files / 318 tests passed. - `pnpm exec turbo run test --filter=bb-plugin-provider-codex --force` — 27 files / 264 tests passed. - A combined two-package test run made nested contention visible: the Codex provider passed, while agent-runtime hit three unrelated existing failures in ACP topology and process-lifecycle tests. Both package suites passed independently, and the changed focused tests remained green. - `pnpm exec turbo run typecheck --filter=@bb/agent-runtime --filter=bb-plugin-provider-codex --force` — passed. - `pnpm exec turbo run build --filter=@bb/agent-runtime --filter=bb-plugin-provider-codex --force` — passed (the scoped packages have no direct build tasks; their applicable dependency graph passed). - `pnpm exec prettier --check packages/agent-runtime/src/runtime.codex-topology.test.ts plugins/provider-codex/src/bridge/app-server-connection.ts plugins/provider-codex/src/bridge/app-server-connection.test.ts plugins/provider-codex/src/bridge/fake-codex-app-server.mjs` — passed. - Every reproduction, stress, test, typecheck, and build command ran inside a bounded process group on `host_nwqfteeqz4`; cleanup checks reported no surviving group or matching test/fake-child process. Fixes: no issue — exact path/signature searches found no open issue or PR, and the prior BB owners are archived without environments. > AGENT GENERATED
Willhong
added a commit
to Willhong/bb
that referenced
this pull request
Sep 11, 2026
…-serving children Since get-bb#3025 the bridge waits for a maintenance child to exit before it settles a maintenance request, so a fake that delays every child on SIGTERM let an in-flight rebuild finish before a concurrent discard could mark the replacement closing. A real maintenance child holds no rollout writer and exits promptly; apply the scripted delay only to children that started, resumed, or forked a thread.
This was referenced Sep 11, 2026
ymichael
added a commit
that referenced
this pull request
Sep 21, 2026
…ff (#3460) ## Human comments <!-- Agents: Do not fill in this section. This is for human users to fill in. --> ## What was wrong Codex >= 0.146 guards each rollout with a per-process `flock` on `~/.codex/thread-writer-locks/<thread-id>.lock`, and refuses `thread/start|resume|fork` with `thread <id> already has an active writer` while another process holds it. The bridge treated that refusal as a terminal, untyped error, so a single refused attempt failed the user's turn outright even when the lock was about to be released a few hundred milliseconds later. This PR originally also carried fixes for the two bb-internal races that produced most of those refusals. **Both have since landed on `main` by other routes**, so they are no longer part of this diff: - Awaiting the previous app-server child's exit before spawning its replacement landed in #3025. - Binding one Codex thread id to two bb threads (the #2327 investigation's root cause) landed in #3496. What remains is the foreign-writer case that neither addresses. `~/.codex/thread-writer-locks/` is global to the user account — bb only overrides it through `CODEX_HOME`, never per instance — so a second bb instance on the same machine, or a `codex` CLI the user ran in a terminal, can hold a rollout bb wants to resume. ## What changed `plugins/provider-codex/src/bridge/bridge.ts` (+40/-4): `requestThreadConstructionWithWriterRetry` wraps the `thread/start|resume|fork` request used by `constructThreadSession` in a bounded 100/400/1000 ms retry ladder, matching only `already has an active writer` and rethrowing everything else unchanged. Each retry writes its method, delay, and attempt number to the bridge's stderr. After the ladder is exhausted the provider's own error surfaces verbatim. An earlier revision appended remediation prose to the exhausted error. It was removed in `dcc795d1cb`: it named no session the user could actually find, told them to wait after the ladder had already waited, and asked them to identify one of several concurrent `codex app-server` processes distinguishable only by a port buried in their arguments. Human-facing remediation is also product policy the server owns, and every other provider failure class carries a structured recovery hint the runtime acts on rather than prose in a plugin bridge. The remaining ~450 lines are test infrastructure, and cover more than the retry: - `bridge.writer-lock.test.ts` (new, 250): seven regressions. Three cover the retry ladder. **The other four cover the writer-handoff serialization that shipped in #3025 with no test coverage at all** — `previousChildExit` and `releasePromise` currently appear only in `bridge.ts` on `main`, in no test file. - `bridge-process.test-support.ts` (new, 130): shared process-lifecycle helpers, missing-log-safe, attempting every cleanup stage even if an earlier one fails. It already pays for itself: `bridge.archived-rebuild.test.ts` is net −31 lines after adopting it. - `fake-codex-app-server.mjs` (+69): a `writer.lock` fixture mirroring Codex's flock semantics, a process-lifecycle log, and a scripted SIGTERM delay applied only to children that served `thread/start|resume|fork` — a real maintenance child holds no writer and exits promptly, and delaying every child let an in-flight rebuild finish before a concurrent discard could mark the replacement closing. No host-daemon wire contract changed, so `HOST_DAEMON_PROTOCOL_VERSION` is unchanged. ## How you verified `pnpm exec turbo run test typecheck lint --filter=bb-plugin-provider-codex --filter=@bb/agent-runtime --filter=@bb/host-daemon` — 13/13 tasks, 324 + 333 + 727 tests passing. After the prose removal, `--filter=bb-plugin-provider-codex --filter=@bb/agent-runtime` — 10/10 tasks, `bridge.writer-lock.test.ts` 7/7. Live end-to-end against real `codex-cli 0.154.0` (the test fake cannot be injected into a running app — `sanitizeInheritedChildProcessEnv` strips every `BB_*` variable from bridge children). A Codex thread was run to completion, its app-server child killed, and an exclusive `flock` taken on its rollout lock by an outside process, reproducing exactly what a leftover Codex session does. Measured from the persisted `events` table: | Scenario | Build | Result | | --- | --- | --- | | Persistent foreign writer | `origin/main` | rejected after **668 ms** — one attempt | | Persistent foreign writer | this branch | rejected after **2503 ms** — four attempts over 100+400+1000 ms | | Lock released 539 ms after the turn was issued | `origin/main` | hard turn failure | | Lock released 539 ms after the turn was issued | this branch | `turn/started` 440 ms later, turn completed normally | The recovery case is the one that matters: the resume was blocked when issued, retried through the backoff, and succeeded once the lock freed, with no error shown to the user. ## Scope This does **not** close #2327. That issue contains two distinct bugs; the identity mixup found during its investigation was fixed by #3496, and the originally reported problem — `turn/completed` published before the commands in that turn terminalize — is untouched here and still open. A persistent foreign writer is improved but not eliminated: if the other owner does not release within ~1.5 s, the turn still fails with Codex's error. A typed `writerBusy` recovery hint carrying the provider thread id would let the runtime name the owning bb thread instead of surfacing a raw UUID; that is deliberately left out of scope. Supersedes #2578. Relates to #2327. > AGENT GENERATED --------- Co-authored-by: Michael Yong <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Human comments
What was wrong
Undoing an archive revived the BB environment but passed its stale
retiringsnapshot to provider command dispatch, so Codex never receivedthread.unarchive. A later resume detected the archived Codex session and attempted recovery while the rejected app-server child was still exiting, allowing the maintenance unarchive request to contend with the old process and time out. Investigation: https://ymichael.getbb.app/projects/proj_qrv2s45kmq/threads/thr_s29iedumciWhat changed
The unarchive route now reloads the environment after cancelling retirement before dispatching the provider command. Codex app-server shutdown is now awaitable and session releases, maintenance children, rejected resumes, and replacement handoffs wait for process exit before acknowledging or starting the next child. Regression tests cover provider forwarding and delayed-process resume → archive → unarchive handoffs.
HOST_DAEMON_PROTOCOL_VERSIONis bumped to 179 because the server now sends an unarchive command that this route previously skipped.How you verified
pnpm exec turbo run test --filter=@bb/server -- test/public/public-thread-environment-decoupling.test.tspnpm exec turbo run test --filter=bb-plugin-provider-codexpnpm exec turbo run test --filter=@bb/host-daemon-contractpnpm exec turbo run typecheck --filter=@bb/server --filter=bb-plugin-provider-codex --filter=@bb/host-daemon-contractbb plugin build plugins/provider-codexgit diff --checkretiringafter archive, unarchived it back toready, sent a follow-up, and receivedmanaged-archive-followup-okFixes #