Files
magpie/internal/gateway/rate_held_test.go
T
yetone 803d7747e7 gateway: an account rate limited till a time its vendor named is not asked again before it (hsjzg58958 on X)
hsjzg58958 on X: a vendor limited an account for 300 seconds, and magpie
asked it again about every half a second as the agent retried, until the
vendor flagged two accounts.

Cause: a candidate resting on a 429 was still "tried all the same" when
it was the last one left (restLast/probeOne), and the last one left was
never rested at all, so every client retry went upstream. Codex on its
own sign-in is relayed by codexUpstreamOn outside the candidate loop and
had no hold at all.

Fix:
- timedHeld/vendorTimed (fallback.go): when every candidate left rests on
  a 429 whose time the vendor named (Retry-After, anthropic-ratelimit-*-
  reset, x-ratelimit-reset-*, resets_in_seconds/resets_at), none is asked;
  the agent gets 429 with Retry-After = when the first is back. A ChatGPT
  account out of its allowance is answered as the backend's
  usage_limit_reached with its x-codex-* windows (Rest.out/outHeader).
- The last one left now rests too on such a 429 (vendorSaidWhen); a 429
  naming no time rests nobody, as before.
- codexHolds (codex_held.go): Codex's own relayed sign-in is held till the
  vendor's time; its retries are answered locally with the stored body,
  x-codex-* headers and Retry-After. Lifted by restarted() for quota holds.
