Skip to content

The agent that grows with you

fix(compression): lean tail floor never claims more than half a configured trigger

Teknium44e9779659a1
diff --git a/agent/context_compressor.py b/agent/context_compressor.py
index 8cc5ed6938f..f1241d50dda 100644
--- a/agent/context_compressor.py
+++ b/agent/context_compressor.py
@@ -36,7 +36,8 @@ from agent.context_compressor_prune import ProactivePruneMixin
from agent.context_compressor_summary import SummaryDispatchMixin
from agent.context_compressor_retry import retryable_user_text
from agent.context_compressor_tail import (
- demote_stale_tail_tools, is_demoted_stub, skill_view_names_by_call_id, with_skill_safety_rule,
+ TAIL_MAX_THRESHOLD_FRACTION, demote_stale_tail_tools, is_demoted_stub, skill_view_names_by_call_id,
+ with_skill_safety_rule,
)
from agent.context_compressor_telemetry import CompressionTelemetryMixin
from agent.error_classifier import FailoverReason, classify_api_error
@@ -2231,7 +2232,10 @@ class ContextCompressor(
if self._tail_token_budget is None:
if getattr(self, "tail_mode", "lean") == "lean":
# Lean mode: tail is a small clamped recency window; the summary carries continuity.
- budget = max(LEAN_TAIL_FLOOR_TOKENS, min(LEAN_TAIL_CAP_TOKENS, int(self.context_length * 0.025)))
+ floor = LEAN_TAIL_FLOOR_TOKENS
+ if self.threshold_tokens > 0:
+ floor = min(floor, int(self.threshold_tokens * TAIL_MAX_THRESHOLD_FRACTION))
+ budget = max(floor, min(LEAN_TAIL_CAP_TOKENS, int(self.context_length * 0.025)))
else:
budget = int(self.threshold_tokens * self.summary_target_ratio)
if self.context_length > 0:
diff --git a/agent/context_compressor_tail.py b/agent/context_compressor_tail.py
index 6f01f0ab9f8..dff5299d590 100644
--- a/agent/context_compressor_tail.py
+++ b/agent/context_compressor_tail.py
@@ -16,6 +16,12 @@ _LEAN_TAIL_DEMOTE_MIN_CHARS = 1_500
# id and a reload marker the stub can pass the Phase-1 prune floor, and re-summarising it would drop both pointers.
_DEMOTED_STUB_RE = re.compile(r"\[[^\s\]]* output demoted at compaction — [\d,]+ chars preserved in session history\.")
+# ``ContextCompressor.tail_token_budget``: the lean 10K floor never claims more than this share of the trigger. A
+# configured ``threshold_tokens`` (or aux summariser ceiling) far below the window left that floor at 83% of a 12K
+# trigger, so head + summary + tail stayed over threshold and every pre-API pass refused as "would grow" (the
+# window-side twin is ``TAIL_MAX_CONTEXT_FRACTION``). The 2.5%-of-window recency window is not clamped.
+TAIL_MAX_THRESHOLD_FRACTION = 0.50
+
def is_demoted_stub(content: str) -> bool:
"""True for a tail tool result already replaced by ``_lean_recovery_stub`` (plus an optional reload marker)."""
diff --git a/tests/agent/test_compression_tail_threshold_share.py b/tests/agent/test_compression_tail_threshold_share.py
new file mode 100644
index 00000000000..1b65e7f7bc5
--- /dev/null
+++ b/tests/agent/test_compression_tail_threshold_share.py
@@ -0,0 +1,44 @@
+"""A configured trigger far below the window must leave compaction room under that trigger.
+
+``compression.threshold_tokens: 12000`` on a 64K window: the lean 10K tail floor used to keep ~10K of verbatim
+tail, so head + summary + tail stayed over the 12K trigger and every follow-up pass reclaimed nothing.
+"""
+
+from unittest.mock import patch
+
+import agent.context_compressor as cc
+from agent.context_compressor import ContextCompressor, _estimate_msg_budget_tokens
+
+
+def _make(ctx: int, threshold_tokens: int | None) -> ContextCompressor:
+ with patch.object(cc, "get_model_context_length", return_value=ctx):
+ comp = ContextCompressor(
+ model="test/model", threshold_percent=0.50, protect_first_n=1, protect_last_n=2,
+ quiet_mode=True, threshold_tokens_cap=threshold_tokens,
+ )
+ _ = comp.context_length # resolve while the window mock is active
+ return comp
+
+
+def _chatty_transcript(turns: int) -> list[dict]:
+ """Short user prompts, long assistant replies: the shape where a few rows fill the whole tail."""
+ msgs: list[dict] = [{"role": "system", "content": "sys"}]
+ for i in range(turns):
+ msgs.append({"role": "user", "content": f"HISTORY_{i} " + "preserve these facts " * 100})
+ msgs.append({"role": "assistant", "content": f"ACK_{i} " + "historical detail " * 700})
+ msgs.append({"role": "user", "content": "next " + "preserve these facts " * 100})
+ return msgs
+
+
+def test_retained_tail_leaves_half_the_configured_trigger_free():
+ comp = _make(64_000, threshold_tokens=12_000)
+ assert comp.threshold_tokens == 12_000
+ msgs = _chatty_transcript(4)
+ start, end = comp._compress_window(msgs)
+ tail_tokens = sum(_estimate_msg_budget_tokens(m) for m in msgs[end:])
+ assert end > start
+ assert tail_tokens <= comp.threshold_tokens // 2 + _estimate_msg_budget_tokens(msgs[-2]), (start, end, tail_tokens)
+
+
+def test_unconfigured_trigger_keeps_the_lean_floor():
+ assert _make(131_072, threshold_tokens=None).tail_token_budget == cc.LEAN_TAIL_FLOOR_TOKENS