#!/usr/bin/env bash
# gate-w1d.sh — re-gate at 9489ef60 (hertz's B-witness fixup on top of 8d980fdf). Product path touched
# (dispatch.rs), so: treqs, prebuild, clippy, the spt-daemon + e2e nextest slice, local pair + arms
# (arms only when BOTH baseline role exit FILES read 0), cross-box baseline. mdbook skipped (docs untouched).
set -u
GW=/c/Users/decid/Documents/projects/spt-core/.worktrees/gate-w1-26a96d58
OUT=$GW/.spt/gate-doyle; PAIR=$GW/.spt/twohost-web; cd "$GW" || exit 9
export CARGO_TARGET_DIR='C:\Users\decid\Documents\projects\spt-core\.worktrees\ws272-w1\target'
unset OWL_SESSION_ID SPT_AGENT_ID SPT_ENDPOINT_ID SPT_HOME SPT_POOL_UNCHECKED
leg() { local n=$1; shift; echo "[$(date -u +%H:%M:%SZ)] LEG $n START" >> "$OUT/driver.log"; "$@" > "$OUT/$n.raw" 2>&1; local e=$?; echo "$e" > "$OUT/$n.exit"; echo "[$(date -u +%H:%M:%SZ)] LEG $n EXIT $e summaries=$(grep -c 'Summary \[' "$OUT/$n.raw") panics=$(grep -c 'panicked at' "$OUT/$n.raw") free_gb=$(df -BG --output=avail /c 2>/dev/null | tail -1 | tr -dc 0-9)" >> "$OUT/driver.log"; }
pair_ok() { [ "$(cat "$PAIR/$1/a.exit" 2>/dev/null)" = "0" ] && [ "$(cat "$PAIR/$1/b.exit" 2>/dev/null)" = "0" ]; }
echo "gate-w1d re-gate at $(git rev-parse --short HEAD) dirty=$(git status --short -uno | wc -l) $(date -u +%FT%TZ) treqs=$(traceable-reqs --version)" >> "$OUT/driver.log"
leg d-treqs     traceable-reqs check
leg d-prebuild  cargo build -p spt -p mock-adapter --bins
leg d-clippy    cargo clippy --workspace --all-targets -- -D warnings
leg d-nextest   cargo nextest run --no-fail-fast -E '(package(spt-daemon) & (kind(lib) | binary(dispatch) | binary(twohost_web))) | (package(spt) & binary(webserve_cross_node_e2e))'
leg d-pair-none bash .spt/twohost-web-local.sh "$GW" d-none none
if pair_ok d-none; then
  leg d-pair-mutA bash .spt/twohost-web-local.sh "$GW" d-mutA A
  leg d-pair-mutB bash .spt/twohost-web-local.sh "$GW" d-mutB B
else
  echo "[$(date -u +%H:%M:%SZ)] mutation arms SKIPPED: baseline d-none a.exit=$(cat "$PAIR/d-none/a.exit" 2>/dev/null) b.exit=$(cat "$PAIR/d-none/b.exit" 2>/dev/null)" >> "$OUT/driver.log"
fi
WAIT=300 leg d-xbox-none bash .spt/twohost-web-xbox.sh "$GW" /home/reavus/spt-w1/.worktrees/ws272-w1 d-none none
echo "[$(date -u +%H:%M:%SZ)] REGATE DONE dirty=$(git status --short -uno | wc -l)" >> "$OUT/driver.log"
echo "procs-after: cargo=$(tasklist | grep -ci '^cargo') nextest=$(tasklist | grep -ci nextest) spt_from_pool=$(wmic process where "ExecutablePath like '%ws272-w1%'" get ProcessId 2>/dev/null | grep -c '[0-9]')" >> "$OUT/driver.log"
