The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): a receipted wake queued behind human follow-ups still runs in a drain
kshitijk4poor05d7b5af1c42
diff --git a/gateway/run_busy.py b/gateway/run_busy.pyindex 2273eefbfbd..f820afeb386 100644--- a/gateway/run_busy.py+++ b/gateway/run_busy.py@@ -143,6 +143,20 @@ class GatewayBusySessionMixin:# else: no adapter — leave the head in place so we don't silently drop it.return pending_event+ def _take_wake_admitted_before_drain(self, session_key: str, adapter: Any) -> Optional[MessageEvent]:+ """Pop the first wake admitted before the drain began from the pending slot, else from the+ overflow FIFO, keeping the slot filled while overflow remains; every other event stays+ queued. Its producer holds a receipt and will not retry it, so a follow-up ahead of it that+ the drain rejects must not strand it."""+ slot = getattr(adapter, "_pending_messages", None)+ if slot and getattr(slot.get(session_key), "_admitted_before_drain", False) is True:+ return self._promote_queued_event(session_key, adapter, slot.pop(session_key))+ overflow = self._overflow_queue(session_key) or []+ for index, event in enumerate(overflow):+ if event._admitted_before_drain is True:+ return overflow.pop(index)+ return None+def _defer_past_exhaustion_reset(self, result: Any, adapter: Any, source: SessionSource, session_key: str, pending_event: MessageEvent,) -> None:diff --git a/gateway/run_turn.py b/gateway/run_turn.pyindex f090da4a75f..e081f2984d2 100644--- a/gateway/run_turn.py+++ b/gateway/run_turn.py@@ -1750,13 +1750,12 @@ class GatewayTurnMixin(GatewayTurnPrepareMixin, GatewayTurnHygieneMixin, Gatewaypending_event = Nonepending = None- 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(),- )- pending_event = None- pending = None+ if self._draining and getattr(pending_event, "_admitted_before_drain", False) is not True:+ if pending_event or pending:+ logger.info("Discarding pending follow-up for session %s during gateway %s", session_key or "?", self._status_action_label())+ # Without a result the caller runs nothing next: leave the queue to the adapter.+ pending_event = self._take_wake_admitted_before_drain(session_key, adapter) if result and adapter and session_key else None+ pending = (pending_event.text or _build_media_placeholder(pending_event)) if pending_event is not None else Nonereturn pending_event, pendingasync def _run_agent_deliver_first_response(diff --git a/tests/gateway/test_wake_admitted_before_drain.py b/tests/gateway/test_wake_admitted_before_drain.pyindex 9b221a86ce2..975ff66643b 100644--- a/tests/gateway/test_wake_admitted_before_drain.py+++ b/tests/gateway/test_wake_admitted_before_drain.py@@ -64,6 +64,29 @@ async def test_wake_queued_before_drain_survives_the_pending_discard():assert (event, text) == (None, None) # other follow-ups are still discarded+async def test_wake_queued_behind_human_follow_ups_still_runs_during_the_drain():+ """Two human follow-ups ahead of the wake: the drain discards the first and stages the second+ for the drain gate to reject, which used to strand the receipted wake behind it. The wake runs+ next; the follow-ups around it stay queued in order, as they do without a wake."""+ 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+ for text in ("first human follow-up", "second human follow-up"):+ runner._enqueue_fifo(key, MessageEvent(text=text, source=source), adapter)+ await deliver_wake(adapter, text=WAKE, source=source)+ runner._enqueue_fifo(key, MessageEvent(text="third human follow-up", source=source), adapter)+ runner._draining = True+ # A turn that ended without a result leaves the queue to the adapter.+ assert await runner._run_agent_drain_pending(None, adapter, source, key) == (None, None)+ event, text = await runner._run_agent_drain_pending(+ {"final_response": "done"}, adapter, source, key+ )+ assert text == WAKE and event.internal+ assert adapter._pending_messages[key].text == "second human follow-up"+ assert [e.text for e in runner._overflow_queue(key)] == ["third human follow-up"]++@pytest.mark.asyncioasync def test_wake_queued_before_a_drain_that_starts_in_the_busy_handlers_later_awaits_runs():"""Guard for a busy handler that awaits after it stores the wake (no in-tree handler does