Skip to content

The agent that grows with you

fix(cron): deliver manual runs through the owner profile's adapters under multiplex

Fabio Ito8be50587d41a
diff --git a/tests/tools/test_cronjob_run_immediate.py b/tests/tools/test_cronjob_run_immediate.py
index 8af17e2b939..df29ef7e107 100644
--- a/tests/tools/test_cronjob_run_immediate.py
+++ b/tests/tools/test_cronjob_run_immediate.py
@@ -133,6 +133,31 @@ class TestCronjobRunExecutesImmediately:
extra_prompt=None,
)
+ def test_execute_job_now_delivers_through_owner_profile_adapters(self):
+ """Under multiplex a manual run fired from a secondary profile must deliver through THAT
+ profile's adapters, not the default profile's ``runner.adapters`` (wrong Telegram bot)."""
+ from pathlib import Path
+
+ default_adapters = {"telegram": object()}
+ work_adapters = {"telegram": object()}
+ gateway_loop = object()
+ runner = SimpleNamespace(
+ adapters=default_adapters,
+ _gateway_loop=gateway_loop,
+ _adapters_for_profile=lambda profile: work_adapters if profile == "work" else default_adapters,
+ )
+ completed = {"id": "job-run-1", "last_status": "ok", "last_error": None}
+
+ with patch("tools.cronjob_tools.claim_job_for_fire", return_value={**_JOB, "fire_claim": {"by": "manual-owner"}}), \
+ patch("gateway.run._gateway_runner_ref", return_value=runner), \
+ patch("hermes_constants.get_hermes_home", return_value=Path("/srv/hermes/profiles/work")), \
+ patch("cron.scheduler.run_one_job", return_value=True) as m_run, \
+ patch("tools.cronjob_tools.get_job", return_value=completed):
+ res = _execute_job_now(dict(_JOB))
+
+ assert res["success"] is True
+ assert m_run.call_args.kwargs["adapters"] is work_adapters
+
def test_execute_job_now_remains_standalone_without_gateway(self):
"""CLI-only runs retain the standalone delivery path."""
completed = {"id": "job-run-1", "last_status": "ok", "last_error": None}
diff --git a/tools/cronjob_tools.py b/tools/cronjob_tools.py
index b2e4efe8410..eaad3dbbe47 100644
--- a/tools/cronjob_tools.py
+++ b/tools/cronjob_tools.py
@@ -296,6 +296,18 @@ def _run_claimed_job(job: Dict[str, Any], extra_prompt: Optional[str] = None) ->
# task" and can break encrypted Matrix delivery (#61495 — salvaged from #63586 by @Fly-onlyone).
runner = runner_ref() if callable(runner_ref) else None
adapters = getattr(runner, "adapters", None) if runner is not None else None
+ # ``runner.adapters`` is the DEFAULT profile's map. Under ``gateway.multiplex_profiles`` a
+ # manual run fired from a secondary profile's agent executes with HERMES_HOME overridden to
+ # that profile's home, so delivering through ``runner.adapters`` sends the result out the
+ # default bot (a Telegram DM reaches the user from the wrong bot). Resolve the owner's map
+ # the same way notifications and goals do — fail closed, no fallback to the default bot.
+ if runner is not None and hasattr(runner, "_adapters_for_profile"):
+ try:
+ from hermes_constants import get_hermes_home, profile_name_for_home
+
+ adapters = runner._adapters_for_profile(profile_name_for_home(get_hermes_home()))
+ except Exception:
+ pass
gateway_loop = getattr(runner, "_gateway_loop", None) if runner is not None else None
try:
# run_one_job records last_run_at/last_status via mark_job_run; `job` is the