#!/usr/bin/env bash
# CHECK-q155 -- the 2026-11-02 workqueue census graded the queue it described,
# and now does so from bytes it can name.
#
# THE DEFECT THIS GRADES (measured at 0ec1c64, before this tick's edits)
#   `wizard-work/ledger/2026-11-02-workqueue-census/CHECK` was a Python program
#   with a `python3` shebang, so `bash CHECK` -- the invocation every caller in
#   this tree uses -- read rc 2 on shell syntax errors before grading a cell.
#   Behind that, it re-derived its rows from the LIVE WORK-QUEUE.md: the queue
#   is append-only, so the next item to land reddened a census whose 88 rows
#   were never wrong. Measured in a scratch copy: baseline rc 0; one appended
#   `## Q199 -- ...` heading -> rc 1, three legs failing (row count, headings,
#   manifest.rows). And the manifest carried no revision at all, so nothing said
#   which queue state the rows described; the README's stated source sha256
#   (`cea6cd3c...`) did not match the file it named (`ceb6cd3c...`).
#
# WHAT THIS GRADES
#   1. gate    bash CHECK -> rc 0 on the committed artifact, and it names its pin
#   2. probes  probe_census.sh -> 13/13 cells read the code they must: mutated
#              Title/Q cells rc 1, corrupted digest rc 1, pin revision that does
#              not hold the blob rc 1, absent blob/TSV/grader rc 2, a queue
#              append absorbed as ADVISORY at rc 0, a dirty extractor refused at
#              rc 1, and --allow-dirty's unpinned manifest left ungradable at rc 2
#   3. lint    the documented invocation is a SOUND CHECK line (tools/check_lint)
#
#   rc 0 every leg holds   rc 1 a leg failed   rc 2 could not grade
set -u
cd "$(dirname "${BASH_SOURCE[0]}")" || exit 2
ROOT="$(pwd)"
DIR="wizard-work/ledger/2026-11-02-workqueue-census"
GATE="$DIR/CHECK"
PROBES="$DIR/probe_census.sh"
for f in "$GATE" "$PROBES" "$DIR/grade_census.py"; do
  [ -f "$f" ] || { echo "CANNOT GRADE: $f is missing"; exit 2; }
done

legs=0
faults=0
leg() { # leg <name> <rc> <detail>
  legs=$((legs + 1))
  if [ "$2" -eq 0 ]; then printf 'PASS %-8s %s\n' "$1" "$3"
  else printf 'FAIL %-8s %s\n' "$1" "$3"; faults=$((faults + 1)); fi
}

out="$(bash "$GATE" 2>&1)"; rc=$?
leg gate "$rc" "$(printf '%s' "$out" | tail -n 1)"

out="$(bash "$PROBES" 2>&1)"; rc=$?
leg probes "$rc" "$(printf '%s' "$out" | tail -n 1)"

out="$(python3 tools/check_lint.py --check "bash $GATE" 2>&1)"; rc=$?
leg lint "$rc" "$(printf '%s' "$out" | tail -n 1)"

if [ "$faults" -eq 0 ]; then
  echo "q155: $legs leg(s), all passed"
  exit 0
fi
echo "q155: $legs leg(s), $faults fault(s)"
exit 1
