#!/bin/bash
# One fresh agent per build, 6 at a time. Each gets only SKILL.md + its comp.
cd /home/ubuntu/standalone
CONCURRENCY=${CONCURRENCY:-6}
PROMPT='Build ONE comp: builds/SLUG. Read SKILL.md first and follow it exactly. Write src/index.html and src/styles.css that reproduce mock.jpg at 1536x864, render with tools/shot.py, and iterate on the largest error until the MAE plateaus — at least 3 render cycles. Before you finish, verify body.getBoundingClientRect().y is 0 and that shot.py prints no anchor warning. Finish by leaving the best version in place and replying with its MAE.'
run() {
  local slug=$1
  local dir=/home/ubuntu/standalone/builds/$slug
  local log=/tmp/sa-$slug.log
  [ -f "$dir/src/index.html" ] && { echo "skip $slug"; return; }
  echo "start $slug $(date +%H:%M:%S)"
  ( cd "$dir" && timeout 1500 pi -p "${PROMPT/SLUG/$slug}" > "$log" 2>&1 )
  echo "done  $slug $(date +%H:%M:%S)"
}
export -f run; export PROMPT
ls builds | xargs -P "$CONCURRENCY" -I{} bash -c 'run "$@"' _ {}
echo "ALL DONE $(date +%H:%M:%S)"
