gateway: ZCode's account-wide 429 (its [1302] label) is a wall, not a moment's rate (#5406)

ZCode's account-wide 429 "[1302][您的账户已达到速率限制,请您控制请求频率][id]"
read as a rate limit that passes: the same-one retries (passing) asked it
again 1/2/4s apart and a patience's rounds sent the whole request again
every 2s..30s — "magpie keeps asking, so the 429 comes back in a loop"
(#5406, erlinerd; two further accounts on v0.1.3216). Rested and retried,
the 429 came back: no moment clears it.

walled now also matches the ZCode plugin's own naming of the refusal,
the vendor's three-part label [1302][its words][its request id] (its
words whole, a 16-hex id). The next account is asked, or the agent has
the error at once, as wallCodes says.

Review change: the patch also added the bare code "1302" to wallCodes,
for every vendor. Zhipu's own API answers 1302 for a concurrency or
minute limit ("并发数过高", or "您的账户已达到速率限制" with no label),
which a moment does clear, so a GLM key would have lost its in-place
retries. The bare code stays a rate limit that passes; only the label's
shape walls. The comment's claim that the 并发数过高 reading "never
reaches here" is gone. Two Zhipu bodies are added to the test's
"still passes" cases.

Run (wsl-run, magpie-desktop, Go 1.26.8):
- go test -count=1 -run 'Wall|MinuteLimit|RateLimit|RestRetry|Patience|Busy|FailureReads|PassErrors|Zhipu|ZCode' ./internal/gateway: ok 21.9s
- the same Wall|RateLimit|Patience|Busy set with -race: ok
- gofmt -l: clean
Broken by hand:
- main's fallback.go: TestZCodeWallIsNotRetriedInPlace red ("the wall
  passes: asked again in place", "the wall is busy") on both labelled bodies.
- the patch's own fallback.go (bare 1302 in wallCodes): red on both
  Zhipu bodies ("doesn't pass", "not busy").
Not tried: a real ZCode account's 429, and whether the next account is
asked on a live rotation.

Reported-by: erlinerd
Co-authored-by: ggbdpq <ggbdpq@gmail.com>
This commit is contained in:
yetone
2026-11-07 21:55:41 +08:00
co-authored by ggbdpq
parent 8373aaf14a
commit 9700dd7cdc
2 changed files with 64 additions and 2 deletions
+18 -2
View File
@@ -2156,6 +2156,19 @@ func passing(status int, header http.Header, body []byte, again int) (time.Durat
// the agent has the error at once.
var wallCodes = map[string]bool{"user_model_rate_limited": true}
// zcodeRateWall is ZCode's account-wide 429, the same bucket as
// wallCodes' (#5406, erlinerd): rested and retried, the 429 came back, and
// two further accounts sat there while magpie kept asking (yetone on two
// Discord reports, v0.1.3216). It is matched by the ZCode plugin's own
// naming of the refusal, the vendor's three-part label `[1302][<its
// words>][<its request id>]`, its words whole — the shape the two log
// lines of #5406 show — never by the bare code: Zhipu's own API answers
// 1302 for a concurrency or minute limit ("并发数过高", "您的账户已达到速率限制"
// with no label), which a moment does clear, and a GLM key asked again
// in place stays so. The words and the id together are the label's own,
// not words anywhere in the body a prompt may quote.
var zcodeRateWall = regexp.MustCompile(`\[1302\]\[您的账户已达到速率限制[^\]]*\]\[[0-9a-fA-F]{16,}\]`)
// walled says whether body is a refusal whose error.code is one of
// wallCodes: the code where the vendor puts it, not the words anywhere in
// the body (a prompt or a message quoting it).
@@ -2165,11 +2178,14 @@ func walled(body []byte) bool {
Code json.RawMessage `json:"code"`
} `json:"error"`
}
if json.Unmarshal(body, &e) != nil {
if err := json.Unmarshal(body, &e); err != nil {
return false
}
var code string
return json.Unmarshal(e.Error.Code, &code) == nil && wallCodes[code]
if json.Unmarshal(e.Error.Code, &code) == nil && wallCodes[code] {
return true
}
return zcodeRateWall.Match(wordsOf(body))
}
// withRoom is, of the candidates left, those another member than c's may
+46
View File
@@ -12,6 +12,52 @@ import (
// throughput used up, word for word. It has no Retry-After.
const raccoonWall = `{"error":{"code":"user_model_rate_limited","message":"您的模型请求频率超限,请稍后重试","type":"rate_limit_error"}}`
// ZCode's account-wide 429 (#5406, erlinerd): the vendor's own error
// carries the code where it puts it, and the plugin names the same code
// in the message it shows, word for word from the two log shapes —
// `[1302][<the vendor's words>][<its request id>]`. Rested and retried,
// the 429 came back: no moment clears it, so it is a wall.
const zcodeWallCode = `{"error":{"code":"1302","message":"[1302][您的账户已达到速率限制,请您控制请求频率][202611051732017a8c6e62fa7d42f5]"}}`
const zcodeWallMessage = `{"error":{"message":"[1302][您的账户已达到速率限制,请您控制请求频率][20261105173006e812537059cd4a83]"}}`
// ZCode's wall is not asked again in place, by the same-one retries or a
// patience's rounds: the loop the reporter saw ("magpie keeps asking, so
// the 429 comes back in a loop") is these two. A quoted `[1302]` without
// the label's shape, and a code of ZCode's that nothing pins the semantics
// of yet, are a rate limit that passes, as before.
func TestZCodeWallIsNotRetriedInPlace(t *testing.T) {
for _, body := range []string{zcodeWallCode, zcodeWallMessage} {
if got := failure(429, []byte(body)); got != failRate {
t.Fatalf("failure: %s, want %s", got, failRate)
}
if _, ok := passing(429, nil, []byte(body), 0); ok {
t.Errorf("the wall passes: asked again in place: %s", body)
}
if busy(429, []byte(body)) {
t.Errorf("the wall is busy: asked again in a patience's rounds: %s", body)
}
}
for _, body := range []string{
// the label's words quoted in a prompt's echo are no wall
`{"error":{"message":"[1302] is the code my request got, why?"}}`,
// the label's shape with a code of another meaning
`{"error":{"message":"[1302][请求成功][20261105173006e812537059cd4a83]"}}`,
// Zhipu's own 1302, a concurrency or minute limit with no
// ZCode label: a moment clears it, as before
`{"error":{"code":"1302","message":"您的账户已达到速率限制,请您控制请求频率"}}`,
`{"error":{"code":"1302","message":"您当前使用该API的并发数过高,请降低并发,或联系客服增加限额。"}}`,
// a neighbour code nothing pins the semantics of yet
`{"error":{"code":"1303","message":"[1303][请求过多][20261105173006e812537059cd4a83]"}}`,
} {
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 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.