Skip to content

The agent that grows with you

fix(gateway): launchd install honours --no-start-now and the wizard's "start now" answer

John Paul Soliva693e4aa12c54
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py
index b5594027849..30e11e4f477 100644
--- a/hermes_cli/gateway.py
+++ b/hermes_cli/gateway.py
@@ -4702,7 +4702,7 @@ def _cmd_install(args):
sys.exit(1)
_install_systemd_from_cli(args, force=force, system=system, run_as_user=run_as_user)
elif backend == "launchd":
- launchd_install(force)
+ launchd_install(force, start_now=getattr(args, "start_now", None) is not False)
elif backend == "windows":
_gw_windows().install(
force=force,
diff --git a/hermes_cli/gateway_launchd.py b/hermes_cli/gateway_launchd.py
index cd60588545d..09ff2720b0f 100644
--- a/hermes_cli/gateway_launchd.py
+++ b/hermes_cli/gateway_launchd.py
@@ -577,11 +577,19 @@ def refresh_launchd_plist_if_needed() -> bool:
return True
-def launchd_install(force: bool = False):
+def launchd_install(force: bool = False, *, start_now: bool = True):
plist_path = _gw().get_launchd_plist_path()
+ label = _gw().get_launchd_label()
+ # Loading the plist starts the gateway (RunAtLoad), so a no-start install writes it without
+ # loading it. A gateway that launchd already runs is still reloaded; this install did not start it.
+ load = start_now or _gw()._launchctl_label_supervising_process(label)
if plist_path.exists() and not force:
- if not _gw().launchd_plist_is_current():
+ if _gw().launchd_plist_is_current():
+ print(f"Service already installed at: {plist_path}")
+ print("Use --force to reinstall")
+ return
+ if load:
print(f"↻ Repairing outdated launchd service at: {plist_path}")
if _gw().refresh_launchd_plist_if_needed():
print("✓ Service definition updated")
@@ -595,9 +603,6 @@ def launchd_install(force: bool = False):
f"{display_hermes_home()}/logs/launchd-reload.log for details."
)
return
- print(f"Service already installed at: {plist_path}")
- print("Use --force to reinstall")
- return
plist_path.parent.mkdir(parents=True, exist_ok=True)
new_plist = _gw().generate_launchd_plist()
@@ -606,8 +611,22 @@ def launchd_install(force: bool = False):
print(f"Installing launchd service to: {plist_path}")
plist_path.write_text(new_plist, encoding="utf-8")
+ if not load:
+ # A job left loaded but idle (a parked clean exit) keeps its old definition, and that is
+ # what `hermes gateway start` would kickstart instead of loading this plist.
+ subprocess.run(
+ ["launchctl", "bootout", f"{_gw()._launchd_domain()}/{label}"],
+ check=False, timeout=90, **_gw()._CAPTURE_TEXT)
+ print()
+ print("✓ Service installed, not started (launchd starts it at your next login)")
+ print()
+ print("Next steps:")
+ print(" hermes gateway start # Start it now")
+ print(" hermes gateway status # Check status")
+ return
+
try:
- _gw()._launchctl_bootstrap(_gw()._launchd_domain(), plist_path, _gw().get_launchd_label(), timeout=30)
+ _gw()._launchctl_bootstrap(_gw()._launchd_domain(), plist_path, label, timeout=30)
except subprocess.CalledProcessError as e:
_gw()._launchd_degrade_or_raise(e, "launchctl bootstrap")
return
diff --git a/hermes_cli/gateway_setup_wizard.py b/hermes_cli/gateway_setup_wizard.py
index e175b8ad2e5..9f702a1cf9c 100644
--- a/hermes_cli/gateway_setup_wizard.py
+++ b/hermes_cli/gateway_setup_wizard.py
@@ -862,7 +862,7 @@ def _wizard_install_service(backend: str) -> None:
force=False, enable_on_startup=start_on_login
)
elif backend == "launchd":
- _gw().launchd_install(force=False)
+ _gw().launchd_install(force=False, start_now=start_now)
else:
_gw()._gw_windows().install(force=False, start_now=start_now, start_on_login=start_on_login)
return
diff --git a/tests/hermes_cli/test_launchd_install_start_now.py b/tests/hermes_cli/test_launchd_install_start_now.py
new file mode 100644
index 00000000000..7ead1f1b13d
--- /dev/null
+++ b/tests/hermes_cli/test_launchd_install_start_now.py
@@ -0,0 +1,99 @@
+"""`hermes gateway install --no-start-now` and the setup wizard's "Start the gateway now?" = No on launchd.
+
+The plist carries RunAtLoad, so loading it (`launchctl bootstrap`) starts the gateway on the spot. A
+no-start install must therefore write the plist without loading it; `hermes gateway start` loads it later.
+"""
+import argparse
+import plistlib
+import subprocess
+from types import SimpleNamespace
+
+import pytest
+
+import hermes_cli.gateway as gateway_cli
+from hermes_cli.subcommands.gateway import build_gateway_parser
+
+LABEL = "ai.hermes.gateway"
+DOMAIN = "gui/501"
+
+
+def _parse(*argv):
+ parser = argparse.ArgumentParser()
+ build_gateway_parser(
+ parser.add_subparsers(dest="command"),
+ cmd_gateway=lambda a: None, cmd_proxy=lambda a: None, cmd_gateway_enroll=lambda a: None,
+ )
+ return parser.parse_args(["gateway", *argv])
+
+
+def launchd(tmp_path, monkeypatch):
+ """launchd backend with every launchctl call recorded instead of run."""
+ state = SimpleNamespace(
+ plist=tmp_path / "LaunchAgents" / f"{LABEL}.plist",
+ supervised_pid=None, bootstraps=[], launchctl=[], refreshes=[], starts=[],
+ )
+ monkeypatch.setattr(gateway_cli, "_service_backend", lambda *a, **k: "launchd")
+ monkeypatch.setattr(gateway_cli, "is_managed", lambda: False)
+ monkeypatch.setattr(gateway_cli, "is_termux", lambda: False)
+ monkeypatch.setattr(gateway_cli, "_guard_named_profile_under_multiplexer", lambda **k: None)
+ monkeypatch.setattr(gateway_cli, "get_launchd_plist_path", lambda: state.plist)
+ monkeypatch.setattr(gateway_cli, "get_launchd_label", lambda: LABEL)
+ monkeypatch.setattr(gateway_cli, "_launchd_domain", lambda: DOMAIN)
+ monkeypatch.setattr(gateway_cli, "_refuse_temp_home_service_write", lambda *a: False)
+ monkeypatch.setattr(gateway_cli, "_clear_launchd_unsupported_marker", lambda: None)
+ monkeypatch.setattr(gateway_cli, "_launchctl_supervised_pid", lambda label: state.supervised_pid)
+ monkeypatch.setattr(gateway_cli, "_launchctl_bootstrap", lambda *a, **k: state.bootstraps.append(a))
+ monkeypatch.setattr(gateway_cli, "refresh_launchd_plist_if_needed", lambda: state.refreshes.append(1) or True)
+ monkeypatch.setattr(gateway_cli, "_setup_service_action", lambda *a, **k: state.starts.append(a))
+
+ def fake_run(cmd, *a, **k):
+ state.launchctl.append(cmd)
+ return SimpleNamespace(returncode=0, stdout="", stderr="")
+
+ monkeypatch.setattr(subprocess, "run", fake_run)
+ return state
+
+
+BOOTOUT = ["launchctl", "bootout", f"{DOMAIN}/{LABEL}"]
+
+
+ "stale_plist, supervised_pid, reloads",
+ [(False, None, False), (True, None, False), (True, 4242, True)],
+ ids=["fresh-install", "repair-stopped-gateway", "repair-running-gateway"],
+)
+def test_cli_no_start_now_loads_only_a_gateway_launchd_already_runs(
+ launchd, capsys, stale_plist, supervised_pid, reloads,
+):
+ """Nothing running: the plist is written, never loaded, and an idle registration left from before
+ (a parked clean exit) is booted out, since that is what `gateway start` would kickstart. A gateway
+ launchd already runs is reloaded as before, not stopped."""
+ if stale_plist:
+ launchd.plist.parent.mkdir(parents=True)
+ launchd.plist.write_text("<plist>old</plist>", encoding="utf-8")
+ launchd.supervised_pid = supervised_pid
+
+ gateway_cli.gateway_command(_parse("install", "--no-start-now"))
+
+ assert launchd.bootstraps == []
+ if reloads:
+ assert launchd.refreshes == [1]
+ assert BOOTOUT not in launchd.launchctl
+ else:
+ assert launchd.refreshes == []
+ assert BOOTOUT in launchd.launchctl
+ assert plistlib.loads(launchd.plist.read_bytes())["Label"] == LABEL
+ assert "not started" in capsys.readouterr().out
+
+
+def test_wizard_no_to_start_now_installs_without_loading(launchd, monkeypatch):
+ answers = iter([False, True]) # "Start the gateway now?" No, "on login" Yes
+ monkeypatch.setattr(gateway_cli, "prompt_yes_no", lambda *a, **k: next(answers))
+ monkeypatch.setattr(gateway_cli, "is_wsl", lambda: False)
+
+ gateway_cli._wizard_install_service("launchd")
+
+ assert launchd.plist.exists()
+ assert launchd.bootstraps == []
+ assert launchd.starts == []
diff --git a/website/docs/user-guide/messaging/index.md b/website/docs/user-guide/messaging/index.md
index 4df31e92783..50310d90798 100644
--- a/website/docs/user-guide/messaging/index.md
+++ b/website/docs/user-guide/messaging/index.md
@@ -667,6 +667,10 @@ The generated plist lives at `~/Library/LaunchAgents/ai.hermes.gateway.plist`. I
launchd plists are static — if you install new tools (e.g. a new Node.js version via nvm, or ffmpeg via Homebrew) after setting up the gateway, run `hermes gateway install` again to capture the updated PATH. The gateway will detect the stale plist and reload automatically.
:::
+:::info Installing without starting
+The plist sets `RunAtLoad`, so loading it starts the gateway. `hermes gateway install --no-start-now`, like answering No to "Start the gateway now?" in `hermes gateway setup`, writes the plist without loading it: the gateway starts at your next login, or when you run `hermes gateway start`. A gateway that launchd is already running is reloaded onto the new plist, not stopped.
+:::
+
:::info Local Network access (LAN devices fail with "No route to host")
macOS Local Network Privacy attributes a socket to the executable launchd spawned for the job. A bare venv Python has no application identity, so a launchd-run gateway could not reach LAN hosts (Home Assistant, local model servers) — every connect failed with `errno 65 No route to host` while the same URL worked from Terminal, and no prompt was ever shown to grant it. The generated plist therefore runs the gateway through `/usr/bin/osascript` (`do shell script "exec …"`), whose children macOS treats as osascript's own — an Apple platform binary, exempt from the check. `ps` shows `osascript → stderr_timestamp → gateway run`; stop/restart/KeepAlive behave exactly as before. A plist installed by an older Hermes is refreshed by `hermes gateway install` (or on the next `hermes gateway start`).
:::