From 363fa90f8f5a49da1e1075244870f0019a712d04 Mon Sep 17 00:00:00 2001 From: bchanot Date: Thu, 29 Oct 2026 13:08:36 +0100 Subject: [PATCH] =?UTF-8?q?test(census):=20resolve=20rules/=20and=20lib/?= =?UTF-8?q?=20=C2=A7=20citations=20against=20their=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tests/doctrine-citers.test.sh | 65 +++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/lib/tests/doctrine-citers.test.sh b/lib/tests/doctrine-citers.test.sh index 7281874..7ed785b 100644 --- a/lib/tests/doctrine-citers.test.sh +++ b/lib/tests/doctrine-citers.test.sh @@ -8,6 +8,9 @@ # Only citations that NAME the doctrine count: `CLAUDE.md "Section"` and # `CLAUDE.md … § Label`. Bare `section "…"` / `§ X` inside a skill refer to the # 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). set -u ROOT="$(cd "$(dirname "$0")/../.." && pwd)" pass=0; fail=0 @@ -50,6 +53,35 @@ citers_check() { return $(( n > 99 ? 99 : n )) } +# _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). +_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:]]+$//' +} + +# citers_check_files … → DANGLING lines; rc = their count (capped 99) +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#*:*:*:}" + if [ ! -f "$root/$target" ] || ! _citers_resolve "$root/$target" "$name"; then + echo "DANGLING: $file:$lno → $target § \"$name\""; n=$((n+1)) + fi + done < <(_citers_extract_files "$@") + return $(( n > 99 ? 99 : n )) +} + # ── flip-test: a synthetic dangling citation must be caught ────────────────── FIX="$(mktemp -d)"; trap 'rm -rf "$FIX"' EXIT # shellcheck disable=SC2016 # literal backticks in the fixture heading @@ -65,6 +97,22 @@ out=$(citers_check "$FIX/doctrine.md" "$FIX/bad.md"); rc=$? check T2-dangling-section-and-label-caught "$rc" 2 check T2b-names-the-culprit "$(printf '%s\n' "$out" | grep -c 'bad.md:1')" 2 check T2c-label-named "$(printf '%s\n' "$out" | grep -c '"Language"')" 1 +mkdir -p "$FIX/rules" "$FIX/lib" +printf '## Versioning\nx\n' > "$FIX/rules/api.md" +printf '## WRITE (before the commit)\n' > "$FIX/lib/cc.md" +# shellcheck disable=SC2016 # literal backticks in the fixture +printf 'ok: see `rules/api.md` § Versioning, and lib/cc.md § WRITE (x)\n' > "$FIX/fgood.md" +printf 'quoted: rules/api.md § "Versioning" first\n' >> "$FIX/fgood.md" +printf '## Design work — full toolchain (tiered)\n' > "$FIX/rules/web.md" +printf 'dash: rules/web.md § "Design work — full toolchain" first\n' >> "$FIX/fgood.md" +printf 'bad: rules/api.md § "Version", rules/none.md § Alpha, lib/cc.md § Read\n' > "$FIX/fbad.md" +printf 'dash: rules/web.md § "Design work — full kit" first\n' >> "$FIX/fbad.md" +citers_check_files "$FIX" "$FIX/fgood.md" >/dev/null; check T4-file-citations-resolve "$?" 0 +out=$(citers_check_files "$FIX" "$FIX/fbad.md"); rc=$? +check T5-file-dangling-caught "$rc" 4 +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 # ── 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' \) \ @@ -73,6 +121,23 @@ mapfile -t FILES < <(cd "$ROOT" && find skills agents lib rules hooks -type f \( 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=$? +[ -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 +# rules/rest-api.md whose heading is renamed (never edit the live rules/) +mkdir -p "$FIX/copy/rules" +cp "$ROOT/rules/rest-api.md" "$FIX/copy/rules/rest-api.md" +(cd "$ROOT" && citers_check_files "$FIX/copy" lib/architecture-defaults.md \ + lib/project-archetypes/rest-api-node.md) >/dev/null +check T7a-real-citers-control "$?" 0 +sed 's/^## Versioning$/## Version policy/' "$ROOT/rules/rest-api.md" > "$FIX/copy/rules/rest-api.md" +(cd "$ROOT" && citers_check_files "$FIX/copy" lib/architecture-defaults.md \ + lib/project-archetypes/rest-api-node.md) >/dev/null +check T7b-real-rename-bites "$?" 2 +# the hook's pointer is checked whole, not as its "Design work" prefix +check T7c-hook-label-whole "$(cd "$ROOT" && _citers_extract_files hooks/design-toolchain-reminder.sh \ + | grep -c ':rules/web-building.md:Design work — full toolchain$')" 2 echo "PASS=$pass FAIL=$fail" [ "$fail" -eq 0 ]