Skip to content

fix: idle timeout 在长耗时工具执行期间误触发重试#257

Merged
ErlichLiu merged 1 commit intodev-agentfrom
fix/idle-timeout-long-running-tools
Apr 12, 2026
Merged

fix: idle timeout 在长耗时工具执行期间误触发重试#257
ErlichLiu merged 1 commit intodev-agentfrom
fix/idle-timeout-long-running-tools

Conversation

@ErlichLiu
Copy link
Copy Markdown
Owner

Summary

  • 长时间运行的 Bash/Agent/TaskOutput/MCP 工具执行期间,SDK 不产出新事件,idle watcher 120s 后误判为 API 无响应并触发自动重试,中断正在执行的工具
  • 通过 canUseTool 回调层检测长耗时工具(isLongRunningTool),执行期间设置 longRunningToolActive 标志抑制 idle timeout,收到 tool_result 或 turn 结束时清除
  • waitingForUserInput 机制对称,共同覆盖所有「预期沉默」场景

Design

canUseTool 回调层(而非消息内容解析层)检测工具类型,仅对确实可能长时间执行的工具抑制 idle timeout:

function isLongRunningTool(toolName: string): boolean {
  return toolName === 'Bash'
    || toolName === 'Agent'
    || toolName === 'TaskOutput'
    || toolName.startsWith('mcp__')
}

清除时机:

  • tool_result user 消息 → 工具执行完毕
  • result 消息 → turn 结束兜底清除

Test plan

  • IDLE_TIMEOUT_MS 改为 10_000,执行 sleep 30,验证不被中断
  • 调用 MCP 工具(如长耗时查询),验证不被中断
  • 工具执行完成后 API 挂起,验证 idle timeout 仍能正常触发
  • Read/Write 等快速工具不影响 idle timeout 检测

🤖 Generated with Claude Code

Bash/Agent/TaskOutput/MCP 等工具执行期间 SDK 不产出新事件,idle
watcher 120s 后误判为 API 无响应并触发自动重试,中断正在执行的工具。

通过 canUseTool 回调层检测长耗时工具(isLongRunningTool),在执行期间
设置 longRunningToolActive 标志抑制 idle timeout。收到 tool_result
或 turn 结束时清除标志,恢复正常超时检测。

与 waitingForUserInput 机制对称,共同覆盖所有「预期沉默」场景:
- waitingForUserInput:等待用户操作(AskUser / 权限确认 / ExitPlanMode)
- longRunningToolActive:等待工具执行结果(Bash / MCP / Agent / TaskOutput)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@ErlichLiu ErlichLiu merged commit 73bb50b into dev-agent Apr 12, 2026
ErlichLiu added a commit that referenced this pull request Apr 12, 2026
merge: sync dev-agent PRs (#255, #257) to main
ErlichLiu added a commit that referenced this pull request Apr 13, 2026
…running-tools"

This reverts commit 73bb50b, reversing
changes made to d2e707a.
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.

1 participant