Skip to content

The agent that grows with you

fix(state): worker read receipts leave runtime_revision unchanged

Tekniuma05b822e6d0b
diff --git a/hermes_state_runtime.py b/hermes_state_runtime.py
index 0cf921ad427..9d6f8bc9fc9 100644
--- a/hermes_state_runtime.py
+++ b/hermes_state_runtime.py
@@ -659,11 +659,17 @@ def apply_worker_receipt(db, *, epoch, execution_id, session_id, generation, seq
if sequence != row['last_sequence'] + 1:
raise RuntimeStoreError('invalid_params')
# Each SQLite retry gets fresh rows; rolled-back annotations must not escape.
+ changes = conn.total_changes
result = handlers[operation](db, conn, session_id, json.loads(encoded))
+ # The CAS fence moves only when the handler changed durable state (or the execution
+ # closes): a read-only receipt (context, history, lineage, lifecycle) must not make a
+ # client's mutation built on the pre-read revision fail revision_conflict.
+ changed = conn.total_changes != changes or operation == 'execution.finish'
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=? 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,))
+ if changed:
+ 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)
diff --git a/tests/hermes_state/test_runtime_worker_read_fence.py b/tests/hermes_state/test_runtime_worker_read_fence.py
new file mode 100644
index 00000000000..bd5727babda
--- /dev/null
+++ b/tests/hermes_state/test_runtime_worker_read_fence.py
@@ -0,0 +1,27 @@
+"""Worker read receipts leave the session CAS fence where a client last saw it (W14)."""
+from hermes_state_runtime import mutate_runtime_session
+from tests.hermes_state.test_runtime_worker_lifecycle import setup_store
+
+
+def test_worker_reads_keep_revision_and_writes_still_advance_it(tmp_path):
+ db, store, scope = setup_store(tmp_path)
+ try:
+ store.append_messages_batch('owned', [{'role': 'user', 'content': 'hello'}])
+ seen = db.get_session('owned')['runtime_revision']
+ store.get_session('owned')
+ store.session_lifecycle_statuses(['owned'])
+ store.get_messages_as_conversation('owned')
+ store.get_compression_tip('owned')
+ assert db.get_session('owned')['runtime_revision'] == seen
+ # A client mutation built on the pre-read revision is not refused by those reads.
+ result = mutate_runtime_session(db, epoch=scope['epoch'], principal_id='client', session_id='owned',
+ request_id='rename', expected_revision=seen,
+ operation='rename', payload={'title': 'renamed'})
+ assert result['revision'] == seen + 1
+ store.touch_session_activity('owned', description='working')
+ assert db.get_session('owned')['runtime_revision'] == seen + 2
+ store.finish()
+ assert db.get_session('owned')['runtime_revision'] == seen + 3
+ finally:
+ store.close()
+ db.close()