Files
personamanagmentlayer__pcl/scripts/validate-skills.py
ANGX 4e07a6a874 feat(stdlib)!: Agent Skills v1.0 conformance, resolver fix, and 17 new skills (#43)
* chore(stdlib): add conformance tooling for the skills library

The library had no gate that could express what a valid skill is, so
malformed frontmatter, over-budget files and credential literals went
unnoticed. These scripts define and enforce that contract.

- validate-skills.py: extend with Agent Skills v1.0 conformance -
  naming rules and directory agreement, an explicit activation trigger
  in the description, canonical layout, the 500-line SKILL.md budget,
  relative-link integrity, and two security rules (no unrestricted
  Bash(*) grant, no credential literals). Structural checks now read a
  skill as a whole so material moved into references/ still counts.
- split-skill-references.py: move the heaviest sections of an oversized
  SKILL.md into references/. Verifies line by line that every source
  line lands in exactly one output and refuses to write otherwise.
- generate-skill-inventory.py: derive SKILLS_INVENTORY.md and
  DIRECTORY_STRUCTURE.md from the tree so they cannot drift.
- generate-skill-catalog.py: index the canonical layout only, count
  references/ toward a skill's size, and stop truncating descriptions
  at 200 characters - the catalog is the discovery surface and must
  carry the activation trigger.
- .gitignore: Python bytecode from these scripts, and local .pcl state.

Reviewed-by: Security Analyst, Compiler Engineer, DevX Engineer

* fix(stdlib): bring all 174 skills to Agent Skills v1.0 conformance

No skill in the library was conformant. 41 of 175 failed the existing
validator outright, and none satisfied the spec. This pass fixes the
tree and records the result. See stdlib/CHANGELOG.md for the per-skill
versioning and migration notes.

BREAKING CHANGE: one skill removed, five moved, seven with a capability
withdrawn. Library version 2.0.0 -> 3.0.0.

Structure
- Convert 71 skills stored as flat files to <category>/<name>/SKILL.md,
  the layout the spec requires and the only one able to carry companion
  resources.
- Rename 5 directories that disagreed with their declared name:, so
  resolution by path and by name stop giving different answers. The
  repository docs already referenced the corrected paths.
- Merge the duplicate legal-technology skills into legaltech-expert and
  delete legal-tech-expert, carrying over everything the survivor
  lacked.

Metadata
- Repair 20 unparseable frontmatters: an H1 preceded the --- block and
  the key was skill_id: instead of name:, so any parser reading
  frontmatter at offset 0 saw no metadata at all. The objectives,
  prerequisites and outcome they carried become real body sections.
- Add the missing version: to 21 skills, fold keywords into tags, move
  expertise_level under metadata, and align 10 category: values with
  their directory.

Discoverability
- Give all 174 descriptions an explicit "Use when ..." trigger, built
  from each skill's own tags and section headings. 152 previously said
  only what the skill was about, leaving an agent nothing to match on.

Progressive disclosure
- Split 127 oversized skills, moving bulk material into references/ read
  on demand and leaving an index behind. A SKILL.md now averages 277
  lines instead of 644 and none exceeds 500. Verified lossless line by
  line against the previous revision.

Security
- Withdraw the unrestricted Bash(*) grant from 7 skills; none of the six
  non-financial ones contained a single shell command. finance-expert
  documents code that charges cards and reads bank accounts, so it now
  grants no shell at all.
- Remove credential literals: Stripe keys, two JWT signing keys, an
  Express session secret, a Redis password and WiFi credentials, all
  inlined in examples. They load from the environment now.
- finance-expert: idempotency keys were documented but absent, so a
  retry double-charged; webhook signature failures were not caught, so a
  forged event raised uncaught instead of being rejected;
  int(amount * 100) truncated fractions of a cent. All fixed, plus a
  money-movement guardrails section.
- trading-expert: place_order routed without any pre-trade risk check
  although a RiskManager sat unused in the next section. Risk validation
  now runs before routing and raises rather than returning a boolean a
  caller can ignore; live routing is opt-in.
- Correct an Ansible Vault example that showed plaintext under an
  "encrypted" label.

Also fixes 8 broken documentation links and a prematurely closed code
fence in skill-creator-expert that leaked template content into prose.

Per the repository file-organization policy, four stale progress reports
(SKILLS_MANIFEST, COMPLETE_SKILLS_LIBRARY, COMPLETE_LIBRARY_MANIFEST,
PHASE_3_DATA_ANALYTICS) move out of stdlib/ into the gitignored
.roadmap/status/stdlib/; they claimed 31 of 100 skills complete. They
appear here as deletions because .roadmap/ is not tracked.

Reviewed-by: Security Analyst, Language Designer, DevX Engineer,
             Documentation Specialist

* fix(skills): resolve standard library skills from their real layout

No stdlib skill was resolvable. SkillResolver looked for
`<stdlibDir>/<name>.md` with stdlibDir defaulting to `stdlib/skills`, a
directory that does not exist, and the path ignored the category level
the library actually uses. The two LSP providers had the same wrong
directory and scanned only one level deep, so neither completion nor
go-to-definition could see a skill either.

- skill-discovery.ts: enumerate skill entry points across the shapes in
  use - a flat `<name>.md`, `<name>/SKILL.md`, and
  `<category>/<name>/SKILL.md` - while skipping references/, scripts/
  and assets/, which hold supporting material rather than entry points.
- skill-resolver.ts: point stdlibDir at `stdlib`, and look the name up
  as a directory under any category as well as the two legacy shapes, so
  existing custom stdlib directories keep working. The name is used as a
  single path segment and rejected if it contains a separator or
  traversal, so a reference cannot escape the stdlib root.
- Rewire both LSP providers onto the shared discovery helper.

Tests: 13 new cases covering the layouts, ordering, the references/
exclusion, path-traversal rejection, and an integration test that
resolves real shipped skills by bare name.

Reviewed-by: Compiler Engineer, Runtime Architect, Security Analyst

* feat(stdlib): add 17 skills covering the library's largest gaps

Selection was driven by cross-referencing coverage against observed usage
rather than by intuition: Stack Overflow and GitHub Octoverse data for
which technologies matter, and install rankings on public agent-skill
registries for the shape of skills people actually use.

That comparison found a mismatch of form, not only of coverage. The
most-installed skills in the ecosystem are procedures - tdd, refactoring,
frontend design, browser automation - while all 174 existing skills were
domain knowledge shaped as <domain>-expert. The new workflows/ category
closes that gap and introduces the <activity>-workflow convention.

New category workflows/ (4)
- tdd-workflow, refactoring-workflow, debugging-workflow,
  code-review-workflow. code-review-workflow complements rather than
  replaces code-review-expert: procedure versus judgement. Merge them if
  the split proves confusing in use.

Applied AI (3) - the largest coverage gap
- llm-engineering-expert, rag-expert, agent-engineering-expert.
  The library referenced LLMs in tags but covered none of it: pgvector
  appeared zero times across 174 skills, LangChain once, MCP twice
  despite the repository shipping an MCP integration guide.

Capabilities (2)
- document-processing-expert (PDF, DOCX, XLSX, PPTX in one skill),
  browser-automation-expert (a client against third-party sites,
  distinct from playwright-expert which tests your own application).

Data (3)
- mysql-expert, the second most used database and previously absent
  while rarer stores had skills; analytical-databases-expert;
  stream-processing-expert.

Security (3)
- secrets-management-expert, supply-chain-security-expert (OWASP A03
  2025), identity-access-expert (OWASP A01).

Interface and meta (2)
- accessibility-expert: design/ held only design-expert, which despite
  its name covers software architecture, so WCAG had no home.
- skill-router: find and compose skills across 191, with a
  disambiguation table for overlapping pairs.

Grouping by decision context rather than by technology keeps the growth
to 17 skills for roughly 50 topics. Five vector stores live inside
rag-expert; four document formats inside document-processing-expert.
Per-tool depth sits in references/, which the progressive-disclosure work
in 3.0.0 made cheap.

Four further gaps are better served by extending existing skills -
PyTorch and TensorFlow in ml-expert, OpenTelemetry in monitoring-expert,
Pulumi and Crossplane in terraform-expert - and are not in this change.

Library 3.0.0 -> 3.1.0, additive only. Adds the workflows label to the
inventory generator; catalogue, inventory, structure and CHANGELOG
regenerated.

Reviewed-by: Product Strategist, Security Analyst, DevX Engineer,
             Documentation Specialist

* fix(test): create test directories with mkdtempSync

CodeQL flagged js/insecure-temporary-file (CWE-377) on both suites.
`join(tmpdir(), <predictable name>)` followed by mkdirSync creates a
world-readable directory at a guessable path, so another local user can
pre-create it or plant a symlink between the name being computed and the
directory being created.

mkdtempSync creates the directory atomically with a random suffix and
0700 permissions, which closes both the disclosure and the race.

One occurrence is new in this branch (skill-discovery); the other
pre-dates it in skill-resolver and is fixed in the same pass rather than
left as a known finding in a file this branch already touches.
2026-09-05 04:35:30 +02:00

661 lines
25 KiB
Python

#!/usr/bin/env python3
"""
PCL Standard Library - Skill Validation Script
Validates all skills in the stdlib for:
- YAML frontmatter syntax
- Required fields
- File structure
- Code block formatting
- Agent Skills v1.0 conformance (naming, description triggers, layout,
progressive disclosure budgets, reference link integrity)
Security: This script uses safe YAML loading and validates all file paths.
"""
import re
import sys
from collections import Counter
from pathlib import Path
from typing import Dict, List, Optional, Tuple
import yaml
class SkillValidator:
"""Validate PCL skills with security best practices"""
REQUIRED_FIELDS = ['name', 'version', 'description', 'category']
RECOMMENDED_FIELDS = ['tags', 'allowed-tools']
# Maximum file size to prevent DoS (10MB)
MAX_FILE_SIZE = 10 * 1024 * 1024
# --- Agent Skills v1.0 conformance budgets -----------------------------
# Skill names: lowercase alphanumerics separated by single hyphens.
NAME_PATTERN = re.compile(r'^[a-z0-9]+(-[a-z0-9]+)*$')
NAME_MAX_LENGTH = 64
DESCRIPTION_MAX_LENGTH = 1024
# Progressive disclosure: SKILL.md is loaded in full on activation, so it
# stays small; bulk material belongs in references/ loaded on demand.
SKILL_MAX_LINES = 500
REFERENCE_MAX_LINES = 1000
# Phrases that make an activation trigger explicit for the agent.
TRIGGER_PATTERNS = ('use when', 'use this skill when', 'use this skill for')
# --- Security rules ----------------------------------------------------
# A skill's allowed-tools is a capability grant. An unrestricted shell lets
# any example in the skill run against the user's machine, so tool grants
# must name the commands they need.
UNRESTRICTED_SHELL = re.compile(r'^Bash\s*\(\s*\*\s*\)$')
# Credential literals in examples teach the pattern of inlining secrets and
# get copied verbatim into real code, so even an obvious placeholder in
# provider-key shape is wrong. These prefixes are unambiguous, so they are
# matched wherever they appear rather than only after a known field name.
SECRET_LITERAL = re.compile(
r'''(?x)
["'](?:
sk_[A-Za-z0-9_.-]{2,} | rk_[A-Za-z0-9_.-]{2,} | pk_live[A-Za-z0-9_.-]* |
AKIA[A-Z0-9]{6,} | ghp_[A-Za-z0-9]{6,} | gho_[A-Za-z0-9]{6,} |
xox[abprs]-[A-Za-z0-9-]{6,} | AIza[A-Za-z0-9_-]{6,} |
eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}
)["']
| -----BEGIN[ A-Z]*PRIVATE\ KEY-----
''')
# A credential-named parameter assigned any string literal. Placeholders are
# common in documentation, so this is a warning rather than an error - but
# the habit it teaches is still the wrong one.
SECRET_ASSIGNMENT = re.compile(
r'''(?ix)
\b(api[_-]?key|secret|secret[_-]?key|client[_-]?secret|password|passwd|
access[_-]?token|auth[_-]?token|private[_-]?key)\b
\s*[:=]\s*
["'](?P<value>[^"'\n]*)["']
''')
# Values that are an indirection rather than a secret: environment lookups,
# template substitutions, and empty placeholders are the correct pattern.
SECRET_INDIRECTION = re.compile(
r'''(?ix)
^\s*$
| \{\{ | \$\{ | \$\( | ^<.*>$ | ^%\w+%$
| \b(os\.environ|os\.getenv|getenv|env_var|environ\[|lookup\s*\(|
secrets?manager|vault|process\.env|System\.getenv)\b
''')
def __init__(self, stdlib_path: str) -> None:
"""
Initialize the skill validator.
Args:
stdlib_path: Path to the stdlib directory
Raises:
ValueError: If stdlib_path is not a valid directory
"""
self.stdlib_path = Path(stdlib_path).resolve()
if not self.stdlib_path.is_dir():
raise ValueError(f"Invalid stdlib path: {stdlib_path}")
# Relative links may legitimately point at repository documentation
# outside stdlib/, but never outside the repository itself.
self.repo_root = self.stdlib_path.parent
self.errors: List[str] = []
self.warnings: List[str] = []
self.stats: Counter = Counter()
def find_skill_files(self) -> List[Path]:
"""
Find all skill files.
Returns:
List of unique Path objects for skill files
Raises:
PermissionError: If directory cannot be accessed
"""
skill_files: List[Path] = []
try:
skill_files.extend(self.stdlib_path.glob("**/SKILL.md"))
skill_files.extend(self.stdlib_path.glob("**/*-expert.md"))
except PermissionError as e:
print(f"Error: Permission denied accessing {self.stdlib_path}: {e}", file=sys.stderr)
raise
# Remove duplicates and validate paths
unique_files = set(skill_files)
validated_files = [
f for f in unique_files
if self._is_safe_path(f)
]
return sorted(validated_files)
def _is_safe_path(self, file_path: Path) -> bool:
"""
Validate that file path is within stdlib directory (prevent path traversal).
Args:
file_path: Path to validate
Returns:
True if path is safe, False otherwise
"""
return self._is_within(file_path, self.stdlib_path)
@staticmethod
def _is_within(candidate: Path, root: Path) -> bool:
"""
Return True when candidate resolves inside root.
Args:
candidate: Path to test
root: Directory that must contain it
Returns:
True if candidate is inside root, False otherwise
"""
try:
return candidate.resolve().is_relative_to(root)
except (ValueError, OSError):
return False
def extract_yaml_frontmatter(self, content: str) -> Tuple[Dict, bool]:
"""
Extract and validate YAML frontmatter using safe loading.
Args:
content: File content
Returns:
Tuple of (frontmatter dict, validity boolean)
Security: Uses yaml.safe_load to prevent code execution
"""
match = re.match(r'^---\s*\n(.*?)\n---\s*\n', content, re.DOTALL)
if not match:
return {}, False
try:
# Use safe_load to prevent arbitrary code execution
frontmatter = yaml.safe_load(match.group(1))
if not isinstance(frontmatter, dict):
return {}, False
return frontmatter, True
except yaml.YAMLError as e:
print(f"Warning: YAML parsing error: {e}", file=sys.stderr)
return {}, False
def validate_skill(self, file_path: Path) -> Dict:
"""
Validate a single skill file.
Args:
file_path: Path to skill file
Returns:
Dictionary with validation results
Security: Validates file size and uses safe file reading
"""
result = {
'path': str(file_path.relative_to(self.stdlib_path)),
'errors': [],
'warnings': [],
'valid': True
}
try:
# Check file size to prevent DoS
file_size = file_path.stat().st_size
if file_size > self.MAX_FILE_SIZE:
result['errors'].append(f"File too large ({file_size} bytes, max {self.MAX_FILE_SIZE})")
result['valid'] = False
return result
# Read file with explicit encoding
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
content = f.read()
# Check for YAML frontmatter
frontmatter, yaml_valid = self.extract_yaml_frontmatter(content)
if not yaml_valid and not frontmatter:
result['errors'].append("Missing or invalid YAML frontmatter")
result['valid'] = False
return result
if not yaml_valid:
result['errors'].append("Invalid YAML syntax in frontmatter")
result['valid'] = False
# Check required fields
for field in self.REQUIRED_FIELDS:
if field not in frontmatter:
result['errors'].append(f"Missing required field: {field}")
result['valid'] = False
# Check recommended fields
for field in self.RECOMMENDED_FIELDS:
if field not in frontmatter:
result['warnings'].append(f"Missing recommended field: {field}")
# Validate field values
if 'version' in frontmatter:
version = frontmatter['version']
if not re.match(r'^\d+\.\d+\.\d+$', str(version)):
result['warnings'].append(
f"Version should follow semantic versioning (x.y.z): {version}"
)
if 'name' in frontmatter:
name = frontmatter['name']
if not isinstance(name, str) or not name:
result['errors'].append("Name must be a non-empty string")
result['valid'] = False
# Check content structure
if not content.strip():
result['errors'].append("File is empty")
result['valid'] = False
return result
# Structural checks look at the skill as a whole: under progressive
# disclosure the bulk sections live in references/, not in SKILL.md.
references = sorted(
ref for ref in file_path.parent.glob('references/*.md')
if self._is_safe_path(ref)
)
whole = content
for ref in references:
try:
whole += '\n' + ref.read_text(encoding='utf-8', errors='ignore')
except (OSError, IOError):
continue
# Check for main heading (in SKILL.md itself, after the frontmatter)
body = re.sub(r'^---\s*\n.*?\n---\s*\n', '', content, count=1, flags=re.DOTALL)
if not re.search(r'^# \S', body, re.MULTILINE):
result['warnings'].append("Missing main heading (# Title)")
# Check for core concepts section
if not re.search(r'^## (Core Concepts|Core Expertise|Expertise)', whole, re.M):
result['warnings'].append("Missing 'Core Concepts' or 'Expertise' section")
# Check for code examples
code_blocks = re.findall(r'```[\s\S]*?```', whole)
if not code_blocks:
result['warnings'].append("No code examples found")
elif len(code_blocks) < 2:
result['warnings'].append("Very few code examples (< 2)")
# Check for best practices
if '## Best Practices' not in whole:
result['warnings'].append("Missing 'Best Practices' section")
# Check for anti-patterns
if not re.search(r'^## Anti-?[Pp]atterns', whole, re.M):
result['warnings'].append("Missing 'Anti-Patterns' section")
# Check for resources
if not re.search(r'^## (Resources|References)', whole, re.M):
result['warnings'].append("Missing 'Resources' section")
# Check size. A small SKILL.md backed by references is the intended
# shape, so only flag a skill that is thin overall.
total_size = file_size + sum(ref.stat().st_size for ref in references)
if total_size < 5000:
result['warnings'].append(f"Skill may be too small ({total_size} bytes)")
elif file_size > 500000: # More than 500KB
result['warnings'].append(f"File may be too large ({file_size} bytes)")
# Agent Skills v1.0 conformance
self._validate_spec_v1(file_path, content, frontmatter, result)
except (OSError, IOError) as e:
result['errors'].append(f"File reading error: {str(e)}")
result['valid'] = False
except Exception as e: # pylint: disable=broad-except
result['errors'].append(f"Unexpected validation error: {str(e)}")
result['valid'] = False
return result
def _skill_sources(self, file_path: Path, content: str):
"""Yield (label, text) for SKILL.md and each of its reference files.
Args:
file_path: Path to the SKILL.md
content: Already-read SKILL.md content
Yields:
Tuples of a short label and the file's text
"""
yield file_path.name, content
for ref in sorted(file_path.parent.glob('references/*.md')):
if not self._is_safe_path(ref):
continue
try:
yield f'references/{ref.name}', ref.read_text(
encoding='utf-8', errors='ignore')
except (OSError, IOError):
continue
@staticmethod
def _strip_code_fences(content: str) -> str:
"""
Remove fenced code blocks, honouring fences longer than three backticks.
A naive non-greedy regex closes a ```` fence on the first ``` inside it,
leaking sample code back into the prose. Track the opening fence length
and close only on a fence at least as long.
Args:
content: Markdown content
Returns:
Content with fenced blocks removed
"""
out: List[str] = []
fence: Optional[str] = None
for line in content.split('\n'):
marker = re.match(r'^\s*(`{3,}|~{3,})', line)
if fence is None:
if marker:
fence = marker.group(1)
else:
out.append(line)
elif marker and marker.group(1)[0] == fence[0] and len(marker.group(1)) >= len(fence):
fence = None
return '\n'.join(out)
def _validate_spec_v1(
self,
file_path: Path,
content: str,
frontmatter: Dict,
result: Dict
) -> None:
"""
Check Agent Skills v1.0 conformance.
Covers the rules the generic field checks above cannot express: the
skill's identity must agree with its location on disk, its description
must state an activation trigger, and its size must respect the
progressive-disclosure budget.
Args:
file_path: Path to the skill file
content: Full file content
frontmatter: Parsed frontmatter mapping
result: Validation result to append errors/warnings to
"""
rel = file_path.relative_to(self.stdlib_path)
parts = rel.parts
# --- Layout: <category>/<skill-name>/SKILL.md --------------------
if file_path.name == 'SKILL.md':
if len(parts) != 3:
result['errors'].append(
f"Unexpected layout: expected <category>/<name>/SKILL.md, got {rel}"
)
result['valid'] = False
slug = parts[-2] if len(parts) >= 2 else ''
else:
result['errors'].append(
"Flat layout: a skill must live in its own directory as "
f"{rel.parent}/{rel.stem}/SKILL.md"
)
result['valid'] = False
slug = rel.stem
category_dir = parts[0] if parts else ''
# --- name -------------------------------------------------------
name = frontmatter.get('name')
if isinstance(name, str) and name:
if len(name) > self.NAME_MAX_LENGTH:
result['errors'].append(
f"Name exceeds {self.NAME_MAX_LENGTH} characters: {len(name)}"
)
result['valid'] = False
if not self.NAME_PATTERN.match(name):
result['errors'].append(
"Name must be lowercase alphanumerics separated by single "
f"hyphens (no leading/trailing/consecutive hyphens): {name!r}"
)
result['valid'] = False
if slug and name != slug:
result['errors'].append(
f"Name {name!r} does not match its directory {slug!r}"
)
result['valid'] = False
# --- description ------------------------------------------------
description = frontmatter.get('description')
if isinstance(description, str) and description:
if len(description) > self.DESCRIPTION_MAX_LENGTH:
result['errors'].append(
f"Description exceeds {self.DESCRIPTION_MAX_LENGTH} characters: "
f"{len(description)}"
)
result['valid'] = False
lowered = description.lower()
if not any(trigger in lowered for trigger in self.TRIGGER_PATTERNS):
result['errors'].append(
"Description states no activation trigger; add a "
"'Use when ...' clause naming the keywords, file types or "
"tasks that should select this skill"
)
result['valid'] = False
elif description is not None and not isinstance(description, str):
result['errors'].append("Description must be a string")
result['valid'] = False
# --- category ---------------------------------------------------
category = frontmatter.get('category')
if category and category_dir and category != category_dir:
result['errors'].append(
f"Category {category!r} does not match its directory {category_dir!r}"
)
result['valid'] = False
# --- capability grants ------------------------------------------
declared = frontmatter.get('allowed-tools')
if isinstance(declared, str):
declared = [t.strip() for t in declared.split(',')]
for tool in declared or []:
if isinstance(tool, str) and self.UNRESTRICTED_SHELL.match(tool.strip()):
result['errors'].append(
"allowed-tools grants an unrestricted shell (Bash(*)); name "
"the commands the skill needs, e.g. Bash(python:*, pytest:*)"
)
result['valid'] = False
# --- credential literals ----------------------------------------
# Checked across the whole skill: an example in references/ is copied
# just as readily as one in SKILL.md.
for source, text in self._skill_sources(file_path, content):
for match in self.SECRET_LITERAL.finditer(text):
result['errors'].append(
f"{source}: credential literal in provider-key shape "
f"({match.group(0)[:24]}...); load it from the environment "
"or a secrets manager instead"
)
result['valid'] = False
for match in self.SECRET_ASSIGNMENT.finditer(text):
if self.SECRET_INDIRECTION.search(match.group('value')):
continue # an env/vault lookup or empty placeholder is correct
result['warnings'].append(
f"{source}: {match.group(1)} assigned a string literal; "
"prefer os.environ or a secrets manager even in examples"
)
# --- progressive disclosure -------------------------------------
line_count = content.count('\n') + 1
if line_count > self.SKILL_MAX_LINES:
result['errors'].append(
f"SKILL.md is {line_count} lines (budget {self.SKILL_MAX_LINES}); "
"move detailed material into references/ and link to it"
)
result['valid'] = False
# --- reference files and link integrity -------------------------
skill_dir = file_path.parent
for ref in sorted(skill_dir.glob('references/*.md')):
if not self._is_safe_path(ref):
continue
try:
ref_lines = ref.read_text(encoding='utf-8', errors='ignore').count('\n') + 1
except (OSError, IOError):
continue
if ref_lines > self.REFERENCE_MAX_LINES:
result['warnings'].append(
f"Reference {ref.name} is {ref_lines} lines "
f"(budget {self.REFERENCE_MAX_LINES}); consider splitting it"
)
# Code samples routinely contain ](...)-looking text; strip fenced and
# inline code before scanning so they are not mistaken for links.
prose = re.sub(r'`[^`\n]*`', '', self._strip_code_fences(content))
for link in re.findall(r'\]\((?!https?://|mailto:|#)([^)\s]+)\)', prose):
target = (skill_dir / link.split('#')[0]).resolve()
if not self._is_within(target, self.repo_root):
result['errors'].append(f"Link escapes the repository: {link}")
result['valid'] = False
elif not target.exists():
result['errors'].append(f"Broken relative link: {link}")
result['valid'] = False
def validate_all(self) -> Dict:
"""
Validate all skills.
Returns:
Dictionary with validation summary
"""
print("Finding skill files...")
skill_files = self.find_skill_files()
print(f"Found {len(skill_files)} skill files\n")
print("Validating skills...")
results: List[Dict] = []
valid_count = 0
error_count = 0
warning_count = 0
for file_path in skill_files:
result = self.validate_skill(file_path)
results.append(result)
if result['valid']:
valid_count += 1
print(f" [OK] {result['path']}")
else:
error_count += 1
print(f" [ERROR] {result['path']}")
warning_count += len(result['warnings'])
# Print detailed results
self._print_results(results, error_count, warning_count, len(skill_files))
return {
'total': len(skill_files),
'valid': valid_count,
'errors': error_count,
'warnings': warning_count,
'results': results
}
def _print_results(
self,
results: List[Dict],
error_count: int,
warning_count: int,
total_files: int
) -> None:
"""
Print validation results.
Args:
results: List of validation results
error_count: Number of errors
warning_count: Number of warnings
total_files: Total number of files validated
"""
print("\n" + "=" * 70)
print("VALIDATION RESULTS")
print("=" * 70)
# Print errors
if error_count > 0:
print(f"\nERRORS ({error_count} skills with errors):")
print("-" * 70)
for result in results:
if not result['valid']:
print(f"\n{result['path']}:")
for error in result['errors']:
print(f" - ERROR: {error}")
# Print warnings
if warning_count > 0:
print(f"\nWARNINGS ({warning_count} total warnings):")
print("-" * 70)
for result in results:
if result['warnings']:
print(f"\n{result['path']}:")
for warning in result['warnings']:
print(f" - WARNING: {warning}")
# Summary
print("\n" + "=" * 70)
print("SUMMARY")
print("=" * 70)
print(f"Total skills: {total_files}")
print(f"Valid skills: {total_files - error_count}")
print(f"Skills with errors: {error_count}")
print(f"Total warnings: {warning_count}")
print(f"\nValidation: {'PASSED' if error_count == 0 else 'FAILED'}")
print("=" * 70)
def main() -> int:
"""
Main entry point.
Returns:
Exit code (0 if passed, 1 if failed)
"""
try:
script_dir = Path(__file__).parent.resolve()
stdlib_path = script_dir.parent / 'stdlib'
print("=" * 70)
print("PCL Standard Library - Skill Validator")
print("=" * 70)
print(f"\nStdlib path: {stdlib_path}\n")
# Validate stdlib path exists
if not stdlib_path.is_dir():
print(f"Error: Stdlib directory not found: {stdlib_path}", file=sys.stderr)
return 1
validator = SkillValidator(str(stdlib_path))
summary = validator.validate_all()
# Exit with error code if validation failed
return 0 if summary['errors'] == 0 else 1
except KeyboardInterrupt:
print("\n\nOperation cancelled by user", file=sys.stderr)
return 130
except Exception as e: # pylint: disable=broad-except
print(f"\nError: {e}", file=sys.stderr)
return 1
if __name__ == '__main__':
sys.exit(main())