diff --git a/lib/tests/doctrine-citers.test.sh b/lib/tests/doctrine-citers.test.sh index 7ed785b..4bacae2 100644 --- a/lib/tests/doctrine-citers.test.sh +++ b/lib/tests/doctrine-citers.test.sh @@ -1,8 +1,9 @@ #!/usr/bin/env bash # lib/tests/doctrine-citers.test.sh — every citation of a CLAUDE.global.md -# section or bold label from skills/agents/lib/rules/hooks must resolve -# (BDR-100). Born from the 2026-09-24 density pass: a bold label was reworded -# and five skills kept pointing at "§ Language" for a day. A flip-test proves +# section or bold label from skills/agents/lib/rules/hooks (file citations +# also from templates/ and .claude/rules/) must resolve (BDR-100). Born from +# the 2026-09-24 density pass: a bold label was reworded and five skills kept +# pointing at "§ Language" for a day. A flip-test proves # the checker bites before the real census runs (LRN-096). # # Only citations that NAME the doctrine count: `CLAUDE.md "Section"` and @@ -10,7 +11,7 @@ # skill's own sections and are out of scope. # Since BDR-125 phase 1 doctrine also lives in rules/ and lib/: a # `rules/.md § Label` / `lib/.md § Label` citation resolves against -# that file (T4-T6). +# that file (T4-T6), nested dirs and `~/.claude/` prefixes included. set -u ROOT="$(cd "$(dirname "$0")/../.." && pwd)" pass=0; fail=0 @@ -55,26 +56,51 @@ citers_check() { # _citers_extract_files … → "file:line:target:name" per citation of a # heading in another doctrine file: `rules/.md § Label` or -# `lib/.md § Label`, backticks around the path and quotes around the -# label allowed. A quoted label runs to its closing quote (`—` included); an -# unquoted one ends at the first character that is not a letter, space or -# hyphen: quote a label that prose follows. A citation of -# `.claude/rules/.md § X` would be read as `rules/.md` (none exist; -# ERE has no lookbehind and the portability census bans grep -P). +# `lib/.md § Label` (nested dirs allowed: `lib//.md`), backticks +# around the path and quotes around the label allowed. A quoted label runs to +# its closing quote (`—` included); an unquoted one ends at the first +# character that is not a letter, space or hyphen: quote a label that prose +# follows. The path needs a left boundary (`foo-rules/x.md` is not +# `rules/x.md`). A `~/.claude/`, `$HOME/.claude/` or `${HOME}/.claude/` prefix +# maps to this repo's tree and is dropped from the target; a bare +# `.claude/` stays (see citers_check_files); `templates/project-rules/.md` +# is its own target. An absolute `/home//…` path is unsupported (read as +# a bare `.claude/…` or by its suffix). +CITE_PATH='(^|[^A-Za-z0-9_-])((~|\$[{]?HOME[}]?)/\.claude/|\.claude/)?' +CITE_PATH+='(templates/project-)?(rules|lib)/([a-z0-9-]+/)*[a-z0-9-]+\.md' +# Strip: optional boundary char (absent at column 1, never `~ $ .`) and the +# home prefix. +CITE_STRIP='s/^([^:]+:[0-9]+):[^A-Za-z0-9_~$.-]?' +CITE_STRIP+='((~|\$[{]?HOME[}]?)\/\.claude\/)?(.*\.md)`? § ?' _citers_extract_files() { - /usr/bin/grep -nHoE '(rules|lib)/[a-z0-9-]+\.md`? § ?"[^"]{2,60}"' "$@" 2>/dev/null \ - | sed -E 's/^([^:]+:[0-9]+):(.*\.md)`? § ?"([^"]+)"$/\1:\2:\3/' - /usr/bin/grep -nHoE '(rules|lib)/[a-z0-9-]+\.md`? § ?[A-Z][A-Za-z][A-Za-z -]{1,40}' "$@" 2>/dev/null \ - | sed -E 's/^([^:]+:[0-9]+):(.*\.md)`? § ?(.*)$/\1:\2:\3/; s/[[:space:]]+$//' + { + /usr/bin/grep -nHoE "$CITE_PATH"'`? § ?"[^"]{2,60}"' "$@" 2>/dev/null + /usr/bin/grep -nHoE "$CITE_PATH"'`? § ?[A-Z][A-Za-z][A-Za-z -]{1,40}' \ + "$@" 2>/dev/null + } | sed -E \ + -e "$CITE_STRIP"'"([^"]+)"$/\1:\4:\5/' \ + -e t \ + -e "$CITE_STRIP"'(.*)$/\1:\4:\5/' \ + -e 's/[[:space:]]+$//' } # citers_check_files … → DANGLING lines; rc = their count (capped 99) +# A bare `.claude/rules/.md` is a project path: cited from this repo's own +# rules (`.claude/rules/*`, `rules/*`) it is this repo's file, from anywhere +# else (skills, agents, templates…) it names the copy emitted from +# `templates/project-rules/.md`. citers_check_files() { local root="$1" line file lno target name n=0; shift while IFS= read -r line; do [ -n "$line" ] || continue file="${line%%:*}"; lno="$(printf '%s' "$line" | cut -d: -f2)" target="$(printf '%s' "$line" | cut -d: -f3)"; name="${line#*:*:*:}" + case "$target:$file" in + .claude/rules/*:.claude/rules/*|.claude/rules/*:*/.claude/rules/*|\ + .claude/rules/*:rules/*) ;; + .claude/rules/*:*) + target="templates/project-rules/${target#.claude/rules/}" ;; + esac if [ ! -f "$root/$target" ] || ! _citers_resolve "$root/$target" "$name"; then echo "DANGLING: $file:$lno → $target § \"$name\""; n=$((n+1)) fi @@ -114,14 +140,74 @@ check T5d-quoted-label-whole "$(printf '%s\n' "$out" | grep -c 'full kit')" 1 check T5b-missing-file-named "$(printf '%s\n' "$out" | grep -c 'rules/none.md')" 1 check T5c-prefix-not-a-heading "$(printf '%s\n' "$out" | grep -c '"Version"')" 1 +# T8a-T8h: blind spots of the file-citation census (nested dirs, project +# rules, home prefixes, left boundary, templates) +mkdir -p "$FIX/lib/sub" "$FIX/.claude/rules" "$FIX/templates/project-rules" +printf '## Alpha\n' > "$FIX/lib/sub/x.md" +printf 'nested: lib/sub/x.md § Alpha\n' > "$FIX/n1.md" +rows=$(_citers_extract_files "$FIX/n1.md") +citers_check_files "$FIX" "$FIX/n1.md" >/dev/null +check T8a-nested-lib-resolves "$rows|$?" "$FIX/n1.md:1:lib/sub/x.md:Alpha|0" +printf 'nested: lib/sub/x.md § Beta\n' > "$FIX/n2.md" +out=$(citers_check_files "$FIX" "$FIX/n2.md"); rc=$? +hits=$(printf '%s\n' "$out" | grep -c 'lib/sub/x.md') +check T8b-nested-lib-dangling "$rc|$hits" "1|1" +printf '## Gamma\n' > "$FIX/.claude/rules/p.md" +printf '.claude/rules/p.md § Gamma\nalso .claude/rules/p.md § Gamma.\n' \ + > "$FIX/.claude/rules/c.md" +citers_check_files "$FIX" "$FIX/.claude/rules/c.md" >/dev/null +check T8c-project-rules-resolve "$?" 0 +printf 'skill: .claude/rules/q.md § Delta.\n' > "$FIX/s.md" +printf '## Delta\n' > "$FIX/templates/project-rules/q.md" +citers_check_files "$FIX" "$FIX/s.md" >/dev/null; rc1=$? +rm "$FIX/templates/project-rules/q.md" +printf '## Delta\n' > "$FIX/rules/q.md" +printf '## Delta\n' > "$FIX/.claude/rules/q.md" +citers_check_files "$FIX" "$FIX/s.md" >/dev/null +check T8d-project-rules-not-user-rules "$rc1|$?" "0|1" +{ + printf '%s/.claude/rules/api.md § Versioning.\n' '~' + printf '%s/.claude/lib/cc.md § WRITE.\n' "\$HOME" + printf 'tilde: %s/.claude/rules/api.md § Versioning.\n' '~' + printf 'braces: %s/.claude/lib/cc.md § WRITE.\n' "\${HOME}" + printf 'quoted: %s/.claude/lib/cc.md § "WRITE (before the commit)".\n' \ + "\$HOME" +} > "$FIX/h.md" +rows=$(_citers_extract_files "$FIX/h.md" | wc -l) +citers_check_files "$FIX" "$FIX/h.md" >/dev/null +check T8e-home-prefix-maps-to-repo "$rows|$?" "5|0" +printf 'prose: foo-rules/api.md § Versioning\n' > "$FIX/b.md" +check T8g-left-boundary "$(_citers_extract_files "$FIX/b.md")" "" +printf '## Eps\n' > "$FIX/templates/project-rules/t.md" +printf 'tpl: templates/project-rules/t.md § Eps.\n' > "$FIX/t1.md" +printf 'tpl: templates/project-rules/t.md § Zeta.\n' > "$FIX/t2.md" +citers_check_files "$FIX" "$FIX/t1.md" >/dev/null; rc1=$? +citers_check_files "$FIX" "$FIX/t2.md" >/dev/null +check T8h-template-rules-path "$rc1|$?" "0|1" + # ── real census: the repo's own citers against CLAUDE.global.md ────────────── mapfile -t FILES < <(cd "$ROOT" && find skills agents lib rules hooks -type f \( -name '*.md' -o -name '*.sh' \) \ -not -path 'skills/graphify/*' -not -path 'skills/impeccable/*' -not -path 'skills/synced/*' \ -not -path 'agents/impeccable-*' -not -path 'lib/tests/*' | sort) +# T6 also scans the templates and this repo's own project rules; T3 must not +# (there `CLAUDE.md` means a project CLAUDE.md, not the doctrine). +mapfile -t FILES_ALL < <(cd "$ROOT" && find skills agents lib rules hooks \ + templates .claude/rules -type f \( -name '*.md' -o -name '*.sh' \) \ + -not -path 'skills/graphify/*' -not -path 'skills/impeccable/*' \ + -not -path 'skills/synced/*' -not -path 'agents/impeccable-*' \ + -not -path 'lib/tests/*' | sort) +# T8f: the T6 list reaches the two blind trees, the T3 list does not +all_hits=$(printf '%s\n' "${FILES_ALL[@]}" | grep -c '^templates/') +rules_hits=$(printf '%s\n' "${FILES_ALL[@]}" | grep -c '^\.claude/rules/') +t3_hits=$(printf '%s\n' "${FILES[@]}" \ + | grep -c -e '^templates/' -e '^\.claude/') +check T8f-scan-lists "$([ "$all_hits" -ge 1 ] && [ "$rules_hits" -ge 1 ] \ + && [ "$t3_hits" -eq 0 ] && echo ok)" ok out=$(cd "$ROOT" && citers_check CLAUDE.global.md "${FILES[@]}"); rc=$? [ -n "$out" ] && printf '%s\n' "$out" check T3-repo-citations-resolve "$rc" 0 -out=$(cd "$ROOT" && citers_check_files "$ROOT" "${FILES[@]}" CLAUDE.global.md); rc=$? +out=$(cd "$ROOT" && citers_check_files "$ROOT" "${FILES_ALL[@]}" \ + CLAUDE.global.md); rc=$? [ -n "$out" ] && printf '%s\n' "$out" check T6-repo-file-citations-resolve "$rc" 0 # T7: the real citers bite — the same two files against a copy of