Files
hermes-agent/tests/hermes_cli/test_plugin_dev.py
T
DevKJ a00ae1d519 fix(cli): stop plugins doctor from copying a non-plugin directory
`hermes plugins doctor` defaults its target to `.`, and
resolve_plugin_path accepted any directory that existed. Doctor then
copytree'd the resolved path into a temporary HERMES_HOME *before* the
runtime got to reject it, so running the command from a directory that
is not a plugin copied that whole tree.

Run from $HOME on macOS this copies the home directory, and because
`Library/CloudStorage` is not excluded it also materializes every
cloud-only Google Drive/iCloud placeholder. Observed locally: 461 GB
written to /private/var/folders and still growing when the process was
killed, on a machine with 49 GB free.

Resolve now requires a manifest before returning a path, mirroring
PluginManager._scan_directory: `plugin.yaml`/`plugin.yml`/`plugin.json`
in the directory itself, or in one immediate subdirectory for the
category layout. Plugin-id candidates are only tried for values that can
be an id, since joining `.` onto a plugins root resolves to the root and
would hand Doctor every installed plugin at once.

Non-plugin targets now fail with a clear message and no copy.
2026-08-31 08:41:15 -07:00

218 lines
7.0 KiB
Python

from __future__ import annotations
import argparse
from pathlib import Path
from hermes_cli.subcommands.plugins import build_plugins_parser
def _parse_plugins_args(*argv: str):
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest="command")
build_plugins_parser(subparsers, cmd_plugins=lambda args: None)
return parser.parse_args(["plugins", *argv])
def test_plugins_parser_exposes_doctor() -> None:
doctor = _parse_plugins_args("doctor", "sample", "--ci")
assert (doctor.plugins_action, doctor.target, doctor.ci) == (
"doctor",
"sample",
True,
)
def test_doctor_uses_registration_to_reject_bad_hook_and_callback_signature(
tmp_path: Path,
) -> None:
from hermes_cli.plugin_dev import doctor_plugin
plugin = tmp_path / "bad-plugin"
plugin.mkdir()
(plugin / "plugin.yaml").write_text(
"\n".join(
[
"name: bad-plugin",
"version: 0.1.0",
"description: broken contract",
"provides_hooks:",
" - typo_hook",
" - pre_tool_call",
]
)
+ "\n",
encoding="utf-8",
)
(plugin / "__init__.py").write_text(
"def callback(tool_name):\n"
" return None\n\n"
"def register(ctx):\n"
" ctx.register_hook('typo_hook', callback)\n"
" ctx.register_hook('pre_tool_call', callback)\n",
encoding="utf-8",
)
report = doctor_plugin(plugin)
messages = "\n".join(f.message for f in report.findings)
assert report.ok is False
assert "unknown hook 'typo_hook'" in messages
assert "must accept **kwargs" in messages
def test_doctor_accepts_manifest_defaults_from_runtime_parser(tmp_path: Path) -> None:
from hermes_cli.plugin_dev import doctor_plugin
plugin = tmp_path / "minimal"
plugin.mkdir()
(plugin / "plugin.yaml").write_text("name: minimal\n", encoding="utf-8")
(plugin / "__init__.py").write_text(
"def register(ctx):\n pass\n", encoding="utf-8"
)
report = doctor_plugin(plugin)
assert report.ok, report.format_text()
assert report.manifest is not None
assert report.manifest.kind == "standalone"
def test_doctor_restores_global_tool_policy_and_module_state(tmp_path: Path) -> None:
import sys
from hermes_cli.plugin_dev import doctor_plugin
from tools.registry import registry
target = tmp_path / "cleanup-plugin"
target.mkdir()
(target / "plugin.yaml").write_text(
"name: cleanup-plugin\nprovides_tools: [cleanup_plugin_ping]\n",
encoding="utf-8",
)
(target / "__init__.py").write_text(
"import json\n\n"
"def ping(args, **kwargs):\n return json.dumps({'ok': True})\n\n"
"def register(ctx):\n"
" ctx.register_tool(name='cleanup_plugin_ping', toolset='cleanup', "
"schema={'name': 'cleanup_plugin_ping', 'description': 'test', "
"'parameters': {'type': 'object'}}, handler=ping)\n",
encoding="utf-8",
)
before_policy = dict(registry._plugin_override_policy)
before_modules = {
name
for name in sys.modules
if name == "hermes_plugins" or name.startswith("hermes_plugins.")
}
report = doctor_plugin(target)
assert report.ok, report.format_text()
assert report.registered_tools == ("cleanup_plugin_ping",)
assert registry.get_entry("cleanup_plugin_ping") is None
assert registry._plugin_override_policy == before_policy
after_modules = {
name
for name in sys.modules
if name == "hermes_plugins" or name.startswith("hermes_plugins.")
}
assert after_modules == before_modules
def test_doctor_blocks_live_network(tmp_path: Path) -> None:
from hermes_cli.plugin_dev import doctor_plugin
plugin = tmp_path / "network-plugin"
plugin.mkdir()
(plugin / "plugin.yaml").write_text("name: network-plugin\n", encoding="utf-8")
(plugin / "__init__.py").write_text(
"import socket\n\n"
"def register(ctx):\n"
" socket.create_connection(('example.com', 443))\n",
encoding="utf-8",
)
report = doctor_plugin(plugin)
assert report.ok is False
assert "network access is disabled while Plugin Doctor runs" in report.format_text()
def test_resolve_rejects_directory_without_manifest(tmp_path: Path) -> None:
"""A non-plugin directory must not resolve — Doctor copies what it resolves."""
import pytest
from hermes_cli.plugin_dev import resolve_plugin_path
not_a_plugin = tmp_path / "home"
(not_a_plugin / "Documents").mkdir(parents=True)
(not_a_plugin / "Documents" / "notes.txt").write_text("x", encoding="utf-8")
with pytest.raises(FileNotFoundError) as excinfo:
resolve_plugin_path(not_a_plugin)
assert "holds no plugin manifest" in str(excinfo.value)
def test_doctor_default_target_does_not_copy_cwd(
tmp_path: Path, monkeypatch
) -> None:
"""``hermes plugins doctor`` with no argument defaults to ``.``.
Before the manifest guard, that copied the whole working directory into
a temporary HERMES_HOME — running it from ``$HOME`` copied the home
directory, cloud-storage placeholders included.
"""
import os
import shutil
from hermes_cli import plugin_dev
workdir = tmp_path / "workdir"
(workdir / "big").mkdir(parents=True)
(workdir / "big" / "payload.bin").write_text("x" * 1024, encoding="utf-8")
monkeypatch.chdir(workdir)
copied: list[tuple[str, str]] = []
real_copytree = shutil.copytree
def _tracking_copytree(src, dst, *args, **kwargs):
copied.append((os.fspath(src), os.fspath(dst)))
return real_copytree(src, dst, *args, **kwargs)
monkeypatch.setattr(plugin_dev.shutil, "copytree", _tracking_copytree)
report = plugin_dev.doctor_plugin()
assert report.ok is False
assert copied == []
def test_resolve_accepts_category_layout(tmp_path: Path) -> None:
"""A category directory holds no manifest itself but discovery finds one."""
from hermes_cli.plugin_dev import resolve_plugin_path
category = tmp_path / "image_gen"
plugin = category / "openai"
plugin.mkdir(parents=True)
(plugin / "plugin.yaml").write_text("name: openai\n", encoding="utf-8")
assert resolve_plugin_path(category) == category.resolve()
def test_resolve_prefers_installed_id_over_unrelated_local_dir(
tmp_path: Path, monkeypatch
) -> None:
"""A same-named local directory must not shadow the installed plugin."""
from hermes_cli import plugin_dev
hermes_home = tmp_path / "hermes-home"
installed = hermes_home / "plugins" / "sample"
installed.mkdir(parents=True)
(installed / "plugin.yaml").write_text("name: sample\n", encoding="utf-8")
workdir = tmp_path / "workdir"
(workdir / "sample").mkdir(parents=True)
monkeypatch.chdir(workdir)
monkeypatch.setattr(plugin_dev, "get_hermes_home", lambda: hermes_home)
assert plugin_dev.resolve_plugin_path("sample") == installed.resolve()