Skip to content

The agent that grows with you

test: verify gateway service consent across import and setup

Teknium4ecf59755e9c
diff --git a/hermes_cli/backup.py b/hermes_cli/backup.py
index f1c61a6a9ee..7acbfeef40a 100644
--- a/hermes_cli/backup.py
+++ b/hermes_cli/backup.py
@@ -1028,7 +1028,7 @@ def _revive_gateway_after_import(hermes_root: Path) -> None:
(native_default / marker).exists() for marker in ("config.yaml", ".env", "state.db")):
print("\nRestored into a non-default home; leaving the gateway service alone to avoid clashing "
f"with the install at {native_default}.\n"
- "To start a gateway for this home, run: hermes gateway install")
+ "To start a gateway for this home, run: hermes gateway run")
return
try:
from hermes_cli.gateway import _is_service_running
@@ -1037,7 +1037,7 @@ def _revive_gateway_after_import(hermes_root: Path) -> None:
print()
ensure_gateway_service(context="import")
except Exception:
- print("\nStart the gateway to activate cron jobs and messaging:\n hermes gateway install")
+ print("\nStart the gateway to activate cron jobs and messaging:\n hermes gateway run")
# --- Quick state snapshots (used by /snapshot slash command and hermes backup --quick) ---
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py
index 61f66ac16f6..2247079031c 100644
--- a/hermes_cli/gateway.py
+++ b/hermes_cli/gateway.py
@@ -5466,7 +5466,6 @@ _WIZARD_BANNER = (
"│ Press Ctrl+C at any time to exit. │",
"└─────────────────────────────────────────────────────────┘",
)
-_WIZARD_BACKEND_LABELS = {"systemd": "systemd", "launchd": "launchd", "windows": "Scheduled Task"}
# Post-setup guidance when no service backend applies, keyed by the fallthrough reason.
_WIZARD_NO_SERVICE_LINES = {
"wsl": (
diff --git a/hermes_cli/gateway_setup_service.py b/hermes_cli/gateway_setup_service.py
index 40ac5a30039..c252d9415a9 100644
--- a/hermes_cli/gateway_setup_service.py
+++ b/hermes_cli/gateway_setup_service.py
@@ -73,7 +73,7 @@ def ensure_gateway_service(context: str = "setup", *, interactive: bool = False,
elif gw.is_macos():
gw.launchd_install(force=False)
else:
- gw._gw_windows().install(force=False)
+ gw._gw_windows().install(force=False, start_now=True, start_on_login=True)
# Installers can refuse without raising (e.g. temporary-home guard).
if not gw._is_service_installed():
gw.print_warning("Gateway service install did not complete. Retry: hermes gateway install")
diff --git a/hermes_cli/setup_platforms.py b/hermes_cli/setup_platforms.py
index 19003bedfa5..08706a749ef 100644
--- a/hermes_cli/setup_platforms.py
+++ b/hermes_cli/setup_platforms.py
@@ -342,10 +342,7 @@ def setup_gateway(config: dict):
print_success("Messaging platforms configured!")
_warn_missing_home_channels()
- # Gateway service setup runs UNCONDITIONALLY — a gateway with zero platforms is a supported
- # mode (cron keeps running; adapters come up once tokens are added via `hermes import` /
- # `hermes setup gateway`). Gating it on messaging config left install-then-import machines
- # with cron jobs and bot tokens but no process to serve them.
+ # Offer optional persistence even with no messaging platforms: cron can run alone.
from hermes_cli.gateway import _is_service_running, supports_systemd_services
from hermes_cli.gateway_setup_service import ensure_gateway_service
supports_systemd = supports_systemd_services()
diff --git a/tests/hermes_cli/test_backup.py b/tests/hermes_cli/test_backup.py
index 3fde74ec1d1..101f1b4763d 100644
--- a/tests/hermes_cli/test_backup.py
+++ b/tests/hermes_cli/test_backup.py
@@ -19,7 +19,7 @@ import hermes_cli.gateway_setup_service as service_setup
@pytest.fixture(autouse=True)
def _no_real_gateway_service(monkeypatch):
- """run_import() auto-installs the gateway service post-restore; tests must
+ """run_import() may start an existing gateway service post-restore; tests must
never touch the host's systemd/launchd. Individual tests re-patch these to
assert the wiring."""
import hermes_cli.gateway as gateway_mod
@@ -480,7 +480,7 @@ class TestImport:
out = capsys.readouterr().out
assert "Done. Your Hermes configuration has been restored." in out
- assert "hermes gateway install" in out
+ assert "hermes gateway run" in out
diff --git a/tests/hermes_cli/test_gateway_service_choice.py b/tests/hermes_cli/test_gateway_service_choice.py
index cf0fcc814a5..13d990eab8a 100644
--- a/tests/hermes_cli/test_gateway_service_choice.py
+++ b/tests/hermes_cli/test_gateway_service_choice.py
@@ -65,14 +65,25 @@ def _operations(calls):
@pytest.mark.linux_only
@pytest.mark.parametrize("choice", [None, "decline", "install"])
-def test_import_and_ordinary_setup_never_authorize_install(supervisor, choice):
[email protected]("installed", [False, True])
+def test_import_and_ordinary_setup_never_authorize_install(supervisor, choice, installed):
profile, calls = supervisor
config_api.save_config({"gateway": {"service_install_choice": choice}})
+ if installed:
+ unit = gateway.get_systemd_unit_path()
+ unit.parent.mkdir(parents=True)
+ unit.write_text(gateway.generate_systemd_unit(), encoding="utf-8")
before = (profile / "config.yaml").read_bytes()
- assert setup_service.ensure_gateway_service() is False
- assert setup_service.ensure_gateway_service(context="import") is False
- assert not gateway.get_systemd_unit_path().exists()
- assert not any(set(op) & {"enable", "enable-linger", "start", "daemon-reload"} for op in _operations(calls))
+ assert setup_service.ensure_gateway_service() is installed
+ assert setup_service.ensure_gateway_service(interactive=True) is installed
+ assert setup_service.ensure_gateway_service(context="import", install=True) is installed
+ from hermes_cli.backup import _revive_gateway_after_import
+ _revive_gateway_after_import(profile)
+ assert gateway.get_systemd_unit_path().exists() is installed
+ forbidden = {"enable", "enable-linger", "daemon-reload"}
+ if not installed:
+ forbidden.add("start")
+ assert not any(set(op) & forbidden for op in _operations(calls))
assert (profile / "config.yaml").read_bytes() == before
@@ -106,13 +117,14 @@ def test_explicit_setup_consent_is_durable_only_after_success(supervisor, monkey
@pytest.mark.linux_only
@pytest.mark.parametrize("start_now", [False, True])
-def test_wizard_declined_service_does_not_install(supervisor, monkeypatch, start_now):
[email protected]("consent", [False, True])
+def test_wizard_service_choice_controls_installation(supervisor, monkeypatch, start_now, consent):
profile, calls = supervisor
config_api.save_config({"gateway": {"service_install_choice": None}})
stream = io.StringIO()
stream.isatty = lambda: True
monkeypatch.setattr(sys, "stdin", stream)
- answers = iter([start_now, False])
+ answers = iter([start_now, consent])
monkeypatch.setattr(gateway, "prompt_yes_no", lambda *args: next(answers))
monkeypatch.setattr(gateway, "prompt_choice", lambda *args, **kwargs: 0)
spawned = []
@@ -127,9 +139,9 @@ def test_wizard_declined_service_does_not_install(supervisor, monkeypatch, start
monkeypatch.setattr(gateway.subprocess, "Popen", popen)
setup_service._wizard_install_service("systemd")
- assert not gateway.get_systemd_unit_path().exists()
- assert bool(spawned) is start_now
- assert config_api.load_config()["gateway"]["service_install_choice"] == "decline"
+ assert gateway.get_systemd_unit_path().exists() is consent
+ assert bool(spawned) is (start_now and not consent)
+ assert config_api.load_config()["gateway"]["service_install_choice"] == ("install" if consent else "decline")
if spawned:
assert "--replace" not in spawned[0][0]
diff --git a/website/docs/developer-guide/gateway-internals.md b/website/docs/developer-guide/gateway-internals.md
index 5b92103db34..1b06a051f3d 100644
--- a/website/docs/developer-guide/gateway-internals.md
+++ b/website/docs/developer-guide/gateway-internals.md
@@ -25,6 +25,32 @@ The messaging gateway is the long-running process that connects Hermes to 20+ ex
| `plugins/platforms/<name>/` | Bundled messaging adapters (most platforms: `adapter.py` + `plugin.yaml`) |
| `gateway/platforms/` | Shared `base.py` plus legacy/direct adapters (Signal, API server, webhooks, …) |
+## Optional service installation
+
+A running gateway and an installed OS service are different things. `hermes gateway run`
+works without a service, including with no messaging platforms configured.
+
+`hermes setup`, quick setup, and `hermes gateway setup` ask interactive users once:
+
+> Install the gateway service so Hermes starts automatically at login and keeps scheduled jobs and messaging available?
+
+The profile's `gateway.service_install_choice` stores `null`, `install`, or `decline`.
+An unset value is not permission to install. Declining records `decline` without creating,
+enabling, or removing a service. In the gateway wizard, choosing **Start now** but declining
+service installation launches an unmanaged gateway; it does not create a disabled service.
+Without service persistence, messaging and scheduled work stop at logout/reboot. Jobs cannot
+run while the host is off.
+
+Noninteractive setup and imports do not install a missing service, even when the imported
+config says `install`. Existing installed services remain authoritative and can be started
+regardless of the saved preference. To opt in explicitly later, run `hermes gateway install`;
+the choice is recorded only after successful installation. To be asked again during setup,
+reset the choice with `hermes config set gateway.service_install_choice null`.
+
+Setup orchestration lives in `hermes_cli/gateway_setup_service.py`. Its
+`ensure_gateway_service` helper is not a general runtime discovery or auto-start API:
+ordinary calls never reinstall a missing service based only on stored preference.
+
## Architecture Overview
```text