Skip to content

The agent that grows with you

fix(gateway): context exhaustion on an old conversation tells the app how to go on

Teknium24e3d05386fa
diff --git a/gateway/run_turn_persistence.py b/gateway/run_turn_persistence.py
index dc87bb75d54..279d949a429 100644
--- a/gateway/run_turn_persistence.py
+++ b/gateway/run_turn_persistence.py
@@ -346,10 +346,14 @@ class GatewayTurnPersistenceMixin:
# The caller addresses an API conversation by its id and every API door re-validates that
# binding (``restore_api_session``): a rotated entry would refuse each later request to it
# (``admission_conflict``). A conversation its chat moved past has no route of its own to
- # reset (gateway/session_detached.py). Either caller gets the typed ``compression_exhausted``
- # result instead, never a reset that did not happen.
+ # reset (gateway/session_detached.py): its app viewer gets the start-fresh guidance a
+ # failed overflow turn gives. Never a reset that did not happen.
logger.info("Keeping session %s after compression exhaustion.",
session_entry.session_id if session_entry else "?")
+ from gateway.run import _context_overflow_reply
+ guidance = _context_overflow_reply() if detached_route_of(source) else ""
+ if guidance not in (response or ""):
+ response = f"{response}\n\n{guidance}" if response else guidance
elif agent_result.get("compression_exhausted") and session_entry and session_key:
if not self._hmwa_defer_exhaustion_reset(session_entry, session_key, source):
session_entry = await self._hmwa_rotate_exhausted_session(session_entry, session_key, source)
diff --git a/tests/gateway/test_authority_lane_moves_on.py b/tests/gateway/test_authority_lane_moves_on.py
index c08ca781e3d..78ed1ba4cfb 100644
--- a/tests/gateway/test_authority_lane_moves_on.py
+++ b/tests/gateway/test_authority_lane_moves_on.py
@@ -158,7 +158,9 @@ async def test_the_old_conversation_never_drives_the_chats_topic_or_claims_a_res
entry = runner.session_store.get_or_create_session(detached)
response, kept = await runner._hmwa_compression_exhaustion_reset(
{'compression_exhausted': True}, 'too long', entry, entry.session_key, detached)
- assert (response, kept.session_id) == ('too long', ref_a.session_id)
+ # The app viewer is told how to go on (the guidance a failed overflow turn gives), never "reset".
+ from gateway.run import _context_overflow_reply
+ assert (response, kept.session_id) == (f'too long\n\n{_context_overflow_reply()}', ref_a.session_id)
@pytest.mark.asyncio