#!/bin/bash
# CHECK-q172 -- 30 verifiers re-pointed from host-absolute paths at the artifact beside them.
#
# Legs
#   selftest-bind    the roster probe's 8 fixture cells: a good chain, a wrong-depth
#                    chain, a host literal, a stale HOST row, and both verdicts
#   selftest-parity  the verdict probe's 8 fixture cells: same rc, an improvement, a
#                    regression, a crash, and the counts it prints
#   roster           exactly 30 rows, every row a file git tracks
#   worktree         every Path(__file__) chain in those 30 resolves to an existing
#                    tracked file, and no PATH-class file still names the host
#   teeth            the SAME roster at the pre-fix commit reads red -- a RED CONTROL
#                    ON REAL HISTORY, so a probe that had gone blind fails here
#   parity           each of the 30 re-run out of the pre-fix tree and out of this
#                    one: no rc may regress, no binding may crash, and the one
#                    stale-host-copy repair is named as an improvement
#   census           the census instrument agrees the positions are gone: ledger,
#                    sol and spark live-unstated at or under the readings taken here
#
# rc 0 every leg as expected | 1 a graded fault | 2 could not grade.
# Run from anywhere; the repo resolves from $0.
set -u

HERE="$(cd "$(dirname "$(readlink -f "$0")")" && pwd)"
REPO="$(cd "$HERE/../.." && pwd)"
BIND="$HERE/q172_path_bind_check.py"
PARITY="$HERE/q172_parity_check.py"
ROSTER="$HERE/q172_hostpath_roster.tsv"
CENSUS="$HERE/q62_lane_census.py"
RED_REV="d3805eb"
RED_FAULTS=83
CEIL_LEDGER=33
CEIL_SOL=72
CEIL_SPARK=123
legs=0
faults=0

ok()  { legs=$((legs + 1)); printf 'ok    %-15s %s\n' "$1" "$2"; }
bad() { legs=$((legs + 1)); faults=$((faults + 1)); printf 'FAULT %-15s %s\n' "$1" "$2"; }

for f in "$BIND" "$PARITY" "$ROSTER" "$CENSUS"; do
  [ -f "$f" ] || { echo "cannot grade -- missing $f"; exit 2; }
done
command -v python3 >/dev/null || { echo "cannot grade -- no python3"; exit 2; }
git -C "$REPO" rev-parse --git-dir >/dev/null 2>&1 \
  || { echo "cannot grade -- not a git repository: $REPO"; exit 2; }

TMP="$(mktemp -d "${TMPDIR:-/tmp}/q172.XXXXXX")" || { echo "cannot grade -- no tmp"; exit 2; }
trap 'rm -rf "$TMP"' EXIT
git -C "$REPO" rev-parse --verify "$RED_REV^{commit}" >/dev/null 2>&1 \
  || { echo "cannot grade -- pre-fix commit $RED_REV absent"; exit 2; }
mkdir -p "$TMP/before"
git -C "$REPO" archive "$RED_REV" | tar -x -C "$TMP/before" \
  || { echo "cannot grade -- cannot export $RED_REV"; exit 2; }

# selftest-bind
out="$(python3 "$BIND" --selftest 2>&1)"; rc=$?
if [ $rc -eq 0 ] && printf '%s' "$out" | grep -q '8 cells, 8 passed, 0 failed'; then
  ok selftest-bind "8 cells, 8 passed, 0 failed"
else
  bad selftest-bind "rc $rc: $(printf '%s' "$out" | tail -n 1)"
fi

# selftest-parity
out="$(python3 "$PARITY" --selftest 2>&1)"; rc=$?
if [ $rc -eq 0 ] && printf '%s' "$out" | grep -q '10 cells, 10 passed, 0 failed'; then
  ok selftest-parity "10 cells, 10 passed, 0 failed"
else
  bad selftest-parity "rc $rc: $(printf '%s' "$out" | tail -n 1)"
fi

# roster -- 30 rows, every one a tracked file
n="$(grep -vc '^#' "$ROSTER")"
untracked=""
while IFS=$'\t' read -r rel _ _; do
  case "$rel" in \#*|"") continue ;; esac
  git -C "$REPO" ls-files --error-unmatch "$rel" >/dev/null 2>&1 || untracked="$untracked $rel"
done < "$ROSTER"
if [ "$n" -eq 30 ] && [ -z "$untracked" ]; then
  ok roster "30 row(s), every one tracked"
else
  bad roster "n=$n (want 30), untracked:$untracked"
fi

# worktree
out="$(python3 "$BIND" --repo "$REPO" --roster "$ROSTER" 2>&1)"; rc=$?
line="$(printf '%s' "$out" | tail -n 1)"
if [ $rc -eq 0 ] && printf '%s' "$line" | grep -q '^30 file(s) graded, 0 fault(s)$'; then
  ok worktree "$line"
else
  bad worktree "rc $rc: $(printf '%s' "$out" | grep '^FAULT' | head -n 3 | tr '\n' ' ')"
fi

# teeth -- the same roster, the tree before the repair
out="$(python3 "$BIND" --repo "$TMP/before" --roster "$ROSTER" 2>&1)"; rc=$?
n2="$(printf '%s\n' "$out" | grep -c '^FAULT')"
if [ $rc -eq 1 ] && [ "$n2" -eq "$RED_FAULTS" ] \
   && printf '%s' "$out" | grep -q 'host-absolute path in a PATH-class file'; then
  ok teeth "rc 1, $n2 fault(s) at $RED_REV, host bindings named"
else
  bad teeth "rc $rc, $n2 fault(s) at $RED_REV, wanted rc 1 / $RED_FAULTS"
fi

# parity -- same verdict, or a better one
out="$(python3 "$PARITY" --before "$TMP/before" --after "$REPO" --roster "$ROSTER" 2>&1)"; rc=$?
line="$(printf '%s' "$out" | tail -n 1)"
if [ $rc -eq 0 ] \
   && printf '%s' "$line" | grep -q '0 rc regression(s), 0 crash(es), 0 improvement(s), 1 unjudged' \
   && printf '%s' "$out" | grep -q '^unjudged wizard-work/ledger/verify_census.py'; then
  ok parity "$line"
else
  bad parity "rc $rc: $line"
fi

# census -- the instrument agrees, lane by lane
over=""
for pair in ledger:$CEIL_LEDGER sol:$CEIL_SOL spark:$CEIL_SPARK; do
  lane="${pair%%:*}"; ceil="${pair##*:}"
  line="$(python3 "$CENSUS" --repo "$REPO" --lane "$lane" 2>&1 | tail -n 1)"
  got="$(printf '%s' "$line" | sed -n 's/.*live-unstated=\([0-9]*\).*/\1/p')"
  [ -n "$got" ] && [ "$got" -le "$ceil" ] || over="$over $lane=$got(want<=$ceil)"
done
if [ -z "$over" ]; then
  ok census "ledger<=$CEIL_LEDGER sol<=$CEIL_SOL spark<=$CEIL_SPARK live-unstated"
else
  bad census "over ceiling:$over"
fi

echo "CHECK-q172: $legs leg(s), $faults fault(s)"
[ $faults -eq 0 ] || exit 1
exit 0
