hermes-agent/tests/tools/test_cronjob_run_primary_routed.py
teknium1 17957f5014 fix(review): ticker heartbeat proves liveness only while its writer is alive; routed hand-off never trusts an unknown probe; satellite manual runs get the ticker's routed grant
MAJOR — `_builtin_gateway_liveness` trusted a bare-epoch ticker heartbeat for
~200 s after its writer died, for every profile, and the multiplexer pid gate
had been dropped: a killed serve/Desktop ticker read as alive and
`hermes -p X cron run` queued a primary-routed run "for the gateway's next
tick" with no ticker present.

- `cron/jobs.py::record_ticker_heartbeat` stamps `<epoch> <pid>`;
  `get_ticker_heartbeat_age` parses the first field (legacy bare stamps still
  yield an age); new `ticker_heartbeat_writer_alive` requires the stamped pid
  to be alive and treats a bare stamp as NOT proof by itself.
- The heartbeat-only rung is now `fresh AND (served-by-multiplexer OR writer
  alive)`: the multiplexer record proves the host process for a served named
  profile (and covers a stale-code multiplexer still writing bare stamps), so
  only the in-process serve/Desktop ticker relies on the heartbeat, and then
  only with a live writer. `cron status`'s in-process-ticker rung applies the
  same rule.

MINOR (a) — `_hand_off_primary_routed_run` gated on `is True`; an unknown
(None) probe returns an error naming the uncertainty instead of "queued …
runs and delivers it".

MINOR (b) — `_run_claimed_job` wraps a shared-bot satellite's resolved map in
`SharedRouteAdapters(primary, _primary_profile_routes_for_current_home())`,
the same grant the ticker's `tick_adapters_for` makes, instead of handing it
the full primary adapter map.

Tests (each red on the previous head a18977090b):
- test_cron_satellite_diagnostics.py::test_in_process_ticker_heartbeat_counts_only_while_its_writer_lives
- test_cronjob_run_primary_routed.py::test_routed_run_without_a_serving_gateway_fails_before_the_turn[None]
- test_cronjob_run_immediate.py::test_execute_job_now_grants_a_shared_bot_satellite_only_its_routed_targets
2026-09-28 05:28:05 -07:00

75 lines
3.5 KiB
Python

"""Manual runs of a satellite job that delivers through the primary gateway's profile route.
Regression for #120330. A multiplexed satellite profile with no ``platforms.<p>`` credential of
its own posts through the primary's bot via a root ``gateway.profile_routes`` entry, so only the
gateway process holding that bot can deliver its jobs. ``hermes -p <profile> cron run`` used to
run the whole agent turn in the CLI process anyway, fail delivery with ``platform 'telegram' not
configured/enabled`` and overwrite the job's ``last_status`` with ``delivery_failed``.
"""
import json
from types import SimpleNamespace
from unittest.mock import patch
import pytest
from hermes_cli.cron import _run_outcome
ROUTED_TARGET = "telegram:-1004306455751:14"
@pytest.fixture
def keeper_job(tmp_path, monkeypatch):
"""A ``keeper`` satellite home whose primary routes one Telegram topic to it, plus one job there."""
root = tmp_path / "root"
keeper_home = root / "profiles" / "keeper"
keeper_home.mkdir(parents=True)
(root / "config.yaml").write_text(json.dumps({"gateway": { # YAML is a JSON superset; the test env ships no pyyaml
"multiplex_profiles": True,
"profile_routes": [{"name": "ops", "platform": "telegram", "chat_id": "-1004306455751",
"thread_id": "14", "profile": "keeper"}],
}}), encoding="utf-8")
monkeypatch.setenv("HERMES_HOME", str(keeper_home)) # what `hermes -p keeper` sets
from cron.jobs import create_job
return create_job(prompt="infra report", schedule="0 9 * * *", name="infra", deliver=ROUTED_TARGET)
def _run(job_id, *, gateway_serves_profile):
from tools.cronjob_tools import cronjob
with patch("hermes_cli.cron._builtin_gateway_liveness", return_value=gateway_serves_profile), \
patch("cron.scheduler.run_one_job", return_value=True) as m_run:
out = json.loads(cronjob(action="run", job_id=job_id))
return out, m_run
def test_routed_run_is_queued_for_the_gateway_that_serves_the_profile(keeper_job, monkeypatch):
from cron.jobs import get_job
out, m_run = _run(keeper_job["id"], gateway_serves_profile=True)
assert out["success"] is True
m_run.assert_not_called() # no agent turn in a process that cannot deliver its result
stored = get_job(keeper_job["id"])
assert stored["next_run_at"] == stored["manual_run_at"] # due on the gateway's next tick
assert stored.get("last_status") is None # the manual run does not rewrite the job's status
assert _run_outcome(out["job"]) == "It will run on the next scheduler tick."
# Control: a profile holding its own credential for the platform runs in-process as before.
monkeypatch.setenv("TELEGRAM_BOT_TOKEN", "123456:keeper-own-bot")
_, m_run = _run(keeper_job["id"], gateway_serves_profile=True)
m_run.assert_called_once()
@pytest.mark.parametrize("gateway_serves_profile", [False, None])
def test_routed_run_without_a_serving_gateway_fails_before_the_turn(keeper_job, gateway_serves_profile):
"""False = no scheduler; None = the probe could not tell. Neither may queue a run that nothing
would pick up."""
from cron.jobs import get_job
out, m_run = _run(keeper_job["id"], gateway_serves_profile=gateway_serves_profile)
assert out["success"] is False
assert "profile route" in out["error"]
assert ("Could not determine" in out["error"]) is (gateway_serves_profile is None)
m_run.assert_not_called()
stored = get_job(keeper_job["id"])
assert stored["next_run_at"] == keeper_job["next_run_at"]
assert "manual_run_at" not in stored