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

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

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

echo "=== datalog 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/datalog -b loops/datalog architecture"
fi
echo

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

echo "=== lib/datalog/ contents ==="
if [ -d "$WT/lib/datalog" ]; then
  ls -1 "$WT/lib/datalog/" 2>/dev/null | sed 's/^/  /'
else
  echo "  (lib/datalog/ 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 lib/guest/ast.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/datalog-on-sx.md ]; then
  awk '/^### Phase / {phase=$0; print "  " phase; phase_seen=1; next}
       /^- \[/ && phase_seen { print "    " $0 }
       /^## [^#]/ {phase_seen=0}' plans/datalog-on-sx.md \
    | head -80
else
  echo "  plans/datalog-on-sx.md NOT found"
fi
echo

echo "=== Tests + scoreboard ==="
if [ -d "$WT/lib/datalog/tests" ]; then
  ls -1 "$WT/lib/datalog/tests/" 2>/dev/null | sed 's/^/  /'
else
  echo "  (no tests yet)"
fi
if [ -f "$WT/lib/datalog/scoreboard.json" ]; then
  echo "  ✓ scoreboard.json present"
  python3 -c "import json
try:
  d=json.load(open('$WT/lib/datalog/scoreboard.json'))
  t=d.get('totals',d.get('overall',{}))
  print(f\"  totals: pass={t.get('pass','?')} fail={t.get('fail','?')}\")
except Exception as e: print(f'  (read error: {e})')" 2>/dev/null
else
  echo "  (no scoreboard 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 'datalog' ==="
if command -v tmux >/dev/null && tmux has-session -t datalog 2>/dev/null; then
  echo "  ✓ session live"
  echo "  Attach: tmux attach -t datalog"
  echo "  Last 8 visible lines:"
  tmux capture-pane -t datalog -p 2>/dev/null \
    | grep -v '^[[:space:]]*$' \
    | tail -8 \
    | sed 's/^/    /'
else
  echo "  ✗ session not running"
fi
echo

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

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

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

    git worktree add /root/rose-ash-loops/datalog -b loops/datalog architecture
    # ALWAYS patch .mcp.json immediately — fresh worktrees have no _build/,
    # so the relative-path mcp_tree.exe will fail and won't reconnect from
    # inside a running claude. Use the main repo's binary via absolute path:
    sed -i 's|"./hosts/ocaml/_build/default/bin/mcp_tree.exe"|"/root/rose-ash/hosts/ocaml/_build/default/bin/mcp_tree.exe"|' \
      /root/rose-ash-loops/datalog/.mcp.json

  If the tmux session died:

    tmux new-session -d -s datalog -c /root/rose-ash-loops/datalog
    tmux send-keys -t datalog 'claude' Enter
    # wait until the Claude UI box appears, then:
    tmux send-keys -t datalog 'You are the Datalog-on-SX loop runner. Read /root/rose-ash/plans/agent-briefings/datalog-loop.md in full and follow the iteration protocol indefinitely. lib-guest is complete; consume lib/guest/lex.sx, lib/guest/pratt.sx, lib/guest/match.sx, lib/guest/ast.sx wherever they fit — you are the natural first real consumer of ast.sx. Worktree: /root/rose-ash-loops/datalog on branch loops/datalog. Push to origin/loops/datalog after every commit. Never push to main or architecture. Resume from the first unchecked [ ] in plans/datalog-on-sx.md.' Enter Enter

  Note: on first run the loop will hit a permission prompt to read the briefing
  outside the worktree. Press "2" to allow agent-briefings/ for the session.

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