Skip to content

The agent that grows with you

fix(gateway): prove the compression child's owner before writing to it

caya8205-2a60e04a32ce8
diff --git a/gateway/session.py b/gateway/session.py
index 85f11d3b6c5..5a6dc77d866 100644
--- a/gateway/session.py
+++ b/gateway/session.py
@@ -1323,6 +1323,13 @@ class SessionStore:
# Memoized so the per-key store lookup stays a dict hit instead of a
# profile-directory stat on every transcript append.
self._profile_home_cache: Dict[str, Optional[Path]] = {}
+ # session_id -> owning routing key, for ids whose ownership is already
+ # proven but not yet published in ``_entries``. The compression
+ # continuation is the case that needs it: the child row is written
+ # before its reroute is published, so an index lookup would miss and
+ # fall back to the ambient store. Entries are dropped as soon as
+ # routing publishes.
+ self._session_owner_hints: Dict[str, str] = {}
from gateway.session_db_recovery import RecoverableHandleCache
self._db_handle_cache = RecoverableHandleCache(
@@ -1496,26 +1503,39 @@ class SessionStore:
# fallback rather than taking routing down.
return None
- def _db_for_session_id(self, session_id: Optional[str]):
- """The SessionDB holding *session_id*'s row.
+ def _owner_key_for_session_id(self, session_id: Optional[str]) -> Optional[str]:
+ """The routing key that owns *session_id*, or None.
- Transcript, compression and rewind entry points are addressed by
- session id rather than routing key, so recover the owning profile
- from the in-memory index. Deliberately lock-free: several of those
- callers already hold ``_lock``, and a miss simply falls back to the
- ambient store — the behavior that predates ``_db_for_key``.
+ The published index is authoritative; ``_session_owner_hints`` covers
+ the window where ownership is already proven but routing has not been
+ published yet. Deliberately lock-free: several callers already hold
+ ``_lock``.
"""
if not session_id:
- return self._db
- key = None
+ return None
try:
for entry in list(self._entries.values()):
if entry.session_id == session_id:
- key = entry.session_key
- break
+ return entry.session_key
except Exception:
- key = None
- return self._db_for_key(key)
+ pass
+ # getattr: suites build bare stores via ``object.__new__`` and never
+ # run __init__, the same reason the other optional maps are read
+ # defensively here.
+ return (getattr(self, "_session_owner_hints", None) or {}).get(session_id)
+
+ def _db_for_session_id(self, session_id: Optional[str]):
+ """The SessionDB holding *session_id*'s row.
+
+ Transcript, compression and rewind entry points are addressed by
+ session id rather than routing key, so the owning profile is
+ recovered from the index (or from a pre-published ownership hint).
+ An id nothing knows about still falls back to the ambient store —
+ the behavior that predates ``_db_for_key``.
+ """
+ if not session_id:
+ return self._db
+ return self._db_for_key(self._owner_key_for_session_id(session_id))
def close_all_db_handles(self) -> None:
"""Close every SessionDB handle this store opened, one per resolved path.
@@ -3938,13 +3958,38 @@ class SessionStore:
# ``get_compression_tip`` returns the input id when no
# continuation exists; adopt only a different, still-live
# tip, otherwise fail closed as before.
+ #
+ # The parent's id IS published in the routing index, so
+ # its owner is already proven; the continuation's id is
+ # not published until after the child write succeeds
+ # (below), so resolving the child by id would miss and
+ # fall back to the ambient store. Carry the proven handle
+ # instead of re-deriving it from an id nothing points at
+ # yet. ``_owner_db`` cannot be None here — the parent
+ # append above just reached a real DB to raise this.
+ _owner_key = self._owner_key_for_session_id(session_id)
+ _owner_db = self._db_for_session_id(session_id)
child_id = ""
- tip = self._db_for_session_id(session_id).get_compression_tip(session_id)
- if tip and tip != session_id:
- tip_row = self._db_for_session_id(session_id).get_session(tip)
- if tip_row is not None and tip_row.get("ended_at") is None:
- child_id = str(tip)
+ if _owner_db is not None:
+ tip = _owner_db.get_compression_tip(session_id)
+ if tip and tip != session_id:
+ tip_row = _owner_db.get_session(tip)
+ if tip_row is not None and tip_row.get("ended_at") is None:
+ child_id = str(tip)
if child_id:
+ # Record the child's owner BEFORE writing to it. The
+ # reroute and the _entries update are published only
+ # after this write succeeds — that ordering is
+ # load-bearing for backlog order — so an index lookup
+ # here would miss and fall back to the ambient store,
+ # which is a live handle and would slip past the
+ # fail-closed guard.
+ if _owner_key:
+ _hints = getattr(self, "_session_owner_hints", None)
+ if _hints is None:
+ _hints = {}
+ self._session_owner_hints = _hints
+ _hints[child_id] = _owner_key
try:
self._append_transcript_message(child_id, msg)
except Exception as reroute_exc:
@@ -3981,6 +4026,11 @@ class SessionStore:
if entry.session_id == session_id:
entry.session_id = child_id
self._save()
+ # Routing now points at the child, so the index is
+ # authoritative again and the hint has no more work.
+ _hints = getattr(self, "_session_owner_hints", None)
+ if _hints:
+ _hints.pop(child_id, None)
if not pending:
return
msg = pending[0]
@@ -4075,8 +4125,6 @@ class SessionStore:
# A named profile with no resolvable home yet. Defer instead of
# writing the row into the ambient store — the caller queues the
# message and a later attempt lands it once the profile exists.
- # Reached via the compression-child id, which is not the id the
- # entry-point guard in append_to_transcript already checked.
raise RuntimeError(
f"no owning session store for {session_id}; deferring transcript write"
)
diff --git a/tests/gateway/test_multiplex_session_db_profile_scope.py b/tests/gateway/test_multiplex_session_db_profile_scope.py
index 56a72449e09..1f60768f9e8 100644
--- a/tests/gateway/test_multiplex_session_db_profile_scope.py
+++ b/tests/gateway/test_multiplex_session_db_profile_scope.py
@@ -26,6 +26,7 @@ where they always did.
import asyncio
import sqlite3
+from datetime import datetime, timezone
from pathlib import Path
from unittest.mock import MagicMock, patch
@@ -33,7 +34,7 @@ import pytest
from gateway.config import GatewayConfig
from gateway.platforms.base import MessageEvent, Platform, SessionSource
-from gateway.session import SessionStore
+from gateway.session import SessionEntry, SessionStore
from hermes_constants import (
get_hermes_home,
reset_hermes_home_override,
@@ -679,3 +680,61 @@ def test_profile_resolution_failure_fails_closed(multiplex_homes, monkeypatch):
assert store._db_for_key(key) is None
assert _session_ids(root / "state.db") == set()
+
+
+def test_compression_child_write_stays_in_the_parents_profile_store(multiplex_homes):
+ """The continuation write must not fall back to root before it is routed.
+
+ ``_append_to_transcript_serialized`` writes the compression child BEFORE
+ publishing the reroute and the ``_entries`` update — that ordering is
+ load-bearing for backlog order — so at that moment nothing in the routing
+ index points at the child id. Resolving the child by id therefore misses
+ and lands on the ambient store, which is a live handle and slips past the
+ fail-closed guard. The parent's owner is already proven, so it is carried
+ into the child write instead.
+
+ Physical regression: real stores on disk, no active profile scope.
+ """
+ root, profile = multiplex_homes
+ store = _multiplex_store(root)
+ key = "agent:fitness:telegram:dm:777"
+ parent_id = "20260830_100000_parent01"
+ child_id = "20260830_100500_child001"
+
+ token = set_hermes_home_override(str(profile))
+ try:
+ db = store._db
+ db.create_session(parent_id, "telegram")
+ db.create_session(child_id, "telegram", parent_session_id=parent_id)
+ db.end_session(parent_id, "compression")
+ finally:
+ reset_hermes_home_override(token)
+
+ entry = SessionEntry(
+ session_key=key,
+ session_id=parent_id,
+ created_at=datetime.now(timezone.utc),
+ updated_at=datetime.now(timezone.utc),
+ )
+ store._entries[key] = entry
+
+ # Background surface: no profile scope installed anywhere.
+ store.append_to_transcript(parent_id, {"role": "user", "content": "after-compaction"})
+
+ # 1. the row landed on the child, in the profile store
+ token = set_hermes_home_override(str(profile))
+ try:
+ rows = store._db.get_messages(child_id)
+ finally:
+ reset_hermes_home_override(token)
+ assert [r["content"] for r in rows] == ["after-compaction"]
+
+ # 2. the pending queue drained
+ assert not store._dirty_transcripts.get(parent_id)
+
+ # 3. routing advanced onto the child
+ assert store._transcript_reroutes.get(parent_id) == child_id
+ assert store._entries[key].session_id == child_id
+
+ # 4. root was never touched
+ assert _session_ids(root / "state.db") == set()