Skip to content

The agent that grows with you

fix(gateway): sudo stop/restart --system checks live work on the unit's home, not root's

Teknium4a0189731069
diff --git a/hermes_cli/gateway_lifecycle_guards.py b/hermes_cli/gateway_lifecycle_guards.py
index dd24a96f0f5..df21d00c35c 100644
--- a/hermes_cli/gateway_lifecycle_guards.py
+++ b/hermes_cli/gateway_lifecycle_guards.py
@@ -47,12 +47,19 @@ def live_work(home: Path) -> Optional[dict[str, Any]]:
return _live_work_from_state(home)
-def _candidate_homes() -> list[Path]:
- """Homes whose gateway a stop/restart from here takes down, in order: this home's own gateway (a
- ``--force`` standalone one included), then the live host owner that serves it."""
- from hermes_cli import gateway as gw
+def _candidate_homes(system: bool = False) -> list[Path]:
+ """Homes whose gateway a stop/restart from here takes down, in order: the system unit's pinned home
+ (``sudo`` strips HERMES_HOME, so this process sees root's), this home's own gateway (a ``--force``
+ standalone one included), then the live host owner that serves it. Only READS the unit: a foreign
+ unit is skipped here and refused by the systemd verb before it adopts any home."""
+ from hermes_cli import gateway as gw, gateway_service_owner
homes = [gw.get_hermes_home()]
+ if (gw.supports_systemd_services() and gw._select_systemd_scope(system)
+ and gateway_service_owner.foreign_systemd_unit_home(True) is None
+ and (unit_home := gateway_service_owner.service_home_for_unit(gw.get_systemd_unit_path(system=True), True))
+ not in homes):
+ homes.insert(0, unit_home)
owner = gw._host_multiplexer_for_all_verb()
if owner is not None and Path(owner.home) not in homes:
homes.append(Path(owner.home))
@@ -84,7 +91,7 @@ def refusal_lines(verb: str, homes: list[Path], *, flag_hint: str = f"hermes gat
def refuse_while_sessions_live(verb: str, args) -> None:
"""Exit 1 naming the live work unless ``--interrupt`` was passed; with it, say what is interrupted."""
- lines = refusal_lines(verb, _candidate_homes())
+ lines = refusal_lines(verb, _candidate_homes(getattr(args, "system", False)))
if not lines:
return
from hermes_cli.gateway import print_error, print_warning
diff --git a/tests/hermes_cli/test_gateway_lifecycle_guards.py b/tests/hermes_cli/test_gateway_lifecycle_guards.py
index 7a8bb6d00a7..4c18d44b447 100644
--- a/tests/hermes_cli/test_gateway_lifecycle_guards.py
+++ b/tests/hermes_cli/test_gateway_lifecycle_guards.py
@@ -15,6 +15,7 @@ Every ``systemctl``/``launchctl``/kill seam is replaced by a recorder; nothing h
from __future__ import annotations
import os
+from pathlib import Path
from types import SimpleNamespace
import pytest
@@ -190,3 +191,36 @@ def test_dashboard_stop_refuses_while_sessions_are_live(monkeypatch):
assert client.post("/api/gateway/stop?interrupt=true").status_code == 200
assert spawned and spawned[0][-1] == "--interrupt"
+
+
+def test_sudo_system_stop_checks_the_units_home_not_roots(tmp_path, monkeypatch, recorded, capsys):
+ """``sudo hermes gateway stop --system``: HOME=/root and HERMES_HOME stripped, while the system unit
+ runs the invoking user's home. The live-work check must ask the unit's home, without adopting it."""
+ import hermes_constants
+ root_home, user_home = tmp_path / "root", tmp_path / "user" / ".hermes"
+ (root_home / ".hermes").mkdir(parents=True)
+ user_home.mkdir(parents=True)
+ _unit(tmp_path / "systemd" / "hermes-gateway.service", user_home)
+ monkeypatch.setattr(gw, "_SYSTEM_UNIT_DIR", tmp_path / "systemd")
+ monkeypatch.setattr(os, "geteuid", lambda: 0)
+ monkeypatch.delenv("SUDO_USER", raising=False)
+ monkeypatch.setenv("HOME", str(root_home))
+ monkeypatch.delenv("HERMES_HOME", raising=False)
+ monkeypatch.setattr(hermes_constants, "_account_home", lambda: root_home)
+ _live_gateway(monkeypatch, None)
+ from gateway import control_socket
+ monkeypatch.setattr(control_socket, "query_gateway_control", lambda home, verb, **kw: (
+ _WORK if verb == "active-work" and Path(home).resolve() == user_home.resolve() else None))
+ monkeypatch.setattr(gw, "_served_by_another_host_gateway", lambda: None)
+ monkeypatch.setattr(gw, "named_profile_served_by_running_multiplexer", lambda: False)
+ stopped: list = []
+ monkeypatch.setattr(gw, "_stop_installed_service", lambda system: stopped.append(system) or True)
+
+ with pytest.raises(SystemExit) as exc:
+ gw._cmd_stop(SimpleNamespace(all=False, system=True, interrupt=False))
+
+ assert exc.value.code == 1 and stopped == [] and recorded == []
+ assert "chat turn agent:main:telegram:dm:7" in capsys.readouterr().out
+ assert "HERMES_HOME" not in os.environ # the check never adopted the unit's home
+