Skip to content

The agent that grows with you

fix(api): flush the SSE head before waiting for the first run event

0xGr1mm6c9f61efed34
diff --git a/gateway/platforms/api_server_runs.py b/gateway/platforms/api_server_runs.py
index 49500ac37ec..14de66590e8 100644
--- a/gateway/platforms/api_server_runs.py
+++ b/gateway/platforms/api_server_runs.py
@@ -1111,6 +1111,11 @@ async def _handle_run_events(self, request: "web.Request", *, _api_server) -> "w
try:
await response.prepare(request)
prepared = True
+ # Flush the response head before waiting on the queue: aiohttp holds the headers
+ # until the first body write, so a subscriber that connects before the run's first
+ # event (e.g. before `approval.request`) sees no bytes and fetch()/EventSource never
+ # resolve. A comment frame is ignored by every conforming SSE consumer.
+ await _write(b": open\n\n")
if replay and replay[0][0] > last_seq + 1:
truncation = _run_event(
run_id,
diff --git a/tests/gateway/test_api_server_runs.py b/tests/gateway/test_api_server_runs.py
index c8dbc25b179..f81bd265b96 100644
--- a/tests/gateway/test_api_server_runs.py
+++ b/tests/gateway/test_api_server_runs.py
@@ -2605,3 +2605,74 @@ class TestHostedRoomRuns:
)
assert rejected.status == 403
create.assert_not_called()
+class _OwnedRequest:
+ """Stand-in request for computing the idempotency scope the real one will get.
+
+ ``_run_idempotency_scope`` reads only headers, so an empty mapping yields the
+ same unauthenticated scope the TestClient request below produces.
+ """
+
+ headers: dict = {}
+
+
+class TestRunEventsHeadFlush:
+ """The SSE head must reach the client before the first event (#80757).
+
+ ``_handle_run_events`` used to call ``prepare()`` and go straight into the
+ queue wait. aiohttp keeps the headers in the socket buffer until the first
+ body write, so a subscriber that connects before the run emits anything got
+ no bytes at all — ``fetch()``/``EventSource`` never resolve and the client
+ looks hung. The approval flow is the worst case: ``approval.request`` only
+ fires after the model thinks, so the client waits that whole time (or the
+ 30s keepalive) for headers that were ready immediately.
+ """
+
+ @pytest.mark.asyncio
+ async def test_head_arrives_before_any_event(self, adapter):
+ """A subscriber on a silent run reads its first byte immediately."""
+ app = _create_runs_app(adapter)
+ run_id = "run_silent_head"
+ # A registered run whose queue stays empty for the whole test — the
+ # exact shape of "subscribed before the first event was emitted".
+ adapter._run_streams[run_id] = asyncio.Queue()
+ # Ownership is a separate gate with its own tests; this one is about
+ # when the head reaches the wire.
+ adapter._run_owners[run_id] = adapter._run_idempotency_scope(_OwnedRequest())
+
+ async with TestClient(TestServer(app)) as cli:
+ resp = await asyncio.wait_for(
+ cli.get(f"/v1/runs/{run_id}/events"), timeout=5.0
+ )
+ assert resp.status == 200
+ assert resp.headers["Content-Type"].startswith("text/event-stream")
+
+ # Without the preamble this blocks until the 30s keepalive.
+ first = await asyncio.wait_for(resp.content.read(1), timeout=3.0)
+ assert first, "no body byte arrived before the first event"
+
+ resp.close()
+
+ @pytest.mark.asyncio
+ async def test_preamble_is_an_ignorable_sse_comment(self, adapter):
+ """The flush must not look like an event to a conforming consumer."""
+ app = _create_runs_app(adapter)
+ run_id = "run_comment_preamble"
+ q: asyncio.Queue = asyncio.Queue()
+ adapter._run_streams[run_id] = q
+ adapter._run_owners[run_id] = adapter._run_idempotency_scope(_OwnedRequest())
+
+ async with TestClient(TestServer(app)) as cli:
+ resp = await asyncio.wait_for(
+ cli.get(f"/v1/runs/{run_id}/events"), timeout=5.0
+ )
+ assert resp.status == 200
+
+ # Close the stream so the body is finite, then read it whole.
+ await q.put(None)
+ body = await asyncio.wait_for(resp.text(), timeout=5.0)
+
+ # Every line before the close sentinel is an SSE comment: no `event:`
+ # or `data:` field, so EventSource dispatches nothing extra.
+ assert body.startswith(":")
+ assert "event:" not in body
+ assert "data:" not in body