#!/usr/bin/env bash
# CHECK-q158 -- tools/check_lint.py can see a leading label, and its forge
# consumer stopped pinning a cell count (volt, 2026-11-02).
#
# The defect: a CHECK written `CHECK: selftest: python3 v.py` -- a label word,
# a colon, then the command -- was graded SOUND by this rubric while bash exits
# 127 on it (`selftest:: command not found`) before running anything. Measured
# on this lane's own ralph iteration, 2026-11-02, and on the runner's
# 2026-10-27 forge log (`line: python3 ...`), which lost an iteration to it.
# The fix added the eighth class, LABEL_PREFIX. The repair that followed is the
# second half: wizard-work/forge/verify_check_lint_classes_dry.py asserted
# `selftest: 37 cells` and byte-identity of the two selftest outputs, so every
# cell check_lint gained reddened a verifier about a refactor nobody touched.
#
# Legs:
#   rubric      the selftest is green at the count the docstring claims, and the
#               baseline pin agrees with both
#   label       the two real shapes grade LABEL_PREFIX, `--fail-on-fault` exits
#               1 on them, and the cut point is printed
#   control     the three boundary shapes (colon not in the first word, colon in
#               an assignment, quoted first token) stay SOUND, so the rule is
#               anchored rather than "any colon anywhere"
#   mutant      a copy whose label rule cannot match reds its own selftest with
#               six cells failing -- the new cells are load-bearing
#   consumer    the forge verifier exits 0, reports 11/11, and its parity leg
#               prints a positive added-cell count instead of pinning 37
#   record      this item's queue record is present and names the class
#
# Exit codes: 0 all legs passed, 1 a fault, 2 could not grade.
set -u
set -o pipefail

cd "$(dirname "${BASH_SOURCE[0]}")" || exit 2
ROOT="$(pwd)"
TOOL="$ROOT/tools/check_lint.py"
FORGE="$ROOT/wizard-work/forge/verify_check_lint_classes_dry.py"
BASELINE="$ROOT/tools/selftest_baseline.tsv"
WORK="$(mktemp -d "${TMPDIR:-/tmp}/check-q158.XXXXXX")" || exit 2
trap 'rm -rf "$WORK"' EXIT

passed=0
failed=0
ungradable=0
row() { printf '  %-10s %s\n' "$1" "$2"; }
fault() { row "$1" "FAULT $2"; failed=$((failed + 1)); }

# --- leg 1: the rubric, its docstring claim, and the pinned count -------------
python3 "$TOOL" --selftest >"$WORK/self.out" 2>&1
self_rc=$?
cells="$(sed -n 's/^selftest: \([0-9][0-9]*\) cells,.*/\1/p' "$WORK/self.out" | head -1)"
grep -q "^selftest: [0-9][0-9]* cells, [0-9][0-9]* passed, 0 failed$" "$WORK/self.out"
shape_rc=$?
doc_claim="$(grep -c "^  python3 check_lint.py --selftest .* # ${cells} cells" "$TOOL" 2>/dev/null)"
pin="$(awk -F'\t' '$1 == "check_lint.py" {print $2; exit}' "$BASELINE" 2>/dev/null)"
if [ "$self_rc" -eq 0 ] && [ "$shape_rc" -eq 0 ] && [ -n "$cells" ] \
   && [ "$doc_claim" = "1" ] && [ "$pin" = "$cells" ]; then
    row rubric "PASS  $(tail -n 1 "$WORK/self.out"); docstring claim and baseline pin both $cells"
    passed=$((passed + 1))
elif [ -z "$cells" ] || [ -z "$pin" ]; then
    row rubric "UNGRADABLE no count parsed (cells='$cells' pin='$pin')"
    cat "$WORK/self.out"
    ungradable=$((ungradable + 1))
else
    fault rubric "rc=$self_rc shape_rc=$shape_rc doc_claim=$doc_claim pin=$pin cells=$cells"
    cat "$WORK/self.out"
fi

# --- leg 2: the two measured shapes are named, rejected, and cut -------------
python3 "$TOOL" --check "selftest: python3 tools/check_lint.py --selftest" \
    >"$WORK/l1.out" 2>&1
l1=$?
python3 "$TOOL" --check "line: python3 tools/check_lint.py --selftest" \
    >"$WORK/l2.out" 2>&1
