#!/usr/bin/env bash
# A/B x migration matrix on the 1200-req trace (sequential, ~47 min each).
#   1. unified           (no A/B, no migration)            anchor
#   2. unified + A+B      (documented champion, no mig)
#   3. unified_v3 + A+B + layer-wise (champion + cheap mig)
# We already have: unified_v3 + layer-wise (no A/B) from the prior run.
#
# Q1 (migration benefit w/ layer-wise): #1 vs prior v3+layerwise(noAB)
# Q2 (does migration add to champion):  #2 vs #3
set -uo pipefail
PROJ_DIR="${PROJ_DIR:-/home/admin/cpfs/wjh/agentic-kv}"
R="$PROJ_DIR/microbench/connector_tax/layerwise/run_v3_trace.sh"
AB="--overload-factor 1.3 --lmetric-decode-weight 0.01"
LOGD=/tmp/dst_break_logs; mkdir -p "$LOGD"

echo "########## 1/3 unified plain ##########"
TAG=unified_plain POLICY=unified MODE=baseline AB_FLAGS="" \
    bash "$R" 2>&1 | tee "$LOGD/abmatrix_1_unified_plain.log" | tail -6

echo "########## 2/3 unified + A+B ##########"
TAG=unified_AB POLICY=unified MODE=baseline AB_FLAGS="$AB" \
    bash "$R" 2>&1 | tee "$LOGD/abmatrix_2_unified_AB.log" | tail -6

echo "########## 3/3 unified_v3 + A+B + layer-wise ##########"
TAG=v3_AB_lw POLICY=unified_v3 MODE=layerwise AB_FLAGS="$AB" \
    bash "$R" 2>&1 | tee "$LOGD/abmatrix_3_v3_AB_lw.log" | tail -6

echo "########## MATRIX DONE ##########"
for t in unified_plain unified_AB v3_AB_lw; do
    D=$(ls -dt "$PROJ_DIR"/outputs/v3trace_${t}_*/unified_v3 2>/dev/null | head -1)
    echo "=== $t ($D) ==="
    sed -n '/\[stats\]/,/\[done\]/p' "$LOGD"/abmatrix_*_${t}.log 2>/dev/null | grep -E "requests:|TTFT|migrations:" || true
done
