Skip to content

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

gateway: one request's own retries don't climb an account's backoff (ly on Discord)

yetone87c68c98c53b
diff --git a/internal/gateway/backoff_one_request_test.go b/internal/gateway/backoff_one_request_test.go
new file mode 100644
index 000000000..3fe02c633
--- /dev/null
+++ b/internal/gateway/backoff_one_request_test.go
@@ -0,0 +1,76 @@
+package gateway
+
+import (
+ "io"
+ "net/http"
+ "testing"
+ "time"
+
+ "github.com/yetone/magpie/internal/provider"
+)
+
+// One request whose model fails alike on every Codex account — the
+// ChatGPT backend's 500, or a stream that fails with server_error — is
+// asked of each account again and again by the request's patience. Each
+// account rests on the backoff ladder's first step, a minute: its own
+// failures within the one rest aren't counted again (ly on Discord: seven
+// Plus accounts on gpt-6-astra all resting up to the 10-minute cap after
+// one bad spell, each counted 13 times by one request). Once its rest has
+// ended, failing again climbs the ladder as before.
+func TestOneRequestDoesNotClimbTheBackoff(t *testing.T) {
+ for _, x := range []struct {
+ name string
+ refuse func(w http.ResponseWriter)
+ }{
+ {"500", func(w http.ResponseWriter) {
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusInternalServerError)
+ io.WriteString(w, `{"error":{"message":"An error occurred while processing your request.","type":"server_error"}}`)
+ }},
+ {"response.failed", func(w http.ResponseWriter) {
+ w.Header().Set("Content-Type", "text/event-stream")
+ io.WriteString(w, sse(`data: {"type":"response.created","response":{"id":"r0"}}`,
+ `data: {"type":"response.failed","response":{"id":"r0","status":"failed","error":{"code":"server_error","message":"An error occurred while processing your request."}}}`))
+ }},
+ } {
+ t.Run(x.name, func(t *testing.T) {
+ codexSignedIn(t, "[email protected]", "[email protected]")
+ if err := provider.SetRouting("codex", provider.Ordered); err != nil {
+ t.Fatal(err)
+ }
+ var tried []string
+ chatgptRefusing(t, &tried, x.refuse, "acct-2", "acct-3")
+ s := New()
+ codexAskOn(s, `{"model":"gpt-6-astra","stream":true,"input":"ping"}`)
+ if len(tried) <= 3 {
+ t.Fatalf("each account asked once only (%v): the request's own retries aren't what this tests", tried)
+ }
+ rested := 0
+ for _, who := range []string{"codex@[email protected]", "codex@[email protected]", "codex@[email protected]"} {
+ r, ok := restOf(who)
+ if !ok {
+ continue
+ }
+ rested++
+ if r.Why != failOther || r.Failures != 1 || time.Until(r.Until) > fallbackCooldown {
+ t.Fatalf("%s rests %v by %s, %d failures in a row, after one request asked it %d times", who, time.Until(r.Until).Round(time.Second), r.By, r.Failures, len(tried)/3)
+ }
+ }
+ if rested == 0 {
+ t.Fatal("no account rests: the test proves nothing")
+ }
+
+ // its rest over, the next failure climbs to the next step
+ restingUntil.Lock()
+ for k := range restingUntil.m {
+ restingUntil.m[k] = time.Now().Add(-time.Second)
+ }
+ restingUntil.Unlock()
+ c := candidate{p: provider.Provider{ID: "codex", Account: &provider.Account{Agent: "codex", User: "[email protected]"}}, model: "gpt-6-astra", rest: "codex@[email protected]"}
+ r := s.restAfter(c, http.StatusInternalServerError, http.Header{}, []byte(`{"error":{"message":"An error occurred while processing your request.","type":"server_error"}}`))
+ if r.Failures != 2 || r.By != "backoff" || time.Until(r.Until) <= fallbackCooldown {
+ t.Fatalf("failing again after its rest: %d failures, rests %v by %s", r.Failures, time.Until(r.Until).Round(time.Second), r.By)
+ }
+ })
+ }
+}
diff --git a/internal/gateway/routing.go b/internal/gateway/routing.go
index 172048546..24117c09d 100644
--- a/internal/gateway/routing.go
+++ b/internal/gateway/routing.go
@@ -1187,9 +1187,21 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo
if unserved {
counted = c.restID()
}
+ // failing again while it still rests for a failure of this kind —
+ // asked all the same, as the last one left, or by the request's
+ // own patience going over every account again — keeps the count
+ // where it is, as a rate limit does (rateRest): it climbs once a
+ // rest has ended and it fails again. Before, one request whose
+ // model failed alike on every account (a 5xx, asked a dozen times
+ // over by patience) counted each account a dozen times and rested
+ // them all the 10-minute cap at once (ly on Discord: seven Codex
+ // accounts on gpt-6-astra, all resting minutes)
+ again := backingOff(counted, now) || !unserved && c.model != "" && backingOff(c.restID(), now)
routed.Lock()
- routed.failures[counted]++
- n := routed.failures[counted]
+ if !again {
+ routed.failures[counted]++
+ }
+ n := max(routed.failures[counted], 1)
// the model's own count too, which only its own answer clears: on
// an account serving several models, a sibling's answer clears
// the account's count, and a model that keeps failing between
@@ -1198,7 +1210,9 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo
// is the model's: it climbs its own ladder and rests alone. A
// candidate naming no model has no sibling to tell apart
if !unserved && c.model != "" {
- routed.failures[c.restID()]++
+ if !again {
+ routed.failures[c.restID()]++
+ }
if own := routed.failures[c.restID()]; own > n {
n, modelBackoff = own, true
}
@@ -1384,6 +1398,15 @@ func restSlow(c candidate, now time.Time) (Rest, bool) {
return r, true
}
+// backingOff says key still rests after a failure that said nothing of
+// why (failOther), whose count a failure now would climb.
+func backingOff(key string, now time.Time) bool {
+ restingUntil.Lock()
+ defer restingUntil.Unlock()
+ until, ok := restingUntil.m[key]
+ return ok && now.Before(until) && restingUntil.note[key].Why == failOther
+}
+
// rateRest is how long a rate-limited candidate sits out: as long as the
// vendor says (Retry-After, a reset header or a reset time in its error),
// and a minute when it says nothing. One limited again as soon as its rest
diff --git a/internal/gateway/routing_test.go b/internal/gateway/routing_test.go
index 9c06fa5f1..38b4fe21f 100644
--- a/internal/gateway/routing_test.go
+++ b/internal/gateway/routing_test.go
@@ -217,6 +217,16 @@ func TestSmartRouting(t *testing.T) {
h.Set("Retry-After", "300")
s.restAfter(key("t#rate"), 429, h, []byte("slow down"))
s.restAfter(key("t#fail"), 500, http.Header{}, nil)
+ // failing again while it rests keeps its step (ly on Discord: one
+ // request's own retries counted alike); once the rest is over, the
+ // next failure climbs to the next
+ s.restAfter(key("t#fail"), 500, http.Header{}, nil)
+ if until("t#fail") != time.Minute {
+ t.Fatalf("failing again while resting: %v", until("t#fail"))
+ }
+ restingUntil.Lock()
+ restingUntil.m["t#fail"] = time.Now().Add(-time.Second)
+ restingUntil.Unlock()
s.restAfter(key("t#fail"), 500, http.Header{}, nil)
if until("t#credit") != creditRest || until("t#rate") != 5*time.Minute || until("t#fail") != 2*time.Minute {
t.Fatalf("rests: %v %v %v", until("t#credit"), until("t#rate"), until("t#fail"))