Repository navigation
fix(claude-cli): show each answer once in Claude CLI chat history - #184098
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs maintainer review before merge. What this changesStores a completed Claude CLI turn’s final answer with its native identity so history can merge it with the corresponding native row. Example: Claude emits NARRATION-ONE, NARRATION-TWO, and FINAL-ANSWER around two tool calls.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Partial Merge readiness✅ Ready for maintainer review This PR provides a focused, maintainer-revised fix for duplicated new Claude CLI history turns; no actionable introduced defect was found. Priority: P2 Before mergeNone. FindingsNone. Agent review detailsHow this fits togetherThe CLI runner receives parsed backend output, persists canonical assistant history, and leaves the existing Gateway history index to merge canonical and native Claude records. flowchart LR
A[Claude CLI output] --> B[CLI parser]
B --> C[Terminal reply preparation]
C --> D[Canonical transcript writer]
E[Native Claude history] --> F[Gateway history merge]
D --> F
F --> G[Chat history clients]
Technical reviewBest possible solution: Land the focused native-identity producer fix and retain historical aggregate cleanup as separate related work. Do we have a high-confidence way to reproduce the issue? Current main persists cumulative text while native history contains separate assistant rows, and the PR supplies a matching live Gateway before/after result; no target code was executed during this review. Is this the best way to solve the issue? The revision is a small producer repair using existing final-message preparation and identity matching, while preserving cumulative channel delivery and interrupted-turn behavior. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against caa23fd5f80e. Provenance checked
TestingProof path: shipped entry point. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource +24, Tests +105. Total +129 across 4 files. Added test files: 1. View PR surface stats
Review metrics
Root-cause clusterRelationship: Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. LabelsLabel changes:
Label justifications:
Rating scale6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior. WorkflowClawSweeper edits this one comment on every review. Comment HistoryReview history (4 earlier review cycles)
Reviewed October 27, 2026, 8:31 AM ET / 12:31 UTC (Revision 5). |
2a283e5 to
72477d6
Compare
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
|
🦞👀 Re-review progress:
|
|
Landed in dce924d.
Thank you @Marvinthebored for finding this and writing the first fix and regression test. The final version reuses your test. |
…enclaw#184098) Related: openclaw#123792, openclaw#183529, openclaw#137788 ## What Problem This Solves Fixes repeated narration and final answers in chat history when a Claude CLI turn has several assistant messages (narration, tool calls, final answer). ## Why This Change Was Made Claude saves each assistant message as its own native row. OpenClaw also saved one canonical row with the whole turn's cumulative text (narration plus final). The history merge could match neither, so chat history showed every narration and the final answer twice. The fix changes only the canonical row. For a completed Claude turn, it now holds the final message alone, with plugin output transforms applied. It is tagged with the native row's identity (`importedFrom: "claude-cli"`, `cliSessionId`, `externalId` = the turn's resume checkpoint, which is the final message's native uuid). The existing merge joins the two by that identity, so text transforms cannot break the match. Narration comes only from Claude's native rows, in native order, with the tool rows between them. Unchanged: - yielded and interrupted turns - turns without a native checkpoint (non-Claude CLI backends) - silent replies - what is delivered to channels This replaces the earlier version of this PR (per-message canonical rows, a batch transcript append and a new output field, +744/−94) with a 24-line production change. The regression test is the earlier version's history-ownership test, adapted. ## User Impact New completed Claude CLI turns show each narration and the final answer once, in order. History written before this change keeps its duplicates. If Claude's native history is unavailable, canonical history shows the final answer without the earlier narration. ## Evidence **Live Gateway, real Claude CLI (Claude Code 2.1.310).** I ran one turn on an isolated Gateway: narration, exec tool, narration, exec tool, final answer. `chat.history` assistant rows: | Text | `main` | this PR | | --- | --- | --- | | `NARRATION-ONE` | 2 rows | 1 row | | `NARRATION-TWO` | 2 rows | 1 row | | `FINAL-ANSWER` | 2 rows | 1 row: the canonical row (`seq 2`), joined to native `externalId` | The tool rows stay between the narration rows, in native order. **Tests** - `cli-runner.history-ownership.test.ts`: through `runPreparedCliAgent`, a completed turn stores one canonical row containing the final message (also with an output transform), tagged with the native identity. Merging it with three native rows gives three rows, and the final row is canonical. Interrupted and yielded turns keep the cumulative row without identity. The completed cases fail on `main`. - These suites pass: `cli-run-transcript`, `cli-run-transcript.admission`, `cli-runner.reliability`, `cli-run-settlement`, `cli-session-history`, `server.chat.cli-history`. - `oxlint` and `oxfmt` are clean. Credit: @Marvinthebored found and reported the duplicates, and wrote the first fix and its regression test on this branch. **Merge danger:** two-way door. A revert restores cumulative canonical rows. Rows written meanwhile keep the final-only text and identity, which the merge handles either way. Blast radius: Claude CLI chat history and canonical-only readers of completed Claude turns. Co-authored-by: Marvinthebored <[email protected]> Co-authored-by: Ayaan Zaidi <[email protected]>
Release adaptation: preserve the stable CLI diagnostics and settlement owners. Port the source regression onto the stable runParams fixture. Both abort and caller-revocation regressions fail before the fix; all 28 CLI hook/model-policy/ cron-root tests pass after it. Companions: #181966 and #186380 follow in order. #184098/#188900 and later catalog/admission refactors fix separate behavior and are not prerequisites. (cherry picked from commit 5b404fd) Co-authored-by: RomneyDa <[email protected]>
Related: #123792, #183529, #137788
What Problem This Solves
Fixes repeated narration and final answers in chat history when a Claude CLI turn has several assistant messages (narration, tool calls, final answer).
Why This Change Was Made
Claude saves each assistant message as its own native row. OpenClaw also saved one canonical row with the whole turn's cumulative text (narration plus final). The history merge could match neither, so chat history showed every narration and the final answer twice.
The fix changes only the canonical row. For a completed Claude turn, it now holds the final message alone, with plugin output transforms applied. It is tagged with the native row's identity (
importedFrom: "claude-cli",cliSessionId,externalId= the turn's resume checkpoint, which is the final message's native uuid). The existing merge joins the two by that identity, so text transforms cannot break the match. Narration comes only from Claude's native rows, in native order, with the tool rows between them.Unchanged:
This replaces the earlier version of this PR (per-message canonical rows, a batch transcript append and a new output field, +744/−94) with a 24-line production change. The regression test is the earlier version's history-ownership test, adapted.
User Impact
New completed Claude CLI turns show each narration and the final answer once, in order. History written before this change keeps its duplicates. If Claude's native history is unavailable, canonical history shows the final answer without the earlier narration.
Evidence
Live Gateway, real Claude CLI (Claude Code 2.1.310). I ran one turn on an isolated Gateway: narration, exec tool, narration, exec tool, final answer.
chat.historyassistant rows:mainNARRATION-ONENARRATION-TWOFINAL-ANSWERseq 2), joined to nativeexternalIdThe tool rows stay between the narration rows, in native order.
Tests
cli-runner.history-ownership.test.ts: throughrunPreparedCliAgent, a completed turn stores one canonical row containing the final message (also with an output transform), tagged with the native identity. Merging it with three native rows gives three rows, and the final row is canonical. Interrupted and yielded turns keep the cumulative row without identity. The completed cases fail onmain.cli-run-transcript,cli-run-transcript.admission,cli-runner.reliability,cli-run-settlement,cli-session-history,server.chat.cli-history.oxlintandoxfmtare clean.Credit: @Marvinthebored found and reported the duplicates, and wrote the first fix and its regression test on this branch.
Merge danger: two-way door. A revert restores cumulative canonical rows. Rows written meanwhile keep the final-only text and identity, which the merge handles either way. Blast radius: Claude CLI chat history and canonical-only readers of completed Claude turns.