fix(profiles): roll back the unroute if a multiplexed rename fails
If old_dir.rename(new_dir) raises (cross-device EXDEV, permissions, a racing writer) after the pre-move tombstone + unroute, the profile was left tombstoned-but-present — enumeration treats it as deleted, so the profile silently vanishes (worse than the ghost this PR fixes). Undo the unroute on failure: clear the tombstone and re-notify the multiplexer to re-serve the old name before re-raising. Adds a regression test (proven red on the base of this branch).
This commit is contained in:
+10
-2
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user