The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: widen the remaining config.yaml stat caches to file_signature
teknium1182ec5c28d5f
diff --git a/hermes_cli/cli_info_mixin.py b/hermes_cli/cli_info_mixin.pyindex 936bbcb6b9d..76aa226a702 100644--- a/hermes_cli/cli_info_mixin.py+++ b/hermes_cli/cli_info_mixin.py@@ -16,7 +16,7 @@ import timefrom hermes_constants import is_termux as _is_termux_environmentfrom rich.markup import escape as _escape-from utils import base_url_hostname+from utils import base_url_hostname, file_signaturefrom hermes_cli.cli_modal_mixin import _gated_confirmfrom hermes_cli.colors import Colors as _Colors@@ -818,13 +818,13 @@ class CLIInfoMixin:if not cfg_path.exists():returntry:- mtime = cfg_path.stat().st_mtime+ sig = file_signature(cfg_path.stat())except OSError:return- if mtime == self._config_mtime:+ if sig == self._config_sig:return # unchanged — fast path- self._config_mtime = mtime+ self._config_sig = sigtry:with open(cfg_path, encoding="utf-8") as f:new_cfg = _yaml.safe_load(f) or {}diff --git a/hermes_cli/cli_tui_mixin.py b/hermes_cli/cli_tui_mixin.pyindex 77d9e995348..9562a9eafb7 100644--- a/hermes_cli/cli_tui_mixin.py+++ b/hermes_cli/cli_tui_mixin.py@@ -1819,7 +1819,7 @@ class CLITuiMixin:# Config file watcher — detect mcp_servers changes and auto-reload.from hermes_cli.config import get_config_path as _get_config_path_cfg_path = _get_config_path()- self._config_mtime: float = _cfg_path.stat().st_mtime if _cfg_path.exists() else 0.0+ self._config_sig: tuple | None = file_signature(_cfg_path.stat()) if _cfg_path.exists() else Noneself._config_mcp_servers: dict = self.config.get("mcp_servers") or {}self._last_config_check: float = 0.0 # monotonic time of last checkdiff --git a/hermes_cli/config.py b/hermes_cli/config.pyindex b72bf8d8e1a..e46f602e4dc 100644--- a/hermes_cli/config.py+++ b/hermes_cli/config.py@@ -204,7 +204,7 @@ _LAST_EXPANDED_CONFIG_BY_PATH: Dict[str, Any] = {}# atomic_yaml_write which produces a fresh inode, so stat() sees a new signature and the next load# repopulates automatically — no explicit invalidation hook. See #58514._LOAD_CONFIG_CACHE: Dict[str, Tuple[int, ...]] = {}-# path -> (mtime_ns, size, raw yaml dict) for read_raw_config() (no defaults merged in).+# path -> (mtime_ns, size, ino, ctime_ns, raw yaml dict) for read_raw_config() (no defaults merged in)._RAW_CONFIG_CACHE: Dict[str, Tuple[int, ...]] = {}# Env var names written to .env that aren't in OPTIONAL_ENV_VARS (managed by setup/provider@@ -2392,7 +2392,7 @@ def save_config(# load_env() memo keyed on (path, *file_signature). Editing .env bumps mtime/inode -> rebuild;# invalidate_env_cache() is the explicit knob for writers on coarse-mtime filesystems.-_env_cache: Optional[Tuple[Tuple[str, Optional[float], Optional[int]], Dict[str, str]]] = None+_env_cache: Optional[Tuple[Tuple[str, Optional[Tuple[int, int, int, int]]], Dict[str, str]]] = Nonedef load_env() -> Dict[str, str]:diff --git a/model_tools.py b/model_tools.pyindex 7f26e94fc3e..f0e56c19e2f 100644--- a/model_tools.py+++ b/model_tools.py@@ -22,6 +22,7 @@ from tools.registry import CHECK_FN_CACHE_BYPASS, check_fn_cache_scope, discoverfrom tools.registry import _MAX_TOOL_ERROR_CHARS as _TOOL_ERROR_MAX_LENfrom toolsets import resolve_toolset, validate_toolsetfrom tools.arg_coercion import coerce_tool_args+from utils import file_signaturelogger = logging.getLogger(__name__)@@ -257,7 +258,7 @@ def _tool_defs_cache_key("""Memo key for get_tool_definitions, or None when caching must be bypassed.Covers every argument plus everything that changes the result without one:- registry generation, config.yaml mtime/size (dynamic schemas), kanban+ registry generation, config.yaml stat signature (dynamic schemas), kanbancontext, profile scope. check_fn results are TTL-cached in the registry."""profile_scope = check_fn_cache_scope()@@ -266,7 +267,7 @@ def _tool_defs_cache_key(try:from hermes_cli.config import get_config_pathcfg_stat = get_config_path().stat()- cfg_fp = (cfg_stat.st_mtime_ns, cfg_stat.st_size)+ cfg_fp = file_signature(cfg_stat)except (FileNotFoundError, OSError, ImportError):cfg_fp = Nonereturn (diff --git a/tests/conftest.py b/tests/conftest.pyindex 0fd5ef8e693..7cbabca510e 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -920,7 +920,7 @@ def _reset_tui_gateway_server_state():if mod is not None:snapshot = {"methods": dict(mod._methods),- "cfg": (mod._cfg_cache, mod._cfg_mtime, mod._cfg_path),+ "cfg": (mod._cfg_cache, mod._cfg_sig, mod._cfg_path),"db": (mod._db, mod._db_error),"real_stdout": mod._real_stdout,}@@ -951,7 +951,7 @@ def _reset_tui_gateway_server_state():if snapshot is not None:mod._methods.clear()mod._methods.update(snapshot["methods"])- mod._cfg_cache, mod._cfg_mtime, mod._cfg_path = snapshot["cfg"]+ mod._cfg_cache, mod._cfg_sig, mod._cfg_path = snapshot["cfg"]mod._db, mod._db_error = snapshot["db"]mod._real_stdout = snapshot["real_stdout"]else:@@ -959,7 +959,7 @@ def _reset_tui_gateway_server_state():# for the globals we could not snapshot (``_methods`` is left to# the importing file's fixture, see block comment above).mod._cfg_cache = None- mod._cfg_mtime = None+ mod._cfg_sig = Nonemod._cfg_path = Nonemod._db = Nonemod._db_error = Nonediff --git a/tests/hermes_cli/test_cli_mcp_config_watch.py b/tests/hermes_cli/test_cli_mcp_config_watch.pyindex acfc7a3473b..0f033518b49 100644--- a/tests/hermes_cli/test_cli_mcp_config_watch.py+++ b/tests/hermes_cli/test_cli_mcp_config_watch.py@@ -3,6 +3,8 @@ import timefrom pathlib import Pathfrom unittest.mock import MagicMock, patch+from utils import file_signature+def _make_cli(tmp_path, mcp_servers=None, extra_config=None):"""Create a minimal HermesCLI instance with mocked config."""@@ -18,7 +20,7 @@ def _make_cli(tmp_path, mcp_servers=None, extra_config=None):cfg_file = tmp_path / "config.yaml"cfg_file.write_text("mcp_servers: {}\n")- obj._config_mtime = cfg_file.stat().st_mtime+ obj._config_sig = file_signature(cfg_file.stat())obj._reload_mcp = MagicMock()obj._busy_command = MagicMock()@@ -40,7 +42,7 @@ class TestMCPConfigWatch:# Simulate user adding a new MCP server to config.yamlcfg_file.write_text(yaml.dump({"mcp_servers": {"github": {"url": "https://mcp.github.com"}}}))- obj._config_mtime = 0.0 # force stale mtime+ obj._config_sig = None # force stale mtimewith patch("hermes_cli.config.get_config_path", return_value=cfg_file):obj._check_config_mcp_changes()@@ -54,7 +56,7 @@ class TestMCPConfigWatch:# Simulate user removing the servercfg_file.write_text(yaml.dump({"mcp_servers": {}}))- obj._config_mtime = 0.0+ obj._config_sig = Nonewith patch("hermes_cli.config.get_config_path", return_value=cfg_file):obj._check_config_mcp_changes()@@ -87,7 +89,7 @@ class TestMCPConfigWatch:"mcp": {"auto_reload_on_config_change": False},"mcp_servers": {"github": {"url": "https://mcp.github.com"}},}))- obj._config_mtime = 0.0 # force stale mtime+ obj._config_sig = None # force stale mtimewith patch("hermes_cli.config.get_config_path", return_value=cfg_file):obj._check_config_mcp_changes()@@ -110,13 +112,13 @@ class TestMCPConfigWatch:"mcp": {"auto_reload_on_config_change": False},"mcp_servers": {"github": {"url": "https://mcp.github.com"}},}))- obj._config_mtime = 0.0+ obj._config_sig = Nonewith patch("hermes_cli.config.get_config_path", return_value=cfg_file):obj._check_config_mcp_changes()# Second pass: same file content, new mtime — no reload, no change.obj._last_config_check = 0.0- obj._config_mtime = 0.0+ obj._config_sig = Noneobj._check_config_mcp_changes()obj._reload_mcp.assert_not_called()@@ -139,7 +141,7 @@ class TestMCPConfigWatch:"auxiliary": {"mcp": {"auto_reload_on_config_change": False}},"mcp_servers": {"github": {"url": "https://mcp.github.com"}},}))- obj._config_mtime = 0.0+ obj._config_sig = Nonewith patch("hermes_cli.config.get_config_path", return_value=cfg_file):obj._check_config_mcp_changes()@@ -184,10 +186,30 @@ class TestMCPConfigWatch:"agent": {"reasoning_effort": "high"},"mcp_servers": raw_servers,}))- obj._config_mtime = 0.0+ obj._config_sig = Nonewith patch("hermes_cli.config.get_config_path", return_value=cfg_file):obj._check_config_mcp_changes()obj._reload_mcp.assert_not_called()assert "MCP server config changed" not in capsys.readouterr().out+++def test_pinned_mtime_same_size_replacement_triggers_reload(tmp_path):+ """#111105: cp -p / rsync -t style replacement (same mtime, same size) must still reload."""+ import os+ import shutil++ obj, cfg_file = _make_cli(tmp_path, mcp_servers={"bb": {"command": "b"}})+ cfg_file.write_text("mcp_servers:\n bb: {command: b}\n")+ obj._config_sig = file_signature(cfg_file.stat())+ other = tmp_path / "other.yaml"+ other.write_text("mcp_servers:\n aa: {command: a}\n")+ shutil.copy2(other, cfg_file)+ os.utime(cfg_file, ns=(obj._config_sig[0], obj._config_sig[0]))++ with patch("hermes_cli.config.get_config_path", return_value=cfg_file):+ obj._check_config_mcp_changes()++ obj._reload_mcp.assert_called_once()+ assert obj._config_mcp_servers == {"aa": {"command": "a"}}diff --git a/tests/tools/test_mcp_stability.py b/tests/tools/test_mcp_stability.pyindex 974ad09cb58..9ce5cf979c2 100644--- a/tests/tools/test_mcp_stability.py+++ b/tests/tools/test_mcp_stability.py@@ -443,7 +443,7 @@ class TestMCPReloadTimeout:# Create a mock HermesCLI-like object with the needed attributesclass FakeCLI:- _config_mtime = 0.0+ _config_sig = None_config_mcp_servers = {}_last_config_check = 0.0_command_running = Falsediff --git a/tests/tools/test_model_tools.py b/tests/tools/test_model_tools.pyindex 0d933576793..2e02e83874c 100644--- a/tests/tools/test_model_tools.py+++ b/tests/tools/test_model_tools.py@@ -570,7 +570,9 @@ class TestBridgeDispatch:assert disp.call_args.args[0] == "mcp_x" and disp.call_args.args[1] == {"a": 1}-# =========================================================================+# ==================================================================++# Browser schema retrieval hints# =========================================================================@@ -600,3 +602,22 @@ class TestBrowserRetrievalHints:rendered = " ".join(d["function"]["description"] for d in _apply_dynamic_schemas(defs + self._defs("terminal")))assert "web_search" not in renderedassert "web_extract" not in rendered+++def test_tool_defs_cache_key_sees_config_replacement_with_pinned_mtime(tmp_path):+ """#111105: a same-size config.yaml swapped in with the old mtime must change the memo key."""+ import os+ import shutil++ from model_tools import _tool_defs_cache_key++ cfg = tmp_path / "config.yaml"+ cfg.write_text("mcp_servers:\n aa: {command: a}\n", encoding="utf-8")+ with patch("hermes_cli.config.get_config_path", return_value=cfg):+ before = _tool_defs_cache_key(None, None, False)+ st = cfg.stat()+ other = tmp_path / "other.yaml"+ other.write_text("mcp_servers:\n bb: {command: b}\n", encoding="utf-8")+ shutil.copy2(other, cfg)+ os.utime(cfg, ns=(st.st_atime_ns, st.st_mtime_ns))+ assert _tool_defs_cache_key(None, None, False) != beforediff --git a/tests/tui_gateway/test_config_profile_scope.py b/tests/tui_gateway/test_config_profile_scope.pyindex d7d1a8e02e4..f10e2d61cc0 100644--- a/tests/tui_gateway/test_config_profile_scope.py+++ b/tests/tui_gateway/test_config_profile_scope.py@@ -45,7 +45,7 @@ def _homes(tmp_path: Path) -> tuple[Path, Path]:def _reset_cfg_cache() -> None:server._cfg_cache = None- server._cfg_mtime = None+ server._cfg_sig = Noneserver._cfg_path = Nonediff --git a/tests/tui_gateway/test_config_set_display_toggles.py b/tests/tui_gateway/test_config_set_display_toggles.pyindex 2139314f266..ef1578e55d2 100644--- a/tests/tui_gateway/test_config_set_display_toggles.py+++ b/tests/tui_gateway/test_config_set_display_toggles.py@@ -16,9 +16,9 @@ from tui_gateway import serverdef config_home(tmp_path, monkeypatch):"""Point the server's config read/write at a temp file."""monkeypatch.setattr(server, "_hermes_home", tmp_path)- server._cfg_cache = server._cfg_mtime = server._cfg_path = None+ server._cfg_cache = server._cfg_sig = server._cfg_path = Noneyield tmp_path / "config.yaml"- server._cfg_cache = server._cfg_mtime = server._cfg_path = None+ server._cfg_cache = server._cfg_sig = server._cfg_path = Nonedef _set(key, value):diff --git a/tests/tui_gateway/test_config_set_voice_chat_mode.py b/tests/tui_gateway/test_config_set_voice_chat_mode.pyindex 5dee8097db3..35b08c2844f 100644--- a/tests/tui_gateway/test_config_set_voice_chat_mode.py+++ b/tests/tui_gateway/test_config_set_voice_chat_mode.py@@ -14,9 +14,9 @@ from tui_gateway import server@pytest.fixturedef config_home(tmp_path, monkeypatch):monkeypatch.setattr(server, "_hermes_home", tmp_path)- server._cfg_cache = server._cfg_mtime = server._cfg_path = None+ server._cfg_cache = server._cfg_sig = server._cfg_path = Noneyield tmp_path / "config.yaml"- server._cfg_cache = server._cfg_mtime = server._cfg_path = None+ server._cfg_cache = server._cfg_sig = server._cfg_path = Nonedef _set(value):diff --git a/tests/tui_gateway/test_goal_command.py b/tests/tui_gateway/test_goal_command.pyindex 674ca15f36a..c767d61ed0f 100644--- a/tests/tui_gateway/test_goal_command.py+++ b/tests/tui_gateway/test_goal_command.py@@ -60,7 +60,7 @@ def server(hermes_home, monkeypatch):# originals on teardown so nothing leaks to later tests either.monkeypatch.setattr(mod, "_hermes_home", hermes_home)monkeypatch.setattr(mod, "_cfg_cache", None)- monkeypatch.setattr(mod, "_cfg_mtime", None)+ monkeypatch.setattr(mod, "_cfg_sig", None)monkeypatch.setattr(mod, "_cfg_path", None)yield mod# Reset module-level session state without re-importing. importlib.reloaddiff --git a/tests/tui_gateway/test_protocol.py b/tests/tui_gateway/test_protocol.pyindex 9ba50ac3fc8..601872d5dd3 100644--- a/tests/tui_gateway/test_protocol.py+++ b/tests/tui_gateway/test_protocol.py@@ -1357,7 +1357,7 @@ def test_skin_live_switch_end_to_end(server, tmp_path, monkeypatch):monkeypatch.setattr(skin_engine, "get_hermes_home", lambda: tmp_path)monkeypatch.setattr(server, "_hermes_home", tmp_path)monkeypatch.setattr(server, "_last_skin_sig", None, raising=False)- server._cfg_cache = server._cfg_mtime = server._cfg_path = None+ server._cfg_cache = server._cfg_sig = server._cfg_path = Noneemitted = []monkeypatch.setattr(server, "_emit", lambda ev, sid, payload=None: emitted.append((ev, payload)))diff --git a/tests/tui_gateway/test_session_control.py b/tests/tui_gateway/test_session_control.pyindex 64e1f2f603f..774243fcebc 100644--- a/tests/tui_gateway/test_session_control.py+++ b/tests/tui_gateway/test_session_control.py@@ -39,7 +39,7 @@ def server(hermes_home, monkeypatch):mod = importlib.import_module("tui_gateway.server")monkeypatch.setattr(mod, "_hermes_home", hermes_home)monkeypatch.setattr(mod, "_cfg_cache", None)- monkeypatch.setattr(mod, "_cfg_mtime", None)+ monkeypatch.setattr(mod, "_cfg_sig", None)monkeypatch.setattr(mod, "_cfg_path", None)yield modmod._sessions.clear()diff --git a/tests/tui_gateway/test_tui_gateway_server.py b/tests/tui_gateway/test_tui_gateway_server.pyindex 0a80e39b44a..3361abb7b73 100644--- a/tests/tui_gateway/test_tui_gateway_server.py+++ b/tests/tui_gateway/test_tui_gateway_server.py@@ -105,7 +105,7 @@ def test_session_slot_is_claimed_on_first_turn_not_on_create(monkeypatch, tmp_patry:server._cfg_cache = None- server._cfg_mtime = None+ server._cfg_sig = Noneserver._cfg_path = None_clear_server_sessions()monkeypatch.setattr(server, "_start_agent_build", lambda *args, **kwargs: None)@@ -139,7 +139,7 @@ def test_session_slot_is_claimed_on_first_turn_not_on_create(monkeypatch, tmp_pafinally:_clear_server_sessions()server._cfg_cache = None- server._cfg_mtime = None+ server._cfg_sig = Noneserver._cfg_path = Nonereset_hermes_home_override(token)@@ -22496,3 +22496,23 @@ def test_workspace_move_rehomes_running_session(monkeypatch, tmp_path):assert captured["row_update"] == (target, str(new_cwd))assert live["cwd"] == str(new_cwd)assert live.get("explicit_cwd") is True+++def test_load_cfg_raw_sees_replacement_with_pinned_mtime_and_size(monkeypatch, tmp_path):+ """#111105: the raw-config cache must not serve (and later write back) a stale document after a+ same-size replacement that keeps the old mtime."""+ import shutil++ cfg = tmp_path / "config.yaml"+ cfg.write_text("model:\n default: bbbb-route\n", encoding="utf-8")+ monkeypatch.setattr(server, "_active_config_path", lambda: cfg)+ monkeypatch.setattr(server, "_cfg_cache", None)+ monkeypatch.setattr(server, "_cfg_sig", None)+ monkeypatch.setattr(server, "_cfg_path", None)+ assert server._load_cfg_raw()["model"]["default"] == "bbbb-route"+ st = cfg.stat()+ other = tmp_path / "other.yaml"+ other.write_text("model:\n default: aaaa-route\n", encoding="utf-8")+ shutil.copy2(other, cfg)+ os.utime(cfg, ns=(st.st_atime_ns, st.st_mtime_ns))+ assert server._load_cfg_raw()["model"]["default"] == "aaaa-route"diff --git a/tui_gateway/server.py b/tui_gateway/server.pyindex 0787278f1b5..ebe1c1e7210 100644--- a/tui_gateway/server.py+++ b/tui_gateway/server.py@@ -26,7 +26,7 @@ from hermes_constants import (get_hermes_home, get_hermes_home_override, profile_name_for_home,reset_hermes_home_override, set_hermes_home_override)from hermes_cli.env_loader import load_hermes_dotenv-from utils import is_truthy_value+from utils import file_signature, is_truthy_valuefrom hermes_state_ids import new_session_idfrom tools.environments.local import hermes_subprocess_envfrom agent.replay_cleanup import canonicalize_replay_history@@ -96,7 +96,7 @@ _cfg_lock = threading.Lock()_profile_ui_meta_lock = threading.Lock()_sessions_lock = threading.RLock() # reentrant: _close_session_by_id may run under callers that already hold it_cfg_cache: dict | None = None-_cfg_mtime: float | None = None+_cfg_sig: tuple | None = None_cfg_path = None_session_resume_lock = threading.Lock()_SLASH_WORKER_TIMEOUT_S = max(5.0, env_float("HERMES_TUI_SLASH_TIMEOUT_S", 45.0))@@ -1227,17 +1227,17 @@ def _load_cfg_raw() -> dict:read→mutate→``_save_cfg`` round-trips and raw inspection (defaults / managed overlay / ``${VAR}``expansion applied here would be persisted on the next save). Behavioral reads use :func:`_load_cfg`.Cache keyed on the resolved path so profiles don't clobber."""- global _cfg_cache, _cfg_mtime, _cfg_path+ global _cfg_cache, _cfg_sig, _cfg_pathwith contextlib.suppress(Exception):p = _active_config_path()- mtime = p.stat().st_mtime if p.exists() else None+ sig = file_signature(p.stat()) if p.exists() else Nonewith _cfg_lock:- if _cfg_cache is not None and _cfg_mtime == mtime and _cfg_path == p:+ if _cfg_cache is not None and _cfg_sig == sig and _cfg_path == p:return copy.deepcopy(_cfg_cache)from hermes_cli.config import read_user_config_rawdata = read_user_config_raw(p) if p.exists() else {}with _cfg_lock: # cache the RAW config: _save_cfg writes _cfg_cache back to disk- _cfg_cache, _cfg_mtime, _cfg_path = copy.deepcopy(data), mtime, p+ _cfg_cache, _cfg_sig, _cfg_path = copy.deepcopy(data), sig, preturn datareturn {}@@ -1254,7 +1254,7 @@ def _load_cfg() -> dict:def _save_cfg(cfg: dict):- global _cfg_cache, _cfg_mtime, _cfg_path+ global _cfg_cache, _cfg_sig, _cfg_pathfrom utils import atomic_roundtrip_yaml_savepath = _active_config_path()# Comment-, ordering- and Unicode-preserving write (a plain safe_dump clobbered hand-written configs);@@ -1263,9 +1263,9 @@ def _save_cfg(cfg: dict):with _cfg_lock:_cfg_cache, _cfg_path = copy.deepcopy(cfg), pathtry:- _cfg_mtime = path.stat().st_mtime+ _cfg_sig = file_signature(path.stat())except Exception:- _cfg_mtime = None+ _cfg_sig = Nonedef _session_for_key(session_key: str) -> dict | None: