#!/bin/bash
# CHECK: wizard-work/forge -- every committed verifier has a ledger row, the row
# is bound to the blob it was measured against, and any red row carries a cause.
#
# Runner note (Q87, 2026-10-31). This lane shipped 39 `verify_*.py` and no gate;
# `tools/verify.py` names `wizard-work/forge` zero times, and the lane would not
# have noticed if all 39 broke at once. This CHECK is `verifier_gate.py --check`:
# it grades a committed ledger (VERIFIER_LEDGER.tsv) against the tree at HEAD and
# runs NOTHING, so it costs well under a second and cannot be skipped for being
# slow. Execution is explicit:
#
#   bash CHECK --emit      # run every verifier, rewrite the ledger (minutes)
#   bash CHECK --strict    # also fail on any non-PASS row (the lane's health)
#   bash CHECK --census    # who else, if anyone, invokes each verifier
#
# The fast path is deliberately a ledger grading and not a run: the run takes
# ~8 minutes here (one recursive sweep and two live-ish harnesses), and a gate
# that takes minutes is a gate that gets skipped -- the condition that produced
# this item. What it can still catch without running anything: a verifier edited
# after its rc was measured (DRIFT, by blob), a verifier added with no row
# (UNGRADED), a row whose file is gone (STALE), a PASS whose rc says otherwise,
# and a red row with no recorded cause (CAUSELESS). `--strict` is the switch
# from "are the books consistent?" to "are the lane's verifiers true?" -- today
# the honest answer to the second is no, and the ledger names which ones.
#
# Every skip is printed rather than implied: the gate's population is the
# committed `verify_*.py` set at HEAD, so an untracked file beside the lane is
# reported as OUTSIDE (a note, not a fault, because it is not a claim about the
# commit) and every non-PASS row is printed by name on every run, strict or not.
#
# Exit codes follow the house contract: 0 clean, 1 graded a fault, 2 could not
# grade (no git, no python3, no lane).
set -o pipefail
cd "$(dirname "$(readlink -f "$0")")" || { echo "CHECK: cannot cd to the lane" >&2; exit 2; }
command -v python3 >/dev/null || { echo "CHECK: python3 not found -- cannot grade" >&2; exit 2; }
command -v git >/dev/null || { echo "CHECK: git not found -- cannot grade" >&2; exit 2; }

set +e
python3 verifier_gate.py --check "$@"
rc=$?
set -e
case $rc in
    0) ;;
    1) echo "CHECK: the forge verifier ledger graded a fault -- see the named rows above (Q87)." >&2; exit 1 ;;
    2) echo "CHECK: could not grade -- see the gate's message above." >&2; exit 2 ;;
    *) echo "CHECK: verifier_gate.py exited $rc -- internal error (a fault)." >&2; exit 1 ;;
esac
exit 0
