Skip to content

fix(claude-cli): show each answer once in Claude CLI chat history - #184098

Merged
obviyus merged 10 commits into
openclaw:mainfrom
Marvinthebored:fix/dupe-assistant-answers
Oct 27, 2026
Merged

obviyus merged 10 commits into
openclaw:mainfrom
Marvinthebored:fix/dupe-assistant-answers

Conversation

@Marvinthebored

@Marvinthebored Marvinthebored commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

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:

  • 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.

@clawsweeper

clawsweeper Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S labels Oct 26, 2026
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 26, 2026
@clawsweeper

clawsweeper Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

Stores 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.

  • Before: History shows the native messages plus a cumulative canonical answer, repeating all three texts.
  • After: History shows each text once, with the canonical final answer matched by native UUID.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) The focused patch has matching live proof and no definite introduced defect, but resolves only new turns within the broader linked history-duplication problem.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (live_output): The current description records a real Claude Code 2.1.310 turn through an isolated Gateway and chat.history, showing two narration markers and the final answer exactly once with native tool ordering; the earlier per-message publication path and redundant helper test are removed.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Partial
User problem: Claude CLI users see narration and final answers repeated in chat history.
Reason: The maintainer revision offers a narrow fix for future turns, explicitly accepts canonical-only narration loss, and leaves historical duplicates to related work.

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
Reviewed head: ffba6519e8380a4ca688f27e90f9538a805a852d

Before merge

None.

Findings

None.

Agent review details

How this fits together

The 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]
Loading

Technical review

Best 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

  • Cumulative pre-tool delivery keeps the original intent (c661061: Preserve pre-tool explanation in durable channel delivery instead of overwriting it with the final closer.)
  • Canonical assistant text changes intended behavior with a stated reason (bd5f760: Keep terminal reply preparation and silence classification authoritative while persisting authored CLI output.)
  • Committed replies and interrupted turns keeps the original intent (0bddd82: A durable final reply remains successful despite later bookkeeping failure; interrupted output retains its existing terminal disposition.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 0bddd82 adds src/agents/cli-runner.ts:526 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 0bddd82e434f; files: src/agents/cli-runner.ts)

PR surface

Source +24, Tests +105. Total +129 across 4 files. Added test files: 1.

View PR surface stats
Area Files Added Removed Net
Source 3 25 1 +24
Tests 1 105 0 +105
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 4 130 1 +129

Review metrics

Metric Value Why it matters
Production versus regression coverage Production +25/-1 lines; tests +105/-0 lines The small producer change reuses existing persistence and merge owners; coverage exercises canonical persistence, transformed output, interruption, and yield behavior.

Root-cause cluster

Relationship: partial_overlap
Canonical: #123792
Summary: The canonical issue tracks aggregate-versus-native history duplication; this PR prevents it for new completed Claude turns while other proposals also address historical records.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Labels

Label changes:

  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • remove status: ⏳ waiting on maintainer: Current PR status label is status: 👀 ready for maintainer look.

Label justifications:

  • P2: Repeated Claude CLI history messages create recurring confusion without blocking message delivery.
  • merge-risk: 🚨 compatibility: Completed Claude canonical rows become final-only, so canonical-only readers lose earlier narration; the maintainer-authored revision explicitly records and accepts this tradeoff.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rating scale

6/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.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

History

Review history (4 earlier review cycles)
  • reviewed 2026-10-26T02:33:07.344Z sha 9bf58b0c3c464a993f27907e7c5dd00fefc20924 :: needs real behavior proof before merge. :: [P1] Preserve narration in canonical history before narrowing the final row
  • reviewed 2026-10-26T23:25:56.981Z sha 18b17e3 :: needs real behavior proof before merge. :: [P2] Preserve non-text blocks before claiming an entire native identity | [P2] Discard rejected envelopes at the existing Stop-hook boundary | [P2] Keep tagged reasoning out of persisted visible text | [P2] Apply configured output replacements to narration rows | [P2] Reconcile the terminal row with the authoritative result
  • reviewed 2026-10-27T03:04:58.496Z sha b26112e :: needs real behavior proof before merge. :: none
  • reviewed 2026-10-27T09:32:00.841Z sha 72655e9 :: blocked before merge. :: none

Reviewed October 27, 2026, 8:31 AM ET / 12:31 UTC (Revision 5).

@Marvinthebored
Marvinthebored force-pushed the fix/dupe-assistant-answers branch from 2a283e5 to 72477d6 Compare October 26, 2026 09:30
@openclaw-barnacle openclaw-barnacle Bot added the r: too-many-prs Auto-close: author has more than twenty active PRs. label Oct 26, 2026
@Marvinthebored
Marvinthebored marked this pull request as ready for review October 26, 2026 10:40
@openclaw-barnacle openclaw-barnacle Bot removed the r: too-many-prs Auto-close: author has more than twenty active PRs. label Oct 26, 2026
@Marvinthebored

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper

clawsweeper Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. status: ⏳ waiting on maintainer Only explicit maintainer approvals or decisions remain; merge is still blocked. labels Oct 27, 2026
@obviyus
obviyus merged commit dce924d into openclaw:main Oct 27, 2026
138 of 141 checks passed
@obviyus

obviyus commented Oct 27, 2026

Copy link
Copy Markdown
Collaborator

Landed in dce924d.

  • Bug: a completed Claude CLI turn saved one canonical row with the whole turn's text, next to Claude's own row for each message. Chat history then showed every narration and the final answer twice.
  • Fix: the canonical row now holds only the final message, tagged with that message's native Claude identity, so the history merge joins the two. Narration comes from Claude's native rows, in order.
  • Proof: one turn on a live isolated Gateway with real Claude CLI (narration, tool, narration, tool, final answer). Each row appears once on this branch and twice on main. The regression test fails on main.

Thank you @Marvinthebored for finding this and writing the first fix and regression test. The final version reuses your test.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 28, 2026
…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]>
tideclaw-bot pushed a commit that referenced this pull request Nov 4, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants