diff --git a/docs/spec/gates.md b/docs/spec/gates.md index e6c8dc1..c157063 100644 --- a/docs/spec/gates.md +++ b/docs/spec/gates.md @@ -1247,22 +1247,31 @@ that `not found in lockfile` form for an identity equal to root `apm.yml`'s own must also exist in the working tree and be absent from the **local** `origin/main`, which must resolve; nothing is fetched. A plugin name must start with a letter or digit and may not contain `..`. 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. +`origin/main`, the follow-up PR that records it in the lock is required. Because the hook reads +only the local `origin/main` ref, a stale or unfetched one keeps the waiver alive after the plugin +has landed. That fails safe: the waiver still requires the full 15-check re-run below to pass, so +it waives only the `not found in lockfile` rows. `git fetch` refreshes the ref and ends the waiver. **The waiver re-runs the full root check set before it passes.** The cut-short run never reaches the other twelve root checks (`content-integrity`, `hook-approval-pins`, `local-bundle-hooks`, `exec-status-current`, `deployed-files-present` and the rest), so the three-check result alone would let a defect in any of them through. Once the conditions above hold, the hook copies the working -tree (minus `.git` and `.venv`) into a `mktemp -d` directory with `tar`. In the copy it drops only the -`dependencies:` > `apm:` items whose `path:` names a waived plugin, checks that the reduced -`apm.yml` differs from the original by those deletions alone, and runs `git init` with the same -`origin` URL, the only git state apm reads. It then runs `apm audit --ci` there. The push passes only +tree (minus `.git`, `.venv` and `.claude/worktrees`) into a `mktemp -d` directory with `tar`. In the +copy it drops only the `dependencies:` > `apm:` items whose `path:` names a waived plugin, checks +that the reduced `apm.yml` differs from the original by those deletions alone and that no entry for +a waived plugin survived, and runs `git init`. It gives that repo the git state apm reads: the +same `origin` URL, for org-policy discovery, and a copy of the real repo's `info/exclude` (from +`git rev-parse --git-path info/exclude`, so a linked worktree's shared one), which +`deployed-files-present` consults through `git check-ignore`. It then runs `apm audit --ci` there. The push passes only if that run exits 0 and prints `All 15 check(s) passed`. On this branch (holocron#179, apm 0.39.3) it does, and the hook prints `root re-run without the unlanded plugins: All 15 check(s) passed`. A copy was chosen over swapping `apm.yml` in place under a `trap`: the copy never writes the working tree, so the tree stays byte-identical even after `SIGKILL`, and a concurrent apm never sees a reduced manifest. `.git` is not copied because in a linked worktree it points into the shared gitdir. `.venv` -is not copied because apm runs from the real one via `uv run --project`. The scratch directory is +is not copied because apm runs from the real one via `uv run --project`. `.claude/worktrees` is not +copied because each worktree there is a full checkout that can carry its own `.venv`. Leaving it out +hides nothing: it is ignored through `info/exclude`, and the root checks look only at lock-recorded +deployed paths, none of which is under it. The scratch directory is removed on exit, including on `INT`, `TERM` and `HUP`. The script header records the full reasoning. `tests/test-apm-audit-ci-waiver.sh` covers the waiver, the re-run, and each of its guards. diff --git a/scripts/apm-audit-ci.sh b/scripts/apm-audit-ci.sh index ecde3a3..e4653cf 100755 --- a/scripts/apm-audit-ci.sh +++ b/scripts/apm-audit-ci.sh @@ -116,11 +116,25 @@ # reading another path cannot silently see an empty one. # * NOT .git: from a linked worktree it is a pointer file into the shared # gitdir, and a git command run in the copy could then refresh that -# worktree's real index. A fresh `git init` with the same origin URL -# stands in for it -- apm reads only the origin remote, for org-policy -# discovery, and the copy prints the same policy line the real root does. +# worktree's real index. A fresh `git init` stands in for it, carrying the +# two pieces of git state apm reads: the origin remote's URL (org-policy +# discovery; the copy prints the same policy line the real root does) and +# the repo's info/exclude, copied from `git rev-parse --git-path +# info/exclude` (which resolves to the shared gitdir in a linked worktree) +# when it exists. deployed-files-present runs `git check-ignore` on each +# lock-recorded path that is absent, and an absent path ignored only +# through info/exclude must not count as missing in the copy either. +# .gitignore files travel with the tree, and core.excludesFile is the same +# user-level config for both runs. # * NOT .venv: apm runs through `uv run --project `, so the # environment is the real one; .venv is no apm input and most of the size. +# * NOT .claude/worktrees: Claude Code's linked worktrees, each a full +# checkout that can carry its own .venv. They are ignored through +# info/exclude, and no root check reads them: deployed-files-present and +# content-integrity look up lock-recorded paths, none of which is under +# .claude/worktrees, and drift compares the replay's deployed files +# against the same paths in the tree. Leaving them out hides nothing the +# real audit would see. # Only list items under `dependencies:` > `apm:` whose `path:` names a waived # plugin are removed (comments and every other line kept), and the result is # checked to differ from the original by deletions only, each waived entry gone. @@ -276,9 +290,10 @@ trap 'exit 143' TERM HUP # 0 and reports the full root check set passing; otherwise prints why, and # apm's output, on stderr and fails. reduced_root_audit() { - local names="$1" origin name rerun rc delta + local names="$1" origin exclude name rerun rc delta SCRATCH="$(mktemp -d "${TMPDIR:-/tmp}/apm-audit-ci.XXXXXX")" || return 1 - if ! tar -C . --exclude=./.git --exclude=./.venv -cf - . | tar -C "$SCRATCH" -xf -; then + if ! tar -C . --exclude=./.git --exclude=./.venv --exclude=./.claude/worktrees -cf - . | + tar -C "$SCRATCH" -xf -; then echo 'apm audit --ci: ref-consistency not waived: could not copy the tree for the re-run' >&2 return 1 fi @@ -309,6 +324,15 @@ reduced_root_audit() { if origin="$(git remote get-url origin 2>/dev/null)"; then git -C "$SCRATCH" remote add origin "$origin" || return 1 fi + # The real repo's ignore rules for deployed-files-present (header, "NOT + # .git"). --git-path resolves to the shared gitdir from a linked worktree. + exclude="$(git rev-parse --path-format=absolute --git-path info/exclude 2>/dev/null)" || exclude="" + if [ -n "$exclude" ] && [ -f "$exclude" ]; then + if ! mkdir -p "$SCRATCH/.git/info" || ! cp "$exclude" "$SCRATCH/.git/info/exclude"; then + echo 'apm audit --ci: ref-consistency not waived: could not copy info/exclude into the re-run copy' >&2 + return 1 + fi + fi rc=0 rerun="$(cd "$SCRATCH" && run_apm audit --ci 2>&1)" || rc=$? if [ "$rc" -ne 0 ] || ! grep -qF "$ROOT_FULL_PASS" <<<"$rerun"; then diff --git a/tests/test-apm-audit-ci-waiver.sh b/tests/test-apm-audit-ci-waiver.sh index 769c158..b810248 100755 --- a/tests/test-apm-audit-ci-waiver.sh +++ b/tests/test-apm-audit-ci-waiver.sh @@ -9,7 +9,9 @@ # every plugin directory. The waiver's re-run is told apart by where it runs: # under $TMPDIR, the script's mktemp scratch copy, where the stub saves the manifest it was given ($STUB_RERUN_LOG) and prints # $STUB_RERUN_OUTPUT (default: the full root pass) with exit $STUB_RERUN_RC -# (default 0). The fixture is a throwaway git repo whose +# (default 0). With $STUB_GIT_LOG set it also saves the copy's +# .git/info/exclude and notes whether .claude/worktrees was copied. The fixture +# is a throwaway git repo whose # refs/remotes/origin/main is set with update-ref -- nothing is fetched. set -euo pipefail @@ -40,6 +42,10 @@ if [[ -f apm.lock.yaml.root-marker ]]; then exit 1 fi [[ -z "${STUB_RERUN_LOG:-}" ]] || cp apm.yml "$STUB_RERUN_LOG" + if [[ -n "${STUB_GIT_LOG:-}" ]]; then + cp .git/info/exclude "$STUB_GIT_LOG.exclude" 2> /dev/null || true + [[ ! -e .claude/worktrees ]] || touch "$STUB_GIT_LOG.worktrees" + fi printf '%s\n' "${STUB_RERUN_OUTPUT-[*] All 15 check(s) passed}" exit "${STUB_RERUN_RC:-0}" fi @@ -141,8 +147,58 @@ cmp -s "$TMP/apm.yml.orig" "$F/apm.yml" && [[ -z "$(git -C "$F" status --porcela && pass "leaves the working tree's apm.yml byte-identical" || fail "apm.yml changed" tmp_clean && pass "removes the scratch copy" || fail "scratch left behind: $(ls -A "$TMPD")" +echo "" +echo "--- the re-run copy: carries info/exclude, leaves out .claude/worktrees ---" +F="$TMP/excl" +make_fixture "$F" +printf '%s\n' '# fixture marker' '**/.claude/worktrees/' >> "$F/.git/info/exclude" +mkdir -p "$F/.claude/worktrees/wt/.venv" +touch "$F/.claude/worktrees/wt/.venv/big" +run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL")" STUB_GIT_LOG="$TMP/gitlog" +[[ $RC -eq 0 ]] && pass "still waived" || fail "exited $RC: $OUT" +cmp -s "$F/.git/info/exclude" "$TMP/gitlog.exclude" \ + && pass "the copy's .git/info/exclude is the repo's" || fail "info/exclude not carried into the copy" +[[ ! -e "$TMP/gitlog.worktrees" ]] && pass ".claude/worktrees is not copied" || fail ".claude/worktrees was copied" + +echo "" +echo "--- not waived: the reduction changes a line, not only deletes (deletions-only guard) ---" +F="$TMP/nonl" +make_fixture "$F" +# No trailing newline: awk prints one back, so the reduced manifest's last +# line differs from the original's, a change rather than a deletion. +printf '%s' "$(cat "$F/apm.yml")" > "$F/apm.yml.new" && mv "$F/apm.yml.new" "$F/apm.yml" +run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL")" +[[ $RC -ne 0 ]] && ! grep -qF "waived ref-consistency" <<< "$OUT" \ + && grep -qF "changed more than the waived entries" <<< "$OUT" \ + && pass "fails when the reduced apm.yml differs by more than deletions" || fail "must fail (rc=$RC): $OUT" + +echo "" +echo "--- not waived: the reduction leaves an rtk entry in place ---" +F="$TMP/blank" +make_fixture "$F" +# A blank line inside the rtk item: dep_identity skips it and still finds the +# entry, but reduce_manifest ends the item there and keeps it whole. +sed -i '/# Routes Bash/{n;s|$|\n|}' "$F/apm.yml" +grep -qx '' "$F/apm.yml" || fail "fixture: no blank line in the rtk item" +run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL")" +[[ $RC -ne 0 ]] && ! grep -qF "waived ref-consistency" <<< "$OUT" \ + && pass "fails when nothing was removed" || fail "must fail (rc=$RC): $OUT" +# A second, blank-split rtk item after a normal one: the normal one is +# removed, a pure deletion, so only the post-reduce dep_identity guard sees the +# entry that survived. +F="$TMP/dup" +make_fixture "$F" +awk -v e=" - git: https://$IDENTITY.git"$'\n\n'" path: plugins/rtk" \ + '/^ mcp:/ { print e } { print }' "$F/apm.yml" > "$F/apm.yml.new" +mv "$F/apm.yml.new" "$F/apm.yml" +run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL")" +[[ $RC -ne 0 ]] && ! grep -qF "waived ref-consistency" <<< "$OUT" \ + && grep -qF "could not remove plugins/rtk's entry" <<< "$OUT" \ + && pass "fails when an rtk entry survives a deletions-only reduction" || fail "must fail (rc=$RC): $OUT" + echo "" echo "--- not waived: the full-set re-run fails ---" +F="$TMP/waived" run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL")" STUB_RERUN_RC=1 \ STUB_RERUN_OUTPUT="$(printf '%s\n' '| [x] | content-integrity | hidden Unicode |' '[x] 1 of 15 check(s) failed')" [[ $RC -ne 0 ]] && ! grep -qF "waived ref-consistency" <<< "$OUT" \