Repository navigation
fix(native-chat): a failed Codex turn keeps its failure and "Worked for" - #23514
Conversation
Codex follows a turn-ending `error` with a failed `turn/completed` for the same turn. The error settled the turn and dropped its start and attributed send; the completion then re-settled it from nothing, so the record lost its start time and flipped from completed/failure to interrupted/failure. A failed turn lost its "Worked for" and read as interrupted. Both ends now go through one settlement that refuses a turn already in the recent-turns window, so every later end (error then completion, a duplicate completion) adds nothing and overwrites nothing.
The fixture was labelled and worded as a failed compaction; this change covers the ordinary-turn path, so its frames now read as one.
Review status: ready for reviewHead: Does it fix the problem? Yes. A failed Codex turn now stays failed and keeps its "Worked for", start time and message link instead of flipping to interrupted. Every turn-ending Codex error is followed by a failed Live QA on this laptop, with real Codex and a model the account rejects, which makes Codex send
New host-level test: it runs through the real translator, write queue and on-disk journal, and makes the two endings merge in the queue. It is red on main's translator (the turn is stored as interrupted with no start time) and green with this PR. With a drain between the two endings it passes on main, which confirms the queue merge is the only path. Architecture: both ways a turn ends go through one step, and a turn's first ending is final, using the translator's existing window of recently settled turns (no new state). This removes the double settle rather than guarding one order of it. Review loop: clean. Checked by reading the code: nothing the skipped second ending used to do is lost:
Also checked:
Not run: live QA. A real failing Codex turn needs a signed-in Codex, which the isolated review rig doesn't have; the tests replay Codex's real frame order. Related: #23059 fixes the same pattern for a failed Late steps from the full review prompt:
|
…ends coalesce in the queue
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
- Settle-once turn end:
complete()andfail()now share one privateend(), whose first check is whetherCodexJournalRecentTurnsalready holds the turn; a turn's first terminal settlement is final. This stops the failedturn/completedCodex always sends after a turn-endingerrorfrom overwriting thefail()record with one that has nostartedAt(and therefore no "Worked for"). CodexJournalRecentTurns.has: new accessor on the bounded recent-turns window, plus a doc comment noting the window now also blocks a second settlement.- Translator test (
codex-structured-journal-translation-turn-settles-once.test.ts): pins error-then-completion to a single terminal write with the opening message, start time, request time and one error row; a duplicate completion to one write; an ordinary turn unchanged; and the next turn settling on its own. - Journal test (
codex-structured-journal-turn-settles-once-journal.test.ts): drives the real translator, deferred sink queue and on-disk journal with the two ends overlapping a pending write, and asserts the stored turn stayscompleted/failurewith a 2-second "Worked for".
I verified these are not theatre by reverting the two production files to origin/main: three of the five new tests fail exactly as described (two terminal writes; interrupted with no startedAt), and all five pass with the fix. The full codex-structured-journal-* set (15 suites, 151 tests) passes, as do tc:node, check:code-quality:changed and oxlint on the changed files. The logic reads soundly: fail() only fires for willRetry: false errors and requires an active turn (always start()ed with startedAt), recentTurns is populated only for the primary thread so the guard is inert for subagent threads, and no path relied on the second settlement (late echoes revise via attributeRequest; provider exit settles through the session sweep).
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (4)
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 2 remain after this review. 📝 WalkthroughWalkthrough
Priority: ➖ Normal Merge Risk: ⚪ Minimal · up to No actionable merge-blocking issue was established. The queue-regression test’s coverage remains unconfirmed, but the available evidence does not show a failure that should prevent merging. Security Architecture ReviewSecurity architecture risk: 🔵 Low · up to The change preserves the first accepted turn ending, preventing a later completion message from erasing a recorded failure and elapsed time. No expanded access or privileges were identified. Protection against unusually delayed duplicate events remains bounded. Retained concerns Security review detailsSecurity Blast Radius
Trust Boundaries and Controls
Resilience and Maintainability Implications
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 2📝 Generate docstrings 💡
🛠️ Fix failing CI checks 💡
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |

