The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(gateway): keep the service-owner fix to the writers
Teknium245ce8e0ffcc
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.pyindex 42797a3d9fa..57587df1dbd 100644--- a/hermes_cli/gateway.py+++ b/hermes_cli/gateway.py@@ -5291,10 +5291,6 @@ def _cmd_install(args):force_unit_path = getattr(args, "force_unit_path", False)if backend == "systemd" and refuses_container_user_scope_install(system):sys.exit(1)- if backend in ("systemd", "launchd"):- from hermes_cli.gateway_service_owner import refuse_foreign_home_install- if refuse_foreign_home_install(get_hermes_home(), force_unit_path):- sys.exit(1)if backend == "systemd":_install_systemd_from_cli(args, force=force, system=system, run_as_user=run_as_user)elif backend == "launchd":diff --git a/hermes_cli/gateway_service_owner.py b/hermes_cli/gateway_service_owner.pyindex 14ce14d4e70..1c48caaf737 100644--- a/hermes_cli/gateway_service_owner.py+++ b/hermes_cli/gateway_service_owner.py@@ -80,43 +80,3 @@ def definition_belongs_to_home(definition_path: Path, home: Path, action: str) -print(" That file is another install's gateway. Use that install, or pass --force-unit-path")print(" to `hermes gateway install` if you really mean to repoint it at this home.")return False---def _installed_definition_pins(home: Path) -> bool:- """True when a definition at this home's service path already pins *home* (a re-install)."""- from hermes_cli import gateway as gw-- paths = [gw.get_systemd_unit_path(system=False), gw.get_systemd_unit_path(system=True)]- if gw.is_macos():- paths.append(gw.get_launchd_plist_path())- for path in paths:- raw = pinned_home(path) if path.exists() else None- if raw is not None and _resolve(raw) == home:- return True- return False---def home_may_install_service(home: Path) -> bool:- """True for a home that may own a host gateway service: inside the platform-default Hermes tree- (``~/.hermes`` and its profiles, the sudo invoker's included), the bare-name owner, or a home whose- installed definition already pins it."""- from hermes_cli import gateway as gw-- resolved = _resolve(home)- if resolved is None:- return False- if any(resolved == root or root in resolved.parents for root in gw._native_service_homes()):- return True- return gw._home_owns_bare_service_name(resolved) or _installed_definition_pins(resolved)---def refuse_foreign_home_install(home: Path, force_unit_path: bool) -> bool:- """True (with guidance printed) when ``hermes gateway install`` must not register a service for *home*."""- if force_unit_path or home_may_install_service(home):- return False- print(f"✗ Refusing to install a gateway service for HERMES_HOME={home}.")- print(" It is outside this account's Hermes tree and no service is installed for it yet, so it looks")- print(" like a scratch / test / E2E home. A service installed for it would outlive that run and restart")- print(" a gateway on this host forever. Run that gateway in the foreground (`hermes gateway run`),")- print(" or pass --force-unit-path if this custom home really is meant to own a host service.")- return Truediff --git a/hermes_cli/subcommands/gateway.py b/hermes_cli/subcommands/gateway.pyindex d67b26fb162..ca4ef776db8 100644--- a/hermes_cli/subcommands/gateway.py+++ b/hermes_cli/subcommands/gateway.py@@ -134,8 +134,7 @@ def build_gateway_parser(_flag(gateway_install, "--if-missing", dest="if_missing",help="Do nothing when a gateway service is already installed")_flag(gateway_install, "--force-unit-path", dest="force_unit_path",- help="Install for a HERMES_HOME outside this account's Hermes tree, or repoint a service "- "definition that pins another home")+ help="Repoint a service definition that pins another HERMES_HOME at this one")_flag(gateway_install, "--elevated-handoff", dest="elevated_handoff", help=argparse.SUPPRESS)gateway_uninstall = gateway_subparsers.add_parser("uninstall", help="Uninstall gateway service")diff --git a/tests/hermes_cli/test_gateway.py b/tests/hermes_cli/test_gateway.pyindex 9a80be91c87..1412a113136 100644--- a/tests/hermes_cli/test_gateway.py+++ b/tests/hermes_cli/test_gateway.py@@ -1136,10 +1136,7 @@ def test_install_if_missing_only_installs_when_no_service_exists(monkeypatch, inmonkeypatch.setattr(gateway, "_guard_named_profile_under_multiplexer", lambda force: None)monkeypatch.setattr(gateway, "_service_mgmt_blocked", lambda: False)monkeypatch.setattr(gateway, "_service_backend", lambda: "launchd")- # The sandbox HERMES_HOME is a scratch home, which `gateway install` refuses by design.- monkeypatch.setattr("hermes_cli.gateway_service_owner.home_may_install_service", lambda home: True)- monkeypatch.setattr(gateway, "launchd_install",- lambda force, start_now, force_unit_path=False: installs.append(force))+ monkeypatch.setattr(gateway, "launchd_install", lambda force, start_now, force_unit_path=False: installs.append(force))gateway._cmd_install(SimpleNamespace(if_missing=True, force=False, system=False, run_as_user=None))diff --git a/tests/hermes_cli/test_gateway_multiplex_served_record.py b/tests/hermes_cli/test_gateway_multiplex_served_record.pyindex ff4f362c961..ecc309eb87c 100644--- a/tests/hermes_cli/test_gateway_multiplex_served_record.py+++ b/tests/hermes_cli/test_gateway_multiplex_served_record.py@@ -161,8 +161,6 @@ def test_service_verbs_do_not_start_a_second_gateway(served_root, monkeypatch, vmonkeypatch.setattr(gw, "_installed_service_kind_for", lambda *a, **k: "systemd")monkeypatch.setattr(gw, "is_managed", lambda: False)monkeypatch.setattr(gw, "is_termux", lambda: False)- # The served root is a tmp dir, which `gateway install` refuses as a scratch home by design.- monkeypatch.setattr("hermes_cli.gateway_service_owner.home_may_install_service", lambda home: True)fn = getattr(gw, f"_cmd_{verb}")ns = argparse.Namespace(system=False, all=False, force=False, run_as_user=None)if verb == "restart":diff --git a/tests/hermes_cli/test_gateway_no_new_standalone_profile.py b/tests/hermes_cli/test_gateway_no_new_standalone_profile.pyindex 5881f52c67e..356f32a3da2 100644--- a/tests/hermes_cli/test_gateway_no_new_standalone_profile.py+++ b/tests/hermes_cli/test_gateway_no_new_standalone_profile.py@@ -39,8 +39,6 @@ def quiet_host(tmp_path, monkeypatch):monkeypatch.setattr(gw, "_dispatch_via_service_manager_if_s6", lambda v: False)monkeypatch.setattr(gw, "is_managed", lambda: False)monkeypatch.setattr(gw, "is_termux", lambda: False)- # tmp root is outside the platform-default tree, which `gateway install` refuses by design.- monkeypatch.setattr("hermes_cli.gateway_service_owner.home_may_install_service", lambda home: True)calls: list[str] = []monkeypatch.setattr(gw, "_install_systemd_from_cli", lambda *a, **k: calls.append("install"))monkeypatch.setattr(gw, "_service_call", lambda backend, v, system: calls.append(v))diff --git a/tests/hermes_cli/test_gateway_service.py b/tests/hermes_cli/test_gateway_service.pyindex eacd7e58d66..2850557cc71 100644--- a/tests/hermes_cli/test_gateway_service.py+++ b/tests/hermes_cli/test_gateway_service.py@@ -2071,8 +2071,6 @@ class TestDockerAwareGateway:monkeypatch.setattr(gateway_cli, "is_termux", lambda: False)monkeypatch.setattr(gateway_cli, "supports_systemd_services", lambda: True)monkeypatch.setattr(gateway_cli, "is_container", lambda: True)- # The sandbox HERMES_HOME is a scratch home, which `gateway install` refuses by design.- monkeypatch.setattr("hermes_cli.gateway_service_owner.home_may_install_service", lambda home: True)calls = []monkeypatch.setattr(gateway_cli,diff --git a/tests/hermes_cli/test_gateway_service_owner.py b/tests/hermes_cli/test_gateway_service_owner.pyindex a9a039f951b..92bc7f4cc86 100644--- a/tests/hermes_cli/test_gateway_service_owner.py+++ b/tests/hermes_cli/test_gateway_service_owner.py@@ -1,231 +1,58 @@"""Gateway service-definition writes belong to the home the definition pins.-Two homes can resolve to one service name: ``<root>/profiles/<name>`` and ``~/.hermes/profiles/<name>``-both take ``hermes-gateway-<name>``. Every gateway boot refreshes "its" unit, so a scratch or E2E gateway-started from such a home rewrote the real install's unit (WorkingDirectory and HERMES_HOME pointed at the-scratch dir) and the real gateway failed on its next restart. The collision is simulated here by pointing-the unit/plist path at a file that pins another home; ``test_colliding_profile_names_share_a_unit`` proves-the collision itself on the real name resolution.+Every gateway boot refreshes "its" unit, and a scratch/E2E process whose service name resolves to the+real install's unit (a fake HERMES_HOME under ~/.hermes/cache/scratch takes the bare name, and+``<root>/profiles/x`` collides with ``~/.hermes/profiles/x``) rewrote that unit to point at the scratch+home. The real gateway then crash-looped on its next restart."""-import argparse-import plistlib-from pathlib import Pathfrom types import SimpleNamespaceimport pytestimport hermes_cli.gateway as gw-from hermes_cli import gateway_launchd, gateway_service_owner-from hermes_cli.subcommands.gateway import build_gateway_parser-REAL_UNIT = (+UNIT = ("[Service]\n"- "ExecStart=/home/ace/.hermes/hermes-agent/venv/bin/python -m hermes_cli.main gateway run\n"+ "ExecStart=/home/ace/.hermes/hermes-agent/.hermes/bin/hermes gateway run\n""WorkingDirectory={home}\n"'Environment="HERMES_HOME={home}"\n')@pytest.fixture-def homes(tmp_path, monkeypatch):- real = tmp_path / "account" / ".hermes" / "profiles" / "coder"- scratch = tmp_path / "ci-scratch" / "run-1" / "profiles" / "coder"+def unit(tmp_path, monkeypatch):+ real = tmp_path / "account" / ".hermes"+ scratch = tmp_path / "scratch" / "fakehome" / ".hermes"real.mkdir(parents=True)scratch.mkdir(parents=True)- # The process is the scratch gateway. A scratch dir is not always under a temp root (CI scratch- # volumes), so the temp-home guard is out of the way: the ownership check is what is under test.- monkeypatch.setenv("HERMES_HOME", str(scratch))- monkeypatch.setattr(gw, "_refuse_temp_home_service_write", lambda definition, kind: False)- return SimpleNamespace(real=real, scratch=scratch)---def test_colliding_profile_names_share_a_unit(homes, tmp_path, monkeypatch):- """The premise: a custom root's profile resolves to the same unit as the account's profile."""- monkeypatch.setattr(gw, "_bare_unit_pinned_home", lambda: None)- monkeypatch.setattr(gw, "user_systemd_unit_dir", lambda: tmp_path / "systemd-user")- monkeypatch.setattr(gw, "_native_service_homes", lambda: {(tmp_path / "account" / ".hermes").resolve()})- scratch_unit = gw.get_systemd_unit_path(system=False)- monkeypatch.setenv("HERMES_HOME", str(homes.real))- assert gw.get_systemd_unit_path(system=False) == scratch_unit---def systemd_unit(tmp_path, homes, monkeypatch):- unit = tmp_path / "systemd" / "hermes-gateway-coder.service"- unit.parent.mkdir()- unit.write_text(REAL_UNIT.format(home=homes.real), encoding="utf-8")+ path = tmp_path / "systemd" / "hermes-gateway.service"+ path.parent.mkdir()+ path.write_text(UNIT.format(home=real), encoding="utf-8")calls = []- monkeypatch.setattr(gw, "get_systemd_unit_path", lambda system=False: unit)+ # The temp-root guard only sees homes under a temp dir; scratch homes elsewhere are what slipped past.+ monkeypatch.setattr(gw, "_refuse_temp_home_service_write", lambda definition, kind: False)+ monkeypatch.setattr(gw, "get_systemd_unit_path", lambda system=False: path)monkeypatch.setattr(gw, "systemd_unit_is_current", lambda system=False: False)monkeypatch.setattr(gw, "_retire_hermes_replace_dropin", lambda system=False: False)monkeypatch.setattr(gw, "_prepare_service_launcher", lambda system=False, run_as_user=None: None)+ monkeypatch.setattr(gw, "generate_systemd_unit", lambda system=False, run_as_user=None: "ExecStart=new\n")monkeypatch.setattr(gw, "_run_systemctl", lambda args, **kw: calls.append(tuple(args)) orSimpleNamespace(returncode=0, stdout="", stderr=""))- monkeypatch.setattr(gw, "has_legacy_hermes_units", lambda: False)- monkeypatch.setattr(gw, "_ensure_linger_enabled", lambda *a, **k: True)- monkeypatch.setattr(gw, "_ensure_system_service_linger", lambda *a, **k: None)- monkeypatch.setattr(gw, "print_systemd_scope_conflict_warning", lambda: None)- monkeypatch.setattr(gw, "print_legacy_unit_warning", lambda: None)- monkeypatch.setattr(gw, "generate_systemd_unit", lambda system=False, run_as_user=None:- REAL_UNIT.format(home=Path(gw.get_hermes_home())))- return SimpleNamespace(path=unit, calls=calls, original=unit.read_text(encoding="utf-8"))---class TestSystemdWriters:- def test_boot_refresh_leaves_a_unit_pinned_to_another_home_untouched(self, systemd_unit, capsys):- assert gw.refresh_systemd_unit_if_needed(system=False) is False- assert systemd_unit.path.read_text(encoding="utf-8") == systemd_unit.original- assert ("daemon-reload",) not in systemd_unit.calls- assert "Refusing to rewrite" in capsys.readouterr().out-- @pytest.fixture- def marker_free_unit(self, monkeypatch):- # refresh's own test belt refuses a generated unit naming a pytest tmpdir; any marker-free body works.- monkeypatch.setattr(gw, "generate_systemd_unit", lambda system=False, run_as_user=None: "ExecStart=new\n")-- def test_boot_refresh_still_rewrites_its_own_stale_unit(self, systemd_unit, homes, marker_free_unit,- monkeypatch):- monkeypatch.setenv("HERMES_HOME", str(homes.real))- assert gw.refresh_systemd_unit_if_needed(system=False) is True- assert systemd_unit.path.read_text(encoding="utf-8") == "ExecStart=new\n"- assert ("daemon-reload",) in systemd_unit.calls-- def test_unpinned_unit_is_nobody_elses(self, systemd_unit, marker_free_unit):- systemd_unit.path.write_text("[Service]\nExecStart=/usr/bin/hermes gateway run\n", encoding="utf-8")- assert gw.refresh_systemd_unit_if_needed(system=False) is True-- def test_force_install_does_not_overwrite_another_homes_unit(self, systemd_unit):- with pytest.raises(SystemExit) as exc:- gw.systemd_install(force=True, non_interactive=True)- assert exc.value.code == 1- assert systemd_unit.path.read_text(encoding="utf-8") == systemd_unit.original- assert systemd_unit.calls == []-- def test_refused_install_never_starts_the_other_homes_service(self, systemd_unit, monkeypatch):- started = []- monkeypatch.setattr(gw, "systemd_start", lambda system=False: started.append(system))- args = SimpleNamespace(start_now=True, start_on_login=True, force_unit_path=False)- with pytest.raises(SystemExit):- gw._install_systemd_from_cli(args, force=False, system=False, run_as_user=None)- assert started == []-- def test_force_unit_path_repoints_the_unit(self, systemd_unit, homes):- gw.systemd_install(force_unit_path=True, non_interactive=True)- assert f"HERMES_HOME={homes.scratch}" in systemd_unit.path.read_text(encoding="utf-8")-- def test_system_refresh_checks_the_callers_home_before_adopting_the_units(- self, systemd_unit, homes, monkeypatch):- # systemd_unit_is_current adopts a system unit's HERMES_HOME into os.environ (sudo strips it);- # checking after that adoption compared the unit with itself.- monkeypatch.setattr(gw, "_read_systemd_user_from_unit", lambda path: None)-- def is_current(system=False):- gw._sync_hermes_home_from_systemd_unit(system=system)- return False- monkeypatch.setattr(gw, "systemd_unit_is_current", is_current)- assert gw.refresh_systemd_unit_if_needed(system=True) is False- assert systemd_unit.path.read_text(encoding="utf-8") == systemd_unit.original- assert Path(gw.get_hermes_home()) == homes.scratch-- def test_sudo_stripped_home_still_refreshes_the_system_unit(self, systemd_unit, homes, marker_free_unit,- monkeypatch):- # Under sudo there is no explicit HERMES_HOME: the unit's own pinned home names the install.- monkeypatch.delenv("HERMES_HOME")- monkeypatch.setattr(gw, "_read_systemd_user_from_unit", lambda path: None)- assert gw.refresh_systemd_unit_if_needed(system=True) is True---class TestLaunchdWriters:- @pytest.fixture- def plist(self, tmp_path, homes, monkeypatch):- path = tmp_path / "LaunchAgents" / "ai.hermes.gateway-coder.plist"- path.parent.mkdir()- data = {"Label": "ai.hermes.gateway-coder", "EnvironmentVariables": {"HERMES_HOME": str(homes.real)}}- path.write_bytes(plistlib.dumps(data))- monkeypatch.setattr(gw, "get_launchd_plist_path", lambda: path)- monkeypatch.setattr(gw, "get_launchd_label", lambda: "ai.hermes.gateway-coder")- monkeypatch.setattr(gw, "launchd_plist_is_current", lambda: False)- monkeypatch.setattr(gw, "generate_launchd_plist", lambda: "<plist>new</plist>")- monkeypatch.setattr(gw, "_prepare_service_launcher", lambda *a, **k: None)- monkeypatch.setattr(gw, "_launchctl_label_supervising_process", lambda label: None)- monkeypatch.setattr(gateway_launchd.subprocess, "run", lambda *a, **k: SimpleNamespace(returncode=0))- return SimpleNamespace(path=path, original=path.read_bytes())-- def test_refresh_leaves_a_plist_pinned_to_another_home_untouched(self, plist):- assert gateway_launchd.refresh_launchd_plist_if_needed() is False- assert plist.path.read_bytes() == plist.original-- def test_force_install_does_not_overwrite_another_homes_plist(self, plist):- with pytest.raises(SystemExit):- gateway_launchd.launchd_install(force=True, start_now=False)- assert plist.path.read_bytes() == plist.original-- def test_force_unit_path_repoints_the_plist(self, plist):- gateway_launchd.launchd_install(start_now=False, force_unit_path=True)- assert plist.path.read_text(encoding="utf-8") == "<plist>new</plist>"---class TestInstallAdmission:- """`gateway install` registers a persistent host service; a scratch/E2E home must not get one."""-- @pytest.fixture- def install_cli(self, homes, tmp_path, monkeypatch):- installs = []- monkeypatch.setattr(gw, "is_managed", lambda: False)- monkeypatch.setattr(gw, "_service_mgmt_blocked", lambda: False)- monkeypatch.setattr(gw, "_guard_named_profile_under_multiplexer", lambda force: None)- monkeypatch.setattr(gw, "_service_backend", lambda: "launchd")- monkeypatch.setattr(gw, "is_macos", lambda: True)- monkeypatch.setattr(gw, "_native_service_homes", lambda: {(tmp_path / "account" / ".hermes").resolve()})- monkeypatch.setattr(gw, "_bare_unit_pinned_home", lambda: None)- monkeypatch.setattr(gw, "get_systemd_unit_path", lambda system=False: tmp_path / "none.service")- monkeypatch.setattr(gw, "get_launchd_plist_path", lambda: tmp_path / "none.plist")- monkeypatch.setattr(gw, "launchd_install", lambda force, start_now, force_unit_path=False:- installs.append(force_unit_path))- return installs-- @staticmethod- def _args(**kw):- return SimpleNamespace(if_missing=False, force=False, system=False, run_as_user=None, **kw)-- def test_scratch_home_is_refused(self, install_cli):- with pytest.raises(SystemExit) as exc:- gw._cmd_install(self._args(force_unit_path=False))- assert exc.value.code == 1- assert install_cli == []-- def test_force_unit_path_admits_a_custom_home(self, install_cli):- gw._cmd_install(self._args(force_unit_path=True))- assert install_cli == [True]-- def test_account_profile_is_admitted(self, install_cli, homes, monkeypatch):- monkeypatch.setenv("HERMES_HOME", str(homes.real))- gw._cmd_install(self._args(force_unit_path=False))- assert install_cli == [False]+ return SimpleNamespace(path=path, real=real, scratch=scratch, calls=calls,+ original=path.read_text(encoding="utf-8"))- def test_custom_home_with_its_own_installed_service_is_admitted(self, install_cli, homes, tmp_path,- monkeypatch):- own = tmp_path / "own.plist"- own.write_bytes(plistlib.dumps({"EnvironmentVariables": {"HERMES_HOME": str(homes.scratch)}}))- monkeypatch.setattr(gw, "get_launchd_plist_path", lambda: own)- gw._cmd_install(self._args(force_unit_path=False))- assert install_cli == [False]- def test_parser_accepts_force_unit_path(self):- parser = argparse.ArgumentParser()- build_gateway_parser(parser.add_subparsers(dest="command"), cmd_gateway=lambda a: None,- cmd_proxy=lambda a: None, cmd_gateway_enroll=lambda a: None)- assert parser.parse_args(["gateway", "install", "--force-unit-path"]).force_unit_path is True+def test_refresh_from_another_home_leaves_the_unit_untouched(unit, monkeypatch, capsys):+ monkeypatch.setenv("HERMES_HOME", str(unit.scratch))+ assert gw.refresh_systemd_unit_if_needed(system=False) is False+ assert unit.path.read_text(encoding="utf-8") == unit.original+ assert ("daemon-reload",) not in unit.calls+ assert "Refusing to rewrite" in capsys.readouterr().out-def test_pinned_home_reads_both_definition_kinds(tmp_path):- unit = tmp_path / "g.service"- unit.write_text('[Service]\nEnvironment="HERMES_HOME=/srv/a"\n', encoding="utf-8")- plist = tmp_path / "g.plist"- plist.write_bytes(plistlib.dumps({"EnvironmentVariables": {"HERMES_HOME": "/srv/b"}}))- broken = tmp_path / "broken.plist"- broken.write_text("not a plist", encoding="utf-8")- assert gateway_service_owner.pinned_home(unit) == "/srv/a"- assert gateway_service_owner.pinned_home(plist) == "/srv/b"- assert gateway_service_owner.pinned_home(broken) is None+def test_refresh_from_the_pinned_home_still_rewrites_a_stale_unit(unit, monkeypatch):+ monkeypatch.setenv("HERMES_HOME", str(unit.real))+ assert gw.refresh_systemd_unit_if_needed(system=False) is True+ assert unit.path.read_text(encoding="utf-8") == "ExecStart=new\n"+ assert ("daemon-reload",) in unit.callsdiff --git a/tests/hermes_cli/test_launchd_install_start_now.py b/tests/hermes_cli/test_launchd_install_start_now.pyindex 2e208f1c4e9..7ead1f1b13d 100644--- a/tests/hermes_cli/test_launchd_install_start_now.py+++ b/tests/hermes_cli/test_launchd_install_start_now.py@@ -41,8 +41,6 @@ def launchd(tmp_path, monkeypatch):monkeypatch.setattr(gateway_cli, "get_launchd_label", lambda: LABEL)monkeypatch.setattr(gateway_cli, "_launchd_domain", lambda: DOMAIN)monkeypatch.setattr(gateway_cli, "_refuse_temp_home_service_write", lambda *a: False)- # The sandbox HERMES_HOME is a scratch home, which `gateway install` refuses by design.- monkeypatch.setattr("hermes_cli.gateway_service_owner.home_may_install_service", lambda home: True)monkeypatch.setattr(gateway_cli, "_clear_launchd_unsupported_marker", lambda: None)monkeypatch.setattr(gateway_cli, "_launchctl_supervised_pid", lambda label: state.supervised_pid)monkeypatch.setattr(gateway_cli, "_launchctl_bootstrap", lambda *a, **k: state.bootstraps.append(a))