#!/usr/bin/env bash
# 5-policy comparison on the first-600s trace, fresh vLLM (cold APC) each arm.
#   leastwork    LPWL (parameter-free)
#   unified_ab   unified hybrid, A+B' tuned (of=1.3, lmw=0.01)
#   unified_def  unified hybrid, defaults  (of=2.0, lmw=0.0)
#   lmetric      P_tokens x BS, no affinity
#   sticky       hard session affinity
set -uo pipefail
export ROOT=/home/admin/cpfs/wjh/agentic-kv
export MODEL=/home/admin/cpfs/wjh/models/Qwen/Qwen3-Coder-30B-A3B-Instruct
TRACE="${TRACE:-$ROOT/traces/w600_r0.0015_st30_first600s.jsonl}"
DATE="$(date +%Y%m%d_%H%M)"
OUT="${OUTROOT:-$ROOT/outputs/policy5_600s_$DATE}"
mkdir -p "$OUT"
echo "OUTROOT=$OUT" | tee "$OUT/RUNINFO.txt"
echo "TRACE=$TRACE  ($(wc -l < "$TRACE") reqs)" | tee -a "$OUT/RUNINFO.txt"
date | tee -a "$OUT/RUNINFO.txt"

run_arm() {  # name policy extra_args
    local name="$1" policy="$2" extra="$3"
    echo "===== ARM: $name (policy=$policy args='$extra') =====" | tee -a "$OUT/RUNINFO.txt"
    local t0=$(date +%s)
    EXTRA_PROXY_ARGS="$extra" bash "$ROOT/scripts/b3_isolated_policy.sh" \
        "$policy" "$TRACE" "$OUT/$name" > "$OUT/$name.log" 2>&1
    echo "$name rc=$? rows=$(wc -l < "$OUT/$name/metrics.jsonl" 2>/dev/null) dur=$(( $(date +%s) - t0 ))s" | tee -a "$OUT/RUNINFO.txt"
}

# Headline contrasts first (so a late failure still leaves the key arms).
run_arm leastwork   leastwork ""
run_arm unified_ab  unified   "--overload-factor 1.3 --lmetric-decode-weight 0.01"
run_arm unified_def unified   "--overload-factor 2.0 --lmetric-decode-weight 0.0"
run_arm lmetric     lmetric   ""
run_arm sticky      sticky    ""

echo "===== ALL DONE: $OUT =====" | tee -a "$OUT/RUNINFO.txt"
date | tee -a "$OUT/RUNINFO.txt"
