gateway: a 400 that only says the request is too long counts as an overflow once it is past half the model's window (#4420, MYCF711)

A group member answered 400 "内容太长了,请精简输入或开启新对话" at ~730K
on a model it declares at 1M. cfcd6648f added that wording to tooLongRe,
the word list. This change adds the judgment MYCF711 proposed as fix A,
so the next vendor that says it differently isn't missed the same way:
the vendor's words decide, and the request's size rules out other meanings.

tooLongUnsaid lowers its margin from unsaidMargin (1.2 of the window) to
lengthMargin (0.5) when saysLength holds:
- a 400 or 422,
- whose message has length words (lengthWords: 太长, 过长, 超长, 精简,
  缩短, 删减, 开启新对话/会话, too long/large/big, shorten, reduce the
  length/size, start a new conversation),
- no pace word (paceWords),
- and names no part of the request (partWords: a picture, a file, the
  reply, a field, a name, max_tokens),
- and tooLongUnsaid's own checks still hold: no quota, credit, model
  unserved, refusal or region words.
Its message is then "prompt is too long…". The overflow branch already
reads that as an overflow: the next member with room is asked
(failOverflow), nobody rests, and the conversation remembers the refusal.
With no member left, the agent is told the prompt is too long and
compacts. When the request is under the declared window, the message
no longer says "more than its context window". It says "more than <model>
takes here, though its context window is N".

Fix B, the word list in tooLongRe and paceWords, already landed in
cfcd6648f, with more wordings than the four proposed. Nothing was added
to it here.

Tests:
- TestLengthWordsPastHalfTheWindowGoToTheNextMember (new): for
  "请求过长,请精简后开启新对话" (raw and \u-escaped) and "Request too
  large. Please shorten it…", none of them in tooLongRe, a request of
  ~1500 tokens to a member of window 2000 goes to the next member: failOverflow,
  no rest. A short request keeps the vendor's 400 for the agent. A pace
  ("请求过长,已达到速率限制"), a file ("文件过长…") and "Image too
  large" aren't taken as the request's length.
- docs/subsystems/gateway-routing.md says it.

Run (mote, via mote-run, at this tree rebased on origin/main 7818dc316):
- go test ./internal/gateway -count=1 -run 'TestLengthWordsPastHalf|
  TestChineseInput|TestChineseNotTooLong|TestRelayInputCap|
  TestEveryVendorsOverflow|TestMemberTooSmall|Unsaid|TooLong|
  PayloadTooLarge|Roomy|Overflow': ok
- go vet ./internal/gateway, GOOS=linux -tags nogui, GOOS=windows: clean;
  gofmt -l clean. The full suite runs after the push, see the issue.

Broken by hand:
- lengthMargin replaced by unsaidMargin: the new test fails, (a 1, b 0),
  and the agent gets the 400.
- partWords dropped from saysLength: the new test fails, "文件过长,请精简后重试"
  taken for an overflow.

Not tried: the reporter's real vendor. The 0.5 floor comes from the
reporter's two cases, both refused past half of a 1M window. It is not a
measurement of other vendors.

