Skip to content

Fix Codex rollout writer handoff races - #2578

Closed
ymichael wants to merge 3 commits into
mainfrom
bb/fix-github-issue-2327-thr_w8uqhhkiw7
Closed

ymichael wants to merge 3 commits into
mainfrom
bb/fix-github-issue-2327-thr_w8uqhhkiw7

Conversation

@ymichael

@ymichael ymichael commented Aug 27, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Codex 0.146+ holds a rollout writer lock for the lifetime of each app-server process, but the Codex bridge acknowledged release and spawned replacement children immediately after sending SIGTERM. A settings rebuild, stop/resume, bridge restart, or environment move could therefore resume while bb's previous child still owned the rollout.

The first implementation awaited child exit, but review found two more bridge-local windows. Reconstruction initially removed the old map entry before awaiting exit, allowing a concurrent stop or release-after-maintenance request to miss the in-flight replacement. Publishing the replacement closed that gap, but the pending old-child exit still lived only on the constructor stack: concurrent release found the connection-less replacement and acknowledged before the old child released its writer lock.

What changed

  • Make Codex child termination awaitable through finalized process exit, including the existing SIGKILL and close-grace bounds.
  • Await release before rebuilding, answering thread/stop, and completing maintenance operations that release a session.
  • Publish a connection-less replacement before waiting for the old child, and park the old child's exit promise on it. Concurrent stop/discard now marks the replacement closing and waits for both the parked old exit and any current child exit before acknowledging; reconstruction re-checks closing before spawning.
  • Retry transient active-writer construction refusals on a bounded 100/400/1000 ms ladder and log every retry with its method, delay, and attempt number.
  • Improve persistent-writer guidance to cover both another visible Codex session and a previous/leftover codex app-server process.
  • Add deterministic regressions for settings rebuild, response-time stop serialization, concurrent stop and discard during reconstruction, transient ownership, and persistent ownership recovery. Shared process-test cleanup is missing-log-safe and attempts every cleanup stage even if an earlier one fails.
  • Keep recovery provider-local. Independent real-Codex reproduction showed that the same losing app-server can retry resume after the owner exits, so no new wire recovery kind is needed. Native late-item ordering is orthogonal to the process-lifetime writer lock.
  • No host-daemon wire contract changed, so this PR does not increment HOST_DAEMON_PROTOCOL_VERSION beyond the version already on main.

This PR fixes bb-owned writer handoff races. A persistent writer owned by an independent Codex process is improved, not eliminated: after bounded retries it still returns an untyped bridge error, now with retry diagnostics and actionable guidance.

How you verified

The regressions were proven red against the exact behaviors they guard:

  • Mutating the stop path back to fire-and-forget makes the response-time lock assertion fail with expected true to be false.
  • Restoring the old map-before-await gap makes both concurrent release tests fail because reconstruction succeeds after release was already acknowledged.
  • Before parking the old-child exit on the published replacement, the concurrent stop and concurrent discard tests both acknowledged while writer.lock still existed; both immediate lock-absence assertions failed deterministically with expected true to be false on two runs.

Green checks on the final tree:

  • pnpm exec turbo run test --filter=bb-plugin-provider-codex --force -- src/bridge/bridge.writer-lock.test.ts src/bridge/bridge.archived-rebuild.test.ts (2 files, 8/8 passed)
  • pnpm exec turbo run test --filter=bb-plugin-provider-codex --force (25 files, 255/255 passed)
  • pnpm exec turbo run test --filter=@bb/agent-runtime --force (22 files, 318/318 passed)
  • pnpm exec turbo run typecheck --filter=bb-plugin-provider-codex --force
  • pnpm exec turbo run build --filter=@bb/host-daemon --force
  • pnpm exec oxlint and pnpm exec oxfmt --check on every changed TypeScript file
  • Real Codex 0.148.0 scratch smoke: app-server B received the native active-writer refusal while A held the rollout, then the same B process resumed successfully after A exited.

Fixes #2327

AGENT GENERATED

@ymichael
ymichael force-pushed the bb/fix-github-issue-2327-thr_w8uqhhkiw7 branch from 8cf0f0b to 0e5d134 Compare August 28, 2026 04:50
@Willhong

Copy link
Copy Markdown
Contributor

Rebased these three commits onto current main (author preserved) in #3460 and added the missing piece on the daemon side.

While reproducing #2327 on a live machine, the active-writer refusal turned out to start in the host daemon, not in the bridge: emitTranslatedEvents bound the delta-assembled thread/identity (empty thread id) to a stale pending-identity slot, so after a message edit the next thread/start on the same bridge recorded its Codex thread id onto the rewound thread, and both bb threads then resumed one rollout. #3460 fixes that binding, refuses to resume a provider thread another hosted thread owns, and carries this PR's retry ladder, guidance, writer.lock fixture, and writer-lock tests. The handoff serialization in constructThreadSession / releaseSession already landed via #3025, so main's implementation is kept there.

Leaving this PR open for you or a maintainer to close once #3460 is reviewed.

AGENT GENERATED

@SawyerHood

Copy link
Copy Markdown
Collaborator

This is an automated message. We have closed this pull request because it has been open for more than two weeks.

@SawyerHood SawyerHood closed this 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]>
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.

turn/completed can precede command completion and wedge later turns between active-writer and no-session errors

3 participants