长运行兜底与稳定性保障
源码里有很多"不是主功能但决定能不能长时间跑"的保护。它们汇总成一份稳定性清单——这是把 demo 推向生产时最容易被忽略的部分。
稳定性清单
以下保护共同决定了一个会话/任务能否长时间稳定运行,而不是跑着跑着卡死、烧钱或失控。
- auto compact circuit breaker:连续失败 3 次后本 session 不再重试(
tengu_auto_compact_circuit_breaker)。reactive 路径同样计数。 - rapid-refill breaker:compact 之后 3 轮内又把窗口填满,连续 3 次就停。
- compact prompt-too-long retry:compact 自己过长时丢最老 API round 后重试。
- 预计算 compact:
tengu_sepia_moth+precomputeCompactionEnabled打开时,主循环可先吃已经算好的 compact。 - session memory:viewer / attachment 还在;不是 2.1.233 默认的先压路径。
- memory extraction coalescing:后台 memory 提取不重入,只保留最新 pending context。
- Bash subcommand cap:2.1.88 是超过 50 个子命令直接 ask。2.1.233 那条 log 字面量不在了,具体上限没再读出来。
- permission resolve-once:本地/远程/hook/classifier 多路竞态只允许一个结果生效,避免重复执行。
- async agent/background task 输出落盘:长 subagent 可后台化,结果写
TaskOutput文件,避免主上下文被工具噪音填满。 - sync agent 可自动/手动 background:前台 agent 超过 2 秒显示 background hint;可转后台继续跑。
- background agent abort 独立:用户 Esc 主线程不杀后台 agent;通过 task kill 显式清理。
- subagent cleanup:
runAgent.tsfinally 清 MCP、session hooks、file cache、Perfetto registry、transcript mapping、todos、后台 shell task。 - MCP reconnect/auth/session expired 处理:401 刷 token,404/-32001 识别 session expired,SSE 重连耗尽主动 close。
- ToolSearch 降上下文压力:MCP/大型工具 schema 默认 defer,避免启动就塞爆工具定义。
- post compact cleanup:清 systemPromptSection、memory file cache、classifier approval、speculative checks、session message cache 等,防 stale state。
- tool result storage / content replacement:大工具结果可替换、落盘或 cache edit,降低上下文与内存占用。