Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

gateway: an image's decode is counted wherever its header sits

yetonea9304efe016c
diff --git a/internal/gateway/codex_images.go b/internal/gateway/codex_images.go
index abf573319..fd022a975 100644
--- a/internal/gateway/codex_images.go
+++ b/internal/gateway/codex_images.go
@@ -321,28 +321,22 @@ func imageHolds(cfg image.Config, format string) (int, bool) {
return int(pixels) * bytesPerPixel, true
}
-// fitsHead is how much of an image's base64 fitHolds reads to tell its
-// header: enough for a PNG's chunks and a JPEG's segments, and little
-// enough that asking costs nothing.
-const fitsHead = 4 << 10
-
// fitHolds is what making an image of url will hold, from its header
-// alone, so that nothing is decoded to find out. An image it can't read
-// holds nothing and isn't made.
+// alone, so that nothing is decoded to find out: its pixels and the bytes
+// they came in, as codexImageRefit counts them. The header is read from
+// the base64 as a stream, as far as the format needs, so a JPEG whose
+// frame header sits behind a large EXIF segment is still counted; an
+// image whose header can't be read at all isn't made either.
func fitHolds(url string) int {
comma := strings.IndexByte(url, ',')
if comma < 0 || !strings.HasSuffix(url[:comma], ";base64") {
return 0
}
b64 := url[comma+1:]
- if len(b64) > fitsHead {
- b64 = b64[:fitsHead&^3] // whole groups of four, as base64 reads them
- }
- head, err := base64.StdEncoding.DecodeString(b64)
- if err != nil {
- return 0
+ if len(b64) > codexImageMaxBytes {
+ return 0 // codexImageRefit turns it away before decoding
}
- cfg, format, err := image.DecodeConfig(bytes.NewReader(head))
+ cfg, format, err := image.DecodeConfig(base64.NewDecoder(base64.StdEncoding, strings.NewReader(b64)))
if err != nil {
return 0
}
@@ -350,7 +344,7 @@ func fitHolds(url string) int {
if !ok {
return 0
}
- return holds
+ return min(base64.StdEncoding.DecodedLen(len(b64))+holds, holdsMost)
}
// fitEntry is what an image is sent as: data in mime, or, with no data,
diff --git a/internal/gateway/codex_images_test.go b/internal/gateway/codex_images_test.go
index bdc5dc0a3..c0e584782 100644
--- a/internal/gateway/codex_images_test.go
+++ b/internal/gateway/codex_images_test.go
@@ -610,3 +610,30 @@ func TestCodexFittedImagesLetGoWhenIdle(t *testing.T) {
t.Errorf("%d bytes held, want %d", held, want)
}
}
+
+// A JPEG's frame header can sit behind an EXIF segment of up to 64 KB, and
+// a phone photo's often does. fitHolds read only the first 4 KB of base64
+// for it, counted such an image as holding nothing, and put it in a batch
+// with three others each holding up to codexImageMaxBytes. It is counted
+// now for what it holds, wherever its header is.
+func TestCodexAHeaderBehindExifIsCounted(t *testing.T) {
+ var shot bytes.Buffer
+ if err := jpeg.Encode(&shot, shotImage(3456, 2234), &jpeg.Options{Quality: 85}); err != nil {
+ t.Fatal(err)
+ }
+ raw := shot.Bytes()
+ // SOI, then an APP1 of the most a segment holds, then the rest
+ exif := make([]byte, 0xffff-2)
+ copy(exif, "Exif\x00\x00")
+ withExif := append([]byte{0xff, 0xd8, 0xff, 0xe1, 0xff, 0xff}, exif...)
+ withExif = append(withExif, raw[2:]...)
+ url := "data:image/jpeg;base64," + base64.StdEncoding.EncodeToString(withExif)
+ if _, _, err := image.DecodeConfig(bytes.NewReader(withExif)); err != nil {
+ t.Fatalf("the photo isn't a JPEG: %v", err)
+ }
+ cfg := image.Config{ColorModel: color.YCbCrModel, Width: 3456, Height: 2234}
+ pixels, _ := imageHolds(cfg, "jpeg")
+ if got, want := fitHolds(url), len(withExif)+pixels; got != want {
+ t.Errorf("a JPEG with its header behind %d KB of EXIF holds %d MB, want %d MB", len(exif)>>10, got>>20, want>>20)
+ }
+}