Skip to content

The agent that grows with you

fix(compression): probe aux feasibility before the first compaction; clear stale clamp notice on un-clamp

teknium1a37293a2ceda
diff --git a/agent/conversation_compression.py b/agent/conversation_compression.py
index b314069b1ef..c31a789ef7b 100644
--- a/agent/conversation_compression.py
+++ b/agent/conversation_compression.py
@@ -1956,6 +1956,11 @@ def check_compression_model_feasibility(agent: Any) -> None:
agent, aux_model=aux_model, aux_context=aux_context, aux_provider=_aux_cfg_provider,
aux_base_url=aux_base_url,
)
+ elif getattr(agent, "_last_feasibility_notice", None) is not None:
+ # Symmetric un-clamp: the summariser fits again, so the stale "auto-lowered" notice must not be
+ # replayed (``replay_compression_warning``) for a session that is no longer clamped (#114707).
+ agent._last_feasibility_notice = None
+ agent._compression_warning = None
except ValueError:
# Hard rejections (aux below minimum context) must propagate so the session refuses to start.
raise
@@ -1979,6 +1984,25 @@ def revalidate_compression_feasibility(agent: Any) -> None:
agent._compression_feasibility_checked = True
+def ensure_compression_feasibility_checked(agent: Any, estimated_tokens: int) -> None:
+ """Run the deferred aux feasibility probe once a request first reaches ``MINIMUM_CONTEXT_LENGTH`` — the
+ smallest window any summariser may have — so an aux clamp lands before the first compaction fires on the
+ main-window threshold instead of after it (#114707). Below that size no summariser can be too small, so
+ short sessions keep the probe-free cold start (#28957). A probe failure leaves the latch unset for the
+ lazy probe in ``compress_context`` to re-raise hard rejections."""
+ if getattr(agent, "_compression_feasibility_checked", False) or not getattr(agent, "context_compressor", None):
+ return
+ from agent.model_metadata import MINIMUM_CONTEXT_LENGTH
+ if int(estimated_tokens or 0) < MINIMUM_CONTEXT_LENGTH:
+ return
+ try:
+ check_compression_model_feasibility(agent)
+ except Exception as exc:
+ logger.debug("Compression feasibility probe deferred to the first compaction: %s", exc)
+ return
+ agent._compression_feasibility_checked = True
+
+
def replay_compression_warning(agent: Any) -> None:
"""Re-send the stored compression warning through ``status_callback``.
Called once at the start of the first ``run_conversation()``, when the gateway callback (absent during
@@ -4186,7 +4210,8 @@ def try_shrink_image_parts_in_messages(api_messages: list, *, max_dimension: int
__all__ = [
"COMPACTION_STATUS", "COMPACTION_DONE_STATUS", "COMPACTION_HEARTBEAT_STATUS", "COMPACTION_STATUS_MARKER", "is_compaction_progress_status",
- "check_compression_model_feasibility", "revalidate_compression_feasibility", "replay_compression_warning",
+ "check_compression_model_feasibility", "ensure_compression_feasibility_checked",
+ "revalidate_compression_feasibility", "replay_compression_warning",
"compress_context",
"try_shrink_image_parts_in_messages",
]
diff --git a/agent/turn_preflight.py b/agent/turn_preflight.py
index 6351182b63c..f5215f51bed 100644
--- a/agent/turn_preflight.py
+++ b/agent/turn_preflight.py
@@ -17,7 +17,7 @@ from agent.context_engine import automatic_compaction_status_message
from agent.conversation_compression import (
PRE_API_COMPRESSION_STATUS_TEMPLATE, _reset_read_dedup_caches, compression_blocked_transiently,
compression_skipped_due_to_lock, context_compression_timed_out,
- conversation_history_after_compression,
+ conversation_history_after_compression, ensure_compression_feasibility_checked,
)
from agent.turn_context import _review_fork_first_request_pending
from agent.turn_context_compaction import (
@@ -90,6 +90,9 @@ def run_preflight_compression(
and len(v.messages) > 1
and v.compression_attempts < max_compression_attempts
)
+ if _eligible:
+ # Aux clamp must land before the first compaction fires on the main-window threshold (#114707).
+ ensure_compression_feasibility_checked(agent, request_pressure_tokens)
if (
_eligible
and not _review_fork_first_request_pending(agent)
@@ -285,6 +288,8 @@ def compress_after_tool_results(
estimate_request_tokens_rough(messages, tools=agent.tools or None),
)
+ if agent.compression_enabled and compression_attempts < max_compression_attempts:
+ ensure_compression_feasibility_checked(agent, _real_tokens)
if (
agent.compression_enabled
and compression_attempts < max_compression_attempts
diff --git a/tests/agent/test_compression_feasibility.py b/tests/agent/test_compression_feasibility.py
index f79b333cb3a..4981dc2cd88 100644
--- a/tests/agent/test_compression_feasibility.py
+++ b/tests/agent/test_compression_feasibility.py
@@ -166,6 +166,57 @@ def test_fallback_activation_reprobes_aux_ceiling_and_keeps_it_durable():
assert notices == 1
+def test_unclamp_clears_stale_clamp_warning():
+ """When a re-probe finds the summariser fits again, the stale 'auto-lowered' text must not survive
+ for ``replay_compression_warning`` to resend on a session that is no longer clamped (#114707)."""
+ from agent.chat_completion_helpers import _update_fallback_context_compressor
+
+ agent = _make_agent(main_context=200_000)
+ compressor = agent.context_compressor = ContextCompressor(
+ "test-main-model", config_context_length=200_000, threshold_percent=0.50, quiet_mode=True,
+ )
+ agent._emit_status = lambda message: None
+ agent._config_context_length = None
+ agent._compression_feasibility_checked = True
+ client = MagicMock(base_url="http://localhost/v1", api_key="test-key")
+ for main_ctx, aux_ctx, label in ((1_000_000, 80_000, "big"), (100_000, 80_000, "small")):
+ agent.model = label
+ with patch("agent.auxiliary_client.get_text_auxiliary_client", return_value=(client, "aux")), \
+ patch("agent.model_metadata.get_model_context_length", side_effect=[main_ctx, aux_ctx]):
+ _update_fallback_context_compressor(agent)
+ assert compressor._aux_context_ceiling is None
+ assert compressor.threshold_tokens == 75_000
+ assert agent._compression_warning is None
+ assert agent._last_feasibility_notice is None
+
+
+def test_near_threshold_probe_clamps_before_first_compaction():
+ """A fresh instance probes once its request first reaches the smallest window any summariser may
+ have, so the aux clamp lands before the first compaction fires on the main-window threshold (#114707);
+ requests below that stay probe-free (#28957)."""
+ from agent.conversation_compression import ensure_compression_feasibility_checked
+ from agent.model_metadata import MINIMUM_CONTEXT_LENGTH
+
+ agent = _make_agent(main_context=1_000_000)
+ compressor = agent.context_compressor = ContextCompressor(
+ "test-main-model", config_context_length=1_000_000, threshold_percent=0.75, quiet_mode=True,
+ )
+ agent._emit_status = lambda message: None
+ agent._compression_feasibility_checked = False
+ client = MagicMock(base_url="http://localhost/v1", api_key="test-key")
+ with patch("agent.auxiliary_client.get_text_auxiliary_client", return_value=(client, "aux")) as aux_client, \
+ patch("agent.model_metadata.get_model_context_length", return_value=80_000):
+ ensure_compression_feasibility_checked(agent, MINIMUM_CONTEXT_LENGTH - 1)
+ aux_client.assert_not_called()
+ assert agent._compression_feasibility_checked is False
+ ensure_compression_feasibility_checked(agent, MINIMUM_CONTEXT_LENGTH)
+ ensure_compression_feasibility_checked(agent, 200_000)
+ assert aux_client.call_count == 1
+ assert agent._compression_feasibility_checked is True
+ assert compressor.threshold_tokens == 80_000
+ assert compressor.should_compress(200_000) is True
+
+
# ── Core warning logic ──────────────────────────────────────────────