The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(state): a restarted owner refuses a late admission-worker handshake
Teknium58d9d012e493
diff --git a/hermes_state_runtime.py b/hermes_state_runtime.pyindex 47c6c266533..0cf921ad427 100644--- a/hermes_state_runtime.py+++ b/hermes_state_runtime.py@@ -476,6 +476,12 @@ def adopt_worker_execution(db, *, epoch: int, execution_id: str, session_id: strraise RuntimeStoreError('stale_generation')if not hmac.compare_digest(row['adoption_digest'], digest):raise RuntimeStoreError('permission_denied')+ # An admission-reserved worker reports to the reserving owner over that owner's pipes and+ # dies with them, so only its same-epoch handshake adopts it. A late handshake reaching a+ # restarted owner would re-arm the recovered `unknown` admission as `started` with nobody+ # left to settle it (and Discard refuses `started`); refuse it so recovery's verdict stands.+ if execution_id.startswith('admission-worker:') and row['owner_epoch'] != epoch:+ raise RuntimeStoreError('stale_epoch')linked = _linked_worker_admission(conn, session_id, generation, row['owner_epoch'], ('started', 'unknown'))if linked is not None:diff --git a/tests/gateway/test_managed_worker_fencing.py b/tests/gateway/test_managed_worker_fencing.pyindex 62708021abd..08105bc66c5 100644--- a/tests/gateway/test_managed_worker_fencing.py+++ b/tests/gateway/test_managed_worker_fencing.py@@ -42,3 +42,44 @@ def test_worker_loss_fences_same_generation_parent_result(tmp_path):if child.poll() is None:child.kill()child.wait(timeout=5)+++async def test_late_bootstrap_handshake_cannot_rearm_a_recovered_admission(tmp_path):+ """The owner reserved and bootstrapped a managed child, then died before the child's first+ ``worker.adopt``; that handshake reaches the restarted owner. Adopting it would flip the+ recovered ``unknown`` admission back to ``started`` under an owner with no pipe to the child+ (which refuses the foreign epoch and exits), stranding the FIFO where Discard cannot reach it."""+ import hermes_state_runtime as rt+ from gateway.session_worker import worker_request+ from gateway.session_worker_reservation import reserve_admission_worker+ with closing(SessionDB(tmp_path / 'state.db')) as db:+ db.create_session('owned', 'cli')+ epoch = begin_runtime_epoch(db, instance_id='owner')+ authority = SimpleNamespace(db=db, epoch=epoch, profile_id=str(tmp_path), _require_admission_open=lambda: None)+ lost = admit_session_input(db, epoch=epoch, principal_id='human', session_id='owned', request_id='lost', payload={})+ row = claim_session_input(db, epoch=epoch, session_id='owned')+ follower = admit_session_input(db, epoch=epoch, principal_id='human', session_id='owned', request_id='next', payload={})+ child = subprocess.Popen([sys.executable, '-c', 'import sys; sys.stdin.read()'],+ stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)+ try:+ scope = reserve_admission_worker(authority, admission_id=row['admission_id'], process=child,+ principal_id='human', hello=hello(child))+ restarted = begin_runtime_epoch(db, instance_id='restarted')+ rt.recover_session_inputs(db, epoch=restarted)+ owner = SimpleNamespace(db=db, epoch=restarted, profile_id=str(tmp_path), _require_admission_open=lambda: None)+ actor = SimpleNamespace(subject='human', profile_id=str(tmp_path), capabilities={'worker:adopt'})+ identity = {k: v for k, v in scope.items() if k != 'epoch'}+ with pytest.raises(rt.RuntimeStoreError, match='stale_epoch'):+ await worker_request(SimpleNamespace(authority=owner, actor=actor), None, identity, operation='adopt')+ assert db._read_one('SELECT status,owner_epoch FROM session_admissions WHERE admission_id=?',+ (lost['admission_id'],))[:] == ('unknown', epoch)+ assert db._read_one('SELECT status,owner_epoch FROM worker_executions')[:] == ('unknown', epoch)+ rt.resolve_unknown_session_input(db, epoch=restarted, admission_id=lost['admission_id'],+ generation=row['generation'])+ assert claim_session_input(db, epoch=restarted, session_id='owned')['admission_id'] == follower['admission_id']+ finally:+ child.stdin.close()+ if child.poll() is None:+ child.kill()+ child.wait(timeout=5)