Skip to content

The agent that grows with you

fix(gateway): show a queued grandchild spawned after its turn ended

yoniebans254520a96515
diff --git a/gateway/local_viewer_events.py b/gateway/local_viewer_events.py
index 5b1fc0cc3df..2fa93bddaa7 100644
--- a/gateway/local_viewer_events.py
+++ b/gateway/local_viewer_events.py
@@ -62,7 +62,7 @@ def show_reasoning(turn) -> bool:
return getattr(live, "show_reasoning", None) is True
-def _is_active_session_subagent(turn, payload: dict) -> bool:
+def _is_active_session_subagent(turn, payload: dict, event_type: str = "") -> bool:
subagent_id = payload.get("subagent_id")
if not isinstance(subagent_id, str) or not subagent_id:
return False
@@ -72,8 +72,12 @@ def _is_active_session_subagent(turn, payload: dict) -> bool:
turn_agent = holder[0] if isinstance(holder, list) and holder else None
if registry is None or turn_agent is None:
return False
+ parent_id = payload.get("parent_id")
with registry._active_subagents_lock:
record = registry._active_subagents.get(subagent_id)
+ if record is None and event_type == "subagent.spawn_requested" and isinstance(parent_id, str):
+ # A queued grandchild is announced before it registers; its spawning parent must be a registered descendant.
+ record = registry._active_subagents.get(parent_id)
return record is not None and registry._is_descendant_of(record.get("agent"), turn_agent)
@@ -92,7 +96,7 @@ def _publish_subagent(turn, event_type, payload) -> None:
if executing.get(session_id) == generation:
turn._publish_execution(event_type, payload)
return
- if stream is None or not _is_active_session_subagent(turn, payload):
+ if stream is None or not _is_active_session_subagent(turn, payload, event_type):
return
# A background delegate_task outlives the spawning turn's running claim; publish without borrowing a later turn's execution identity.
saved = getattr(stream, "execution", None)
diff --git a/tests/gateway/test_local_viewer_events_turn.py b/tests/gateway/test_local_viewer_events_turn.py
index 7b8e27ba70b..c710b969082 100644
--- a/tests/gateway/test_local_viewer_events_turn.py
+++ b/tests/gateway/test_local_viewer_events_turn.py
@@ -467,6 +467,26 @@ def test_progress_publishes_subagent_lifecycle_events_for_local_viewers():
_unregister_subagent('sa-0-abc12345')
assert publish_calls == ['subagent.start'] and stream_frames == []
+ # Out of turn, a queued grandchild's spawn_requested is admitted only through a registered descendant parent.
+ authority.executing = {'s': 2}
+ grandchild_relay = _build_child_progress_callback(
+ 0, 'grandchild goal', parent, subagent_id='sa-1-grand', parent_id='sa-0-abc12345', depth=2)
+ orphan_relay = _build_child_progress_callback(
+ 0, 'orphan goal', parent, subagent_id='sa-1-orphan', parent_id='sa-foreign-same-sid', depth=2)
+ _register_subagent({'subagent_id': 'sa-0-abc12345', 'agent': owned_child, 'owner_agent_session_id': 's'})
+ _register_subagent({'subagent_id': 'sa-foreign-same-sid', 'agent': foreign_child, 'owner_agent_session_id': 's'})
+ try:
+ stream_frames.clear()
+ orphan_relay('subagent.spawn_requested', preview='orphan goal')
+ grandchild_relay('subagent.start')
+ assert stream_frames == []
+ grandchild_relay('subagent.spawn_requested', preview='grandchild goal')
+ finally:
+ _unregister_subagent('sa-0-abc12345')
+ _unregister_subagent('sa-foreign-same-sid')
+ assert [f['params']['type'] for f in stream_frames] == ['subagent.spawn_requested']
+ assert stream_frames[0]['params']['payload']['subagent_id'] == 'sa-1-grand'
+
# Messaging routes never publish subagent.* viewer frames.
msg_published = []
msg_turn = _turn(Platform.TELEGRAM, msg_published)