diff --git a/hermes_cli/profiles.py b/hermes_cli/profiles.py index cd8e2d1294..1ecce71c7f 100644 --- a/hermes_cli/profiles.py +++ b/hermes_cli/profiles.py @@ -1717,9 +1717,26 @@ def rename_profile(old_name: str, new_name: str) -> Path: _cleanup_gateway_service(old_canon, old_dir) _stop_gateway_process(old_dir) + # 1b. Unroute the old name from a live multiplexer BEFORE the rename. A multiplexed + # secondary has no gateway.pid of its own, so the check above reports it stopped while + # the default gateway still holds its adapters, cron ticker, logging and SQLite handles. + # Tombstone + notify so the multiplexer stops those adapters and releases its handles + # into old_dir; without it the live components immediately re-``mkdir`` the old home + # (no tombstone → ``mkdir_under_hermes_home`` does not refuse it) and the periodic + # reconcile re-adopts the resurrected dir as a ghost served profile. + served_by_mux = _served_by_running_multiplexer(old_canon) + if served_by_mux: + mark_named_profile_deleted(old_dir) + _notify_multiplexer(old_canon) + # 2. Rename directory old_dir.rename(new_dir) print(f"✓ Renamed {old_dir.name} → {new_dir.name}") + # The tombstone lived at profiles/.deleted/; old_dir is gone now so it can no + # longer resurrect, and new_dir carries no tombstone. Clear the stale marker so a future + # profile reusing the old name is not treated as deleted. + if served_by_mux: + clear_named_profile_deleted(old_dir) # 3. Update profile-scoped Honcho host blocks, preserving aiPeer identity _migrate_honcho_profile_host(old_canon, new_canon, new_dir) @@ -1735,6 +1752,11 @@ def rename_profile(old_name: str, new_name: str) -> Path: # 5. Update active_profile if it pointed to old name _retarget_active_profile(old_canon, new_canon, f"✓ Active profile updated: {new_canon}") + + # 6. Ask a live multiplexer to hot-serve the renamed profile now (mirrors create); it + # also rescans periodically, so a missed signal only delays serving. + if served_by_mux: + _notify_multiplexer(new_canon) return new_dir diff --git a/tests/hermes_cli/test_profiles.py b/tests/hermes_cli/test_profiles.py index b7c7bf61a4..7825053015 100644 --- a/tests/hermes_cli/test_profiles.py +++ b/tests/hermes_cli/test_profiles.py @@ -768,6 +768,62 @@ class TestRenameProfile: assert cfg["hosts"]["hermes_heimdall"]["aiPeer"] == "ssi_health" assert cfg["hosts"]["hermes_heimdall"]["peerName"] == "user-peer" + def test_multiplexed_rename_unroutes_old_then_hot_serves_new(self, profile_env): + """A profile served by a live multiplexer is unrouted (tombstone + notify) BEFORE the + directory move, and the new name is hot-served after — so the old name cannot be + re-``mkdir``'d back into a ghost served profile (issue: rename resurrects old name).""" + tmp_path = profile_env + create_profile("oldname", no_alias=True) + old_dir = tmp_path / ".hermes" / "profiles" / "oldname" + new_dir = tmp_path / ".hermes" / "profiles" / "newname" + + calls = [] + + def _record_notify(name): + # Snapshot the world at each multiplexer signal to pin ordering. + calls.append({ + "name": name, + "old_exists": old_dir.exists(), + "new_exists": new_dir.exists(), + "old_tombstoned": profiles.named_profile_is_deleted(old_dir), + }) + + 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=_record_notify): + rename_profile("oldname", "newname") + + # Old name unrouted before the move: first signal names oldname, while old_dir still + # exists and is tombstoned so no live component can re-create it. + assert calls[0]["name"] == "oldname" + assert calls[0]["old_exists"] is True + assert calls[0]["old_tombstoned"] is True + # New name hot-served after the move completed. + assert calls[-1]["name"] == "newname" + assert calls[-1]["new_exists"] is True + assert calls[-1]["old_exists"] is False + # End state: old gone, new present, and no stale tombstone left to poison a future + # profile that reuses the old name. + assert not old_dir.exists() + assert new_dir.is_dir() + assert not profiles.named_profile_is_deleted(old_dir) + + def test_unmultiplexed_rename_does_not_signal_multiplexer(self, profile_env): + """No live multiplexer serves this profile → rename must not tombstone or ping it + (guards against over-firing the unroute path on a single-profile install).""" + tmp_path = profile_env + create_profile("oldname", no_alias=True) + old_dir = tmp_path / ".hermes" / "profiles" / "oldname" + + with patch("hermes_cli.profiles.check_alias_collision", return_value="skip"), \ + patch("hermes_cli.profiles._served_by_running_multiplexer", return_value=False), \ + patch("hermes_cli.profiles._notify_multiplexer") as notify: + new_dir = rename_profile("oldname", "newname") + + notify.assert_not_called() + assert not profiles.named_profile_is_deleted(old_dir) + assert new_dir.is_dir() + # =================================================================== # TestExportImport