Skip to content

fix: backfill tool_result for all unfinished tool_call entries on Ctrl+C interrupt to preserve valid message sequencing#46

Open
townwish4git wants to merge 1 commit intovigo999:refactor-arch-4.31from
townwish4git:refactor-arch-4.31-b-03271007-interupted-tool
Open

fix: backfill tool_result for all unfinished tool_call entries on Ctrl+C interrupt to preserve valid message sequencing#46
townwish4git wants to merge 1 commit intovigo999:refactor-arch-4.31from
townwish4git:refactor-arch-4.31-b-03271007-interupted-tool

Conversation

@townwish4git
Copy link
Copy Markdown
Collaborator

@townwish4git townwish4git commented Mar 27, 2026

4fac09cf-415d-4ede-8fdc-cb49cd87cee1

Bug

  • 现象:agent 发起 tool_call 时,用户按 Ctrl+C 中断后,被打断的tool_call 没有 tool_result。
  • 后果:会产生不合法的消息轨迹(tool_call/tool_result 不配对),不符合 OpenAI/Anthropic tool-calling 格式约束。

Solution

  • 引擎:
    • 当前执行中的 call 补写 tool execution interrupted by user。
    • 当 tool call 循环中遇到 context.Canceled,为后续未执行的 calls 批量补写 tool_result(interrupted before execution)。
    • 发出 ToolError 事件,让 TUI 明确呈现中断而非成功态。
  • shell/runtime:
    • runtime runner 在命令结束后检查 ctx.Err(),将取消状态写入 Result.Error。
    • shell tool 对 context.Canceled/DeadlineExceeded 直接返回 error,不再伪装为成功结果。

Impact

  • 覆盖所有 provider(OpenAI completion / OpenAI responses / Anthropic),因为修复发生在统一 agent loop。
  • 保证 session trajectory 中每个 tool_call_id 都有对应 tool_result,序列始终可回放、可继续对话。
  • 改善 TUI 反馈:中断不再显示为绿色成功完成。

Validation

  • go test ./agent/loop -run TestRunAddsInterruptedToolResultOnCanceledToolCall -count=1
  • go test ./agent/loop -run TestRunBackfillsInterruptedResultsForUnexecutedToolCalls -count=1
  • go test ./agent/loop -count=1
  • go test ./tools/shell -count=1
  • go test ./internal/app -run TestInterruptTokenCancelsActiveTask -count=1

@townwish4git townwish4git changed the title fix: 在 Ctrl+C 中断时为所有未完成 tool_call 回填 tool_result,保证消息序列合法 fix: backfill tool_result for all unfinished tool_call entries on Ctrl+C interrupt to preserve valid message sequencing Mar 27, 2026
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