Files
magpie/internal/gateway/overflow_cap_test.go
T
ggbdpq e0a5091b7d gateway: a member that refused this conversation as too long is asked last, not first
What changed: an overflow (400, "prompt is too long") is the request's
length, not the account's, so it rests nobody — and left no trace: the
next request of the same conversation tried the member again, failed the
same way, and fell back again, for every request (#3453: a rule sent
every turn to a Claude subscription bridge that answered "Prompt is too
long" and DeepSeek answered; two consecutive requests of one turn each
paid the refused try first). Now the conversation remembers, per
provider's model, how large the request was when it was refused, and the
member is put behind the others — behind, not out: it is still asked
when every other member has failed, a rule that names it is still
heard (ruleOver has run), and what it answers makes magpie forget the
refusal. A request smaller than what was refused — the agent has
compacted the conversation — finds the member where the rule put it.
Another conversation is unaffected, and a byte cap (413) is not
remembered: it is not the conversation too long for the model.

TestRelayInputCapGoesToTheNextMember's second phase shares its server
with the first, whose overflow left this conversation's memory of a's
refusal behind; as that phase says how a plain bad request is classed,
its memory starts empty.

Run: go test -tags nogui -count=1 -run
'TestOverflowRefusedOnceIsAskedLast' ./internal/gateway/ (red on
c688211d1 with the test alone, green here); go test -tags nogui
-count=1 ./internal/gateway/; go vet -tags nogui ./internal/gateway/;
gofmt -l internal/gateway clean. Broken by hand: the product half of
the commit dropped (affinity.go, gateway.go hooks), the test red at
"second: (a 1, b 1)"; restored, green.

Generated-by: GLM-5.3-Flash (ZCode)
2026-11-01 16:43:20 +08:00

103 lines
4.6 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. The
// overflow above left this conversation's memory of a's refusal
// behind, and #3453's learning would put a behind b before either is
// asked; this phase is about how a plain 400 is classed, so the
// memory starts empty
overflowLearned.Lock()
overflowLearned.m = map[string]map[string]refusedLong{}
overflowLearned.Unlock()
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)
}
}
}