Skip to content

fix(long-task-reminder): 修复非结构化 PTY CLI 完成后误报长任务提醒并清洗卡片标题 - #3767

Merged
deepcoldy merged 1 commit into
deepcoldy:masterfrom
Han-wangyu:fix/long-task-reminder-pty-lifecycle
Nov 24, 2026
Merged

deepcoldy merged 1 commit into
deepcoldy:masterfrom
Han-wangyu:fix/long-task-reminder-pty-lifecycle

Conversation

@Han-wangyu

Copy link
Copy Markdown
Contributor

背景与问题

开启 longTaskReminder(如 thresholdMinutes: 10、intervalMinutes: 10)后,使用非结构化桥接 PTY CLI(例如默认终端模式下的 antigravity,以及 kimi、opencode、copilot、dsh-tui 等)会在轮次已回复并回到空闲提示符后持续误发橙色「任务仍在执行」提醒卡片;同一话题多轮对话中每轮都会泄漏一条未关闭的 replyExecutionStarts 记录,导致多个历史轮次交错触发重复提醒,直到空闲休眠杀掉 worker 才停止。

同时,提醒卡片正文直接渲染原始 session.title:群话题场景下标题以 @<机器人名> 开头并按 50 字符截断,机器人名较长时卡片标题几乎全被 @mention 占满且句中无省略号截断。

根因

  1. 执行时钟生命周期不对称:src/worker.ts 在所有 CLI 的真实写入点都会发 turn_execution_started 写入 ds.replyExecutionStarts,但此前只有 case 'turn_terminal' 会把记录标为 completed = true。对 sessionUsesTurnTerminal(ds, botCfg) === false 的纯 PTY CLI,正常完成只会走 prompt_ready + screen_update(status='idle')(以及 botmux send --response-kind final 写 turn-sends/<sessionId>.jsonl),不会发 turn_terminal,导致已结束轮次一直滞留在 runningLongTasks(session) 中。
  2. steered 轮次未退休时钟:case 'thinking_superseded' 已调用 cliNotices.finish 和 settleAcceptedInputTurn,但未把被合并轮次在 replyExecutionStarts 中标为 completed = true。
  3. 标题未做展示清洗:longTaskReminderCard 直接展示 task.session.session.title || '当前任务'。

修改内容

1. 补齐执行时钟退休语义(src/core/worker-pool.ts、src/core/types.ts)

  • 抽出 retireReplyExecutionStarts 与 settleNonTerminalReplyExecutionsOnIdle,并导出复用既有单一事实源 sessionUsesTurnTerminal(ds, botCfg?)(底层委托 hasTurnTerminalCapability)。
  • screen_update 空闲结算(仅 !sessionUsesTurnTerminal(ds, botCfg)):当 ds.lastScreenStatus === 'idle' 且无 backgroundTaskCount 时,在发布 Dashboard patch 之前退休打开的执行时钟。若帧携带 msg.turnId 命中已追踪轮次,仅退休该轮及不晚于它的更早开始记录,避免延迟的异步空闲截屏误杀刚启动的新一轮;否则仅在 working/analyzing → idle 边沿退休。
  • turn_execution_started 覆盖旧轮(仅 !sessionUsesTurnTerminal(ds, botCfg)):单轮串行 PTY CLI 在新一轮开始时退休同 generation 下更早的未完成记录,避免中间漏掉空闲帧(如排队追问/打断)时历史轮次泄漏。
  • 区分权威终态与启发式空闲:在 replyExecutionStarts 条目上增加 completedByTerminal?: boolean。权威终态(turn_terminal / thinking_superseded)写入 tombstone 并永久阻止同 attempt 复活;启发式空闲退休仍拦截重复/更早时间戳,但允许同 generation 上时间戳严格更新的重放/真实写入重新打开时钟(覆盖进程内 CLI 重启重放与 init 后 flushPending 真实写入)。
  • thinking_superseded 结算:同步退休被合并轮次的执行时钟并写 tombstone。

2. 资格判定纵深防御(src/core/long-task-reminder-eligibility.ts)

  • 对 !sessionUsesTurnTerminal(session) 的会话,若已处于 lastScreenStatus === 'idle' 且无 backgroundTaskCount,拒绝发送提醒。
  • 新增 hasExplicitFinalSendForTurn:仅在超过阈值进入 canSendLongTaskReminder 时按尾 64KB 读取 turn-sends/<sessionId>.jsonl,经 finalBridgeSendMarkerCoversOwnThread 校验本会话显式 --response-kind final 已送达且满足 dispatchAttempt / startedAtMs 隔离边界后拦截提醒(不污染运行中的 sessionExecutionActivity)。

