Skip to content

fix(gateway): OpenAI-compatible endpoints blame the provider when the Gateway cancels a run - #167995

Merged
steipete merged 3 commits into
mainfrom
steipete/openai-compat-cancelled-run
Oct 9, 2026
Merged

steipete merged 3 commits into
mainfrom
steipete/openai-compat-cancelled-run

Conversation

@steipete

@steipete steipete commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes: /v1/chat/completions and /v1/responses answer 504 "upstream provider timeout" when the Gateway cancels its own agent run, even though no provider was ever slow or even contacted.

User Impact

User impact: clients and operators get 500 api_error "agent run was cancelled" for a Gateway-side cancellation instead of being sent to debug a provider that never timed out. Provider timeouts and timeout-caused aborts keep their existing 504 "upstream provider timeout"; /v1/embeddings is unchanged.

Why This Change Was Made

Both endpoints map agent errors through the failover classifier, which deliberately treats the transport message This operation was aborted as a provider timeout. At the agent-run boundary that guess is wrong: provider failures arrive here already classified as FailoverErrors, and run budgets abort with a TimeoutError. A bare AbortError without a timeout cause is therefore the Gateway cancelling its own work. The agent-run error mapper (shared by the JSON and streaming paths of both endpoints) now reports that as a cancellation before falling through to the classifier. The shared classifier and the lower-level mapper used by embeddings are untouched, so provider failover behavior does not change. The Gateway log keeps the original error (chat completion failed: AbortError: ...). The Chat Completions and OpenResponses API docs now state this outcome next to the existing client-disconnect cancellation note.

This surfaced while investigating the startup-scope regression from #167514 (fixed on main by #167604 and #167697): during that window every OpenAI-compatible request failed with this misleading 504, which pointed at the provider instead of the Gateway.

Evidence

  • Real Gateway, same trigger before/after. Isolated QA Gateway child (createQaGatewayChild, mock OpenAI provider) built from ae64a98a4eb1, where the startup-scope bug aborts every HTTP agent run:
    • without this change: /v1/chat/completions → 504 {"error":{"message":"upstream provider timeout","type":"api_error"}}; /v1/responses → 504, status failed
    • with this change applied: /v1/chat/completions → 500 {"error":{"message":"agent run was cancelled","type":"api_error"}}; /v1/responses → 500, status failed
    • Gateway log still records [openai-compat] chat completion failed: AbortError: This operation was aborted in both runs.
  • Regression at the mapper owner (src/gateway/openai-compatible-agent-run.test.ts, "OpenAI-compatible agent error envelopes"): a Gateway-cancelled run fails on current main (expected { status: 504 } to deeply equal { status: 500 }) and passes with the fix; the two timeout guards (a FailoverError timeout carrying the abort message, and an abort caused by a TimeoutError) keep 504 on both.
  • pnpm test src/gateway/openai-compatible-agent-run.test.ts --maxWorkers=1: 23 passed. The three new cases take 13–30 ms each (well under 0.1 CI seconds) and add no fixtures, servers, or timers; file wall time was 2m53s on a host at load ~60, dominated by cold transform.
  • Endpoint suites with the fix (openai-http.test.ts, openresponses-http.test.ts, openai-compatible-agent-run.test.ts, embeddings-http.test.ts): 249 passed, including the same three cases driven through the real /v1/chat/completions handler before they moved to the mapper owner (the endpoint test file is over the line cap and may not grow).
  • node scripts/check-changed.mjs on the changed files: passed (core and core-test typecheck, lint, dead-export scan, import cycles, line-cap ratchet); git diff --check clean.
  • $autoreview (Codex, P2): scoped-clean on the code and on the docs commit.

Not covered: on current main there is no ordinary trigger left that produces a bare abort at these endpoints (operator chat.abort does not currently stop OpenAI-compatible HTTP runs, which is a separate follow-up), so the live before/after uses the historical startup-scope trigger.

…re cancelled

