gateway: "Context window is full" is a conversation too long for the model
ergou0954_54426 on Discord (#feedback, 11-01) routes by intent across several models, one a "kiro opus" of an API-key relay (not magpie's Kiro plugin). On long conversations the relay answered 400 "Context window is full. Reduce conversation history, system prompt, or tools. (request id: …)". That is the wording kiro.rs (src/anthropic/handlers.rs) and kiro2claude (error-mapper.ts) give Kiro's CONTENT_LENGTH_EXCEEDS_THRESHOLD, always as a 400 invalid_request_error, with new-api's request id after it. tooLongRe had no words for it, so the 400 was an ordinary request error: the group never asked the next member, and the agent got the relay's 400 as a plain error, with no context_length_exceeded to compact on. In a stream's error event it stood for a 502 (streamStatus), a passing vendor fault that rests the account and is asked again. tooLongRe now takes "context (window )?(is )?full", so overflowed, failure, retryable, streamStatus, lateRests and writeErrorSaid all read it the same way: on 400, 413 or 422, or in a stream's error event, the member is passed for one with room (failOverflow, no rest), and alone the agent is told context_length_exceeded. Not changed: a 5xx carrying the words is still the vendor's fault (tooLong is 4xx only, as TestContextOverflowSaidTheClientsWay holds for a 502), and tooLongUnsaid still covers a 5xx only where the window is known. Routing could not have skipped the member up front: a custom relay's model has a window only from its own /v1/models list, models.dev by id (catalog.ContextOf, with its spellings), or the user's Context setting. A relay id such as "kiro-opus" matches none, so the window is unknown (0), roomy keeps it and withRoom counts it as having room. A probe in a fresh HOME gave windowOf 0 for claude-opus-4-6, kiro-claude-opus-4.6 and kiro-opus on a custom provider. TestContextWindowFullIsAnOverflow and TestContextWindowFullGoesToAMemberWithRoom are built from the literal text, with and without the request id, Anthropic- and OpenAI-shaped, plus kiro.rs's sibling "Input is too long. Reduce the size of your messages."; ordinary 400s (invalid model, model not found, messages required) stay non-overflows. Found and filed, not fixed: the last candidate's stream error event that says too long reaches the agent as the vendor wrote it (#4675). Run on mote, on this change at base6f3dfc282: gofmt -l internal/gateway clean; go vet ./internal/gateway, GOOS=linux go vet -tags nogui, GOOS=windows go vet: ok; go test -timeout 45m -count=1 ./internal/gateway: ok (730s). At the pushed head (rebased on445891cf7, which changed internal/gateway and internal/provider): gofmt and go vet ./internal/gateway clean, and go test -run of the two new tests, TestContextOverflowSaidTheClientsWay, TestEveryVendorsOverflow, TestChineseNotTooLong, TestKiroPluginOverflowGoesToAMemberWithRoom and TestOverflowGoesToAMemberWithRoom: ok. Broken by hand: the two new tests at origin/main6f3dfc282(without the regex change) fail: every status reads as not an overflow, the stream error stands for 502 and rests, and the group answers the relay's 400 with the second member never asked. Not run: the whole gateway package at the pushed head, the full ./... suite (another session's suite held mote), the GUI suite (no GUI change), a real Kiro-backed relay pushed past its window.
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// contextFull is how Kiro-backed relays say a conversation is past the
|
||||
// model's window: kiro.rs (src/anthropic/handlers.rs, map_provider_error)
|
||||
// and kiro2claude (error-mapper.ts) turn Kiro's
|
||||
// CONTENT_LENGTH_EXCEEDS_THRESHOLD into it, as a 400
|
||||
// invalid_request_error, and a new-api relay in front of one adds its
|
||||
// request id. ergou0954_54426 on Discord had it from a "kiro opus" model
|
||||
// of an API-key relay in an intent-routed group: not read as an overflow,
|
||||
// it went to the agent as a plain 400, which never compacted, and no other
|
||||
// member was asked.
|
||||
const contextFull = "Context window is full. Reduce conversation history, system prompt, or tools."
|
||||
|
||||
// The words are an overflow on every request-error status a relay sends
|
||||
// them with, and in a stream's error event; neither a used-up plan nor
|
||||
// a fault another account of the model would answer differently. An
|
||||
// ordinary 400 is still not one.
|
||||
func TestContextWindowFullIsAnOverflow(t *testing.T) {
|
||||
said := contextFull + " (request id: 2026110112345678901234567)"
|
||||
bodies := []string{
|
||||
`{"type":"error","error":{"type":"invalid_request_error","message":"` + contextFull + `"}}`,
|
||||
`{"error":{"message":"` + said + `","type":"invalid_request_error","param":"","code":null}}`,
|
||||
// its sibling in kiro.rs, for one input past Kiro's own limit
|
||||
`{"type":"error","error":{"type":"invalid_request_error","message":"Input is too long. Reduce the size of your messages."}}`,
|
||||
}
|
||||
for _, status := range []int{400, 413, 422} {
|
||||
for _, b := range bodies {
|
||||
if !overflowed(status, []byte(b)) {
|
||||
t.Errorf("%d %s isn't an overflow", status, b)
|
||||
}
|
||||
if retryable(status, []byte(b)) {
|
||||
t.Errorf("%d %s is asked of another account", status, b)
|
||||
}
|
||||
if f := failure(status, []byte(b)); f != failOther {
|
||||
t.Errorf("%d %s is a %s failure", status, b, f)
|
||||
}
|
||||
}
|
||||
}
|
||||
// in a stream, as an error event: an overflow's 400, not a vendor's
|
||||
// passing 502 that rests the account and is asked again
|
||||
if got := streamStatus(said); got != 400 {
|
||||
t.Errorf("a stream's %q stands for %d", said, got)
|
||||
}
|
||||
if lateRests(said) {
|
||||
t.Errorf("a reply cut by %q rests the account", said)
|
||||
}
|
||||
for _, msg := range []string{
|
||||
`{"error":{"message":"invalid model: kiro-opus-9 (request id: 20261101)","type":"invalid_request_error"}}`,
|
||||
`{"error":{"message":"model not found","type":"invalid_request_error"}}`,
|
||||
`{"error":{"message":"Invalid request: messages is required","type":"invalid_request_error"}}`,
|
||||
} {
|
||||
if overflowed(400, []byte(msg)) {
|
||||
t.Errorf("an ordinary 400 %s is taken for an overflow", msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// In a group the member saying its context window is full is passed for
|
||||
// the next, asked once and not rested, both as an error status and as an
|
||||
// error event of a stream begun with 200; alone, the status tells the
|
||||
// agent context_length_exceeded, so it compacts.
|
||||
func TestContextWindowFullGoesToAMemberWithRoom(t *testing.T) {
|
||||
said := contextFull + " (request id: 2026110112345678901234567)"
|
||||
for _, full := range []reply{
|
||||
{400, "application/json", `{"error":{"message":"` + said + `","type":"invalid_request_error","param":"","code":null}}`},
|
||||
{200, "text/event-stream", "data: {\"error\":{\"message\":\"" + said + "\",\"type\":\"invalid_request_error\"}}\n\n"},
|
||||
} {
|
||||
fresh(t)
|
||||
relay := &scripted{replies: []reply{full}}
|
||||
other := &scripted{replies: []reply{{200, "", chatOK}}}
|
||||
scriptedOn(t, "r", provider.Chat, relay)
|
||||
scriptedOn(t, "d", provider.Chat, other)
|
||||
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"r/m", "d/m"}, Routing: provider.Ordered}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := New()
|
||||
code, body := postAs(t, s, "", `{"model":"group/g","messages":[{"role":"user","content":"hi"}]}`)
|
||||
if code != 200 || !strings.Contains(body, "hello") || relay.n != 1 || other.n != 1 {
|
||||
t.Fatalf("%d %s\ngroup: %d %s (relay %d, other %d)", full.code, full.ctype, code, body, relay.n, other.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 {
|
||||
t.Fatalf("%d %s\ntries %+v", full.code, full.ctype, r.Tries)
|
||||
}
|
||||
|
||||
// alone, the agent is told it overflowed, asked once. (A stream's
|
||||
// error event from the one candidate left reaches the agent as the
|
||||
// relay sent it; that is #4675.)
|
||||
if full.code != 400 {
|
||||
continue
|
||||
}
|
||||
relay.n = 0
|
||||
code, body = postAs(t, s, "", `{"model":"r/m","messages":[{"role":"user","content":"hi"}]}`)
|
||||
if code != 400 || !strings.Contains(body, "context_length_exceeded") || relay.n != 1 {
|
||||
t.Fatalf("%d %s\nalone: %d %s (relay %d)", full.code, full.ctype, code, body, relay.n)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8580,6 +8580,9 @@ func writeErrorSaid(w http.ResponseWriter, proto provider.Protocol, status int,
|
||||
// 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).
|
||||
// "Context window is full" is how Kiro-backed relays (kiro.rs,
|
||||
// kiro2claude) say Kiro's CONTENT_LENGTH_EXCEEDS_THRESHOLD, a 400
|
||||
// (ergou0954_54426 on Discord, through a new-api relay).
|
||||
// The third line is Chinese that names the input rather than the context
|
||||
// (#4420, MYCF711): a group member's 400 "内容太长了,请精简输入或开启新对话",
|
||||
// z.ai's code 1261 "Prompt 超长", iFlytek Spark's 10907 "token数量超过上限。
|
||||
@@ -8588,7 +8591,7 @@ func writeErrorSaid(w http.ResponseWriter, proto provider.Protocol, status int,
|
||||
// 提示词, prompt…) beside its length, so "名称超出最大长度" (a field) or
|
||||
// "输出过长" (the reply) isn't taken; bare "token数量超过上限" isn't either,
|
||||
// as a day's allowance is said so too.
|
||||
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|上下文(长度)?(超|过长)|超(过|出)(了)?(模型)?(的)?(最大)?上下文` +
|
||||
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)|context (window )?(is )?full\b|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 tokens? (count )?(exceeds?|exceeded) (the )?(max(imum)? )?limit` +
|
||||
`|(内容|输入|文本|提示词?|prompt|问题|对话|消息)(内容|文本)?(长度)?\s*(太长|过长|超长)|字数(太多|过多)` +
|
||||
`|(内容|输入|文本|提示词?|prompt|问题|对话|消息)[^,。,.;;!!]{0,12}超(过|出)[^,。,.;;!!]{0,12}(长度|token|令牌|字数)` +
|
||||
|
||||
Reference in New Issue
Block a user