The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): auto-archive every served profile's store, not just the launch home
teknium1411a75a56271
diff --git a/gateway/run.py b/gateway/run.pyindex c658a82da3e..e93dc6455fa 100644--- a/gateway/run.py+++ b/gateway/run.py@@ -4594,7 +4594,12 @@ def _housekeeping_org_skill_sync() -> None:def _housekeeping_auto_archive() -> None:"""Stale-session auto-archive on a live timer (the startup hook fires once); maybe_auto_archive()- is gated by sessions.min_interval_hours. Opens its own SessionDB — SQLite connections are thread-bound."""+ is gated by sessions.min_interval_hours. Opens its own SessionDB — SQLite connections are thread-bound.++ Profile-scoped by its caller: ``acquire()`` and ``load_config()`` both resolve through+ ``get_hermes_home()``, so an unscoped tick swept only the LAUNCH profile's store and a+ multiplexed secondary was never archived by anyone — the dashboard/serve trigger defers to+ the gateway for every profile a gateway owns (``web_server_sessions``)."""from hermes_cli.config import load_config as _load_full_configfrom hermes_state_registry import acquire, release_or_close_sess_cfg = (_load_full_config().get("sessions") or {})@@ -4693,11 +4698,12 @@ def _start_gateway_housekeeping(# already ended (#111010). Runs every tick so the outage is bounded by one housekeeping interval.chores.append((1, "Cron ticker supervisor", cron_thread.restart_if_dead))chores += [- # Per served profile: each profile has its own skills tree, curator state and Nous login.+ # Per served profile: each profile has its own skills tree, curator state, Nous login+ # and state.db.(60, "Curator tick", profile_scoped_chore(runner, _housekeeping_curator)),(60, "Sync pull tick", profile_scoped_chore(runner, _housekeeping_skill_sync)),(60, "Org sync pull tick", profile_scoped_chore(runner, _housekeeping_org_skill_sync)),- (60, "Auto-archive tick", _housekeeping_auto_archive),+ (60, "Auto-archive tick", profile_scoped_chore(runner, _housekeeping_auto_archive)),(1, "Deferred FTS retry tick", _housekeeping_deferred_fts_retry),(1, "gateway housekeeping memory trim", _housekeeping_memory_trim),(1, "MCP config reconcile", _mcp_config_reconciler(runner)),diff --git a/hermes_cli/web_server_sessions.py b/hermes_cli/web_server_sessions.pyindex 48cd8dcface..a7a0472b07b 100644--- a/hermes_cli/web_server_sessions.py+++ b/hermes_cli/web_server_sessions.py@@ -231,13 +231,17 @@ def _maybe_auto_archive_for_profile(profile: Optional[str]) -> None:cfg = (_load_full_config().get("sessions") or {})if not cfg.get("auto_archive", False):return- from gateway.status import is_gateway_runtime_lock_active-- # A live gateway owns this profile's store and runs the same sweep in its own- # housekeeping. Opening it WRITABLE from `hermes serve` adds a second writer to a- # database another process is already archiving, for zero extra coverage (#110405).- lock_path = _session_db_path_for_profile(profile).with_name("gateway.lock")- if is_gateway_runtime_lock_active(lock_path):+ from hermes_cli.profiles import _check_gateway_running++ # A live gateway owns this profile's store and runs the same sweep on its own+ # housekeeping tick ("Auto-archive tick" in gateway/run.py, profile-scoped so a+ # multiplexed secondary's store is swept too). Opening it WRITABLE from `hermes+ # serve` adds a second writer to a database another process is already archiving,+ # for zero extra coverage (#110405). `_check_gateway_running` is the canonical+ # per-profile predicate (`_maybe_run_skill_maintenance` below uses it): its+ # multiplexer rung catches a served secondary, which owns no gateway.pid or lock+ # of its own and a bare lock-file probe would report stopped.+ if _check_gateway_running(_session_db_path_for_profile(profile).parent):returndb = _open_session_db_for_profile(profile, read_only=False)try:diff --git a/tests/gateway/test_housekeeping_profile_scope.py b/tests/gateway/test_housekeeping_profile_scope.pyindex 32d7f5b25bb..a201eb562c9 100644--- a/tests/gateway/test_housekeeping_profile_scope.py+++ b/tests/gateway/test_housekeeping_profile_scope.py@@ -101,6 +101,33 @@ def test_multiplexed_sync_ticks_run_once_per_profile_in_its_own_scope(two_homes,assert get_hermes_home() == a+def test_multiplexed_auto_archive_tick_sweeps_every_served_profile_store(two_homes, monkeypatch):+ """The auto-archive sweep reaches each served profile's OWN state.db.++ ``acquire()`` resolves through ``get_hermes_home()``, so an unscoped tick archived the+ launch profile's store only — and `hermes serve`/the dashboard defer to the gateway for+ every profile it owns, so a served secondary would have had no archiver at all.+ """+ from agent.secret_scope import set_multiplex_active+ from hermes_state import SessionDB++ a, b = two_homes+ swept: list = []+ monkeypatch.setattr(+ SessionDB, "maybe_auto_archive", lambda self, **kw: swept.append(Path(self.db_path)))+ monkeypatch.setattr(+ "hermes_cli.config.load_config",+ lambda *args, **kwargs: {"sessions": {"auto_archive": True, "min_interval_hours": 0}})++ set_multiplex_active(True)+ try:+ _run_60_ticks(SimpleNamespace(config=SimpleNamespace(multiplex_profiles=True)))+ finally:+ set_multiplex_active(False)++ assert swept == [a / "state.db", b / "state.db"]++def test_single_profile_sync_ticks_run_once_against_the_process_home(two_homes, monkeypatch):"""Control: a single-profile gateway (multiplex off) still runs each chore exactly once againstthe process home — the named profile directory on disk is not visited."""diff --git a/tests/hermes_cli/test_web_server_auto_archive_gateway_lock.py b/tests/hermes_cli/test_web_server_auto_archive_gateway_lock.pyindex cb7216e4c80..fdbd9315c1d 100644--- a/tests/hermes_cli/test_web_server_auto_archive_gateway_lock.py+++ b/tests/hermes_cli/test_web_server_auto_archive_gateway_lock.py@@ -1,9 +1,11 @@"""Invariant: `hermes serve`'s opportunistic auto-archive never opens a WRITABLE-SessionDB for a profile whose gateway holds the runtime lock (#110405).+SessionDB for a profile whose gateway is live (#110405).-The lock holder is a real subprocess taking the same ``flock(LOCK_EX)`` on-``gateway.lock`` that ``gateway.status`` uses, so the guard is exercised against-kernel lock state rather than a patched predicate.+The gateway stand-in is a real subprocess: it takes the same ``flock(LOCK_EX)`` on+``gateway.lock`` that ``gateway.status`` uses, runs under a ``hermes gateway run``+command line and is recorded in ``gateway.pid``, so ``_check_gateway_running``+decides on kernel lock state plus live process identity rather than a patched+predicate."""from __future__ import annotations@@ -14,7 +16,7 @@ import timeimport pytest-from gateway.status import is_gateway_runtime_lock_active+from hermes_cli.profiles import _check_gateway_runningfrom hermes_cli import web_server_sessions as wsspytestmark = pytest.mark.skipif(@@ -23,7 +25,7 @@ pytestmark = pytest.mark.skipif(_HOLDER = ("import fcntl, sys, time\n"- "handle = open(sys.argv[1], 'a+')\n"+ "handle = open(sys.argv[1], 'a+', encoding='utf-8')\n""fcntl.flock(handle.fileno(), fcntl.LOCK_EX)\n""sys.stdout.write('locked\\n')\n""sys.stdout.flush()\n"@@ -65,17 +67,24 @@ def archive_probe(tmp_path, monkeypatch):return tmp_path, opens, archived-def test_serve_auto_archive_defers_to_the_gateway_holding_the_runtime_lock(archive_probe):[email protected]_gateway_lookalike # a flock-holding stub this test reaps by PID+def test_serve_auto_archive_defers_to_a_live_gateway_for_the_profile(archive_probe):tmp_path, opens, archived = archive_probe+ # argv0 basename `hermes` + the `gateway run` subcommand is what+ # gateway.status's process-identity check reads off /proc for this PID.+ entrypoint = tmp_path / "hermes"+ entrypoint.write_text(_HOLDER, encoding="utf-8")lock_path = tmp_path / "gateway.lock"holder = subprocess.Popen(- [sys.executable, "-c", _HOLDER, str(lock_path)],+ [sys.executable, str(entrypoint), str(lock_path), "gateway", "run"],stdout=subprocess.PIPE, stdin=subprocess.DEVNULL, text=True,)stdout = holder.stdoutassert stdout is not Nonetry:assert stdout.readline().strip() == "locked"+ (tmp_path / "gateway.pid").write_text(str(holder.pid), encoding="utf-8")+ assert _check_gateway_running(tmp_path), "probe did not look like a live gateway"wss._maybe_auto_archive_for_profile(None)@@ -86,13 +95,13 @@ def test_serve_auto_archive_defers_to_the_gateway_holding_the_runtime_lock(archiholder.wait(timeout=10)stdout.close()- # Lock released: the serve-side sweep is the only archiver left and must run.+ # Gateway gone: the serve-side sweep is the only archiver left and must run.deadline = time.monotonic() + 5- while is_gateway_runtime_lock_active(lock_path) and time.monotonic() < deadline:+ while _check_gateway_running(tmp_path) and time.monotonic() < deadline:time.sleep(0.05)wss._last_auto_archive_check.clear()wss._maybe_auto_archive_for_profile(None)- assert opens == [False], "serve must still archive when no gateway holds the lock"+ assert opens == [False], "serve must still archive when no gateway owns the profile"assert len(archived) == 1