Skip to content

The agent that grows with you

fix(gateway): read the drain state before admission awaits

calvinnwqc6676c3cee3f
diff --git a/tests/gateway/test_wake_admitted_before_drain.py b/tests/gateway/test_wake_admitted_before_drain.py
index 3b9ea617dbd..4dd1f503b92 100644
--- a/tests/gateway/test_wake_admitted_before_drain.py
+++ b/tests/gateway/test_wake_admitted_before_drain.py
@@ -9,7 +9,7 @@ import asyncio
import pytest
from gateway.platforms.event import MessageEvent
-from gateway.wake import deliver_wake
+from gateway.wake import WakeNotAccepted, deliver_wake
from tests.gateway.restart_test_helpers import make_restart_runner, make_restart_source
WAKE = "[kanban] Task t_1 completed."
@@ -62,3 +62,34 @@ async def test_wake_queued_before_drain_survives_the_pending_discard():
{"final_response": "done"}, adapter, source, key
)
assert (event, text) == (None, None) # other follow-ups are still discarded
+
+
+async def test_wake_queued_before_a_drain_that_starts_in_the_busy_handlers_later_awaits_runs():
+ """The busy handler can await after it stores the wake; a drain that begins there must not
+ turn an admitted wake, whose producer already holds the receipt, into a discarded one.
+ Exactly one of two outcomes is correct: the receipt is withheld (WakeNotAccepted, as the
+ drain re-check from #137553 does), so the producer retries, or the wake runs. Never both,
+ never neither, whichever of the two PRs merges first."""
+ 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
+ store = adapter._busy_session_handler
+
+ async def store_then_drain(event, session_key):
+ handled = await store(event, session_key) # the wake is now the pending follow-up
+ runner._draining = True # /restart begins during the handler's later awaits
+ await asyncio.sleep(0)
+ return handled
+
+ adapter._busy_session_handler = store_then_drain
+ try:
+ await deliver_wake(adapter, text=WAKE, source=source)
+ receipted = True # the producer will not retry it
+ except WakeNotAccepted:
+ receipted = False # the producer keeps it and retries after the restart
+ event, text = await runner._run_agent_drain_pending(
+ {"final_response": "done"}, adapter, source, key
+ )
+ ran = text == WAKE and event.internal
+ assert ran == receipted, f"receipted={receipted} ran={ran}"