Skip to content

fix(codex): keep each turn's assistant reply when the provider reuses message ids - #47737

Merged
nwparker merged 1 commit into
mainfrom
bugbash/issue-47718
Nov 23, 2026
Merged

nwparker merged 1 commit into
mainfrom
bugbash/issue-47718

Conversation

@nwparker

@nwparker nwparker commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

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.

Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​37 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​37
Prod 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​2 $\color{#cf222e}{\Huge{\mathbf{−}}}$​1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​1

What Changed

  • Before: In a Codex Native Chat backed by a provider that reuses assistant message IDs (reported with deepseek through Codex), each new assistant reply replaced the previous turn's reply. Earlier answers disappeared, and the newest answer showed up above the newest prompt.
  • After: Each turn's reply stays in its own row after its own prompt.
  • Mechanism: CodexJournalItems.identityFor caches 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. identityFor now 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 main the second turn's reply overwrites the first and only codex:t:turn-a:1 remains. On this branch both codex:t:turn-a:1 and codex:t:turn-b:1 survive with their own text.

Testing

  • Added codex-structured-journal-items.test.ts. It replays two turns that both use agent message ID msg_0 (user echo, start, delta, complete) and checks that each turn keeps its own row and text. It fails on main (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/** and src/main/bots/bot-run-turn-watch.test.ts / bot-request-settlement.test.ts. All pass.

  • Checked the upstream Codex source (codex-rs protocol): ItemCompletedNotification carries turn_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:

  • typical unique IDs: 27.27 ms (old) vs 27.29 ms (new). No change.
  • adversarial reused IDs: 21.39 ms (old) vs 23.50 ms (new), about 1 µs more per turn. The extra time comes from emitting the reply row that main was 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

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Ensure no issues in: Security, Cross-platform support (Linux, Windows, Mac), Remote SSH, Mobile, general backwards compatibility, performance

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • I stated the risk: what could break, for whom, and how to back it out
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

Reported by @fidding

… 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>
@coderabbitai

coderabbitai Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The identity logic now creates a new item identity when a provider reuses a message ID in a different non-null turn. A regression test submits two turns with the same item ID and checks that the journal sink contains separate rows with each turn’s answer.

Merge Risk: Minimal · up to 46137

This change keeps assistant replies in their own rows when a provider reuses message IDs across turns, and it adds a regression test for that case. No concrete defect or performance regression was found. It is ready to merge with normal checks.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: when a provider reuses message IDs, each turn's assistant reply keeps its own row. It also gives the scope (fix in codex). It is specific and accurate to the changeset…
Description check Passed The description follows the repository template closely. It has ELI5, What Changed (before/after and mechanism), Why, Linked Issue (partial fix, Refs #47718), Visual Proof (N/A with reason), Testing, …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: when a provider reuses message IDs, each turn's assistant reply keeps its own row. It also gives the scope (fix in codex). It is specific and accurate to the changeset, which adds a new identity when a cached Codex identity belongs to a different turn.

Full details: Description check

Explanation

The description follows the repository template closely. It has ELI5, What Changed (before/after and mechanism), Why, Linked Issue (partial fix, Refs #47718), Visual Proof (N/A with reason), Testing, Performance, Risk (Level: Low, with a rollback method), AI Disclosure, Review, Notes, and Checklist. Its content is specific and matches the raw summary. Gaps: the Linked Issue section uses 'Refs' and not 'Fixes', which the template says is not sufficient for outside contributors. The lint/typecheck/test/build checklist item is unchecked. The AI Disclosure section is left as the template placeholder. The template's Level field is filled in.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 781f0368-0606-4f90-ab12-cb3eeb9b5c5a
Commits

Reviewing files that changed from the base of the PR and between 9b664bf and 461375b.

Files selected for processing: 2

  • src/main/codex/structured/journal/codex-structured-journal-items.test.ts
  • src/main/codex/structured/journal/codex-structured-journal-items.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

@nwparker
nwparker merged commit 6e4ebc8 into main Nov 23, 2026
54 checks passed
@nwparker
nwparker deleted the bugbash/issue-47718 branch November 23, 2026 17:09
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.

1 participant