#!/usr/bin/env bash # Body Snatcher — self-orchestrating build factory. # Each phase runs in a FRESH `pi -p` context (reads AGENTS.md + verify.js), # gated mechanically by `node verify.js N`. Sequential. Max 3 retries + a # targeted fix attempt, then an adversarial review, then a final all-gates check. # Do NOT pin a model — use the configured default. Do NOT parallelize workers. set -euo pipefail cd "$(dirname "$0")" MAX_RETRIES="${MAX_RETRIES:-3}" PHASE_TIMEOUT="${PHASE_TIMEOUT:-2400}" # seconds per pi -p invocation (40 min) LOG="build.log" PROMPTS="build/prompts" mkdir -p build : > "$LOG" # truncate single explicit log file (no rm, no globs) log(){ echo "[$(date +%H:%M:%S)] $*" | tee -a "$LOG"; } # run one pi -p worker; never let a non-zero exit stop the script run_pi(){ local prompt_file="$1" logfile="$2" timeout "${PHASE_TIMEOUT}" pi -p -a --no-session \ @AGENTS.md @verify.js "$(cat "$prompt_file")" > "$logfile" 2>&1 || true } run_phase(){ local phase="$1" local prompt="$PROMPTS/phase${phase}.txt" log "=== PHASE $phase START ===" # skip-if-already-passing (idempotent re-runs) if node verify.js "$phase" > "build/phase${phase}.pregate.log" 2>&1; then log "=== PHASE $phase already PASS (skipped) ===" return 0 fi local attempt for attempt in $(seq 1 "$MAX_RETRIES"); do log "--- phase $phase attempt $attempt/$MAX_RETRIES ---" run_pi "$prompt" "build/phase${phase}.attempt${attempt}.log" if node verify.js "$phase" > "build/phase${phase}.gate${attempt}.log" 2>&1; then log "=== PHASE $phase PASSED (attempt $attempt) ===" cp index.html "build/index.phase${phase}.bak" return 0 fi log "=== phase $phase FAILED gate (attempt $attempt) ===" done # retries exhausted -> targeted fix agent (snapshot first so we can revert) log "=== phase $phase exhausted retries; running targeted FIX ===" cp index.html "build/index.pre-fix${phase}.bak" 2>/dev/null || true timeout "${PHASE_TIMEOUT}" pi -p -a --no-session @AGENTS.md @verify.js \ "Phase ${phase} of Body Snatcher failed its verification gate. Read AGENTS.md and the current index.html, then run \`node verify.js ${phase}\` to see the exact failing assertions. Fix EVERY failure with TARGETED edits — do NOT rewrite index.html from scratch; preserve all working features. Re-run \`node verify.js ${phase}\` until it prints PASSED (exit 0)." \ > "build/phase${phase}.fix.log" 2>&1 || true if node verify.js "$phase" > "build/phase${phase}.gate-fix.log" 2>&1; then log "=== PHASE $phase PASSED after fix ===" cp index.html "build/index.phase${phase}.bak" return 0 fi log "=== PHASE $phase FAILED after fix ===" return 1 } all_gates_pass(){ local p for p in 1 2 3 4 5; do node verify.js "$p" > "build/final.gate${p}.log" 2>&1 || return 1 done return 0 } # ---- main ----------------------------------------------------------------- log "BUILD START (model=default retries=$MAX_RETRIES phase_timeout=${PHASE_TIMEOUT}s)" for p in 1 2 3 4 5; do run_phase "$p" || { log "BUILD HALTED at phase $p"; exit 1; } done # snapshot a known all-passing build before the adversarial pass if all_gates_pass; then cp index.html "build/index.allpass.bak" log "snapshot build/index.allpass.bak (all 5 gates green)" else log "WARN: not all gates green entering adversarial; continuing anyway" fi log "=== ADVERSARIAL REVIEW ===" cp index.html "build/index.prereview.bak" timeout "${PHASE_TIMEOUT}" pi -p -a --no-session @AGENTS.md @verify.js \ "$(cat "$PROMPTS/adversarial.txt")" > build/adversarial.log 2>&1 || true # if the review regressed any gate, revert to the last known-good build if ! all_gates_pass; then if [ -f "build/index.allpass.bak" ]; then log "adversarial regressed a gate; restoring build/index.allpass.bak" cp build/index.allpass.bak index.html else log "adversarial regressed a gate and no allpass snapshot exists; leaving review output for manual review" fi fi log "=== FINAL VERIFICATION (all 5 gates) ===" ok=1 for p in 1 2 3 4 5; do if node verify.js "$p" > "build/final.gate${p}.log" 2>&1; then log "gate $p PASS" else log "gate $p FAIL"; ok=0 fi done if [ "$ok" = 1 ]; then log "ALL GATES PASSED" else log "FINAL GATE(S) FAILED — see build/final.gate*.log" fi log "BUILD COMPLETE" [ "$ok" = 1 ] || exit 1