Files
magpie/internal/gateway/credit_400_test.go
T
yetone 3627742104 gateway: an error read as out of credit moves on at any 4xx (#4095)
mycf_711 (#4095): SuperMagic answered an account with no points left
400 "非常抱歉,当前对话需要调用大模型 / 付费工具,您的可用积分已不足。
您可以点击右上角「用户头像」,通过「充值」通道补充积分…". failure() read
it as failCredit (creditWords: 充值), but retryable()'s 400/422 branch
asked quotaWords only, which has neither 积分, 不足 nor 充值. So
hw.failed() was false: the 400 went to the agent while another account
in the order had 6,467 points, and the empty account was never rested.

retryable() now takes any 4xx failure() reads as failCredit, after the
overflow check, so the two deciders agree on credit at every status
(400, 422, and the 4xx outside the always-retried list, 409 say).
Sibling: shapeRefused() kept out quotaWords but not creditWords, so a
shape refusal saying 请充值 would rest nobody; it now keeps out both.
Quota: usedUpWords (failQuota) is a subset of quotaWords, so 400/422
already agreed; a quota word at another 4xx (413's "exceeded the
limit" is the request's size) is left alone on purpose.

Tests (credit_400_test.go), with the reporter's literal body:
TestCreditRefusalIsRetryableAtAnyStatus (400/409/422, raw and as magpie
tells it on, plus shapeRefused) and TestCredit400MovesToTheNextAccount
(two keys: the empty one 400s, the next answers 200, the empty one
rests as credit 400, the next request goes straight past it).
docs/subsystems/gateway-routing.md says it.

Broken by hand: with the new case disabled and creditWords taken out
of shapeRefused, both tests are red (every status not retryable, the
shape case rests nobody, the request ends 400 after k-empty alone).
Run on mote, at 157d575c3 with both #4095 commits on it (the pushed tree):
go test -count=1 -timeout 45m ./internal/gateway/... ./internal/routes/...
ok (gateway 781s, routes 0.4s); gofmt -l internal/gateway internal/routes
empty; GOOS=windows and GOOS=linux go vet ./internal/gateway/...
./internal/routes/... ok. The same was green at the earlier bases
40f206dff and 7a7931833. Pushed on 99299c7b1, whose commits since
157d575c3 change only internal/edit's tests; not run again there.
Not tried: a real SuperMagic account at 0 points (the plugin 0.3.26
already turns this 400 into 402 there).
2026-10-29 01:14:42 +08:00

114 lines
4.1 KiB
Go

package gateway
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// superMagicBroke is SuperMagic's answer to an account whose points ran
// out, word for word as mycf_711's trace showed it (#4095): a 400, which
// failure reads as out of credit ("充值") while none of quotaWords is in it.
const superMagicBroke = "非常抱歉,当前对话需要调用大模型 / 付费工具,您的可用积分已不足。\n您可以点击右上角「用户头像」,通过「充值」通道补充积分,完成后即可继续对话。"
func superMagicBrokeBody() string {
b, _ := json.Marshal(map[string]any{"error": map[string]any{"message": superMagicBroke}})
return string(b)
}
// An error failure reads as out of credit is one another account may
// answer, whatever its 4xx: retryable and failure agree, and a shape
// refusal that also says to top up is the credit it says (#4095).
func TestCreditRefusalIsRetryableAtAnyStatus(t *testing.T) {
for _, body := range []string{
superMagicBrokeBody(),
"超级麦吉: " + superMagicBroke, // as magpie tells it on
} {
for _, status := range []int{400, 409, 422} {
if f := failure(status, []byte(body)); f != failCredit {
t.Fatalf("%d: failure %q, want credit", status, f)
}
if !retryable(status, []byte(body)) {
t.Errorf("%d %s: not retryable, though failure reads it as out of credit", status, body)
}
}
}
if shapeRefused(400, []byte(`{"error":{"message":"Unknown parameter: 'x'. 请充值后重试"}}`)) {
t.Errorf("a shape refusal that says to top up rests nobody")
}
// what isn't credit is as before
if retryable(400, []byte(`{"error":{"message":"messages: field required"}}`)) {
t.Errorf("a plain bad request is retryable")
}
}
// mycf_711 (#4095): of three SuperMagic accounts, the one asked answered
// 400 「可用积分已不足」 and that went to the agent, though another had
// 6,467 points. The next account is asked and answers, and the empty one
// rests as out of credit, so the next request goes past it.
func TestCredit400MovesToTheNextAccount(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
restingUntil.Lock()
restingUntil.m, restingUntil.note = map[string]time.Time{}, map[string]Rest{}
restingUntil.Unlock()
routed.Lock()
routed.failures = map[string]int{}
routed.Unlock()
var mu sync.Mutex
var seen []string
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
key := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
mu.Lock()
seen = append(seen, key)
mu.Unlock()
w.Header().Set("Content-Type", "application/json")
if key == "k-empty" {
w.WriteHeader(400)
io.WriteString(w, superMagicBrokeBody())
return
}
io.WriteString(w, `{"id":"from-`+key+`","choices":[]}`)
}))
defer up.Close()
p := provider.Provider{ID: "magicrew", Name: "超级麦吉", Chat: up.URL + "/v1", Models: []string{"m1"},
Routing: provider.Ordered, Key: "k-empty", KeyName: "Empty", Keys: []provider.KeyAccount{{Name: "Full", Key: "k-full"}}}
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
saved, err := provider.Find("magicrew")
if err != nil {
t.Fatal(err)
}
s := New()
send := func() (int, string) {
rec := httptest.NewRecorder()
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/chat/completions",
strings.NewReader(`{"model":"magicrew/m1","messages":[{"role":"user","content":"hi"}]}`)))
return rec.Code, rec.Body.String()
}
if code, body := send(); code != 200 || !strings.Contains(body, "from-k-full") {
t.Fatalf("%d %s; tried %v", code, body, seen)
}
if strings.Join(seen, ",") != "k-empty,k-full" {
t.Fatalf("tried %v", seen)
}
r, ok := RestOf(saved.RestKey(saved.KeyList()[0]))
if !ok || r.Why != failCredit || r.Status != 400 {
t.Fatalf("the empty account's rest: %+v %v", r, ok)
}
mu.Lock()
seen = nil
mu.Unlock()
if code, body := send(); code != 200 || !strings.Contains(body, "from-k-full") || strings.Join(seen, ",") != "k-full" {
t.Fatalf("next: %d %s, tried %v", code, body, seen)
}
}