NEKO-CwC asked, after v0.1.3141, why a custom API-key Responses relay's 400/413/422 is still turned into context_length_exceeded when the relay never said so. It shouldn't be. tooLongUnsaid read any provider's 400/413/422 that said nothing of length, for a request 1.2 times past the model's window, as "prompt is too long". That rule came from three vendors' refusals inb3ab3e42e(v0.1.735, 10-03): Devin's 502 "capacity issues", Qoder's 400 "Error in upstream response" and WorkBuddy's 400 "Invalid request parameters". #711 (v0.1.808) then kept the 413 case for Factory's byte cap.978284dd6(v0.1.3141) had already scoped the 5xx to Devin. The 4xx half still applied to every provider. Now: - unsaidStatus lists who gets the inference, keyed by the id of the plugin that serves the vendor (all of them are retired as built-ins), never by the provider's id, which a user's own provider can share: devin 500/502/503/504; qoder, qoder-cn, workbuddy, workbuddy-ai 400/413/422; factory 413. unsaidOverflows replaces busyOverflows. - Every other provider's failure reaches the client with its own status and words: custom and by-address providers, API-key relays, OpenAI, Anthropic, other plugins. Transport failures are never inferred (unchanged since978284dd6). - An upstream that says it overflowed (tooLongRe, overflow codes) is still passed on as an overflow. A 400/422 whose own words say the request is too long (saysLength, #4420) is still read past half the window for any vendor, because the vendor named the cause. - The single call site in serve runs for every client route (Responses, Codex's /responses, Chat, Messages) and every upstream protocol, held or streamed, before anything is sent. Mid-stream SSE errors were never inferred. Tests: - TestRelayFailureKeepsItsOwnWords (new, 126 subtests): a custom relay speaking Responses, Chat or Messages answers 400 "Invalid request parameters", 400 "Error in upstream response", 413, 422 or (Responses) 502/503 to a request past 1.2 of its window. Asked through /v1/responses (held and streamed), Codex's /responses, /v1/chat (held and streamed) and /v1/messages (held and streamed), the client gets that status and the relay's words, with no context_length_exceeded. - TestUnsaidVendorsStillOverflow (new): each listed vendor and status, under its own id and a -plugin id, is told as too long past the window and left alone within it. Another vendor's plugin, and a user provider named like the vendor, are not. Statuses a vendor wasn't seen to use are not. - TestQoderPluginUnsaidOverflow (new): through the fake plugin moved as qoder, end to end. - TestOverflowTheVendorDidntSay's 400/413 rows now expect the vendor's status and words. TestPayloadTooLargeIsNotContextOverflow and TestRegionRefusalIsNotTooLong use Factory's and WorkBuddy AI's plugin. TestWindowNotBelowWhatWasAnswered and TestWindowNotBelowListedMax now use a length-words 400, the one path a generic provider still has. Broken by hand, on mote: - the old fallback.go (origin/main) with the new tests: 84 of TestRelayFailureKeepsItsOwnWords' subtests red (every 4xx one; the 5xx ones were already fixed by978284dd6), and TestUnsaidVendorsStillOverflow, TestOverflowTheVendorDidntSay and TestPayloadTooLargeIsNotContextOverflow red; - unsaidOverflows returning false: TestUnsaidVendorsStillOverflow, TestQoderPluginUnsaidOverflow, TestDevinBusyOverflowStillTooLong, TestPayloadTooLargeIsNotContextOverflow and TestRegionRefusalIsNotTooLong red; - keyed by p.ID instead of the plugin: TestUnsaidVendorsStillOverflow (user provider named like the vendor) and TestDevinBusyOverflowStillTooLong (devin-plugin) red. Run (mote-run, at this tree): gofmt -l clean; go vet ./internal/gateway for darwin, GOOS=linux and GOOS=windows ok; go test -count=1 -timeout 45m ./internal/gateway: ok (809s) at basebe5e8f29f. Rebased ontoe8df78f33(two routing commits touched gateway.go and trace.go), then at the pushed tree: go vet ok and the overflow, too-long, payload, window and response-id tests ok (14s). Not tried: a real relay, or real Devin, Qoder, WorkBuddy or Factory accounts, against this change. A vendor not listed that hides an overflow behind a bare 400 now hands the agent that 400 rather than compacting; it gets added to unsaidStatus when it is seen. Reported-by: NEKO-CwC (GitHub), ne9ko9 (Discord)
301 lines
14 KiB
Go
301 lines
14 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"regexp"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// A byte-size refusal alone is not evidence of a token-window overflow.
|
|
// An independent text estimate well past the window still asks for compaction.
|
|
func TestPayloadTooLargeIsNotContextOverflow(t *testing.T) {
|
|
for _, msg := range []string{
|
|
"Factory: Request Entity Too Large",
|
|
`{"error":{"type":"request_too_large","message":"Request exceeds the maximum size"}}`,
|
|
} {
|
|
if tooLong(413, msg) || overflowed(413, []byte(msg)) {
|
|
t.Errorf("byte limit classified as token limit: %s", msg)
|
|
}
|
|
p := provider.Provider{ID: "fake", Name: "Fake", Contexts: map[string]int{"m": 100}}
|
|
// Image bytes don't make a text estimate pass the token margin.
|
|
tokens := estimate(&Request{Messages: []Message{{Role: "user", Parts: []Part{
|
|
{Kind: Text, Text: strings.Repeat("word", 100)},
|
|
{Kind: Image, Data: strings.Repeat(pngA, 4096)},
|
|
}}}})
|
|
if inferred, ok := tooLongUnsaid(p, "m", tokens, 0, 413, []byte(msg)); ok {
|
|
t.Errorf("byte limit inferred as token limit: %s", inferred)
|
|
}
|
|
// a 413 is read as an overflow only for Factory (#711), whose
|
|
// plugin serves it; any other vendor's byte cap is its own
|
|
if inferred, ok := tooLongUnsaid(p, "m", 1000, 0, 413, []byte(msg)); ok {
|
|
t.Errorf("another vendor's byte cap inferred as a token limit: %s", inferred)
|
|
}
|
|
factory := provider.PluginForTest("factory", "factory")
|
|
factory.Name, factory.Contexts = "Factory", map[string]int{"m": 100}
|
|
if inferred, ok := tooLongUnsaid(factory, "m", 1000, 0, 413, []byte(msg)); !ok || !strings.Contains(inferred, "prompt is too long") {
|
|
t.Errorf("text estimate past the window lost compaction: %s", inferred)
|
|
}
|
|
if retryable(413, []byte(msg)) {
|
|
t.Error("unchanged oversized request would be retried")
|
|
}
|
|
}
|
|
}
|
|
|
|
// Body limits belong to a vendor, so another member may accept the same
|
|
// request. Neither account retry nor resting helps a member with a byte cap.
|
|
func TestPayloadTooLargeTriesAnotherMember(t *testing.T) {
|
|
const answer = `{"id":"ok","type":"message","role":"assistant","model":"m","content":[{"type":"text","text":"hello"}],"stop_reason":"end_turn","usage":{"input_tokens":1,"output_tokens":1}}`
|
|
for _, path := range []string{"/v1/messages", CodexPath + "/responses"} {
|
|
for _, refusal := range []struct{ name, body string }{
|
|
{"plain", "Request Entity Too Large"},
|
|
{"typed", `{"error":{"type":"request_too_large","message":"Request exceeds the maximum size"}}`},
|
|
} {
|
|
for _, accepts := range []bool{true, false} {
|
|
name := path + "/" + refusal.name + "/exhausted"
|
|
if accepts {
|
|
name = path + "/" + refusal.name + "/accepts"
|
|
}
|
|
t.Run(name, func(t *testing.T) {
|
|
fresh(t)
|
|
a := &scripted{replies: []reply{{413, "", refusal.body}}}
|
|
b := &scripted{replies: []reply{{413, "", refusal.body}}}
|
|
if accepts {
|
|
b.replies = []reply{{200, "", answer}}
|
|
if strings.HasSuffix(path, "/responses") {
|
|
b.replies = []reply{{200, "text/event-stream", payloadAnswerStream()}}
|
|
}
|
|
}
|
|
scriptedOn(t, "a", provider.Anthropic, a)
|
|
scriptedOn(t, "b", provider.Anthropic, b)
|
|
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"a/m", "b/m"}, Routing: provider.Ordered}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body := `{"model":"group/g","max_tokens":10,"messages":[{"role":"user","content":"hi"}]}`
|
|
if strings.HasSuffix(path, "/responses") {
|
|
body = `{"model":"group/g","input":[{"role":"user","content":"hi"}]}`
|
|
}
|
|
s := New()
|
|
w := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(w, httptest.NewRequest("POST", path, strings.NewReader(body)))
|
|
if a.n != 1 || b.n != 1 {
|
|
t.Fatalf("body limit did not ask each member once: a %d, b %d, status %d", a.n, b.n, w.Code)
|
|
}
|
|
if accepts {
|
|
if w.Code != 200 || !strings.Contains(w.Body.String(), "hello") || strings.Contains(w.Body.String(), "byte limit") {
|
|
t.Fatalf("accepted fallback: %d %s", w.Code, w.Body.String())
|
|
}
|
|
} else if w.Code != 413 || strings.Count(w.Body.String(), "byte limit") != 1 || strings.Contains(w.Body.String(), "context_length_exceeded") {
|
|
t.Fatalf("exhausted byte limits: %d %s", w.Code, w.Body.String())
|
|
}
|
|
route := lastRoute(s)
|
|
if len(route.Tries) != 2 || route.Tries[0].Status != 413 || route.Tries[0].Fail == failOverflow || route.Tries[0].Rest != nil || route.Tries[1].Rest != nil {
|
|
t.Fatalf("byte cap rested a member or became a token limit: %+v", route.Tries)
|
|
}
|
|
restingUntil.Lock()
|
|
n := len(restingUntil.m)
|
|
restingUntil.Unlock()
|
|
if n != 0 {
|
|
t.Fatalf("byte cap persisted %d rests", n)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPayloadTooLargeSkipsSameMemberAccounts(t *testing.T) {
|
|
fresh(t)
|
|
a := &scripted{replies: []reply{{413, "", "Request Entity Too Large"}}}
|
|
b := &scripted{replies: []reply{{200, "", chatOK}}}
|
|
up := httptest.NewServer(a)
|
|
t.Cleanup(up.Close)
|
|
if err := provider.Save(provider.Provider{ID: "a", Name: "A", Key: "first", Keys: []provider.KeyAccount{{Key: "second"}}, Chat: up.URL + "/v1", Models: []string{"m"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
scriptedOn(t, "b", provider.Chat, b)
|
|
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"a/m", "b/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") || a.n != 1 || b.n != 1 {
|
|
t.Fatalf("byte cap accounts: %d %s (a %d, b %d)", code, body, a.n, b.n)
|
|
}
|
|
}
|
|
|
|
func TestPayloadTooLargeOverWindowTriesMemberWithRoom(t *testing.T) {
|
|
fresh(t)
|
|
a := &scripted{replies: []reply{{413, "", `{"error":{"type":"request_too_large","message":"Request exceeds the maximum size"}}`}}}
|
|
b := &scripted{replies: []reply{{200, "", chatOK}}}
|
|
c := &scripted{replies: []reply{{200, "", chatOK}}}
|
|
for _, member := range []struct {
|
|
id string
|
|
reply *scripted
|
|
window int
|
|
}{{"a", a, 2000}, {"b", b, 2000}, {"c", c, 10000}} {
|
|
up := httptest.NewServer(member.reply)
|
|
t.Cleanup(up.Close)
|
|
if err := provider.Save(provider.Provider{ID: member.id, Name: member.id, Key: "synthetic", Chat: up.URL + "/v1", Models: []string{"m"}, Contexts: map[string]int{"m": member.window}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"a/m", "b/m", "c/m"}, Routing: provider.Ordered}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := New()
|
|
code, body := postAs(t, s, "", `{"model":"group/g","messages":[{"role":"user","content":"`+strings.Repeat("word ", 2400)+`"}]}`)
|
|
route := lastRoute(s)
|
|
// text past a's and b's windows isn't sent them at all (roomy): only
|
|
// c, which holds it, is asked
|
|
if code != 200 || !strings.Contains(body, "hello") || a.n != 0 || b.n != 0 || c.n != 1 || len(route.Tries) != 1 || route.Tries[0].ID != "c" {
|
|
t.Fatalf("text overflow: %d %s (a %d, b %d, c %d), tries %+v", code, body, a.n, b.n, c.n, route.Tries)
|
|
}
|
|
}
|
|
|
|
func payloadAnswerStream() string {
|
|
return sse(
|
|
`event: message_start`+"\n"+`data: {"type":"message_start","message":{"id":"ok","role":"assistant","content":[],"usage":{"input_tokens":1}}}`,
|
|
`event: content_block_start`+"\n"+`data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}`,
|
|
`event: content_block_delta`+"\n"+`data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"hello"}}`,
|
|
`event: message_stop`+"\n"+`data: {"type":"message_stop"}`)
|
|
}
|
|
|
|
func TestPayloadTooLargeErrorShapes(t *testing.T) {
|
|
for _, proto := range []provider.Protocol{provider.Responses, provider.Chat, provider.Anthropic, provider.Gemini} {
|
|
t.Run(string(proto), func(t *testing.T) {
|
|
msg := "Factory: request_too_large: Request exceeds the maximum size"
|
|
held := newHoldWriter(httptest.NewRecorder(), true)
|
|
writeError(held, proto, 413, msg)
|
|
if strings.Contains(string(held.errBody()), "byte limit") {
|
|
t.Fatal("byte hint added before other members were tried")
|
|
}
|
|
immediate := httptest.NewRecorder()
|
|
writeError(newHoldWriter(immediate, false), proto, 413, msg)
|
|
if strings.Count(immediate.Body.String(), "byte limit") != 1 {
|
|
t.Fatal("final unheld byte refusal lost its hint")
|
|
}
|
|
for range 2 { // another Magpie can relay a message already carrying the hint
|
|
w := httptest.NewRecorder()
|
|
status := writeError(w, proto, 413, msg)
|
|
var body struct {
|
|
Error struct {
|
|
Type string `json:"type"`
|
|
Message string `json:"message"`
|
|
Code any `json:"code"`
|
|
} `json:"error"`
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if status != 413 || w.Code != 413 || body.Error.Code == "context_length_exceeded" || strings.Count(body.Error.Message, "byte limit") != 1 {
|
|
t.Fatalf("byte refusal mapped to context overflow: %d %s", status, w.Body.String())
|
|
}
|
|
if proto == provider.Anthropic && body.Error.Type != "request_too_large" {
|
|
t.Fatalf("wrong Anthropic error type: %s", w.Body.String())
|
|
}
|
|
msg = body.Error.Message
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Exercise Codex's actual endpoint and a moved Factory routing group:
|
|
// Responses is translated to Messages, then the plugin's fetch returns
|
|
// a size refusal. No request goes to a real provider.
|
|
func TestMovedFactoryPayloadTooLarge(t *testing.T) {
|
|
for _, tc := range []struct{ name, reply string }{
|
|
{"plain", "Request Entity Too Large"},
|
|
{"anthropic", `{"error":{"type":"request_too_large","message":"Request exceeds the maximum size"}}`},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
catalog.Reset()
|
|
t.Cleanup(catalog.Reset)
|
|
var calls atomic.Int32
|
|
const system = "Synthetic system instructions."
|
|
toolOutput := strings.Repeat("synthetic tool output ", 256)
|
|
movedFake(t, "factory", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
calls.Add(1)
|
|
body, err := io.ReadAll(r.Body)
|
|
if err != nil || !strings.HasSuffix(r.URL.Path, "/messages") || len(body) < 2048 {
|
|
t.Errorf("wrong size-refusal fixture: path %s, bytes %d, error %v", r.URL.Path, len(body), err)
|
|
}
|
|
for _, part := range []string{system, toolOutput, pngA, `"tool_use_id":"call_1"`, `"name":"inspect_fixture"`, "continue"} {
|
|
if !strings.Contains(string(body), part) {
|
|
t.Errorf("size-refusal fixture lost system, schema, tool output, image or final text")
|
|
}
|
|
}
|
|
t.Logf("synthetic Messages body: %d bytes; tool text: %d bytes; image base64: %d bytes", len(body), len(toolOutput), len(pngA))
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusRequestEntityTooLarge)
|
|
io.WriteString(w, tc.reply)
|
|
}))
|
|
var fallbackCalls atomic.Int32
|
|
fallback := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
fallbackCalls.Add(1)
|
|
body, _ := io.ReadAll(r.Body)
|
|
for _, part := range []string{system, toolOutput, pngA, `"tool_use_id":"call_1"`, `"name":"inspect_fixture"`, "continue"} {
|
|
if !strings.Contains(string(body), part) {
|
|
t.Errorf("fallback lost system, schema, tool output, image or final text")
|
|
}
|
|
}
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
io.WriteString(w, payloadAnswerStream())
|
|
}))
|
|
t.Cleanup(fallback.Close)
|
|
if err := provider.Save(provider.Provider{ID: "other", Name: "Other", Key: "synthetic", Anthropic: fallback.URL, Models: []string{"m"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := catalog.SaveLive("other", fallback.URL, []catalog.Model{{ID: "m", Images: true, ImageInput: imageInputBool(true)}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := provider.SaveGroup(provider.Group{ID: "payload", Name: "Payload", Members: []string{"factory/fake-claude", "other/m"}, Routing: provider.Ordered}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body, _ := json.Marshal(map[string]any{
|
|
"model": "group/payload", "stream": true, "instructions": system,
|
|
"tools": []any{map[string]any{"type": "function", "name": "inspect_fixture", "parameters": map[string]any{"type": "object", "properties": map[string]any{}}}},
|
|
"input": []any{
|
|
map[string]any{"role": "user", "content": "Inspect the fixture."},
|
|
map[string]any{"type": "function_call", "name": "inspect_fixture", "call_id": "call_1", "arguments": "{}"},
|
|
map[string]any{"type": "function_call_output", "call_id": "call_1", "output": []any{
|
|
map[string]any{"type": "input_text", "text": toolOutput},
|
|
map[string]any{"type": "input_image", "image_url": "data:image/png;base64," + pngA},
|
|
}},
|
|
map[string]any{"role": "assistant", "content": "fixture received"},
|
|
map[string]any{"role": "user", "content": "continue"},
|
|
},
|
|
})
|
|
t.Logf("synthetic Responses body: %d bytes; input items: 5; tool schemas: 1; images: 1", len(body))
|
|
rec := httptest.NewRecorder()
|
|
s := New()
|
|
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", CodexPath+"/responses", strings.NewReader(string(body))))
|
|
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "hello") || strings.Contains(rec.Body.String(), "byte limit") || calls.Load() != 1 || fallbackCalls.Load() != 1 {
|
|
t.Fatalf("size fallback: status %d, Factory calls %d, fallback calls %d, tries %+v", rec.Code, calls.Load(), fallbackCalls.Load(), lastRoute(s).Tries)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A vendor's byte cap reaches an agent on the OpenAI routes in words pi
|
|
// knows (its overflow.ts matches request_too_large), so pi compacts and
|
|
// tries once more instead of stopping — @wilsonxuuuu on X, whose pi got
|
|
// an api_error 413 and never compacted.
|
|
func TestPayloadTooLargeSaysRequestTooLargeToPi(t *testing.T) {
|
|
const nginx = "<html>\r\n<head><title>413 Request Entity Too Large</title></head>\r\n<body>\r\n<center><h1>413 Request Entity Too Large</h1></center>\r\n<hr><center>nginx</center>\r\n</body>\r\n</html>\r\n"
|
|
piOverflow := regexp.MustCompile(`(?i)request_too_large`)
|
|
fresh(t)
|
|
scriptedOn(t, "a", provider.Chat, &scripted{replies: []reply{{413, "text/html", nginx}}})
|
|
code, body := postAs(t, New(), "", `{"model":"a/m","messages":[{"role":"user","content":"hi"}]}`)
|
|
if code != 413 || !piOverflow.MatchString(body) || !strings.Contains(body, "byte limit") || strings.Contains(body, "context_length_exceeded") {
|
|
t.Fatalf("byte cap on the chat route: %d %s", code, body)
|
|
}
|
|
}
|