Skip to content

server: cancel a running or queued request by its id - #3193

Merged
ashhart merged 2 commits into
mainfrom
lead/cancel-request-by-id
Nov 13, 2026
Merged

ashhart merged 2 commits into
mainfrom
lead/cancel-request-by-id

Conversation

@ashhart

@ashhart ashhart commented Nov 13, 2026

Copy link
Copy Markdown
Owner

POST /v1/requests/{id}/cancel (or /requests/{id}/cancel) cancels a running or queued request by the id the server returned: a chat completion's chatcmpl-, a completion's cmpl- or a Responses resp_ id. The engine gets the same cancel a client disconnect sends, so other streams are unchanged. The reply ends well formed with the tokens it had (none if it was still queued): finish_reason: "cancelled", or a Responses status: "cancelled" in its response.incomplete event. A Cortheon turn runs no further rounds. An unknown or finished id answers 404 request_not_found. The id is held for the whole client request, so compaction and Cortheon rounds honour the cancel, and the server's own requests (compaction notes, warm prefills) never take it.

Who may cancel:

  • With API keys: only a full or [admin] key. A scoped key gets 403 insufficient_scope and a missing key 401, before the body is read; the route checks the matched key again.
  • Without API keys: only a caller whose socket is on the loopback address (127.0.0.0/8, ::1, or its IPv4-mapped form). The socket's own address decides; a forwarded address is never read. A request carrying a proxy header (Forwarded, X-Forwarded-For, X-Forwarded-Host, X-Real-IP, Via, CF-Connecting-IP, True-Client-IP) is refused, since a reverse proxy on the same machine makes every caller look local. Everyone else gets 403 loopback_only. docs/api.md documents the route and says to give a proxied server keys.

Tests (host only): a stream cancelled mid-decode through the route ends cancelled with its tokens so far and one engine cancel; a queued request ends at once with no tokens while the running stream's text is unchanged; a Responses turn cancelled by its resp_ id ends cancelled; unknown ids answer 404; keyless refusals from remote IPv4 and IPv6 addresses and from a proxied loopback request; over sockets, scoped keys 403, no key 401, full keys 200. Ran zig build test-server-cpu (1351 pass), test-key-scopes-host, test-server-security, test-security-golden-host and test-connection-admission-host.

Fixes #3172

  • Synthena (Ash's Agent)

POST /v1/requests/{id}/cancel finds a request by the chatcmpl-, cmpl- or resp_ id its client got and sends the
engine the cancel a disconnect sends; the reply ends "cancelled" with the tokens it had, a queued one with none, and
other streams are unchanged. 404 request_not_found for an id no request holds.

The ids are held for the whole client request in openai.runNamed, and the ask rides on chat.Input, so every
generation for that request reads it (compaction's reply, Cortheon's rounds) and server-made ones never do. A
Cortheon turn stops after a cancelled round. A Responses reply ends with status "cancelled" in its
response.incomplete event, without completed_at, and a Responses socket keeps it like an incomplete one.

Who may cancel: with API keys, a full or [admin] key (the key gate refuses scoped keys, and the route checks the
matched key again). Without keys, only a caller whose socket is on loopback: the peer address from accept(), never
a header, including listener.peerText's IPv4-mapped form, which IpAddress.parse refuses. A keyless request that
carries Forwarded, X-Forwarded-For, X-Forwarded-Host, X-Real-IP, Via, CF-Connecting-IP or True-Client-IP is refused,
since a reverse proxy on the same machine makes every caller loopback. Refusals answer 403 loopback_only.

Fixes #3172
@ashhart
ashhart force-pushed the lead/cancel-request-by-id branch from 1607ff4 to 6485ff4 Compare November 13, 2026 17:02
@ashhart
ashhart merged commit 1af23a6 into main Nov 13, 2026
2 of 10 checks passed
@ashhart
ashhart deleted the lead/cancel-request-by-id branch November 13, 2026 17:02
ashhart added a commit that referenced this pull request Nov 13, 2026
…ays at 600 lines

Main's cancel registration (#3193) and this PR's n dispatch together took
openai.zig to 604 lines. The four VTable field notes move onto their lines;
no code changes.
ashhart added a commit that referenced this pull request Nov 14, 2026
…ays at 600 lines

Main's cancel registration (#3193) and this PR's n dispatch together took
openai.zig to 604 lines. The four VTable field notes move onto their lines;
no code changes.
ashhart added a commit that referenced this pull request Nov 15, 2026
…ays at 600 lines

Main's cancel registration (#3193) and this PR's n dispatch together took
openai.zig to 604 lines. The four VTable field notes move onto their lines;
no code changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cancel a running request by its id

1 participant