The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(install-state): a system gateway counts only for the account it runs as
alt-glitch2c64fe6f6715
diff --git a/hermes_cli/install_state.py b/hermes_cli/install_state.pyindex 0116f507527..de121a625f8 100644--- a/hermes_cli/install_state.py+++ b/hermes_cli/install_state.py@@ -15,6 +15,7 @@ import contextlibimport enumimport jsonimport os+import reimport sqlite3import sysimport threading@@ -248,12 +249,35 @@ def _registry_home() -> Iterable[Path]:def _service_homes() -> Iterable[Path]:+ """The ``HERMES_HOME`` of every gateway service this OS user runs: its own definitions, and the+ machine-wide units that run as it. Another account's gateway is that account's install."""from hermes_cli.gateway_service_owner import service_definition_homesfrom hermes_platform.resolver import known_dirs- for definition in _expand_all(known_dirs.gateway_service_definition_globs()):- with contextlib.suppress(OSError, UnicodeError, ValueError):- yield from (Path(home.strip().strip('"')) for home in service_definition_homes(_read_text(definition)))+ for machine_wide, globs in ((False, known_dirs.gateway_service_definition_globs()),+ (True, known_dirs.system_gateway_service_definition_globs())):+ for definition in _expand_all(globs):+ with contextlib.suppress(OSError, UnicodeError, ValueError):+ text = _read_text(definition)+ account = _unit_account(text) or ("root" if machine_wide else None)+ if account is None or _is_this_account(account):+ yield from (Path(home.strip().strip('"')) for home in service_definition_homes(text))+++def _unit_account(unit: str) -> str | None:+ """The account a systemd unit runs as (its last ``User=``), or None when it names none."""+ accounts = re.findall(r"^\s*User=\s*(\S+)\s*$", unit, flags=re.MULTILINE)+ return accounts[-1] if accounts else None+++def _is_this_account(account: str) -> bool:+ import pwd++ try:+ uid = int(account) if account.isdigit() else pwd.getpwnam(account).pw_uid+ except KeyError: # no such account: the unit cannot run as anyone here+ return False+ return uid == os.getuid() # windows-footgun: ok — only systemd units name a User=def _launcher_root() -> Iterable[Path]:diff --git a/hermes_platform/resolver/known_dirs.py b/hermes_platform/resolver/known_dirs.pyindex fe0959eb2f7..f81046b392b 100644--- a/hermes_platform/resolver/known_dirs.py+++ b/hermes_platform/resolver/known_dirs.py@@ -55,13 +55,17 @@ def hermes_home_globs() -> tuple[str, ...]:def gateway_service_definition_globs() -> tuple[str, ...]:- """Gateway service definitions, each naming the HERMES_HOME it serves."""+ """This OS user's gateway service definitions, each naming the HERMES_HOME it serves."""if _WIN:return ("%APPDATA%/Microsoft/Windows/Start Menu/Programs/Startup/Hermes_Gateway*",)if sys.platform == "darwin":return ("~/Library/LaunchAgents/ai.hermes.gateway*.plist",)- return ("$XDG_CONFIG_HOME/systemd/user/hermes-gateway*.service", "~/.config/systemd/user/hermes-gateway*.service",- "/etc/systemd/system/hermes-gateway*.service")+ return ("$XDG_CONFIG_HOME/systemd/user/hermes-gateway*.service", "~/.config/systemd/user/hermes-gateway*.service")+++def system_gateway_service_definition_globs() -> tuple[str, ...]:+ """Machine-wide gateway services. Each runs as the account its ``User=`` names (root when none)."""+ return ("/etc/systemd/system/hermes-gateway*.service",) if _POSIX and sys.platform != "darwin" else ()def mac_privacy_protected_dirs() -> tuple[str, ...]:diff --git a/tests/hermes_cli/test_install_state.py b/tests/hermes_cli/test_install_state.pyindex 5555bccda70..4fbc9ba84a6 100644--- a/tests/hermes_cli/test_install_state.py+++ b/tests/hermes_cli/test_install_state.py@@ -140,6 +140,31 @@ def test_gateway_service_definitions_name_their_homes(tmp_path, monkeypatch):assert set(install_state._service_homes()) == {home, Path(f"{home}-win")}[email protected]("posix")+def test_a_gateway_service_counts_only_for_the_account_it_runs_as(tmp_path, monkeypatch):+ """A machine-wide unit is another account's install unless it runs as this one (``User=``, root+ when it names none): that account's gateway must not cost a new desktop user the trial."""+ import getpass++ from hermes_platform.resolver import known_dirs++ me = getpass.getuser()+ units = {"mine": f"User={me}\n", "theirs": "User=nobody\n" if me != "nobody" else "User=root\n", "root": ""}+ for name, identity in units.items():+ (tmp_path / f"hermes-gateway-{name}.service").write_text(+ f'[Service]\n{identity}Environment="HERMES_HOME={tmp_path / name}"\n', encoding="utf-8")+ monkeypatch.setattr(known_dirs, "gateway_service_definition_globs",+ lambda: (str(tmp_path / "hermes-gateway-[mt]*.service"),))+ monkeypatch.setattr(known_dirs, "system_gateway_service_definition_globs",+ lambda: (str(tmp_path / "hermes-gateway-root.service"),), raising=False)++ homes = set(install_state._service_homes())++ assert tmp_path / "mine" in homes+ assert tmp_path / "theirs" not in homes+ assert (tmp_path / "root" in homes) == (me == "root")++def test_a_state_db_another_process_holds_locked_is_not_use(machine, monkeypatch):other, _places = machinedb = other / "state.db"