The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): publish an in-turn subagent event once even with no live viewer
yoniebans75a2bb6e68b6
diff --git a/gateway/local_viewer_events.py b/gateway/local_viewer_events.pyindex 6765d4ef3c4..5b1fc0cc3df 100644--- a/gateway/local_viewer_events.py+++ b/gateway/local_viewer_events.py@@ -79,31 +79,30 @@ def _is_active_session_subagent(turn, payload: dict) -> bool:def _publish_subagent(turn, event_type, payload) -> None:owner = getattr(turn, "_approval_owner", None)- if owner is not None:- authority, session_id, generation = owner- executing = getattr(authority, "executing", None)- if not (isinstance(executing, dict) and executing.get(session_id) != generation):- if turn._publish_execution(event_type, payload) is not False:- return- if not _is_active_session_subagent(turn, payload):+ executing = getattr(owner[0], "executing", None) if owner is not None else None+ if owner is None or not isinstance(executing, dict):+ turn._publish_execution(event_type, payload)+ return+ authority, session_id, generation = owner+ stream = getattr(authority.sessions.get(session_id), "event_stream", None)+ lock = getattr(stream, "lock", None)+ import contextlib+ # The claim is stamped and cleared under this lock, so the in-turn/out-of-turn choice cannot race the turn's end.+ with lock if lock is not None else contextlib.nullcontext():+ 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):return# A background delegate_task outlives the spawning turn's running claim; publish without borrowing a later turn's execution identity.- live = authority.sessions.get(session_id)- stream = getattr(live, "event_stream", None)- if stream is not None:- import contextlib- lock = getattr(stream, "lock", None)- with lock if lock is not None else contextlib.nullcontext():- saved = getattr(stream, "execution", None)- if isinstance(saved, dict):- stream.execution = {}- try:- stream.publish(session_id, payload, event_type=event_type)- finally:- if isinstance(saved, dict):- stream.execution = saved- return- turn._publish_execution(event_type, payload)+ saved = getattr(stream, "execution", None)+ if isinstance(saved, dict):+ stream.execution = {}+ try:+ stream.publish(session_id, payload, event_type=event_type)+ finally:+ if isinstance(saved, dict):+ stream.execution = saveddef publish_progress(turn, event_type, name, preview, kwargs) -> None:diff --git a/tests/gateway/test_local_viewer_events_turn.py b/tests/gateway/test_local_viewer_events_turn.pyindex 0c7865bd249..7b8e27ba70b 100644--- a/tests/gateway/test_local_viewer_events_turn.py+++ b/tests/gateway/test_local_viewer_events_turn.py@@ -454,6 +454,19 @@ def test_progress_publishes_subagent_lifecycle_events_for_local_viewers():assert params['type'] == 'subagent.complete' and 'admission_id' not in params and 'execution_generation' not in paramsassert live.event_stream.execution == {'admission_id': 'turn-b', 'execution_generation': 2}+ # In-turn event with no live viewer: recorded once with the turn's identity, never retried as an out-of-turn frame.+ authority = turn._approval_owner[0]+ authority.executing = {'s': 1}+ publish_calls = []+ turn._publish_execution = lambda kind, payload: publish_calls.append(kind) or False+ _register_subagent({'subagent_id': 'sa-0-abc12345', 'agent': owned_child, 'owner_agent_session_id': 's'})+ try:+ stream_frames.clear()+ relay('subagent.start')+ finally:+ _unregister_subagent('sa-0-abc12345')+ assert publish_calls == ['subagent.start'] and stream_frames == []+# Messaging routes never publish subagent.* viewer frames.msg_published = []msg_turn = _turn(Platform.TELEGRAM, msg_published)