#!/usr/bin/env bash
# Usage: run_side.sh <worktree> <label>
# Adds the scratch project to that worktree's running dev server and runs one agent turn
# that calls preview_open + preview_snapshot on the synthetic 2,000-row page.
set -euo pipefail
wt="$1"; label="$2"
out="/tmp/issue-lead-33743-r1/runtime/$label-$(date +%Y%m%dT%H%M%S)"
mkdir -p "$out"
cd "$wt/apps/server"
node src/bin.ts project add --base-dir "$wt/.t3" /tmp/issue-lead-33743-r1/runtime/project > "$out/project.txt" 2>&1 || true
cat > "$out/prompt.txt" <<'EOF'
Call the preview_open tool with url "http://127.0.0.1:18961/big/" and open false. Then call preview_snapshot on that tab with includeImage false. Do not call any other tool. Reply with exactly three lines: the length in characters of the visibleText field, the first 60 characters of visibleText, and the omitted array verbatim as JSON.
EOF
node src/bin.ts thread start --base-dir "$wt/.t3" --provider claudeAgent --model <cheapest model> --title "issue-33743 $label" --wait /tmp/issue-lead-33743-r1/runtime/project - < "$out/prompt.txt" > "$out/thread.txt" 2>&1 || echo "thread exit $?" >> "$out/thread.txt"
echo "$out"
