Skip to content

The agent that grows with you

fix(desktop): a gateway live at backend start no longer stops Desktop cron for good

AhmetArif0b94716f2dbf8
diff --git a/hermes_cli/web_server.py b/hermes_cli/web_server.py
index a8320d45cb9..abcc72882ce 100644
--- a/hermes_cli/web_server.py
+++ b/hermes_cli/web_server.py
@@ -95,26 +95,6 @@ def _start_desktop_cron_ticker(stop_event: "threading.Event", interval: int = 60
"""
from cron.scheduler_provider import InProcessCronScheduler, resolve_cron_scheduler
- # A live gateway on THIS backend's HERMES_HOME owns cron delivery with live platform
- # adapters (#52202): let it tick, and start nothing here. Without this, the fail-open
- # paths below (profile enumeration failure, empty served set, external provider) start
- # an ungated single-store ticker that races the gateway's tick-lock; when the desktop
- # wins, delivery has no live adapter and the cold send hangs until script_timeout.
- try:
- 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: fall through to the existing per-tick gating, which
- # still stands down profile-by-profile for gateway-owned homes.
- _log.warning("Desktop cron: gateway-ownership probe failed; using per-tick gating only", exc_info=True)
-
provider = resolve_cron_scheduler()
start_kwargs: dict = {"interval": interval}
@@ -153,6 +133,27 @@ def _start_desktop_cron_ticker(stop_event: "threading.Event", interval: int = 60
# Fail open to the single-store ticker so the active profile keeps firing.
_log.exception("Desktop cron: profile enumeration failed; ticking active profile only")
+ # 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.
+ if "profile_gate" not in start_kwargs:
+ try:
+ 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)
+
_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 4a0ca0c62d0..1c9f266a22b 100644
--- a/tests/hermes_cli/test_desktop_cron_ticker_gateway_standdown.py
+++ b/tests/hermes_cli/test_desktop_cron_ticker_gateway_standdown.py
@@ -2,8 +2,9 @@
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. The
-fix bails out before resolving the provider when a gateway is live on this home.
+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.
"""
from __future__ import annotations
@@ -82,3 +83,31 @@ def test_ticker_fails_open_when_ownership_probe_raises(ticker_env, monkeypatch,
assert "kwargs" in started # per-tick gating fallback, not a silent stand-down
assert "gateway-ownership probe failed" in caplog.text
+
+
+def test_gated_ticker_resumes_after_the_gateway_stops(ticker_env, monkeypatch):
+ """A gateway live at backend start must not silence Desktop cron for good: the multiplex
+ ticker still starts, and its per-tick gate stands down only while that gateway runs."""
+ import cron.scheduler_provider as sp
+ import hermes_cli.profiles as profiles
+ import hermes_logging
+ from hermes_cli import web_server
+
+ home, started = ticker_env
+
+ class _InProcess(sp.InProcessCronScheduler):
+ def start(self, stop_event, **kwargs):
+ started["kwargs"] = kwargs
+
+ gateway = {"running": True}
+ monkeypatch.setattr(sp, "resolve_cron_scheduler", lambda: _InProcess())
+ monkeypatch.setattr(profiles, "profiles_to_serve", lambda **_kw: [("default", home)])
+ monkeypatch.setattr(profiles, "_check_gateway_running", lambda _home: gateway["running"])
+ monkeypatch.setattr(hermes_logging, "enable_profile_log_routing", lambda _homes: None)
+
+ web_server._start_desktop_cron_ticker(threading.Event(), interval=0)
+
+ gate = started["kwargs"]["profile_gate"]
+ 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