Skip to content

The agent that grows with you

fix(gateway): rescue orphaned FIFO overflow when session goes idle (#99882)

salch-cred5f43a3ff4848
diff --git a/gateway/run.py b/gateway/run.py
index 27057ff85dd..e66a60bce51 100644
--- a/gateway/run.py
+++ b/gateway/run.py
@@ -9765,6 +9765,66 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
depth += 1
return depth
+ def _rescue_orphaned_overflow(self, session_key: str, adapter: Any) -> int:
+ """Stage any orphaned FIFO overflow into the pending slot (#99882).
+
+ The FIFO overflow (``queued_events``) drains only at the post-turn
+ promotion site (``_promote_queued_event`` inside the ``_run_agent``
+ drain). When a busy window ends without that drain running — the
+ #99882 shape: a follow-up queued during compression-in-flight lands
+ in overflow, compression finishes, the slot event's turn runs, but
+ the drain recursion exits before promoting (or the busy window ends
+ through an exception / interrupt / generation-bump exit that never
+ reaches the promotion site) — the overflow entries are silently
+ orphaned: never dispatched, never persisted, never logged.
+
+ This rescue runs at the point where a NEW event arrives for a
+ session that is NOT busy (the idle entry in
+ ``_process_message_priority``). If the session went idle with a
+ populated overflow, the orphaned events are re-staged in FIFO
+ order ahead of the incoming event's own enqueue, so arrival order
+ (#28503) is preserved: the orphaned follow-ups run first, then the
+ new message. The slot must be empty at this point (the session is
+ idle), so staging is a plain slot assignment.
+
+ Returns the number of orphaned events re-staged (0 when none).
+ """
+ try:
+ _q_state = self._peek_session_state(session_key)
+ overflow = _q_state.conversation.queued_events if _q_state else None
+ if not overflow:
+ return 0
+ pending_slot = getattr(adapter, "_pending_messages", None)
+ if not isinstance(pending_slot, dict) or pending_slot.get(session_key):
+ # Slot occupied (busy) or no slot storage — promotion owns
+ # this; do not fight it from the idle path.
+ return 0
+ # Only stage ONE orphan into the slot — the remaining overflow
+ # stays queued and will drain via the normal
+ # _promote_queued_event post-turn promotion. Staging more than
+ # one would clobber the slot (single-slot design).
+ pending_slot[session_key] = overflow.pop(0)
+ rescued = 1
+ if rescued:
+ logger.warning(
+ "Rescued %d orphaned FIFO overflow event(s) for idle session "
+ "%s — they were queued during a busy window but the post-turn "
+ "drain never promoted them (#99882)",
+ rescued,
+ session_key,
+ )
+ if overflow:
+ logger.warning(
+ "%d overflow event(s) still queued for session %s after "
+ "rescue staging (will drain via normal promotion)",
+ len(overflow),
+ session_key,
+ )
+ return rescued
+ except Exception:
+ logger.debug("FIFO overflow rescue failed for %s", session_key, exc_info=True)
+ return 0
+
@staticmethod
def _is_goal_continuation_event(event_or_text: Any) -> bool:
"""Return True for synthetic /goal continuation turns.
@@ -19712,6 +19772,52 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
_quick_key,
)
return _limit_message
+
+ # ── FIFO orphan rescue (#99882) ────────────────────────────────
+ # If this session went idle with a populated overflow (queued
+ # during a busy window whose post-turn drain never promoted —
+ # e.g. a compression-demoted follow-up after the compression
+ # window ended through an exit that skipped the promotion site),
+ # those events were silently orphaned. We are starting the next
+ # turn for this session NOW: re-stage the orphans in FIFO order
+ # and enqueue the incoming event behind them, so arrival order
+ # (#28503) holds: oldest orphan runs as this turn, the rest drain
+ # in order, the new message last. Skipped for control commands
+ # (/stop etc. own their own semantics) and internal events.
+ try:
+ _orphan_adapter = self._adapter_for_source(source)
+ if (
+ _orphan_adapter is not None
+ and not bool(getattr(event, "internal", False))
+ and not event.get_command()
+ and self._queue_depth(
+ _quick_key, adapter=_orphan_adapter
+ ) >= 1
+ and not _orphan_adapter._pending_messages.get(_quick_key)
+ ):
+ _rescued = self._rescue_orphaned_overflow(
+ _quick_key, _orphan_adapter
+ )
+ if _rescued:
+ # Orphans staged in the slot; park the incoming event
+ # behind them in the overflow so it runs AFTER the
+ # rescued chain (FIFO).
+ _head = _orphan_adapter._pending_messages.get(_quick_key)
+ if _head is not None:
+ # The slot head runs as this turn via the loop
+ # below; enqueue the incoming event into overflow.
+ self._session_state(_quick_key).conversation.queued_events.append(
+ event
+ )
+ # Swap: this turn now processes the oldest orphan.
+ event = _head
+ except Exception:
+ logger.debug(
+ "FIFO orphan rescue pre-claim failed for %s",
+ _quick_key,
+ exc_info=True,
+ )
+
_claim_state = self._session_state(_quick_key)
if _active_session_lease is not None:
_claim_state.turn.lease = _active_session_lease
diff --git a/tests/gateway/test_fifo_overflow_rescue.py b/tests/gateway/test_fifo_overflow_rescue.py
new file mode 100644
index 00000000000..932a7a735aa
--- /dev/null
+++ b/tests/gateway/test_fifo_overflow_rescue.py
@@ -0,0 +1,132 @@
+"""Regression tests for #99882: FIFO overflow orphan rescue.
+
+When a follow-up is demoted to /queue during compression-in-flight,
+it lands in SessionState.conversation.queued_events (overflow) with
+the current turn's event occupying adapter._pending_messages[session_key]
+(slot). After the slot's turn completes, _promote_queued_event moves
+the overflow head into the slot. When that drain never runs — the
+compression window ended through an exit that skipped the promotion
+site — the overflow is silently orphaned: never dispatched, never
+persisted, never logged.
+
+The rescue in GatewayRunner._rescue_orphaned_overflow stages one orphan
+into the slot on the next idle arrival, so FIFO order (#28503) holds.
+"""
+
+import asyncio
+from unittest.mock import MagicMock
+
+import pytest
+
+from gateway.platforms.base import (
+ BasePlatformAdapter,
+ MessageEvent,
+ MessageType,
+ Platform,
+ PlatformConfig,
+)
+from gateway.run import GatewayRunner
+
+
+class _StubAdapter(BasePlatformAdapter):
+ def __init__(self):
+ super().__init__(PlatformConfig(enabled=True, token="test"), Platform.TELEGRAM)
+
+ async def connect(self, *, is_reconnect: bool = False) -> bool:
+ return True
+
+ async def disconnect(self) -> None:
+ self._mark_disconnected()
+
+ async def send(self, chat_id, content, reply_to=None, metadata=None):
+ from gateway.platforms.base import SendResult
+
+ return SendResult(success=True, message_id="msg-1")
+
+ async def get_chat_info(self, chat_id):
+ return {"id": chat_id, "type": "dm"}
+
+
+def _text_event(text: str, msg_id: str) -> MessageEvent:
+ return MessageEvent(
+ text=text,
+ message_type=MessageType.TEXT,
+ source=MagicMock(chat_id="123", platform=Platform.TELEGRAM, profile=None),
+ message_id=msg_id,
+ )
+
+
+class TestRescueOrphanedOverflow:
+ def test_moves_overflow_head_to_empty_slot(self):
+ runner = GatewayRunner.__new__(GatewayRunner)
+ runner._queued_events = {}
+ # Minimal session_state with queued_events
+ adapter = _StubAdapter()
+ session_key = "telegram:user:1"
+ # Two overflow items orphaned after slot turn completed
+ runner._session_state(session_key).conversation.queued_events.extend(
+ [_text_event("orphan-1", "o1"), _text_event("orphan-2", "o2")]
+ )
+ # Slot empty (session went idle)
+ assert session_key not in adapter._pending_messages
+
+ rescued = runner._rescue_orphaned_overflow(session_key, adapter)
+
+ assert rescued == 1
+ # Slot now holds the oldest orphan
+ assert adapter._pending_messages[session_key].text == "orphan-1"
+ # Remaining orphan stays in overflow
+ overflow = runner._session_state(session_key).conversation.queued_events
+ assert len(overflow) == 1
+ assert overflow[0].text == "orphan-2"
+
+ def test_noop_when_slot_occupied(self):
+ runner = GatewayRunner.__new__(GatewayRunner)
+ runner._queued_events = {}
+ adapter = _StubAdapter()
+ session_key = "telegram:user:2"
+ runner._session_state(session_key).conversation.queued_events.append(
+ _text_event("orphan", "o1")
+ )
+ adapter._pending_messages[session_key] = _text_event("busy-slot", "slot")
+
+ rescued = runner._rescue_orphaned_overflow(session_key, adapter)
+
+ assert rescued == 0
+ assert adapter._pending_messages[session_key].text == "busy-slot"
+ assert len(runner._session_state(session_key).conversation.queued_events) == 1
+
+ def test_noop_when_no_overflow(self):
+ runner = GatewayRunner.__new__(GatewayRunner)
+ runner._queued_events = {}
+ adapter = _StubAdapter()
+ session_key = "telegram:user:3"
+
+ rescued = runner._rescue_orphaned_overflow(session_key, adapter)
+
+ assert rescued == 0
+ assert session_key not in adapter._pending_messages
+
+ def test_fifo_order_preserved_across_rescue_and_new_message(self):
+ """Oldest orphan runs first, new arrival last — FIFO (#28503)."""
+ runner = GatewayRunner.__new__(GatewayRunner)
+ runner._queued_events = {}
+ adapter = _StubAdapter()
+ session_key = "telegram:user:4"
+
+ # Two orphans from the lost window
+ runner._session_state(session_key).conversation.queued_events.extend(
+ [_text_event("orphan-1", "o1"), _text_event("orphan-2", "o2")]
+ )
+
+ # New message arrives for idle session — rescue stages orphan-1
+ rescued = runner._rescue_orphaned_overflow(session_key, adapter)
+ assert rescued == 1
+ # Simulate the caller enqueueing the new message behind the rescued chain
+ new_event = _text_event("new-msg", "new1")
+ runner._session_state(session_key).conversation.queued_events.append(new_event)
+
+ # Drain order: slot (orphan-1), then overflow[0] (orphan-2), then new-msg
+ assert adapter._pending_messages[session_key].text == "orphan-1"
+ overflow_texts = [e.text for e in runner._session_state(session_key).conversation.queued_events]
+ assert overflow_texts == ["orphan-2", "new-msg"]