Author SHA1 Message Date
Defame1297andClaude Opus 5.5 190d9fb322 fix(hooks): keep git -c overrides when scrubbing apm's git env
Why: the `git rev-parse --local-env-vars` loop in clean-git-env.sh also
unset GIT_CONFIG_PARAMETERS / GIT_CONFIG_COUNT, silently dropping `git -c`
overrides (extraHeader, credentials, safe.directory) that apm's remote
clones may need. Only the repo-locating vars caused #204.

Implementation Notes:
- clean-git-env.sh now unsets an explicit list only: GIT_DIR,
  GIT_WORK_TREE, GIT_INDEX_FILE, GIT_OBJECT_DIRECTORY, GIT_COMMON_DIR,
  GIT_PREFIX, GIT_IMPLICIT_WORK_TREE; gates.md updated to match.
- apm-pack-check-clean.sh points at the real gates.md paragraph.
- run-tests.sh's comment now says git exports GIT_DIR to hooks only from
  a linked worktree, matching LESSONS.md.
- test-apm-hooks-git-env.sh isolates from global/system git config.
- Reflow the "Two of these shell out to apm" paragraph in gates.md.

Refs #204

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi
2026-11-26 23:35:31 +00:00
Defame1297andClaude Opus 5.5 f2c483ca2c fix(hooks): scrub git env before apm pre-push hooks run
Why: git push from a linked worktree exports an absolute
GIT_DIR=<main>/.git/worktrees/<name> to its hooks. apm audit --ci's drift
replay (apm 0.28.0) runs git init / git checkout in a temp dir with an env
copied from os.environ, so the inherited GIT_DIR won: init wrote
core.bare=true into the shared .git/config and checkout moved the worktree's
HEAD to the lock-pinned commit. tests/run-tests.sh already scrubbed these
vars for itself; the apm hooks never pass through it.

Implementation Notes:
- scripts/lib/clean-git-env.sh unsets GIT_DIR, GIT_WORK_TREE,
  GIT_INDEX_FILE, GIT_OBJECT_DIRECTORY, GIT_COMMON_DIR, then everything
  git rev-parse --local-env-vars lists (a superset).
- scripts/apm-audit-ci.sh sources it; apm-pack-check-clean now runs via
  the new scripts/apm-pack-check-clean.sh wrapper, which sources it too.
- tests/test-apm-hooks-git-env.sh runs both hooks from a linked worktree
  of a throwaway repo against a stub apm that runs git init/checkout in a
  temp dir, asserting core.bare stays false, the worktree keeps its
  branch, and apm sees no GIT_DIR.
- LESSONS.md gets the incident entry and a correction to 2026-11-13: git
  exports GIT_DIR to hooks only from a linked worktree. gates.md documents
  the scrubbed env.

Fixes #204

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi
2026-11-26 23:21:41 +00:00
11 changed files with 196 additions and 51 deletions

No files matched your search

