fix(skills): bind plugin snapshots to approved content (#6225)
* fix(skills): bind plugin snapshots to approved content * fix(skills): preserve plugin document newline normalization
This commit is contained in:
1 parent
3a28b9e6ce
commit
af6abde2f4
3 files changed
+204
-36
No files matched your search
+51
-19
@@ -25,6 +25,16 @@ _MCP_SERVER_FIELDS = {"type", "command", "args", "env", "cwd"}
|
||||
_MAX_LOGO_BYTES = 256 * 1024
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class AgentPluginSkill:
|
||||
"""A skill document captured from the approved package fingerprint."""
|
||||
|
||||
name: str
|
||||
path: Path
|
||||
content: str
|
||||
metadata: dict[str, object]
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class _PackageSnapshot:
|
||||
root: Path
|
||||
@@ -81,25 +91,45 @@ def _installed_plugins(workspace: Path) -> list[AgentPlugin]:
|
||||
|
||||
def enabled_agent_plugin_skills(workspace: Path) -> list[tuple[str, Path]]:
|
||||
"""Verify and return skills from plugins the user has explicitly enabled."""
|
||||
skills: list[tuple[str, Path]] = []
|
||||
return [(skill.name, skill.path) for skill in enabled_agent_plugin_skill_documents(workspace)]
|
||||
|
||||
|
||||
def enabled_agent_plugin_skill_documents(workspace: Path) -> list[AgentPluginSkill]:
|
||||
"""Return documents using the same bytes that passed package activation."""
|
||||
skills: list[AgentPluginSkill] = []
|
||||
packages: list[_PackageSnapshot] = []
|
||||
for plugin in _installed_plugins(workspace):
|
||||
plugin_skills = _discover_plugin_skills(plugin.name, plugin.root)
|
||||
fingerprint = _enabled_package_fingerprint(workspace, plugin)
|
||||
skill_files = _plugin_skill_files(plugin.name, plugin.root)
|
||||
documents: dict[Path, bytes | None] = {path: None for _name, path in skill_files}
|
||||
fingerprint = _enabled_package_fingerprint(workspace, plugin, documents=documents)
|
||||
if fingerprint is None:
|
||||
continue
|
||||
plugin_skills: list[AgentPluginSkill] = []
|
||||
for name, path in skill_files:
|
||||
raw = documents[path]
|
||||
try:
|
||||
content = raw.decode("utf-8").replace("\r\n", "\n").replace("\r", "\n") if raw is not None else ""
|
||||
except UnicodeError:
|
||||
content = ""
|
||||
metadata = parse_skill_metadata(content)
|
||||
if metadata is None or not valid_skill_metadata(metadata, name):
|
||||
logger.warning("Ignoring Agent Plugin '{}' skill '{}': invalid metadata", plugin.name, name)
|
||||
continue
|
||||
plugin_skills.append(AgentPluginSkill(name, path, content, metadata))
|
||||
skills.extend(plugin_skills)
|
||||
if plugin_skills:
|
||||
packages.append(
|
||||
_PackageSnapshot(
|
||||
root=plugin.root,
|
||||
fingerprint=fingerprint,
|
||||
skill_dirs=tuple(path.parent for _name, path in plugin_skills),
|
||||
skill_dirs=tuple(skill.path.parent for skill in plugin_skills),
|
||||
)
|
||||
)
|
||||
|
||||
key = _skill_cache_key(workspace)
|
||||
_SKILL_CACHE[key] = _SkillCacheEntry(tuple(skills), tuple(packages))
|
||||
_SKILL_CACHE[key] = _SkillCacheEntry(
|
||||
tuple((skill.name, skill.path) for skill in skills), tuple(packages)
|
||||
)
|
||||
return skills
|
||||
|
||||
|
||||
@@ -156,7 +186,9 @@ def _invalidate_skill_cache(workspace: Path) -> None:
|
||||
_SKILL_CACHE.pop(_skill_cache_key(workspace), None)
|
||||
|
||||
|
||||
def _package_fingerprint(root: Path) -> str | None:
|
||||
def _package_fingerprint(
|
||||
root: Path, *, documents: dict[Path, bytes | None] | None = None,
|
||||
) -> str | None:
|
||||
"""Hash package paths, link targets, and file contents."""
|
||||
digest = sha256()
|
||||
try:
|
||||
@@ -167,8 +199,11 @@ def _package_fingerprint(root: Path) -> str | None:
|
||||
digest.update(b"\0link\0")
|
||||
digest.update(candidate.readlink().as_posix().encode())
|
||||
elif candidate.is_file():
|
||||
content = candidate.read_bytes()
|
||||
digest.update(b"\0file\0")
|
||||
digest.update(candidate.read_bytes())
|
||||
digest.update(content)
|
||||
if documents is not None and candidate in documents:
|
||||
documents[candidate] = content
|
||||
elif candidate.is_dir():
|
||||
digest.update(b"\0dir\0")
|
||||
else:
|
||||
@@ -413,14 +448,16 @@ def _plugin_data_dir(workspace: Path, name: str, *, create: bool) -> Path:
|
||||
return current
|
||||
|
||||
|
||||
def _enabled_package_fingerprint(workspace: Path, plugin: AgentPlugin) -> str | None:
|
||||
def _enabled_package_fingerprint(
|
||||
workspace: Path, plugin: AgentPlugin, *, documents: dict[Path, bytes | None] | None = None,
|
||||
) -> str | None:
|
||||
"""Return the content fingerprint when this exact package is enabled."""
|
||||
marker = _plugin_data_dir(workspace, plugin.name, create=False) / "enabled"
|
||||
try:
|
||||
if not marker.is_file():
|
||||
return None
|
||||
current = marker.read_text(encoding="utf-8")
|
||||
activation = _activation_marker(plugin)
|
||||
activation = _activation_marker(plugin, documents=documents)
|
||||
if activation is None:
|
||||
marker.unlink(missing_ok=True)
|
||||
_invalidate_skill_cache(workspace)
|
||||
@@ -447,9 +484,11 @@ def _enabled(workspace: Path, plugin: AgentPlugin) -> bool:
|
||||
return _enabled_package_fingerprint(workspace, plugin) is not None
|
||||
|
||||
|
||||
def _activation_marker(plugin: AgentPlugin) -> str | None:
|
||||
def _activation_marker(
|
||||
plugin: AgentPlugin, *, documents: dict[Path, bytes | None] | None = None,
|
||||
) -> str | None:
|
||||
"""Bind activation to one immutable package snapshot."""
|
||||
fingerprint = _package_fingerprint(plugin.root)
|
||||
fingerprint = _package_fingerprint(plugin.root, documents=documents)
|
||||
if fingerprint is None:
|
||||
return None
|
||||
return json.dumps(
|
||||
@@ -459,7 +498,7 @@ def _activation_marker(plugin: AgentPlugin) -> str | None:
|
||||
)
|
||||
|
||||
|
||||
def _discover_plugin_skills(plugin_name: str, plugin_root: Path) -> list[tuple[str, Path]]:
|
||||
def _plugin_skill_files(plugin_name: str, plugin_root: Path) -> list[tuple[str, Path]]:
|
||||
skills_root = _contained(plugin_root / "skills", plugin_root, directory=True)
|
||||
if skills_root is None:
|
||||
return []
|
||||
@@ -472,13 +511,6 @@ def _discover_plugin_skills(plugin_name: str, plugin_root: Path) -> list[tuple[s
|
||||
skill_file = _contained(skill_root / "SKILL.md", plugin_root)
|
||||
if skill_file is None:
|
||||
continue
|
||||
try:
|
||||
metadata = parse_skill_metadata(skill_file.read_text(encoding="utf-8"))
|
||||
except (OSError, UnicodeError):
|
||||
metadata = None
|
||||
if metadata is None or not valid_skill_metadata(metadata, candidate.name):
|
||||
logger.warning("Ignoring Agent Plugin '{}' skill '{}': invalid metadata", plugin_name, candidate.name)
|
||||
continue
|
||||
skills.append((candidate.name, skill_file))
|
||||
return skills
|
||||
|
||||
|
||||
+21
-12
@@ -121,32 +121,37 @@ class SkillsLoader:
|
||||
return skills
|
||||
|
||||
def _discover_skills(self) -> list[dict[str, str]]:
|
||||
from nanobot.agent.plugins import enabled_agent_plugin_skills
|
||||
from nanobot.agent.plugins import enabled_agent_plugin_skill_documents
|
||||
|
||||
plugin_skills = enabled_agent_plugin_skills(self.workspace)
|
||||
plugin_skills = enabled_agent_plugin_skill_documents(self.workspace)
|
||||
disabled = set(self.disabled_skills)
|
||||
if disabled:
|
||||
for legacy, canonical in self._skill_aliases().items():
|
||||
if legacy in disabled or canonical in disabled:
|
||||
disabled.update((legacy, canonical))
|
||||
skills = self._skill_entries_from_dir(self.workspace_skills, "workspace")
|
||||
seen_names = {entry["name"] for entry in skills}
|
||||
for name, path in plugin_skills:
|
||||
if name in seen_names:
|
||||
snapshot = self._snapshot.get()
|
||||
for skill in plugin_skills:
|
||||
if skill.name in seen_names:
|
||||
continue
|
||||
skills.append(
|
||||
{
|
||||
"name": name,
|
||||
"path": str(path),
|
||||
"name": skill.name,
|
||||
"path": str(skill.path),
|
||||
"source": "plugin",
|
||||
}
|
||||
)
|
||||
seen_names.add(name)
|
||||
if snapshot is not None and skill.name not in disabled:
|
||||
snapshot.documents[skill.name] = skill.content
|
||||
snapshot.metadata[skill.name] = skill.metadata
|
||||
seen_names.add(skill.name)
|
||||
if self.builtin_skills and self.builtin_skills.exists():
|
||||
skills.extend(
|
||||
self._skill_entries_from_dir(self.builtin_skills, "builtin", skip_names=seen_names)
|
||||
)
|
||||
|
||||
if self.disabled_skills:
|
||||
disabled = set(self.disabled_skills)
|
||||
for legacy, canonical in self._skill_aliases().items():
|
||||
if legacy in disabled or canonical in disabled:
|
||||
disabled.update((legacy, canonical))
|
||||
if disabled:
|
||||
skills = [s for s in skills if s["name"] not in disabled]
|
||||
|
||||
return skills
|
||||
@@ -161,6 +166,10 @@ class SkillsLoader:
|
||||
Returns:
|
||||
Skill content or None if not found.
|
||||
"""
|
||||
with self.snapshot():
|
||||
return self._load_skill(name)
|
||||
|
||||
def _load_skill(self, name: str) -> str | None:
|
||||
skills = self.list_skills(filter_unavailable=False)
|
||||
available = {skill["name"] for skill in skills}
|
||||
resolved = name if name in available else self._skill_aliases().get(name, name)
|
||||
|
||||
Reference in new issue
Block a user