The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): the dock keeps background subagents across a parent rebuild
Teknium322828093e70
diff --git a/gateway/session_ancillary.py b/gateway/session_ancillary.pyindex 3fb8c97b866..54f72c73cf4 100644--- a/gateway/session_ancillary.py+++ b/gateway/session_ancillary.py@@ -483,10 +483,23 @@ def subagent_records(parent, session_id=None, *, session_db=None, route=None, in# 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_sids, expected_db, effective_route, registry)]+ or _has_fallback_evicted_ancestor(record.get('agent'), target_sids, expected_db, effective_route, registry)+ or _owned_by_lineage(record, target_sids, expected_db)]+++def _owned_by_lineage(record, target_sids, expected_db):+ """The read-only roster follows the conversation, as ``delegate_task(action="list")`` does: a+ child whose durable owner id is in this session's compression lineage, written to the same+ session database file. Without it every cached-agent rebuild (``/reasoning``, ``/model``, a+ policy refresh, idle eviction) hid still-running children from the dock while the status bar+ kept counting them. Control and tail stay pinned to the resident parent."""+ if not target_sids or record.get('owner_agent_session_id') not in target_sids:+ return False+ expected_path = getattr(expected_db, 'db_path', None)+ child_path = getattr(getattr(record.get('agent'), '_session_db', None), 'db_path', None)+ return expected_path is not None and child_path is not None and str(child_path) == str(expected_path)def _compression_lineage(db, session_id):diff --git a/tests/gateway/test_local_viewer_events_turn.py b/tests/gateway/test_local_viewer_events_turn.pyindex c27c309a893..9e53a4ff2bc 100644--- a/tests/gateway/test_local_viewer_events_turn.py+++ b/tests/gateway/test_local_viewer_events_turn.py@@ -632,3 +632,37 @@ def test_subagent_roster_retains_running_child_after_fallback_evicts_parent():finally:for sid in ('fallback-child', 'wrong-db-child', 'wrong-route-child', 'stale-child'):_unregister_subagent(sid)+++def test_subagent_roster_survives_a_parent_rebuild():+ """A cached-agent rebuild (/reasoning, /model, a policy refresh, idle eviction) gives the session a+ NEW parent object while a background child keeps running under the old one. subagent.list follows+ the conversation (durable owner id in this session's lineage, same session DB file), so the dock+ keeps the child; another conversation or another profile's DB never sees it."""+ import weakref+ from gateway.session_ancillary import subagent_records+ from tools.delegate_tool_registry import _register_subagent, _unregister_subagent++ class Parent:+ pass++ def db(path):+ return SimpleNamespace(db_path=path)++ old = Parent()+ old.session_id, old._session_db = 'conv-1', db('/p/a/state.db')+ rebuilt = Parent()+ rebuilt.session_id, rebuilt._session_db = 'conv-1', db('/p/a/state.db')+ child = SimpleNamespace(_delegate_parent_ref=weakref.ref(old), _session_db=db('/p/a/state.db'))+ other_profile = SimpleNamespace(_delegate_parent_ref=weakref.ref(old), _session_db=db('/p/b/state.db'))+ other_conv = SimpleNamespace(_delegate_parent_ref=weakref.ref(old), _session_db=db('/p/a/state.db'))+ _register_subagent({'subagent_id': 'live-child', 'agent': child, 'owner_agent_session_id': 'conv-1'})+ _register_subagent({'subagent_id': 'other-profile', 'agent': other_profile, 'owner_agent_session_id': 'conv-1'})+ _register_subagent({'subagent_id': 'other-conv', 'agent': other_conv, 'owner_agent_session_id': 'conv-2'})+ try:+ assert [r['subagent_id'] for r in subagent_records(rebuilt)] == ['live-child']+ # Read-only projection only: the control/tail path (no lineage) still sees nothing for the new parent.+ assert subagent_records(rebuilt, include_fallback_evicted=False) == []+ finally:+ for sid in ('live-child', 'other-profile', 'other-conv'):+ _unregister_subagent(sid)