Skip to content

The agent that grows with you

fix(skills): snapshot import reports refused entries; pin the None-vs-False boundary in tests

ThereWasAYangbd44e2fea978
diff --git a/hermes_cli/skills_hub.py b/hermes_cli/skills_hub.py
index 9a365c06490..26ba2de2d5a 100644
--- a/hermes_cli/skills_hub.py
+++ b/hermes_cli/skills_hub.py
@@ -1372,19 +1372,20 @@ def do_snapshot_export(output_path: str, console: Optional[Console] = None) -> N
def do_snapshot_import(input_path: str, force: bool = False,
- console: Optional[Console] = None) -> None:
- """Re-install skills from a snapshot file."""
+ console: Optional[Console] = None) -> Optional[bool]:
+ """Re-install skills from a snapshot file. Returns False when the file is unreadable or any
+ entry failed to install (e.g. refused by the scan gate), else None — as ``do_update`` does."""
from tools.skills_hub import TapsManager
c = console or _console
inp = Path(input_path)
if not inp.exists():
_print_error(c, f"File not found: {inp}")
- return
+ return False
try:
snapshot = json.loads(inp.read_text(encoding="utf-8-sig"))
except json.JSONDecodeError:
_print_error(c, f"Invalid JSON in {inp}")
- return
+ return False
taps = snapshot.get("taps", [])
if taps:
@@ -1399,26 +1400,34 @@ def do_snapshot_import(input_path: str, force: bool = False,
c.print("[dim]No skills in snapshot to install.[/]\n")
return
c.print(f"[bold]Importing {len(skills)} skill(s) from snapshot...[/]\n")
+ failed: list[str] = []
for entry in skills:
identifier = entry.get("identifier", "")
if not identifier:
c.print(f"[yellow]Skipping entry with no identifier: {entry.get('name', '?')}[/]")
continue
c.print(f"[bold]--- {entry.get('name', identifier)} ---[/]")
- do_install(identifier, category=entry.get("category", ""), force=force, console=c)
+ if do_install(identifier, category=entry.get("category", ""), force=force, console=c) is False:
+ failed.append(entry.get("name", identifier))
+ if failed:
+ c.print(f"[bold red]Snapshot import incomplete.[/] Not installed: {', '.join(failed)} "
+ "(see the messages above).\n")
+ return False
c.print("[bold green]Snapshot import complete.[/]\n")
+ return None
# --- CLI argparse entry point ---
-def _snapshot_cli(args) -> None:
+def _snapshot_cli(args) -> Optional[bool]:
snap_action = getattr(args, "snapshot_action", None)
if snap_action == "export":
do_snapshot_export(args.output)
elif snap_action == "import":
- do_snapshot_import(args.input, force=getattr(args, "force", False))
+ return do_snapshot_import(args.input, force=getattr(args, "force", False))
else:
_console.print("Usage: hermes skills snapshot [export|import]\n")
+ return None
def _tap_cli(args) -> None:
diff --git a/tests/hermes_cli/test_skills_failure_exit_code.py b/tests/hermes_cli/test_skills_failure_exit_code.py
index 540fb3a14f6..bbf3adb016f 100644
--- a/tests/hermes_cli/test_skills_failure_exit_code.py
+++ b/tests/hermes_cli/test_skills_failure_exit_code.py
@@ -1,4 +1,4 @@
-"""A failed ``hermes skills install|update|uninstall`` exits non-zero (#118301).
+"""A failed ``hermes skills install|update|uninstall|snapshot import`` exits non-zero (#118301).
The Desktop spawns these as detached actions and learns their outcome only from the
exit code (``apps/desktop/src/store/hub-actions.ts`` toasts on non-zero), so a refused
@@ -91,6 +91,60 @@ def test_already_installed_is_not_a_failure(monkeypatch, tmp_path):
assert cli_hub.do_install("org/repo/risky-skill", console=console, skip_confirm=True) is None
+def test_declined_install_confirm_is_not_a_failure(monkeypatch, tmp_path):
+ """The user answering "no" at the install prompt is a no-op they own: None, exit 0."""
+ installs, _ = _scan_gate_env(monkeypatch, tmp_path, verdict="safe")
+ monkeypatch.setattr(cli_hub, "_confirm_install", lambda *a, **k: False)
+ console, _ = _console()
+
+ assert cli_hub.do_install("org/repo/risky-skill", console=console, skip_confirm=False) is None
+ assert installs == []
+
+
+def test_pinned_source_that_is_gone_reports_failure(monkeypatch):
+ """``do_update`` pins the lockfile's registry; when no adapter matches, the install is refused
+ ("Refusing to resolve ...") and must report False, not a success that over-counts the update."""
+ import tools.skills_hub as hub
+
+ class _Source:
+ def source_id(self):
+ return "skills-sh"
+
+ monkeypatch.setattr(hub, "ensure_hub_dirs", lambda: None)
+ monkeypatch.setattr(cli_hub, "_sources", lambda: [_Source()])
+ monkeypatch.setattr(cli_hub, "_record_skill_install", lambda *a, **k: None)
+ monkeypatch.setattr(cli_hub, "_resolve_source_meta_and_bundle",
+ lambda *a, **k: pytest.fail("a pinned-source refusal must not resolve elsewhere"))
+ console, sink = _console()
+
+ assert cli_hub.do_install("org/x", console=console, skip_confirm=True, source_id="gone-registry") is False
+ assert "Refusing to resolve" in sink.getvalue()
+
+
+def test_update_whose_registry_is_gone_is_not_updated(monkeypatch):
+ """End to end through the real do_install: the pinned-source refusal surfaces as Not updated."""
+ import tools.skills_hub as hub
+ import tools.skills_hub_install as hub_install
+
+ class _Source:
+ def source_id(self):
+ return "skills-sh"
+
+ monkeypatch.setattr(hub, "ensure_hub_dirs", lambda: None)
+ monkeypatch.setattr(cli_hub, "_sources", lambda: [_Source()])
+ monkeypatch.setattr(cli_hub, "_record_skill_install", lambda *a, **k: None)
+ monkeypatch.setattr(hub_install, "check_for_skill_updates", lambda **_k: [
+ {"name": "orphan", "identifier": "org/orphan", "source": "gone-registry", "status": "update_available"}])
+ monkeypatch.setattr(hub, "HubLockFile", lambda: type(
+ "Lock", (), {"get_installed": lambda self, n: None})())
+ console, sink = _console()
+
+ assert cli_hub.do_update(console=console) is False
+ out = sink.getvalue()
+ assert "Not updated:" in out and "orphan" in out
+ assert "Updated 1 skill(s)" not in out
+
+
def test_unresolvable_source_reports_failure(monkeypatch):
import tools.skills_hub as hub
@@ -147,14 +201,92 @@ def test_uninstall_refusal_reports_failure(monkeypatch):
assert cli_hub.do_uninstall("builtin-skill", console=console, skip_confirm=True) is False
-def test_uninstall_success(monkeypatch):
[email protected]("invalidate_cache", [True, False])
+def test_uninstall_success_applies_the_change(monkeypatch, invalidate_cache):
+ """A removal reports True and still reaches the cache step (invalidate now, or defer)."""
import tools.skills_hub_install as hub_install
monkeypatch.setattr(hub_install, "uninstall_skill", lambda name: (True, f"Uninstalled '{name}'"))
- monkeypatch.setattr(cli_hub, "_finish_change", lambda *a, **k: None)
+ finished: list = []
+ monkeypatch.setattr(cli_hub, "_finish_change", lambda c, invalidate: finished.append(invalidate))
+ console, _ = _console()
+
+ assert cli_hub.do_uninstall("hub-skill", console=console, skip_confirm=True,
+ invalidate_cache=invalidate_cache) is True
+ assert finished == [invalidate_cache]
+
+
+def test_uninstall_refusal_does_not_touch_the_cache(monkeypatch):
+ import tools.skills_hub_install as hub_install
+
+ monkeypatch.setattr(hub_install, "uninstall_skill", lambda name: (False, "not a hub skill"))
+ finished: list = []
+ monkeypatch.setattr(cli_hub, "_finish_change", lambda *a, **k: finished.append(a))
+ console, _ = _console()
+
+ assert cli_hub.do_uninstall("builtin-skill", console=console, skip_confirm=True) is False
+ assert finished == []
+
+
+def test_declined_uninstall_is_not_a_failure(monkeypatch):
+ import tools.skills_hub_install as hub_install
+
+ monkeypatch.setattr(cli_hub, "_confirm_or_cancel", lambda *a, **k: False)
+ monkeypatch.setattr(hub_install, "uninstall_skill",
+ lambda name: pytest.fail("a declined prompt must not uninstall"))
+ console, _ = _console()
+
+ assert cli_hub.do_uninstall("hub-skill", console=console, skip_confirm=False) is None
+
+
+def _snapshot(tmp_path, names):
+ import json
+
+ path = tmp_path / "snap.json"
+ path.write_text(json.dumps({"taps": [], "skills": [
+ {"name": n, "identifier": f"org/{n}"} for n in names]}), encoding="utf-8")
+ return str(path)
+
+
+def test_snapshot_import_reports_refused_entries(monkeypatch, tmp_path):
+ """Restoring a snapshot whose entries the scan gate refuses must not end in "complete"."""
+ outcomes = {"good": True, "blocked": False}
+ monkeypatch.setattr(cli_hub, "do_install", lambda identifier, **_k: outcomes[identifier.split("/", 1)[1]])
+ console, sink = _console()
+
+ assert cli_hub.do_snapshot_import(_snapshot(tmp_path, ["good", "blocked"]), console=console) is False
+ out = sink.getvalue()
+ assert "Snapshot import complete" not in out
+ assert "Not installed: blocked" in out
+
+
+def test_snapshot_import_all_installed_is_not_a_failure(monkeypatch, tmp_path):
+ monkeypatch.setattr(cli_hub, "do_install", lambda identifier, **_k: True)
+ console, sink = _console()
+
+ assert cli_hub.do_snapshot_import(_snapshot(tmp_path, ["good"]), console=console) is None
+ assert "Snapshot import complete" in sink.getvalue()
+
+
+def test_snapshot_import_unreadable_file_is_a_failure(tmp_path):
console, _ = _console()
- assert cli_hub.do_uninstall("hub-skill", console=console, skip_confirm=True) is True
+ assert cli_hub.do_snapshot_import(str(tmp_path / "missing.json"), console=console) is False
+
+
+def test_snapshot_import_with_real_scan_gate_exits_non_zero(monkeypatch, tmp_path):
+ """Two snapshot entries, both refused by the real ``should_allow_install`` policy, through
+ the real ``hermes skills snapshot import`` entry point: exit 1, nothing installed."""
+ from hermes_cli.main import main
+
+ installs, audit = _scan_gate_env(monkeypatch, tmp_path, verdict="dangerous")
+ monkeypatch.setattr(sys, "argv", ["hermes", "skills", "snapshot", "import",
+ _snapshot(tmp_path, ["risky-skill", "risky-skill-2"])])
+ with pytest.raises(SystemExit) as exc:
+ main()
+ assert exc.value.code == 1
+ assert installs == []
+ assert [row[0] for row in audit] == ["BLOCKED", "BLOCKED"]
# --- the process exit code, through the real `hermes` entry point ---
@@ -168,6 +300,8 @@ def test_uninstall_success(monkeypatch):
(["skills", "update"], "do_update", None, 0),
(["skills", "uninstall", "x", "--yes"], "do_uninstall", False, 1),
(["skills", "uninstall", "x", "--yes"], "do_uninstall", True, 0),
+ (["skills", "snapshot", "import", "snap.json"], "do_snapshot_import", False, 1),
+ (["skills", "snapshot", "import", "snap.json"], "do_snapshot_import", None, 0),
])
def test_cli_exit_code_follows_the_action_outcome(monkeypatch, argv, target, outcome, code):
from hermes_cli.main import main