Files
magpie/internal/gateway/token_wall_test.go
T
yetone 067e4c4846 gateway: Raccoon's token wall (user_model_rate_limited) isn't asked again in place (plugins#512)
SenseTime Raccoon answers an account whose token throughput is used up
with 429 {"error":{"code":"user_model_rate_limited","message":"您的模型请
求频率超限,请稍后重试","type":"rate_limit_error"}} and no Retry-After.
Its words read as a short rate limit, so the lone account was asked again
by the same-one retries (passing: 1, 2, 4s) and then by the plain
provider's patience (busy/replan: 2, 4, 8, 16s): eight asks in ~38s,
each one resending the whole ~88k-token request into the bucket it was
waiting on (cycocyco's numbers in plugins#512).

A 429 whose error.code is in wallCodes (only user_model_rate_limited) is
now neither passing nor busy. It still reads as failRate and rests as one
(a minute, doubled each time it comes back, up to 30 min, rateRest), so a
group's next member or the provider's next account is asked, and a lone
one hands the agent the vendor's error after one ask. The code is matched
at error.code only, not as words anywhere in the body. Other rate limits,
the same Chinese words under another code included, are retried as
before. The plugin passes the vendor's body through unchanged, so this is
the place for it.

Run (mote, at this change on origin/main 244d80e23):
- go test ./internal/gateway -run 'TestTokenWall|TestRateLimitIsWaitedOut|TestShortReset|TestLastOneLeft|TestGroup': ok
- gofmt -l internal cmd: clean; GOOS=linux go vet -tags nogui ./... and
  GOOS=windows go vet ./...: ok
- go test -timeout 45m -count=1 ./...: every package ok except
  internal/gui, whose ten real-window AppKit tests (dock, panel, tray,
  titlebar, plugin page) were killed at their deadlines, mote's known
  state (#4125, #2645); this change touches no GUI code.
Rebased on origin/main 88e6fb9cf: go test -timeout 45m ./internal/gateway
./internal/usage: ok. Rebased again on 88e6fb9cf (routing.go's credit
rests changed, not the rate path): go test ./internal/gateway -run
'TestTokenWall|TestRateLimitIsWaitedOut|TestShortReset|TestLastOneLeft|
TestGroup|Rest|Credit': ok; the whole package not rerun there.
Broken by hand: with both !walled(body) guards taken out,
TestTokenWallIsNotRetriedInPlace is red (passes, busy) and
TestTokenWallFailsFast is red with "429 after 16 tries".
Not tried: the real Raccoon API (no account here), nor the reporter's
Hermes on Windows.
2026-10-30 15:40:49 +08:00

76 lines
2.9 KiB
Go

package gateway
import (
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// plugins#512 (cycocyco): SenseTime Raccoon's 429 for an account's token
// throughput used up, word for word. It has no Retry-After.
const raccoonWall = `{"error":{"code":"user_model_rate_limited","message":"您的模型请求频率超限,请稍后重试","type":"rate_limit_error"}}`
// The wall is a rate limit, rested as one, but no moment clears it: it is
// not asked again in place, by the same-one retries or a patience's
// rounds. A rate limit without its code still is.
func TestTokenWallIsNotRetriedInPlace(t *testing.T) {
if got := failure(429, []byte(raccoonWall)); got != failRate {
t.Fatalf("failure: %s, want %s", got, failRate)
}
if _, ok := passing(429, nil, []byte(raccoonWall), 0); ok {
t.Error("the wall passes: asked again in place")
}
if busy(429, []byte(raccoonWall)) {
t.Error("the wall is busy: asked again in a patience's rounds")
}
// the same words with another code, or the code in a message, are a
// rate limit that passes, as before
for _, body := range []string{
`{"error":{"code":"rate_limited","message":"您的模型请求频率超限,请稍后重试","type":"rate_limit_error"}}`,
`{"error":{"message":"Rate limit exceeded (user_model_rate_limited), please try again later"}}`,
`{"error":{"code":429,"message":"Rate limit exceeded"}}`,
} {
if _, ok := passing(429, nil, []byte(body), 0); !ok {
t.Errorf("%s: doesn't pass", body)
}
if !busy(429, []byte(body)) {
t.Errorf("%s: not busy", body)
}
}
}
// The reporter's case: the lone account's wall reaches the agent after one
// ask, not eight in 38s; in a group the next member answers, and the
// walled one rests, so the next request isn't sent to it at all.
func TestTokenWallFailsFast(t *testing.T) {
fresh(t)
a := &scripted{replies: []reply{{429, "", raccoonWall}}}
scriptedOn(t, "a", provider.Chat, a)
start := time.Now()
code, body := postAs(t, New(), "", `{"model":"a/m","messages":[{"role":"user","content":"hi"}]}`)
if code != 429 || a.n != 1 || !strings.Contains(body, "user_model_rate_limited") && !strings.Contains(body, "频率超限") {
t.Fatalf("%d after %d tries: %s", code, a.n, body)
}
if d := time.Since(start); d > 5*time.Second {
t.Fatalf("the agent had it after %v", d)
}
fresh(t)
a = &scripted{replies: []reply{{429, "", raccoonWall}}}
b := &scripted{replies: []reply{{200, "", chatOK}}}
scriptedOn(t, "a", provider.Chat, a)
scriptedOn(t, "b", provider.Chat, b)
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"a/m", "b/m"}, Routing: provider.Ordered}); err != nil {
t.Fatal(err)
}
s := New()
for i := range 2 {
code, body := postAs(t, s, "", `{"model":"group/g","messages":[{"role":"user","content":"hi"}]}`)
if code != 200 || !strings.Contains(body, "hello") || a.n != 1 || b.n != i+1 {
t.Fatalf("request %d: %d %s (a %d, b %d)", i+1, code, body, a.n, b.n)
}
}
}