Skip to content

The agent that grows with you

fix(profile): let `profile alias --remove` clean up an orphan alias

dlcsouza6fa79e9b968e
diff --git a/hermes_cli/profile_cmd.py b/hermes_cli/profile_cmd.py
index e13115b03ed..cab4b50fe46 100644
--- a/hermes_cli/profile_cmd.py
+++ b/hermes_cli/profile_cmd.py
@@ -411,19 +411,23 @@ def _profile_alias(args):
name = args.profile_name
remove = getattr(args, "remove", False)
custom_name = getattr(args, "alias_name", None)
- if not profile_exists(name):
- _die(f"Error: Profile '{name}' does not exist.")
alias_name = custom_name or name
try:
validate_alias_name(alias_name)
except ValueError as exc:
_die(f"Error: {exc}")
if remove:
+ # Removal touches only the wrapper file, never the profile: the alias names
+ # a profile that may already be gone -- which is exactly the "orphan alias"
+ # the doctor reports, and this is the only command that can clean it up.
+ # Behind profile_exists() that case was unremovable.
if remove_wrapper_script(alias_name):
print(f"✓ Removed alias '{alias_name}'")
else:
print(f"No alias '{alias_name}' found to remove.")
return
+ if not profile_exists(name):
+ _die(f"Error: Profile '{name}' does not exist.")
collision = check_alias_collision(alias_name)
if collision:
_die(f"Error: {collision}")
diff --git a/tests/hermes_cli/test_profile_alias_orphan_removal.py b/tests/hermes_cli/test_profile_alias_orphan_removal.py
new file mode 100644
index 00000000000..9d0a3fbbefe
--- /dev/null
+++ b/tests/hermes_cli/test_profile_alias_orphan_removal.py
@@ -0,0 +1,70 @@
+"""`hermes profile alias <name> --remove` must clean up an ORPHAN alias.
+
+An orphan alias is a wrapper in ``~/.local/bin`` pointing at a profile that no
+longer exists — the state ``hermes doctor`` reports as
+``Orphan alias: <name> -> profile '<p>' no longer exists``.
+
+Removal is the one path that must work in that state, and it is the only cleanup
+the doctor can point users at. It used to be impossible: the handler checked
+``profile_exists()`` before looking at ``--remove``, so the command died with
+"Profile '<p>' does not exist" and the wrapper was only removable by hand.
+"""
+
+from pathlib import Path
+from types import SimpleNamespace
+
+import pytest
+
+from hermes_cli import profile_cmd
+
+
+def isolated_home(tmp_path, monkeypatch):
+ """Isolate the profile root (HERMES_HOME) and the wrapper dir (Path.home())."""
+ hermes_home = tmp_path / ".hermes"
+ hermes_home.mkdir()
+ monkeypatch.setattr(Path, "home", lambda: tmp_path)
+ monkeypatch.setenv("HERMES_HOME", str(hermes_home))
+ return tmp_path
+
+
+def _write_wrapper(home: Path, name: str) -> Path:
+ wrapper = home / ".local" / "bin" / name
+ wrapper.parent.mkdir(parents=True, exist_ok=True)
+ wrapper.write_text(f'#!/bin/sh\nexec hermes -p {name} "$@"\n', encoding="utf-8")
+ return wrapper
+
+
+def test_remove_alias_whose_profile_is_gone(isolated_home, capsys):
+ wrapper = _write_wrapper(isolated_home, "petcare")
+ assert not (isolated_home / ".hermes" / "profiles" / "petcare").exists()
+
+ profile_cmd._profile_alias(
+ SimpleNamespace(profile_name="petcare", remove=True, alias_name=None)
+ )
+
+ assert not wrapper.exists(), "the orphan wrapper must be gone"
+ assert "Removed alias 'petcare'" in capsys.readouterr().out
+
+
+def test_remove_never_unlinks_a_file_that_is_not_a_wrapper(isolated_home):
+ """Dropping the profile check is safe only because removal stays name- and
+ content-guarded: it refuses a traversal-shaped name and only unlinks a file
+ that reads as a Hermes wrapper."""
+ not_a_wrapper = isolated_home / ".local" / "bin" / "notes"
+ not_a_wrapper.parent.mkdir(parents=True, exist_ok=True)
+ not_a_wrapper.write_text("just my notes\n", encoding="utf-8")
+
+ profile_cmd._profile_alias(
+ SimpleNamespace(profile_name="notes", remove=True, alias_name=None)
+ )
+
+ assert not_a_wrapper.exists()
+
+
+def test_create_still_requires_an_existing_profile(isolated_home):
+ """The reorder must not weaken the create path."""
+ with pytest.raises(SystemExit):
+ profile_cmd._profile_alias(
+ SimpleNamespace(profile_name="ghost", remove=False, alias_name=None)
+ )