Skip to content

fix(claude-code): 被停止的任务通知续写不再多出一条用户消息 / attribute interruptions of task-notification prompts - #1199

Merged
iruriQ merged 2 commits into
BytePioneer-AI:mainfrom
sduhantao-maker:fix/claude-notification-interruption
Nov 29, 2026
Merged

iruriQ merged 2 commits into
BytePioneer-AI:mainfrom
sduhantao-maker:fix/claude-notification-interruption

Conversation

@sduhantao-maker

@sduhantao-maker sduhantao-maker commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

摘要

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,显示为已完成。由此出现两个问题:

  • 历史里多出一条内容为「[Request interrupted by user]」的用户消息,被停止的续写反而显示为已完成。
  • 如果这个假 Turn 是 Thread 中第一个已完成的 Turn,保存的预览就会变成「[Request interrupted by user]」。没有标题的 Thread(例如从这个对话 Fork 出的侧边聊天)在 Web 侧栏中就用这句话作标题。

现在 conversationMessages() 的做法:

  • 记录当前 Turn 的提示 ID:开启该 Turn 的输入(人类输入或任务通知),加上并入该 Turn、但没有开新轮的通知。新 Turn 开始时清空。
  • 只有中断记录的 promptId 在这个集合中,它才归入该 Turn:该 Turn 标为已取消,Checkpoint 为这条中断记录,与人类输入被中断时的处理相同。
  • 中断记录同时复位 pendingNotification,之后到来的通知按原规则单独开轮,不会并入已经取消的 Turn。
  • 没有 promptId 的人类输入不记录 ID,与之前一样,之后的中断记录不归入任何 Turn。真实用户输入即使引用了这段文字,也仍然单独成轮(既有用例覆盖)。

为什么要记录并入的通知:在本机真实的 transcript 中,被停止的续写,其中断记录复用的正是一条并入当前 Turn、没有开新轮的通知的 promptId(pendingNotification 已经为 true)。只记开新轮的记录,这个问题会回来;只记最近一条记录,后台任务占用的人类 Turn 中途并入通知后,人类提示的中断会认不出来。

已有 Thread 不需要迁移:

  • 下次读取历史时,alignSnapshot() 按原生 Turn 键对齐,消失的那一轮的映射被丢掉,其他 Turn 的 Host Turn ID 不变。
  • Checkpoint 变化只改写映射,不算会话活动。
  • 保存的预览也在下次读取历史时更新。

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 覆盖三种情况:人类输入被中断;任务通知续写被中断;中断后的下一条通知单独开轮,并入其中的通知被中断时归入该轮。
    • 后台 Agent 占用的人类 Turn 用例覆盖:并入通知之后,人类提示的中断仍归入原 Turn。
    • 换回 main 的 claude-history.ts,识别中断记录的 2 个用例失败。只保留第一个提交的改动时,新增的 4 个断言失败(含后台 Agent 用例:第一个提交曾让并入的通知覆盖人类提示的 promptId)。
  • 改动文件 prettier --check 通过,eslint 没有报错。
  • 上一版 CI 中失败的 packages/host-runtime/test/app-server-host.thread-references.test.ts(等待 Host 输出超时),在本分支上本地连续运行 3 次,均通过(每次 4/4)。本 PR 不涉及 host-runtime。
  • 只读回放本机 156 个 Claude Code transcript,修改前后的历史映射都能处理全部文件:
    • 与 main 相比,内容只有中断文字的用户 Turn 从 19 个减少到 6 个。没有一个文件的这类 Turn 增加。
    • 状态改变的 Turn 恰好 13 个,全部由 unknown 变为已取消,其余 Turn 不变。
    • 第二个提交没有改变这批 transcript 的结果。
    • 剩下 6 条中断记录所用的 promptId,在同一 transcript 中没有其他记录使用,按既有规则保持原样。
  • 未测试:在 Desktop 或 Web 中实际停止一段续写后重新打开会话;Windows。

UI Demo

修复前侧栏标题是 [Request interrupted by user],修复后是该对话第一条已完成的用户消息

