The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(compression): hold only the floor when the trigger cannot fit a summary
Teknium8373d67547e3
diff --git a/agent/context_compressor.py b/agent/context_compressor.pyindex 4e9883409db..500ec52035f 100644--- a/agent/context_compressor.py+++ b/agent/context_compressor.py@@ -36,7 +36,7 @@ from agent.context_compressor_prune import ProactivePruneMixinfrom agent.context_compressor_summary import SummaryDispatchMixinfrom agent.context_compressor_retry import retryable_user_textfrom agent.context_compressor_tail import (- TAIL_MAX_THRESHOLD_FRACTION, demote_stale_tail_tools, is_demoted_stub, skill_view_names_by_call_id,+ demote_stale_tail_tools, lean_tail_budget, is_demoted_stub, skill_view_names_by_call_id,elide_keeping_skill_safety_rule, with_skill_safety_rule, without_skill_safety_rule,)from agent.context_compressor_telemetry import CompressionTelemetryMixin@@ -2232,9 +2232,7 @@ 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)))- 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))+ budget = lean_tail_budget(self.context_length, self.threshold_tokens)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 590e0ee0ec9..e819c45d505 100644--- a/agent/context_compressor_tail.py+++ b/agent/context_compressor_tail.py@@ -16,13 +16,28 @@ _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 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``). It caps the whole lean budget, the 2.5%-of-window term included.+# :func:`lean_tail_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 the 10K floor at 83% of a 12K trigger, and+# the 2.5%-of-window term at 216% of it on a 1M window, 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``).TAIL_MAX_THRESHOLD_FRACTION = 0.50+def lean_tail_budget(context_length: int, threshold_tokens: int) -> int:+ """Lean-mode tail selection budget: 2.5% of the window clamped to the 10K floor / 25K cap, then held to+ :data:`TAIL_MAX_THRESHOLD_FRACTION` of the trigger. At or below ``_MIN_SUMMARY_TOKENS`` the summary alone fills+ the trigger, so no tail leaves room under it and only the floor is held: trimming the recency window there+ would lose context without ending the loop."""+ from agent.context_compressor import _MIN_SUMMARY_TOKENS, LEAN_TAIL_CAP_TOKENS, LEAN_TAIL_FLOOR_TOKENS+ floor, window_term = LEAN_TAIL_FLOOR_TOKENS, min(LEAN_TAIL_CAP_TOKENS, int(context_length * 0.025))+ if threshold_tokens > 0:+ share = int(threshold_tokens * TAIL_MAX_THRESHOLD_FRACTION)+ floor = min(floor, share)+ if threshold_tokens > _MIN_SUMMARY_TOKENS:+ window_term = min(window_term, share)+ return max(floor, window_term)++def is_demoted_stub(content: str) -> bool:"""True for a tail tool result already replaced by ``_lean_recovery_stub`` (plus an optional reload marker)."""return len(content) < 1_000 and _DEMOTED_STUB_RE.match(content) is not Nonediff --git a/website/docs/developer-guide/context-compression-and-caching.md b/website/docs/developer-guide/context-compression-and-caching.mdindex 31fcb5fc8d0..6dd5aea303f 100644--- a/website/docs/developer-guide/context-compression-and-caching.md+++ b/website/docs/developer-guide/context-compression-and-caching.md@@ -351,7 +351,7 @@ changing the selected tail policy. In `lean` mode the selection budget remains bon 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 asmall 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+half of a compression trigger above the 2K minimum summary budget (a `threshold_tokens` of 12K on a 1M window would otherwise keep a 25K tail, soevery 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 instead