Skip to content

fix(cli): trim prompts before appPrompt dedupe match - #1750

Open
xiaojingyu812 wants to merge 1 commit into
slopus:mainfrom
xiaojingyu812:fix/cli-app-prompt-dedupe-trailing-newline
Open

fix(cli): trim prompts before appPrompt dedupe match#1750
xiaojingyu812 wants to merge 1 commit into
slopus:mainfrom
xiaojingyu812:fix/cli-app-prompt-dedupe-trailing-newline

Conversation

@xiaojingyu812

Copy link
Copy Markdown

Problem

In remote (daemon-spawned) sessions, every prompt sent from the app is persisted twice on the server: once by the app itself, and once again by the remote-mode JSONL scanner re-forwarding it.

Observed on live sessions (see my comment on #914 for the full investigation): decrypting the stored history shows two consecutive role: 'user' messages with identical text — e.g. seq=9 "回复ok" (sentFrom: ios) and seq=14 "回复ok" (sentFrom: cli).

Root cause

runClaude.ts dedupes app-sent prompts against the scanner by exact content match:

  • recordAppPrompt() receives the app-delivered text: "回复ok"
  • consumeAppPrompt() receives the JSONL-echoed text the SDK wrote: "回复ok\n" (trailing newline)

The exact match misses, the scanner treats the prompt as terminal-typed, and forwards it again.

Fix

Extract the ring buffer into appPromptDedupe.ts (behavior otherwise unchanged: 5-minute window, consume-once semantics, stale entries roll off) and trim on both record and consume so the comparison ignores the SDK's trailing newline. The existing empty-line guard in the scanner already uses trim semantics, so this is consistent with how "same prompt" is defined there.

Tests

Six unit tests in appPromptDedupe.test.ts, including the trailing-newline regression case and the 5-minute expiry window (vitest run src/claude/appPromptDedupe.test.ts → 6/6 passing).

The remote-mode JSONL scanner dedupes app-sent prompts by exact content
match, but the SDK writes the prompt to the JSONL with a trailing
newline while the app delivers it without one. The match misses and the
scanner re-forwards the prompt, persisting the user's message twice on
the server (observed on live sessions as duplicate consecutive user
messages with identical text).

Extract the ring buffer into appPromptDedupe.ts, trim on both record
and consume, and add unit tests including the trailing-newline
regression.
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