l2=$?
python3 "$TOOL" --check "selftest: python3 x.py" --fail-on-fault >/dev/null 2>&1
l3=$?
if [ "$l1" -eq 0 ] && [ "$l2" -eq 0 ] \
   && grep -q '^LABEL_PREFIX:' "$WORK/l1.out" \
   && grep -q '^LABEL_PREFIX:' "$WORK/l2.out" \
   && grep -q '"command_after_label": "python3 tools/check_lint.py --selftest"' "$WORK/l1.out" \
   && [ "$l3" -eq 1 ]; then
    row label "PASS  selftest:/line: -> LABEL_PREFIX, cut point printed, --fail-on-fault rc 1"
    passed=$((passed + 1))
else
    fault label "rc($l1,$l2,$l3) want (0,0,1) with a LABEL_PREFIX verdict and a cut point"
    head -3 "$WORK/l1.out" "$WORK/l2.out"
fi

# --- leg 3: the boundary stays SOUND -----------------------------------------
bad=0
for c in "python3 x.py: --selftest" "PATH=/a:/b python3 x.py" "'note: x' cmd"; do
    python3 "$TOOL" --check "$c" --fail-on-fault >"$WORK/c.out" 2>&1 || bad=$((bad + 1))
done
if [ "$bad" -eq 0 ]; then
    row control "PASS  three boundary shapes stay SOUND under --fail-on-fault"
    passed=$((passed + 1))
else
    fault control "$bad of 3 boundary shapes were graded a fault"
    cat "$WORK/c.out"
fi

# --- leg 4: the label cells are load-bearing ---------------------------------
mkdir -p "$WORK/mut/tools"
cp "$TOOL" "$WORK/mut/tools/check_lint.py"
sed -i 's|^LABEL_RE = re.compile(.*)$|LABEL_RE = re.compile(r"^(?P<label>[A-Z][A-Z0-9_]*)")|' \
    "$WORK/mut/tools/check_lint.py"
python3 "$WORK/mut/tools/check_lint.py" --selftest >"$WORK/mut.out" 2>&1
mut_rc=$?
if [ "$mut_rc" -eq 1 ] && grep -q '^selftest: [0-9][0-9]* cells, [0-9][0-9]* passed, 6 failed$' "$WORK/mut.out"; then
    row mutant "PASS  a label rule that cannot match reds its own selftest: $(tail -n 1 "$WORK/mut.out")"
    passed=$((passed + 1))
else
    fault mutant "rc=$mut_rc (want 1 with 6 failed)"
    tail -4 "$WORK/mut.out"
fi

# --- leg 5: the consumer no longer pins a count ------------------------------
python3 "$FORGE" >"$WORK/forge.out" 2>&1
forge_rc=$?
added="$(sed -n 's/.*pair(s) all survive (\([0-9][0-9]*\) cell(s) added).*/\1/p' "$WORK/forge.out" | head -1)"
if [ "$forge_rc" -eq 0 ] && grep -q '^verify: 11 checks, 11 passed, 0 failed$' "$WORK/forge.out" \
   && [ -n "$added" ] && [ "$added" -gt 0 ]; then
    row consumer "PASS  forge verifier 11/11 rc 0, parity leg reports $added added cell(s), no pinned count"
    passed=$((passed + 1))
else
    fault consumer "rc=$forge_rc added='$added'"
    tail -4 "$WORK/forge.out"
fi

# --- leg 6: the record ------------------------------------------------------
# The block, not a fixed window: the class is named nine lines into the record,
# and an -A window made this leg red on a correct append.
q="$ROOT/WORK-QUEUE.md"
awk '/^## Q158 closure/{f=1; next} f && /^## /{f=0} f' "$q" >"$WORK/q158.md"
q_lines="$(wc -l <"$WORK/q158.md" | tr -d ' ')"
if grep -q '^## Q158 closure' "$q" && [ "${q_lines:-0}" -gt 0 ] \
   && grep -q 'LABEL_PREFIX' "$WORK/q158.md"; then
    row record "PASS  WORK-QUEUE.md carries the Q158 closure ($q_lines lines) and names LABEL_PREFIX"
    passed=$((passed + 1))
else
    fault record "the Q158 closure is missing from WORK-QUEUE.md, empty, or does not name the class (lines=$q_lines)"
fi

echo
echo "CHECK-q158: $passed leg(s) passed, $failed fault(s), $ungradable ungradable"
[ "$failed" -eq 0 ] || exit 1
[ "$ungradable" -eq 0 ] || exit 2
exit 0