- Not held: LimitRestOff accounts (the user's setting), auto-reset weeks,
  rests "there" (answered by the vendor's own hold), Codex's API key.

Run:
- New TestRateLimitedKeysHeldUntilTheirReset (4 signals x 1 and 2 keys)
  and TestRateLimitedCodexAccountsHeldUntilTheirReset (3 refusals x 1 and
  2 accounts): red at fc9ce34d8 (each key/account asked on every retry),
  green after.
- Broken by hand: held branch off -> every case red (4 extra hits);
  last-one rest off -> 1-key cases red, Retry-After lost; codexHeld off ->
  1-account Codex cases red.
- mote: go test ./internal/gateway -run 'TestRateLimited|TestAllResting|
  TestQuotaHeld|TestGroupSpent|TestCodex|TestRetryAfter|TestHeldLimit'
  ok; TestCodexLeavingModelCompactsLocally updated (the used-up account is
  now asked once, then held) and ok.
- gofmt -l clean; go vet, GOOS=linux -tags nogui and GOOS=windows go vet
  and build ok.
- mote, whole internal/gateway package at the rebased head 46199a6ae
  (on 80161247a): ok but for TestStrictAffinityPassesTheErrorOn, which
  asserted the bug itself ("resting now, it is still the one asked"). A
  strict conversation's account resting till ChatGPT's named reset now
  gets its own usage-limit 429 from the rest, with nobody asked; updated,
  and with TestRateLimited|TestCodexLeavingModel|TestCodexEveryAccountOut
  ok at this head.
- The full ./... suite was queued behind six other sessions' suites on
  mote's lock and not run before the push; queued again at this head.

Not tried: a real vendor account limited live (no account was rate
limited to test on); Codex API-key relay is not held (OpenAI's platform
headers are Codex's own to read); plugin providers are held only through
the shared candidate loop; a held Codex passthrough body's
resets_in_seconds is stale by the time elapsed (Retry-After is exact).
2026-11-10 00:58:01 +08:00

188 lines
7.2 KiB
Go

package gateway
import (
"io"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// limitedUp is a vendor that has every key or account it serves over its
// rate limit for the next five minutes, and says so as refuse does. It
// counts what it was asked.
type limitedUp struct {
refuse func(w http.ResponseWriter)
n atomic.Int32
}
func (u *limitedUp) ServeHTTP(w http.ResponseWriter, r *http.Request) {
io.Copy(io.Discard, r.Body)
u.n.Add(1)
w.Header()["Content-Type"] = nil
u.refuse(w)
}
// limitedFor is the 300 seconds hsjzg58958's vendor named.
const limitedFor = 300 * time.Second
// rateRefusals are the ways a vendor says an account or key is rate
// limited for limitedFor: the Retry-After header, Anthropic's
// anthropic-ratelimit-*-reset and OpenAI's x-ratelimit-reset-* headers
// with no Retry-After, and ChatGPT's resets_in_seconds in the error.
var rateRefusals = []struct {
name string
refuse func(w http.ResponseWriter)
}{
{"Retry-After", func(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Retry-After", "300")
w.WriteHeader(429)
io.WriteString(w, `{"error":{"message":"Rate limit reached for requests","type":"rate_limit_error"}}`)
}},
{"anthropic-ratelimit reset", func(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("anthropic-ratelimit-requests-remaining", "0")
w.Header().Set("anthropic-ratelimit-requests-reset", time.Now().Add(limitedFor).UTC().Format(time.RFC3339))
w.WriteHeader(429)
io.WriteString(w, `{"type":"error","error":{"type":"rate_limit_error","message":"This request would exceed your account's rate limit. Please try again later."}}`)
}},
{"x-ratelimit reset", func(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("x-ratelimit-remaining-requests", "0")
w.Header().Set("x-ratelimit-reset-requests", "5m0s")
w.WriteHeader(429)
io.WriteString(w, `{"error":{"message":"Rate limit reached for gpt-5.5 on requests per min (RPM): Limit 3, Used 3, Requested 1.","type":"requests","code":"rate_limit_exceeded"}}`)
}},
{"resets_in_seconds", func(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(429)
io.WriteString(w, `{"error":{"type":"rate_limit_exceeded","message":"Rate limit reached. Please try again later.","resets_in_seconds":300}}`)
}},
}
// clientRetries is an agent asking again every half a second after a
// 429, as Codex and Claude Code do on their own; it returns the last
// answer.
func clientRetries(t *testing.T, n int, ask func() *httptest.ResponseRecorder) *httptest.ResponseRecorder {
t.Helper()
var rec *httptest.ResponseRecorder
for range n {
time.Sleep(500 * time.Millisecond)
rec = ask()
if rec.Code != http.StatusTooManyRequests {
t.Fatalf("retry answered %d: %s", rec.Code, rec.Body)
}
}
return rec
}
// heldRetryAfter checks that an answer given while every candidate is
// limited tells the agent when it is back, within the vendor's time.
func heldRetryAfter(t *testing.T, rec *httptest.ResponseRecorder) {
t.Helper()
ra, err := strconv.Atoi(rec.Header().Get("Retry-After"))
if err != nil || ra < int(limitedFor/time.Second)-30 || ra > int(limitedFor/time.Second) {
t.Errorf("Retry-After %q, want about %v", rec.Header().Get("Retry-After"), limitedFor)
}
}
// hsjzg58958 on X: a vendor rate limited the account for 300 seconds, and
// magpie asked it again about every half second, as the agent retried,
// till the vendor flagged two accounts. A key or account the vendor named
// a time for is not asked again before it: the agent's retries are
// answered here, 429 with when the first of them is back.
func TestRateLimitedKeysHeldUntilTheirReset(t *testing.T) {
for _, x := range rateRefusals {
for _, keys := range [][]string{{"k-a"}, {"k-a", "k-b"}} {
t.Run(x.name+"/"+strconv.Itoa(len(keys))+" keys", func(t *testing.T) {
fresh(t)
up := &limitedUp{refuse: x.refuse}
srv := httptest.NewServer(up)
t.Cleanup(srv.Close)
p := provider.Provider{ID: "lim", Name: "lim", Chat: srv.URL + "/v1", Models: []string{"m"}, Key: keys[0]}
for _, k := range keys[1:] {
p.Keys = append(p.Keys, provider.KeyAccount{Key: k})
}
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
s := New()
ask := func() *httptest.ResponseRecorder {
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"lim/m","messages":[{"role":"user","content":"hi"}]}`))
req.Header.Set("Content-Type", "application/json")
s.Handler().ServeHTTP(rec, req)
return rec
}
if rec := ask(); rec.Code != http.StatusTooManyRequests {
t.Fatalf("first: %d %s", rec.Code, rec.Body)
}
first := int(up.n.Load())
if first != len(keys) {
t.Errorf("first request asked %d times, want each key once", first)
}
rec := clientRetries(t, 4, ask)
if n := int(up.n.Load()); n != first {
t.Errorf("the agent's 4 retries reached the vendor %d times while every key was limited for %v", n-first, limitedFor)
}
heldRetryAfter(t, rec)
})
}
}
}
// The same for ChatGPT accounts behind Codex, one or two (the thread
// hsjzg58958 replied in): each refused for five minutes is asked once,
// whether ChatGPT says it is rate limited or out of its allowance till
// then.
func TestRateLimitedCodexAccountsHeldUntilTheirReset(t *testing.T) {
cases := append([]struct {
name string
refuse func(w http.ResponseWriter)
}{{"usage_limit_reached", func(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(429)
io.WriteString(w, `{"error":{"type":"usage_limit_reached","message":"The usage limit has been reached","plan_type":"plus","resets_in_seconds":300}}`)
}}}, rateRefusals[0], rateRefusals[3])
for _, x := range cases {
for _, spares := range [][]string{nil, {"spare@example.com"}} {
t.Run(x.name+"/"+strconv.Itoa(len(spares)+1)+" accounts", func(t *testing.T) {
codexSignedIn(t, spares...)
up := &limitedUp{refuse: x.refuse}
srv := httptest.NewServer(up)
t.Cleanup(srv.Close)
was := provider.CodexBase
provider.CodexBase = srv.URL + "/backend-api/codex"
t.Cleanup(func() { provider.CodexBase = was })
s := New()
ask := func() *httptest.ResponseRecorder {
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", CodexPath+"/responses", strings.NewReader(oneAccountTurn))
req.Header.Set("Authorization", "Bearer chatgpt-token")
req.Header.Set("chatgpt-account-id", "acct-1")
s.Handler().ServeHTTP(rec, req)
return rec
}
if rec := ask(); rec.Code != http.StatusTooManyRequests {
t.Fatalf("first: %d %s", rec.Code, rec.Body)
}
first := int(up.n.Load())
if first != len(spares)+1 {
t.Errorf("first request asked ChatGPT %d times, want each account once", first)
}
rec := clientRetries(t, 4, ask)
if n := int(up.n.Load()); n != first {
t.Errorf("Codex's 4 retries reached ChatGPT %d times while every account was limited for %v", n-first, limitedFor)
}
heldRetryAfter(t, rec)
})
}
}
}