Skip to content

The agent that grows with you

fix(compression): count a would-grow refusal as an ineffective strike

liuhao102462016a1b0a40
diff --git a/agent/context_compressor.py b/agent/context_compressor.py
index 03639cf985a..8c33b9be6c9 100644
--- a/agent/context_compressor.py
+++ b/agent/context_compressor.py
@@ -2373,6 +2373,31 @@ class ContextCompressor(ContextEngine):
self._ineffective_compression_count = count
self._persist_ineffective_compression_count()
+ def record_rejected_compaction(self) -> None:
+ """Record one compaction whose result was REJECTED before committing.
+
+ The anti-growth guard in the commit layer (conversation_compression)
+ discards a candidate that would grow the transcript and keeps the
+ original. Without recording the attempt, the anti-thrash breaker
+ never sees a strike, so automatic compression retries the SAME
+ unchanged transcript on every turn — same summary request, same
+ refusal, same user-facing warning (#88568). This counts one
+ ineffective strike (persisted, so the normal >= 2 latch and its
+ recovery window apply) WITHOUT arming post-compaction real-usage
+ verification — nothing was committed, so there is no new compaction
+ to verify — and without touching the fallback-summary streak (no
+ summary was accepted).
+ """
+ self._record_ineffective_compression_verdict(
+ self._ineffective_compression_count + 1
+ )
+ if not self.quiet_mode:
+ logger.warning(
+ "Compaction rejected before commit (would grow the "
+ "transcript); ineffective_compression_count=%d",
+ self._ineffective_compression_count,
+ )
+
def record_completed_compaction(
self, *, used_fallback: bool = False, feasibility_skip: bool = False,
) -> None:
diff --git a/agent/conversation_compression.py b/agent/conversation_compression.py
index ba142416b3b..77a8a4ab930 100644
--- a/agent/conversation_compression.py
+++ b/agent/conversation_compression.py
@@ -3425,6 +3425,20 @@ def compress_context(
split_status="aborted",
failure_class="would_grow",
)
+ # Record the rejected attempt as an ineffective
+ # compaction strike so the anti-thrash breaker latches
+ # after the normal threshold. Without this, the unchanged
+ # transcript stays over the compression threshold and
+ # automatic compression retries the identical summary
+ # request on every turn (#88568). Manual /compress keeps
+ # bypassing the latch (force=True skips the guards).
+ try:
+ agent.context_compressor.record_rejected_compaction()
+ except Exception:
+ logger.debug(
+ "could not record rejected-compaction strike",
+ exc_info=True,
+ )
_release_lock()
return messages, _existing_sp
diff --git a/tests/agent/test_compaction_anti_thrash.py b/tests/agent/test_compaction_anti_thrash.py
index a8626681588..457f28a5c32 100644
--- a/tests/agent/test_compaction_anti_thrash.py
+++ b/tests/agent/test_compaction_anti_thrash.py
@@ -203,3 +203,47 @@ class TestMinimumMessagesBranch:
assert len(out) == len(msgs), "nothing should have been compressed"
assert cc._last_compression_made_progress is False
assert cc._ineffective_compression_count == before + 1
+
+
+class TestRejectedCompactionStrike:
+ """#88568 — a would-grow refusal must count as an ineffective strike.
+
+ The anti-growth guard correctly keeps the original transcript, but the
+ rejection used to leave ``_ineffective_compression_count`` untouched, so
+ the breaker never latched and automatic compression retried the SAME
+ unchanged transcript on every turn.
+ """
+
+ def test_rejected_compaction_increments_strike(self):
+ cc = _compressor(threshold_tokens=1)
+ assert cc._ineffective_compression_count == 0
+
+ cc.record_rejected_compaction()
+
+ assert cc._ineffective_compression_count == 1
+
+ def test_two_rejections_stop_further_automatic_compression(self):
+ cc = _compressor(threshold_tokens=1)
+ cc.record_rejected_compaction()
+ cc.record_rejected_compaction()
+
+ # The latch consumers key on the counter itself (>= 2 blocks);
+ # pin the counter and the recovery-clock arming side effect.
+ assert cc._ineffective_compression_count >= 2
+
+ def test_rejection_does_not_arm_real_usage_verification(self):
+ """Nothing was committed, so the next response must not be scored
+ against the pre-rejection transcript (that verdict belongs to
+ committed compactions only)."""
+ cc = _compressor(threshold_tokens=1)
+ cc.record_rejected_compaction()
+
+ assert cc._verify_compaction_cleared_threshold is False
+
+ def test_rejection_leaves_fallback_streak_untouched(self):
+ cc = _compressor(threshold_tokens=1)
+ cc._fallback_compression_streak = 1
+
+ cc.record_rejected_compaction()
+
+ assert cc._fallback_compression_streak == 1