Skip to content

The agent that grows with you

test(agent): trim Codex patch-budget tests to two invariants

Teknium14a6a75ebed5
diff --git a/tests/run_agent/test_image_shrink_recovery.py b/tests/run_agent/test_image_shrink_recovery.py
index 7d724d13c7d..5d8083c039c 100644
--- a/tests/run_agent/test_image_shrink_recovery.py
+++ b/tests/run_agent/test_image_shrink_recovery.py
@@ -107,14 +107,18 @@ class TestImageTooLargeClassification:
assert result.reason != FailoverReason.image_too_large
-class TestImagePatchBudgetMaxDimension:
- """_image_error_max_dimension must derive a tile-aware per-side cap from a
- Codex patch-budget rejection instead of falling back to the 8000 px default
- — a 6000 px image exceeds the 30000-patch ceiling yet sails under 8000 px,
- so the default cap would leave it unshrunk and burn the single retry (#106337).
- """
+class TestImagePatchBudgetShrink:
+ def test_codex_patch_budget_shrinks_responses_image_under_budget(self):
+ """The Codex 400 names a tile-patch budget, not a pixel ceiling: a 5444x6200 PNG
+ (33174 patches) sails under the 8000 px default cap and would be left unshrunk,
+ burning the single retry (#106337). The cap derived from the rejection must make the
+ real shrink pass rewrite the Responses ``input_image`` part to within the budget."""
+ import io
+
+ from PIL import Image
+
+ from agent.conversation_compression import try_shrink_image_parts_in_messages
- def test_codex_patch_budget_maps_to_tile_aware_cap(self):
err = _FakeApiError(
status_code=400,
message=(
@@ -122,48 +126,23 @@ class TestImagePatchBudgetMaxDimension:
"exceeding the limit of 30000. Please resize the image and try again."
),
)
- # isqrt(30000) = 173 tiles per side → 173*32 = 5536 px; 173² = 29929 ≤ 30000.
- assert _image_error_max_dimension(err) == 5536
-
- def test_codex_patch_budget_non_square_worst_case_still_under_budget(self):
- import math
- cap = _image_error_max_dimension(_FakeApiError(
- status_code=400,
- message="requires 33174 patches after processing, exceeding the limit of 30000",
- ))
- tiles = math.ceil(cap / 32)
- assert tiles * tiles <= 30000
-
- def test_patch_wording_without_limit_returns_none(self):
- err = _FakeApiError(
- status_code=400,
- message="image patches after processing are too numerous",
- )
- assert _image_error_max_dimension(err) is None
-
- def test_non_patch_limit_of_wording_not_misread(self):
- """"exceeding the limit of N" without the patch vocabulary must stay None."""
- err = _FakeApiError(
- status_code=400,
- message="request exceeds the limit of 100 images per minute",
- )
- assert _image_error_max_dimension(err) is None
-
- def test_patch_budget_below_floor_returns_none(self):
- """A pathologically tiny budget maps under the 512 px floor → None (8000 fallback)."""
- err = _FakeApiError(
- status_code=400,
- message="requires 90 patches after processing, exceeding the limit of 81",
- )
- assert _image_error_max_dimension(err) is None
-
- def test_anthropic_pixel_ceiling_path_unchanged(self):
- err = _FakeApiError(
- status_code=400,
- message="image dimensions exceed max allowed size: 8000 pixels per side",
- )
- assert _image_error_max_dimension(err) == 8000
-
+ cap = _image_error_max_dimension(err)
+ assert cap is not None and cap < 8000
+
+ buf = io.BytesIO()
+ Image.new("RGB", (5444, 6200), (200, 30, 30)).save(buf, format="PNG")
+ url = "data:image/png;base64," + base64.b64encode(buf.getvalue()).decode()
+ msgs = [{"role": "user", "content": [{"type": "input_image", "image_url": url}]}]
+
+ assert try_shrink_image_parts_in_messages(msgs, max_dimension=cap) is True
+ shrunk = msgs[0]["content"][0]["image_url"]
+ w, h = Image.open(io.BytesIO(base64.b64decode(shrunk.split(",", 1)[1]))).size
+ assert -(-w // 32) * -(-h // 32) <= 30000
+
+ # "limit of N" without the patch vocabulary is not a dimension ceiling.
+ assert _image_error_max_dimension(_FakeApiError(
+ status_code=400, message="request exceeds the limit of 100 images per minute",
+ )) is None
# ─── Shrink helper ───────────────────────────────────────────────────────────