The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(state): share one worker receipt transaction across persist families
Teknium735496d8bfd8
diff --git a/gateway/session_worker_delegation.py b/gateway/session_worker_delegation.pyindex 581167033ca..90838b2aa73 100644--- a/gateway/session_worker_delegation.py+++ b/gateway/session_worker_delegation.py@@ -193,40 +193,11 @@ def persist_worker_delegation(db, *, epoch, execution_id, session_id, generationGateway calls this ONLY after its ordinary worker claim verification. Theverified process identity is deliberately separate from the operation payload."""- from gateway.session_admission import admission_fingerprint- from hermes_state_runtime import _epoch, _worker_assignment, _json+ from hermes_state_runtime import apply_worker_receipthandlers = worker_delegation_handlers(execution_id, worker_pid, worker_birth)- if type(sequence) is not int or sequence < 1 or not isinstance(operation, str) or operation not in handlers:- raise RuntimeStoreError('invalid_params')- encoded = _json(payload)- if len(encoded.encode('utf-8', errors='surrogatepass')) > 4 * 1024 * 1024:- raise RuntimeStoreError('invalid_params')- digest = admission_fingerprint(canonical_target=session_id,- payload={'operation': operation, 'payload': json.loads(encoded)})-- 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 row['status'] not in ('registered', 'running'):- raise RuntimeStoreError('stale_generation')- if sequence != row['last_sequence'] + 1:- raise RuntimeStoreError('invalid_params')- 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.dumps(result, ensure_ascii=True, allow_nan=False)))- conn.execute("UPDATE worker_executions SET last_sequence=?,status='running' WHERE execution_id=?",- (sequence, execution_id))- conn.execute('UPDATE sessions SET runtime_revision=runtime_revision+1 WHERE id=?', (session_id,))- return result- return db._execute_write(write, patience_s=db._TRANSCRIPT_WRITE_PATIENCE_S)+ return apply_worker_receipt(db, epoch=epoch, execution_id=execution_id, session_id=session_id,+ generation=generation, sequence=sequence, operation=operation,+ payload=payload, handlers=handlers)async def worker_delegation_request(connection, ref, params):diff --git a/hermes_state_runtime.py b/hermes_state_runtime.pyindex ab86e0ab7a5..f43ecea1a5b 100644--- a/hermes_state_runtime.py+++ b/hermes_state_runtime.py@@ -599,7 +599,7 @@ 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."""from hermes_state_worker_context import worker_context, worker_prompt, worker_sidecars, worker_tool_names- from hermes_state_worker_compression import WORKER_COMPRESSION_HANDLERS, worker_receipt_assignment+ from hermes_state_worker_compression import WORKER_COMPRESSION_HANDLERSfrom hermes_state_worker_lifecycle import WORKER_LIFECYCLE_HANDLERShandlers = {**WORKER_LIFECYCLE_HANDLERS,@@ -615,6 +615,21 @@ def mutate_worker_execution(db, *, epoch, execution_id, session_id, generation,**{name: (lambda db, conn, sid, p, op=name: _worker_turn(db, conn, sid, p, op))for name in ('turn.acquire', 'turn.renew', 'turn.release')},}+ return apply_worker_receipt(db, epoch=epoch, execution_id=execution_id, session_id=session_id,+ generation=generation, sequence=sequence, operation=operation,+ payload=payload, handlers=handlers)+++def apply_worker_receipt(db, *, epoch, execution_id, session_id, generation, sequence,+ operation, payload, handlers):+ """The one worker receipt transaction every ``worker.persist`` family shares.++ An exact (execution, sequence, digest) retry returns the stored result; otherwise the+ epoch, assignment, live status and next sequence are fenced, ``handlers[operation]`` runs on+ the transaction connection (it must not commit), and its result is stored as the receipt.+ ``execution.finish`` is the only operation that makes the execution terminal.+ """+ from hermes_state_worker_compression import worker_receipt_assignmentif type(sequence) is not int or sequence < 1 or not isinstance(operation, str) or operation not in handlers:raise RuntimeStoreError('invalid_params')encoded = _json(payload)