#!/usr/bin/env bash
# Spawn a single claude session to implement SX primitives in sequence.
# Runs in its own git worktree on branch sx-primitives from architecture.
#
# Usage: ./scripts/sx-primitives-up.sh [interval]
#   interval defaults to self-paced (omit to let model decide)
#
# After the script prints done:
#   tmux a -t sx-primitives
#   Ctrl-B + d to detach
#
# Stop: ./scripts/sx-primitives-down.sh
set -euo pipefail

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT"
SESSION="sx-primitives"
WORKTREE="$ROOT"          # runs in the main worktree — architecture branch
BRANCH="architecture"
INTERVAL="${1:-}"
BOOT_WAIT=20

if tmux has-session -t "$SESSION" 2>/dev/null; then
  echo "Session '$SESSION' already exists."
  echo "  Attach:  tmux a -t $SESSION"
  echo "  Kill:    ./scripts/sx-primitives-down.sh"
  exit 1
fi

# Write settings into the main worktree .claude dir
SETTINGS_DIR="$ROOT/.claude"
mkdir -p "$SETTINGS_DIR"
cat > "$SETTINGS_DIR/settings.local.json" <<'SETTINGS'
{
  "permissions": {
    "allow": [
      "mcp__sx-tree__sx_summarise",
      "mcp__sx-tree__sx_read_tree",
      "mcp__sx-tree__sx_read_subtree",
      "mcp__sx-tree__sx_get_context",
      "mcp__sx-tree__sx_find_all",
      "mcp__sx-tree__sx_find_across",
      "mcp__sx-tree__sx_get_siblings",
      "mcp__sx-tree__sx_validate",
      "mcp__sx-tree__sx_replace_node",
      "mcp__sx-tree__sx_insert_child",
      "mcp__sx-tree__sx_insert_near",
      "mcp__sx-tree__sx_delete_node",
      "mcp__sx-tree__sx_wrap_node",
      "mcp__sx-tree__sx_rename_symbol",
      "mcp__sx-tree__sx_replace_by_pattern",
      "mcp__sx-tree__sx_rename_across",
      "mcp__sx-tree__sx_write_file",
      "mcp__sx-tree__sx_pretty_print",
      "mcp__sx-tree__sx_eval",
      "mcp__sx-tree__sx_harness_eval",
      "mcp__sx-tree__sx_macroexpand",
      "mcp__sx-tree__sx_trace",
      "mcp__sx-tree__sx_deps",
      "mcp__sx-tree__sx_diff",
      "mcp__sx-tree__sx_diff_branch",
      "mcp__sx-tree__sx_changed",
      "mcp__sx-tree__sx_blame",
      "mcp__sx-tree__sx_build",
      "mcp__sx-tree__sx_build_manifest",
      "mcp__sx-tree__sx_build_bytecode",
      "mcp__sx-tree__sx_test",
      "mcp__sx-tree__sx_format_check",
      "mcp__sx-tree__sx_comp_list",
      "mcp__sx-tree__sx_comp_usage",
      "mcp__sx-tree__sx_nav",
      "mcp__sx-tree__sx_env",
      "mcp__sx-tree__sx_playwright",
      "mcp__hs-test__hs_test_run",
      "mcp__hs-test__hs_test_regen",
      "mcp__hs-test__hs_test_kill",
      "mcp__hs-test__hs_test_status",
      "Bash(node *)",
      "Bash(python3 *)",
      "Bash(bash *)",
      "Bash(cp *)",
      "Bash(git *)",
      "Bash(tmux *)"
    ]
  },
  "enabledMcpjsonServers": [
    "sx-tree",
    "rose-ash-services",
    "hs-test"
  ]
}
SETTINGS

echo "Creating tmux session '$SESSION' in $ROOT ..."
tmux new-session -d -s "$SESSION" -n "primitives" -c "$ROOT"

echo "Starting claude..."
tmux send-keys -t "$SESSION:primitives" "claude" C-m

echo "Waiting ${BOOT_WAIT}s for claude to boot..."
sleep "$BOOT_WAIT"

if [ -n "$INTERVAL" ]; then
  preamble="/loop $INTERVAL "
else
  preamble="/loop "
fi

cmd="${preamble}Read plans/agent-briefings/primitives-loop.md and do ONE step per fire: find the first unchecked [ ] task, implement it fully, run the relevant tests to verify, commit with a short factual message, push to origin/architecture, tick the box [x] in the plan, append one dated line to the Progress log (newest first), then stop. You are on branch architecture in /root/rose-ash. Use sx-tree MCP for all .sx edits. Never push to main."

tmux send-keys -t "$SESSION:primitives" "$cmd"
sleep 0.5
tmux send-keys -t "$SESSION:primitives" Enter

echo ""
echo "Done. SX primitives loop started in tmux session '$SESSION'."
echo ""
echo "  Attach:    tmux a -t $SESSION"
echo "  Detach:    Ctrl-B d"
echo "  Stop:      ./scripts/sx-primitives-down.sh"
echo ""
