Skip to content

The agent that grows with you

fix(agent): a batch retire that would blind the model stops at the keep-newest floor

teknium109d68f8a65bf
diff --git a/agent/anthropic_message_convert.py b/agent/anthropic_message_convert.py
index 6e5c0d13c2d..7e3ac0b8419 100644
--- a/agent/anthropic_message_convert.py
+++ b/agent/anthropic_message_convert.py
@@ -639,14 +639,21 @@ def _evict_old_screenshots(result: List[Dict[str, Any]]) -> None:
# 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)
+ floor = min(_MAX_KEEP_SCREENSHOTS, len(carriers))
+
+ def _fits(kept: int) -> bool:
+ return reserved + sum(n for _, n in carriers[:kept]) <= _OUTBOUND_IMAGE_LIMIT
+
+ max_retire = len(carriers) - floor if not _fits(0) else len(carriers)
retire = 0
while retire < max_retire:
- retire = min(retire + _SCREENSHOT_EVICTION_BATCH, max_retire)
- if reserved + sum(n for _, n in carriers[: len(carriers) - retire]) <= _OUTBOUND_IMAGE_LIMIT:
+ step = min(retire + _SCREENSHOT_EVICTION_BATCH, max_retire)
+ if step > len(carriers) - floor and _fits(floor):
+ # A whole batch would retire the frames the model was just asked about while
+ # keeping the floor already clears the ceiling; take the smaller edit instead.
+ step = len(carriers) - floor
+ retire = step
+ if _fits(len(carriers) - retire):
break
for block, _ in carriers[-retire:] if retire else []:
placeholder = _text_block("[screenshot removed to save context]")
diff --git a/agent/context_compressor.py b/agent/context_compressor.py
index abb9a536085..6c096105cca 100644
--- a/agent/context_compressor.py
+++ b/agent/context_compressor.py
@@ -1330,8 +1330,10 @@ def _outbound_image_retire_count(
then cannot fix the request and would only blind the model on the frames it was just
asked about, which is worse than the stricter dimension cap the block limit avoids.
Whenever retiring tool content CAN bring the request inside the ceiling, it does so,
- even past the floor — and byte pressure never yields to the floor at all, because the
- request-size limit is hard and the provider answers 413.
+ past the floor only when keeping the floor does not fit — a batch step that would blind
+ the model while the floor alone clears the ceiling is cut back to the floor. Byte
+ pressure never yields to the floor, because the request-size limit is hard and the
+ provider answers 413.
"""
total = len(block_counts_newest_first)
@@ -1347,17 +1349,22 @@ def _outbound_image_retire_count(
if _fits(total):
return 0
- # The floor may only shelter a violation that retiring tool content cannot fix.
- floor = max(keep_newest, 0)
- if _fits(0) or not _bytes_fit(floor):
- # Eviction can clear the ceiling, or bytes breach it even at the floor: no shelter.
- max_retire = total
+ floor = min(max(keep_newest, 0), total)
+ if not _fits(0) and _bytes_fit(floor):
+ # Reserved uploads alone breach the block ceiling: no retirement can fix it, so the
+ # newest frames stay rather than blinding the model for nothing.
+ max_retire = total - floor
else:
- max_retire = max(total - floor, 0)
+ max_retire = total
retire = 0
while retire < max_retire:
- retire = min(retire + batch, max_retire)
+ step = min(retire + batch, max_retire)
+ if step > total - floor and _fits(floor):
+ # A whole batch would retire the frames the model was just asked about while
+ # keeping the floor already clears the ceiling; take the smaller edit instead.
+ step = total - floor
+ retire = step
if _fits(total - retire):
break
return retire
diff --git a/tests/agent/test_outbound_stale_vision.py b/tests/agent/test_outbound_stale_vision.py
index 51149826d06..e3cb6406cca 100644
--- a/tests/agent/test_outbound_stale_vision.py
+++ b/tests/agent/test_outbound_stale_vision.py
@@ -266,6 +266,33 @@ class TestOutboundStaleVisionEviction:
evict_stale_outbound_tool_images(outbound)
assert len(_image_bearing_tool_ids(outbound)) == _MAX_KEEP_TOOL_IMAGES
+ def test_a_batch_that_would_blind_the_model_stops_at_the_floor(self):
+ """A whole-batch retire must not take the newest frames when the floor already fits.
+
+ Fifteen reserved uploads leave five block slots; the sixth screenshot breaches, and
+ one eight-wide batch would retire all six tool frames -- including the one the model
+ was just asked about -- although keeping the newest three already clears the limit.
+ """
+ uploads = _OUTBOUND_IMAGE_LIMIT - 5
+ history: list[dict] = [
+ {
+ "role": "user",
+ "content": [
+ {"type": "text", "text": "look"},
+ *[
+ {"type": "image_url", "image_url": {"url": f"data:image/png;base64,U{k}"}}
+ for k in range(uploads)
+ ],
+ ],
+ }
+ ]
+ for i in range(6):
+ history.extend(_image_tool(i))
+ outbound = sanitize_api_messages(history)
+ evict_stale_outbound_tool_images(outbound)
+ assert _outbound_image_blocks(outbound) <= _OUTBOUND_IMAGE_LIMIT
+ assert _image_bearing_tool_ids(outbound) == [f"call_{i}" for i in range(3, 6)]
+
def test_byte_pressure_overrides_the_keep_newest_floor(self):
"""A hard request-size breach must not be preserved by the floor.
diff --git a/tests/tools/test_computer_use.py b/tests/tools/test_computer_use.py
index 7c63dbf11ae..273f04105b6 100644
--- a/tests/tools/test_computer_use.py
+++ b/tests/tools/test_computer_use.py
@@ -562,6 +562,23 @@ class TestAnthropicAdapterMultimodal:
_evict_old_screenshots(result)
assert not any(x.get("type") == "image" for x in result[0]["content"][0]["content"])
+ def test_a_batch_that_would_blind_the_model_stops_at_the_floor(self):
+ """Fifteen reserved uploads plus six one-frame tool_results: one eight-wide batch would
+ retire every frame although keeping the newest three already clears the ceiling."""
+ 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": [dict(img) for _ in range(_OUTBOUND_IMAGE_LIMIT - 5)]}]
+ for i in range(6):
+ result.append({"role": "assistant", "content": [{"type": "tool_use", "id": f"t{i}", "name": "s", "input": {}}]})
+ result.append({"role": "user", "content": [{"type": "tool_result", "tool_use_id": f"t{i}", "content": [dict(img)]}]})
+ _evict_old_screenshots(result)
+ kept = [
+ b["tool_use_id"] for m in result for b in m["content"]
+ if b.get("type") == "tool_result" and any(x.get("type") == "image" for x in b["content"])
+ ]
+ assert kept == ["t3", "t4", "t5"]
+
def test_eviction_frontier_holds_between_batch_advances(self):
"""Screenshot eviction must not rewrite a new block on every capture.