Skip to content

The agent that grows with you

fix(gateway): a late route announcement cannot hand the chat back its old conversation

Tekniumdd2ae84a40f3
diff --git a/gateway/session_detached.py b/gateway/session_detached.py
index decae974487..17e926d46b6 100644
--- a/gateway/session_detached.py
+++ b/gateway/session_detached.py
@@ -144,7 +144,15 @@ def watch_routes(runner, store) -> None:
return
def name():
- authority.name_lane(route, authority.logical_owner(session_id), origin)
+ # Writers announce outside the store lock, so announcements can land out of order: one
+ # the store has since moved past (a worker's /resume overtaken by a /new) would hand the
+ # chat back a conversation it left; the newer write announces itself. Compared by logical
+ # owner: a compression repoint since (unannounced) is still the same conversation.
+ current = store.lookup_by_session_key(route)
+ owner = authority.logical_owner(session_id)
+ if current is None or authority.logical_owner(current.session_id) != owner:
+ return
+ authority.name_lane(route, owner, origin)
try:
on_loop = asyncio.get_running_loop() is loop
except RuntimeError:
diff --git a/tests/gateway/test_detached_conversation_lifecycle.py b/tests/gateway/test_detached_conversation_lifecycle.py
index 82e7d2ba221..6a81d899f49 100644
--- a/tests/gateway/test_detached_conversation_lifecycle.py
+++ b/tests/gateway/test_detached_conversation_lifecycle.py
@@ -241,3 +241,37 @@ async def test_a_reminder_the_app_set_on_the_old_conversation_fires_into_it(monk
for message in authority.db.get_messages(ref_a.session_id)),
timeout=10, what='the reminder note in A')
assert sent[chat_before:] == []
+
+
+async def test_a_late_route_announcement_never_hands_the_chat_back_its_old_conversation(monkeypatch):
+ """Review on #144840: a worker's ``/resume`` of A announces its route after a newer ``/new``
+ moved the chat on to C. The stale announcement must not reattach A to the chat: the app's next
+ prompt on A still runs in A, off the chat, and the chat keeps C."""
+ import threading
+ runner, adapter, authority, source, sent, ran, _gates, lane, ref_a = await _moved_on(monkeypatch)
+ store = runner.session_store
+ announce, paused, release = store._announce_route, threading.Event(), threading.Event()
+
+ def held(session_key, entry): # the worker pauses after its store write, before announcing
+ if entry.session_id == ref_a.session_id and not paused.is_set():
+ paused.set()
+ release.wait(10)
+ announce(session_key, entry)
+ monkeypatch.setattr(store, '_announce_route', held)
+ worker = asyncio.create_task(asyncio.to_thread(store.switch_session, lane, ref_a.session_id))
+ await async_wait_until(paused.is_set, what='the worker to pause before announcing')
+ await _chat(adapter, source, sent, '/new')
+ await _chat(adapter, source, sent, 'newest')
+ c = store.peek_session_id(lane)
+ assert c != ref_a.session_id
+ release.set()
+ await worker # its announcement was handed to the loop before the worker's result, so it has run
+
+ assert store.peek_session_id(lane) == c
+ chat_replies = len(sent)
+ assert await _app_turn(authority, ref_a, 'app prompt on A') == ('terminal', 'completed')
+ assert ran[-1][:2] == ('app prompt on A', ref_a.session_id) and ran[-1][2] != lane
+ assert sent[chat_replies:] == [], f'the chat got the old conversation\'s reply: {sent[chat_replies:]}'
+ await _chat(adapter, source, sent, 'still C')
+ assert ran[-1][1:] == (c, lane) and sent[-1] == f"reply to 'still C' from {c}"