#!/usr/bin/env bash
# runner.sh — wraps `claude -p` to drive autoresearch iterations unattended.
# Karpathy's autoresearch is invoked by simply spinning up Claude/Codex in
# the repo root. This wrapper does that with a fixed-cycle budget so you can
# run it overnight via cron / tmux.

set -euo pipefail

HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ITERATIONS="${ITERATIONS:-30}"
MODEL="${MODEL:-claude-opus-4-7}"

# Sanity: claude CLI must be installed locally
command -v claude >/dev/null 2>&1 || { echo "claude CLI not found. Install from anthropic.com/claude/code"; exit 1; }

cd "$HERE"
mkdir -p log

# Snapshot baseline if not already
if [[ ! -f log/baseline.json ]]; then
  ./measure.sh > log/baseline.json
  echo "Baseline saved to log/baseline.json"
fi

for i in $(seq 1 "$ITERATIONS"); do
  STAMP=$(date +%Y-%m-%d-%H%M%S)
  ITER_DIR="log/iter-${STAMP}"
  mkdir -p "$ITER_DIR"

  echo "=== Iteration $i / $ITERATIONS @ $STAMP ==="
  ./measure.sh > "$ITER_DIR/before.json"
  BEFORE=$(jq -r .score "$ITER_DIR/before.json")
  echo "  Score before: $BEFORE/115"

  # Hand off to claude -p with the program.md as the spec
  claude -p --model "$MODEL" \
    --output-format text \
    --max-turns 30 \
    < program.md \
    > "$ITER_DIR/agent_transcript.txt" 2>&1 || true

  ./measure.sh > "$ITER_DIR/after.json"
  AFTER=$(jq -r .score "$ITER_DIR/after.json")
  echo "  Score after:  $AFTER/115"

  if (( AFTER > BEFORE )); then
    echo "  ✓ improved by $((AFTER - BEFORE)). Iteration kept."
    echo "iter-${STAMP}: ${BEFORE} → ${AFTER} (kept)" >> log/timeline.log
  elif (( AFTER < BEFORE )); then
    echo "  ✗ regressed by $((BEFORE - AFTER)). Reverting."
    # Revert to last known good
    git -C "${PATIENT:-../}" stash >/dev/null 2>&1 || true
    echo "iter-${STAMP}: ${BEFORE} → ${AFTER} (reverted)" >> log/timeline.log
  else
    echo "  ─ no change. Iteration kept (may be partial work)."
    echo "iter-${STAMP}: ${BEFORE} → ${AFTER} (no change)" >> log/timeline.log
  fi

  # Stop if we've reached 100% (max may be > 100 with weights so check both)
  PCT=$(jq -r .percent "$ITER_DIR/after.json")
  if awk "BEGIN { exit ($PCT >= 100) ? 0 : 1 }"; then
    echo "Reached 100% parity score. Stopping."
    break
  fi
done

echo "=== Done. Tail of timeline ==="
tail -20 log/timeline.log
