set +e
GUARD=re_serve_resets_resume_cursor_after_a_pre_attached_consume
cd ~/actions-runner/_work/spt-core/spt-core
rm -rf /tmp/p1cvr2 /tmp/p1cvr2_tgt
git worktree add -f --detach /tmp/p1cvr2 bdb51c2 >/dev/null 2>&1 || { echo WT-FAIL; exit 1; }
cd /tmp/p1cvr2
echo "=== baseline (unpatched) in DEDICATED target — guard must PASS ==="
DTGT=/tmp/p1cvr2_tgt
CARGO_TARGET_DIR=$DTGT timeout 400 cargo test -p spt-daemon --test broker $GUARD -- --exact 2>&1 | grep -E "test result|FAILED" | head -2
echo "=== now PATCH brain.rs (disable subscribe_with reset) + REBUILD in same dedicated target ==="
sed -i 's|self.session_cursors.insert(session_id, from_seq);|/* REVERT-TEST */|' crates/spt-daemon/src/brain.rs
echo "patched lines: $(grep -c 'REVERT-TEST' crates/spt-daemon/src/brain.rs)"
# touch to force fingerprint change
rr=0
for i in 1 2 3; do
  R=$(CARGO_TARGET_DIR=$DTGT timeout 200 cargo test -p spt-daemon --test broker $GUARD -- --exact 2>&1)
  echo "$R" | grep -q "test result: ok" && echo "revert $i: PASS(BAD)" || { rr=$((rr+1)); echo "revert $i: FAIL-RED ($(echo "$R"|grep -oE 'panicked at[^,]*|must reset|advanced to'|head -1))"; }
done
echo "REVERT_RED=$rr/3"
cd ~/actions-runner/_work/spt-core/spt-core && git worktree remove --force /tmp/p1cvr2 2>/dev/null; git worktree prune
rm -rf /tmp/p1cvr2_tgt
echo "REVERT-CLEAN-DONE"
