Skip to content

The agent that grows with you

fix(gateway): `threshold_tokens: null` rebuilds a cached agent instead of waiting for a restart

kshitijk4poor8eeec5423b1e
diff --git a/gateway/run_agent_cache.py b/gateway/run_agent_cache.py
index b743f89045e..1a73ab5af09 100644
--- a/gateway/run_agent_cache.py
+++ b/gateway/run_agent_cache.py
@@ -47,9 +47,14 @@ class GatewayAgentCacheMixin:
@classmethod
def _extract_cache_busting_config(cls, user_config: dict | None) -> dict:
- """Values that must bust the cached agent, as a flat dict keyed by 'section.key'. Missing keys /
- non-dict sections yield None (still enters the signature). Includes the live tool registry
- generation: MCP reloads mutate the registry without touching config.yaml."""
+ """Values that must bust the cached agent, as a flat dict keyed by 'section.key'. ``user_config``
+ is the raw file (no DEFAULT_CONFIG merge), so an absent key is filled from DEFAULT_CONFIG — the
+ value the agent was actually built with. Mapping 'absent' to None made an explicit ``null``
+ (the documented opt-out of a non-None default such as ``compression.threshold_tokens``)
+ signature-identical to 'unset', so the opt-out never rebuilt a live session. Non-dict sections
+ count as absent. Includes the live tool registry generation: MCP reloads mutate the registry
+ without touching config.yaml."""
+ from hermes_cli.config import DEFAULT_CONFIG
out: Dict[str, Any] = {}
cfg = user_config if isinstance(user_config, dict) else {}
for section, key in cls._CACHE_BUSTING_CONFIG_KEYS:
@@ -57,8 +62,10 @@ class GatewayAgentCacheMixin:
if section == "checkpoints" and isinstance(section_val, bool):
# Legacy ``checkpoints: true``: a live toggle must still rebuild the cached agent.
out[f"{section}.{key}"] = section_val if key == "enabled" else None
+ elif isinstance(section_val, dict) and key in section_val:
+ out[f"{section}.{key}"] = section_val[key]
else:
- out[f"{section}.{key}"] = section_val.get(key) if isinstance(section_val, dict) else None
+ out[f"{section}.{key}"] = cfg_get(DEFAULT_CONFIG, section, key)
try:
from tools.registry import registry
out["tools.registry_generation"] = getattr(registry, "_generation", None)
diff --git a/tests/gateway/test_agent_cache.py b/tests/gateway/test_agent_cache.py
index 21ec4c6c246..c9aae23321b 100644
--- a/tests/gateway/test_agent_cache.py
+++ b/tests/gateway/test_agent_cache.py
@@ -186,33 +186,55 @@ class TestExtractCacheBustingConfig:
assert out["compression.codex_app_server_auto"] == "hermes"
- def test_missing_keys_yield_none(self):
- """Absent config keys must produce None values (still contribute to signature)."""
+ def test_missing_keys_yield_the_shipped_default(self):
+ """An absent key carries the value in force — DEFAULT_CONFIG's — so the signature reflects
+ what the agent was built with, not the raw file's shape. Every documented key is present."""
from gateway.run import GatewayRunner
+ from hermes_cli.config import DEFAULT_CONFIG, cfg_get
out = GatewayRunner._extract_cache_busting_config({})
- # Every documented cache-busting key must be present, even if None
for section, key in GatewayRunner._CACHE_BUSTING_CONFIG_KEYS:
assert f"{section}.{key}" in out
- assert out[f"{section}.{key}"] is None
+ assert out[f"{section}.{key}"] == cfg_get(DEFAULT_CONFIG, section, key)
+
+ def test_explicit_null_differs_from_absent_when_default_is_set(self):
+ """`threshold_tokens: null` is the documented opt-out of the shipped 256K cap. A raw-file read
+ that mapped 'absent' to None made the opt-out signature-identical to 'unset', so the cached
+ agent kept the cap until a restart. Absent must equal the explicit default, not explicit null."""
+ from gateway.run import GatewayRunner
+ from hermes_cli.config import DEFAULT_CONFIG
+
+ default_cap = DEFAULT_CONFIG["compression"]["threshold_tokens"]
+ assert default_cap is not None # the premise: a non-None default whose opt-out is null
+ absent = GatewayRunner._extract_cache_busting_config({})["compression.threshold_tokens"]
+ explicit_default = GatewayRunner._extract_cache_busting_config(
+ {"compression": {"threshold_tokens": default_cap}})["compression.threshold_tokens"]
+ opted_out = GatewayRunner._extract_cache_busting_config(
+ {"compression": {"threshold_tokens": None}})["compression.threshold_tokens"]
+ assert absent == explicit_default == default_cap
+ assert opted_out is None and opted_out != absent
def test_non_dict_section_treated_as_missing(self):
from gateway.run import GatewayRunner
- # compression is a string — should not crash, all compression.* keys None
+ from hermes_cli.config import DEFAULT_CONFIG
+
+ # compression is a string — should not crash; compression.* keys fall back to the shipped defaults
out = GatewayRunner._extract_cache_busting_config(
{"compression": "broken", "model": {"context_length": 100_000}}
)
- assert out["compression.enabled"] is None
- assert out["compression.threshold"] is None
+ assert out["compression.enabled"] == DEFAULT_CONFIG["compression"]["enabled"]
+ assert out["compression.threshold"] == DEFAULT_CONFIG["compression"]["threshold"]
assert out["model.context_length"] == 100_000
def test_none_config_is_safe(self):
from gateway.run import GatewayRunner
+ from hermes_cli.config import DEFAULT_CONFIG, cfg_get
+
out = GatewayRunner._extract_cache_busting_config(None)
for section, key in GatewayRunner._CACHE_BUSTING_CONFIG_KEYS:
- assert out[f"{section}.{key}"] is None
+ assert out[f"{section}.{key}"] == cfg_get(DEFAULT_CONFIG, section, key)
assert "tools.registry_generation" in out
def test_extract_includes_live_tool_registry_generation(self, monkeypatch):