set +e
TGT=~/actions-runner/_work/spt-core/spt-core/target
T=attach_survives_target_brain_restart_exactly_once
cd ~/actions-runner/_work/spt-core/spt-core
# ── 20x clean-fix gate @0dc7018 ──
rm -rf /tmp/p1cfix; git worktree add -f --detach /tmp/p1cfix 0dc7018 >/dev/null 2>&1 || { echo WT-FAIL-CLEAN; exit 1; }
cd /tmp/p1cfix; echo "CLEAN HEAD=$(git rev-parse --short HEAD)"
CARGO_TARGET_DIR=$TGT timeout 300 cargo test -p spt-daemon --test attach $T --no-run >/tmp/vb.log 2>&1 || { echo BUILD-FAIL; tail -6 /tmp/vb.log; exit 1; }
p=0;f=0;h=0
for i in $(seq 1 20); do
  R=$(CARGO_TARGET_DIR=$TGT timeout 90 cargo test -p spt-daemon --test attach $T -- --test-threads=1 2>&1); C=$?
  pkill -9 -f attach- 2>/dev/null
  if [ $C -eq 124 ]; then h=$((h+1)); echo "iter $i: HANG"
  elif echo "$R" | grep -q "test result: ok"; then p=$((p+1)); echo "iter $i: PASS"
  else f=$((f+1)); echo "iter $i: FAIL($(echo "$R"|grep -oE 'attach.rs:[0-9]+|output gap[^\"]*'|head -1))"; fi
done
echo "=== CLEAN-FIX 20x: PASS=$p FAIL=$f HANG=$h ==="
cd ~/actions-runner/_work/spt-core/spt-core && git worktree remove --force /tmp/p1cfix 2>/dev/null
# ── positive confirm on instrumented @9efc4e4 (up to 8 runs, capture one showing seq-K forwarded + no gap) ──
rm -rf /tmp/p1cic; git worktree add -f --detach /tmp/p1cic 9efc4e4 >/dev/null 2>&1
cd /tmp/p1cic
CARGO_TARGET_DIR=$TGT timeout 300 cargo test -p spt-daemon --test attach $T --no-run >/dev/null 2>&1
for i in $(seq 1 8); do
  R=$(CARGO_TARGET_DIR=$TGT SPT_REORDER_DIAG=1 timeout 60 cargo test -p spt-daemon --test attach $T -- --exact --nocapture 2>&1)
  pkill -9 -f attach- 2>/dev/null
  echo "$R" | grep -q "FORWARD GAP" && echo "INSTR iter $i: GAP-SEEN"
  echo "$R" | grep -q "test result: ok" || { echo "INSTR iter $i: FAIL"; }
done > /tmp/instr_summary.txt 2>&1
# capture the last run's diag for inspection
echo "$R" | grep -E "RACEDIAG|boundary K|FORWARD GAP|forward Output seq=1" > /tmp/confirm.diag 2>&1
cat /tmp/instr_summary.txt
echo "=== INSTR CONFIRM (no GAP-SEEN = fix holds; serve forward seq=1 present = positive) ==="
cd ~/actions-runner/_work/spt-core/spt-core && git worktree remove --force /tmp/p1cic 2>/dev/null
echo VERIFY-DONE
