The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cron): key the dead-owner reap throttle by profile home
Yagna Vudathudf753d4fc367
diff --git a/cron/scheduler.py b/cron/scheduler.pyindex a33532dbdb1..7cb3c2e43d3 100644--- a/cron/scheduler.py+++ b/cron/scheduler.py@@ -29,7 +29,7 @@ except ImportError:except ImportError:msvcrt = Nonefrom pathlib import Path-from typing import Any, Callable, List, Optional, Protocol+from typing import Any, Callable, Dict, List, Optional, Protocol# Must precede repo-level imports: standalone invocations (e.g. module reload after# `hermes update`) otherwise fail with ModuleNotFoundError for hermes_time et al.@@ -3621,11 +3621,14 @@ def create_job_with_scheduler_registration(**kwargs) -> dict:# Dead-owner reap is throttled (opens the executions ledger). Tests may reset-# _last_dead_owner_reap_at to None to force a reap next tick.+# _last_dead_owner_reap_at to {} to force a reap next tick.# Dead-owner claim reclaim throttle (#86721): recover_interrupted_executions opens the executions ledger, so# the per-tick reap is rate-limited rather than run on every idle 60s cycle.+# The throttle is keyed by profile home: under multiplex_profiles the ticker+# ticks every profile each cycle, and a process-global slot would let the+# first profile starve all the others._DEAD_OWNER_REAP_INTERVAL_SECONDS = 300.0-_last_dead_owner_reap_at: Optional[float] = None+_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@@ -3743,6 +3746,14 @@ def _release_tick_lock(lock_fd) -> None:lock_fd.close()+def _reap_throttle_key() -> str:+ """Profile scope for the dead-owner reap throttle (multiplex-safe)."""+ try:+ return str(_get_hermes_home().resolve())+ except Exception:+ return "default"++def _maybe_reap_dead_owners() -> None:"""Dead-owner reclaim: a run that died mid-flight would leave its row 'claimed' forever. Onlyrows whose owner process is proved gone are touched (_owner_is_live). Throttled."""@@ -3752,14 +3763,15 @@ def _maybe_reap_dead_owners() -> None:# the long-lived gateway ticker kept running — blocking every future run of that job. Reap provably-dead# owners periodically so stale claims auto-clear without a gateway restart. Throttled so idle 60s ticks# don't pay a ledger connection every cycle (#33612).- global _last_dead_owner_reap_at+ _reap_key = _reap_throttle_key()_reap_now = time.monotonic()+ _last_reap = _last_dead_owner_reap_at.get(_reap_key)if (- _last_dead_owner_reap_at is not None- and _reap_now - _last_dead_owner_reap_at < _DEAD_OWNER_REAP_INTERVAL_SECONDS+ _last_reap is not None+ and _reap_now - _last_reap < _DEAD_OWNER_REAP_INTERVAL_SECONDS):return- _last_dead_owner_reap_at = _reap_now+ _last_dead_owner_reap_at[_reap_key] = _reap_nowtry:from cron.executions import recover_interrupted_executionsdiff --git a/tests/cron/test_dead_owner_claim_reclaim.py b/tests/cron/test_dead_owner_claim_reclaim.pyindex 7559ac23b08..4b07e0a670b 100644--- a/tests/cron/test_dead_owner_claim_reclaim.py+++ b/tests/cron/test_dead_owner_claim_reclaim.py@@ -42,7 +42,7 @@ def executions(monkeypatch, tmp_path):@pytest.fixture(autouse=True)def _fresh_reap_window(monkeypatch):"""Each test starts with the reap throttle open."""- monkeypatch.setattr(scheduler_mod, "_last_dead_owner_reap_at", None)+ monkeypatch.setattr(scheduler_mod, "_last_dead_owner_reap_at", {})def _dead_pid() -> int:@@ -129,7 +129,8 @@ class TestTickReapsDeadOwnerClaims:monkeypatch.setattr(scheduler_mod,"_last_dead_owner_reap_at",- time.monotonic() - scheduler_mod._DEAD_OWNER_REAP_INTERVAL_SECONDS - 1,+ {scheduler_mod._reap_throttle_key():+ time.monotonic() - scheduler_mod._DEAD_OWNER_REAP_INTERVAL_SECONDS - 1},)_run_tick()assert len(calls) == 2, "an expired throttle window must reap again"@@ -203,3 +204,28 @@ class TestOneShotCliRunIsSynchronous:{"id": "job-x", "name": "job-x"}, session_id="sess-1")assert result is None+++def test_reap_throttle_is_profile_scoped(monkeypatch, tmp_path):+ """In multiplex mode each profile's tick must get its own reap scan: the+ first profile in a cycle must not consume a process-global throttle slot+ that starves every other profile."""+ import cron.executions as executions_mod++ calls = []+ monkeypatch.setattr(+ executions_mod, "recover_interrupted_executions", lambda: calls.append(1) or 0+ )+ home_a = tmp_path / "profile-a"+ home_b = tmp_path / "profile-b"+ home_a.mkdir()+ home_b.mkdir()+ monkeypatch.setattr(scheduler_mod, "_last_dead_owner_reap_at", {})+ monkeypatch.setattr(scheduler_mod, "_get_hermes_home", lambda: home_a)+ scheduler_mod._maybe_reap_dead_owners()+ monkeypatch.setattr(scheduler_mod, "_get_hermes_home", lambda: home_b)+ scheduler_mod._maybe_reap_dead_owners()+ assert len(calls) == 2, (+ "each profile must get its own dead-owner scan per cycle, "+ f"got {len(calls)} scan(s) for 2 profiles"+ )