#!/bin/bash
# Supervised wrapper around score.py.
#
# score.py has been resumable since the first line of it — append-only JSONL, keyed by passage
# id, skip what is already there. That design was load-bearing in theory and inert in practice,
# because nothing ever restarted it. On 2026-08-28 the hello scan stopped at 2,384 of 12,247:
# no traceback, no partial line, a count landing exactly on a batch boundary. That is not a
# Python error, that is the kernel taking the process — one core, ~2 GB, no swap. The chain
# waiting on its "done" marker would have waited forever, and a crashed writer and a finished
# writer leave the same file behind.
#
# So: retry until the output file has as many lines as the input, then emit the marker. The
# resume is exact rather than approximate — score.py shuffles under the fixed seed *before*
# subtracting the skip-set, so a resumed run continues along the same order it was already
# walking. The prefix property that makes a partial scan publishable survives a restart.
#
#   ./run_scan.sh <hello|openai> <corpus|control> <logfile>
set -u
cd /home/agent/work/aidetect-fpr

key="$1"; mode="$2"; log="$3"
if [ "$mode" = "control" ]; then
  flag="--control"; src="control.jsonl"; out="scores_control_${key}.jsonl"
else
  flag=""; src="passages.jsonl"; out="scores_${key}.jsonl"
fi

want=$(wc -l < "$src")
for i in $(seq 1 30); do
  have=0; [ -f "$out" ] && have=$(wc -l < "$out")
  [ "$have" -ge "$want" ] && break
  echo "[run_scan] $key/$mode attempt $i, $have/$want already scored" >> "$log"
  python3 score.py "$key" $flag >> "$log" 2>&1
  sleep 10
done

have=0; [ -f "$out" ] && have=$(wc -l < "$out")
if [ "$have" -ge "$want" ]; then
  # score.py returns early and silently when the skip-set already covers everything, so the
  # marker has to be written here too — otherwise a scan that finished on its last attempt
  # would leave a waiter blocked on a file that will never gain the line it wants.
  grep -q '^done$' "$log" 2>/dev/null || echo done >> "$log"
  echo "[run_scan] $key/$mode complete: $have/$want" >> "$log"
else
  echo "[run_scan] $key/$mode GAVE UP at $have/$want after 30 attempts" >> "$log"
  exit 1
fi
