Skip to content

The agent that grows with you

fix(tui-gateway): restore the default threshold cap when the live config key is absent

liuhao1024b06052ecd926
diff --git a/tests/tui_gateway/test_compression_config_hot_reload.py b/tests/tui_gateway/test_compression_config_hot_reload.py
index 19f00b2b0d8..2e97335b614 100644
--- a/tests/tui_gateway/test_compression_config_hot_reload.py
+++ b/tests/tui_gateway/test_compression_config_hot_reload.py
@@ -114,7 +114,7 @@ def test_unchanged_compression_config_is_noop(monkeypatch):
assert compressor.threshold_tokens == 99_999
-def test_clearing_threshold_tokens_restores_ratio_trigger(monkeypatch):
+def test_clearing_threshold_tokens_restores_default_cap(monkeypatch):
session, compressor = _session_with_compressor(threshold_tokens_cap=100_000)
assert compressor.threshold_tokens == 100_000
@@ -128,8 +128,42 @@ def test_clearing_threshold_tokens_restores_ratio_trigger(monkeypatch):
)
server._sync_agent_compression_with_config("sid-95151", session)
+ # Key removal restores what a fresh agent build installs (merged DEFAULT_CONFIG), not "no
+ # cap": a None here re-derives the uncapped ratio trigger and the 256K default is lost.
+ assert compressor.threshold_tokens_cap == 256_000
assert compressor.threshold_tokens > 100_000
- assert compressor.threshold_tokens_cap is None
+
+
+def test_absent_threshold_tokens_keeps_default_cap_on_1m_window(monkeypatch):
+ """#117093: the live read is unmerged (missing key = unset), so a config.yaml without
+ compression.threshold_tokens used to wipe the ctor-installed 256K cap at the first
+ turn's sync — the trigger re-derived to the uncapped ratio value (500K on a 1M window)
+ and compaction stopped firing at 256K while telemetry still reported the capped figure."""
+ compressor = ContextCompressor(
+ model="unset-test-model",
+ threshold_percent=0.85,
+ config_context_length=1_000_000,
+ threshold_tokens_cap=256_000,
+ quiet_mode=True,
+ )
+ agent = SimpleNamespace(
+ model="unset-test-model",
+ provider="",
+ context_compressor=compressor,
+ compression_enabled=True,
+ compression_idle_compact_after_seconds=0,
+ codex_responses_native_compaction=False,
+ codex_responses_compact_threshold=200_000,
+ )
+ session = {"agent": agent, "session_key": "session-unset"}
+ assert compressor.threshold_tokens == 256_000 # min(1M * 0.85, 256K cap)
+
+ _sync_with_cfg(monkeypatch, session, {"model": {"context_length": 1_000_000}, "compression": {}})
+
+ assert compressor.threshold_tokens_cap == 256_000
+ # threshold is absent too, so the derived 0.50 ratio applies — the 256K cap must still win.
+ assert compressor.threshold_tokens == 256_000
+
def test_prompt_submit_calls_compression_sync_after_model_sync():
@@ -237,7 +271,8 @@ def test_removing_threshold_restores_derived_default(monkeypatch):
)
assert compressor._config_threshold_percent == 0.50
assert compressor.threshold_percent == 0.50
- assert compressor.threshold_tokens == int(600_000 * 0.50)
+ # The absent cap key restores the 256K default, which binds below the 300K ratio value.
+ assert compressor.threshold_tokens == min(int(600_000 * 0.50), 256_000)
def test_removing_context_length_reinfers_from_model_metadata(monkeypatch):
@@ -254,7 +289,7 @@ def test_removing_context_length_reinfers_from_model_metadata(monkeypatch):
_sync_with_cfg(monkeypatch, session, {"model": {}, "compression": {}})
assert compressor._config_context_length is None
assert compressor.context_length == 1_000_000
- assert compressor.threshold_tokens == int(1_000_000 * 0.50)
+ assert compressor.threshold_tokens == min(int(1_000_000 * 0.50), 256_000)
def test_removing_idle_compact_after_seconds_restores_zero(monkeypatch):
diff --git a/tui_gateway/session_compression.py b/tui_gateway/session_compression.py
index f4db291eeda..514c0ea688f 100644
--- a/tui_gateway/session_compression.py
+++ b/tui_gateway/session_compression.py
@@ -39,6 +39,21 @@ def _compressor_ctor_default(name: str, fallback: Any) -> Any:
return fallback
+def _default_threshold_tokens_cap():
+ """The cap a fresh agent build installs when the key is absent: DEFAULT_CONFIG's
+ ``compression.threshold_tokens``. agent_init reads the MERGED config, so "no key in
+ config.yaml" still installs the 256K default at construction; key removal here must
+ restore that same value. ``None`` instead would re-derive the uncapped ratio trigger
+ (500K on a 1M-window model) and the default cap would be gone after the first turn
+ (#117093). An explicit ``threshold_tokens: null`` stays ratio-only — the key is present,
+ so ``.get`` returns it untouched."""
+ try:
+ from hermes_cli.config_defaults import DEFAULT_CONFIG
+ return (DEFAULT_CONFIG.get("compression") or {}).get("threshold_tokens")
+ except Exception:
+ return None
+
+
def _derived_default_threshold_percent(agent: Any, compression: dict) -> float:
"""Default compaction threshold when ``compression.threshold`` is unset. Mirrors agent_init: ctor
global default, then per-model resolution (Codex autoraise etc.) via the SAME
@@ -153,7 +168,9 @@ def _apply_live_compression_config(agent: Any, cfg: dict | None) -> None:
# next access (construction's deferred resolution); re-applies the small-context floor too.
set_config_context_length(agent, None)
cc._resolved_context_length = None
- cc.threshold_tokens_cap = cc._coerce_threshold_tokens_cap(compression.get("threshold_tokens"))
+ cc.threshold_tokens_cap = cc._coerce_threshold_tokens_cap(
+ compression.get("threshold_tokens", _default_threshold_tokens_cap())
+ )
# Invalidate the cached trigger so the next preflight re-derives from percent/window, then the cap.
cc._threshold_tokens = cc._tail_token_budget = None