Match `CLI version X.Y.Z`, the banner apm prints, instead of any `version X.Y.Z`, so other version-like text on stdout is not taken for apm's version. Fail clearly when APM holds only whitespace, instead of running an empty command. The seam B test now pins both properties. The fake uv also writes a banner-shaped `uv CLI version 0.0.1` to stderr, so a parser reading the merged stream fails it (23 failures with the round-1 bug restored). A stub that prints `version 0.40.0` before a 0.28.0 banner must still fail. The git-env test's stub prints the real banner. Refs: #271 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi
111 lines
5.0 KiB
Bash
111 lines
5.0 KiB
Bash
#!/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, and an APM that holds only
|
|
# whitespace fails that check rather than silently running the default.
|
|
#
|
|
# 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="$(CDPATH='' 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"
|
|
|
|
APM_CMD=()
|
|
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[@]+"${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 stderr_file
|
|
if [[ ${#APM_CMD[@]} -eq 0 ]]; then
|
|
echo "FAIL: APM is set but holds only whitespace, so there is no apm command to run." >&2
|
|
echo " Fix: unset APM to run the repo-pinned apm, or set it to a command." >&2
|
|
return 1
|
|
fi
|
|
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
|
|
# stdout only. uv writes its own lines to stderr when `uv run` syncs first
|
|
# (`Using CPython 3.14.6`, `Installed 62 packages`), and a merged stream
|
|
# would hand the parser uv's numbers instead of apm's. stderr is kept aside
|
|
# and shown only when the check fails.
|
|
stderr_file="$(mktemp)"
|
|
if ! output="$(run_apm --version 2>"$stderr_file")"; then
|
|
cat "$stderr_file" >&2
|
|
printf '%s\n' "$output" >&2
|
|
rm -f "$stderr_file"
|
|
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
|
|
# Anchored to apm's banner, `Agent Package Manager (APM) CLI version X.Y.Z`,
|
|
# so no other version-like text on stdout can be taken for apm's version.
|
|
version="$(grep -oE 'CLI version [0-9]+\.[0-9]+\.[0-9]+' <<<"$output" | head -n 1 || true)"
|
|
version="${version#CLI version }"
|
|
if [[ ! "$version" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
|
|
cat "$stderr_file" >&2
|
|
rm -f "$stderr_file"
|
|
echo "FAIL: '${APM_CMD[*]} --version' printed no 'CLI version X.Y.Z' banner, so the apm version is unknown." >&2
|
|
echo " Fix: run 'uv sync --frozen' at the repo root, then retry." >&2
|
|
return 1
|
|
fi
|
|
rm -f "$stderr_file"
|
|
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
|
|
}
|