fix: add context to interruption messages for model awareness
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.
This commit is contained in:
parent
99f7582175
commit
2394e18729
1 changed files with 9 additions and 6 deletions
15
run_agent.py
15
run_agent.py
|
|
@ -2519,9 +2519,10 @@ class AIAgent:
|
||||||
if remaining_calls:
|
if remaining_calls:
|
||||||
print(f"{self.log_prefix}⚡ Interrupt: skipping {len(remaining_calls)} tool call(s)")
|
print(f"{self.log_prefix}⚡ Interrupt: skipping {len(remaining_calls)} tool call(s)")
|
||||||
for skipped_tc in remaining_calls:
|
for skipped_tc in remaining_calls:
|
||||||
|
skipped_name = skipped_tc.function.name
|
||||||
skip_msg = {
|
skip_msg = {
|
||||||
"role": "tool",
|
"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,
|
"tool_call_id": skipped_tc.id,
|
||||||
}
|
}
|
||||||
messages.append(skip_msg)
|
messages.append(skip_msg)
|
||||||
|
|
@ -2724,9 +2725,10 @@ class AIAgent:
|
||||||
remaining = len(assistant_message.tool_calls) - i
|
remaining = len(assistant_message.tool_calls) - i
|
||||||
print(f"{self.log_prefix}⚡ Interrupt: skipping {remaining} remaining tool call(s)")
|
print(f"{self.log_prefix}⚡ Interrupt: skipping {remaining} remaining tool call(s)")
|
||||||
for skipped_tc in assistant_message.tool_calls[i:]:
|
for skipped_tc in assistant_message.tool_calls[i:]:
|
||||||
|
skipped_name = skipped_tc.function.name
|
||||||
skip_msg = {
|
skip_msg = {
|
||||||
"role": "tool",
|
"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
|
"tool_call_id": skipped_tc.id
|
||||||
}
|
}
|
||||||
messages.append(skip_msg)
|
messages.append(skip_msg)
|
||||||
|
|
@ -3274,7 +3276,7 @@ class AIAgent:
|
||||||
self._persist_session(messages, conversation_history)
|
self._persist_session(messages, conversation_history)
|
||||||
self.clear_interrupt()
|
self.clear_interrupt()
|
||||||
return {
|
return {
|
||||||
"final_response": "Operation interrupted.",
|
"final_response": f"Operation interrupted: retrying API call after rate limit (retry {retry_count}/{max_retries}).",
|
||||||
"messages": messages,
|
"messages": messages,
|
||||||
"api_calls": api_call_count,
|
"api_calls": api_call_count,
|
||||||
"completed": False,
|
"completed": False,
|
||||||
|
|
@ -3383,10 +3385,11 @@ class AIAgent:
|
||||||
if thinking_spinner:
|
if thinking_spinner:
|
||||||
thinking_spinner.stop("")
|
thinking_spinner.stop("")
|
||||||
thinking_spinner = None
|
thinking_spinner = None
|
||||||
|
api_elapsed = time.time() - api_start_time
|
||||||
print(f"{self.log_prefix}⚡ Interrupted during API call.")
|
print(f"{self.log_prefix}⚡ Interrupted during API call.")
|
||||||
self._persist_session(messages, conversation_history)
|
self._persist_session(messages, conversation_history)
|
||||||
interrupted = True
|
interrupted = True
|
||||||
final_response = "Operation interrupted."
|
final_response = f"Operation interrupted: waiting for model response ({api_elapsed:.1f}s elapsed)."
|
||||||
break
|
break
|
||||||
|
|
||||||
except Exception as api_error:
|
except Exception as api_error:
|
||||||
|
|
@ -3435,7 +3438,7 @@ class AIAgent:
|
||||||
self._persist_session(messages, conversation_history)
|
self._persist_session(messages, conversation_history)
|
||||||
self.clear_interrupt()
|
self.clear_interrupt()
|
||||||
return {
|
return {
|
||||||
"final_response": "Operation interrupted.",
|
"final_response": f"Operation interrupted: handling API error ({error_type}: {str(api_error)[:80]}).",
|
||||||
"messages": messages,
|
"messages": messages,
|
||||||
"api_calls": api_call_count,
|
"api_calls": api_call_count,
|
||||||
"completed": False,
|
"completed": False,
|
||||||
|
|
@ -3610,7 +3613,7 @@ class AIAgent:
|
||||||
self._persist_session(messages, conversation_history)
|
self._persist_session(messages, conversation_history)
|
||||||
self.clear_interrupt()
|
self.clear_interrupt()
|
||||||
return {
|
return {
|
||||||
"final_response": "Operation interrupted.",
|
"final_response": f"Operation interrupted: retrying API call after error (retry {retry_count}/{max_retries}).",
|
||||||
"messages": messages,
|
"messages": messages,
|
||||||
"api_calls": api_call_count,
|
"api_calls": api_call_count,
|
||||||
"completed": False,
|
"completed": False,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue