Files
Holocron/scripts/apm-audit-ci.sh
T
Defame1297andClaude Opus 5.5 7e63f22bf8 docs: re-verify the apm 0.28.0 claims in hook and test comments
Checked against apm 0.39.3 live and in its source. The apm-audit-ci
description and comments now give 15 root checks and 2 plugin checks,
about 70 s through uv run, manifest-parse as a named row, and the new
"nothing to scan" wording. The run-bats copytree note, the #204 git-env
test header and its LESSONS entry say what holds on 0.39.3.

Refs #272

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi
2026-12-02 11:49:22 +00:00

107 lines
4.8 KiB
Bash
Executable File

#!/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/<pkg>/.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, whatever it reports
# 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.
set -uo pipefail
# apm is the repo-pinned one, run through scripts/lib/apm.sh as
# `uv run --project <repo root> --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'
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
# Axis 1: the root is never waived.
# 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"