#!/usr/bin/env bash
# CHECK-q156 -- the Q85 landing left the gate red in three legs; repaired, and
# the three legs plus the record that says so are graded here.
#
# WHAT WAS WRONG (measured at 8e08516, before this tick's edits)
#   c05f05a appended a `## Q85 closure` heading and a `**Q85 — DONE ...` record
#   to WORK-QUEUE.md and shipped neither the re-rendered docs/queue-index.md
#   nor the PIN_COUNTS move. tools/verify.py --fast read 12 legs, 8 pass,
#   3 fault (index-current, append-audit, open-set), gate FAULT, rc 1; each leg
#   read alone was rc 1 with STALE at line 7 / `STALE c05f05a index:no` /
#   headings 102 -> 103, records 116 -> 117, record_ids 82 -> 83.
#
# WHAT THIS GRADES
#   1. gate          tools/verify.py --fast -- 12 legs, 0 fault, rc 0.
#   2. open-set      open_set.py --check -- the PIN_COUNTS match this queue.
#   3. index         queue_index.py --check -- the rendered index matches it.
#   4. append-guard  queue_append_audit.py --guard --to HEAD -- the newest
#                    queue-touching commit left the index current in its OWN
#                    tree. This leg is why the repair had to touch the queue
#                    and the index in one commit: the guard grades the newest
#                    queue-touching commit, so a repair commit that skipped the
#                    queue would still leave c05f05a under the guard.
#   5. record        the Q156 record is in WORK-QUEUE.md and the Q156/Q85 pin
#                    comment is in open_set.py -- drop either and this reds
#                    instead of leaving a silent record.
#   6. lint          check_lint.py --check "bash CHECK-q156" -- SOUND, so the
#                    line a runner extracts is the one this file runs.
#
#   rc 0 every leg holds   rc 1 a leg failed   rc 2 could not grade
set -u
ROOT="$(git -C "$(dirname "$0")" rev-parse --show-toplevel 2>/dev/null)" || exit 2
[ -n "$ROOT" ] || exit 2
cd "$ROOT" || exit 2

QUEUE=WORK-QUEUE.md
INDEX=docs/queue-index.md
PINS=wizard-work/volt/q137-open-set/open_set.py
for f in "$QUEUE" "$INDEX" "$PINS" tools/verify.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 %-12s %s\n' "$1" "$3"
  else printf 'FAIL %-12s %s\n' "$1" "$3"; faults=$((faults + 1)); fi
}

out="$(python3 tools/verify.py --fast 2>&1)"; rc=$?
leg gate "$rc" "$(printf '%s' "$out" | grep -m1 -E '^(gate|verify):' | tr -d '\n')"

out="$(python3 wizard-work/volt/q137-open-set/open_set.py --repo . --check 2>&1)"; rc=$?
leg open-set "$rc" "$(printf '%s' "$out" | grep -m1 '^CHECK ')"

out="$(python3 tools/queue_index.py --check "$INDEX" 2>&1)"; rc=$?
leg index "$rc" "$(printf '%s' "$out" | grep -m1 '^index current' )"

out="$(python3 tools/queue_append_audit.py --guard --to HEAD 2>&1)"; rc=$?
leg append-guard "$rc" "$(printf '%s' "$out" | grep -m1 '^verdict')"

grep -q '^## Q156 closure' "$QUEUE" && grep -q '^# Q85 (2026-11-02)' "$PINS" \
  && grep -q '^# Q156 (2026-11-02)' "$PINS"
leg record "$?" "$(grep -c '^## Q156 closure' "$QUEUE") Q156 heading(s) in $QUEUE"

out="$(python3 tools/check_lint.py --check "bash CHECK-q156" 2>&1)"; rc=$?
leg lint "$rc" "$(printf '%s' "$out" | grep -m1 -E 'SOUND|FAULT' | cut -c1-60)"

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