From cd49c3badc81cf560abce4113fff5474c73d4443 Mon Sep 17 00:00:00 2001 From: xielevi <212198284+xielevi@users.noreply.github.com> Date: Sun, 13 Sep 2026 00:29:38 +0800 Subject: [PATCH] fix(profiles): rename under a live multiplexer no longer resurrects the old name A multiplexed secondary profile has no gateway.pid of its own, so rename_profile's _check_gateway_running(old_dir) reported it stopped and skipped teardown. Unlike delete_profile, rename never tombstoned the old name nor notified the multiplexer, so at the moment old_dir.rename(new_dir) ran the default gateway still held the old profile's adapters, cron ticker, logging and SQLite handles. Those live components immediately re-mkdir'd the old home (no .deleted tombstone -> mkdir_under_hermes_home does not refuse it) and the periodic reconcile re-adopted the resurrected dir as a ghost served profile. Give rename the same unroute-before-mutate discipline delete already has: when the old name is served by a live multiplexer, tombstone + notify before the move so its adapters stop and handles release into old_dir; clear the stale tombstone after the move; then notify for the new name to hot-serve it (mirrors create). Non-multiplexed renames are untouched. Fixes #109267 --- hermes_cli/profiles.py | 22 ++++++++++++ tests/hermes_cli/test_profiles.py | 56 +++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) 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