Skip to content

The agent that grows with you

fix(profiles): delete/rename never removes another install's gateway service

Tekniumf6718a57c48a
diff --git a/hermes_cli/profiles.py b/hermes_cli/profiles.py
index b1662b81ad7..f81e091e64d 100644
--- a/hermes_cli/profiles.py
+++ b/hermes_cli/profiles.py
@@ -1924,14 +1924,14 @@ def _cleanup_gateway_service(name: str, profile_dir: Path) -> bool:
def _run(*cmd: str) -> None:
subprocess.run(list(cmd), capture_output=True, check=False, timeout=10)
- from hermes_cli.profiles_service_cleanup import remove_system_systemd_unit, remove_windows_task
+ from hermes_cli.profiles_service_cleanup import owned_by_profile, remove_system_systemd_unit, remove_windows_task
system = _platform.system()
removed = False
if system == "Linux":
svc_name = get_service_name()
svc_file = user_systemd_unit_dir() / f"{svc_name}.service"
- if svc_file.exists():
+ if svc_file.exists() and owned_by_profile(svc_file, profile_dir):
_run("systemctl", "--user", "disable", svc_name)
_run("systemctl", "--user", "stop", svc_name)
svc_file.unlink(missing_ok=True)
@@ -1940,10 +1940,10 @@ def _cleanup_gateway_service(name: str, profile_dir: Path) -> bool:
removed = True
# `gateway install --system` writes the same name under /etc; a survivor there
# restarts the removed profile at boot exactly like the user unit at login.
- removed = remove_system_systemd_unit() or removed
+ removed = remove_system_systemd_unit(profile_dir) or removed
elif system == "Darwin":
plist_path = get_launchd_plist_path()
- if plist_path.exists():
+ if plist_path.exists() and owned_by_profile(plist_path, profile_dir):
_run("launchctl", "unload", str(plist_path))
plist_path.unlink(missing_ok=True)
print("✓ Launchd service removed")
diff --git a/hermes_cli/profiles_service_cleanup.py b/hermes_cli/profiles_service_cleanup.py
index 74a0a5b4723..1261992e575 100644
--- a/hermes_cli/profiles_service_cleanup.py
+++ b/hermes_cli/profiles_service_cleanup.py
@@ -11,16 +11,31 @@ from __future__ import annotations
import os
import subprocess
+from pathlib import Path
-def remove_system_systemd_unit() -> bool:
+def owned_by_profile(definition: Path, profile_dir: Path) -> bool:
+ """False (one-line notice) when *definition* pins another HERMES_HOME. Unit names derive from the
+ profile name, and a scratch ``HOME``'s ``profiles/<name>`` resolves the ACCOUNT's real
+ ``hermes-gateway-<name>`` unit (the unit dir is account-anchored): delete/rename then disabled,
+ stopped and unlinked a live gateway of another install. Same owner rule as
+ ``gateway_service_owner.definition_belongs_to_home``."""
+ from hermes_cli.gateway_service_owner import _resolve, pinned_home
+ raw = pinned_home(definition)
+ if raw is None or _resolve(raw) == _resolve(profile_dir):
+ return True
+ print(f"↷ {definition} runs HERMES_HOME={raw}, not {profile_dir}: another install's service, left alone")
+ return False
+
+
+def remove_system_systemd_unit(profile_dir: Path) -> bool:
"""Remove ``/etc/systemd/system/hermes-gateway-<profile>.service`` when root; otherwise
name the leftover and the exact command, because the profile dir the ``gateway uninstall
--system`` verb would need to resolve the name from is about to vanish."""
from hermes_cli import gateway
svc_name = gateway.get_service_name()
unit = gateway._SYSTEM_UNIT_DIR / f"{svc_name}.service"
- if not unit.exists():
+ if not unit.exists() or not owned_by_profile(unit, profile_dir):
return False
if os.geteuid() != 0: # windows-footgun: ok — called only from the Linux arm of profiles._cleanup_gateway_service
print(f"⚠ System service {svc_name} remains at {unit}; it will restart the removed profile at boot.")
diff --git a/tests/hermes_cli/test_profile_cleanup_foreign_unit.py b/tests/hermes_cli/test_profile_cleanup_foreign_unit.py
new file mode 100644
index 00000000000..58633f3f165
--- /dev/null
+++ b/tests/hermes_cli/test_profile_cleanup_foreign_unit.py
@@ -0,0 +1,48 @@
+"""`hermes profile delete|rename` never removes another install's gateway service.
+
+Service names derive from the profile name and the user unit dir is account-anchored, so a scratch-HOME
+delete of `coder` named the ACCOUNT's real `hermes-gateway-coder` unit and disabled, stopped and unlinked
+it. Every service-manager seam is a recorder; nothing here executes one.
+"""
+
+from __future__ import annotations
+
+import os
+
+import pytest
+
+from hermes_cli import gateway as gw
+
+
+def _unit(path, home) -> None:
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text(f'[Service]\nEnvironment="HERMES_HOME={home}"\nExecStart=/usr/bin/hermes gateway run\n',
+ encoding="utf-8")
+
+
+def test_profile_delete_from_scratch_home_leaves_the_accounts_profile_unit(tmp_path, monkeypatch, capsys):
+ """``hermes profile delete|rename coder`` with HOME=<scratch>: the name resolves the ACCOUNT's real
+ ``hermes-gateway-coder`` unit (account-anchored unit dir). It pins another home, so cleanup never
+ disables, stops or unlinks it."""
+ import hermes_constants
+ from hermes_cli import profiles
+ account, scratch = tmp_path / "account", tmp_path / "scratch"
+ profile_dir = scratch / ".hermes" / "profiles" / "coder"
+ profile_dir.mkdir(parents=True)
+ real_unit = account / ".config" / "systemd" / "user" / "hermes-gateway-coder.service"
+ _unit(real_unit, account / ".hermes" / "profiles" / "coder")
+ monkeypatch.delenv("HERMES_HOME", raising=False)
+ monkeypatch.delenv("XDG_CONFIG_HOME", raising=False)
+ monkeypatch.setenv("HOME", str(scratch))
+ monkeypatch.setenv("HERMES_REAL_HOME", str(account))
+ monkeypatch.setattr(os, "geteuid", lambda: 1000)
+ monkeypatch.setattr(hermes_constants, "_account_home", lambda: account)
+ monkeypatch.setattr(gw, "_SYSTEM_UNIT_DIR", tmp_path / "no-system-units")
+ calls: list = []
+ monkeypatch.setattr(profiles.subprocess, "run", lambda argv, *a, **k: calls.append(list(argv)))
+
+ assert profiles._cleanup_gateway_service("coder", profile_dir) is False
+
+ assert calls == [] and real_unit.exists()
+ assert "another install's service, left alone" in capsys.readouterr().out