"""Knowledge distillation: frozen ens_all teachers -> vlaai_ws 10 s front-end.

Teacher = equal average of published logit_tf_v2 checkpoints
  stem_ms96, ms_w96, ms_w192, recipe_hp48  (ens_all; no teacher retrain).
Student = DirectNet vlaai_ws, window 10 s, prep eaP_n5_t60, sw2, SparrKULee init.
Loss = α * T^2 * BCE(student/T, sigmoid(teacher/T)) + (1-α) * BCE(student, hard)
  with α=0.6, T=2. Fuglsang windows have no teacher soft label -> hard BCE only.

~1000 steps/fold (4 epochs x 250). Score LODO 5 + AASD + Carta at p_stay 0.994
scale 0.8; also causal tau-30 high-pass of student evidence (honest linear_hp30).

usage:
  STEPS=1000 python kd_ensemble_front.py
"""
import json
import os
import pickle
import time
from types import SimpleNamespace

import numpy as np
import torch
import torch.nn.functional as F

import decoders
import direct_classifier as DC
import ensemble_clf_nested as N
import ensemble_clf_transformer_v2 as V
import ensemble_clf_transformer_v4 as V4
import eval_external as E
import score as S
from common import DS_KEYS, SEED
from common_exp import CFG, EXP, FS, fold_file, load_trial, rows_of, save_pickle
from decoders import D, DEV, _targets
from e1_switch import draw_switch_times
from run_experiment import load_external, log_to

EXP_NAME = "direct_clf__eaP_n5_t60_w10b3_ptSPK_kd_ens"
INIT = ("/root/disk_3tb/EEG-sAAD-Grand-Challenge-Training-Dataset/experiments/decoder_lodo/models/"
        "pretrain_spk_direct_clf_w10_b3_s0.pt")
TEACHER_DIR = os.path.join(EXP, "models", "logit_tf_v2")
CKPT_DIR = os.path.join(EXP, "models", "kd_ens_front")
LOG_PATH = os.path.join(EXP, "experiments", "kd_ensemble_front.log")
OUT_JSON = os.path.join(EXP, "results", f"{EXP_NAME}.json")
OUT_MD = os.path.join(EXP, "multi_agent_opt", "results_kd.md")
EXT_DIR = os.path.join(EXP, "results", "research")
ALL5_PT = os.path.join(EXP, "models", "all5_direct_clf_kd_ens_s0.pt")
TAG = "_w10b3_ptSPK_kd_ens"

# name, family, window, d_model, nlayers, stem
TEACHERS = (
    ("stem_ms96", "ms", 96, 64, 2, True),
    ("ms_w96", "ms", 96, 64, 2, False),
    ("ms_w192", "ms", 192, 48, 2, False),
    ("recipe_hp48", "hp", 48, 32, 2, False),
)

P_STAY, SCALE = 0.994, 0.8
MAX_EPOCHS = 4  # 4 * 250 = 1000 steps/fold
ALPHA = float(os.environ.get("KD_ALPHA", "0.6"))
TEMP = float(os.environ.get("KD_TEMP", "2.0"))
FRONTAL = DC.FRONTAL


def log_line(text):
    print(text, flush=True)
    os.makedirs(os.path.dirname(LOG_PATH), exist_ok=True)
    with open(LOG_PATH, "a") as fh:
        fh.write(text + "\n")


def configure():
    DC.W = int(10 * FS)
    DC.POS0, DC.LAB = DC.W - DC.FUT - DC.HOP, DC.W - FS
    D["targets"] = ["env", "onset", "dom"]
    D["augment"] = {**CFG["dnn_augment"], **CFG["aug_variants"]["cluster"], "clf_p_switch2": 0.5}
    D["train_seed"] = int(CFG["seed"])
    D["model_blocks"] = 3
    D["max_epochs"] = MAX_EPOCHS
    D["steps_per_epoch"] = 250
    decoders.apply_overrides(["model_blocks=3"], ["clf_p_switch2=0.5"])


def namespace():
    return SimpleNamespace(
        backbone="vlaai_ws", p_switch=0.5, zero_eeg=False, min_epochs=MAX_EPOCHS,
        init_from=INIT, prep="eaP_n5_t60", tag=TAG, trial_switch="",
    )


