#!/usr/bin/env bash # Enforce documentation word budgets. # # Every doc an agent or contributor reads costs context. Budgets keep that cost bounded and # force edits to delete as much as they add. See AGENTS.md §11 and CONTRIBUTING.md. # # Usage: ./scripts/check-docs.sh [--list] # Exit: 0 = within budget, 1 = over budget or unbudgeted doc found. set -uo pipefail cd "$(dirname "$0")/.." || exit 1 # path -> word ceiling. Raise a budget only with the maintainer's explicit approval. declare -A BUDGET=( ["AGENTS.md"]=1450 ["AI_POLICY.md"]=800 ["CONTRIBUTING.md"]=950 ["LICENSING.md"]=950 ["README.md"]=450 ["docs/ROADMAP.md"]=4600 ["docs/ARCHITECTURE.md"]=1900 ["docs/BACKLOG.md"]=2900 ["docs/FEATURE-INVENTORY.md"]=2100 ["docs/PRIOR-ART.md"]=1650 ["docs/device/nakamichi-nam5260.md"]=900 ["docs/device/perodua-viva-elite-2011.md"]=750 ["docs/adr/0001-reference-material-outside-repo.md"]=550 ["docs/adr/0002-target-hardware.md"]=550 ["docs/adr/0003-projection-apps-are-complements.md"]=550 ["docs/adr/0004-licensing.md"]=550 ["docs/adr/0005-design-system.md"]=600 ) TOTAL_BUDGET=22000 if [ "${1:-}" = "--list" ]; then for f in "${!BUDGET[@]}"; do printf "%-50s %5s\n" "$f" "${BUDGET[$f]}"; done | sort exit 0 fi red() { printf '\033[31m%s\033[0m\n' "$*"; } warn() { printf '\033[33m%s\033[0m\n' "$*"; } fail=0 total=0 printf "%-50s %6s %6s %6s\n" FILE WORDS BUDGET USED printf "%-50s %6s %6s %6s\n" ---- ----- ------ ---- for f in $(printf '%s\n' "${!BUDGET[@]}" | sort); do if [ ! -f "$f" ]; then red "MISSING $f (budgeted but absent — remove it from the budget table)" fail=1 continue fi words=$(wc -w < "$f" | tr -d ' ') max=${BUDGET[$f]} total=$((total + words)) pct=$(( words * 100 / max )) line=$(printf "%-50s %6s %6s %5s%%" "$f" "$words" "$max" "$pct") if [ "$words" -gt "$max" ]; then printf '\033[31m%s OVER by %s\033[0m\n' "$line" "$((words - max))" fail=1 elif [ "$pct" -ge 95 ]; then printf '\033[33m%s (hot)\033[0m\n' "$line" else printf '%s\n' "$line" fi done # Every markdown file must be budgeted, or the budget is decorative. while IFS= read -r f; do if [ -z "${BUDGET[$f]+x}" ]; then red "UNBUDGETED $f — add a budget here or fold the content into an existing doc" fail=1 fi done < <(find . -name '*.md' -not -path './.git/*' -not -path './build/*' | sed 's|^\./||' | sort) echo printf "%-50s %6s %6s %5s%%\n" TOTAL "$total" "$TOTAL_BUDGET" "$(( total * 100 / TOTAL_BUDGET ))" if [ "$total" -gt "$TOTAL_BUDGET" ]; then red "TOTAL OVER by $((total - TOTAL_BUDGET)) words" fail=1 fi if [ "$fail" -ne 0 ]; then echo red "FAIL — cut or merge text before adding more." exit 1 fi echo "OK — all documents within budget."