The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: native delete closes over delegate and fork continuations
Tekniumf2d934d1eb59
diff --git a/hermes_state_mutation_guards.py b/hermes_state_mutation_guards.pyindex 96664daa9a3..fc06eebddfb 100644--- a/hermes_state_mutation_guards.py+++ b/hermes_state_mutation_guards.py@@ -54,8 +54,25 @@ def require_idle(db, conn, session_ids):_require_transcript_unleased(db, conn, sid)+def _continuation_edges(conn, sid):+ """One both-direction compression step from *sid*: its continuation children and its+ compression parent. Fork markers are matched against the edge's parent id (the+ ``_non_continuation_child_sql`` rule), never by presence: a continuation copies its parent's+ ``model_config``, so an inherited ``_branched_from``/``_delegate_from`` naming an older row+ does not make it a fork, and a delegate's continuation published by a canonical ``compress``+ carries no marker at all."""+ from hermes_state_common import _non_continuation_child_sql+ edge = _non_continuation_child_sql('child.', 'parent.id')+ rows = conn.execute(+ 'SELECT child.id FROM sessions parent JOIN sessions child ON child.parent_session_id=parent.id '+ "WHERE parent.id=? AND parent.end_reason='compression'\n" + edge+ + ' UNION SELECT parent.id FROM sessions child JOIN sessions parent ON parent.id=child.parent_session_id '+ "WHERE child.id=? AND parent.end_reason='compression'\n" + edge, (sid, sid)).fetchall()+ return [row[0] for row in rows]++def delete_targets(conn, session_id):- from hermes_state_sessions import _collect_delegate_child_ids, _expand_compression_lineage_ids+ from hermes_state_sessions import _collect_delegate_child_idsimport jsonfrom hermes_state_compression import _CHAIN_CAPfrom hermes_state_local import POLICY_PREFIX@@ -72,16 +89,28 @@ def delete_targets(conn, session_id):# message on the route re-admits the "deleted" conversation through the surviving child.# The walk also runs BACKWARD to the root (#57543): a sidebar row carries the chain tip's# id, and a surviving root re-projects as the "deleted" conversation on the next reload.- # Every walked row must belong to the requested row's principal domain: only that row was+ # Every walked row must belong to its anchor's principal domain: only the requested row was# authorized, so a parent link into another principal's chain (an imported or forged edge)- # stops the walk instead of deleting their conversation.+ # stops the walk instead of deleting their conversation. A delegate subtree is the parent's+ # by construction and anchors its own chain (a continuation copies the delegate's route).+ # Delegates and continuations close over each other to a fixed point: a compressed+ # delegate's continuation (and its queued work) goes with it, or require_idle never sees it+ # and the delete orphans it.from hermes_state_mutation_binding import same_history_owner+ anchor = dict.fromkeys(targets, session_id)frontier = list(targets)for _ in range(_CHAIN_CAP):- frontier = [sid for sid in _expand_compression_lineage_ids(conn, frontier)- if sid not in targets and same_history_owner(conn, session_id, sid)]- if not frontier:+ found = {}+ for sid in frontier:+ for nxt in _continuation_edges(conn, sid):+ if nxt not in targets and nxt not in found and same_history_owner(conn, anchor[sid], nxt):+ found[nxt] = anchor[sid]+ for sid in _collect_delegate_child_ids(conn, frontier):+ if sid not in targets:+ found.setdefault(sid, sid)+ if not found:break- targets.update(frontier)- targets.update(_collect_delegate_child_ids(conn, targets))+ anchor.update(found)+ targets.update(found)+ frontier = list(found)return [session_id, *sorted(targets - {session_id})]diff --git a/tests/hermes_state/test_runtime_destructive_mutations.py b/tests/hermes_state/test_runtime_destructive_mutations.pyindex ee3ecb85a99..e6e544b1a19 100644--- a/tests/hermes_state/test_runtime_destructive_mutations.py+++ b/tests/hermes_state/test_runtime_destructive_mutations.py@@ -68,3 +68,60 @@ def test_delete_of_compressed_logical_root_retires_every_physical_continuation(tassert db.get_session('child') is Noneassert db.get_messages_as_conversation('child') == []assert db.load_gateway_routing_entries() == {}+++def _compressed(db, parent, child, model_config=None):+ assert db.try_acquire_compression_lock(parent, 'holder')+ try:+ db.publish_compression_child(parent_session_id=parent, child_session_id=child, source='cli',+ model_config=model_config, messages=[{'role': 'user', 'content': f'history of {child}'}],+ compression_lock_holder='holder')+ finally:+ db.release_compression_lock(parent, 'holder')+++def _delegate_topology(db, shape):+ """root → (delegate | inherited fork) → its compression continuation(s); returns the tip.+ A real rotation copies the fork's model_config (marker names the ORIGINAL parent); a+ canonical compress publishes the continuation with no fork marker at all."""+ db.create_session('root', source='telegram')+ if shape == 'inherited-branch':+ cfg = {'_branched_from': 'ancestor'}+ db.create_session('ancestor', source='telegram')+ db._execute_write(lambda c: c.execute(+ "UPDATE sessions SET parent_session_id='ancestor', model_config=? WHERE id='root'", ('{"_branched_from": "ancestor"}',)))+ _compressed(db, 'root', 'tip', cfg)+ return 'tip', {'root', 'tip'}+ db.create_session('delegate', source='cli', parent_session_id='root', model_config={'_delegate_from': 'root'})+ _compressed(db, 'delegate', 'tip', {'_delegate_from': 'root'} if shape == 'inherited-delegate' else None)+ _compressed(db, 'tip', 'tip2', None)+ return 'tip2', {'root', 'delegate', 'tip', 'tip2'}++[email protected]('shape', ['inherited-delegate', 'unmarked-delegate', 'inherited-branch'])+def test_queued_work_on_a_delegate_or_fork_continuation_blocks_the_delete(tmp_path, shape):+ """The descendant walk used to skip continuations whose model_config carries a fork marker+ (inherited from the fork, or absent after a canonical compress): the delete then succeeded,+ leaving the continuation and its queued admission orphaned under a deleted parent."""+ with SessionDB(db_path=tmp_path / 'state.db') as db:+ tip, _ = _delegate_topology(db, shape)+ epoch = rt.begin_runtime_epoch(db, instance_id='owner')+ rt.admit_session_input(db, epoch=epoch, principal_id='human', session_id=tip, request_id='q', payload={'text': 'keep'})+ before = [dict(r) for r in db._read_all('SELECT id, parent_session_id FROM sessions ORDER BY id')]+ with pytest.raises(rt.RuntimeStoreError, match='session_busy'):+ rt.mutate_runtime_session(db, epoch=epoch, principal_id='human', session_id='root', request_id='delete',+ expected_revision=0, expected_generation=0, operation='delete', payload={})+ assert [dict(r) for r in db._read_all('SELECT id, parent_session_id FROM sessions ORDER BY id')] == before++[email protected]('shape', ['inherited-delegate', 'unmarked-delegate', 'inherited-branch'])+def test_idle_delete_takes_every_delegate_and_fork_continuation(tmp_path, shape):+ with SessionDB(db_path=tmp_path / 'state.db') as db:+ _, chain = _delegate_topology(db, shape)+ epoch = rt.begin_runtime_epoch(db, instance_id='owner')+ receipt = rt.mutate_runtime_session(db, epoch=epoch, principal_id='human', session_id='root', request_id='delete',+ expected_revision=0, expected_generation=0, operation='delete', payload={})+ assert set(receipt['deleted_ids']) == chain+ assert all(db.get_session(sid) is None for sid in chain)+ if shape == 'inherited-branch':+ assert db.get_session('ancestor') is not None