#!/usr/bin/env bash
# gate-w1.sh — doyle's W1 (#249) gate battery at 8d980fdf, run FROM the gate worktree INTO todlando's
# released ws272-w1 pool (claimed for lane gate-w1 02:28Z, NO hatch from here on: owner == this tree
# or the pool guard must refuse — that refusal would itself be a finding).
# Every leg: its own <leg>.exit + <leg>.raw (exit FILES are the record, never the harness line);
# every nextest --no-fail-fast; Summary count read before any FAIL; runs ALL legs (no stop-at-first-red),
# mutation arms only if the baseline local pair was green. Launched by launch-gate.py (proven env scrub).
set -u
GW=/c/Users/decid/Documents/projects/spt-core/.worktrees/gate-w1-26a96d58
POOL='C:\Users\decid\Documents\projects\spt-core\.worktrees\ws272-w1\target'
OUT=$GW/.spt/gate-doyle
mkdir -p "$OUT"; cd "$GW" || exit 9
export CARGO_TARGET_DIR="$POOL"
unset OWL_SESSION_ID SPT_AGENT_ID SPT_ENDPOINT_ID SPT_HOME SPT_POOL_UNCHECKED
env | grep -E '^(OWL_SESSION_ID|SPT_AGENT_ID|SPT_ENDPOINT_ID|SPT_POOL_UNCHECKED)=' > "$OUT/env-leak.txt"; echo "leaked=$(wc -l < "$OUT/env-leak.txt")" >> "$OUT/env-leak.txt"
leg() { # name cmd...
  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"
}
echo "gate-w1 battery at $(git rev-parse --short HEAD) dirty=$(git status --short -uno | wc -l) $(date -u +%FT%TZ)" > "$OUT/driver.log"
echo "procs-before: 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"

leg treqs      traceable-reqs check
leg prebuild   cargo build -p spt --bins -p mock-adapter --bins
leg xtask      cargo run -p xtask -- check
leg clippy     cargo clippy --workspace --all-targets -- -D warnings
leg nextest    cargo nextest run --no-fail-fast -E 'package(spt-net) | (package(spt-store) & kind(lib)) | (package(spt-daemon) & (kind(lib) | binary(dispatch) | binary(twohost_web))) | (package(spt) & binary(webserve_cross_node_e2e))'
leg e2e        cargo nextest run --no-fail-fast --no-capture -p spt --test webserve_cross_node_e2e
leg pair-none  bash .spt/twohost-web-local.sh "$GW" none none
if [ "$(cat "$OUT/pair-none.exit")" = "0" ]; then
  leg pair-mutA bash .spt/twohost-web-local.sh "$GW" mutA A
  leg pair-mutB bash .spt/twohost-web-local.sh "$GW" mutB B
else
  echo "mutation arms SKIPPED: baseline pair red" >> "$OUT/driver.log"
fi
leg mdbook     mdbook build docs-site
echo "[$(date -u +%H:%M:%SZ)] BATTERY DONE" >> "$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"
