The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): key session retirement by profile in the shared routing index
unsupportedpastels511bc2971880
diff --git a/gateway/session_mutations.py b/gateway/session_mutations.pyindex 1726c2f87a2..c819e41fe9b 100644--- a/gateway/session_mutations.py+++ b/gateway/session_mutations.py@@ -325,14 +325,22 @@ def retire_live_sessions(authority, session_ids, *, require_fence=False):store.retire_runtime_sessions(session_ids)# The delete transaction fenced and dropped routes in the owning store; a multiplexed# secondary's routes live in the launch home's shared index, so the store fences them- # there too (markers + drop in one transaction). Without the marker in THAT database a+ # there too (fences + drop in one transaction). Without the fence in THAT database a# whole-index snapshot taken before this delete and saved after it restored the route- # (#138826). A failure surfaces after the rest of the cleanup: an exact retry replays the+ # (#138826). The fence is keyed by this profile's routing namespace, so the launch+ # profile never takes it for its own deletion, and it ages out with a sweep's marker.+ # A failure surfaces after the rest of the cleanup: an exact retry replays the# committed receipt and repeats this fence.routing = getattr(store, '_routing_db', None)if session_ids and routing is not None and routing.db_path != authority.db.db_path:try:- store.fence_retired_routes(session_ids)+ from gateway.session import _session_key_namespace+ from hermes_state_mutation_retirement import expiring_retirements+ registry = getattr(authority.runner, 'session_authorities', None)+ profile = registry.profile_name(authority) if registry is not None else None+ store.fence_retired_routes(+ session_ids, namespace=_session_key_namespace(profile).split(':', 1)[1],+ expiring=expiring_retirements(authority.db, session_ids))except Exception as exc:logger.warning('could not fence shared routing rows for deleted sessions', exc_info=True)fence_error = excdiff --git a/gateway/session_persistence.py b/gateway/session_persistence.pyindex b47ba6a9f9a..3ffa9b6a491 100644--- a/gateway/session_persistence.py+++ b/gateway/session_persistence.py@@ -50,15 +50,16 @@ class SessionPersistenceMixin:for sid in targets:self._session_owner_hints.pop(sid, None)- def fence_retired_routes(self, session_ids) -> None:- """Durably retire *session_ids* in the routing index's own database (markers + route+ def fence_retired_routes(self, session_ids, *, namespace, expiring=()) -> None:+ """Durably retire *session_ids* in the routing index's own database (fences + routedrop in one transaction). A delete committed in another profile's store fences only that- store; the shared index lives in the launch home, so its saves need the fence here too.+ store; the shared index lives in the launch home, so its saves need the fence here too,+ keyed by the deleting profile's routing *namespace* (``retire_gateway_routes``).Raises when the routing store cannot take the fence: the delete must not report success."""retire = self._routing_db_method("retire_gateway_routes")if retire is None:raise RuntimeError("routing store unavailable for deletion fence")- retire(session_ids)+ retire(session_ids, namespace=namespace, expiring=expiring)def _open_session_db_for_active_scope(self, db_path: Optional[Path] = None):"""SessionDB for the active profile scope. ``db_path`` pins the store; otherwise@@ -355,9 +356,10 @@ class SessionPersistenceMixin:type(entry_data).__name__)continuetry:- from hermes_state_mutation_retirement import retired_session+ from hermes_state_mutation_retirement import route_retireddb = self._routing_db- if db is not None and retired_session(db, entry_data['session_id']):+ if db is not None and route_retired(db, key, json.dumps(+ {'session_id': entry_data['session_id']})):continueself._entries[key] = SessionEntry.from_dict(entry_data)imported += 1diff --git a/hermes_state_gateway.py b/hermes_state_gateway.pyindex ebad0035f33..940b9cbf73e 100644--- a/hermes_state_gateway.py+++ b/hermes_state_gateway.py@@ -298,28 +298,28 @@ class SessionGatewayMixin:namespaces the index per sessions_dir so two stores never share routing state."""if not session_key or not entry_json:return+ from hermes_state_mutation_retirement import route_namespace, route_retired_sqlself._write_sql(- """INSERT INTO gateway_routing (scope, session_key, entry_json, updated_at)- SELECT ?, ?, ?, ? WHERE NOT EXISTS (- SELECT 1 FROM state_meta WHERE key='gateway.retired_session.v1.' || json_extract(?, '$.session_id'))+ f"""INSERT INTO gateway_routing (scope, session_key, entry_json, updated_at)+ SELECT ?1, ?2, ?3, ?4 WHERE NOT {route_retired_sql('?3', '?5')}ON CONFLICT(scope, session_key) DO UPDATE SETentry_json = excluded.entry_json,updated_at = excluded.updated_at""",- (scope, session_key, entry_json, time.time(), entry_json),+ (scope, session_key, entry_json, time.time(), route_namespace(session_key)),)def replace_gateway_routing_entries(self, entries: dict[str, str], *, scope: str = "") -> None:"""Atomically replace the routing index for *scope* (keys absent from *entries*are removed); other scopes untouched."""+ from hermes_state_mutation_retirement import route_namespace, route_retired_sqlnow = time.time()def _do(conn):conn.execute("DELETE FROM gateway_routing WHERE scope = ?", (scope,))if entries:conn.executemany("INSERT INTO gateway_routing (scope, session_key, entry_json, updated_at) "- "SELECT ?, ?, ?, ? WHERE NOT EXISTS (SELECT 1 FROM state_meta "- "WHERE key='gateway.retired_session.v1.' || json_extract(?, '$.session_id'))",- [(scope, k, v, now, v) for k, v in entries.items() if k and v])+ 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)def load_gateway_routing_entries(self, *, scope: str = "") -> dict[str, str]:@@ -403,25 +403,20 @@ class SessionGatewayMixin:self._write_sql("DELETE FROM gateway_routing WHERE scope = ? AND session_key = ?", doomed, many=True)return len(doomed)- def retire_gateway_routes(self, session_ids) -> None:- """Publish deletion fences for *session_ids* in THIS (routing) database and drop the- routes naming them, in one transaction. Routing saves skip any entry whose session id- carries a marker here, so a whole-index snapshot taken before a delete in another- profile's store cannot restore the route when it is saved after it."""+ def retire_gateway_routes(self, session_ids, *, namespace, expiring=()) -> None:+ """Publish routing fences for *session_ids*, retired in the profile whose routing keys use+ *namespace*, in THIS (routing) database and drop that profile's routes naming them, in one+ transaction (``fence_shared_routes``). Routing saves skip a fenced route, so a whole-index+ snapshot taken before a delete in another profile's store cannot restore it when it is+ saved after it. The fence is not this store's own deletion marker: this profile's creates,+ binds and generation walks stay free. *expiring* ids (a sweep's) age out at the horizon."""ids = sorted({str(sid) for sid in session_ids if sid})if not ids:returndef _do(conn):- from hermes_state_mutation_retirement import RETIRED_PREFIX, entry_session_id- retired = json.dumps({"retired_at": time.time()})- conn.executemany("INSERT OR REPLACE INTO state_meta(key,value) VALUES(?,?)",- [(RETIRED_PREFIX + sid, retired) for sid in ids])- targets = set(ids)- doomed = [(row[0], row[1]) for row in conn.execute(- "SELECT scope, session_key, entry_json FROM gateway_routing").fetchall()- if entry_session_id(row[2]) in targets]- conn.executemany("DELETE FROM gateway_routing WHERE scope = ? AND session_key = ?", doomed)+ from hermes_state_mutation_retirement import fence_shared_routes+ fence_shared_routes(conn, namespace, ids, expiring=expiring)self._execute_write(_do)def prune_never_active_keyed_sessions(diff --git a/hermes_state_mutation_retirement.py b/hermes_state_mutation_retirement.pyindex 5c826f0380c..38143ab4278 100644--- a/hermes_state_mutation_retirement.py+++ b/hermes_state_mutation_retirement.py@@ -3,7 +3,15 @@ import jsonimport timefrom hermes_state_runtime import RuntimeStoreError, admission_fingerprint, mutation_receipt_key, _json, _text+# This store's own retired session (its profile is the store's). Written as ``own`` since the+# shared routing index also carries other profiles' fences; a marker without ``own`` predates+# that and still fences every route in the index.RETIRED_PREFIX = 'gateway.retired_session.v1.'+# A multiplexed secondary's retired session, in the routing index every served profile shares+# (the launch home's state.db): ``<prefix><namespace>:<id>``, keyed by the ``agent:<namespace>:``+# slot of that profile's routing keys. Only routing saves read it, so the launch profile's own+# creates, binds and generation walks never see another profile's retirement.+ROUTE_FENCE_PREFIX = 'gateway.retired_route.v1.'# A last viewer's close stored until the owner's idle point (gateway/session_viewer_close).VIEWER_CLOSE_PREFIX = 'gateway.viewer_close.v1:'@@ -271,20 +279,24 @@ def entry_session_id(raw):return value.get('session_id') if isinstance(value, dict) else None-def retire_routes(conn, session_ids, *, expiring=()):+def _mark_retired(conn, key, *, expires, **fields):# OR REPLACE: the marker is a fact, not a receipt; re-retiring an id that was recreated# beside its tombstone must not abort the delete that removes it again (and restarts its horizon).- # An ``expiring`` (sweep) marker ages out at the horizon and never shortens a permanent one.+ # An ``expires`` (sweep) marker ages out at the horizon and never shortens a permanent one.+ value = {'retired_at': time.time(), **fields}+ if expires:+ conn.execute('INSERT INTO state_meta(key,value) VALUES(?,?) ON CONFLICT(key) DO UPDATE '+ "SET value=excluded.value WHERE json_valid(state_meta.value) "+ "AND json_extract(state_meta.value, '$.expires')",+ (key, json.dumps({**value, 'expires': True})))+ else:+ conn.execute('INSERT OR REPLACE INTO state_meta(key,value) VALUES(?,?)', (key, json.dumps(value)))+++def retire_routes(conn, session_ids, *, expiring=()):expiring = set(expiring)for sid in session_ids:- if sid in expiring:- conn.execute('INSERT INTO state_meta(key,value) VALUES(?,?) ON CONFLICT(key) DO UPDATE '- "SET value=excluded.value WHERE json_valid(state_meta.value) "- "AND json_extract(state_meta.value, '$.expires')",- (RETIRED_PREFIX + sid, json.dumps({'retired_at': time.time(), 'expires': True})))- else:- conn.execute('INSERT OR REPLACE INTO state_meta(key,value) VALUES(?,?)',- (RETIRED_PREFIX + sid, json.dumps({'retired_at': time.time()})))+ _mark_retired(conn, RETIRED_PREFIX + sid, expires=sid in expiring, own=True)conn.execute('DELETE FROM state_meta WHERE key=?', ('gateway.api.settings.v1.' + sid,))conn.execute('DELETE FROM state_meta WHERE key=?', (VIEWER_CLOSE_PREFIX + sid,))targets = set(session_ids)@@ -294,6 +306,67 @@ def retire_routes(conn, session_ids, *, expiring=()):(row['scope'], row['session_key']))+def route_namespace(session_key):+ """The ``<ns>`` slot of an ``agent:<ns>:...`` routing key (``main`` for the launch profile, else+ the profile's ``_session_key_namespace`` slot); ``main`` for a key outside that shape."""+ parts = str(session_key or '').split(':')+ return parts[1] if len(parts) > 2 and parts[0] == 'agent' and parts[1] else 'main'+++def route_fence_key(namespace, session_id):+ return f'{ROUTE_FENCE_PREFIX}{namespace}:{session_id}'+++def route_retired_sql(entry, namespace):+ """SQL true when a routing save must skip an entry (SQL expressions *entry*: its JSON, and+ *namespace*: its key's ``route_namespace``), checked only against the profile the route belongs+ to: this store's own marker for a launch (``main``) route, that profile's fence for a+ secondary's. A launch-keyed route to an id this store holds no session of can only name+ another profile's session, so a secondary's fence covers it too (``fence_shared_routes``).+ An unscoped marker (no ``own``: an earlier release's, written for either) still fences every route."""+ sid = f"json_extract({entry}, '$.session_id')"+ return (f"(EXISTS (SELECT 1 FROM state_meta m WHERE m.key = '{RETIRED_PREFIX}' || {sid} AND "+ f"({namespace} = 'main' OR NOT (json_valid(m.value) AND json_extract(m.value, '$.own') IS NOT NULL))) "+ f"OR EXISTS (SELECT 1 FROM state_meta f WHERE f.key = '{ROUTE_FENCE_PREFIX}' || {namespace} || ':' || {sid} "+ f"AND ({namespace} != 'main' OR NOT EXISTS (SELECT 1 FROM sessions s WHERE s.id = {sid}))))")+++def route_retired(db, session_key, entry_json):+ """A routing save (or the legacy sessions.json import) must skip this entry."""+ with db._read_ctx() as conn:+ return bool(conn.execute(f"SELECT {route_retired_sql('?1', '?2')}",+ (entry_json, route_namespace(session_key))).fetchone()[0])+++def expiring_retirements(db, session_ids):+ """The ids among *session_ids* whose own marker in *db* is a sweep's (ages out at the horizon)."""+ with db._read_ctx() as conn:+ return {key[len(RETIRED_PREFIX):] for (key,) in conn.execute(+ "SELECT key FROM state_meta WHERE key IN (SELECT ? || value FROM json_each(?)) "+ "AND json_valid(value) AND json_extract(value, '$.expires')",+ (RETIRED_PREFIX, json.dumps(list(session_ids))))}+++def fence_shared_routes(conn, namespace, session_ids, *, expiring=()):+ """In the shared routing index (*conn*), fence the routes of *session_ids*, retired in the+ profile whose routing keys use *namespace*, and drop them. The fence is keyed by (profile, id)+ and copies the owner's marker: permanent for a delete, aging out with an *expiring* sweep's.+ Unless this store holds a session of that id, the launch (``main``) namespace is fenced too: a+ launch-keyed route to it names the retired session."""+ expiring, owned = set(expiring), {}+ for sid in session_ids:+ names = {namespace}+ if not conn.execute('SELECT 1 FROM sessions WHERE id=?', (sid,)).fetchone():+ names.add('main')+ for name in names:+ _mark_retired(conn, route_fence_key(name, sid), expires=sid in expiring)+ owned[sid] = names+ for row in conn.execute('SELECT scope,session_key,entry_json FROM gateway_routing').fetchall():+ if route_namespace(row['session_key']) in owned.get(entry_session_id(row['entry_json']), ()):+ conn.execute('DELETE FROM gateway_routing WHERE scope=? AND session_key=?',+ (row['scope'], row['session_key']))++RETRY_HORIZON_S = 7 * 86400 # exact-retry horizon: a retry within it gets the byte-exact settled reply_EXPIRED = "json_valid(value) AND json_extract(value, '$.retired_at') < ?"@@ -301,7 +374,7 @@ _EXPIRED = "json_valid(value) AND json_extract(value, '$.retired_at') < ?"def prune_retired_tombstones(conn, older_than=RETRY_HORIZON_S):"""Reclaim deletion tombstones retired more than *older_than* seconds ago: the terminal admissionwith its identity pointer and compacted result, terminal workers and a sweep's (``expires``)- retired-session marker. A user delete's marker is the deletion fence itself and is never+ retired-session marker or shared routing fence. A user delete's marker is the deletion fence itself and is neverreclaimed: it holds only ``retired_at``, and without it a deleted id could be recreated,re-admitted or reissued as a Bot Chat generation.Tombstones written before ``retired_at`` existed are stamped now and age from this pass."""@@ -318,5 +391,6 @@ def prune_retired_tombstones(conn, older_than=RETRY_HORIZON_S):conn.execute('DELETE FROM state_meta WHERE key IN (?,?,?)', (key, RESULT_PREFIX + row['admission_id'],identity_key(row['principal_id'], row['target_session_id'], row['request_id'])))conn.execute(f'DELETE FROM state_meta WHERE key GLOB ? AND {_EXPIRED}', (WORKER_PREFIX + '*', cutoff))- conn.execute(f"DELETE FROM state_meta WHERE key GLOB ? AND {_EXPIRED} AND json_extract(value, '$.expires')",- (RETIRED_PREFIX + '*', cutoff))+ for prefix in (RETIRED_PREFIX, ROUTE_FENCE_PREFIX):+ conn.execute(f"DELETE FROM state_meta WHERE key GLOB ? AND {_EXPIRED} AND json_extract(value, '$.expires')",+ (prefix + '*', cutoff))diff --git a/tests/gateway/test_delete_secondary_profile_multiplex.py b/tests/gateway/test_delete_secondary_profile_multiplex.pyindex b4f36924119..fc56be7f3e1 100644--- a/tests/gateway/test_delete_secondary_profile_multiplex.py+++ b/tests/gateway/test_delete_secondary_profile_multiplex.py@@ -144,7 +144,7 @@ def test_a_failed_shared_fence_fails_the_canonical_delete_not_the_prune(monkeypa'gateway.session_worker_construct.release_session_prefill'):monkeypatch.setattr(target, lambda *a, **k: None)- def broken(ids):+ def broken(ids, **_):raise OSError('disk gone')store = SimpleNamespace(retire_runtime_sessions=lambda ids: None, fence_retired_routes=broken,_routing_db=SimpleNamespace(db_path='/launch/state.db'))diff --git a/tests/gateway/test_secondary_route_fence_scope.py b/tests/gateway/test_secondary_route_fence_scope.pynew file mode 100644index 00000000000..1fba57bebc9--- /dev/null+++ b/tests/gateway/test_secondary_route_fence_scope.py@@ -0,0 +1,193 @@+"""Retirement is keyed by (profile, id): a multiplexed secondary's delete or prune retires the id+there, never in the launch profile.++The launch home's ``state.db`` is both the launch profile's session store and the routing index+every served profile shares. A secondary profile's delete or prune fences its routes there, so a+whole-index snapshot taken before it and saved after it cannot restore them (#138826). That fence+was the launch profile's own session-deletion marker, so the launch profile then refused the id+for good: ``POST /api/sessions`` answered 409 ``session_deleted`` for a session it never had, and+after a prune the refusal never aged out.+"""+import json+import time+from contextlib import asynccontextmanager+from datetime import UTC, datetime+from types import SimpleNamespace++import pytest+from aiohttp import web+from aiohttp.test_utils import TestClient, TestServer++from tests.gateway.test_session_authorities_multiplex import _reserve_homes, _runner++_KEY = 'route-fence-key'+_AUTH = {'Authorization': 'Bearer ' + _KEY}+++@asynccontextmanager+async def _served(tmp_path, monkeypatch):+ """A multiplexed gateway: the default (launch) profile serving ``alpha``, real state.db files."""+ from gateway import run+ from gateway.config import PlatformConfig+ from gateway.platforms.api_server import APIServerAdapter+ from gateway.run_runtime import initialize_gateway_runtime+ from gateway.runtime_ownership import process_ownership+ from gateway.session_contract import Principal++ monkeypatch.setattr(run, '_load_gateway_config', lambda: {'platform_toolsets': {'cli': []}})+ root, homes = _reserve_homes(tmp_path, monkeypatch, names=('alpha',))+ process_ownership.reserve([home for _, home in homes])+ runner = adapter = None+ try:+ runner = _runner(root, homes)+ await initialize_gateway_runtime(runner)+ launch, home = runner.session_authority, homes[1][1]+ alpha = runner.session_authorities.for_home(home)+ assert runner.session_store._routing_db.db_path == launch.db.db_path != alpha.db.db_path+ actor = Principal('owner', alpha.profile_id, frozenset(+ {'session:create', 'session:read', 'session:control', 'session:operator'}), 'socket')+ adapter = APIServerAdapter(PlatformConfig(enabled=True))+ adapter._api_key, adapter.gateway_runner, adapter._session_db = _KEY, runner, launch.db+ yield SimpleNamespace(runner=runner, launch=launch, alpha=alpha, home=home, actor=actor,+ store=runner.session_store, adapter=adapter)+ finally:+ if adapter is not None:+ adapter._response_store.close()+ adapter._run_idempotency_store.close()+ for a in list(getattr(runner, 'session_authorities', None) or []):+ a.db.close()+ for _, home in homes:+ process_ownership.release(home)+++async def _delete(served, ref):+ from gateway.session_mutations import mutate_session+ await mutate_session(served.alpha, served.actor, ref, dict(+ session_id=ref.session_id, request_id='delete', expected_revision=0,+ expected_generation=0, operation='delete', payload={}))+++async def _prune(served, ref):+ from gateway.session_prune import _apply+ served.alpha.db.end_session(ref.session_id, 'test')+ await _apply(served.alpha, {'older_than_days': None}, None) # the owner-run `hermes sessions prune`+++def _entry(key, sid):+ from gateway.session import SessionEntry+ now = datetime.now(UTC)+ return SessionEntry(session_key=key, session_id=sid, created_at=now, updated_at=now)+++def _durable(store):+ return store._routing_db.load_gateway_routing_entries(scope=store._routing_scope())+++def _past_the_horizon(*dbs):+ from hermes_state_mutation_retirement import prune_retired_tombstones+ for db in dbs:+ db._execute_write(lambda conn: prune_retired_tombstones(conn, older_than=0))+++def _conflict(db, sid):+ from hermes_state_mutation_retirement import create_conflict+ with db._read_ctx() as conn:+ return create_conflict(conn, sid)+++async def _create(served, sid):+ app = web.Application()+ app.router.add_post('/api/sessions', served.adapter._handle_create_session)+ async with TestClient(TestServer(app)) as client:+ created = await client.post('/api/sessions', json={'id': sid}, headers=_AUTH)+ return created.status, await created.text()++[email protected]('retire', [_delete, _prune], ids=['delete', 'prune'])+async def test_a_secondary_retirement_leaves_the_id_free_in_the_launch_profile(tmp_path, monkeypatch, retire):+ from gateway.run import _profile_runtime_scope+ from gateway.session_api import bind_api_session+ from gateway.session_local import create_local_session++ async with _served(tmp_path, monkeypatch) as served:+ store = served.store+ own, launch_keyed = 'agent:alpha:telegram:333', 'agent:main: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': []})+ sid = ref.session_id+ store._entries[own] = _entry(own, sid)+ store._entries[launch_keyed] = _entry(launch_keyed, sid) # a route keyed in the launch namespace+ with store._lock:+ stale = store._snapshot_routing_locked() # a worker's snapshot, saved late+ await retire(served, ref)+ store._persist_routing_data(*stale)+ assert served.alpha.db.get_session(sid) is None+ assert own not in _durable(store) and launch_keyed not in _durable(store), \+ 'a stale whole-index save restored the deleted route'++ # The launch profile never had this session: its own create and bind see no deletion.+ assert _conflict(served.launch.db, sid) is None+ status, text = await _create(served, sid)+ assert status == 201, text+ bind_api_session(served.launch, sid) # the X-Hermes-Session-Id chat's bind+ fence = 'gateway.retired_route.v1.alpha:' + sid # alpha's (profile, id) fence in the shared index+ assert served.launch.db.get_meta(fence) is not None+ # The secondary itself still refuses its own retired id within the horizon.+ assert _conflict(served.alpha.db, sid) == ('session_deleted', 'Session was deleted; its id cannot be reused')++ _past_the_horizon(served.alpha.db, served.launch.db)+ if retire is _delete: # a user delete stays final in alpha, and so does its routing fence+ assert _conflict(served.alpha.db, sid) is not None+ assert served.launch.db.get_meta(fence) is not None+ store._routing_db.save_gateway_routing_entry(own, json.dumps(stale[0][own]), scope=store._routing_scope())+ assert own not in _durable(store), 'a late per-entry save restored the deleted route'+ else: # a prune is not a delete: alpha may reuse the id, and the shared fence ages out with it+ assert _conflict(served.alpha.db, sid) is None+ assert served.launch.db.get_meta(fence) is None+++async def test_a_launch_delete_fences_only_the_launch_profiles_routes(tmp_path, monkeypatch):+ """The launch profile deleting its ``user-42`` fences its own routes to that id in the shared+ index, not ``alpha``'s route to alpha's own ``user-42``."""+ async with _served(tmp_path, monkeypatch) as served:+ store = served.store+ for db in (served.launch.db, served.alpha.db):+ db.create_session('user-42', source='cli')+ launch_key, alpha_key = 'agent:main:telegram:1', 'agent:alpha:telegram:1'+ store._entries[launch_key] = _entry(launch_key, 'user-42')+ store._entries[alpha_key] = _entry(alpha_key, 'user-42')+ with store._lock:+ stale = store._snapshot_routing_locked()+ served.launch.db.delete_session('user-42')+ store._persist_routing_data(*stale)+ assert launch_key not in _durable(store), "a stale save restored the launch profile's deleted route"+ assert alpha_key in _durable(store), "alpha's route to its own session was refused"+ assert _conflict(served.alpha.db, 'user-42') == ('session_exists', 'Session already exists')+++async def test_a_shared_fence_an_earlier_release_wrote_still_guards_every_route(tmp_path, monkeypatch):+ """An earlier release fenced a secondary's delete in the shared index with the launch profile's+ own marker (``retired_session.v1.<id>``, only ``retired_at``), which can't be told apart from a+ launch delete. Read through the new code it keeps both meanings: it still refuses every route to+ the id (a sessions.json mirror from before the delete must not bring one back), and the launch+ profile keeps refusing the id as deleted."""+ from hermes_state_mutation_retirement import RETIRED_PREFIX+ async with _served(tmp_path, monkeypatch) as served:+ store = served.store+ served.launch.db.set_meta(RETIRED_PREFIX + 'gone', json.dumps({'retired_at': time.time() - 30 * 86400}))+ _past_the_horizon(served.launch.db)+ key = 'agent:alpha:telegram:9'+ store._entries[key] = _entry(key, 'gone')+ 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)+ with 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')