Skip to content

The agent that grows with you

fix(gateway): foreign HERMES_HOME no longer resolves to the default hermes-gateway unit

Teknium4746e34448b4
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py
index 57f0b87b725..782fc18fea3 100644
--- a/hermes_cli/gateway.py
+++ b/hermes_cli/gateway.py
@@ -1949,16 +1949,21 @@ def _profile_name_from_home(home: Path, default: Path) -> str | None:
def _profile_suffix() -> str:
- """Service-name suffix for HERMES_HOME: "" for the default root, the profile name for
- ``<root>/profiles/<name>``, else a short hash of the path."""
+ """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."""
import hashlib
- from hermes_constants import get_default_hermes_root
+ from hermes_constants import _get_platform_default_hermes_home, get_default_hermes_root
home = get_hermes_home().resolve()
- default = get_default_hermes_root().resolve()
- if home == default:
+ if home == _get_platform_default_hermes_home().resolve():
return ""
- # Fallback: short hash for arbitrary HERMES_HOME paths
- return _profile_name_from_home(home, default) or hashlib.sha256(str(home).encode()).hexdigest()[:8]
+ name = _profile_name_from_home(home, get_default_hermes_root().resolve())
+ return name or hashlib.sha256(str(home).encode()).hexdigest()[:8]
def _profile_arg(hermes_home: str | None = None, default_root: str | Path | None = None) -> str:
diff --git a/tests/hermes_cli/test_gateway_service.py b/tests/hermes_cli/test_gateway_service.py
index 0ef1687906c..da3d2659bd3 100644
--- a/tests/hermes_cli/test_gateway_service.py
+++ b/tests/hermes_cli/test_gateway_service.py
@@ -186,6 +186,38 @@ class TestRequireServiceInstalled:
gateway_cli._require_service_installed("start")
+class TestServiceIdentityForForeignHome:
+ """A HERMES_HOME that is neither ``~/.hermes`` nor ``~/.hermes/profiles/<name>`` must never resolve to
+ the default profile's ``hermes-gateway`` unit (a temp-home harness uninstalled the production gateway)."""
+
+ @pytest.fixture
+ def machine_home(self, tmp_path, monkeypatch):
+ home = tmp_path / "home"
+ home.mkdir()
+ monkeypatch.setattr(Path, "home", lambda: home)
+ return home
+
+ def test_foreign_home_gets_its_own_unit(self, machine_home, tmp_path, monkeypatch):
+ foreign = tmp_path / "elsewhere"
+ foreign.mkdir()
+ monkeypatch.setenv("HERMES_HOME", str(foreign))
+
+ default_unit = machine_home / ".config" / "systemd" / "user" / "hermes-gateway.service"
+ assert gateway_cli.get_service_name() != "hermes-gateway"
+ assert gateway_cli.get_systemd_unit_path() != default_unit
+ assert gateway_cli.get_systemd_unit_path().parent == default_unit.parent
+
+ def test_default_and_named_profile_homes_keep_their_names(self, machine_home, monkeypatch):
+ default_home = machine_home / ".hermes"
+ (default_home / "profiles" / "alpha").mkdir(parents=True)
+
+ monkeypatch.setenv("HERMES_HOME", str(default_home))
+ assert gateway_cli.get_service_name() == "hermes-gateway"
+
+ monkeypatch.setenv("HERMES_HOME", str(default_home / "profiles" / "alpha"))
+ assert gateway_cli.get_service_name() == "hermes-gateway-alpha"
+
+
class TestGetCronDrainTimeout:
def test_missing_config_falls_back_to_default(self, monkeypatch):
monkeypatch.delenv("HERMES_CRON_DRAIN_TIMEOUT", raising=False)