Why The two audit skills carried 1,724 lines of byte-identical duplication: the ADR-0020 boundary resolver (1,061), vale-wrap.sh (526), the Vale style rules (44) and the Contributing-files parser (93). Nothing shared them — they were held in sync by a 413-line pre-push gate and its 797-line test suite. Sync-by-gate had already failed once: at484357athe two parser copies drifted into different spellings of the bullet loop while a docstring asserted they were identical. That drift was behaviour-neutral and was re-unified by hand at598a7c3, so the copies were identical at merge time — but nothing had caught it, and the next drift need not be neutral. Implementation Notes Self-containment binds BETWEEN skills, not within one. The agentskills.io spec forbids reaching across skill directories, which is why two separate skills needed embedded copies; two files inside ONE skill may source a third. That is the whole reason the merge removes duplication rather than relocating it. The union of both bodies measured 1,532 words against BODY_MAX_WORDS=900, and only 211 of those words were shared, so SKILL.md is a dispatch body. Step 0 resolves the flow from the target path before any validation, and its table mirrors validate.sh's detection exactly: a directory holding SKILL.md or a SKILL.md file (skill); a *.agent.md, or a .md directly under an agents/ directory (agent); anything else stops without running a validator. Steps 1-3 live in references/skill-flow.md and references/agent-flow.md, and gotchas that apply to one flow live in that flow's file, since it is loaded on every invocation anyway. If validate.sh reports on the other artifact type, the body restarts at Step 0. Named factory-audit rather than forge-audit because forge is a live skill, and a family prefix that matches a live sibling reads as ownership rather than membership. The description carries one arrow per boundary target, because ADR-0020 resolves only the first target after an arrow. It drops the quoted "audit this skill"-style phrases, which restated "audited" in a second register (ADR-0020's duplicate-register rule). 241 characters, Gotchas 16% of the body: no size SUGGESTIONs. The boundary resolver stays embedded in two files rather than imported: a cache-installed plugin cannot read outside its own directory, and the repo-root hook resolves via .pre-commit-hooks.yaml where entry[0] is the only token pre-commit rewrites, so no single file is reachable by both. tests/test-adr0020-contract.sh hashes both copies for byte-identity, and asserts validate.sh sources the resolver and that no third copy exists. The entry scripts classify the target from its resolved parent directory, so a bare agent filename typed inside agents/ works; resolve SCRIPT_DIR CDPATH-safely; and exit 2 when a lib-*.sh is missing, rather than dying with exit 1, the tier the flows relay as real findings. The provenance run functions stash their findings code in KYBERFORGE_PROV_RC and return 0, so validate-provenance.sh calls them UNTESTED. Testing a function's status (`f || RC=$?`) disables errexit for its entire body, and no subshell or `set -e` inside can re-arm it once the call sits in a condition context (measured, both spellings). Their error paths use `exit`, which is unaffected either way; this keeps errexit armed for anything added later. Case 0's readability guard reads the file instead of asking `[[ -r ]]`. `-r` is access(2), which answers yes for uid 0 even on a mode-000 file, and this repo's dev environment is root -- so the guard could never fire where it exists to fire. A read attempt is also the stricter question, catching EIO. This is the reasoning scripts/check-vale-style-sync.sh carried before this commit deleted it; the hazard did not go with it. All three entry scripts are CDPATH-safe, vale-wrap.sh included: both of its cd sites are cleared, the --config resolution and the directory-mirror walk, where an exported CDPATH would otherwise print a decoy path into the -print0 stream and build the mirror from the decoy's files. The two remaining bare cd calls take absolute paths, which CDPATH is never consulted for. Impact BREAKING: skill-audit and agent-audit no longer exist as invocable skills. kyberforge goes to 2.0.0 (catalog 0.4.7). Check logic is unchanged: differential runs of the old and new validators across every skill and agent produced byte-identical stdout, stderr and exit codes, and the reconstructed Python payloads differ only in comments and the references/field-inventory.md -> agent-field-inventory.md rename. One doctrine governs the tiers: exit 0 is audited and clean, exit 1 is audited with findings OR a target present but unreadable, exit 2 is that nothing was audited at all. Edge paths DID change, deliberately (full table in ADR-0025): - a missing target exits 2 (never ran), not 1, under its own "does not exist" message; detection is by path shape, so a shape-matching path that is simply absent used to reach the validator and come back as a FAIL against a file that never existed; - an unshaped target exits 2 under the generic "matches neither" message, and a directory with no SKILL.md under a third, distinct one -- three exit-2 messages, not one; - a dangling symlink or a symlink loop stays exit 1: it is present but broken, which is a finding about the artifact rather than a usage error; - a SKILL.md file path is audited as its skill directory instead of refused; - a .md agent outside an agents/ directory is refused rather than audited; - a missing script library, a missing python3, a missing PyYAML, and no argument at all each exit 2. validate-provenance.sh already exited 2 for the last two; validate.sh now matches it. .pre-commit-hooks.yaml is a published contract consumed by external repos. Both hook IDs and both files: regexes are unchanged; only entry: and description: moved. scripts/check-vale-style-sync.sh (413), scripts/sync-vale-styles.sh (21), tests/test-check-vale-style-sync.sh (797) and agent-audit/scripts/README.md (47) are deleted. The checker made 17 assertions: 6 compared the two Vale copies and are moot; 10 are rehomed into tests/test-vale-wrap.sh (case 0, cases 28-31, and the suite's Vale-absent skip); and the cross-manifest files: agreement check, which selected hooks by entry: and so could not survive both hooks sharing one, is ported as case 33 pairing hooks by id:. Cases 28, 30 and 33 carry mutation self-tests; narrowing the local skill prefilter to 6 of 38 SKILL.md files now fails the suite. Skills go 39 to 38. Pre-push goes 9 repo-authored hooks to 8. ADR: 0025 BREAKING-CHANGE: the skill-audit and agent-audit skills are removed. Both flows are served by factory-audit, which auto-detects whether it was handed a skill directory or an agent file. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YR2CjVumUbEGWcMikcoXBD
168 lines
7.4 KiB
Bash
Executable File
168 lines
7.4 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
# Run factory-audit's validate.sh over every REAL plugin-scope agent file in
|
|
# this repo (plugins/*/.apm/agents/*.agent.md).
|
|
#
|
|
# Since ADR-0025 merged skill-audit and agent-audit into factory-audit, that
|
|
# validate.sh is a single auto-detecting entry point: it reads the target and
|
|
# sources the skill-mode or agent-mode check library itself. The invocation
|
|
# below is therefore unchanged apart from the path — no mode flag is passed,
|
|
# and passing one would be wrong.
|
|
#
|
|
# Why this exists: validate.sh was previously exercised only by
|
|
# scripts/check-scope-walkup-sync.sh, and only against synthetic fixtures built
|
|
# in mktemp trees. It had never once run against the four agent files it
|
|
# actually governs. That is how ADR-0016 could be amended to bless a
|
|
# `disallowedTools` frontmatter field while validate.sh's allowlist still
|
|
# rejected it -- the spec and its enforcer disagreed, every gate stayed green,
|
|
# and the contradiction only surfaced when someone ran the validator by hand.
|
|
#
|
|
# A validator that checks fixtures but never artifacts is the same
|
|
# green-because-nothing-was-checked shape as a suite that runs on an empty file
|
|
# set. This closes it: the artifacts are the input.
|
|
#
|
|
# Run from repo root, or pass REPO_ROOT as the first argument (tests do).
|
|
# Needs no network.
|
|
|
|
REPO_ROOT="${1:-$(git rev-parse --show-toplevel 2>/dev/null || pwd)}"
|
|
|
|
# A nonexistent REPO_ROOT must fail loudly rather than fall through to the
|
|
# zero-files floor below with a confusing message -- a typo'd or stale path is a
|
|
# different problem from a repo that genuinely has no agents, and the fix
|
|
# differs too.
|
|
if [[ ! -d "$REPO_ROOT" ]]; then
|
|
echo "APM agent validation failed: REPO_ROOT '$REPO_ROOT' is not a directory." >&2
|
|
exit 1
|
|
fi
|
|
REPO_ROOT="$(cd "$REPO_ROOT" && pwd)"
|
|
|
|
VALIDATE="$REPO_ROOT/plugins/kyberforge/.apm/skills/factory-audit/scripts/validate.sh"
|
|
|
|
# The validator's own absence is a hard failure, never a skip. If validate.sh
|
|
# moves or is deleted, every assertion below evaporates and the hook would
|
|
# otherwise exit 0 having validated nothing -- indistinguishable, from
|
|
# pre-commit's silent-on-pass output, from a run where all four agents passed.
|
|
if [[ ! -f "$VALIDATE" ]]; then
|
|
echo "APM agent validation failed: validator not found at $VALIDATE — this script's path has gone stale, so no agent file was checked. Update it to wherever factory-audit's validate.sh now lives." >&2
|
|
exit 1
|
|
fi
|
|
|
|
# validate.sh is a bash wrapper around a heredoc'd python3 program. Without
|
|
# python3 it dies with a bare "command not found" per file and no pointer, which
|
|
# reads like a validation failure rather than a missing dependency. Fail closed,
|
|
# but say which it is.
|
|
if ! command -v python3 >/dev/null 2>&1; then
|
|
echo "APM agent validation failed: python3 not found on PATH — factory-audit's validate.sh is a python3 program and cannot run. Install python3; this gate does not degrade to a pass." >&2
|
|
exit 1
|
|
fi
|
|
|
|
# --- Discover the agent files ---
|
|
# `-not -path` mirrors tests/run-bats.sh: worktrees under .claude/ are other
|
|
# checkouts of this same repo, not additional content.
|
|
AGENT_FILES=()
|
|
while IFS= read -r f; do
|
|
[[ -n "$f" ]] && AGENT_FILES+=("$f")
|
|
done < <(
|
|
find "$REPO_ROOT/plugins" -type f -name '*.agent.md' \
|
|
-path '*/.apm/agents/*' \
|
|
-not -path '*/.claude/worktrees/*' \
|
|
2>/dev/null | sort
|
|
)
|
|
|
|
# --- Derive the EXPECTED set from the index, not from a hardcoded count ---
|
|
# Same reasoning as tests/run-bats.sh: a magic number goes stale the moment a
|
|
# plugin gains or loses an agent, and slack in a floor is exactly where a
|
|
# silently-deleted file hides. `git ls-files` needs no maintenance -- a newly
|
|
# `git add`ed agent file joins the expectation immediately.
|
|
#
|
|
# Direction matters, and it is the same direction run-bats.sh uses: every
|
|
# TRACKED file must have been discovered, but a discovered file need not be
|
|
# tracked. An untracked new agent file is ordinary work in progress (and is
|
|
# still validated below), while a file that vanished from the worktree without
|
|
# leaving the index is an accident and fails here. A deliberate `git rm` leaves
|
|
# the index, so intentional removal passes.
|
|
#
|
|
# The exact-equality check on --show-toplevel keeps this off the mktemp fixture
|
|
# trees in tests/test-check-apm-agents-valid.sh, which resolve no worktree. That
|
|
# degradation is announced rather than silent, and the zero-file floor below is
|
|
# unconditional regardless.
|
|
EXPECTED_FILES=()
|
|
DERIVED=false
|
|
GIT_TOPLEVEL="$(git -C "$REPO_ROOT" rev-parse --show-toplevel 2>/dev/null || true)"
|
|
if [[ -n "$GIT_TOPLEVEL" && "$GIT_TOPLEVEL" == "$REPO_ROOT" ]]; then
|
|
DERIVED=true
|
|
while IFS= read -r f; do
|
|
[[ -n "$f" ]] && EXPECTED_FILES+=("$REPO_ROOT/$f")
|
|
done < <(
|
|
git -C "$REPO_ROOT" ls-files -- 'plugins/*/.apm/agents/*.agent.md' \
|
|
| grep -Ev '(^|/)\.claude/worktrees/' \
|
|
| sort || true
|
|
)
|
|
else
|
|
echo "Note: $REPO_ROOT is not a git worktree root, so the expected agent file set could not be derived from the index — only the zero-file floor below applies" >&2
|
|
fi
|
|
|
|
if [[ "$DERIVED" == true && ${#EXPECTED_FILES[@]} -gt 0 ]]; then
|
|
MISSING=()
|
|
for expected in ${EXPECTED_FILES[@]+"${EXPECTED_FILES[@]}"}; do
|
|
found=false
|
|
for actual in ${AGENT_FILES[@]+"${AGENT_FILES[@]}"}; do
|
|
if [[ "$actual" == "$expected" ]]; then
|
|
found=true
|
|
break
|
|
fi
|
|
done
|
|
[[ "$found" == true ]] || MISSING+=("${expected#"$REPO_ROOT"/}")
|
|
done
|
|
if [[ ${#MISSING[@]} -gt 0 ]]; then
|
|
echo "APM agent validation failed: ${#MISSING[@]} of ${#EXPECTED_FILES[@]} tracked agent file(s) were not discovered under $REPO_ROOT — they were deleted without being removed from the index, or this script's search path no longer reaches them:" >&2
|
|
for m in ${MISSING[@]+"${MISSING[@]}"}; do
|
|
echo " $m" >&2
|
|
done
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
# Unconditional floor, separate from the derived check above: a tree with
|
|
# nothing tracked (a tarball export, a fresh scaffold, a moved plugins/ root)
|
|
# still must not validate an empty set and call it green. Zero files is an
|
|
# error, not a pass -- that is the entire defect this script was written to
|
|
# close, one level up.
|
|
if [[ ${#AGENT_FILES[@]} -eq 0 ]]; then
|
|
echo "APM agent validation failed: found 0 plugin-scope agent file(s) under $REPO_ROOT/plugins — the search path is wrong or every .apm/agents/ directory has been emptied. Zero files is never a pass." >&2
|
|
exit 1
|
|
fi
|
|
|
|
# --- Validate ---
|
|
FAIL=0
|
|
FAILED_FILES=()
|
|
for f in ${AGENT_FILES[@]+"${AGENT_FILES[@]}"}; do
|
|
rel="${f#"$REPO_ROOT"/}"
|
|
# validate.sh prints its FAIL lines on stdout and its own errors on stderr;
|
|
# both are captured and replayed under the filename so the reason travels with
|
|
# the file that caused it. pre-commit shows a failing hook's output verbatim,
|
|
# so this is what a developer reads.
|
|
out=""
|
|
rc=0
|
|
out="$(bash "$VALIDATE" "$f" 2>&1)" || rc=$?
|
|
if [[ "$rc" -ne 0 ]]; then
|
|
FAIL=1
|
|
FAILED_FILES+=("$rel")
|
|
echo "FAIL: $rel (validate.sh exit $rc)" >&2
|
|
if [[ -n "$out" ]]; then
|
|
printf '%s\n' "$out" | sed 's/^/ /' >&2
|
|
else
|
|
echo " (validate.sh produced no output — see its exit code above; 2 means script error, e.g. a missing references/agent-field-inventory.md or lib-*.sh)" >&2
|
|
fi
|
|
fi
|
|
done
|
|
|
|
if [[ "$FAIL" -ne 0 ]]; then
|
|
echo "" >&2
|
|
echo "APM agent validation failed: ${#FAILED_FILES[@]} of ${#AGENT_FILES[@]} agent file(s) did not pass factory-audit's validate.sh." >&2
|
|
exit 1
|
|
fi
|
|
|
|
echo "APM agent validation passed: ${#AGENT_FILES[@]} plugin-scope agent file(s) validated against factory-audit's validate.sh."
|