Skip to content

The agent that grows with you

fix(agent): derive tile-aware shrink cap from Codex patch-budget 400s

liuhao1024cabe430a2516
diff --git a/agent/turn_recovery.py b/agent/turn_recovery.py
index be2a46378f3..1d543327586 100644
--- a/agent/turn_recovery.py
+++ b/agent/turn_recovery.py
@@ -10,6 +10,7 @@ mutate ``agent`` / ``messages`` / ``api_messages`` in place. Logger name stays
from __future__ import annotations
import logging
+import math
import re
import time
from dataclasses import dataclass
@@ -60,6 +61,17 @@ def _image_error_max_dimension(error: Exception) -> Optional[int]:
except Exception:
pass
text = " ".join(parts).lower()
+ # OpenAI Codex Responses reports a tile-patch budget (ceil(w/32)×ceil(h/32))
+ # instead of a pixel ceiling. A square image is the worst case for the budget,
+ # so a per-side cap of isqrt(limit)*32 px keeps isqrt(limit)² ≤ limit — for the
+ # 30000-patch ceiling that is 5536 px. Without this the caller falls back to
+ # 8000 px and a 6000 px image that already exceeds the budget is skipped (#106337).
+ if "patches after processing" in text:
+ match = re.search(r"exceeding the limit of\s*(\d{2,7})", text)
+ if not match:
+ return None
+ max_dimension = math.isqrt(int(match.group(1))) * 32
+ return max_dimension if 512 <= max_dimension <= 8000 else None
if "image" not in text or "dimension" not in text or "max allowed size" not in text:
return None
match = re.search(r"max allowed size(?:\s+for [^:]+)?:\s*(\d{3,5})\s*pixels?", text)
diff --git a/tests/run_agent/test_image_shrink_recovery.py b/tests/run_agent/test_image_shrink_recovery.py
index 62df5e28cdc..7d724d13c7d 100644
--- a/tests/run_agent/test_image_shrink_recovery.py
+++ b/tests/run_agent/test_image_shrink_recovery.py
@@ -107,6 +107,64 @@ 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).
+ """
+
+ def test_codex_patch_budget_maps_to_tile_aware_cap(self):
+ err = _FakeApiError(
+ status_code=400,
+ message=(
+ "The image you provided requires 33174 patches after processing, "
+ "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
+
+
# ─── Shrink helper ───────────────────────────────────────────────────────────