The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): keep the unit anchor Linux-only and pin the order it depends on
JoaoMarcos441ff56d9ed9ec
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.pyindex 24a4eabc12c..aab7419a72e 100644--- a/hermes_cli/gateway.py+++ b/hermes_cli/gateway.py@@ -1978,13 +1978,19 @@ def _native_service_homes() -> set[Path]:def _bare_unit_pinned_home() -> Path | None:"""Resolved ``HERMES_HOME`` pinned by an installed ``hermes-gateway.service``, or None.- The installed unit is the authority on which home owns the BARE name: under ``sudo`` the naming- basis moves MID-COMMAND (sudo strips HERMES_HOME and sets HOME=/root, then- ``_sync_hermes_home_from_systemd_unit()`` adopts the unit's own HERMES_HOME into ``os.environ``),- so a basis derived from the process would name one unit before the adoption and another after it.- Reading it from the unit is stable for every elevated identity, including ``sudo -i`` and cron- where SUDO_USER is absent.+ Under ``sudo`` the process-derived naming basis moves MID-COMMAND: sudo strips HERMES_HOME and+ sets HOME=/root, then ``_sync_hermes_home_from_systemd_unit()`` adopts the unit's own HERMES_HOME+ into ``os.environ``, so a process-derived basis names one unit before the adoption and another+ after it. The installed unit is the only basis that holds still, and it holds for every elevated+ identity — ``sudo -i`` and cron included, where SUDO_USER is absent.++ Linux-gated because a systemd unit is not an identity authority for launchd labels, Windows+ scheduled tasks, or s6 slots, which share ``_profile_suffix()``. ``is_linux()`` is a plain+ ``sys.platform`` test; ``supports_systemd_services()`` would be wrong here, since it can shell out+ to ``systemctl is-system-running`` on WSL/containers and this runs on every name resolution."""+ if not is_linux():+ return Nonepinned = _hermes_home_pinned_by_unit(_SYSTEM_UNIT_DIR / f"{_SERVICE_BASE}.service")if not pinned:return None@@ -1999,12 +2005,16 @@ def _profile_suffix() -> str:``<root>/profiles/<name>``, else a short hash of the path.Bare-name owners: this process's platform-native default (``~/.hermes``), under sudo the invoking- user's native default, and the home pinned by an installed ``hermes-gateway.service``. The bare name- is deliberately NOT tied to ``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; a home with no installed bare unit and no native default keeps its- own suffix.+ user's native default, and the home pinned by an installed ``hermes-gateway.service``. The unit-pinned+ check must precede the profile branch: ``sudo hermes gateway install --system`` resolves the BARE name+ from root's default, then pins the invoking user's remapped home, so the bare unit legitimately carries+ a ``<root>/profiles/<name>`` home and must keep answering with the bare name it was installed under.++ The bare name is deliberately NOT tied to ``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; a home with no installed bare unit and+ no native default keeps its own suffix."""import hashlibfrom hermes_constants import get_default_hermes_rootdiff --git a/tests/hermes_cli/test_gateway_service.py b/tests/hermes_cli/test_gateway_service.pyindex cb1318c2a23..94afbbad3ef 100644--- a/tests/hermes_cli/test_gateway_service.py+++ b/tests/hermes_cli/test_gateway_service.py@@ -2775,8 +2775,13 @@ class TestTimeoutStopSecCoversCronFloor:class TestUnitAnchoredServiceIdentity:"""The installed ``hermes-gateway.service`` owns the bare name: under ``sudo`` the naming basis moves- mid-command when ``_sync_hermes_home_from_systemd_unit()`` adopts the unit's HERMES_HOME (#108674)."""+ mid-command when ``_sync_hermes_home_from_systemd_unit()`` adopts the unit's HERMES_HOME (#108674).+ ``linux_only`` because ``_bare_unit_pinned_home()`` is Linux-gated on purpose: a systemd unit is not+ an identity authority for launchd labels, Windows tasks, or s6 slots, which share the same resolver.+ """++ @pytest.mark.linux_onlydef test_service_name_survives_hermes_home_adoption_from_unit(self, tmp_path, monkeypatch):alice_home = tmp_path / "alice" / ".hermes"alice_home.mkdir(parents=True)@@ -2793,6 +2798,7 @@ class TestUnitAnchoredServiceIdentity:monkeypatch.setenv("HERMES_HOME", str(alice_home))assert gateway_cli.get_service_name() == pre_name+ @pytest.mark.linux_onlydef test_unit_pinned_home_owns_the_bare_name(self, tmp_path, monkeypatch):alice_home = tmp_path / "alice" / ".hermes"alice_home.mkdir(parents=True)@@ -2808,6 +2814,7 @@ class TestUnitAnchoredServiceIdentity:assert gateway_cli.get_service_name() == gateway_cli._SERVICE_BASEassert gateway_cli.get_systemd_unit_path(system=True) == unit_path+ @pytest.mark.linux_onlydef test_foreign_home_without_installed_unit_keeps_its_suffix(self, tmp_path, monkeypatch):unit_dir = tmp_path / "empty"unit_dir.mkdir()@@ -2822,6 +2829,7 @@ class TestUnitAnchoredServiceIdentity:assert name != gateway_cli._SERVICE_BASEassert name.startswith(gateway_cli._SERVICE_BASE + "-")+ @pytest.mark.linux_onlydef test_home_not_pinned_by_unit_keeps_its_suffix(self, tmp_path, monkeypatch):alice_home = tmp_path / "alice" / ".hermes"alice_home.mkdir(parents=True)@@ -2840,3 +2848,73 @@ class TestUnitAnchoredServiceIdentity:name = gateway_cli.get_service_name()assert name != gateway_cli._SERVICE_BASEassert name.startswith(gateway_cli._SERVICE_BASE + "-")++ @pytest.mark.linux_only+ def test_bare_unit_pinning_a_named_profile_home_keeps_the_bare_name(self, tmp_path, monkeypatch):+ """``sudo ... install --system`` names the unit from root's default but pins the invoking user's+ remapped home, so the BARE unit legitimately carries a ``profiles/<name>`` home. The unit-pinned+ check therefore has to win over the profile branch, which would answer ``-kimi`` for a unit that+ was installed bare."""+ profile_home = tmp_path / "alice" / ".hermes" / "profiles" / "kimi"+ profile_home.mkdir(parents=True)+ root_home = tmp_path / "root" / ".hermes"+ root_home.mkdir(parents=True)+ unit_dir = tmp_path / "systemd"+ unit_dir.mkdir()+ unit_path = unit_dir / f"{gateway_cli._SERVICE_BASE}.service"+ unit_path.write_text(f'[Service]\nEnvironment="HERMES_HOME={profile_home}"\n', encoding="utf-8")+ monkeypatch.setattr(gateway_cli, "_SYSTEM_UNIT_DIR", unit_dir, raising=False)+ monkeypatch.setattr(hermes_constants, "_get_platform_default_hermes_home", lambda: root_home)+ monkeypatch.setenv("HERMES_HOME", str(profile_home))+ assert gateway_cli.get_service_name() == gateway_cli._SERVICE_BASE+ # The profile branch, consulted against the home that owns the profile, would have answered+ # with the readable suffix -- which is why the unit-pinned check has to be evaluated first.+ assert gateway_cli._profile_name_from_home(profile_home, profile_home.parent.parent) == profile_home.name++ @pytest.mark.linux_only+ def test_real_unit_sync_keeps_the_name_it_validated(self, tmp_path, monkeypatch):+ """Drive the production sync instead of simulating the adoption with setenv: the name resolved+ before ``_sync_hermes_home_from_systemd_unit()`` must survive the mutation it performs."""+ alice_home = tmp_path / "alice" / ".hermes"+ alice_home.mkdir(parents=True)+ root_home = tmp_path / "root" / ".hermes"+ root_home.mkdir(parents=True)+ unit_dir = tmp_path / "systemd"+ unit_dir.mkdir()+ (unit_dir / f"{gateway_cli._SERVICE_BASE}.service").write_text(+ f'[Service]\nEnvironment="HERMES_HOME={alice_home}"\n', encoding="utf-8"+ )+ monkeypatch.setattr(gateway_cli, "_SYSTEM_UNIT_DIR", unit_dir, raising=False)+ monkeypatch.setattr(hermes_constants, "_get_platform_default_hermes_home", lambda: root_home)+ monkeypatch.delenv("HERMES_HOME", raising=False)++ pre_sync_name = gateway_cli.get_service_name()+ gateway_cli._sync_hermes_home_from_systemd_unit(system=True)++ assert os.environ["HERMES_HOME"] == str(alice_home) # the sync really ran+ assert gateway_cli.get_service_name() == pre_sync_name++ @pytest.mark.linux_only+ def test_unreadable_unit_does_not_grant_the_bare_name(self, tmp_path, monkeypatch):+ """A unit we cannot read must not hand its bare name to an unrelated home: the parser returns+ None on OSError, so resolution falls through to the suffix branches."""+ foreign_home = tmp_path / "elsewhere"+ foreign_home.mkdir()+ root_home = tmp_path / "root" / ".hermes"+ root_home.mkdir(parents=True)+ unit_dir = tmp_path / "systemd"+ unit_dir.mkdir()+ unit_path = unit_dir / f"{gateway_cli._SERVICE_BASE}.service"+ unit_path.write_text(f'[Service]\nEnvironment="HERMES_HOME={foreign_home}"\n', encoding="utf-8")+ real_read_text = Path.read_text++ def deny_unit(self, *args, **kwargs):+ if self == unit_path:+ raise PermissionError(13, "Permission denied")+ return real_read_text(self, *args, **kwargs)++ monkeypatch.setattr(gateway_cli, "_SYSTEM_UNIT_DIR", unit_dir, raising=False)+ monkeypatch.setattr(hermes_constants, "_get_platform_default_hermes_home", lambda: root_home)+ monkeypatch.setattr(Path, "read_text", deny_unit)+ monkeypatch.setenv("HERMES_HOME", str(foreign_home))+ assert gateway_cli.get_service_name().startswith(gateway_cli._SERVICE_BASE + "-")