Skip to content

workflows: 后续模型回合可永久无进展,first-response watchdog 无法兜底 #171

Closed

Description

@tt-a1i

背景

一次真实的全仓库审查 Workflow 出现三个 Agent 永久停滞:

  • Run:wf_cfaa8bfbca10
  • 总时长:38m34s(最终人工 workflow_stop)
  • 9/12 Agent 正常完成
  • 3 个 Agent 在本地 Read/Grep 已完成后,等待下一轮模型响应超过 34 分钟,无新的 thinking、assistant message 或 tool call
  • 同一批运行中观察到 OpenAI Codex WebSocket error 和显著 Prompt Cache miss
  • 停止后 artifacts、transcripts 和 9 个成功结果均正确保留

失败 Agent 在卡住前已经成功运行多个回合:

  • lifecycle:14 turns,约 165k context tokens
  • tests:16 turns,约 100k context tokens
  • performance:12 turns,约 156k context tokens

这说明它们不是 startup/first-response 卡住,而是“工具返回后的后续 Provider 请求”没有完成。

当前保护边界

extensions/workflows/runner.ts 有 45 秒首次响应 watchdog:

export const FIRST_RESPONSE_TIMEOUT_MS = 45_000

runWorkflowAgent() 在整个 childSession.prompt(...) 外创建一次 watchdog;第一条 assistant event 调用 markResponse() 后便解除。Agent 后续可以经历任意数量的 model → tool → model 回合,但这些后续 Provider 请求不再受 watchdog 保护。

现有测试还明确锁定了:

workflow agent invocations have no per-request wall timer

Pi 的 httpIdleTimeoutMs 默认 5 分钟,但它是 HTTP/stream idle 保护,不是 model completion deadline。WebSocket 心跳、非模型可见底层事件,或未被该 timeout 覆盖的等待状态,仍可能让一次请求长期不产生 response.completed。

问题判断

  • Provider/WebSocket 波动是触发器;
  • Runtime 允许已启动 Agent 在后续模型回合无限无进展,是生命周期/可恢复性缺口;
  • 不应依赖用户通过 /workflows 观察后手动停止;
  • 不建议直接给整个 Workflow 加固定总时长:长 Workflow 可以合理运行数小时,只要持续有可观测进展。

需要讨论的设计目标

Runtime 应区分:

  1. 长时间运行但持续有模型/工具进展:允许继续;
  2. 工具调用正在运行:继续使用现有 3 分钟 child tool timeout;
  3. Provider 请求没有任何模型可见进展:有界终止或恢复;
  4. Provider 有底层心跳但没有 thinking/text/tool call/完成事件:不能因此无限续命;
  5. 用户显式选择超长推理模型:允许配置更宽的边界,但仍不能无界。

可能方案:

A. 每轮 model-progress watchdog(倾向)

在每个 model request/turn 开始后等待第一条模型可见进展;thinking/text/tool call/assistant completion 才重置或解除。原始 WebSocket 心跳不算进展。

难点:需要确认 Pi child Session 事件是否能可靠标识每次 Provider request/turn 的开始,而不是只标识整次 prompt()。

B. Agent 无进展 watchdog

基于最后一条模型可见事件或工具完成事件计时;超过可配置窗口时 abort child。实现 seam 可能更简单,但需要避免长时间 server-side reasoning 的误杀。

C. 可配置 per-agent deadline

Workflow agent() 增加可选 deadline,Runtime 同时提供一个较宽的 hard ceiling。适合调用者明确约束,但不能把安全默认完全交给 prompt/script。

不倾向

  • 固定整个 Workflow 总超时;
  • 只依赖 httpIdleTimeoutMs;
  • 仅在 TUI 显示“疑似卡住”但不释放并发槽;
  • Provider heartbeat 每到一次就无限延长 watchdog;
  • 超时后猜测 Agent 成功或丢弃部分 artifacts。

终止语义

触发 watchdog 后应:

  • abort 当前 child Provider request;
  • 走现有 bounded shutdown/dispose;
  • 释放 Workflow call/concurrency slot;
  • 持久化精确终态(timeout/error;若无法确认则 uncertain),不得猜成功;
  • 保留已完成 transcript、usage、tool evidence;
  • 忽略晚到的 Provider completion,不能翻转为 success;
  • 允许 Workflow 的 sibling items 和后续 Report 按失败覆盖数继续运行。

回归测试

建议增加真实故障形状,而非只测试首请求:

  1. child 首轮正常产生 assistant/tool call;
  2. tool result 正常完成;
  3. 第二轮 Provider promise/stream 永不完成;
  4. watchdog 在边界内 abort;
  5. child slot 被释放,pipeline sibling 不受影响;
  6. transcript 和 usage 被保留;
  7. 晚到 completion 不能变成成功;
  8. 持续 thinking/text/tool 进展不会被误杀;
  9. 原始 transport heartbeat 不应被当作模型进展;
  10. 显式 workflow_stop 与 watchdog 竞态仍只产生一个 terminal result;
  11. replay 不得把 timeout/uncertain 调用当作安全成功缓存。

严重程度

P1 reliability/lifecycle。不会直接造成权限逃逸或数据损坏,但会无限占用 Agent 槽、延迟后台交付、增加费用,并要求用户人工恢复。

Activity

  1. added
    bug
    on Aug 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions