Skip to content

The agent that grows with you

fix(cron): surface gateway liveness in cronjob tool results (#87033)

BrunoBza302085ab2e09
diff --git a/tests/cron/test_87033_cronjob_gateway_liveness.py b/tests/cron/test_87033_cronjob_gateway_liveness.py
new file mode 100644
index 00000000000..6ef26f449a5
--- /dev/null
+++ b/tests/cron/test_87033_cronjob_gateway_liveness.py
@@ -0,0 +1,148 @@
+"""Tests for issue #87033 — the cronjob tool must surface gateway liveness.
+
+The builtin cron ticker only runs inside the gateway process. Before the
+fix, ``cronjob(action="create")`` returned a clean success even with no
+gateway running, so the agent confidently told the user a recurring task
+was scheduled while the job could never fire. The CLI already warned
+(``hermes cron list`` / ``hermes cron status``); the agent path did not.
+
+Contract pinned here:
+
+* create with a live gateway → ``gateway_running: true``, no warning;
+* create with no gateway → ``gateway_running: false`` + explicit warning
+ telling the model the job is saved but will not fire yet;
+* non-builtin scheduler providers are exempt (they fire without the gateway);
+* a failed liveness probe stays neutral (``gateway_running: null``) instead
+ of claiming either way.
+"""
+
+from __future__ import annotations
+
+import json
+
+import pytest
+
+
+def hermes_env(tmp_path, monkeypatch):
+ """Isolate HERMES_HOME for each test so jobs don't leak."""
+ home = tmp_path / ".hermes"
+ home.mkdir()
+ (home / "cron").mkdir()
+ monkeypatch.setenv("HERMES_HOME", str(home))
+
+ import importlib
+
+ import hermes_constants
+ importlib.reload(hermes_constants)
+ import cron.jobs
+ importlib.reload(cron.jobs)
+ import cron.scheduler
+ importlib.reload(cron.scheduler)
+
+ return home
+
+
+def _create_job() -> dict:
+ from tools.cronjob_tools import cronjob
+
+ return json.loads(
+ cronjob(
+ action="create",
+ schedule="every 10m",
+ prompt="say hi",
+ name="liveness-probe-job",
+ deliver="local",
+ )
+ )
+
+
+class TestCreateSurfacesGatewayLiveness:
+ def test_create_with_gateway_running_has_no_warning(self, hermes_env):
+ with patch_liveness(provider="builtin", pids=[12345]) as patches:
+ result = _create_job()
+
+ assert result["success"] is True
+ assert result["gateway_running"] is True
+ assert "warning" not in result
+
+ def test_create_without_gateway_warns_not_scheduled(self, hermes_env):
+ with (
+ patch_liveness(provider="builtin", pids=[]),
+ ):
+ result = _create_job()
+
+ assert result["success"] is True, (
+ "the job itself is still created successfully"
+ )
+ assert result["gateway_running"] is False
+ warning = result.get("warning", "")
+ assert "not running" in warning.lower()
+ assert "will NOT fire" in warning, (
+ "the model must be told the job won't fire (#87033)"
+ )
+ assert "gateway" in warning.lower()
+
+ def test_non_builtin_provider_is_exempt(self, hermes_env):
+ """External schedulers (e.g. Chronos) fire without the gateway —
+ no false alarm may be raised for them."""
+ with patch_liveness(provider="chronos", pids=[]):
+ result = _create_job()
+
+ assert result["success"] is True
+ assert result["gateway_running"] is True
+ assert "warning" not in result
+
+ def test_failed_probe_stays_neutral(self, hermes_env):
+ """If liveness cannot be determined, say nothing either way."""
+ with patch_liveness(provider=None, pids=[]): # probe raises → None
+ result = _create_job()
+
+ assert result["success"] is True
+ assert result["gateway_running"] is None
+ assert "warning" not in result
+
+
+# ---------------------------------------------------------------------------
+
+
+from contextlib import ExitStack
+
+
+class _LivenessPatches:
+ """Context manager patching the provider/gateway-pid probes."""
+
+ def __init__(self, *, provider, pids):
+ self._provider = provider
+ self._pids = pids
+
+ def __enter__(self):
+ from unittest.mock import patch
+
+ self._stack = ExitStack()
+
+ def _fake_provider_name():
+ if self._provider is None:
+ raise RuntimeError("probe failure")
+ return self._provider
+
+ self._stack.enter_context(
+ patch(
+ "hermes_cli.cron._active_cron_provider_name",
+ side_effect=_fake_provider_name,
+ )
+ )
+ self._stack.enter_context(
+ patch(
+ "hermes_cli.gateway.find_gateway_pids",
+ return_value=list(self._pids),
+ )
+ )
+ return self
+
+ def __exit__(self, *exc):
+ return self._stack.__exit__(*exc)
+
+
+def patch_liveness(*, provider, pids):
+ return _LivenessPatches(provider=provider, pids=pids)
diff --git a/tools/cronjob_tools.py b/tools/cronjob_tools.py
index b4f21d3c8d5..ed090e68b4a 100644
--- a/tools/cronjob_tools.py
+++ b/tools/cronjob_tools.py
@@ -1223,6 +1223,28 @@ 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.
+ """
+ try:
+ from hermes_cli.cron import _active_cron_provider_name
+
+ 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 cronjob(
action: str,
job_id: Optional[str] = None,
@@ -1368,22 +1390,38 @@ def cronjob(
_local_notice = _local_delivery_notice(job, _normalize_deliver_param(deliver))
if _local_notice:
_create_message = f"{_create_message} {_local_notice}"
- return json.dumps(
- {
- "success": True,
- "job_id": job["id"],
- "name": job["name"],
- "skill": job.get("skill"),
- "skills": job.get("skills", []),
- "schedule": job["schedule_display"],
- "repeat": _repeat_display(job),
- "deliver": job.get("deliver", "local"),
- "next_run_at": job["next_run_at"],
- "job": _format_job(job),
- "message": _create_message,
- },
- indent=2,
- )
+ # Gateway liveness surfacing (#87033): the builtin scheduler's
+ # ticker lives in the gateway process, so a job created with no
+ # 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"],
+ "name": job["name"],
+ "skill": job.get("skill"),
+ "skills": job.get("skills", []),
+ "schedule": job["schedule_display"],
+ "repeat": _repeat_display(job),
+ "deliver": job.get("deliver", "local"),
+ "next_run_at": job["next_run_at"],
+ "job": _format_job(job),
+ "message": _create_message,
+ }
+ 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)]