#!/usr/bin/env bash
# ds_ab.sh RUN -- console build-axis A/B of the prod vision6 recipe on the 110 VM's RTX 4090, through the committed
# harness (scripts/bench/console_build_ab.sh): A = tree_a (fix/exl3-head-stock-call-sites, the base) -> B = repo (the fix),
# then the null control B -> NULL (tree_n: B plus one comment line). Census method on top of compose.dflash.yaml:
# kernels from the census share, stale image bakes masked, TKV_NO_JIT=1, fail-loud nvcc/ninja stubs.
# Boot cache: the head-route agent's, shared by the owner's decision; flat-weights dump dropped at the end.
set -uo pipefail
W=$HOME/draftersites; RUN=$W/ab/$1; mkdir -p $RUN $W/ab/{home-cache,inductor,bench-store}; chmod 777 $W/ab/bench-store
IMG=local/arbi-serve:slim-e0197dd-ta0841; SP=/opt/venv/lib/python3.12/site-packages
p=$(df --output=pcent / | tail -1 | tr -dc 0-9); echo "root $p% before the run"; [ "$p" -gt 83 ] && { echo "not booting"; exit 1; }
T=$W/repo; OVL=(-v $T/arbi_serve:$SP/arbi_serve:ro)
MASK=(); MASKV=(); for it in 1 2 3 4; do
  found=$(docker run --rm "${OVL[@]}" "${MASKV[@]}" -v $W/scripts/s3c_stale.py:/s/st.py:ro -e S3C_CAP=8,9 -e CUDA_VISIBLE_DEVICES= \
    --entrypoint /opt/venv/bin/python "$IMG" -I /s/st.py 2>/dev/null); [ -z "$found" ] && break
  for q in $found; do MASK+=("$q"); MASKV+=(-v "$W/empty:$q:ro"); done; done
{ cat <<Y
# LOCAL, UNCOMMITTED: 110 VM overlay for the prod vision6 recipe (census method), generated by ds_ab.sh.
services:
  arbi-serve:
    user: "1000:1000"
    mem_limit: 26g
    environment:
      ARBI_REASONING_EFFORT: ""
      TKV_NO_JIT: "1"
      ARBI_EXT_SHARE_DIR: /ext-cache
      ARBI_BOOT_CACHE_DIR: /cache/arbi-serve
      ARBI_BOOT_CACHE_ISOLATED: "1"
      ARBI_BOOT_CACHE_MAX_GB: "6"
      ARBI_ASSERT_WARM_CACHE: "0"
      TKV_CALIBRATION_CORPUS_DIR: /mnt/k8scache/calibration-corpus/real-corpus-v2
      ARBI_ENABLE_VISION: "1"
      ARBI_SERVE_LOGPROBS: sampled
      CUBLAS_WORKSPACE_CONFIG: ":1024:1"
      ARBI_PREFIX_TIER: "host:4"
    volumes:
      - $W/scripts/stubs:/stubs:ro
      - $W/scripts/stubs/nvcc:/usr/local/cuda-13.1/bin/nvcc:ro
      - $W/scripts/stubs/fail:/usr/bin/ninja:ro
      - $HOME/headroute/cache:/cache
      - $W/ab/home-cache:/home/arbi/.cache
      - $W/ab/inductor:/tmp/torchinductor_arbi
Y
for q in "${MASK[@]}"; do echo "      - $W/empty:$q:ro"; done
cat <<Y
  arbi-console:
    user: "1000:1000"
    volumes:
      - $HOME/headroute/cache:/cache
Y
} > $T/compose.ds-local.yaml
echo "masked ${#MASK[@]} stale bakes"
cd $T
set -a
ARBI_IMAGE=$IMG
ARBI_MODELS_ROOT=/mnt/k8scache/models ARBI_CALIBRATIONS_ROOT=/mnt/k8scache/calibrations
ARBI_BENCH_RESULTS_ROOT=$W/ab/bench-store ARBI_BENCH_STORE=$W/ab/bench-store ARBI_EXT_SHARE_ROOT=$W/share
ARBI_MODEL=/models/Qwen3.8-27B-exl3-4.0bpw-vision6 ARBI_DFLASH_DRAFT=/models/Qwen3.8-27B-DFlash2-exl3-6.0bpw
ARBI_CALIBRATION_FILE=/models/Qwen3.8-27B-exl3-4.0bpw-vision6/kv_calibration.json
ARBI_MAX_BATCH=4 ARBI_MTP_K=7 ARBI_DRAFT_VOCAB_PREFIX=half ARBI_DRAFT_VOCAB_TAIL=derive ARBI_HF_OFFLINE=1
ARBI_PORT=19742 ARBI_CONSOLE_HOST_PORT=19743 ARBI_GPU_DEVICE=0 ARBI_RESTART_POLICY=no
ARBI_CONTAINER_NAME=ds-ab-engine ARBI_CONSOLE_CONTAINER_NAME=ds-ab-console
set +a
export ARBI_AB_RECIPE=compose.dflash.yaml ARBI_AB_OVERLAYS=compose.ds-local.yaml
export COMPOSE_PROJECT_NAME=ds-ab ARBI_AB_BOOT_WAIT_S=5400
export ARBI_AB_CONCURRENCY=${ARBI_AB_CONCURRENCY:-1,4} ARBI_AB_REPS=${ARBI_AB_REPS:-3}
export GPU_LOCK_WAIT_S=43200
source $T/scripts/ci/gpu_lock.sh; gpu_lock 0 || exit 1
nvidia-smi --query-gpu=pstate,power.limit,temperature.gpu,memory.used --format=csv,noheader | tee $RUN/gpu-before.txt
bash scripts/bench/console_build_ab.sh "$RUN" A=$W/tree_a B=$T NULL=$W/tree_n
rc=$?
nvidia-smi --query-gpu=pstate,power.limit,temperature.gpu,power.draw --format=csv,noheader | tee $RUN/gpu-after.txt
docker logs ds-ab-engine > $RUN/engine-last.log 2>&1
bash scripts/bench/console_build_ab.sh down
rm -rf $HOME/headroute/cache/arbi-serve/flat-weights; echo "root $(df --output=pcent / | tail -1 | tr -dc 0-9)% after; rc=$rc"
echo AB_DONE rc=$rc
exit $rc
