diff --git a/internal/gateway/codex_capacity_label_test.go b/internal/gateway/codex_capacity_label_test.go new file mode 100644 index 000000000..e9d963d0c --- /dev/null +++ b/internal/gateway/codex_capacity_label_test.go @@ -0,0 +1,82 @@ +package gateway + +import ( + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/yetone/magpie/internal/provider" +) + +// 蛋仔 on Discord: a Codex turn over two ChatGPT accounts, the first's +// model at capacity (529), the last out of its allowance. Codex is told +// the overload ("Selected model is at capacity. Please try a different +// model."), as it should be, but the Requests row read "529 · +// usage_limit_reached": the status and words were the overload's, the +// kind the last account's. The row's kind is the failure Codex was told, +// and only the account out of its allowance rests for long. +func TestCodexCapacityNotLabelledUsageLimit(t *testing.T) { + for name, overload := range map[string]string{ + "backend's overload": overloadedStatus, + "the words Codex showed": `{"error":{"type":"server_error","code":"server_is_overloaded","message":"Selected model is at capacity. Please try a different model."}}`, + } { + t.Run(name, func(t *testing.T) { + codexSignedIn(t, "spare@example.com") + var tried []string + up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + io.ReadAll(r.Body) + acct := r.Header.Get("chatgpt-account-id") + tried = append(tried, acct) + w.Header().Set("Content-Type", "application/json") + if acct == "acct-1" { + w.WriteHeader(529) + io.WriteString(w, overload) + return + } + w.WriteHeader(429) + io.WriteString(w, `{"error":{"type":"usage_limit_reached","message":"The usage limit has been reached","plan_type":"plus","resets_in_seconds":7200}}`) + })) + t.Cleanup(up.Close) + was := provider.CodexBase + provider.CodexBase = up.URL + "/backend-api/codex" + t.Cleanup(func() { provider.CodexBase = was }) + pause := retryPause + retryPause = time.Millisecond + t.Cleanup(func() { retryPause = pause }) + + code, body := codexPost(t, oneAccountTurn) + if code != 529 || strings.Contains(strings.ToLower(body), "usage limit") { + t.Fatalf("Codex told %d %s (tried %v)", code, body, tried) + } + if tried[0] != "acct-1" || tried[len(tried)-1] != "acct-2" { + t.Fatalf("tried %v", tried) + } + rec := lastUsage(t) + if rec.Status != 529 || rec.ErrType == "usage_limit_reached" || !strings.Contains(strings.ToLower(rec.Error), "overloaded") && !strings.Contains(rec.Error, "at capacity") { + t.Fatalf("row: %d · %q · %q", rec.Status, rec.ErrType, rec.Error) + } + if rec.ErrType != "server_error" { + t.Fatalf("row's kind %q, want the overload's server_error", rec.ErrType) + } + // the overloaded account sits out a moment at most; the one out + // of its allowance rests till ChatGPT says it is back + restingUntil.Lock() + defer restingUntil.Unlock() + long := 0 + for id, u := range restingUntil.m { + switch d := time.Until(u); { + case d > time.Hour: + long++ + case d > overloadRestMost: + t.Errorf("%s rests %v for an overload", id, d) + } + } + if long != 1 { + t.Errorf("%d accounts rest for hours, want the one out of its allowance: %v", long, restingUntil.m) + } + }) + } +} diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 69e4b07c4..4a23c6228 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -3169,7 +3169,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro msg := "" switch { case other != nil: - call.Status, call.Error = other.Status, other.Error + call.Status, call.Error, call.Usage.ErrType = other.Status, other.Error, other.errType case call.Status < 400: call.Status, call.Error = 404, "every account passed over by a middleware" msg = fmt.Sprintf("none of %s's models is ready", call.Model) @@ -3331,7 +3331,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro // but with a quota, the agent is told this, as it would be had // Antigravity been asked if other == nil { - other = &Try{Status: turnedAwayStatus, Error: c.p.Name + ": " + hint} + other = &Try{Status: turnedAwayStatus, Error: c.p.Name + ": " + hint, errType: turnedAwayErrType} } skipped = append(skipped, c.label()+": "+hint) continue @@ -3840,7 +3840,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro break } if other == nil { - other = &Try{Status: call.Status, Error: call.Error} + other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType} } skipped = append(skipped, c.label()+": "+call.Error) continue @@ -4060,7 +4060,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro // nobody after it answers but with a quota of its own, that is // what the agent is told, at a status it doesn't retry if other == nil { - other = &Try{Status: turnedAwayStatus, Error: call.Error} + other = &Try{Status: turnedAwayStatus, Error: call.Error, errType: turnedAwayErrType} } try.Fail = failPrompt s.trace.update(tr, func(t *Route) { @@ -4102,7 +4102,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro // Nothing is wrong with the account, so it doesn't rest; its // mates, carrying the same prompt, are asked last if other == nil { - other = &Try{Status: call.Status, Error: call.Error} + other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType} } try.Fail = failPrompt s.trace.update(tr, func(t *Route) { t.Tries[len(t.Tries)-1] = try }) @@ -4148,7 +4148,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro } if left := withRoom(cands[i+1:], c, tokens); len(left) > 0 { if other == nil { - other = &Try{Status: call.Status, Error: call.Error} + other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType} } try.Fail = failOverflow if hw.code() == http.StatusRequestEntityTooLarge { @@ -4187,7 +4187,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro // input item it doesn't know, #350) another's may: the next is // asked once, as each is, and this one doesn't rest if other == nil { - other = &Try{Status: call.Status, Error: call.Error} + other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType} } try.Fail = failShape s.trace.update(tr, func(t *Route) { t.Tries[len(t.Tries)-1] = try }) @@ -4199,7 +4199,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro // to the vendor, so this one doesn't rest, and a member that // goes another way (or the proxy back up) may still answer (#381) if other == nil { - other = &Try{Status: call.Status, Error: call.Error} + other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType} } try.Fail = failProxy s.trace.update(tr, func(t *Route) { t.Tries[len(t.Tries)-1] = try }) @@ -4211,7 +4211,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro // request never reached the vendor, so this one doesn't rest, // and is asked first again once the network is back if other == nil { - other = &Try{Status: call.Status, Error: call.Error} + other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType} } try.Fail = failOffline s.trace.update(tr, func(t *Route) { t.Tries[len(t.Tries)-1] = try }) @@ -4224,7 +4224,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro // is signed in again (provider/claude_auth.go), and the next // one is asked if other == nil { - other = &Try{Status: call.Status, Error: call.Error} + other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType} } try.Fail = failAuth s.trace.update(tr, func(t *Route) { t.Tries[len(t.Tries)-1] = try }) @@ -4251,7 +4251,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro noRoom := hw.failed() && claudeNoRoom(hw.code(), hw.errBody()) if protected || zdr || noRoom { if other == nil { - other = &Try{Status: call.Status, Error: call.Error} + other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType} } try.Fail = failOther left := elsewhere(cands[i+1:], c) @@ -4299,7 +4299,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro // The last one left is asked again after a moment, as for any // 503, before the agent is told if other == nil { - other = &Try{Status: call.Status, Error: call.Error} + other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType} } try.Fail = failOther s.trace.update(tr, func(t *Route) { t.Tries[len(t.Tries)-1] = try }) @@ -4311,7 +4311,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro // is the model's doing, not the account's: the next is asked, // and nobody rests (#2274) if other == nil { - other = &Try{Status: call.Status, Error: call.Error} + other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType} } try.Fail = failOther s.trace.update(tr, func(t *Route) { t.Tries[len(t.Tries)-1] = try }) @@ -4324,7 +4324,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro // and mates of the same model, likely to end empty too, are // asked last if other == nil { - other = &Try{Status: call.Status, Error: call.Error} + other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType} } try.Fail = failOther s.trace.update(tr, func(t *Route) { t.Tries[len(t.Tries)-1] = try }) @@ -4365,7 +4365,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro }) if !last { if other == nil { - other = &Try{Status: call.Status, Error: call.Error} + other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType} } skipped = append(skipped, c.label()+": "+call.Error) matesLast(cands[i+1:], c) @@ -4405,7 +4405,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro next, nextW, unlisted, unlistedW = unlistedNext(unlisted, unlistedW, key) if len(next) > 0 && last { if f := failure(hw.code(), hw.errBody()); other == nil && f != failQuota && f != failCredit { - other = &Try{Status: call.Status, Error: call.Error} + other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType} } rest := s.restAfterMarked(c, hw.code(), hw.header, hw.errBody(), hw.sharedPool) try.Fail, try.Rest = rest.Why, &rest @@ -4431,7 +4431,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro // whose reply said nothing (#1221) or was cut short by its // output limit (#2274): the next is asked, and nobody rests if other == nil { - other = &Try{Status: call.Status, Error: call.Error} + other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType} } try.Fail = failOther s.trace.update(tr, func(t *Route) { t.Tries[len(t.Tries)-1] = try }) @@ -4446,7 +4446,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro // then the next member as any failure's is (#2873) try.Fail = failCut if other == nil { - other = &Try{Status: call.Status, Error: call.Error} + other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType} } s.trace.update(tr, func(t *Route) { t.Tries[len(t.Tries)-1] = try }) skipped = append(skipped, c.label()+": "+call.Error) @@ -4486,7 +4486,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro } if !last && hw.failed() { if f := failure(hw.code(), hw.errBody()); other == nil && f != failQuota && f != failCredit { - other = &Try{Status: call.Status, Error: call.Error} + other = &Try{Status: call.Status, Error: call.Error, errType: call.Usage.ErrType} } rest := s.restAfterMarked(c, hw.code(), hw.header, hw.errBody(), hw.sharedPool) try.Fail, try.Rest = rest.Why, &rest @@ -4692,7 +4692,10 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro // (Discord, waroy) try.Fail, try.Earlier = f, true s.trace.update(tr, func(t *Route) { t.Tries[len(t.Tries)-1] = try }) - call.Status, call.Error = other.Status, other.Error + // and its row says that one's kind too: a 529 overload told + // beside the last account's usage_limit_reached read as the + // overload being the quota (蛋仔 on Discord) + call.Status, call.Error, call.Usage.ErrType = other.Status, other.Error, other.errType failTo(w, kept, from, call.Status, call.Error) break } else if outgrew { diff --git a/internal/gateway/trace.go b/internal/gateway/trace.go index 8c513e621..e8ca0addf 100644 --- a/internal/gateway/trace.go +++ b/internal/gateway/trace.go @@ -482,6 +482,10 @@ type Try struct { // allowance's (Discord, waroy). Whether a try went on is the next // try in Tries, which is appended only as it is asked (#2281) Earlier bool `json:"earlier,omitempty"` + // errType: the kind of error the failure's body named (Usage.ErrType), + // kept for the one told in place of a later try's (Earlier), so the + // row's kind is that failure's and not the later try's + errType string // Queued: ms it waited for one of its key's or account's slots, the // provider's MaxConcurrency out already (concurrency.go) Queued int64 `json:"queued,omitempty"`