The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cron): verify fresh gateway before yielding ticks
fangliquanflqbc04c35f6a7b
diff --git a/cron/scheduler.py b/cron/scheduler.pyindex fd4351cbdfa..4b9ef051aae 100644--- a/cron/scheduler.py+++ b/cron/scheduler.py@@ -160,10 +160,10 @@ class CronTickYielded(RuntimeError):"""A stale-code ticker yielded this tick to a fresh gateway.Raised by ``tick()`` BEFORE the tick lock when boot fingerprint ≠ disk, this process does NOT- own the runtime lock and a fresh process holds it — the stale process must stay out of the- dispatch race (contention would starve the fresh ticker). Skew ``None`` never yields (fail- open). Raised, not returned, so ``record_ticker_error`` sees it and ``hermes cron status``- isn't green.+ own the runtime lock and its live holder reports the disk revision — the stale process must+ stay out of the dispatch race (contention would starve the fresh ticker). Skew ``None`` never+ yields (fail open). Raised, not returned, so ``record_ticker_error`` sees it and ``hermes cron+ status`` isn't green."""def __init__(self, boot_rev: str, disk_rev: str) -> None:@@ -182,8 +182,9 @@ _last_yield_log: dict[str, object] = {}def _should_yield_tick_to_fresh_gateway() -> tuple[str, str] | None:"""``(boot_rev, disk_rev)`` when this tick must yield to a fresher gateway, else None. Yields- only when ALL hold: code skew, we don't own the runtime lock, another process holds it. Every- probe failure returns None — yielding is a certainty claim, never a guess."""+ only when ALL hold: code skew, we don't own the runtime lock, another process holds it, and+ that holder's live status reports the disk revision. Every probe failure returns None —+ yielding is a certainty claim, never a guess."""skew = _detect_gateway_code_skew()if skew is None:return None@@ -196,6 +197,16 @@ def _should_yield_tick_to_fresh_gateway() -> tuple[str, str] | None:return Noneif not _gateway_status.is_gateway_runtime_lock_active():return None+ holder_pid = _gateway_status.get_running_pid(cleanup_stale=False)+ holder_status = _gateway_status.read_runtime_status()+ if (+ holder_pid is None+ or not isinstance(holder_status, dict)+ or holder_status.get("pid") != holder_pid+ or _gateway_status.runtime_status_is_stale(holder_status)+ or holder_status.get("code_sha") != skew[1]+ ):+ return Noneexcept Exception:return Nonereturn skewdiff --git a/tests/cron/test_cron_tick_stale_yield.py b/tests/cron/test_cron_tick_stale_yield.pyindex fe5187d3fbc..d3dd7b105ed 100644--- a/tests/cron/test_cron_tick_stale_yield.py+++ b/tests/cron/test_cron_tick_stale_yield.py@@ -43,8 +43,11 @@ def _wait_until(predicate, timeout=10.0, interval=0.005):return predicate()-def _gate_mocks(monkeypatch, *, owns: bool, active: bool, skew=SKEW):- """Point the yield gate's three probes at fixed answers."""+def _gate_mocks(+ monkeypatch, *, owns: bool, active: bool, skew=SKEW,+ holder_sha: str | None = SKEW[1], holder_status_stale: bool = False,+):+ """Point the yield gate's process and holder-status probes at fixed answers."""from gateway import status as gateway_statusmonkeypatch.setattr(scheduler_mod, "_detect_gateway_code_skew", lambda: skew)@@ -52,6 +55,15 @@ def _gate_mocks(monkeypatch, *, owns: bool, active: bool, skew=SKEW):monkeypatch.setattr(gateway_status, "is_gateway_runtime_lock_active", lambda lock_path=None: active)+ monkeypatch.setattr(gateway_status, "get_running_pid", lambda **_kwargs: 4321)+ monkeypatch.setattr(+ gateway_status,+ "read_runtime_status",+ lambda: {"pid": 4321, "code_sha": holder_sha},+ )+ monkeypatch.setattr(+ gateway_status, "runtime_status_is_stale", lambda _record: holder_status_stale+ )class TestTickYieldGate:@@ -93,6 +105,24 @@ class TestTickYieldGate:_gate_mocks(monkeypatch, owns=False, active=False)assert scheduler_mod.tick(verbose=False) == 0+ @pytest.mark.parametrize(+ ("holder_sha", "holder_status_stale"),+ [(SKEW[0], False), (SKEW[1], True)],+ ids=["holder-is-stale-code", "holder-heartbeat-is-stale"],+ )+ def test_skew_plus_unfit_lock_holder_proceeds(+ self, monkeypatch, tmp_path, holder_sha, holder_status_stale+ ):+ """A lock alone never proves another ticker can dispatch this occurrence."""+ _gate_mocks(+ monkeypatch,+ owns=False,+ active=True,+ holder_sha=holder_sha,+ holder_status_stale=holder_status_stale,+ )+ assert scheduler_mod.tick(verbose=False) == 0+def test_no_fingerprint_proceeds(self, monkeypatch, tmp_path):"""(d) skew is None (non-git install, no boot fingerprint, probefailure) → always proceed."""