Skip to content

The agent that grows with you

fix(gateway): fence a route whose retirement value is not valid JSON

unsupportedpastelscc4b3a053271
diff --git a/hermes_state_mutation_retirement.py b/hermes_state_mutation_retirement.py
index 52601db2433..0e3afdb7d3f 100644
--- a/hermes_state_mutation_retirement.py
+++ b/hermes_state_mutation_retirement.py
@@ -298,8 +298,9 @@ def _mark_retired(conn, key, *, expires, **fields):
(key, json.dumps({**value, 'expires': True})))
else:
conn.execute('INSERT INTO state_meta(key,value) VALUES(?,?) ON CONFLICT(key) DO UPDATE SET value=CASE '
- "WHEN json_valid(state_meta.value) AND (json_extract(state_meta.value, '$.own') IS NOT NULL "
- "OR json_extract(state_meta.value, '$.expires')) THEN excluded.value "
+ "WHEN NOT json_valid(state_meta.value) THEN json_remove(excluded.value, '$.own') "
+ "WHEN json_extract(state_meta.value, '$.own') IS NOT NULL "
+ "OR json_extract(state_meta.value, '$.expires') THEN excluded.value "
"ELSE json_remove(excluded.value, '$.own') END", (key, json.dumps(value)))
@@ -334,17 +335,22 @@ def route_fence_key(namespace, session_id):
# 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. A delete's fence (no ``expires``) never does:
# its age test is NULL, which ``coalesce`` reads as not expired. An unscoped marker (no ``own``: an
-# earlier release's, written for either) still fences every route.
+# earlier release's, written for either) still fences every route. So does a marker or fence that is
+# not valid JSON (no writer stores one): ``json_extract`` raises ``malformed JSON`` on it even behind
+# ``json_valid(...) AND``, so each JSON read sits in a ``CASE WHEN json_valid(...)`` branch.
# Bound by name (``route_retired_params``): :entry is the route's JSON, :ns its key's
# ``route_namespace``, :now the save's time.
ROUTE_RETIRED_SQL = """(
EXISTS (SELECT 1 FROM state_meta m
WHERE m.key = :retired_prefix || json_extract(:entry, '$.session_id')
- AND (:ns = 'main' OR NOT (json_valid(m.value) AND json_extract(m.value, '$.own') IS NOT NULL)))
+ AND (:ns = 'main' OR NOT CASE WHEN json_valid(m.value)
+ THEN json_extract(m.value, '$.own') IS NOT NULL ELSE 0 END))
OR EXISTS (SELECT 1 FROM state_meta f
WHERE f.key = :fence_prefix || :ns || ':' || json_extract(:entry, '$.session_id')
- AND NOT coalesce(json_valid(f.value) AND json_extract(f.value, '$.expires')
- AND json_extract(f.value, '$.retired_at') < :now - :horizon, 0)
+ AND NOT CASE WHEN json_valid(f.value)
+ THEN coalesce(json_extract(f.value, '$.expires')
+ AND json_extract(f.value, '$.retired_at') < :now - :horizon, 0)
+ ELSE 0 END
AND (:ns != 'main' OR NOT EXISTS (
SELECT 1 FROM sessions s WHERE s.id = json_extract(:entry, '$.session_id')))))"""
diff --git a/tests/gateway/test_secondary_route_fence_scope.py b/tests/gateway/test_secondary_route_fence_scope.py
index a9b1ec00fc1..be877825807 100644
--- a/tests/gateway/test_secondary_route_fence_scope.py
+++ b/tests/gateway/test_secondary_route_fence_scope.py
@@ -414,6 +414,43 @@ async def test_a_pruned_route_stays_out_of_the_mirror_and_a_reused_id_keeps_one_
assert 'admission_conflict' not in str(verdict), verdict
+async def test_a_malformed_retirement_value_fences_its_route_without_aborting_a_save_or_the_import(
+ tmp_path, monkeypatch):
+ """No writer stores a marker or fence that isn't JSON, but reading one must not raise
+ ``malformed JSON``: it fences its route (a fence as a permanent one, a marker as an unscoped
+ one), and the rest of a whole-index save, a per-entry save and the sessions.json import go on."""
+ from hermes_state_mutation_retirement import (
+ RETIRED_PREFIX, ROUTE_FENCE_PREFIX, _reclaim_expired, expiring_retirements, retire_routes)
+ async with _served(tmp_path, monkeypatch) as served:
+ store, launch = served.store, served.launch.db
+ launch.set_meta(ROUTE_FENCE_PREFIX + 'alpha:bad-fence', '{not json')
+ launch.set_meta(RETIRED_PREFIX + 'bad-marker', '{not json')
+ routes = {'agent:alpha:telegram:1': 'bad-fence', 'agent:alpha:telegram:2': 'bad-marker',
+ 'agent:alpha:telegram:3': 'fine'}
+ for key, sid in routes.items():
+ store._entries[key] = _entry(key, sid)
+ with store._lock:
+ store._save()
+ assert set(_durable(store)) == {'agent:alpha:telegram:3'}, 'a malformed value aborted the whole-index save'
+ for key in ('agent:alpha:telegram:1', 'agent:alpha:telegram:2'):
+ store._routing_db.save_gateway_routing_entry(
+ key, json.dumps(_entry(key, routes[key]).to_dict()), scope=store._routing_scope())
+ assert key not in _durable(store), f'a malformed value let a per-entry save restore {key}'
+
+ store._routing_db._execute_write(lambda conn: conn.execute('DELETE FROM gateway_routing'))
+ store._save_sessions_json({key: _entry(key, sid).to_dict() for key, sid in routes.items()})
+ _restart(store)
+ assert set(store._entries) == {'agent:alpha:telegram:3'}, 'a malformed value aborted the import'
+
+ # The lane's other reads of these values: listing, reclaiming and rewriting a marker.
+ assert expiring_retirements(launch, ['bad-marker']) == set()
+ launch._execute_write(lambda conn: _reclaim_expired(conn, ROUTE_FENCE_PREFIX, time.time()))
+ assert launch.get_meta(ROUTE_FENCE_PREFIX + 'alpha:bad-fence') == '{not json'
+ launch._execute_write(lambda conn: retire_routes(conn, ['bad-marker'])) # a launch delete over it
+ assert 'own' not in json.loads(launch.get_meta(RETIRED_PREFIX + 'bad-marker')), 'it became launch-only'
+
+
@pytest.mark.asyncio
async def test_deleting_the_launch_session_keeps_an_earlier_releases_shared_fence_on_every_route(tmp_path, monkeypatch):
"""Before the upgrade, alpha deleted its ``user-42`` while the launch profile had a live