diff --git a/scripts/check-apm-agents-valid.sh b/scripts/check-apm-agents-valid.sh index 5ef905f..b121795 100755 --- a/scripts/check-apm-agents-valid.sh +++ b/scripts/check-apm-agents-valid.sh @@ -58,15 +58,18 @@ if ! command -v python3 >/dev/null 2>&1; then fi # --- Discover the agent files --- -# `-not -path` mirrors tests/run-bats.sh: worktrees under .claude/ are other -# checkouts of this same repo, not additional content. +# 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 (/.claude/worktrees//) every path contains that segment, +# so it excluded every agent file and failed the derived check below (#199). 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 ) diff --git a/tests/test-check-apm-agents-valid.sh b/tests/test-check-apm-agents-valid.sh index 0739d53..37148c2 100755 --- a/tests/test-check-apm-agents-valid.sh +++ b/tests/test-check-apm-agents-valid.sh @@ -17,6 +17,12 @@ 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 @@ -277,6 +283,34 @@ 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 /.claude/worktrees//, 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=t@example.invalid -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 ]]