The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(config): reuse unchanged legacy gateway reads
Victor Kyriazakos4f3c4be7cac3
diff --git a/gateway/config_loader.py b/gateway/config_loader.pyindex a5f147b9f0d..f39ea1b78fa 100644--- a/gateway/config_loader.py+++ b/gateway/config_loader.py@@ -7,6 +7,8 @@ silently replaced by the nested one. Both overwrite whatever legacy gateway.json"""import contextlib+import copy+from functools import lru_cacheimport jsonimport loggingimport os@@ -20,28 +22,39 @@ logger = logging.getLogger("gateway.config")def load_legacy_gateway_json(home: Path) -> Any:- """Legacy ``gateway.json`` base layer (config.yaml keys always win). Malformed → ``{}`` + warning."""+ """Legacy base layer; cache unchanged files, never merged policy or failed reads.++ Return a private copy because platform merging mutates this layer. File identity+ and ctime also invalidate atomic replacements and same-size timestamp-preserving edits.+ """path = home / "gateway.json"if not path.exists():return {}try:- with open(path, "r", encoding="utf-8") as f:- data = json.load(f) or {}- # Relay accepts a legacy opt-out as authoritative, just like YAML.- # Preserve that intent through credential activation and the exclusive- # native-adapter sweep, even when config.yaml is missing. Other legacy- # platforms retain their existing env-driven enablement semantics.- platforms = data.get("platforms") if isinstance(data, dict) else None- relay = platforms.get("relay") if isinstance(platforms, dict) else None- if isinstance(relay, dict) and "enabled" in relay:- _dict_slot(relay, "extra")["_enabled_explicit"] = True- logger.info("Loaded legacy %s — consider moving settings to config.yaml", path)- return data+ st = path.stat()+ version = (st.st_mtime_ns, st.st_size, st.st_ctime_ns, st.st_dev, st.st_ino)+ return copy.deepcopy(_read_legacy_gateway_json(path.absolute(), version))except Exception as e:logger.warning("Failed to load %s: %s", path, e)return {}+@lru_cache(maxsize=32)+def _read_legacy_gateway_json(path: Path, version: tuple) -> Any:+ with open(path, "r", encoding="utf-8") as f:+ data = json.load(f) or {}+ # Relay accepts a legacy opt-out as authoritative, just like YAML.+ # Preserve that intent through credential activation and the exclusive+ # native-adapter sweep, even when config.yaml is missing. Other legacy+ # platforms retain their existing env-driven enablement semantics.+ platforms = data.get("platforms") if isinstance(data, dict) else None+ relay = platforms.get("relay") if isinstance(platforms, dict) else None+ if isinstance(relay, dict) and "enabled" in relay:+ _dict_slot(relay, "extra")["_enabled_explicit"] = True+ logger.info("Loaded legacy %s — consider moving settings to config.yaml", path)+ return data++# --- top-level key bridging ----------------------------------------------------## Top-level settings are also accepted nested under ``gateway:`` (what ``hermes config setdiff --git a/tests/gateway/relay/test_legacy_config_cache.py b/tests/gateway/relay/test_legacy_config_cache.pynew file mode 100644index 00000000000..4e444622190--- /dev/null+++ b/tests/gateway/relay/test_legacy_config_cache.py@@ -0,0 +1,125 @@+"""Legacy disk caching must never become a cached relay policy decision."""+import builtins+import json+import logging+import os+from pathlib import Path++import pytest++from gateway.config_loader import load_legacy_gateway_json+from gateway.relay import relay_explicitly_disabled+from hermes_constants import reset_hermes_home_override, set_hermes_home_override+++def test_legacy_reads_are_reused_without_stale_policy(tmp_path, monkeypatch, caplog):+ home = tmp_path / "primary"+ other = tmp_path / "secondary"+ managed = tmp_path / "managed"+ for directory in (home, other, managed):+ directory.mkdir()+ monkeypatch.setenv("HERMES_HOME", str(home))+ monkeypatch.setenv("HERMES_MANAGED_DIR", str(managed))+ legacy = home / "gateway.json"+ disabled = json.dumps({"platforms": {"relay": {"enabled": False}}})+ enabled = json.dumps({"platforms": {"relay": {"enabled": True}}})+ actual_open = builtins.open+ opens = []++ def count_open(file, *args, **kwargs):+ if Path(file).name == "gateway.json":+ opens.append(Path(file))+ return actual_open(file, *args, **kwargs)++ monkeypatch.setattr(builtins, "open", count_open)+ caplog.set_level(logging.INFO, logger="gateway.config")+ assert not relay_explicitly_disabled() # An absent file must not be cached.+ legacy.write_text(disabled, encoding="utf-8")+ for _ in range(10):+ assert relay_explicitly_disabled()+ assert opens == [legacy]+ assert len([r for r in caplog.records if "Loaded legacy" in r.message]) == 1++ # Both the marker-bearing parse and nested data belong to each caller.+ loaded = load_legacy_gateway_json(home)+ loaded["platforms"]["relay"]["enabled"] = True+ loaded["platforms"]["relay"]["extra"]["_enabled_explicit"] = False+ assert relay_explicitly_disabled()+ assert load_legacy_gateway_json(home)["platforms"]["relay"]["extra"]["_enabled_explicit"]++ (other / "gateway.json").write_text(enabled, encoding="utf-8")+ token = set_hermes_home_override(other)+ try:+ assert not relay_explicitly_disabled()+ finally:+ reset_hermes_home_override(token)+ assert relay_explicitly_disabled()+ assert opens == [legacy, other / "gateway.json"]++ # User and managed policy are recomposed even on a legacy cache hit.+ user = home / "config.yaml"+ user.write_text("platforms:\n relay:\n enabled: true\n", encoding="utf-8")+ assert not relay_explicitly_disabled()+ overlay = managed / "config.yaml"+ overlay.write_text("platforms:\n relay:\n enabled: ${CACHE_TEST_RELAY}\n", encoding="utf-8")+ monkeypatch.setenv("CACHE_TEST_RELAY", "false")+ assert relay_explicitly_disabled()+ monkeypatch.setenv("CACHE_TEST_RELAY", "true")+ assert not relay_explicitly_disabled()+ overlay.write_text("platforms:\n relay:\n enabled: false\n", encoding="utf-8")+ assert relay_explicitly_disabled()+ overlay.unlink()+ assert not relay_explicitly_disabled()+ user.unlink()+ assert relay_explicitly_disabled()+ assert opens == [legacy, other / "gateway.json"]++ legacy.write_text(enabled, encoding="utf-8")+ assert not relay_explicitly_disabled()+ # Atomic replacement must invalidate even with equal mtime and byte length.+ replacement = home / "replacement.json"+ replacement.write_text(disabled, encoding="utf-8")+ replacement.replace(legacy)+ assert relay_explicitly_disabled()+ stat = legacy.stat()+ replacement.write_text(enabled + " ", encoding="utf-8")+ os.utime(replacement, ns=(stat.st_atime_ns, stat.st_mtime_ns))+ assert replacement.stat().st_size == stat.st_size+ replacement.replace(legacy)+ assert not relay_explicitly_disabled()+ legacy.unlink()+ assert not relay_explicitly_disabled()+ legacy.write_text(disabled, encoding="utf-8")+ assert relay_explicitly_disabled()++[email protected]("invalid", ["malformed", "unreadable"])+def test_failed_legacy_reads_warn_and_recover(tmp_path, monkeypatch, caplog, invalid):+ legacy = tmp_path / "gateway.json"+ valid = json.dumps({"platforms": {"relay": {"enabled": False}}})+ legacy.write_text(valid, encoding="utf-8")+ assert load_legacy_gateway_json(tmp_path)["platforms"]["relay"]["enabled"] is False+ caplog.set_level(logging.WARNING, logger="gateway.config")+ actual_open = builtins.open+ unreadable = invalid == "unreadable"+ attempts = []++ def read(file, *args, **kwargs):+ if Path(file) == legacy:+ attempts.append(file)+ if unreadable:+ raise PermissionError("synthetic unreadable legacy file")+ return actual_open(file, *args, **kwargs)++ monkeypatch.setattr(builtins, "open", read)+ legacy.write_text("{" if invalid == "malformed" else valid + " ", encoding="utf-8")+ for _ in range(2):+ assert load_legacy_gateway_json(tmp_path) == {}+ assert len(attempts) == 2+ assert len([r for r in caplog.records if "Failed to load" in r.message]) == 2+ unreadable = False+ # A different recovered value proves we did not resurrect the old parse.+ legacy.write_text(json.dumps({"platforms": {"relay": {"enabled": True}}}), encoding="utf-8")+ assert load_legacy_gateway_json(tmp_path)["platforms"]["relay"]["enabled"] is True+ assert load_legacy_gateway_json(tmp_path)["platforms"]["relay"]["enabled"] is True+ assert len(attempts) == 3