#!/usr/bin/env bash
# Bounded Q4-KV follow-up.  It waits for the active auxiliary matrix and never
# clears a thermal latch or changes a hardware limit.  This is evidence for an
# experimental memory profile, not a promotion or long-context acceptance run.
set -euo pipefail

root=/home/lab/bench/ornith/modular/vllm
log="$root/q4-cache-followup.log"
mkdir -p "$root/runs/full-int8-kv4"

power_limit=$(nvidia-smi -i GPU-5f32faf3-97b9-895e-0a9e-ffa0f51bca3f \
  --query-gpu=power.limit --format=csv,noheader,nounits | tr -d '[:space:]')
awk -v value="$power_limit" 'BEGIN { exit !(value > 0 && value <= 250.5) }' || {
  echo "Q4 follow-up refused: GPU2 must already be at or below 250 W (found ${power_limit:-unknown} W)." | tee -a "$log"
  exit 1
}

while pgrep -f '[p]ython3 /home/lab/bench/ornith/modular/vllm/spec_grid.py' >/dev/null; do
  printf '%s waiting for active auxiliary matrix\n' "$(date -u +%FT%TZ)" >> "$log"
  sleep 10
done

if test -e "$root/thermal-stop.json"; then
  echo 'Q4 follow-up blocked by thermal safety latch; no restart performed.' | tee -a "$log"
  exit 0
fi

echo 'Q4 KV bounded trial: same full-INT8 target, INT4 per-token/head KV.' | tee -a "$log"
python3 "$root/candidate.py" "$root/cases/full-int8-kv4.json" 2>&1 | tee -a "$log"
python3 "$root/greedy_compare.py" \
  --out "$root/runs/full-int8-kv4/greedy.json" \
  --baseline "$root/runs/full-int8-bf16-250w/greedy.json" 2>&1 | tee -a "$log"
python3 "$root/quality_gate.py" \
  --out "$root/runs/full-int8-kv4/quality.json" 2>&1 | tee -a "$log"

# One repeat at each declared short/medium context and both scheduler levels.
# A failure here ends the script; it does not attempt a long grid or promote Q4.
python3 "$root/final_suite.py" --id full-int8-kv4 --out q4-retrieval-8k \
  --lengths 8192 --concurrency 1,3 --repeats 1 --task retrieval \
  --max-output 256 --seed 4400 2>&1 | tee -a "$log"
python3 "$root/final_suite.py" --id full-int8-kv4 --out q4-retrieval-32k \
  --lengths 32768 --concurrency 1,3 --repeats 1 --task retrieval \
  --max-output 256 --seed 4400 2>&1 | tee -a "$log"

echo 'Q4 KV bounded trial completed; review required before any further use.' | tee -a "$log"