The OpenAI-compatible chat completions and responses endpoints mapped every
agent error through the failover classifier. That classifier treats the
transport message "This operation was aborted" as a provider timeout, so a run
cancelled by the Gateway itself was reported to clients as
504 "upstream provider timeout" (responses: 504, status "failed").

Provider failures reach this boundary as FailoverErrors and run budgets abort
with a TimeoutError, so a bare AbortError there is Gateway-owned cancellation.
Report it as 500 api_error "agent run was cancelled"; provider timeouts and
timeout-caused aborts keep their 504.
@clawsweeper

clawsweeper Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: XS labels Oct 9, 2026
@steipete
steipete marked this pull request as ready for review October 9, 2026 22:26
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Oct 9, 2026
@clawsweeper

clawsweeper Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

The Gateway’s OpenAI-compatible APIs report cancelled agent runs as cancellations while preserving provider-timeout errors.

Example: The Gateway cancels a Chat Completions run while its HTTP client remains connected.

  • Before: The client receives HTTP 504 with "upstream provider timeout".
  • After: The client receives HTTP 500 with "agent run was cancelled".

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused, correct repair with real HTTP before/after evidence and meaningful timeout controls; the historical trigger limits the proof’s breadth.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (live_output): The recorded isolated Gateway HTTP comparison exercises the production agent-error mapper through both endpoints and observes 504 becoming the intended cancellation 500. The provider is mocked and the cancellation trigger is historical; those limits are disclosed, and the latest commit changes documentation only.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Clients and operators are told to investigate a provider timeout when the Gateway cancelled its own run.
Reason: Cancellation and provider timeout have different causes and need distinct diagnostics. The focused repair adds no configuration or execution-policy change.

Merge readiness

✅ Ready for maintainer review

This PR remains useful: current main still misattributes Gateway cancellation, and the merged startup repairs address the trigger rather than the error mapping. No actionable patch defects were found.

Priority: P2
Reviewed head: 24b158623c6b83c4faa300796534bc9696c684ee

Before merge

None.

Findings

None.

Agent review details

How this fits together

The Gateway accepts Chat Completions and Responses requests and executes OpenClaw agent runs. A shared error mapper translates execution failures into JSON responses or streaming error events.

flowchart TD
  A[Chat Completions or Responses request] --> B[Gateway agent run]
  B --> C[Shared error mapper]
  C --> D{Gateway cancellation?}
  D -->|Yes| E[Cancellation error]
  D -->|No| F[Provider error classifier]
  E --> G[JSON response or streaming event]
  F --> G
Loading

Technical review

Best possible solution:

Keep cancellation attribution in the shared agent-error mapper while retaining provider-owned timeout classification.

Do we have a high-confidence way to reproduce the issue?

Yes at the mapper boundary: a bare AbortSignal.abort().reason on current main produces the wrong timeout envelope. The recorded real-Gateway comparison uses a historical startup fault; no ordinary current-main runtime trigger is established.

Is this the best way to solve the issue?

Yes. The repair distinguishes cancellation at the common agent boundary, reuses existing error identities, and preserves provider classification and endpoint response handling.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 286fff87a2ab.