3. 清洗提醒卡片标题(src/services/long-task-reminder-notification.ts)

  • 新增并导出 formatLongTaskReminderTitle(rawTitle?, botName?, maxChars = 40):
    • 去除开头的 @<botName>(支持含空格机器人名、中英文标点分隔、无空格中英文边界,以及 50 字符截断在机器人名中间的场景)与通用 @mention 前缀;
    • 保留 @scope/pkg 类包名与 <at id=all> 纯文本(卡片仍用 plain_text 防注入);
    • 折叠空白与换行,纯 mention/空串回退为 当前任务;
    • 按 Unicode 码点安全截断至 40 字符并补 …。

影响范围评估

  • 有权威终态的 CLI(claude-code、codex、codex-app、traex、grok、pi、oh-my-pi、ebsd、coco、hermes、mtr、doubao、cursor 非 scratch、relay/seed、mira/mir/dsh official、minimax、antigravity ACP / zero-prompt):sessionUsesTurnTerminal 返回 true,空闲帧与新轮覆盖分支均不生效,仍完全由 turn_terminal(及 thinking_superseded)结算;瞬态 idle 不会误取消提醒。
  • 无权威终态的 PTY CLI(antigravity 默认终端模式、kimi、opencode、copilot、kiro-cli、zcode、aiden、reasonix、dsh-tui、cursor scratch):空闲回归与新轮提交均正确退休旧时钟,停止误报提醒并同步消除 Dashboard/监控侧过期的运行中活动状态。
  • 回复耗时渲染(getDaemonReplyCardRenderSnapshot):retireReplyExecutionStarts 保留原条目的 startedAtMs 而不删除,不影响回复耗时计算。
  • 配置与数据兼容性:无配置 schema 变更,无需迁移。

对抗复核问题清单与修法

提交前围绕并发/时序交叠、跨会话边界与标题清洗做了针对性对抗排查,发现并随本 PR 一并修正了 5 处边界问题:

  1. [P1] explicit_reply_observed 缺少 dispatchAttempt 与本会话校验,直接退休时钟会误杀重试 attempt 并清空运行中 sessionExecutionActivity:移除 case 'explicit_reply_observed' 对 replyExecutionStarts 的退休与 tombstone 写入,统一由 canSendLongTaskReminder 的 hasExplicitFinalSendForTurn 在资格判定层执行本会话 + attempt/时间戳精确校验。
  2. [P1] 非权威空闲退休后 turn_execution_started 误把 completed === true 当成永久 tombstone,导致进程内 CLI 重启重放或 init→idle→flushPending 真实写入被丢弃:引入 completedByTerminal 区分权威终态与启发式空闲;后者仅拦截 startedAtMs <= previousExecution.startedAtMs 的重复/滞后事件,放行严格更新的真实写入/重放,并补齐回归用例。
  3. [P1] 标题清洗通用正则遇到无空格中文标点(如 @SomeBot,帮我查一下线上502原因)会吞掉正文,@Bot: 正文 会残留冒号,多词 @BotName中文 边界漏匹配:收窄通用 @mention 边界至中英文标点/空白、消费 @<botName> 后的分隔标点并保留左括号、支持 ASCII 词尾与非 ASCII 正文边界,补充全部回归用例。
  4. [P2] sessionUsesTurnTerminal 在 botCfg 未就绪回退分支缺少 adapter,以及 turn_execution_started 未透传作用域内的 botCfg:两处均补齐 botCfg / createCliAdapterSync 兜底,避免 Claude 系分叉适配器在回退路径误判。
  5. [P2] hasExplicitFinalSendForTurn 单测使用了非标准 deliveryKind,且无 dispatchAttempt 标记在缺失 startedAtMs 时可能误拦带版本号的重试:单测改用真实的 current_session_send / detoured_send,并对带 dispatchAttempt 的轮次要求精确匹配或可证明的 sentAtMs >= startedAtMs。

