Skip to content

The agent that grows with you

fix(state): queued input waits for a worker that compression moved onto a child session

Tekniuma32fc3b096ff
diff --git a/hermes_state_runtime.py b/hermes_state_runtime.py
index 4f669dc468f..a02192d0193 100644
--- a/hermes_state_runtime.py
+++ b/hermes_state_runtime.py
@@ -145,9 +145,16 @@ 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 conn.execute("SELECT 1 FROM worker_executions WHERE session_id=? AND status='unknown'", (session_id,)).fetchone():
+ # A worker follows its physical transcript (compression publish / local reset target)
+ # while this FIFO stays keyed on the logical owner: scan the whole lineage.
+ from hermes_state_local_lineage import local_physical_target
+ lineage = {*_canonical_chain(conn, session_id), *_canonical_chain(conn, local_physical_target(conn, session_id))}
+ marks = ','.join('?' * len(lineage))
+ workers = {row[0] for row in conn.execute(
+ f"SELECT status FROM worker_executions WHERE session_id IN ({marks}) AND status!='terminal'", tuple(lineage))}
+ if 'unknown' in workers:
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():
+ if blocked or workers:
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:
diff --git a/tests/hermes_state/test_runtime_worker_compression_admission.py b/tests/hermes_state/test_runtime_worker_compression_admission.py
index e708f878655..3abb06de8b7 100644
--- a/tests/hermes_state/test_runtime_worker_compression_admission.py
+++ b/tests/hermes_state/test_runtime_worker_compression_admission.py
@@ -54,3 +54,34 @@ def test_rotation_preserves_admission_identity_and_lost_ack_fifo(tmp_path):
store.journal['pending'] = []
store.close()
db.close()
+
+
+def test_fifo_stays_blocked_while_its_worker_lives_on_a_compression_child(tmp_path):
+ """Compression moves a live worker onto the physical child while the FIFO stays keyed on the
+ logical root. The root's queued input must not be claimed until that worker ends, or two
+ writers run in one lineage."""
+ db = SessionDB(tmp_path / 'state.db')
+ db.create_session('root', 'cli', system_prompt='PREFIX')
+ epoch = begin_runtime_epoch(db, instance_id='owner')
+ scope = dict(epoch=epoch, execution_id='worker', session_id='root', generation=0)
+ register_worker_execution(db, **scope, kind='compute', adoption_secret='private', require_idle=True)
+ store = RuntimeSessionStore(lambda method, **params: mutate_worker_execution(db, **params),
+ scope, tmp_path / 'outbox')
+ try:
+ messages = [{'role': 'user', 'content': 'kept'}]
+ store.append_messages_batch('root', messages)
+ assert store.try_acquire_compression_lock('root', 'holder')
+ store.publish_compression_child(parent_session_id='root', child_session_id='child', source='cli',
+ messages=messages, system_prompt='PREFIX', compression_lock_holder='holder')
+ assert store.scope['session_id'] == 'child'
+ follower = admit_session_input(db, epoch=epoch, principal_id='human', session_id='root',
+ request_id='follower', payload={'text': 'next'})
+ assert claim_session_input(db, epoch=epoch, session_id='root') is None
+ store.finish()
+ claimed = claim_session_input(db, epoch=epoch, session_id='root')
+ assert claimed is not None and claimed['admission_id'] == follower['admission_id']
+ finally:
+ store.failure = None
+ store.journal['pending'] = []
+ store.close()
+ db.close()