Repository navigation
Expand file tree
/
Copy pathvalidate-plugin.py
More file actions
338 lines (295 loc) · 12.3 KB
/
Copy pathvalidate-plugin.py
File metadata and controls
338 lines (295 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
"""Local equivalent of the upstream `plugin-validate` action.
Why this exists (no upstream dependency): the official
`NousResearch/hermes-agent/.github/actions/plugin-validate` action
pip-installs `hermes-agent` from git (`pip install
"git+https://github.com/NousResearch/hermes-agent@<ref>"`), but
hermes-agent's own `setup.py` build guard rejects wheel/sdist builds
outside a sealed Nix derivation ("Building wheels or sdists for
hermes-agent is not supported ... distributed via the shell installer,
Docker image, or Nix"). The action has a single revision in history, so
there is no last-good action version to pin — every ref fails the same
way. This script therefore performs the same admission checks locally,
stdlib-only, without installing hermes-agent:
1. plugin.yaml manifest schema (required fields, requires_hermes spec,
config_schema / requires_env shapes),
2. stdlib-only imports (hermes-provided `agent` / `providers` exempt),
3. capability probe: import registers a provider and `register(ctx)`
runs probe-safe against a recording stub context.
Usage: python .github/scripts/validate-plugin.py [--path .]
"""
from __future__ import annotations
import argparse
import ast
import importlib.util
import os
import re
import sys
import tempfile
from pathlib import Path
try:
import yaml
except ImportError: # pragma: no cover - CI installs pyyaml explicitly
yaml = None
_VERSION_CLAUSE_RE = re.compile(
r"^(>=|<=|==|!=|>|<|~=)?\s*\d+(\.\d+)*(\.\*)?\s*$"
)
_UPPER_SNAKE_RE = re.compile(r"^[A-Z][A-Z0-9_]*$")
_CONFIG_TYPES = {
"str", "string", "int", "integer", "float", "number",
"bool", "boolean", "list", "array", "dict", "mapping", "map",
}
# Top-level modules provided by the Hermes runtime at load time (not pip
# dependencies), plus everything in the stdlib.
_RUNTIME_PROVIDED = {"agent", "providers"}
_failures: list[str] = []
_warnings: list[str] = []
def check(name: str, ok: bool, detail: str = "") -> None:
status = "PASS" if ok else "FAIL"
print(f"[{status}] {name}" + (f": {detail}" if detail else ""))
if not ok:
_failures.append(f"{name}: {detail}" if detail else name)
def warn(message: str) -> None:
print(f"[WARN] {message}")
_warnings.append(message)
def _requires_hermes_spec_valid(spec: str) -> bool:
return all(
_VERSION_CLAUSE_RE.match(clause.strip())
for clause in spec.split(",")
if clause.strip()
)
def _check_manifest(manifest: dict) -> None:
missing = [f for f in ("name", "version", "description") if not manifest.get(f)]
check(
"manifest fields",
not missing,
"name, version, description present"
if not missing
else f"plugin.yaml missing required field(s): {', '.join(missing)}",
)
spec = str(manifest.get("requires_hermes") or "").strip()
if not spec:
check("requires_hermes", True, "not declared")
else:
check(
"requires_hermes",
_requires_hermes_spec_valid(spec),
f"spec {spec!r} parses"
if _requires_hermes_spec_valid(spec)
else f"spec {spec!r} does not parse "
'(expected e.g. ">=0.21.3" or ">=0.21, <1.0")',
)
raw = manifest.get("config_schema")
if raw in (None, [], {}):
check("config schema", True, "not declared")
elif not isinstance(raw, dict):
check("config schema", False, "config_schema: must be a mapping")
else:
problems = []
for skey, entry in raw.items():
if not isinstance(entry, dict):
problems.append(f"config_schema.{skey}: must be a mapping")
continue
typ = entry.get("type")
if typ is not None and str(typ).lower() not in _CONFIG_TYPES:
problems.append(f"config_schema.{skey}: bad type {typ!r}")
if "required" in entry and not isinstance(entry["required"], bool):
problems.append(f"config_schema.{skey}: required must be bool")
check(
"config schema",
not problems,
"shape valid" if not problems else "; ".join(problems),
)
raw_env = manifest.get("requires_env") or []
if not isinstance(raw_env, list):
check("requires_env", False, "requires_env: must be a list")
else:
problems = []
for i, entry in enumerate(raw_env):
name = entry if isinstance(entry, str) else str((entry or {}).get("name") or "")
if not _UPPER_SNAKE_RE.match(name):
problems.append(f"requires_env[{i}]: {name!r} not UPPER_SNAKE_CASE")
check(
"requires_env",
not problems,
"all entries UPPER_SNAKE" if not problems else "; ".join(problems),
)
def _check_stdlib_only(plugin_dir: Path) -> None:
stdlib = set(sys.stdlib_module_names)
problems = []
for path in sorted(plugin_dir.glob("*.py")):
if path.name == "validate-plugin.py":
continue
try:
tree = ast.parse(path.read_text(encoding="utf-8"), filename=str(path))
except SyntaxError as exc:
problems.append(f"{path.name}: syntax error: {exc}")
continue
for node in ast.walk(tree):
if isinstance(node, ast.Import):
mods = [a.name.split(".")[0] for a in node.names]
elif isinstance(node, ast.ImportFrom):
if node.level:
continue # relative import within the plugin
mods = [node.module.split(".")[0]] if node.module else []
else:
continue
for mod in mods:
if mod not in stdlib and mod not in _RUNTIME_PROVIDED:
problems.append(f"{path.name}: third-party import {mod!r}")
check(
"stdlib-only",
not problems,
"no third-party runtime imports"
if not problems
else "; ".join(sorted(set(problems))),
)
class _RecordingContext:
plugin_config = {}
profile_name = "default"
plugin_id = "local_validate_probe"
def __init__(self) -> None:
self.tools: list[str] = []
self.hooks: list[str] = []
self.middleware: list[str] = []
def register_tool(self, name, *args, **kwargs) -> None:
self.tools.append(str(name))
def register_hook(self, hook_name, callback) -> None:
self.hooks.append(str(hook_name))
def register_middleware(self, kind, callback) -> None:
self.middleware.append(str(kind))
def register_command(self, name, *args, **kwargs) -> None:
self.tools.append(str(name))
register_cli_command = register_command
def get_config(self, key, default=None):
return default
def _check_capability_probe(plugin_dir: Path, manifest: dict) -> None:
init = plugin_dir / "__init__.py"
if not init.is_file():
warn("no __init__.py — capability probe skipped (manifest-only plugin)")
check("capability probe", True, "skipped (no __init__.py)")
return
import types
scratch = tempfile.mkdtemp(prefix="hermes-local-validate-")
os.environ["MUSE_CODE_SUB_CREDENTIALS"] = os.path.join(scratch, "absent.json")
os.environ.pop("MUSE_CODE_SUB_TOKEN", None)
recorded_providers: list[str] = []
providers_mod = types.ModuleType("providers")
def _record_provider(profile) -> None:
recorded_providers.append(str(getattr(profile, "name", profile)))
providers_mod.register_provider = _record_provider # type: ignore[attr-defined]
base_mod = types.ModuleType("providers.base")
class ProviderProfile: # minimal stub of the runtime base
def __init__(self, **kwargs) -> None:
self.__dict__.update(kwargs)
def fetch_models(self, **kwargs):
return None
base_mod.ProviderProfile = ProviderProfile # type: ignore[attr-defined]
saved = {k: sys.modules.get(k) for k in ("providers", "providers.base")}
sys.modules["providers"] = providers_mod
sys.modules["providers.base"] = base_mod
try:
spec = importlib.util.spec_from_file_location(
"local_validate_probe_plugin", str(init),
submodule_search_locations=[str(plugin_dir)],
)
module = importlib.util.module_from_spec(spec)
module.__path__ = [str(plugin_dir)]
sys.modules[spec.name] = module
try:
spec.loader.exec_module(module)
except Exception as exc: # noqa: BLE001 - report, don't crash
check("capability probe", False, f"import failed: {exc}")
return
if manifest.get("kind") == "model-provider":
check(
"capability probe",
bool(recorded_providers),
"import registered provider(s): " + ", ".join(recorded_providers)
if recorded_providers
else "model-provider plugin registered no ProviderProfile at import",
)
else:
check("capability probe", True, "import ok")
register = getattr(module, "register", None)
if register is None:
check("capability probe", False, "no register() function")
return
try:
ctx = _RecordingContext()
register(ctx)
except Exception as exc: # noqa: BLE001 - report, don't crash
check("capability probe", False, f"register() raised: {exc}")
return
for kind, attr, manifest_key in (
("tools", "tools", "provides_tools"),
("hooks", "hooks", "provides_hooks"),
("middleware", "middleware", "provides_middleware"),
):
declared = set(manifest.get(manifest_key) or [])
actual = set(getattr(ctx, attr))
undeclared = sorted(actual - declared)
if undeclared:
check(
f"declared {kind}",
False,
f"undeclared {kind} registered (not in {manifest_key}): "
+ ", ".join(undeclared),
)
else:
check(f"declared {kind}", True, "matches registrations")
unregistered = sorted(declared - actual)
if unregistered:
warn(
f"{manifest_key} declares {', '.join(unregistered)} "
"but register() did not register them"
)
finally:
sys.modules.pop("local_validate_probe_plugin", None)
for key, mod in saved.items():
if mod is None:
sys.modules.pop(key, None)
else:
sys.modules[key] = mod
def main() -> int:
parser = argparse.ArgumentParser(description="Local plugin validation")
parser.add_argument("--path", default=".", help="plugin directory")
args = parser.parse_args()
plugin_dir = Path(args.path).resolve()
if not plugin_dir.is_dir():
check("plugin directory", False, f"{plugin_dir} is not a directory")
return 1
manifest_file = plugin_dir / "plugin.yaml"
if not manifest_file.is_file():
manifest_file = plugin_dir / "plugin.yml"
if not manifest_file.is_file():
check("manifest", False, "no plugin.yaml in the plugin directory")
return 1
if yaml is None:
check("manifest", False, "pyyaml not installed (CI must pip install pyyaml)")
return 1
try:
manifest = yaml.safe_load(manifest_file.read_text(encoding="utf-8")) or {}
except Exception as exc: # noqa: BLE001 - report, don't crash
check("manifest", False, f"plugin.yaml does not parse: {exc}")
return 1
if not isinstance(manifest, dict):
check("manifest", False, "plugin.yaml top level must be a mapping")
return 1
check("manifest", True, "plugin.yaml parses")
_check_manifest(manifest)
_check_stdlib_only(plugin_dir)
for path in sorted(plugin_dir.glob("*.py")):
try:
compile(path.read_text(encoding="utf-8"), str(path), "exec")
except SyntaxError as exc:
check("compile", False, f"{path.name}: {exc}")
return 1
check("compile", True, "all modules compile")
_check_capability_probe(plugin_dir, manifest)
if _failures:
print(f"\nFAIL: {len(_failures)} check(s) failed")
return 1
print("\nPASS: plugin validated cleanly (local validator)")
return 0
if __name__ == "__main__":
raise SystemExit(main())