Skip to content

The agent that grows with you

fix: reject foreign local receipts inside lineage publication

Teknium1ffba2ac1fa0
diff --git a/hermes_state_local_lineage.py b/hermes_state_local_lineage.py
index 679aff07fd7..9d168f34ad5 100644
--- a/hermes_state_local_lineage.py
+++ b/hermes_state_local_lineage.py
@@ -11,6 +11,9 @@ from hermes_state_runtime import RuntimeStoreError, _epoch, _json
def validate_local_lineage(conn, receipt):
from hermes_state_compression import _CHAIN_STEP_SQL
+ from gateway.session_local_recovery import local_identity
+ if receipt['session_id'] != local_identity(receipt['profile_id'], receipt['principal_id'], receipt['request_id']):
+ raise RuntimeStoreError('storage_unavailable')
lineage = receipt.get('lineage', [receipt['session_id']])
if (not isinstance(lineage, list) or not lineage or len(set(lineage)) != len(lineage)
or lineage[0] != receipt['session_id'] or lineage[-1] != receipt['entry']['session_id']):
diff --git a/tests/gateway/fixtures/local_lineage_probe.py b/tests/gateway/fixtures/local_lineage_probe.py
index 6fe793cf65b..12383b0f00f 100644
--- a/tests/gateway/fixtures/local_lineage_probe.py
+++ b/tests/gateway/fixtures/local_lineage_probe.py
@@ -29,6 +29,7 @@ def probe(tmp_path):
env.update(HOME=str(user), USERPROFILE=str(user), HERMES_HOME=str(home), PYTHONPATH=str(root),
OPENAI_API_KEY='loopback-only', OPENAI_BASE_URL=url, PYTHONUNBUFFERED='1')
sessions, params, targets, pids, epochs = {}, {}, {}, [], []
+ route_targets = {}
def db_rows():
with sqlite3.connect(f'file:{home / "state.db"}?mode=ro', uri=True) as db:
return dict(db.execute('SELECT request_id,status FROM session_admissions'))
@@ -70,6 +71,8 @@ def probe(tmp_path):
async def recovered(desc):
async with websocket(home, desc) as ws:
for name in ('safe', 'reset'):
+ snapshot = await rpc(ws, 'session.resume', session_id=sessions[name])
+ assert 'result' in snapshot, snapshot
await terminal(name)
snapshot = await rpc(ws, 'session.resume', session_id=sessions[name])
assert 'result' in snapshot, snapshot
@@ -100,10 +103,9 @@ def probe(tmp_path):
('gateway.local_policy.v1:' + sid,)).fetchone()[0])
targets[name] = receipt['entry']['session_id']
if name != 'foreign':
- assert targets[name] != sid, receipt
route = json.loads(db.execute("SELECT entry_json FROM gateway_routing WHERE scope='' AND session_key=?",
(receipt['route'],)).fetchone()[0])
- assert route['session_id'] == targets[name]
+ route_targets[name] = route['session_id']
row = db.execute('SELECT cwd FROM sessions WHERE id=?', (targets[name],)).fetchone()
assert row[0] == params[name]['cwd'], row
else:
@@ -126,6 +128,8 @@ def probe(tmp_path):
asyncio.run(recovered(desc))
assert db_rows() == {'warm-safe': 'terminal', 'warm-reset': 'terminal', 'started': 'unknown',
'follower': 'queued', 'reset': 'terminal', 'foreign': 'queued', 'safe': 'terminal'}
+ assert all(route_targets[name] == targets[name] for name in ('safe', 'reset', 'unknown')), route_targets
+ assert all(targets[name] != sessions[name] for name in ('safe', 'reset', 'unknown')), targets
texts = [next((m.get('content') for m in reversed(r['messages']) if m['role'] == 'user'), '') for r in peer.requests]
assert sorted(texts) == sorted(['WARM_HISTORY', 'RESET_OLD', 'BLOCK_STARTED', 'RECOVER_SAFE', 'RECOVER_RESET']), texts
for name in ('safe', 'reset'):
diff --git a/tests/gateway/test_local_session_lineage.py b/tests/gateway/test_local_session_lineage.py
index 490fe699b79..d7df84bfe97 100644
--- a/tests/gateway/test_local_session_lineage.py
+++ b/tests/gateway/test_local_session_lineage.py
@@ -40,6 +40,17 @@ async def test_local_lineage_transitions_preserve_owner_or_roll_back(tmp_path, m
source='gui', messages=history, compression_lock_holder='fixture')
finally:
db.release_compression_lock(parent, 'fixture')
+ publish_row = db._publish_child_session_row
+ def abort_publication(*args, **kwargs):
+ publish_row(*args, **kwargs)
+ raise RuntimeError('publication abort')
+ monkeypatch.setattr(db, '_publish_child_session_row', abort_publication)
+ with pytest.raises(RuntimeError, match='publication abort'):
+ compress(ref.session_id, 'aborted-child')
+ monkeypatch.setattr(db, '_publish_child_session_row', publish_row)
+ assert db.get_session('aborted-child') is None
+ assert local_receipt(db, ref.session_id) == original
+ assert db.get_session(ref.session_id)['end_reason'] is None
compress(ref.session_id, 'compressed')
second = runner()
cold = await initialize_session_authority(second, profile_id='fixture', instance_id='second')
@@ -87,6 +98,12 @@ async def test_local_lineage_transitions_preserve_owner_or_roll_back(tmp_path, m
with pytest.raises(RuntimeStoreError, match='unknown_execution'):
claim_session_input(restarted.db, epoch=restarted.epoch, session_id=ref.session_id)
assert len(list_session_admissions(restarted.db, session_id=ref.session_id)) == 1
+ receipt = local_receipt(db, ref.session_id)
+ db.set_meta('gateway.local_policy.v1:' + ref.session_id, json.dumps(dict(receipt, profile_id='foreign')))
+ with pytest.raises(RuntimeStoreError, match='storage_unavailable'):
+ compress('compressed-again', 'foreign-child')
+ assert db.get_session('foreign-child') is None
+ db.set_meta('gateway.local_policy.v1:' + ref.session_id, json.dumps(receipt))
foreign = Principal('foreign', 'fixture', actor.capabilities, 'foreign')
with pytest.raises(RuntimeStoreError, match='permission_denied'):
await restarted.attach(foreign, ref)