gateway: a relay's cap on one request's input fails over in a group (#2445)

B.ai answered a 380K request, under the model's 1M window, with 400
"Input token exceed the limit (request id: …)". No pattern in tooLongRe
matched it, and quotaWords wants "exceeded", so retryable() was false and
group/top's other three members were never asked (worksyang).

tooLongRe now also matches "input token(s) (count) exceed(s|ed) (the)
(max|maximum) limit". The refusal is taken as an overflow, so the chat
loop's existing #700 path asks the next member that may hold the request
(withRoom, failOverflow). Nobody rests, another account of the same model
isn't asked, and with no member left the agent is told it overflowed, to
compact. paceWords still keeps rate limits worded with input tokens out,
and a plain bad request still isn't asked of anyone else. System One's
decision groups (decide_group.go, which the report cites) are unchanged:
their requests are short decision text.

Test: TestRelayInputCapGoesToTheNextMember uses the reporter's message
verbatim. The JSON around it is not the reporter's; the issue shows only
the message. Docs: gateway-routing.md failure paragraph + verification.

Run:
- Broken by hand, the new test went red each time:
  - pattern removed: group answered 400, member 2 never asked;
  - paceWords check removed: two rate phrasings taken as overflow;
  - retryable true for every 400: the plain bad request reached member 2.
- TestRelayInputCap, TestEveryVendorsOverflow, TestOverflow*,
  TestContextOverflow*, TestOutOfBudget*: ok.
- gofmt -l: clean. go vet -tags nogui ./... on darwin, linux, windows: ok.

Full suite: go test -tags nogui -timeout 45m ./... at 0cad76cd9 + this
change, sandbox HOME: 66 packages ok, 0 FAIL (internal/gateway 1768s).
This commit is contained in:
yetone
2026-10-16 04:58:32 +08:00
parent 7e4336e719
commit 7fefbc614e
3 changed files with 70 additions and 4 deletions
File diff suppressed because one or more lines are too long
+4 -2
View File
@@ -6464,9 +6464,11 @@ func writeErrorSaid(w http.ResponseWriter, proto provider.Protocol, status int,
// Kimi's "exceeded model token limit", DS4's "configured context size",
// z.ai's "Prompt too long" / "Prompt exceeds max length" and
// model_context_window_exceeded, Ollama's "exceeded max context length",
// DashScope's "Range of input length should be", "token limit exceeded".
// DashScope's "Range of input length should be", "token limit exceeded",
// a relay's own cap on one request's input, "Input token exceed the limit"
// (#2445: B.ai, under the model's window, which another member holds).
var tooLongRe = regexp.MustCompile(`(?i)context_length_exceeded|prompt is too long|input is too long|(exceeds?|exceeded|over|beyond)( the)?( model'?s?)?( maximum)? context|context (length|limit|window) (exceeded|is exceeded)|maximum context length|too many (input |prompt )?tokens|上下文(长度)?(超|过长)|超(过|出)(了)?(模型)?(的)?(最大)?上下文` +
`|input token count.*exceeds the maximum|maximum prompt length is \d|reduce the length of the messages|maximum allowed input length|longer than the model'?s context length|available context size|greater than the context length|prompt token count of [\d,]+ exceeds the limit|context window exceeds limit|exceeded model token limit|configured context size|prompt too long|prompt exceeds max length|context_window_exceeded|exceeded (max |maximum )?context length|range of input length should be|token limit exceeded`)
`|input token count.*exceeds the maximum|maximum prompt length is \d|reduce the length of the messages|maximum allowed input length|longer than the model'?s context length|available context size|greater than the context length|prompt token count of [\d,]+ exceeds the limit|context window exceeds limit|exceeded model token limit|configured context size|prompt too long|prompt exceeds max length|context_window_exceeded|exceeded (max |maximum )?context length|range of input length should be|token limit exceeded|input tokens? (count )?(exceeds?|exceeded) (the )?(max(imum)? )?limit`)
// overflowed says a refused request was too long for the model — a
// request error (400, 413, 422) whose words say so.
+64
View File
@@ -0,0 +1,64 @@
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)
}
}