#!/usr/bin/env bash
# Train vlaai_ws 10s seed s4 (SPK init) then score 5-seed linear_mean / linear_hp30.
# Waits for gpu.lock (after irm-synth-fuse). Abort if AVGC fold looks chance.
set -euo pipefail
cd /root/disk_3tb/eeg-thao
PY=/root/disk_3tb/EEG-sAAD-Grand-Challenge-Training-Dataset/.venv/bin/python
SEED=4
export PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 OMP_NUM_THREADS=8 MKL_NUM_THREADS=8
mkdir -p experiments logs models results/research multi_agent_opt folds

exec 9>/root/disk_3tb/eeg-thao/multi_agent_opt/gpu.lock
echo "$(date -Is) waiting for GPU lock..."
while ! flock -n 9; do
  echo "$(date -Is) GPU busy; sleep 60"
  sleep 60
done
echo "$(date -Is) GPU lock acquired pid=$$"
nvidia-smi --query-gpu=memory.used,utilization.gpu --format=csv

# mark claim running
python3 - <<'PY'
from pathlib import Path
p = Path("multi_agent_opt/CLAIMS.md")
t = p.read_text()
old = "| 2026-10-29 10:50 +07 | newseed-hp30 | Train new vlaai_ws 10s seed (s4+); 5-seed linear_hp30 vs 4-seed 53.12; LODO+AASD+Carta | claimed | GPU after irm-synth-fuse; AASD Acc push. |"
new = "| 2026-10-29 10:50 +07 | newseed-hp30 | Train new vlaai_ws 10s seed (s4+); 5-seed linear_hp30 vs 4-seed 53.12; LODO+AASD+Carta | running | seed=4 SPK+LODO+all5; ABORT_IF_CHANCE on AVGC. |"
if old in t:
    p.write_text(t.replace(old, new, 1))
    print("CLAIMS -> running")
else:
    print("CLAIMS row already updated or missing; continue")
PY

INIT="models/pretrain_spk_direct_clf_w10_b3_s${SEED}.pt"
if [[ ! -f "$INIT" && ! -f "/root/disk_3tb/EEG-sAAD-Grand-Challenge-Training-Dataset/experiments/decoder_lodo/$INIT" ]]; then
  echo "=== SPK pretrain seed ${SEED} ==="
  GLIMPSE_THR=onorm $PY -u direct_classifier.py --window-s 10 --pretrain-spk \
    --tag _w10_b3 --train-seed "$SEED" --min-epochs 25 --max-epochs 80 --set model_blocks=3 \
    2>&1 | tee "experiments/pretrain_spk_w10_b3_s${SEED}_console.log"
else
  echo "SPK init present: $INIT (or DATA_EXP copy)"
fi

LODO_EXP="direct_clf__eaP_n5_t60_w10b3_ptSPK_s${SEED}_aug-sw2"
if [[ ! -f "folds/${LODO_EXP}/USTC.pkl" ]]; then
  echo "=== LODO train seed ${SEED} ==="
  # AVGC is first held-out; ABORT_IF_CHANCE kills near-chance after 50 steps
  ABORT_IF_CHANCE=1 GLIMPSE_THR=onorm $PY -u direct_classifier.py \
    --prep eaP_n5_t60 --window-s 10 --set model_blocks=3 \
    --init-from "models/pretrain_spk_direct_clf_w10_b3_s${SEED}.pt" \
    --min-epochs 10 --train-seed "$SEED" \
    --p-switch 0.5 --aug-set clf_p_switch2=0.5 \
    --tag "_w10b3_ptSPK_s${SEED}_aug-sw2" \
    2>&1 | tee "experiments/${LODO_EXP}_console.log"
  EC=${PIPESTATUS[0]}
  if [[ $EC -eq 3 ]]; then
    echo "ABORT AVGC chance"
    python3 - <<'PY'
from pathlib import Path
p = Path("multi_agent_opt/CLAIMS.md")
t = p.read_text()
t2 = t.replace(
  "| running | seed=4 SPK+LODO+all5; ABORT_IF_CHANCE on AVGC. |",
  "| abandoned | AVGC chance abort after 50 steps. |",
  1,
)
p.write_text(t2)
PY
    exit 3
  fi
  [[ $EC -eq 0 ]] || exit $EC
else
  echo "LODO folds cached: folds/${LODO_EXP}/"
