The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(profiles): rename removes the old name's gateway service even when stopped (#124401, salvage #124402)
John Paul Solivaef3fa2a6f959
diff --git a/hermes_cli/profiles.py b/hermes_cli/profiles.pyindex 8cb9fc2a174..f072acdd3e4 100644--- a/hermes_cli/profiles.py+++ b/hermes_cli/profiles.py@@ -1872,8 +1872,8 @@ def _maybe_unregister_gateway_service(profile_name: str) -> None:print(f"⚠ Could not unregister s6 gateway service: {exc}")-def _cleanup_gateway_service(name: str, profile_dir: Path) -> None:- """Disable and remove systemd/launchd service for a profile."""+def _cleanup_gateway_service(name: str, profile_dir: Path) -> bool:+ """Disable and remove systemd/launchd service for a profile; True when a unit was removed."""import platform as _platform# The service name follows get_hermes_home(): bind the override (the seam a multiplexed@@ -1899,12 +1899,14 @@ def _cleanup_gateway_service(name: str, profile_dir: Path) -> None:svc_file.unlink(missing_ok=True)_run("systemctl", "--user", "daemon-reload")print(f"✓ Service {svc_name} removed")+ return Trueelif system == "Darwin":plist_path = get_launchd_plist_path()if plist_path.exists():_run("launchctl", "unload", str(plist_path))plist_path.unlink(missing_ok=True)print("✓ Launchd service removed")+ return Trueexcept Exception as e:print(f"⚠ Service cleanup: {e}")finally:@@ -1912,6 +1914,7 @@ def _cleanup_gateway_service(name: str, profile_dir: Path) -> None:os.environ.pop("HERMES_HOME", None)if old_home is not None:os.environ["HERMES_HOME"] = old_home+ return Falsedef _stop_gateway_process(profile_dir: Path) -> None:@@ -2328,9 +2331,14 @@ def rename_profile(old_name: str, new_name: str) -> Path:if new_dir.exists():raise _profile_exists_error(new_canon)- # 1. Stop gateway if running, and the screen whose launcher holds paths under the old name- if _check_gateway_running(old_dir):- _cleanup_gateway_service(old_canon, old_dir)+ # 1. Remove the old name's service even when its gateway is stopped: the unit is named after+ # the old profile and runs ``--profile <old>``, so the service manager would crash-loop it on+ # the next login or container boot. Then stop the gateway and the screen whose launcher+ # holds paths under the old name.+ gw_running = _check_gateway_running(old_dir)+ service_removed = _cleanup_gateway_service(old_canon, old_dir)+ _maybe_unregister_gateway_service(old_canon)+ if gw_running:_stop_gateway_process(old_dir)_stop_bot_desktop(old_dir)@@ -2360,6 +2368,9 @@ def rename_profile(old_name: str, new_name: str) -> Path:if live_mux:clear_named_profile_deleted(old_dir)_notify_multiplexer(old_canon)+ _maybe_register_gateway_service(old_canon)+ if service_removed:+ print(f"⚠ The gateway service was removed. Reinstall it with: hermes -p {old_canon} gateway install")raiseprint(f"✓ Renamed {old_dir.name} → {new_dir.name}")# The tombstone lives at profiles/.deleted/<old_name>; old_dir is gone so nothing can@@ -2396,6 +2407,9 @@ def rename_profile(old_name: str, new_name: str) -> Path:# 7. Hot-serve the renamed profile now (mirrors create; a missed signal only delays it).if live_mux:_notify_multiplexer(new_canon)+ _maybe_register_gateway_service(new_canon)+ if service_removed:+ print(f"⚠ The gateway service was removed. Reinstall it with: hermes -p {new_canon} gateway install")return new_dirdiff --git a/tests/hermes_cli/test_profile_rename_service.py b/tests/hermes_cli/test_profile_rename_service.pynew file mode 100644index 00000000000..a450515c9df--- /dev/null+++ b/tests/hermes_cli/test_profile_rename_service.py@@ -0,0 +1,87 @@+"""Renaming a profile removes the gateway service registered under its old name.++The launchd/systemd unit and the s6 slot are named after the profile and start ``--profile+<old>``. A rename that kept them left a service that the next login or container boot would+start for a profile that no longer exists, while the renamed profile had none.+"""+from __future__ import annotations++import os+import platform+import pwd+import subprocess+from pathlib import Path++import pytest++from hermes_cli import gateway, profiles+from hermes_constants import reset_hermes_home_override, set_hermes_home_override+++def profile_env(tmp_path, monkeypatch):+ home = tmp_path / ".hermes"+ home.mkdir()+ monkeypatch.setattr(Path, "home", lambda: tmp_path)+ monkeypatch.setenv("HERMES_HOME", str(home))+ monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path / "xdg"))+ real = pwd.getpwuid(os.getuid())+ fake = pwd.struct_passwd((real.pw_name, real.pw_passwd, real.pw_uid, real.pw_gid,+ real.pw_gecos, str(tmp_path), real.pw_shell))+ monkeypatch.setattr(pwd, "getpwuid", lambda uid: fake) # launchd plists live under the account home+ calls: list[list[str]] = []++ def _run(cmd, *args, **kwargs):+ calls.append(list(cmd))+ return subprocess.CompletedProcess(cmd, 1, "", "")++ monkeypatch.setattr(profiles.subprocess, "run", _run)+ return tmp_path+++def _host_unit_path(profile_dir: Path) -> Path:+ token = set_hermes_home_override(str(profile_dir))+ try:+ if platform.system() == "Darwin":+ return gateway.get_launchd_plist_path()+ return gateway.user_systemd_unit_dir() / f"{gateway.get_service_name()}.service"+ finally:+ reset_hermes_home_override(token)++[email protected]("posix")+def test_rename_removes_the_old_names_service_while_the_gateway_is_stopped(profile_env):+ old_dir = profiles.create_profile("coder", no_alias=True)+ unit = _host_unit_path(old_dir)+ assert "coder" in unit.name+ unit.parent.mkdir(parents=True, exist_ok=True)+ unit.write_text("installed by `hermes -p coder gateway install`\n", encoding="utf-8")++ new_dir = profiles.rename_profile("coder", "dev")++ assert not unit.exists()+ assert not _host_unit_path(new_dir).exists()+++def test_rename_moves_the_s6_slot_to_the_new_name(profile_env, monkeypatch):+ class _S6:+ def __init__(self):+ self.slots = {"coder"}++ def supports_runtime_registration(self):+ return True++ def register_profile_gateway(self, name, *, start_now=True):+ self.slots.add(name)++ def unregister_profile_gateway(self, name):+ self.slots.discard(name)++ s6 = _S6()+ monkeypatch.setattr("hermes_cli.service_manager.detect_service_manager", lambda: "s6")+ monkeypatch.setattr("hermes_cli.service_manager.get_service_manager", lambda: s6)+ profiles.create_profile("coder", no_alias=True)++ profiles.rename_profile("coder", "dev")++ assert s6.slots == {"dev"}diff --git a/website/docs/reference/profile-commands.md b/website/docs/reference/profile-commands.mdindex e629a91d378..5f7ce5b5774 100644--- a/website/docs/reference/profile-commands.md+++ b/website/docs/reference/profile-commands.md@@ -229,7 +229,11 @@ hermes profile alias work --removehermes profile rename <old-name> <new-name>```-Renames a profile. Updates the directory and shell alias.+Renames a profile. Updates the directory and shell alias. A gateway service installed under the+old name (`hermes -p <old-name> gateway install`) is removed, whether or not the gateway is+running, because it would start the old name at the next login; reinstall it with+`hermes -p <new-name> gateway install`. Inside the Docker image the s6 gateway slot moves to the+new name.| Argument | Description ||----------|-------------|diff --git a/website/docs/user-guide/profiles.md b/website/docs/user-guide/profiles.mdindex 06688de2f36..3d294f61c58 100644--- a/website/docs/user-guide/profiles.md+++ b/website/docs/user-guide/profiles.md@@ -339,7 +339,7 @@ fails that step for the whole install — see```bashhermes profile list # show all profiles with statushermes profile show coder # detailed info for one profile-hermes profile rename coder dev-bot # rename (updates alias + service)+hermes profile rename coder dev-bot # rename (updates alias; removes the old name's gateway service)hermes profile migrate-identity coder dev-bot # retry a rename's identity migrationhermes profile purge-identity dev-bot # retry a delete's identity purgehermes profile export coder # pack into coder.tar.gz (shareable; keys stripped)diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/reference/profile-commands.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/reference/profile-commands.mdindex 009f0065f32..438bd2acac5 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/reference/profile-commands.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/reference/profile-commands.md@@ -226,7 +226,7 @@ hermes profile alias work --removehermes profile rename <old-name> <new-name>```-重命名 profile,同时更新目录和 shell alias。+重命名 profile,同时更新目录和 shell alias。以旧名称安装的网关服务(`hermes -p <old-name> gateway install`)无论网关是否在运行都会被移除,因为它会在下次登录时启动旧名称;请用 `hermes -p <new-name> gateway install` 重新安装。在 Docker 镜像内,s6 网关槽位会迁移到新名称。| 参数 | 描述 ||----------|-------------|diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/profiles.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/profiles.mdindex 959c436fe0a..105b70aa66c 100644--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/profiles.md+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/profiles.md@@ -232,7 +232,7 @@ hermes update```bashhermes profile list # 显示所有 profile 及其状态hermes profile show coder # 显示某个 profile 的详细信息-hermes profile rename coder dev-bot # 重命名(同步更新别名和服务)+hermes profile rename coder dev-bot # 重命名(更新别名;移除旧名称的网关服务)hermes profile export coder # 导出为 coder.tar.gzhermes profile import coder.tar.gz # 从归档文件导入```