#!/usr/bin/env bash
# restore-loop.sh — print recovery state for the js-on-sx loop.
#
# Claude Code can't be driven from shell, so this script shows you where things
# stand and points at the briefing file. To respawn the agent, feed
# plans/agent-briefings/loop.md into Claude via the Agent tool with
# run_in_background=true.
#
# Usage:
#   bash plans/restore-loop.sh          # status snapshot
#   bash plans/restore-loop.sh --print  # also cat the briefing
#
set -uo pipefail

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

echo "=== js-on-sx loop state ==="
echo
echo "Branch:        $(git rev-parse --abbrev-ref HEAD)"
echo "HEAD:          $(git log -1 --oneline)"
echo
echo "Recent commits on lib/js/ and plans/:"
git log -10 --oneline -- lib/js/ plans/js-on-sx.md 2>/dev/null || echo "  (none yet)"
echo
echo "=== Test baseline ==="
if [ -x hosts/ocaml/_build/default/bin/sx_server.exe ]; then
  echo -n "Unit tests:     "; bash lib/js/test.sh 2>&1 | tail -1 || true
  echo -n "Conformance:    "; bash lib/js/conformance.sh 2>&1 | tail -1 || true
else
  echo "sx_server.exe not built — run ./scripts/sx-build-all.sh first."
fi
echo
echo "=== test262 scoreboard ==="
if [ -f lib/js/test262-scoreboard.json ]; then
  echo "  ✓ lib/js/test262-scoreboard.json exists"
  python3 -c "import json;d=json.load(open('lib/js/test262-scoreboard.json'));t=d.get('totals', d.get('overall', {}));print(f\"  totals: {t}\")" 2>/dev/null || true
else
  echo "  ✗ lib/js/test262-scoreboard.json NOT found"
fi
echo
echo "=== regex platform hook ==="
if grep -q js-regex-platform-override lib/js/runtime.sx 2>/dev/null; then
  echo "  ✓ js-regex-platform-override! wired in runtime.sx"
else
  echo "  ✗ js-regex-platform-override! not yet wired"
fi
echo
echo "=== Briefing ==="
for f in plans/agent-briefings/*.md; do
  [ -f "$f" ] && echo "  $f"
done
echo
echo "To respawn: paste plans/agent-briefings/loop.md into Claude via the Agent tool"
echo "            with run_in_background=true. The agent reads the plan's progress"
echo "            log and picks up wherever the queue left off."

if [ "${1:-}" = "--print" ]; then
  echo
  echo "=== Briefing contents ==="
  for f in plans/agent-briefings/*.md; do
    echo
    echo "---- $f ----"
    cat "$f"
  done
fi