def mark_claim(status, note=None):
    path = os.path.join(EXP, "multi_agent_opt", "CLAIMS.md")
    rows = open(path).read().splitlines()
    out = []
    for line in rows:
        if "| kd |" in line and "KD from frozen ensemble" in line:
            parts = line.split("|")
            if len(parts) >= 6:
                parts[4] = f" {status} "
                if note is not None:
                    parts[5] = f" {note} "
                line = "|".join(parts)
        out.append(line)
    open(path, "w").write("\n".join(out) + ("\n" if rows and not rows[-1].endswith("\n") else ""))


def load_teacher(name, family, window, d_model, nlayers, stem, held, device):
    path = os.path.join(TEACHER_DIR, f"{name}__held-{held}.pt")
    if not os.path.exists(path):
        raise FileNotFoundError(path)
    blob = torch.load(path, map_location="cpu", weights_only=False)
    c_in = int(np.asarray(blob["mu"]).shape[0])
    net = V.LogitTransformer(c_in, window, d_model=d_model, nlayers=nlayers, stem=stem)
    net.load_state_dict(blob["state"])
    net = net.to(device).eval()
    for p in net.parameters():
        p.requires_grad_(False)
    mu = np.asarray(blob["mu"], np.float32)
    sd = np.asarray(blob["sd"], np.float32)
    return net, mu, sd, family, window


@torch.no_grad()
def build_ens_soft(held, feat, device, datasets=None):
    """Equal average of the four frozen teachers (ens_all) for the given held-out fold."""
    datasets = list(datasets) if datasets is not None else list(DS_KEYS)
    parts = []
    for name, family, window, d_model, nlayers, stem in TEACHERS:
        net, mu, sd, fam, win = load_teacher(name, family, window, d_model, nlayers, stem, held, device)
        table = {ds: feat[fam][ds] for ds in datasets}
        streams = V.infer_all(net, table, mu, sd, win, device)
        parts.append(streams)
        del net
        if device.type == "cuda":
            torch.cuda.empty_cache()
        log_line(f"  teacher {name} held-{held} inferred on {datasets}")
    out = {}
    for ds in datasets:
        out[ds] = {}
        for key in parts[0][ds]:
            acc = np.array(parts[0][ds][key], dtype=np.float64, copy=True)
            for other in parts[1:]:
                acc += other[ds][key]
            out[ds][key] = (acc / len(parts)).astype(np.float32)
    return out


