Skip to content

The agent that grows with you

fix(change-watcher): probe profile liveness once per profiles/ mtime + TTL, not on every 2 s pairing tick

teknium11eb5cd61248d
diff --git a/tests/tui_gateway/test_change_watcher.py b/tests/tui_gateway/test_change_watcher.py
index a651079bb19..b1f2b3c6b2c 100644
--- a/tests/tui_gateway/test_change_watcher.py
+++ b/tests/tui_gateway/test_change_watcher.py
@@ -26,6 +26,7 @@ def watcher_home(tmp_path, monkeypatch):
monkeypatch.setattr(server, "_change_checked_at", {})
monkeypatch.setattr(server, "_change_broadcast_at", {})
monkeypatch.setattr(server, "_bot_relay_outbox_seen", 0)
+ monkeypatch.setattr(server, "_pairing_roots_cache", None, raising=False)
events = []
monkeypatch.setattr(
@@ -125,6 +126,38 @@ def test_pairing_signal_follows_a_profile_store(watcher_home):
assert ("pairing.changed", {}) in events
+def test_pairing_probe_reuses_live_profile_roots_until_the_profile_set_moves(watcher_home, monkeypatch):
+ """The per-profile liveness probe (~14 stats each) runs once per profiles/ mtime + TTL, not
+ on every 2 s tick (#114041 §2); ledger writes under known roots are still seen each tick,
+ and a newly created profile is picked up because creating it bumps the parent's mtime."""
+ import hermes_constants
+
+ home, events = watcher_home
+ live_calls = []
+ real_live = hermes_constants.named_profile_is_live
+ monkeypatch.setattr(hermes_constants, "named_profile_is_live",
+ lambda p: live_calls.append(p.name) or real_live(p))
+
+ def _profile(name):
+ (home / "profiles" / name / "platforms" / "pairing").mkdir(parents=True)
+ (home / "profiles" / name / "config.yaml").write_text("{}\n", encoding="utf-8")
+
+ _profile("work")
+ server._broadcast_watched_changes(now=0.0)
+ (home / "profiles" / "work" / "platforms" / "pairing" / "telegram-pending.json").write_text("{}", encoding="utf-8")
+ server._broadcast_watched_changes(now=10.0)
+ assert events == [("pairing.changed", {})]
+ assert live_calls == ["work"] # second tick reused the cached roots, still saw the ledger
+
+ _profile("play")
+ os.utime(home / "profiles", ns=(0, 10**18)) # deterministic parent-mtime bump
+ server._broadcast_watched_changes(now=20.0)
+ (home / "profiles" / "play" / "platforms" / "pairing" / "discord-approved.json").write_text("{}", encoding="utf-8")
+ server._broadcast_watched_changes(now=30.0)
+ assert events == [("pairing.changed", {})] * 2
+ assert sorted(live_calls) == ["play", "work", "work"]
+
+
def test_rate_limit_churn_does_not_broadcast_pairing_changed(watcher_home):
"""_rate_limits.json moves on every unauthorized DM, including ones that
produce no new row — signalling on it would refetch for nothing."""
diff --git a/tui_gateway/change_watcher.py b/tui_gateway/change_watcher.py
index 84f50f4af75..071ee77c207 100644
--- a/tui_gateway/change_watcher.py
+++ b/tui_gateway/change_watcher.py
@@ -133,16 +133,8 @@ def _pairing_sig():
"""Newest mtime across every profile's pairing ledgers (legacy ``pairing/`` and
``platforms/pairing/``): the gateway process writes pending codes, so the files are the only
shared signal (a pairing request moves nothing in gateway_state.json)."""
- home = _watcher_home()
- roots = [home / "pairing", home / "platforms" / "pairing"]
- from hermes_constants import named_profile_is_live
-
- with contextlib.suppress(OSError):
- for profile_dir in (home / "profiles").iterdir():
- if named_profile_is_live(profile_dir):
- roots += [profile_dir / "pairing", profile_dir / "platforms" / "pairing"]
entries = []
- for root in roots:
+ for root in _pairing_roots(_watcher_home()):
with contextlib.suppress(OSError):
# Only the ledgers: _rate_limits.json moves on every unauthorized DM.
entries += [
@@ -150,6 +142,31 @@ def _pairing_sig():
return _newest_mtime_ns(entries)
+# Live-profile pairing roots, cached on (home, ``profiles/`` dir mtime) with a TTL. The liveness
+# probe costs ~14 stats per profile; on the 2 s tick that was the watcher's share of the idle
+# profile-tree burn (#114041 §2/§3). The profile SET only moves when a dir is added or removed —
+# which bumps the parent's mtime — while a marker/tombstone landing inside one is caught by the TTL.
+_PAIRING_ROOTS_TTL_S = 30.0
+_pairing_roots_cache: tuple[Path, int | None, float, list] | None = None
+
+
+def _pairing_roots(home: Path) -> list:
+ global _pairing_roots_cache
+ profiles_dir = home / "profiles"
+ dir_mtime, now = _watcher_mtime_ns(profiles_dir), time.monotonic()
+ cached = _pairing_roots_cache
+ if cached is not None and cached[0] == home and cached[1] == dir_mtime and now - cached[2] < _PAIRING_ROOTS_TTL_S:
+ return cached[3]
+ from hermes_constants import named_profile_is_live
+ roots = [home / "pairing", home / "platforms" / "pairing"]
+ with contextlib.suppress(OSError):
+ for profile_dir in profiles_dir.iterdir():
+ if named_profile_is_live(profile_dir):
+ roots += [profile_dir / "pairing", profile_dir / "platforms" / "pairing"]
+ _pairing_roots_cache = (home, dir_mtime, now, roots)
+ return roots
+
+
# Newest outbox-envelope mtime EVER seen (monotone): a drain empties the outbox,
# and falling back to None would fire a spurious pending event after every drain.
_bot_relay_outbox_seen = 0