fix(agent): align preflight token-progress floor to 5% (#23767, #39548)

Follow-up to the salvaged preflight token-progress fix: require a material
(>5%) token reduction to count as progress, matching the overflow-handler
retry path (conversation_loop.py, #39550), so a sub-5% wobble can't keep the
3-pass preflight loop spinning. Adds boundary + zero-token regression tests.
This commit is contained in:
kshitijk4poor 2026-06-22 15:51:52 +05:30
parent b08ee8ad04
commit 69de0360a1
2 changed files with 29 additions and 3 deletions

View file

@ -47,8 +47,14 @@ def _compression_made_progress(
context window. See issue #39548 for an observed case: 220 → 220
messages, ~288k ~183k tokens on a 1M-context model still triggered
auto-reset.
The token reduction must be *material* (>5%) to count as progress the
same floor the overflow-handler retry path uses (conversation_loop.py,
#39550) — so a sub-5% wobble doesn't keep the multi-pass loop spinning.
"""
return new_len < orig_len or new_tokens < orig_tokens
if new_len < orig_len:
return True
return orig_tokens > 0 and new_tokens < orig_tokens * 0.95
@dataclass

View file

@ -7,8 +7,9 @@ estimated request token count without removing any rows — and surfaces a
spurious ``Context length exceeded`` failure followed by an auto-reset of
an otherwise healthy session.
These tests pin the contract of ``_compression_made_progress``: either a
row-count reduction OR a token-count reduction counts as progress.
These tests pin the contract of ``_compression_made_progress``: a
row-count reduction OR a *material* (>5%) token-count reduction counts as
progress.
"""
from __future__ import annotations
@ -64,3 +65,22 @@ class TestCompressionMadeProgress:
assert _compression_made_progress(
orig_len=10, new_len=5, orig_tokens=1000, new_tokens=1100
) is True
def test_sub_5pct_token_drop_is_not_progress(self):
"""A token reduction below the 5% material floor does NOT count as
progress matching the overflow-handler retry path (#39550) so a
marginal wobble can't keep the multi-pass loop spinning."""
# 1000 -> 970 is a 3% drop, below the 5% floor.
assert _compression_made_progress(
orig_len=10, new_len=10, orig_tokens=1000, new_tokens=970
) is False
# 1000 -> 940 is a 6% drop, above the floor.
assert _compression_made_progress(
orig_len=10, new_len=10, orig_tokens=1000, new_tokens=940
) is True
def test_zero_orig_tokens_is_not_progress(self):
"""Degenerate estimate (0 tokens) must not be read as a token win."""
assert _compression_made_progress(
orig_len=10, new_len=10, orig_tokens=0, new_tokens=0
) is False