feat(scripts): run the apm gates through the uv-pinned apm

Add scripts/lib/apm.sh, the one place the repo spells its apm command:
`uv run --project <repo root> --frozen apm`. --project comes from the
helper's own location, so it names the repo root even when apm runs inside
plugins/<name>/. Sourcing it keeps the git-env scrub (#204), and
require_apm_version fails with a clear message, naming `uv sync --frozen`,
when the resolved apm is below 0.39.2, unreadable, or uv is missing. APM
overrides the command, as ADR-0034 provides.

apm-audit-ci.sh and apm-pack-check-clean.sh use it. The audit script's
matching logic is unchanged; only its verified-against note now records
0.39.3.

tests/test-apm-gate-invocation.sh is seam B: a fake uv and a stub apm
first on a PATH without the real ones. It proves the command shape,
--project from a plugin directory, the scrub, and the version floor.
test-apm-hooks-git-env.sh reaches its stub through a fake uv, and the
real-apm probe in test-apm-current-hook.sh now runs the pinned apm.

Refs: #271

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-02 08:47:12 +00:00
1 parent d15688ee99
commit 7291b46271
6 files changed
+345 -18

No files matched your search

+15 -8
View File
@@ -28,7 +28,9 @@
# 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.
# 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
#
@@ -55,12 +57,17 @@
set -uo pipefail
# Scrub 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 the helper for the full account.
# shellcheck source=scripts/lib/clean-git-env.sh
source "$(dirname "${BASH_SOURCE[0]}")/lib/clean-git-env.sh"
# 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'
@@ -68,7 +75,7 @@ readonly SOLE_FAILURE='1 of 1 check(s) failed'
status=0
for manifest_dir in . plugins/*/; do
output="$(cd "$manifest_dir" && apm audit --ci 2>&1)"
output="$(cd "$manifest_dir" && run_apm audit --ci 2>&1)"
exit_code=$?
if [ "$exit_code" -eq 0 ]; then
+7 -5
View File
@@ -1,12 +1,14 @@
#!/usr/bin/env bash
# apm-pack-check-clean pre-push hook: `apm pack --check-versions --check-clean
# --dry-run`, run with git's repo-locating env scrubbed first. A script rather
# than a bare `apm ...` entry only so it can source that scrub -- see
# --dry-run`, run through scripts/lib/apm.sh. That helper runs the repo-pinned
# apm as `uv run --project <repo root> --frozen apm` (ADR-0034), fails clearly
# below the minimum version, and scrubs git's repo-locating env first -- see
# scripts/lib/clean-git-env.sh and issue #204, and docs/spec/gates.md,
# "apm-audit-ci", the "apm runs with git's repo-locating env scrubbed" paragraph.
set -euo pipefail
# shellcheck source=scripts/lib/clean-git-env.sh
source "$(dirname "${BASH_SOURCE[0]}")/lib/clean-git-env.sh"
# shellcheck source=scripts/lib/apm.sh
source "$(dirname "${BASH_SOURCE[0]}")/lib/apm.sh"
exec apm pack --check-versions --check-clean --dry-run
require_apm_version || exit 1
exec "${APM_CMD[@]}" pack --check-versions --check-clean --dry-run
+90
View File
@@ -0,0 +1,90 @@
#!/usr/bin/env bash
# The one place this repo spells how its scripts run apm (ADR-0034).
#
# Source it, call `require_apm_version || exit 1` once, then run apm as
# `run_apm <args>`. Sourcing also applies scripts/lib/clean-git-env.sh, so every
# gate that reaches apm through this helper runs it with git's repo-locating env
# scrubbed (issue #204).
#
# THE COMMAND
#
# uv run --project <repo root> --frozen apm
#
# * The pin is the dev group in the root pyproject.toml, locked by uv.lock. A
# bare `apm` would be whatever is on PATH, which on this machine is the global
# pipx 0.28.0 that refuses the v4 apm.lock.yaml (ADR-0034, consequence 1).
# * --project is the repo root, derived from this file's own location, never
# from the cwd. apm-audit-ci.sh runs apm from inside each plugins/<name>/, and
# uv's project discovery would otherwise start there. --project does not
# change the cwd, so apm still reads the manifest of the directory it runs in.
# * --frozen runs from uv.lock as committed and never re-resolves it. It does
# still sync the environment to that lock before running, and that is
# deliberate for gates, unlike the SessionStart hook, which adds --no-sync
# --offline (ADR-0034's 2026-12-02 amendment). A gate runs after the one-time
# `uv sync --frozen`, where that sync is a no-op that needs no network; and if
# the pin moves (a pull that bumps apm-cli), the sync brings the environment
# to the new pin instead of letting the gate certify the tree with a stale
# apm. The hook cannot afford that sync inside its timeout; a push gate can.
#
# APM overrides the command, as a seam for tests (ADR-0034, "Invocation"). It is
# split on whitespace, so it can name a stub or a full command line. The
# minimum-version check applies to it too.
#
# Not meant to be executed directly.
# shellcheck source=scripts/lib/clean-git-env.sh
source "$(dirname "${BASH_SOURCE[0]}")/clean-git-env.sh"
APM_REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
# 0.39.2 is the lowest apm that reads and writes lockfile v4 in a form later
# versions accept, and it is above 0.39.0, which keeps Claude command keys such
# as disable-model-invocation (ADR-0034, "Minimum version").
APM_MIN_VERSION="0.39.2"
if [[ -n "${APM:-}" ]]; then
read -r -a APM_CMD <<<"$APM"
else
APM_CMD=(uv run --project "$APM_REPO_ROOT" --frozen apm)
fi
run_apm() {
"${APM_CMD[@]}" "$@"
}
# Fails, with a message naming the fix, when the apm this helper resolves is
# missing, unreadable, or below APM_MIN_VERSION. Returns 0 when it is new enough.
require_apm_version() {
local output version
if [[ -z "${APM:-}" ]] && ! command -v uv >/dev/null 2>&1; then
echo "FAIL: uv is not on PATH, and this repo runs apm as 'uv run --frozen apm' (ADR-0034)." >&2
echo " Fix: install uv, then run 'uv sync --frozen' at the repo root." >&2
return 1
fi
if ! output="$(run_apm --version 2>&1)"; then
printf '%s\n' "$output" >&2
echo "FAIL: could not run '${APM_CMD[*]} --version', so the apm version is unknown." >&2
echo " Fix: run 'uv sync --frozen' at the repo root, then retry." >&2
return 1
fi
version="$(grep -oE '[0-9]+\.[0-9]+\.[0-9]+' <<<"$output" | head -n 1 || true)"
if [[ ! "$version" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
echo "FAIL: '${APM_CMD[*]} --version' printed no version, so the apm version is unknown." >&2
echo " Fix: run 'uv sync --frozen' at the repo root, then retry." >&2
return 1
fi
local -a have=("${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" "${BASH_REMATCH[3]}")
local -a need
IFS=. read -r -a need <<<"$APM_MIN_VERSION"
local i
for i in 0 1 2; do
((10#${have[i]} > 10#${need[i]})) && return 0
if ((10#${have[i]} < 10#${need[i]})); then
echo "FAIL: apm $version is below $APM_MIN_VERSION, the minimum this repo needs (ADR-0034)." >&2
echo " Why: apm before 0.39.2 cannot read this repo's lockfile v4 apm.lock.yaml in a form" >&2
echo " later versions accept, and before 0.39.0 it drops Claude command keys." >&2
echo " Fix: run 'uv sync --frozen' at the repo root so '${APM_CMD[*]}' resolves the pinned apm." >&2
return 1
fi
done
return 0
}
+19 -3
View File
@@ -773,9 +773,24 @@ echo "--- the greped phrase, against the real apm ---"
# The genuine output is then replayed into the real hook through the mock, so
# what is asserted is the hook's own matching logic against real apm text —
# no pattern is duplicated here to drift out of sync.
#
# "The real apm" is this repo's pinned one, run through scripts/lib/apm.sh as
# `uv run --project <repo root> --frozen apm` (ADR-0034), never whatever apm is
# on PATH: that may be an older global install whose wording is not what this
# repo ships against. Sourced in a subshell so the helper's git-env scrub and
# its functions stay out of this suite's shell. The probe is skipped only when
# that apm cannot be run at all (no uv, or `uv sync --frozen` never ran).
probe_apm() {
(
# shellcheck source=scripts/lib/apm.sh
source "$REPO_ROOT/scripts/lib/apm.sh"
run_apm "$@"
)
}
SKIP_REASON=""
if ! command -v apm > /dev/null 2>&1 || ! command -v git > /dev/null 2>&1; then
SKIP_REASON="SKIP: apm and git are both required to verify the hook's phrase match against real \`apm outdated\` output — everything above ran, this axis did not"
if ! command -v git > /dev/null 2>&1 \
|| ! ( source "$REPO_ROOT/scripts/lib/apm.sh" && require_apm_version ) > /dev/null 2>&1; then
SKIP_REASON="SKIP: git and the repo-pinned apm (uv sync --frozen) are both required to verify the hook's phrase match against real \`apm outdated\` output — everything above ran, this axis did not"
echo " $SKIP_REASON"
else
PROBE="$(mktemp -d)"
@@ -821,13 +836,14 @@ else
done
echo "deployments: []"
} > "$PROBE/consumer/apm.lock.yaml"
# env cannot run a shell function, so probe_apm is re-declared in a child bash.
(
cd "$PROBE/consumer" &&
env HOME="$PROBE/home" \
GIT_CONFIG_GLOBAL="$PROBE/gitconfig" \
GIT_CONFIG_NOSYSTEM=1 \
GIT_TERMINAL_PROMPT=0 \
apm outdated 2>&1
bash -c "$(declare -f probe_apm); REPO_ROOT=\"\$1\"; probe_apm outdated" _ "$REPO_ROOT" 2>&1
)
}
+196
View File
@@ -0,0 +1,196 @@
#!/usr/bin/env bash
# Tests how the apm pre-push gates invoke apm (ADR-0034, issue #271).
#
# Seam B: a fake `uv` and a stub `apm` sit first on a PATH that excludes the
# machine's real uv and any global apm. The fake uv records its cwd, its argv
# and the GIT_DIR it inherited, accepts only the `run --project <root> --frozen
# apm ...` shape, and hands the rest to the stub. The stub answers --version
# with $STUB_APM_VERSION and records every other call. Nothing here runs a real
# apm or touches the repo this suite lives in.
#
# It proves, for both scripts/apm-audit-ci.sh and scripts/apm-pack-check-clean.sh:
# * the command is `uv run --project <repo root> --frozen apm`, and --project
# names the repo root even when apm runs from a plugin directory;
# * git's repo-locating env is still scrubbed before apm runs (issue #204);
# * an apm below 0.39.2 fails with a clear message before any gate work runs.
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
AUDIT="$REPO_ROOT/scripts/apm-audit-ci.sh"
PACK="$REPO_ROOT/scripts/apm-pack-check-clean.sh"
PASS=0
FAIL=0
pass() { echo " PASS: $1"; PASS=$((PASS + 1)); }
fail() { echo " FAIL: $1"; FAIL=$((FAIL + 1)); }
# The test must never inherit an override or the caller's repo.
unset APM GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_OBJECT_DIRECTORY GIT_COMMON_DIR
TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT
BIN="$TMP/bin"
mkdir -p "$BIN"
UV_LOG="$TMP/uv.log"
APM_LOG="$TMP/apm.log"
cat > "$BIN/uv" << EOF
#!/usr/bin/env bash
{
printf 'cwd=%s\n' "\$PWD"
printf 'git_dir=%s\n' "\${GIT_DIR-<unset>}"
printf 'git_work_tree=%s\n' "\${GIT_WORK_TREE-<unset>}"
printf 'argv=%s\n' "\$*"
} >> "$UV_LOG"
if [[ "\${1:-}" != run || "\${2:-}" != --project || "\${4:-}" != --frozen || "\${5:-}" != apm ]]; then
echo "fake uv: unexpected argv: \$*" >&2
exit 97
fi
shift 5
exec "$BIN/apm" "\$@"
EOF
cat > "$BIN/apm" << EOF
#!/usr/bin/env bash
if [[ "\${1:-}" == --version ]]; then
printf '%s\n' "\${STUB_APM_OUTPUT:-Agent Package Manager (APM) CLI version \${STUB_APM_VERSION:-0.39.3}}"
exit 0
fi
printf 'cwd=%s args=%s\n' "\$PWD" "\$*" >> "$APM_LOG"
exit 0
EOF
chmod +x "$BIN/uv" "$BIN/apm"
# A PATH with the fakes first and only system directories after them: no
# /usr/local/bin (the real uv) and no ~/.local/bin (the global pipx apm).
FAKE_PATH="$BIN:/usr/bin:/bin"
NO_UV_PATH="/usr/bin:/bin"
for tool in uv apm; do
if PATH="$NO_UV_PATH" command -v "$tool" > /dev/null 2>&1; then
echo "SKIP: a real $tool is on $NO_UV_PATH, so the fake cannot be isolated"
exit 77
fi
done
# A fixture tree for apm-audit-ci.sh's per-manifest loop: a root and one
# plugin package. The script iterates `. plugins/*/` relative to its cwd.
FIXTURE="$TMP/fixture"
mkdir -p "$FIXTURE/plugins/demo"
reset_logs() { : > "$UV_LOG"; : > "$APM_LOG"; }
# run_gate <cwd> <script> [VAR=value ...] — sets RC and OUT (stdout+stderr).
RC=0
OUT=""
run_gate() {
local cwd="$1" script="$2"
shift 2
RC=0
OUT="$(cd "$cwd" && env PATH="$FAKE_PATH" "$@" bash "$script" 2>&1)" || RC=$?
}
# Every uv call names the repo root as --project and uses --frozen.
check_project_root() {
local label="$1" n bad
n="$(grep -c '^argv=' "$UV_LOG" || true)"
if [[ "$n" -eq 0 ]]; then
fail "$label: uv was never called"
return
fi
bad="$(grep '^argv=' "$UV_LOG" | grep -vF "argv=run --project $REPO_ROOT --frozen apm " || true)"
if [[ -z "$bad" ]]; then
pass "$label: all $n uv call(s) are 'run --project <repo root> --frozen apm'"
else
fail "$label: uv called with another shape: $bad"
fi
}
echo "--- apm-pack-check-clean: the command, run from a plugin directory ---"
reset_logs
run_gate "$REPO_ROOT/plugins/kyberforge" "$PACK"
[[ $RC -eq 0 ]] && pass "exits 0 when the pinned apm passes" || fail "exited $RC: $OUT"
check_project_root "apm-pack-check-clean from plugins/kyberforge"
grep -qxF "cwd=$REPO_ROOT/plugins/kyberforge args=pack --check-versions --check-clean --dry-run" "$APM_LOG" \
&& pass "runs 'pack --check-versions --check-clean --dry-run' in the caller's cwd" \
|| fail "unexpected apm calls: $(cat "$APM_LOG")"
echo ""
echo "--- apm-audit-ci: --project stays the repo root inside each plugin dir ---"
reset_logs
run_gate "$FIXTURE" "$AUDIT"
[[ $RC -eq 0 ]] && pass "exits 0 when every audit passes" || fail "exited $RC: $OUT"
check_project_root "apm-audit-ci"
grep -qxF "cwd=$FIXTURE/plugins/demo args=audit --ci" "$APM_LOG" \
&& pass "audits plugins/demo from inside that directory" \
|| fail "no audit ran in plugins/demo: $(cat "$APM_LOG")"
grep -qxF "cwd=$FIXTURE args=audit --ci" "$APM_LOG" \
&& pass "audits the root manifest" || fail "no root audit: $(cat "$APM_LOG")"
echo ""
echo "--- the git-env scrub still applies ---"
for pair in "apm-audit-ci:$AUDIT" "apm-pack-check-clean:$PACK"; do
label="${pair%%:*}"
script="${pair#*:}"
reset_logs
run_gate "$FIXTURE" "$script" GIT_DIR="$TMP/elsewhere/.git" GIT_WORK_TREE="$TMP/elsewhere"
[[ $RC -eq 0 ]] || fail "$label: exited $RC with GIT_DIR exported: $OUT"
if grep -q '^git_dir=' "$UV_LOG" && ! grep -q '^git_dir=[^<]' "$UV_LOG" \
&& ! grep -q '^git_work_tree=[^<]' "$UV_LOG"; then
pass "$label: uv (and so apm) saw no GIT_DIR or GIT_WORK_TREE"
else
fail "$label: git env leaked: $(grep '^git_' "$UV_LOG" | sort -u | tr '\n' ' ')"
fi
done
echo ""
echo "--- the minimum-version check ---"
for pair in "apm-audit-ci:$AUDIT" "apm-pack-check-clean:$PACK"; do
label="${pair%%:*}"
script="${pair#*:}"
for old in 0.39.1 0.28.0; do
reset_logs
run_gate "$FIXTURE" "$script" STUB_APM_VERSION="$old"
[[ $RC -ne 0 ]] && pass "$label: fails on apm $old" || fail "$label: apm $old must fail"
grep -qF "apm $old is below 0.39.2" <<< "$OUT" && grep -qF "uv sync --frozen" <<< "$OUT" \
&& pass "$label: says $old is below 0.39.2 and names the fix" \
|| fail "$label: unclear message for $old: $OUT"
[[ ! -s "$APM_LOG" ]] && pass "$label: ran no gate work on apm $old" \
|| fail "$label: ran apm past the version check: $(cat "$APM_LOG")"
done
for ok in 0.39.2 0.39.10 1.0.0; do
reset_logs
run_gate "$FIXTURE" "$script" STUB_APM_VERSION="$ok"
[[ $RC -eq 0 ]] && pass "$label: accepts apm $ok" || fail "$label: apm $ok must pass: $OUT"
done
reset_logs
run_gate "$FIXTURE" "$script" STUB_APM_OUTPUT="apm, version unknown"
[[ $RC -ne 0 ]] && grep -qF "printed no version" <<< "$OUT" \
&& pass "$label: fails when the version cannot be read" \
|| fail "$label: an unreadable version must fail clearly (rc=$RC): $OUT"
done
echo ""
echo "--- no uv, and the APM override ---"
RC=0
OUT="$(cd "$FIXTURE" && env PATH="$NO_UV_PATH" bash "$PACK" 2>&1)" || RC=$?
[[ $RC -ne 0 ]] && grep -qF "uv is not on PATH" <<< "$OUT" \
&& pass "fails clearly when uv is missing" || fail "missing uv must fail clearly (rc=$RC): $OUT"
reset_logs
RC=0
OUT="$(cd "$FIXTURE" && env PATH="$NO_UV_PATH" APM="$BIN/apm" bash "$PACK" 2>&1)" || RC=$?
[[ $RC -eq 0 && ! -s "$UV_LOG" ]] && grep -qF "args=pack" "$APM_LOG" \
&& pass "APM overrides the command and bypasses uv" \
|| fail "APM override not honoured (rc=$RC): $OUT"
reset_logs
RC=0
OUT="$(cd "$FIXTURE" && env PATH="$NO_UV_PATH" APM="$BIN/apm" STUB_APM_VERSION=0.28.0 bash "$PACK" 2>&1)" || RC=$?
[[ $RC -ne 0 ]] && grep -qF "below 0.39.2" <<< "$OUT" \
&& pass "the version check applies to an APM override too" \
|| fail "an old apm via APM must fail (rc=$RC): $OUT"
echo ""
echo "Results: $PASS passed, $FAIL failed"
[[ "$FAIL" -eq 0 ]]
+18 -2
View File
@@ -13,10 +13,15 @@
# and `git checkout` inside a fresh `mktemp -d` -- and each hook script is run
# from a linked worktree of a throwaway repo with GIT_DIR exported the way git
# exports it. Nothing here touches the repo this suite lives in.
#
# The hooks run apm as `uv run --project <repo root> --frozen apm` (ADR-0034,
# scripts/lib/apm.sh), so a fake `uv` sits beside the stub and hands everything
# after `apm` to it. tests/test-apm-gate-invocation.sh pins that command shape;
# this suite only needs the call to land on the stub.
set -euo pipefail
# This suite builds its own repos; it must never inherit the caller's.
unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_OBJECT_DIRECTORY GIT_COMMON_DIR
unset APM GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_OBJECT_DIRECTORY GIT_COMMON_DIR
export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
@@ -34,7 +39,8 @@ g() { git -c user.name=fixture -c [email protected] "$@"; }
# Builds <dir>/main (two commits on `main`) and <dir>/wt, a linked worktree on
# branch `feature`, with a plugins/demo/ package dir so the audit script's
# per-package loop has something to iterate. Also writes <dir>/bin/apm, the stub.
# per-package loop has something to iterate. Also writes <dir>/bin/apm, the stub,
# and <dir>/bin/uv, the fake that reaches it.
make_fixture() {
local dir="$1"
mkdir -p "$dir/bin"
@@ -51,8 +57,18 @@ make_fixture() {
local locked
locked="$(g -C "$dir/main" rev-parse HEAD~1)"
cat > "$dir/bin/uv" <<EOF
#!/usr/bin/env bash
[[ "\${1:-}" == run && "\${5:-}" == apm ]] || { echo "fake uv: unexpected argv: \$*" >&2; exit 97; }
shift 5
exec "$dir/bin/apm" "\$@"
EOF
chmod +x "$dir/bin/uv"
# --version answers above the gates' 0.39.2 floor and records nothing.
cat > "$dir/bin/apm" <<EOF
#!/usr/bin/env bash
if [[ "\${1:-}" == --version ]]; then echo "apm version 0.39.3"; exit 0; fi
printf '%s\n' "\${GIT_DIR-<unset>}" >> "$dir/apm-saw-git-dir"
tmp="\$(mktemp -d)"
( cd "\$tmp" && git init -q . >/dev/null 2>&1; git checkout -q --detach $locked >/dev/null 2>&1 ) || true