#!/usr/bin/env bash # reproduce.sh — ONE COMMAND. Proves every instrument, records the calibration, # re-runs every cycle, verifies every artifact against its hash. # # Capturing evidence is not enough. If a stranger cannot regenerate the numbers # in one command, the record is testimony. This is the command. # # usage: ./docs/experiments/reproduce.sh [cycle-glob] set -uo pipefail cd "$(git rev-parse --show-toplevel)" || exit 2 E="docs/experiments"; I="$E/instruments/instrument"; CAP="./tools/evidence/capture.sh" STAMP=$(date -u +%Y%m%dT%H%M%SZ); FAIL=0 hdr(){ printf '\n\033[1m%s\033[0m\n' "$1"; } hdr "1. INSTRUMENTS — proven before anything is measured" for p in "$I"/*/prove.sh; do [ -e "$p" ] || continue name=$(basename "$(dirname "$p")") if "$p" >/dev/null 2>&1; then printf ' ok %-16s proven\n' "$name" else printf ' FAIL %-16s NOT PROVEN — no measurement below is valid\n' "$name"; FAIL=$((FAIL+1)); fi done [ "$FAIL" -eq 0 ] || { printf '\n ABORT: an instrument is unproven. Nothing measured today is reportable.\n'; exit 1; } hdr "2. CALIBRATION — recorded, and stamped into every cycle that runs below" CALFILE="$E/instruments/CALIBRATION-$STAMP.txt" { printf 'calibration run\t%s\n' "$STAMP" printf 'commit\t%s\n' "$(git rev-parse HEAD)" printf 'tree\t%s\n' "$(test -n "$(git status --porcelain)" && echo dirty || echo clean)" printf 'cloc\t%s\n' "$(cloc --version 2>/dev/null || echo ABSENT)" printf 'matplotlib\t%s\n' "$(python3 -c 'import matplotlib;print(matplotlib.__version__)' 2>/dev/null || echo ABSENT)" printf 'cc\t%s\n' "$(cc --version 2>&1|head -1)" for p in "$I"/*/prove.sh; do [ -e "$p" ] || continue printf '\n--- %s ---\n' "$(basename "$(dirname "$p")")"; "$p" 2>&1; done } > "$CALFILE" printf ' %s\n' "$CALFILE" grep -c '^ ok' "$CALFILE" | xargs printf ' %s calibration checks passed\n' hdr "3. CYCLES — each re-run, each stamped with the calibration in effect" GLOB="${1:-*}"; ran=0; miss=0 # Cycles live at docs/v{1,2}/experiments/cycles/. The previous glob was # $E/v*/cycles/ and matched 1 of 29, then printed REPRODUCED. for m in docs/v*/experiments/cycles/$GLOB/measure.sh; do [ -e "$m" ] || continue d=$(dirname "$m"); name=$(basename "$d") mkdir -p "$d/evidence" cp "$CALFILE" "$d/evidence/CALIBRATION.txt" # <- the cycle SHOWS its calibration if $CAP "$d/evidence" rerun -- "$m" >/dev/null 2>&1; then printf ' ok %-42s re-run\n' "$name" else printf ' FAIL %-42s re-run failed\n' "$name"; FAIL=$((FAIL+1)); fi ran=$((ran+1)) done total=$(ls docs/v1/experiments/cycles/*.md docs/v2/experiments/cycles/*.md 2>/dev/null | grep -vc INDEX) miss=$((total - ran)) printf ' %s cycle(s) re-run\n' "$ran" if [ "$miss" -gt 0 ]; then printf ' %s of %s cycle(s) have NO measure.sh and are NOT REPRODUCIBLE.\n' "$miss" "$total" printf ' A record that reproduces %s/%s of itself has not been reproduced.\n' "$ran" "$total" FAIL=$((FAIL+1)) fi hdr "4. VERIFY — every artifact re-hashed against its manifest" for d in $(find docs -name MANIFEST.tsv -exec dirname {} \; | sort -u); do ./tools/evidence/verify-manifest.sh "$d" || FAIL=$((FAIL+1)) done for d in $E/v*/cycles/*/evidence $I/*/evidence; do [ -f "$d/MANIFEST.tsv" ] || continue ./tools/evidence/verify-manifest.sh "$(dirname "$d")" >/dev/null 2>&1 || true done ./tools/evidence/verify-notebook.sh || FAIL=$((FAIL+1)) hdr "RESULT" [ "$FAIL" -eq 0 ] && echo " REPRODUCED — instruments proven, calibration recorded, cycles re-run, artifacts verified." \ || echo " NOT REPRODUCED — $FAIL failure(s) above." exit "$FAIL"