set +e
WS=~/actions-runner/_work/spt-core/spt-core
TGT=$WS/target
SHA=9b0025b
TEST=attach_survives_target_brain_restart_exactly_once
cd $WS && git fetch -q origin 2>&1 | tail -1
rm -rf /tmp/p1cgate 2>/dev/null
git worktree add -f --detach /tmp/p1cgate $SHA >/tmp/wt2.log 2>&1 || { echo "WORKTREE-FAIL"; cat /tmp/wt2.log; exit 1; }
cd /tmp/p1cgate
echo "HEAD=$(git rev-parse --short HEAD)"
# warm build once
CARGO_TARGET_DIR=$TGT timeout 300 cargo test -p spt-daemon --test attach $TEST --no-run >/tmp/build.log 2>&1 || { echo "BUILD-FAIL"; tail -5 /tmp/build.log; }
pass=0; fail=0; hang=0
for i in $(seq 1 20); do
  R=$(CARGO_TARGET_DIR=$TGT timeout 90 cargo test -p spt-daemon --test attach $TEST -- --test-threads=1 2>&1); C=$?
  pkill -9 -f "attach-" 2>/dev/null
  if [ $C -eq 124 ]; then hang=$((hang+1)); echo "iter $i: HANG";
  elif echo "$R" | grep -q "test result: ok"; then pass=$((pass+1)); echo "iter $i: PASS";
  else fail=$((fail+1)); echo "iter $i: FAIL ($(echo "$R" | grep -oiE 'output gap[^"]*|panicked at [^ ]+' | head -1))"; fi
done
echo "=== 20x RESULT: PASS=$pass FAIL=$fail HANG=$hang ==="
cd $WS && git worktree remove --force /tmp/p1cgate 2>/dev/null
echo "GATE20X-DONE"
