hermes-agent/tests/tools/test_process_registry_lazy_restore.py
teknium1 d6c9191368 fix(review): replay the launch ledger regardless of the first consumer's profile scope (#123265)
Review finding (major): ProcessRegistry.restore_completions() is once-per-process but read
async_delegation._db_path() (ContextVar-aware) under whatever scope the FIRST consumer ran in.
In the TUI gateway both first consumers (the session notification poller and the prompt_turn
drain) run inside _session_profile_runtime_scope(session), so under multi-profile
`hermes serve` the first session's profile ledger was replayed and the LAUNCH profile's
undelivered completions were never replayed for the life of the process (origin/main's
import-time replay always covered the launch profile).

Fix: restore_completions() clears the hermes-home override for the duration of the replay
(set_hermes_home_override(None) + reset), so the once-per-process replay always reads the
launch ledger whoever gets there first; the caller's scope is restored afterwards. Smaller
than a per-home restored-set plus a tui_gateway boot hook: it restores exactly main's
invariant with no new boot seam, and secondaries stay where they were (gateway
_restore_secondary_completion_ledgers). Docstring states the invariant.

Tests:
- tests/tools/test_process_registry_lazy_restore.py::test_first_drain_under_secondary_scope_replays_the_launch_ledger
  (red on the PR head: replayed profiles/b/state.db; green now)
- tests/gateway/test_multiplex_unserved_shared_ingress.py::test_boot_replays_the_launch_ledger_before_secondaries_and_watchers
  (minor: pins the gateway boot hook — launch-scope replay in _start_secondary_profiles, i.e.
  before the secondary bind and before _async_delegation_watcher, the only production consumer
  that reads completion_queue.get_nowait() without drain_notifications)
2026-09-28 05:37:55 -07:00

50 lines
2.5 KiB
Python

"""Importing the tool registry must not touch state.db; the durable completion replay runs when a
consumer asks for it (#123265)."""
import os
import subprocess
import sys
from pathlib import Path
REPO = Path(__file__).resolve().parents[2]
def test_model_tools_import_creates_no_state_db(tmp_path):
typo_home = tmp_path / "profiles" / "typo" # a missing named profile: a typo'd HERMES_HOME
env = {**os.environ, "HERMES_HOME": str(typo_home), "PYTHONPATH": str(REPO)}
env.pop("HERMES_DELEGATED_CHILD_CONTEXT", None)
proc = subprocess.run([sys.executable, "-c", "import model_tools"], cwd=REPO, env=env,
text=True, capture_output=True, timeout=120)
assert proc.returncode == 0, proc.stderr[-2000:]
assert not (typo_home / "state.db").exists(), sorted(os.listdir(typo_home)) if typo_home.exists() else None
def test_restore_runs_once_on_first_drain(monkeypatch):
from tools import async_delegation, process_registry as pr_mod
calls = []
monkeypatch.setattr(async_delegation, "restore_undelivered_completions", lambda q: calls.append(q) or 1)
registry = pr_mod.ProcessRegistry()
assert calls == [] # construction (module import) does not replay the ledger
registry.drain_notifications("sess")
registry.drain_notifications("sess")
assert registry.restore_completions() == 0
assert calls == [registry.completion_queue] # first consumer restores, exactly once
def test_first_drain_under_secondary_scope_replays_the_launch_ledger(monkeypatch, tmp_path):
"""Under multi-profile ``hermes serve`` the first consumer is a session bound to a secondary
profile (TUI poller / prompt_turn drain); the once-per-process replay must still read the LAUNCH
ledger, or it is never replayed for the life of the process."""
from hermes_constants import get_hermes_home, reset_hermes_home_override, set_hermes_home_override
from tools import async_delegation, process_registry as pr_mod
monkeypatch.setenv("HERMES_HOME", str(tmp_path / "launch"))
ledgers = []
monkeypatch.setattr(async_delegation, "restore_undelivered_completions",
lambda q: ledgers.append(async_delegation._db_path()) or 0)
registry = pr_mod.ProcessRegistry()
token = set_hermes_home_override(tmp_path / "launch" / "profiles" / "b")
try:
registry.drain_notifications("sess")
assert get_hermes_home() == tmp_path / "launch" / "profiles" / "b" # caller scope untouched
finally:
reset_hermes_home_override(token)
assert ledgers == [tmp_path / "launch" / "state.db"]