From e14fca123d3a707a57e6aee44bbcadb2ccdba79b Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Sun, 6 Dec 2026 10:07:49 +0000 Subject: [PATCH] fix(gates): re-run the full root audit after the unlanded-plugin waiver Why: on apm 0.39.3 a failing ref-consistency stops the root run after three checks, so while the unlanded-plugin waiver applied, the other twelve root checks (content-integrity, hook-approval-pins, local-bundle-hooks, exec-status-current, deployed-files-present, ...) never ran. Implementation notes: once the waiver's conditions hold, the hook copies the working tree (minus .git and .venv) into a mktemp dir. There it drops only the waived plugins' dependencies.apm items, checks the diff is deletions only, and runs git init with the same origin. It then runs apm audit --ci in the copy, and passes only on exit 0 with "All 15 check(s) passed". A copy was chosen over a trap-guarded in-place swap of apm.yml: it never writes the working tree, so the tree stays byte-identical even after SIGKILL. The plugin name must now start alphanumeric and contain no "..". Impact: tests cover a failing, short, or non-zero re-run; a one-of-three failure on another row; a duplicated details header; and dotted names. Each guard was mutation-checked. On this branch the re-run reports all 15 checks passing. Refs: #179 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi --- docs/spec/gates.md | 23 ++++- scripts/apm-audit-ci.sh | 137 +++++++++++++++++++++++++++++- tests/test-apm-audit-ci-waiver.sh | 104 +++++++++++++++++++++-- 3 files changed, 253 insertions(+), 11 deletions(-) diff --git a/docs/spec/gates.md b/docs/spec/gates.md index 97f84ac..e6c8dc1 100644 --- a/docs/spec/gates.md +++ b/docs/spec/gates.md @@ -1245,9 +1245,26 @@ the root run to three checks (holocron#179), so the hook waives it only when the that `not found in lockfile` form for an identity equal to root `apm.yml`'s own git+path entry for `plugins/` (derived by `check-executables-allow-sync`'s rule). Each `plugins//apm.yml` must also exist in the working tree and be absent from the **local** `origin/main`, which must -resolve; nothing is fetched. Any other output fails closed. The waiver lapses for a plugin by -itself: once it is on `origin/main`, the follow-up PR that records it in the lock is required. -`tests/test-apm-audit-ci-waiver.sh` covers it. +resolve; nothing is fetched. A plugin name must start with a letter or digit and may not contain +`..`. Any other output fails closed. The waiver lapses for a plugin by itself: once it is on +`origin/main`, the follow-up PR that records it in the lock is required. + +**The waiver re-runs the full root check set before it passes.** The cut-short run never reaches the +other twelve root checks (`content-integrity`, `hook-approval-pins`, `local-bundle-hooks`, +`exec-status-current`, `deployed-files-present` and the rest), so the three-check result alone +would let a defect in any of them through. Once the conditions above hold, the hook copies the working +tree (minus `.git` and `.venv`) into a `mktemp -d` directory with `tar`. In the copy it drops only the +`dependencies:` > `apm:` items whose `path:` names a waived plugin, checks that the reduced +`apm.yml` differs from the original by those deletions alone, and runs `git init` with the same +`origin` URL, the only git state apm reads. It then runs `apm audit --ci` there. The push passes only +if that run exits 0 and prints `All 15 check(s) passed`. On this branch (holocron#179, apm 0.39.3) it +does, and the hook prints `root re-run without the unlanded plugins: All 15 check(s) passed`. A copy +was chosen over swapping `apm.yml` in place under a `trap`: the copy never writes the working tree, so +the tree stays byte-identical even after `SIGKILL`, and a concurrent apm never sees a reduced +manifest. `.git` is not copied because in a linked worktree it points into the shared gitdir. `.venv` +is not copied because apm runs from the real one via `uv run --project`. The scratch directory is +removed on exit, including on `INT`, `TERM` and `HUP`. The script header records the full reasoning. +`tests/test-apm-audit-ci-waiver.sh` covers the waiver, the re-run, and each of its guards. **Dropping `--ci` for package directories was considered and rejected.** It is the smaller change and it is wrong. Verified on apm 0.28.0, and again on 0.39.3 (holocron#272), against a scratch diff --git a/scripts/apm-audit-ci.sh b/scripts/apm-audit-ci.sh index 9e3787f..ecde3a3 100755 --- a/scripts/apm-audit-ci.sh +++ b/scripts/apm-audit-ci.sh @@ -82,9 +82,48 @@ # 3. plugins//apm.yml exists in the working tree # 4. plugins//apm.yml is absent from the LOCAL origin/main ref -- no # fetch, the hook stays offline -- and origin/main must resolve +# 5. a re-run of `apm audit --ci` against the root with those plugins' +# `dependencies.apm` entries removed passes with "All 15 check(s) passed" # Anything else, including output in a shape not recognised here, fails the # push. The waiver lapses for a plugin by itself: once it is on origin/main, # condition 4 is false and the follow-up lock PR has to record it. +# +# WHY THE RE-RUN (CONDITION 5) +# +# The cut-short run is exactly the problem: while ref-consistency fails, apm +# never runs the other twelve root checks (content-integrity, +# hook-approval-pins, local-bundle-hooks, exec-status-current, +# deployed-files-present, ...), so waiving the three-check run alone would let +# a defect in any of them through. Removing the unlanded plugins' entries is +# the one change that makes ref-consistency pass, and apm then runs the full +# set. Verified on apm 0.39.3 (#179): this branch's root, so reduced, prints +# "All 15 check(s) passed". The count is asserted, not just the exit code, so +# an apm that runs fewer checks, or adds one, fails closed until re-verified. +# +# The re-run happens in a SCRATCH COPY, never in place. The working tree minus +# .git and .venv is copied with tar into a mktemp -d dir, apm.yml is reduced +# there, and apm runs there. An in-place swap of apm.yml guarded by a trap was +# the alternative and is weaker: no trap survives SIGKILL or a power cut, and +# for the swap's duration a concurrent apm (the SessionStart hook, another +# shell) would read the reduced manifest. The copy never writes the working +# tree at all, so it is byte-identical afterwards whatever happens; at worst a +# killed run leaks a temp dir. What the copy holds, and why: +# * everything apm reads for the root audit -- apm.yml, apm.lock.yaml, +# apm_modules/ (the cache-only replay's source), .claude/ and every other +# deployed tree (deployed-files-present, content-integrity, drift), and +# the plugins/ sources the marketplace and local entries name. Copying the +# whole tree rather than a hand-picked list means a check that starts +# reading another path cannot silently see an empty one. +# * NOT .git: from a linked worktree it is a pointer file into the shared +# gitdir, and a git command run in the copy could then refresh that +# worktree's real index. A fresh `git init` with the same origin URL +# stands in for it -- apm reads only the origin remote, for org-policy +# discovery, and the copy prints the same policy line the real root does. +# * NOT .venv: apm runs through `uv run --project `, so the +# environment is the real one; .venv is no apm input and most of the size. +# Only list items under `dependencies:` > `apm:` whose `path:` names a waived +# plugin are removed (comments and every other line kept), and the result is +# checked to differ from the original by deletions only, each waived entry gone. set -uo pipefail @@ -105,7 +144,10 @@ readonly SOLE_FAILURE='1 of 1 check(s) failed' readonly REF_SOLE_FAILURE='1 of 3 check(s) failed' readonly REF_FAILED_ROW='^\| \[x\] +\| ref-consistency +\|' readonly REF_DETAILS_HEADER=' ref-consistency details:' -readonly REF_DETAIL='^ - (.+)/plugins/([A-Za-z0-9._-]+): not found in lockfile$' +# The name must start alphanumeric, which rules out `.` and `..`; `..` +# anywhere else is rejected separately in ref_waivable. +readonly REF_DETAIL='^ - (.+)/plugins/([A-Za-z0-9][A-Za-z0-9._-]*): not found in lockfile$' +readonly ROOT_FULL_PASS='All 15 check(s) passed' # dep_identity -- the identity root apm.yml's git+path entry # for plugins/ yields: ///plugins/. The URL rule @@ -172,6 +214,10 @@ ref_waivable() { fi identity="${BASH_REMATCH[1]}" name="${BASH_REMATCH[2]}" + if [[ "$name" == *..* ]]; then + echo "apm audit --ci: ref-consistency not waived: plugin name '$name' contains '..'" >&2 + return 1 + fi if [ "$identity/plugins/$name" != "$(dep_identity "$name")" ]; then echo "apm audit --ci: ref-consistency not waived: $identity/plugins/$name is not root apm.yml's git+path entry for plugins/$name" >&2 return 1 @@ -189,6 +235,91 @@ ref_waivable() { printf '%s' "$names" } +# reduce_manifest -- prints apm.yml (cwd) with every +# `dependencies:` > `apm:` list item whose `path:` is plugins/ for a +# listed name removed, and every other line unchanged. Same line scan, and the +# same one shape, as dep_identity. +reduce_manifest() { + REDUCE_NAMES="$1" awk ' + BEGIN { n = split(ENVIRON["REDUCE_NAMES"], a, "\n"); for (i = 1; i <= n; i++) if (a[i] != "") drop["plugins/" a[i]] = 1 } + function flush() { if (buf != "" && !(p in drop)) printf "%s", buf; buf = ""; p = ""; in_item = 0 } + /^[^ \t#]/ && $0 !~ /^$/ { flush(); in_deps = ($0 ~ /^dependencies:/); in_apm = 0; print; next } + !in_deps { print; next } + /^ [^ #]/ { flush(); in_apm = ($0 ~ /^ apm:/); print; next } + !in_apm { print; next } + /^ - / { flush(); in_item = 1; buf = $0 "\n" + v = $0; sub(/^ - /, "", v) + if (v ~ /^path:/) { sub(/^path:/, "", v); setp(v) } + next } + in_item && /^ / { + if ($0 ~ /^ path:/) { v = $0; sub(/^ path:/, "", v); setp(v) } + buf = buf $0 "\n"; next } + { flush(); print } + function setp(v) { + sub(/^[ \t]+/, "", v); sub(/[ \t]+$/, "", v) + if (v ~ /^".*"$/ || v ~ /^\047.*\047$/) v = substr(v, 2, length(v) - 2) + sub(/^\.\//, "", v); sub(/\/$/, "", v); p = v + } + END { flush() } + ' apm.yml +} + +SCRATCH="" +cleanup() { [ -z "$SCRATCH" ] || rm -rf "$SCRATCH"; } +trap cleanup EXIT +trap 'exit 130' INT +trap 'exit 143' TERM HUP + +# reduced_root_audit -- condition 5 of the second waiver: +# re-runs the root audit in a scratch copy with the named plugins' dependency +# entries removed (header, "WHY THE RE-RUN"). Succeeds only when that run exits +# 0 and reports the full root check set passing; otherwise prints why, and +# apm's output, on stderr and fails. +reduced_root_audit() { + local names="$1" origin name rerun rc delta + SCRATCH="$(mktemp -d "${TMPDIR:-/tmp}/apm-audit-ci.XXXXXX")" || return 1 + if ! tar -C . --exclude=./.git --exclude=./.venv -cf - . | tar -C "$SCRATCH" -xf -; then + echo 'apm audit --ci: ref-consistency not waived: could not copy the tree for the re-run' >&2 + return 1 + fi + if ! reduce_manifest "$names" >"$SCRATCH/apm.yml"; then + echo 'apm audit --ci: ref-consistency not waived: could not reduce apm.yml for the re-run' >&2 + return 1 + fi + # Deletions only: diff's normal format prints a `NdM` / `N,MdK` header and + # `< ` lines for each deleted range, and anything else is an addition or a + # change. A here-string, not a pipe into grep -q (see the axis-1 comment + # below), and an empty delta fails too: something must have been removed. + delta="$(diff apm.yml "$SCRATCH/apm.yml")" + if grep -qvE '^([0-9]+(,[0-9]+)?d[0-9]+|< .*|<)$' <<<"$delta"; then + echo 'apm audit --ci: ref-consistency not waived: reducing apm.yml changed more than the waived entries' >&2 + return 1 + fi + while IFS= read -r name; do + [ -n "$name" ] || continue + if [ -n "$(cd "$SCRATCH" && dep_identity "$name")" ]; then + echo "apm audit --ci: ref-consistency not waived: could not remove plugins/$name's entry for the re-run" >&2 + return 1 + fi + done <<<"$names" + if ! git init -q "$SCRATCH"; then + echo 'apm audit --ci: ref-consistency not waived: could not git init the re-run copy' >&2 + return 1 + fi + if origin="$(git remote get-url origin 2>/dev/null)"; then + git -C "$SCRATCH" remote add origin "$origin" || return 1 + fi + rc=0 + rerun="$(cd "$SCRATCH" && run_apm audit --ci 2>&1)" || rc=$? + if [ "$rc" -ne 0 ] || ! grep -qF "$ROOT_FULL_PASS" <<<"$rerun"; then + printf '%s\n' "$rerun" >&2 + echo "apm audit --ci: ref-consistency not waived: the root re-run without the unlanded plugins did not report '$ROOT_FULL_PASS' (exit $rc)" >&2 + return 1 + fi + rm -rf "$SCRATCH" + SCRATCH="" +} + status=0 for manifest_dir in . plugins/*/; do @@ -201,7 +332,9 @@ for manifest_dir in . plugins/*/; do # The root's one waiver: plugins not yet on origin/main (header, second # waiver). - if [ "$manifest_dir" = "." ] && waived="$(ref_waivable "$output")"; then + if [ "$manifest_dir" = "." ] && waived="$(ref_waivable "$output")" && + reduced_root_audit "$waived"; then + printf 'apm audit --ci: root re-run without the unlanded plugins: %s\n' "$ROOT_FULL_PASS" while IFS= read -r name; do printf 'apm audit --ci: waived ref-consistency for plugins/%s (not yet on origin/main; ADR-0019)\n' \ "$name" diff --git a/tests/test-apm-audit-ci-waiver.sh b/tests/test-apm-audit-ci-waiver.sh index 9a853bf..769c158 100755 --- a/tests/test-apm-audit-ci-waiver.sh +++ b/tests/test-apm-audit-ci-waiver.sh @@ -6,7 +6,10 @@ # Same seam as tests/test-apm-gate-invocation.sh: a stub `apm`, reached through # the APM override so no uv or real apm runs. The stub prints canned `audit # --ci` output for the root manifest ($STUB_ROOT_OUTPUT, exit 1) and passes in -# every plugin directory. The fixture is a throwaway git repo whose +# every plugin directory. The waiver's re-run is told apart by where it runs: +# under $TMPDIR, the script's mktemp scratch copy, where the stub saves the manifest it was given ($STUB_RERUN_LOG) and prints +# $STUB_RERUN_OUTPUT (default: the full root pass) with exit $STUB_RERUN_RC +# (default 0). The fixture is a throwaway git repo whose # refs/remotes/origin/main is set with update-ref -- nothing is fetched. set -euo pipefail @@ -32,8 +35,13 @@ if [[ "${1:-}" == --version ]]; then exit 0 fi if [[ -f apm.lock.yaml.root-marker ]]; then - printf '%s\n' "$STUB_ROOT_OUTPUT" - exit 1 + if [[ "$PWD" != "$TMPDIR"/* ]]; then + printf '%s\n' "$STUB_ROOT_OUTPUT" + exit 1 + fi + [[ -z "${STUB_RERUN_LOG:-}" ]] || cp apm.yml "$STUB_RERUN_LOG" + printf '%s\n' "${STUB_RERUN_OUTPUT-[*] All 15 check(s) passed}" + exit "${STUB_RERUN_RC:-0}" fi exit 0 EOF @@ -93,10 +101,19 @@ EOF RC=0 OUT="" -run_audit() { # +# TMPDIR is a fresh per-run dir, so the re-run's scratch copy can be checked +# for cleanup. +TMPD="$TMP/tmpdir" +run_audit() { # [VAR=value ...] + local dir="$1" root="$2" + shift 2 RC=0 - OUT="$(cd "$1" && env APM="$BIN/apm" STUB_ROOT_OUTPUT="$2" bash "$AUDIT" 2>&1)" || RC=$? + rm -rf "$TMPD" + mkdir -p "$TMPD" + OUT="$(cd "$dir" && env APM="$BIN/apm" STUB_ROOT_OUTPUT="$root" TMPDIR="$TMPD" "$@" \ + bash "$AUDIT" 2>&1)" || RC=$? } +tmp_clean() { [[ -z "$(ls -A "$TMPD")" ]]; } WAIVER_LINE='apm audit --ci: waived ref-consistency for plugins/rtk (not yet on origin/main; ADR-0019)' SOLE='[x] 1 of 3 check(s) failed' @@ -105,9 +122,43 @@ RTK_DETAIL="$IDENTITY/plugins/rtk: not found in lockfile" echo "--- waived: one plugin not yet on origin/main ---" F="$TMP/waived" make_fixture "$F" -run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL")" +cp "$F/apm.yml" "$TMP/apm.yml.orig" +run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL")" STUB_RERUN_LOG="$TMP/rerun.yml" [[ $RC -eq 0 ]] && pass "exits 0" || fail "exited $RC: $OUT" grep -qxF "$WAIVER_LINE" <<< "$OUT" && pass "prints the waiver line" || fail "no waiver line: $OUT" +grep -qxF 'apm audit --ci: root re-run without the unlanded plugins: All 15 check(s) passed' <<< "$OUT" \ + && pass "reports the full-set re-run" || fail "no re-run line: $OUT" +# The re-run's manifest is the original minus exactly the rtk item. +if [[ -f "$TMP/rerun.yml" ]] && diff "$TMP/apm.yml.orig" "$TMP/rerun.yml" > "$TMP/rerun.diff"; then + fail "the re-run saw an unreduced apm.yml" +elif [[ "$(cat "$TMP/rerun.diff" 2>/dev/null)" == "$(printf '%s\n' '7,8d6' \ + "< - git: https://$IDENTITY.git" '< path: plugins/rtk')" ]]; then + pass "the re-run's apm.yml drops only the plugins/rtk entry" +else + fail "unexpected re-run manifest: $(cat "$TMP/rerun.diff" 2>/dev/null)" +fi +cmp -s "$TMP/apm.yml.orig" "$F/apm.yml" && [[ -z "$(git -C "$F" status --porcelain -- apm.yml)" ]] \ + && pass "leaves the working tree's apm.yml byte-identical" || fail "apm.yml changed" +tmp_clean && pass "removes the scratch copy" || fail "scratch left behind: $(ls -A "$TMPD")" + +echo "" +echo "--- not waived: the full-set re-run fails ---" +run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL")" STUB_RERUN_RC=1 \ + STUB_RERUN_OUTPUT="$(printf '%s\n' '| [x] | content-integrity | hidden Unicode |' '[x] 1 of 15 check(s) failed')" +[[ $RC -ne 0 ]] && ! grep -qF "waived ref-consistency" <<< "$OUT" \ + && grep -qF "content-integrity" <<< "$OUT" \ + && pass "fails, showing the re-run's failing check" || fail "must fail (rc=$RC): $OUT" +cmp -s "$TMP/apm.yml.orig" "$F/apm.yml" && pass "apm.yml still byte-identical" || fail "apm.yml changed" +tmp_clean && pass "removes the scratch copy on failure too" || fail "scratch left behind: $(ls -A "$TMPD")" +run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL")" STUB_RERUN_RC=1 +[[ $RC -ne 0 ]] && ! grep -qF "waived ref-consistency" <<< "$OUT" \ + && pass "fails when the re-run exits non-zero despite a passing summary" || fail "must fail (rc=$RC): $OUT" +run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL")" STUB_RERUN_OUTPUT='[*] All 14 check(s) passed' +[[ $RC -ne 0 ]] && ! grep -qF "waived ref-consistency" <<< "$OUT" \ + && pass "fails when the re-run runs fewer than the full set" || fail "must fail (rc=$RC): $OUT" +run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL")" STUB_RERUN_OUTPUT='' +[[ $RC -ne 0 ]] && ! grep -qF "waived ref-consistency" <<< "$OUT" \ + && pass "fails when the re-run prints no summary" || fail "must fail (rc=$RC): $OUT" echo "" echo "--- not waived: the plugin is already on origin/main ---" @@ -129,6 +180,47 @@ run_audit "$F" "$(root_output '[x] 2 of 3 check(s) failed' \ [[ $RC -ne 0 ]] && ! grep -qF "waived ref-consistency" <<< "$OUT" \ && pass "fails when drift fails too" || fail "must fail (rc=$RC): $OUT" +echo "" +echo "--- not waived: one of three fails, but it is not ref-consistency ---" +F="$TMP/otherrow" +make_fixture "$F" +run_audit "$F" "$(root_output "$SOLE" \ + '| [x] | drift | 1 file(s) differ |' "$RTK_DETAIL" | + sed 's/^| \[x\] | ref-consistency/| [+] | ref-consistency/')" +[[ $RC -ne 0 ]] && ! grep -qF "waived ref-consistency" <<< "$OUT" \ + && pass "fails when the sole failing row is drift" || fail "must fail (rc=$RC): $OUT" + +echo "" +echo "--- not waived: a duplicated details header ---" +F="$TMP/duphdr" +make_fixture "$F" +run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL" + printf '%s\n' '' ' ref-consistency details:' ' - github.com/acme/skills: not found in lockfile')" +[[ $RC -ne 0 ]] && ! grep -qF "waived ref-consistency" <<< "$OUT" \ + && pass "fails when a second details block could hide a detail" || fail "must fail (rc=$RC): $OUT" + +echo "" +echo "--- not waived: a plugin name that is . or .., or holds .. ---" +F="$TMP/dotnames" +make_fixture "$F" +# Each entry goes inside the apm: list, above ` mcp: []`, so dep_identity +# finds it and only the name guards stand between it and a waiver. +for n in .. . .hidden a..b; do + awk -v e=" - git: https://$IDENTITY.git"$'\n'" path: plugins/$n" \ + '/^ mcp:/ { print e } { print }' "$F/apm.yml" > "$F/apm.yml.new" + mv "$F/apm.yml.new" "$F/apm.yml" + mkdir -p "$F/plugins/$n" + [[ -f "$F/plugins/$n/apm.yml" ]] || printf 'name: x\n' > "$F/plugins/$n/apm.yml" +done +for n in .. . .hidden; do + run_audit "$F" "$(root_output "$SOLE" "" "$IDENTITY/plugins/$n: not found in lockfile")" + [[ $RC -ne 0 ]] && grep -qF "unrecognised detail line" <<< "$OUT" \ + && pass "fails on plugins/$n" || fail "must fail on plugins/$n (rc=$RC): $OUT" +done +run_audit "$F" "$(root_output "$SOLE" "" "$IDENTITY/plugins/a..b: not found in lockfile")" +[[ $RC -ne 0 ]] && grep -qF "contains '..'" <<< "$OUT" \ + && pass "fails on plugins/a..b" || fail "must fail on plugins/a..b (rc=$RC): $OUT" + echo "" echo "--- not waived: a mismatch outside plugins/ ---" F="$TMP/nonplugin"