#!/usr/bin/env bash
# ARM B PREPARATION — claim, prebuild fixtures + workspace, freeze the selected listing.
# Builds are NOT observations (hertz's protocol); logs kept separate from measurement.
set -u
W="/c/Users/decid/Documents/projects/spt-core/.worktrees/hertz-294-sync-stages"
P="/c/Users/decid/Documents/projects/spt-core/.spt/preserved/deployah-294-armB-20260910"
cd "$W" || exit 90
SEL="$(cat "$P/selection.expr")"

cargo run -p xtask -- pool-claim --pool target --label deployah-armB > "$P/04-claim.log" 2>&1
echo "$?" > "$P/04-claim.exit"

{
  cargo build -p mock-adapter --bin mock-session
  cargo build -p mock-adapter --bin mock-shell
  cargo build -p mock-adapter --bin capture-player
  cargo build -p mock-adapter --bin console-mode-probe
  cargo build -p spt-daemon --bin service_fixture
  cargo build --workspace
} > "$P/05-prebuild-fixtures.log" 2>&1
echo "$?" > "$P/05-prebuild-fixtures.exit"

cargo nextest run --workspace --profile ci-windows --no-fail-fast --success-output immediate \
  -E "$SEL" --no-run > "$P/06-prebuild-tests.log" 2>&1
echo "$?" > "$P/06-prebuild-tests.exit"

cargo nextest list --workspace --profile ci-windows -E "$SEL" \
  > "$P/07-selected-listing.txt" 2>"$P/07-selected-listing.err"
echo "$?" > "$P/07-selected-listing.exit"

grep -c 'two_tier_sync_lands_and_gate_refuses_server_side' "$P/07-selected-listing.txt" > "$P/07-subject-count"
grep -c 'diag_continuation_preserves_original_failure'   "$P/07-selected-listing.txt" > "$P/07-control-count"
grep -cE '^ +[a-zA-Z0-9_:]+$' "$P/07-selected-listing.txt" > "$P/07-selected-total" 2>/dev/null || true

powershell -NoProfile -Command "Get-PSDrive C | Select-Object @{n='FreeGiB';e={[math]::Round(\$_.Free/1GB,2)}} | ConvertTo-Json" > "$P/08-freespace-post.json" 2>&1
date -u +%Y-%m-%dT%H:%M:%SZ > "$P/prep-finished.utc"
