Skip to content

The agent that grows with you

fix(cron): compare full gateway code identities

fangliquanflq153b5f0145e2
diff --git a/cron/scheduler.py b/cron/scheduler.py
index 4b9ef051aae..d159d8a1a1b 100644
--- a/cron/scheduler.py
+++ b/cron/scheduler.py
@@ -156,6 +156,16 @@ def _detect_gateway_code_skew() -> tuple[str, str] | None:
return None
+def _current_gateway_code_sha() -> str | None:
+ """Full revision currently on disk; kept separate from display-shortened skew labels."""
+ try:
+ from gateway.code_skew import current_code_sha
+
+ return current_code_sha()
+ except Exception:
+ return None
+
+
class CronTickYielded(RuntimeError):
"""A stale-code ticker yielded this tick to a fresh gateway.
@@ -188,6 +198,9 @@ def _should_yield_tick_to_fresh_gateway() -> tuple[str, str] | None:
skew = _detect_gateway_code_skew()
if skew is None:
return None
+ disk_sha = _current_gateway_code_sha()
+ if disk_sha is None:
+ return None
try:
from gateway import status as _gateway_status
except Exception:
@@ -204,7 +217,7 @@ def _should_yield_tick_to_fresh_gateway() -> tuple[str, str] | 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]
+ or holder_status.get("code_sha") != disk_sha
):
return None
except Exception:
diff --git a/gateway/code_skew.py b/gateway/code_skew.py
index a5a200142fe..943829663b5 100644
--- a/gateway/code_skew.py
+++ b/gateway/code_skew.py
@@ -40,6 +40,15 @@ def _short(fingerprint: str) -> str:
return sha[:10] if sha and sha != "unresolved" and len(sha) > 10 else (sha or fingerprint)
+def current_code_sha() -> str | None:
+ """Full SHA for the checkout currently on disk, or None when unresolved."""
+ fingerprint = _fingerprint()
+ if fingerprint is None:
+ return None
+ sha = fingerprint.rsplit(":", 1)[-1]
+ return sha if sha and sha != "unresolved" else None
+
+
def detect_code_skew() -> tuple[str, str] | None:
"""``(boot_rev, disk_rev)`` short labels if the checkout drifted since boot, else ``None``."""
current = _fingerprint() if _boot_fingerprint is not None else None
diff --git a/tests/cron/test_cron_tick_stale_yield.py b/tests/cron/test_cron_tick_stale_yield.py
index d3dd7b105ed..37e1bdd5562 100644
--- a/tests/cron/test_cron_tick_stale_yield.py
+++ b/tests/cron/test_cron_tick_stale_yield.py
@@ -29,7 +29,9 @@ import pytest
import cron.scheduler as scheduler_mod
-SKEW = ("boot0123abcd", "disk4567efgh")
+BOOT_SHA = "a" * 40
+DISK_SHA = "b" * 40
+SKEW = (BOOT_SHA[:10], DISK_SHA[:10])
def _wait_until(predicate, timeout=10.0, interval=0.005):
@@ -45,12 +47,13 @@ def _wait_until(predicate, timeout=10.0, interval=0.005):
def _gate_mocks(
monkeypatch, *, owns: bool, active: bool, skew=SKEW,
- holder_sha: str | None = SKEW[1], holder_status_stale: bool = False,
+ holder_sha: str | None = DISK_SHA, holder_status_stale: bool = False,
):
"""Point the yield gate's process and holder-status probes at fixed answers."""
from gateway import status as gateway_status
monkeypatch.setattr(scheduler_mod, "_detect_gateway_code_skew", lambda: skew)
+ monkeypatch.setattr(scheduler_mod, "_current_gateway_code_sha", lambda: DISK_SHA)
monkeypatch.setattr(gateway_status, "owns_gateway_runtime_lock", lambda: owns)
monkeypatch.setattr(
gateway_status, "is_gateway_runtime_lock_active", lambda lock_path=None: active
@@ -107,7 +110,7 @@ class TestTickYieldGate:
@pytest.mark.parametrize(
("holder_sha", "holder_status_stale"),
- [(SKEW[0], False), (SKEW[1], True)],
+ [(BOOT_SHA, False), (DISK_SHA, True)],
ids=["holder-is-stale-code", "holder-heartbeat-is-stale"],
)
def test_skew_plus_unfit_lock_holder_proceeds(