Skip to content

The agent that grows with you

15.6 KB
Raw Blame
"""The ConfigBackend seam (config-config design §4.1, D10) and its reader gate."""

import sys
from pathlib import Path
from unittest.mock import patch

import pytest

from hermes_cli import config_backend as cb

REPO = Path(__file__).resolve().parents[2]


@pytest.fixture
def home(tmp_path, monkeypatch):
    monkeypatch.delenv(cb.BACKEND_ENV, raising=False)
    h = tmp_path / "home"
    h.mkdir()
    return h


class TestRouting:

    def test_default_is_file(self, home):
        assert cb.get_config_backend().name == "file"
        assert cb.supports_file_tooling() is True

    def test_unknown_backend_fails_closed(self, home, monkeypatch):
        # No config value can select the backend, and an unknown one never falls back to the file.
        monkeypatch.setenv(cb.BACKEND_ENV, "bogus")
        with pytest.raises(cb.ConfigBackendUnavailable):
            cb.get_config_backend()
        with pytest.raises(cb.ConfigBackendUnavailable):
            cb.read_config_doc(home / "config.yaml")

    def test_unconfigured_remote_backend_fails_closed_on_read(self, home, monkeypatch):
        # Selected from the env alone; without its plane identity the first read exits, and the
        # local config.yaml is never used as a fallback (D2).
        from plugins.config_backends import remote
        (home / "config.yaml").write_text("a: 1\n", encoding="utf-8")
        monkeypatch.setenv(cb.BACKEND_ENV, "remote")
        monkeypatch.delenv("HERMES_CONFIG_INSTANCE_ID", raising=False)
        remote._reset_for_tests()
        try:
            assert cb.get_config_backend().name == "remote"
            with pytest.raises(cb.ConfigBackendUnavailable, match="Remote Config"):
                cb.read_config_doc(home / "config.yaml")
        finally:
            remote._reset_for_tests()

    def test_explicit_non_config_file_never_reaches_the_backend(self, home, monkeypatch):
        # Only ``<home>/config.yaml`` is a user layer; any other file is read where it is.
        def no_backend():
            raise AssertionError("an explicit file must not consult the backend")
        monkeypatch.setattr(cb, "get_config_backend", no_backend)
        other = home / "import-source.yaml"
        other.write_text("a: 1\n", encoding="utf-8")
        assert cb.read_config_doc(other) == {"a": 1}


class TestBootstrapSelector:
    """The one bootstrap (``_ensure_bootstrap_env``) keeps the .env selector rules #134362 had in
    ``_dotenv_backend_override``: the launch home's ``.env`` overrides a stale shell value, an
    explicitly empty value resets to ``file``, an absent key leaves the shell value alone."""

    @pytest.fixture(autouse=True)
    def fresh_bootstrap(self, home, monkeypatch):
        from plugins.config_backends import remote
        monkeypatch.setenv("HERMES_HOME", str(home))
        monkeypatch.delenv("HERMES_CONFIG_INSTANCE_ID", raising=False)
        monkeypatch.setattr(cb, "_BOOTSTRAPPED", False)
        remote._reset_for_tests()
        yield
        remote._reset_for_tests()

    @pytest.mark.parametrize("shell, dotenv, expected", [
        ("remote", "HERMES_CONFIG_BACKEND=file\n", "file"),       # stale shell value, .env says file
        ("remote", "HERMES_CONFIG_BACKEND=\n", "file"),           # explicit empty value resets
        (None, "HERMES_CONFIG_BACKEND=remote\n", "remote"),       # .env-only remote selects remote
        ("remote", "OTHER=1\n", "remote"),                        # absent key keeps the shell value
    ])
    def test_dotenv_selector(self, home, monkeypatch, shell, dotenv, expected):
        if shell is None:
            monkeypatch.delenv(cb.BACKEND_ENV, raising=False)
        else:
            monkeypatch.setenv(cb.BACKEND_ENV, shell)
        (home / ".env").write_text(dotenv, encoding="utf-8")
        assert cb.get_config_backend().name == expected

    def test_dotenv_backend_override_is_gone(self):
        # One mechanism: the bootstrap replaces #134362's per-call .env override.
        assert not hasattr(cb, "_dotenv_backend_override")


