Skip to content

The agent that grows with you

refactor(agent): one builder for the interrupted_during_api_call exit reason

kshitijk4poor429f319d4582
diff --git a/agent/interrupt_control.py b/agent/interrupt_control.py
index 561be617323..4276f64d2ac 100644
--- a/agent/interrupt_control.py
+++ b/agent/interrupt_control.py
@@ -32,6 +32,12 @@ def interrupt_issuer(agent) -> Optional[str]:
return str(reason).strip().replace(" ", "_")
+def interrupted_during_api_call_reason(agent) -> str:
+ """Turn exit reason for an API call cut short by an interrupt (``turn_explainers`` matches the prefix)."""
+ issuer = interrupt_issuer(agent)
+ return f"interrupted_during_api_call({issuer})" if issuer else "interrupted_during_api_call"
+
+
def _fence_cancel_before_commit(fence, *, when_in_flight: bool, failure_log: str) -> None:
"""Call ``type(fence).cancel_before_commit(fence)`` when ``commit_in_flight`` matches.
diff --git a/agent/turn_finalizer.py b/agent/turn_finalizer.py
index 5c98a3961ea..9c44467439d 100644
--- a/agent/turn_finalizer.py
+++ b/agent/turn_finalizer.py
@@ -14,7 +14,7 @@ from typing import Any, Callable, List, Optional, Tuple
from agent.codex_responses_adapter import _summarize_user_message_for_log
from agent.delegation_context import is_dispatcher_owned_worker_context
-from agent.interrupt_control import interrupt_issuer
+from agent.interrupt_control import interrupted_during_api_call_reason
from agent.turn_failure_copy import exit_reason_failure, stamp_failure
from agent.context_compressor import _DB_PERSISTED_MARKER
from agent.message_content import flatten_message_text
@@ -160,15 +160,12 @@ def _resolve_budget_fallback(
try:
final_response = agent._handle_max_iterations(messages, api_call_count)
except InterruptedError:
- # Same surfacing as an interrupted loop API call (handle_api_interrupt):
- # the turn is interrupted, so the pending interrupt message is returned
- # for requeue instead of being cleared behind a fallback summary.
+ # The turn ends interrupted, so the pending interrupt message is returned
+ # for requeue instead of being cleared behind a fallback summary. A redirect
+ # also ends it: the budget is spent, so there is no loop to restart into.
from agent.conversation_loop import INTERRUPT_WAITING_FOR_MODEL_PREFIX
interrupted = True
- _issuer = interrupt_issuer(agent)
- _turn_exit_reason = (
- f"interrupted_during_api_call({_issuer})" if _issuer else "interrupted_during_api_call"
- )
+ _turn_exit_reason = interrupted_during_api_call_reason(agent)
final_response = f"{INTERRUPT_WAITING_FOR_MODEL_PREFIX}{time.time() - _summary_start:.1f}s elapsed)."
# A kanban worker must record a terminal outcome whether or not a fallback path
diff --git a/agent/turn_iteration_prep.py b/agent/turn_iteration_prep.py
index 66163155a48..2ee6cfa30fe 100644
--- a/agent/turn_iteration_prep.py
+++ b/agent/turn_iteration_prep.py
@@ -17,7 +17,7 @@ from dataclasses import dataclass
from typing import Any, Dict
from agent.display import KawaiiSpinner
-from agent.interrupt_control import interrupt_issuer
+from agent.interrupt_control import interrupt_issuer, interrupted_during_api_call_reason
from agent.turn_context_compaction import _reanchor
from agent.turn_truncation import boosted_output_cap
@@ -471,10 +471,7 @@ def apply_retry_restarts(
return _verdict("continue")
if interrupted:
- _issuer = interrupt_issuer(agent)
- _turn_exit_reason = (
- f"interrupted_during_api_call({_issuer})" if _issuer else "interrupted_during_api_call"
- )
+ _turn_exit_reason = interrupted_during_api_call_reason(agent)
return _verdict("break")
if _retry.restart_with_compressed_messages: