Repository navigation
fix(codex): keep each turn's assistant reply when the provider reuses message ids - #47737
Conversation
… message ids Live Codex item identities were cached by thread and item id only. When a Responses-compatible provider reuses an assistant message id in a later turn, the later reply took the earlier turn's identity and overwrote that reply in place, above the newest prompt. Mint a new identity when a cached message identity belongs to a different turn. Refs #47718 Co-authored-by: Claude <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
ELI5
Some model providers used through Codex give every reply the same message ID. Orca used that ID to decide which chat row a reply belongs to, so each new reply was written over the previous one. Now a reply from a different turn always gets its own row.
What Changed
CodexJournalItems.identityForcaches live item identities by(threadId, itemId). A later turn that reused an item ID got the earlier turn's cached identity, so its row upserted over the old one.identityFornow creates a new identity (with a new per-turn ordinal) when the cached Codex identity belongs to a different turn than the event. Items within one turn, and command items that finish after their turn ends (Codex reports them with their original turn ID), still reuse the cached identity.Why
Item IDs come from the provider and are not guaranteed to be unique across turns. The turn ID is the reliable boundary, so we check it before reusing a cached identity. The ordinal numbering stays the same as history import, so a reloaded transcript gets the same row keys. This is a small, local change and needs no schema or protocol changes.
Linked Issue
Refs #47718
This is a partial fix. It covers the live-stream overwrite caused by reused message IDs. The other symptoms in the report may have other causes and are not covered here.
Visual Proof
N/A. This is a fix to journal row identity in the main process. The repro needs a Responses-compatible provider that reuses message IDs, which we can't trigger on demand in a GUI run. The regression test replays that exact notification sequence. On
mainthe second turn's reply overwrites the first and onlycodex:t:turn-a:1remains. On this branch bothcodex:t:turn-a:1andcodex:t:turn-b:1survive with their own text.Testing
Added
codex-structured-journal-items.test.ts. It replays two turns that both use agent message IDmsg_0(user echo, start, delta, complete) and checks that each turn keeps its own row and text. It fails onmain(expected [ 'codex:t:turn-a:1' ] to deeply equal [ 'codex:t:turn-a:1', 'codex:t:turn-b:1' ]) and passes on this branch.Ran neighbor suites:
src/main/codex/structured/**andsrc/main/bots/bot-run-turn-watch.test.ts/bot-request-settlement.test.ts. All pass.Checked the upstream Codex source (
codex-rsprotocol):ItemCompletedNotificationcarriesturn_id, and exec command end events carry the turn the command belongs to. So a background command that finishes after its turn still matches its cached identity.Static gates on the touched files: oxlint (default, anti-slop, native code-quality, type-aware), ast-grep, oxfmt, and the changed-code quality check. All clean.
Platforms: macOS (unit level). The change is platform-independent pure logic.
I manually tested these changes locally
Automated tests added/updated, or explained why not below
Every added test fails without this change and avoids the test anti-patterns
Performance
This runs on the main-process Codex notification path, once per item event. Deltas only reach it when a stream starts. It adds one O(1) field comparison and no new I/O, timers, or subprocesses. Measured with a translator replay of 2000 turns × (start + 20 deltas + complete), median of 7, old vs new:
mainwas wrongly dropping.Per-frame microbench: typical case 407–416 ns (old) vs 397–399 ns (new). Identity cache size is still capped by
MAX_CODEX_IDENTITY_ENTRIES.Risk
Level: Low
The riskiest part: if Codex ever re-emitted an existing item under a different turn ID, it would now show up as a new row instead of updating the old one. Upstream Codex ties item and command events to their originating turn, so this should not happen with stock Codex. History import already keys by turn, so it is unchanged. No saved data, settings, or remote-protocol changes. A plain revert backs it out.
Review
Agent skill upstream boundary
Notes
Ensure no issues in: Security, Cross-platform support (Linux, Windows, Mac), Remote SSH, Mobile, general backwards compatibility, performance
Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)Reported by @fidding