Skip to content

fix(gateway): report client-actionable status codes (404 for unknown models, 413 for oversized payloads) - #139

Merged
jasonxu114514 merged 1 commit into
jasonxu114514:mainfrom
SK-DEV-AI:fix/explicit-client-error-statuses
Oct 29, 2026
Merged

jasonxu114514 merged 1 commit into
jasonxu114514:mainfrom
SK-DEV-AI:fix/explicit-client-error-statuses

Conversation

@SK-DEV-AI

Copy link
Copy Markdown
Contributor

What

Two client-facing errors are uninformative today, and a harness cannot recover from either.

1. An unknown model returns 400, not 404

the model uses an upstream protocol that opencode2api does not expose is accurate for a model the catalog knows but cannot serve (a capability or routing problem) — and wrong for a model the upstream has never offered.

The same 400 was returned for both, so a request for a model the upstream retired (it answers 410 Gone) looked like a gateway capability bug, and clients retried a model that no longer exists.

ModelCatalog.Known now separates the two cases. An unknown model gets 404 with code model_not_found; a known-but-unservable model keeps the 400 and its accurate explanation.

2. An oversized payload returns 504, not 413

A request whose every attempt failed while writing the body was reported as 504 upstream request timed out. The upstream never finished reading the body, so the payload is too large — and a client that can shrink the request has no way to learn that.

Harnesses that recover from an oversized payload generally key on HTTP 413 (and image rejection on 400), so a 504 sent the same oversized payload around forever.

A write-phase failure while the request budget is still alive now returns 413 with code payload_too_large. Every other phase stays a 504:

phase meaning status
write the upstream never finished reading the body 413
headers the upstream was slow to answer 504
first_event the model was thinking 504

The phase is read from the innermost phasedError, so a wrapped error (the key loop hands the attempt error back through its own wrapper) still classifies correctly.

Both changes apply to the chat, responses and messages endpoints as well as to /v1/systemone.

Tests

internal/gateway/interop_status_test.go:

  • an unknown model returns 404 with model_not_found; a known-but-unservable model keeps the 400;
  • the 413 vs 504 split, including the "request budget expired" case (which is a slow request, not a payload problem).

Both are mutation-proved: reverting either change fails the matching test.

Note on the write phase

The write phase is not reproducible over loopback — the kernel accepts a body into the socket buffers even when the peer never reads it, so a 4/16/64 MiB body against a non-reading upstream still reports phase=headers. Production does see write-phase timeouts (1700+ legs in 48h on a live deployment, every one at exactly the attempt timeout), so the classifier is pinned directly rather than through a live upstream.

Two client-facing errors were uninformative, and a harness cannot recover
from either.

1. An unknown model was reported as 400 "the model uses an upstream protocol
   that opencode2api does not expose". That phrasing is accurate for a model
   the catalog KNOWS but cannot serve (a capability or routing problem), and
   wrong for a model the upstream has never offered. A model the upstream
   retired (it answers 410 Gone) produced this 400, so a harness kept
   retrying a model that no longer exists. The catalog can now tell the two
   cases apart (ModelCatalog.Known), and an unknown model returns 404 with
   code model_not_found.

2. A request whose every attempt failed while WRITING the body was reported
   as 504 "upstream request timed out". The upstream never finished reading
   the body, so the payload is too large, and a client that can shrink the
   request knows to shrink it. Harnesses that recover from an oversized
   payload generally key on HTTP 413 (and image rejection on 400), so a 504
   sent the same oversized payload around forever. A write-phase failure
   while the request budget is still alive now returns 413 with code
   payload_too_large; every other phase (headers, first_event) stays a 504,
   because a slow upstream or a thinking model is not the client's fault.

Both paths are applied to the chat, responses and messages endpoints as well as
to /v1/systemone.

Tests: the 404 vs 400 split, and the 413 vs 504 split. Both mutation-proved:
reverting either change fails the matching test.
@jasonxu114514
jasonxu114514 merged commit d07ee0e into jasonxu114514:main Oct 29, 2026
5 checks passed
jasonxu114514 added a commit that referenced this pull request Oct 29, 2026
Strips the tests that arrived with #152 and #139 after their squash
merges.
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.

2 participants