Skip to content

The agent that grows with you

fix(agent): a gateway shutdown interrupt never relabels a user Stop already pending

Teknium027f587ea47f
diff --git a/agent/interrupt_control.py b/agent/interrupt_control.py
index 6e4930ee69f..a36a1e14228 100644
--- a/agent/interrupt_control.py
+++ b/agent/interrupt_control.py
@@ -22,6 +22,8 @@ _REASON_HARD_STOP = "explicit stop requested"
_REASON_NEW_MESSAGE = "user sent a new message"
_REASON_USER_INTERRUPT = "user interrupt"
USER_INTERRUPT_REASONS = frozenset({_REASON_HARD_STOP, _REASON_NEW_MESSAGE, _REASON_USER_INTERRUPT})
+# ``tool_reason`` the gateway's restart/shutdown stop passes (gateway.run._INTERRUPT_TOOL_REASON_GATEWAY_SHUTDOWN).
+_REASON_GATEWAY_SHUTDOWN = "gateway shutdown"
def interrupt_issuer(agent) -> Optional[str]:
@@ -188,9 +190,12 @@ 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)):
+ self._interrupt_message = message
+ self._tool_interrupt_reason = tool_interrupt_reason
self._interrupt_requested = True
- self._interrupt_message = message
- self._tool_interrupt_reason = tool_interrupt_reason
self._turn_user_intervened = True
# The turn record and the log must agree on WHO asked for the stop (#112647).
logger.info("Interrupt requested (%s): %s", "hard" if hard_cancel else "soft", tool_interrupt_reason)
diff --git a/tests/gateway/test_restart_interrupt_notice.py b/tests/gateway/test_restart_interrupt_notice.py
index 3dd2219f7d9..2098b2b02b6 100644
--- a/tests/gateway/test_restart_interrupt_notice.py
+++ b/tests/gateway/test_restart_interrupt_notice.py
@@ -226,3 +226,40 @@ def test_managed_worker_turn_settles_with_the_cause_of_the_stop_that_reached_it(
assert 'warning' not in complete, complete
else:
assert complete['warning'] == RESTART_INTERRUPT_NOTICE
+
+
[email protected]('user_stopped_first', [False, True])
+def test_in_process_turn_keeps_the_user_stop_when_shutdown_interrupts_it_again(user_stopped_first):
+ """An in-process turn the user stopped is still unwinding (e.g. inside a tool) when the restart
+ interrupts its agent: the shutdown reason must not overwrite the pending Stop, so the turn
+ records no restart cause and gets no notice. A turn only the restart stopped still gets it."""
+ from agent.interrupt_compat import request_hard_interrupt
+ from gateway.session_settlement_recovery import owner_stop_notice
+ from run_agent import AIAgent
+ from tests.gateway.restart_test_helpers import make_restart_runner
+ from tools.interrupt import set_interrupt
+
+ 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
+ runner, _adapter = make_restart_runner()
+ runner._restart_requested = True
+ runner._running_agents = {'route': agent}
+ try:
+ if user_stopped_first:
+ request_hard_interrupt(agent) # SessionAuthority.interrupt: the user's Stop carries no message
+ runner._interrupt_running_agents(runner._shutdown_interrupt_reason())
+ assert agent._interrupt_requested and agent._hard_interrupt_requested.is_set()
+ result = {'interrupted': True, **({'interrupt_message': agent._interrupt_message} if agent._interrupt_message else {})}
+ notice = owner_stop_notice({'result': result}, 'interrupted')
+ if user_stopped_first:
+ assert agent._interrupt_message is None and notice is None
+ else:
+ assert agent._interrupt_message == 'Gateway restarting' and notice == RESTART_INTERRUPT_NOTICE
+ finally:
+ set_interrupt(False)