Skip to content

The agent that grows with you

fix(cli): stale-profile recovery refuses uninstall --full and abbreviated --data

kshitijk4poor167d3bb6f3f6
diff --git a/hermes_cli/main.py b/hermes_cli/main.py
index 2ff20e5f583..2785de5b37a 100644
--- a/hermes_cli/main.py
+++ b/hermes_cli/main.py
@@ -628,19 +628,9 @@ def _apply_profile_override() -> None:
except FileNotFoundError as exc:
hermes_home = _resolve_sudo_user_profile_env(profile_name)
if not hermes_home and from_sticky_profile:
- from hermes_cli._parser import command_argv
-
- command = command_argv(argv)
- # Keep ordinary commands fail-closed: silently running them in the default profile
- # would read or write the wrong profile's state. Only permit the commands that can
- # inspect/reset the stale selector or uninstall the now-unreachable installation.
- recovery_command = (
- command[:2] == ["profile", "list"]
- or (command[:2] == ["profile", "use"] and len(command) > 2
- and command[2].casefold() == "default")
- or (command[:1] == ["uninstall"] and "--data" not in command)
- )
- if recovery_command:
+ from hermes_cli.main_profile_recovery import is_stale_profile_recovery_command
+
+ if is_stale_profile_recovery_command(argv):
hermes_home = resolve_profile_env("default")
print(
f"Warning: saved profile '{profile_name}' no longer exists; "
diff --git a/hermes_cli/main_profile_recovery.py b/hermes_cli/main_profile_recovery.py
new file mode 100644
index 00000000000..3daee029e9b
--- /dev/null
+++ b/hermes_cli/main_profile_recovery.py
@@ -0,0 +1,45 @@
+"""Which commands may run in the default home when the sticky ``active_profile`` is gone.
+
+``_apply_profile_override`` fails closed when ``active_profile`` names a deleted profile:
+running an ordinary command in the default profile would read or write the wrong profile's
+state. The commands here are the way out of that state, so they must still reach their handler.
+"""
+
+from __future__ import annotations
+
+import argparse
+import contextlib
+import io
+
+
+def _uninstall_keeps_data(args: list[str]) -> bool:
+ """True when ``uninstall <args>`` removes no user data.
+
+ Parsed with the real ``uninstall`` subparser so abbreviations (``--dat``, ``--fu``) resolve
+ exactly as they will for the handler. ``--full`` wipes the default root, which also holds
+ every other profile under ``profiles/``, so it is refused like ``--data``.
+ """
+ from hermes_cli.subcommands.uninstall import build_uninstall_parser
+
+ parser = argparse.ArgumentParser(add_help=False)
+ build_uninstall_parser(parser.add_subparsers(dest="command"), cmd_uninstall=lambda _args: None)
+ # The real parser prints help/usage errors once the handler runs; this probe stays silent.
+ # --help exits 0 (harmless, let it through); an unparseable argv fails closed.
+ with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()):
+ try:
+ ns, _ = parser.parse_known_args(["uninstall", *args])
+ except SystemExit as exit_:
+ return exit_.code == 0
+ return not (ns.data or ns.full)
+
+
+def is_stale_profile_recovery_command(argv: list[str]) -> bool:
+ """``profile list``, ``profile use default``, or an ``uninstall`` that keeps user data."""
+ from hermes_cli._parser import command_argv
+
+ command = command_argv(argv)
+ if command[:2] == ["profile", "list"]:
+ return True
+ if command[:2] == ["profile", "use"]:
+ return len(command) > 2 and command[2].casefold() == "default"
+ return command[:1] == ["uninstall"] and _uninstall_keeps_data(command[1:])
diff --git a/tests/hermes_cli/test_apply_profile_override.py b/tests/hermes_cli/test_apply_profile_override.py
index 9a2dec50b97..e88c8e1424a 100644
--- a/tests/hermes_cli/test_apply_profile_override.py
+++ b/tests/hermes_cli/test_apply_profile_override.py
@@ -76,6 +76,7 @@ def _run_apply_profile_override(
["hermes", "profile", "use", "default"],
["hermes", "uninstall"],
["hermes", "uninstall", "--dry-run"],
+ ["hermes", "uninstall", "--help"],
])
@pytest.mark.parametrize("exported_home", [False, True])
def test_missing_sticky_profile_allows_recovery_commands(
@@ -89,7 +90,7 @@ def test_missing_sticky_profile_allows_recovery_commands(
)
assert result == str(root)
- assert "ray" in capsys.readouterr().err
+ assert "saved profile 'ray' no longer exists; running this recovery command" in capsys.readouterr().err
if argv[1:3] == ["profile", "use"]:
from hermes_cli.profile_cmd import cmd_profile
@@ -102,6 +103,10 @@ def test_missing_sticky_profile_allows_recovery_commands(
@pytest.mark.parametrize("argv", [
["hermes", "chat"],
["hermes", "uninstall", "--data"],
+ ["hermes", "uninstall", "--dat", "--yes"],
+ ["hermes", "uninstall", "--full", "--yes"],
+ ["hermes", "uninstall", "--fu"],
+ ["hermes", "uninstall", "--full", "--data"],
["hermes", "-p", "ray", "uninstall"],
])
def test_missing_profile_still_blocks_other_or_explicit_commands(tmp_path, monkeypatch, argv):