Skip to content

The agent that grows with you

fix(cron): bot-chat delivery launches the running install, not whatever `hermes` PATH names

teknium1f336048b08e5
diff --git a/cron/scheduler_delivery.py b/cron/scheduler_delivery.py
index 3842c87b6db..f843ee40dbf 100644
--- a/cron/scheduler_delivery.py
+++ b/cron/scheduler_delivery.py
@@ -736,19 +736,22 @@ def _deliver_to_bot_chat(job: dict, content: str, profile: str, *, deferred: Opt
# Discovery/admission uncertainty must never open a second-writer fallback.
return f"bot-chat delivery to profile '{profile_label}' unverified: {exc}"
- hermes_bin = shutil.which("hermes")
- if hermes_bin:
- argv = [hermes_bin]
+ # The running install first (same trust order as gateway.run._resolve_hermes_bin): the
+ # scheduler lives in the long-running gateway, so a PATH-first lookup would hand delivery
+ # to whatever `hermes` PATH names — another install, or a planted one — instead of this one.
+ try:
+ import importlib.util as _ilu
+ found = _ilu.find_spec("hermes_cli") is not None
+ except Exception:
+ found = False
+ if found:
+ argv = [sys.executable, "-m", "hermes_cli.main"]
else:
- try:
- import importlib.util as _ilu
- found = _ilu.find_spec("hermes_cli") is not None
- except Exception:
- found = False
- if not found:
+ hermes_bin = shutil.which("hermes")
+ if not hermes_bin:
return ("Hermes could not deliver this result to Bot Chat: the `hermes` command was not found. "
"The result is saved; run `hermes cron runs` to see it, or `hermes doctor` if this keeps happening")
- argv = [sys.executable, "-m", "hermes_cli.main"]
+ argv = [hermes_bin]
def _fail(msg: str, **log_kwargs) -> str:
logger.warning("Job '%s': %s", job_id, msg, **log_kwargs)
diff --git a/tests/cron/test_bot_chat_cli_home.py b/tests/cron/test_bot_chat_cli_home.py
index 96c8c6ae7bd..f08a3573b8f 100644
--- a/tests/cron/test_bot_chat_cli_home.py
+++ b/tests/cron/test_bot_chat_cli_home.py
@@ -34,7 +34,8 @@ def test_cli_keeps_discovered_home_when_launch_selection_changes(tmp_path, monke
code = ('import json,sys; sys.argv=["hermes"]+json.loads(sys.argv[1]); '
'import hermes_cli.main; from hermes_constants import get_hermes_home; '
'print(json.dumps(str(get_hermes_home())))')
- result = real_run([sys.executable, "-c", code, json.dumps(argv[1:])],
+ # Everything after the launcher (binary or ``python -m hermes_cli.main``) is the CLI argv.
+ result = real_run([sys.executable, "-c", code, json.dumps(argv[argv.index("-p"):])],
env=kwargs["env"], capture_output=True, text=True, timeout=30)
assert result.returncode == 0, result.stderr
seen.append(Path(json.loads(result.stdout.strip().splitlines()[-1])))
diff --git a/tests/cron/test_bot_chat_pending.py b/tests/cron/test_bot_chat_pending.py
index 4cf3d751a0d..8aa2096d6ef 100644
--- a/tests/cron/test_bot_chat_pending.py
+++ b/tests/cron/test_bot_chat_pending.py
@@ -1,4 +1,5 @@
"""Only never-started cron delivery may wait for a CLI owner's release."""
+import importlib.util
import subprocess
from pathlib import Path
from unittest.mock import Mock
@@ -57,10 +58,11 @@ def test_delivery_exception_retains_attempt_and_continues_siblings(tmp_path, mon
original_is_dir = Path.is_dir
armed = False
- def resolve_cli(_):
+ def resolve_cli(name, *args, **kwargs):
+ # Delivery resolves the CLI (the running install's ``hermes_cli``) right after discovery.
nonlocal armed
armed = True
- return "/bin/hermes"
+ return object()
def is_dir(self):
if armed and self == blocked_home:
@@ -71,7 +73,7 @@ def test_delivery_exception_retains_attempt_and_continues_siblings(tmp_path, mon
calls.append(kwargs["env"]["HERMES_HOME"])
return subprocess.CompletedProcess([], 0, "", "")
- monkeypatch.setattr(delivery.shutil, "which", resolve_cli)
+ monkeypatch.setattr(importlib.util, "find_spec", resolve_cli)
monkeypatch.setattr(delivery.subprocess, "run", run)
monkeypatch.setattr(Path, "is_dir", is_dir)
queue.drain()
diff --git a/tests/cron/test_cron_bot_chat_delivery.py b/tests/cron/test_cron_bot_chat_delivery.py
index fbfa7939cb8..7d0ab443011 100644
--- a/tests/cron/test_cron_bot_chat_delivery.py
+++ b/tests/cron/test_cron_bot_chat_delivery.py
@@ -7,6 +7,7 @@ and the delivery-targets listing used by UI pickers.
"""
import subprocess
+import sys
from unittest import mock
import pytest
@@ -134,8 +135,9 @@ def test_deliver_runs_canonical_bot_chat_lane():
assert err is None
argv = calls["argv"]
- assert argv[0] == "/usr/bin/hermes"
- assert argv[1:3] == ["-p", "default"] # do not follow active_profile
+ # The running install's interpreter, not whatever `hermes` PATH names (same order as /update).
+ assert argv[:3] == [sys.executable, "-m", "hermes_cli.main"]
+ assert argv[3:5] == ["-p", "default"] # do not follow active_profile
assert "chat" in argv
assert "Bot Chat" in argv
assert "--create-if-missing" in argv