ELI5
When a Codex turn fails, Codex tells Orca twice: once with an error, then again with a "turn finished: failed" message. Orca ended the turn both times. Usually the second end was thrown away, but if the two arrived close enough together, the second one replaced the first and the chat showed the failed turn as interrupted with no "Worked for" time. Now Orca ends a turn once, and the first ending is final.
What Changed
Before: Codex reports a turn-ending fault as an
error, and then always sends a failedturn/completedfor the same turn. Orca's Codex translator ended the turn on the error (recorded as failed, with its start time and the message that opened it) and forgot that turn's state. When the completion arrived, it ended the same turn a second time from nothing: interrupted, no start time, no link to the opening message.When the user saw it: both endings are written under the same settlement id, and the journal skips a settlement id it has already applied, so most of the time the second ending never landed. It did land when the completion arrived while the error's ending was still waiting in the session's write queue behind an earlier write (for example the error's own status row). That queue merges pending lifecycle writes with the same settlement id, so the second ending replaced the first before either was written. The turn was then stored as interrupted without a start time, so the host no longer gave it a "Worked for" duration.
After: both ways a Codex turn ends go through one step, and a turn's first ending is final. A later end for a main-thread turn Orca has already ended publishes nothing (subagent threads have no turn record, so a second end there changes nothing either). That covers an error followed by a completion, a duplicate completion, and a second error. The journal's settlement-id check is still there, but it is no longer what keeps a failed turn's record correct.
Mechanism:
complete()andfail()incodex-structured-journal-translation-turn-boundaries.tseach carried a copy of the settle logic, and onlyfail()skipped a turn that was no longer open. They now share oneend(), whose first check is whether the translator's existing window of recently settled turns (CodexJournalRecentTurns) already holds the turn. No new state is added.Why
Checked in Codex's own source: a turn-ending error is followed by
TurnComplete, which the app-server maps to a failedturn/completedfor the same turn. So every failed Codex turn produced a second ending; whether it reached the stored record depended only on write timing.Fixing it where the second ending is produced removes the double write at its source. Relying on the journal's dedupe left it exposed to anything that merges or reorders writes before they reach the journal, which is exactly what the write queue does.
The later completion's only extra field is Codex's own duration, which Orca's timing uses only when both host timestamps are missing; the first ending has both. An echo arriving after the turn ended still updates the record through the existing request-origin revision, and a provider exit settles through the session sweep, so neither path relied on a second ending.
Alternative considered: skipping only "a completion after an error". Rejected, because a duplicate completion would still produce a second write for a finished turn; one rule where a turn ends covers every order.
Alternative considered: stop treating the turn-ending error as an end and let
turn/completedalone settle the turn, since current Codex always sends it. Not done here: the error ending was added (#22047) because a failed turn could otherwise read as working forever, and removing it is a separate behaviour change that first needs a capture from the oldest supported Codex showing the completion always follows. With both ends kept, the first one has to be final.Linked Issue
No issue. Found while reviewing #23059, which fixes the same pattern for a failed Codex
/compactin its own new code. This PR fixes the ordinary-turn path that is on main today. The two touch the same file and will be merged together cleanly by whichever lands second.Visual Proof
N/A: no UI change of its own, and the failure depends on write timing. Live QA on main plus #23552 with a real Codex model failure did not reproduce the relabel (0 of 6 failed turns), because the queue merge needs the two frames to overlap a pending write. With this PR, the same real failure was checked live: one settlement, the turn stays failed, "Worked for" stays, and nothing relabels it after 10 seconds.
With this PR (and current main), a real Codex turn that failed because the account rejects the model. Ten seconds after it ended, the turn still reads failed (red), with its "Worked for":
The same turn expanded, showing the error rows:
Testing
New
codex-structured-journal-turn-settles-once-journal.test.tsdrives a real translator, the real session write queue and a real on-disk journal. It sends a turn-endingerror(willRetry: false) and the failedturn/completedback to back while the error's status row is still being written, so the two endings meet in the queue. It checks the stored turn stays completed with outcome failure, keeps its start time and error-time end, and still yields a 2-second "Worked for" duration. With main's translator it fails: the turn is stored as interrupted with no start time. With a drain between the two frames, main's translator passes, which confirms the queue merge is the path.New
codex-structured-journal-translation-turn-settles-once.test.tschecks the translator alone:error, thenturn/completedstatus failed): exactly one terminal write, a record that stays completed with outcome failure and keeps its start time, request time and message link, and one error row.turn/completeddoes not produce a second write for a successful turn.Reverting the fix makes the new journal test fail and the first two translator tests fail ("expected 1 terminal write, got 2"). Node typecheck, oxlint and the changed-lines quality gate pass, as do the eight Codex structured-journal translation suites, the new journal test and the event-sink suite (132 tests).
Review
Agent skill upstream boundary
docs/reference/agent-skill-sharing-upstream-boundary.mdand copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.Notes
Author X: @BrennanKB5
Security: none. Cross-platform, SSH and mobile: the translator runs on the execution host; no wire or client change. Backwards compatibility: none affected; it only stops a second write.
Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)