The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): keep a retired route out of the sessions.json mirror
unsupportedpastels084067a4e5b4
diff --git a/gateway/session_persistence.py b/gateway/session_persistence.pyindex 3ffa9b6a491..8983395c952 100644--- a/gateway/session_persistence.py+++ b/gateway/session_persistence.py@@ -329,19 +329,25 @@ class SessionPersistenceMixin:self.sessions_dir.mkdir(parents=True, exist_ok=True)db_load_succeeded = self._load_routing_rows_locked()db_had_entries = db_load_succeeded and bool(self._entries)- self._import_legacy_sessions_json(db_had_entries)+ mirror_retired = self._import_legacy_sessions_json(db_had_entries)self._loaded = Trueself._routing_db_loaded = db_load_succeededself._routing_fallback_baseline = None if db_load_succeeded else self._entries_as_dicts()+ if mirror_retired and db_load_succeeded:+ # Rewrite the mirror while the fences that kept those routes out still hold: a prune's+ # ages out, and the import would then restore the retired route.+ self._save()# A hard crash skips graceful shutdown and leaves sessions.json pointing at ended sessions.self._prune_stale_sessions_locked()- def _import_legacy_sessions_json(self, db_had_entries: bool) -> None:- """Legacy import: sessions.json fills only keys the DB lacks. Lock held."""+ def _import_legacy_sessions_json(self, db_had_entries: bool) -> int:+ """Legacy import: sessions.json fills only keys the DB lacks. Lock held. Returns how many+ mirror entries it skipped as retired routes."""from gateway.session import SessionEntrysessions_file = self.sessions_dir / "sessions.json"+ retired = 0if not sessions_file.exists():- return+ return retiredtry:with open(sessions_file, "r", encoding="utf-8-sig") as f:data = json.load(f)@@ -360,6 +366,7 @@ class SessionPersistenceMixin:db = self._routing_dbif db is not None and route_retired(db, key, json.dumps({'session_id': entry_data['session_id']})):+ retired += 1continueself._entries[key] = SessionEntry.from_dict(entry_data)imported += 1@@ -371,6 +378,7 @@ class SessionPersistenceMixin:"state.db routing table", imported, "y" if imported == 1 else "ies")except Exception as e:print(f"[gateway] Warning: Failed to load sessions: {e}")+ return retireddef _prune_stale_sessions_locked(self) -> None:"""Remove routing entries whose session has ended in state.db (startup, lock held). Stale ==@@ -513,8 +521,12 @@ class SessionPersistenceMixin:replacer = self._routing_db_method("replace_gateway_routing_entries")if replacer is not None:try:- replacer({k: json.dumps(v) for k, v in data.items()}, scope=self._routing_scope())+ retired = replacer({k: json.dumps(v) for k, v in data.items()}, scope=self._routing_scope())db_saved = True+ # A retired route the save skipped stays out of the mirror too: its fence may+ # age out (a prune's), and the next start's legacy import would restore it.+ if isinstance(retired, set) and retired:+ data = {k: v for k, v in data.items() if k not in retired}except Exception as exc:logger.warning("gateway.session: state.db routing save failed: %s", exc)if getattr(self, "_write_sessions_json", True) or not db_saved:diff --git a/hermes_state_gateway.py b/hermes_state_gateway.pyindex 940b9cbf73e..c8f2f7a017a 100644--- a/hermes_state_gateway.py+++ b/hermes_state_gateway.py@@ -308,19 +308,23 @@ class SessionGatewayMixin:(scope, session_key, entry_json, time.time(), route_namespace(session_key)),)- def replace_gateway_routing_entries(self, entries: dict[str, str], *, scope: str = "") -> None:+ def replace_gateway_routing_entries(self, entries: dict[str, str], *, scope: str = "") -> set[str]:"""Atomically replace the routing index for *scope* (keys absent from *entries*- are removed); other scopes untouched."""+ are removed); other scopes untouched. Returns the keys skipped as retired routes, which+ the sessions.json mirror must leave out too."""from hermes_state_mutation_retirement import route_namespace, route_retired_sqlnow = time.time()+ rows = [(scope, k, v, now, route_namespace(k)) for k, v in entries.items() if k and v]def _do(conn):conn.execute("DELETE FROM gateway_routing WHERE scope = ?", (scope,))- if entries:+ if rows:conn.executemany("INSERT INTO gateway_routing (scope, session_key, entry_json, updated_at) "f"SELECT ?1, ?2, ?3, ?4 WHERE NOT {route_retired_sql('?3', '?5')}",- [(scope, k, v, now, route_namespace(k)) for k, v in entries.items() if k and v])- self._execute_write(_do)+ rows)+ kept = {r[0] for r in conn.execute("SELECT session_key FROM gateway_routing WHERE scope = ?", (scope,))}+ return {row[1] for row in rows} - kept+ return self._execute_write(_do)def load_gateway_routing_entries(self, *, scope: str = "") -> dict[str, str]:"""Load routing entries for *scope* as {session_key: entry_json}."""diff --git a/tests/gateway/test_secondary_route_fence_scope.py b/tests/gateway/test_secondary_route_fence_scope.pyindex 1fba57bebc9..fd85e7536bf 100644--- a/tests/gateway/test_secondary_route_fence_scope.py+++ b/tests/gateway/test_secondary_route_fence_scope.py@@ -185,9 +185,79 @@ async def test_a_shared_fence_an_earlier_release_wrote_still_guards_every_route(with store._lock:store._save()assert key not in _durable(store)- assert key in json.loads((store.sessions_dir / 'sessions.json').read_text())- store._entries.pop(key)+ assert key not in json.loads((store.sessions_dir / 'sessions.json').read_text())+ store._save_sessions_json({key: store._entries.pop(key).to_dict()}) # an earlier release's unfiltered mirrorwith store._lock:store._import_legacy_sessions_json(True)assert key not in store._entries, 'the sessions.json mirror restored a fenced route'assert _conflict(served.launch.db, 'gone') == ('session_deleted', 'Session was deleted; its id cannot be reused')+++def _mirror(store):+ return json.loads((store.sessions_dir / 'sessions.json').read_text())+++def _restart(store):+ """The next gateway start's routing load: state.db rows, then the sessions.json import."""+ with store._lock:+ store._entries.clear()+ store._loaded = False+ store._ensure_loaded_locked()+++async def _pruned_alpha_route(served, tmp_path):+ """``(key, sid, stale)``: alpha's routed session, pruned through the owner path after a+ worker took a whole-index snapshot that still names its route."""+ from gateway.run import _profile_runtime_scope+ from gateway.session_local import create_local_session+ store, key = served.store, 'agent:alpha:telegram:333'+ with _profile_runtime_scope(served.home, {}):+ ref = create_local_session(served.alpha, served.actor, {+ 'request_id': 'r', 'source': 'gui', 'cwd': str(tmp_path), 'model': 'frozen', 'toolsets': []})+ store._entries[key] = _entry(key, ref.session_id)+ with store._lock:+ stale = store._snapshot_routing_locked()+ await _prune(served, ref)+ return key, ref.session_id, stale+++async def test_a_delayed_snapshot_cannot_restore_a_pruned_route_once_its_fence_ages_out(tmp_path, monkeypatch):+ """The delayed snapshot is filtered in ``gateway_routing``, and in its sessions.json mirror too:+ otherwise, once the prune's shared fence aged out, the next start imported the mirror's route+ (its session row is gone, and the stale pass keeps routes without a row). The id itself is free+ again: alpha recreating it routes as usual."""+ async with _served(tmp_path, monkeypatch) as served:+ store = served.store+ key, sid, stale = await _pruned_alpha_route(served, tmp_path)+ store._persist_routing_data(*stale)+ assert key not in _durable(store)+ assert key not in _mirror(store), 'the sessions.json mirror kept the pruned route'+ _past_the_horizon(served.alpha.db, served.launch.db)+ _restart(store)+ assert key not in store._entries, 'a pruned route came back on startup after its fence aged out'++ served.alpha.db.create_session(sid, source='telegram')+ store._entries[key] = _entry(key, sid)+ with store._lock:+ store._save()+ assert key in _durable(store) and key in _mirror(store), 'the recreated id was refused a route'+ _restart(store)+ assert store._entries[key].session_id == sid+++async def test_a_start_within_the_horizon_strips_a_pruned_route_from_an_older_mirror(tmp_path, monkeypatch):+ """A sessions.json an earlier release wrote unfiltered (or one left by a crash before the next+ save) still names the pruned route. The first start while the fence holds skips it and rewrites+ the mirror, so the route cannot come back once the fence ages out."""+ async with _served(tmp_path, monkeypatch) as served:+ store = served.store+ key, _sid, stale = await _pruned_alpha_route(served, tmp_path)+ store._save_sessions_json(stale[0])+ _restart(store)+ assert key not in store._entries+ assert key not in _mirror(store), 'the start kept the pruned route in the mirror'+ _past_the_horizon(served.alpha.db, served.launch.db)+ _restart(store)+ assert key not in store._entries, 'a pruned route came back on startup after its fence aged out'