set +e
WS=~/actions-runner/_work/spt-core/spt-core
TGT=$WS/target
TEST="attach_survives_target_brain_restart_exactly_once"
run_one() { # $1=label
  R=$(timeout 150 cargo test -p spt-daemon --test attach $TEST -- --test-threads=1 2>&1); C=$?
  pkill -9 -f "attach-8cbac7c" 2>/dev/null
  if [ $C -eq 124 ]; then echo "$1: HANG"
  elif echo "$R" | grep -q "test result: ok"; then echo "$1: PASS"
  else echo "$1: FAIL ($(echo "$R" | grep -oiE 'output gap[^"]*|panicked at [^ ]+' | head -1))"; fi
}
cd $WS
echo "=== POST-P1b (fa9d047, current workspace src) ==="
run_one "post-P1b#1"; run_one "post-P1b#2"
echo "=== PRE-P1b (f7cf380 src via worktree, shared target) ==="
rm -rf /tmp/sptp1b 2>/dev/null
git worktree add -f --detach /tmp/sptp1b f7cf380 >/tmp/wt.log 2>&1 || { echo "worktree-add FAILED"; cat /tmp/wt.log; }
cd /tmp/sptp1b
CARGO_TARGET_DIR=$TGT run_one_pre() { :; }
R=$(CARGO_TARGET_DIR=$TGT timeout 200 cargo test -p spt-daemon --test attach $TEST -- --test-threads=1 2>&1); C=$?
pkill -9 -f "attach-8cbac7c" 2>/dev/null
if [ $C -eq 124 ]; then echo "pre-P1b#1: HANG"; elif echo "$R" | grep -q "test result: ok"; then echo "pre-P1b#1: PASS"; else echo "pre-P1b#1: FAIL ($(echo "$R" | grep -oiE 'output gap[^"]*|panicked at [^ ]+|error\[' | head -1))"; fi
R=$(CARGO_TARGET_DIR=$TGT timeout 200 cargo test -p spt-daemon --test attach $TEST -- --test-threads=1 2>&1); C=$?
pkill -9 -f "attach-8cbac7c" 2>/dev/null
if [ $C -eq 124 ]; then echo "pre-P1b#2: HANG"; elif echo "$R" | grep -q "test result: ok"; then echo "pre-P1b#2: PASS"; else echo "pre-P1b#2: FAIL ($(echo "$R" | grep -oiE 'output gap[^"]*|panicked at [^ ]+|error\[' | head -1))"; fi
cd $WS; git worktree remove --force /tmp/sptp1b 2>/dev/null
echo "DIFFERENTIAL-DONE"