测试验证

  • bun run build(含 audit:domains、tsc、typecheck、dashboard:bundle、audit-dist、audit-embedded-assets):全部通过(exit 0)
  • Vitest 定向回归与卫生检查:
    bun run test -- \
      test/long-task-reminder.test.ts \
      test/long-task-reminder-delivery.test.ts \
      test/long-task-reminder-eligibility.test.ts \
      test/long-task-reminder-adversarial.test.ts \
      test/long-task-reminder-lifecycle-probe.test.ts \
      test/test-hygiene.test.ts \
      test/execution-activity.test.ts \
      test/reply-render-snapshot-console.test.ts \
      test/worker-pool-cli-progress.test.ts
    结果:9 passed (9),138 passed (138)
  • Bun 逐文件隔离测试(对齐 CI bun-test 路径):
    for f in \
      test/long-task-reminder.test.ts \
      test/long-task-reminder-delivery.test.ts \
      test/long-task-reminder-eligibility.test.ts \
      test/long-task-reminder-lifecycle-probe.test.ts; do
      bun test "$f" || exit 1
    done
    结果:4 个文件共 50 pass, 0 fail

@deepcoldy deepcoldy left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

复审结论:代码可合,新增 2 条非阻断边界观察

复审对象:作者 head 7345d63eb0;本地 rebase 后 head 7168518456。作者提交与 rebase 提交的 stable patch-id 均为 2af102aa231b170a0bbd22de7beddbdcd2ce16cb,确认评审的是同一份净改动。

未发现新的 P1/P2。核心退休路径、权威 tombstone 与启发式退休的复活差异、turn-terminal CLI 的能力门、explicit final 的 attempt/时间窗隔离均有行为测试覆盖。以下两项建议合后 follow-up,不挡本 PR。

P3:已配置的中文机器人名遇到无分隔正文时,标题会退化为「当前任务」

位置:src/services/long-task-reminder-notification.ts:38-58。

当 botName='机器人'、标题为 @机器人帮我查502 时,精确 bot 前缀虽然匹配,但因为机器人名不是 ASCII 词尾、下一字符又是中文,boundary 不成立;随后通用 mention 正则把整段无空白文本都当成 mention,最终返回 当前任务,正文全部丢失。

真实函数探针结果:

formatLongTaskReminderTitle('@机器人帮我查502', '机器人')
预期:帮我查502
实际:当前任务

建议让已配置 bot 的精确前缀匹配优先于通用 mention 剥离,至少在该边界无法确认时保留原文而不是再交给通用正则吞掉,并补无空格 CJK 正文回归。

P3:explicit-final 证据超出 64 KiB 尾部窗口后会重新具备提醒资格

位置:src/core/long-task-reminder-eligibility.ts:31-39。

hasExplicitFinalSendForTurn 只搜索 journal 尾部 64 KiB。同一执行已发送 final 后,如果后续 progress/marker 累积超过该窗口且 CLI 仍未进入可退休的空闲状态,final 证据会从搜索窗口消失,后续扫描可能重新发送「任务仍在执行」。

使用真实 recordBridgeReplyMarker + canSendLongTaskReminder 的探针:先写本会话、本话题、同 turn 的 final 时返回 false;再追加 20 条各含 4000 字符正文的 progress marker 后,返回值变为 true。正常完成后的 idle 退休仍提供主防线,因此这是高活动量且 final 后继续运行的边角,不列为阻断。后续可考虑复用精确的 per-turn final receipt/index,或按块向前查找,而不是只保留固定尾部语义。

实际验证

  • bun run build:通过,含 domain audit、TypeScript/typecheck、Dashboard bundle、dist/embedded-assets audits。
  • 定向 Vitest:10 文件、180 测试全绿(PR 相关 9 文件加 background-wait-card)。
  • 全部 test/long-task-reminder*.test.ts:5 文件、56 测试全绿。
  • 追加 screen-idle / worker-input 相关定向测试:4 文件、63 测试全绿。
  • Bun 运行时腿:4 个 long-task-reminder 文件,共 50 pass、0 fail。
  • 临时对抗探针另确认:构造「同 turn 新 attempt + 无 attempt 的迟到 idle」会退休新 attempt;但当前 worker 所有正常带 turnId 的 screen_update 发送点均透传当前 dispatchAttempt,异步 sampler 也按 attempt 钉住并丢弃跨 attempt 捕获,所以这里只记录为理论兼容性防御缺口,不作为当前版本的可达回归。

工作树已恢复干净,未修改作者提交。GitHub CI 当前仍有多个 job 排队;test (2/4)、两个编译二进制 smoke、network boundary 已通过,合并仍以剩余 checks 全绿为准。

@deepcoldy
deepcoldy merged commit 5d8c7d4 into deepcoldy:master Nov 24, 2026
16 checks passed
@github-actions

Copy link
Copy Markdown

🚀 Released in v3.59.32

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