+2 -2
View File
@@ -173,8 +173,8 @@ repos:
- id: apm-pack-check-clean
name: apm pack --check-clean
description: Release gate -- verify .claude-plugin/marketplace.json still matches what apm.yml + .apm/ would currently generate, and that per-package versions agree with the per_package versioning strategy. Closes issue #90's deferred item 3 (a check-clean-equivalent gate) using apm's own flag instead of custom drift logic.
entry: apm pack --check-versions --check-clean --dry-run
description: Release gate -- verify .claude-plugin/marketplace.json still matches what apm.yml + .apm/ would currently generate, and that per-package versions agree with the per_package versioning strategy. Closes issue #90's deferred item 3 (a check-clean-equivalent gate) using apm's own flag instead of custom drift logic. Runs via scripts/apm-pack-check-clean.sh so apm gets a scrubbed env (issue #204).
entry: scripts/apm-pack-check-clean.sh
language: system
stages: [pre-push]
pass_filenames: false
+7 -1
View File
@@ -144,8 +144,14 @@ The guard added above (`tests/test-no-pipefail-early-exit-grep.sh`) reported the
## 2026-11-13 — Hook-exported GIT_DIR turned test fixtures loose on the real repo
Under the `run-tests` pre-push hook, git exports `GIT_DIR` to the hook, so a bats fixture's `git -C "$tmp" init/commit/update-ref` hit this repo instead of its temp dir: 84 failures, `core.bare=true` and an emptied index on every push. The 2026-07-05 "repo root is a bare checkout" entry was likely this bug's symptom, not the repo's real shape. Fix: `tests/run-tests.sh` unsets git's repo-locating env vars before any suite runs. Rule: a test that builds its own repo must never inherit the caller's git environment.
Under the `run-tests` pre-push hook, git exported `GIT_DIR` to the hook, so a bats fixture's `git -C "$tmp" init/commit/update-ref` hit this repo instead of its temp dir: 84 failures, `core.bare=true` and an emptied index on every push. The 2026-07-05 "repo root is a bare checkout" entry was likely this bug's symptom, not the repo's real shape. Fix: `tests/run-tests.sh` unsets git's repo-locating env vars before any suite runs. Rule: a test that builds its own repo must never inherit the caller's git environment.
*Corrected 2026-11-26 (#204):* git exports `GIT_DIR` to hooks only when the push runs from a **linked worktree**, where it is the absolute `<main>/.git/worktrees/<name>`. From the main checkout it exports none. So the bug reproduces only on pushes from a worktree.
## 2026-11-22 — A local-path install skips the validation a remote install runs
#205 shipped `ansible` and `k8s` as `apm.yml`-only skeletons after a scratch consumer installed all four new packages from `../pkgs/<name>` with exit 0. Fetched from the remote, the same packages fail: apm accepts a remote subdirectory only when it has `.apm/`, declared dependencies or a `skills/` bundle (`detect_package_type` in `apm_cli/models/validation.py`). `gitops` failed too, through its dependency on `k8s`. So `main` published uninstallable packages until #206 added `.apm/.gitkeep`. A green local-path install says nothing about whether consumers can install. Fix: before merging a new or restructured package, push the branch and install it with `git:`/`path:`/`ref: <branch>`, or run `validate_apm_package` on a `git archive` export. `file://` URLs are rejected in the object form, so they can't stand in for the remote.
## 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.
+18 -5
View File
@@ -73,7 +73,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-pack-check-clean` | `apm pack --check-versions --check-clean --dry-run` — the compiled marketplace still matches what `apm.yml` + `.apm/` would generate, and per-package versions agree with the `per_package` strategy |
| `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)
@@ -87,10 +87,11 @@ version-blind, so a stale key deploys fine (see [apm gates](#apm-gates)).
|---|---|
| `check-skill-version-bump` | fails if a skill directory changed since the pushed commit's merge-base with `main` without its `metadata.version` rising above both the merge-base's and `main`'s tip's (see [below](#check-skill-version-bump)) |
Two of these shell out to `apm`: `apm-audit-ci` and `apm-pack-check-clean`. The second is a bare
`apm …` entry and the first is `scripts/apm-audit-ci.sh`, which calls `apm` once per manifest, so
without the CLI on PATH the push dies on a "command not found" from inside the hook. Install with `apm-install`, or
`curl -sSL https://aka.ms/apm-unix | sh`; verify with `apm --version`.
Two of these shell out to `apm`: `apm-audit-ci` and `apm-pack-check-clean`. The second is
`scripts/apm-pack-check-clean.sh`, a one-call wrapper, and the first is `scripts/apm-audit-ci.sh`,
which calls `apm` once per manifest, so without the CLI on PATH the push dies on a "command not
found" from inside the hook. Install with `apm-install`, or `curl -sSL https://aka.ms/apm-unix | sh`;
verify with `apm --version`.
### `check-skill-version-bump`
@@ -1163,6 +1164,18 @@ Verified: a malformed dependency entry passes
`apm pack --check-versions --check-clean --dry-run` and fails `apm audit --ci` in that package's
directory. Costs ~0.5s per package.
**apm runs with git's repo-locating env scrubbed.** Both apm hooks source
`scripts/lib/clean-git-env.sh` before calling `apm`, which unsets `GIT_DIR`, `GIT_WORK_TREE`,
`GIT_INDEX_FILE`, `GIT_OBJECT_DIRECTORY`, `GIT_COMMON_DIR`, `GIT_PREFIX` and
`GIT_IMPLICIT_WORK_TREE`. It deliberately keeps the config-passing vars (`GIT_CONFIG_PARAMETERS`,
`GIT_CONFIG_COUNT`), because they carry `git -c` overrides apm's remote clones may need. The reason: `git push` from a **linked worktree** exports an
absolute `GIT_DIR=<main>/.git/worktrees/<name>` to its hooks, and from the main checkout it exports
none. `drift`'s install replay (apm 0.28.0) runs `git init` and `git checkout` in a temp dir with an
env copied from `os.environ`, so the inherited `GIT_DIR` wins. The `init` wrote `core.bare=true` into
the shared `.git/config`, and the `checkout` moved the worktree's HEAD to the lock-pinned commit
(issue #204). Pre-commit runs hooks from the worktree root, so once the vars are gone plain
discovery finds the right repo. `tests/test-apm-hooks-git-env.sh` pins this for both hooks.
**What it actually runs is asymmetric**, and the two manifest classes are not comparable. Verified by
running `apm audit --ci` (apm 0.28.0) at the repo root and in `plugins/lint/`, reading the check
names straight off its own compliance table:
+7
View File
@@ -55,6 +55,13 @@
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"
readonly WAIVED_CHECK='declares dependencies but apm.lock.yaml is absent'
readonly SOLE_FAILURE='1 of 1 check(s) failed'
+12
View File
@@ -0,0 +1,12 @@
#!/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
# 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"
exec apm pack --check-versions --check-clean --dry-run
+3 -6
View File
@@ -58,18 +58,15 @@ if ! command -v python3 >/dev/null 2>&1; then
fi
# --- Discover the agent files ---
# No `.claude/worktrees` exclusion, unlike tests/run-bats.sh: the walk starts at
# $REPO_ROOT/plugins, so a worktree nested at $REPO_ROOT/.claude/worktrees/ is
# unreachable and there is nothing to exclude. A `-not -path` glob here would
# match against ABSOLUTE paths, and when this checkout is itself a Claude
# worktree (<repo>/.claude/worktrees/<name>/) every path contains that segment,
# so it excluded every agent file and failed the derived check below (#199).
# `-not -path` mirrors tests/run-bats.sh: worktrees under .claude/ are other
# checkouts of this same repo, not additional content.
AGENT_FILES=()
while IFS= read -r f; do
[[ -n "$f" ]] && AGENT_FILES+=("$f")
done < <(
find "$REPO_ROOT/plugins" -type f -name '*.agent.md' \
-path '*/.apm/agents/*' \
-not -path '*/.claude/worktrees/*' \
2>/dev/null | sort
)
+20
View File
@@ -0,0 +1,20 @@
#!/usr/bin/env bash
# Scrubs git's repo-locating environment variables from the current shell, so
# every git process it spawns -- directly or through another tool -- discovers
# its repository from its own working directory. Issue #204.
#
# Why: `git push` from a LINKED worktree exports an absolute
# GIT_DIR=<main>/.git/worktrees/<name> to its hooks (from the main checkout it
# exports none). A hook that runs a tool which itself runs git in a temp dir --
# apm's drift replay does `git init` / `git checkout` there with os.environ --
# hands that GIT_DIR on, and the temp-dir git operates on THIS repo instead:
# core.bare=true in the shared .git/config, and the worktree's HEAD moved to a
# lock-pinned commit. Hooks run from the worktree root, so plain discovery
# finds the right repo once these are gone.
#
# Config-passing vars (GIT_CONFIG_PARAMETERS, GIT_CONFIG_COUNT) are kept on purpose: they carry `git -c` overrides apm's remote clones may need.
#
# Not meant to be executed directly -- source it before any git call.
unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_OBJECT_DIRECTORY GIT_COMMON_DIR \
GIT_PREFIX GIT_IMPLICIT_WORK_TREE
+4 -2
View File
@@ -33,8 +33,10 @@
# TEST_DIR — override root to search for test-*.sh (default: REPO_ROOT); used by tests.
set -euo pipefail
# Git exports GIT_DIR (and can export GIT_WORK_TREE / GIT_INDEX_FILE) to the
# hooks it runs, so under the run-tests pre-push hook every `git -C "$tmp" ...`
# When the push runs from a linked worktree, git exports an absolute GIT_DIR
# (and can export GIT_WORK_TREE / GIT_INDEX_FILE) to the hooks it runs (#204;
# from the main checkout it exports none), so under the run-tests pre-push
# hook every `git -C "$tmp" ...`
# a fixture runs resolves to THIS repo, not the temp one -- the
# validate-provenance-skill.bats commit_as_base helper flipped core.bare=true and
# emptied the index here on every push. Suites build their own throwaway repos;
+1 -1
View File
@@ -741,7 +741,7 @@ EXPECTED = {
'check-apm-agents-valid': (
'bash scripts/check-apm-agents-valid.sh', ['pre-push']),
'apm-pack-check-clean': (
'apm pack --check-versions --check-clean --dry-run', ['pre-push']),
'scripts/apm-pack-check-clean.sh', ['pre-push']),
'check-scope-walkup-sync': (
'bash scripts/check-scope-walkup-sync.sh', ['pre-push']),
'check-provenance-corpus': (
+122
View File
@@ -0,0 +1,122 @@
#!/usr/bin/env bash
# Regression test for issue #204: the apm pre-push hooks must not hand a
# hook-exported GIT_DIR to apm.
#
# `git push` from a LINKED worktree exports GIT_DIR=<main>/.git/worktrees/<name>
# (absolute) to its hooks; from the main checkout it exports none. apm 0.28.0's
# drift replay runs `git init` / `git checkout` in a temp dir with an env copied
# from os.environ, so the inherited GIT_DIR wins: `git init` wrote
# core.bare=true into the shared .git/config, and a later checkout moved the
# worktree's HEAD to the lock-pinned commit.
#
# The stub `apm` below reproduces that pattern -- record GIT_DIR, then `git init`
# 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.
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
export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
PASS=0
FAIL=0
pass() { echo " PASS: $1"; PASS=$((PASS + 1)); }
fail() { echo " FAIL: $1"; FAIL=$((FAIL + 1)); }
FIXTURES=()
cleanup() { [[ ${#FIXTURES[@]} -eq 0 ]] || rm -rf "${FIXTURES[@]}"; }
trap cleanup EXIT
g() { git -c user.name=fixture -c user.email=[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.
make_fixture() {
local dir="$1"
mkdir -p "$dir/bin"
g init -q -b main "$dir/main"
echo one > "$dir/main/f"
g -C "$dir/main" add f
g -C "$dir/main" commit -q -m one
echo two > "$dir/main/f"
g -C "$dir/main" commit -q -am two
g -C "$dir/main" worktree add -q -b feature "$dir/wt"
mkdir -p "$dir/wt/plugins/demo"
# The "locked sha" the replay checks out: the first commit.
local locked
locked="$(g -C "$dir/main" rev-parse HEAD~1)"
cat > "$dir/bin/apm" <<EOF
#!/usr/bin/env bash
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
rm -rf "\$tmp"
exit 0
EOF
chmod +x "$dir/bin/apm"
}
# check_hook <label> <script>
check_hook() {
local label="$1" script="$2" dir rc=0
dir="$(mktemp -d)"
FIXTURES+=("$dir")
make_fixture "$dir"
echo ""
echo "--- $label: run from a linked worktree with GIT_DIR exported ---"
if [[ ! -f "$script" ]]; then
fail "$label: $script does not exist"
return
fi
(
cd "$dir/wt"
export GIT_DIR="$dir/main/.git/worktrees/wt"
export PATH="$dir/bin:$PATH"
bash "$script"
) > "$dir/out" 2>&1 || rc=$?
if [[ "$rc" -eq 0 ]]; then
pass "$label: exits 0 when apm passes"
else
fail "$label: exited $rc; output: $(cat "$dir/out")"
fi
local bare
bare="$(git -C "$dir/main" config --get core.bare || true)"
if [[ "$bare" == "false" ]]; then
pass "$label: main repo core.bare is still false"
else
fail "$label: main repo core.bare is '$bare', expected false"
fi
local branch
branch="$(git -C "$dir/wt" symbolic-ref -q --short HEAD || echo '<detached>')"
if [[ "$branch" == "feature" ]]; then
pass "$label: worktree still on its branch"
else
fail "$label: worktree HEAD is '$branch', expected feature"
fi
if [[ ! -s "$dir/apm-saw-git-dir" ]]; then
fail "$label: stub apm never ran"
elif grep -qvxF '<unset>' "$dir/apm-saw-git-dir"; then
fail "$label: apm inherited GIT_DIR: $(sort -u "$dir/apm-saw-git-dir" | tr '\n' ' ')"
else
pass "$label: apm saw no GIT_DIR"
fi
}
check_hook apm-audit-ci "$REPO_ROOT/scripts/apm-audit-ci.sh"
check_hook apm-pack-check-clean "$REPO_ROOT/scripts/apm-pack-check-clean.sh"
echo ""
echo "Results: $PASS passed, $FAIL failed"
[[ "$FAIL" -eq 0 ]]
-34
View File
@@ -17,12 +17,6 @@ set -euo pipefail
# defect it exists to close — so the clean run's own count is asserted against
# the index rather than taken on trust.
# Fixtures below run `git init`/`git commit` in temp dirs. Under a git hook,
# git exports GIT_DIR (and can export GIT_WORK_TREE / GIT_INDEX_FILE), which
# would point every `git -C "$fixture"` at THIS repo instead (see
# tests/run-tests.sh). Never inherit the caller's repository.
unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_OBJECT_DIRECTORY GIT_COMMON_DIR
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
SCRIPT="$REPO_ROOT/scripts/check-apm-agents-valid.sh"
PASS=0
@@ -283,34 +277,6 @@ else
pass "a missing validate.sh exits 1 and names the stale path"
fi
# --- 8. A checkout that itself sits under .claude/worktrees/ discovers its agents ---
# A Claude Code worktree lives at <repo>/.claude/worktrees/<name>/, so every
# absolute path the script sees contains /.claude/worktrees/. A filter matched
# against that absolute path excluded every agent file and the gate reported
# "N of N tracked agent file(s) were not discovered" (#199).
echo ""
echo "--- a checkout under .claude/worktrees/ discovers its own agent files ---"
FIX8_PARENT="$(make_fixture)"
FIXTURES+=("$FIX8_PARENT")
FIX8="$FIX8_PARENT/.claude/worktrees/agent-x"
mkdir -p "$FIX8"
mv "$FIX8_PARENT/plugins" "$FIX8/plugins"
git -C "$FIX8" init -q
git -C "$FIX8" add -A
git -C "$FIX8" -c user.email=[email protected] -c user.name=t commit -qm "fixture"
if ! bash "$SCRIPT" "$FIX8" > "$RUN_TMP/worktree.out" 2>&1; then
fail "a checkout under .claude/worktrees/ failed — its own agent files were excluded"
sed 's/^/ /' "$RUN_TMP/worktree.out"
elif grep -q 'not a git worktree root' "$RUN_TMP/worktree.out"; then
fail "the fixture did not resolve as its own git worktree root, so the derived check never ran"
sed 's/^/ /' "$RUN_TMP/worktree.out"
elif ! grep -q 'APM agent validation passed: 1 plugin-scope' "$RUN_TMP/worktree.out"; then
fail "a checkout under .claude/worktrees/ passed but did not validate its one agent file"
sed 's/^/ /' "$RUN_TMP/worktree.out"
else
pass "a checkout under .claude/worktrees/ discovers and validates its own agent file"
fi
echo ""
echo "Results: $PASS passed, $FAIL failed"
[[ $FAIL -eq 0 ]]