Skip to content

The agent that grows with you

refactor(tools): skills group pass 2c — walrus/guard folds in skill_usage and skills_guard, ast_audit suppress(), skillevaluator/provenance header compaction, docstring tightening

Tekniumdc5ca5a46138
diff --git a/tools/skill_provenance.py b/tools/skill_provenance.py
index 41362380850..3df9cf14304 100644
--- a/tools/skill_provenance.py
+++ b/tools/skill_provenance.py
@@ -6,13 +6,10 @@ AIAgent._memory_write_origin: ``token = set_current_write_origin(...)`` / ``rese
import contextvars
_write_origin: contextvars.ContextVar[str] = contextvars.ContextVar("skill_write_origin", default="foreground")
-
-# Sentinel used by the background review fork (run_agent._spawn_background_review).
-BACKGROUND_REVIEW = "background_review"
+BACKGROUND_REVIEW = "background_review" # sentinel used by run_agent._spawn_background_review
def set_current_write_origin(origin: str) -> contextvars.Token[str]:
- """Bind the active write origin; pass the Token to reset_current_write_origin."""
return _write_origin.set(origin or "foreground")
diff --git a/tools/skill_usage.py b/tools/skill_usage.py
index a6cc0ab0e37..ba6c95d13ac 100644
--- a/tools/skill_usage.py
+++ b/tools/skill_usage.py
@@ -30,9 +30,7 @@ except ImportError: # pragma: no cover - platform-specific fallback
import msvcrt
-STATE_ACTIVE = "active"
-STATE_STALE = "stale"
-STATE_ARCHIVED = "archived"
+STATE_ACTIVE, STATE_STALE, STATE_ARCHIVED = "active", "stale", "archived"
_VALID_STATES = {STATE_ACTIVE, STATE_STALE, STATE_ARCHIVED}
# Load-bearing built-ins (by frontmatter ``name``) the curator must NEVER archive/consolidate regardless of
@@ -41,7 +39,6 @@ PROTECTED_BUILTIN_SKILLS: Set[str] = set()
def is_protected_builtin(skill_name: str) -> bool:
- """Exempt from archival/consolidation on every path: auto transitions, LLM pass, direct archive_skill."""
return skill_name in PROTECTED_BUILTIN_SKILLS
@@ -75,8 +72,8 @@ def _usage_file_lock():
if msvcrt and (not lock_path.exists() or lock_path.stat().st_size == 0):
lock_path.write_text(" ", encoding="utf-8") # msvcrt needs a non-empty byte range to lock
with open(lock_path, "r+" if msvcrt else "a+", encoding="utf-8") as fd:
+ _flock(fd, True)
try:
- _flock(fd, True)
yield
finally:
with suppress(OSError, IOError):
@@ -183,16 +180,16 @@ def _toggle_suppressed_name(skill_name: str, *, add: bool) -> None:
if not skill_name or (skill_name in (names := read_suppressed_names())) == add:
return
(names.add if add else names.discard)(skill_name)
- data = "\n".join(sorted(names)) + ("\n" if names else "")
try:
- atomic_write_text(_skills_dir() / ".curator_suppressed", data, tmp_prefix=".curator_suppressed_")
+ atomic_write_text(_skills_dir() / ".curator_suppressed", "\n".join(sorted(names)) + ("\n" if names else ""),
+ tmp_prefix=".curator_suppressed_")
except Exception as e:
logger.debug("Failed to write curator suppression list: %s", e, exc_info=True)
def _iter_skill_mds(base: Path, *, local_only: bool) -> Iterator[Tuple[str, Path]]:
- """``(frontmatter name, SKILL.md)`` for flat and nested skills under *base*, skipping metadata/VCS/venv/cache
- dirs; *local_only* also skips external skill dirs mounted below the tree (curation must not touch them)."""
+ """``(frontmatter name, SKILL.md)`` under *base* minus metadata/VCS/venv/cache dirs; *local_only* also skips
+ external skill dirs mounted below the tree (curation must not touch them)."""
for skill_md in base.rglob("SKILL.md"):
if not (is_excluded_skill_path(skill_md) or (local_only and is_external_skill_path(skill_md))):
yield _read_skill_name(skill_md, fallback=skill_md.parent.name), skill_md
@@ -200,8 +197,7 @@ def _iter_skill_mds(base: Path, *, local_only: bool) -> Iterator[Tuple[str, Path
def _scan_local_skills(keep: Callable[[str, Path, Set[str], Dict[str, Any]], bool]) -> List[str]:
"""Sorted local skill names passing *keep(name, skill_md, bundled, usage)*; hub/protected names never reach it."""
- base = _skills_dir()
- if not base.exists():
+ if not (base := _skills_dir()).exists():
return []
hub, bundled, usage = _read_hub_installed_names(), _read_bundled_manifest_names(), load_usage()
return sorted({name for name, skill_md in _iter_skill_mds(base, local_only=True)
@@ -256,7 +252,7 @@ def _external_read_only_message(skill_name: str) -> str:
def is_curation_eligible(skill_name: str, skill_path: Optional[Path] = None) -> bool:
"""Agent-created: yes. Bundled: only with ``curator.prune_builtins``. Hub / external-dir / protected built-ins:
- never (external owner). Org-shared skills are eligible for improvement but protected from ARCHIVE/DELETE elsewhere."""
+ never (external owner). Org-shared skills are eligible here but protected from ARCHIVE/DELETE elsewhere."""
if ((skill_path is not None and is_external_skill_path(skill_path)) or is_protected_builtin(skill_name)
or is_hub_installed(skill_name)):
return False
@@ -273,7 +269,6 @@ def _is_curator_managed_record(record: Any) -> bool:
def is_curator_managed(skill_name: str) -> bool:
- """Policy-intent alias for the ``created_by`` marker check."""
return _is_curator_managed_record(load_usage().get(skill_name))
@@ -287,8 +282,8 @@ def list_unmanaged_skill_names() -> List[str]:
def unmanaged_report() -> List[Dict[str, Any]]:
- """Rows for :func:`list_unmanaged_skill_names`; ``has_provenance_key`` (False = pre-dates ``created_by``, True =
- present but unset) explains WHY, it is not a signal to adopt on."""
+ """Rows for :func:`list_unmanaged_skill_names`; ``has_provenance_key`` (False = pre-dates ``created_by``) explains
+ WHY, it is not a signal to adopt on."""
usage = load_usage()
return [_report_row(n, usage.get(n), has_provenance_key="created_by" in usage.get(n, {}), has_record=n in usage)
for n in list_unmanaged_skill_names()]
@@ -315,9 +310,9 @@ def adopt_skill(skill_name: str) -> Tuple[bool, str]:
if is_curator_managed(skill_name):
return True, f"'{skill_name}' is already curator-managed"
mark_agent_created(skill_name)
- if not is_curator_managed(skill_name):
- return False, f"could not mark '{skill_name}' as curator-managed"
- return True, f"adopted '{skill_name}' into curator management"
+ if is_curator_managed(skill_name):
+ return True, f"adopted '{skill_name}' into curator management"
+ return False, f"could not mark '{skill_name}' as curator-managed"
# --- Sidecar I/O ---
@@ -328,10 +323,10 @@ def _empty_record() -> Dict[str, Any]:
def _backfilled(rec: Any) -> Dict[str, Any]:
- """*rec* with every missing default key filled in (a fresh record when not a dict)."""
+ """*rec* with every missing default key appended (a fresh record when not a dict)."""
if not isinstance(rec, dict):
return _empty_record()
- return {**rec, **{k: v for k, v in _empty_record().items() if k not in rec}} # rec's key order first
+ return {**rec, **{k: v for k, v in _empty_record().items() if k not in rec}}
def _report_row(name: str, raw: Any, **extra: Any) -> Dict[str, Any]:
@@ -345,10 +340,10 @@ def load_usage() -> Dict[str, Dict[str, Any]]:
path = _usage_file()
try:
data = json.loads(path.read_text(encoding="utf-8")) if path.exists() else {}
- return {str(k): v for k, v in data.items() if isinstance(v, dict)} if isinstance(data, dict) else {}
except (OSError, json.JSONDecodeError) as e:
logger.debug("Failed to read %s: %s", path, e)
return {}
+ return {str(k): v for k, v in data.items() if isinstance(v, dict)} if isinstance(data, dict) else {}
def save_usage(data: Dict[str, Dict[str, Any]]) -> bool:
@@ -369,7 +364,7 @@ def get_record(skill_name: str) -> Dict[str, Any]:
def _locked_update(skill_name: str, op: Callable[[Dict[str, Dict[str, Any]]], Tuple[Any, bool]], fail_log: str,
guard: Optional[Callable[[], bool]] = None) -> Any:
- """Run *op(data) -> (result, dirty)* under the file lock, saving only when dirty; *guard* runs before locking.
+ """*op(data) -> (result, dirty)* under the file lock, saving only when dirty; *guard* runs before locking.
None when the guard failed, the save did not land, or anything raised (DEBUG-logged via *fail_log*)."""
try:
if guard is not None and not guard():
@@ -393,9 +388,8 @@ def seed_record_if_missing(skill_name: str) -> None:
def _mutate(skill_name: str, mutator, *, require_curation_eligible: bool = False) -> Any:
- """Load, apply *mutator(record)* in place, save; returns the mutator result (None if nothing landed). Telemetry
- is recorded for ANY skill; lifecycle mutators pass ``require_curation_eligible=True`` so state never lands on
- a skill the curator can't manage."""
+ """Load, apply *mutator(record)* in place, save; the mutator result (None if nothing landed). Telemetry is
+ recorded for ANY skill; lifecycle mutators pass ``require_curation_eligible=True`` (never write onto unmanaged)."""
if not skill_name:
return None
return _locked_update(skill_name, lambda data: (mutator(data.setdefault(skill_name, _empty_record())), True),
@@ -422,8 +416,8 @@ def telemetry_provenance(skill_name: str, record: Optional[Dict[str, Any]] = Non
from hermes_cli.plugins import get_plugin_manager
if get_plugin_manager().find_plugin_skill(skill_name) is not None:
return "installed"
- created_by = record.get("created_by") if isinstance(record, dict) else None
- if label := {"installed": "installed", "agent": "agent_created"}.get(created_by):
+ if label := {"installed": "installed", "agent": "agent_created"}.get(
+ record.get("created_by") if isinstance(record, dict) else None):
return label
if _find_external_skill_dir(skill_name) is not None:
return "external"
@@ -448,12 +442,11 @@ def _emit_skill_lifecycle(skill_name: str, action: str, *, record: Optional[Dict
def _mutate_and_emit(skill_name: str, action: str, mutator: Callable[[Dict[str, Any]], Dict[str, Any]],
**hook_kwargs: Any) -> None:
"""``_mutate`` then emit *action* with the mutator's facts as the record — only if the write landed."""
- facts = _mutate(skill_name, mutator)
- if isinstance(facts, dict):
+ if isinstance(facts := _mutate(skill_name, mutator), dict):
_emit_skill_lifecycle(skill_name, action, record=facts, **hook_kwargs)
-# --- Public counter-bump helpers — telemetry for ALL skills regardless of provenance (observability only) ---
+# --- Counter bumps — telemetry for ALL skills regardless of provenance (observability only) ---
def bump_view(skill_name: str) -> None:
_mutate(skill_name, lambda rec: _bump(rec, "view_count", "last_viewed_at"))
@@ -485,8 +478,7 @@ def bump_patch(skill_name: str, *, action: str = "patch", task_id: Optional[str]
def record_created(skill_name: str, *, agent_created: bool, task_id: Optional[str] = None,
session_id: Optional[str] = None) -> None:
- """Persist creation provenance and emit a create fact; the record is reset (a create is a new logical skill
- even if stale sidecar state survived an earlier deletion)."""
+ """Persist creation provenance and emit a create fact; the record is reset (a create is a new logical skill)."""
def _apply(rec: Dict[str, Any]) -> Dict[str, Any]:
rec.clear()
rec.update(_empty_record(), created_by="agent" if agent_created else None)
@@ -508,8 +500,7 @@ def mark_agent_created(skill_name: str) -> None:
def set_state(skill_name: str, state: str) -> None:
- """Set lifecycle state (no-op for invalid state / unmanageable skill). Emits archived/stale/restored
- (active<-archived); active<-stale emits nothing."""
+ """Set lifecycle state (no-op if invalid / unmanageable). Emits archived/stale/restored; active<-stale is silent."""
if state not in _VALID_STATES:
logger.debug("set_state: invalid state %r for %s", state, skill_name)
return
@@ -530,13 +521,12 @@ def set_state(skill_name: str, state: str) -> None:
def set_pinned(skill_name: str, pinned: bool) -> bool:
- """Set/clear the pin flag; False when the write did not land (not curation-eligible)."""
+ """False when the write did not land (not curation-eligible)."""
return _set_field(skill_name, "pinned", bool(pinned))
def set_sync(skill_name: str, sync: bool) -> None:
- """Opt-in ``sync`` flag (read by ``skills_sync_client.list_synced_skill_names``); curation-gated so bundled/hub/
- external skills can't be marked."""
+ """Opt-in ``sync`` flag (read by ``skills_sync_client``); curation-gated so bundled/hub/external can't be marked."""
_set_field(skill_name, "sync", bool(sync))
@@ -545,7 +535,6 @@ def is_sync_enabled(skill_name: str) -> bool:
def forget(skill_name: str) -> None:
- """Drop a skill's usage entry entirely (skill deleted)."""
if skill_name:
_locked_update(skill_name, lambda d: (None, d.pop(skill_name, None) is not None), "skill_usage.forget(%s) failed: %s")
@@ -578,8 +567,8 @@ def _relocate(src: Path, dest: Path, skill_name: str, action: str, **capture_kwa
def archive_skill(skill_name: str) -> Tuple[bool, str]:
- """Move a curator-eligible skill dir to ``.archive/`` (flattened; timestamp suffix on collision). Never
- hub skills; bundled built-ins only with ``curator.prune_builtins`` (and then suppressed from re-seeding)."""
+ """Move a curator-eligible skill dir to ``.archive/`` (flattened; timestamp suffix on collision). Never hub;
+ bundled built-ins only with ``curator.prune_builtins`` (and then suppressed from re-seeding)."""
skill_dir = _find_skill_dir(skill_name)
if skill_dir is None and _find_external_skill_dir(skill_name) is not None:
return False, _external_read_only_message(skill_name)
@@ -606,9 +595,8 @@ def archive_skill(skill_name: str) -> Tuple[bool, str]:
def restore_skill(skill_name: str) -> Tuple[bool, str]:
- """Move an archived skill back to the flat top-level layout (nesting NOT reconstructed). Refuses names now
- colliding with a hub skill, or a bundled built-in unless ``curator.prune_builtins`` is on (restoring is the
- documented way to lift a prune) — either would shadow the upstream copy."""
+ """Move an archived skill back to the flat layout (nesting NOT reconstructed). Refuses a name now colliding with
+ a hub skill, or a bundled built-in unless ``curator.prune_builtins`` is on (restoring lifts a prune)."""
if is_hub_installed(skill_name):
return False, f"skill '{skill_name}' is now hub-installed; restore would shadow the upstream version"
if is_bundled(skill_name) and not _prune_builtins_enabled():
@@ -625,8 +613,7 @@ def restore_skill(skill_name: str) -> Tuple[bool, str]:
and p.name[len(prefix):].isdigit()), reverse=True)
if not candidates:
return False, f"skill '{skill_name}' not found in archive"
- dest = _skills_dir() / skill_name
- if dest.exists():
+ if (dest := _skills_dir() / skill_name).exists():
return False, f"destination already exists: {dest}"
return _relocate(candidates[0], dest, skill_name, "restore")
@@ -636,14 +623,11 @@ def _match_skill_dir(skill_mds: Iterable[Path], skill_name: str) -> Optional[Pat
def _find_skill_dir(skill_name: str) -> Optional[Path]:
- """Skill dir by frontmatter ``name`` (flat or nested); the gated index iterator resolves org mirrors ONLY for
- the active org."""
- base = _skills_dir()
- if not base.exists():
- return None
+ """Skill dir by frontmatter ``name`` (flat or nested); the gated index iterator sees only the active org mirror."""
from agent.skill_utils import iter_skill_index_files
- return _match_skill_dir(
- (p for p in iter_skill_index_files(base, "SKILL.md") if not is_external_skill_path(p)), skill_name)
+ base = _skills_dir()
+ return _match_skill_dir((p for p in iter_skill_index_files(base, "SKILL.md") if not is_external_skill_path(p)),
+ skill_name) if base.exists() else None
def _find_external_skill_dir(skill_name: str) -> Optional[Path]:
@@ -656,8 +640,8 @@ def _find_external_skill_dir(skill_name: str) -> Optional[Path]:
# --- Reporting — for the curator CLI / slash command ---
def curated_report() -> List[Dict[str, Any]]:
- """One backfilled row per curator-managed skill with ``provenance`` and ``_persisted`` (a real record exists;
- fresh backfills get their inactivity clock seeded instead of counting as ancient)."""
+ """One backfilled row per curator-managed skill with ``provenance`` and ``_persisted`` (real record exists; fresh
+ backfills get their inactivity clock seeded instead of counting as ancient)."""
data = load_usage()
names = set(list_agent_created_skill_names())
# Pinned-but-unmanaged skills stay visible or their pin silently vanishes from `curator status`; the local-dir
@@ -668,14 +652,13 @@ def curated_report() -> List[Dict[str, Any]]:
def provenance(skill_name: str) -> str:
- """'hub', 'bundled', or 'agent' (the latter covers agent-authored AND local manually-authored skills)."""
+ """'hub' | 'bundled' | 'agent' (the latter also covers local manually-authored skills)."""
return "hub" if is_hub_installed(skill_name) else "bundled" if is_bundled(skill_name) else "agent"
def usage_report() -> List[Dict[str, Any]]:
"""Usage rows for EVERY skill on disk (built-ins and hub included); ``curated_report()`` is the managed subset."""
- base = _skills_dir()
- if not base.exists():
+ if not (base := _skills_dir()).exists():
return []
data = load_usage()
return [_report_row(n, data.get(n), provenance=provenance(n), _persisted=n in data)
diff --git a/tools/skillevaluator_scan.py b/tools/skillevaluator_scan.py
index 2d1fead27ea..2a516713b1c 100644
--- a/tools/skillevaluator_scan.py
+++ b/tools/skillevaluator_scan.py
@@ -20,17 +20,14 @@ from typing import List
logger = logging.getLogger(__name__)
SCANNER_BIN = "skillevaluator"
-
-# Keyless, deterministic checks. Schema/quality are index-pipeline hygiene, not install-time signal.
-# `security` invokes NVIDIA SkillSpector (static rules, no LLM); when absent it reports status="incomplete".
+# Keyless, deterministic checks (schema/quality are index-pipeline hygiene, not install-time signal). `security`
+# invokes NVIDIA SkillSpector (static rules, no LLM); when absent it reports status="incomplete".
TIER1_CHECKS = "pii,unicode,lint,license,security"
SCAN_TIMEOUT_SECONDS = 120
-# SkillEvaluator pii_patterns.yaml categories that indicate a possible REAL credential rather than
-# personal-info hygiene — the only findings that earn a confirmation prompt.
-SECRETS_CLASS_CHECKS = frozenset({
- "database_credentials", "hardcoded_secrets", "jwt_tokens", "webhook_urls",
- "aws_identifiers", "github_tokens", "private_keys"})
+# pii_patterns.yaml categories indicating a possible REAL credential (not PII hygiene) — the only prompt-worthy ones.
+SECRETS_CLASS_CHECKS = frozenset({"database_credentials", "hardcoded_secrets", "jwt_tokens", "webhook_urls",
+ "aws_identifiers", "github_tokens", "private_keys"})
@dataclass
@@ -102,9 +99,9 @@ def _parse_report(report: dict) -> Tier1Report:
def run_tier1_scan(skill_dir: Path, timeout: int = SCAN_TIMEOUT_SECONDS) -> Tier1Report:
"""Run SkillEvaluator Tier 1 over one skill dir; any failure returns ``available=False``, never raises."""
- if shutil.which(SCANNER_BIN) is None:
- return Tier1Report(available=False, error="scanner not on PATH")
unavailable = lambda why: Tier1Report(available=False, error=why) # noqa: E731
+ if shutil.which(SCANNER_BIN) is None:
+ return unavailable("scanner not on PATH")
with tempfile.TemporaryDirectory(prefix="se-tier1-") as outdir:
try:
subprocess.run([SCANNER_BIN, "validate", str(skill_dir), "--checks", TIER1_CHECKS, "--no-dedup",
@@ -113,8 +110,7 @@ def run_tier1_scan(skill_dir: Path, timeout: int = SCAN_TIMEOUT_SECONDS) -> Tier
return unavailable(f"scan timed out after {timeout}s")
except OSError as exc:
return unavailable(f"scanner failed to launch: {exc}")
- reports = sorted(Path(outdir).glob("skillevaluator-output-*.json"))
- if not reports:
+ if not (reports := sorted(Path(outdir).glob("skillevaluator-output-*.json"))):
return unavailable("scanner produced no JSON report")
try:
parsed = json.loads(reports[-1].read_text(encoding="utf-8"))
diff --git a/tools/skills_ast_audit.py b/tools/skills_ast_audit.py
index c2094da9e85..ddc09004ee2 100644
--- a/tools/skills_ast_audit.py
+++ b/tools/skills_ast_audit.py
@@ -5,6 +5,7 @@ legitimate uses, so findings are hints, not verdicts."""
from __future__ import annotations
import ast
+from contextlib import suppress
from pathlib import Path
from typing import List, Tuple
@@ -13,10 +14,8 @@ Finding = Tuple[str, int, str, str]
_IGNORED_DIRS = {"__pycache__", ".venv", "venv", "node_modules"}
# builtin name -> (index of the argument that must be a literal, pattern_id, description)
-_DYNAMIC_CALLS = {
- "__import__": (0, "dynamic_import_computed", "__import__ with non-literal module name"),
- "getattr": (1, "dynamic_getattr", "getattr with non-literal attribute name"),
-}
+_DYNAMIC_CALLS = {"__import__": (0, "dynamic_import_computed", "__import__ with non-literal module name"),
+ "getattr": (1, "dynamic_getattr", "getattr with non-literal attribute name")}
def _is_importlib(name: str) -> bool:
@@ -59,10 +58,8 @@ def _scan_source(content: str, rel_path: str) -> List[Finding]:
hit(node, "importlib_import", f"from {node.module} import ... — enables dynamic module loading")
self.generic_visit(node)
- try:
+ with suppress(RecursionError, ValueError, RuntimeError): # hostile input: keep what was collected so far
V().visit(tree)
- except (RecursionError, ValueError, RuntimeError):
- pass # hostile/pathological input: return what was collected so far
return findings
diff --git a/tools/skills_guard.py b/tools/skills_guard.py
index 38837111289..19dbfaec8b8 100644
--- a/tools/skills_guard.py
+++ b/tools/skills_guard.py
@@ -50,7 +50,7 @@ class Finding:
class ScanResult:
skill_name: str
source: str
- trust_level: str # "builtin" | "trusted" | "community"
+ trust_level: str # "builtin" | "trusted" | "community" | "agent-created"
verdict: str # "safe" | "caution" | "dangerous"
findings: List[Finding] = field(default_factory=list)
scanned_at: str = ""
@@ -59,7 +59,6 @@ class ScanResult:
# --- Threat patterns — (regex, pattern_id, severity, category, description) --
-
# File-modification verbs for the agent-config persistence tiers: a verb shortly before a config
# filename on the same line is scored as modification; a bare mention is not.
MODIFY_VERB_RE = (
@@ -101,9 +100,7 @@ def _prose_modify_re(file_alt: str) -> str:
def _content_contract_re(file_alt: str) -> str:
""""<file> should contain/include ..." prose. Authoring guides and attacks share this shape and are not
separable statically, so the tier is scored high (caution → confirmation), never critical."""
- return (
- rf'{file_alt}\b[^\n]{{0,40}}?\b(?:should|must|needs?\s+to)\s+'
- rf'(?:contain|say|include|have|list)\b')
+ return rf'{file_alt}\b[^\n]{{0,40}}?\b(?:should|must|needs?\s+to)\s+(?:contain|say|include|have|list)\b'
THREAT_PATTERNS = [
# ── Exfiltration: shell commands leaking secrets ──
@@ -350,19 +347,15 @@ THREAT_PATTERNS = [
_COMPILED_THREAT_PATTERNS = [(re.compile(pattern, re.IGNORECASE), *rest) for pattern, *rest in THREAT_PATTERNS]
-# Structural limits for skill directories
-MAX_FILE_COUNT = 50 # skills shouldn't have 50+ files
-MAX_TOTAL_SIZE_KB = 5120 # 5MB — large skills are informational only, not blocking
-MAX_SINGLE_FILE_KB = 256 # individual file > 256KB is suspicious
+# Structural limits: file count; total KB (5MB, informational only — large skills don't block); single-file KB.
+MAX_FILE_COUNT, MAX_TOTAL_SIZE_KB, MAX_SINGLE_FILE_KB = 50, 5120, 256
# Text extensions to scan; known binary extensions that should NOT be in a skill; script types allowed +x.
SCANNABLE_EXTENSIONS = {
'.md', '.txt', '.py', '.sh', '.bash', '.js', '.ts', '.rb', '.yaml', '.yml', '.json', '.toml',
- '.cfg', '.ini', '.conf', '.html', '.css', '.xml', '.tex', '.r', '.jl', '.pl', '.php',
-}
+ '.cfg', '.ini', '.conf', '.html', '.css', '.xml', '.tex', '.r', '.jl', '.pl', '.php'}
SUSPICIOUS_BINARY_EXTENSIONS = {
- '.exe', '.dll', '.so', '.dylib', '.bin', '.dat', '.com', '.msi', '.dmg', '.app', '.deb', '.rpm',
-}
+ '.exe', '.dll', '.so', '.dylib', '.bin', '.dat', '.com', '.msi', '.dmg', '.app', '.deb', '.rpm'}
_SCRIPT_EXTENSIONS = {'.sh', '.bash', '.py', '.rb', '.pl'}
# Zero-width / directional unicode used for text hiding, with the readable name reported in the finding.
@@ -372,8 +365,7 @@ _INVISIBLE_CHAR_NAMES = {
'\u2064': "invisible plus", '\ufeff': "BOM/zero-width no-break space",
'\u202a': "LTR embedding", '\u202b': "RTL embedding", '\u202c': "pop directional",
'\u202d': "LTR override", '\u202e': "RTL override", '\u2066': "LTR isolate", '\u2067': "RTL isolate",
- '\u2068': "first strong isolate", '\u2069': "pop directional isolate",
-}
+ '\u2068': "first strong isolate", '\u2069': "pop directional isolate"}
INVISIBLE_CHARS = set(_INVISIBLE_CHAR_NAMES)
@@ -432,8 +424,7 @@ def scan_skill(skill_path: Path, source: str = "community") -> ScanResult:
ignore = _load_skill_ignore(skill_path)
findings.extend(_check_structure(skill_path, ignore=ignore))
for f in skill_path.rglob("*"):
- rel = str(f.relative_to(skill_path))
- if f.is_file() and not ignore(rel):
+ if f.is_file() and not ignore(rel := str(f.relative_to(skill_path))):
findings.extend(scan_file(f, rel))
elif skill_path.is_file():
findings.extend(scan_file(skill_path, skill_path.name))
@@ -446,10 +437,9 @@ def _content_digest(skill_path: Path) -> str:
"""Canonical SHA-256 over (POSIX relative path, file bytes) ORDERED by the rel-path STRING — Path sorting is
case-insensitive on Windows and diverged from ``skills_hub.bundle_content_hash`` (every installed skill then
reported ``update_available`` forever). String order keeps both sides byte-symmetric."""
- h = hashlib.sha256()
if not skill_path.is_dir():
- h.update(skill_path.read_bytes())
- return h.hexdigest()
+ return hashlib.sha256(skill_path.read_bytes()).hexdigest()
+ h = hashlib.sha256()
for rel, p in sorted((p.relative_to(skill_path).as_posix(), p) for p in skill_path.rglob("*") if p.is_file()):
h.update(rel.encode("utf-8") + b"\x00")
h.update(p.read_bytes())
@@ -462,9 +452,8 @@ def content_hash(skill_path: Path) -> str:
return f"sha256:{_content_digest(skill_path)[:16]}"
-def scan_skill_cached(
- skill_path: Path, source: str = "community", *, source_url: str = "", cache_dir: Path | None = None,
-) -> Tuple[ScanResult, dict]:
+def scan_skill_cached(skill_path: Path, source: str = "community", *, source_url: str = "",
+ cache_dir: Path | None = None) -> Tuple[ScanResult, dict]:
"""Scan plus attestation dict; the cache (keyed by content digest + source identity) only serves exact
current content under the current scanner version."""
digest = _content_digest(skill_path)
@@ -550,13 +539,11 @@ def _check_structure(skill_dir: Path, ignore=None) -> List[Finding]:
st = f.stat()
except OSError:
continue
- size = st.st_size
- total_size += size
+ total_size += (size := st.st_size)
if size > MAX_SINGLE_FILE_KB * 1024:
add("oversized_file", "medium", "structural", rel, f"{size // 1024}KB",
f"file is {size // 1024}KB (limit: {MAX_SINGLE_FILE_KB}KB)")
- ext = f.suffix.lower()
- if ext in SUSPICIOUS_BINARY_EXTENSIONS:
+ if (ext := f.suffix.lower()) in SUSPICIOUS_BINARY_EXTENSIONS:
add("binary_file", "critical", "structural", rel, f"binary: {ext}",
f"binary/executable file ({ext}) should not be in a skill")
if ext not in _SCRIPT_EXTENSIONS and st.st_mode & 0o111:
@@ -584,8 +571,8 @@ def _load_skill_ignore(skill_dir: Path):
for ig in (skill_dir / name for name in _SKILL_IGNORE_FILENAMES):
with suppress(UnicodeDecodeError, OSError):
if ig.is_file():
- lines = (raw.strip() for raw in ig.read_text(encoding="utf-8").splitlines())
- patterns.extend(line for line in lines if line and not line.startswith("#"))
+ patterns.extend(s for s in map(str.strip, ig.read_text(encoding="utf-8").splitlines())
+ if s and not s.startswith("#"))
def ignore(rel: str) -> bool:
rel_posix = Path(rel).as_posix()
@@ -629,8 +616,8 @@ def _resolve_trust_level(source: str) -> str:
def _determine_verdict(findings: List[Finding]) -> str:
"""critical → dangerous, high → caution; medium/low alone are informational (safe)."""
- severities = {f.severity for f in findings}
- return "dangerous" if "critical" in severities else "caution" if "high" in severities else "safe"
+ sev = {f.severity for f in findings}
+ return "dangerous" if "critical" in sev else "caution" if "high" in sev else "safe"
def _build_summary(name: str, source: str, trust: str, verdict: str, findings: List[Finding]) -> str: