Skip to content

The agent that grows with you

fix(cron): share liveness helper with CLI and extend it to cronjob list

kshitijk4poor5843b2f59594
diff --git a/hermes_cli/cron.py b/hermes_cli/cron.py
index b135d532da8..775b2ea4047 100644
--- a/hermes_cli/cron.py
+++ b/hermes_cli/cron.py
@@ -63,6 +63,27 @@ def _active_cron_provider_name() -> str:
return "builtin"
+def _builtin_gateway_liveness() -> Optional[bool]:
+ """Tri-state liveness of the builtin cron scheduler's trigger.
+
+ Single source of truth shared by the CLI (``_warn_if_gateway_not_running``)
+ and the ``cronjob`` model tool (#87033): the builtin ticker only runs
+ inside the gateway process, so a scheduled job with no live gateway can
+ never fire. Non-builtin providers (e.g. Chronos) fire through their own
+ machinery and are deliberately exempt — a missing gateway process means
+ nothing for them, so they report active. ``None`` = probe failed; callers
+ must not claim either way.
+ """
+ try:
+ if _active_cron_provider_name() != "builtin":
+ return True # external provider fires jobs without the gateway
+ from hermes_cli.gateway import find_gateway_pids
+
+ return bool(find_gateway_pids())
+ except Exception:
+ return None
+
+
def _warn_if_gateway_not_running() -> None:
"""Warn that scheduled jobs won't fire unless the gateway is running.
@@ -79,15 +100,11 @@ def _warn_if_gateway_not_running() -> None:
built-in ticker's trigger.
"""
try:
- if _active_cron_provider_name() != "builtin":
- return
-
- from hermes_cli.gateway import find_gateway_pids
-
- if find_gateway_pids():
+ if _builtin_gateway_liveness() is not False:
+ # Active scheduler (gateway up, or an exempt non-builtin provider),
+ # or the probe failed — stay quiet rather than nag either way.
return
except Exception:
- # If we can't determine gateway state, stay quiet rather than nag.
return
print(color(" ⚠ Gateway is not running — jobs won't fire automatically.", Colors.YELLOW))
diff --git a/tests/cron/test_87033_cronjob_gateway_liveness.py b/tests/cron/test_87033_cronjob_gateway_liveness.py
index 6ef26f449a5..2bbd82d6291 100644
--- a/tests/cron/test_87033_cronjob_gateway_liveness.py
+++ b/tests/cron/test_87033_cronjob_gateway_liveness.py
@@ -103,6 +103,58 @@ class TestCreateSurfacesGatewayLiveness:
assert "warning" not in result
+class TestListSurfacesGatewayLiveness:
+ """The `list` action has the same silent-inert-job failure mode as
+ create (#87033): an agent inspecting jobs with no gateway running must
+ learn they are not firing, not just see a clean list."""
+
+ def _list_jobs(self) -> dict:
+ from tools.cronjob_tools import cronjob
+
+ return json.loads(cronjob(action="list"))
+
+ def test_list_with_gateway_running_has_no_warning(self, hermes_env):
+ _create_job() # ensure at least one job exists
+ with patch_liveness(provider="builtin", pids=[12345]):
+ result = self._list_jobs()
+
+ assert result["success"] is True
+ assert result["count"] >= 1
+ assert result["gateway_running"] is True
+ assert "warning" not in result
+
+ def test_list_without_gateway_warns_jobs_inert(self, hermes_env):
+ _create_job()
+ with patch_liveness(provider="builtin", pids=[]):
+ result = self._list_jobs()
+
+ assert result["success"] is True
+ assert result["gateway_running"] is False
+ warning = result.get("warning", "")
+ assert "will NOT fire" in warning, (
+ "the model must be told the listed jobs won't fire (#87033)"
+ )
+ assert "these jobs" in warning
+
+ def test_list_empty_without_gateway_stays_quiet(self, hermes_env):
+ """Nothing scheduled + no gateway → no alarm; there is nothing inert."""
+ with patch_liveness(provider="builtin", pids=[]):
+ result = self._list_jobs()
+
+ assert result["success"] is True
+ assert result["count"] == 0
+ assert "warning" not in result
+
+ def test_list_non_builtin_provider_is_exempt(self, hermes_env):
+ _create_job()
+ with patch_liveness(provider="chronos", pids=[]):
+ result = self._list_jobs()
+
+ assert result["success"] is True
+ assert result["gateway_running"] is True
+ assert "warning" not in result
+
+
# ---------------------------------------------------------------------------
diff --git a/tools/cronjob_tools.py b/tools/cronjob_tools.py
index ed090e68b4a..f81a324d190 100644
--- a/tools/cronjob_tools.py
+++ b/tools/cronjob_tools.py
@@ -1223,26 +1223,34 @@ def _apply_continuity(
return refs or None
-def _builtin_gateway_liveness() -> Optional[bool]:
- """Gateway liveness for the builtin cron scheduler, or None when unknown.
-
- Mirrors the CLI's ``_warn_if_gateway_not_running`` heuristic (#87033): the
- builtin ticker only runs inside the gateway process, so a scheduled job
- with no live gateway can never fire. Non-builtin providers (e.g. Chronos)
- fire through their own machinery and are deliberately exempt — a missing
- gateway process means nothing for them. ``None`` = probe failed; callers
- must not claim either way.
+def _gateway_liveness_notice() -> dict:
+ """Build the ``gateway_running``/``warning`` payload for tool results.
+
+ Thin adapter over the shared CLI helper ``hermes_cli.cron._builtin_gateway_liveness``
+ (#87033) so the CLI and this tool can never disagree about what "scheduler
+ active" means. Returns ``{}`` when the scheduler is active (nothing to say),
+ or carries ``gateway_running: false`` plus a model-facing ``warning`` when
+ the builtin ticker has no gateway process to run it.
"""
try:
- from hermes_cli.cron import _active_cron_provider_name
+ from hermes_cli.cron import _builtin_gateway_liveness
- if _active_cron_provider_name() != "builtin":
- return True # external provider fires jobs without the gateway
- from hermes_cli.gateway import find_gateway_pids
-
- return bool(find_gateway_pids())
+ _gw = _builtin_gateway_liveness()
except Exception:
- return None
+ return {"gateway_running": None}
+ if _gw is False:
+ return {
+ "gateway_running": False,
+ "warning": (
+ "The Hermes gateway is not running — this job is saved "
+ "but will NOT fire until the gateway is started "
+ "(hermes gateway install / hermes gateway start). "
+ "Tell the user the task is scheduled but not active yet."
+ ),
+ }
+ if _gw is None:
+ return {"gateway_running": None}
+ return {"gateway_running": True}
def cronjob(
@@ -1395,7 +1403,6 @@ def cronjob(
# gateway running is stored but will never fire. Tell the model
# here — the CLI already warns, but the agent path saw only a
# clean success and confidently told the user it was scheduled.
- _gw_running = _builtin_gateway_liveness()
_result = {
"success": True,
"job_id": job["id"],
@@ -1408,24 +1415,29 @@ def cronjob(
"next_run_at": job["next_run_at"],
"job": _format_job(job),
"message": _create_message,
+ **_gateway_liveness_notice(),
}
- if _gw_running is False:
- _result["gateway_running"] = False
- _result["warning"] = (
- "The Hermes gateway is not running — this job is saved "
- "but will NOT fire until the gateway is started "
- "(hermes gateway install / hermes gateway start). "
- "Tell the user the task is scheduled but not active yet."
- )
- elif _gw_running is None:
- _result["gateway_running"] = None
- else:
- _result["gateway_running"] = True
return json.dumps(_result, indent=2)
if normalized == "list":
jobs = [_format_job(job) for job in list_jobs(include_disabled=include_disabled)]
- return json.dumps({"success": True, "count": len(jobs), "jobs": jobs}, indent=2)
+ _result = {"success": True, "count": len(jobs), "jobs": jobs}
+ # Same silent-inert-job class as create (#87033): an agent
+ # inspecting existing jobs in a gateway-less environment must
+ # learn they are not firing, not just see a clean list.
+ _liveness = _gateway_liveness_notice()
+ if len(jobs) or "gateway_running" in _liveness and _liveness["gateway_running"] is None:
+ if len(jobs):
+ _liveness_warning = _liveness.get("warning", "")
+ if _liveness_warning:
+ _liveness["warning"] = (
+ "Note: " + _liveness_warning.replace(
+ "this job is saved but will NOT fire",
+ "these jobs are saved but will NOT fire",
+ )
+ )
+ _result.update(_liveness)
+ return json.dumps(_result, indent=2)
if not job_id:
return tool_error(f"job_id is required for action '{normalized}'", success=False)