Skip to content

The agent that grows with you

fix(gateway): run wakes admitted before a restart drain

calvinnwq1e1fa5afd355
diff --git a/gateway/platforms/event.py b/gateway/platforms/event.py
index 5574769f888..0f6dd644a94 100644
--- a/gateway/platforms/event.py
+++ b/gateway/platforms/event.py
@@ -114,6 +114,10 @@ class MessageEvent:
# Process-local admission receipt, never routing metadata or execution acknowledgement.
_gateway_accepted: bool = field(default=False, init=False, repr=False, compare=False)
+ # Set when ``admit_internal_event`` hands its producer a receipt before any drain began. The
+ # producer will not retry, so a restart drain runs this event instead of refusing it at the
+ # drain gate or discarding it as a queued follow-up (#138123).
+ _admitted_before_drain: bool = field(default=False, init=False, repr=False, compare=False)
# Run-owned final presentation snapshot; never deserialized from ingress metadata.
_notification_reply_muted: Optional[bool] = field(default=None, init=False, repr=False, compare=False)
diff --git a/gateway/run_inbound_hooks.py b/gateway/run_inbound_hooks.py
index 964cd969c0d..319cde0d93f 100644
--- a/gateway/run_inbound_hooks.py
+++ b/gateway/run_inbound_hooks.py
@@ -211,7 +211,7 @@ class GatewayInboundHooksMixin:
) -> tuple[bool, Optional[str], Optional[str]]:
"""Drain gate, user-defined quick commands (exec/alias) and plugin slash commands →
``(handled, result, command)``; an alias quick command rewrites ``command``."""
- if self._draining:
+ if self._draining and getattr(event, "_admitted_before_drain", False) is not True:
return True, t("gateway.busy.drain_rejected_new_work", action=self._status_action_gerund()), command
# User-defined quick commands (bypass agent loop, no LLM call)
diff --git a/gateway/run_turn.py b/gateway/run_turn.py
index 85adec0ee9f..f090da4a75f 100644
--- a/gateway/run_turn.py
+++ b/gateway/run_turn.py
@@ -1750,7 +1750,7 @@ class GatewayTurnMixin(GatewayTurnPrepareMixin, GatewayTurnHygieneMixin, Gateway
pending_event = None
pending = None
- if self._draining and (pending_event or pending):
+ if self._draining and (pending_event or pending) and getattr(pending_event, "_admitted_before_drain", False) is not True:
logger.info(
"Discarding pending follow-up for session %s during gateway %s",
session_key or "?", self._status_action_label(),
diff --git a/gateway/wake.py b/gateway/wake.py
index 0473cd3374f..fb78998e91b 100644
--- a/gateway/wake.py
+++ b/gateway/wake.py
@@ -115,6 +115,9 @@ async def admit_internal_event(adapter: Any, event: Any) -> None:
if _runner_draining(adapter):
raise WakeNotAccepted("gateway began draining during admission; retry after restart",
reason="runtime_draining")
+ # No drain began before or during admission, so the producer takes this receipt as final and
+ # will not retry: a drain that begins later must run the wake rather than drop it (#138123).
+ event._admitted_before_drain = True
def _runner_draining(adapter: Any) -> bool:
diff --git a/tests/gateway/test_wake_admitted_before_drain.py b/tests/gateway/test_wake_admitted_before_drain.py
new file mode 100644
index 00000000000..3b9ea617dbd
--- /dev/null
+++ b/tests/gateway/test_wake_admitted_before_drain.py
@@ -0,0 +1,64 @@
+"""A wake admitted before a restart drain begins must still run.
+
+Its producer was told the wake was delivered and will not retry it, so the drain gate and
+the pending follow-up discard must not drop it (#138123).
+"""
+
+import asyncio
+
+import pytest
+
+from gateway.platforms.event import MessageEvent
+from gateway.wake import deliver_wake
+from tests.gateway.restart_test_helpers import make_restart_runner, make_restart_source
+
+WAKE = "[kanban] Task t_1 completed."
+
+
+async def drain(adapter):
+ while adapter._background_tasks:
+ await asyncio.gather(*list(adapter._background_tasks), return_exceptions=True)
+ await asyncio.sleep(0)
+
+
+def wake_route():
+ runner, adapter = make_restart_runner()
+ adapter.gateway_runner = runner
+ adapter.set_message_handler(runner._handle_message)
+ runner._delivery_adapter_for = lambda source: adapter
+ turns = []
+
+ async def agent_turn(event, source, quick_key, run_generation):
+ turns.append(event.text)
+
+ runner._handle_message_with_agent = agent_turn
+ return runner, adapter, make_restart_source(), turns
+
+
+async def test_wake_claimed_before_drain_runs():
+ runner, adapter, source, turns = wake_route()
+ await deliver_wake(adapter, text=WAKE, source=source)
+ runner._draining = True # /restart begins before the claimed turn reaches the drain gate
+ await drain(adapter)
+ assert turns == [WAKE]
+ assert adapter.sent == [] # no drain notice for work the gateway already accepted
+
+
+async def test_wake_queued_before_drain_survives_the_pending_discard():
+ runner, adapter, source, _turns = wake_route()
+ key = adapter._event_session_key(MessageEvent(text="", source=source))
+ adapter._active_sessions[key] = asyncio.Event() # a turn is running in this chat
+ await deliver_wake(adapter, text=WAKE, source=source)
+ runner._draining = True # /restart begins while the wake waits behind that turn
+ event, text = await runner._run_agent_drain_pending(
+ {"final_response": "done"}, adapter, source, key
+ )
+ assert text == WAKE and event.internal # runs next instead of being discarded
+
+ runner._enqueue_fifo(key, MessageEvent(text="a human follow-up", source=source), adapter)
+ event, text = await runner._run_agent_drain_pending(
+ {"final_response": "done"}, adapter, source, key
+ )
+ assert (event, text) == (None, None) # other follow-ups are still discarded