The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(compression): the trigger cap covers the whole lean tail budget
Tekniuma56af5699f73
diff --git a/agent/context_compressor.py b/agent/context_compressor.pyindex 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.pyindex 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.50diff --git a/tests/agent/test_compression_tail_threshold_share.py b/tests/agent/test_compression_tail_threshold_share.pyindex 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 pfrom unittest.mock import patch+import pytest+import agent.context_compressor as ccfrom 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.mdindex 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 witchanging the selected tail policy. In `lean` mode the selection budget remains basedon the **main model's context window**: 2.5%, clamped to 10K–25K tokens, and never morethan 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 25Kselection budget even when feasibility lowers its trigger from 850K to 512K. Explicit `legacy` mode insteadrecomputes `threshold_tokens × target_ratio` (102,400 tokens at 512K × 0.20).