#!/usr/bin/env bash
# restore-lib-guest.sh — print recovery state for the lib/guest extraction loop.
#
# The loop runs as a Claude Code instance inside a tmux session named `lib-guest`.
# This script shows you where things stand. To respawn, see the bottom of the output.
#
# Usage:
#   bash plans/restore-lib-guest.sh           # status snapshot
#   bash plans/restore-lib-guest.sh --print   # also cat the briefing
#
set -uo pipefail

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

echo "=== lib/guest loop state ==="
echo
echo "Branch:        $(git rev-parse --abbrev-ref HEAD)"
echo "HEAD:          $(git log -1 --oneline)"
echo

echo "=== Recent commits on lib/guest/, canaries, and plan ==="
git log -15 --oneline -- lib/guest/ lib/lua/ lib/prolog/ lib/common-lisp/ lib/haskell/ lib/tcl/ plans/lib-guest.md plans/agent-briefings/lib-guest-loop.md 2>/dev/null \
  || echo "  (none yet)"
echo

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

echo "=== Baseline snapshots ==="
if [ -d lib/guest/baseline ]; then
  for f in lib/guest/baseline/*.json; do
    [ -f "$f" ] || continue
    name=$(basename "$f" .json)
    totals=$(python3 -c "import json,sys
try:
  d=json.load(open('$f'))
  t=d.get('totals',d.get('overall',{}))
  if t: print(f\"pass={t.get('pass','?')} fail={t.get('fail','?')}\")
  else: print('(no totals)')
except Exception as e: print(f'(read error: {e})')" 2>/dev/null)
    printf "  %-14s %s\n" "$name" "$totals"
  done
else
  echo "  (lib/guest/baseline/ does not exist yet — Step 0 not done)"
fi
echo

echo "=== Plan progress (status column) ==="
if [ -f plans/lib-guest.md ]; then
  awk '/^\| Step \|/,/^$/' plans/lib-guest.md | grep -E '^\| [0-9]' | sed 's/^/  /' || true
else
  echo "  plans/lib-guest.md NOT found"
fi
echo

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

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

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

echo "=== To respawn ==="
cat <<'EOF'
  If the tmux session died:

    tmux new-session -d -s lib-guest -c /root/rose-ash
    tmux send-keys -t lib-guest 'claude' Enter
    # wait until the Claude UI box appears, then:
    tmux send-keys -t lib-guest 'You are the lib/guest extraction loop runner. Read /root/rose-ash/plans/agent-briefings/lib-guest-loop.md in full and follow the iteration protocol indefinitely. Resume from the first pending step in the plan. Branch: architecture. Never push, never touch main.' Enter Enter

  If the session is alive but stuck, attach with `tmux attach -t lib-guest` 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/lib-guest-loop.md ] && cat plans/agent-briefings/lib-guest-loop.md
fi
