#!/usr/bin/env bash
# CHECK -- the Q168 repair: the append audit now grades the queue's own PIN, in
# each commit's own tree, beside the rendered index.
#
# `tools/queue_append_audit.py` graded one axis: was `docs/queue-index.md`
# re-rendered in the commit that appended to `WORK-QUEUE.md`? `56d3853` is the
# counter-example the repo gate paid for -- it re-rendered its index (PASS,
# rc 0) and left `open_set.py`'s PIN_COUNTS two headings behind, which the
# open-set leg only read two commits later. Five legs:
#   selftest  the tool's own cells pass, and there are 43 of them
#   gate      the newest queue-touching commit is clean on BOTH axes
#   axes      the historic commit reads index PASS, pin DRIFT, rc 1 -- and the
#             same commit read --axis index alone reads rc 0, so the red is the
#             pin axis and not the range
#   preaxis   the tool AS IT STOOD BEFORE THE AXIS reads rc 0 over that same
#             range, so the changed reading is the axis and not the history
#   axis      --axis pin grades the pin alone and sets no index verdict
# rc 0 clean, rc 1 a fault it graded, rc 2 it could not grade at all.
set -u

ROOT="$(cd "$(dirname "$0")" && pwd)"
TOOL="tools/queue_append_audit.py"
PIN_TOOL="wizard-work/volt/q137-open-set/open_set.py"
PRE_AXIS_REV="12fb1b0"      # the last commit before the pin axis was written
DRIFT_REV="56d3853"         # index PASS, pin DRIFT: the shape the axis exists for
DRIFT_PARENT="1c3c855"
DRIFT_COUNT="count headings moved 116 -> 117"
WANT_CELLS=43

faults=0
leg() { printf '%-9s %s\n' "$1" "$2"; }

for f in "$ROOT/$TOOL" "$ROOT/$PIN_TOOL"; do
    if [ ! -f "$f" ]; then
        echo "append-pin: missing $f; cannot grade" >&2
        exit 2
    fi
done
if ! git -C "$ROOT" rev-parse --git-dir >/dev/null 2>&1; then
    echo "append-pin: $ROOT is not a git repository; no commit can be graded" >&2
    exit 2
fi
if ! git -C "$ROOT" cat-file -e "$PRE_AXIS_REV:$TOOL" 2>/dev/null; then
    echo "append-pin: $PRE_AXIS_REV has no $TOOL; the pre-axis reading cannot be taken" >&2
    exit 2
fi

TMP="$(mktemp -d "${TMPDIR:-/tmp}/append-pin.XXXXXX")" || exit 2
trap 'rm -rf "$TMP"' EXIT

# selftest -- the tool's own cells, including the six pin-axis cells
out="$(cd "$ROOT" && python3 "$TOOL" --selftest 2>&1)"; rc=$?
summary="$(printf '%s\n' "$out" | grep '^selftest:')"
if [ "$rc" -eq 0 ] && [ "$summary" = "selftest: $WANT_CELLS cells, $WANT_CELLS passed, 0 failed" ]; then
    leg selftest "ok (rc 0, $summary)"
else
    leg selftest "FAULT (rc $rc, ${summary:-no summary line})"; faults=$((faults + 1))
fi

# gate -- the newest queue-touching commit, on both axes, in its own tree
out="$(cd "$ROOT" && python3 "$TOOL" --repo "$ROOT" --guard 2>&1)"; rc=$?
if [ "$rc" -eq 0 ] \
   && printf '%s\n' "$out" | grep -q 'pin:PASS' \
   && printf '%s\n' "$out" | grep -q 'every graded commit left its own tree consistent'; then
    leg gate "ok (rc 0, $(printf '%s\n' "$out" | grep -o 'pin:[A-Z]*' | head -1), index and pin clean)"
else
    leg gate "FAULT (rc $rc: $(printf '%s\n' "$out" | grep '^verdict:' | head -1))"
    faults=$((faults + 1))
fi

# axes -- the commit the index axis called clean and the pin axis calls DRIFT
out="$(cd "$ROOT" && python3 "$TOOL" --repo "$ROOT" --from "$DRIFT_PARENT" --to "$DRIFT_REV" 2>&1)"; rc=$?
idx="$(cd "$ROOT" && python3 "$TOOL" --repo "$ROOT" --from "$DRIFT_PARENT" --to "$DRIFT_REV" --axis index 2>&1)"; irc=$?
if [ "$rc" -eq 1 ] && [ "$irc" -eq 0 ] \
   && printf '%s\n' "$out" | grep -q 'pin:DRIFT' \
   && printf '%s\n' "$out" | grep -qF "$DRIFT_COUNT" \
   && printf '%s\n' "$out" | grep -q "drift:   $DRIFT_REV"; then
    leg axes "ok ($DRIFT_REV rc 1 pin:DRIFT, --axis index rc 0; $(printf '%s\n' "$out" | grep -o 'count headings moved [0-9]* -> [0-9]*'))"
else
    leg axes "FAULT (rc $rc want 1, --axis index rc $irc want 0)"; faults=$((faults + 1))
fi

# preaxis -- the same range through the tool before the axis: rc 0
git -C "$ROOT" show "$PRE_AXIS_REV:$TOOL" > "$TMP/pre_axis.py" || exit 2
out="$(cd "$ROOT" && python3 "$TMP/pre_axis.py" --repo "$ROOT" --from "$DRIFT_PARENT" --to "$DRIFT_REV" 2>&1)"; rc=$?
if [ "$rc" -eq 0 ] && printf '%s\n' "$out" | grep -q 'every graded commit left the index current'; then
    leg preaxis "ok ($PRE_AXIS_REV's tool reads rc 0 over the same range; the axis moved the reading)"
else
    leg preaxis "FAULT (pre-axis tool rc $rc want 0 -- the range was red before the axis)"; faults=$((faults + 1))
fi

# axis -- --axis pin grades one axis and writes no index verdict for it
json="$TMP/pin.json"
out="$(cd "$ROOT" && python3 "$TOOL" --repo "$ROOT" --guard --axis pin --json "$json" 2>&1)"; rc=$?
verdicts="$(grep -c '"verdict"' "$json" 2>/dev/null || true)"
passes="$(grep -c '"pin": "PASS"' "$json" 2>/dev/null || true)"
if [ "$rc" -eq 0 ] && [ "${verdicts:-1}" -eq 0 ] && [ "${passes:-0}" -eq 1 ]; then
    leg axis "ok (rc 0, 0 index verdicts, 1 pin PASS in the JSON)"
else
    leg axis "FAULT (rc $rc, index verdicts ${verdicts:-?}, pin PASS ${passes:-?})"; faults=$((faults + 1))
fi

if [ "$faults" -eq 0 ]; then
    echo "append-pin: 5 leg(s), 0 fault(s)"
    exit 0
fi
echo "append-pin: 5 leg(s), $faults fault(s)"
exit 1
