Repository navigation
fix(claude-code): 被停止的任务通知续写不再多出一条用户消息 / attribute interruptions of task-notification prompts - #1199
Conversation
|
No actionable comments were generated in the recent review. Summary
Merge Risk:
|
|
感谢修复 Claude Code 后台任务自动续写被中断后多出假用户消息的问题!改动非常聚焦,合入前麻烦看下两处小边界和 CI:
先标 |
Task notifications carry their own prompt ID. Track it so an interruption marks the notification Turn as cancelled and supplies its checkpoint.
Keep the opening prompt ID and merged notification IDs until the next Turn starts. Clear pending notifications on interruption so the next notification can start a new Turn.
d9950e6 to
f5f2c21
Compare
|
@iruriQ 谢谢评审!两处边界都已修复,分支已变基到最新的
另外,只读重放了本机 156 个 Claude Code transcript:
PR 说明已同步到最新提交。 @iruriQ Thanks for the review! Both edge cases are fixed. The branch is rebased onto the latest
I also ran a read-only replay of 156 local Claude Code transcripts:
The PR description is updated to the latest commits. |
摘要
Claude 收到后台任务的结束通知后,常会自己接着处理,也就是自主续写。这一轮由一条根级 user 记录开始,它带
origin.kind: "task-notification"、promptSource: "system"和自己的promptId(见docs/harnesses/claude-code/claude-code-background-terminals.md)。用户停止这段续写时,Claude Code 写入原生中断记录[Request interrupted by user],这条记录沿用被停止的那条提示的promptId。历史映射原来只记最近一条人类输入的
promptId,所以认不出这条中断记录,把它当成了一个新的用户 Turn。这个 Turn 没有回复,结果是 unknown,显示为已完成。由此出现两个问题:现在
conversationMessages()的做法:promptId在这个集合中,它才归入该 Turn:该 Turn 标为已取消,Checkpoint 为这条中断记录,与人类输入被中断时的处理相同。pendingNotification,之后到来的通知按原规则单独开轮,不会并入已经取消的 Turn。promptId的人类输入不记录 ID,与之前一样,之后的中断记录不归入任何 Turn。真实用户输入即使引用了这段文字,也仍然单独成轮(既有用例覆盖)。为什么要记录并入的通知:在本机真实的 transcript 中,被停止的续写,其中断记录复用的正是一条并入当前 Turn、没有开新轮的通知的
promptId(pendingNotification已经为 true)。只记开新轮的记录,这个问题会回来;只记最近一条记录,后台任务占用的人类 Turn 中途并入通知后,人类提示的中断会认不出来。已有 Thread 不需要迁移:
alignSnapshot()按原生 Turn 键对齐,消失的那一轮的映射被丢掉,其他 Turn 的 Host Turn ID 不变。docs/harnesses/claude-code/claude-code-background-terminals.md说明了中断记录的归属规则。关联 Issue
无 / N/A
测试
基于
main(addff944d),macOS 27.2.1(arm64),Node 26.0.0 和 22.22.0:npx vitest run --config tests/vitest.config.js packages/adapters/claude-code/test/claude-history.test.ts packages/adapters/claude-code/test/claude-queued-history.test.ts:50 个通过。it.each覆盖三种情况:人类输入被中断;任务通知续写被中断;中断后的下一条通知单独开轮,并入其中的通知被中断时归入该轮。main的claude-history.ts,识别中断记录的 2 个用例失败。只保留第一个提交的改动时,新增的 4 个断言失败(含后台 Agent 用例:第一个提交曾让并入的通知覆盖人类提示的promptId)。prettier --check通过,eslint没有报错。packages/host-runtime/test/app-server-host.thread-references.test.ts(等待 Host 输出超时),在本分支上本地连续运行 3 次,均通过(每次 4/4)。本 PR 不涉及 host-runtime。main相比,内容只有中断文字的用户 Turn 从 19 个减少到 6 个。没有一个文件的这类 Turn 增加。promptId,在同一 transcript 中没有其他记录使用,按既有规则保持原样。UI Demo
这是示意图,不是实机截图:用修改前后的历史映射、预览计算和 Web 的
threadListTitle()处理同一段合成 transcript(任务通知续写被停止,之后有一轮正常对话),再把得到的标题画成简化的侧栏。Summary
After Claude receives a background task's completion notification, it often keeps working on its own: an autonomous continuation. That Turn starts with a root user record that carries
origin.kind: "task-notification",promptSource: "system"and its ownpromptId(seedocs/harnesses/claude-code/claude-code-background-terminals.md). When the user stops the continuation, Claude Code writes the native interruption record[Request interrupted by user], which reuses thepromptIdof the prompt it stops.History mapping used to track only the latest human prompt's
promptId, so it did not recognize the record and turned it into a new user Turn. That Turn has no reply, so its outcome is unknown and it shows as completed. This causes two problems:What
conversationMessages()does now:promptIdis in that set. That Turn ends cancelled and its checkpoint is the interruption record, the same handling as an interrupted human prompt.pendingNotification, so the next notification starts its own Turn under the existing rules instead of joining the cancelled Turn.promptIdrecords no ID, as before, so a later interruption record joins no Turn. A real user prompt that quotes the text still gets its own Turn (covered by an existing test).Why merged notifications count: in real local transcripts, the interruption record of a stopped continuation reuses the
promptIdof a notification that was merged into the current Turn without starting one (pendingNotificationwas already true). Tracking only records that start a Turn would bring the problem back. Tracking only the latest record would lose a human prompt's interruption when a notification merges into a Turn held by background work.Existing Threads need no migration:
alignSnapshot()aligns Turns by native Turn key. The removed Turn's mapping is dropped, and the other Turns keep their Host Turn IDs.docs/harnesses/claude-code/claude-code-background-terminals.mddescribes where history puts an interruption record.Related issues
N/A
Test plan
On
main(addff94), macOS 27.2.1 (arm64), Node 26.0.0 and 22.22.0:npx vitest run --config tests/vitest.config.js packages/adapters/claude-code/test/claude-history.test.ts packages/adapters/claude-code/test/claude-queued-history.test.ts: 50 passed.it.eachthat recognizes interruption records covers three situations: an interrupted human input; an interrupted task-notification continuation; after an interruption, the next notification starts its own Turn, and an interruption of a notification merged into it joins that Turn.main'sclaude-history.ts, the 2 interruption-recognition cases fail. With only the first commit's change, the 4 new assertions fail, including the background Agent case: the first commit let a merged notification replace the human prompt'spromptId.prettier --checkpasses andeslintreports no errors on the changed files.packages/host-runtime/test/app-server-host.thread-references.test.ts, which failed in the previous CI run (timed out waiting for Host output), passed 3 runs in a row locally on this branch (4/4 each). This PR does not touch host-runtime.main, user Turns whose only text is an interruption marker drop from 19 to 6. No file gains such a Turn.promptIdthat no other record in the same transcript carries, so the existing rule leaves them unchanged.UI Demo
This is a schematic, not a screenshot. The old and new history mapping, preview computation and the Web's
threadListTitle()processed the same synthetic transcript (a stopped task-notification continuation followed by a normal exchange), and the resulting titles were drawn as a simplified sidebar.