The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(vision): parse the ftyp compatible-brand list; don't gate AVIF on pillow-heif
Sam Painterf5a7dc14c74d
diff --git a/tests/tools/test_image_source.py b/tests/tools/test_image_source.pyindex 0b7745f01a0..f28e6565091 100644--- a/tests/tools/test_image_source.py+++ b/tests/tools/test_image_source.py@@ -399,11 +399,21 @@ class TestLazySandboxBringUp:await isrc.resolve_image_source(str(secret), isrc.ResolveContext(task_id="t1"))-# HEIF/HEIC/AVIF ISO-BMFF headers: 'ftyp' box at bytes 4-8, major brand at 8-12.+# HEIF/HEIC/AVIF ISO-BMFF headers: 'ftyp' box at bytes 4-8, major brand at 8-12,+# minor version at 12-16, then a list of 4-byte compatible brands.# 0x18-byte box length prefix mirrors a real iPhone HEIC (\x00\x00\x00\x18 ftyp...).HEIC_HEADER = b"\x00\x00\x00\x18ftypheic\x00\x00\x00\x00mif1heic" + b"\x00" * 32AVIF_HEADER = b"\x00\x00\x00\x1cftypavif\x00\x00\x00\x00avifmif1" + b"\x00" * 32MIF1_HEADER = b"\x00\x00\x00\x18ftypmif1\x00\x00\x00\x00mif1heic" + b"\x00" * 32+# AVIF encoders routinely stamp the generic still-image brand 'mif1' as the+# MAJOR brand and declare the AV1 codec only in the compatible-brand list, so a+# major-brand-only sniff mislabels these as HEVC-coded HEIC.+MIF1_MAJOR_AVIF_COMPATIBLE = (+ b"\x00\x00\x00\x1cftypmif1\x00\x00\x00\x00mif1avif" + b"\x00" * 32+)+MIF1_MAJOR_AV01_COMPATIBLE = (+ b"\x00\x00\x00 ftypmif1\x00\x00\x00\x00mif1av01avif" + b"\x00" * 32+)class TestHeicDetection:@@ -423,6 +433,47 @@ class TestHeicDetection:from tools.vision_tools import _detect_image_mime_type_from_bytesassert _detect_image_mime_type_from_bytes(AVIF_HEADER) == "image/avif"+ def test_mif1_major_with_avif_compatible_brand_is_avif(self):+ """Regression: a 'mif1'-major file that lists 'avif' among its+ compatible brands is AV1-coded and must be reported as AVIF. Sniffing+ only the major brand labeled every mif1 file HEIC, which routes an AVIF+ to the HEVC error text and mis-reports the format to the caller."""+ from tools.vision_tools import _detect_image_mime_type_from_bytes+ assert _detect_image_mime_type_from_bytes(+ MIF1_MAJOR_AVIF_COMPATIBLE) == "image/avif"++ def test_mif1_major_with_av01_compatible_brand_is_avif(self):+ """Same as above for the 'av01' codec brand, deeper in the list."""+ from tools.vision_tools import _detect_image_mime_type_from_bytes+ assert _detect_image_mime_type_from_bytes(+ MIF1_MAJOR_AV01_COMPATIBLE) == "image/avif"++ def test_mif1_major_with_heic_compatible_stays_heic(self):+ """The compatible-brand scan must not over-trigger: a genuine HEVC-coded+ HEIF (mif1 major, heic compatible, no AV1 brand) is still HEIC."""+ from tools.vision_tools import _detect_image_mime_type_from_bytes+ assert _detect_image_mime_type_from_bytes(MIF1_HEADER) == "image/heic"++ def test_brand_scan_does_not_read_past_the_ftyp_box(self):+ """The scan is bounded by the declared ftyp box size, so an 'avif' token+ sitting in a FOLLOWING box must not upgrade a HEIC to AVIF."""+ from tools.vision_tools import _detect_image_mime_type_from_bytes+ # ftyp box is exactly 0x18 bytes (major mif1 + one compatible 'heic');+ # the next box then contains the literal bytes 'avif'.+ hdr = (+ b"\x00\x00\x00\x18ftypmif1\x00\x00\x00\x00heic"+ b"\x00\x00\x00\x10metaavif" + b"\x00" * 32+ )+ assert _detect_image_mime_type_from_bytes(hdr) == "image/heic"++ def test_truncated_ftyp_header_does_not_crash(self):+ """A short/garbage read must return a value, not raise."""+ from tools.vision_tools import _detect_image_mime_type_from_bytes+ assert _detect_image_mime_type_from_bytes(b"\x00\x00\x00\x18ftyp") is None+ # Bogus (huge) declared box size must fall back to the sniffed window.+ assert _detect_image_mime_type_from_bytes(+ b"\xff\xff\xff\xffftypmif1\x00\x00\x00\x00mif1avif") == "image/avif"+def test_ftyp_with_unknown_brand_not_misdetected(self):"""An ISO-BMFF ftyp box that isn't an image brand (e.g. mp4) staysunrecognized — we must not claim every ftyp container is an image."""@@ -474,3 +525,53 @@ class TestHeicDetection:path, mime, err = vt._normalize_to_supported_image(heic, "image/heic")assert path is Noneassert "pillow-heif" in err++ def test_avif_converts_without_pillow_heif(self, tmp_path, monkeypatch):+ """Regression: AVIF must NOT be gated on pillow-heif.++ Pillow >= 11.3 bundles a native AvifImagePlugin, while pillow-heif+ wheels are commonly built with no AV1 codec at all (libheif_info()+ reports ``AVIF: ''``). Gating AVIF on a pillow-heif import therefore+ rejected files Pillow could already decode and told the user to install+ a library that cannot decode them. With pillow-heif absent, a real AVIF+ must still normalize to PNG.+ """+ pytest.importorskip("PIL")+ from PIL import Image, features+ if not features.check("avif"):+ pytest.skip("this Pillow build has no native AVIF codec")+ from tools import vision_tools as vt+ _reload(monkeypatch, tmp_path / "hermes")++ avif = tmp_path / "photo.avif"+ Image.new("RGB", (8, 8), (10, 180, 90)).save(str(avif), format="AVIF")++ import builtins+ real_import = builtins.__import__++ def _no_heif(name, *args, **kwargs):+ if name == "pillow_heif":+ raise ImportError("pillow_heif not installed")+ return real_import(name, *args, **kwargs)++ with patch.object(builtins, "__import__", side_effect=_no_heif):+ path, mime, err = vt._normalize_to_supported_image(avif, "image/avif")++ assert err is None, f"AVIF was rejected without pillow-heif: {err}"+ assert mime == "image/png"+ assert path.read_bytes().startswith(b"\x89PNG\r\n\x1a\n")+ path.unlink()++ def test_avif_error_names_av1_not_just_pillow_heif(self, tmp_path, monkeypatch):+ """When an AVIF genuinely cannot be decoded, the guidance must mention+ the AV1/Pillow path — not blame pillow-heif alone, which frequently+ ships without any AV1 codec."""+ from tools import vision_tools as vt+ _reload(monkeypatch, tmp_path / "hermes")+ # Valid AVIF brand, but the payload is not decodable by anything.+ broken = tmp_path / "broken.avif"+ broken.write_bytes(AVIF_HEADER)++ path, mime, err = vt._normalize_to_supported_image(broken, "image/avif")+ assert path is None+ assert "AV1" in err or "Pillow" in err