diff --git a/scripts/lib/apm.sh b/scripts/lib/apm.sh index 972a0cb..5737ab2 100644 --- a/scripts/lib/apm.sh +++ b/scripts/lib/apm.sh @@ -28,7 +28,8 @@ # # 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. +# 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. @@ -41,6 +42,7 @@ APM_REPO_ROOT="$(CDPATH='' cd -- "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" # 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 @@ -48,13 +50,18 @@ else fi run_apm() { - "${APM_CMD[@]}" "$@" + ${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 @@ -73,14 +80,14 @@ require_apm_version() { echo " Fix: run 'uv sync --frozen' at the repo root, then retry." >&2 return 1 fi - # Anchored to apm's banner, `... CLI version X.Y.Z`, so no other number on - # the line can be taken for the version. - version="$(grep -oE 'version [0-9]+\.[0-9]+\.[0-9]+' <<<"$output" | head -n 1 || true)" - version="${version#version }" + # 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 'version X.Y.Z', so the apm version is unknown." >&2 + 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 diff --git a/tests/test-apm-gate-invocation.sh b/tests/test-apm-gate-invocation.sh index df2e765..9474915 100755 --- a/tests/test-apm-gate-invocation.sh +++ b/tests/test-apm-gate-invocation.sh @@ -48,9 +48,12 @@ if [[ "\${1:-}" != run || "\${2:-}" != --project || "\${4:-}" != --frozen || "\$ exit 97 fi shift 5 -# What a real \`uv run --frozen\` prints on stderr when it syncs first. The -# version check must parse apm's stdout, never this (review round 1 of #271). +# What a real \`uv run --frozen\` prints on stderr when it syncs first, plus a +# decoy shaped exactly like apm's banner. The version check must parse apm's +# stdout only: a parser reading the merged stream takes 0.0.1 from the decoy +# and fails a current apm (review rounds 1 and 2 of #271). echo "Using CPython 3.14.6" >&2 +echo "uv CLI version 0.0.1" >&2 exec "$BIN/apm" "\$@" EOF @@ -168,11 +171,37 @@ for pair in "apm-audit-ci:$AUDIT" "apm-pack-check-clean:$PACK"; do done reset_logs run_gate "$FIXTURE" "$script" STUB_APM_OUTPUT="apm, version unknown" - [[ $RC -ne 0 ]] && grep -qF "printed no 'version X.Y.Z'" <<< "$OUT" \ + [[ $RC -ne 0 ]] && grep -qF "printed no 'CLI version X.Y.Z' banner" <<< "$OUT" \ && pass "$label: fails when the version cannot be read" \ || fail "$label: an unreadable version must fail clearly (rc=$RC): $OUT" done +echo "" +echo "--- the version is read from apm's stdout banner, nothing else ---" +# The fake uv writes a banner-shaped `uv CLI version 0.0.1` to stderr on every +# call, so these fail if stderr reaches the parser. +reset_logs +run_gate "$FIXTURE" "$PACK" STUB_APM_VERSION=0.39.3 +[[ $RC -eq 0 ]] && pass "stdout only: apm 0.39.3 passes despite a 0.0.1 decoy on uv's stderr" \ + || fail "stdout only: 0.39.3 must pass, the stderr decoy was parsed: $OUT" +reset_logs +run_gate "$FIXTURE" "$PACK" STUB_APM_VERSION=0.28.0 +[[ $RC -ne 0 ]] && grep -qF "apm 0.28.0 is below 0.39.2" <<< "$OUT" \ + && pass "stdout only: apm 0.28.0 still fails" || fail "stdout only: 0.28.0 must fail: $OUT" + +# Other version-like text on stdout, before the banner, is not the version. +reset_logs +run_gate "$FIXTURE" "$PACK" \ + STUB_APM_OUTPUT=$'reads lockfile version 0.40.0\nAgent Package Manager (APM) CLI version 0.28.0' +[[ $RC -ne 0 ]] && grep -qF "apm 0.28.0 is below 0.39.2" <<< "$OUT" \ + && pass "banner anchor: a 'version 0.40.0' line before the banner does not rescue 0.28.0" \ + || fail "banner anchor: the decoy line was taken for the version (rc=$RC): $OUT" +reset_logs +run_gate "$FIXTURE" "$PACK" \ + STUB_APM_OUTPUT=$'reads lockfile version 0.40.0\nAgent Package Manager (APM) CLI version 0.39.3' +[[ $RC -eq 0 ]] && pass "banner anchor: the banner's 0.39.3 is the version read" \ + || fail "banner anchor: 0.39.3 behind a decoy line must pass (rc=$RC): $OUT" + echo "" echo "--- no uv, and the APM override ---" RC=0 @@ -187,6 +216,12 @@ OUT="$(cd "$FIXTURE" && env PATH="$NO_UV_PATH" APM="$BIN/apm" bash "$PACK" 2>&1) && pass "APM overrides the command and bypasses uv" \ || fail "APM override not honoured (rc=$RC): $OUT" +RC=0 +OUT="$(cd "$FIXTURE" && env PATH="$FAKE_PATH" APM=" " bash "$PACK" 2>&1)" || RC=$? +[[ $RC -ne 0 ]] && grep -qF "holds only whitespace" <<< "$OUT" \ + && pass "a whitespace-only APM fails clearly" \ + || fail "a whitespace-only APM must fail clearly (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=$? diff --git a/tests/test-apm-hooks-git-env.sh b/tests/test-apm-hooks-git-env.sh index 75e679b..ba540b4 100644 --- a/tests/test-apm-hooks-git-env.sh +++ b/tests/test-apm-hooks-git-env.sh @@ -68,7 +68,7 @@ EOF # --version answers above the gates' 0.39.2 floor and records nothing. cat > "$dir/bin/apm" <}" >> "$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