Skip to content

The agent that grows with you

fix(gateway): a reminder the app set on an old conversation keeps firing after the chat resumes it or a restart

Tekniumd92f074b1d59
diff --git a/gateway/session_cron.py b/gateway/session_cron.py
index b5bb977958e..0cc474d4539 100644
--- a/gateway/session_cron.py
+++ b/gateway/session_cron.py
@@ -43,13 +43,21 @@ async def _admit_local_delivery(authority, chat_id, text, metadata):
from gateway.session_authorities import owner_scope
from gateway.session_automation import admit_route_wake
from gateway.session_local_recovery import local_adapter_map, restore_local_session
+ from gateway.session_route_recovery import mountable, restore_routed_session
with owner_scope(authority):
live = authority.sessions.get(chat_id)
- if live is None or not live.detached: # a conversation its chat moved past is live in the apps
+ if live is None and mountable(authority, chat_id):
+ # A messaging conversation a reminder set from the app names (gateway/session_detached.py),
+ # not mounted (a restart): it mounts off the chat, or on it when the chat names it again.
+ restore_routed_session(authority, chat_id)
+ elif live is None or live.source is None or live.source.platform == Platform.LOCAL:
restore_local_session(authority, chat_id) # a chat closed before this fire, as for completions
source = authority.sessions[chat_id].source
- return await admit_route_wake(authority.runner, local_adapter_map(authority)[Platform.LOCAL], source,
- text, 'cron', metadata)
+ # Off the chat its app viewers take it (as a note); back on the chat (its /resume) it is that
+ # conversation's own turn there.
+ adapter = (local_adapter_map(authority)[Platform.LOCAL] if source.platform == Platform.LOCAL
+ else authority.runner._adapter_for_source(source))
+ return await admit_route_wake(authority.runner, adapter, source, text, 'cron', metadata)
def deliver_to_local_chat(home, chat_id, text, *, diagnostic=False):
diff --git a/tests/gateway/test_detached_conversation_results.py b/tests/gateway/test_detached_conversation_results.py
new file mode 100644
index 00000000000..6c0d14c1430
--- /dev/null
+++ b/tests/gateway/test_detached_conversation_results.py
@@ -0,0 +1,42 @@
+"""What an old conversation started keeps landing in it after its chat moved on (re-review of #144840).
+
+Teknium's ruling: nothing a conversation the chat moved past (A) produces reaches the chat, and A stays
+fully usable from the apps, its reminders and background results included. Real GatewayRunner,
+DiscordAdapter (``send`` recorded), SessionAuthority, drain and SessionStore over a real SessionDB; the
+agent run is the stand-in of ``test_authority_lane_moves_on``.
+"""
+import asyncio
+from pathlib import Path
+
+import pytest
+
+from tests.gateway.test_authority_lane_moves_on import _chat
+from tests.gateway.test_detached_conversation_lifecycle import _moved_on
+from tests.waiting import async_wait_until
+
+
+def _noted(authority, session_id, text):
+ return any(text in str(message.get('content')) for message in authority.db.get_messages(session_id))
+
+
+async def test_a_reminder_the_app_set_on_the_old_conversation_fires_after_a_resume_and_after_a_restart(monkeypatch):
+ """N1: the reminder names A (``local:<A>``). Back on the chat (its ``/resume``) A's fire is A's own
+ turn there; with A not mounted (any restart) it mounts off the chat and is written into A. It is
+ never refused for a local receipt A never had, so a recurring reminder keeps firing."""
+ from gateway.session_cron import deliver_to_local_chat
+ from gateway.session_mutations import retire_live_sessions
+ _runner, adapter, authority, source, sent, ran, _gates, lane, ref_a = await _moved_on(monkeypatch)
+ a, home = ref_a.session_id, Path(authority.db.db_path).parent
+
+ await _chat(adapter, source, sent, '/resume First chat')
+ assert await asyncio.to_thread(deliver_to_local_chat, home, a, 'fire after resume') is None
+ await async_wait_until(lambda: f"reply to 'fire after resume' from {a}" in sent, timeout=10, what="A's turn in the chat")
+ assert [turn[1:] for turn in ran if turn[0] == 'fire after resume'] == [(a, lane)]
+
+ await _chat(adapter, source, sent, '/new')
+ retire_live_sessions(authority, [a]) # what a restart leaves: A not mounted
+ chat_before = len(sent)
+ assert await asyncio.to_thread(deliver_to_local_chat, home, a, 'fire after restart') is None
+ await async_wait_until(lambda: _noted(authority, a, 'fire after restart'), timeout=10, what='the note in A')
+ assert authority.sessions[a].detached and sent[chat_before:] == []