Files
magpie/internal/gateway/overflow_cap_test.go
T
yetone cfcd6648fc gateway: Chinese that says the input is too long is an overflow, Chinese pace words a rate limit (#4420)
A group member answered 400 "内容太长了,请精简输入或开启新对话" once a
turn passed ~730K on a model it declares at 1M. tooLongRe's Chinese only
matched wordings that name 上下文, so the try was filed as a plain
failure that rested the member, and the group's other members were never
asked. Reported by MYCF711 (Discord, issue #4420).

tooLongRe gets a third line for Chinese that names what was sent (内容,
输入, 文本, 提示词/prompt, 问题, 对话, 消息) beside its length: "…太长/过长/
超长", "…超过/超出…长度/token/字数", "输入长度/token…超过", and
"字数太多/过多". Tied to: the reporter's vendor, z.ai's 1261 "Prompt 超长",
iFlytek Spark's 10907 "token数量超过上限。对话历史+问题的字数太多,需要精简
输入". Left out on purpose: bare "超出最大长度" (a field's length), bare
"token数量超过上限" (a day's allowance is said so too), "输出过长" (the
reply), and "请求体过大" (a 413 byte limit, already handled by status).

paceWords gets Chinese pace words (速率, 频繁, 频率, 限流, 并发, 每秒/
分钟/小时/天/日, 稍后再试/重试), so "内容太长,已达到速率限制" is a rate
limit, not a size. rateWords gains 过于频繁/速率 and quotaWords 频繁/速率,
so such a 400 is asked of the next account and such a 429 is failRate.

Tests:
- TestEveryVendorsOverflow: 8 Chinese rows (red on the old code, each).
- TestChineseNotTooLong (new): pace-with-size, a field's length, the
  reply's length, a day's token cap and plain errors aren't overflows; the
  pace 400 is retryable.
- TestChineseInputTooLongGoesToTheNextMember (new): two-member group, the
  reporter's literal words, raw and \u-escaped: member b answers, failOverflow,
  nobody rests (red on the old code: a 1, b 0, 400 to the agent).
- docs/subsystems/gateway-routing.md names the new wordings.

Run (mote, via mote-run, at this tree):
- go test ./internal/gateway -count=1 -timeout 45m: ok 500.9s
- go vet ./internal/gateway, GOOS=linux and GOOS=windows: clean; gofmt -l clean
- Broken by hand: old gateway.go/routing.go/fallback.go with the new
  tests: all three tests red (11 failures). paceWords alone reverted:
  TestChineseNotTooLong red on 4 pace messages and the retryable check.

Not tried: the reporter's real vendor (unknown which member it is), nor a
real z.ai or iFlytek account; the wordings come from their docs. Only
internal/gateway was run, not the whole repo; the change is inside that
package's classifiers.
2026-10-30 17:52:14 +08:00

96 lines
4.3 KiB
Go

package gateway
import (
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// A relay's own cap on one request's input is a refusal over the input's
// size, like a model's window: the next member of a group, another vendor,
// may hold it (#2445, worksyang: B.ai's "Input token exceed the limit" on a
// 380K request, under the model's 1M window, failed the whole group, its
// other three members never asked). It goes the way an overflow goes
// (#700): the next member with room is asked, nobody rests, and another
// account of the same model isn't. A request the vendor can't read still
// isn't asked of anyone else, nor is a rate limit worded with input tokens
// taken for one.
func TestRelayInputCapGoesToTheNextMember(t *testing.T) {
// the vendor's words as the reporter's routing log has them, after
// magpie's "B.ai: "; the JSON around them is not the reporter's
const capped = "Input token exceed the limit (request id: 2026…)"
if !tooLong(400, "B.ai: "+capped) {
t.Errorf("%q isn't taken for a request too long", capped)
}
if !overflowed(400, []byte(`{"error":{"message":"`+capped+`","type":"invalid_request_error"}}`)) {
t.Errorf("%q in an error body isn't an overflow", capped)
}
for _, msg := range []string{
"Input token exceed the limit per minute",
"Input tokens exceeded the limit, please wait",
"input tokens exceed the TPM limit",
} {
if tooLong(400, msg) {
t.Errorf("a rate limit %q taken for a request too long", msg)
}
}
fresh(t)
a := &scripted{replies: []reply{{400, "", `{"error":{"message":"` + capped + `"}}`}}}
b := &scripted{replies: []reply{{200, "", chatOK}}}
scriptedOn(t, "a", provider.Chat, a)
scriptedOn(t, "b", provider.Chat, b)
if err := provider.SaveGroup(provider.Group{Name: "Top", Members: []string{"a/m", "b/m"}, Routing: provider.Ordered}); err != nil {
t.Fatal(err)
}
s := New()
code, body := postAs(t, s, "", `{"model":"group/top","messages":[{"role":"user","content":"hi"}]}`)
if code != 200 || !strings.Contains(body, "hello") || a.n != 1 || b.n != 1 {
t.Fatalf("capped: %d %s (a %d, b %d)", code, body, a.n, b.n)
}
r := s.trace.routes[len(s.trace.routes)-1]
if len(r.Tries) != 2 || r.Tries[0].Status != 400 || r.Tries[0].Fail != failOverflow || r.Tries[0].Rest != nil || r.Tries[1].Rest != nil {
t.Fatalf("tries %+v", r.Tries)
}
// a plain bad request is as bad at the next member: not asked
bad := `{"error":{"message":"Invalid parameter: messages","type":"invalid_request_error"}}`
a.replies, a.n, b.n = []reply{{400, "", bad}}, 0, 0
code, body = postAs(t, s, "", `{"model":"group/top","messages":[{"role":"user","content":"hi"}]}`)
if code != 400 || !strings.Contains(body, "Invalid parameter") || a.n != 1 || b.n != 0 {
t.Fatalf("bad request: %d %s (a %d, b %d)", code, body, a.n, b.n)
}
}
// #4420 (MYCF711 on Discord): a group member's 400 "内容太长了,请精简输入
// 或开启新对话", past ~730K on a model it declares at 1M, names the input
// rather than the context. It was filed as a failure that rested the
// member, and the group's other members were never asked. It goes the way
// an overflow goes: the next member is asked and nobody rests, in the
// vendor's words and \u-escaped alike.
func TestChineseInputTooLongGoesToTheNextMember(t *testing.T) {
for _, said := range []string{
`{"error":{"message":"内容太长了,请精简输入或开启新对话","type":"invalid_request_error"}}`,
`{"error":{"message":"内容太长了,请精简输入或开启新对话"}}`,
} {
fresh(t)
a := &scripted{replies: []reply{{400, "", said}}}
b := &scripted{replies: []reply{{200, "", chatOK}}}
scriptedOn(t, "a", provider.Chat, a)
scriptedOn(t, "b", provider.Chat, b)
if err := provider.SaveGroup(provider.Group{Name: "Top", Members: []string{"a/m", "b/m"}, Routing: provider.Ordered}); err != nil {
t.Fatal(err)
}
s := New()
code, body := postAs(t, s, "", `{"model":"group/top","messages":[{"role":"user","content":"hi"}]}`)
if code != 200 || !strings.Contains(body, "hello") || a.n != 1 || b.n != 1 {
t.Fatalf("%s: %d %s (a %d, b %d)", said, code, body, a.n, b.n)
}
r := s.trace.routes[len(s.trace.routes)-1]
if len(r.Tries) != 2 || r.Tries[0].Fail != failOverflow || r.Tries[0].Rest != nil || r.Tries[1].Rest != nil {
t.Fatalf("%s: tries %+v", said, r.Tries)
}
}
}