这是示意图,不是实机截图:用修改前后的历史映射、预览计算和 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 own promptId (see docs/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 the promptId of 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:

  • History shows an extra user message reading "[Request interrupted by user]", while the stopped continuation shows as completed.
  • If that fake Turn is the Thread's first completed Turn, the stored preview becomes "[Request interrupted by user]". Untitled Threads, such as side chats forked from this conversation, then use that text as their title in the Web sidebar.

What conversationMessages() does now:

  • It keeps the prompt IDs of the current Turn: the input that started it (a human input or a task notification), plus notifications merged into it without starting a new Turn. The set is cleared when a new Turn starts.
  • An interruption record joins the Turn only when its promptId is in that set. That Turn ends cancelled and its checkpoint is the interruption record, the same handling as an interrupted human prompt.
  • An interruption record also resets pendingNotification, so the next notification starts its own Turn under the existing rules instead of joining the cancelled Turn.
  • A human input without a promptId records 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 promptId of a notification that was merged into the current Turn without starting one (pendingNotification was 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:

  • On the next history read, alignSnapshot() aligns Turns by native Turn key. The removed Turn's mapping is dropped, and the other Turns keep their Host Turn IDs.
  • The checkpoint change only rewrites a mapping and does not count as conversation activity.
  • The stored preview is also updated on the next history read.

docs/harnesses/claude-code/claude-code-background-terminals.md describes 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.
    • The it.each that 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.
    • The human Turn held by a background Agent covers this: after a notification merges in, the human prompt's interruption still joins the original Turn.
    • With main's claude-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's promptId.
  • prettier --check passes and eslint reports 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.
  • A read-only replay of 156 local Claude Code transcripts. The old and new mapping both process every file:
    • Compared with main, user Turns whose only text is an interruption marker drop from 19 to 6. No file gains such a Turn.
    • Exactly 13 Turns change status, all from unknown to cancelled. All other Turns are unchanged.
    • The second commit does not change the results for these transcripts.
    • The 6 remaining interruption records use a promptId that no other record in the same transcript carries, so the existing rule leaves them unchanged.
  • Not tested: stopping a continuation in Desktop or Web and reopening the Thread; Windows.

UI Demo

Before: the sidebar title is [Request interrupted by user]; after: it is the conversation's first completed user message

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.

@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • 改进 Claude Code 中断记录的历史归属:任务通知及批量任务通知关联的回合会正确标记为已取消,并保留回合身份与检查点。
    • 修正 Agent 延续场景中的中断归属;用户中断提示不会单独显示为用户回合。
  • Documentation
    • 补充 Claude Code 中断记录与历史回放行为的说明。

Claude Code 历史解析器现在按 Turn 累积多个 promptId,并使用该集合识别中断记录。中断时,解析器清除待处理通知状态。测试覆盖通知及占用中延续场景,文档说明中断记录的回放归属。

Changes: 中断记录回放

Layer / File(s) Summary
提示 ID 关联与中断回放
packages/adapters/claude-code/src/claude-history.ts, packages/adapters/claude-code/test/claude-history.test.ts, docs/harnesses/claude-code/claude-code-background-terminals.md
解析器将当前 Turn 的提示 ID 存入集合,并用集合匹配中断记录。新 Turn 开始时会清空集合;中断时会清除待处理通知状态。测试覆盖任务通知、批量任务通知和占用中延续关联的取消回合及检查点。文档说明中断记录归入关联 Turn,且中断文本不单独显示为用户 Turn。

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

可能相关的 PR

  • BytePioneer-AI/codex-host#892:该 PR 修改 Claude Code 历史恢复中的 Turn checkpoint;本 PR 也修改取消 Turn 的归属和 interruption record 的 checkpoint。

Suggested reviewers: air-hl, yanyuncy, lpmasser

Merge Risk: Minimal · up to f5f2c

This change attributes Claude Code interruption records to the Turn they stopped, so they no longer show up as separate user Turns or as Thread previews. No concrete merge-blocking risk was found. Desktop/Web stop-and-reopen behavior and Windows were not tested.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed 标题准确概括了主要变更:修复被停止的任务通知续写产生多余用户消息的问题,并在 claude-history 中把中断记录归入被停止的轮次。标题与变更集相关。标题中使用了中英双语,但内容与改动一致。
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed PR 描述与变更内容直接相关。描述说明了 Claude Code 历史映射如何识别后台任务通知续写被中断时的 [Request interrupted by user] 记录,并说明 conversationMessages() 的 promptId 集合逻辑、pendingNotification 复位、无需迁移的原因、测试计划和文档位置。这些内容与原始摘要中的文档、claude-his…
Full details: Description check

Explanation

PR 描述与变更内容直接相关。描述说明了 Claude Code 历史映射如何识别后台任务通知续写被中断时的 [Request interrupted by user] 记录,并说明 conversationMessages() 的 promptId 集合逻辑、pendingNotification 复位、无需迁移的原因、测试计划和文档位置。这些内容与原始摘要中的文档、claude-history.ts 和测试文件改动一致。描述包含具体的测试数据和设计理由,信息充分。

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@iruriQ iruriQ added the awaiting-author Waiting for the author to respond or update the PR. label Nov 26, 2026
@iruriQ

iruriQ commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

感谢修复 Claude Code 后台任务自动续写被中断后多出假用户消息的问题!改动非常聚焦,合入前麻烦看下两处小边界和 CI:

  1. pendingNotification 与 promptId 边界:claude-history.ts 中 if (interrupted) 分支未复位 pendingNotification = false(连续后台通知且无 assistant 回复时可能误并入已取消 Turn);另外非起轮的 notification 也会无条件覆盖 promptId,可能影响原人类轮次的中断归属。
  2. CI Vitest 失败:请看下当前失败的用例并修绿。

先标 awaiting-author,改完随时告诉我们。

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.
@sduhantao-maker
sduhantao-maker force-pushed the fix/claude-notification-interruption branch from d9950e6 to f5f2c21 Compare November 29, 2026 11:38
@sduhantao-maker

Copy link
Copy Markdown
Contributor Author

@iruriQ 谢谢评审!两处边界都已修复,分支已变基到最新的 main(addff944d,没有冲突),新增提交 f5f2c21。

  1. pendingNotification:意见成立。中断记录现在会同时复位 pendingNotification。之后到来的通知按原规则单独开轮,不会再并入已经取消的 Turn。
  2. 不开新轮的通知覆盖 promptId:意见成立。现在不再只记一个 promptId,而是记录当前 Turn 的提示 ID:开启该 Turn 的输入,以及并入该 Turn、但没有开新轮的通知。新 Turn 开始时清空。只有中断记录的 promptId 在这个集合中,中断才归入该 Turn。
    • 没有改成「只记开新轮的通知」。原因是在本机真实的 transcript 中,被停止的续写,其中断记录复用的正是一条并入当前 Turn、没有开新轮的通知的 promptId。
    • 测试都扩展自原有用例,覆盖三种情况:
      • 中断之后,下一条通知单独开轮;
      • 并入的通知被中断时,中断归入该 Turn;
      • 后台任务占用的人类 Turn 中并入了通知,人类提示的中断仍归入原 Turn。
    • 去掉这次的源码改动后,这 4 个断言(两种中断文字、两种 Agent 配置)失败;加上改动后,claude-history 与 claude-queued-history 共 50 个用例通过。
  3. CI Vitest:失败的是 packages/host-runtime/test/app-server-host.thread-references.test.ts 中的「reads original Host-qualified references through the CLI after turn/start from desktop」,报错 Timed out waiting for Host output(约 2.7 秒)。
    • 本 PR 不涉及 host-runtime。
    • 在变基后的分支上,本地连续运行该文件 3 次,均通过(每次 4/4),没有复现超时。
    • 新的推送会重新运行 CI。

另外,只读重放了本机 156 个 Claude Code transcript:

  • 与 main 相比,内容只有中断文字的用户 Turn 从 19 个减少到 6 个;
  • 13 个 Turn 由 unknown 变为 cancelled;
  • 没有任何文件新增这类 Turn;
  • 结果与原 PR 提交相同。

PR 说明已同步到最新提交。


@iruriQ Thanks for the review! Both edge cases are fixed. The branch is rebased onto the latest main (addff944d, no conflicts), with a new commit, f5f2c21.

  1. pendingNotification: valid. An interruption record now also resets pendingNotification. The next notification starts its own Turn under the existing rules instead of joining the cancelled Turn.
  2. A notification that starts no Turn overwriting promptId: valid. Instead of a single promptId, the mapper now keeps the prompt IDs of the current Turn: the input that started it, plus notifications merged into it without starting a new Turn. The set is cleared when a new Turn starts. An interruption joins the Turn only when its promptId is in that set.
    • I did not limit it to notifications that start a Turn. In real local transcripts, the interruption record of a stopped continuation reuses the promptId of a notification that was merged into the current Turn without starting one.
    • The tests extend existing cases and cover three situations:
      • after an interruption, the next notification starts its own Turn;
      • an interruption of a merged notification joins that Turn;
      • in a human Turn held by background work with a merged notification, the human prompt's interruption still joins the original Turn.
    • Without this commit's source change, these 4 assertions (two interruption texts, two Agent configurations) fail. With it, claude-history and claude-queued-history pass 50/50.
  3. CI Vitest: the failing case was "reads original Host-qualified references through the CLI after turn/start from desktop" in packages/host-runtime/test/app-server-host.thread-references.test.ts, which reported Timed out waiting for Host output (about 2.7 s).
    • This PR does not touch host-runtime.
    • On the rebased branch, that file passed 3 runs in a row locally (4/4 each). The timeout did not reproduce.
    • The new push reruns CI.

I also ran a read-only replay of 156 local Claude Code transcripts:

  • compared with main, user Turns whose only text is an interruption marker drop from 19 to 6;
  • 13 Turns change from unknown to cancelled;
  • no file gains such a Turn;
  • the results match the original PR commit.

The PR description is updated to the latest commits.

@iruriQ iruriQ removed the awaiting-author Waiting for the author to respond or update the PR. label Nov 29, 2026
@iruriQ
iruriQ merged commit 53ee897 into BytePioneer-AI:main Nov 29, 2026
7 checks passed
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.

2 participants