Skip to content

The agent that grows with you

fix(gateway): stamp late subagent frames explicitly instead of blanking the shared stamp

yoniebans4826cb2fd7d3
diff --git a/gateway/local_viewer_events.py b/gateway/local_viewer_events.py
index 2fa93bddaa7..a4d509ed805 100644
--- a/gateway/local_viewer_events.py
+++ b/gateway/local_viewer_events.py
@@ -96,17 +96,9 @@ 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, event_type):
- return
+ if stream is not None and _is_active_session_subagent(turn, payload, event_type):
# 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)
- if isinstance(saved, dict):
- stream.execution = {}
- try:
- stream.publish(session_id, payload, event_type=event_type)
- finally:
- if isinstance(saved, dict):
- stream.execution = saved
+ stream.publish(session_id, payload, event_type=event_type, execution={})
def publish_progress(turn, event_type, name, preview, kwargs) -> None:
diff --git a/gateway/session_events.py b/gateway/session_events.py
index 77475d89233..f669acfb44e 100644
--- a/gateway/session_events.py
+++ b/gateway/session_events.py
@@ -35,11 +35,12 @@ class SessionEvents:
self.sequence = 0
return self.epoch, self.sequence
- def publish(self, session_id, payload, *, event_type="message.complete"):
+ def publish(self, session_id, payload, *, event_type="message.complete", execution=None):
+ # ``execution`` stamps this frame alone; the shared stamp is read without the lock elsewhere.
with self.lock:
self.watermark()
frame = {'jsonrpc': '2.0', 'method': 'event', 'params': {
- **self.execution, 'type': event_type, 'session_id': self._key,
+ **(self.execution if execution is None else execution), 'type': event_type, 'session_id': self._key,
'payload': deepcopy(payload), 'replay_epoch': self.epoch}}
event_replay._stamp_event(frame)
if frame['params']['seq'] <= self.sequence:
diff --git a/tests/gateway/test_local_viewer_events_turn.py b/tests/gateway/test_local_viewer_events_turn.py
index 5cbdff39238..0543b0015b8 100644
--- a/tests/gateway/test_local_viewer_events_turn.py
+++ b/tests/gateway/test_local_viewer_events_turn.py
@@ -428,9 +428,17 @@ def test_progress_publishes_subagent_lifecycle_events_for_local_viewers():
assert published[6][1]['status'] == 'completed' and published[6][1]['summary'] == 'all clear'
# Registered background child descending from the turn's agent publishes without borrowing the next turn's execution identity; unregistered or foreign-parent children (even with matching owner_agent_session_id) are rejected.
+ class _RecordingEvents(SessionEvents):
+ # Records every write of the shared stamp after construction: unlocked readers must never see it change.
+ def __setattr__(self, name, value):
+ if name == 'execution' and hasattr(self, 'execution'):
+ self.__dict__.setdefault('stamp_writes', []).append(value)
+ super().__setattr__(name, value)
+
stream_frames = []
- live.event_stream = SessionEvents()
+ live.event_stream = _RecordingEvents()
live.event_stream.execution = {'admission_id': 'turn-b', 'execution_generation': 2}
+ live.event_stream.stamp_writes = []
live.event_stream.observers.add(stream_frames.append)
turn._approval_owner[0].executing = {'s': 2}
stale_relay = _build_child_progress_callback(0, 'stale goal', parent, subagent_id='sa-stale')
@@ -453,19 +461,24 @@ def test_progress_publishes_subagent_lifecycle_events_for_local_viewers():
params = stream_frames[0]['params']
assert params['type'] == 'subagent.complete' and 'admission_id' not in params and 'execution_generation' not in params
assert live.event_stream.execution == {'admission_id': 'turn-b', 'execution_generation': 2}
+ assert live.event_stream.stamp_writes == []
- # In-turn event with no live viewer: recorded once with the turn's identity, never retried as an out-of-turn frame.
+ # In-turn event with no live viewer: the real publish_execution records it in replay and reports no delivery; it is stored once, with the turn's identity.
+ from gateway.session_authority import SessionAuthority
authority = turn._approval_owner[0]
authority.executing = {'s': 1}
- publish_calls = []
- turn._publish_execution = lambda kind, payload: publish_calls.append(kind) or False
+ live.event_stream = SessionEvents()
+ live.event_stream.execution = {'admission_id': 'turn-a', 'execution_generation': 1}
+ turn._publish_execution = lambda kind, payload: SessionAuthority.publish_execution(authority, 's', 1, kind, payload)
_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 == []
+ replay = live.event_stream.since(live.event_stream.epoch, 0)['events']
+ assert [(f['type'], f.get('execution_generation')) for f in replay] == [('subagent.start', 1)]
+ stream_frames = []
+ live.event_stream.observers.add(stream_frames.append)
# Out of turn, a queued grandchild's spawn_requested is admitted only through a registered descendant parent.
authority.executing = {'s': 2}