diff --git a/LESSONS.md b/LESSONS.md index ebb4fcb..462f464 100644 --- a/LESSONS.md +++ b/LESSONS.md @@ -155,3 +155,7 @@ Under the `run-tests` pre-push hook, git exported `GIT_DIR` to the hook, so a ba ## 2026-11-26 — A hook's GIT_DIR leaks through any tool that runs git #204: pushing from a linked worktree left the shared `.git/config` with `core.bare=true` and the worktree on a detached HEAD at the lock-pinned commit. git exports an absolute `GIT_DIR` to hooks run from a linked worktree. `apm-audit-ci` ran `apm audit --ci`, and its drift replay (apm 0.28.0) runs `git init` and `git checkout` in a temp dir with an env copied from `os.environ`. The inherited `GIT_DIR` won, so both operations hit this repo instead of the temp dir. The 2026-11-13 fix did not cover this: it scrubbed the env only inside `tests/run-tests.sh`, and the apm hooks never pass through that file. Fix: `scripts/lib/clean-git-env.sh` unsets the repo-locating vars, and both `scripts/apm-audit-ci.sh` and the new `scripts/apm-pack-check-clean.sh` source it before calling apm. `tests/test-apm-hooks-git-env.sh` reproduces the leak with a stub `apm`. Rule: any hook that shells out to a tool that runs git must scrub `GIT_*` first. Scrubbing one entry point does not protect the others. *2026-12-02 (#272):* apm 0.39.3's `apm_cli/utils/git_env.py` provides `git_subprocess_env`, which strips `GIT_DIR` and the other repo-locating vars. The leak was not re-tested on 0.39.3, and the scrub stays, because the rule is about the hook, not about one tool version. + +## 2026-12-06 — Two correct gates can make a first landing unpushable + +#179: `check-executables-allow-sync` requires a new hook-shipping plugin's git+path entry in root `apm.yml`, while root deps resolve from the remote's `main` (ADR-0019), so `apm-audit-ci`'s `ref-consistency` fails until the plugin lands — no state of the branch passes both. Fix: a narrow, self-expiring waiver in `scripts/apm-audit-ci.sh` keyed on the plugin being absent from local `origin/main`. Rule: when adding a gate that reads the remote's `main`, check what a first landing of the thing it guards looks like. diff --git a/docs/spec/gates.md b/docs/spec/gates.md index 4daf1fb..97f84ac 100644 --- a/docs/spec/gates.md +++ b/docs/spec/gates.md @@ -72,7 +72,7 @@ version-blind, so a stale key deploys fine (see [apm gates](#apm-gates)). | Hook | Guards | |---|---| -| `apm-audit-ci` | `scripts/apm-audit-ci.sh` — `apm audit --ci` once per manifest, root plus each plugin package under `plugins/`, waiving only a package's `lockfile-exists` (see [below](#apm-audit-ci)) | +| `apm-audit-ci` | `scripts/apm-audit-ci.sh` — `apm audit --ci` once per manifest, root plus each plugin package under `plugins/`, waiving only a package's `lockfile-exists` and the root's `ref-consistency` for a plugin not yet on `origin/main` (see [below](#apm-audit-ci)) | | `apm-pack-check-clean` | `scripts/apm-pack-check-clean.sh` — `apm pack --check-versions --check-clean --dry-run` with a scrubbed git env (see [`apm-audit-ci`](#apm-audit-ci)) — the compiled marketplace still matches what `apm.yml` + `.apm/` would generate, and per-package versions agree with the `per_package` strategy | **Host validators** (needs the `claude` CLI on PATH) @@ -1232,7 +1232,22 @@ The cause is that apm treats any directory holding both `apm.yml` and `apm.lock. root**, and a plugin package is not one. `scripts/apm-audit-ci.sh` therefore waives `lockfile-exists` and nothing else, and only for a non-root manifest: it asserts the string `1 of 1 check(s) failed`, so any second failing check changes the count and the run fails normally, and output it does not -recognise fails closed. The root manifest is never waived. Recorded as ADR-0026. +recognise fails closed. The root manifest is never waived for `lockfile-exists`. Recorded as ADR-0026. + +**The root has one waiver of its own: `ref-consistency` for a plugin not yet on `origin/main`.** +Root `apm.yml`'s first-party dependencies resolve from the remote's `main` (ADR-0019), so the lock +cannot record a plugin before it lands, while `check-executables-allow-sync` refuses a push of a +hook- or bin-shipping plugin whose git+path entry is missing from root `apm.yml`. Without a waiver a +first-landing executable-shipping plugin is unpushable: root `ref-consistency` fails with +`/plugins/: not found in lockfile`. On apm 0.39.3 a failing `ref-consistency` cuts +the root run to three checks (holocron#179), so the hook waives it only when the output reads `1 of +3 check(s) failed`, the one failing row is `ref-consistency`, and every line of its details block is +that `not found in lockfile` form for an identity equal to root `apm.yml`'s own git+path entry for +`plugins/` (derived by `check-executables-allow-sync`'s rule). Each `plugins//apm.yml` +must also exist in the working tree and be absent from the **local** `origin/main`, which must +resolve; nothing is fetched. Any other output fails closed. The waiver lapses for a plugin by +itself: once it is on `origin/main`, the follow-up PR that records it in the lock is required. +`tests/test-apm-audit-ci-waiver.sh` covers it. **Dropping `--ci` for package directories was considered and rejected.** It is the smaller change and it is wrong. Verified on apm 0.28.0, and again on 0.39.3 (holocron#272), against a scratch diff --git a/scripts/apm-audit-ci.sh b/scripts/apm-audit-ci.sh index 3f7ee4c..9e3787f 100755 --- a/scripts/apm-audit-ci.sh +++ b/scripts/apm-audit-ci.sh @@ -47,7 +47,8 @@ # 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 +# 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 @@ -57,6 +58,33 @@ # 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 @@ -74,6 +102,92 @@ 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 @@ -85,7 +199,17 @@ for manifest_dir in . plugins/*/; do continue fi - # Axis 1: the root is never waived. + # 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 diff --git a/tests/test-apm-audit-ci-waiver.sh b/tests/test-apm-audit-ci-waiver.sh new file mode 100755 index 0000000..9a853bf --- /dev/null +++ b/tests/test-apm-audit-ci-waiver.sh @@ -0,0 +1,181 @@ +#!/usr/bin/env bash +# Tests scripts/apm-audit-ci.sh's root ref-consistency waiver: a plugin whose +# git+path entry is in root apm.yml but which is not yet on origin/main (#179, +# ADR-0019). See docs/spec/gates.md, "apm-audit-ci". +# +# Same seam as tests/test-apm-gate-invocation.sh: a stub `apm`, reached through +# the APM override so no uv or real apm runs. The stub prints canned `audit +# --ci` output for the root manifest ($STUB_ROOT_OUTPUT, exit 1) and passes in +# every plugin directory. The fixture is a throwaway git repo whose +# refs/remotes/origin/main is set with update-ref -- nothing is fetched. +set -euo pipefail + +REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +AUDIT="$REPO_ROOT/scripts/apm-audit-ci.sh" +PASS=0 +FAIL=0 + +pass() { echo " PASS: $1"; PASS=$((PASS + 1)); } +fail() { echo " FAIL: $1"; FAIL=$((FAIL + 1)); } + +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" +cat > "$BIN/apm" << 'EOF' +#!/usr/bin/env bash +if [[ "${1:-}" == --version ]]; then + echo "Agent Package Manager (APM) CLI version 0.39.3" + exit 0 +fi +if [[ -f apm.lock.yaml.root-marker ]]; then + printf '%s\n' "$STUB_ROOT_OUTPUT" + exit 1 +fi +exit 0 +EOF +chmod +x "$BIN/apm" + +IDENTITY="git.rkdr.net/Mortis/Holocron" + +# The output apm 0.39.3 printed for this branch's root manifest (#179). +root_output() { # + local count="$1" extra="$2" + shift 2 + printf '%s\n' \ + '[+] No drift detected' \ + '+------------------------------------------------------------------------------+' \ + '| Status | Check | Message |' \ + '|----------+-----------------+-------------------------------------------------|' \ + '| [+] | lockfile-exists | Lockfile present |' \ + "| [x] | ref-consistency | $# ref mismatch -- run 'apm install' to update |" \ + '| | | lockfile |' + if [[ -n "$extra" ]]; then printf '%s\n' "$extra"; else + printf '%s\n' '| [+] | drift | no drift detected against lockfile |' + fi + printf '%s\n' \ + '+------------------------------------------------------------------------------+' \ + '' \ + ' ref-consistency details:' + local d + for d in "$@"; do printf ' - %s\n' "$d"; done + printf '%s\n' '' "$count" +} + +# make_fixture — a repo with root apm.yml naming plugins/core and +# plugins/rtk, both plugin dirs present, origin/main at a commit holding +# plugins/core only. +make_fixture() { + local dir="$1" + mkdir -p "$dir/plugins/core" "$dir/plugins/rtk" + cat > "$dir/apm.yml" << EOF +name: fixture +dependencies: + apm: + - git: https://$IDENTITY.git + path: plugins/core + # Routes Bash tool calls through rtk. + - git: https://$IDENTITY.git + path: plugins/rtk + mcp: [] +EOF + printf 'name: core\nversion: 1.0.0\n' > "$dir/plugins/core/apm.yml" + touch "$dir/apm.lock.yaml.root-marker" + git -C "$dir" init -q + git -C "$dir" add apm.yml plugins/core/apm.yml + git -C "$dir" -c user.name=t -c user.email=t@t commit -qm base + git -C "$dir" update-ref refs/remotes/origin/main HEAD + printf 'name: rtk\nversion: 0.1.0\n' > "$dir/plugins/rtk/apm.yml" +} + +RC=0 +OUT="" +run_audit() { # + RC=0 + OUT="$(cd "$1" && env APM="$BIN/apm" STUB_ROOT_OUTPUT="$2" bash "$AUDIT" 2>&1)" || RC=$? +} + +WAIVER_LINE='apm audit --ci: waived ref-consistency for plugins/rtk (not yet on origin/main; ADR-0019)' +SOLE='[x] 1 of 3 check(s) failed' +RTK_DETAIL="$IDENTITY/plugins/rtk: not found in lockfile" + +echo "--- waived: one plugin not yet on origin/main ---" +F="$TMP/waived" +make_fixture "$F" +run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL")" +[[ $RC -eq 0 ]] && pass "exits 0" || fail "exited $RC: $OUT" +grep -qxF "$WAIVER_LINE" <<< "$OUT" && pass "prints the waiver line" || fail "no waiver line: $OUT" + +echo "" +echo "--- not waived: the plugin is already on origin/main ---" +F="$TMP/landed" +make_fixture "$F" +git -C "$F" add plugins/rtk/apm.yml +git -C "$F" -c user.name=t -c user.email=t@t commit -qm landed +git -C "$F" update-ref refs/remotes/origin/main HEAD +run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL")" +[[ $RC -ne 0 ]] && grep -qF "plugins/rtk is already on origin/main" <<< "$OUT" \ + && pass "fails, naming the landed plugin" || fail "must fail (rc=$RC): $OUT" + +echo "" +echo "--- not waived: a second check also fails ---" +F="$TMP/second" +make_fixture "$F" +run_audit "$F" "$(root_output '[x] 2 of 3 check(s) failed' \ + '| [x] | drift | 1 file(s) differ |' "$RTK_DETAIL")" +[[ $RC -ne 0 ]] && ! grep -qF "waived ref-consistency" <<< "$OUT" \ + && pass "fails when drift fails too" || fail "must fail (rc=$RC): $OUT" + +echo "" +echo "--- not waived: a mismatch outside plugins/ ---" +F="$TMP/nonplugin" +make_fixture "$F" +run_audit "$F" "$(root_output "$SOLE" "" "github.com/acme/skills: not found in lockfile")" +[[ $RC -ne 0 ]] && grep -qF "unrecognised detail line" <<< "$OUT" \ + && pass "fails on a non-plugins detail" || fail "must fail (rc=$RC): $OUT" +run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL" "github.com/acme/skills: not found in lockfile")" +[[ $RC -ne 0 ]] && ! grep -qF "waived ref-consistency" <<< "$OUT" \ + && pass "fails when an unlanded plugin is mixed with a non-plugins detail" \ + || fail "must fail on a mixed block (rc=$RC): $OUT" +run_audit "$F" "$(root_output "$SOLE" "" "$IDENTITY/plugins/rtk: ref main != abc123")" +[[ $RC -ne 0 ]] && pass "fails on a plugins/ detail of another kind" || fail "must fail (rc=$RC): $OUT" + +echo "" +echo "--- not waived: the identity is not root apm.yml's entry ---" +run_audit "$F" "$(root_output "$SOLE" "" "git.example.com/Other/Repo/plugins/rtk: not found in lockfile")" +[[ $RC -ne 0 ]] && grep -qF "is not root apm.yml's git+path entry" <<< "$OUT" \ + && pass "fails on a foreign identity" || fail "must fail (rc=$RC): $OUT" + +echo "" +echo "--- not waived: plugins//apm.yml is absent ---" +F="$TMP/absent" +make_fixture "$F" +rm "$F/plugins/rtk/apm.yml" +run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL")" +[[ $RC -ne 0 ]] && grep -qF "plugins/rtk/apm.yml is not in the working tree" <<< "$OUT" \ + && pass "fails when the plugin manifest is missing" || fail "must fail (rc=$RC): $OUT" + +echo "" +echo "--- fails closed: origin/main does not resolve ---" +F="$TMP/noorigin" +make_fixture "$F" +git -C "$F" update-ref -d refs/remotes/origin/main +run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL")" +[[ $RC -ne 0 ]] && grep -qF "origin/main does not resolve locally" <<< "$OUT" \ + && pass "fails without origin/main" || fail "must fail (rc=$RC): $OUT" + +echo "" +echo "--- fails closed: unrecognised output ---" +F="$TMP/garbled" +make_fixture "$F" +run_audit "$F" "something apm never printed" +[[ $RC -ne 0 ]] && pass "fails on output of an unknown shape" || fail "must fail (rc=$RC): $OUT" +run_audit "$F" "$(root_output "$SOLE" "")" +[[ $RC -ne 0 ]] && pass "fails on an empty details block" || fail "must fail (rc=$RC): $OUT" + +echo "" +echo "Results: $PASS passed, $FAIL failed" +[[ "$FAIL" -eq 0 ]]