Skip to content

The agent that grows with you

fix(api-server): make streamed chat-completion approvals resolvable via /v1/runs

kshitijk4poor3a271156f4aa
diff --git a/gateway/platforms/api_server.py b/gateway/platforms/api_server.py
index 0fa4c10f059..b7b26d27303 100644
--- a/gateway/platforms/api_server.py
+++ b/gateway/platforms/api_server.py
@@ -109,6 +109,22 @@ def _approval_event_choices(*, smart_denied: bool, allow_session: bool, allow_pe
return ["once", "session", "always", "deny"] if allow_permanent else ["once", "session", "deny"]
+def _approval_request_event(run_id: str, approval_data: Optional[Dict[str, Any]], **fields: Any) -> Dict[str, Any]:
+ """The ``approval.request`` payload every approval surface emits (runs bridge, session stream,
+ chat completions): the flagged command redacted before egress (#48456), the ``_run_event``
+ envelope, and the ``choices`` the client may send back to ``POST /v1/runs/{id}/approval``."""
+ from gateway.platforms.api_server_runs import _run_event
+ event = dict(approval_data or {})
+ if "command" in event:
+ from gateway.run import _redact_approval_command
+ event["command"] = _redact_approval_command(event.get("command"))
+ event.update(_run_event(run_id, "approval.request", **fields, choices=_approval_event_choices(
+ smart_denied=bool(event.get("smart_denied")),
+ allow_session=event.get("allow_session") is not False,
+ allow_permanent=event.get("allow_permanent") is not False)))
+ return event
+
+
try:
from aiohttp import web
AIOHTTP_AVAILABLE = True
@@ -3547,16 +3563,8 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):
self._run_approval_sessions[run_id] = run_id
def _approval_notify(approval_data: Dict[str, Any]) -> None:
- event = dict(approval_data or {})
- if "command" in event:
- from gateway.run import _redact_approval_command
- event["command"] = _redact_approval_command(event.get("command"))
- event.update({
- "message_id": message_id,
- "choices": _approval_event_choices(
- smart_denied=bool(event.get("smart_denied")),
- allow_session=event.get("allow_session") is not False,
- allow_permanent=event.get("allow_permanent") is not False)})
+ event = _approval_request_event(run_id, approval_data, message_id=message_id)
+ self._set_run_status(run_id, "waiting_for_approval", last_event="approval.request", approval=event)
events.enqueue("approval.request", event) # executor thread -> loop hop inside
return _approval_notify
@@ -4100,10 +4108,8 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):
result = agent.run_conversation(**conversation_kwargs)
finally:
if approval_token is not None:
- from tools.approval import unregister_gateway_notify
from tools.approval_context import reset_current_session_key
- with suppress(Exception):
- unregister_gateway_notify(approval_session_key)
+ _api_runs._unregister_approval_notify(approval_session_key)
with suppress(Exception):
reset_current_session_key(approval_token)
result, usage = self._finish_turn_result(
diff --git a/gateway/platforms/api_server_openai_routes.py b/gateway/platforms/api_server_openai_routes.py
index a7213124947..15d0907f0d7 100644
--- a/gateway/platforms/api_server_openai_routes.py
+++ b/gateway/platforms/api_server_openai_routes.py
@@ -538,32 +538,41 @@ class OpenAICompatRoutesMixin:
requested_provider=overrides.get("requested_provider"), route=route)
return route, overrides, (_error_response(err, 400) if err else None)
- def _register_stream_approval(self, completion_id, stream_q, session_id):
- """Expose a streaming completion to ``POST /v1/runs/{completion_id}/approval`` and return
- the notify callback that pushes ``approval.request`` onto ``stream_q`` (#51871). Keyed by
- the completion id (never the shared session key) so concurrent turns can't cross-resolve."""
- from gateway.platforms.api_server import _approval_event_choices
+ def _register_stream_approval(self, request, completion_id, stream_q, session_id) -> tuple:
+ """Expose a streaming completion as a run for ``POST /v1/runs/{completion_id}/approval``
+ (#51871) -> ``(notify, on_done)``. Owner + status are stamped like the session stream, so
+ ``_load_owned_run`` finds it; keyed by the completion id (never the shared session key) so
+ concurrent turns can't cross-resolve. ``on_done`` retires it when the agent task ends."""
+ from gateway.platforms.api_server import _approval_request_event
+ self._run_owners[completion_id] = self._run_idempotency_scope(request)
+ self._set_run_status(completion_id, "running", session_id=session_id or "")
self._run_approval_sessions[completion_id] = completion_id
def _approval_notify(approval_data):
- event = dict(approval_data or {})
- if "command" in event:
- from gateway.run import _redact_approval_command
- event["command"] = _redact_approval_command(event.get("command"))
- event.update({
- "event": "approval.request", "run_id": completion_id, "session_id": session_id or "",
- "timestamp": time.time(),
- "choices": _approval_event_choices(
- smart_denied=bool(event.get("smart_denied")),
- allow_session=event.get("allow_session") is not False,
- allow_permanent=event.get("allow_permanent") is not False)})
+ event = _approval_request_event(completion_id, approval_data, session_id=session_id or "")
+ self._set_run_status(completion_id, "waiting_for_approval", last_event="approval.request",
+ approval=event)
stream_q.put_threadsafe(("__approval__", event))
- return _approval_notify
- def _spawn_stream_agent(self, stream_q, **run_kwargs) -> tuple:
+ def _on_done(fut):
+ from gateway.platforms.api_server_runs import terminal_run_status
+ self._run_approval_sessions.pop(completion_id, None)
+ if fut.cancelled():
+ status = "cancelled"
+ elif fut.exception() is not None:
+ status = "failed"
+ else:
+ result = (fut.result() or (None,))[0]
+ status = terminal_run_status(result)[0] if isinstance(result, dict) else "completed"
+ self._set_run_status(completion_id, status)
+ self._release_run_owner_if_forgotten(completion_id)
+ return _approval_notify, _on_done
+
+ def _spawn_stream_agent(self, stream_q, *, on_done=None, **run_kwargs) -> tuple:
"""Start ``_run_agent`` for an SSE writer -> ``(agent_task, agent_ref)``. ``agent_ref[0]``
lets the writer interrupt on disconnect; the EOS sentinel is enqueued from the task's done
- callback so drain loops never race a polled ``agent_task.done()``."""
+ callback so drain loops never race a polled ``agent_task.done()``. ``on_done(task)`` runs
+ in that same callback, before EOS."""
def _on_delta(delta):
# None from the agent is a CLI box-close signal, not EOS — forwarding it would end
# the stream early. Called from the run_conversation worker thread: put_threadsafe.
@@ -586,7 +595,15 @@ class OpenAICompatRoutesMixin:
agent_task = asyncio.ensure_future(self._run_agent(
stream_delta_callback=_on_delta, reasoning_callback=_on_reasoning, status_callback=_on_status,
agent_ref=agent_ref, **run_kwargs))
- agent_task.add_done_callback(lambda _fut: stream_q.put_nowait(None))
+ def _done(fut):
+ try:
+ if on_done is not None:
+ on_done(fut)
+ except Exception:
+ logger.debug("[api_server] stream on_done hook failed", exc_info=True)
+ finally:
+ stream_q.put_nowait(None)
+ agent_task.add_done_callback(_done)
return agent_task, agent_ref
async def _handle_chat_completions(self, request: "web.Request") -> "web.Response":
@@ -724,20 +741,14 @@ class OpenAICompatRoutesMixin:
# tool_progress_callback deliberately NOT wired: it would duplicate the structured
# start/complete callbacks (which carry the tool_call id).
- approval_notify = self._register_stream_approval(completion_id, _stream_q, session_id)
+ # The completion id doubles as the run id; on_done drops the approval mapping once the
+ # turn ends (POST /v1/runs/{id}/approval then answers 409) and sets the terminal status.
+ approval_notify, end_stream_run = self._register_stream_approval(
+ request, completion_id, _stream_q, session_id)
agent_task, agent_ref = self._spawn_stream_agent(
- _stream_q, tool_start_callback=_on_tool_start,
+ _stream_q, on_done=end_stream_run, tool_start_callback=_on_tool_start,
tool_complete_callback=_on_tool_complete, approval_notify_callback=approval_notify,
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.
- 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
diff --git a/gateway/platforms/api_server_runs.py b/gateway/platforms/api_server_runs.py
index fc389b33166..88a66a3c65f 100644
--- a/gateway/platforms/api_server_runs.py
+++ b/gateway/platforms/api_server_runs.py
@@ -799,18 +799,8 @@ def _make_approval_notify(self, run: _RunLaunch, *, _api_server) -> Callable[[Di
run_id, q, loop = run.run_id, run.queue, asyncio.get_running_loop()
def _approval_notify(approval_data: Dict[str, Any]) -> None:
- event = dict(approval_data or {})
- # Clients must never receive the raw flagged command: redact before it hits the stream.
- # Redact credentials from the command before it enters the SSE/API event stream — same egress bug as
- # #48456, second transport: API/desktop clients would otherwise receive the raw command Tirith
- # flagged. Reuse the gateway seam.
- if "command" in event:
- from gateway.run import _redact_approval_command
- event["command"] = _redact_approval_command(event.get("command"))
- event.update(_run_event(run_id, "approval.request", choices=_api_server._approval_event_choices(
- smart_denied=bool(event.get("smart_denied")),
- allow_session=event.get("allow_session") is not False,
- allow_permanent=event.get("allow_permanent") is not False)))
+ # Clients must never receive the raw flagged command (#48456): the shared builder redacts.
+ event = _api_server._approval_request_event(run_id, approval_data)
self._set_run_status(run_id, "waiting_for_approval", last_event="approval.request", approval=event)
with suppress(Exception):
loop.call_soon_threadsafe(q.put_nowait, event)