class TestRemoteBackendShape:

    def test_remote_backend_is_not_a_file_backend(self):
        # probe_config_readable and every FileBackend-typed path must never treat the remote
        # layer as a local file.
        from plugins.config_backends.remote.backend import RemoteBackend
        assert not issubclass(RemoteBackend, cb.FileBackend)
        for member in ("read_user_doc", "read_user_doc_readonly", "version", "exists",
                       "probe_readable", "write_changes"):
            assert callable(getattr(RemoteBackend, member, None)), member


class TestFileBackend:

    def test_missing(self, home):
        path = home / "config.yaml"
        assert cb.config_exists(path) is False
        with pytest.raises(FileNotFoundError):
            cb.config_version(path)
        with pytest.raises(FileNotFoundError):
            cb.read_config_doc(path)

    def test_version_or_none_and_token(self, home):
        path = home / "config.yaml"
        assert cb.config_version_or_none(path) is None
        assert cb.version_token(None) == 0
        path.write_text("a: 1\n", encoding="utf-8")
        version = cb.config_version_or_none(path)
        assert version == cb.config_version(path)
        assert cb.version_token(version) == version[0] / 1_000_000_000 > 0
        # An opaque non-file version still yields a stable non-zero number.
        assert cb.version_token(("etag-1",)) == cb.version_token(("etag-1",)) != 0
        assert cb.version_token(("etag-1",)) != cb.version_token(("etag-2",))

    def test_read_version_and_bom(self, home):
        path = home / "config.yaml"
        path.write_bytes("\ufeffmodel:\n  default: x\n".encode("utf-8"))
        assert cb.config_exists(path)
        assert cb.read_config_doc(path) == {"model": {"default": "x"}}
        assert cb.read_config_doc_readonly(path) == {"model": {"default": "x"}}
        v1 = cb.config_version(path)
        path.write_text("model:\n  default: longer-value\n", encoding="utf-8")
        assert cb.config_version(path) != v1

    def test_writes_preserve_comments(self, home):
        path = home / "config.yaml"
        path.write_text("# keep me\nmodel:\n  default: a  # inline\n", encoding="utf-8")
        cb.write_config_key(path, "model.default", "b")
        cb.write_config_key(path, "display.personality", "kawaii")
        text = path.read_text(encoding="utf-8")
        assert "# keep me" in text and "# inline" in text
        assert cb.read_config_doc(path) == {"model": {"default": "b"}, "display": {"personality": "kawaii"}}
        cb.write_config_document(path, {"model": {"default": "c"}})
        assert "# keep me" in path.read_text(encoding="utf-8")
        assert cb.read_config_doc(path) == {"model": {"default": "c"}}


class _RecordingBackend(cb.FileBackend):
    """The file backend, recording which operations each pipeline asks of it."""

    name = "recording"

    def __init__(self):
        self.calls = []

    def _rec(self, op, home):
        self.calls.append((op, Path(home)))

    def read_user_doc(self, home):
        self._rec("read", home)
        return super().read_user_doc(home)

    def read_user_doc_readonly(self, home):
        self._rec("read", home)
        return super().read_user_doc_readonly(home)

    def version(self, home):
        self._rec("version", home)
        return super().version(home)

    def exists(self, home):
        self._rec("exists", home)
        return super().exists(home)

    def write_changes(self, home, changes):
        self._rec("write", home)
        return super().write_changes(home, changes)

    def ops(self, home):
        return {op for op, h in self.calls if h == Path(home)}


