Skip to content

The agent that grows with you

fix(gateway): keep fallback-evicted children listed across a rotating compress

yoniebans3e3665c610b0
diff --git a/gateway/session_ancillary.py b/gateway/session_ancillary.py
index 51af5bc8691..0dd46c1e341 100644
--- a/gateway/session_ancillary.py
+++ b/gateway/session_ancillary.py
@@ -367,8 +367,8 @@ def control_snapshot(authority, ref):
'updated_at': _snapshot_updated_at(*states)}
-def _has_fallback_evicted_ancestor(agent, target_sid, expected_db, route, registry):
- if not target_sid:
+def _has_fallback_evicted_ancestor(agent, target_sids, expected_db, route, registry):
+ if not target_sids:
return False
current = agent
for _ in range(getattr(registry, '_MAX_ANCESTOR_HOPS', 8)):
@@ -379,7 +379,7 @@ def _has_fallback_evicted_ancestor(agent, target_sid, expected_db, route, regist
marker = getattr(current, '_evicted_on_fallback', False)
if not (marker is True or (isinstance(marker, str) and bool(marker))):
continue
- if getattr(current, 'session_id', None) != target_sid:
+ if getattr(current, 'session_id', None) not in target_sids:
continue
if (expected_db is not None or getattr(current, '_session_db', None) is not None) and getattr(current, '_session_db', None) is not expected_db:
continue
@@ -398,11 +398,31 @@ def subagent_records(parent, session_id=None, *, session_db=None, route=None, in
return []
expected_db = session_db if session_db is not None else getattr(parent, '_session_db', None)
effective_route = route if route is not None else getattr(parent, 'gateway_session_key', None)
+ # A rotating /compress moves the conversation to a new stored id; an evicted parent keeps its earlier one.
+ target_sids = _compression_lineage(expected_db, target_sid)
with registry._active_subagents_lock:
# Deliberately no unconditional durable-ID fallback: only a parent evicted by this session's own fallback keeps its children visible in the read-only roster.
return [dict(record) for record in registry._active_subagents.values()
if (parent is not None and registry._is_descendant_of(record.get('agent'), parent))
- or _has_fallback_evicted_ancestor(record.get('agent'), target_sid, expected_db, effective_route, registry)]
+ or _has_fallback_evicted_ancestor(record.get('agent'), target_sids, expected_db, effective_route, registry)]
+
+
+def _compression_lineage(db, session_id):
+ """*session_id* plus the sessions it continues by compression, walked backward so a closed sibling under a compressed parent cannot divert the walk."""
+ if not session_id:
+ return frozenset()
+ ids, current = {session_id}, session_id
+ is_child, get = getattr(db, '_is_compression_child_row', None), getattr(db, 'get_session', None)
+ if callable(is_child) and callable(get):
+ row = get(current)
+ while row and is_child(row) and row['parent_session_id'] not in ids and len(ids) < 200:
+ current = row['parent_session_id']
+ ids.add(current)
+ row = get(current)
+ return frozenset(ids)
+ lineage = getattr(db, 'get_compression_lineage', None)
+ found = lineage(session_id) if callable(lineage) else None
+ return frozenset(found if isinstance(found, (list, tuple)) else ()) | ids
def subagent_tail(records, subagent_id):
diff --git a/tests/gateway/test_local_viewer_events_turn.py b/tests/gateway/test_local_viewer_events_turn.py
index b8d15a8a92d..0c7865bd249 100644
--- a/tests/gateway/test_local_viewer_events_turn.py
+++ b/tests/gateway/test_local_viewer_events_turn.py
@@ -533,6 +533,37 @@ def test_subagent_roster_retains_running_child_after_fallback_evicts_parent():
assert [r['subagent_id'] for r in _owner_objects(authority_a, ref, include_fallback_evicted=True)[1]] == ['fallback-child']
assert _owner_objects(authority_a, ref, include_fallback_evicted=False)[1] == []
assert subagent_records(replacement, include_fallback_evicted=False) == []
+ # A rotating /compress after the fallback moves the conversation to a new stored id; the lineage keeps the child listed, still scoped by DB and route.
+ class _LineageDB:
+ def get_compression_lineage(self, session_id):
+ return ['same-parent', 'same-parent-c1'] if session_id in ('same-parent', 'same-parent-c1') else [session_id]
+ lineage_db = _LineageDB()
+ for agent_obj in (original, wrong_route_parent, replacement):
+ agent_obj._session_db = lineage_db
+ rotated = Parent()
+ rotated.session_id = 'same-parent-c1'
+ rotated._session_db = lineage_db
+ rotated.gateway_session_key = 'route-a'
+ assert [r['subagent_id'] for r in subagent_records(rotated)] == ['fallback-child']
+ assert subagent_records(None, session_id='unrelated', session_db=lineage_db, route='route-a') == []
+ # Ancestry is walked backward from the live session on the real store, so an orphaned closed sibling under a compressed parent never hides the evicted parent's children.
+ import tempfile
+ from pathlib import Path
+ from hermes_state import SessionDB
+ with tempfile.TemporaryDirectory() as tmp:
+ real_db = SessionDB(db_path=Path(tmp) / 'state.db')
+ with real_db:
+ real_db.create_session('same-parent', source='cli')
+ real_db.end_session('same-parent', 'compression')
+ real_db.create_session('orphan', source='cli', parent_session_id='same-parent')
+ real_db.end_session('orphan', 'ws_orphan_reap')
+ real_db.create_session('live', source='cli', parent_session_id='same-parent')
+ real_db.end_session('live', 'compression')
+ real_db.create_session('tip', source='cli', parent_session_id='live')
+ real_db.create_session('elsewhere', source='cli')
+ original._session_db = real_db
+ assert [r['subagent_id'] for r in subagent_records(None, session_id='tip', session_db=real_db, route='route-a')] == ['fallback-child']
+ assert subagent_records(None, session_id='elsewhere', session_db=real_db, route='route-a') == []
finally:
for sid in ('fallback-child', 'wrong-db-child', 'wrong-route-child', 'stale-child'):
_unregister_subagent(sid)