crash-continue 崩溃续跑

宿主崩溃 / 断电后,仍留有「用户消息已落盘但 turn 没有走到 turn/end」的会话。 crash-continue 插件负责把这种半完成状态恢复为一条可解释的提示或一次安全的 自动续跑,避免无声丢失或重复回放工具副作用。对应实现: extensions/longe-plugin-crash-continue/(out-of-tree 插件包, 经 longe-plugin.json 声明)。本页是规格说明,详细设计见 public/superpowers/plans/2026-10-21-crash-continue-plugin.md。

核心不变式

不变式说明
无副作用重复同一 (session, digest, turn) 有持久 crash-continue/claims 记录:create-only 成功才可算 attempt 序号/写 attempt;活 owner → claim-held,死 owner → etag CAS 接管后重查 attempt 指针。delivered 或任意 epoch pending 都先 reconcile/复用,仍未知则拒绝新 attempt(不换 admission key)
无副作用回放恢复调用只重放 read-only 白名单工具;非白名单/身份 mismatch 一律转提示
决策可否决PromptOnly 下用户可拒;dismiss 后同 (session, header 摘要, turn) 不再重报,列表与横幅都过滤已 dismissed 项
主机隔离其他宿主仍持有 session.lock 时不接管:attempt 记 not-delivered(locked) 并退还预算,不清除对方的 hint
结算单调attempt outcome 用 etag CAS 写;既有终态先行者胜,冲突重读收敛,绝不 expectation:null 覆盖

检测信号(durable 尾部)

判定一个会话「中断」依赖三类 durable 事实:

  • turn/start 之后没有对应 turn/end(open turn 未闭合);
  • turn/end{reason=aborted}(上一宿主标记 turn 被中断关闭);
  • assistant/message{interrupted=true}(生成中途中断的流截断)。

插件在挂载时先把「本进程 epoch」写进 hint,再按 SessionProjection 会话事件订阅与 ISessionQuery 慢速全量重列双通道发现候选——subscribe-before-snapshot 保证 HMR 重激活窗口内不漏事件。无 hint 的冷会话需经一段稳定期(StabilityWindowMs,默认 2s)

  • EventCount 不变两次确认后才进入确认管道,避免把正在写入的会话误判为「中断」。

两种模式

模式Mode行为
prompt-only(默认)crash-continue.mode=prompt-only发 crash-continue/interrupted 提示;不落 attempt intent、不调 ResumeAsync、不给会话追加任何 closer 事件
auto-resumecrash-continue.mode=auto-resume写 attempt intent → 预算 CAS 占额 → 冷启动 ResumeAsync:F4 在场时由 driver 在原回合内续跑(重放白名单 deferred 调用);F4 缺席时按 #650 默认语义合成 closer 闭合被中断回合,再投递 follow-up

PromptOnly 是只读的:重启只做检查与提示。AutoResume 每次发起都先落 attempt intent、CAS 占额,再走真实的 resume 通道——失败按预算退款;simple 投递只有 AdmitDurableAsync 返回且 confirmDurable flush ACK 完成后才看结果:Pending 与 Consumed 都记 delivered(消息已 durable 入队),纯观察 durable admission 记录 时仍只有 Consumed 能证明 delivered。F4 attempt 则由 driver 写出的 turn/end 作为 delivered 证据(见下节)。

判定顺序(固定十条)

evaluate 十条件按固定顺序短路,与 CrashContinuePolicy.Evaluate 一一对应:

  1. 本进程已在跑此会话 ⇒ skip(live-here;用户点「继续」豁免)
  2. 该会话归属子任务 ⇒ skip(subagent)
  3. 本 (session, header 摘要, turn) 已被 dismiss ⇒ skip(dismissed)
  4. open turn 内有 goal 消息 ⇒ promptOnly(goal-rearm-required)
  5. 有取消证据(hint stopRequested 或 open turn 内 assistant/message{interrupted})⇒ promptOnly(cancel-requested)
  6. authority 在场但 P4 策略注册失败 ⇒ promptOnly(policy-conflict)
  7. Mode != "auto-resume" ⇒ promptOnly(prompt-only-mode;用户点「继续」豁免)
  8. 预算耗尽 ⇒ promptOnly(budget-exhausted)
  9. turn 内有非白名单未决工具调用 ⇒ promptOnly(non-whitelist-pending-call)
  10. 全部通过 ⇒ autoResume

goal 先于 cancel,cancel 先于策略冲突——同一会话只会落到一个判定分支。 用户点「继续」从第 8 条起判定(跳过 1 与 7),但 2–6 照拦;⑨ 白名单同样照拦。 所有非 AutoResume 判决都返回 409 且零投递副作用。durable 上该回合已由 driver 正常闭合(Completed|Aborted|…)时,端点额外返回 409 turn-closed——只有合成 closer(Interrupted|Forked)或仍 open 的回合可以被「继续」。

F4 协同

AgentLoopOptions.EnableTurnResume 打开时 AgentLoop 提供 ITurnResumeAuthority;crash-continue 看到 Enabled=true 后 fork 子作用域注册 自己的 ITurnResumePolicy,attempt 因此记 mode=f4:

  • DecideAsync 只对「本 epoch 正在发起的 attempt」做 (SessionId, HeaderDigest, Turn) 精确匹配 → Resume;其余一律 Close。P4 永不返回 Prompt——保留 open tail 会让会话卡死在「上一条消息发不出去」的状态。
  • Close 决策同样进 decisions 记录,列表显示「打开时已按默认闭合」并保留 「继续」操作;此时「继续」改投 simple follow-up 给已 live 的 agent。
  • 已有其他策略占位导致注册失败 ⇒ policyConflict + policy-conflict 通知 + API 409,绝不 simple 投递(避免 F4 续跑与 follow-up 双投)。
  • 无 authority(EnableTurnResume=false)时没有保留的 open tail:resume 先按 #650 默认语义合成 closer 闭合被中断回合,再由同一条 attempt 投递 follow-up。