Co-authored-by: MYCF711 <323101370+MYCF711@users.noreply.github.com>
This commit is contained in:
yetone
2026-11-04 21:52:48 +08:00
co-authored by MYCF711
parent dd50cbba25
commit a1f1810e93
3 changed files with 104 additions and 3 deletions
File diff suppressed because one or more lines are too long
+41 -2
View File
@@ -1868,6 +1868,36 @@ func lateRests(msg string) bool {
// estimated at 151,851), so one past it by a fifth hardly fits.
const unsaidMargin = 1.2
// lengthMargin is how far into a model's window a request must be for a
// refusal that speaks of length, without naming what is long, to be taken
// as too long for it (#4420, MYCF711): a group member answered 400
// "内容太长了,请精简输入或开启新对话" at ~730K on a model it declares at
// 1M, its own ceiling lower than the window it declares, so the request
// never came near unsaidMargin. What the vendor's message says decides:
// length words (lengthWords) and nothing of a limit of another kind; the
// request's size only rules out the other things such words are said of,
// a field or a picture, which a request under half the window more likely
// is. tooLongRe's wordings are the fallback that holds at any size.
const lengthMargin = 0.5
// lengthWords say the request is too long, or to make it shorter or start
// over, without naming what overflowed.
var lengthWords = regexp.MustCompile(`(?i)太长|过长|超长|精简|缩短|删减|(开启|新建|开始|重新开启)(一个)?新的?(对话|会话|聊天)|too (long|large|big)|shorten|reduce (the )?(length|size)|start a new (conversation|chat|session)`)
// partWords name a part of the request whose length isn't the
// conversation's: a picture, a file, the reply, a field, a name.
var partWords = regexp.MustCompile(`(?i)图片|图像|文件|附件|输出|回复|名称|字段|参数|标题|image|picture|file|attachment|output|max_tokens|max_completion|completion|title|description`)
// saysLength says a request error's message speaks of the request's length
// and of nothing else: not a pace, a quota, a part of the request.
func saysLength(status int, body []byte) bool {
if status != 400 && status != 422 {
return false
}
said := vendorMessage(body)
return lengthWords.MatchString(said) && !partWords.MatchString(said) && !paceWords.MatchString(said)
}
// tooLongUnsaid says a failure that doesn't say why was the request being too
// long for the model, as some vendors put it: Devin's 502 "capacity
// issues", WorkBuddy's 400 "Invalid request parameters", Qoder's 400
@@ -1877,13 +1907,15 @@ const unsaidMargin = 1.2
// as a vendor's error, never compacts. The message it is told instead
// says the prompt is too long. Not for a rate limit, an account's
// trouble, a model it doesn't serve, a region it doesn't serve, or one
// already saying it overflowed.
// already saying it overflowed. A request error that speaks of length
// (saysLength) needs only lengthMargin of the window.
func tooLongUnsaid(p provider.Provider, model string, tokens, status int, body []byte) (string, bool) {
switch status {
case 400, 413, 422, 500, 502, 503, 504:
default:
return "", false
}
raw := body
// the words are matched decoded, too (#1669)
body = wordsOf(body)
if overflowed(status, body) || failure(status, body) != failOther || quotaWords.Match(body) ||
@@ -1891,10 +1923,17 @@ func tooLongUnsaid(p provider.Provider, model string, tokens, status int, body [
return "", false
}
window := windowOf(p, model)
if window <= 0 || float64(tokens) < float64(window)*unsaidMargin {
margin := unsaidMargin
if saysLength(status, raw) {
margin = lengthMargin
}
if window <= 0 || float64(tokens) < float64(window)*margin {
return "", false
}
msg := fmt.Sprintf("prompt is too long: about %d tokens, more than %s's context window of %d", tokens, model, window)
if float64(tokens) < float64(window) {
msg = fmt.Sprintf("prompt is too long: about %d tokens, more than %s takes here, though its context window is %d", tokens, model, window)
}
said := strings.TrimSpace(vendorMessage(body))
if len(said) > 200 {
said = said[:200] + "…"
+62
View File
@@ -100,3 +100,65 @@ func TestChineseInputTooLongGoesToTheNextMember(t *testing.T) {
}
}
}
// #4420 (MYCF711): a vendor whose own ceiling is under the window it
// declares says only that the request is long, or to shorten it or start
// over, without naming what is long. Past half the member's window, that
// is the request being too long for it: the next member with room is
// asked, and nobody rests. Under half, or said of a pace or of a part of
// the request (a file), the same words are left as they were.
func TestLengthWordsPastHalfTheWindowGoToTheNextMember(t *testing.T) {
long := strings.Repeat("长对话 ", 600) // ~1500 tokens by estimate's count: past half a's 2000, under its whole
ask := func(t *testing.T, said, content string) (*Server, *scripted, *scripted, int, string) {
t.Helper()
fresh(t)
a := &scripted{replies: []reply{{400, "", said}}}
b := &scripted{replies: []reply{{200, "", chatOK}}}
roomyMember(t, "a", 2000, a)
roomyMember(t, "b", 10000, 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":"`+content+`"}]}`)
return s, a, b, code, body
}
for _, said := range []string{
`{"error":{"message":"请求过长,请精简后开启新对话"}}`,
`{"error":{"message":"请求过长,请精简后开启新对话"}}`,
`{"error":{"message":"Request too large. Please shorten it and try again.","type":"invalid_request_error","param":null}}`,
} {
// the word list alone doesn't take it: the length words decide
if overflowed(400, []byte(said)) {
t.Fatalf("%s: already an overflow by its words; the test needs one that isn't", said)
}
s, a, b, code, body := ask(t, said, long)
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)
}
// a short request isn't too long for anything: the vendor's
// words reach the agent as they did
s, a, b, code, body = ask(t, said, "hi")
if code != 400 || a.n != 1 || b.n != 0 {
t.Fatalf("%s, short: %d %s (a %d, b %d)", said, code, body, a.n, b.n)
}
if r := s.trace.routes[len(s.trace.routes)-1]; r.Tries[0].Fail == failOverflow {
t.Fatalf("%s, short: taken for an overflow", said)
}
}
for _, said := range []string{
`{"error":{"message":"请求过长,已达到速率限制"}}`, // a pace
`{"error":{"message":"文件过长,请精简后重试"}}`, // a part of the request
`{"error":{"message":"Image too large"}}`,
} {
s, _, _, _, body := ask(t, said, long)
if r := s.trace.routes[len(s.trace.routes)-1]; r.Tries[0].Fail == failOverflow || strings.Contains(body, "prompt is too long") {
t.Fatalf("%s: taken for the request's length: %s %+v", said, body, r.Tries)
}
}
}