The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cron): register the owning profile's config hooks in the external worker
liuhao1024946eafdf2fe9
diff --git a/cron/scheduler.py b/cron/scheduler.pyindex 2da0a402ac2..b2e3349b0c6 100644--- a/cron/scheduler.py+++ b/cron/scheduler.py@@ -3941,6 +3941,24 @@ def _run_external_worker_payload(payload_path: Path, ack_path: Path) -> bool:discover_plugins()hydrate_profile_secret_sources(profile_home)+ # Config-declared hooks (``hooks:`` shell hooks + ``hooks.outbound``) live in the owning+ # profile's config.yaml, which this one-shot process never registered — cron sessions+ # silently lost them the moment fires were handed here instead of running in-process in+ # the gateway (#131764, same gap `hermes serve` had in #102504). Under the home override+ # both loaders resolve the OWNING profile's config; consent must come from that profile's+ # allowlist / HERMES_ACCEPT_HOOKS / hooks_auto_accept because there is no TTY here.+ # Never raises: a broken hooks block must not take the cron execution down with it+ # (register_from_config already skips malformed entries; this guard covers config load).+ try:+ from agent.shell_hooks import register_from_config as register_shell_hooks+ from agent.outbound_webhooks import register_from_config as register_outbound_webhooks+ from hermes_cli.config import load_config++ hooks_cfg = load_config()+ register_shell_hooks(hooks_cfg, accept_hooks=False)+ register_outbound_webhooks(hooks_cfg)+ except Exception:+ logger.warning("Cron external worker: config hook registration failed", exc_info=True)secret_token = set_secret_scope(build_profile_secret_scope(profile_home), profile_home=str(profile_home))with use_cron_store(profile_home):if adopt_claimed_execution(execution_id) is None:diff --git a/tests/cron/test_external_worker_config_hooks.py b/tests/cron/test_external_worker_config_hooks.pynew file mode 100644index 00000000000..337422a2ebc--- /dev/null+++ b/tests/cron/test_external_worker_config_hooks.py@@ -0,0 +1,102 @@+"""External cron workers must register the owning profile's config-declared hooks (#131764).++``python -m cron.scheduler --external-worker-file`` starts with the builtin registry alone and+used to never call ``shell_hooks.register_from_config()`` / ``outbound_webhooks.register_from_+config()``, so ``hooks:`` blocks in the owning profile's config.yaml silently stopped firing for+cron sessions the moment fires were handed to the worker instead of running in-process in the+gateway — plugin hooks kept working (``discover_plugins()`` ran) and nothing was logged. Both+registrations must run under the payload's home override so a multiplexed worker registers the+OWNING profile's hooks, and consent must resolve non-interactively (``hooks_auto_accept`` /+allowlist / env — this process has no TTY).+"""+from __future__ import annotations++import json++import pytest++HOOK_CMD = "echo cron-hook-fired"+OUTBOUND_URL = "https://hooks.example.test/cron"+++def homes(tmp_path, monkeypatch):+ """A launch home with no hooks config and a profile home declaring both hook kinds."""+ launch = tmp_path / "launch"+ launch.mkdir()+ monkeypatch.setenv("HERMES_HOME", str(launch))+ profile = tmp_path / "profile"+ profile.mkdir()+ (profile / "config.yaml").write_text(+ "hooks:\n"+ " on_session_start:\n"+ f" - command: \"{HOOK_CMD}\"\n"+ " outbound:\n"+ f" - url: {OUTBOUND_URL}\n"+ " events: [on_session_end]\n"+ # Non-interactive consent: the worker has no TTY, and the pair is not allowlisted.+ "hooks_auto_accept: true\n",+ encoding="utf-8",+ )+ yield launch, profile+ import agent.outbound_webhooks as outbound_webhooks+ import agent.shell_hooks as shell_hooks+ from hermes_cli.plugins import _reset_plugin_managers_for_tests++ shell_hooks.reset_for_tests()+ outbound_webhooks.reset_for_tests()+ _reset_plugin_managers_for_tests()+++def test_worker_registers_owning_profile_config_hooks(homes, tmp_path, monkeypatch):+ launch, profile = homes+ import agent.outbound_webhooks as outbound_webhooks+ import agent.shell_hooks as shell_hooks+ import cron.scheduler as scheduler+ from hermes_constants import hermes_home_key++ payload = tmp_path / "payload.json"+ payload.write_text(+ json.dumps({"job": {"id": "job-1", "execution_id": "exec-1"},+ "profile_home": str(profile), "multiplex_active": True}),+ encoding="utf-8",+ )+ monkeypatch.setattr(+ "cron.executions.adopt_claimed_execution",+ lambda execution_id: {"id": execution_id, "status": "running"},+ )+ monkeypatch.setattr(scheduler, "run_one_job", lambda *a, **k: True)++ assert scheduler._run_external_worker_payload(payload, tmp_path / "exec-1.ready") is True+ # Registered under the OWNING profile's home key, not the launch home's.+ profile_key = hermes_home_key(profile)+ assert any(home == profile_key and event == "on_session_start" and command == HOOK_CMD+ for home, event, _matcher, command in shell_hooks._registered)+ assert any(home == profile_key and event == "on_session_end" and url == OUTBOUND_URL+ for home, event, url in outbound_webhooks._registered)+++def test_worker_survives_a_broken_hooks_config(homes, tmp_path, monkeypatch):+ """A malformed hooks block must not take the cron execution down with it."""+ launch, profile = homes+ import cron.scheduler as scheduler++ (profile / "config.yaml").write_text(+ "hooks:\n"+ " on_session_start: not-a-list\n"+ "hooks_auto_accept: true\n",+ encoding="utf-8",+ )+ payload = tmp_path / "payload.json"+ payload.write_text(+ json.dumps({"job": {"id": "job-1", "execution_id": "exec-1"},+ "profile_home": str(profile)}),+ encoding="utf-8",+ )+ monkeypatch.setattr(+ "cron.executions.adopt_claimed_execution",+ lambda execution_id: {"id": execution_id, "status": "running"},+ )+ monkeypatch.setattr(scheduler, "run_one_job", lambda *a, **k: True)++ assert scheduler._run_external_worker_payload(payload, tmp_path / "exec-1.ready") is True