预算与退避

MaxResumeAttempts 默认 3,InitialBackoffMs/MaxBackoffMs 控制指数退避。 每次 auto-resume 先 CAS 占额(attemptsSinceLastCleanEnd += 1),发起失败退款; driver 写出六种正常 turn/end 之一即清零(合成 closer 不清零)。账本与会话日志 正交:写在 IStorageHub 的 json backend(domain crash-continue/attempts、 crash-continue/budgets;key 为 {sid}:{turn}:{attempt:D2} 与 {sid}), 会话持久化层不感知这份账本。

前端与通知

PromptOnly 决策按 <sid> 发 crash-continue/interrupted 事件;Web 面板对应 管理面由 CrashContinueWebPlugin 提供:

端点作用
GET /api/crash-continue/interrupted列表(hint + 判定原因 + attempt/退避 + 决策记录)
GET /api/crash-continue/interrupted/{id}详情(hint + open tail 分析 + attempt 历史)
POST /api/crash-continue/interrupted/{id}/resume用户确认:从判定 ⑧ 起。dismissed/subagent/goal/cancel/policy/budget/non-whitelist → 409(reason 原样);driver 已闭合回合 → 409 turn-closed;live 但回合仍 open → 409 live-open-turn;卸载中 → 503 unloading;成功返回 {resumed, duplicate, attempt}
POST /api/crash-continue/interrupted/{id}/dismiss写 dismissal 记录;列表/横幅随后过滤该 (会话, 摘要, 回合)

前端两部分:ClientBundle.js 往 shell.overlay 注入横幅(15s 轮询列表, 「继续 / 忽略 / 详情」),/crash-continue 提供独立管理页;管理页与资源路由是 /crash-continue + /crash-continue/assets/{app.js,style.css},index 用同名绝对 路径引用,不再依赖「/crash-continue 是目录」的相对解析。resume/dismiss 遇到 非 2xx 时保留当前列表并把服务端 reason 显示在横幅上,不静默刷新掉错误。 桌面端另经 IDesktopShell 发一条系统通知;所有结构化事件都是 crash-continue/* 的 contained 通知,不新增 durable 事件类型。

锁不确定、HMR 与预算

  • 候选确认是三态 Closed / Indeterminate / Open。session.lock/I/O 异常返回 Indeterminate,绝不写 recheckedDeadEpoch;锁释放后的下一轮会重试。
  • HMR 撤销 ISessionPersistence/ISessionQuery 或 ITurnResumeAuthority 时, reconciler/persistence 引用与 policy flags 原子复位;authority 恢复时重新注册, 避免撤销后仍选 F4 或继续使用旧 store。
  • 预算 refund 覆盖 admission 之前的失败(cold start、locked、persistence 缺失); ACK 未知/已确认后的结算写失败保持 pending,不退款、不换 key。

宿主锁与 SIGKILL

每宿主锁文件 crash-continue/hosts/{epoch}.lock:OwnerOnlyPath.OpenExclusiveLease 独占句柄,崩溃后文件保留。新宿主 probe:独占锁冲突表示「旧宿主仍活」(跳过); 成功取锁表示没有旧持有者,按 identity 回收残留后接受接管。其他 I/O 或身份错误 向上传播,不能当作宿主已死。epoch 不复用。

对 SIGKILL 的验收依赖 tests/Longe.CrashContinue.KillChild:fixture 有 crash(真实 Agent/LLM 推进后挂起,等父进程 SIGKILL)与 host 两模式。父测试 启动第二个真实宿主进程,经真实 HTTP 端点逻辑验证矩阵:prompt-only 只读、 白名单 F4 同回合续跑一次、非白名单/身份 mismatch 提示、live 已闭合回合继续、 预算/退避跨重启、gate 关闭 503。宿主进程之间只共享 home/storage 与 session 日志,不复用任何内存状态。

验收边界

  • 进程级矩阵是真实 production composition(JSONL + Coordinator + Query + AgentLoop + crash-continue)+ 真实 SIGKILL/第二宿主,不是人工 seed 代替。
  • tests/Longe.WebHost.Browser.Tests/crash-continue.test.mjs 通过真实 WebHost、 真实安装的树外插件与 Playwright Chrome 验证管理页列表、详情 API、拒绝续跑的 409 与可见错误,以及忽略后列表和持久状态的变化。测试专用 seed 模式通过 生产 JSONL store 写入会话,再设置死 owner hint;它不替代进程级 SIGKILL 验收。
  • ClientBundle.js 的横幅逻辑另由 Node VM hooks stub 测试覆盖;该测试不声称 使用真实 React runtime。

相关文档

  • 会话持久化:turn/end、header、catalog、attempt admission 的 durable 事实来源。
  • Durable Task 契约:admission record 的模式 (Pending/Consumed/Cancelled)借鉴了 #652 的 awaitingTurn 声明。
  • 会话投影:SnapshotSurface / LatestEvent 的判定数据。
在 GitHub 上编辑此页