Skip to content

The agent that grows with you

fix(compression): release the reclamation no-op dedup key on every rearm reset

kshitijk4poor58a4a11727c7
diff --git a/agent/context_compressor.py b/agent/context_compressor.py
index 4a592cf2ee3..7da5df06b85 100644
--- a/agent/context_compressor.py
+++ b/agent/context_compressor.py
@@ -2380,7 +2380,7 @@ class ContextCompressor(ContextEngine):
self._last_compression_telemetry = None
self._active_compression_telemetry = None
self._compression_telemetry_seed = None
- self._proactive_prune_rearm_tokens = 0
+ self._reset_proactive_prune_rearm()
# Micro-compaction state reset
self._micro_compact_cursor = 0
@@ -2685,7 +2685,7 @@ class ContextCompressor(ContextEngine):
self._last_compression_telemetry = None
self._active_compression_telemetry = None
self._compression_telemetry_seed = None
- self._proactive_prune_rearm_tokens = 0
+ self._reset_proactive_prune_rearm()
def bind_session_state(self, session_db: Any = None, session_id: str = "") -> None:
"""Bind the current session row so durable cooldowns can round-trip."""
@@ -2700,7 +2700,7 @@ class ContextCompressor(ContextEngine):
self._prellm_skip_count = 0
self._anti_thrash_recovery_deadline = 0.0
self._structural_no_op_backoff_until = 0.0
- self._proactive_prune_rearm_tokens = 0
+ self._reset_proactive_prune_rearm()
self.get_active_compression_failure_cooldown()
self._load_fallback_compression_streak()
self._load_ineffective_compression_count()
@@ -3315,7 +3315,7 @@ class ContextCompressor(ContextEngine):
# sizes. Same durable-sync discipline as the strike reset above: clear
# the model_config copy too, so a restart doesn't resurrect a runway
# this recalibration just voided.
- self._proactive_prune_rearm_tokens = 0
+ self._reset_proactive_prune_rearm()
self._clear_durable_proactive_prune_rearm()
# When the MINIMUM_CONTEXT_LENGTH floor meets/exceeds a small context
@@ -4446,6 +4446,18 @@ class ContextCompressor(ContextEngine):
return result, pruned
+ def _reset_proactive_prune_rearm(self) -> None:
+ """Fully rearm the proactive prune and let a future lockout warn again.
+
+ Every path that zeroes the rearm mark (compaction, session
+ reset/end/rebind, model recalibration) is a reclamation or a fresh
+ start, so the over-threshold no-op dedup key must not survive it —
+ otherwise an identical lockout after a full compaction (rearm back
+ at 0) would be silent (#101889).
+ """
+ self._proactive_prune_rearm_tokens = 0
+ self._last_reclaim_block_warn = None
+
def _billed_basis_over_threshold(self, current_tokens: "int | None") -> bool:
"""Whether a provider-billed reading says the session is over threshold.
@@ -4476,10 +4488,15 @@ class ContextCompressor(ContextEngine):
the log to explain it. Silent below the threshold (a declined prune
there is ordinary hysteresis, not a lockout). Deduped on
``reason`` + the rearm snapshot so a busy tool loop logs once per
- distinct state, not once per iteration; the key is cleared whenever a
- prune commits so a later lockout warns again.
+ distinct state, not once per iteration; the key is cleared whenever
+ the session drops back under threshold or any reclamation resets the
+ rearm mark (prune commit, compaction, session reset/rebind, model
+ recalibration) so a later lockout warns again.
"""
- if not self._billed_basis_over_threshold(current_tokens):
+ if current_tokens is None or not self._billed_basis_over_threshold(
+ current_tokens
+ ):
+ self._last_reclaim_block_warn = None
return
key = (reason, int(self._proactive_prune_rearm_tokens))
if self._last_reclaim_block_warn == key:
@@ -8888,7 +8905,7 @@ This compaction should PRIORITISE preserving all information related to the focu
self._micro_compact_cursor = 0
self._micro_compact_consecutive_failures = 0
self._micro_compact_last_failure_cursor = -1
- self._proactive_prune_rearm_tokens = 0
+ self._reset_proactive_prune_rearm()
return compressed
diff --git a/tests/agent/test_proactive_prune_rearm_threshold.py b/tests/agent/test_proactive_prune_rearm_threshold.py
index 2e3c0766d43..16cca30f0c8 100644
--- a/tests/agent/test_proactive_prune_rearm_threshold.py
+++ b/tests/agent/test_proactive_prune_rearm_threshold.py
@@ -187,3 +187,53 @@ def test_under_threshold_no_op_is_not_warned(caplog) -> None:
if r.levelno >= logging.WARNING
and "over the compression threshold" in r.getMessage()
]
+
+
+def _over_threshold_warnings(caplog) -> list:
+ return [
+ r for r in caplog.records
+ if r.levelno >= logging.WARNING
+ and "over the compression threshold" in r.getMessage()
+ ]
+
+
+def test_lockout_warns_again_after_rearm_reset(caplog) -> None:
+ """A full compaction (or session rebind / model recalibration) zeroes the
+ rearm mark. An identical lockout afterwards must warn again — the dedup key
+ must not outlive the reclamation that should have cleared it."""
+ c = _compressor(proactive_prune_min_reclaim_tokens=10_000_000)
+ msgs = _history()
+ billed = c.threshold_tokens + 5_000
+
+ with caplog.at_level(logging.WARNING, logger="agent.context_compressor"):
+ c.prune_tool_results_only(msgs, current_tokens=billed)
+ assert len(_over_threshold_warnings(caplog)) == 1
+ # Same state, same key (reason, rearm=0): deduped.
+ with caplog.at_level(logging.WARNING, logger="agent.context_compressor"):
+ c.prune_tool_results_only(msgs, current_tokens=billed)
+ assert len(_over_threshold_warnings(caplog)) == 1
+
+ # Every path that fully rearms the prune goes through this helper
+ # (compress(), on_session_reset/end, bind_session_state, update_model).
+ c._reset_proactive_prune_rearm()
+ assert c._proactive_prune_rearm_tokens == 0
+
+ with caplog.at_level(logging.WARNING, logger="agent.context_compressor"):
+ c.prune_tool_results_only(msgs, current_tokens=billed)
+ assert len(_over_threshold_warnings(caplog)) == 2, (
+ "lockout after a rearm reset was deduped against the stale key"
+ )
+
+
+def test_dropping_under_threshold_clears_dedup_key(caplog) -> None:
+ """Back under threshold (e.g. compaction elsewhere shrank the request), the
+ key is released so the next over-threshold lockout is reported."""
+ c = _compressor(proactive_prune_min_reclaim_tokens=10_000_000)
+ msgs = _history()
+ billed = c.threshold_tokens + 5_000
+
+ with caplog.at_level(logging.WARNING, logger="agent.context_compressor"):
+ c.prune_tool_results_only(msgs, current_tokens=billed)
+ c.prune_tool_results_only(msgs, current_tokens=c.threshold_tokens - 1)
+ c.prune_tool_results_only(msgs, current_tokens=billed)
+ assert len(_over_threshold_warnings(caplog)) == 2
diff --git a/tests/run_agent/test_proactive_prune_loop_wiring.py b/tests/run_agent/test_proactive_prune_loop_wiring.py
index 486865ad905..259bbbb25b6 100644
--- a/tests/run_agent/test_proactive_prune_loop_wiring.py
+++ b/tests/run_agent/test_proactive_prune_loop_wiring.py
@@ -192,8 +192,13 @@ class TestProactivePruneLoopWiring:
def test_should_compress_true_but_skipped_is_warned(self, agent):
"""``should_compress_info`` says RUN (``(True, None)``) yet this branch
was taken — the per-turn compression budget is spent. Over threshold
- with no reclamation running must not be swallowed silently (#101889)."""
- agent.context_compressor.should_compress.return_value = False
+ with no reclamation running must not be swallowed silently (#101889).
+
+ Faithful to the real engine: ``should_compress()`` is
+ ``should_compress_info()[0]``, so the only way into this branch with
+ ``(True, None)`` is an exhausted per-turn budget."""
+ agent.max_compression_attempts = 0 # budget already spent this turn
+ agent.context_compressor.should_compress.return_value = True
agent.context_compressor.should_compress_info.return_value = (True, None)
agent.context_compressor.prune_tool_results_only = (
lambda messages, current_tokens=None: (messages, 0)