The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(file-safety): resolve HERMES_HOME/config per call so multiplex profiles don't poison the write guards (#107327)
PRATHAMESH751271622e4b30
diff --git a/tests/tools/test_file_write_safety.py b/tests/tools/test_file_write_safety.pyindex ad5c7f2e12d..238ca1cd4cb 100644--- a/tests/tools/test_file_write_safety.py+++ b/tests/tools/test_file_write_safety.py@@ -690,5 +690,87 @@ class TestProtectedInstructionFiles:assert rendered["choices"] == ["once", "deny"]+class TestMultiplexProfileWriteGuardsAreProfileScoped:+ """#107327: a multiplexed gateway scopes ``HERMES_HOME`` per turn via a+ contextvar. The home/config path getters must resolve per call, or whichever+ profile ran first in the process freezes both the protected-instruction gate+ and the ``config.yaml`` hard-block for every later profile — up to letting a+ later profile rewrite its own ``config.yaml`` the block exists to protect."""++ def _profiles(self, tmp_path: Path):+ root = tmp_path / "home"+ a, b = root / "profiles" / "alpha", root / "profiles" / "beta"+ for home in (a, b):+ (home / "workspace").mkdir(parents=True)+ (home / "config.yaml").write_text(+ "model:\n default: original\n", encoding="utf-8"+ )+ return a, b++ def test_home_getter_tracks_active_profile_after_a_prior_scope(self, tmp_path):+ import tools.file_tools_write_guards as ft+ from hermes_constants import (+ reset_hermes_home_override,+ set_hermes_home_override,+ )++ a, b = self._profiles(tmp_path)+ # A normal alpha turn resolves (and, on the buggy path, would freeze) home.+ tok = set_hermes_home_override(str(a))+ try:+ assert ft._get_real_hermes_home() == os.path.realpath(str(a))+ finally:+ reset_hermes_home_override(tok)+ # The next turn is beta — the getter must now return beta's home, not alpha's.+ tok = set_hermes_home_override(str(b))+ try:+ assert ft._get_real_hermes_home() == os.path.realpath(str(b))+ finally:+ reset_hermes_home_override(tok)++ def test_config_getter_tracks_active_profile_after_a_prior_scope(self, tmp_path):+ import tools.file_tools_write_guards as ft+ from hermes_constants import (+ reset_hermes_home_override,+ set_hermes_home_override,+ )++ a, b = self._profiles(tmp_path)+ tok = set_hermes_home_override(str(a))+ try:+ assert ft._get_hermes_config_resolved() == str((a / "config.yaml").resolve())+ finally:+ reset_hermes_home_override(tok)+ tok = set_hermes_home_override(str(b))+ try:+ assert ft._get_hermes_config_resolved() == str((b / "config.yaml").resolve())+ finally:+ reset_hermes_home_override(tok)++ def test_config_hard_block_refuses_beta_config_even_after_alpha_turn(self, tmp_path):+ """End-to-end: the ``config.yaml`` hard-block must fire for beta's own+ config under beta's scope, regardless of alpha having run first."""+ import tools.file_tools_write_guards as ft+ from hermes_constants import (+ reset_hermes_home_override,+ set_hermes_home_override,+ )++ a, b = self._profiles(tmp_path)+ tok = set_hermes_home_override(str(a))+ try:+ ft._get_hermes_config_resolved() # warm the (formerly poisoning) alpha lookup+ finally:+ reset_hermes_home_override(tok)++ tok = set_hermes_home_override(str(b))+ try:+ err = ft._check_sensitive_path(str(b / "config.yaml"), "default")+ finally:+ reset_hermes_home_override(tok)+ assert err is not None+ assert "Refusing to write to Hermes config file" in err++if __name__ == "__main__":pytest.main([__file__, "-v"])diff --git a/tools/file_tools_write_guards.py b/tools/file_tools_write_guards.pyindex a7b63431c74..b567720140e 100644--- a/tools/file_tools_write_guards.py+++ b/tools/file_tools_write_guards.py@@ -24,28 +24,21 @@ _SENSITIVE_PATH_PREFIXES = ("/private/var/db/", "/private/var/root/")_SENSITIVE_EXACT_PATHS = {"/var/run/docker.sock", "/run/docker.sock"}+# NOTE: these four are a TEST-OVERRIDE surface, not a process cache. Both getters+# resolve per call in production (below) because ``get_hermes_home()`` /+# ``get_config_path()`` are per-turn contextvar-scoped: a multiplexed gateway+# (``gateway.multiplex_profiles: true``) serves many profiles in one process, and+# a process-wide memo would freeze whichever profile's home/config ran first —+# making the protected-instruction gate and the ``config.yaml`` hard-block+# order-dependent, up to letting a later profile rewrite its own ``config.yaml``+# the block exists to protect (#107327). A test can still pin a value by setting+# the slot and its ``_loaded`` flag._hermes_config_resolved: str | None = None_hermes_config_resolved_loaded = False_real_hermes_home_cached: str | None = None_real_hermes_home_loaded = False-def _cached_lookup(slot: str, flag: str, primary, fallback) -> str | None:- """Fill module global *slot* once (guarded by *flag*) from ``primary()``, else- ``fallback()``, else None. Module globals so tests can monkeypatch the slots."""- g = globals()- if not g[flag]:- g[flag] = True- try:- g[slot] = primary()- except Exception:- try:- g[slot] = fallback()- except Exception:- g[slot] = None- return g[slot]--def _config_path_resolved() -> str:from hermes_cli.config import get_config_pathreturn str(get_config_path().resolve())@@ -57,15 +50,35 @@ def _hermes_home_real() -> str:def _get_hermes_config_resolved() -> str | None:- """Return the resolved absolute path of the Hermes config file (cached)."""- return _cached_lookup("_hermes_config_resolved", "_hermes_config_resolved_loaded", _config_path_resolved,- lambda: str(Path(_expand_tilde("~/.hermes/config.yaml")).resolve()))+ """Resolved absolute path of the Hermes config file for the ACTIVE profile.++ Resolved per call so it tracks the per-turn ``HERMES_HOME`` scope (#107327);+ a test may pin it via ``_hermes_config_resolved`` + ``_hermes_config_resolved_loaded``."""+ if _hermes_config_resolved_loaded:+ return _hermes_config_resolved+ try:+ return _config_path_resolved()+ except Exception:+ try:+ return str(Path(_expand_tilde("~/.hermes/config.yaml")).resolve())+ except Exception:+ return Nonedef _get_real_hermes_home() -> str | None:- """Return the realpath of the authoritative Hermes home (cached)."""- return _cached_lookup("_real_hermes_home_cached", "_real_hermes_home_loaded", _hermes_home_real,- lambda: os.path.realpath(_expand_tilde("~/.hermes")))+ """Realpath of the authoritative Hermes home for the ACTIVE profile.++ Resolved per call so it tracks the per-turn ``HERMES_HOME`` scope (#107327);+ a test may pin it via ``_real_hermes_home_cached`` + ``_real_hermes_home_loaded``."""+ if _real_hermes_home_loaded:+ return _real_hermes_home_cached+ try:+ return _hermes_home_real()+ except Exception:+ try:+ return os.path.realpath(_expand_tilde("~/.hermes"))+ except Exception:+ return Nonedef _resolved_or_raw(filepath: str, task_id: str) -> str: