#!/usr/bin/env bash
# Usage: drive.sh <projectPath> <turns>
# Starts a Replay (dev) thread in the project and times each follow-up turn
# (`thread send --wait`), which includes waiting behind the previous turn's checkpoint capture.
set -euo pipefail
H=/tmp/factory-issue-48170-a1796296975-triage/home
cd ~/wt/factory-issue-48170-a1796296975-triage/apps/server
t() { node src/bin.ts "$@" --base-dir "$H"; }
s=$(date +%s.%N)
out=$(t thread start "$1" "hello" --provider devReplay --model multi-turn --wait --json)
e=$(date +%s.%N)
tid=$(echo "$out" | python3 -c 'import json,sys; print(json.load(sys.stdin)["threadId"])')
echo "thread $tid start: $(echo "$e - $s" | bc)s"
for i in $(seq 1 "$2"); do
  s=$(date +%s.%N)
  t thread send "$tid" "follow-up $i" --wait --json > /dev/null
  e=$(date +%s.%N)
  echo "turn $i: $(echo "$e - $s" | bc)s"
done
