The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(api): a run admitted but not yet started settles interrupted at shutdown
teknium1581165391441
diff --git a/contributors/emails/[email protected] b/contributors/emails/[email protected]new file mode 100644index 00000000000..1e1f658f578--- /dev/null+++ b/contributors/emails/[email protected]@@ -0,0 +1 @@+rockminsterdiff --git a/gateway/platforms/api_server_runs.py b/gateway/platforms/api_server_runs.pyindex 86a5c89c47b..d5505826712 100644--- a/gateway/platforms/api_server_runs.py+++ b/gateway/platforms/api_server_runs.py@@ -677,6 +677,11 @@ async def _execute_run(self, run: _RunLaunch, *, _api_server) -> None: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+ # interrupt yet, and starting a turn now would outlive the gateway.+ if run_id in self._shutdown_interrupted_run_ids:+ _finish("interrupted")+ returnself._set_run_status(run_id, "running")if run_id in self._stopping_run_ids:_finish("cancelled")diff --git a/website/docs/developer-guide/programmatic-integration.md b/website/docs/developer-guide/programmatic-integration.mdindex 505cf5accc8..08def49235e 100644--- a/website/docs/developer-guide/programmatic-integration.md+++ b/website/docs/developer-guide/programmatic-integration.md@@ -186,6 +186,7 @@ The terminal status of a run is derived from how the agent's turn actually ended| Final answer produced | `completed` | `run.completed` | `completed: true` || Interrupted (`/stop`, or an interrupt inside the agent) | `cancelled` | `run.cancelled` | `completed: false`, `interrupted: true`, `turn_exit_reason` naming the issuer — `interrupted_by_user` for a human stop, `interrupted_by_system(<issuer>)` / `interrupted_during_api_call(<issuer>)` when a watchdog (e.g. `cron_inactivity_watchdog`, `turn_liveness_watchdog`, `gateway_inactivity_watchdog`, `session_turn_lease_lost`) ended the turn || Provider/agent failure | `failed` | `run.failed` | `completed: false`, `error` |+| Gateway shut down while the run was active (`/v1/runs` only) | `interrupted` | `run.interrupted` | `error: "Gateway shutdown interrupted the run."` — recorded before the agent is interrupted and never overwritten by the turn's late result; a client `/stop` still settles as `cancelled` || Ended without finishing (iteration budget, truncated or partial reply) | `failed` | `run.failed` | `completed: false`, `partial` when applicable, `turn_exit_reason` (e.g. `max_iterations_reached(60/60)`), `output` with any fallback text |A run is never reported as `completed` with `completed: false` or `partial: true` in the same payload. The same rule applies to `/api/sessions/{id}/chat/stream`, whose `assistant.completed` payload carries the real `completed` / `partial` / `interrupted` flags and whose terminal event is `run.completed`, `run.failed`, or `run.cancelled`.diff --git a/website/docs/user-guide/features/api-server.md b/website/docs/user-guide/features/api-server.mdindex ca5ab601cc6..74a55ee20da 100644--- a/website/docs/user-guide/features/api-server.md+++ b/website/docs/user-guide/features/api-server.md@@ -469,7 +469,7 @@ Poll the current run state. This is useful for dashboards that need status witho}```-Statuses are retained briefly after terminal states (`completed`, `failed`, or `cancelled`) for polling and UI reconciliation.+Statuses are retained briefly after terminal states (`completed`, `failed`, `cancelled`, or `interrupted`) for polling and UI reconciliation. When the gateway shuts down while a run is active, the run is persisted as `interrupted` (error `Gateway shutdown interrupted the run.`, terminal event `run.interrupted`) before the agent is asked to stop, so a durable run never survives a restart as `running`; a late result from the interrupted turn cannot overwrite it.### GET /v1/runs/\{run_id\}/events