Skip to content

The agent that grows with you

fix(gateway): the app's turn on an old conversation reopens the row the chat's /new ended

Teknium584f6e5dd168
diff --git a/gateway/session_api.py b/gateway/session_api.py
index 7e0ecdac5bc..53e753189c9 100644
--- a/gateway/session_api.py
+++ b/gateway/session_api.py
@@ -280,7 +280,8 @@ def _heal_replacement(conn, caller, holder):
def reopen_api_session(authority, ref):
"""The drain's admitted turn reopens a finalized API row, as ``reopen_local_session`` does a
- local one: the binding keeps the route on it (``SessionStore._api_bound``)."""
+ local one: the binding keeps the route on it (``SessionStore._api_bound``). So does a turn of a
+ conversation its chat moved past (gateway/session_detached.py), whose row the chat's /new ended."""
target = authority.physical_target(ref)
row = authority.db.get_session(target)
if row is not None and row.get('end_reason') is not None:
diff --git a/gateway/session_authority.py b/gateway/session_authority.py
index 9313c6abf06..5f1d9b28d3f 100644
--- a/gateway/session_authority.py
+++ b/gateway/session_authority.py
@@ -1094,6 +1094,11 @@ class SessionAuthority:
from gateway.session_runtime_workers import tracked_write
check_api_turn(self, ref, first['payload'])
await tracked_write(self, partial(reopen_api_session, self, ref))
+ elif first is not None and live.detached:
+ # The chat's /new ended its row; the app's turn makes it live again, as an API row's does.
+ from gateway.session_api import reopen_api_session
+ from gateway.session_runtime_workers import tracked_write
+ await tracked_write(self, partial(reopen_api_session, self, ref))
if first is not None and self._classic_turn_holds(live.route):
# A classic turn (fallback wake, CLI handoff, legacy resume) holds the route's slot:
# a claim now would hand this row to its busy path and settle it unrun. The head
diff --git a/tests/gateway/test_detached_conversation_lifecycle.py b/tests/gateway/test_detached_conversation_lifecycle.py
index 3dcb406e7dd..1600984fbf3 100644
--- a/tests/gateway/test_detached_conversation_lifecycle.py
+++ b/tests/gateway/test_detached_conversation_lifecycle.py
@@ -70,6 +70,17 @@ async def test_what_the_old_conversation_schedules_for_here_names_the_app_not_th
assert origin is None or origin['platform'] == 'local', origin
+async def test_the_old_conversation_is_live_again_once_the_app_uses_it(monkeypatch):
+ """F4: the chat's /new ended A's row; the app's turn reopens it, as an API or local turn reopens
+ theirs, so its background results and a rotating compression are not refused as ended."""
+ _runner, _adapter, authority, _source, _sent, _ran, _gates, _lane, ref_a = await _moved_on(monkeypatch)
+ assert authority.db.get_session(ref_a.session_id)['end_reason'] == 'session_reset'
+ assert await _app_turn(authority, ref_a, 'from the app') == ('terminal', 'completed')
+ row = authority.db.get_session(ref_a.session_id)
+ assert (row['ended_at'], row['end_reason']) == (None, None)
+
+
@pytest.mark.asyncio
async def test_the_chats_resume_drops_the_agent_the_app_cached_for_the_old_conversation(monkeypatch):
"""F6: the agent cached under A's detached key goes when the chat takes A back, instead of being