The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cron): drop a departed profile's store state by its resolved store path
kshitijk4poor844e514e6769
diff --git a/cron/scheduler_ownership.py b/cron/scheduler_ownership.pyindex cef9f5ae860..fc92a52531c 100644--- a/cron/scheduler_ownership.py+++ b/cron/scheduler_ownership.py@@ -27,6 +27,9 @@ from hermes_constants import get_hermes_home, hermes_home_key# Home key -> home path for every profile this process ticks, republished each ticker cycle so a# profile created or tombstoned mid-run is reflected without a restart._ticked_homes: dict[str, Path] = {}+# Home key -> resolved cron store, taken while the home exists: a departed profile's home may be+# gone by the time its store state is dropped, and a symlinked cron/ resolves elsewhere.+_ticked_stores: dict[str, str] = {}_ticked_lock = threading.Lock()@@ -37,21 +40,26 @@ def register_ticked_homes(homes) -> None:live until process exit, so serving (or churning) many profiles leaks one ThreadPoolExecutorand its worker threads per home ever ticked."""- resolved = {}+ from cron.store_health import forget_homes, store_key++ resolved, stores = {}, {}for home in homes:path = Path(home)- resolved[hermes_home_key(path)] = path+ key = hermes_home_key(path)+ resolved[key] = path+ stores[key] = store_key(path)with _ticked_lock:departed = set(_ticked_homes) - set(resolved)+ departed_stores = [_ticked_stores[k] for k in departed if k in _ticked_stores]_ticked_homes.clear()_ticked_homes.update(resolved)+ _ticked_stores.clear()+ _ticked_stores.update(stores)if departed:# Late import: cron.scheduler imports this module.from cron.scheduler import discard_parallel_pools- from cron.store_health import forget_homes-discard_parallel_pools(departed)- forget_homes(departed)+ forget_homes(departed_stores)def ticked_homes() -> dict:diff --git a/cron/store_health.py b/cron/store_health.pyindex cd94acdd4eb..cfc0efdd2ec 100644--- a/cron/store_health.py+++ b/cron/store_health.py@@ -162,10 +162,16 @@ def outage_covers(cron_dir: Path, due_at: float, grace: float) -> bool:for r in (_degraded.get(_key(cron_dir)), _recovered.get(_key(cron_dir))))-def forget_homes(home_keys) -> None:- """Drop the state of stores whose profile home (``hermes_home_key``) this process no longer- ticks, so a profile that left this gateway cannot keep the host-wide gauges at writable=0."""- stores = {_key(Path(home) / "cron") for home in home_keys} # a symlinked cron/ resolves elsewhere+def store_key(home) -> str:+ """Record key of a profile home's cron store (a symlinked cron/ resolves elsewhere)."""+ return _key(Path(home) / "cron")+++def forget_homes(stores) -> None:+ """Drop the state of ``stores`` (``store_key`` values, resolved while each home existed) whose+ profile this process no longer ticks, so a profile that left this gateway cannot keep the+ host-wide gauges at writable=0."""+ stores = set(stores)with _lock:for records in (_degraded, _recovered):for store in [s for s in records if s in stores]:diff --git a/gateway/cron_store_notices.py b/gateway/cron_store_notices.pyindex c9953bc4a3d..ece241dc468 100644--- a/gateway/cron_store_notices.py+++ b/gateway/cron_store_notices.py@@ -91,17 +91,17 @@ async def send_cron_store_notice(runner, event: str, record) -> None:"""Post one ``unwritable``/``recovered`` notice to the home channels of the store's profile."""from gateway.run import _async_profile_runtime_scopefrom gateway.warning_notifications import present_notification- from hermes_constants import get_routing_process_hermes_home, hermes_home_key+ from cron.store_health import store_key+ from hermes_constants import get_routing_process_hermes_homefields = record.notice_fields()key = "gateway.cron_store.unwritable" if event == "unwritable" else "gateway.cron_store.recovered"- store_key = hermes_home_key(record.store)served_homes = runner._served_profile_homes or {}logger.info("Broadcasting cron store %s notice for %s", event, record.store)for profile, platform, _cfg, home, transport in list(runner._served_home_channel_transports()):served_home = served_homes.get(profile) if profile is not None else None# record.store is resolved: a cron/ symlinked to another disk has a foreign parent.- if hermes_home_key(Path(served_home or get_routing_process_hermes_home()) / "cron") != store_key:+ if store_key(served_home or get_routing_process_hermes_home()) != record.store:continue# The opt-out is the owning profile's; the launch profile needs no extra scope.scope = _async_profile_runtime_scope(Path(served_home)) if served_home else contextlib.nullcontext()diff --git a/tests/cron/test_due_scan_save_failure.py b/tests/cron/test_due_scan_save_failure.pyindex af53da906ec..f797f2eb5a1 100644--- a/tests/cron/test_due_scan_save_failure.py+++ b/tests/cron/test_due_scan_save_failure.py@@ -18,6 +18,7 @@ import jsonimport asyncioimport loggingimport os+import shutilimport sqlite3from datetime import timedelta@@ -453,3 +454,10 @@ def test_unwritable_store_is_shown_in_cron_status_and_announced_once(cron_store,assert f"fix permissions on {cron_dir}" in sent[0][1]assert "writable again; 1 skipped run(s), catching up once per job" in sent[1][1]assert any(r.levelname == "WARNING" and "unwritable notice for" in r.getMessage() for r in caplog.records)+ from cron import scheduler_ownership # the profile leaves this gateway, its home already deleted+ monkeypatch.setattr(scheduler_ownership, "_ticked_homes", {})+ scheduler_ownership.register_ticked_homes([profile])+ assert store_health.degraded_record(cron_dir) is not None+ shutil.rmtree(profile) # removes the cron/ symlink, never the store it points at+ scheduler_ownership.register_ticked_homes([])+ assert store_health.degraded_record(cron_dir) is None