Skip to content

The agent that grows with you

fix(gateway): an unresolvable profile store preserves the flush file instead of writing to the root store

kshitijk4poorf6978426a2f7
diff --git a/gateway/session_recovery.py b/gateway/session_recovery.py
index 9de0921d959..c1b45c9386f 100644
--- a/gateway/session_recovery.py
+++ b/gateway/session_recovery.py
@@ -197,11 +197,16 @@ class SessionRecoveryMixin:
recoverable reasons match; explicit boundaries do not). A row started after the flush
(``started_at > not_after``) cannot be the origin and is never adopted. Never mints a
session; None means the caller must preserve the flush file. ``db`` is the store owning
- the key, so the append lands in the right profile partition.
+ the key, so the append lands in the right profile partition. When that store cannot be
+ resolved (``_db_for_key`` fails closed for a profile without a reachable home) the answer
+ is None even if the routing map knows the id: appending to the ambient root store would
+ split one session identity across two physical stores (#66887/#102157).
"""
if not session_key:
return None
db = self._db_for_key(session_key)
+ if db is None:
+ return None
session_id = self.peek_session_id(session_key)
if session_id:
return session_id, db
diff --git a/gateway/shutdown_flush.py b/gateway/shutdown_flush.py
index 3d3a11e0142..21cc61b3053 100644
--- a/gateway/shutdown_flush.py
+++ b/gateway/shutdown_flush.py
@@ -268,7 +268,10 @@ def _recover_one_payload(session_db, path: Path, payload: Dict[str, Any], *,
"the flush file has been preserved", path)
return False
# session_key is a gateway routing key (e.g. "agent:main:telegram:..."); appending a row
- # needs the real session_id, which real payloads lack — the resolver supplies it.
+ # needs the real session_id, which real payloads lack — the resolver supplies it together with
+ # the store owning the key. ``session_db`` (the owned default) serves only payloads that already
+ # carry a session_id; a resolver-resolved payload goes to the resolver's db alone, never the
+ # ambient root store (a None db from the resolver is not a fallback signal — it is "preserve").
session_id, target_db = data.get("session_id", ""), session_db
if not session_id and session_resolver is not None:
try:
@@ -276,9 +279,8 @@ def _recover_one_payload(session_db, path: Path, payload: Dict[str, Any], *,
except Exception as exc:
logger.debug("Session key->id resolution failed for %s: %s", session_key, exc)
resolved = None
- if resolved:
- session_id, routed_db = resolved
- target_db = routed_db if routed_db is not None else session_db
+ if resolved and resolved[1] is not None:
+ session_id, target_db = resolved
if not session_id:
logger.warning("Cannot recover pending message for %s: no session_id in flush file and "
"session_key-to-id resolution failed. "
diff --git a/tests/gateway/test_session_recovery.py b/tests/gateway/test_session_recovery.py
index 455c703c700..434a3457c5b 100644
--- a/tests/gateway/test_session_recovery.py
+++ b/tests/gateway/test_session_recovery.py
@@ -28,9 +28,10 @@ def _store(tmp_path, db):
return store
-def test_resolve_profile_namespaced_key_does_not_adopt_main_row(tmp_path):
+def test_resolve_profile_namespaced_key_does_not_adopt_main_row(tmp_path, monkeypatch):
"""A profile-namespaced key must never resolve into the root store's
- ``agent:main`` row: the exact-key finder only matches the namespaced key."""
+ ``agent:main`` row: the exact-key finder only matches the namespaced key, and an
+ unresolvable profile store (``_db_for_key`` → None) yields None, never the root store."""
db = _FakeGatewayDB({"agent:main:whatsapp:dm:15551234567": {"id": "main-row"}})
store = _store(tmp_path, db)
assert (
@@ -39,6 +40,14 @@ def test_resolve_profile_namespaced_key_does_not_adopt_main_row(tmp_path):
)
assert db.queries and all("agent:test-bot" in q for q in db.queries)
assert not any(q.startswith("agent:main") for q in db.queries)
+ # Fail-closed store: when the profile's home is unresolvable, _db_for_key returns None and the
+ # resolver must answer None too — even with a routing-map hit — so the flush file is preserved
+ # instead of being appended to the ambient root store (#66887/#102157 split-identity class).
+ key = "agent:test-bot:whatsapp:dm:15551234567"
+ monkeypatch.setattr(store, "peek_session_id", lambda session_key: "routed-sid")
+ monkeypatch.setattr(store, "_db_for_key", lambda session_key: None)
+ assert store.resolve_session_id_for_key(key) is None
+ assert store.resolve_session_id_for_key(key, not_after=1700000000) is None
def test_resolve_db_fallback_rejects_row_started_after_flush(tmp_path):
diff --git a/tests/gateway/test_shutdown_flush.py b/tests/gateway/test_shutdown_flush.py
index 136a316912f..8c1b6e9a20a 100644
--- a/tests/gateway/test_shutdown_flush.py
+++ b/tests/gateway/test_shutdown_flush.py
@@ -116,16 +116,27 @@ def test_recover_payload_without_session_id_uses_resolver_and_deletes_file(
flush_file.write_text(json.dumps(payload), encoding="utf-8")
mock_db = MagicMock()
- resolver = MagicMock(return_value=("sid-resolved", None))
+ routed_db = MagicMock() # the store owning the key, as the resolver reports it
+ resolver = MagicMock(return_value=("sid-resolved", routed_db))
count = recover_pending_to_db(mock_db, session_resolver=resolver)
assert count == 1
resolver.assert_called_once_with("agent:main:whatsapp:dm:15551234567", not_after=ts)
- mock_db.append_message.assert_called_once_with(
+ routed_db.append_message.assert_called_once_with(
session_id="sid-resolved", role="user", content="lost message", timestamp=ts
)
+ # The resolver's db is authoritative: the owned default store never sees a resolved payload.
+ mock_db.append_message.assert_not_called()
assert not flush_file.exists()
+ # A resolver that names an id but no store (fail-closed profile home) preserves the file
+ # instead of appending to the owned root store.
+ flush_file.write_text(json.dumps(payload), encoding="utf-8")
+ resolver = MagicMock(return_value=("sid-resolved", None))
+ assert recover_pending_to_db(mock_db, session_resolver=resolver) == 0
+ mock_db.append_message.assert_not_called()
+ assert flush_file.exists()
+
def test_recover_closes_owned_db_when_unexpected_exception_escapes(
tmp_path, monkeypatch