fix(skill-frontmatter): check every file, scope checks to frontmatter
The hook is `entry: bash` with `args: ['-c', <script>]`. pre-commit appends filenames after the script string, so the first becomes `$0` and never enters `"$@"` — on a single-file commit, the common case, the loop body never ran and the hook reported Passed having measured nothing. ADR-0022 leans on this hook as the enforcement for a mandatory `metadata.version`, so the vacuous green was the whole gate. Implementation notes: - An arg0 placeholder absorbs `$0` so every filename lands in `"$@"`. - Checks now run against the YAML frontmatter block only, extracted with awk. The old `grep -A10 "^metadata:"` matched a `metadata:` inside a body code fence, spanned past the block into a following `source:` entry's `version:`, accepted any indentation, and missed a `version:` more than ten lines in. An unreadable frontmatter block is now an error, never a pass. - The value is asserted against three-part semver. `write-docs` carried "1.0" through the entire ADR-0022 retrofit undetected, which a presence-only check cannot catch. Impact: `tests/test-skill-frontmatter.sh` is the first test this hook has ever had. It drives the real `entry`/`args` composition read out of the config rather than a copy of the script, which is the only shape that catches the arg0 bug; against the pre-fix hook it scores 7/20. gates.md described the hook wrongly in both directions and is rewritten, with a carve-out explaining why this one stays a shell parser next to the "python3 and PyYAML are hard requirements" reasoning that argues otherwise. Refs: #127 ADR: 0022 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EeH8SCbcrCAQrtymkNuhKP
This commit is contained in:
@@ -254,27 +254,79 @@ repos:
|
||||
entry: bash
|
||||
language: system
|
||||
files: '^plugins/[^/]+/\.apm/skills/[^/]+/SKILL\.md$'
|
||||
# Pinned by tests/test-skill-frontmatter.sh, which drives this exact
|
||||
# `bash -c <script> <arg0> <files...>` call shape rather than a copy of
|
||||
# the script -- the bug below was invisible to any test that did not.
|
||||
args:
|
||||
- -c
|
||||
- |
|
||||
# Every check reads the FRONTMATTER only, never the whole file. A
|
||||
# `metadata:` / `name:` / `description:` line inside a body code
|
||||
# fence is documentation (skill-author quotes exactly such a block)
|
||||
# and used to satisfy these greps.
|
||||
for f in "$@"; do
|
||||
if [[ -f "$f" ]]; then
|
||||
missing=""
|
||||
if ! grep -q "^name:" "$f"; then
|
||||
missing="${missing}name: "
|
||||
fi
|
||||
if ! grep -q "^description:" "$f"; then
|
||||
missing="${missing}description: "
|
||||
fi
|
||||
if ! grep -A10 "^metadata:" "$f" | grep -q " version:"; then
|
||||
missing="${missing}metadata.version "
|
||||
fi
|
||||
if [[ -n "$missing" ]]; then
|
||||
echo "ERROR: $f is missing required frontmatter fields (${missing})"
|
||||
exit 1
|
||||
fi
|
||||
[[ -f "$f" ]] || continue
|
||||
|
||||
fm="$(awk '
|
||||
{ sub(/\r$/, "") }
|
||||
NR == 1 { sub(/^\357\273\277/, "") }
|
||||
!opened && /^[[:blank:]]*$/ { next }
|
||||
!opened {
|
||||
if ($0 ~ /^---[[:blank:]]*$/) { opened = 1; next }
|
||||
exit
|
||||
}
|
||||
/^---[[:blank:]]*$/ { closed = 1; exit }
|
||||
{ print }
|
||||
END { if (!opened || !closed) exit 3 }
|
||||
' "$f")" || {
|
||||
echo "ERROR: $f has no closing YAML frontmatter block (expected --- ... --- at the top of the file)"
|
||||
exit 1
|
||||
}
|
||||
|
||||
missing=""
|
||||
printf '%s\n' "$fm" | grep -q "^name:" || missing="${missing}name: "
|
||||
printf '%s\n' "$fm" | grep -q "^description:" || missing="${missing}description: "
|
||||
|
||||
# Scoped to the `metadata:` block and stopped at the next
|
||||
# top-level key, so a `version:` under a following `source:` list
|
||||
# cannot stand in for it; the `^ version:` anchor is exact, so a
|
||||
# deeper-nested ` version:` cannot either. No line budget, so a
|
||||
# long `metadata:` block does not hide the key.
|
||||
ver="$(printf '%s\n' "$fm" | awk '
|
||||
/^metadata:/ { inm = 1; next }
|
||||
inm && /^[A-Za-z]/ { exit }
|
||||
inm && /^ version:/ {
|
||||
v = $0
|
||||
sub(/^ version:[[:blank:]]*/, "", v)
|
||||
sub(/[[:blank:]]+#.*$/, "", v)
|
||||
sub(/[[:blank:]]+$/, "", v)
|
||||
print "found:" v
|
||||
exit
|
||||
}
|
||||
')"
|
||||
[[ -n "$ver" ]] || missing="${missing}metadata.version "
|
||||
|
||||
if [[ -n "$missing" ]]; then
|
||||
echo "ERROR: $f is missing required frontmatter fields (${missing})"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
raw="${ver#found:}"
|
||||
v="$raw"
|
||||
case "$v" in
|
||||
\"*\") v="${v#\"}"; v="${v%\"}" ;;
|
||||
\'*\') v="${v#\'}"; v="${v%\'}" ;;
|
||||
esac
|
||||
if [[ ! "$v" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
|
||||
echo "ERROR: $f has a malformed frontmatter metadata.version (${raw:-<empty>}) -- expected a three-part semver, e.g. \"1.0.0\""
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
# arg0 for `bash -c`. WITHOUT it pre-commit's first filename lands in
|
||||
# $0 and is dropped from "$@" -- so a single-file commit, the normal
|
||||
# case, ran the loop zero times and reported Passed having checked
|
||||
# nothing. Do not remove; tests/test-skill-frontmatter.sh pins it.
|
||||
- skill-frontmatter
|
||||
|
||||
- id: skill-size-check
|
||||
stages: ['pre-commit']
|
||||
|
||||
Reference in New Issue
Block a user