Skip to content

The agent that grows with you

fix(gateway): an old conversation's background result follows it back onto the chat, and after a restart

Tekniumfa80e05a0f21
diff --git a/gateway/session_detached.py b/gateway/session_detached.py
index d8c33f6df65..decae974487 100644
--- a/gateway/session_detached.py
+++ b/gateway/session_detached.py
@@ -61,7 +61,8 @@ def detach(authority, live, session_id) -> None:
from gateway.session_local_recovery import local_adapter
adapter = local_adapter(authority) # its delivery; a messaging-only gateway has none yet
live.route = detached_key(live.route, session_id)
- live.source = replace_source(live.source, detached_route=live.route)
+ # No reply anchor: its replies reach the apps, and a background result's event is this very source.
+ live.source = replace_source(live.source, detached_route=live.route, message_id=None)
adapter.register_source(live.source) # what admits its background results (``authorize_source``)
@@ -201,7 +202,11 @@ async def follow_detached_pin(runner, evt):
key = str(evt.get('session_key') or '')
pinned = str(evt.get('parent_session_id') or '').strip()
from gateway.session_authorities import all_authorities
- if not key or not pinned or detached_session_id(key) or not all_authorities(runner):
+ if not key or not all_authorities(runner):
+ return evt
+ if (detached := detached_session_id(key)) is not None:
+ return _follow_detached_key(runner, evt, key, detached)
+ if not pinned:
return evt
store = runner.session_store
authority = _route_authority(runner, store, key)
@@ -215,6 +220,25 @@ async def follow_detached_pin(runner, evt):
row = authority.db.get_session(target)
if row is None or row.get('ended_at'):
return evt
+ live = _mounted(authority, session_id)
+ if live is None or live.route != detached_key(key, session_id):
+ return evt
+ return {**evt, 'session_key': live.route}
+
+
+def _follow_detached_key(runner, evt, key, session_id):
+ """A result of a turn that ran on detached *key*: on the chat's route once the chat names the
+ conversation again (its ``/resume``), as that conversation's own result there."""
+ authority = _route_authority(runner, runner.session_store, key)
+ live = _mounted(authority, session_id) if authority is not None else None
+ if live is None or live.route == key:
+ return evt
+ return {**evt, 'session_key': live.route}
+
+
+def _mounted(authority, session_id):
+ """*session_id*'s live session, mounted as its route would after a restart (off the chat when the
+ chat names another conversation); None when it cannot be mounted now."""
if session_id not in authority.sessions:
from gateway.session_authorities import owner_scope
from gateway.session_route_recovery import restore_routed_session
@@ -223,11 +247,8 @@ async def follow_detached_pin(runner, evt):
with owner_scope(authority):
restore_routed_session(authority, session_id)
except RuntimeStoreError: # draining: the drain retries it
- return evt
- live = authority.sessions.get(session_id)
- if live is None or live.route != detached_key(key, session_id):
- return evt
- return {**evt, 'session_key': live.route}
+ return None
+ return authority.sessions.get(session_id)
def detached_live_source(runner, key):
diff --git a/tests/gateway/test_detached_conversation_results.py b/tests/gateway/test_detached_conversation_results.py
index 4901fad9cbc..8ebfb3815f6 100644
--- a/tests/gateway/test_detached_conversation_results.py
+++ b/tests/gateway/test_detached_conversation_results.py
@@ -95,6 +95,23 @@ async def test_a_result_the_chat_started_on_the_old_conversation_lands_in_it_onc
assert sent[chat_before:] == [], f'the chat got the old conversation\'s result: {sent[chat_before:]}'
+async def test_a_result_of_the_apps_turn_that_finishes_after_the_chat_resumed_the_conversation_runs_there(monkeypatch):
+ """A process the app's turn on A started (on A's own key) finishes after the chat ``/resume``d A:
+ it runs as A's turn on the chat's route, never refused for a key that no longer names A."""
+ from tests.gateway.test_detached_conversation_lifecycle import _process_event
+ runner, adapter, authority, source, sent, ran, _gates, lane, ref_a = await _moved_on(monkeypatch)
+ a = ref_a.session_id
+ assert await _app_turn(authority, ref_a, 'start a job') == ('terminal', 'completed')
+ evt = _process_event(runner, authority.sessions[a], a, exit_code=0, output='done')
+ await _chat(adapter, source, sent, '/resume First chat')
+ ran_before = len(ran)
+
+ assert await runner._inject_watch_notification('[process proc_1 finished]', evt) is True
+ await async_wait_until(lambda: len(ran) > ran_before, timeout=10, what="A's result turn")
+ assert [turn[1:] for turn in ran[ran_before:]] == [(a, lane)]
+
+
@pytest.mark.asyncio
async def test_a_secondary_profiles_old_conversation_gets_its_result_after_the_source_cache_forgot_it(
tmp_path, monkeypatch):
@@ -125,7 +142,8 @@ async def test_a_secondary_profiles_old_conversation_gets_its_result_after_the_s
runner.session_authorities = SessionAuthorities(root, multiplexed=True)
runner.session_authorities.add(root, launch)
runner.session_authorities.add(profile, fitness, name='fitness')
- source = SessionSource(platform=Platform.TELEGRAM, chat_id='555', chat_type='dm', user_id='u1', profile='fitness')
+ source = SessionSource(platform=Platform.TELEGRAM, chat_id='555', chat_type='dm', user_id='u1', profile='fitness',
+ message_id='chat-message-1') # as the chat's last inbound left it
route = runner.session_store._generate_session_key(source)
assert route.startswith('agent:fitness:')
a = 'conversation-a'