Skip to content

The agent that grows with you

fix(compression): clear preflight block on provider-confirmed rearm

Tekniumfbec3c78fd52
diff --git a/agent/conversation_loop.py b/agent/conversation_loop.py
index 0e26bc7f910..c05167d7685 100644
--- a/agent/conversation_loop.py
+++ b/agent/conversation_loop.py
@@ -3741,35 +3741,17 @@ def run_conversation(
max_compression_attempts,
)
compression_attempts = 0
-
- # ``compression_attempts`` bounds one *continuous pressure
- # episode*, not the lifetime of a tool-heavy user turn. A
- # successful provider call whose real prompt usage is back
- # below the compression threshold proves the previous
- # episode converged. Re-arm the budget and clear its
- # insufficient-progress blocker so later tool output in the
- # same turn can trigger a new episode.
- _compression_threshold = int(
- getattr(agent.context_compressor, "threshold_tokens", 0)
- or 0
- )
- if (
- (
- compression_attempts > 0
- or _preflight_compression_blocked
- or _last_preflight_pressure is not None
- )
- and prompt_tokens > 0
- and _compression_threshold > 0
- and prompt_tokens < _compression_threshold
- ):
- logger.info(
- "Compression pressure cleared at %s < %s tokens; "
- "re-arming same-turn compression budget",
- f"{prompt_tokens:,}",
- f"{_compression_threshold:,}",
- )
- compression_attempts = 0
+ # Provider-confirmed recovery also invalidates the
+ # insufficient-progress preflight state: with the
+ # prompt proven back below the threshold, a prior
+ # "insufficient progress" verdict (and the stale
+ # pressure reading it would be compared against)
+ # describes a request shape that no longer exists.
+ # Left armed, _preflight_compression_blocked keeps the
+ # pre-API gate dark for the rest of the turn even
+ # though the attempt budget was just rearmed, so a
+ # later pressure spike would grow unchecked until the
+ # provider's overflow handler fired.
_preflight_compression_blocked = False
_last_preflight_pressure = None
diff --git a/tests/run_agent/test_compression_budget_rearm.py b/tests/run_agent/test_compression_budget_rearm.py
index 93220dfae81..cf081ef75b0 100644
--- a/tests/run_agent/test_compression_budget_rearm.py
+++ b/tests/run_agent/test_compression_budget_rearm.py
@@ -121,6 +121,8 @@ def test_pre_api_compression_budget_rearms_only_after_pressure_clears(
compressor.threshold_tokens = 100
compressor.context_length = 1_000
compressor.last_prompt_tokens = -1
+ compressor._verify_compaction_cleared_threshold = False
+ compressor.awaiting_real_usage_after_compression = False
compressor.should_compress.side_effect = lambda tokens: tokens >= 100
compressor.should_compress_info.return_value = (False, None)
compressor.should_compress_preflight.return_value = False
@@ -128,14 +130,40 @@ def test_pre_api_compression_budget_rearms_only_after_pressure_clears(
compressor.get_active_compression_failure_cooldown.return_value = None
compressor.select_context.return_value = None
compressor.get_automatic_compaction_status_message.return_value = ""
+
+ def _update_from_response(usage):
+ # Mirror the real compressor: the next provider usage reading
+ # consumes the completed-compaction verification latch.
+ compressor.last_prompt_tokens = int(usage.get("prompt_tokens", 0) or 0)
+ compressor._verify_compaction_cleared_threshold = False
+ compressor.awaiting_real_usage_after_compression = False
+
+ compressor.update_from_response.side_effect = _update_from_response
agent.compression_enabled = True
agent.context_compressor = compressor
estimate_values = iter([200, 190, 200, 10])
+ _last_estimate = [10]
+
+ def _next_estimate(*_args, **_kwargs):
+ # The provider-recovery variant re-runs the pre-API preflight after
+ # fallback activation (#84733), consuming an extra estimate reading.
+ # Hold the final low-pressure value once the scripted sequence is
+ # exhausted instead of raising StopIteration.
+ try:
+ _last_estimate[0] = next(estimate_values)
+ except StopIteration:
+ pass
+ return _last_estimate[0]
+
compress_calls = []
def _fake_compress(messages, _system_message, **_kwargs):
compress_calls.append(messages)
+ # Arm the same provider-verification boundary the real compression
+ # path arms after a completed compaction.
+ compressor._verify_compaction_cleared_threshold = True
+ compressor.awaiting_real_usage_after_compression = True
return list(messages), "compressed prompt"
def _fake_execute_tool_calls(assistant_message, messages, *_args):
@@ -161,7 +189,7 @@ def test_pre_api_compression_budget_rearms_only_after_pressure_clears(
),
patch(
"agent.conversation_loop.estimate_messages_tokens_rough",
- side_effect=lambda *_args, **_kwargs: next(estimate_values),
+ side_effect=_next_estimate,
),
patch(
"agent.conversation_loop._estimate_tools_tokens_rough",