orphan-ref-validator
Purpose
Scans structured artifacts (specs, ADRs, eval fixtures, plugin manifests, skill descriptions) for references to entities that do not exist in the working tree:
- Skill names that no longer have a
.claude/skills/<name>/ directory. Emitted as Finding(kind="skill_name", severity="critical").
- Script paths under
build/scripts/, scripts/validation/, scripts/, or tests/ that are not present on disk. Emitted as Finding(kind="script_path", severity="critical").
- Rule and instruction paths under
.claude/rules/, .github/instructions/, or src/copilot-cli/instructions/ that are not present on disk. Emitted as Finding(kind="rule_path" | "instruction_path", severity="critical").
- Retired-surface paths: a citation of the slash-command directory that ADR-064 retired, in live guidance documents only (skills, rules, instruction mirrors; not source code). Emitted as
Finding(kind="retired_path", severity="critical"). A line that says Migrated from or names ADR-064 records the retirement and is not flagged. Specs, ADRs, and tests are dated records and are not checked.
Emits findings per the ADR-056 envelope and a final verdict line. Exit code follows ADR-035: VERDICT: PASS or VERDICT: WARN exits 0; VERDICT: CRITICAL_FAIL exits 1; usage/configuration or incomplete scan exits 2; external failures exit 3; permission failures exit 4.
The skill ships with vendored installs. Missing targets are errors by default. Use --allow-missing-targets only when the caller intentionally runs in a partial vendored tree, and use --allow-empty-scan only when zero scanned files is the declared scope.
Triggers
| Trigger |
Effect |
scan for orphan refs |
Run with default targets |
validate orphan references |
Run on a specific path |
check skill catalog drift |
Run with default targets |
build mandatory exit gate |
Invoked by the build lifecycle command |
Path conventions
Commands below run through the plugin-root locator. It tries CLAUDE_PLUGIN_ROOT, then COPILOT_PLUGIN_ROOT, then .claude, and uses the first root that holds lib/ptk_run.py. They run unchanged from a repo checkout, a Claude plugin install, or the Copilot CLI mirror. The /build gate invokes this skill through whichever invocation form its platform provides; each platform mirror runs its own copy of scan.py.
Inputs
Run scan.py through the plugin-root locator shown under Examples. Write every option inside the locator, before its closing ; fi:
scan.py
[--targets PATH ...]
[--include-adrs]
[--include-skill-descriptions]
[--allow-missing-targets]
[--allow-empty-scan]
[--baseline FILE]
[--repo-root PATH]
[--output {json,human}]
[--log-level {DEBUG,INFO,WARNING,ERROR}]
| Flag |
Purpose |
Default |
--targets |
Files or directories to scan |
tracked .md, .json, .yaml, and .yml files under .project-toolkit/specs/, .claude/rules/, .github/instructions/, src/copilot-cli/instructions/, and tests/, plus plugin manifest JSON files |
--include-adrs |
Add .project-toolkit/architecture/ and docs/ to defaults (opt-in) |
off |
--include-skill-descriptions |
Add .claude/skills/*/SKILL.md to defaults (opt-in until preexisting drift is cleaned) |
off |
--allow-missing-targets |
Treat missing targets as optional vendored-install paths |
off |
--allow-empty-scan |
Permit PASS after scanning zero files |
off |
--baseline |
Path to a file of known pre-existing finding keys (target_file:line:kind:referenced_entity). Matching findings are marked suppressed and do not fail the scan; new findings still exit 1. Accepts a JSON list of keys, a saved scan envelope (Data.findings), or one key per line (# comments allowed). |
none |
--repo-root |
Repository root. Walks up from CWD for the nearest .git directory; falls back to CWD. Validates that user-supplied paths exist and are directories (returns ADR-035 exit 2 otherwise). |
walked from CWD |
--output |
json (ADR-056 envelope) or human (compact summary) |
json |
--log-level |
Python logging level |
WARNING |
Outputs
json mode (default):
{
"Success": true,
"Data": {
"findings": [
{
"kind": "skill_name",
"severity": "critical",
"target_file": "docs/old.md",
"line": 12,
"referenced_entity": "doc-sync",
"recommendation": "Skill `doc-sync` not present at .claude/skills/. Update reference, restore the skill, or remove the mention."
}
],
"verdict": "CRITICAL_FAIL",
"counts": {
"files_scanned": 142,
"files_skipped": 0,
"refs_checked": 318,
"findings_total": 1,
"findings_suppressed": 0,
"directive_suppressed": 0,
"incomplete_scans": 0
},
"directive_suppressed": [],
"incomplete_scans": []
},
"Error": null,
"Metadata": {"Script": "scan.py", "Version": "1.0.0", "Timestamp": "..."}
}
VERDICT: CRITICAL_FAIL
human mode:
orphan-ref-validator 1.0.0
files_scanned: 142
files_skipped: 0
refs_checked: 318
findings: 1
suppressed: 0
directive_suppressed: 0
incomplete_scans: 0
# citation-freshness: ignore -- illustrative finding matching the target_file value in the JSON example above; docs/old.md is a deliberately fake path, not a real citation
[critical] docs/old.md:12 skill_name `doc-sync` -- Skill `doc-sync` not present at .claude/skills/. ...
VERDICT: CRITICAL_FAIL
Process
Phase 1: Resolve Targets
- Read
--targets if supplied, else derive default targets from git ls-files.
- Append
OPT_IN_ADR_TARGETS if --include-adrs is set.
- Append
OPT_IN_SKILL_TARGETS if --include-skill-descriptions is set.
- Expand glob patterns containing
* or ? against the repository root.
- Mark missing targets, outside-repo targets, and empty glob matches as incomplete scans unless
--allow-missing-targets is set.
Phase 2: Walk Files
- For directory targets, recurse and yield files whose suffix matches
.md, .json, .yaml, .yml.
- Exclude paths whose any segment is in
EXCLUDE_DIR_NAMES (__pycache__, .git, node_modules, worktrees, cache, templates). The first five are vendor/VCS directories; templates is added because placeholder files legitimately cite entities that do not exist. Skill references/ directories are walked: handoff prose and relationship tables live there. A reference doc that cites an absent entity on purpose takes a line-scope orphan-ref-ignore directive.
- Exclude files matching the secret denylist.
- Mark unreadable files, broken symlinks, outside-repo symlinks, symlink loops, stat failures, walk failures, and files larger than 5 MB as incomplete scans.
Phase 3: Detect References
- Apply skill, script, rule, and instruction path extractors line by line.
- Filter known-kebab tokens (model IDs, frontmatter fields, Action names, bot ids, git hooks, vocabulary terms).
- Skip script refs on example-placeholder lines, such as
Example: and e.g. snippets.
- Honor the ignore directives described below.
Ignore directives
| Directive |
Scope |
Where it must appear |
Effect |
<!-- orphan-ref-ignore-file --> |
Whole file |
Anywhere in the first 50 lines of the file |
Skip the file entirely; emit no findings and report suppressed references under directive_suppressed. |
<!-- orphan-ref-ignore --> |
Single line |
Anywhere on the same line as a reference |
Skip every reference on that line and report it under directive_suppressed. |
Place file-scope directives below the YAML frontmatter (if any) and well within the first 50-line window. Adding a directive at line 51 or later silently fails because the scanner only reads text.splitlines()[:50].
Script references on example-placeholder lines are ignored automatically. This covers lines that start with Example:, e.g., or For example, plus prose examples that document an intentionally absent helper.
A fenced block whose last prose line before the fence says "citations, not local paths" is a citation block. The scanner skips its references and reports them under directive_suppressed with reason citation block. Rule templates use this for historical evidence lists.
Backticked skill routing-role values (front-door, explicit-only, conditional-adjunct, lifecycle, nested-helper, deprecated) are category words, not skill names. A line like "a front-door skill" produces no skill_name finding. An explicit route such as "the lifecycle skill" still does when no such skill exists. A script cited by a skill-relative path, followed by "in the" and a backticked skill name, also resolves against that named skill's directory.
Specs stay in scope. A spec that records files a later PR deleted carries a line-scope directive on each such line, so the rest of the spec keeps its orphan checks.
Use file-scope on M1-deletion specs and proposed-entity catalogs whose every reference is intentional history. Use line-scope for one-off references that document an absence (for example, "the script scripts/validation/manifest_counts.py was not created").
Phase 4: Resolve and Verdict
- For each surviving reference, check the source of truth (skill set, file presence).
- Build the ADR-056 envelope with findings, counts, and verdict.
- Verdict is
CRITICAL_FAIL if any finding has severity critical, else WARN if findings exist, else PASS.
- Print envelope and
VERDICT: line. Exit 1 on CRITICAL_FAIL, 2 on usage/configuration or incomplete-scan error, 3 on external error, 4 on permission error, 0 otherwise.
Verification
Success criteria for the skill:
- [ ] In a repository checkout,
uv run pytest tests/skills/orphan-ref-validator/ -q reports all tests passed. Plugin installs do not ship tests.
- [ ]
( for d in "${CLAUDE_PLUGIN_ROOT}" "${COPILOT_PLUGIN_ROOT}" .claude; do if [ -n "$d" ] && [ -f "$d/lib/ptk_run.py" ]; then exec python3 "$d/lib/ptk_run.py" skills/orphan-ref-validator/scripts/scan.py --help; fi; done; echo "ptk_run: no plugin root holds lib/ptk_run.py" >&2; exit 2 ) exits 0 with the documented argparse output.
- [ ]
( for d in "${CLAUDE_PLUGIN_ROOT}" "${COPILOT_PLUGIN_ROOT}" .claude; do if [ -n "$d" ] && [ -f "$d/lib/ptk_run.py" ]; then exec python3 "$d/lib/ptk_run.py" skills/orphan-ref-validator/scripts/scan.py --targets missing.md; fi; done; echo "ptk_run: no plugin root holds lib/ptk_run.py" >&2; exit 2 ) exits 2 with VERDICT: ERROR.
- [ ]
( for d in "${CLAUDE_PLUGIN_ROOT}" "${COPILOT_PLUGIN_ROOT}" .claude; do if [ -n "$d" ] && [ -f "$d/lib/ptk_run.py" ]; then exec python3 "$d/lib/ptk_run.py" skills/orphan-ref-validator/scripts/scan.py; fi; done; echo "ptk_run: no plugin root holds lib/ptk_run.py" >&2; exit 2 ) from the repo root exits 0 with VERDICT: PASS on default tracked text targets.
- [ ] The
build skill Mandatory Exit Gates list orphan-ref-validator as gate 4.
Scripts