def attach_soft(trials, soft_by_ds):
    """Attach evidence-rate teacher soft logits; Fuglsang / missing keys stay without _soft."""
    n_ok, n_miss = 0, 0
    for tr in trials:
        key = tr["key"]
        found = None
        for ds, table in soft_by_ds.items():
            if key in table:
                found = table[key]
                break
        if found is None:
            n_miss += 1
            continue
        # evidence frame i <-> EEG samples [i*HOP, (i+1)*HOP)
        eeg_n = len(tr["eeg"])
        n_ev = int(eeg_n // DC.HOP)
        soft = np.asarray(found, np.float32)
        if len(soft) < n_ev:
            soft = np.pad(soft, (0, n_ev - len(soft)), mode="edge")
        tr["_soft"] = soft[:n_ev]
        n_ok += 1
    return n_ok, n_miss


def windows_kd(trials, n, rng, aug, p_sw, zero_eeg):
    """Like DC.windows but also returns soft teacher targets (nan if unavailable)."""
    lens = np.array([len(t["eeg"]) - DC.W for t in trials])
    p = np.maximum(lens, 0).astype(float)
    ti = rng.choice(len(trials), n, p=p / p.sum())
    windows_kd.last_ti = ti
    X = np.empty((n, DC.W, 58), np.float32)
    S = np.empty((n, DC.W) + trials[0]["_S"].shape[1:], np.float32)
    y = np.empty(n, np.float32)
    soft = np.full(n, np.nan, np.float32)
    A = (D.get("augment") or {}) if aug is not None else {}
    lag = int(A.get("clf_lag_jitter", 0))
    p_fd = A.get("clf_p_feat_drop", 0.0)
    p_sw2 = A.get("clf_p_switch2", 0.0)
    for j, i in enumerate(ti):
        s = int(rng.integers(0, lens[i] + 1))
        tr = trials[i]
        s2 = int(np.clip(s + rng.integers(-lag, lag + 1), 0, lens[i])) if lag else s
        X[j], S[j] = tr["eeg"][s:s + DC.W], tr["_S"][s2:s2 + DC.W]
        col = int(tr["att"][s + DC.LAB])
        flipped = False
        if rng.random() < p_sw:
            tau = int(rng.integers(FS // 2, DC.W - FS // 2))
            S[j, tau:] = S[j, tau:, ::-1]
            if tau <= DC.LAB:
                col = 1 - col
                flipped = not flipped
            if p_sw2 and rng.random() < p_sw2 and DC.W - FS // 2 - tau > 2 * FS:
                tau2 = int(rng.integers(tau + 2 * FS, DC.W - FS // 2))
                S[j, tau2:] = S[j, tau2:, ::-1]
                if tau2 <= DC.LAB:
                    col = 1 - col
                    flipped = not flipped
        if p_fd and rng.random() < p_fd:
            S[j, :, :, rng.integers(S.shape[-1])] = 0.0
        y[j] = 1.0 if col == 0 else 0.0
        if "_soft" in tr:
            fi = int(np.clip((s + DC.LAB) // DC.HOP, 0, len(tr["_soft"]) - 1))
            tlogit = float(tr["_soft"][fi])
            if flipped:
                tlogit = -tlogit
            soft[j] = tlogit
    if aug is not None:
        X = aug._augment(X, rng)
    if zero_eeg:
        X[:] = 0.0
    return (torch.from_numpy(X), torch.from_numpy(S), torch.from_numpy(y),
            torch.from_numpy(soft))


def kd_loss(logit, y, soft, alpha, temp):
    hard = F.binary_cross_entropy_with_logits(logit, y)
    mask = torch.isfinite(soft)
    if mask.any():
        p = torch.sigmoid(soft[mask] / temp)
        soft_ce = F.binary_cross_entropy_with_logits(logit[mask] / temp, p) * (temp ** 2)
        loss = alpha * soft_ce + (1.0 - alpha) * hard
        n_soft = int(mask.sum().item())
    else:
        loss = hard
        soft_ce = hard.detach() * 0.0
        n_soft = 0
    return loss, hard, soft_ce, n_soft


def fit_kd(train, val, log, a):
    Fs = train[0]["_S"].shape[-1]
    seed = D.get("train_seed", CFG["seed"])
    torch.manual_seed(seed)
    net = DC.DirectNet(58, Fs, a.backbone).to(DEV)
    if a.init_from:
        from common_exp import resolve_model
        path = resolve_model(a.init_from)
        net.load_state_dict(torch.load(path, map_location=DEV, weights_only=False))
        log(f"  initialised from {path}")
    aug = decoders.DNNDec.__new__(decoders.DNNDec)
    aug.W, aug.fut, aug.hop, aug.C = DC.W, DC.FUT, DC.HOP, 58
    rs = np.random.default_rng(seed)
    Xv, Sv, yv, softv = windows_kd(val, D["val_windows"], rs, None, a.p_switch, a.zero_eeg)
    opt = torch.optim.AdamW(net.parameters(), lr=D["lr"], weight_decay=D["weight_decay"])
    sch = torch.optim.lr_scheduler.ReduceLROnPlateau(opt, mode="min", factor=0.5, patience=D["lr_patience"])
    best, bad, state = np.inf, 0, None
    for ep in range(D["max_epochs"]):
        t1, tl, th, ts, ns = time.time(), [], [], [], []
        net.train()
        for _ in range(D["steps_per_epoch"]):
            X, S, y, soft = windows_kd(train, D["batch_size"], rs, aug, a.p_switch, a.zero_eeg)
            X, S, y, soft = X.to(DEV), S.to(DEV), y.to(DEV), soft.to(DEV)
            logit = net(X, S)
            loss, hard, soft_ce, n_soft = kd_loss(logit, y, soft, ALPHA, TEMP)
            opt.zero_grad(set_to_none=True)
            loss.backward()
            torch.nn.utils.clip_grad_norm_(net.parameters(), 5.0)
            opt.step()
            tl.append(float(loss.item()))
            th.append(float(hard.item()))
            ts.append(float(soft_ce.item()) if n_soft else 0.0)
            ns.append(n_soft)
        net.eval()
        with torch.no_grad():
            lv = torch.cat([net(Xv[i:i + 256].to(DEV), Sv[i:i + 256].to(DEV)).cpu()
                            for i in range(0, len(Xv), 256)])
            soft_cpu = softv
            loss_v, hard_v, soft_v, _ = kd_loss(lv, yv, soft_cpu, ALPHA, TEMP)
            va = ((lv > 0).float() == yv).float().mean().item()
        vl = float(hard_v.item())
        if ep >= a.min_epochs:
            sch.step(vl)
        log(f"    epoch {ep:02d} train {np.mean(tl):.4f} (hard {np.mean(th):.4f} "
            f"kd {np.mean(ts):.4f} soft_frac {np.mean(ns)/D['batch_size']:.2f}) "
            f"val hard {vl:.4f} kd {float(loss_v):.4f} acc {va:.3f} ({time.time() - t1:.0f}s)")
        if vl < best - 1e-4:
            best, bad, state = vl, 0, {k: v.detach().clone() for k, v in net.state_dict().items()}
        elif ep >= a.min_epochs:
            bad += 1
            if bad >= D["patience"]:
                break
    net.load_state_dict(state)
    net.eval()
    return net, best


def run_fold_kd(held, exp, log, a, soft_by_ds):
    rng = np.random.default_rng(CFG["seed"] + DS_KEYS.index(held))
    train, val = [], []
    for ds in DS_KEYS:
        if ds == held:
            continue
        rows = rows_of(ds)
        vs = rng.choice(sorted(rows.subject.unique()))
        for _, r in rows.iterrows():
            (val if r["subject"] == vs else train).append(load_trial(a.prep, r["key"]))
    train += load_external(["Fuglsang2020"], a.prep, log)
    for tr in train + val:
        tr["_S"] = _targets(tr)
    n_ok, n_miss = attach_soft(train + val, soft_by_ds)
    log(f"  fold held-out={held}: {len(train)} train / {len(val)} val; "
        f"soft labels {n_ok} trials, missing {n_miss} (Fuglsang expected)")
    t0 = time.time()
    net, best = fit_kd(train, val, log, a)
    t_fit = time.time() - t0
    os.makedirs(CKPT_DIR, exist_ok=True)
    torch.save(net.state_dict(), os.path.join(CKPT_DIR, f"held-{held}.pt"))
    del train, val
    srng = np.random.default_rng(SEED + DS_KEYS.index(held))
    ev, ctl = {}, {k: {} for k in ("normal", "zero_eeg", "shift_eeg", "no_frontal")}
    for _, r in rows_of(held).iterrows():
        tr = load_trial(a.prep, r["key"])
        S, x, att, T128 = _targets(tr), tr["eeg"], tr["att"].astype(int), int(r["n"])
        times = draw_switch_times(srng, T128 / 128)
        srng.integers(0, 2)
        st128, cur, prev = np.zeros(T128, bool), False, 0
        for tt in times:
            s0 = min(T128 - 1, int(round(tt * 128)))
            st128[prev:s0] = cur
            cur, prev = not cur, s0
        st128[prev:] = cur
        st = st128[np.minimum(T128 - 1, np.round((np.arange(len(x)) / FS + tr["t0"]) * 128).astype(int))]
        Ssw = np.where(st[:, None, None], S[:, ::-1], S)
        lg, starts = DC.logits(net, x, Ssw)
        truth = (att ^ st)[np.maximum(starts - DC.HOP, 0)]
        n10 = len(lg) // 40
        ev[r["key"]] = dict(
            r0=lg / 2, r1=-lg / 2, att=truth.astype(np.int8),
            t_end=(starts / FS + tr["t0"]).astype(np.float32),
            r0_10=lg[:n10 * 40].reshape(n10, 40).mean(1) / 2,
            r1_10=-lg[:n10 * 40].reshape(n10, 40).mean(1) / 2,
            att10=(truth[:n10 * 40].reshape(n10, 40).mean(1) > 0.5).astype(np.int8),
            pre_switched=True)
        xf = x.copy()
        xf[:, FRONTAL] = 0.0
        for name, xc in (("normal", x), ("zero_eeg", np.zeros_like(x)),
                         ("shift_eeg", np.roll(x, len(x) // 2, 0)), ("no_frontal", xf)):
            l1, s1 = DC.logits(net, xc, S, step=4)
            ctl[name].setdefault(r["subj_uid"], []).append((l1 > 0) == (att[np.maximum(s1 - DC.HOP, 0)] == 0))
    controls = {k: dict(pooled=float(np.mean(np.concatenate([np.concatenate(v) for v in d.values()]))),
                        subject_median=float(np.median([np.mean(np.concatenate(v)) for v in d.values()])))
                for k, d in ctl.items()}
    info = dict(decoder="direct_clf", prep=a.prep, held_out=held, fit_s=t_fit, total_s=time.time() - t0,
                lookahead_s=(DC.FUT + DC.HOP) / FS, best_val=best, channels=58, controls=controls,
                kd=dict(alpha=ALPHA, temp=TEMP, teachers=[t[0] for t in TEACHERS]))
    save_pickle(dict(evidence={"causal": ev}, info=info), fold_file(exp, held))
    log(f"  fold held-out={held} done in {info['total_s']:.0f}s: controls "
        + "  ".join(f"{k} {v['pooled']:.3f}" for k, v in controls.items()))
    del net
    if DEV.type == "cuda":
        torch.cuda.empty_cache()


def score_hp30(ev, la, log):
    """Causal tau-30 high-pass of student signed evidence; emissions from corpus only."""
    hp_ev = {}
    for ds in DS_KEYS:
        hp_ev[ds] = {}
        for key, e in ev[ds].items():
            signed = (e["r0"] - e["r1"]).astype(np.float64)
            hp = V4.highpass(signed[:, None], tau=30.0, hop=0.25)[:, 0]
            hp_ev[ds][key] = dict(e)
            hp_ev[ds][key]["r0"] = (hp / 2).astype(np.float32)
            hp_ev[ds][key]["r1"] = (-hp / 2).astype(np.float32)
    per = {}
    for ds in DS_KEYS:
        A = np.concatenate([(e["r0"] - e["r1"]) * np.where(e["att"] == 0, 1.0, -1.0)
                            for d in DS_KEYS if d != ds for e in hp_ev[d].values()])
        mu, sd = float(A.mean()), float(A.std())
        r = S.score_dataset(ds, hp_ev, la, emis=(mu, sd * SCALE), p_stay=P_STAY)
        per[ds] = r
        log(f"  HP30 LODO {ds}  H {r['H']:.4f}  Acc {r['Acc']:.2f}")
    mH = float(np.mean([per[d]["H"] for d in DS_KEYS]))
    mA = float(np.mean([per[d]["Acc"] for d in DS_KEYS]))
    log(f"  HP30 LODO MEAN  H {mH:.4f}  Acc {mA:.2f}")
    return per, mH, mA


def score_preset(log, a):
    la = (DC.FUT + DC.HOP) / FS
    ev, _ = S.load_folds(EXP_NAME, "causal")
    per = {}
    for ds in DS_KEYS:
        A = np.concatenate([(e["r0"] - e["r1"]) * np.where(e["att"] == 0, 1.0, -1.0)
                            for d in DS_KEYS if d != ds for e in ev[d].values()])
        mu, sd = float(A.mean()), float(A.std())
        r = S.score_dataset(ds, ev, la, emis=(mu, sd * SCALE), p_stay=P_STAY)
        per[ds] = r
        log(f"  PRESET LODO {ds}  H {r['H']:.4f}  Acc {r['Acc']:.2f}  L {r['L']:.3f}  SDT {r['SDT']:.2f}")
    mH = float(np.mean([per[d]["H"] for d in DS_KEYS]))
    mA = float(np.mean([per[d]["Acc"] for d in DS_KEYS]))
    log(f"  PRESET LODO MEAN  H {mH:.4f}  Acc {mA:.2f}")
    hp_per, hp_mH, hp_mA = score_hp30(ev, la, log)
    A = np.concatenate([(e["r0"] - e["r1"]) * np.where(e["att"] == 0, 1.0, -1.0)
                        for ds in DS_KEYS for e in ev[ds].values()])
    mu, sd = float(A.mean()), float(A.std())
    log(f"  external emissions mu {mu:.4f} sd {sd:.4f} scale {SCALE}")
    E.CONF.update(prep=a.prep, targets=list(D["targets"]))
    os.makedirs(EXT_DIR, exist_ok=True)
    ev_paths = {ds: os.path.join(EXT_DIR, f"ext_clf_{ds}{TAG}_evidence.pkl") for ds in ("AASD", "Carta2026")}
    if any(not os.path.exists(p) for p in ev_paths.values()):
        if os.path.exists(ALL5_PT):
            net = DC.DirectNet(58, 3, "vlaai_ws").to(DEV)
            net.load_state_dict(torch.load(ALL5_PT, map_location=DEV, weights_only=False))
            net.eval()
            log(f"  loaded {ALL5_PT}")
        else:
            # all-5 soft labels: teacher ensemble from held-AVGC ckpts (trained on other 4)
            # averaged with each held-* soft cache written during LODO when present
            device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
            soft_cache = os.path.join(CKPT_DIR, "soft_ens")
            soft_parts = []
            for h in DS_KEYS:
                sp = os.path.join(soft_cache, f"held-{h}.pkl")
                if os.path.exists(sp):
                    with open(sp, "rb") as fh:
                        soft_parts.append(pickle.load(fh))
            if len(soft_parts) < 5:
                feat = V.build_features()
                soft_parts = [build_ens_soft(h, feat, device) for h in DS_KEYS]
            soft_all = {}
            for ds in DS_KEYS:
                soft_all[ds] = {}
                for key in soft_parts[0][ds]:
                    acc = sum(p[ds][key] for p in soft_parts) / len(soft_parts)
                    soft_all[ds][key] = np.asarray(acc, np.float32)
            rng = np.random.default_rng(CFG["seed"] + 99)
            train, val = [], []
            for ds in DS_KEYS:
                rows = rows_of(ds)
                vs = rng.choice(sorted(rows.subject.unique()))
                for _, r in rows.iterrows():
                    (val if r["subject"] == vs else train).append(load_trial(a.prep, r["key"]))
            train += load_external(["Fuglsang2020"], a.prep, log)
            for tr in train + val:
                tr["_S"] = _targets(tr)
            n_ok, n_miss = attach_soft(train + val, soft_all)
            log(f"  all-5 train {len(train)} / val {len(val)}; soft {n_ok} miss {n_miss}")
            net, best = fit_kd(train, val, log, a)
            torch.save(net.state_dict(), ALL5_PT)
            log(f"  saved {ALL5_PT} best val hard {best:.4f}")
            del train, val
        for ds, path in ev_paths.items():
            if os.path.exists(path):
                continue
            EV = {}
            trials = E.ext_trials(ds)
            log(f"  external {ds}: {len(trials)} trials")
            for i, tr in enumerate(trials):
                lg, starts = DC.logits(net, tr["eeg"], _targets(tr))
                truth = tr["att"].astype(int)[np.maximum(starts - DC.HOP, 0)]
                EV[tr["key"]] = dict(
                    r0=lg / 2, r1=-lg / 2, att=truth.astype(np.int8),
                    t_end=(starts / FS + tr["t0"]).astype(np.float32),
                    att128=tr["att128"], n128=tr["n128"], subject=tr["subject"])
                if (i + 1) % 40 == 0 or i + 1 == len(trials):
                    log(f"    {ds} {i + 1}/{len(trials)}")
            with open(path, "wb") as fh:
                pickle.dump(EV, fh)
            log(f"  wrote {path}")
        del net
        if DEV.type == "cuda":
            torch.cuda.empty_cache()
    ext = {}
    for ds, path in ev_paths.items():
        with open(path, "rb") as fh:
            EV = pickle.load(fh)
        r = E.score(EV, mu, sd * SCALE, P_STAY, 0.75)
        plain = E.plain(EV)
        EV_hp = {}
        for key, e in EV.items():
            signed = (e["r0"] - e["r1"]).astype(np.float64)
            hp = V4.highpass(signed[:, None], tau=30.0, hop=0.25)[:, 0]
            ee = dict(e)
            ee["r0"] = (hp / 2).astype(np.float32)
            ee["r1"] = (-hp / 2).astype(np.float32)
            EV_hp[key] = ee
        # emissions from hp of corpus student evidence only (no AASD/Carta labels)
        hp_signed = []
        for d in DS_KEYS:
            for e in ev[d].values():
                s = (e["r0"] - e["r1"]).astype(np.float64)
                hp_signed.append(
                    V4.highpass(s[:, None], tau=30.0, hop=0.25)[:, 0]
                    * np.where(e["att"] == 0, 1.0, -1.0))
        A_hp = np.concatenate(hp_signed)
        mu_hp, sd_hp = float(A_hp.mean()), float(A_hp.std())
        r_hp = E.score(EV_hp, mu_hp, sd_hp * SCALE, P_STAY, 0.75)
        plain_hp = E.plain(EV_hp)
        ext[ds] = dict(**r, acc1=plain["acc1"],
                       hp30=dict(**r_hp, acc1=plain_hp["acc1"]))
        log(f"  PRESET EXT {ds}  H {r['H']:.4f}  Acc {r['Acc']:.2f}  L {r['L']:.3f}  "
            f"SDT {r['SDT']:.2f}  acc1 {plain['acc1']:.3f}")
        log(f"  HP30   EXT {ds}  H {r_hp['H']:.4f}  Acc {r_hp['Acc']:.2f}  acc1 {plain_hp['acc1']:.3f}")
    return per, ext, mu, sd, mH, mA, hp_per, hp_mH, hp_mA


def write_results(per, ext, mH, mA, hp_per, hp_mH, hp_mA):
    stem = dict(AVGC=(0.7945, 74.06), DTU=(0.9061, 89.85), KUL=(0.8755, 82.33),
                UHD=(0.8407, 79.47), USTC=(0.9219, 86.36))
    swa = dict(AVGC=(0.8015, 74.91), UHD=(0.8553, 80.09))
    lines = [
        "# results_kd",
        "",
        "Path: **KD** (not soft-H). Frozen ens_all teachers "
        "(stem_ms96 + ms_w96 + ms_w192 + recipe_hp48 held-* checkpoints, no retrain) "
        f"distilled into vlaai_ws 10 s (`eaP_n5_t60`, sw2, SPK init). "
        f"α={ALPHA}, T={TEMP}, {MAX_EPOCHS}×250 steps/fold. "
        f"HMM p_stay {P_STAY} scale {SCALE}.",
        "",
        "## LODO (preset)",
        "",
        "| Held-out | KD H | KD Acc % | stem H/Acc | SWA AVGC/UHD |",
        "|---|---:|---:|---|---|",
    ]
    for ds in DS_KEYS:
        r = per[ds]
        sh, sa = stem[ds]
        extra = ""
        if ds in swa:
            extra = f" SWA {swa[ds][0]:.4f}/{swa[ds][1]:.2f}"
        lines.append(f"| {ds} | {r['H']:.4f} | {r['Acc']:.2f} | stem {sh:.4f}/{sa:.2f} |{extra} |")
    lines += [
        f"| **mean** | **{mH:.4f}** | **{mA:.2f}** | stem 0.8677/82.42 | |",
        "",
        f"Student hp30 LODO mean H {hp_mH:.4f} Acc {hp_mA:.2f} "
        f"(cf. linear_hp30 train 0.7899/69.21).",
        "",
        "## External (preset)",
        "",
        "| Set | KD H | KD Acc % | KD hp30 H/Acc | linear_hp30 | stem_ms96 |",
        "|---|---:|---:|---|---|---|",
        f"| AASD | {ext['AASD']['H']:.4f} | {ext['AASD']['Acc']:.2f} | "
        f"{ext['AASD']['hp30']['H']:.4f}/{ext['AASD']['hp30']['Acc']:.2f} | "
        f"0.6422/53.12 | 0.6158/51.54 |",
        f"| Carta2026 | {ext['Carta2026']['H']:.4f} | {ext['Carta2026']['Acc']:.2f} | "
        f"{ext['Carta2026']['hp30']['H']:.4f}/{ext['Carta2026']['hp30']['Acc']:.2f} | "
        f"0.6520/60.54 | 0.5695/51.03 |",
        "",
        f"Checkpoints: `{CKPT_DIR}/`, `{ALL5_PT}`. Folds: `{EXP_NAME}`. JSON: `{OUT_JSON}`.",
        "",
    ]
    open(OUT_MD, "w").write("\n".join(lines))
    return OUT_MD


def main():
    configure()
    log = log_to(LOG_PATH)
    a = namespace()
    mark_claim("running", "GPU train under flock; results_kd.md")
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    log(f"=== {EXP_NAME} KD ens->vlaai_ws device {device} α={ALPHA} T={TEMP} epochs {MAX_EPOCHS} ===")
    # verify teachers exist
    missing = [f"{n}__held-{h}.pt" for n, *_ in TEACHERS for h in DS_KEYS
               if not os.path.exists(os.path.join(TEACHER_DIR, f"{n}__held-{h}.pt"))]
    if missing:
        raise SystemExit(f"missing teacher checkpoints (abort to soft-H): {missing[:5]}...")
    log(f"  teachers OK: {[t[0] for t in TEACHERS]}")
    N.load_all()
    feat = V.build_features()
    for held in DS_KEYS:
        path = fold_file(EXP_NAME, held)
        if os.path.exists(path):
            log(f"  fold held-out={held}: cached {path}")
            continue
        soft_cache = os.path.join(CKPT_DIR, "soft_ens")
        os.makedirs(soft_cache, exist_ok=True)
        soft_path = os.path.join(soft_cache, f"held-{held}.pkl")
        if os.path.exists(soft_path):
            with open(soft_path, "rb") as fh:
                soft = pickle.load(fh)
            log(f"  loaded cached teacher soft {soft_path}")
        else:
            soft = build_ens_soft(held, feat, device)
            with open(soft_path, "wb") as fh:
                pickle.dump(soft, fh)
        soft_train = {ds: soft[ds] for ds in DS_KEYS if ds != held}
        run_fold_kd(held, EXP_NAME, log, a, soft_train)
        del soft, soft_train
        if device.type == "cuda":
            torch.cuda.empty_cache()
    per, ext, mu, sd, mH, mA, hp_per, hp_mH, hp_mA = score_preset(log, a)
    out = dict(
        experiment=EXP_NAME, path="KD", decoder="direct_clf", prep="eaP_n5_t60",
        targets=D["targets"], p_switch=0.5, init_from=INIT, backbone="vlaai_ws",
        window_s=10.0, max_epochs=MAX_EPOCHS, steps_per_epoch=D["steps_per_epoch"],
        kd=dict(alpha=ALPHA, temp=TEMP, teachers=[t[0] for t in TEACHERS]),
        train_seed=D["train_seed"], extra_train="Fuglsang2020", augment=D["augment"],
        preset=dict(p_stay=P_STAY, scale=SCALE, emis_mu=mu, emis_sd=sd,
                    lodo={d: per[d] for d in DS_KEYS}, external=ext,
                    hp30_lodo={d: hp_per[d] for d in DS_KEYS},
                    hp30_mean_H=hp_mH, hp30_mean_Acc=hp_mA),
        mean_H=mH, mean_Acc=mA,
    )
    os.makedirs(os.path.dirname(OUT_JSON), exist_ok=True)
    with open(OUT_JSON, "w") as fh:
        json.dump(out, fh, indent=1, default=float)
    rpath = write_results(per, ext, mH, mA, hp_per, hp_mH, hp_mA)
    # update ALREADY_TRIED
    tried = os.path.join(EXP, "multi_agent_opt", "ALREADY_TRIED.md")
    addendum = (
        f"- **KD ens_all → vlaai_ws 10 s** (α={ALPHA}, T={TEMP}, eaP_n5_t60, sw2, SPK init; "
        f"teachers stem_ms96+ms_w96+ms_w192+recipe_hp48 held-*): "
        f"LODO mean H {mH:.4f} Acc {mA:.2f} (AVGC {per['AVGC']['H']:.4f}/{per['AVGC']['Acc']:.2f}); "
        f"AASD {ext['AASD']['H']:.4f}/{ext['AASD']['Acc']:.2f}; "
        f"Carta {ext['Carta2026']['H']:.4f}/{ext['Carta2026']['Acc']:.2f}. "
        f"Do not rerun this KD recipe. `{OUT_MD}`.\n"
    )
    with open(tried, "a") as fh:
        fh.write(addendum)
    note = (f"LODO mean H {mH:.4f} Acc {mA:.2f}. AASD {ext['AASD']['H']:.4f}/{ext['AASD']['Acc']:.2f}. "
            f"Carta {ext['Carta2026']['H']:.4f}/{ext['Carta2026']['Acc']:.2f}. `{rpath}`.")
    mark_claim("done", note)
    log(f"wrote {OUT_JSON} and {rpath}")
    log("=== done ===")


if __name__ == "__main__":
    main()
