The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): read a routed profile's store in the cached agent's dead-session check
Totoro-qaq940f374a0110
diff --git a/gateway/run_turn_runner.py b/gateway/run_turn_runner.pyindex 76d8e5ec56c..38e29962903 100644--- a/gateway/run_turn_runner.py+++ b/gateway/run_turn_runner.py@@ -1017,8 +1017,10 @@ class TurnRunner:peek_sid = entry[3]dead = Falseif peek_sid is not None and ctx.session_id is not None and peek_sid != ctx.session_id:+ # The cache is keyed by session_key, so the snapshot's row lives in that key's profile+ # store; its id is no longer in the routing index once the self-heal moved the key on.with suppress(Exception):- dead = self._runner.session_store._is_session_ended_in_db(peek_sid)+ dead = self._runner.session_store._is_session_ended_in_db(peek_sid, session_key=ctx.session_key)return peek_sid, deaddef _current_message_count(self):diff --git a/gateway/session_lifecycle.py b/gateway/session_lifecycle.pyindex 06ecd9855bf..6327e6272b4 100644--- a/gateway/session_lifecycle.py+++ b/gateway/session_lifecycle.py@@ -58,7 +58,7 @@ def auto_continue_freshness_window() -> float:class SessionLifecycleMixin:"""SessionStore explicit boundaries and crash-recovery markers."""- def _is_session_ended_in_db(self, session_id: str) -> bool:+ def _is_session_ended_in_db(self, session_id: str, session_key: Optional[str] = None) -> bool:"""True iff state.db says the session is gone: ended (non-null end_reason) or hard-deleted(no row in a readable owning DB). No DB or a DB error -> False (same failure mode as``_prune_stale_sessions_locked``). Lets routing self-heal a session finalized or deleted@@ -75,8 +75,12 @@ class SessionLifecycleMixin:The store is resolved from the row's owning profile rather than the ambient scope: an unscopedbackground writer keeps its own copy of the same session, and comparing against that copy reports alive session as ended (#66887).++ Pass *session_key* when the owning key is known but the id may have left the routing index:+ after the self-heal re-homes the key, the id has no owner and would resolve to the launch+ store, which never holds a routed profile's rows (#118862)."""- db = self._db_for_session_id(session_id)+ db = self._db_for_key(session_key) if session_key else self._db_for_session_id(session_id)if not db or not session_id:return Falsetry:diff --git a/tests/gateway/test_multiplex_session_db_profile_scope.py b/tests/gateway/test_multiplex_session_db_profile_scope.pyindex 2def12f6e3a..584f5725356 100644--- a/tests/gateway/test_multiplex_session_db_profile_scope.py+++ b/tests/gateway/test_multiplex_session_db_profile_scope.py@@ -578,6 +578,44 @@ def test_unscoped_staleness_check_reads_the_key_owner_store(multiplex_homes):assert store._is_session_ended_in_db(entry.session_id) is True+def test_cached_agent_dead_check_reads_the_routed_profile_store(multiplex_homes):+ """The agent cache's dead-session guard must read the routed profile's store (#118862).++ After the #54878 self-heal re-homes the routing key, the cached agent's session id is in+ neither the routing index nor the owner hints, so resolving its store from the id falls+ back to the launch home. The routed profile's row is not there, the guard reports the+ ended session as live, and the stale agent keeps appending to it.+ """+ import threading+ from types import SimpleNamespace++ from gateway.run_turn_runner import TurnRunner++ root, profile = multiplex_homes+ store = _multiplex_store(root)++ token = set_hermes_home_override(str(profile))+ try:+ old = store.get_or_create_session(_profile_source())+ store._db.end_session(old.session_id, "session_reset") # ended outside the gateway+ healed = store.get_or_create_session(_profile_source())++ assert healed.session_key == old.session_key+ assert healed.session_id != old.session_id++ cache = {old.session_key: (object(), "sig", 0, old.session_id)}+ turn = TurnRunner(+ SimpleNamespace(session_store=store),+ SimpleNamespace(session_key=old.session_key, session_id=healed.session_id),+ )+ peek_sid, dead = turn._cached_sid_is_dead(threading.Lock(), cache)+ finally:+ reset_hermes_home_override(token)++ assert peek_sid == old.session_id+ assert dead is True++def test_default_namespace_keeps_ambient_resolution(multiplex_homes):"""Guardrail: the legacy ``agent:main`` namespace must not change stores.