Skip to content

The agent that grows with you

fix(gateway): keep the deferred /queue overflow across the exhaustion reset

Tekniuma94108f99b64
diff --git a/gateway/run_turn_persistence.py b/gateway/run_turn_persistence.py
index 9811520e385..47b0cee2d6c 100644
--- a/gateway/run_turn_persistence.py
+++ b/gateway/run_turn_persistence.py
@@ -327,8 +327,13 @@ class GatewayTurnPersistenceMixin:
try:
await move_route_wakes(self, session_key, session_entry.session_id)
self._evict_cached_agent(session_key)
+ # The /queue overflow behind the deferred head slot is queued user input, like the
+ # re-homed native rows: it survives the boundary the funnel below otherwise clears.
+ queued = list(self._overflow_queue(session_key) or ())
# Conversation boundary: the funnel clears every conversation-scoped per-session dict.
self._clear_conversation_scope(session_key, reason="compression_exhausted_reset")
+ if queued:
+ self._session_state(session_key).conversation.queued_events.extend(queued)
if new_entry is not None:
# Re-point the Telegram topic binding at the fresh session, or the binding-heal walk
# switches the next message back onto the bloated child and re-triggers exhaustion
diff --git a/tests/gateway/test_native_fifo_context_exhaustion.py b/tests/gateway/test_native_fifo_context_exhaustion.py
index c7def218fb6..00414161b99 100644
--- a/tests/gateway/test_native_fifo_context_exhaustion.py
+++ b/tests/gateway/test_native_fifo_context_exhaustion.py
@@ -260,3 +260,23 @@ async def test_admission_reserved_on_the_old_route_commits_on_the_fresh_session(
await authority._drain(SessionRef(authority.profile_id, new))
assert executed == [("A", old.session_id), ("B", new), ("C", new), ("D", new)]
assert [r["request_id"] for r in list_session_admissions(db, session_id=old.session_id, pending_only=False)] == ["A"]
+
+
+async def test_exhaustion_reset_keeps_the_adapter_queue_for_the_fresh_session(native):
+ """A /queue chain deferred behind the exhausted turn (head in the adapter slot, rest in the
+ overflow) survives the reset's conversation-scope clear, in order, for the post-handler drain."""
+ runner, adapter, source = native.runner, native.adapter, native.source
+ _, receipt = await _admit(native, "A")
+ session_key = runner.session_store.lookup_by_session_key(
+ native.authority.sessions[receipt.ref.session_id].route).session_key
+ head, second = (MessageEvent(text=t, source=source, message_id=t) for t in ("Q1", "Q2"))
+ runner._enqueue_fifo(session_key, head, adapter)
+ runner._enqueue_fifo(session_key, second, adapter)
+ entry = runner.session_store.lookup_by_session_key(session_key)
+ result = {"failed": True, "compression_exhausted": True, "messages": []}
+ _, new_entry = await runner._hmwa_compression_exhaustion_reset(
+ result, "context exhausted", entry, session_key, source)
+ assert new_entry.session_id != entry.session_id
+ assert adapter._pending_messages[session_key] is head
+ assert runner._overflow_queue(session_key) == [second]