The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(agent): image eviction keeps batching when uploads alone breach the ceiling (#133999)
arcabotaid58998124909
diff --git a/agent/image_eviction_policy.py b/agent/image_eviction_policy.pyindex adc6edf837e..5e90ba00ea7 100644--- a/agent/image_eviction_policy.py+++ b/agent/image_eviction_policy.py@@ -179,7 +179,10 @@ def outbound_image_retire_count(The floor is a SATISFIABILITY floor: it shelters the newest frames only when reserveduploads alone breach the block ceiling (no retirement can fix that), and never under byte- pressure — the request-size limit is hard and the provider answers 413.+ pressure — the request-size limit is hard and the provider answers 413. Even then the+ count stays a step function: it retires whole batches down toward the floor (keeping+ ``floor`` to ``floor + batch - 1`` carriers), so the cached prefix is rewritten once per+ batch rather than on every new image."""if limit is None:limit = outbound_image_block_limit(many_image_safe)@@ -201,9 +204,17 @@ def outbound_image_retire_count(return 0floor = min(max(floor, 0), total)- max_retire = total - floor if not _fits(0) and _bytes_fit(floor) else total- if max_retire <= 0:- return 0+ step = max(batch, 1)+ if not _fits(0) and _bytes_fit(floor):+ # Reserved uploads alone breach the block ceiling: no retirement can make the request+ # fit it, so shelter the floor but keep advancing in whole batches. Retiring down to+ # exactly ``total - floor`` here moves the frontier on every new image (#133999).+ excess = total - floor+ retire = excess - excess % step+ while not _bytes_fit(total - retire):+ retire = min(retire + step, excess)+ return retire+ max_retire = totalwindow = max(k for k in range(total + 1) if _fits(k)) if _fits(0) else 0quantum = max(1, min(batch, window - floor))diff --git a/tests/agent/test_image_eviction_policy.py b/tests/agent/test_image_eviction_policy.pyindex d90da1380a7..fad44d9cdd5 100644--- a/tests/agent/test_image_eviction_policy.py+++ b/tests/agent/test_image_eviction_policy.py@@ -110,8 +110,9 @@ def _wire_image_blocks(result: list[dict]) -> int:([OUTBOUND_IMAGE_LIMIT + 5], 0, None, 0, 1),# four 10-block carriers: retire until it fits, not everything([10, 10, 10, 10], 0, None, 0, 2),- # uploads alone breach: unfixable, keep the floor- ([1] * 5, OUTBOUND_IMAGE_LIMIT + 1, None, 0, 5 - OUTBOUND_IMAGE_FLOOR),+ # uploads alone breach: unfixable, shelter the floor, retire only whole batches+ ([1] * 5, OUTBOUND_IMAGE_LIMIT + 1, None, 0, 0),+ ([1] * (OUTBOUND_IMAGE_FLOOR + IMAGE_EVICTION_BATCH), OUTBOUND_IMAGE_LIMIT + 1, None, 0, IMAGE_EVICTION_BATCH),# uploads alone breach with fewer carriers than the floor: nothing to gain([1, 1], OUTBOUND_IMAGE_LIMIT + 5, None, 0, 0),# byte pressure from uploads: floor never shelters a 413@@ -252,3 +253,29 @@ def test_quantum_shrinks_to_the_fit_window_for_heavy_carriers():assert all(3 * (n - c) <= OUTBOUND_IMAGE_LIMIT for n, c in zip(range(window + 1, window + 10), counts))moves = sum(a != b for a, b in itertools.pairwise(counts))assert moves <= len(counts) // (window - OUTBOUND_IMAGE_FLOOR)+++def test_unfixable_upload_breach_still_retires_in_batches():+ """#133999: when reserved uploads alone exceed the block ceiling, retiring tool images+ cannot make the request fit, so the retire count must still be a step function of the+ carrier count. Retiring to exactly ``total - floor`` would rewrite the cached prefix on+ every new tool image."""+ uploads = 51+ span = range(1, 31)+ counts = [outbound_image_retire_count([1] * n, uploads) for n in span]+ kept = [n - c for n, c in zip(span, counts)]+ assert all(k >= min(n, OUTBOUND_IMAGE_FLOOR) for n, k in zip(span, kept))+ assert all(k < OUTBOUND_IMAGE_FLOOR + IMAGE_EVICTION_BATCH for k in kept)+ moves = sum(a != b for a, b in itertools.pairwise(counts))+ assert moves <= -(-(len(span) - OUTBOUND_IMAGE_FLOOR) // IMAGE_EVICTION_BATCH)+++def test_unfixable_upload_breach_still_honours_the_byte_budget():+ """The batch hold never keeps a request over the byte budget: it advances further (down+ to the floor at most) until the kept carriers fit."""+ sizes = [3 * MB] * 10+ retire = outbound_image_retire_count(+ [1] * 10, OUTBOUND_IMAGE_LIMIT + 1, carrier_bytes_newest_first=sizes, reserved_bytes=10 * MB+ )+ assert 10 * MB + 3 * MB * (10 - retire) <= 24 * MB+ assert 10 - retire >= OUTBOUND_IMAGE_FLOORdiff --git a/tests/agent/test_outbound_stale_vision.py b/tests/agent/test_outbound_stale_vision.pyindex 736d5d0016e..cabbb6b95c2 100644--- a/tests/agent/test_outbound_stale_vision.py+++ b/tests/agent/test_outbound_stale_vision.py@@ -316,7 +316,9 @@ class TestOutboundStaleVisionEviction:history.extend(_image_tool(i))outbound = sanitize_api_messages(history)evict_stale_outbound_tool_images(outbound)- assert len(_image_bearing_tool_ids(outbound)) == OUTBOUND_IMAGE_FLOOR+ # At least the floor survives; retirement still moves in whole batches (#133999).+ kept = len(_image_bearing_tool_ids(outbound))+ assert OUTBOUND_IMAGE_FLOOR <= kept < OUTBOUND_IMAGE_FLOOR + IMAGE_EVICTION_BATCHdef 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.