@pytest.fixture
def recording(monkeypatch, tmp_path):
    """A fresh HERMES_HOME with a config.yaml and every config read/write recorded."""
    from hermes_cli import config as config_mod
    home = tmp_path / ".hermes"
    home.mkdir()
    (home / "config.yaml").write_text(
        "# hand-tuned\nmodel:\n  default: my-model\nsecrets:\n  sources: []\n", encoding="utf-8")
    monkeypatch.setenv("HERMES_HOME", str(home))
    for cache in (config_mod._LOAD_CONFIG_CACHE, config_mod._RAW_CONFIG_CACHE):
        cache.clear()
    backend = _RecordingBackend()
    monkeypatch.setattr(cb, "get_config_backend", lambda: backend)
    return home, backend


class TestPipelinesConsultTheBackend:
    """Every read pipeline and writer reaches the selected backend, keyed by the hermes home."""

    def test_load_config(self, recording):
        from hermes_cli.config import load_config
        home, backend = recording
        assert load_config()["model"]["default"] == "my-model"
        assert {"version", "read"} <= backend.ops(home)

    def test_raw_primitives(self, recording):
        from hermes_cli.config import read_raw_config, read_user_config_raw
        home, backend = recording
        assert read_raw_config()["model"]["default"] == "my-model"
        assert read_user_config_raw()["model"]["default"] == "my-model"
        assert {"version", "read"} <= backend.ops(home)

    def test_effective_loader(self, recording):
        from hermes_cli import config_effective
        home, backend = recording
        config_effective._EFFECTIVE_CACHE.clear()
        assert config_effective.load_user_config_effective()["model"]["default"] == "my-model"
        assert {"version", "read"} <= backend.ops(home)

    def test_cli_loader(self, recording):
        from hermes_cli.cli_config_load import load_cli_config
        home, backend = recording
        load_cli_config()
        assert {"exists", "read"} <= backend.ops(home)

    def test_gateway_yaml_layers(self, recording):
        from gateway.config_loader import read_yaml_layers
        home, backend = recording
        assert read_yaml_layers(home)["model"]["default"] == "my-model"
        assert "read" in backend.ops(home)

    def test_dotenv_secrets_read(self, recording):
        from hermes_cli.env_loader import _load_secrets_config
        home, backend = recording
        _load_secrets_config(home)
        assert "read" in backend.ops(home)

    def test_writers(self, recording):
        from hermes_cli.config import load_config, save_config
        from hermes_cli.personality import persist_personality
        home, backend = recording
        cfg = load_config()
        cfg["model"]["default"] = "other-model"
        save_config(cfg)
        assert persist_personality("kawaii")
        assert [op for op, h in backend.calls if op == "write" and h == home] == ["write", "write"]
        text = (home / "config.yaml").read_text(encoding="utf-8")
        assert "# hand-tuned" in text and "other-model" in text and "kawaii" in text


class TestFileTooling:

    def test_file_backend_allows_it(self, home):
        cb.require_file_tooling("x")

    def test_backend_without_a_file_refuses_it(self, home, monkeypatch):
        class NoFile(cb.FileBackend):
            name = "nofile"

            def supports_file_tooling(self):
                return False
        monkeypatch.setattr(cb, "get_config_backend", NoFile)
        from hermes_cli.config_backups import backup_config
        (home / "config.yaml").write_text("a: 1\n", encoding="utf-8")
        assert backup_config(home / "config.yaml", "test") is None
        with pytest.raises(cb.ConfigBackendUnavailable, match="Profile clone"):
            from hermes_cli.profiles import _resolve_clone_source
            _resolve_clone_source(None)


