#!/usr/bin/env bash
# Served 27B vision A/B on box 201 card 0 (4090, TP1): console build axis, the vision agent's harness (#5120).
# Arms: FUSED = branch tree, MAIN = main tip tree, NULL = main tip + one comment line.
# effect = FUSED -> MAIN, control = MAIN -> NULL (main vs main). usage: ab201.sh RUN_NAME MEDIA_JSON
set -uo pipefail
W=/home/arbi-admin/vfuse/ab; N=/mnt/k8scache/agent-snapshots/exl3-vision-fuse-20261112; IMG=local/arbi-serve:main-43d0876
RUN=$W/runs/$1; MEDIA=$2
mkdir -p $W/abcache/home-cache $W/abcache/inductor $W/abcache/jit $W/cache-FUSED $W/cache-MAIN $W/cache-NULL $W/runs $W/bench-store "$RUN"
chmod -R a+rwX $W
( flock 9
  echo "lock card 0 for $1 $(date -Is); root $(df -h / | tail -1)"
  for n in FUSED MAIN NULL; do
    rm -rf $W/cache-$n/arbi-serve/flat* 2>/dev/null
    printf 'ARBI_BOOT_CACHE_DIR=/cache/arm-%s/arbi-serve\nTORCHINDUCTOR_COMPILE_THREADS=1\n' "$n" > "$RUN/engine-$n.env"
    echo "ARBI_ENV_FILE=$RUN/engine-$n.env" > "$RUN/arm-$n.env"
  done
  chmod -R a+rX "$RUN"; chmod 777 $W/bench-store
  cd $N/repo
  set -a
  ARBI_IMAGE=$IMG
  ARBI_MODELS_ROOT=/mnt/k8scache/models ARBI_CALIBRATIONS_ROOT=/mnt/k8scache/calibrations
  ARBI_BENCH_RESULTS_ROOT=$W/bench-store ARBI_BENCH_STORE=$W/bench-store
  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=8 ARBI_MTP_K=7 ARBI_DRAFT_VOCAB_PREFIX=full ARBI_DRAFT_VOCAB_TAIL=derive
  ARBI_PORT=19653 ARBI_CONSOLE_HOST_PORT=19713 ARBI_GPU_DEVICE=0 ARBI_TP_SIZE=1 ARBI_RESTART_POLICY=no
  ARBI_ENV_FILE=$RUN/engine-MAIN.env
  ARBI_CONTAINER_NAME=vfuse-engine ARBI_CONSOLE_CONTAINER_NAME=vfuse-console
  ARBI_DOCKER_GID=$(stat -c %g /var/run/docker.sock)
  set +a
  export ARBI_AB_RECIPE=compose.dflash.yaml ARBI_AB_OVERLAYS=vfuse201.yaml
  export COMPOSE_PROJECT_NAME=vfuse ARBI_AB_BOOT_WAIT_S=5400 TORCHINDUCTOR_COMPILE_THREADS=1
  export ARBI_AB_MEDIA="$MEDIA" ARBI_AB_MEDIA_REPEAT=distinct
  export ARBI_AB_CONCURRENCY=${ARBI_AB_CONCURRENCY:-1,4} ARBI_AB_REPS=${ARBI_AB_REPS:-12} ARBI_AB_PREFILL=${ARBI_AB_PREFILL:-256} ARBI_AB_MAX_TOKENS=0
  # Every engine boot's own log and the container's lifecycle events, kept per boot: the console restarts
  # the engine per arm, and only the last boot's log survives in the container otherwise.
  mkdir -p $N/ab/$1-boots
  ( docker events --filter container=vfuse-engine --filter container=vfuse-console --format '{{.Time}} {{.Actor.Attributes.name}} {{.Action}} exit={{.Actor.Attributes.exitCode}} sig={{.Actor.Attributes.signal}}' | grep --line-buffered -v exec_ > $N/ab/$1-boots/events.log ) &
  EV=$!
  ( while :; do docker logs -f vfuse-engine > $N/ab/$1-boots/engine-$(date +%H%M%S).log 2>&1; sleep 3; done ) &
  LG=$!
  bash scripts/bench/console_build_ab.sh "$RUN" FUSED=$N/treeB MAIN=$N/treeA NULL=$N/treeNULL
  rc=$?
  docker logs vfuse-engine > "$RUN/last-engine.log" 2>&1
  docker logs vfuse-console > "$RUN/console.log" 2>&1
  bash scripts/bench/console_build_ab.sh down
  kill $EV $LG 2>/dev/null; pkill -f "docker events --filter container=vfuse-engine" 2>/dev/null; pkill -f "docker logs -f vfuse-engine" 2>/dev/null
  find $N/ab/$1-boots -name "engine-*.log" -size -200c -delete
  rm -rf $W/cache-*/arbi-serve/flat* 2>/dev/null
  mkdir -p $N/ab; cp -r "$RUN" $N/ab/; cp -r $W/bench-store $N/ab/ 2>/dev/null
  echo "$1 rc=$rc $(date -Is); root $(df -h / | tail -1)"
  exit $rc
) 9</run/lock/arbi-gpu-0.lock
