Skip to content

Correlate codex turn dispatches with the turn the turn/start response names - #2639

Closed
ratulsarna wants to merge 1 commit into
get-bb:mainfrom
ratulsarna:fix/codex-turn-start-response-correlation
Closed

ratulsarna wants to merge 1 commit into
get-bb:mainfrom
ratulsarna:fix/codex-turn-start-response-correlation

Conversation

@ratulsarna

Copy link
Copy Markdown
Contributor

Human comments

What was wrong

The bridge ignored the turn/start result (z.unknown(), discarded) and settled accepted dispatches by guessing: if no turn/started claimed the correlation within 250 ms of the answer, it fabricated a completed zero-work turn. Codex answers turn/start at turn creation and can emit turn/started later than that window (observed 351 ms to 2.7 s under load), so the bridge recorded a synthetic completed turn followed by the real turn. A parent thread saw its child complete with no output while the child was still working. Root cause and repro in #2580.

What changed

  • plugins/provider-codex/src/bridge/bridge.ts: handleTurnStart captures the dispatch result. settleAcceptedDispatch parses it; a response naming a turn settles against that real turn, anything else (compact's {}, malformed body) keeps the existing zero-work timer unchanged.
  • plugins/provider-codex/src/translator.ts: openTurnFromStartResponse proves ownership the same way claim() does (dispatch still at the correlation queue head), then opens the turn through the same turn/started translation the real notification would take. The provider's own later turn/started for that id is suppressed so it cannot double-open the turn or shift another dispatch's correlation. A response reporting an already-terminal status settles the full lifecycle at once and suppresses both later notifications. A dispatch codex steers into the running turn only emits input.accepted against it.
  • Because the turn enters openCodexTurnIds at response time, interrupt waiting, steering, and child-exit settlement see it with no new state: a crash between answer and notification now settles the real turn as failed instead of fabricating a completed one.
  • packages/host-daemon-contract: HOST_DAEMON_PROTOCOL_VERSION 173 → 174 (same delta kinds, but the lifecycle sequences the daemon emits change) and the version assertion in contract.test.ts.
  • fake-codex-app-server.mjs: turn/start scenarios now answer with the turn like every supported codex (0.136.0 up already returns { turn }; verified against codex-rs rust-v0.136.0), the late-start delay crosses the grace window, and new prompts cover the crash, steer, terminal-response, and settle-before-started shapes.

Known limit: a codex whose turn/start answers without a turn still gets the old 250 ms guess; with no turn id there is nothing to correlate. No supported version does this.

How you verified

  • New/updated tests in bridge.zero-work-turn.test.ts fail before and pass after: exactly-once lifecycle when turn/started lags past the grace window (the issue's repro, previously two lifecycles), failed settlement when codex dies before turn/started (previously a fabricated completed turn), steered dispatch acknowledged on the running turn, terminal-status response settling at once, and no resurrection when an interrupt settles a response-opened turn before its turn/started arrives.
  • pnpm exec turbo run test green for bb-plugin-provider-codex (253), @bb/host-daemon-contract (51), @bb/provider-bridge-protocol (238), @bb/agent-runtime (318), @bb/host-daemon (561), @bb/integration-tests (77); full-repo turbo run typecheck (80 tasks).
  • Probed real codex app-server 0.149.1: turn/start answers at turn creation with {"turn":{"id":…,"status":"inProgress"}}, and the answer can precede turn/started.

Fixes #2580

AGENT GENERATED

… names

The bridge ignored the turn/start result and guessed at settlement with a
250 ms timer. When codex answered before emitting turn/started and the
notification lagged past the grace window, the timer fabricated a completed
zero-work turn and the real turn arrived later as a second lifecycle, so a
parent could see its child complete with no output while the child was still
working (get-bb#2580).

Every supported codex (0.136.0 up) answers turn/start with the turn it
opened. The bridge now opens that turn from the response, attaches the
accepted input to it, and suppresses the provider's own later turn/started.
A response reporting an already-terminal turn settles the full lifecycle at
once, with the later notifications suppressed the same way. A dispatch codex
steers into the already-running turn only acknowledges the input against
that turn. The zero-work timer remains solely for responses that prove no
turn: thread/compact/start's empty result or a malformed body. A crash
between the answer and the notification now settles the real turn as failed
instead of fabricating a completed one.
SawyerHood pushed a commit that referenced this pull request Sep 11, 2026
… names

The bridge ignored the turn/start result and guessed at settlement with a
250 ms timer. When codex answered before emitting turn/started and the
notification lagged past the grace window, the timer fabricated a completed
zero-work turn and the real turn arrived later as a second lifecycle, so a
parent could see its child complete with no output while the child was still
working (#2580).

Every supported codex (0.136.0 up) answers turn/start with the turn it
opened. The bridge now opens that turn from the response, attaches the
accepted input to it, and suppresses the provider's own later turn/started.
A response reporting an already-terminal turn settles the full lifecycle at
once, with the later notifications suppressed the same way. A dispatch codex
steers into the already-running turn only acknowledges the input against
that turn. The zero-work timer remains solely for responses that prove no
turn: thread/compact/start's empty result or a malformed body. A crash
between the answer and the notification now settles the real turn as failed
instead of fabricating a completed one.

Cherry-picked from #2639 (Ratul Sarna) onto the #3462 fix; the protocol
version pin is resolved to 201 on top of that branch's 200.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
SawyerHood pushed a commit that referenced this pull request Sep 11, 2026
… names

The bridge ignored the turn/start result and guessed at settlement with a
250 ms timer. When codex answered before emitting turn/started and the
notification lagged past the grace window, the timer fabricated a completed
zero-work turn and the real turn arrived later as a second lifecycle, so a
parent could see its child complete with no output while the child was still
working (#2580).

Every supported codex (0.136.0 up) answers turn/start with the turn it
opened. The bridge now opens that turn from the response, attaches the
accepted input to it, and suppresses the provider's own later turn/started.
A response reporting an already-terminal turn settles the full lifecycle at
once, with the later notifications suppressed the same way. A dispatch codex
steers into the already-running turn only acknowledges the input against
that turn. The zero-work timer remains solely for responses that prove no
turn: thread/compact/start's empty result or a malformed body. A crash
between the answer and the notification now settles the real turn as failed
instead of fabricating a completed one.

Cherry-picked from #2639 (Ratul Sarna) onto the #3462 fix; the protocol
version pin is resolved to 201 on top of that branch's 200.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
SawyerHood added a commit that referenced this pull request Sep 11, 2026
A thread could wedge with every send refused as a competing turn while
the app showed it idle or errored, and `bb thread stop` reported success
without changing anything (#3462).

Root cause, traced on four production Codex threads from 2026-09-01: the
Codex bridge settled the spawn turn as a synthetic zero-work completion
250 ms after `turn/start` responded, before the real turn opened. The
server went idle, a follow-up send was dispatched, and the real turn then
started as an unrequested root turn. The daemon correctly refused the
follow-up `turn.submit` as a competing turn, but the server settled that
refusal as `run.failed`, moving the thread to `error` while a root turn
it had already stored was still running. From `error` or `idle` an
explicit stop sends `intent: "release"`, which the daemon deliberately
skips when a turn is active, and the release result was indistinguishable
from a completed release, so the stop was a silent no-op until the daemon
turn ended. When it did, `run.succeeded` is illegal from `error`, so the
thread stayed errored.

The Codex zero-work settlement race that started the sequence is tracked
separately in #2580 (candidate fix #2639); this change does not touch the
bridge. It repairs the server and daemon sides so any provider turn the
server lost track of can no longer wedge a thread or be reported as a
successful stop.

Changes:
- Daemon: `thread.stop` with `intent: "release"` now reports
  `activeTurnRetained: true` when it keeps an active turn. Runtime and
  dispatcher refusals carry the `competing_turn` error code
  (`CompetingTurnError`, `COMPETING_TURN_ERROR_CODE`).
- Server: explicit stops (stop route, context clear) that get a retained
  release re-activate the thread from the daemon's report, mark it
  stopping, and interrupt it through the normal stop path, so the stored
  turn is completed as interrupted and the thread settles idle. Automatic
  release paths and the daemon's release race guard are unchanged.
- Server: a `turn.submit` refused with `competing_turn` while the thread
  has an uncompleted stored root turn records the rejection but keeps the
  thread active instead of failing the run; the running turn's completion
  settles it.
- Protocol version 199 -> 200 for the new optional result field and error
  code.

Verification: new server tests for idle/error retained-turn stops,
context clear, concurrent stops, a failed escalated interrupt, and the
competing-turn settlement with negative controls; all six behavioral
tests fail on the previous lifecycle implementation. Daemon dispatch and
error-code tests updated. Full @bb/server (2426), @bb/host-daemon (591),
@bb/agent-runtime (318) and @bb/host-daemon-contract (51) suites pass.
Live dev-app check with Codex: a running turn whose thread was flipped
to error/idle is interrupted by `bb thread stop` and the next send
succeeds; a send refused as competing keeps the thread active and it
settles idle when the daemon turn completes.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
SawyerHood pushed a commit that referenced this pull request Sep 11, 2026
… names

The bridge ignored the turn/start result and guessed at settlement with a
250 ms timer. When codex answered before emitting turn/started and the
notification lagged past the grace window, the timer fabricated a completed
zero-work turn and the real turn arrived later as a second lifecycle, so a
parent could see its child complete with no output while the child was still
working (#2580).

Every supported codex (0.136.0 up) answers turn/start with the turn it
opened. The bridge now opens that turn from the response, attaches the
accepted input to it, and suppresses the provider's own later turn/started.
A response reporting an already-terminal turn settles the full lifecycle at
once, with the later notifications suppressed the same way. A dispatch codex
steers into the already-running turn only acknowledges the input against
that turn. The zero-work timer remains solely for responses that prove no
turn: thread/compact/start's empty result or a malformed body. A crash
between the answer and the notification now settles the real turn as failed
instead of fabricating a completed one.

Cherry-picked from #2639 (Ratul Sarna) onto the #3462 fix; the protocol
version pin is resolved to 201 on top of that branch's 200.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
SawyerHood pushed a commit that referenced this pull request Sep 11, 2026
… names

The bridge ignored the turn/start result and guessed at settlement with a
250 ms timer. When codex answered before emitting turn/started and the
notification lagged past the grace window, the timer fabricated a completed
zero-work turn and the real turn arrived later as a second lifecycle, so a
parent could see its child complete with no output while the child was still
working (#2580).

Every supported codex (0.136.0 up) answers turn/start with the turn it
opened. The bridge now opens that turn from the response, attaches the
accepted input to it, and suppresses the provider's own later turn/started.
A response reporting an already-terminal turn settles the full lifecycle at
once, with the later notifications suppressed the same way. A dispatch codex
steers into the already-running turn only acknowledges the input against
that turn. The zero-work timer remains solely for responses that prove no
turn: thread/compact/start's empty result or a malformed body. A crash
between the answer and the notification now settles the real turn as failed
instead of fabricating a completed one.

Cherry-picked from #2639 (Ratul Sarna) onto the #3462 fix; the protocol
version pin is resolved to 201 on top of that branch's 200.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
SawyerHood pushed a commit that referenced this pull request Sep 11, 2026
… names

The bridge ignored the turn/start result and guessed at settlement with a
250 ms timer. When codex answered before emitting turn/started and the
notification lagged past the grace window, the timer fabricated a completed
zero-work turn and the real turn arrived later as a second lifecycle, so a
parent could see its child complete with no output while the child was still
working (#2580).

Every supported codex (0.136.0 up) answers turn/start with the turn it
opened. The bridge now opens that turn from the response, attaches the
accepted input to it, and suppresses the provider's own later turn/started.
A response reporting an already-terminal turn settles the full lifecycle at
once, with the later notifications suppressed the same way. A dispatch codex
steers into the already-running turn only acknowledges the input against
that turn. The zero-work timer remains for results that name no turn
(thread/compact/start's empty result and a malformed body). A crash between
the answer and the notification now settles the real turn as failed instead
of fabricating a completed one.

Cherry-picked from #2639 (Ratul Sarna) onto the #3462 fix; the host-daemon
protocol version moves from #3462's 204 to 205.

Co-Authored-By: Claude Code <[email protected]>
SawyerHood pushed a commit that referenced this pull request Sep 11, 2026
… names

The bridge ignored the turn/start result and guessed at settlement with a
250 ms timer. When codex answered before emitting turn/started and the
notification lagged past the grace window, the timer fabricated a completed
zero-work turn and the real turn arrived later as a second lifecycle, so a
parent could see its child complete with no output while the child was still
working (#2580).

Every supported codex (0.136.0 up) answers turn/start with the turn it
opened. The bridge now opens that turn from the response, attaches the
accepted input to it, and suppresses the provider's own later turn/started.
A response reporting an already-terminal turn settles the full lifecycle at
once, with the later notifications suppressed the same way. A dispatch codex
steers into the already-running turn only acknowledges the input against
that turn. The zero-work timer remains for results that name no turn
(thread/compact/start's empty result and a malformed body). A crash between
the answer and the notification now settles the real turn as failed instead
of fabricating a completed one.

Cherry-picked from #2639 (Ratul Sarna) onto the #3462 fix; the host-daemon
protocol version moves from #3462's 204 to 205.

Co-Authored-By: Claude Code <[email protected]>
@SawyerHood SawyerHood closed this Sep 12, 2026
@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 added a commit that referenced this pull request Sep 16, 2026
A thread could wedge with every send refused as a competing turn while
the app showed it idle or errored, and `bb thread stop` reported success
without changing anything (#3462).

Root cause, traced on four production Codex threads from 2026-09-01: the
Codex bridge settled the spawn turn as a synthetic zero-work completion
250 ms after `turn/start` responded, before the real turn opened. The
server went idle, a follow-up send was dispatched, and the real turn then
started as an unrequested root turn. The daemon correctly refused the
follow-up `turn.submit` as a competing turn, but the server settled that
refusal as `run.failed`, moving the thread to `error` while a root turn
it had already stored was still running. From `error` or `idle` an
explicit stop sends `intent: "release"`, which the daemon deliberately
skips when a turn is active, and the release result was indistinguishable
from a completed release, so the stop was a silent no-op until the daemon
turn ended. When it did, `run.succeeded` is illegal from `error`, so the
thread stayed errored.

The Codex zero-work settlement race that started the sequence is tracked
separately in #2580 (candidate fix #2639); this change does not touch the
bridge. It repairs the server and daemon sides so any provider turn the
server lost track of can no longer wedge a thread or be reported as a
successful stop.

Changes:
- Daemon: `thread.stop` with `intent: "release"` now reports
  `activeTurnRetained: true` when it keeps an active turn. Runtime and
  dispatcher refusals carry the `competing_turn` error code
  (`CompetingTurnError`, `COMPETING_TURN_ERROR_CODE`).
- Server: explicit stops (stop route, context clear) that get a retained
  release re-activate the thread from the daemon's report, mark it
  stopping, and interrupt it through the normal stop path, so the stored
  turn is completed as interrupted and the thread settles idle. Automatic
  release paths and the daemon's release race guard are unchanged.
- Server: a `turn.submit` refused with `competing_turn` while the thread
  has an uncompleted stored root turn records the rejection but keeps the
  thread active instead of failing the run; the running turn's completion
  settles it.
- Protocol version 199 -> 200 for the new optional result field and error
  code.

Verification: new server tests for idle/error retained-turn stops,
context clear, concurrent stops, a failed escalated interrupt, and the
competing-turn settlement with negative controls; all six behavioral
tests fail on the previous lifecycle implementation. Daemon dispatch and
error-code tests updated. Full @bb/server (2426), @bb/host-daemon (591),
@bb/agent-runtime (318) and @bb/host-daemon-contract (51) suites pass.
Live dev-app check with Codex: a running turn whose thread was flipped
to error/idle is interrupted by `bb thread stop` and the next send
succeeds; a send refused as competing keeps the thread active and it
settles idle when the daemon turn completes.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
SawyerHood pushed a commit that referenced this pull request Sep 16, 2026
… names

The bridge ignored the turn/start result and guessed at settlement with a
250 ms timer. When codex answered before emitting turn/started and the
notification lagged past the grace window, the timer fabricated a completed
zero-work turn and the real turn arrived later as a second lifecycle, so a
parent could see its child complete with no output while the child was still
working (#2580).

Every supported codex (0.136.0 up) answers turn/start with the turn it
opened. The bridge now opens that turn from the response, attaches the
accepted input to it, and suppresses the provider's own later turn/started.
A response reporting an already-terminal turn settles the full lifecycle at
once, with the later notifications suppressed the same way. A dispatch codex
steers into the already-running turn only acknowledges the input against
that turn. The zero-work timer remains for results that name no turn
(thread/compact/start's empty result and a malformed body). A crash between
the answer and the notification now settles the real turn as failed instead
of fabricating a completed one.

Cherry-picked from #2639 (Ratul Sarna) onto the #3462 fix; the host-daemon
protocol version moves from #3462's 204 to 205.

Co-Authored-By: Claude Code <[email protected]>
SawyerHood added a commit that referenced this pull request Sep 16, 2026
… names (#3503)

## Human comments

## What was wrong

Stacked on #3502. Codex can return `turn/start` before emitting
`turn/started`. The bridge discarded the response and fabricated a
completed turn after 250 ms, attaching accepted input to that synthetic
turn while the real work ran in a second, unaccepted turn (#2580 and
consolidated #2634). Compaction's empty response has the same
delayed-notification problem.

## What changed

Preserves Ratul Sarna's original #2639 implementation and authorship,
with lifecycle and failure-path fixes:

- Open and correlate the turn named by `turn/start`; deduplicate its
later native lifecycle notifications. Dedupe is scoped by provider
thread, cleared on exit, and bounded to 256 pending lifecycle records
per thread.
- Await compaction's actual turn. An idle/system-error report or process
exit can settle an unopened dispatch. Register it before sending the
request, buffer early status until RPC acceptance, and remove it as soon
as its input is accepted. A rejected request cannot fabricate an
accepted/completed turn.
- Retry an interrupt once only for the exact `-32600` / `no active turn
to interrupt` rejection when that same response-opened turn lacked
native activation at the initial attempt. Wait for activation, return
the original error on timeout, and avoid retrying a turn that already
settled.
- Preserve the legacy zero-work fallback only for a `turn/start` result
that names no turn.
- Bump `HOST_DAEMON_PROTOCOL_VERSION` from #3502's 212 to 213. Update
lifecycle regressions to use the current test harness after rebasing
onto main.

## How you verified

- Nine new failure-path regressions fail against the previous #3503
implementation and pass with these fixes: unrelated interrupt errors,
interrupt after native activation, completion-before-error, activation
timeout, compact idle/error before response, cross-thread dedupe, and
exit cleanup. Additional cases exercise a single retry,
activation-before-rejection, delayed terminal notifications, compaction
exit, and consecutive requests' tool attribution.
- `pnpm exec turbo run test typecheck --filter=bb-plugin-provider-codex
--filter=@bb/provider-bridge-protocol --filter=@bb/provider-parity
--filter=@bb/host-daemon-contract` passes on the rebased layer. Codex:
317 tests; protocol: 279; parity: 56; contract: 59.
- Real Codex 0.154.0 via the configured account pool, with fresh empty
`CODEX_HOME` directories and workspaces: a relay delays native start
notifications 400 ms. Reverting this layer to #3502 reproduces two
lifecycles for both an ordinary turn and compaction; the final layer
produces one accepted lifecycle. Delaying completion notifications also
proves the review fix: the prior bridge sends two interrupts after
native activation/real completion; the fixed bridge sends one and
preserves the real `-32600` rejection. No BB core or imported store was
started, and no credentials were copied.

Fixes #2580
Closes #2639

> AGENT GENERATED

---------

Co-authored-by: Ratul Sarna <[email protected]>
Co-authored-by: Claude Code <[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.

Codex bridge fabricates a completed turn when turn/started is delayed

2 participants