#!/usr/bin/env bash # apm-audit-ci pre-push hook. # # Runs `apm audit --ci` once per manifest -- the root one and each plugin # package -- because the root-only invocation audits the marketplace manifest # and nothing else, and `apm pack --check-clean` does not parse plugin # `dependencies:` blocks either. Full rationale: docs/spec/gates.md, # "apm-audit-ci". # # WHY THIS IS A SCRIPT AND NOT THE ONE-LINE `for` LOOP IT REPLACED # # apm treats any directory holding both apm.yml and apm.lock.yaml as an INSTALL # ROOT. A plugin package is not one: it is content to be installed elsewhere. # While every plugin declared `dependencies: {apm: [], mcp: []}` the distinction # never surfaced, because the plugin-level `lockfile-exists` check reported # `No dependencies declared -- lockfile not required` and passed vacuously. # # plugins/onedev was the first package to declare a real dependency (it pinned # OneDev's TOD skills; removed 2026-11-11, plugins/mattpocock is the live case), # which armed that check and left no green state: # # * no apm.lock.yaml in the package -> `lockfile-exists` fails with # "apm.yml declares dependencies but apm.lock.yaml is absent" # * an apm.lock.yaml in the package -> `lockfile-exists` passes and thereby # arms the rest of the root check set (nine more on apm 0.28.0, fourteen # on 0.39.3), and `drift` then fails demanding the # dependency's skills be DEPLOYED inside the package # (plugins//.agents/skills/...), which is meaningless for a package # and additionally litters it with an apm_modules/ tree # # So this hook waives exactly one failure: a plugin package whose ONLY failing # check is `lockfile-exists`. Verified against apm 0.28.0, and re-verified # against 0.39.3 (spike #269, on #268): both matched strings below are unchanged, # and no other check fails in any plugin directory. # # WHAT IS DELIBERATELY NOT WAIVED # # Dropping `--ci` in package directories would have been the smaller change and # is WRONG. Verified on apm 0.28.0, and again on 0.39.3 (#272), against a # scratch package whose dependency entry carried no git/path/registry field: # `apm audit --ci` exits 1 naming the field (a failing manifest-parse row on # 0.39.3), while plain `apm audit` exits 0 with "nothing to scan" ("No # recognized deployed primitives found" on 0.39.3, "No apm.lock.yaml found" # on 0.28.0). Malformed-dependency detection is the reason gates.md gives # for auditing packages at all, and a package WITH dependencies is the only kind # that can carry a malformed dependency entry -- so the check would have been # discarded precisely where it earns its keep. # # The waiver is therefore narrow on three axes, and fails closed on each: # 1. the root manifest is never waived for it (the root's own, separate # waiver is described below) # 2. the failing check must be `lockfile-exists` and no other -- the # "1 of 1 check(s) failed" assertion is what makes that true, since any # second failing check changes the count and the run fails normally # 3. output apm does not produce in the recognised shape is a failure # # Matching on apm's stdout is the weak point: an apm upgrade that rewords either # line silently turns the waiver off, which fails the push rather than hiding a # defect. If that happens, re-verify against the new output and update the two # patterns below rather than widening them. # # THE SECOND WAIVER: A PLUGIN NOT YET ON origin/main (ROOT MANIFEST ONLY) # # Root apm.yml's first-party dependencies resolve from the remote's `main` # (ADR-0019), so the lock cannot record a plugin until that plugin has landed. # check-executables-allow-sync.sh meanwhile requires a hook- or bin-shipping # plugin to have its git+path entry in root apm.yml before it can be pushed at # all. Together they make a first-landing executable-shipping plugin # unpushable: the entry is mandatory, and the root `ref-consistency` check then # fails it as `/plugins/: not found in lockfile`. Verified on # apm 0.39.3 (#179): a failing ref-consistency cuts the root run short at three # checks (lockfile-exists, ref-consistency, drift), so the sole-failure line is # `1 of 3 check(s) failed`. # # So the root manifest's audit is waived when, and only when, ALL of these hold: # 1. the failure count is exactly "1 of 3 check(s) failed" and the one # failing row is ref-consistency -- any second failure changes the count # 2. the ref-consistency details block exists and EVERY line in it reads # `/plugins/: not found in lockfile`, where is # the one root apm.yml's own git+path entry for plugins/ yields, # derived by check-executables-allow-sync.sh's rule # 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 # 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. set -uo pipefail # apm is the repo-pinned one, run through scripts/lib/apm.sh as # `uv run --project --frozen apm` (ADR-0034). Sourcing the helper # also scrubs git's repo-locating env before apm runs: pushed from a linked # worktree, the hook inherits an absolute GIT_DIR, and apm's drift replay runs # `git init` / `git checkout` in a temp dir with it -- flipping core.bare=true # and moving the worktree's HEAD (issue #204). See clean-git-env.sh for the # full account. # shellcheck source=scripts/lib/apm.sh source "$(dirname "${BASH_SOURCE[0]}")/lib/apm.sh" require_apm_version || exit 1 readonly WAIVED_CHECK='declares dependencies but apm.lock.yaml is absent' 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$' # dep_identity -- the identity root apm.yml's git+path entry # for plugins/ yields: ///plugins/. The URL rule # is check-executables-allow-sync.sh's identity(): drop the scheme (or turn an # scp-like host:path into host/path), any user@, a trailing / and .git. Empty # when there is no such entry. A line scan of the one shape root apm.yml uses # (`dependencies:` > `apm:` > `- git:` / `path:` items), not a YAML parse: an # entry in any other shape yields nothing, and so fails closed. dep_identity() { local want="plugins/$1" url url="$(awk -v want="$want" ' function unq(v) { sub(/^[ \t]+/, "", v); sub(/[ \t]+$/, "", v) if (v ~ /^".*"$/ || v ~ /^\047.*\047$/) v = substr(v, 2, length(v) - 2) return v } function flush() { if (g != "" && p == want) print g; g = ""; p = "" } /^[ \t]*(#|$)/ { next } /^[^ ]/ { flush(); in_deps = ($0 ~ /^dependencies:/); in_apm = 0; next } !in_deps { next } /^ [^ ]/ { flush(); in_apm = ($0 ~ /^ apm:/); next } !in_apm { next } /^ - / { flush(); sub(/^ - /, " ") } /^ git:/ { v = $0; sub(/^ git:/, "", v); g = unq(v); next } /^ path:/ { v = $0; sub(/^ path:/, "", v); p = unq(v); sub(/^\.\//, "", p); sub(/\/$/, "", p); next } END { flush() } ' apm.yml 2>/dev/null | head -n 1)" [ -n "$url" ] || return 0 if [[ "$url" == *://* ]]; then url="${url#*://}" elif [[ "$url" == *:* ]]; then url="${url/://}" fi url="${url#*@}" url="${url%/}" url="${url%.git}" printf '%s/%s' "$url" "$want" } # ref_waivable -- succeeds, printing each waived plugin dir name # on its own line, only when the root output is exactly the unlanded-plugin # ref-consistency failure described in the header. Otherwise prints why on # stderr and fails. ref_waivable() { local output="$1" details line identity name names="" grep -qF "$REF_SOLE_FAILURE" <<<"$output" || return 1 grep -qE "$REF_FAILED_ROW" <<<"$output" || return 1 [ "$(grep -cxF "$REF_DETAILS_HEADER" <<<"$output")" -eq 1 ] || return 1 # The details block: every line after the header up to the first blank one. details="$(awk -v h="$REF_DETAILS_HEADER" ' $0 == h { on = 1; next } on && /^[ \t]*$/ { exit } on { print } ' <<<"$output")" [ -n "$details" ] || return 1 if ! git rev-parse --verify --quiet 'origin/main^{commit}' >/dev/null; then echo 'apm audit --ci: ref-consistency not waived: origin/main does not resolve locally, so whether a plugin has landed is unknown' >&2 return 1 fi while IFS= read -r line; do if [[ ! "$line" =~ $REF_DETAIL ]]; then echo "apm audit --ci: ref-consistency not waived: unrecognised detail line: $line" >&2 return 1 fi identity="${BASH_REMATCH[1]}" name="${BASH_REMATCH[2]}" 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 fi if [ ! -f "plugins/$name/apm.yml" ]; then echo "apm audit --ci: ref-consistency not waived: plugins/$name/apm.yml is not in the working tree" >&2 return 1 fi if git cat-file -e "origin/main:plugins/$name/apm.yml" 2>/dev/null; then echo "apm audit --ci: ref-consistency not waived: plugins/$name is already on origin/main, so the lock must record it" >&2 return 1 fi names+="$name"$'\n' done <<<"$details" printf '%s' "$names" } status=0 for manifest_dir in . plugins/*/; do output="$(cd "$manifest_dir" && run_apm audit --ci 2>&1)" exit_code=$? if [ "$exit_code" -eq 0 ]; then continue fi # The root's one waiver: plugins not yet on origin/main (header, second # waiver). if [ "$manifest_dir" = "." ] && waived="$(ref_waivable "$output")"; then 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" done <<<"$waived" continue fi # Axis 1: the root is never waived for lockfile-exists. # Here-strings, not `printf ... | grep -q`: under `set -o pipefail` grep -q # exits on its first match, SIGPIPEs the writer, and the writer's death # becomes the pipeline's status -- a race tests/test-no-pipefail-early-exit-grep.sh # scans every tracked script for. if [ "$manifest_dir" != "." ] && grep -qF "$WAIVED_CHECK" <<<"$output" && grep -qF "$SOLE_FAILURE" <<<"$output"; then printf 'apm audit --ci: waived lockfile-exists in %s (package, not an install root)\n' \ "$manifest_dir" continue fi printf '%s\n' "$output" >&2 printf 'apm audit --ci failed in %s\n' "$manifest_dir" >&2 status=1 done exit "$status"