Provenance checked

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

  • Pinned introduced change: The introduced patch adds cancellation recognition at the existing shared agent-error mapper; it leaves the provider classifier, embeddings mapper, execution authority, and stored state unchanged. (src/gateway/openai-compatible-agent-run.ts:37, 24b158623c6b)
  • Current main still needs the repair: Fetched main still passes agent errors directly to the provider-oriented mapper without distinguishing bare cancellation. The mapper and both changed documentation pages have no difference between the pinned merge base and fetched main. (src/gateway/openai-compatible-agent-run.ts:44, 286fff87a2ab)
  • Recorded real HTTP comparison: The complete PR body records an isolated QA Gateway child with a mock OpenAI provider and the historical startup-scope trigger at ae64a98. Applying the repair changes Chat Completions from 504 "upstream provider timeout" to 500 "agent run was cancelled", and Responses from 504 to 500 with failed status, while preserving the original AbortError log. The body explicitly discloses that this historical trigger has been repaired on main. Captured sourceRevision: 3f36a135eb5b31f95000e7d039ea0b3ea6054123556d724cf3f6feebcb4a9373. (24b158623c6b)
  • Timeout controls and shared callers: The three added cases distinguish bare cancellation, a typed provider timeout, and an abort caused by TimeoutError. Both JSON and streaming branches of both endpoints consume this mapper; embeddings consumes the unchanged lower-level mapper. The contributor reports the cancellation regression failing on main and passing with the repair, while both timeout controls retain 504. Tests were inspected, not executed during this read-only review. (src/gateway/openai-compatible-agent-run.test.ts:420, 24b158623c6b)
  • Shared mapper history: The history search and parent-relative patch identify the shared error mapper in commit 0522d79. refactor(gateway): deslop gateway request owners #162413 explains its intent: centralize classification while preserving endpoint envelopes and Chat Completions fallback precedence. (src/gateway/openai-compatible-agent-run.ts:58, 0522d799b827)
  • Related repairs are distinct: fix(gateway): prevent public chat 503s after startup #167604 and fix(gateway): prevent workspace icon failures after startup #167697 repair resident resources inheriting a retired startup lifetime; neither supplies this cancellation envelope. fix(agents): propagate client tool cancellation #117352 concerns cancellation before tool side effects and does not replace the HTTP error-mapping repair.

Likely related people:

  • Peter Steinberger: Raw commit 0522d79 adds src/gateway/openai-compatible-agent-run.ts:44 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 0522d799b827; files: src/gateway/openai-compatible-agent-run.ts)
  • Dallin Romney: Raw commit e9e3ac9 adds src/gateway/openai-compatible-agent-run.ts:32 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: e9e3ac919106; files: src/gateway/openai-compatible-agent-run.ts)

PR surface

Source +14, Tests +34, Docs 0. Total +48 across 4 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 14 0 +14
Tests 1 34 0 +34
Docs 2 2 2 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 4 50 2 +48

Review metrics

Metric Value Why it matters
Production versus test growth production +14, tests +34 The small production addition is justified by a distinct cancellation envelope at the existing shared owner.

Labels

Label changes:

No label changes.

Label justifications:

  • P2: Misleading cancellation diagnostics are a bounded API correctness problem; the historical startup trigger has already been repaired.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only; repair and merge need explicit maintainer commands such as @clawsweeper autofix or @clawsweeper automerge.

History

Review history (1 earlier review cycle)
  • reviewed 2026-10-09T22:33:08.047Z sha fe49907 :: needs maintainer review before merge. :: none

Reviewed October 9, 2026, 6:46 PM ET / 22:46 UTC (Revision 2).

@openclaw-barnacle openclaw-barnacle Bot added the docs Improvements or additions to documentation label Oct 9, 2026
@steipete
steipete merged commit 97c7a5e into main Oct 9, 2026
124 of 128 checks passed
@steipete
steipete deleted the steipete/openai-compat-cancelled-run branch October 9, 2026 23:30
@steipete

steipete commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 10, 2026
… Gateway cancels a run (openclaw#167995)

* fix(gateway): stop blaming the provider when OpenAI-compatible runs are cancelled

The OpenAI-compatible chat completions and responses endpoints mapped every
agent error through the failover classifier. That classifier treats the
transport message "This operation was aborted" as a provider timeout, so a run
cancelled by the Gateway itself was reported to clients as
504 "upstream provider timeout" (responses: 504, status "failed").

Provider failures reach this boundary as FailoverErrors and run budgets abort
with a TimeoutError, so a bare AbortError there is Gateway-owned cancellation.
Report it as 500 api_error "agent run was cancelled"; provider timeouts and
timeout-caused aborts keep their 504.

* test(gateway): cover cancelled-run envelopes at the agent error mapper

* docs(gateway): document cancelled-run errors for OpenAI-compatible endpoints
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation gateway Gateway runtime P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: XS status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant