Files
magpie/internal
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
..
…
…
…
…
…
…
…