Skip to content

The agent that grows with you

fix(agent): byte pressure narrows the upload-breach quantum instead of collapsing to the floor

Tekniumb95b7bd69c58
diff --git a/agent/image_eviction_policy.py b/agent/image_eviction_policy.py
index 5e90ba00ea7..1e538208774 100644
--- a/agent/image_eviction_policy.py
+++ b/agent/image_eviction_policy.py
@@ -180,9 +180,9 @@ def outbound_image_retire_count(
The floor is a SATISFIABILITY floor: it shelters the newest frames only when reserved
uploads 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. 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.
+ count stays a step function: it retires whole quanta down toward the floor (keeping
+ ``floor`` to ``floor + batch - 1`` carriers, fewer when the byte budget binds), so the
+ cached prefix is rewritten once per quantum rather than on every new image.
"""
if limit is None:
limit = outbound_image_block_limit(many_image_safe)
@@ -204,23 +204,22 @@ def outbound_image_retire_count(
return 0
floor = min(max(floor, 0), total)
- 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).
+ # fit it, so shelter the floor but keep advancing in whole quanta. Retiring down to
+ # exactly ``total - floor`` here moves the frontier on every new image (#133999). The
+ # quantum is capped by how many carriers the byte budget lets us keep, so byte
+ # pressure narrows the step instead of collapsing it back to the floor.
+ byte_window = max(k for k in range(floor, total + 1) if _bytes_fit(k))
+ quantum = max(1, min(batch, byte_window - floor + 1))
excess = total - floor
- retire = excess - excess % step
- while not _bytes_fit(total - retire):
- retire = min(retire + step, excess)
- return retire
- max_retire = total
+ return excess - excess % quantum
window = max(k for k in range(total + 1) if _fits(k)) if _fits(0) else 0
quantum = max(1, min(batch, window - floor))
retire = 0
- while retire < max_retire:
- retire = min(retire + quantum, max_retire)
+ while retire < total:
+ retire = min(retire + quantum, total)
if _fits(total - retire):
break
return retire
diff --git a/tests/agent/test_image_eviction_policy.py b/tests/agent/test_image_eviction_policy.py
index fad44d9cdd5..31c9e29d42d 100644
--- a/tests/agent/test_image_eviction_policy.py
+++ b/tests/agent/test_image_eviction_policy.py
@@ -270,12 +270,19 @@ def test_unfixable_upload_breach_still_retires_in_batches():
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_FLOOR
+def test_unfixable_upload_breach_under_byte_pressure_fits_and_still_steps():
+ """The hold never keeps a request over the byte budget, and byte pressure narrows the
+ step instead of collapsing to the floor (which would move the frontier every image)."""
+ span = range(1, 31)
+ counts = [
+ outbound_image_retire_count(
+ [1] * n, OUTBOUND_IMAGE_LIMIT + 1, carrier_bytes_newest_first=[3 * MB] * n, reserved_bytes=6 * MB
+ )
+ for n in span
+ ]
+ kept = [n - c for n, c in zip(span, counts)]
+ assert all(6 * MB + 3 * MB * k <= 24 * MB for k in kept)
+ assert all(k >= min(n, OUTBOUND_IMAGE_FLOOR) for n, k in zip(span, kept))
+ # bytes allow 6 carriers, floor is 3 -> a 4-carrier quantum, so at most one move per 4 images
+ moves = sum(a != b for a, b in itertools.pairwise(counts))
+ assert moves <= -(-(len(span) - OUTBOUND_IMAGE_FLOOR) // 4)