The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(api): queue terminal events after worker commentary
ethernete34c0316a21b
diff --git a/gateway/platforms/api_server_runs.py b/gateway/platforms/api_server_runs.pyindex f018926c39c..8c4deb65740 100644--- a/gateway/platforms/api_server_runs.py+++ b/gateway/platforms/api_server_runs.py@@ -898,7 +898,9 @@ async def _execute_run(self, run: _RunLaunch, *, _api_server) -> None:extra = {}self._set_run_status(run_id, status, **fields, last_event=f"run.{status}", **extra)with suppress(Exception):- run.put_event(_run_event(run_id, f"run.{status}", **fields, **extra))+ # A worker can finish before its Future is wrapped, so awaiting it+ # need not yield to already-scheduled commentary/tool callbacks.+ loop.call_soon(run.put_event, _run_event(run_id, f"run.{status}", **fields, **extra))try:# Shutdown landed between admission and the task's first tick: nothing to@@ -950,7 +952,7 @@ async def _execute_run(self, run: _RunLaunch, *, _api_server) -> None:# Event; unregistering releases it. Idempotent on normal completion._unregister_approval_notify(run.approval_session_key)with suppress(Exception):- run.put_event(None) # sentinel: close the SSE stream+ loop.call_soon(run.put_event, None) # close after the queued events_retire_live_run(self, run_id)diff --git a/tests/gateway/test_api_server_runs.py b/tests/gateway/test_api_server_runs.pyindex 0a85ddadfd3..e360595df1d 100644--- a/tests/gateway/test_api_server_runs.py+++ b/tests/gateway/test_api_server_runs.py@@ -338,11 +338,22 @@ class TestStartRun:mock_create.assert_not_called()@pytest.mark.asyncio- async def test_events_stream_forwards_interim_commentary(self, adapter):- """Mid-turn assistant commentary (Codex ``phase="commentary"``) reaches /v1/runs clients- as ``message.interim`` {text, already_streamed}; the final answer is unchanged (#67580)."""+ @pytest.mark.parametrize("worker_fails", [False, True], ids=["completed", "failed"])+ async def test_events_stream_forwards_interim_commentary(self, adapter, worker_fails):+ """Commentary reaches /v1/runs clients before the terminal event, even+ when the worker finishes before asyncio wraps its Future (#67580)."""import json+ from concurrent.futures import ThreadPoolExecutor+ class CompletedWorkerExecutor(ThreadPoolExecutor):+ def submit(self, fn, /, *args, **kwargs):+ future = super().submit(fn, *args, **kwargs)+ # A fast worker may finish before asyncio wraps its future.+ # Make that ordering deterministic, with real thread callbacks.+ future.exception(timeout=10)+ return future++ asyncio.get_running_loop().set_default_executor(CompletedWorkerExecutor(max_workers=1))app = _create_runs_app(adapter)def create_agent(**kwargs):@@ -352,6 +363,8 @@ class TestStartRun:def run_conversation(**_kw):interim("Checking the docs first.", already_streamed=False)interim("Applying the fix.", already_streamed=True)+ if worker_fails:+ raise RuntimeError("worker failed")return {"final_response": "Done."}agent.run_conversation.side_effect = run_conversation@@ -367,8 +380,13 @@ class TestStartRun:events = [json.loads(line[6:]) for line in body.splitlines() if line.startswith("data: ")]interim = [(e["text"], e["already_streamed"]) for e in events if e["event"] == "message.interim"]assert interim == [("Checking the docs first.", False), ("Applying the fix.", True)]- completed = next(e for e in events if e["event"] == "run.completed")- assert completed["output"] == "Done."+ assert [e["event"] for e in events] == [+ "message.interim", "message.interim", "run.failed" if worker_fails else "run.completed",+ ]+ if worker_fails:+ assert events[-1]["error"] == "worker failed"+ else:+ assert events[-1]["output"] == "Done."@pytest.mark.asyncioasync def test_start_passes_request_model_provider_options_to_create_agent(self, adapter):