NeoPengZhile's Codex (signed in to ChatGPT, through /backend-api/codex/
responses) got "413 Payload Too Large: request body exceeds gateway
limit" on all five reconnects in a long session that had read many
images; a new session worked. Codex sends the whole conversation every
turn, screenshots inline as base64, zstd-compressed; magpie's limit is on
the decoded body and was 128 MB, while magpie compresses a ChatGPT turn
again on the way out, so the backend sees about what Codex alone would
send. The message didn't say whose limit it was or what to do.
What changed:
- request_bounds.go: defaultBodyLimit 128 MB -> 512 MB (decoded, which
also sets the zstd decoder's memory cap). Every route that reads with
the default gets it, keyLimited's read among them, so a limited key
isn't held to the old number first. Routes with their own lower limit
(System One 16 MB, a Claude subscription's 16 MB) keep it.
- overLimit: magpie's own 413 says "request body is over magpie's
gateway limit of 512 MB (decoded); the provider was not asked".
- gateway.go writeError: the hint added to a 413 now says it is a byte
limit, not the model's context, and to /compact (Codex, Claude Code) or
start a new conversation.
- request_bounds_test.go: TestRequestBodyTooLarge uses defaultBodyLimit+1
in place of a literal 129 MB.
- docs/subsystems/gateway-routing.md says so.
Run: go test ./internal/gateway/ -count=1 (ok, 354s), with the new
TestImageHeavyCodexTurnIsRead (a zstd body of inline images decoding to
136 MB on Codex's backend path, read with the default limits) and
TestOverLimitSaysWhoseAndWhatToDo; go test ./internal/gui/ (ok); go vet
./... on darwin, GOOS=linux -tags nogui and GOOS=windows; gofmt -l.
Broken by hand: the limit back at 128 MB -> TestImageHeavyCodexTurnIsRead
fails with the 413; the old "exceeds gateway limit" message, or the old
hint, -> TestOverLimitSaysWhoseAndWhatToDo fails.
Not tried: the reporter's real session, or how big a turn the ChatGPT
backend itself takes; the test's images repeat, so its wire size is far
smaller than real screenshots'. Peak memory for a turn near 512 MB was
not measured.
Only the Codex backend path decoded a Content-Encoding (codexReader);
/v1/responses, /v1/chat/completions, /v1/messages, count_tokens, Gemini's
routes, embeddings, images, videos and System One read the compressed
bytes as JSON. Codex sends zstd to a custom provider's /v1/responses by
default. readBoundedRequestBody now always decodes (decodedBody, moved
from codexReader): identity, gzip and zstd are read, the header is
dropped so the provider gets plain JSON, the size limit holds for the
decoded bytes (413), and any other encoding is refused 415 naming it.
keyLimited reads a limited key's compressed body the same way, so its
reservation is the decoded size, not the compressed one.
Verified: request_encoding_test.go posts real gzip and zstd bodies to a
running httptest server of New().Handler() on every route. Without the
change TestEncodedBodiesOnEveryRoute and TestEncodedEmbeddingsBody fail
with 400 "expected a JSON object" / "the body isn't a JSON object",
TestEncodedBodyInflatedPastLimit gets 400 instead of 413,
TestUnknownBodyEncodingRefused gets 400/502 instead of 415, and with only
key_limit.go reverted TestKeyLimitReservesDecodedBody fails "held at 19
tokens for a 40061-byte body sent as 79 bytes". gofmt; go vet -tags nogui
./internal/gateway for GOOS linux, windows, darwin; go test -tags nogui
./internal/gateway (temp HOME, pinned caches) ok; the new tests -race
-count=5 ok. Not tried with the real Codex CLI.