114 lines
4.4 KiB
Python
114 lines
4.4 KiB
Python
"""
|
|
The owner's skills a guest run gets, as a throwaway Claude Code plugin.
|
|
|
|
A guest run starts in its project (``~/projects/<slug>``), so Claude Code only
|
|
loads that project's ``.claude/skills`` — the lab's own skills live in the
|
|
homelab repo and never reach it. The scope's ``skills`` list names the ones
|
|
the owner shared; :func:`plugin_dir` symlinks them into
|
|
``<cache>/<hash>/skills/`` next to a ``.claude-plugin/plugin.json`` named
|
|
``shared``, and the run is launched with ``--plugin-dir <that dir>``: the
|
|
skills show up as ``shared:<name>``.
|
|
|
|
One dir per distinct set (keyed by its hash), rebuilt in place when a link is
|
|
missing, so concurrent runs never see a half-written plugin.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import re
|
|
|
|
from config import SHARED_SKILLS_CACHE, SHARED_SKILLS_PROJECTS, SHARED_SKILLS_REPO
|
|
|
|
PLUGIN_NAME = "shared"
|
|
_PART = r"[A-Za-z0-9][A-Za-z0-9._-]{0,63}"
|
|
_REF_RE = re.compile(rf"^(?:({_PART})/)?({_PART})$")
|
|
|
|
|
|
# ``@<name>``: a skill this repo ships for guests' runs (``cli/skills/`` —
|
|
# the guests' edition of the ai-agent skill, backend users/cli.py).
|
|
BUILTIN_SKILLS = pathlib.Path(__file__).resolve().parent.parent / "cli" / "skills"
|
|
# ``plugin:<uid>/<id>``: materialized skill dir of an installed live plugin
|
|
# (backend plugins/store.py → ``/data/plugins/live-skills/<uid>/<id>``).
|
|
PLUGINS_SKILLS_ROOT = pathlib.Path(os.path.expanduser(os.environ.get(
|
|
"SIDECAR_PLUGINS_SKILLS_DIR",
|
|
"/data/plugins/live-skills" if os.path.isdir("/data/plugins")
|
|
else "~/homelab/services/ai-agent/data/plugins/live-skills",
|
|
)))
|
|
_PLUGIN_REF_RE = re.compile(r"^plugin:([a-z0-9][a-z0-9_-]{0,63})/([a-z0-9][a-z0-9-]{0,47})$")
|
|
|
|
|
|
def resolve(ref: str) -> pathlib.Path | None:
|
|
"""A ref's skill dir — ``name`` in the repo, ``<project>/<name>`` in a
|
|
project, ``@<name>`` built in, ``plugin:<uid>/<id>`` live plugin — when it
|
|
holds a ``SKILL.md``; None for anything else."""
|
|
if (ref or "").startswith("plugin:"):
|
|
m = _PLUGIN_REF_RE.match(ref)
|
|
if not m:
|
|
return None
|
|
d = PLUGINS_SKILLS_ROOT / m.group(1) / m.group(2)
|
|
return d if (d / "SKILL.md").is_file() else None
|
|
if (ref or "").startswith("@"):
|
|
name = ref[1:]
|
|
d = BUILTIN_SKILLS / name
|
|
return d if re.fullmatch(_PART, name) and (d / "SKILL.md").is_file() else None
|
|
m = _REF_RE.match(ref or "")
|
|
if not m or ".." in ref:
|
|
return None
|
|
project, name = m.groups()
|
|
root = SHARED_SKILLS_PROJECTS / project / ".claude" / "skills" if project \
|
|
else SHARED_SKILLS_REPO
|
|
d = root / name
|
|
return d if (d / "SKILL.md").is_file() else None
|
|
|
|
|
|
def sources(refs: list[str] | None) -> list[pathlib.Path]:
|
|
"""The real directories behind ``refs`` (what a sandboxed run must be
|
|
allowed to read for its shared skills to run)."""
|
|
out = []
|
|
for ref in refs or []:
|
|
d = resolve(ref)
|
|
if d is not None:
|
|
out.append(d.resolve())
|
|
return out
|
|
|
|
|
|
def plugin_dir(refs: list[str] | None) -> pathlib.Path | None:
|
|
"""The plugin dir carrying ``refs`` (None when none resolves). A later ref
|
|
whose name is already taken is skipped: the plugin is one flat namespace."""
|
|
found: dict[str, pathlib.Path] = {}
|
|
for ref in refs or []:
|
|
d = resolve(ref)
|
|
if d is not None and d.name not in found:
|
|
found[d.name] = d
|
|
if not found:
|
|
return None
|
|
key = hashlib.sha256(json.dumps(sorted((n, str(p)) for n, p in found.items()))
|
|
.encode()).hexdigest()[:16]
|
|
root = SHARED_SKILLS_CACHE / key
|
|
manifest = root / ".claude-plugin" / "plugin.json"
|
|
skills = root / "skills"
|
|
if manifest.is_file() and all((skills / n).exists() for n in found):
|
|
return root
|
|
skills.mkdir(parents=True, exist_ok=True)
|
|
manifest.parent.mkdir(parents=True, exist_ok=True)
|
|
for name, target in found.items():
|
|
link = skills / name
|
|
if link.is_symlink() or link.exists():
|
|
continue
|
|
tmp = skills / f".{name}.{os.getpid()}"
|
|
try:
|
|
os.symlink(target, tmp)
|
|
os.replace(tmp, link)
|
|
except OSError:
|
|
tmp.unlink(missing_ok=True)
|
|
tmp = manifest.with_suffix(f".{os.getpid()}.tmp")
|
|
tmp.write_text(json.dumps({
|
|
"name": PLUGIN_NAME, "version": "1.0.0",
|
|
"description": "Skills the ai-agent's owner shares with guests"}))
|
|
os.replace(tmp, manifest)
|
|
return root
|