Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

gateway: an error carried in a 200 fails as its own status would

yetonee68327b217bd
diff --git a/internal/gateway/carried_error_test.go b/internal/gateway/carried_error_test.go
new file mode 100644
index 000000000..21e0f2270
--- /dev/null
+++ b/internal/gateway/carried_error_test.go
@@ -0,0 +1,194 @@
+package gateway
+
+import (
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/yetone/magpie/internal/provider"
+)
+
+// What monater's OpenAI-format provider answered for gpt-6.1-sol, with
+// HTTP 200 (Discord, #general): word for word.
+const carriedTPM = "Rate limit reached for gpt-6.1-sol in organization org-xxx on tokens per min (TPM): Limit 500000000, Used 499954050, Requested 101782. Please try again in 6ms. Visit https://platform.openai.com/account/rate-limits to learn more."
+
+var carriedTPMBody = `{"error":{"message":"` + carriedTPM + `","type":"tokens","param":null,"code":"rate_limit_exceeded"}}`
+
+const carriedQuotaBody = `{"error":{"message":"You exceeded your current quota, please check your plan and billing details.","type":"insufficient_quota","param":null,"code":"insufficient_quota"}}`
+
+// An error a reply carried under a 200 fails as the same error with its
+// real status does: the minute limit is a rate limit, not a plan used up,
+// and insufficient_quota stays out of credit.
+func TestCarriedErrorFailsAsItsStatus(t *testing.T) {
+ c := candidate{p: provider.Provider{ID: "custom", Chat: "https://relay.example/v1", Key: "k"}, model: "gpt-6.1-sol"}
+ for _, body := range []string{carriedTPM, carriedTPMBody, carriedQuotaBody} {
+ want := failure(429, []byte(body))
+ if got := failure(200, []byte(body)); got != want {
+ t.Errorf("failure(200, %s) = %s, want %s as at 429", body, got, want)
+ }
+ if got := failureOf(c, 200, []byte(body)); got != want {
+ t.Errorf("failureOf(200, %s) = %s, want %s as at 429", body, got, want)
+ }
+ }
+ if got := failure(200, []byte(carriedTPM)); got != failRate {
+ t.Errorf("the TPM limit at 200 is %s, want %s", got, failRate)
+ }
+ if got := failure(200, []byte(carriedQuotaBody)); got != failCredit {
+ t.Errorf("insufficient_quota at 200 is %s, want %s", got, failCredit)
+ }
+ // minute limits that name themselves a rate limit fail the same at 200
+ for body, want := range minuteLimitBodies {
+ if streamStatus(string(wordsOf([]byte(body)))) != 429 {
+ continue
+ }
+ if got := failure(200, []byte(body)); got != want {
+ t.Errorf("failure(200, %s) = %s, want %s", body, got, want)
+ }
+ }
+}
+
+// The rest a 200-carried minute limit leaves is a rate limit's, shown
+// with the 429 it stands for, not "200 quota used up".
+func TestCarriedMinuteLimitRestsAMinute(t *testing.T) {
+ old := allowances
+ defer func() { allowances = old }()
+ allowances = func(string) map[string]provider.Allowance { return nil }
+ clineRestsCleared(t)
+ s := &Server{}
+ p := provider.Provider{ID: "custom", Chat: "https://relay.example/v1", Key: "k"}
+ c := candidate{p: p, model: "gpt-6.1-sol", rest: p.ID}
+ r := s.restAfter(c, 200, nil, []byte(carriedTPM))
+ if left := time.Until(r.Until); r.Why != failRate || r.Status != 429 || left > 2*time.Minute || left <= 0 {
+ t.Errorf("rests %v as %s (%d, by %s), want a rate limit's minute at 429", left, r.Why, r.Status, r.By)
+ }
+}
+
+// errorReply knows a whole 200 that is only an error from a reply.
+func TestErrorReply(t *testing.T) {
+ for body, want := range map[string]int{
+ carriedTPMBody: 429,
+ carriedQuotaBody: 402,
+ `{"type":"error","error":{"type":"rate_limit_error","message":"Number of request tokens has exceeded your per-minute rate limit"}}`: 429,
+ `{"error":"Rate limit reached, please try again later"}`: 429,
+ `{"id":"r1","object":"response","status":"failed","output":[],"error":{"code":"rate_limit_exceeded","message":"` + carriedTPM + `"}}`: 429,
+ } {
+ status, msg, ok := errorReply([]byte(body))
+ if !ok || status != want || msg == "" {
+ t.Errorf("errorReply(%s) = %d %q %v, want %d", body, status, msg, ok, want)
+ }
+ }
+ for _, body := range []string{
+ chatOK,
+ `{"id":"r1","object":"response","status":"completed","output":[{"type":"message"}],"error":null}`,
+ `{"error":null,"choices":[{"message":{"content":"hi"}}]}`,
+ `{"error":""}`,
+ `{"error":{}}`,
+ `not json`,
+ } {
+ if _, _, ok := errorReply([]byte(body)); ok {
+ t.Errorf("errorReply(%s) took a reply for an error", body)
+ }
+ }
+}
+
+// A whole 200 that is only an error is booked failed, with its words.
+func TestSnifferBooksA200Error(t *testing.T) {
+ sn := newSniffer(provider.Chat, "application/json")
+ sn.write([]byte(carriedTPMBody))
+ sn.settleWhole()
+ if !strings.Contains(sn.failed, "Rate limit reached for gpt-6.1-sol") {
+ t.Errorf("failed = %q", sn.failed)
+ }
+ ok := newSniffer(provider.Chat, "application/json")
+ ok.write([]byte(chatOK))
+ ok.settleWhole()
+ if ok.failed != "" {
+ t.Errorf("an answer booked failed: %q", ok.failed)
+ }
+}
+
+func firstTry(t *testing.T, s *Server) Try {
+ t.Helper()
+ if len(s.trace.routes) == 0 || len(s.trace.routes[len(s.trace.routes)-1].Tries) == 0 {
+ t.Fatal("no try traced")
+ }
+ return s.trace.routes[len(s.trace.routes)-1].Tries[0]
+}
+
+// A non-streamed 200 whose body is only monater's TPM error is retried as
+// a rate limit, and the agent gets the answer, not the error as one.
+func TestCarriedErrorBodyRetriedAsRate(t *testing.T) {
+ fresh(t)
+ clineRestsCleared(t)
+ a := &scripted{replies: []reply{{200, "", carriedTPMBody}, {200, "", chatOK}}}
+ scriptedOn(t, "a", provider.Chat, a)
+ s := New()
+ code, body := postAs(t, s, "", `{"model":"a/m","messages":[{"role":"user","content":"hi"}]}`)
+ if code != 200 || !strings.Contains(body, "hello") || a.n != 2 {
+ t.Fatalf("%d %s (asked %d times)", code, body, a.n)
+ }
+ if try := firstTry(t, s); try.Fail != failRate {
+ t.Errorf("first try failed as %q, want %q", try.Fail, failRate)
+ }
+}
+
+// The same on the Responses route.
+func TestCarriedErrorBodyRetriedAsRateOnResponses(t *testing.T) {
+ fresh(t)
+ clineRestsCleared(t)
+ failed := `{"id":"r0","object":"response","status":"failed","output":[],"error":{"code":"rate_limit_exceeded","message":"` + carriedTPM + `"}}`
+ done := `{"id":"r1","object":"response","status":"completed","output":[{"type":"message","role":"assistant","content":[{"type":"output_text","text":"hello"}]}]}`
+ a := &scripted{replies: []reply{{200, "", failed}, {200, "", done}}}
+ responsesOn(t, "a", a)
+ s := New()
+ code, body := sendTo(s, "/v1/responses", `{"model":"a/m","input":"hi"}`)
+ if code != 200 || !strings.Contains(body, "hello") || a.n != 2 {
+ t.Fatalf("%d %s (asked %d times)", code, body, a.n)
+ }
+ if try := firstTry(t, s); try.Fail != failRate {
+ t.Errorf("first try failed as %q, want %q", try.Fail, failRate)
+ }
+}
+
+// A 200 stream that broke off with monater's TPM error after some of the
+// answer rests the account a rate limit's minute, shown as 429, not as
+// "200 quota used up".
+func TestCarriedStreamErrorRestsAsRate(t *testing.T) {
+ fresh(t)
+ clineRestsCleared(t)
+ stream := sse(`data: {"id":"c1","object":"chat.completion.chunk","model":"gpt-6.1-sol","choices":[{"index":0,"delta":{"role":"assistant","content":"Hel"}}]}`,
+ `data: {"error":{"message":"`+carriedTPM+`","type":"tokens","param":null,"code":"rate_limit_exceeded"}}`)
+ a := &scripted{replies: []reply{{200, "text/event-stream", stream}}}
+ scriptedOn(t, "a", provider.Chat, a)
+ s := New()
+ postAs(t, s, "", `{"model":"a/m","stream":true,"messages":[{"role":"user","content":"hi"}]}`)
+ try := firstTry(t, s)
+ if try.Fail != failRate {
+ t.Errorf("failed as %q, want %q", try.Fail, failRate)
+ }
+ if try.Rest == nil || try.Rest.Why != failRate || try.Rest.Status != 429 || time.Until(try.Rest.Until) > 2*time.Minute {
+ t.Errorf("rest %+v, want a rate limit's minute at 429", try.Rest)
+ }
+}
+
+// An Anthropic client's request translated to a chat provider whose 200
+// stream is only the TPM error fails that try as a rate limit, not as a
+// vendor's 502.
+func TestCarriedStreamErrorTranslatedAsRate(t *testing.T) {
+ fresh(t)
+ clineRestsCleared(t)
+ failed := sse(`data: {"error":{"message":"` + carriedTPM + `","type":"tokens","param":null,"code":"rate_limit_exceeded"}}`)
+ done := sse(`data: {"id":"c1","object":"chat.completion.chunk","model":"m","choices":[{"index":0,"delta":{"role":"assistant","content":"hello"}}]}`,
+ `data: {"id":"c1","object":"chat.completion.chunk","model":"m","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":2,"completion_tokens":1}}`,
+ `data: [DONE]`)
+ a := &scripted{replies: []reply{{200, "text/event-stream", failed}, {200, "text/event-stream", done}}}
+ scriptedOn(t, "a", provider.Chat, a)
+ s := New()
+ code, body := sendTo(s, "/v1/messages", `{"model":"a/m","max_tokens":16,"messages":[{"role":"user","content":"hi"}]}`)
+ if code != 200 || !strings.Contains(body, "hello") {
+ t.Fatalf("%d %s (asked %d times)", code, body, a.n)
+ }
+ if try := firstTry(t, s); try.Fail != failRate {
+ t.Errorf("first try failed as %q, want %q", try.Fail, failRate)
+ }
+}
diff --git a/internal/gateway/fallback.go b/internal/gateway/fallback.go
index f365dd70f..6d075593f 100644
--- a/internal/gateway/fallback.go
+++ b/internal/gateway/fallback.go
@@ -2484,6 +2484,15 @@ func (h *holdWriter) settle() {
h.failure, h.failMsg, h.refused = refusedStatus, msg, true
return
}
+ if h.status < 400 {
+ // a 200 that is only an error fails as that error's own status
+ // would (errorReply): asked again, or of the next, not handed on
+ // as an answer
+ if status, msg, ok := errorReply(h.held.Bytes()); ok {
+ h.failure, h.failMsg = status, msg
+ return
+ }
+ }
if h.alive != nil && h.alive.sent && h.status < 400 && wholeEvents(h.alive.proto, h.held.Bytes()) == nil {
// a 200 that is no reply of the protocol's, once the agent has a
// stream's headers: it can't go as that stream, so it failed, as
@@ -3394,6 +3403,48 @@ func refusedReply(b []byte) (string, bool) {
return "", false
}
+// errorReply says whether a reply that came whole under a success status
+// is only an error: OpenAI's or Gemini's {"error":{…}}, Anthropic's
+// {"type":"error","error":{…}}, a Responses object whose error is set —
+// with no reply beside it. msg is what it says (provider.APIError), and
+// status what it stands for (streamStatus), as the same error said in a
+// stream is read: a relay that answers OpenAI's TPM limit as a 200 fails
+// as its 429 would, asked again in a moment, not as a reply that went
+// through (monater on Discord).
+func errorReply(body []byte) (status int, msg string, ok bool) {
+ b := bytes.TrimSpace(body)
+ if len(b) == 0 || b[0] != '{' {
+ return 0, "", false
+ }
+ var v struct {
+ Error json.RawMessage `json:"error"`
+ Choices []any `json:"choices"`
+ Content []any `json:"content"`
+ Candidates []any `json:"candidates"`
+ Output []any `json:"output"`
+ Status string `json:"status"`
+ }
+ if json.Unmarshal(b, &v) != nil || !carries(v.Error) || len(v.Choices)+len(v.Content)+len(v.Candidates) > 0 ||
+ len(v.Output) > 0 && v.Status != "failed" {
+ return 0, "", false
+ }
+ var e struct {
+ Message any `json:"message"`
+ Type any `json:"type"`
+ Code any `json:"code"`
+ Status any `json:"status"`
+ }
+ var said string
+ if json.Unmarshal(v.Error, &said) == nil {
+ if strings.TrimSpace(said) == "" {
+ return 0, "", false
+ }
+ } else if json.Unmarshal(v.Error, &e) != nil || e.Message == nil && e.Type == nil && e.Code == nil && e.Status == nil {
+ return 0, "", false
+ }
+ return streamStatus(string(wordsOf(b))), provider.APIError(b, "the upstream reported an error"), true
+}
+
// streamStatus is the status an error in a stream stands for, as a vendor
// would have answered it before streaming.
func streamStatus(msg string) int {
diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go
index 0971cd264..e9fee3752 100644
--- a/internal/gateway/gateway.go
+++ b/internal/gateway/gateway.go
@@ -5184,6 +5184,8 @@ func (s *Server) passthrough(w http.ResponseWriter, r *http.Request, p provider.
}
if sse {
sniff.drain()
+ } else {
+ sniff.settleWhole()
}
if gather != nil {
cut := ""
@@ -6327,8 +6329,9 @@ func (s *Server) translate(w http.ResponseWriter, r *http.Request, p provider.Pr
}
// a request the vendor refused in its stream is answered as
// refused, with its code (magpie-releases#18); anything else is
- // the vendor failing
- return writeVendorError(w, from, cmp.Or(col.errStatus, 502), p.Name+": "+said, col.errSaid), said
+ // the vendor failing, with the status its words would have come
+ // with: a minute limit in a 200 stream is a 429, not a 502
+ return writeVendorError(w, from, cmp.Or(col.errStatus, streamStatus(said)), p.Name+": "+said, col.errSaid), said
}
if col.err == "" && !ended && saysItsEnd(p, actual, model) && r.Context().Err() == nil {
// an Anthropic stream that just stopped — no stop_reason, no
diff --git a/internal/gateway/routing.go b/internal/gateway/routing.go
index 602124ea6..57c95d456 100644
--- a/internal/gateway/routing.go
+++ b/internal/gateway/routing.go
@@ -361,8 +361,29 @@ var proxyDown = regexp.MustCompile(`proxyconnect |socks connect `)
// the strings of a JSON body that escapes what isn't ASCII decoded (#1669).
func wordsOf(body []byte) []byte { return catalog.Words(body) }
+// carriedStatus is the status an error a reply carried under a success
+// status stands for: an `error` event, or a `{"error":{…}}` body, sent
+// with HTTP 200, is read as streamStatus reads an error in a stream, so
+// it fails as the same error would with its real status. Read as the
+// 200 it came with, OpenAI's "Rate limit reached for gpt-6.1-sol … on
+// tokens per min (TPM) … Please try again in 6ms." held usedUpWords'
+// "limit reached" and rested the account as out of quota (monater on
+// Discord), where its 429 is a minute's rate limit. Words streamStatus
+// doesn't know (its 502) keep the 200, and read as before; any other
+// status is the reply's own.
+func carriedStatus(status int, body []byte) int {
+ if status < 200 || status >= 400 {
+ return status
+ }
+ if said := streamStatus(string(wordsOf(body))); said != http.StatusBadGateway {
+ return said
+ }
+ return status
+}
+
// failure says why a reply failed.
func failure(status int, body []byte) string {
+ status = carriedStatus(status, body)
if status == http.StatusBadGateway && proxyDown.Match(body) {
return failProxy
}
@@ -456,6 +477,7 @@ func dashScopeRate(p provider.Provider, status int, body []byte) bool {
// till it is signed in again; another vendor saying the same words fails
// as failure says, and rests as before.
func failureOf(c candidate, status int, body []byte) string {
+ status = carriedStatus(status, body)
if status >= 400 && c.p.Account != nil && c.p.Account.Agent == "claude" && provider.ClaudeSignInRequired(string(body)) {
return failAuth
}
@@ -1000,6 +1022,9 @@ func (s *Server) restAfter(c candidate, status int, header http.Header, body []b
// restAfterMarked keeps an upstream routing fact through gateway error
// translation without putting it in the response sent to the client.
func (s *Server) restAfterMarked(c candidate, status int, header http.Header, body []byte, sharedPool bool) Rest {
+ // an error carried under a 200 rests as its own status would, and
+ // says that status where the rest is shown
+ status = carriedStatus(status, body)
now := time.Now()
d := fallbackCooldown
why := failureOf(c, status, body)
diff --git a/internal/gateway/sniff.go b/internal/gateway/sniff.go
index d618a3937..aada6ca1a 100644
--- a/internal/gateway/sniff.go
+++ b/internal/gateway/sniff.go
@@ -268,6 +268,19 @@ func (s *usageSniffer) drain() {
}
}
+// settleWhole reads a reply that came whole, once it has ended, for an
+// error it carried under its success status (errorReply): told as failed,
+// with what it said, not counted as an answer.
+func (s *usageSniffer) settleWhole() {
+ if s.sse || s.over || s.failed != "" {
+ return
+ }
+ if _, msg, ok := errorReply(s.buf); ok {
+ s.failed = msg
+ s.u.ErrType = provider.ErrorType(bytes.TrimSpace(s.buf))
+ }
+}
+
// usage is what the reply reported, the model it named in Served; call it
// once the body has ended.
func (s *usageSniffer) usage() Usage {