class TestReaderGate:

    def _guard(self):
        sys.path.insert(0, str(REPO / "scripts"))
        try:
            import check_config_yaml_readers as guard
        finally:
            sys.path.pop(0)
        return guard

    def test_flags_direct_readers(self, tmp_path):
        guard = self._guard()
        bad = tmp_path / "hermes_cli" / "bad_reader.py"
        bad.parent.mkdir()
        bad.write_text(
            "import yaml\n"
            "def a(home):\n    p = home / 'config.yaml'\n    return yaml.safe_load(p.read_text())\n"
            "def b(config_path):\n    return config_path.exists()\n"
            "def c(other):\n    return other.read_text()\n"
            "def d(cfg_path):\n    return cfg_path.stat()  # config-reader: ok — test\n"
            "def e():\n    from utils import atomic_roundtrip_yaml_update\n"
            "    atomic_roundtrip_yaml_update(get_config_path(), 'a', 1)\n",
            encoding="utf-8")
        with patch.object(guard, "ROOT", tmp_path):
            problems = guard.scan_file(bad)
        assert sorted(int(p.split(":")[1]) for p in problems) == [4, 6, 13], problems

    @pytest.mark.parametrize("call", [
        "yaml.full_load(config_path)", "yaml.unsafe_load(config_path)", "yaml.compose(config_path)",
        "yaml.load_all(config_path)", "os.path.getsize(config_path)",
        "config_path.stat()  # config-reader: ok",  # an escape without a reason does not suppress
    ])
    def test_every_read_form_is_flagged(self, tmp_path, call):
        guard = self._guard()
        bad = tmp_path / "hermes_cli" / "bad_reader.py"
        bad.parent.mkdir()
        bad.write_text(f"import os, yaml\ndef a(config_path):\n    return {call}\n", encoding="utf-8")
        with patch.object(guard, "ROOT", tmp_path):
            assert len(guard.scan_file(bad)) == 1

    @pytest.mark.parametrize("body", [
        "env_path, p = home / '.env', home / 'config.yaml'\n    return p.exists()",
        "self.cfg = home / 'config.yaml'\n    return open(self.cfg)",
        "for p in (home / 'config.yaml',):\n        p.read_text()",
        "p = home / 'config.yaml'\n    return Path.read_text(p)",
        "p = home / 'config.yaml'\n    shutil.copy2(p, home / 'bak')",
        "return read_bytes_or_none(home / 'config.yaml')",
        "p = home / 'config.yaml'\n    return io.open(p)",
        "p = home / 'config.yaml'\n    return builtins.open(p)",
        "p = home / 'config.yaml'\n    return open(file=p)",
        "first = home / 'config.yaml'\n    second = first\n    third = second\n    return third.read_text()",
    ])
    def test_every_binding_form_is_tracked(self, tmp_path, body):
        # Tuple unpacking, attributes, loop targets, unbound Path methods, copies, byte reads.
        guard = self._guard()
        bad = tmp_path / "pm" / "bad_reader.py"
        bad.parent.mkdir()
        bad.write_text(f"def a(self, home):\n    {body}\n", encoding="utf-8")
        with patch.object(guard, "ROOT", tmp_path):
            assert len(guard.scan_file(bad)) == 1

    def test_scopes_follow_python(self, tmp_path):
        # An attribute bound in __init__ is seen by every method; a comprehension's name stays inside it.
        guard = self._guard()
        src = tmp_path / "hermes_cli" / "scoped.py"
        src.parent.mkdir()
        src.write_text(
            "from hermes_cli.config_backend import read_config_doc\n"
            "class A:\n    def __init__(self, home):\n        self.cfg = home / 'config.yaml'\n"
            "    def read(self):\n        return self.cfg.read_text()\n"
            "def b(home, p):\n    docs = [read_config_doc(p) for p in (home / 'config.yaml',)]\n"
            "    return docs, p.read_text()\n",
            encoding="utf-8")
        with patch.object(guard, "ROOT", tmp_path):
            problems = guard.scan_file(src)
        assert [int(p.split(":")[1]) for p in problems] == [6], problems

    def test_backend_calls_are_clean(self, tmp_path):
        guard = self._guard()
        good = tmp_path / "hermes_cli" / "good_reader.py"
        good.parent.mkdir()
        good.write_text(
            "from hermes_cli.config_backend import config_exists, read_config_doc\n"
            "def a(home):\n    p = home / 'config.yaml'\n"
            "    return read_config_doc(p) if config_exists(p) else {}\n",
            encoding="utf-8")
        with patch.object(guard, "ROOT", tmp_path):
            assert guard.scan_file(good) == []