Skip to content

fix(session-memory): deduplicate consecutive assistant messages with identical text#92966

Closed
hnlqt666 wants to merge 2 commits into
openclaw:mainfrom
hnlqt666:fix/session-memory-duplicate-thinking-messages
Closed

fix(session-memory): deduplicate consecutive assistant messages with identical text#92966
hnlqt666 wants to merge 2 commits into
openclaw:mainfrom
hnlqt666:fix/session-memory-duplicate-thinking-messages

Conversation

@hnlqt666

Copy link
Copy Markdown

When thinking/reasoning is enabled, the session JSONL stores both the original assistant message (with thinking blocks) and a stripped copy (without thinking blocks), causing session-memory hook to produce duplicate entries.

Skip consecutive assistant messages with identical text content to avoid doubling every assistant reply in memory files.

Fixes #92563

Summary

  • When thinking blocks are enabled, session-memory hook writes every assistant reply twice because the JSONL transcript contains both versions.
  • This fix deduplicates consecutive assistant messages that have identical text, so memory files only contain one copy per reply.
  • A user message between two identical assistant texts resets the dedup guard, preserving legitimate repeated replies across turns.

Linked context

Closes #92563

Real behavior proof (required for external PRs)

  • Behavior or issue addressed: Duplicate assistant messages in session-memory output when thinking/reasoning is enabled.
  • Real environment tested: OpenClaw dev environment (Node.js v24.16.0, pnpm 11.2.2) on Windows.
  • Exact steps or command run after this patch: pnpm exec vitest run --config test/vitest/vitest.hooks.config.ts src/hooks/bundled/session-memory/handler.test.ts
  • Evidence after fix: All 25 tests passed (23 original + 2 new).
  • Observed result after fix: No duplicate entries in memory files; non-consecutive identical replies preserved.
  • What was not tested: End-to-end live test with a real thinking-enabled provider session.
  • Proof limitations or environment constraints: Live testing requires thinking-enabled model provider access which was not available. Unit tests cover the dedup logic thoroughly with synthetic JSONL transcripts.

Tests and validation

Which commands did you run?

pnpm exec vitest run --config test/vitest/vitest.hooks.config.ts src/hooks/bundled/session-memory/handler.test.ts

What regression coverage was added or updated?

Added 2 test cases to handler.test.ts:

  • "deduplicates consecutive assistant messages with identical text" — verifies consecutive identical assistant texts are collapsed
  • "does not deduplicate non-consecutive identical assistant texts" — verifies identical texts separated by a user message are both kept

What failed before this fix, if known?

Session-memory output contained duplicate entries for every assistant reply when thinking/reasoning was enabled.

Risk checklist

Did user-visible behavior change? (Yes)

Did config, environment, or migration behavior change? (No)

Did security, auth, secrets, network, or tool execution behavior change? (No)

What is the highest-risk area?

The dedup logic could accidentally drop a legitimate consecutive identical assistant reply.

How is that risk mitigated?

Only skips when the immediately preceding assistant message has identical text. User messages break the chain. Two unit tests validate both paths.

Current review state

What is the next action?

Ready for maintainer review.

…identical text

When thinking/reasoning is enabled, the session JSONL stores both the
original assistant message (with thinking blocks) and a stripped copy
(without thinking blocks), causing session-memory hook to produce
duplicate entries.

Skip consecutive assistant messages with identical text content to
avoid doubling every assistant reply in memory files.

Fixes openclaw#92563
@openclaw-barnacle openclaw-barnacle Bot added size: S triage: mock-only-proof Candidate: PR proof only shows tests, mocks, snapshots, lint, typecheck, or CI. labels Jun 14, 2026
@clawsweeper

clawsweeper Bot commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

Thanks for the context here. I did a careful shell check against current main, and this is already implemented.

Current main already contains the narrower semantic session-memory fix from a merged canonical PR, so this older broad text-dedup branch should close rather than remain a landing candidate.

Root-cause cluster
Relationship: superseded
Canonical: #94401
Summary: This PR is an older candidate for the same session-memory duplicate bug now implemented on current main by the merged canonical PR.

Members:

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

So I’m closing this as already implemented rather than keeping a duplicate issue open.

Review details

Best possible solution:

Keep the merged semantic current-main implementation from #94401 and close this older broad-dedupe branch.

Do we have a high-confidence way to reproduce the issue?

Yes. The original bug is source-reproducible from the transcript reader path: duplicated raw plus delivery-mirror assistant rows become duplicated session-memory lines without the semantic filter.

Is this the best way to solve the issue?

No. This PR is a plausible workaround, but current main’s delivery-mirror-aware filter is the safer and narrower solution because it preserves legitimate same-text assistant rows.

Security review:

Security review cleared: The diff only changes local session-memory transcript filtering and tests; it adds no dependency, workflow, secret, auth, network, or command-execution surface.

AGENTS.md: found and applied where relevant.

What I checked:

Likely related people:

  • SunnyShu0925: Authored the merged canonical PR that added the current delivery-mirror session-memory filter and tests. (role: merged fix author; confidence: high; commits: 1052652a7168; files: src/hooks/bundled/session-memory/transcript.ts, src/hooks/bundled/session-memory/handler.test.ts)
  • sallyom: Pushed the final command-turn reset commit on the merged canonical branch before maintainer acceptance. (role: recent follow-up owner; confidence: high; commits: 28cbe0024711; files: src/hooks/bundled/session-memory/transcript.ts, src/hooks/bundled/session-memory/handler.test.ts)
  • steipete: Introduced the internal hooks system and later runtime internalization connected to session transcript behavior. (role: historical feature contributor; confidence: medium; commits: faba508fe0ae, bb46b79d3c14; files: src/hooks/bundled/session-memory/handler.ts, src/hooks/bundled/session-memory/transcript.ts, src/agents/embedded-agent-runner/replay-history.ts)
  • vincentkoc: Recent shared-runtime and transcript-helper history is connected to the same session-memory reader surface. (role: recent runtime refactor contributor; confidence: medium; commits: c109a7623b13, e085fa1a3ffd; files: src/hooks/bundled/session-memory/transcript.ts, src/shared/transcript-only-openclaw-assistant.ts)
  • Tomas Hajek: Added the rotated transcript fallback used by the same session-memory reader after reset/new flows. (role: adjacent session-memory contributor; confidence: medium; commits: 19ae7a4e17d8; files: src/hooks/bundled/session-memory/handler.ts, src/hooks/bundled/session-memory/handler.test.ts)

Codex review notes: model internal, reasoning high; reviewed against 5a73361ed2b3; fix evidence: commit 1052652a7168, main fix timestamp 2026-06-29T15:19:25Z.

@clawsweeper clawsweeper Bot added 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. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. labels Jun 14, 2026
@clawsweeper

clawsweeper Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

ClawSweeper applied the proposed close for this PR.

@clawsweeper clawsweeper Bot closed this Jul 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: S status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. triage: mock-only-proof Candidate: PR proof only shows tests, mocks, snapshots, lint, typecheck, or CI.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

session-memory hook duplicates assistant messages when thinking is stripped

1 participant