The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
feat(plugins): validate warns on imports of private Hermes core modules
Teknium1a4af83a8a9d
diff --git a/hermes_cli/plugin_validate.py b/hermes_cli/plugin_validate.pyindex e58af8ce855..a7c689c97ce 100644--- a/hermes_cli/plugin_validate.py+++ b/hermes_cli/plugin_validate.py@@ -25,6 +25,7 @@ from typing import Any, Dict, List, Optional, Tuplefrom hermes_cli.plugin_validate_core_override import check_core_overridefrom hermes_cli.plugin_validate_desktop import check_desktop_surface+from hermes_cli.plugin_validate_private_imports import check_private_importsfrom hermes_cli.plugin_validate_locales import check_language_packsfrom hermes_cli.plugins_manifest import _CONFIG_SCHEMA_TYPES@@ -535,6 +536,7 @@ def validate_plugin_dir(_check_trusted_inbound(report, recorded)_check_security_scan(report, plugin_dir)check_core_override(report, plugin_dir)+ check_private_imports(report, plugin_dir)check_desktop_surface(report, plugin_dir)check_language_packs(report, manifest, plugin_dir)from hermes_cli.plugin_isolation_audit import audit_plugin_dirdiff --git a/hermes_cli/plugin_validate_private_imports.py b/hermes_cli/plugin_validate_private_imports.pynew file mode 100644index 00000000000..d5d75544626--- /dev/null+++ b/hermes_cli/plugin_validate_private_imports.py@@ -0,0 +1,145 @@+"""Admission lint: name a plugin's imports of private Hermes core modules (a warning, never a refusal).++Catalog rule 9 ("public surfaces only") is enforced for runtime rebinds by+``plugin_validate_core_override``; an import of a core internal (``tools.tirith_security``,+``hermes_cli.cli_config_load._parse_service_tier_config``) passes that check yet breaks the plugin on+the release that moves or removes the internal. This static pass lists those imports, with+file:line, so reviewers see the coupling at admission. Imports of the plugin's own modules, the+standard library and third-party packages are never flagged; neither are ``TYPE_CHECKING`` imports.+"""++from __future__ import annotations++import ast+import tomllib+from functools import lru_cache+from pathlib import Path++from hermes_cli.plugin_validate_core_override import _hermes_top_level, _python_files++# WHY: "public" must mean what Hermes documents for plugin authors, not a reviewer's taste.+# Derived from every Hermes-module ``import`` in website/docs/developer-guide/**/*.md and+# website/docs/user-guide/features/plugins*.md code blocks (plus extending-the-dashboard.md, which+# desktop-plugin-sdk.md names as the plugin backend reference), and what PluginContext hands out in+# hermes_cli/plugins.py (property/return types, base classes its ``register_*`` methods check), and+# the names those plugin pages tell authors to call, raise, return or subclass in prose. ``module`` entries allow the module (underscore names excepted);+# ``module:Name`` entries allow one name. Re-harvest when those docs or PluginContext change.+_PUBLIC = frozenset({+ # developer-guide / plugins docs+ "agent.account_usage", "agent.browser_provider", "agent.context_engine", "agent.credential_pool",+ "agent.image_gen_provider", "agent.memory_manager", "agent.memory_provider", "agent.secret_scope",+ "agent.secret_sources.base", "agent.subagent_lifecycle", "agent.terminal_env_provider",+ "agent.transports", "agent.transports.chat_completions", "agent.video_gen_provider",+ "agent.web_search_provider", "cli", "gateway.config", "gateway.platform_registry",+ "gateway.platforms._shared", "gateway.platforms.base", "gateway.platforms.event",+ "gateway.platforms.helpers", "gateway.status", "hermes_cli.auth_oauth_pkce_plugin",+ "hermes_cli.config", "hermes_cli.plugin_events", "hermes_cli.plugin_provider_requests",+ "hermes_constants", "hermes_platform", "hermes_state", "plugins.plugin_storage", "plugins.plugin_utils", "pm",+ "providers", "providers.base", "tools.browser_supervisor", "tools.browser_supervisor_capture",+ "tools.computer_use.backend", "tools.registry",+ # PluginContext (hermes_cli/plugins.py)+ "agent.context_references", "agent.plugin_llm", "agent.transcription_provider", "agent.tts_provider",+ "cron.execution_identity",+ "hermes_cli.dashboard_auth", "hermes_cli.platform_actions", "hermes_cli.plugins:PluginContext",+ "hermes_cli.plugins_ledger:PluginRegistration", "hermes_cli.plugins_state:PluginState",+ # prose in memory-provider / model-provider / terminal-environment plugin pages+ "agent.runtime_cwd:resolve_agent_cwd", "agent.usage_pricing:CostResult", "hermes_cli.auth_constants:AuthError",+ "tools.environments.base:BaseEnvironment", "tools.environments:BaseEnvironment", # package ``__all__``+ "utils:atomic_json_write", "utils:read_json_or_empty",+})++_SHOWN = 10+++@lru_cache(maxsize=1)+def _hermes_packages() -> frozenset:+ """Hermes-owned top-level names: pyproject's ``packages.find`` includes plus the root modules+ setup.py ships as ``py-modules`` (every root ``*.py``)."""+ root = Path(__file__).resolve().parents[1]+ try:+ pyproject = tomllib.loads((root / "pyproject.toml").read_text(encoding="utf-8-sig"))+ packages = {name.split(".")[0] for name in pyproject["tool"]["setuptools"]["packages"]["find"]["include"]}+ except (OSError, KeyError, ValueError):+ packages = set(_hermes_top_level()) - {"hermes_plugins", "tests", "evals"}+ return frozenset((packages | {p.stem for p in root.glob("*.py")}) - {"setup", "conftest"})+++def _local_modules(rels) -> set[str]:+ """Every dotted name under which one of the plugin's own files could be imported."""+ names = set()+ for rel in rels:+ parts = rel.with_suffix("").parts+ parts = parts[:-1] if parts[-1] == "__init__" else parts+ names |= {".".join(parts[start:end]) for start in range(len(parts)) for end in range(start + 1, len(parts) + 1)}+ return names+++def _type_checking_nodes(tree: ast.AST) -> set[int]:+ skipped: set[int] = set()+ for node in ast.walk(tree):+ test = getattr(node, "test", None)+ if isinstance(node, ast.If) and (getattr(test, "id", None) or getattr(test, "attr", None)) == "TYPE_CHECKING":+ skipped |= {id(n) for stmt in node.body for n in ast.walk(stmt)}+ return skipped+++def _imported(node: ast.AST) -> list[tuple[str, str]]:+ """``(module, name)`` pairs an import node binds; ``name`` is "" for ``import module``."""+ if isinstance(node, ast.Import):+ return [(alias.name, "") for alias in node.names]+ if isinstance(node, ast.ImportFrom) and not node.level and node.module:+ return [(node.module, alias.name) for alias in node.names]+ if isinstance(node, ast.Call) and node.args and isinstance(node.args[0], ast.Constant) \+ and isinstance(node.args[0].value, str) and ast.unparse(node.func) in ("importlib.import_module", "import_module"):+ return [(node.args[0].value, "")]+ return []+++def _private_target(module: str, name: str, core: frozenset, local: set[str]) -> str:+ """The dotted target to report, or "" when the import is public, local or not Hermes's."""+ target = f"{module}.{name}" if name else module+ hermes = module.split(".")[0] in core and module not in local+ public = target in _PUBLIC or f"{module}:{name}" in _PUBLIC \+ or (module in _PUBLIC and not name.startswith("_")) # ``from gateway.platforms import base`` too+ return target if hermes and not public else ""+++def private_import_findings(plugin_dir: Path) -> list[str]:+ """``["<rel>:<line> <module>[.<name>]", ...]`` for every import of a private Hermes core module:+ the first site of each distinct module first (so a truncated list still shows the breadth),+ then the rest, each group in file/line order."""+ plugin_dir = Path(plugin_dir)+ # Hidden dirs (``.github/`` CI helpers) never load inside Hermes.+ files = [(path, rel) for path, rel in _python_files(plugin_dir) if not any(p.startswith(".") for p in rel.parts[:-1])]+ local = _local_modules(rel for _path, rel in files)+ core = _hermes_packages()+ found: set[tuple[str, int, str, str]] = set()+ for path, rel in files:+ try:+ tree = ast.parse(path.read_text(encoding="utf-8-sig", errors="replace"))+ except (SyntaxError, ValueError):+ continue+ skipped = _type_checking_nodes(tree)+ for node in ast.walk(tree):+ if id(node) in skipped:+ continue+ for module, name in _imported(node):+ target = _private_target(module, name, core, local)+ if target:+ found.add((rel.as_posix(), getattr(node, "lineno", 0), target, module))+ seen: set[str] = set()+ first = [site for site in sorted(found) if site[3] not in seen and not seen.add(site[3])]+ rest = sorted(found.difference(first))+ return [f"{rel}:{line} {target}" for rel, line, target, _module in first + rest]+++def check_private_imports(report, plugin_dir: Path) -> None:+ """Warn (never fail) when the plugin imports private Hermes core modules."""+ hits = private_import_findings(plugin_dir)+ if hits:+ more = f"; and {len(hits) - _SHOWN} more" if len(hits) > _SHOWN else ""+ report.warn(+ "private core imports (catalog rule 9: public surfaces only; these can break on any release): "+ + "; ".join(hits[:_SHOWN]) + more+ + ". Use ctx.* / documented plugin APIs, or open an issue for the seam you need."+ )diff --git a/tests/hermes_cli/test_plugin_validate_private_imports.py b/tests/hermes_cli/test_plugin_validate_private_imports.pynew file mode 100644index 00000000000..5ef360d0639--- /dev/null+++ b/tests/hermes_cli/test_plugin_validate_private_imports.py@@ -0,0 +1,70 @@+"""``hermes plugins validate`` warns (never fails) on imports of private Hermes core modules."""++from __future__ import annotations++from pathlib import Path++import hermes_yaml as yaml++from hermes_cli.plugin_validate import validate_plugin_dir+++def _plugin(root: Path, files: dict[str, str]) -> Path:+ root.mkdir(parents=True)+ (root / "plugin.yaml").write_text(yaml.safe_dump({"name": root.name, "version": "1.0.0", "description": "x"}),+ encoding="utf-8")+ files = {"__init__.py": "def register(ctx):\n pass\n", **files}+ for rel, text in files.items():+ (root / rel).parent.mkdir(parents=True, exist_ok=True)+ (root / rel).write_text(text, encoding="utf-8")+ return root+++def _private_warnings(report) -> list[str]:+ return [w for w in report.warnings if w.startswith("private core imports")]+++def test_private_core_import_warns_with_file_and_line_but_validate_passes(tmp_path):+ plugin = _plugin(tmp_path / "router", {"lib/agent_runner.py": (+ "import json\n"+ "def run():\n"+ " from tools.tirith_security import ensure_installed\n"+ " from hermes_cli.config import _private_helper\n"+ " return ensure_installed, _private_helper\n"+ )})++ report = validate_plugin_dir(plugin)++ assert report.ok, report.failures+ [warning] = _private_warnings(report)+ assert "lib/agent_runner.py:3 tools.tirith_security.ensure_installed" in warning+ assert "lib/agent_runner.py:4 hermes_cli.config._private_helper" in warning+ assert "catalog rule 9" in warning+++def test_documented_public_stdlib_and_own_package_imports_are_not_flagged(tmp_path):+ plugin = _plugin(tmp_path / "clean", {+ "__init__.py": (+ "import os, json\n"+ "from typing import TYPE_CHECKING\n"+ "import requests\n"+ "from agent.memory_provider import MemoryProvider\n" # documented provider base class+ "from gateway.platforms import base\n" # documented module, imported from its package+ "from hermes_cli.plugins import PluginContext\n" # what register() receives+ "from clean_lib.helpers import local_helper\n" # the plugin's own package+ "from .client import Client\n"+ "if TYPE_CHECKING:\n"+ " from run_agent import AIAgent\n"+ "def register(ctx):\n"+ " from hermes_cli.plugins import get_plugin_manager\n" # same module, undocumented name+ " return get_plugin_manager\n"+ ),+ "clean_lib/__init__.py": "",+ "clean_lib/helpers.py": "def local_helper():\n pass\n",+ "client.py": "class Client:\n pass\n",+ })++ [warning] = _private_warnings(validate_plugin_dir(plugin))++ sites = warning.split("release): ", 1)[1].split(". Use ctx.*")[0].split("; ")+ assert sites == ["__init__.py:12 hermes_cli.plugins.get_plugin_manager"], sites