Skip to content

The agent that grows with you

fix(anthropic): give the wire image-eviction pass the 24 MB request budget

salch-cred8ad29a890864
diff --git a/agent/anthropic_message_convert.py b/agent/anthropic_message_convert.py
index 39080ebf8cf..111bd035d96 100644
--- a/agent/anthropic_message_convert.py
+++ b/agent/anthropic_message_convert.py
@@ -634,10 +634,12 @@ def _evict_old_screenshots(result: list[dict[str, Any]]) -> None:
Mutates ``result`` in place. The block ceiling is conditional on the request's actual image
dimensions (the strict 20 only when some image exceeds the many-image per-side cap), so this
- pass reads the headers of the ``source.data`` payloads it already holds. The auxiliary Anthropic
- client (``agent.auxiliary_client`` via ``anthropic_adapter.build_anthropic_kwargs``) reaches
- this pass without the compressor's send-path pass, so it must hold the invariant alone. Policy
- and rationale: :mod:`agent.image_eviction_policy`.
+ pass reads the headers of the ``source.data`` payloads it already holds. It sizes the block
+ budget AND the byte budget, the latter because the auxiliary Anthropic client
+ (``agent.auxiliary_client`` via ``anthropic_adapter.build_anthropic_kwargs``) reaches this pass
+ without the compressor's send-path pass, so it must hold the invariant alone -- previously with
+ no request-size guard at all (post-#113953 review). Policy and rationale:
+ :mod:`agent.image_eviction_policy`.
"""
reserved = [
(block, _image_source_data(block))
@@ -663,7 +665,13 @@ def _evict_old_screenshots(result: list[dict[str, Any]]) -> None:
]
)
retire = outbound_image_retire_count(
- [len(images) for _, images in carriers], len(reserved), many_image_safe=many_image_safe
+ [len(images) for _, images in carriers],
+ len(reserved),
+ carrier_bytes_newest_first=[
+ sum(len(data) for _, data in images if data) for _, images in carriers
+ ],
+ reserved_bytes=sum(len(data) for _, data in reserved if data),
+ many_image_safe=many_image_safe,
)
for block, _ in carriers[len(carriers) - retire:]:
placeholder = _text_block("[screenshot removed to save context]")
diff --git a/tests/agent/test_image_eviction_policy.py b/tests/agent/test_image_eviction_policy.py
index decdafab484..d90da1380a7 100644
--- a/tests/agent/test_image_eviction_policy.py
+++ b/tests/agent/test_image_eviction_policy.py
@@ -17,6 +17,7 @@ from agent.anthropic_message_convert import _evict_old_screenshots
from agent.context_compressor import evict_stale_outbound_tool_images
from agent.image_eviction_policy import (
IMAGE_EVICTION_BATCH,
+ OUTBOUND_IMAGE_BUDGET_BYTES,
OUTBOUND_IMAGE_FLOOR,
OUTBOUND_IMAGE_LIMIT,
outbound_image_retire_count,
@@ -179,6 +180,44 @@ def test_one_oversized_image_keeps_the_strict_trigger():
assert _wire_image_blocks(wire) == OUTBOUND_IMAGE_LIMIT - IMAGE_EVICTION_BATCH + 1
+def test_wire_pass_enforces_the_byte_budget_below_the_block_ceiling():
+ """The aux/MoA Anthropic path reaches the wire pass alone, and that pass used to have no
+ request-size guard: 10 image blocks is half the block ceiling, but 30 MB of base64 is over
+ Anthropic's 32 MB Messages limit once text and JSON framing are added. Byte pressure must
+ retire carriers even while the block count is comfortably fine."""
+ big = "Q" * (3 * MB) # ~2.2 MB decoded per image, unreadable header -> strict trigger
+ result: list[dict] = [
+ {
+ "role": "user",
+ "content": [
+ {"type": "image", "source": {"type": "base64", "media_type": "image/jpeg", "data": big}}
+ ],
+ }
+ ]
+ for i in range(9):
+ result.append({"role": "assistant", "content": [{"type": "text", "text": "s"}]})
+ result.append(
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "tool_result",
+ "tool_use_id": f"t{i}",
+ "content": [
+ {"type": "image", "source": {"type": "base64", "media_type": "image/jpeg", "data": big}}
+ ],
+ }
+ ],
+ }
+ )
+ blocks_before = _wire_image_blocks(result)
+ assert 10 * len(big) > OUTBOUND_IMAGE_BUDGET_BYTES and blocks_before < OUTBOUND_IMAGE_LIMIT
+ _evict_old_screenshots(result)
+ assert _wire_image_blocks(result) < blocks_before, (
+ "the wire pass ignored byte pressure while under the block ceiling"
+ )
+
+
def test_dimension_classification_reads_headers_and_fails_closed():
"""The ceiling may only relax on dimensions this module can actually read.