Files
magpie/internal/gateway/body_cap_page_test.go
T
yetone 4c1b84aca5 gateway: a proxy's 502 page to a request of 4 MB or more is a byte cap, said as a 413
chenxiang0010 (Discord, v0.1.3265): a Claude Code session on an
OpenAI-compatible relay ("ultrafast", deepseek-v4.1-flash-1017-ultrafast[1m],
effort max) grew to an 8.2 MB request. The relay's nginx answered it with
its own "502 Bad Gateway" page (150 B down). magpie passed that on as a
502, Claude Code asked the same body again (attempt 4/9 …), and every
`claude --resume` of the session met the same page. The two windows'
sessions were not mixed: the logs show different session ids, and the
other window's turns were 313 KB and served.

Why 8.2 MB: no duplication was found in the Anthropic → Chat translation
(buildChat puts a tool result's images once, replays reasoning_content
once); the transcript itself is that large. fastjson's escaping of < > &
adds a few percent on code-heavy text, not a multiple.

Now refitOverCap also reads as a byte-cap refusal (pageOverSize) a 413,
500, 502, 503, 504 or 520-524 whose body is a web page or empty, to a
request of 4 MiB or more. As with a vendor's own byte-cap words:
- older images are left out and it is asked again when that brings it
  under; otherwise
- it is said as a 413 request_too_large: "<provider> answered a web page
  … ; a proxy in front of it answers so for a request larger than it
  takes (HTTP 502: the request is 8.2 MB …)", with the existing hint to
  /compact. A 413 goes to the route's next member, rests nobody, and
  Claude Code doesn't ask it again unchanged.
A page is only a guess at a cap, so it isn't learned for the provider's
later requests. A small request's page stays the 502 it was. The body
is fitted for the size check only after the status and page are known,
so other errors cost nothing more.

Run (mote; package run at base 60cb1bd9e, rebased onto c7c05dcf2, which changes no internal/gateway file; the -run set again at the rebased head: ok):
- go test ./internal/gateway -run 'BigBodyProxyPage|PageOverSize|ByteCap|
  PayloadTooLarge|OverByteCap|CompactTooLarge': ok
- go test ./internal/gateway -count=1 -timeout 45m: ok (1012s)
- go vet ./internal/gateway, GOOS=linux and GOOS=windows: ok; gofmt -l: none
Broken by hand: pageOverSize always false. TestBigBodyProxyPageIsByteCap
(all 4: nginx/empty page, streamed or not) goes red with the reporter's
own error, "1017dp: 502 Bad Gateway: a web page, not an API reply:
“502 Bad Gateway”", and TestPageOverSize's true rows go red.
TestBigBodyProxyPageTriesAnotherMember passes on the old code too (a 502
already fell over); it guards that the 413 keeps doing so.
Not tried: the reporter's relay itself, or a real 8 MB Claude Code
session; the full suite runs after the push.

Reported-by: chenxiang0010 on Discord
2026-11-08 03:10:47 +08:00

100 lines
3.6 KiB
Go

package gateway
import (
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// nginx502 is the page an nginx in front of a relay serves when what it
// proxies to gave up on a request, as chenxiang0010's relay did for an
// 8.2 MB Claude Code turn.
const nginx502 = "<html>\r\n<head><title>502 Bad Gateway</title></head>\r\n<body>\r\n<center><h1>502 Bad Gateway</h1></center>\r\n<hr><center>nginx</center>\r\n</body>\r\n</html>\r\n"
// bigMessages is a Claude Code turn of about mib MiB of text, to model.
func bigMessages(model string, mib int, stream bool) string {
s := "false"
if stream {
s = "true"
}
return `{"model":"` + model + `","stream":` + s + `,"max_tokens":10,"messages":[{"role":"user","content":"` + strings.Repeat("word ", mib<<20/5) + `"}]}`
}
// A relay whose proxy answers a very large request with its own 502 page
// refused it for its size: the client is told the request is too large
// for the provider and to compact, once, not handed a 502 it asks again
// with the same body (chenxiang0010 on Discord: "Retrying … attempt 4/9",
// and every --resume of the session the same).
func TestBigBodyProxyPageIsByteCap(t *testing.T) {
for _, stream := range []bool{false, true} {
for _, page := range []struct{ name, ctype, body string }{
{"nginx", "text/html", nginx502},
{"empty", "", ""},
} {
name := page.name
if stream {
name += "/stream"
}
t.Run(name, func(t *testing.T) {
fresh(t)
up := &capped{proto: provider.Chat, most: 1 << 20, status: 502, ctype: page.ctype, body: page.body}
cappedOn(t, up)
code, body := postMessages(t, bigMessages("fake/m1", 5, stream))
if code != 413 || !strings.Contains(body, "request_too_large") {
t.Fatalf("a proxy's page for a 5 MB request: %d %.600s", code, body)
}
for _, want := range []string{"the request is 5.0 MB", "1017dp answered", "/compact"} {
if !strings.Contains(body, want) {
t.Errorf("the error lacks %q: %.600s", want, body)
}
}
if n := len(up.calls()); n != 1 {
t.Fatalf("the same body was sent %d times", n)
}
})
}
}
}
// A route's next member may take what one member's proxy turned away.
func TestBigBodyProxyPageTriesAnotherMember(t *testing.T) {
fresh(t)
a := &scripted{replies: []reply{{502, "text/html", nginx502}}}
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: "G", Members: []string{"a/m", "b/m"}, Routing: provider.Ordered}); err != nil {
t.Fatal(err)
}
body := `{"model":"group/g","messages":[{"role":"user","content":"` + strings.Repeat("word ", 5<<20/5) + `"}]}`
code, out := postAs(t, New(), "", body)
if code != 200 || !strings.Contains(out, "hello") || a.n != 1 || b.n != 1 {
t.Fatalf("the next member wasn't asked: %d %.300s (a %d, b %d)", code, out, a.n, b.n)
}
}
// A small request's page is the relay's own trouble, not its size, and
// a vendor's own words are never a page.
func TestPageOverSize(t *testing.T) {
for _, c := range []struct {
status, sent int
body string
want bool
}{
{502, 8 << 20, nginx502, true},
{413, 8 << 20, nginx413, true},
{504, 8 << 20, "", true},
{524, 8 << 20, "<!DOCTYPE html><html><head><title>A timeout occurred</title></head></html>", true},
{502, 64 << 10, nginx502, false},
{502, 8 << 20, `{"error":{"message":"upstream error"}}`, false},
{429, 8 << 20, nginx502, false},
{401, 8 << 20, "", false},
{400, 8 << 20, nginx502, false},
} {
if got := pageOverSize(c.status, []byte(c.body), c.sent); got != c.want {
t.Errorf("%d %d %.40q: %v", c.status, c.sent, c.body, got)
}
}
}