Skip to content

The agent that grows with you

fix(profiles): roll back the unroute if a multiplexed rename fails

xielevi4741b71c513f
diff --git a/hermes_cli/profiles.py b/hermes_cli/profiles.py
index 1ecce71c7fc..217510d7caf 100644
--- a/hermes_cli/profiles.py
+++ b/hermes_cli/profiles.py
@@ -1729,8 +1729,16 @@ def rename_profile(old_name: str, new_name: str) -> Path:
mark_named_profile_deleted(old_dir)
_notify_multiplexer(old_canon)
- # 2. Rename directory
- old_dir.rename(new_dir)
+ # 2. Rename directory. If the move fails (cross-device EXDEV, permissions, a racing
+ # writer), undo the unroute above so we never strand the profile as tombstoned-but-present:
+ # restore its directory to the served set and clear the marker before re-raising.
+ try:
+ old_dir.rename(new_dir)
+ except Exception:
+ if served_by_mux:
+ clear_named_profile_deleted(old_dir)
+ _notify_multiplexer(old_canon)
+ raise
print(f"✓ Renamed {old_dir.name} → {new_dir.name}")
# The tombstone lived at profiles/.deleted/<old_name>; old_dir is gone now so it can no
# longer resurrect, and new_dir carries no tombstone. Clear the stale marker so a future
diff --git a/tests/hermes_cli/test_profiles.py b/tests/hermes_cli/test_profiles.py
index 7825053015a..6ea3e9fbff0 100644
--- a/tests/hermes_cli/test_profiles.py
+++ b/tests/hermes_cli/test_profiles.py
@@ -824,6 +824,29 @@ class TestRenameProfile:
assert not profiles.named_profile_is_deleted(old_dir)
assert new_dir.is_dir()
+ def test_multiplexed_rename_failure_rolls_back_unroute(self, profile_env):
+ """If the directory move fails, the pre-move unroute is undone: the old name is
+ re-served (tombstone cleared, multiplexer re-notified) instead of left stranded as
+ tombstoned-but-present (which would make the profile vanish, worse than a ghost)."""
+ tmp_path = profile_env
+ create_profile("oldname", no_alias=True)
+ old_dir = tmp_path / ".hermes" / "profiles" / "oldname"
+
+ signals = []
+ with patch("hermes_cli.profiles.check_alias_collision", return_value="skip"), \
+ patch("hermes_cli.profiles._served_by_running_multiplexer", return_value=True), \
+ patch("hermes_cli.profiles._notify_multiplexer", side_effect=signals.append), \
+ patch("hermes_cli.profiles.Path.rename", side_effect=OSError("EXDEV")):
+ with pytest.raises(OSError, match="EXDEV"):
+ rename_profile("oldname", "newname")
+
+ # Old dir still there, tombstone cleared, and the last signal re-served the old name.
+ assert old_dir.is_dir()
+ assert not profiles.named_profile_is_deleted(old_dir)
+ assert signals[0] == "oldname" # unroute on the way in
+ assert signals[-1] == "oldname" # rollback re-serves it, never "newname"
+ assert "newname" not in signals
+
# ===================================================================
# TestExportImport