Repository navigation
fix(gateway): OpenAI-compatible endpoints blame the provider when the Gateway cancels a run - #167995
Conversation
…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.
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs maintainer review before merge. What this changesThe 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.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Complete 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 Before mergeNone. FindingsNone. Agent review detailsHow this fits togetherThe 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
Technical reviewBest 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
TestingProof path: shipped entry point. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource +14, Tests +34, Docs 0. Total +48 across 4 files. Added test files: 0. View PR surface stats
Review metrics
LabelsLabel changes: No label changes. Label justifications:
Rating scale6/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. WorkflowClawSweeper edits this one comment on every review. Comment HistoryReview history (1 earlier review cycle)
Reviewed October 9, 2026, 6:46 PM ET / 22:46 UTC (Revision 2). |
|
Merged via squash.
|
… 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
What Problem This Solves
Fixes:
/v1/chat/completionsand/v1/responsesanswer504 "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 existing504 "upstream provider timeout";/v1/embeddingsis unchanged.Why This Change Was Made
Both endpoints map agent errors through the failover classifier, which deliberately treats the transport message
This operation was abortedas a provider timeout. At the agent-run boundary that guess is wrong: provider failures arrive here already classified asFailoverErrors, and run budgets abort with aTimeoutError. A bareAbortErrorwithout 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
createQaGatewayChild, mock OpenAI provider) built fromae64a98a4eb1, where the startup-scope bug aborts every HTTP agent run:/v1/chat/completions→504 {"error":{"message":"upstream provider timeout","type":"api_error"}};/v1/responses→504, statusfailed/v1/chat/completions→500 {"error":{"message":"agent run was cancelled","type":"api_error"}};/v1/responses→500, statusfailed[openai-compat] chat completion failed: AbortError: This operation was abortedin both runs.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 (aFailoverErrortimeout carrying the abort message, and an abort caused by aTimeoutError) keep504on 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.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/completionshandler before they moved to the mapper owner (the endpoint test file is over the line cap and may not grow).node scripts/check-changed.mjson the changed files: passed (core and core-test typecheck, lint, dead-export scan, import cycles, line-cap ratchet);git diff --checkclean.$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.abortdoes not currently stop OpenAI-compatible HTTP runs, which is a separate follow-up), so the live before/after uses the historical startup-scope trigger.