Skip to content

The agent that grows with you

refactor(tools): skills_sync_optional inlines hub-lock/active-copy helpers; compact docstrings across group

Tekniumd1210e6ea455
diff --git a/tools/skills_sync.py b/tools/skills_sync.py
index 677b2e23eb1..655385fdb2a 100644
--- a/tools/skills_sync.py
+++ b/tools/skills_sync.py
@@ -1,11 +1,11 @@
#!/usr/bin/env python3
"""Skills Sync -- manifest-based seeding and updating of bundled skills.
-Copies repo skills/ into ~/.hermes/skills/, tracking each synced skill's origin
-hash in .bundled_manifest (v2 "name:hash" lines; v1 plain names auto-migrate).
-NEW skills are copied and recorded; EXISTING skills update only when bundled
-changed AND the user copy still matches the origin hash (else user-customized ->
-SKIP); user-DELETED skills are not re-added; upstream-REMOVED ones leave the manifest.
+Copies repo skills/ into ~/.hermes/skills/, tracking each synced skill's origin hash in
+.bundled_manifest (v2 "name:hash" lines; v1 plain names auto-migrate). NEW skills are copied
+and recorded; EXISTING skills update only when bundled changed AND the user copy still matches
+the origin hash (else user-customized -> SKIP); user-DELETED skills are not re-added;
+upstream-REMOVED ones leave the manifest.
"""
import hashlib
@@ -22,10 +22,8 @@ from typing import Dict, Iterator, List, Optional, Set, Tuple
# Force UTF-8 stdout/stderr: GBK-style Windows locales can't encode the glyphs
# printed here (✓ ↑ →), and install.ps1 parses this script's stdout as UTF-8.
for _stream in (sys.stdout, sys.stderr):
- try:
+ with suppress(AttributeError, ValueError, TypeError):
_stream.reconfigure(encoding="utf-8", errors="replace")
- except (AttributeError, ValueError, TypeError):
- pass
from hermes_constants import get_bundled_skills_dir, get_hermes_home, get_optional_skills_dir
from agent.skill_utils import ESSENTIAL_SKILLS, is_excluded_skill_path
from tools.skill_usage import _read_skill_name, read_suppressed_names # noqa: F401 (re-exported)
@@ -37,10 +35,10 @@ HERMES_HOME = get_hermes_home()
SKILLS_DIR = HERMES_HOME / "skills"
MANIFEST_FILE = SKILLS_DIR / ".bundled_manifest"
-# Import-time snapshots backing the call-time accessors: long-lived multi-profile
-# runtimes retarget HERMES_HOME after import, and frozen constants would resolve
-# (and for reset_bundled_skill() DELETE) against the wrong profile. Accessors honor
-# an explicitly patched module global and otherwise re-resolve on every call.
+# Import-time snapshots backing the call-time accessors: long-lived multi-profile runtimes
+# retarget HERMES_HOME after import, and frozen constants would resolve (and for
+# reset_bundled_skill() DELETE) against the wrong profile. Accessors honor an explicitly
+# patched module global and otherwise re-resolve on every call.
_HERMES_HOME_AT_IMPORT = HERMES_HOME
_SKILLS_DIR_AT_IMPORT = SKILLS_DIR
_MANIFEST_FILE_AT_IMPORT = MANIFEST_FILE
@@ -129,9 +127,8 @@ def _write_manifest(entries: Dict[str, str]):
def _discover_bundled_skills(bundled_dir: Path) -> List[Tuple[str, Path]]:
- """``(skill_name, skill_dir)`` per SKILL.md under the bundled dir. Exclusions are
- evaluated relative to the bundled tree: the install prefix itself may contain
- ``venv``/``site-packages`` (which once made wheel installs discover zero skills)."""
+ """``(skill_name, skill_dir)`` per SKILL.md under the bundled dir. Exclusions are evaluated
+ relative to the bundled tree: the install prefix itself may contain ``venv``/``site-packages``."""
if not bundled_dir.exists():
return []
return [
@@ -169,10 +166,10 @@ def _copy_dir(src: Path, dest: Path) -> None:
def _recover_renamed_skill(st: "_SyncState", skill_name: str, dest: Path) -> Optional[str]:
- """Move a bundled skill's stale copy to its new canonical path after an upstream
- RENAME/RECATEGORIZATION (else it is misread as user-deleted and stranded forever).
- Only a copy byte-identical to the origin hash — proof *we* placed it — is moved;
- user-edited or hub-installed copies stay. Returns the rel source path on move."""
+ """Move a bundled skill's stale copy to its new canonical path after an upstream RENAME /
+ RECATEGORIZATION (else it is misread as user-deleted and stranded forever). Only a copy
+ byte-identical to the origin hash — proof *we* placed it — is moved; user-edited or
+ hub-installed copies stay. Returns the rel source path on move."""
origin_hash = st.manifest.get(skill_name, "")
if not origin_hash:
return None
@@ -244,9 +241,9 @@ def _recover_orphan_backup(dest: Path) -> None:
def _defer_to_external(st: _SyncState, skill_name: str, dest: Path, bundled_hash: str) -> None:
- """An external_dirs source provides this skill; a local copy would be a name collision
- the loader refuses. Defer for ALL manifest states and remove a stale local shadow from
- an earlier sync — only when byte-identical (a user's own skill differs)."""
+ """An external_dirs source provides this skill; a local copy would be a name collision the
+ loader refuses. Defer for ALL manifest states; remove a stale local shadow from an earlier
+ sync only when byte-identical (a user's own skill differs)."""
st.shadowed_by_external.append(skill_name)
st.skipped += 1
st.say(f" ⇢ {skill_name} (deferred to external_dirs, not written to local tree)")
@@ -347,9 +344,9 @@ def _seed_category_descriptions(bundled_dir: Path, only_dirs: Optional[Set[Path]
def sync_skills(quiet: bool = False) -> dict:
- """Sync bundled skills into ~/.hermes/skills/ using the manifest; returns the
- per-category result dict (see the final return). Opted-out profiles seed ONLY
- ESSENTIAL_SKILLS: the system prompt always points at ``hermes-agent``."""
+ """Sync bundled skills into ~/.hermes/skills/ using the manifest; returns the per-category
+ result dict. Opted-out profiles seed ONLY ESSENTIAL_SKILLS (the system prompt always
+ points at ``hermes-agent``)."""
essential_only = (_hermes_home() / NO_BUNDLED_SKILLS_MARKER).exists()
if essential_only and not quiet:
print(" (profile opted out of bundled skills via .no-bundled-skills — seeding essential skills only)")
@@ -400,15 +397,14 @@ def sync_skills(quiet: bool = False) -> dict:
"total_bundled": len(bundled_skills),
"optional_provenance_backfilled": _backfill_optional_provenance(quiet=quiet),
"shadowed_by_external": st.shadowed_by_external,
- "skipped_opt_out": essential_only, # lets callers report "opted out" rather than a normal sync
- }
+ "skipped_opt_out": essential_only} # lets callers report "opted out" rather than a normal sync
def _rmtree_writable(path: Path) -> None:
- """rmtree that first makes read-only entries writable (Nix/deb/rpm keep r-x dirs;
- unlinking a child needs a writable parent, so chmod both). Scope guard: refuses
- anything not a STRICT child of the active skills root, so a bad join / missing
- HERMES_HOME / malicious manifest entry raises instead of wiping ``~/.hermes``."""
+ """rmtree that first makes read-only entries writable (Nix/deb/rpm keep r-x dirs; unlinking
+ a child needs a writable parent, so chmod both). Scope guard: refuses anything not a STRICT
+ child of the active skills root, so a bad join / missing HERMES_HOME / malicious manifest
+ entry raises instead of wiping ``~/.hermes``."""
target = Path(path).resolve()
skills_root = _skills_dir().resolve()
if skills_root not in target.parents:
diff --git a/tools/skills_sync_optional.py b/tools/skills_sync_optional.py
index a15acc229dc..9d4401cc42e 100644
--- a/tools/skills_sync_optional.py
+++ b/tools/skills_sync_optional.py
@@ -6,6 +6,7 @@ time so tests and multi-profile runtimes patching ``tools.skills_sync`` keep wor
import json
import logging
+from contextlib import suppress
from datetime import datetime, timezone
from pathlib import Path, PurePosixPath
from typing import Dict, Iterator, List, Optional, Set, Tuple
@@ -23,8 +24,7 @@ def _ss():
def _content_hash(directory: Path) -> str:
- """Hub-lock hash style; provenance metadata only, so fall back to the local MD5
- if guard deps are unavailable."""
+ """Hub-lock hash style; provenance metadata only, so fall back to local MD5 without guard deps."""
try:
from tools.skills_guard import content_hash
return content_hash(directory)
@@ -46,14 +46,10 @@ def _skill_file_list(skill_dir: Path) -> List[str]:
return [f.relative_to(skill_dir).as_posix() for f in sorted(skill_dir.rglob("*")) if f.is_file()]
-def _hub_lock_path() -> Path:
- return _ss()._skills_dir() / ".hub" / "lock.json"
-
-
def _load_hub_lock() -> Optional[dict]:
"""Parse the skills-hub lock; None when missing or unreadable."""
try:
- return json.loads(_hub_lock_path().read_text(encoding="utf-8"))
+ return json.loads((_ss()._skills_dir() / ".hub" / "lock.json").read_text(encoding="utf-8"))
except (FileNotFoundError, json.JSONDecodeError, OSError):
return None
@@ -69,12 +65,6 @@ def _read_hub_install_paths() -> Set[str]:
return {str(e["install_path"]).strip("/") for e in _hub_lock_entries(_load_hub_lock()) if e.get("install_path")}
-def _write_hub_lock(lock_path: Path, data: dict) -> None:
- """Atomic: a crash mid-write must not wipe all provenance (the reader's
- JSONDecodeError fallback resets ``installed`` to empty)."""
- atomic_write_text(lock_path, json.dumps(data, indent=2, ensure_ascii=False) + "\n", tmp_prefix=".lock_")
-
-
def _iter_optional_skills(optional_dir: Path, *, root_relative: bool) -> Iterator[Tuple[Path, Path, str]]:
"""Yield ``(skill_md, src, install_path)`` for every safe official optional skill."""
for skill_md in sorted(optional_dir.rglob("SKILL.md")):
@@ -112,20 +102,10 @@ def _move_to_restore_backup(path: Path, backup_root: Path) -> str:
return rel.as_posix()
-def _find_active_copies(folder_name: str, src_frontmatter: str, dest: Path) -> List[Path]:
- """Active copies of an official skill (by frontmatter name or folder slug),
- even when the curator moved it into another category; excludes ``dest``."""
- ss = _ss()
- names = {folder_name, src_frontmatter}
- return [
- md.parent for md in ss._iter_active_skill_mds(sort=True)
- if md.parent != dest and (md.parent.name == folder_name or ss._read_skill_name(md, md.parent.name) in names)]
-
-
def restore_official_optional_skill(name: str, *, restore: bool = False) -> dict:
- """Restore one or all official optional skills from repo source. ``restore=False``
- only backfills exact-match provenance; ``restore=True`` also backs up matching
- active copies and copies the official source into its canonical path."""
+ """Restore one or all official optional skills from repo source. ``restore=False`` only
+ backfills exact-match provenance; ``restore=True`` also backs up matching active copies
+ and copies the official source into its canonical path."""
ss = _ss()
index = _optional_skill_index()
if index and name in {"all", "*"}:
@@ -142,9 +122,13 @@ def restore_official_optional_skill(name: str, *, restore: bool = False) -> dict
for folder_name, install_path, src in targets if restore else []:
dest = ss._skills_dir() / Path(*install_path.split("/"))
canonical_ok = dest.exists() and ss._dir_hash(dest) == ss._dir_hash(src)
- src_frontmatter = ss._read_skill_name(src / "SKILL.md", folder_name)
- for match in _find_active_copies(folder_name, src_frontmatter, dest):
- if match.exists():
+ # Active copies by frontmatter name or folder slug — the curator may have moved
+ # the skill into another category; ``dest`` itself is handled below.
+ names = {folder_name, ss._read_skill_name(src / "SKILL.md", folder_name)}
+ for md in ss._iter_active_skill_mds(sort=True):
+ match = md.parent
+ if match != dest and match.exists() and (
+ match.name == folder_name or ss._read_skill_name(md, match.name) in names):
backed_up.append(_move_to_restore_backup(match, backup_root))
if dest.exists() and not canonical_ok:
backed_up.append(_move_to_restore_backup(dest, backup_root))
@@ -158,38 +142,35 @@ def restore_official_optional_skill(name: str, *, restore: bool = False) -> dict
def _index_installed_skill_dirs_by_name() -> Dict[str, List[Path]]:
- """Index installed skills by directory name with one active-tree scan,
- skipping anything that resolves outside the skills tree (symlinks/external)."""
+ """Installed skills by directory name in one active-tree scan, skipping anything that
+ resolves outside the skills tree (symlinks/external)."""
ss = _ss()
index: Dict[str, List[Path]] = {}
root = ss._skills_dir().resolve()
for skill_md in ss._iter_active_skill_mds():
- try:
+ with suppress(OSError, ValueError):
skill_md.parent.resolve().relative_to(root)
- except (OSError, ValueError):
- continue
- index.setdefault(skill_md.parent.name, []).append(skill_md.parent)
+ index.setdefault(skill_md.parent.name, []).append(skill_md.parent)
return index
def _relocated_dest(src_name: str, index: Dict[str, List[Path]]) -> Optional[Tuple[Path, str]]:
- """The active tree may hold a skill under a DIFFERENT category path than the repo
- (upstream reorganized; the installed copy kept its location). Fall back to a UNIQUE
- same-directory-name match; ambiguity gives no basis to pick. ``(dest, install_path)`` or None."""
+ """``(dest, install_path)`` of a UNIQUE same-directory-name match, else None: the active
+ tree may hold a skill under a DIFFERENT category than the repo (upstream reorganized,
+ installed copy kept its place); ambiguity gives no basis to pick."""
if len(candidates := index.get(src_name, [])) != 1:
return None
- dest = candidates[0]
try:
- return dest, _safe_rel_install_path(dest, _ss()._skills_dir())
+ return candidates[0], _safe_rel_install_path(candidates[0], _ss()._skills_dir())
except ValueError as e:
- logger.debug("Skipping relocated optional skill %s: %s", dest, e)
+ logger.debug("Skipping relocated optional skill %s: %s", candidates[0], e)
return None
def _backfill_optional_provenance(quiet: bool = False) -> List[str]:
- """Mark already-present official optional skills as hub-installed: formerly bundled
- (or hand-copied) skills now under optional-skills/ get official provenance when
- byte-identical to the source. Modified/local skills are left alone."""
+ """Mark already-present official optional skills as hub-installed: formerly bundled (or
+ hand-copied) skills now under optional-skills/ get official provenance when byte-identical
+ to the source; modified/local skills are left alone."""
ss = _ss()
optional_dir = ss._get_optional_dir()
if not optional_dir.exists():
@@ -216,20 +197,15 @@ def _backfill_optional_provenance(quiet: bool = False) -> List[str]:
continue
timestamp = datetime.now(timezone.utc).isoformat()
installed[lock_name] = {
- "source": "official",
- "identifier": f"official/{install_path}",
- "trust_level": "builtin",
- "scan_verdict": "backfilled",
- "content_hash": _content_hash(dest),
- "install_path": install_path,
- "files": _skill_file_list(dest),
- "metadata": {"backfilled_from": "optional-skills"},
- "installed_at": timestamp,
- "updated_at": timestamp}
+ "source": "official", "identifier": f"official/{install_path}", "trust_level": "builtin",
+ "scan_verdict": "backfilled", "content_hash": _content_hash(dest), "install_path": install_path,
+ "files": _skill_file_list(dest), "metadata": {"backfilled_from": "optional-skills"},
+ "installed_at": timestamp, "updated_at": timestamp}
existing_paths.add(install_path)
backfilled.append(lock_name)
if not quiet:
print(f" = {lock_name} (official optional provenance backfilled)")
- if backfilled:
- _write_hub_lock(_hub_lock_path(), data)
+ if backfilled: # atomic: a crash mid-write must not wipe all provenance (reader resets on bad JSON)
+ atomic_write_text(ss._skills_dir() / ".hub" / "lock.json",
+ json.dumps(data, indent=2, ensure_ascii=False) + "\n", tmp_prefix=".lock_")
return backfilled
diff --git a/tools/skills_tool.py b/tools/skills_tool.py
index 4f51509cc5c..4fd00ed1c4a 100644
--- a/tools/skills_tool.py
+++ b/tools/skills_tool.py
@@ -1,11 +1,10 @@
#!/usr/bin/env python3
"""Skills Tool — list and view skill documents (progressive disclosure).
-A skill is a directory holding SKILL.md (YAML frontmatter + instructions) plus
-optional references/, templates/, assets/, scripts/. `skills_list` returns
-name/description only; `skill_view` returns full content and linked files.
-Sibling modules (skills_tool_setup / _plugin / _dedup) have every name re-exported
-here so ``from tools.skills_tool import X`` / ``patch("tools.skills_tool.X")`` keep working.
+A skill is a directory holding SKILL.md (YAML frontmatter + instructions) plus optional
+references/, templates/, assets/, scripts/. `skills_list` returns name/description only;
+`skill_view` returns full content and linked files. Sibling modules (skills_tool_setup /
+_plugin / _dedup) have every name re-exported here so imports and patches keep working.
"""
import json
@@ -40,11 +39,10 @@ from tools.skills_tool_dedup import ( # noqa: F401
logger = logging.getLogger(__name__)
-# Per-session discovery cache: {cache_key: (signature, timestamp, skills_list)}.
-# Signature = per-dir max mtime of the dir and its immediate children (add/remove
-# inside a category does NOT bump the root mtime) + the disabled set (config-only
-# change, no mtime at all) + platform; the TTL bounds staleness from in-place
-# SKILL.md edits, which no directory signature can see.
+# Per-session discovery cache: {cache_key: (signature, timestamp, skills_list)}. Signature =
+# per-dir max mtime of the dir and its immediate children (add/remove inside a category does
+# NOT bump the root mtime) + the disabled set (config-only change, no mtime) + platform; the
+# TTL bounds staleness from in-place SKILL.md edits, which no directory signature can see.
_SKILLS_CACHE: dict = {}
_SKILLS_CACHE_TTL_SECONDS = 30.0
@@ -75,9 +73,8 @@ _SKILLS_DIR_AT_IMPORT = SKILLS_DIR
def _skills_dir() -> Path:
- """Active profile's skills dir at call time: the patched ``SKILLS_DIR`` when a
- test/external patcher changed it, else the live profile-scoped HERMES_HOME
- (long-lived runtimes may import this module before the profile is set)."""
+ """Active profile's skills dir at call time: the patched ``SKILLS_DIR`` when a patcher changed
+ it, else live profile-scoped HERMES_HOME (long-lived runtimes may import before profile set)."""
configured = Path(SKILLS_DIR)
return configured if configured != _SKILLS_DIR_AT_IMPORT else get_hermes_home() / "skills"
@@ -87,9 +84,8 @@ _LOOKUP_HINT = "Use a skill name or relative path within the skills directory."
def _skill_lookup_path_error(name: str) -> Optional[str]:
- """Error if lookup *name* could escape the search roots it is joined onto.
- Windows drive paths are rejected too: their ``:`` would be misread as a
- plugin namespace separator."""
+ """Error if lookup *name* could escape the search roots it is joined onto. Windows drive
+ paths are rejected too: their ``:`` would be misread as a plugin namespace separator."""
from tools.path_security import has_traversal_component
if not isinstance(name, str):
return "Skill name must be a string."
@@ -122,8 +118,8 @@ def set_secret_capture_callback(callback) -> None:
def _skill_utils_delegate(attr: str):
- """Lazy call-time delegate to ``agent.skill_utils.<attr>`` — public re-exports so
- existing callers (and tests patching either module) don't need updating."""
+ """Lazy call-time delegate to ``agent.skill_utils.<attr>`` (re-export; patches on either
+ module are honored)."""
def _delegate(*args):
from agent import skill_utils
return getattr(skill_utils, attr)(*args)
@@ -132,8 +128,7 @@ def _skill_utils_delegate(attr: str):
skill_matches_platform = _skill_utils_delegate("skill_matches_platform")
-# Offer-time relevance gate (kanban/docker/s6), NOT a hard-compatibility gate;
-# explicit skill loads bypass it.
+# Offer-time relevance gate (kanban/docker/s6), NOT hard compatibility; explicit loads bypass it.
skill_matches_environment = _skill_utils_delegate("skill_matches_environment")
_parse_frontmatter = _skill_utils_delegate("parse_frontmatter")
_get_disabled_skill_names = _skill_utils_delegate("get_disabled_skill_names")
@@ -145,8 +140,8 @@ def check_skills_requirements() -> bool:
def _get_category_from_path(skill_path: Path) -> Optional[str]:
- """``~/.hermes/skills/mlops/axolotl/SKILL.md`` -> ``"mlops"``; active profile
- dir first (respects test monkeypatching), then skills.external_dirs."""
+ """``~/.hermes/skills/mlops/axolotl/SKILL.md`` -> ``"mlops"``; active profile dir first
+ (respects test monkeypatching), then skills.external_dirs."""
dirs_to_check = [_skills_dir()]
with suppress(Exception):
from agent.skill_utils import get_external_skills_dirs
@@ -198,8 +193,8 @@ def _is_skill_disabled(name: str, platform: str = None) -> bool:
def _skill_search_dirs() -> Tuple[list, list, Path]:
- """(project_dirs, all_dirs, active_skills_dir); trusted project-local dirs come
- FIRST so first-wins dedup / the collision resolver prefer them."""
+ """(project_dirs, all_dirs, active_skills_dir); trusted project-local dirs come FIRST so
+ first-wins dedup / the collision resolver prefer them."""
from agent.skill_utils import get_external_skills_dirs, get_project_skills_dirs
project_dirs = list(get_project_skills_dirs())
active_skills_dir = _skills_dir()
@@ -209,9 +204,8 @@ def _skill_search_dirs() -> Tuple[list, list, Path]:
def _find_all_skills(*, skip_disabled: bool = False) -> List[Dict[str, Any]]:
- """All skills (name, description, category) across project/local/external dirs,
- first-wins by name; cached per session. ``skip_disabled=True`` returns ALL skills
- regardless of disabled state (``hermes skills`` config UI)."""
+ """All skills (name, description, category) across project/local/external dirs, first-wins
+ by name; cached per session. ``skip_disabled=True`` ignores disabled state (config UI)."""
from agent.skill_utils import iter_project_skill_files, iter_skill_index_files
cache_key = "with_disabled" if skip_disabled else "filtered"
disabled = set() if skip_disabled else _get_disabled_skill_names()
@@ -244,10 +238,8 @@ def _find_all_skills(*, skip_disabled: bool = False) -> List[Dict[str, Any]]:
(ln for ln in map(str.strip, body.strip().split("\n")) if ln and not ln.startswith("#")),
description)
seen_names.add(name)
- skills.append({
- "name": name,
- "description": _truncate_description(description),
- "category": _get_category_from_path(skill_md)})
+ skills.append({"name": name, "description": _truncate_description(description),
+ "category": _get_category_from_path(skill_md)})
except (UnicodeDecodeError, PermissionError) as e:
logger.debug("Failed to read skill file %s: %s", skill_md, e)
except Exception as e:
@@ -279,18 +271,14 @@ def skills_list(category: str = None, task_id: str = None) -> str:
except Exception:
logger.debug("Plugin skill listing failed", exc_info=True)
if not all_skills:
- return _json({
- "success": True, "skills": [], "categories": [],
- "message": "No skills found in skills/ directory."})
+ return _json({"success": True, "skills": [], "categories": [],
+ "message": "No skills found in skills/ directory."})
if category:
all_skills = [s for s in all_skills if s.get("category") == category]
all_skills = _sort_skills(all_skills)
categories = sorted({s.get("category") for s in all_skills if s.get("category")})
return _json({
- "success": True,
- "skills": all_skills,
- "categories": categories,
- "count": len(all_skills),
+ "success": True, "skills": all_skills, "categories": categories, "count": len(all_skills),
"hint": "Use skill_view(name) to see full content, tags, and linked files"})
except Exception as e:
return tool_error(str(e), success=False)
@@ -299,10 +287,10 @@ def skills_list(category: str = None, task_id: str = None) -> str:
# ── skill_view helpers ─────────────────────────────────────────────────────
def _resolve_plugin_skill(name, file_path, task_id, preprocess):
- """Qualified-name dispatch for ``plugin:skill`` names: ``(result_json, None)`` when
- answered, else ``(None, local_category_name)`` to fall through to the flat-tree
- scan — categorized local skills also use ``category:skill`` in config and gateway
- prompts, so the on-disk ``category/skill`` form is returned (None without a bare part)."""
+ """``plugin:skill`` dispatch: ``(result_json, None)`` when answered, else ``(None,
+ local_category_name)`` to fall through to the flat-tree scan — categorized local skills
+ also use ``category:skill`` in config/gateway prompts, so the on-disk ``category/skill``
+ form is returned (None without a bare part)."""
from agent.skill_utils import is_valid_namespace, parse_qualified_name
from hermes_cli.plugins import discover_plugins, get_plugin_manager
namespace, bare = parse_qualified_name(name)
@@ -353,10 +341,10 @@ def _under_any(path: Path, dirs) -> bool:
def _collect_skill_candidates(name, local_category_name, all_dirs):
- """ALL (skill_dir, skill_md) candidates across every dir and lookup strategy
- (direct path, recursive by dir / frontmatter name, legacy flat <name>.md), deduped
- by resolved path. Collision detection is the point: silent shadowing of a local
- skill by a same-named external one is a real bug class, so the caller refuses >1."""
+ """ALL (skill_dir, skill_md) candidates across every dir and lookup strategy (direct path,
+ recursive by dir / frontmatter name, legacy flat <name>.md), deduped by resolved path.
+ Collision detection is the point: silent shadowing of a local skill by a same-named
+ external one is a real bug class, so the caller refuses >1."""
from agent.skill_utils import iter_skill_index_files
candidates: List[Tuple[Optional[Path], Path]] = []
seen_md: set = set()
@@ -397,8 +385,7 @@ _LINKED_FILE_SPECS = (
("references", ["*.md"], False, False),
("templates", ["*.md", "*.py", "*.yaml", "*.yml", "*.json", "*.tex", "*.sh"], True, False),
("assets", ["*"], True, True),
- ("scripts", ["*.py", "*.sh", "*.bash", "*.js", "*.ts", "*.rb"], False, False),
-)
+ ("scripts", ["*.py", "*.sh", "*.bash", "*.js", "*.ts", "*.rb"], False, False))
def _skill_linked_files(skill_dir: Optional[Path]) -> dict:
@@ -418,9 +405,8 @@ def _skill_linked_files(skill_dir: Optional[Path]) -> dict:
def _org_provenance_header(skill_dir: Path, active_skills_dir: Path):
- """(org_provenance dict, header text) for an org-mirror skill, else (None, "").
- Announced IN the content the model consumes; the author is token-verified at push
- time by the sync plane, so the header is trustworthy."""
+ """(org_provenance dict, header text) for an org-mirror skill, else (None, ""). Announced IN
+ the content the model consumes; the author is token-verified at push time by the sync plane."""
from agent.skill_utils import ORG_PROVENANCE_FILE, is_org_mirror_path, org_id_of_path
if not is_org_mirror_path(skill_dir, active_skills_dir):
return None, ""
@@ -445,10 +431,9 @@ def _org_provenance_header(skill_dir: Path, active_skills_dir: Path):
def _skill_readiness(frontmatter: Dict[str, Any], skill_name: str) -> Tuple[dict, dict]:
- """Resolve required env vars / credential files (prompting for secrets where the
- surface allows) and register what's available for sandboxes. Returns ``(fields,
- extras)``: ``fields`` go before ``_source_path`` in the skill_view result, ``extras``
- after it — result key order is part of the tool output."""
+ """Resolve required env vars / credential files (prompting for secrets where the surface
+ allows) and register what's available for sandboxes. Returns ``(fields, extras)``: fields go
+ before ``_source_path`` in the skill_view result, extras after — key order is tool output."""
legacy_env_vars, _ = _collect_prerequisite_values(frontmatter)
required_env_vars = _get_required_environment_variables(frontmatter, legacy_env_vars)
backend = _get_terminal_backend_name()
@@ -481,33 +466,26 @@ def _skill_readiness(frontmatter: Dict[str, Any], skill_name: str) -> Tuple[dict
logger.debug("Could not register credential files for skill %s", skill_name, exc_info=True)
status = SkillReadinessStatus.SETUP_NEEDED if setup_needed else SkillReadinessStatus.AVAILABLE
fields = {
- "required_environment_variables": required_env_vars,
- "required_commands": [],
- "missing_required_environment_variables": remaining,
- "missing_credential_files": missing_cred_files,
- "missing_required_commands": [],
- "setup_needed": setup_needed,
- "setup_skipped": capture_result["setup_skipped"],
- "readiness_status": status.value}
+ "required_environment_variables": required_env_vars, "required_commands": [],
+ "missing_required_environment_variables": remaining, "missing_credential_files": missing_cred_files,
+ "missing_required_commands": [], "setup_needed": setup_needed,
+ "setup_skipped": capture_result["setup_skipped"], "readiness_status": status.value}
extras: dict = {}
if setup_help := next((e["help"] for e in required_env_vars if e.get("help")), None):
extras["setup_help"] = setup_help
if capture_result["gateway_setup_hint"]:
extras["gateway_setup_hint"] = capture_result["gateway_setup_hint"]
- if setup_needed:
- missing_items = [f"env ${n}" for n in remaining] + [f"file {p}" for p in missing_cred_files]
- setup_note = _build_setup_note(status, missing_items, setup_help)
- if _is_remote_env_backend(backend) and setup_note:
+ missing_items = [f"env ${n}" for n in remaining] + [f"file {p}" for p in missing_cred_files]
+ if setup_needed and (setup_note := _build_setup_note(status, missing_items, setup_help)):
+ if _is_remote_env_backend(backend):
setup_note = f"{setup_note} {backend.upper()}-backed skills need these requirements available inside the remote environment as well."
- if setup_note:
- extras["setup_note"] = setup_note
+ extras["setup_note"] = setup_note
return fields, extras
def _locate_skill(name: str, local_category_name: Optional[str], project_dirs: list, all_dirs: list):
- """Unique on-disk skill for *name*: collision refusal, project-tier precedence,
- quarantine gate, not-found listing. Returns ``(error_json, skill_dir, skill_md)``;
- ``skill_md`` is set whenever ``error_json`` is None."""
+ """Unique on-disk skill for *name*: collision refusal, project-tier precedence, quarantine
+ gate, not-found listing. ``(error_json, skill_dir, skill_md)``; skill_md set when error is None."""
if not all_dirs:
return _fail("Skills directory does not exist yet. It will be created on first install."), None, None
candidates = _collect_skill_candidates(name, local_category_name, all_dirs)
@@ -543,8 +521,8 @@ def _locate_skill(name: str, local_category_name: Optional[str], project_dirs: l
def _log_security_warnings(name: str, skill_md: Path, content: str, all_dirs: list, active_skills_dir: Path):
- """Warn (never block) when loaded from outside the trusted dirs (project + local +
- external) and/or when common prompt-injection patterns appear."""
+ """Warn (never block) when loaded from outside the trusted dirs (project + local + external)
+ and/or when common prompt-injection patterns appear."""
trusted_dirs = [active_skills_dir.resolve()]
with suppress(Exception):
trusted_dirs.extend(d.resolve() for d in all_dirs)
@@ -559,11 +537,10 @@ def _log_security_warnings(name: str, skill_md: Path, content: str, all_dirs: li
def skill_view(
name: str, file_path: str = None, task_id: str = None, preprocess: bool = True) -> str:
- """View a skill (SKILL.md) or a file within its directory, as JSON.
-
- ``name`` is a skill name or path ("axolotl", "03-fine-tuning/axolotl"); "plugin:skill"
- resolves plugin-provided skills. ``preprocess`` applies the configured SKILL.md
- template / inline shell rendering; slash/preload callers render the message themselves."""
+ """View a skill (SKILL.md) or a file within its directory, as JSON. ``name`` is a skill name
+ or path ("axolotl", "03-fine-tuning/axolotl"); "plugin:skill" resolves plugin-provided
+ skills. ``preprocess`` applies the configured SKILL.md template / inline shell rendering;
+ slash/preload callers render the message themselves."""
try:
# Validate before the ':' dispatch so a Windows drive path (C:\skills\foo)
# can't be reinterpreted as a plugin namespace.
@@ -621,15 +598,9 @@ def skill_view(
except Exception:
logger.debug("Could not resolve org provenance for %s", skill_name, exc_info=True)
result = {
- "success": True,
- "name": skill_name,
- "description": frontmatter.get("description", ""),
- "tags": tags,
- "related_skills": related_skills,
- "content": rendered_content,
- "path": rel_path,
- "skill_dir": str(skill_dir) if skill_dir else None,
- "org_provenance": org_provenance,
+ "success": True, "name": skill_name, "description": frontmatter.get("description", ""),
+ "tags": tags, "related_skills": related_skills, "content": rendered_content, "path": rel_path,
+ "skill_dir": str(skill_dir) if skill_dir else None, "org_provenance": org_provenance,
"linked_files": linked_files if linked_files else None,
"usage_hint": "To view linked files, call skill_view(name, file_path) where file_path is e.g. 'references/api.md' or 'assets/config.yaml'" if linked_files else None,
**readiness,
@@ -693,15 +664,15 @@ registry.register(
def _skill_view_with_bump(args, **kw):
- """Invoke skill_view, then bump view_count/use on success (best-effort). Repeat-view
- dedup mirrors read_file's unchanged-stub: a SAME, unchanged skill file already loaded
- in this session returns a short stub (cache cleared on context compression)."""
+ """Invoke skill_view, then bump view_count/use on success (best-effort). Repeat-view dedup
+ mirrors read_file's unchanged-stub: a SAME, unchanged skill file already loaded in this
+ session returns a short stub (cache cleared on context compression)."""
name = args.get("name", "")
task_id = kw.get("task_id")
if (stub := _check_skill_view_dedup(task_id, name, args.get("file_path"))) is not None:
return stub
result = skill_view(name, file_path=args.get("file_path"), task_id=task_id)
- try:
+ with suppress(Exception):
parsed = json.loads(result)
if isinstance(parsed, dict) and parsed.get("success"):
_record_skill_view(task_id, name, args.get("file_path"), parsed)
@@ -711,8 +682,6 @@ def _skill_view_with_bump(args, **kw):
# Viewing is actively loading the skill to act on it — that counts as use
# (the curator's stale timer keys off last_used_at).
bump_use(str(resolved), task_id=kw.get("task_id"), session_id=kw.get("session_id"))
- except Exception:
- pass
return result