PaulT on Discord #feedback: an OpenAI API-key provider answering 402 out of credit showed "Resting · 402 out of credit · back at 04:10 PM" and was asked again every 30 minutes, refused the same each time.1534474c8gave ChatGPT accounts a rest until their credits are back; keys still had the flat 30-minute creditRest. An API key (Account == nil) whose vendor says it is out of money now climbs creditLadder: 30m, 2h, 8h, 24h, 3d, then 7d each time after. Refused again while it still rests (the lone key, asked all the same) keeps its rung; a rest that ended and is refused again goes a rung up. "Try now" (Unrest) and an answer (servedCandidate) start it over at 30m. It covers what failure() already calls failCredit: a bare 402, OpenAI's insufficient_quota (its billing errors are 429s with type insufficient_quota and codes credit_balance_exhausted, organization_/project_spend_limit_exceeded, organization_usage_limit_exceeded, per developers.openai.com/api/docs/guides/error-codes), DeepSeek's "Insufficient Balance" 402, Anthropic's "credit balance is too low" 400, Zhipu's 余额不足 and WorkBuddy's exhausted 429. What stays as before: a refusal that names when it is back (resets_at, a reset header) rests till then, capped at longestMonthly. A 402 that can pass soon or for a smaller request rests creditRest flat and never climbs: one with Retry-After (OpenRouter's openrouter_in_flight_budget, per openrouter.ai/docs/api-reference/errors), OpenRouter's "can only afford" / "fewer max_tokens", and "try again in N" / "N 分钟后" (creditSoon). Subscription accounts other than ChatGPT's keep 30m flat, and another computer's magpie rests the model only, flat (TestCreditLadderIsKeysOnly). Run (mote-run; first at this change on origin/maind31710c71): - go test -count=1 -timeout 45m ./internal/gateway ./internal/gui: internal/gateway red only on TestAntigravityTurnsCodexsGPT5ThreadAwayOnce, TestAntigravityTurnedAwayIsForeseen, ...LastIsNoQuota, ...InsideTheReplyLast and TestCodexSpentGrokFailingIsTold, each red the same on origin/maind31710c71(#4362), and TestBurstTellsNoSpeed under load (#2719); internal/gui red only on the real-window AppKit tests killed at their 20-40s deadline under mote's load (TestDockJoinLeadsCommandTab, TestPanelCloseHidesIt, TestMainWindowCorners, TestCoveredWindowStaysUp, TestTrayCellClickReleasedPanel and others: #2645, #4125, #1936, #3178); the same five red at origin/maind31710c71alone; TestBurstTellsNoSpeed passed alone on both; TestCoreScriptBudget passed - earlier, -run 'Credit|Rest|Quota|...' ./internal/gateway: red only on TestAntigravityTurnedAwayLastIsNoQuota, red the same on origin/maind31710c71, filed as #4362. - rebased on764a032f6(c37b8b659's remote-magpie quota change among it; the routing doc line conflicted and was merged by hand): go vet ./internal/gateway native, GOOS=linux, GOOS=windows ok; -run 'Credit|Rest|Quota|Remote|Magpie|Unrest|Codex|Spent|Broke|Balance|Afford' ./internal/gateway red only on #4362's tests. Then rebased again on the 20 commits after it (46fb0c1b9) with no conflict; there go vet ./internal/gateway and the three new tests pass. - gofmt -l clean; go vet ./internal/gateway native, GOOS=linux and GOOS=windows. - The full go test ./... is still to come: mote was running other sessions' full suites. Any red it shows gets an issue. Broken by hand, each red: - the credit branch in restAfterMarked off: "refusal 2: rests 30m0s ... want 2h0m0s". - the rung climb after an ended rest (else if had) off: red. - creditSoon off: OpenRouter's "can only afford" climbs ("failures 1 ... want flat"). Not tried: PaulT's own 402 body (not known; the test holds OpenAI's documented bodies and the vendors' bodies magpie's tests already had). No real out-of-credit key was asked. A key's balance reading doesn't lift its rest early; "Try now" does. The ladder is in memory, so a restart of magpie starts it over at 30 minutes.
173 lines
8.5 KiB
Go
173 lines
8.5 KiB
Go
package gateway
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// Out-of-money refusals as vendors send them. OpenAI's: its legacy body
|
|
// (type and code insufficient_quota, the message every client has seen),
|
|
// and its billing codes (developers.openai.com/api/docs/guides/error-codes:
|
|
// credit_balance_exhausted, organization_/project_spend_limit_exceeded,
|
|
// organization_usage_limit_exceeded, each a 429 whose type is still
|
|
// insufficient_quota; the page gives the cause, not the message, so the
|
|
// message here is the cause it gives). The others are the bodies magpie's
|
|
// tests already hold from their reporters.
|
|
const (
|
|
openAIQuotaLegacy = `{"error":{"message":"You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors.","type":"insufficient_quota","param":null,"code":"insufficient_quota"}}`
|
|
openAICreditsGone = `{"error":{"message":"Your organization has no prepaid credits remaining.","type":"insufficient_quota","param":null,"code":"credit_balance_exhausted"}}`
|
|
openAISpendLimit = `{"error":{"message":"Your project reached its enforced spend limit.","type":"insufficient_quota","param":null,"code":"project_spend_limit_exceeded"}}`
|
|
deepSeekNoBalance = `{"error":{"message":"Insufficient Balance","type":"unknown_error","param":null,"code":"invalid_request_error"}}`
|
|
anthropicTooLow = `{"type":"error","error":{"type":"invalid_request_error","message":"Your credit balance is too low to access the Anthropic API. Please go to Plans & Billing to upgrade or purchase credits."}}`
|
|
// OpenRouter: the request's size, not the balance spent
|
|
openRouterAfford = `{"error":{"message":"This request requires more credits, or fewer max_tokens. You requested up to 65536 tokens, but can only afford 12000. To increase, visit https://openrouter.ai/settings/credits and add more credits","code":402,"metadata":{"provider_name":null}}}`
|
|
// OpenRouter: its in-flight budget, a 402 sent with Retry-After
|
|
openRouterInFlight = `{"error":{"code":402,"message":"Insufficient credits","metadata":{"limit_source":"openrouter_in_flight_budget"}}}`
|
|
)
|
|
|
|
// expire ends id's rest as if its time had passed, its note kept.
|
|
func expire(id string) {
|
|
restingUntil.Lock()
|
|
restingUntil.m[id] = time.Now().Add(-time.Second)
|
|
restingUntil.Unlock()
|
|
}
|
|
|
|
// PaulT on Discord: an OpenAI key out of credit answered 402 and was
|
|
// asked again, refused the same, every 30 minutes. A key its vendor says
|
|
// is out of money now rests longer each time it is refused so again,
|
|
// 30 minutes to a week; "Try now" and an answer start it over.
|
|
func TestKeyOutOfCreditRestsLongerEachTime(t *testing.T) {
|
|
near := func(d, want time.Duration) bool { return d > want-time.Minute && d <= want }
|
|
cases := []struct {
|
|
name string
|
|
status int
|
|
body string
|
|
}{
|
|
{"bare 402", http.StatusPaymentRequired, ""},
|
|
{"openai legacy", http.StatusTooManyRequests, openAIQuotaLegacy},
|
|
{"openai credits", http.StatusTooManyRequests, openAICreditsGone},
|
|
{"openai spend limit", http.StatusTooManyRequests, openAISpendLimit},
|
|
{"openai-shaped 402", http.StatusPaymentRequired, openAICreditsGone},
|
|
{"deepseek", http.StatusPaymentRequired, deepSeekNoBalance},
|
|
{"anthropic", http.StatusBadRequest, anthropicTooLow},
|
|
{"zhipu", http.StatusTooManyRequests, zhipuNoBalance},
|
|
{"workbuddy", http.StatusTooManyRequests, wbExhausted},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
fresh(t)
|
|
s := New()
|
|
key := candidate{p: provider.Provider{ID: "openai", Key: "sk-x"}, model: "gpt-x", rest: "openai#k"}
|
|
id := key.restKey()
|
|
refuse := func() Rest { return s.restAfter(key, tc.status, http.Header{}, []byte(tc.body)) }
|
|
for i, want := range creditLadder {
|
|
r := refuse()
|
|
if r.Why != failCredit || r.By != "credit" || r.Failures != i+1 || !near(time.Until(r.Until), want) {
|
|
t.Fatalf("refusal %d: rests %v (%s, by %s, failures %d), want %v", i+1, time.Until(r.Until).Round(time.Second), r.Why, r.By, r.Failures, want)
|
|
}
|
|
// refused again while it rests (the lone key, asked all the
|
|
// same): the same rung
|
|
if r := refuse(); r.Failures != i+1 || !near(time.Until(r.Until), want) {
|
|
t.Fatalf("refusal %d again while resting: %v, failures %d", i+1, time.Until(r.Until).Round(time.Second), r.Failures)
|
|
}
|
|
expire(id)
|
|
}
|
|
// the top rung holds
|
|
if r := refuse(); !near(time.Until(r.Until), creditLadder[len(creditLadder)-1]) {
|
|
t.Fatalf("past the ladder: %v", time.Until(r.Until).Round(time.Second))
|
|
}
|
|
// "Try now": lifted, and refused again it starts over
|
|
if !s.Unrest(id) {
|
|
t.Fatal("Try now lifted nothing")
|
|
}
|
|
if _, ok := restOf(id); ok {
|
|
t.Fatal("still resting after Try now")
|
|
}
|
|
if r := refuse(); r.Failures != 1 || !near(time.Until(r.Until), creditRest) {
|
|
t.Fatalf("after Try now: %v, failures %d", time.Until(r.Until).Round(time.Second), r.Failures)
|
|
}
|
|
expire(id)
|
|
refuse() // 2h
|
|
// an answer clears the count
|
|
servedCandidate(key, 1)
|
|
if r := refuse(); r.Failures != 1 || !near(time.Until(r.Until), creditRest) {
|
|
t.Fatalf("after an answer: %v, failures %d", time.Until(r.Until).Round(time.Second), r.Failures)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A 402 that can pass soon, or for a smaller request, rests creditRest
|
|
// each time, as before; one that names when it is back rests till then.
|
|
func TestKeyCreditSoonDoesNotClimb(t *testing.T) {
|
|
near := func(d, want time.Duration) bool { return d > want-time.Minute && d <= want }
|
|
for _, tc := range []struct {
|
|
name string
|
|
status int
|
|
header http.Header
|
|
body string
|
|
}{
|
|
{"openrouter afford", http.StatusPaymentRequired, http.Header{}, openRouterAfford},
|
|
{"openrouter in-flight", http.StatusPaymentRequired, http.Header{"Retry-After": {"20"}}, openRouterInFlight},
|
|
{"try again in seconds", http.StatusTooManyRequests, http.Header{}, `{"error":{"message":"Insufficient balance. Please try again in 30 seconds"}}`},
|
|
{"一分钟后重试", http.StatusTooManyRequests, http.Header{}, `{"error":{"message":"余额不足,请充值后 1 分钟后重试"}}`},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
fresh(t)
|
|
s := New()
|
|
key := candidate{p: provider.Provider{ID: "or", Key: "k"}, model: "m", rest: "or#k"}
|
|
for i := range 4 {
|
|
r := s.restAfter(key, tc.status, tc.header, []byte(tc.body))
|
|
// creditRest, or the shorter time it names (resetsAt
|
|
// reads "try again in 30 seconds"): never a rung
|
|
if r.Why != failCredit || r.Failures != 0 || time.Until(r.Until) > creditRest {
|
|
t.Fatalf("refusal %d: %v (%s, by %s, failures %d), want %v at most, flat", i+1, time.Until(r.Until).Round(time.Second), r.Why, r.By, r.Failures, creditRest)
|
|
}
|
|
expire(key.restKey())
|
|
}
|
|
})
|
|
}
|
|
|
|
// a spend limit that says when it resets is out till then
|
|
fresh(t)
|
|
s := New()
|
|
key := candidate{p: provider.Provider{ID: "k", Key: "k"}, model: "m", rest: "k#k"}
|
|
at := time.Now().Add(10 * 24 * time.Hour).Unix()
|
|
body := `{"error":{"type":"insufficient_quota","code":"project_spend_limit_exceeded","message":"x","resets_at":` + strconv.FormatInt(at, 10) + `}}`
|
|
if r := s.restAfter(key, http.StatusTooManyRequests, http.Header{}, []byte(body)); r.By != "resets" || !near(time.Until(r.Until), 10*24*time.Hour) {
|
|
t.Fatalf("named reset: %v by %s", time.Until(r.Until).Round(time.Second), r.By)
|
|
}
|
|
}
|
|
|
|
// What doesn't climb: a subscription account other than ChatGPT's, and
|
|
// another computer's magpie, whose model rests for its own provider there.
|
|
func TestCreditLadderIsKeysOnly(t *testing.T) {
|
|
near := func(d, want time.Duration) bool { return d > want-time.Minute && d <= want }
|
|
fresh(t)
|
|
s := New()
|
|
other := candidate{p: provider.Provider{ID: "claude", Account: &provider.Account{Agent: "claude", User: "me@example.com"}}, model: "claude-x", rest: "claude@me@example.com"}
|
|
for range 3 {
|
|
if r := s.restAfter(other, http.StatusPaymentRequired, http.Header{}, nil); r.Failures != 0 || !near(time.Until(r.Until), creditRest) {
|
|
t.Fatalf("account: %v failures %d", time.Until(r.Until).Round(time.Second), r.Failures)
|
|
}
|
|
expire(other.restKey())
|
|
}
|
|
h := http.Header{answeredThereHeader: {"openai"}}
|
|
remote := candidate{p: provider.Provider{ID: "lan", Key: "k"}, model: "m", rest: "lan#k"}
|
|
for range 3 {
|
|
r := s.restAfter(remote, http.StatusPaymentRequired, h, []byte(deepSeekNoBalance))
|
|
if r.Failures != 0 || !near(time.Until(r.Until), creditRest) {
|
|
t.Fatalf("remote magpie: %v failures %d", time.Until(r.Until).Round(time.Second), r.Failures)
|
|
}
|
|
if !strings.Contains(r.Key, "/") {
|
|
t.Fatalf("remote magpie rests %q, not its model", r.Key)
|
|
}
|
|
expire(r.Key)
|
|
}
|
|
}
|