The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(compression): bound default trigger at 256K tokens
fangliquanflq489cb746308f
diff --git a/cli-config.yaml.example b/cli-config.yaml.exampleindex 46e1712b89b..7ebcce51346 100644--- a/cli-config.yaml.example+++ b/cli-config.yaml.example@@ -643,14 +643,12 @@ compression:# "claude-sonnet": 0.35# "gpt-5": 0.30- # Optional absolute token cap for the compression trigger (default: null = disabled).- # When set, compression fires at the LOWER of the ratio-based threshold and this- # absolute token count — first-fires-wins. It never fires later than this count- # regardless of which model is active (useful when switching between models with- # very different context windows). Clamped to the model's context length at- # apply-time, so a cap above the window is a no-op (ratio-based threshold wins).- # Survives model switches and fallback activations.- # threshold_tokens: 200000+ # Absolute token cap for the compression trigger (default: 256000).+ # Compression fires at the LOWER of the ratio-based threshold and this count,+ # bounding large-window sessions while lower proportional triggers still win+ # whenever they fall below the cap. The cap survives model switches and fallbacks.+ # Set null to restore ratio-only behavior.+ threshold_tokens: 256000# Existing Codex gpt-5.5 behavior: raise Hermes' compaction trigger to 85%# for the ChatGPT Codex OAuth route. Set false to opt back down to threshold.diff --git a/hermes_cli/config_defaults.py b/hermes_cli/config_defaults.pyindex b50618ce1d4..47893cdde71 100644--- a/hermes_cli/config_defaults.py+++ b/hermes_cli/config_defaults.py@@ -541,8 +541,10 @@ DEFAULT_CONFIG = {# above 0.75 to override the floor."threshold": 0.50,# threshold_tokens: absolute token cap — compression triggers at the lower of the ratio- # threshold and this count. Clamped to the model's context length.- "threshold_tokens": None,+ # threshold and this count. Clamped to the model's context length. 256K bounds 1M-window+ # models (their 50% trigger sat at 500K, so compaction never fired) while every lower+ # ratio trigger still wins; null = ratio-only.+ "threshold_tokens": 256_000,# "progress_notices": False, # opt-in (#52995): when True, routine compression"target_ratio": 0.20, # fraction of threshold to preserve as recent tail# tail_mode: "lean" = clamped 2.5%-of-window tail (10K floor / 25K cap) plus chunkeddiff --git a/tests/agent/test_context_compressor.py b/tests/agent/test_context_compressor.pyindex 91ba9748ab4..1b6b6eb0cc6 100644--- a/tests/agent/test_context_compressor.py+++ b/tests/agent/test_context_compressor.py@@ -2263,6 +2263,24 @@ class TestThresholdTokensCap:assert comp.threshold_tokens == 500_000assert comp.threshold_tokens_cap is None+ def test_default_config_bounds_one_million_context_trigger(self):+ """The shipped default must not let a 1M window defer compaction to 500K."""+ from hermes_cli.config import DEFAULT_CONFIG++ default_cap = DEFAULT_CONFIG["compression"]["threshold_tokens"]+ with patch("agent.context_compressor.get_model_context_length", return_value=1_000_000):+ comp = ContextCompressor(+ "model-a",+ threshold_percent=DEFAULT_CONFIG["compression"]["threshold"],+ threshold_tokens_cap=default_cap,+ quiet_mode=True,+ )+ _ = comp.context_length++ assert comp.threshold_tokens == 256_000+ assert comp.should_compress(255_999) is False+ assert comp.should_compress(256_000) is True+@@ -2304,32 +2322,22 @@ class TestThresholdTokensCap:assert comp.should_compress(200_000) is True # at cap (below 500K pct)assert comp.should_compress(250_000) is True # above cap- def test_default_config_disabled_and_no_behavior_change(self):- """DEFAULT_CONFIG ships threshold_tokens=None (disabled) and both- None and 0 leave the ratio-based trigger byte-identical."""+ def test_default_config_cap_survives_model_switch(self):+ """The shipped cap remains effective when the active model changes."""from hermes_cli.config import DEFAULT_CONFIG- assert DEFAULT_CONFIG["compression"]["threshold_tokens"] is Nonewith patch("agent.context_compressor.get_model_context_length", return_value=1_000_000):- baseline = ContextCompressor(- "model-a", threshold_percent=0.50, quiet_mode=True,- )- comp_none = ContextCompressor(- "model-a", threshold_percent=0.50, quiet_mode=True,- threshold_tokens_cap=None,- )- comp_zero = ContextCompressor(- "model-a", threshold_percent=0.50, quiet_mode=True,- threshold_tokens_cap=0,+ comp = ContextCompressor(+ "model-a",+ threshold_percent=DEFAULT_CONFIG["compression"]["threshold"],+ threshold_tokens_cap=DEFAULT_CONFIG["compression"]["threshold_tokens"],+ quiet_mode=True,)- assert comp_none.threshold_tokens == baseline.threshold_tokens- assert comp_zero.threshold_tokens == baseline.threshold_tokens- # And after a model switch, still identical to baseline.- baseline.update_model("model-b", context_length=200_000)- comp_none.update_model("model-b", context_length=200_000)- comp_zero.update_model("model-b", context_length=200_000)- assert comp_none.threshold_tokens == baseline.threshold_tokens- assert comp_zero.threshold_tokens == baseline.threshold_tokens+ _ = comp.context_length++ assert comp.threshold_tokens == 256_000+ comp.update_model("model-b", context_length=2_000_000)+ assert comp.threshold_tokens == 256_000diff --git a/website/docs/user-guide/configuration.md b/website/docs/user-guide/configuration.mdindex ccd91d82c67..5fa9fd2550e 100644--- a/website/docs/user-guide/configuration.md+++ b/website/docs/user-guide/configuration.md@@ -969,7 +969,7 @@ compression:enabled: true # Toggle compression on/offprogress_notices: false # Opt-in: deliver routine compression progress notices to chat platforms — see belowthreshold: 0.50 # Compress at this % of context limit- threshold_tokens: null # Absolute token cap (optional) — takes lower of ratio vs absolute+ threshold_tokens: 256000 # Absolute token cap — takes lower of ratio vs absolutetarget_ratio: 0.20 # Fraction of threshold to preserve as recent tailtail_mode: lean # Tail retention: "lean" (default — clamped 2.5% tail, 10K-25K, with a detailed session log + anchor index + session_search recovery pointers in the summary, all from ONE auxiliary summarizer call; ~3x fewer retained tokens after compaction) or "legacy" (0.20×threshold verbatim tail)protect_last_n: 20 # Min recent messages to keep uncompressed@@ -1023,7 +1023,7 @@ The value is the **first rung** of an escalating ladder, not a fixed interval: c`in_place` (default `true`) controls what happens to the session identity when compaction fires. When `true`, compaction rewrites the message list and rebuilds the system prompt **without rotating the session id** — the conversation keeps one durable id for its whole life (no `parent_session_id` chain, no `name #2` / `#3` renumbering in session lists). Compaction is non-destructive: the live context is compacted, but the pre-compaction turns are soft-archived under the same id (marked inactive/compacted) — still searchable via `session_search` and recoverable, not deleted. Hooks see the mode via the `in_place` field on the `session:compress` event. Set `in_place: false` to restore the legacy behavior where each compaction rotates to a new session id linked to the old one.-`threshold_tokens` sets an optional **absolute token cap** for the compression trigger. When set, compression fires at the lower of the ratio-based `threshold` and this absolute count — so compression never fires later than the user's preferred token number regardless of which model is active. This solves the problem where switching between models with different context windows (e.g. 1M → 400K) shifts the absolute trigger point. The cap is clamped to the model's context length, so setting it higher than the model supports is safe — the ratio-based threshold is used instead. Default `null` (disabled — ratio-based threshold only). The cap survives model switches and fallback activations.+`threshold_tokens` sets an **absolute token cap** for the compression trigger. Compression fires at the lower of the ratio-based `threshold` and this absolute count, so large-window models cannot silently defer compaction to hundreds of thousands of tokens. The default is `256000`: it bounds a 1M model's default 50% trigger at 256K, while any lower proportional trigger still wins (including the 272K Codex window). The cap survives model switches and fallback activations and is clamped to the model's context length. Set it to `null` to restore ratio-only behavior, or choose a different positive count for your workload.`idle_compact_after_seconds` is an **opt-in, time-based** trigger that complements the size-based `threshold`. Default `0` (disabled). When set above 0, a session that resumes after at least that many seconds of inactivity compacts its accumulated history up front, before the first reply — so a long-lived thread (e.g. a Telegram conversation you come back to hours later) doesn't re-read its full stale context on every subsequent turn. It never fires when the context is already at or below the post-compression target (`threshold × target_ratio`), and it honors the same failure-cooldown, anti-thrash, and per-session lock guards as every automatic compaction. Example: `idle_compact_after_seconds: 1800` compacts after 30 minutes idle.