fi

EV_AASD="results/research/ext_clf_AASD_w10b3sw2P_evidence_s${SEED}.pkl"
EV_CARTA="results/research/ext_clf_Carta2026_w10b3sw2P_evidence_s${SEED}.pkl"
if [[ ! -f "$EV_AASD" || ! -f "$EV_CARTA" ]]; then
  echo "=== all5 external clf seed ${SEED} ==="
  GLIMPSE_THR=norm $PY -u eval_external_clf.py \
    --seed "$SEED" --prep eaP_n5_t60 --window-s 10 --set model_blocks=3 \
    --p-switch 0.5 --aug-set clf_p_switch2=0.5 \
    --init-from "models/pretrain_spk_direct_clf_w10_b3_s${SEED}.pt" \
    --ref "$LODO_EXP" --tag _w10b3sw2P \
    2>&1 | tee "experiments/eval_external_clf_w10b3sw2P_s${SEED}_console.log"
else
  echo "external evidence cached"
fi

echo "=== score 5-seed linear_mean / linear_hp30 ==="
$PY -u newseed_hp30.py 2>&1 | tee experiments/newseed_hp30_console.log

# append ALREADY + STATUS + CLAIMS done
python3 - <<'PY'
from pathlib import Path
import json, re
from datetime import datetime
j = json.load(open("results/research/newseed_hp30.json"))
a5 = j["external_5"]["linear_hp30"]["AASD"]
m5 = j["external_5"]["linear_mean"]["AASD"]
l5m = j["lodo_5"]["linear_mean"]
l5h = j["lodo_5"]["linear_hp30"]
note = (
  f"seed4. LODO5 mean {l5m['mean_H']:.4f}/{l5m['mean_Acc']:.2f} "
  f"hp30 {l5h['mean_H']:.4f}/{l5h['mean_Acc']:.2f}. "
  f"AASD linear_mean {m5['H']:.4f}/{m5['Acc']:.2f}; "
  f"linear_hp30 {a5['H']:.4f}/{a5['Acc']:.2f} (Δ{a5['Acc']-53.12:+.2f} vs 53.12). "
  f"`results_newseed_hp30.md`."
)
p = Path("multi_agent_opt/CLAIMS.md")
t = p.read_text()
t = t.replace(
  "| running | seed=4 SPK+LODO+all5; ABORT_IF_CHANCE on AVGC. |",
  f"| done | {note} |",
  1,
)
p.write_text(t)

already = Path("multi_agent_opt/ALREADY_TRIED.md")
block = f"""
## newseed-hp30 (2026-10-29) — DO NOT REPEAT identical s4 bank expand without new frame recipe

Train one extra published-recipe vlaai_ws 10s classifier seed (s4, eaP_n5_t60, sw2, SPK w10_b3 init), then 5-seed linear_mean / linear_hp30 vs 4-seed AASD Acc 53.12.

- AASD linear_hp30 Acc **{a5['Acc']:.2f}** (Δ {a5['Acc']-53.12:+.2f})
- LODO5 mean H/Acc {l5m['mean_H']:.4f}/{l5m['mean_Acc']:.2f}; hp30 {l5h['mean_H']:.4f}/{l5h['mean_Acc']:.2f}
- Code: `newseed_hp30.py`, `experiments/run_newseed_hp30.sh`; folds `{j['lodo_exp']}`
"""
with already.open("a") as fh:
    fh.write(block)

status = Path("multi_agent_opt/STATUS_GAPS.md")
st = status.read_text()
# refresh in-flight line if present
st2 = re.sub(
    r"3\. \*\*vlaai-newseed-hp\*\*[^\n]*",
    f"3. **newseed-hp30** — DONE. 5-seed linear_hp30 AASD Acc {a5['Acc']:.2f} (Δ{a5['Acc']-53.12:+.2f} vs 53.12).",
    st,
    count=1,
)
if st2 == st:
    st2 = st.rstrip() + f"\n\n- **newseed-hp30 done**: 5-seed AASD linear_hp30 Acc {a5['Acc']:.2f} (Δ{a5['Acc']-53.12:+.2f}).\n"
status.write_text(st2)
print("CLAIMS/ALREADY/STATUS updated")
print(note)
PY

echo "$(date -Is) newseed-hp30 finished"
nvidia-smi --query-gpu=memory.used,utilization.gpu --format=csv
