机制专题 · 09

长运行兜底与稳定性保障

源码里有很多"不是主功能但决定能不能长时间跑"的保护。它们汇总成一份稳定性清单——这是把 demo 推向生产时最容易被忽略的部分。

稳定性清单

以下保护共同决定了一个会话/任务能否长时间稳定运行,而不是跑着跑着卡死、烧钱或失控。

  1. auto compact circuit breaker:连续失败 3 次后本 session 不再重试(tengu_auto_compact_circuit_breaker)。reactive 路径同样计数。
  2. rapid-refill breaker:compact 之后 3 轮内又把窗口填满,连续 3 次就停。
  3. compact prompt-too-long retry:compact 自己过长时丢最老 API round 后重试。
  4. 预计算 compacttengu_sepia_moth + precomputeCompactionEnabled 打开时,主循环可先吃已经算好的 compact。
  5. session memory:viewer / attachment 还在;不是 2.1.233 默认的先压路径。
  6. memory extraction coalescing:后台 memory 提取不重入,只保留最新 pending context。
  7. Bash subcommand cap:2.1.88 是超过 50 个子命令直接 ask。2.1.233 那条 log 字面量不在了,具体上限没再读出来。
  8. permission resolve-once:本地/远程/hook/classifier 多路竞态只允许一个结果生效,避免重复执行。
  9. async agent/background task 输出落盘:长 subagent 可后台化,结果写 TaskOutput 文件,避免主上下文被工具噪音填满。
  10. sync agent 可自动/手动 background:前台 agent 超过 2 秒显示 background hint;可转后台继续跑。
  11. background agent abort 独立:用户 Esc 主线程不杀后台 agent;通过 task kill 显式清理。
  12. subagent cleanup:runAgent.ts finally 清 MCP、session hooks、file cache、Perfetto registry、transcript mapping、todos、后台 shell task。
  13. MCP reconnect/auth/session expired 处理:401 刷 token,404/-32001 识别 session expired,SSE 重连耗尽主动 close。
  14. ToolSearch 降上下文压力:MCP/大型工具 schema 默认 defer,避免启动就塞爆工具定义。
  15. post compact cleanup:清 systemPromptSection、memory file cache、classifier approval、speculative checks、session message cache 等,防 stale state。
  16. tool result storage / content replacement:大工具结果可替换、落盘或 cache edit,降低上下文与内存占用。