#!/usr/bin/env bash
# restore-minikanren.sh — print recovery state for the miniKanren-on-SX loop.
#
# The loop runs as a Claude Code instance inside a tmux session named `minikanren`,
# operating in a git worktree at /root/rose-ash-loops/minikanren on branch
# loops/minikanren. This script shows you where things stand. To respawn, see the
# bottom of the output.
#
# Usage:
#   bash plans/restore-minikanren.sh           # status snapshot
#   bash plans/restore-minikanren.sh --print   # also cat the briefing
#
set -uo pipefail

cd "$(dirname "$0")/.."

WT="/root/rose-ash-loops/minikanren"

echo "=== minikanren loop state ==="
echo
if [ -d "$WT" ]; then
  echo "Worktree:      $WT"
  echo "Branch:        $(git -C "$WT" rev-parse --abbrev-ref HEAD 2>/dev/null || echo '?')"
  echo "HEAD:          $(git -C "$WT" log -1 --oneline 2>/dev/null || echo '?')"
else
  echo "Worktree:      MISSING ($WT)"
  echo "  Recreate with:"
  echo "    git worktree add /root/rose-ash-loops/minikanren -b loops/minikanren architecture"
fi
echo

echo "=== Recent commits on lib/minikanren/ + plan ==="
if [ -d "$WT" ]; then
  git -C "$WT" log -15 --oneline -- lib/minikanren/ plans/minikanren-on-sx.md plans/agent-briefings/minikanren-loop.md 2>/dev/null \
    || echo "  (none yet)"
else
  echo "  (worktree missing)"
fi
echo

echo "=== lib/minikanren/ contents ==="
if [ -d "$WT/lib/minikanren" ]; then
  ls -1 "$WT/lib/minikanren/" 2>/dev/null | sed 's/^/  /'
else
  echo "  (lib/minikanren/ does not exist yet — Phase 1 not started)"
fi
echo

echo "=== lib/guest/ prerequisites ==="
for f in lib/guest/lex.sx lib/guest/pratt.sx lib/guest/match.sx; do
  if [ -f "$f" ]; then
    printf "  ✓ %s (%d lines)\n" "$f" "$(wc -l < "$f")"
  else
    printf "  ✗ %s MISSING\n" "$f"
  fi
done
echo

echo "=== Plan progress (phase checkboxes) ==="
if [ -f plans/minikanren-on-sx.md ]; then
  awk '/^### Phase [0-9]/ {phase=$0; next}
       /^- \[/ && phase {
         if (!shown[phase]++) print "  " phase
         print "    " $0
       }
       /^### Phase / && !/^### Phase [0-9]/ {phase=""}
       /^## / && !/^### / {phase=""}' plans/minikanren-on-sx.md \
    | head -60
else
  echo "  plans/minikanren-on-sx.md NOT found"
fi
echo

echo "=== Tests (if Phase 1+ shipped) ==="
if [ -d "$WT/lib/minikanren/tests" ]; then
  ls -1 "$WT/lib/minikanren/tests/" 2>/dev/null | sed 's/^/  /'
else
  echo "  (no tests yet)"
fi
echo

echo "=== sx_server.exe ==="
if [ -x hosts/ocaml/_build/default/bin/sx_server.exe ]; then
  echo "  ✓ built"
else
  echo "  ✗ NOT built — loop conformance runs need it. Run: sx_build target=ocaml"
fi
echo

echo "=== tmux session 'minikanren' ==="
if command -v tmux >/dev/null && tmux has-session -t minikanren 2>/dev/null; then
  echo "  ✓ session live"
  echo "  Attach: tmux attach -t minikanren"
  echo "  Last 8 visible lines:"
  tmux capture-pane -t minikanren -p 2>/dev/null \
    | grep -v '^[[:space:]]*$' \
    | tail -8 \
    | sed 's/^/    /'
else
  echo "  ✗ session not running"
fi
echo

echo "=== Remote loops/minikanren ==="
if git ls-remote --exit-code origin loops/minikanren >/dev/null 2>&1; then
  echo "  ✓ origin/loops/minikanren exists"
  if [ -d "$WT" ]; then
    AHEAD=$(git -C "$WT" rev-list --count origin/loops/minikanren..HEAD 2>/dev/null || echo "?")
    BEHIND=$(git -C "$WT" rev-list --count HEAD..origin/loops/minikanren 2>/dev/null || echo "?")
    echo "  local ahead: $AHEAD,  local behind: $BEHIND"
  fi
else
  echo "  (origin/loops/minikanren not yet pushed)"
fi
echo

echo "=== Briefing ==="
[ -f plans/agent-briefings/minikanren-loop.md ] \
  && echo "  plans/agent-briefings/minikanren-loop.md" \
  || echo "  briefing NOT found"
echo

echo "=== To respawn ==="
cat <<'EOF'
  If the worktree is missing:

    git worktree add /root/rose-ash-loops/minikanren -b loops/minikanren architecture

  If the tmux session died:

    tmux new-session -d -s minikanren -c /root/rose-ash-loops/minikanren
    tmux send-keys -t minikanren 'claude' Enter
    # wait until the Claude UI box appears, then:
    tmux send-keys -t minikanren 'You are the miniKanren-on-SX loop runner. Read /root/rose-ash/plans/agent-briefings/minikanren-loop.md in full and follow the iteration protocol indefinitely. Run the pre-flight check first; if lib/guest/match.sx is missing or lib-guest Step 6 has regressed, stop and report. Worktree: /root/rose-ash-loops/minikanren on branch loops/minikanren. Push to origin/loops/minikanren after every commit. Never push to main or architecture. Resume from the first unchecked [ ] in plans/minikanren-on-sx.md.' Enter Enter

  If the session is alive but stuck, attach with `tmux attach -t minikanren` and
  unstick manually. The plan file is the source of truth — the loop reads it
  fresh every iteration and picks up wherever the queue left off.
EOF

if [ "${1:-}" = "--print" ]; then
  echo
  echo "=== Briefing contents ==="
  [ -f plans/agent-briefings/minikanren-loop.md ] && cat plans/agent-briefings/minikanren-loop.md
fi
