From 8aebb1c00e769045a770900e99c44b269bd2b402 Mon Sep 17 00:00:00 2001 From: kshitijk4poor <82637225+kshitijk4poor@users.noreply.github.com> Date: Thu, 24 Sep 2026 17:09:47 +0530 Subject: [PATCH] fix(api-server): mark streamed completion run terminal when the turn ends (#51871) --- gateway/platforms/api_server_openai_routes.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/gateway/platforms/api_server_openai_routes.py b/gateway/platforms/api_server_openai_routes.py index 4ed8177b06..a721312494 100644 --- a/gateway/platforms/api_server_openai_routes.py +++ b/gateway/platforms/api_server_openai_routes.py @@ -731,8 +731,13 @@ class OpenAICompatRoutesMixin: approval_session_key=completion_id, **run_kwargs) # The completion id doubles as the run id: drop the approval mapping once the turn # ends so POST /v1/runs/{id}/approval answers 409 rather than resolving stale keys. - agent_task.add_done_callback( - lambda _fut: self._run_approval_sessions.pop(completion_id, None)) + def _end_stream_run(fut): + self._run_approval_sessions.pop(completion_id, None) + if completion_id in self._run_statuses: # terminal state for pollers of this id + failed = not fut.cancelled() and fut.exception() is not None + self._set_run_status(completion_id, "cancelled" if fut.cancelled() + else "failed" if failed else "completed") + agent_task.add_done_callback(_end_stream_run) # #13437 identity contract: an explicit-header client keeps addressing the id it # sent; the response echoes that stable id while reads/writes adopt the live tip, # so a rotation mid-turn (after these headers are prepared) never changes what the