The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(profiles): unroute a renamed profile whenever a live default multiplexer exists
teknium1a7254e2d4c17
diff --git a/hermes_cli/profiles.py b/hermes_cli/profiles.pyindex f3e557dc070..d3ad434d594 100644--- a/hermes_cli/profiles.py+++ b/hermes_cli/profiles.py@@ -952,6 +952,16 @@ def _notify_multiplexer(canon: str) -> None:notify_multiplexer_profiles_changed(canon)+def _live_default_multiplexer() -> bool:+ """True when a live default gateway has recorded a served-profile set: every dir under+ profiles/ is then served by it, so a profile-identity change must be unrouted first."""+ try:+ from hermes_cli.gateway_multiplex_served import recorded_served_profiles+ return recorded_served_profiles() is not None+ except Exception:+ return False++def seed_profile_skills(profile_dir: Path, quiet: bool = False) -> Optional[dict]:"""Seed bundled skills into a profile via subprocess (sync_skills() caches HERMES_HOME atmodule level). Returns the sync result dict, or None on failure. ``--no-skills`` profiles@@ -1727,33 +1737,30 @@ 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:+ # 1b. Unroute the old name from a live multiplexer BEFORE the rename (same protocol as+ # delete_profile). 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; those re-``mkdir`` the old home the moment it moves (no tombstone →+ # ``mkdir_under_hermes_home`` does not refuse it) and the periodic reconcile re-adopts the+ # resurrected dir as a ghost served profile (#109267).+ live_mux = _live_default_multiplexer()+ if live_mux:mark_named_profile_deleted(old_dir)_notify_multiplexer(old_canon)- # 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.+ # 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:old_dir.rename(new_dir)except Exception:- if served_by_mux:+ if live_mux:clear_named_profile_deleted(old_dir)_notify_multiplexer(old_canon)raiseprint(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- # profile reusing the old name is not treated as deleted.- if served_by_mux:+ # The tombstone lives at profiles/.deleted/<old_name>; old_dir is gone so nothing can+ # resurrect it, and a future profile reusing the old name must not read as deleted.+ if live_mux:clear_named_profile_deleted(old_dir)# 3. Update profile-scoped Honcho host blocks, preserving aiPeer identity@@ -1771,9 +1778,8 @@ 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:+ # 6. Hot-serve the renamed profile now (mirrors create; a missed signal only delays it).+ if live_mux:_notify_multiplexer(new_canon)return new_dirdiff --git a/tests/hermes_cli/test_profiles.py b/tests/hermes_cli/test_profiles.pyindex 7be2157a9af..d210e4a271c 100644--- a/tests/hermes_cli/test_profiles.py+++ b/tests/hermes_cli/test_profiles.py@@ -780,9 +780,10 @@ class TestRenameProfile: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)."""+ """Under a live multiplexer the old name is tombstoned + unrouted BEFORE the directory+ moves and the new name is hot-served after, so a stale runtime mkdir of the old home is+ refused instead of resurrecting a ghost served profile (#109267)."""+ from hermes_constants import mkdir_under_hermes_hometmp_path = profile_envcreate_profile("oldname", no_alias=True)old_dir = tmp_path / ".hermes" / "profiles" / "oldname"@@ -792,71 +793,37 @@ class TestRenameProfile: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),- })+ calls.append((name, old_dir.exists(), new_dir.exists(), profiles.named_profile_is_deleted(old_dir)))+ if name == "oldname" and old_dir.exists():+ # A still-live component of the multiplexer writing into the old home mid-teardown.+ with pytest.raises(FileNotFoundError):+ mkdir_under_hermes_home(old_dir / "logs")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._live_default_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)+ # (name, old_exists, new_exists, old_tombstoned): unroute first, hot-serve last.+ assert calls[0] == ("oldname", True, False, True)+ assert calls[-1] == ("newname", False, True, False)+ assert not old_dir.exists() and new_dir.is_dir()+ assert not profiles.named_profile_is_deleted(old_dir) # a future 'oldname' is not born deleteddef 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)."""+ """No live multiplexer → rename must neither tombstone nor ping (single-profile installs)."""tmp_path = profile_envcreate_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._live_default_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()-- 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+ assert not (tmp_path / ".hermes" / "profiles" / ".deleted").exists()+ assert not old_dir.exists() and new_dir.is_dir()# ===================================================================