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 <[email protected]>
Claude-Session: https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi
This commit is contained in:
Defame1297andClaude Opus 5.5 committed 2026-12-06 10:07:49 +00:00
1 parent aaa73dddd3
commit e14fca123d
3 files changed
+253 -11

No files matched your search

+135 -2
View File
@@ -82,9 +82,48 @@
# 3. plugins/<name>/apm.yml exists in the working tree
# 4. plugins/<name>/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 <repo root>`, 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 <plugin dir name> -- the identity root apm.yml's git+path entry
# for plugins/<name> yields: <host>/<owner>/<repo>/plugins/<name>. 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 <names, one per line> -- prints apm.yml (cwd) with every
# `dependencies:` > `apm:` list item whose `path:` is plugins/<name> 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 <names, one per line> -- 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"