Skip to content

The agent that grows with you

fix(compression): the trigger cap covers the whole lean tail budget

Tekniuma56af5699f73
diff --git a/agent/context_compressor.py b/agent/context_compressor.py
index db7541d869f..ea73e5bb3e9 100644
--- a/agent/context_compressor.py
+++ b/agent/context_compressor.py
@@ -2232,10 +2232,9 @@ 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.
- 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)))
+ budget = max(LEAN_TAIL_FLOOR_TOKENS, min(LEAN_TAIL_CAP_TOKENS, int(self.context_length * 0.025)))
+ if self.threshold_tokens > 0: # floor AND window term: 25K at 1M outgrew a 12K/30K trigger
+ budget = min(budget, int(self.threshold_tokens * TAIL_MAX_THRESHOLD_FRACTION))
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 37e655633ac..bc8bc493990 100644
--- a/agent/context_compressor_tail.py
+++ b/agent/context_compressor_tail.py
@@ -16,10 +16,10 @@ _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
+# ``ContextCompressor.tail_token_budget``: the lean tail 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.
+# window-side twin is ``TAIL_MAX_CONTEXT_FRACTION``). It caps the whole lean budget, the 2.5%-of-window term included.
TAIL_MAX_THRESHOLD_FRACTION = 0.50
diff --git a/tests/agent/test_compression_tail_threshold_share.py b/tests/agent/test_compression_tail_threshold_share.py
index 1b65e7f7bc5..2b57b3e08d0 100644
--- a/tests/agent/test_compression_tail_threshold_share.py
+++ b/tests/agent/test_compression_tail_threshold_share.py
@@ -6,6 +6,8 @@ tail, so head + summary + tail stayed over the 12K trigger and every follow-up p
from unittest.mock import patch
+import pytest
+
import agent.context_compressor as cc
from agent.context_compressor import ContextCompressor, _estimate_msg_budget_tokens
@@ -42,3 +44,16 @@ def test_retained_tail_leaves_half_the_configured_trigger_free():
def test_unconfigured_trigger_keeps_the_lean_floor():
assert _make(131_072, threshold_tokens=None).tail_token_budget == cc.LEAN_TAIL_FLOOR_TOKENS
+
+
[email protected]("ctx, threshold", [(400_000, 12_000), (1_000_000, 12_000), (1_000_000, 30_000)])
+def test_large_window_retained_tail_stays_well_under_the_configured_trigger(ctx, threshold):
+ """The 2.5%-of-window recency term (25K at 1M) must not outgrow a configured trigger either: the kept tail stays
+ within half the trigger plus the soft ceiling's whole-row overrun (1.5x), not at or past the trigger."""
+ comp = _make(ctx, threshold_tokens=threshold)
+ assert comp.threshold_tokens == threshold
+ msgs = _chatty_transcript(16)
+ 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 <= threshold * 3 // 4, (start, end, tail_tokens)
diff --git a/website/docs/developer-guide/context-compression-and-caching.md b/website/docs/developer-guide/context-compression-and-caching.md
index 77a66ba2494..31fcb5fc8d0 100644
--- a/website/docs/developer-guide/context-compression-and-caching.md
+++ b/website/docs/developer-guide/context-compression-and-caching.md
@@ -350,7 +350,9 @@ A smaller auxiliary compression model can lower the live compression trigger wit
changing the selected tail policy. In `lean` mode the selection budget remains based
on the **main model's context window**: 2.5%, clamped to 10K–25K tokens, and never more
than 20% of that window (the 10K floor alone is 61% of a 16K local window, so without the cap a
-small model's "protected" tail was the whole request and compaction reclaimed nothing). For example,
+small model's "protected" tail was the whole request and compaction reclaimed nothing), nor more than
+half of the compression trigger (a `threshold_tokens` of 12K on a 1M window would otherwise keep a 25K tail, so
+every pass stayed over the trigger and refused as "would grow"). For example,
a 1M main model with a 512K auxiliary model retains a 25K
selection budget even when feasibility lowers its trigger from 850K to 512K. Explicit `legacy` mode instead
recomputes `threshold_tokens × target_ratio` (102,400 tokens at 512K × 0.20).