The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): preserve sudo system service name
Huklafdb9b50a1549
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.pyindex 2032d427db9..446fefe3106 100644--- a/hermes_cli/gateway.py+++ b/hermes_cli/gateway.py@@ -1950,19 +1950,41 @@ def _profile_name_from_home(home: Path, default: Path) -> str | None:return None+def _native_service_homes() -> set[Path]:+ """Homes that own the bare native gateway service name in this process."""+ from hermes_constants import _get_platform_default_hermes_home+ from pathlib import Path as _Path++ homes = {_get_platform_default_hermes_home().resolve()}+ if getattr(os, "geteuid", lambda: -1)() != 0:+ return homes++ sudo_user = os.environ.get("SUDO_USER", "").strip()+ if not sudo_user or sudo_user == "root":+ return homes+ try:+ import pwd++ homes.add((_Path(pwd.getpwnam(sudo_user).pw_dir) / ".hermes").resolve())+ except (ImportError, KeyError, AttributeError, TypeError):+ pass+ return homes++def _profile_suffix() -> str:- """Service-name suffix for HERMES_HOME: "" for the platform-native default home (``~/.hermes``), the- profile name for ``<root>/profiles/<name>``, else a short hash of the path.-- The bare name is reserved for the NATIVE default, not ``get_default_hermes_root()``: that helper- treats any HERMES_HOME outside ``~/.hermes`` (Docker ``/opt/data``, a temp dir) as "the root itself",- which let a temp-home harness resolve to the default profile's ``hermes-gateway`` unit and uninstall- the production gateway. Service names are host-wide identities; only the real default home owns the- bare one."""+ """Service-name suffix for HERMES_HOME: "" for a native default home (``~/.hermes``), the profile+ name for ``<root>/profiles/<name>``, else a short hash of the path.++ The bare name is reserved for the process user's native default and, under sudo, the invoking user's+ native default. It deliberately does not use ``get_default_hermes_root()``: that helper treats any+ HERMES_HOME outside ``~/.hermes`` (Docker ``/opt/data``, a temp dir) as "the root itself", which let a+ temp-home harness resolve to the default profile's ``hermes-gateway`` unit and uninstall the+ production gateway. Service names are host-wide identities; only real default homes own the bare+ one."""import hashlib- from hermes_constants import _get_platform_default_hermes_home, get_default_hermes_root+ from hermes_constants import get_default_hermes_roothome = get_hermes_home().resolve()- if home == _get_platform_default_hermes_home().resolve():+ if home in _native_service_homes():return ""name = _profile_name_from_home(home, get_default_hermes_root().resolve())return name or hashlib.sha256(str(home).encode()).hexdigest()[:8]diff --git a/tests/hermes_cli/test_gateway_service.py b/tests/hermes_cli/test_gateway_service.pyindex 52e6b761e39..878a1780f60 100644--- a/tests/hermes_cli/test_gateway_service.py+++ b/tests/hermes_cli/test_gateway_service.py@@ -217,6 +217,40 @@ class TestServiceIdentityForForeignHome:monkeypatch.setenv("HERMES_HOME", str(default_home / "profiles" / "alpha"))assert gateway_cli.get_service_name() == "hermes-gateway-alpha"+ def test_sudo_user_default_home_keeps_bare_service_name(self, machine_home, tmp_path, monkeypatch):+ sudo_home = tmp_path / "alice"+ sudo_default = sudo_home / ".hermes"+ sudo_default.mkdir(parents=True)+ monkeypatch.setattr(os, "geteuid", lambda: 0)+ monkeypatch.setenv("SUDO_USER", "alice")+ monkeypatch.setattr(pwd, "getpwnam", lambda user: SimpleNamespace(pw_dir=str(sudo_home)))++ # Before unit sync, sudo resolves the root process's native home.+ monkeypatch.delenv("HERMES_HOME", raising=False)+ assert gateway_cli.get_service_name() == "hermes-gateway"++ # After unit sync, HERMES_HOME points at the invoking user's native home.+ monkeypatch.setenv("HERMES_HOME", str(sudo_default))+ assert gateway_cli.get_service_name() == "hermes-gateway"++ def test_sudo_user_named_and_custom_homes_remain_distinct(+ self, machine_home, tmp_path, monkeypatch+ ):+ sudo_home = tmp_path / "alice"+ named_home = sudo_home / ".hermes" / "profiles" / "alpha"+ custom_home = tmp_path / "custom-hermes"+ named_home.mkdir(parents=True)+ custom_home.mkdir()+ monkeypatch.setattr(os, "geteuid", lambda: 0)+ monkeypatch.setenv("SUDO_USER", "alice")+ monkeypatch.setattr(pwd, "getpwnam", lambda user: SimpleNamespace(pw_dir=str(sudo_home)))++ monkeypatch.setenv("HERMES_HOME", str(named_home))+ assert gateway_cli.get_service_name() == "hermes-gateway-alpha"++ monkeypatch.setenv("HERMES_HOME", str(custom_home))+ assert gateway_cli.get_service_name() not in {"hermes-gateway", "hermes-gateway-alpha"}+class TestUninstallRefusesForeignUnit:"""systemd_uninstall must not stop/disable/unlink a unit pinned to another HERMES_HOME."""