Ark's 429 for a used-up month says "You have exceeded the monthly usage
quota. It will reset at 2026-10-25 23:59:59 +0800 CST." with no
Retry-After. resetsWhen read an offset only after UTC or GMT, so nothing
read it, the key rested quotaRest and was asked again every 15 minutes
until the month turned. Two more limits stood behind that one: a written
reset over 7 days off (longestWritten) isn't read, and a quota rest is
held to 8 days (longestQuota), so wyx1818's reset 9 days off would still
have fallen to the 15-minute loop.
- resetsWhen also reads the time as Go prints one (Ark's): four offset
digits, a space and the zone's name in capitals, then punctuation or
重置. The offset is read, the name isn't (CST is more than one zone).
The UTC/GMT form is unchanged; a bare offset with no zone's name, or
"+08:00" without UTC, isn't read, as Ark doesn't send them.
- monthlyWords (monthly, 月度, 每月, 本月): a refusal that says its
allowance is a month's has its written reset believed up to
longestMonthly (35 days) instead of the week (longestSaid). That covers
Zhipu's 1310 "Weekly/Monthly Limit Exhausted" too (resetsZhipu), the
sibling held to the same week.
- In failQuota the 8-day cap is lifted to 35 days only when the rest
came from the vendor's own reset ("resets") and the refusal is a
month's. A window read full, a Retry-After and the user's own quota
rest are held to 8 days as before.
Ported from skygunner's branch skygunner:fix/volcengine-resets (e6c2b6fb,
posted on both issues). Taken: the monthly trust window, longestMonthly,
monthlyWords, the reporters' bodies as fixtures and the shape of
TestMonthlyWrittenResetRestsTheMonth. Changed:
- its regex made UTC|GMT optional and took any 2 to 5 letters,
case-insensitively, after any offset, for every vendor, so "reset at
<date> +8 now." would read. Scoped here to Go's layout, the zone's
name matched case-sensitively.
- it lifted the cap to 35 days whenever the body said monthly, whatever
the rest came from (a window read full, the user's own rest); here
only for the vendor's written reset.
- its resetsZhipu kept the week; here it shares longestSaid.
vincenty on Discord: no link to another fix is on either issue
(skygunner's commit is authored as "vincent").
Not changed: Ark's plan windows read by its AccessKey (volcPlanQuotas)
still reach only the usage card, not routing (layer 2 of #3251: the
quota is the account's, not one inference key's, and a provider has one
plan, #2849). #3251 stays open for it.
Tests (fixtures: #3251's and #3276's bodies byte for byte, #3276's as
its pretty-printed JSON):
- TestResetsWritten: ark (3 days), ark 9 days off, ark escaped, 35 days
read and 36 not, past, cut after the offset / in the offset / before
the full stop, without the zone, +9900, Ark's form without "monthly"
9 days off (0: the week holds), WorkBuddy's en 9 days off (0: another
vendor unchanged), "retry after … +0800 CST." (0). Every existing
WorkBuddy and UTC/GMT case passes unchanged.
- TestResetsZhipu: 1310 9 days off read, 36 days not; 1308 over a week
off still 0.
- TestMonthlyWrittenResetRestsTheMonth (new): Ark's 429 3d7h and 9d12h
off reads failQuota and rests by "resets" till then, raw and as the
agent's error with keepRetry's note; the same body without "monthly"
rests quotaRest; a month's refusal with no time on an account whose
window reads full 20 days off rests 8 days by "window".
Broken by hand, in the worktree:
- old routing.go with the new tests: TestResetsWritten (ark: 0s, want
79h7m59s; 9 days, escaped, 35 days), TestMonthlyWrittenResetRestsTheMonth
(79h0m0s off: rests 14m59.99s by quota) and TestResetsZhipu (1310, 9
days off: 0s) red.
- cap not lifted: 228h0m0s off rests 191h59m59s by resets, red.
- longestSaid always the week: ark 9 days / 35 days and Zhipu 1310 red.
- zone's name made optional: "ark without the zone" reads 79h7m59s, red.
- skygunner's cap (any monthly body): window 20 days off rests 479h59m,
want 192h, red.
Each restored, green.
Run at 268c1d34c (this change on origin/main 95be6bb75, unchanged at
push): gofmt -l prints nothing; go vet ./..., GOOS=linux go vet -tags
nogui ./..., GOOS=windows go vet ./... clean; go build, GOOS=linux go
build -tags nogui, GOOS=windows go build pass. set -o pipefail; go test
-tags nogui -timeout 45m ./... (no other suite on the machine): 69 ok,
no FAIL in the log (internal/gateway ok 571s).
Not tried: a real Ark key (none here), so not against the live API; the
bodies are the reporters' curl output. Not run: the GUI suite (no GUI
change).
Co-authored-by: skygunner <84759+skygunner@users.noreply.github.com>
162 lines
7.6 KiB
Go
162 lines
7.6 KiB
Go
package gateway
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// Zhipu's 1308 and 1310, in the words its error code pages give (docs.z.ai
|
|
// api-code, docs.bigmodel.cn api-code). zaiPlayer is the refusal as Player
|
|
// quoted it on Discord (2026-10-15 08:25 UTC), its time to the minute.
|
|
const (
|
|
zaiPlayer = "Usage limit reached for 5 hour. Your limit will reset at 2026-10-15 20:12"
|
|
zai1308 = "Usage limit reached for 5 hour. Your limit will reset at 2026-10-15 20:12:13"
|
|
zai1310 = "Weekly/Monthly Limit Exhausted. Your limit will reset at 2026-10-10 21:00:06"
|
|
bigmodel = "已达到 5 小时的使用上限。您的限额将在 2026-10-15 20:12:13 重置"
|
|
)
|
|
|
|
// A Zhipu reset time, written without a zone, is read as Beijing's, in the
|
|
// words of either site and either envelope; one cut short, with a zone of
|
|
// its own, garbled, past, too far off, or another vendor's words, is not
|
|
// read here.
|
|
func TestResetsZhipu(t *testing.T) {
|
|
player := time.Date(2026, 10, 15, 8, 25, 0, 0, time.UTC) // 16:25 in Beijing
|
|
for _, c := range []struct {
|
|
name, body string
|
|
now time.Time
|
|
want time.Duration
|
|
}{
|
|
{"Player's words", zaiPlayer, player, 3*time.Hour + 47*time.Minute},
|
|
{"Player's words, in Z.ai's OpenAI envelope", `{"error":{"code":"1308","message":"` + zaiPlayer + `"}}`, player, 3*time.Hour + 47*time.Minute},
|
|
{"with seconds, in Z.ai's Anthropic envelope", `{"type":"error","error":{"type":"1308","message":"` + zai1308 + `"},"request_id":"x"}`, player, 3*time.Hour + 47*time.Minute + 13*time.Second},
|
|
// as code-yeongyu/senpi#3348 saw it: 39 seconds as UTC+8, 8 hours as UTC
|
|
{"senpi#3348", "Usage limit reached for 5 hour. Your limit will reset at 2026-09-22 01:18:11", time.Date(2026, 9, 21, 17, 17, 32, 0, time.UTC), 39 * time.Second},
|
|
// as kodustech/kodus-ai#2104 saw it, at 11:32:34 UTC
|
|
{"1310", `{"error":{"code":"1310","message":"` + zai1310 + `"}}`, time.Date(2026, 10, 9, 11, 32, 34, 0, time.UTC), 25*time.Hour + 27*time.Minute + 32*time.Second},
|
|
{"bigmodel.cn", `{"error":{"code":"1308","message":"` + bigmodel + `"}}`, player, 3*time.Hour + 47*time.Minute + 13*time.Second},
|
|
{"bigmodel.cn escaped", escaped(bigmodel, "1308"), player, 3*time.Hour + 47*time.Minute + 13*time.Second},
|
|
// a Remote magpie's held refusal, read by the magpie asking it
|
|
{"held by a Remote magpie", "Z.ai: " + zai1308 + " (out of its allowance: not asked again before 20:12 UTC+8)", player, 3*time.Hour + 47*time.Minute + 13*time.Second},
|
|
// cut short
|
|
{"cut in the seconds", zai1308[:len(zai1308)-1], player, 0},
|
|
{"cut after the minute's colon", zai1308[:len(zai1308)-2], player, 0},
|
|
{"cut in the minute", zaiPlayer[:len(zaiPlayer)-1], player, 0},
|
|
{"cut in the date", zaiPlayer[:strings.Index(zaiPlayer, " 20:12")], player, 0},
|
|
// a zone of its own is resetsWritten's to read, never taken as Beijing's
|
|
{"its own UTC", zai1308 + " UTC", player, 0},
|
|
{"its own offset", zai1308 + "+00:00", player, 0},
|
|
{"its own Z", zai1308 + "Z", player, 0},
|
|
// garbled, past, too far off
|
|
{"month 13", strings.Replace(zai1308, "2026-10-15", "2026-13-15", 1), player, 0},
|
|
{"hour 25", strings.Replace(zai1308, "20:12:13", "25:12:13", 1), player, 0},
|
|
{"past", zai1308, time.Date(2026, 10, 15, 12, 13, 0, 0, time.UTC), 0},
|
|
{"over a week off", zai1308, time.Date(2026, 10, 7, 0, 0, 0, 0, time.UTC), 0},
|
|
// 1310 is a week's or a month's: a month's reset is believed
|
|
// weeks off, as Ark's is (#3276)
|
|
{"1310, 9 days off", zai1310, time.Date(2026, 10, 1, 13, 0, 6, 0, time.UTC), 9 * 24 * time.Hour},
|
|
{"1310, 36 days off", zai1310, time.Date(2026, 9, 4, 13, 0, 6, 0, time.UTC), 0},
|
|
// another vendor's words, without a zone: not taken as Beijing's
|
|
{"WorkBuddy without its offset", strings.Replace(wbZh, " UTC+8", "", 1), player, 0},
|
|
{"a date in passing", "this model is retired at 2026-10-15 20:12:13", player, 0},
|
|
} {
|
|
if got := resetsZhipu([]byte(c.body), c.now); got != c.want {
|
|
t.Errorf("%s: %v, want %v", c.name, got, c.want)
|
|
}
|
|
if c.want > 0 {
|
|
if got := resetsAt([]byte(c.body), c.now); got != c.want {
|
|
t.Errorf("%s: resetsAt %v, want %v", c.name, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// heldClock gives the time with its offset, so a reader on another clock
|
|
// can tell whose it is. Each zone is passed in, never set on time.Local:
|
|
// time.Now reads time.Local, and a goroutine an earlier test left running
|
|
// read it while this test wrote it, a race on ubuntu CI (run 39242954115).
|
|
func TestHeldClock(t *testing.T) {
|
|
cst := time.FixedZone("CST", 8*3600)
|
|
now := time.Date(2026, 10, 15, 16, 25, 0, 0, cst)
|
|
for _, c := range []struct {
|
|
zone *time.Location
|
|
at time.Time
|
|
want string
|
|
}{
|
|
{cst, time.Date(2026, 10, 15, 20, 12, 13, 0, cst), "20:12 UTC+8"},
|
|
{cst, time.Date(2026, 10, 16, 9, 0, 0, 0, cst), "Oct 16 09:00 UTC+8"},
|
|
{time.UTC, time.Date(2026, 10, 15, 20, 12, 13, 0, cst), "12:12 UTC+0"},
|
|
{time.FixedZone("", -(3*3600 + 30*60)), time.Date(2026, 10, 15, 20, 12, 13, 0, cst), "08:42 UTC-3:30"},
|
|
} {
|
|
got := heldClockIn(c.at, now, c.zone)
|
|
if got != c.want {
|
|
t.Errorf("%v in %v: %q, want %q", c.at, c.zone, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Player on Discord: a Z.ai key refused with 1308, which names 20:12 in
|
|
// Beijing's time, rested a quarter of an hour, and the refusal given again
|
|
// said "not asked again before 17:12", a time of no zone the vendor never
|
|
// named. It rests until the time Z.ai named, and says it with its offset.
|
|
func TestZhipuRestsUntilItsReset(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()
|
|
t.Cleanup(func() {
|
|
restingUntil.Lock()
|
|
restingUntil.m, restingUntil.note = map[string]time.Time{}, map[string]Rest{}
|
|
restingUntil.Unlock()
|
|
})
|
|
// three hours on, to the second, as Z.ai writes it
|
|
reset := time.Now().Add(3 * time.Hour).Truncate(time.Second)
|
|
said := "Usage limit reached for 5 hour. Your limit will reset at " + reset.In(zhipuZone).Format("2006-01-02 15:04:05")
|
|
asked := 0
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
io.Copy(io.Discard, r.Body)
|
|
asked++
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusTooManyRequests)
|
|
io.WriteString(w, `{"error":{"code":"1308","message":"`+said+`"}}`)
|
|
}))
|
|
t.Cleanup(up.Close)
|
|
p := provider.Provider{ID: "mockzai", Name: "Z.ai", Chat: up.URL + "/v1", Models: []string{"glm-5.2"}, Key: "k"}
|
|
if err := provider.Save(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
srv := New()
|
|
post := func() *httptest.ResponseRecorder {
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"mockzai/glm-5.2","messages":[{"role":"user","content":"hi"}]}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
srv.Handler().ServeHTTP(rec, req)
|
|
return rec
|
|
}
|
|
want := int(time.Until(reset) / time.Second)
|
|
for i := range 2 {
|
|
rec := post()
|
|
if rec.Code != http.StatusTooManyRequests {
|
|
t.Fatalf("request %d: %d %s", i, rec.Code, rec.Body)
|
|
}
|
|
if ra, err := strconv.Atoi(rec.Header().Get("Retry-After")); err != nil || ra < want-10 || ra > want+1 {
|
|
t.Errorf("request %d: Retry-After %q, want about %d (Z.ai's own time)", i, rec.Header().Get("Retry-After"), want)
|
|
}
|
|
if i == 1 {
|
|
if !strings.Contains(rec.Body.String(), "not asked again before "+heldClock(reset, time.Now())) {
|
|
t.Errorf("held refusal doesn't name Z.ai's time with its offset: %s", rec.Body)
|
|
}
|
|
}
|
|
}
|
|
if asked != 1 {
|
|
t.Errorf("Z.ai asked %d times, want once: the second is held until its reset", asked)
|
|
}
|
|
}
|