Skip to content

The agent that grows with you

fix(agent): a pending redirect does not hide the restart from a shutdown stop

Teknium55f708e4ddec
diff --git a/agent/interrupt_control.py b/agent/interrupt_control.py
index a36a1e14228..af647a1c4f1 100644
--- a/agent/interrupt_control.py
+++ b/agent/interrupt_control.py
@@ -190,9 +190,11 @@ class InterruptControlMixin:
)
def _publish_interrupt_state() -> None:
- # A shutdown stop never relabels an interrupt already pending (the user's Stop wins): the turn
- # records the first cause, so it is not announced as interrupted by the restart.
- if not (tool_reason == _REASON_GATEWAY_SHUTDOWN and getattr(self, "_interrupt_requested", False)):
+ # A shutdown stop never relabels a user Stop already pending (the user's Stop wins): the turn
+ # records the first cause, so it is not announced as interrupted by the restart. A pending
+ # redirect (a mid-request correction) is not a Stop: the restart drops it and is the cause.
+ if not (tool_reason == _REASON_GATEWAY_SHUTDOWN and getattr(self, "_interrupt_requested", False)
+ and getattr(self, "_tool_interrupt_reason", None) in USER_INTERRUPT_REASONS):
self._interrupt_message = message
self._tool_interrupt_reason = tool_interrupt_reason
self._interrupt_requested = True
diff --git a/tests/gateway/test_restart_interrupt_notice.py b/tests/gateway/test_restart_interrupt_notice.py
index 2098b2b02b6..d818398f3b8 100644
--- a/tests/gateway/test_restart_interrupt_notice.py
+++ b/tests/gateway/test_restart_interrupt_notice.py
@@ -263,3 +263,42 @@ def test_in_process_turn_keeps_the_user_stop_when_shutdown_interrupts_it_again(u
assert agent._interrupt_message == 'Gateway restarting' and notice == RESTART_INTERRUPT_NOTICE
finally:
set_interrupt(False)
+
+
+def _redirect_agent():
+ from run_agent import AIAgent
+ agent = AIAgent.__new__(AIAgent)
+ agent._interrupt_requested = False
+ agent._interrupt_message = agent._tool_interrupt_reason = None
+ agent._hard_interrupt_requested = threading.Event()
+ agent._execution_thread_id = None
+ agent._interrupt_thread_signal_pending = False
+ agent._active_children, agent._active_children_lock = [], threading.Lock()
+ agent.quiet_mode = True
+ agent._pending_redirect = None
+ agent._pending_redirect_lock = threading.Lock()
+ agent._model_request_active = threading.Event(); agent._model_request_active.set()
+ agent._executing_tools = False
+ agent._lock = threading.Lock()
+ agent.turn_active = True
+ return agent
+
+
+def test_restart_after_a_pending_redirect_still_gets_the_restart_notice():
+ """A mid-request correction is not a Stop: the restart drops it and is the turn's cause."""
+ from gateway.session_settlement_recovery import owner_stop_notice
+ from tests.gateway.restart_test_helpers import make_restart_runner
+ from tools.interrupt import set_interrupt
+ agent = _redirect_agent()
+ runner, _ = make_restart_runner()
+ runner._restart_requested = True
+ runner._running_agents = {'route': agent}
+ try:
+ assert agent.redirect("actually use python 3.12") is True
+ runner._interrupt_running_agents(runner._shutdown_interrupt_reason())
+ result = {'interrupted': True, **({'interrupt_message': agent._interrupt_message} if agent._interrupt_message else {})}
+ notice = owner_stop_notice({'result': result}, 'interrupted')
+ assert agent._pending_redirect is None # the restart dropped the correction: the turn ends interrupted
+ assert notice == RESTART_INTERRUPT_NOTICE, (agent._interrupt_message, notice)
+ finally:
+ set_interrupt(False)