将 request_too_large 与明确的上下文超限分别处理。成员返回普通或结构化 413 时尝试其他成员,跳过同成员的额外账号,不将该成员休眠。仅在最终向客户端返回 413 时加入减少图片、大型工具结果或带摘要开启新会话的建议。 保留 tooLongUnsaid 对文本估算超过已知窗口 1.2 倍的判断,以及已有的上下文溢出窗口过滤和压缩提示。 补充 Anthropic 与 Codex 两条协议路径、成功及耗尽回退、同成员账号跳过、高文本估算和迁移 Factory 插件的合成回归测试。
70 lines
3.4 KiB
Go
70 lines
3.4 KiB
Go
package gateway
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// Some vendors don't say a conversation is too long for the model: Devin
|
|
// answered one of about 225k tokens for swe-1.7-lightning (a 202,752
|
|
// window) with a 502 "capacity issues", WorkBuddy one for glm-5.1 with a
|
|
// 400 "Invalid request parameters", Qoder one of 2M for qmodel with a 400
|
|
// "Error in upstream response" — each answered at a size the model holds.
|
|
// Asked again in place three times, the agent was then handed a 502 and
|
|
// never compacted. A request well past the model's window that fails so
|
|
// is told as one too long, at once.
|
|
func TestOverflowTheVendorDidntSay(t *testing.T) {
|
|
big := strings.Repeat("word ", 2400) // about 3000 tokens as estimated
|
|
small := strings.Repeat("word ", 100) // about 125
|
|
near := strings.Repeat("word ", 1760) // about 2200, which may yet fit
|
|
for _, tc := range []struct {
|
|
name, path, body string
|
|
code int
|
|
reply string
|
|
wantCode, calls int
|
|
want string
|
|
}{
|
|
{"502 anthropic", "/v1/messages", big, 502, `{"error":{"message":"We are experiencing capacity issues"}}`, 400, 1, "prompt is too long"},
|
|
{"400 chat", "/v1/chat/completions", big, 400, `{"error":{"message":"Invalid request parameters"}}`, 400, 1, "context_length_exceeded"},
|
|
// Text alone well past the window is enough to ask for compaction.
|
|
{"413 bytes", "/v1/messages", big, 413, `{"error":{"message":"Request Entity Too Large"}}`, 400, 1, "prompt is too long"},
|
|
{"413 request_too_large", "/v1/messages", big, 413, `{"error":{"type":"request_too_large","message":"Request exceeds the maximum size"}}`, 400, 1, "prompt is too long"},
|
|
// not the conversation's length
|
|
{"small 502", "/v1/messages", small, 502, `{"error":{"message":"We are experiencing capacity issues"}}`, 502, 1 + lastRetries, "capacity issues"},
|
|
{"near the window", "/v1/messages", near, 502, `{"error":{"message":"We are experiencing capacity issues"}}`, 502, 1 + lastRetries, "capacity issues"},
|
|
{"429", "/v1/messages", big, 429, `{"error":{"message":"Rate limit reached"}}`, 429, 1 + rateRetries, "Rate limit"},
|
|
{"401", "/v1/messages", big, 401, `{"error":{"message":"invalid token"}}`, 401, 1, "invalid token"},
|
|
{"quota", "/v1/messages", big, 400, `{"error":{"message":"insufficient balance"}}`, 400, 1, "insufficient balance"},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
proto := provider.Anthropic
|
|
if strings.Contains(tc.path, "chat") {
|
|
proto = provider.Chat
|
|
}
|
|
f := &fake{code: tc.code, ctype: "application/json", reply: tc.reply}
|
|
up := setup(t, proto, f)
|
|
p := provider.Provider{ID: "fake", Name: "Fake", Key: "k", Models: []string{"m1"}, Contexts: map[string]int{"m1": 2000}}
|
|
if proto == provider.Chat {
|
|
p.Chat = up.URL + "/v1"
|
|
} else {
|
|
p.Anthropic = up.URL
|
|
}
|
|
if err := provider.Save(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req := `{"model":"fake/m1","max_tokens":10,"messages":[{"role":"user","content":"` + tc.body + `"}]}`
|
|
code, body := post(t, tc.path, req)
|
|
if code != tc.wantCode || !strings.Contains(body, tc.want) || f.calls != tc.calls {
|
|
t.Fatalf("status %d after %d calls (want %d after %d): %s", code, f.calls, tc.wantCode, tc.calls, body)
|
|
}
|
|
if tc.wantCode != 400 || tc.name == "quota" {
|
|
if strings.Contains(body, "prompt is too long") || strings.Contains(body, "context_length_exceeded") {
|
|
t.Fatalf("told as an overflow: %s", body)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|