Skip to content

The agent that grows with you

fix: preserve live delegate tasks during parent deletion

Tekniumb0f792ac49a7
diff --git a/gateway/session_mutations.py b/gateway/session_mutations.py
index 7ae0184dbf5..e2d20e35621 100644
--- a/gateway/session_mutations.py
+++ b/gateway/session_mutations.py
@@ -30,24 +30,26 @@ async def mutate_session(authority, actor, ref, params):
applied = False
- def live_guard():
+ def live_guard(targets):
nonlocal applied
applied = True
if operation in _METADATA or operation == 'import':
return
- if live is not None and live.task is not None and not live.task.done():
- raise RuntimeStoreError('session_busy')
+ for sid in targets:
+ candidate = authority.sessions.get(sid)
+ if candidate is not None and candidate.task is not None and not candidate.task.done():
+ raise RuntimeStoreError('session_busy')
if operation == 'delete':
# Route retirement must commit with deletion, not recreate the same
# physical ID on the next native message. Do not fake that handoff.
store = getattr(authority.runner, 'session_store', None)
- if store is not None and store.lookup_by_session_id(ref.session_id) is not None:
+ if store is not None and any(store.lookup_by_session_id(sid) is not None for sid in targets):
raise RuntimeStoreError('runtime_coordination_required')
if operation == 'rewind' and not callable(getattr(authority.runner, '_evict_cached_agent', None)):
raise RuntimeStoreError('runtime_coordination_required')
if operation in _RUNTIME_ACTIONS:
- live_guard()
+ live_guard([ref.session_id])
raise RuntimeStoreError('runtime_coordination_required')
result = mutate_runtime_session(authority.db, epoch=authority.epoch,
principal_id=actor.subject, session_id=ref.session_id, request_id=params['request_id'],
diff --git a/hermes_state_runtime.py b/hermes_state_runtime.py
index 6bb61a16b6f..e8818dc274b 100644
--- a/hermes_state_runtime.py
+++ b/hermes_state_runtime.py
@@ -217,7 +217,9 @@ def mutate_runtime_session(db, *, epoch: int, principal_id: str, session_id: str
if expected_generation is not None and (session is None or session['runtime_generation'] != expected_generation):
raise RuntimeStoreError('stale_generation')
if _live_guard is not None:
- _live_guard()
+ from hermes_state_mutation_guards import delete_targets
+ targets = delete_targets(conn, session_id) if operation == 'delete' else [session_id]
+ _live_guard(targets)
affected, projection = apply_action(db, conn, session_id, operation, payload)
conn.executemany('UPDATE sessions SET runtime_revision=runtime_revision+1 WHERE id=?',
[(target,) for target in affected])
diff --git a/tests/gateway/test_session_mutation_delete_fences.py b/tests/gateway/test_session_mutation_delete_fences.py
new file mode 100644
index 00000000000..7cb1778971f
--- /dev/null
+++ b/tests/gateway/test_session_mutation_delete_fences.py
@@ -0,0 +1,31 @@
+"""Deletion covers live descendants before their durable claim."""
+import asyncio
+from types import SimpleNamespace
+import pytest
+from gateway.session_authority import LiveSession, SessionAuthority
+from gateway.session_controls import AuthorityConnection
+from hermes_state import SessionDB
+import hermes_state_runtime as rt
+
+
+async def test_delete_cannot_remove_a_live_delegate_without_a_claim(tmp_path):
+ with SessionDB(db_path=tmp_path / 'state.db') as db:
+ db.create_session('s', source='test')
+ db.create_session('child', source='test', parent_session_id='s', model_config={'_delegate_from': 's'})
+ epoch = rt.begin_runtime_epoch(db, instance_id='owner')
+ authority = SessionAuthority(SimpleNamespace(_draining=False), profile_id='owned', instance_id='owner', db=db, epoch=epoch)
+ authority.sessions['s'] = LiveSession(None, 'route')
+ child = authority.sessions['child'] = LiveSession(None, 'child-route')
+ child.task = asyncio.create_task(asyncio.Event().wait())
+ owner = AuthorityConnection(authority, object(), {'user_id': 'human'})
+ try:
+ response = await owner.dispatch({'id': 1, 'method': 'session.mutate', 'params': {'session_id': 's',
+ 'request_id': 'delete', 'expected_revision': 0, 'expected_generation': 0,
+ 'operation': 'delete', 'payload': {}}})
+ assert response['error']['message'] == 'session_busy'
+ assert db.get_session('child') is not None
+ finally:
+ child.task.cancel()
+ await asyncio.gather(child.task, return_exceptions=True)
+ await owner.close()