Skip to content

The agent that grows with you

fix: preflight durable local policy before claiming queued work

Tekniumd98dd7435ad1
diff --git a/gateway/session_authority.py b/gateway/session_authority.py
index 183e9f3e2e5..111013a4c33 100644
--- a/gateway/session_authority.py
+++ b/gateway/session_authority.py
@@ -271,6 +271,12 @@ class SessionAuthority:
if any(row['status'] == 'unknown' for row in pending):
return
first = next((row for row in pending if row['status'] == 'queued'), None)
+ from gateway.config import Platform
+ if first is not None and live.source.platform == Platform.LOCAL:
+ from gateway.session_local_recovery import restore_local_session
+ restore_local_session(self, ref.session_id)
+ if first['principal_id'] != live.source.user_id or set(first['payload']) != {'text'}:
+ raise RuntimeStoreError('permission_denied')
if first is not None and 'native_text_v1' in first['payload']:
from gateway.session_envelope import check_native_route
await check_native_route(self.runner, first['payload'], ref.session_id, live.source,
diff --git a/gateway/session_local_recovery.py b/gateway/session_local_recovery.py
index 33ebd464265..0ba5240aa6c 100644
--- a/gateway/session_local_recovery.py
+++ b/gateway/session_local_recovery.py
@@ -65,6 +65,10 @@ def restore_local_session(authority, sid):
adapter.policies[chat_id] = policy
authority.sessions[sid] = LiveSession(source, route)
adapter.register_source(source)
+ elif (not adapter.authorize_source(live.source)
+ or live.source.to_dict() != source.to_dict()
+ or adapter.policies.get(chat_id) != policy):
+ raise RuntimeStoreError('storage_unavailable')
return SessionRef(authority.profile_id, sid)
diff --git a/tests/gateway/test_local_session_recovery.py b/tests/gateway/test_local_session_recovery.py
index b566499c5b5..30d137304ac 100644
--- a/tests/gateway/test_local_session_recovery.py
+++ b/tests/gateway/test_local_session_recovery.py
@@ -57,6 +57,13 @@ async def test_local_create_receipt_survives_cold_authority_atomically(tmp_path,
policies = conn.execute("SELECT value FROM state_meta WHERE key LIKE 'gateway.local_policy.v1:%'").fetchall()
assert len(policies) == 1
assert json.loads(policies[0][0])['principal_id'] == actor.subject
+ from hermes_state_runtime import admit_session_input, list_session_admissions
+ admit_session_input(db, epoch=cold.epoch, principal_id=actor.subject,
+ session_id=ref.session_id, request_id='preclaim', payload={'text': 'never run'})
+ db._write_sql('UPDATE state_meta SET value=? WHERE key=?',
+ ('{}', 'gateway.local_policy.v1:' + ref.session_id))
+ await cold._drain(ref)
+ assert list_session_admissions(db, session_id=ref.session_id, pending_only=False)[0]['status'] == 'queued'
@pytest.mark.linux_only