The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): reminders and subscriptions created from the app on an old conversation never target the chat
Teknium4196427e40aa
diff --git a/gateway/run.py b/gateway/run.pyindex efda43e498c..6ee7a7dc87d 100644--- a/gateway/run.py+++ b/gateway/run.py@@ -3966,31 +3966,31 @@ class GatewayRunner("""Set session context variables (contextvars, not os.environ, so concurrent messages can'toverwrite each other). Returns reset tokens for ``_clear_session_env`` in a ``finally``."""from gateway.session_context import set_session_vars- # Async-delivery capability tells async tools whether this channel can wake a later turn. Default- # True keeps CLI/unknown paths working; stateless adapters (api_server) declare False.- _adapter = (getattr(self, "adapters", None) or {}).get(context.source.platform)+ from gateway.session_detached import session_env_source+ source = session_env_source(self, context.source)+ # Can async tools wake a later turn here? Default True (CLI/unknown); api_server declares False.+ _adapter = (getattr(self, "adapters", None) or {}).get(source.platform)_async_delivery = getattr(_adapter, "supports_async_delivery", True)- # #98619: an admitted API turn carries its session-id provenance in the admission settings;- # every other platform leaves it undeclared.+ # #98619: an admitted API turn carries its session-id provenance; other platforms leave it unset.from gateway.session_api_turn import api_session_vars_history_delivery = api_session_vars().get("session_history_delivery")return set_session_vars(- platform=context.source.platform.value,- chat_id=context.source.chat_id,- chat_type=str(context.source.chat_type) if context.source.chat_type else "",- chat_name=context.source.chat_name or "",- thread_id=str(context.source.thread_id) if context.source.thread_id else "",- user_id=str(context.source.user_id) if context.source.user_id else "",- user_id_alt=str(context.source.user_id_alt) if context.source.user_id_alt else "",- user_name=str(context.source.user_name) if context.source.user_name else "",- scope_id=str(getattr(context.source, "scope_id", "") or ""),- parent_chat_id=str(getattr(context.source, "parent_chat_id", "") or ""),+ platform=source.platform.value,+ chat_id=source.chat_id,+ chat_type=str(source.chat_type) if source.chat_type else "",+ chat_name=source.chat_name or "",+ thread_id=str(source.thread_id) if source.thread_id else "",+ user_id=str(source.user_id) if source.user_id else "",+ user_id_alt=str(source.user_id_alt) if source.user_id_alt else "",+ user_name=str(source.user_name) if source.user_name else "",+ scope_id=str(getattr(source, "scope_id", "") or ""),+ parent_chat_id=str(getattr(source, "parent_chat_id", "") or ""),session_key=context.session_key,# A cached agent binds HERMES_SESSION_ID only on the turn that built it; every later turn# (and a command it yields to the background) must still see its own session.session_id=context.session_id or "",- message_id=str(context.source.message_id) if context.source.message_id else "",- profile=getattr(context.source, "profile", "") or "",+ message_id=str(source.message_id) if source.message_id else "",+ profile=getattr(source, "profile", "") or "",async_delivery=_async_delivery,cron_session="", session_history_delivery=_history_delivery)diff --git a/gateway/session_detached.py b/gateway/session_detached.pyindex e86cfc59154..3f3405dcc17 100644--- a/gateway/session_detached.py+++ b/gateway/session_detached.py@@ -17,8 +17,10 @@ replies reach its viewers only, never the chat, which now gets only its new convsession's does: its sends publish nothing, viewers read the canonical event stream.The key carries no platform slot, so a reader that parses a chat out of a session key-(``gateway.run._parse_session_key``) finds none and delivers nowhere. The chat naming the-conversation again (``/resume``) moves it back onto the chat's route (``SessionAuthority.name_lane``).+(``gateway.run._parse_session_key``) finds none and delivers nowhere. Its turns bind the session+variables a Desktop session's turn binds (``session_env_source``), so a reminder, a kanban subscription+or a ``send_message`` it starts names the app, never the chat. The chat naming the conversation again+(``/resume``) moves it back onto the chat's route (``SessionAuthority.name_lane``).Every route change reaches ``name_lane`` from one place: the session store announces each conversation aroute comes to name (``SessionStore.route_named``: ``/new``, ``/resume``, ``/branch``, ``/handoff``,@@ -88,6 +90,18 @@ def drop_owner_hints(store, key) -> None:hints.pop(session_id, None)+def session_env_source(runner, source):+ """The source a turn's session variables (``HERMES_SESSION_PLATFORM``, ``_CHAT_ID``, ...) name.+ A conversation its chat moved past binds the local view a Desktop session's turn binds+ (``local_source``): whatever it schedules for "here" reaches the apps, never the chat."""+ key = detached_route_of(source)+ if key is None:+ return source+ from gateway.session_authorities import active_authority+ from gateway.session_local_recovery import local_source+ return local_source(active_authority(runner), detached_session_id(key), source.user_id)++def _route_authority(runner, store, route):"""The authority owning *route*: its key names the profile, whatever scope the writer runs in."""from gateway.session_authorities import authority_for_homediff --git a/tests/gateway/test_detached_conversation_lifecycle.py b/tests/gateway/test_detached_conversation_lifecycle.pyindex caa310d98cb..3dcb406e7dd 100644--- a/tests/gateway/test_detached_conversation_lifecycle.py+++ b/tests/gateway/test_detached_conversation_lifecycle.py@@ -53,6 +53,23 @@ async def test_the_chats_context_exhaustion_reset_moves_the_old_conversation_offassert sent[chat_replies:] == [], f'the chat got the old conversation\'s reply: {sent[chat_replies:]}'+async def test_what_the_old_conversation_schedules_for_here_names_the_app_not_the_chat(monkeypatch):+ """F3: a detached turn binds the session variables a Desktop session's turn binds, so a reminder,+ kanban subscription or send_message origin it creates never names the chat."""+ from gateway.session import build_session_context+ from gateway.session_context import get_session_env+ from tools.cronjob_job_args import _origin_from_env+ runner, _adapter, authority, _source, _sent, _ran, _gates, _lane, ref_a = await _moved_on(monkeypatch)+ live = authority.sessions[ref_a.session_id]+ entry = runner.session_store.get_or_create_session(live.source)+ with runner._session_env_scope(build_session_context(live.source, runner.config, entry)):+ env = {name: get_session_env(f'HERMES_SESSION_{name}') for name in ('PLATFORM', 'CHAT_ID', 'KEY')}+ origin = _origin_from_env()+ assert env == {'PLATFORM': 'local', 'CHAT_ID': ref_a.session_id, 'KEY': live.route}+ assert origin is None or origin['platform'] == 'local', origin++@pytest.mark.asyncioasync 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