#!/bin/sh
# TinyVault CI: every check the guide recommends, in one script.
# Usage: LEAN4CHECKER=/path/to/lean4checker ./ci.sh
#        ./ci.sh --skip-replay      (leaves out stage 3 and says so at the end)
set -eu
cd "$(dirname "$0")"
REPLAY="replay included"

echo "[1/6] banned-construct scan (every .lean file except Audit.lean and scratch/)"
# Audit.lean is deliberately not scanned: it is the one allowlisted metaprogram.
# This scan is a cheap first filter. The axiom audit in stage 2 is the backstop.
scan() { # usage: scan <grep flags> <pattern>; prints matches, succeeds if any
  find . -name '*.lean' -not -path './.lake/*' -not -path './scratch/*' -not -name Audit.lean \
    -exec grep -n "$1" "$2" {} +
}
if scan -wE 'sorry|admit|axiom|native_decide|implemented_by|unsafe|run_cmd|run_elab|run_meta|skipKernelTC|macro|elab|initialize' \
   || scan -E '#eval|decide \+native'; then
  echo "FAIL: banned construct found (the scan is textual: reword a comment that trips it)" >&2
  exit 1
fi

echo "[2/6] lake build --wfail (kernel-checks every proof, then runs Audit.lean)"
lake build --wfail

echo "[3/6] independent kernel replay with lean4checker"
if [ -n "${LEAN4CHECKER:-}" ]; then
  lake env "$LEAN4CHECKER" TinyVault.Spec TinyVault.Proofs
  echo "  replayed TinyVault.Spec and TinyVault.Proofs: accepted"
elif [ "${1:-}" = "--skip-replay" ]; then
  REPLAY="replay skipped"
  echo "  SKIPPED on request (--skip-replay)"
else
  echo "FAIL: LEAN4CHECKER is not set. Build lean4checker for this toolchain:" >&2
  echo "  git clone --branch v4.20.0 https://github.com/leanprover/lean4checker ../lean4checker && (cd ../lean4checker && lake build)" >&2
  echo "then run: LEAN4CHECKER=\$PWD/../lean4checker/.lake/build/bin/lean4checker ./ci.sh" >&2
  echo "or run ./ci.sh --skip-replay to leave this stage out." >&2
  exit 1
fi

echo "[4/6] the Step 4 naive proof attempt must still be rejected"
if lake env lean scratch/NaiveAttempt.lean >stuck.log 2>&1; then
  echo "FAIL: scratch/NaiveAttempt.lean compiled; the naive capacity is not provable" >&2
  exit 1
fi
if ! grep -q "omega could not prove the goal" stuck.log; then
  echo "FAIL: scratch/NaiveAttempt.lean failed for a different reason:" >&2
  cat stuck.log >&2
  exit 1
fi
echo "  rejected as documented: omega could not prove the goal"
rm -f stuck.log

echo "[5/6] Rust u128 differential oracle"
rustc -O -C overflow-checks=on oracle_test.rs -o oracle_test
./oracle_test

echo "[6/6] mutation check: each planted Rust bug must make the oracle fail"
for mutant in floor wrapping payout-ceil strict-cap paused-transfer; do
  status=0
  ./oracle_test --mutant "$mutant" >/dev/null 2>mutant.log || status=$?
  # Detected means: the comparison itself reported a mismatch (exit code 1).
  if [ "$status" != 1 ] || ! grep -q '^FAIL at vector' mutant.log; then
    echo "FAIL: mutant '$mutant' was not detected" >&2
    exit 1
  fi
  echo "  detected '$mutant': $(sed -n 1p mutant.log)"
done
rm -f mutant.log

echo "ALL CHECKS PASSED ($REPLAY)"
