The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): recheck drain after wake admission
calvinnwqfe614d29e17f
diff --git a/gateway/wake.py b/gateway/wake.pyindex a568c56c00a..4bea4183df5 100644--- a/gateway/wake.py+++ b/gateway/wake.py@@ -102,11 +102,21 @@ async def admit_internal_event(adapter: Any, event: Any) -> None:event._gateway_accepted = False# A draining gateway answers new work with a drain notice and discards queued follow-ups,# so a wake admitted now would never run. Refuse it so the caller keeps it for a retry.- if getattr(getattr(adapter, "gateway_runner", None), "_draining", False) is True:+ if _runner_draining(adapter):raise WakeNotAccepted("gateway is draining; retry after restart")await adapter.handle_message(event)if event._gateway_accepted is not True:raise WakeNotAccepted()+ # handle_message can await (Telegram topic recovery, the busy handler's profile scope)+ # before it claims or queues the event. The in-tree adapters do not await after the claim,+ # so this read sees the drain state the claim was made under: a drain that began+ # mid-admission dooms the claimed turn the same way.+ if _runner_draining(adapter):+ raise WakeNotAccepted("gateway began draining during admission; retry after restart")+++def _runner_draining(adapter: Any) -> bool:+ return getattr(getattr(adapter, "gateway_runner", None), "_draining", False) is Trueasync def deliver_wake(adapter: Any, *, text: str, session_id: str = "", source: Any = None,diff --git a/tests/gateway/test_wake_during_restart_drain.py b/tests/gateway/test_wake_during_restart_drain.pyindex 4c30e4573e9..bc68f4ff1ce 100644--- a/tests/gateway/test_wake_during_restart_drain.py+++ b/tests/gateway/test_wake_during_restart_drain.py@@ -5,6 +5,7 @@ wake it admitted would never run, while the Kanban notifier had already advanced"""import asyncio+import threadingimport pytest@@ -108,3 +109,27 @@ async def test_wake_for_a_busy_session_is_refused_while_draining():with pytest.raises(WakeNotAccepted):await deliver_wake(adapter, text="[kanban] Task t_1 completed.", source=source)assert key not in adapter._pending_messages # would be discarded when the turn ends+++async def test_wake_is_refused_when_drain_starts_during_admission():+ runner, adapter, source, turns = wake_route()+ entered, release = threading.Event(), threading.Event()++ def slow_topic_recovery(+ _source,+ ): # Telegram DM admission awaits this before it claims+ entered.set()+ release.wait(5)++ adapter._topic_recovery_fn = slow_topic_recovery+ admission = asyncio.create_task(+ deliver_wake(adapter, text="[kanban] Task t_1 completed.", source=source)+ )+ assert await asyncio.to_thread(entered.wait, 5)+ runner._draining = True # /restart begins while admission is parked+ release.set()+ with pytest.raises(WakeNotAccepted):+ await admission+ await drain(adapter)+ assert turns == []