diff --git a/internal/gateway/fallback.go b/internal/gateway/fallback.go index 0ccb3dbab..48451f41e 100644 --- a/internal/gateway/fallback.go +++ b/internal/gateway/fallback.go @@ -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 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 diff --git a/internal/gateway/token_wall_test.go b/internal/gateway/token_wall_test.go index d96abb5b9..8f9418271 100644 --- a/internal/gateway/token_wall_test.go +++ b/internal/gateway/token_wall_test.go @@ -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][][]`. 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.