Skip to content

The agent that grows with you

fix(agent): publish the Ctrl-C reason before the sequential cancel hook

kshitijk4poor764938ed742f
diff --git a/agent/tool_executor.py b/agent/tool_executor.py
index d70a158d66f..49e287eb30e 100644
--- a/agent/tool_executor.py
+++ b/agent/tool_executor.py
@@ -1760,9 +1760,10 @@ def _run_sequential_call(
except KeyboardInterrupt:
if not dispatch.handles_keyboard_interrupt:
raise
- _spinner_result = ref.emit_cancelled(agent, tool_start_time)
+ # Publish the user-interrupt reason BEFORE emitting, so the hook sees it (as the concurrent path does).
with contextlib.suppress(Exception):
agent.interrupt("keyboard interrupt", tool_reason=_REASON_USER_INTERRUPT)
+ _spinner_result = ref.emit_cancelled(agent, tool_start_time)
_append_skipped_tool_results(
agent, messages, remaining_calls, ref.task_id,
content=f"[Tool execution cancelled — {{name}} was skipped. {interrupt_skip_wording(agent)}]",
diff --git a/tests/agent/test_run_agent.py b/tests/agent/test_run_agent.py
index 0513ed72cca..be9697b62e1 100644
--- a/tests/agent/test_run_agent.py
+++ b/tests/agent/test_run_agent.py
@@ -1626,6 +1626,8 @@ class TestExecuteToolCalls:
assert post_calls[0]["status"] == "cancelled"
assert post_calls[0]["error_type"] == "keyboard_interrupt"
assert json.loads(post_calls[0]["result"])["status"] == "cancelled"
+ # The hook sees the user-interrupt attribution, not the generic "Turn interrupted".
+ assert post_calls[0]["error_message"] == "Tool execution cancelled. User interrupt"
def test_interrupt_skips_remaining(self, agent, monkeypatch):
tc1 = _mock_tool_call(name="web_search", arguments="{}", call_id="c1")