Skip to content

The agent that grows with you

fix(cli): model-switch warning quotes the capped compression trigger

fangliquanflqb8a77fa92c24
diff --git a/agent/context_compressor.py b/agent/context_compressor.py
index 52a9c0457c3..f4f87a100c1 100644
--- a/agent/context_compressor.py
+++ b/agent/context_compressor.py
@@ -2312,6 +2312,17 @@ class ContextCompressor(SummaryDispatchMixin, MicroCompactionMixin, ContextEngin
logger.debug("compression cancellation check failed", exc_info=True)
return False
+ def preview_threshold_tokens(self, model: str, context_length: int, provider: str = "") -> int:
+ """The trigger ``update_model`` would install for ``model``/``context_length``, without mutating
+ state — the model-switch guard quotes it in its preflight-compression warning."""
+ config_percent = getattr(self, "_config_threshold_percent", self.threshold_percent)
+ base_percent = resolve_model_threshold(model, self.model_thresholds, config_percent, provider)
+ threshold_percent = self._effective_threshold_percent(context_length, base_percent)
+ threshold = self._compute_threshold_tokens(context_length, threshold_percent, self.max_tokens)
+ if self.threshold_tokens_cap is not None and self.threshold_tokens_cap > 0:
+ threshold = min(threshold, self.threshold_tokens_cap, context_length)
+ return threshold
+
def update_model(
self, model: str, context_length: int, base_url: str = "", api_key: Any = "", provider: str = "",
api_mode: str = "", max_tokens: int | None = None,
diff --git a/hermes_cli/context_switch_guard.py b/hermes_cli/context_switch_guard.py
index a7e1defba25..703c5d07560 100644
--- a/hermes_cli/context_switch_guard.py
+++ b/hermes_cli/context_switch_guard.py
@@ -15,8 +15,13 @@ def _append_warning(result: ModelSwitchResult, text: str) -> None:
result.warning_message = text
-def _threshold_tokens(context_length: int, threshold_percent: float) -> int:
- return max(int(context_length * threshold_percent), MINIMUM_CONTEXT_LENGTH)
+def _threshold_tokens(compressor: Any, model: str, context_length: int, provider: str = "") -> int:
+ """The trigger the compressor WILL use after the switch (cap, model_thresholds and small-window
+ floor included), so the warning quotes the real number; duck-typed engines keep the plain ratio."""
+ preview = getattr(compressor, "preview_threshold_tokens", None)
+ if callable(preview):
+ return int(preview(model, context_length, provider))
+ return max(int(context_length * float(getattr(compressor, "threshold_percent", 0.5))), MINIMUM_CONTEXT_LENGTH)
def _estimate_tokens(agent: Any, messages: Optional[List[dict]]) -> Optional[int]:
@@ -94,7 +99,7 @@ def merge_preflight_compression_warning(
if estimate is None:
return
- new_threshold = _threshold_tokens(new_ctx, float(getattr(cc, "threshold_percent", 0.5)))
+ new_threshold = _threshold_tokens(cc, result.new_model, new_ctx, result.target_provider)
if estimate < new_threshold:
return
diff --git a/tests/hermes_cli/test_context_switch_guard.py b/tests/hermes_cli/test_context_switch_guard.py
index 65cfb4d83d2..1982b78abb6 100644
--- a/tests/hermes_cli/test_context_switch_guard.py
+++ b/tests/hermes_cli/test_context_switch_guard.py
@@ -22,7 +22,12 @@ def _result(*, model: str = "small-model") -> ModelSwitchResult:
)
-def _compressor(monkeypatch, *, context_length: int = 200_000):
+def _compressor(
+ monkeypatch,
+ *,
+ context_length: int = 200_000,
+ threshold_tokens_cap: int | None = None,
+):
from agent.context_compressor import ContextCompressor
monkeypatch.setattr(
@@ -36,6 +41,7 @@ def _compressor(monkeypatch, *, context_length: int = 200_000):
protect_last_n=20,
quiet_mode=True,
config_context_length=context_length,
+ threshold_tokens_cap=threshold_tokens_cap,
)
@@ -64,6 +70,34 @@ def test_merge_appends_to_existing_warning(monkeypatch):
assert "preflight compression" in result.warning_message
+def test_default_cap_warns_before_large_window_ratio_threshold(monkeypatch):
+ monkeypatch.setattr(
+ "hermes_cli.context_switch_guard._estimate_tokens",
+ lambda *a, **k: 300_000,
+ )
+ monkeypatch.setattr(
+ "hermes_cli.context_switch_guard.resolve_display_context_length",
+ lambda *a, **k: 1_000_000,
+ )
+ cc = _compressor(
+ monkeypatch,
+ context_length=200_000,
+ threshold_tokens_cap=256_000,
+ )
+ agent = SimpleNamespace(
+ context_compressor=cc,
+ compression_enabled=True,
+ base_url="",
+ api_key="",
+ )
+
+ result = _result(model="large-model")
+ merge_preflight_compression_warning(result, agent=agent)
+
+ assert "preflight compression" in result.warning_message
+ assert "auto-compress at ~256,000" in result.warning_message
+
+
def test_custom_provider_context_avoids_false_shrink_warning(monkeypatch):