A Codex turn's inline image is decoded, resized and written again before
it goes to ChatGPT (codexImagesFit). What a decode may hold was judged in
pixels: codexImageMaxPixels, 80<<20. A pixel is not a byte, and a
compressed image declares its size before it carries any pixels, so a
header of a few dozen bytes commanded the allocation: 110 bytes declaring
8000x8000 at 16 bits a channel held 488 MB, and four of them in one
request held 1710 MB (fitWorkers makes 4 at once, with no budget for what
they hold between them). The decode then fails and the image goes as it
came, so the bytes were spent for nothing. With a few agents sending such
a turn at once the process is one the system kills.
The bytes a decode will hold are now what is judged, from the header
alone, before anything is decoded (imageHolds: the pixel count times the
bytes its format's image allocates a pixel — 4 for anything with colour or
alpha, 1, 2 or 8 as image/png's readImagePass allocates them, in int64 so
that 70000x70000 can't wrap). codexImageMaxBytes keeps that at 320 MB,
which is what 80<<20 pixels was taken as, so every screenshot Codex sends
is fitted as before: 3456x2234 holds 29 MB, 4032x3024 46 MB, 6000x4000
91 MB. What one request makes at once is batched against fitBudget
(512 MB), so a request of 100 screenshots holds what 4 do and not what 100
do.
Verified at this head, with `-tags nogui`:
- New tests fail without the change. With codexImageRefit's two gates and
the batching put back to the old behaviour (helpers kept, so nothing
fails to compile):
TestCodexAHeaderAloneCantHoldPastTheBudget — "8000x8000 at 16 bits a
channel: 110 bytes of upload held 488 MB, past the 320 MB a decode may
hold"; TestCodexFourHeadersInOneRequestCantHoldTheHeap — "1299 bytes of
upload, sent as 4 images in one request, held 1710 MB, past 128 MB".
Both pass with the change.
- The first version of the header test asserted only that the image went
as it came, and it passed on the old code, because the old decode also
failed — after spending the bytes. The tests measure what the decode
allocates (runtime.MemStats TotalAlloc either side) instead.
- TestCodexFittingHoldsTheBudget covers the batching's edges, including an
image that holds more than the budget by itself (made alone, turned away
by codexImageRefit).
- TestCodexRealScreenshotsFitBytesTheyHold and
TestCodexFittingLeavesATurnThatFitsAsItWas: a real screenshot is neither
turned away nor made differently.
- go test -race -tags nogui -run TestCodex ./internal/gateway: pass
(228s). go test -tags nogui -run TestCodex ./internal/gateway: pass.
- gofmt clean; go vet -tags nogui ./internal/gateway, GOOS=linux go vet
-tags nogui, GOOS=windows go vet: clean. go build -tags nogui,
GOOS=windows go build: pass.
- Full go test -tags nogui ./...: fails in internal/agent (aside, cursor,
copilot, qoder, omp, zed), internal/cli (TestKeyNote) and
internal/gateway (TestClaudeClientCacheAbsentUnchanged). Each was
reproduced on origin/main dee5c9d7 with this change stashed, so none is
this change's; they read the PATH for pnpm, cursor-agent and copilot,
and two runs of the agent package failed different sets.
Found by an AI scan of the image paths, not by a report.
Not changed here: the same judgement is missing on other paths this scan
touched (draw.go's image requests, and holdWriter.wantWhole skipping
holdMost), which want issues of their own.