Skip to content

The agent that grows with you

fix(anthropic): the screenshot-eviction floor is satisfiability-aware on the wire pass too

teknium1593a2f1074d2
diff --git a/agent/anthropic_message_convert.py b/agent/anthropic_message_convert.py
index f8b94d4318e..6e5c0d13c2d 100644
--- a/agent/anthropic_message_convert.py
+++ b/agent/anthropic_message_convert.py
@@ -610,8 +610,8 @@ def _evict_old_screenshots(result: List[Dict[str, Any]]) -> None:
The ceiling is counted in image blocks, so a ``tool_result`` holding several
screenshots weighs several blocks, and user-uploaded images are reserved against the
- limit without ever being rewritten. ``_MAX_KEEP_SCREENSHOTS`` is a floor: when reserved
- uploads alone fill the ceiling the newest frames still reach the model.
+ limit without ever being rewritten. ``_MAX_KEEP_SCREENSHOTS`` is a satisfiability
+ floor: only when reserved uploads alone fill the ceiling do the newest frames stay.
"""
reserved = sum(
1
@@ -636,7 +636,13 @@ def _evict_old_screenshots(result: List[Dict[str, Any]]) -> None:
# Extend by whole batches: this runs statelessly on every request, so a fixed one-batch
# retire would stop enforcing the limit after the first batch, and an exact
# "limit minus batch" target would move the frontier on every new screenshot.
- max_retire = max(len(carriers) - _MAX_KEEP_SCREENSHOTS, 0)
+ # The keep floor only shelters a breach that retiring every screenshot cannot fix
+ # (reserved uploads alone over the ceiling); one tool_result carrying 25 frames is
+ # fixable and must not hide behind a floor counted in carriers.
+ if reserved <= _OUTBOUND_IMAGE_LIMIT:
+ max_retire = len(carriers)
+ else:
+ max_retire = max(len(carriers) - _MAX_KEEP_SCREENSHOTS, 0)
retire = 0
while retire < max_retire:
retire = min(retire + _SCREENSHOT_EVICTION_BATCH, max_retire)
diff --git a/tests/tools/test_computer_use.py b/tests/tools/test_computer_use.py
index f81dcd9d4f2..7c63dbf11ae 100644
--- a/tests/tools/test_computer_use.py
+++ b/tests/tools/test_computer_use.py
@@ -542,6 +542,26 @@ class TestAnthropicAdapterMultimodal:
]
assert survivors == [f"t{i}" for i in range(_SCREENSHOT_EVICTION_BATCH, n)]
+ def test_floor_yields_when_one_carrier_breaches_the_block_limit(self):
+ """The keep floor shelters only breaches eviction cannot fix.
+
+ One tool_result carrying more image blocks than the ceiling is a single carrier;
+ a floor of three counted in carriers would retire nothing and ship a request the
+ API rejects. With no reserved uploads the breach is fixable, so it must be fixed.
+ """
+ from agent.anthropic_message_convert import _OUTBOUND_IMAGE_LIMIT, _evict_old_screenshots
+
+ img = {"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": "A"}}
+ result = [{
+ "role": "user",
+ "content": [{
+ "type": "tool_result", "tool_use_id": "t0",
+ "content": [dict(img) for _ in range(_OUTBOUND_IMAGE_LIMIT + 5)],
+ }],
+ }]
+ _evict_old_screenshots(result)
+ assert not any(x.get("type") == "image" for x in result[0]["content"][0]["content"])
+
def test_eviction_frontier_holds_between_batch_advances(self):
"""Screenshot eviction must not rewrite a new block on every capture.