#!/usr/bin/env bash
# doyle second-producer gate for todlando PR #234 (CLI spawn custody) @__SHA__ (rebased on hertz #233 ff).
# Pool: sequential takeover of todlando's RELEASED lane pool (__POOL__) — claim leg hatched (SPT_POOL_UNCHECKED=1,
# --foreign-pool), every later leg builds through the claimed pool unhatched.
# Env mirrors golden.yml's Windows test job block; perch identity scrubbed (IR-138 list).
set -u
W=/c/Users/decid/Documents/projects/spt-core/.worktrees/gate-307-__SHA8__
OUT=/c/Users/decid/Documents/projects/spt-core/.spt/preserved/307/gate-307-__SHA8__
cd "$W" || exit 90
export CARGO_TARGET_DIR="__POOL__"
export SPT_TEST_EPHEMERAL_ADVISORY_PORTS=1
export SPT_ATTACH_GATE_WATCHDOG_MS=120000
export SPT_ATTACH_IPC_DEADLINE_MS=30000
export SPT_REFRESH_RIG_PYTHON='C:\Program Files\Python312\python.exe'
unset OWL_SESSION_ID SPT_ENDPOINT_ID SPT_SESSION_NAME SPT_ADAPTER SPT_HOST_PID SPT_INJECT_VERIFY_ECHO
env | grep -E '^(OWL_|SPT_|CARGO_TARGET_DIR)' > "$OUT/env-scrub-check.txt"
git rev-parse HEAD > "$OUT/head.txt"; git status --porcelain >> "$OUT/head.txt"
S="$OUT/status.txt"; echo "start $(date -u +%FT%TZ)" > "$S"
tasklist | grep -iE 'cargo|rustc|Runner\.Worker|nextest' > "$OUT/census-before.txt" || echo none > "$OUT/census-before.txt"
leg() { local n=$1; shift; "$@" > "$OUT/$n.log" 2>&1; local rc=$?; echo "$n exit $rc $(date -u +%TZ)" >> "$S"; grep -hE '^ *Summary \[' "$OUT/$n.log" | sed "s/^/$n: /" >> "$OUT/summaries.txt"; return $rc; }
NX=(cargo nextest run --locked --profile ci-windows --test-threads 1 --retries 0 --no-fail-fast --success-output immediate)
leg pool-claim env SPT_POOL_UNCHECKED=1 cargo run -q -p xtask -- pool-claim --pool "$CARGO_TARGET_DIR" --label gate-307-__SHA8__ --foreign-pool
leg build    cargo build --locked -p spt -p spt-daemon --tests --bins
leg clippy   cargo clippy --locked --workspace --all-targets -- -D warnings
leg custody  "${NX[@]}" -p spt --test cli_resume_custody -E 'all()'
leg rcattach "${NX[@]}" -p spt --test rc_attach_truth -E 'all()'
leg wedge    "${NX[@]}" -p spt --test attach_wedge_e2e -E 'all()'
leg selfheal "${NX[@]}" -p spt-daemon --test driven_by_selfheal -E 'all()'
leg livehost "${NX[@]}" -p spt-daemon --lib -E 'test(/pull_liveness|custody|resume_in_flight|reconcile/)'
leg storeunit "${NX[@]}" -p spt-store --lib -E 'test(/custody/)'
leg trace    traceable-reqs check
leg pool-release cargo run -q -p xtask -- pool-release --pool "$CARGO_TARGET_DIR" --label gate-307-__SHA8__
tasklist | grep -iE 'cargo|rustc|nextest' > "$OUT/census-after.txt" || echo none > "$OUT/census-after.txt"
echo "end $(date -u +%FT%TZ)" >> "$S"
echo DONE > "$OUT/DONE"
