From 2394e18729b085937d865e848ce069dbfc8de001 Mon Sep 17 00:00:00 2001 From: teknium1 Date: Sun, 8 Mar 2026 18:58:23 -0700 Subject: [PATCH] fix: add context to interruption messages for model awareness MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the agent is interrupted, the model now receives descriptive context instead of a generic 'Operation interrupted.' string: - Tool skip messages include the tool name: '[Tool execution cancelled — terminal was skipped due to user interrupt]' '[Tool execution skipped — web_search was not started. User sent a new message]' - API call interrupts include timing: 'Operation interrupted: waiting for model response (4.2s elapsed).' - Retry/error interrupts include retry context: 'Operation interrupted: retrying API call after rate limit (retry 2/5).' 'Operation interrupted: handling API error (Timeout: connection timed out).' This helps the model understand what was happening when it was interrupted, reducing wasted iterations spent re-discovering state. --- run_agent.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/run_agent.py b/run_agent.py index 0537dd97..3b0e4a26 100644 --- a/run_agent.py +++ b/run_agent.py @@ -2519,9 +2519,10 @@ class AIAgent: if remaining_calls: print(f"{self.log_prefix}⚡ Interrupt: skipping {len(remaining_calls)} tool call(s)") for skipped_tc in remaining_calls: + skipped_name = skipped_tc.function.name skip_msg = { "role": "tool", - "content": "[Tool execution cancelled - user interrupted]", + "content": f"[Tool execution cancelled — {skipped_name} was skipped due to user interrupt]", "tool_call_id": skipped_tc.id, } messages.append(skip_msg) @@ -2724,9 +2725,10 @@ class AIAgent: remaining = len(assistant_message.tool_calls) - i print(f"{self.log_prefix}⚡ Interrupt: skipping {remaining} remaining tool call(s)") for skipped_tc in assistant_message.tool_calls[i:]: + skipped_name = skipped_tc.function.name skip_msg = { "role": "tool", - "content": "[Tool execution skipped - user sent a new message]", + "content": f"[Tool execution skipped — {skipped_name} was not started. User sent a new message]", "tool_call_id": skipped_tc.id } messages.append(skip_msg) @@ -3274,7 +3276,7 @@ class AIAgent: self._persist_session(messages, conversation_history) self.clear_interrupt() return { - "final_response": "Operation interrupted.", + "final_response": f"Operation interrupted: retrying API call after rate limit (retry {retry_count}/{max_retries}).", "messages": messages, "api_calls": api_call_count, "completed": False, @@ -3383,10 +3385,11 @@ class AIAgent: if thinking_spinner: thinking_spinner.stop("") thinking_spinner = None + api_elapsed = time.time() - api_start_time print(f"{self.log_prefix}⚡ Interrupted during API call.") self._persist_session(messages, conversation_history) interrupted = True - final_response = "Operation interrupted." + final_response = f"Operation interrupted: waiting for model response ({api_elapsed:.1f}s elapsed)." break except Exception as api_error: @@ -3435,7 +3438,7 @@ class AIAgent: self._persist_session(messages, conversation_history) self.clear_interrupt() return { - "final_response": "Operation interrupted.", + "final_response": f"Operation interrupted: handling API error ({error_type}: {str(api_error)[:80]}).", "messages": messages, "api_calls": api_call_count, "completed": False, @@ -3610,7 +3613,7 @@ class AIAgent: self._persist_session(messages, conversation_history) self.clear_interrupt() return { - "final_response": "Operation interrupted.", + "final_response": f"Operation interrupted: retrying API call after error (retry {retry_count}/{max_retries}).", "messages": messages, "api_calls": api_call_count, "completed": False,