Skip to content

The agent that grows with you

fix: release the profile MCP stderr handle before rename too

teknium16dc6c92ea21b
diff --git a/hermes_cli/profiles.py b/hermes_cli/profiles.py
index eea7c94cac9..5e6b16f7300 100644
--- a/hermes_cli/profiles.py
+++ b/hermes_cli/profiles.py
@@ -1807,6 +1807,13 @@ def rename_profile(old_name: str, new_name: str) -> Path:
mark_named_profile_deleted(old_dir)
_notify_multiplexer(old_canon)
+ # 1c. Release this process's cached MCP stderr handle into the old home (same as
+ # delete_profile): Windows refuses to rename a directory holding an open file, and the
+ # handle would otherwise stay cached under the old key after the move.
+ from hermes_constants import hermes_home_key
+ from tools.mcp_tool_lifecycle import shutdown_mcp_servers
+ shutdown_mcp_servers(scope=hermes_home_key(old_dir))
+
# 2. Rename directory. If the move fails (cross-device EXDEV, permissions, a racing writer),
# undo the unroute so the profile is never stranded tombstoned-but-present.
try:
diff --git a/tests/tools/test_mcp_stderr_lifecycle.py b/tests/tools/test_mcp_stderr_lifecycle.py
index 0f42f7bfe7a..5430db640e7 100644
--- a/tests/tools/test_mcp_stderr_lifecycle.py
+++ b/tests/tools/test_mcp_stderr_lifecycle.py
@@ -1,5 +1,7 @@
"""MCP shutdown releases only the selected profile's cached stderr handle."""
+from pathlib import Path
+
from hermes_constants import (
hermes_home_key,
reset_hermes_home_override,
@@ -35,4 +37,31 @@ def test_scoped_shutdown_releases_log_and_preserves_other_profile(tmp_path):
assert second.closed and reopened.closed
finally:
for handle in handles:
- handle.close()
\ No newline at end of file
+ handle.close()
+
+def test_rename_profile_releases_cached_log_handle(tmp_path, monkeypatch):
+ from hermes_cli import profiles
+ from tools import mcp_tool_config
+
+ monkeypatch.setattr(Path, "home", lambda: tmp_path)
+ home = tmp_path / ".hermes"
+ home.mkdir()
+ monkeypatch.setenv("HERMES_HOME", str(home))
+ monkeypatch.setattr(profiles, "_cleanup_gateway_service", lambda *_: None)
+ monkeypatch.setattr(profiles, "_notify_multiplexer", lambda *_: None)
+ monkeypatch.setattr(profiles, "_live_default_multiplexer", lambda: False)
+ old_dir = profiles.create_profile("mcp-log-old", no_alias=True)
+ token = set_hermes_home_override(old_dir)
+ try:
+ handle = _get_mcp_stderr_log()
+ finally:
+ reset_hermes_home_override(token)
+ try:
+ new_dir = profiles.rename_profile("mcp-log-old", "mcp-log-new")
+ # The old home's handle must be released before the directory moves (Windows refuses
+ # to rename a directory holding an open file) and must not linger under the old key.
+ assert handle.closed
+ assert hermes_home_key(old_dir) not in mcp_tool_config._mcp_stderr_log_fh
+ assert new_dir.is_dir() and not old_dir.exists()
+ finally:
+ handle.close()