Skip to content

The agent that grows with you

fix(gateway): keep a secondary's delete fence past the retry horizon

unsupportedpastels9102bdc9ce90
diff --git a/hermes_state_mutation_retirement.py b/hermes_state_mutation_retirement.py
index 98a2648984c..ae91b09904a 100644
--- a/hermes_state_mutation_retirement.py
+++ b/hermes_state_mutation_retirement.py
@@ -329,14 +329,15 @@ def route_retired_sql(entry, namespace, now):
can only name another profile's session, so a secondary's fence covers it too
(``fence_shared_routes``). A sweep's fence stops fencing at the horizon by itself: only the
owner's own store sweeps the owner's marker, and nothing guarantees the launch profile ever
- sweeps this one. An unscoped marker (no ``own``: an earlier release's, written for either)
+ sweeps this one. A delete's fence (no ``expires``) never does: the age test is NULL for it,
+ and NULL counts as not expired. 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 NOT (json_valid(f.value) AND json_extract(f.value, '$.expires') "
- f"AND json_extract(f.value, '$.retired_at') < {now} - {RETRY_HORIZON_S}) "
+ f"AND NOT coalesce(json_valid(f.value) AND json_extract(f.value, '$.expires') "
+ f"AND json_extract(f.value, '$.retired_at') < {now} - {RETRY_HORIZON_S}, 0) "
f"AND ({namespace} != 'main' OR NOT EXISTS (SELECT 1 FROM sessions s WHERE s.id = {sid}))))")
diff --git a/tests/gateway/test_secondary_route_fence_scope.py b/tests/gateway/test_secondary_route_fence_scope.py
index 3d49e17e38e..6b541cff3b9 100644
--- a/tests/gateway/test_secondary_route_fence_scope.py
+++ b/tests/gateway/test_secondary_route_fence_scope.py
@@ -83,8 +83,21 @@ def _durable(store):
return store._routing_db.load_gateway_routing_entries(scope=store._routing_scope())
+def _age_retirements(*dbs, days=8):
+ """Move every retirement marker and routing fence *days* into the past: the wall clock passing
+ the horizon, with no sweep run anywhere."""
+ from hermes_state_mutation_retirement import RETIRED_PREFIX, ROUTE_FENCE_PREFIX
+ for db in dbs:
+ db._execute_write(lambda conn: conn.execute(
+ "UPDATE state_meta SET value=json_set(value, '$.retired_at', json_extract(value, '$.retired_at') - ?) "
+ "WHERE (key GLOB ? OR key GLOB ?) AND json_valid(value)",
+ (days * 86400, RETIRED_PREFIX + '*', ROUTE_FENCE_PREFIX + '*')))
+
+
def _past_the_horizon(*dbs):
+ """The wall clock passes the horizon (every fence aged), then each store sweeps its tombstones."""
from hermes_state_mutation_retirement import prune_retired_tombstones
+ _age_retirements(*dbs)
for db in dbs:
db._execute_write(lambda conn: prune_retired_tombstones(conn, older_than=0))
@@ -246,17 +259,6 @@ async def test_a_delayed_snapshot_cannot_restore_a_pruned_route_once_its_fence_a
assert store._entries[key].session_id == sid
-def _age_retirements(*dbs, days=8):
- """Move every retirement marker and routing fence *days* into the past: the wall clock passing
- the horizon, with no sweep run anywhere."""
- from hermes_state_mutation_retirement import RETIRED_PREFIX, ROUTE_FENCE_PREFIX
- for db in dbs:
- db._execute_write(lambda conn: conn.execute(
- "UPDATE state_meta SET value=json_set(value, '$.retired_at', json_extract(value, '$.retired_at') - ?) "
- "WHERE (key GLOB ? OR key GLOB ?) AND json_valid(value)",
- (days * 86400, RETIRED_PREFIX + '*', ROUTE_FENCE_PREFIX + '*')))
-
-
@pytest.mark.asyncio
async def test_a_pruned_id_routes_again_after_the_owner_alone_sweeps_past_the_horizon(tmp_path, monkeypatch):
"""Only the owner's own housekeeping runs (the launch profile's auto-prune is off by default):
@@ -304,6 +306,39 @@ async def test_the_owners_next_prune_reclaims_its_expired_shared_fences(tmp_path
assert served.launch.db.get_meta(fence('alpha', kept.session_id)) is not None, "a delete's fence aged out"
+async def test_a_secondary_delete_fence_outlives_the_horizon_while_a_prune_fence_ages_out(tmp_path, monkeypatch):
+ """A delete's shared fence has no ``expires``: 8 days later, with no sweep run, it still refuses
+ alpha's route and the launch-keyed one, both to a per-entry save and to the sessions.json import
+ on the next start (#138826). A prune's fence aged the same way no longer fences."""
+ from gateway.run import _profile_runtime_scope
+ from gateway.session_local import create_local_session
+ async with _served(tmp_path, monkeypatch) as served:
+ store = served.store
+ pruned_key, pruned, _stale = await _pruned_alpha_route(served, tmp_path)
+ keys = ('agent:alpha:telegram:44', 'agent:main:telegram:44')
+ with _profile_runtime_scope(served.home, {}):
+ ref = create_local_session(served.alpha, served.actor, {
+ 'request_id': 'd', 'source': 'gui', 'cwd': str(tmp_path), 'model': 'frozen', 'toolsets': []})
+ for key in keys:
+ store._entries[key] = _entry(key, ref.session_id)
+ with store._lock:
+ stale = store._snapshot_routing_locked()
+ await _delete(served, ref)
+ _age_retirements(served.alpha.db, served.launch.db)
+ for key in keys:
+ store._routing_db.save_gateway_routing_entry(key, json.dumps(stale[0][key]), scope=store._routing_scope())
+ assert key not in _durable(store), f'an aged delete fence let a per-entry save restore {key}'
+ store._save_sessions_json({key: stale[0][key] for key in keys}) # a mirror written before the delete
+ _restart(store)
+ for key in keys:
+ assert key not in store._entries, f'an aged delete fence let the sessions.json import restore {key}'
+
+ store._routing_db.save_gateway_routing_entry(
+ pruned_key, json.dumps(_entry(pruned_key, pruned).to_dict()), scope=store._routing_scope())
+ assert pruned_key in _durable(store), 'a prune fence aged the same way still fenced its route'
+
+
@pytest.mark.asyncio
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