The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(cron): the per-repo worktree prune claim lives in its own sibling
Tekniumc1b934a9d053
diff --git a/cron/scheduler.py b/cron/scheduler.pyindex 6134e3bb0af..4ced0e7e1c3 100644--- a/cron/scheduler.py+++ b/cron/scheduler.py@@ -4038,7 +4038,6 @@ _last_dead_owner_reap_at: dict[str, float] = {}# Worktree prune throttle: the cron tick is the only reliably periodic process on gateway boxes._WORKTREE_MAINTENANCE_INTERVAL_SECONDS = 6 * 3600.0_last_worktree_maintenance_at: Optional[dict] = None # profile home -> monotonic stamp-_last_worktree_prune_at: dict[str, float] = {} # resolved repo root -> monotonic stamp_worktree_maintenance_lock = threading.Lock()@@ -4078,31 +4077,11 @@ def _worktree_maintenance_repos() -> list[str]:return [r for r in sorted(repos) if (Path(r) / ".worktrees").is_dir()]-def _claim_due_worktree_repos(repos: list[str]) -> list[str]:- """The subset of *repos* not pruned within the interval by ANY profile this process serves,- stamped as claimed. Served profiles mostly resolve the same repos (the hermes checkout at- least), and every prune pass ends in a footprint probe of the whole ``.worktrees/``, so a- per-profile pass multiplied that disk walk by the profile count at the same second."""- now = time.monotonic()- due = []- with _worktree_maintenance_lock:- for repo in repos:- key = os.path.realpath(repo)- last = _last_worktree_prune_at.get(key)- if last is None or now - last >= _WORKTREE_MAINTENANCE_INTERVAL_SECONDS:- _last_worktree_prune_at[key] = now- due.append(repo)- return due--def _maybe_run_worktree_maintenance() -> None:"""Throttled worktree prune from the cron tick, on a daemon thread so the tick never waits ongit. Same pruner as ``hermes -w`` startup (idle dirty/unpushed trees archived before removal,- live trees untouched). Repo discovery is throttled per profile home (each profile has its own- registry and jobs); the prune itself runs once per repo per interval across every served- profile, under the config of the profile that claims it first. The thread runs in- a copy of the caller's context so the profile-home override travels with it. Errors never- propagate: GC is hygiene, not scheduling."""+ live trees untouched). Discovery is throttled per profile home, the prune once per repo across+ served profiles. Runs in a copy of the caller's context (profile-home override); never raises."""global _last_worktree_maintenance_atimport contextvars@@ -4119,7 +4098,8 @@ def _maybe_run_worktree_maintenance() -> None:def _run() -> None:try:- repos = _claim_due_worktree_repos(_worktree_maintenance_repos())+ from cron.scheduler_worktree_maintenance import claim_due_worktree_repos as claim+ repos = claim(_worktree_maintenance_repos(), _WORKTREE_MAINTENANCE_INTERVAL_SECONDS)if not repos:returnfrom hermes_cli.worktree_ops import _prune_stale_worktreesdiff --git a/cron/scheduler_worktree_maintenance.py b/cron/scheduler_worktree_maintenance.pynew file mode 100644index 00000000000..9879fa81bac--- /dev/null+++ b/cron/scheduler_worktree_maintenance.py@@ -0,0 +1,27 @@+"""Per-repo claim for the cron worktree prune.++Served profiles mostly resolve the same repos (the hermes checkout at least), and every prune+pass ends in a footprint probe of the whole ``.worktrees/``, so a per-profile pass multiplied that+disk walk by the profile count at the same second. One process claims each repo once per interval.+"""++import os+import threading+import time++_last_worktree_prune_at: dict[str, float] = {} # resolved repo root -> monotonic stamp+_lock = threading.Lock()+++def claim_due_worktree_repos(repos: list[str], interval_s: float) -> list[str]:+ """The subset of *repos* no served profile pruned within *interval_s*, stamped as claimed."""+ now = time.monotonic()+ due = []+ with _lock:+ for repo in repos:+ key = os.path.realpath(repo)+ last = _last_worktree_prune_at.get(key)+ if last is None or now - last >= interval_s:+ _last_worktree_prune_at[key] = now+ due.append(repo)+ return duediff --git a/tests/cron/test_worktree_maintenance_once_per_repo.py b/tests/cron/test_worktree_maintenance_once_per_repo.pyindex 80f09fb7753..96c4804005b 100644--- a/tests/cron/test_worktree_maintenance_once_per_repo.py+++ b/tests/cron/test_worktree_maintenance_once_per_repo.py@@ -43,7 +43,8 @@ def test_profiles_sharing_a_repo_prune_it_once_per_interval(tmp_path, monkeypatcpruned = []monkeypatch.setattr(worktree_ops, "_prune_stale_worktrees", lambda repo: pruned.append(repo))monkeypatch.setattr(sched, "_last_worktree_maintenance_at", None)- monkeypatch.setattr(sched, "_last_worktree_prune_at", {}, raising=False)+ from cron import scheduler_worktree_maintenance as claims+ monkeypatch.setattr(claims, "_last_worktree_prune_at", {})class _InlineThread:def __init__(self, target, args=(), **_kw):