The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
feat: fence worker terminal receipt after sequenced durable writes
Teknium21a48aea09d7
diff --git a/agent/runtime_session_store.py b/agent/runtime_session_store.pyindex 8f79c306ad9..12e463d7d31 100644--- a/agent/runtime_session_store.py+++ b/agent/runtime_session_store.py@@ -212,6 +212,9 @@ class RuntimeSessionStore:raise WorkerPersistenceError('pending_receipt')return True+ def finish(self):+ return self._apply('execution.finish', {})+def close(self):with self.lock:try:diff --git a/hermes_state_runtime.py b/hermes_state_runtime.pyindex 18997c033ba..68179192e8a 100644--- a/hermes_state_runtime.py+++ b/hermes_state_runtime.py@@ -528,11 +528,18 @@ def _worker_usage(db, conn, session_id, payload, *, auxiliary=False):return {'value': None}+def _worker_finish(db, conn, session_id, payload):+ if payload:+ raise RuntimeStoreError('invalid_params')+ return {'status': 'terminal'}++def mutate_worker_execution(db, *, epoch, execution_id, session_id, generation,sequence, operation, payload):"""One closed durable mutation and receipt; never call a self-committing API here."""handlers = {'transcript.append': _worker_append,+ 'execution.finish': _worker_finish,'usage.main': _worker_usage,'usage.auxiliary': lambda db, conn, sid, p: _worker_usage(db, conn, sid, p, auxiliary=True),**{name: (lambda db, conn, sid, p, op=name: _worker_turn(db, conn, sid, p, op))@@ -564,8 +571,8 @@ def mutate_worker_execution(db, *, epoch, execution_id, session_id, generation,result = handlers[operation](db, conn, session_id, json.loads(encoded))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))+ conn.execute("UPDATE worker_executions SET last_sequence=?,status=? WHERE execution_id=?",+ (sequence, 'terminal' if operation == 'execution.finish' else 'running', execution_id))conn.execute('UPDATE sessions SET runtime_revision=runtime_revision+1 WHERE id=?', (session_id,))return resultreturn db._execute_write(write, patience_s=db._TRANSCRIPT_WRITE_PATIENCE_S)diff --git a/tests/gateway/fixtures/persistence_worker.py b/tests/gateway/fixtures/persistence_worker.pyindex c07489104ee..59061f31cfb 100644--- a/tests/gateway/fixtures/persistence_worker.py+++ b/tests/gateway/fixtures/persistence_worker.py@@ -79,6 +79,8 @@ for line in sys.stdin:adopted = transport('worker.adopt', **{k: v for k, v in scope.items() if k != 'epoch'})store.adopt(adopted['owner_epoch'])store.refresh_session_turn_lease(sid, 'owned-worker-lease')+ store.release_session_turn_lease(sid, 'owned-worker-lease')+ assert store.finish()['status'] == 'terminal'output = {'stale': stale, 'epoch': adopted['owner_epoch'], 'pid': os.getpid(),'writable_canonical_fds': writable_fds(home)}sys.stdout.write(json.dumps(output) + '\n'); sys.stdout.flush()diff --git a/tests/state/test_runtime_worker_operations.py b/tests/state/test_runtime_worker_operations.pyindex 7ef40379e1a..704b204f094 100644--- a/tests/state/test_runtime_worker_operations.py+++ b/tests/state/test_runtime_worker_operations.py@@ -46,5 +46,16 @@ def test_structured_worker_receipts_are_atomic_and_scoped(tmp_path):apply(3, 'SQL', {'sql': 'DELETE FROM messages'})assert apply(3, 'turn.renew', {'holder': 'worker-lease', 'ttl_seconds': 300})['value']assert apply(4, 'turn.release', {'holder': 'worker-lease'}) == {'value': None}+ assistant = {'role': 'assistant', 'content': 'canonical winner'}+ winner = apply(5, 'transcript.append', {'messages': [assistant]})+ repaired = apply(6, 'transcript.append', {'messages': [{'role': 'assistant',+ 'content': 'loser', '_row_id': winner['annotations'][0]['_row_id']}]})+ assert repaired['count'] == 0+ assert repaired['annotations'][0]['_canonical_content'] == 'canonical winner'+ terminal = apply(7, 'execution.finish', {})+ assert terminal['status'] == 'terminal'+ assert apply(7, 'execution.finish', {}) == terminal+ with pytest.raises(RuntimeStoreError, match='stale_generation'):+ apply(8, 'usage.main', {'input_tokens': 1})finally:db.close()