The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
perf(agent): cache per-turn display-flag config reads
spfcrazee25cafc83e93
diff --git a/run_agent.py b/run_agent.pyindex b6f655c355a..9181aa3e1e1 100644--- a/run_agent.py+++ b/run_agent.py@@ -3568,12 +3568,21 @@ class AIAgent:``HERMES_FILE_MUTATION_VERIFIER`` env var overrides config. Exposedas a method so tests can patch a single seam without reaching intothe private ``_turn_failed_file_mutations`` state dict.++ The config lookup is read once per agent and cached (mirroring+ ``_credits_notices_enabled``) — the footer gate runs at the end of+ every turn, and a config flip applying on the next session is fine.+ The env-var override stays authoritative on every call and is never+ cached, so tests and operators can still flip it at runtime."""try:import os as _osenv = _os.environ.get("HERMES_FILE_MUTATION_VERIFIER")if env is not None:return env.strip().lower() not in {"0", "false", "no", "off"}+ cached = getattr(self, "_file_mutation_verifier_enabled_cache", None)+ if cached is not None:+ return cached# Read from the persisted config.yaml so gateway and CLI share# the same setting. Import lazily to avoid a startup-time cycle.try:@@ -3583,7 +3592,11 @@ class AIAgent:_cfg = {}_display = _cfg.get("display") if isinstance(_cfg, dict) else Noneif isinstance(_display, dict) and "file_mutation_verifier" in _display:- return bool(_display.get("file_mutation_verifier"))+ enabled = bool(_display.get("file_mutation_verifier"))+ else:+ enabled = True # safe default: verifier on+ self._file_mutation_verifier_enabled_cache = enabled+ return enabledexcept Exception:passreturn True # safe default: verifier on@@ -3665,12 +3678,21 @@ class AIAgent:True). ``HERMES_TURN_COMPLETION_EXPLAINER`` env var overridesconfig. Exposed as a method so tests can patch a single seam,mirroring ``_file_mutation_verifier_enabled``.++ The config lookup is read once per agent and cached (mirroring+ ``_credits_notices_enabled``) — the gate runs at the end of every+ turn, and a config flip applying on the next session is fine.+ The env-var override stays authoritative on every call and is never+ cached, so tests and operators can still flip it at runtime."""try:import os as _osenv = _os.environ.get("HERMES_TURN_COMPLETION_EXPLAINER")if env is not None:return env.strip().lower() not in {"0", "false", "no", "off"}+ cached = getattr(self, "_turn_completion_explainer_enabled_cache", None)+ if cached is not None:+ return cached# Read from the persisted config.yaml so gateway and CLI share# the same setting. Import lazily to avoid a startup-time cycle.try:@@ -3680,7 +3702,11 @@ class AIAgent:_cfg = {}_display = _cfg.get("display") if isinstance(_cfg, dict) else Noneif isinstance(_display, dict) and "turn_completion_explainer" in _display:- return bool(_display.get("turn_completion_explainer"))+ enabled = bool(_display.get("turn_completion_explainer"))+ else:+ enabled = True # safe default: explainer on+ self._turn_completion_explainer_enabled_cache = enabled+ return enabledexcept Exception:passreturn True # safe default: explainer ondiff --git a/tests/run_agent/test_file_mutation_verifier.py b/tests/run_agent/test_file_mutation_verifier.pyindex bfb5ff49c04..526177f8584 100644--- a/tests/run_agent/test_file_mutation_verifier.py+++ b/tests/run_agent/test_file_mutation_verifier.py@@ -313,6 +313,56 @@ class TestVerifierEnabled:agent = _bare_agent()assert agent._file_mutation_verifier_enabled() is False+ def test_config_read_once_then_cached(self, monkeypatch):+ """Measured-work pin: the config lookup happens once per agent.++ The footer gate runs at the end of every turn, so a fresh+ ``load_config()`` per call is wasted work (~1 ms deepcopy per turn,+ see #74211 for the sibling per-turn-config kill). The config read+ must be cached after the first call; the env-var override must still+ win on every call, cached or not.+ """+ monkeypatch.delenv("HERMES_FILE_MUTATION_VERIFIER", raising=False)+ agent = _bare_agent()+ calls = {"n": 0}++ import hermes_cli.config as _cfg_mod++ def counting_load():+ calls["n"] += 1+ return {"display": {"file_mutation_verifier": True}}++ monkeypatch.setattr(_cfg_mod, "load_config", counting_load)++ # First call reads config and caches the result.+ assert agent._file_mutation_verifier_enabled() is True+ assert calls["n"] == 1+ # Subsequent calls must not re-read config.+ assert agent._file_mutation_verifier_enabled() is True+ assert agent._file_mutation_verifier_enabled() is True+ assert calls["n"] == 1+ # Env override stays authoritative even after the cache is warm.+ monkeypatch.setenv("HERMES_FILE_MUTATION_VERIFIER", "0")+ assert agent._file_mutation_verifier_enabled() is False+ assert calls["n"] == 1 # env path never touches config++ def test_cache_respects_config_value(self, monkeypatch):+ """A disabled config value is cached as False, not re-read."""+ monkeypatch.delenv("HERMES_FILE_MUTATION_VERIFIER", raising=False)+ agent = _bare_agent()++ import hermes_cli.config as _cfg_mod+ monkeypatch.setattr(+ _cfg_mod, "load_config", lambda: {"display": {"file_mutation_verifier": False}}+ )+ assert agent._file_mutation_verifier_enabled() is False+ # Warm cache: flip the underlying config; the agent still reports the+ # cached value (same next-session semantics as _credits_notices_enabled).+ monkeypatch.setattr(+ _cfg_mod, "load_config", lambda: {"display": {"file_mutation_verifier": True}}+ )+ assert agent._file_mutation_verifier_enabled() is False+diff --git a/tests/run_agent/test_turn_completion_explainer.py b/tests/run_agent/test_turn_completion_explainer.pyindex 8e0b8a75222..185ea460f20 100644--- a/tests/run_agent/test_turn_completion_explainer.py+++ b/tests/run_agent/test_turn_completion_explainer.py@@ -293,6 +293,40 @@ def test_explainer_disabled_via_env():assert agent._turn_completion_explainer_enabled() is False+def test_explainer_config_read_once_then_cached():+ """Measured-work pin: the config lookup happens once per agent.++ The explainer gate runs at the end of every turn, so a fresh+ ``load_config()`` per call is wasted work (per-turn config reads were+ killed repo-wide in #74211; this seam was missed). The config read must+ be cached after the first call; the env-var override must still win on+ every call, cached or not.+ """+ agent = _make_agent()+ calls = {"n": 0}++ def counting_load():+ calls["n"] += 1+ return {"display": {"turn_completion_explainer": True}}++ with patch.dict(os.environ, {}, clear=False):+ os.environ.pop("HERMES_TURN_COMPLETION_EXPLAINER", None)+ with patch("hermes_cli.config.load_config", counting_load):+ # First call reads config and caches the result.+ assert agent._turn_completion_explainer_enabled() is True+ assert calls["n"] == 1+ # Subsequent calls must not re-read config.+ assert agent._turn_completion_explainer_enabled() is True+ assert agent._turn_completion_explainer_enabled() is True+ assert calls["n"] == 1+ # Env override stays authoritative even after the cache is warm.+ with patch.dict(+ os.environ, {"HERMES_TURN_COMPLETION_EXPLAINER": "0"}, clear=False+ ):+ assert agent._turn_completion_explainer_enabled() is False+ assert calls["n"] == 1 # env path never touches config++# --------------------------------------------------------------------------