With one worker serving a whole default-plugins pass, every request still re-read every home: each home's config.yaml (selection read and publication snapshot), each enabled plugin's manifest and pyproject.toml, core's pyproject.toml once per member, a walk of the bundled plugins dir per home with a flat tree, and the marker environment of the target interpreter in a child process. That kept the pass quadratic in the profile count. - read_home_selection caches each local config.yaml's validated selection by the file's bytes (only when the bytes are unchanged across the parse; a remote config backend is never cached), returning copies. - read_native_manifest and the module's pyproject reads parse each distinct file text once (stdlib only: PM's bootstrap reads these before the app's packages exist). - selection_projection is memoized by the document bytes. - enabled_selection judges every home's flat trees against one walk of the bundled dir (plugin_identity.one_bundled_walk). - _target_environment asks each interpreter (by resolved path and stat) once.
280 lines
14 KiB
Python
280 lines
14 KiB
Python
"""Plugin identity: which plugin a ``plugins.enabled`` / ``plugins.disabled`` entry names.
|
|
|
|
ONE rule for every reader of those lists: the loader's gate (``plugins_discovery.gate_manifest``), PM's
|
|
dependency selection (``pm.plugins_state``), the default-plugin installer (``default_plugins._settled``),
|
|
the left-core migration's notices and installers, and ``hermes plugins`` status all ask
|
|
:func:`plugin_listed` about a plugin's :func:`plugin_identity`.
|
|
|
|
A plugin's identity is its registry key (:func:`tree_key`: path-derived ``<category>/<dir>``, or the manifest
|
|
name for a flat tree), its manifest ``name:`` and, for a plugin that left core, the bundled key and manifest
|
|
name core shipped it under (``left_core_migration.legacy_plugin_keys``). An entry names the plugin exactly when
|
|
it is one of those strings: no case folding, no bare leaf of a nested key (``team/qqbot`` is not ``qqbot``),
|
|
and a blank entry names nothing. A reader that starts from a config entry resolves the installed tree first
|
|
(:func:`installed_trees`, discovery's walk) and asks about THAT tree's identity, so a fork or a nested copy is
|
|
judged by its own names, as the loader judges it.
|
|
|
|
Import-light on purpose: PM's isolated runtime reads selections through here without the agent's packages.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import contextvars
|
|
import json
|
|
import os
|
|
import stat
|
|
from contextlib import contextmanager
|
|
from pathlib import Path
|
|
from typing import Any, Iterator, Optional
|
|
|
|
# Per-harness manifest directories plugin repos ship for OTHER agent harnesses (e.g. obra/superpowers keeps one
|
|
# plugin.json per harness). Their plugin.json is not an Agent Plugins v1 manifest and can never validate, so
|
|
# parsing it on every discovery pass only spams warnings (#101962).
|
|
_FOREIGN_HARNESS_MANIFEST_DIRS = frozenset({
|
|
".claude-plugin", ".codex-plugin", ".cursor-plugin", ".devin-plugin", ".kimi-plugin",
|
|
})
|
|
# An installer's staging copy beside the plugins it replaces (``plugins_cmd_install``): read as a category, it
|
|
# would answer to the plugin's own name mid-install.
|
|
_STAGING_PREFIXES = (".install-", ".update-")
|
|
|
|
|
|
def is_skipped_plugin_dir(name: str) -> bool:
|
|
"""True for directory names discovery never treats as plugins. Cache/dunder dirs (__pycache__,
|
|
__MACOSX__, …) can raise PermissionError when walked and take down every later tool call
|
|
(#86996); foreign-harness dirs hold another tool's manifest convention; staging dirs are transient."""
|
|
return ((name.startswith("__") and name.endswith("__")) or name in _FOREIGN_HARNESS_MANIFEST_DIRS
|
|
or name.startswith(_STAGING_PREFIXES))
|
|
|
|
|
|
def tree_key(prefix: str, dir_name: str, manifest_name: str) -> str:
|
|
"""Registry key of a plugin tree: ``<prefix>/<dir>`` under a category dir, else its manifest name."""
|
|
return f"{prefix}/{dir_name}" if prefix else manifest_name
|
|
|
|
|
|
def plugin_identity(key: str, name: str = "") -> frozenset[str]:
|
|
"""Every entry that names the plugin with registry *key* and manifest *name* (default: the key)."""
|
|
from hermes_cli.left_core_migration import legacy_plugin_keys
|
|
return frozenset(n for n in (key, name or key, *legacy_plugin_keys(key)) if isinstance(n, str) and n.strip())
|
|
|
|
|
|
def manifest_identity(manifest) -> frozenset[str]:
|
|
""":func:`plugin_identity` of a discovered manifest."""
|
|
return plugin_identity(manifest.key or manifest.name, manifest.name)
|
|
|
|
|
|
def plugin_listed(identity: frozenset[str], entries: Any) -> bool:
|
|
"""A ``plugins.enabled`` / ``plugins.disabled`` value (a list; anything else lists nothing) names the
|
|
plugin with *identity*."""
|
|
return isinstance(entries, (list, tuple, set, frozenset)) and any(
|
|
isinstance(entry, str) and entry in identity for entry in entries)
|
|
|
|
|
|
# Manifest file -> (utils.file_cache_version, name) of each native manifest this process parsed. Every selection
|
|
# read walks every home's plugins dir, and each flat tree there makes _winners walk the whole bundled set:
|
|
# re-parsing the same unchanged YAML once per home made each PM request O(homes x bundled manifests), and a
|
|
# default-plugins pass (two requests per install, one install per home) quadratic in the profile count. A changed
|
|
# file has a new version, so it is read again.
|
|
_MANIFEST_NAMES: dict[Path, tuple[tuple, Optional[str]]] = {}
|
|
_MANIFEST_NAMES_MAX = 4096
|
|
|
|
|
|
def _native_manifest_name(manifest_file: Path, child: Path) -> Optional[str]:
|
|
"""``name:`` of the native manifest *manifest_file* (default: *child*'s dir name), ``None`` when malformed."""
|
|
from pm.plugin_declarations import read_native_manifest
|
|
from utils import file_cache_version
|
|
try:
|
|
signature = file_cache_version(manifest_file)
|
|
except OSError as exc:
|
|
raise ValueError(f"could not read plugin manifest: {manifest_file}") from exc
|
|
cached = _MANIFEST_NAMES.get(manifest_file)
|
|
if cached is not None and cached[0] == signature:
|
|
return cached[1]
|
|
try:
|
|
name: Optional[str] = str(read_native_manifest(manifest_file).get("name", child.name))
|
|
except ValueError as exc:
|
|
if isinstance(exc.__cause__, OSError):
|
|
raise
|
|
name = None # malformed: discovery warns and skips it
|
|
if len(_MANIFEST_NAMES) >= _MANIFEST_NAMES_MAX:
|
|
_MANIFEST_NAMES.clear()
|
|
_MANIFEST_NAMES[manifest_file] = (signature, name)
|
|
return name
|
|
|
|
|
|
def _manifest_name(child: Path) -> Optional[str]:
|
|
"""The ``name:`` discovery gives the tree at *child* (``plugin.yaml``, else portable ``plugin.json``), ``""``
|
|
when it has neither manifest, ``None`` when its manifest is not a plugin discovery loads. Raises
|
|
``ValueError`` on an unreadable tree."""
|
|
from pm.plugin_declarations import native_manifest_file
|
|
manifest_file = native_manifest_file(child)
|
|
if manifest_file is not None:
|
|
return _native_manifest_name(manifest_file, child)
|
|
portable = child / "plugin.json"
|
|
try:
|
|
if not (portable.exists() or portable.is_symlink()):
|
|
return ""
|
|
data = json.loads(portable.read_text(encoding="utf-8-sig"))
|
|
except OSError as exc:
|
|
raise ValueError(f"could not read plugin manifest: {portable}") from exc
|
|
except ValueError:
|
|
return None
|
|
name = data.get("name") if isinstance(data, dict) else None
|
|
return name if isinstance(name, str) and name else None
|
|
|
|
|
|
def _walk(path: Path, prefix: str = "", depth: int = 0, *, skip_names: frozenset = frozenset(),
|
|
unreadable: Optional[list] = None) -> Iterator[tuple[Path, str, str]]:
|
|
"""``(tree, key, manifest name)`` in discovery's order and nesting (``plugins_discovery.scan_directory``:
|
|
a manifest-less dir at the top recurses once as a category). A folder discovery cannot read is skipped as
|
|
discovery skips it, and noted in *unreadable*: only a reader that selects it may refuse
|
|
(``pm.workspace.selected_plugin_dir``). Raises ``ValueError`` when *path* itself cannot be listed."""
|
|
try:
|
|
children = sorted(path.iterdir())
|
|
except FileNotFoundError:
|
|
return
|
|
except OSError as exc:
|
|
if depth:
|
|
if unreadable is not None:
|
|
unreadable.append(path)
|
|
return
|
|
raise ValueError(f"could not inspect plugin directory: {path}") from exc
|
|
for child in children:
|
|
if is_skipped_plugin_dir(child.name) or (depth == 0 and child.name in skip_names):
|
|
continue
|
|
try:
|
|
if not stat.S_ISDIR(child.stat().st_mode):
|
|
continue
|
|
name = _manifest_name(child)
|
|
except FileNotFoundError:
|
|
continue
|
|
except (OSError, ValueError):
|
|
if unreadable is not None:
|
|
unreadable.append(child)
|
|
continue
|
|
if name:
|
|
yield child, tree_key(prefix, child.name, name), name
|
|
elif name == "" and depth == 0:
|
|
yield from _walk(child, tree_key(prefix, child.name, child.name), depth + 1, unreadable=unreadable)
|
|
|
|
|
|
# Bundled top-level dirs with their own discovery (memory providers, context engines, ...): never plugins of the
|
|
# general sweep (``plugins_discovery.collect_directory_manifests``).
|
|
BUNDLED_OWN_DISCOVERY = frozenset({
|
|
"memory", "context_engine", "model-providers", "cron_providers", "computer_use", "config_backends",
|
|
})
|
|
|
|
|
|
def bundled_plugins_dir() -> Path:
|
|
"""Bundled ``plugins/`` dir: ``HERMES_BUNDLED_PLUGINS`` (Nix wrapper / packaged installs, read-only store
|
|
paths) first, else the in-repo path. Stdlib only, so PM's isolated runtime resolves it too."""
|
|
from hermes_cli._startup_fast import project_root_str
|
|
return Path(os.getenv("HERMES_BUNDLED_PLUGINS") or Path(project_root_str()) / "plugins")
|
|
|
|
|
|
# Set inside :func:`one_bundled_walk`: the walk that block's reads share, once one of them took it.
|
|
_BUNDLED_WALK: contextvars.ContextVar[Optional[dict]] = contextvars.ContextVar("bundled_walk", default=None)
|
|
|
|
|
|
@contextmanager
|
|
def one_bundled_walk() -> Iterator[None]:
|
|
"""Inside this block the bundled plugins dir is walked at most once. A read of every home's selection
|
|
(``pm.plugins_state.enabled_selection``) judges each home's flat trees against the bundled set; walking it
|
|
again per home made every PM request O(homes x bundled dirs)."""
|
|
token = _BUNDLED_WALK.set({}) if _BUNDLED_WALK.get() is None else None
|
|
try:
|
|
yield
|
|
finally:
|
|
if token is not None:
|
|
_BUNDLED_WALK.reset(token)
|
|
|
|
|
|
def _bundled_dirs() -> dict[str, str]:
|
|
"""Registry key -> directory name of every bundled plugin the general sweep reads."""
|
|
shared = _BUNDLED_WALK.get()
|
|
if shared is not None and "dirs" in shared:
|
|
return shared["dirs"]
|
|
try:
|
|
found = _walk(bundled_plugins_dir(), skip_names=BUNDLED_OWN_DISCOVERY)
|
|
dirs = {key: tree.name for tree, key, _name in found}
|
|
except ValueError:
|
|
dirs = {} # discovery reads no bundled plugin from an unlistable dir either
|
|
if shared is not None:
|
|
shared["dirs"] = dirs
|
|
return dirs
|
|
|
|
|
|
def _winners(plugins_dir: Path, unreadable: Optional[list] = None) -> dict[str, tuple[Path, str]]:
|
|
"""Registry key -> ``(tree, manifest name)`` of every user tree discovery loads from *plugins_dir*, by
|
|
``resolve_manifest_winners``' rule: on a key collision the later tree wins, and a flat tree claiming a bundled
|
|
plugin's key from a differently named directory is an impostor, skipped."""
|
|
winners: dict[str, tuple[Path, str]] = {}
|
|
bundled: Optional[dict[str, str]] = None
|
|
for tree, key, name in _walk(Path(plugins_dir), unreadable=unreadable):
|
|
if "/" not in key: # a flat tree's key is its manifest name: it may claim a bundled one
|
|
bundled = _bundled_dirs() if bundled is None else bundled
|
|
if key in bundled and bundled[key] != tree.name:
|
|
continue
|
|
winners[key] = (tree, name)
|
|
return winners
|
|
|
|
|
|
def installed_trees(plugins_dir: Path, unreadable: Optional[list] = None) -> dict[Path, frozenset[str]]:
|
|
"""Every plugin tree discovery loads from the user plugins dir *plugins_dir* (:func:`_winners`), with its
|
|
identity. A folder it cannot read is left out, as discovery leaves it out, and noted in *unreadable*."""
|
|
return {tree: plugin_identity(key, name) for key, (tree, name) in _winners(plugins_dir, unreadable).items()}
|
|
|
|
|
|
def registry_key(plugins_dir: Path, tree: Path) -> str:
|
|
"""The registry key discovery gives *tree* under *plugins_dir* (unique per loaded tree), ``""`` for a tree
|
|
it does not load."""
|
|
return next((key for key, (found, _name) in _winners(plugins_dir).items() if found == Path(tree)), "")
|
|
|
|
|
|
def _same_dir(path: Path, tree: Path) -> bool:
|
|
"""*path* is the directory *tree* (``os.path.samefile``: a case-insensitive filesystem's other spelling too)."""
|
|
try:
|
|
return path == tree or os.path.samefile(path, tree)
|
|
except OSError:
|
|
return False
|
|
|
|
|
|
def path_tree(plugins_dir: Path, entry: str, trees: dict[Path, frozenset[str]], *,
|
|
installing: Optional[Path] = None) -> Optional[tuple[Path, frozenset[str]]]:
|
|
"""``(tree, identity)`` of the dir in *plugins_dir* that *entry* selects by its path, for a dir discovery loads
|
|
no plugin from (a manifest-less tree, or the install in flight at that path; *trees* are the ones it does
|
|
load): the home's own ``<entry>`` first, then the dir of the catalog plugin a core-era key names
|
|
(``google_meet`` -> ``google-meet``). Each answers to the name of its path (:func:`plugin_identity`), so the
|
|
home's own ``<entry>`` beats the alias and is never turned off by the catalog plugin's names. ``None`` when
|
|
neither is there, or *entry* is not a relative path inside *plugins_dir*."""
|
|
from hermes_cli.left_core_migration import legacy_plugin_alias
|
|
if not entry.strip() or Path(entry).is_absolute() or ".." in Path(entry).parts:
|
|
return None
|
|
alias = legacy_plugin_alias(entry)
|
|
for relative in (entry, *([alias] if alias else [])):
|
|
candidate = Path(plugins_dir) / relative
|
|
if installing is not None and candidate.resolve() == Path(installing).resolve():
|
|
return candidate, plugin_identity(relative, entry if relative == entry else "")
|
|
if any(_same_dir(candidate, tree) for tree in trees):
|
|
continue # a plugin discovery loads: it answers to its own names, not its path
|
|
try:
|
|
present = stat.S_ISDIR(candidate.stat().st_mode)
|
|
except FileNotFoundError:
|
|
present = False
|
|
except OSError as exc:
|
|
raise ValueError(f"could not inspect plugin directory: {candidate}") from exc
|
|
if present:
|
|
return candidate, plugin_identity(relative, entry if relative == entry else "")
|
|
return None
|
|
|
|
|
|
def tree_identity(plugins_dir: Path, tree: Path, *, default: str = "") -> frozenset[str]:
|
|
"""Identity of the installed *tree* under *plugins_dir*. A tree discovery does not load (no manifest yet,
|
|
an unreadable dir) has no loader verdict to agree with: it answers to its dir name (the name its manifest
|
|
would default to) and *default*, the catalog name it was installed under."""
|
|
try:
|
|
found = installed_trees(plugins_dir).get(Path(tree))
|
|
except ValueError:
|
|
found = None
|
|
if found is not None:
|
|
return found
|
|
return plugin_identity(Path(tree).name) | (plugin_identity(default) if default else frozenset())
|