Repository navigation
fix(gateway): report client-actionable status codes (404 for unknown models, 413 for oversized payloads) - #139
Merged
jasonxu114514 merged 1 commit intoOct 29, 2026
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 exposeis 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.Knownnow separates the two cases. An unknown model gets404with codemodel_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
413with codepayload_too_large. Every other phase stays a 504:writeheadersfirst_eventThe 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:model_not_found; a known-but-unservable model keeps the 400;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.