Skip to content

The agent that grows with you

fix(gateway): stop delegate walks at user-boundary ends

unsupportedpastels637b4e9496a3
diff --git a/gateway/session_automation.py b/gateway/session_automation.py
index 7e338a4912b..625c5b4c3b0 100644
--- a/gateway/session_automation.py
+++ b/gateway/session_automation.py
@@ -261,9 +261,13 @@ def _route_owner(authority, entry):
def _delegating_session(db, session_id):
"""The conversation a (nested) delegate_task child runs under, else ``session_id``. As in the
completion pre-flight (``_lookup_completion_owner``), only ``model_config._delegate_from`` leads
- out of an execution transcript: ``parent_session_id`` also records compression and branches."""
+ out of an execution transcript: ``parent_session_id`` also records compression and branches.
+ Likewise a session closed at a user boundary (/new, a resume away) leads nowhere."""
+ from gateway.run import _USER_BOUNDARY_END_REASONS
lineage = [session_id]
- while (parent := db.get_session_model_config_value(lineage[-1], '_delegate_from')) not in (None, *lineage):
+ while (not ((row := db.get_session(lineage[-1]) or {}).get('ended_at')
+ and row.get('end_reason') in _USER_BOUNDARY_END_REASONS)
+ and (parent := db.get_session_model_config_value(lineage[-1], '_delegate_from')) not in (None, *lineage)):
lineage.append(parent)
return lineage[-1]
diff --git a/tests/gateway/test_automation_wake_route.py b/tests/gateway/test_automation_wake_route.py
index 246b2b85ef7..49ad38d3c79 100644
--- a/tests/gateway/test_automation_wake_route.py
+++ b/tests/gateway/test_automation_wake_route.py
@@ -90,6 +90,22 @@ async def test_subagent_row_without_delegate_provenance_owns_no_route(tmp_path,
assert len(list_session_admissions(authority.db, session_id=root, pending_only=False)) == 1
+async def test_subagent_notice_is_dropped_after_its_resumed_child_was_switched_away(tmp_path, monkeypatch):
+ runner, _, authority, source = await _discord_owner(tmp_path, monkeypatch)
+ root = (await _admit_human(runner, authority, MessageEvent(text='human', source=source, message_id='human'))).ref.session_id
+ key = runner.session_store._generate_session_key(source)
+ worker = root + '-worker'
+ authority.db.create_session(worker, source='subagent', parent_session_id=root, model_config={'_delegate_from': root})
+ # /resume the child, then /resume its parent: the child ends at a user boundary (session_switch).
+ runner.session_store.switch_session(key, worker)
+ runner.session_store.switch_session(key, root)
+ event = {'type': 'completion', 'session_id': 'proc-worker', 'started_at': 1,
+ 'session_key': key, 'parent_session_id': worker}
+ assert await runner._inject_watch_notification('worker-finished', event) is None
+ assert len(list_session_admissions(authority.db, session_id=root, pending_only=False)) == 1
+
+
def _relay_adapter():
from gateway.relay.adapter import RelayAdapter
from gateway.relay.descriptor import CapabilityDescriptor