Skip to content

The agent that grows with you

7.6 KB
Raw Blame
"""Existing databases remain usable while a config writer finishes publication."""

from concurrent.futures import ThreadPoolExecutor
from contextlib import closing
import os
from pathlib import Path
import sqlite3
import subprocess
import sys
import textwrap
from threading import Event, Thread

import pytest

from hermes_cli import config
from hermes_state_wal import apply_wal_with_fallback
from utils import atomic_yaml_write
import hermes_state_wal as wal


def test_config_import_and_provider_env_catalog_never_read_config(tmp_path):
    """Importing hermes_cli.config and building the provider part of OPTIONAL_ENV_VARS list no
    providers through discovery (which reads config), so neither can take part in a config/WAL
    lock-order cycle with a thread opening a database."""
    script = textwrap.dedent(
        """
        from pathlib import Path
        from threading import Thread
        import os, sqlite3

        import providers
        import hermes_state_wal as wal

        listed = []
        providers.list_providers = lambda: listed.append(1) or []
        root = Path(os.environ["STATE_CONFIG_TEST_ROOT"]) / "cold-import"
        root.mkdir()
        os.environ["HOME"] = str(root)
        os.environ["HERMES_HOME"] = str(root / ".hermes")
        database = root / "state.db"
        with sqlite3.connect(database) as connection:
            connection.execute("CREATE TABLE retained (value TEXT)")
        outcomes = []

        def importer():
            import hermes_cli.config
            outcomes.append("import-returned")
            assert "ANTHROPIC_API_KEY" in hermes_cli.config.OPTIONAL_ENV_VARS
            outcomes.append("fill-returned")

        def opener():
            with sqlite3.connect(database) as connection:
                outcomes.append(f"open:{wal.apply_wal_with_fallback(connection)}")

        threads = [Thread(target=importer), Thread(target=opener)]
        for t in threads:
            t.start()
        for t in threads:
            t.join(10)
        assert not any(t.is_alive() for t in threads), outcomes
        assert "import-returned" in outcomes and "fill-returned" in outcomes, outcomes
        assert any(value.startswith("open:") for value in outcomes)
        assert listed == [], "the config import or the env catalog ran provider discovery"
        """
    )
    completed = subprocess.run(
        [sys.executable, "-c", script],
        cwd=Path(__file__).parents[1],
        env={
            **os.environ,
            "HOME": str(tmp_path),
            "HERMES_HOME": str(tmp_path / ".hermes"),
            "STATE_CONFIG_TEST_ROOT": str(tmp_path),
        },
        capture_output=True,
        text=True,
        timeout=15, check=False
    )
    assert completed.returncode == 0, completed.stdout + completed.stderr


@pytest.mark.parametrize(
    "mode,require_wal,vulnerable",
    [
        ("delete", False, True),
        ("delete", False, False),
        ("wal", False, False),
        ("wal", True, False),
    ],
)
def test_existing_database_does_not_wait_for_config_writer(
    tmp_path, monkeypatch, caplog, mode, require_wal, vulnerable
):
    monkeypatch.setenv("HOME", str(tmp_path))
    monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))
    monkeypatch.setattr(Path, "home", lambda: tmp_path)
    monkeypatch.setattr(wal, "is_sqlite_wal_reset_vulnerable", lambda: vulnerable)
    config.save_config({"database": {"journal_mode": mode}, "marker": "before"})
    config.load_config_readonly()
    path = tmp_path / "existing.db"
    with closing(sqlite3.connect(path)) as conn, conn:
        assert conn.execute(f"PRAGMA journal_mode={mode}").fetchone()[0] == mode
        conn.execute("CREATE TABLE retained (value TEXT)")
        conn.execute("INSERT INTO retained VALUES ('keep')")

    entered, release = Event(), Event()

    def writer():
        with config._CONFIG_LOCK:
            atomic_yaml_write(config.get_config_path(), {
                "database": {"journal_mode": "delete"}, "marker": "after replacement",
            })
            entered.set()
            assert release.wait(10)

    def opener():
        conn = sqlite3.connect(path)
        try:
            selected = apply_wal_with_fallback(
                conn,
                db_label=f"contention-{mode}-{require_wal}-{vulnerable}",
                require_wal=require_wal,
            )
            retained = conn.execute("SELECT value FROM retained").fetchone()[0]
            conn.execute("INSERT INTO retained VALUES ('during config write')")
            conn.commit()
            return selected, retained
        finally:
            conn.close()

    worker = Thread(target=writer)
    worker.start()
    assert entered.wait(5)
    with ThreadPoolExecutor(max_workers=1) as pool:
        future = pool.submit(opener)
        try:
            assert future.result(timeout=2) == (mode, "keep")
        finally:
            release.set()
            worker.join(5)
    assert not worker.is_alive()
    warnings = [
        record
        for record in caplog.records
        if "WAL-reset corruption bug" in record.message
        and "using journal_mode=DELETE" in record.message
    ]
    if vulnerable:
        assert len(warnings) == 1
    else:
        assert not warnings
    assert config.load_config_readonly()["marker"] == "after replacement"
    with closing(sqlite3.connect(path)) as conn:
        assert conn.execute("SELECT COUNT(*) FROM retained").fetchone()[0] == 2


@pytest.mark.parametrize("state", ["empty", "unknown", "required_wal"])
def test_unestablished_or_strict_modes_still_wait_for_real_policy(tmp_path, monkeypatch, state):
    monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))
    monkeypatch.setenv("HOME", str(tmp_path))
    monkeypatch.setattr(Path, "home", lambda: tmp_path)
    monkeypatch.setattr(wal, "is_sqlite_wal_reset_vulnerable", lambda: False)
    config.save_config({"database": {"journal_mode": "wal"}})
    path = tmp_path / "policy.db"
    if state != "empty":
        with closing(sqlite3.connect(path)) as conn:
            conn.execute("CREATE TABLE existing (value TEXT)")
    if state == "unknown":
        monkeypatch.setattr(wal, "_on_disk_journal_mode", lambda _conn: None)
    resolving = Event()
    original = wal.resolve_journal_mode

    def resolve():
        resolving.set()
        return original()

    monkeypatch.setattr(wal, "resolve_journal_mode", resolve)

    def opener():
        with closing(sqlite3.connect(path)) as conn:
            return wal.apply_wal_with_fallback(conn, require_wal=state == "required_wal")

    with ThreadPoolExecutor(max_workers=1) as pool:
        with config._CONFIG_LOCK:
            pending = pool.submit(opener)
            assert resolving.wait(5)
            assert not pending.done()
        assert pending.result(timeout=5) == "wal"


def test_deferred_mode_change_is_applied_after_writer_finishes(tmp_path, monkeypatch):
    monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))
    monkeypatch.setenv("HOME", str(tmp_path))
    monkeypatch.setattr(Path, "home", lambda: tmp_path)
    monkeypatch.setattr(wal, "is_sqlite_wal_reset_vulnerable", lambda: False)
    config.save_config({"database": {"journal_mode": "wal"}})
    path = tmp_path / "deferred.db"
    with closing(sqlite3.connect(path)) as conn:
        conn.execute("CREATE TABLE existing (value TEXT)")

    def opener():
        with closing(sqlite3.connect(path)) as conn:
            return wal.apply_wal_with_fallback(conn)

    with ThreadPoolExecutor(max_workers=1) as pool:
        with config._CONFIG_LOCK:
            assert pool.submit(opener).result(timeout=2) == "delete"
        assert pool.submit(opener).result(timeout=5) == "wal"