#!/usr/bin/env bash
# Gate driver for the #234 respin delta at 1a7167e1 (doyle).
# Per-leg exit FILES are the verdicts; this wrapper's own status is meaningless
# by design (exit-status-after-capture rule). Logs + exits land beside this script.
set +e
cd "$(dirname "$0")" || exit 9
export CARGO_INCREMENTAL=0
export SPT_HOME=""
unset SPT_HOME OWL_SESSION_ID SPT_AGENT_ID SPT_ENDPOINT_ID

leg() {
  local name="$1"; shift
  echo "=== LEG $name: $* ($(date -u +%H:%M:%SZ)) ==="
  "$@" > "G234_${name}.log" 2>&1
  echo $? > "G234_${name}.exit"
  echo "=== LEG $name exit $(cat G234_${name}.exit) ($(date -u +%H:%M:%SZ)) ==="
}

# 1. xtask first (fresh pool; claim tool builds through this pool)
leg xtask_build cargo build -p xtask
# 2. claim the fresh pool from this lane's own cwd
leg pool_claim cargo run -p xtask -- pool-claim --pool target --label gate-234-respin
# 3. fixture prereq: fresh-pool trap — build workspace bins ONCE
leg bins_build cargo build --workspace --bins
# 4. the new leg (the point of the respin), serialized as its own header says
leg newleg cargo nextest run -p spt --test io_events_undriven_kinds_e2e --test-threads 1
# 5. the sibling cell whose coverage justified the exclusion ruling
leg polleg cargo nextest run -p spt --test io_events_poll_e2e --test-threads 1
# 6. unit populations the diff touches
leg daemon_units cargo nextest run -p spt-daemon --lib
leg store_units cargo nextest run -p spt-store --lib
# 7. workspace clippy, deny warnings
leg clippy cargo clippy --workspace --all-targets -- -D warnings
# 8. traceability
leg treqs traceable-reqs check
echo "DRIVER DONE $(date -u +%H:%M:%SZ)"
