Skip to content

The agent that grows with you

feat: fence typed worker persistence with atomic replay receipts

Teknium7cc69d293faa
diff --git a/hermes_state_runtime.py b/hermes_state_runtime.py
index 837776e3e2c..685151256d0 100644
--- a/hermes_state_runtime.py
+++ b/hermes_state_runtime.py
@@ -114,7 +114,9 @@ def claim_session_input(db, *, epoch: int, session_id: str) -> dict | None:
blocked = conn.execute("SELECT status FROM session_admissions WHERE target_session_id=? AND status IN ('started','unknown')", (session_id,)).fetchall()
if any(row[0] == 'unknown' for row in blocked):
raise RuntimeStoreError('unknown_execution')
- if blocked:
+ if conn.execute("SELECT 1 FROM worker_executions WHERE session_id=? AND status='unknown'", (session_id,)).fetchone():
+ raise RuntimeStoreError('unknown_execution')
+ if blocked or conn.execute("SELECT 1 FROM worker_executions WHERE session_id=? AND status IN ('registered','running')", (session_id,)).fetchone():
return None
row = conn.execute("SELECT * FROM session_admissions WHERE target_session_id=? AND status='queued' ORDER BY seq LIMIT 1", (session_id,)).fetchone()
if row is None:
@@ -163,6 +165,7 @@ def recover_session_inputs(db, *, epoch: int) -> int:
"""Never replay started work. Live worker adoption is a separate explicit operation."""
def write(conn):
_epoch(conn, epoch)
+ conn.execute("UPDATE worker_executions SET status='unknown' WHERE status IN ('registered','running') AND owner_epoch!=?", (epoch,))
return conn.execute("UPDATE session_admissions SET status='unknown' WHERE status='started' AND owner_epoch!=?", (epoch,)).rowcount
return db._execute_write(write)
@@ -263,3 +266,117 @@ def resolve_unknown_session_input(db, *, epoch: int, admission_id: str, generati
conn.execute('UPDATE sessions SET runtime_revision=runtime_revision+1 WHERE id=?', (row['target_session_id'],))
return _row(_admission(conn, admission_id))
return db._execute_write(write)
+
+
+def _worker_public(row):
+ result = dict(row)
+ result.pop('adoption_digest')
+ return result
+
+
+def _worker_assignment(conn, execution_id, session_id, generation):
+ row = conn.execute('SELECT * FROM worker_executions WHERE execution_id=?', (execution_id,)).fetchone()
+ if row is None:
+ raise RuntimeStoreError('not_found')
+ if row['session_id'] != session_id:
+ raise RuntimeStoreError('permission_denied')
+ if (type(generation) is not int or row['generation'] != generation
+ or _session(conn, session_id)['runtime_generation'] != generation
+ or row['status'] == 'terminal'):
+ raise RuntimeStoreError('stale_generation')
+ return row
+
+
+def _secret_digest(secret):
+ import hashlib
+ _text(secret)
+ return hashlib.sha256(secret.encode('utf-8')).hexdigest()
+
+
+def register_worker_execution(db, *, epoch: int, execution_id: str, session_id: str,
+ generation: int, kind: str, adoption_secret: str) -> dict:
+ for value in (execution_id, session_id):
+ _text(value)
+ if kind not in ('cron', 'child', 'compute', 'kanban'):
+ raise RuntimeStoreError('invalid_params')
+ digest = _secret_digest(adoption_secret)
+ def write(conn):
+ _epoch(conn, epoch)
+ session = _session(conn, session_id)
+ if type(generation) is not int or session['runtime_generation'] != generation:
+ raise RuntimeStoreError('stale_generation')
+ old = conn.execute('SELECT * FROM worker_executions WHERE execution_id=?', (execution_id,)).fetchone()
+ if old is not None:
+ if (old['session_id'], old['generation'], old['kind'], old['owner_epoch'], old['adoption_digest']) != (session_id, generation, kind, epoch, digest):
+ raise RuntimeStoreError('admission_conflict')
+ return _worker_public(old)
+ if conn.execute("SELECT 1 FROM worker_executions WHERE session_id=? AND status!='terminal'", (session_id,)).fetchone():
+ raise RuntimeStoreError('stale_generation')
+ if conn.execute("SELECT 1 FROM session_admissions WHERE target_session_id=? AND status='unknown'", (session_id,)).fetchone():
+ raise RuntimeStoreError('unknown_execution')
+ conn.execute("""INSERT INTO worker_executions(execution_id,session_id,kind,owner_epoch,generation,status,adoption_digest)
+ VALUES(?,?,?,?,?,'registered',?)""", (execution_id, session_id, kind, epoch, generation, digest))
+ return _worker_public(_worker_assignment(conn, execution_id, session_id, generation))
+ return db._execute_write(write)
+
+
+def adopt_worker_execution(db, *, epoch: int, execution_id: str, session_id: str,
+ generation: int, adoption_secret: str) -> dict:
+ """Authority first verifies the original producer claim and live worker proof."""
+ import hmac
+ digest = _secret_digest(adoption_secret)
+ def write(conn):
+ _epoch(conn, epoch)
+ row = _worker_assignment(conn, execution_id, session_id, generation)
+ if not hmac.compare_digest(row['adoption_digest'], digest):
+ raise RuntimeStoreError('permission_denied')
+ conn.execute("UPDATE worker_executions SET owner_epoch=?,status='running' WHERE execution_id=?", (epoch, execution_id))
+ return _worker_public(_worker_assignment(conn, execution_id, session_id, generation))
+ return db._execute_write(write)
+
+
+def persist_worker_message(db, *, epoch: int, execution_id: str, session_id: str,
+ generation: int, sequence: int, role: str, content: str) -> dict:
+ """Typed text append primitive, NOT a general remote SessionDB implementation.
+
+ Structured tools/reasoning/usage/compression require their own typed operations.
+ No caller-supplied callable can commit inside this transaction.
+ """
+ import time
+ if type(sequence) is not int or sequence < 1 or role not in ('user', 'assistant', 'system') or not isinstance(content, str):
+ raise RuntimeStoreError('invalid_params')
+ digest = admission_fingerprint(canonical_target=session_id, payload={'operation': 'append_text', 'role': role, 'content': content})
+ def write(conn):
+ _epoch(conn, epoch)
+ row = _worker_assignment(conn, execution_id, session_id, generation)
+ if row['owner_epoch'] != epoch:
+ raise RuntimeStoreError('stale_epoch')
+ old = conn.execute('SELECT * FROM worker_receipts WHERE execution_id=? AND sequence=?', (execution_id, sequence)).fetchone()
+ if old is not None:
+ if old['payload_digest'] != digest:
+ raise RuntimeStoreError('admission_conflict')
+ return json.loads(old['result_json'])
+ if sequence != row['last_sequence'] + 1:
+ raise RuntimeStoreError('invalid_params')
+ now = time.time()
+ message = conn.execute('INSERT INTO messages(session_id,role,content,timestamp) VALUES(?,?,?,?)', (session_id, role, content, now))
+ conn.execute('UPDATE sessions SET message_count=message_count+1,last_activity_at=?,runtime_revision=runtime_revision+1 WHERE id=?', (now, session_id))
+ result = {'message_id': message.lastrowid}
+ conn.execute('INSERT INTO worker_receipts(execution_id,sequence,payload_digest,result_json) VALUES(?,?,?,?)', (execution_id, sequence, digest, _json(result)))
+ conn.execute("UPDATE worker_executions SET last_sequence=?,status='running' WHERE execution_id=?", (sequence, execution_id))
+ return result
+ return db._execute_write(write)
+
+
+def finish_worker_execution(db, *, epoch: int, execution_id: str, session_id: str,
+ generation: int) -> dict:
+ def write(conn):
+ _epoch(conn, epoch)
+ row = _worker_assignment(conn, execution_id, session_id, generation)
+ if row['owner_epoch'] != epoch:
+ raise RuntimeStoreError('stale_epoch')
+ conn.execute("UPDATE worker_executions SET status='terminal' WHERE execution_id=?", (execution_id,))
+ result = _worker_public(row)
+ result['status'] = 'terminal'
+ return result
+ return db._execute_write(write)
diff --git a/tests/state/test_runtime_worker.py b/tests/state/test_runtime_worker.py
new file mode 100644
index 00000000000..04932d528e3
--- /dev/null
+++ b/tests/state/test_runtime_worker.py
@@ -0,0 +1,63 @@
+import pytest
+import sqlite3
+
+from hermes_state import SessionDB
+import hermes_state_runtime as rt
+
+
+def setup_worker(tmp_path):
+ assert callable(getattr(rt, 'register_worker_execution', None)), 'canonical worker receipts missing'
+ db = SessionDB(db_path=tmp_path / 'state.db')
+ db.create_session('s', source='test')
+ epoch = rt.begin_runtime_epoch(db, instance_id='boot')
+ assignment = dict(execution_id='worker', session_id='s', generation=0)
+ rt.register_worker_execution(db, epoch=epoch, **assignment, kind='compute', adoption_secret='private-fixture')
+ return db, epoch, assignment
+
+
+def test_worker_receipts_require_exact_sequence_assignment_and_adoption(tmp_path):
+ db, epoch, assignment = setup_worker(tmp_path)
+ try:
+ assert rt.claim_session_input(db, epoch=epoch, session_id='s') is None
+ args = dict(epoch=epoch, **assignment, sequence=1, role='assistant', content='kept output')
+ result = rt.persist_worker_message(db, **args)
+ assert rt.persist_worker_message(db, **args) == result
+ assert len(db.get_messages('s')) == 1
+ for change, reason in [({'content': 'different'}, 'admission_conflict'), ({'sequence': 3}, 'invalid_params'), ({'session_id': 'other'}, 'permission_denied'), ({'generation': 1}, 'stale_generation')]:
+ with pytest.raises(rt.RuntimeStoreError, match=reason):
+ rt.persist_worker_message(db, **(args | change))
+ new_epoch = rt.begin_runtime_epoch(db, instance_id='replacement')
+ rt.recover_session_inputs(db, epoch=new_epoch)
+ with pytest.raises(rt.RuntimeStoreError, match='stale_epoch'):
+ rt.persist_worker_message(db, **args)
+ with pytest.raises(rt.RuntimeStoreError, match='stale_epoch'):
+ rt.persist_worker_message(db, **(args | {'epoch': new_epoch}))
+ with pytest.raises(rt.RuntimeStoreError, match='permission_denied'):
+ rt.adopt_worker_execution(db, epoch=new_epoch, **assignment, adoption_secret='wrong')
+ adopted = rt.adopt_worker_execution(db, epoch=new_epoch, **assignment, adoption_secret='private-fixture')
+ assert adopted['owner_epoch'] == new_epoch and 'adoption_digest' not in adopted
+ assert rt.persist_worker_message(db, **(args | {'epoch': new_epoch})) == result
+ rt.persist_worker_message(db, **(args | {'epoch': new_epoch, 'sequence': 2, 'content': 'next'}))
+ rt.finish_worker_execution(db, epoch=new_epoch, **assignment)
+ with pytest.raises(rt.RuntimeStoreError, match='stale_generation'):
+ rt.persist_worker_message(db, **(args | {'epoch': new_epoch, 'sequence': 3}))
+ assert [r['content'] for r in db.get_messages('s')] == ['kept output', 'next']
+ finally:
+ db.close()
+
+
+def test_worker_mutation_and_receipt_roll_back_together(tmp_path):
+ db, epoch, assignment = setup_worker(tmp_path)
+ try:
+ db._conn.execute("CREATE TRIGGER reject_receipt BEFORE INSERT ON worker_receipts BEGIN SELECT RAISE(ABORT, 'receipt fixture'); END")
+ args = dict(epoch=epoch, **assignment, sequence=1, role='assistant', content='once')
+ with pytest.raises(sqlite3.IntegrityError, match='receipt fixture'):
+ rt.persist_worker_message(db, **args)
+ assert db.get_messages('s') == []
+ assert db.get_session('s')['message_count'] == 0
+ assert db._conn.execute("SELECT last_sequence FROM worker_executions WHERE execution_id='worker'").fetchone()[0] == 0
+ db._conn.execute('DROP TRIGGER reject_receipt')
+ rt.persist_worker_message(db, **args)
+ assert len(db.get_messages('s')) == db.get_session('s')['message_count'] == 1
+ finally:
+ db.close()