Skip to content

The agent that grows with you

fix(desktop): gate the fail-open cron paths per tick instead of standing down for good

unsupportedpastels284f849d562e
diff --git a/hermes_cli/web_server.py b/hermes_cli/web_server.py
index abcc72882ce..cf32178540f 100644
--- a/hermes_cli/web_server.py
+++ b/hermes_cli/web_server.py
@@ -135,24 +135,43 @@ def _start_desktop_cron_ticker(stop_event: "threading.Event", interval: int = 60
# A live gateway on THIS backend's HERMES_HOME owns cron delivery with live platform
# adapters (#52202). The per-tick ``profile_gate`` already stands down while it runs and
- # resumes once it stops (Desktop's own gateway stop, a crash), so only the fail-open paths
- # (profile enumeration failure, empty served set, external provider) bail out here: their
- # ungated single-store ticker would race the gateway's tick-lock, and when the desktop wins,
- # delivery has no live adapter and the cold send hangs until script_timeout.
+ # resumes once it stops (Desktop's own gateway stop, a crash). The fail-open paths (profile
+ # enumeration failure, empty served set, external provider) have no profile gate: ungated,
+ # their single-store ticker would race the gateway's tick-lock, and when the desktop wins,
+ # delivery has no live adapter and the cold send hangs until script_timeout. They re-check
+ # the same ownership probe instead of taking it once at startup, so they also take over
+ # once that gateway stops (#126822).
if "profile_gate" not in start_kwargs:
- try:
- from hermes_constants import get_hermes_home
- from hermes_cli.profiles import _check_gateway_running
+ from hermes_constants import get_hermes_home
+ from hermes_cli.profiles import _check_gateway_running
- if _check_gateway_running(Path(get_hermes_home())):
- _log.info(
- "Desktop cron scheduler not started: live gateway owns cron on this "
- "HERMES_HOME; the gateway ticks with live adapters"
- )
- return
- except Exception:
- # Liveness probe failed: start the ticker rather than silently stand down.
- _log.warning("Desktop cron: gateway-ownership probe failed; starting the ticker", exc_info=True)
+ own_home = Path(get_hermes_home())
+ probe_failed: list = []
+
+ def _gateway_owns_cron() -> bool:
+ try:
+ return _check_gateway_running(own_home)
+ except Exception:
+ # Start the ticker rather than silently stand down; warn once, not every tick.
+ if not probe_failed:
+ probe_failed.append(True)
+ _log.warning("Desktop cron: gateway-ownership probe failed; starting the ticker", exc_info=True)
+ return False
+
+ if isinstance(provider, InProcessCronScheduler):
+ # Per-tick gate: a skipped tick leaves due jobs in place for the next allowed one.
+ start_kwargs["can_dispatch"] = lambda: not _gateway_owns_cron()
+ elif _gateway_owns_cron():
+ # External providers take no per-tick gate: defer their start until the gateway is gone.
+ _log.info(
+ "Desktop cron scheduler waiting: live gateway owns cron on this HERMES_HOME; "
+ "the gateway ticks with live adapters (re-probing every %ds)", interval,
+ )
+ while _gateway_owns_cron():
+ if stop_event.wait(interval):
+ return
+ if stop_event.is_set():
+ return # shutdown landed during the final probe
_log.info("Desktop cron scheduler started (provider=%s, interval=%ds)", provider.name, interval)
provider.start(stop_event, **start_kwargs)
diff --git a/tests/hermes_cli/test_desktop_cron_ticker_gateway_standdown.py b/tests/hermes_cli/test_desktop_cron_ticker_gateway_standdown.py
index 1c9f266a22b..9008db35807 100644
--- a/tests/hermes_cli/test_desktop_cron_ticker_gateway_standdown.py
+++ b/tests/hermes_cli/test_desktop_cron_ticker_gateway_standdown.py
@@ -1,10 +1,9 @@
-"""Desktop cron ticker stands down when a live gateway owns cron on the same HERMES_HOME (#52202).
+"""Desktop cron ticker yields to a live gateway that owns cron on the same HERMES_HOME (#52202).
-The ticker's per-tick ``profile_gate`` only arms in the multiplex path; the fail-open
-paths (profile enumeration failure, empty served set, external provider) start an
-ungated single-store ticker that races a live gateway on the same HERMES_HOME. Those
-paths bail out when a gateway is live on this home; the gated multiplex ticker still
-starts, so it resumes once that gateway stops.
+The multiplex path gates each tick through ``profile_gate``. The built-in fail-open paths
+(profile enumeration failure, empty served set) gate each tick through ``can_dispatch`` on
+the same ownership probe, and an external provider defers its start until the gateway is
+gone. Every path takes over once that gateway stops (#126822).
"""
from __future__ import annotations
@@ -43,17 +42,30 @@ def _set_gateway_running(monkeypatch, running: bool) -> None:
def test_ticker_stands_down_when_gateway_owns_cron(ticker_env, monkeypatch, caplog):
+ """An external provider has no per-tick gate: its start waits out the live gateway, and
+ happens once that gateway is gone instead of never (#126822)."""
from hermes_cli import web_server
home, started = ticker_env
_set_gateway_running(monkeypatch, True)
+ stopped = threading.Event()
+ stopped.set()
with caplog.at_level(logging.INFO, logger="hermes_cli.web_server"):
- web_server._start_desktop_cron_ticker(threading.Event(), interval=0)
+ web_server._start_desktop_cron_ticker(stopped, interval=0)
- assert started == {} # provider.start never called
+ assert started == {} # backend shut down while the gateway still owned cron
assert "live gateway owns cron" in caplog.text
+ import hermes_cli.profiles as profiles
+
+ probes = iter([True, True, False])
+ monkeypatch.setattr(profiles, "_check_gateway_running", lambda _home: next(probes))
+
+ web_server._start_desktop_cron_ticker(threading.Event(), interval=0)
+
+ assert "kwargs" in started # the gateway stopped: Desktop takes over
+
def test_ticker_starts_when_no_gateway(ticker_env, monkeypatch):
from hermes_cli import web_server
@@ -111,3 +123,34 @@ def test_gated_ticker_resumes_after_the_gateway_stops(ticker_env, monkeypatch):
assert gate("default", home) is False # the live gateway ticks with its adapters
gateway["running"] = False
assert gate("default", home) is True # it stopped: Desktop cron fires again
+
+
+def test_fail_open_ticker_gates_every_tick_on_the_gateway(ticker_env, monkeypatch):
+ """With no profile gate (enumeration failed), the single-store ticker still starts and stands
+ down per tick only while a gateway owns this home, including one that comes back."""
+ import cron.scheduler_provider as sp
+ import hermes_cli.profiles as profiles
+ from hermes_cli import web_server
+
+ home, started = ticker_env
+
+ class _InProcess(sp.InProcessCronScheduler):
+ def start(self, stop_event, **kwargs):
+ started["kwargs"] = kwargs
+
+ def _enumeration_fails(**_kw):
+ raise RuntimeError("enumeration failed")
+
+ gateway = {"running": True}
+ monkeypatch.setattr(sp, "resolve_cron_scheduler", lambda: _InProcess())
+ monkeypatch.setattr(profiles, "profiles_to_serve", _enumeration_fails)
+ monkeypatch.setattr(profiles, "_check_gateway_running", lambda _home: gateway["running"])
+
+ web_server._start_desktop_cron_ticker(threading.Event(), interval=0)
+
+ can_dispatch = started["kwargs"]["can_dispatch"]
+ assert can_dispatch() is False # the live gateway ticks with its adapters
+ gateway["running"] = False
+ assert can_dispatch() is True # it stopped: Desktop cron fires again
+ gateway["running"] = True
+ assert can_dispatch() is False # a returning gateway is not raced
diff --git a/tests/hermes_cli/test_desktop_cron_ticker_profiles.py b/tests/hermes_cli/test_desktop_cron_ticker_profiles.py
index f6df8a56934..10a3bc63479 100644
--- a/tests/hermes_cli/test_desktop_cron_ticker_profiles.py
+++ b/tests/hermes_cli/test_desktop_cron_ticker_profiles.py
@@ -114,7 +114,9 @@ def test_enumeration_failure_fails_open(monkeypatch, _providers):
ws._start_desktop_cron_ticker(threading.Event(), interval=11)
- assert builtin.start_kwargs == {"interval": 11}
+ # Single-store fallback (no profile_homes); its gateway gate is covered by the stand-down tests.
+ assert builtin.start_kwargs["interval"] == 11
+ assert "profile_homes" not in builtin.start_kwargs
def test_external_provider_never_gets_profile_homes(monkeypatch, tmp_path):