#!/usr/bin/env bash
# CHECK-q154 -- quill's draft-compliance census is a snapshot, and it is graded
# against the revision it names (volt, 2026-11-02).
#
# Five legs:
#   gate        the repaired quill gate reads 0: selftest / pinned / rev-probe /
#               regenerate / copy-parity
#   rev-probe   the verifier's revision mode is a measurement: the pinned rev is
#               0, 8847c7c -- the commit that hand-edited the table -- is 1, and
#               a rev that does not resolve is 2, not a pass
#   delegation  the lane copy is a pointer to the root gate (0 in place) and a
#               copy without its target is ungradable (2), never a pass
#   beam        the adjacent host-path binding the repo gate was red on is
#               repaired: the authority reads 0 faults, and the test file in a
#               copy grades the module BESIDE it (poisoned sibling 1, no sibling 1)
#   pin         the pin names a rev that resolves, the committed table is the
#               bytes it names, and the table carries its header row
#
# 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)"
LANE="$ROOT/wizard-work/quill"
PIN="$LANE/draft-compliance-report-2026-10-31.pin"
TSV="$LANE/draft-compliance-report-2026-10-31.tsv"
WORK="$(mktemp -d "${TMPDIR:-/tmp}/check-q154.XXXXXX")" || exit 2
trap 'rm -rf "$WORK"' EXIT

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

PIN_REV="$(sed -n 's/^rev[[:space:]]*//p' "$PIN" 2>/dev/null | head -1)"
PIN_TSV="$(sed -n 's/^tsv[[:space:]]*//p' "$PIN" 2>/dev/null | head -1)"

# --- leg 1: the quill gate ---------------------------------------------------
bash "$ROOT/CHECK-quill-draft-compliance" >"$WORK/gate.out" 2>&1
gate_rc=$?
if [ "$gate_rc" -eq 0 ]; then
    row gate "PASS  $(tail -n 1 "$WORK/gate.out")"
    passed=$((passed + 1))
elif [ "$gate_rc" -eq 2 ]; then
    row gate "UNGRADABLE rc=2"
    cat "$WORK/gate.out"
    ungradable=$((ungradable + 1))
else
    row gate "FAULT rc=$gate_rc"
    cat "$WORK/gate.out"
    failed=$((failed + 1))
fi

# --- leg 2: revision mode is a measurement ----------------------------------
if [ -z "$PIN_REV" ]; then
    row rev-probe "UNGRADABLE pin names no rev"
    ungradable=$((ungradable + 1))
else
    python3 "$LANE/verify_draft_compliance.py" --rev "$PIN_REV" >"$WORK/r1.out" 2>&1
    r1=$?
    python3 "$LANE/verify_draft_compliance.py" --rev 8847c7c >"$WORK/r2.out" 2>&1
    r2=$?
    python3 "$LANE/verify_draft_compliance.py" --rev 0000000000000000000000000000000000000000 \
        >"$WORK/r3.out" 2>&1
    r3=$?
    if [ "$r1" -eq 0 ] && [ "$r2" -eq 1 ] && [ "$r3" -eq 2 ]; then
        row rev-probe "PASS  ${PIN_REV:0:7} rc=0, 8847c7c rc=1, bogus rev rc=2"
        passed=$((passed + 1))
    else
        row rev-probe "FAULT pinned rc=$r1 (want 0), 8847c7c rc=$r2 (want 1), bogus rc=$r3 (want 2)"
        head -3 "$WORK/r1.out" "$WORK/r2.out" "$WORK/r3.out"
        failed=$((failed + 1))
    fi
fi

# --- leg 3: the lane copy delegates, and says so when it cannot -------------
bash "$LANE/CHECK-quill-draft-compliance" >"$WORK/d1.out" 2>&1
d1=$?
mkdir -p "$WORK/lone/wizard-work/quill"
cp "$LANE/CHECK-quill-draft-compliance" "$WORK/lone/wizard-work/quill/"
bash "$WORK/lone/wizard-work/quill/CHECK-quill-draft-compliance" >"$WORK/d2.out" 2>&1
d2=$?
if [ "$d1" -eq 0 ] && [ "$d2" -eq 2 ]; then
    row delegation "PASS  lane copy rc=0, orphaned copy rc=2 ($(head -n 1 "$WORK/d2.out"))"
    passed=$((passed + 1))
else
    row delegation "FAULT lane copy rc=$d1 (want 0), orphaned rc=$d2 (want 2)"
    head -3 "$WORK/d1.out" "$WORK/d2.out"
    failed=$((failed + 1))
fi

# --- leg 4: the adjacent beam binding, and that it is really bound ----------
python3 "$ROOT/tools/import_resolve_check.py" --repo "$ROOT" --rev WORKTREE \
    >"$WORK/irc.out" 2>&1
irc=$?
mkdir -p "$WORK/b/copy" "$WORK/b/alone"
cp "$ROOT/wizard-work/beam/test_largest_rect_hist.py" \
   "$ROOT/wizard-work/beam/largest_rect_hist.py" "$WORK/b/copy/"
( cd "$WORK/b/copy" && python3 test_largest_rect_hist.py >/dev/null 2>&1 )
b_ok=$?
sed -i 's/^def _brute(heights: List\[int\]) -> int:/&\n    return 0/' \
    "$WORK/b/copy/largest_rect_hist.py"
( cd "$WORK/b/copy" && python3 test_largest_rect_hist.py >/dev/null 2>&1 )
b_poison=$?
cp "$ROOT/wizard-work/beam/test_largest_rect_hist.py" "$WORK/b/alone/"
( cd "$WORK/b/alone" && python3 test_largest_rect_hist.py >/dev/null 2>&1 )
b_alone=$?
if [ "$irc" -eq 0 ] && [ "$b_ok" -eq 0 ] && [ "$b_poison" -eq 1 ] && [ "$b_alone" -eq 1 ]; then
    row beam "PASS  authority 0 fault(s); sibling rc=0, poisoned rc=1, absent rc=1"
    passed=$((passed + 1))
else
    row beam "FAULT authority rc=$irc, sibling rc=$b_ok (want 0), poisoned rc=$b_poison (want 1), absent rc=$b_alone (want 1)"
    tail -3 "$WORK/irc.out"
    failed=$((failed + 1))
fi

# --- leg 5: the pin's three claims ------------------------------------------
if [ -z "$PIN_REV" ] || [ -z "$PIN_TSV" ]; then
    row pin "UNGRADABLE pin lacks a rev or a tsv digest"
    ungradable=$((ungradable + 1))
else
    git -C "$ROOT" cat-file -e "$PIN_REV^{commit}" 2>/dev/null
    pin_rev_rc=$?
    have="$(sha256sum "$TSV" | cut -d' ' -f1)"
    header="$(sed -n 2p "$TSV")"
    want_header="$(printf 'filename	lines	has_verdict	has_check	check_clean	check_command')"
    if [ "$pin_rev_rc" -eq 0 ] && [ "$have" = "$PIN_TSV" ] && [ "$header" = "$want_header" ]; then
        row pin "PASS  rev resolves, sha256 ${have:0:12}, header row present"
        passed=$((passed + 1))
    else
        row pin "FAULT rev rc=$pin_rev_rc, digest ${have:0:12} vs pin ${PIN_TSV:0:12}, header=$(printf %q "$header")"
        failed=$((failed + 1))
    fi
fi

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