"""speech-xattn-dec — Multi-head cross-attn decoder (speech KV, EEG Q).

SPK-init VLAAI produces EEG latents h; speech-env/onset/dom tokens are KV;
causal MH cross-attn residual with zero-init out_proj ⇒ identity @step 0:
  h = bb(eeg‖speech); tok = speech_tokens(sp);
  h' = h + MHCA_out_proj(LN(h), LN(tok), LN(tok))
SPK warm-start remaps bb.* → bb.bb.*.

DISTINCT from:
  * temp-attn-spk — temporal SELF-attn (Q=K=V=h)
  * speech-resid  — FiLM/additive gate on speech BEFORE VLAAI
  * aux-env-spk   — aux envelope MSE loss
  * env_xattn     — from-scratch EEG-conv Q (chance; no SPK)
  * noise-aug / emis-shift / SSL / Path A / tip PP (CLOSED)

Prep eaP_n5_t60, sw2, 10s × 3 blocks. Swap+chance abort ≥0.685@50.
If learns: LODO5 + AASD/Carta Acc @0.994/0.8 (+hp30); soft-fuse tip Acc if natural.

usage:
  GLIMPSE_THR=norm python speech_xattn_dec_lodo.py
"""
from __future__ import annotations

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_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
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_speechxattn_aug-sw2"
TAG = "_w10b3_ptSPK_speechxattn_aug-sw2"
BACKBONE = "speech_xattn_dec"
INIT = (
    "/root/disk_3tb/EEG-sAAD-Grand-Challenge-Training-Dataset/experiments/"
    "decoder_lodo/models/pretrain_spk_direct_clf_w10_b3_s0.pt"
)
LOG_PATH = os.path.join(EXP, "experiments", "speech_xattn_dec_lodo.log")
OUT_JSON = os.path.join(EXP, "results", f"{EXP_NAME}.json")
EXT_DIR = os.path.join(EXP, "results", "research")
ALL5_PT = os.path.join(EXP, "models", f"all5_direct_clf{TAG}.pt")
CKPT_DIR = os.path.join(EXP, "models", "speech_xattn_dec")
RESULTS_MD = os.path.join(EXP, "multi_agent_opt", "results_speech_xattn_dec.md")
P_STAY, SCALE = 0.994, 0.8
WINDOW_S = 10.0
MAX_EPOCHS = int(os.environ.get("MAX_EPOCHS", "4"))
MIN_EPOCHS = int(os.environ.get("MIN_EPOCHS", "4"))
BATCH = int(os.environ.get("BATCH", "64"))
ABORT_STEPS = int(os.environ.get("ABORT_STEPS", "50"))
ABORT_LOSS = float(os.environ.get("ABORT_LOSS", "0.685"))
N_EEG = 58
ATTN_HEADS = int(os.environ.get("ATTN_HEADS", "4"))
SPEECH_KERN = int(os.environ.get("SPEECH_KERN", "9"))
LEADER_AASD = 53.53
CLAIM_KEY = "speech-xattn-dec"
TIP_FUSE_A = float(os.environ.get("TIP_FUSE_A", "0.5"))


class ChanceStuckError(RuntimeError):
    """Train loss stuck near chance — abort per protocol."""


def load_spk_into_speech_xattn(net, init_path, log):
    """Map published DirectNet SPK weights into SpeechXAttnVLAAI (bb.* → bb.bb.*)."""
    from common_exp import resolve_model
    path = resolve_model(init_path)
    sd = torch.load(path, map_location=DEV, weights_only=False)
    mapped = {}
    for k, v in sd.items():
        if k.startswith("bb."):
            mapped["bb.bb." + k[3:]] = v
        else:
            mapped[k] = v
    missing, unexpected = net.load_state_dict(mapped, strict=False)
    log(f"  SPK→speech_xattn_dec from {path} (strict=False; missing {len(missing)} "
        f"unexpected {len(unexpected)})")
    return missing, unexpected


def configure():
    DC.W = int(WINDOW_S * 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["model_dropout"] = 0.0
    D["model_kernels"] = 64
    D["model_n_raw"] = N_EEG
    D["model_heads"] = ATTN_HEADS
    D["model_speech_kern"] = SPEECH_KERN
    D["model_attn_drop"] = 0.0
    D["model_attn_dropout"] = 0.0
    D["max_epochs"] = MAX_EPOCHS
    D["steps_per_epoch"] = 250
    D["batch_size"] = BATCH
    D["patience"] = 6
    D["val_windows"] = int(os.environ.get("VAL_WINDOWS", "512"))
    decoders.apply_overrides(["model_blocks=3", f"model_speech_kern={SPEECH_KERN}"],
                             ["clf_p_switch2=0.5"])


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


def swap_and_identity_check(log):
    """Speaker-order must change logit; zero out_proj ⇒ SpeechXAttn.bb ≡ inner vlaai_ws."""
    torch.manual_seed(0)
    configure()
    net = DC.DirectNet(N_EEG, 3, BACKBONE).to(DEV).eval()
    with torch.no_grad():
        E = torch.randn(8, DC.W, N_EEG, device=DEV)
        A = torch.randn(8, DC.W, 3, device=DEV)
        B = torch.randn(8, DC.W, 3, device=DEV)
        S_ab = torch.stack([A, B], dim=2)
        y_ab = net(E, S_ab)
        y_ba = net(E, S_ab.flip(2))
        anti_err = float((y_ab + y_ba).abs().mean())
        gab = net.g(E, A, B)
        gba = net.g(E, B, A)
        delta = float((gab - gba).abs().mean())
        gab0 = net.g(torch.zeros_like(E), A, B)
        gba0 = net.g(torch.zeros_like(E), B, A)
        delta0 = float((gab0 - gba0).abs().mean())
        out_w = float(net.bb.attn.out_proj.weight.abs().max())
        out_b = float(net.bb.attn.out_proj.bias.abs().max()) if net.bb.attn.out_proj.bias is not None else 0.0
        # Zero-init out_proj ⇒ SpeechXAttn forward == inner VLAAI on same concat.
        x = torch.cat([E, A, B], -1)
        y_w = net.bb(x)
        y_w = y_w[0] if isinstance(y_w, (tuple, list)) else y_w
        y_i = net.bb.bb(x)
        y_i = y_i[0] if isinstance(y_i, (tuple, list)) else y_i
        id_err = float((y_w - y_i).abs().max())
        tok = net.bb._speech_tokens(x[..., N_EEG:])
        assert tok.shape == y_i.shape, (tok.shape, y_i.shape)
    n_param = sum(p.numel() for p in net.parameters())
    n_xattn = sum(p.numel() for p in list(net.bb.ln_q.parameters())
                  + list(net.bb.ln_kv.parameters())
                  + list(net.bb.attn.parameters())
                  + list(net.bb.sp_dw.parameters())
                  + list(net.bb.sp_proj.parameters()))
    log(f"swap-test params {n_param} (speech_xattn {n_xattn}) anti_err {anti_err:.3e} "
        f"swap_delta {delta:.4f} zero_eeg_delta {delta0:.4f} |out_proj| {out_w:.3e}+{out_b:.3e} "
        f"bb_id_err {id_err:.3e} heads={ATTN_HEADS} speech_kern={SPEECH_KERN}")
    del net
    if DEV.type == "cuda":
        torch.cuda.empty_cache()
    if anti_err > 1e-5:
        raise RuntimeError(f"antisymmetry broken: {anti_err}")
    if delta < 1e-4:
        raise RuntimeError(f"swap-delta≈0 ({delta:.3e})")
    if out_w > 1e-8 or out_b > 1e-8:
        raise RuntimeError(f"zero-init out_proj broken: |w|={out_w} |b|={out_b}")
    if id_err > 1e-5:
        raise RuntimeError(f"zero-out_proj SpeechXAttn.bb != inner vlaai: {id_err}")
    return delta, delta0, anti_err


def fit_net(train, val, log, a, check_chance=True):
    Fs = train[0]["_S"].shape[-1]
    seed = D.get("train_seed", CFG["seed"])
    torch.manual_seed(seed)
    net = DC.DirectNet(N_EEG, Fs, a.backbone).to(DEV)
    if a.init_from:
        load_spk_into_speech_xattn(net, a.init_from, log)
    n_param = sum(p.numel() for p in net.parameters())
    n_train = sum(p.numel() for p in net.parameters() if p.requires_grad)
    log(f"  SpeechXAttn-VLAAI params {n_param} (trainable {n_train}) "
        f"backbone={a.backbone} batch {D['batch_size']} heads={ATTN_HEADS}")
    aug = decoders.DNNDec.__new__(decoders.DNNDec)
    aug.W, aug.fut, aug.hop, aug.C = DC.W, DC.FUT, DC.HOP, N_EEG
    rs = np.random.default_rng(seed)
    Xv, Sv, yv = DC.windows(val, D["val_windows"], rs, None, a.p_switch, a.zero_eeg)
    opt = torch.optim.AdamW(
        [p for p in net.parameters() if p.requires_grad],
        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
    early = []
    global_step = 0
    for ep in range(D["max_epochs"]):
        t1, tl = time.time(), []
        net.train()
        for _ in range(D["steps_per_epoch"]):
            X, S_, y = [z.to(DEV) for z in DC.windows(
                train, D["batch_size"], rs, aug, a.p_switch, a.zero_eeg)]
            loss = F.binary_cross_entropy_with_logits(net(X, S_), y)
            opt.zero_grad()
            loss.backward()
            torch.nn.utils.clip_grad_norm_(net.parameters(), 5.0)
            opt.step()
            lv = float(loss.detach())
            tl.append(lv)
            global_step += 1
            if check_chance and global_step <= ABORT_STEPS:
                early.append(lv)
                if global_step == ABORT_STEPS:
                    mean50 = float(np.mean(early))
                    log(f"    chance-probe step {ABORT_STEPS} mean_loss {mean50:.4f} "
                        f"(abort if >={ABORT_LOSS})")
                    if mean50 >= ABORT_LOSS:
                        raise ChanceStuckError(
                            f"ABORT_CHANCE: mean loss {mean50:.4f} >= {ABORT_LOSS} after "
                            f"{ABORT_STEPS} steps (near chance)")
        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)])
        vl = float(F.binary_cross_entropy_with_logits(lv, yv))
        va = float(((lv > 0).float() == yv).float().mean())
        if ep >= a.min_epochs:
            sch.step(vl)
        with torch.no_grad():
            onorm = float(net.bb.attn.out_proj.weight.detach().norm())
        log(f"    epoch {ep:02d} train {np.mean(tl):.4f} val BCE {vl:.4f} acc {va:.3f} "
            f"||out_proj|| {onorm:.4f} ({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
    if state is None:
        state = {k: v.detach().clone() for k, v in net.state_dict().items()}
    net.load_state_dict(state)
    net.eval()
    return net, best


def run_fold(held, exp, log, a, check_chance=False):
    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)
    log(f"  fold held-out={held}: {len(train)} train / {len(val)} val "
        f"chance_probe={'on' if check_chance else 'off'}")
    t0 = time.time()
    net, best = fit_net(train, val, log, a, check_chance=check_chance)
    t_fit = time.time() - t0
    del train, val
    os.makedirs(CKPT_DIR, exist_ok=True)
    ckpt = os.path.join(CKPT_DIR, f"held-{held}.pt")
    torch.save(net.state_dict(), ckpt)
    log(f"  saved {ckpt}")
    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"])
        Sfeat, 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], Sfeat[:, ::-1], Sfeat)
        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[:, DC.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, Sfeat, 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=N_EEG, backbone=BACKBONE,
                attn_heads=ATTN_HEADS, controls=controls)
    from common_exp import save_pickle
    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 best_val {best:.4f} "
        + "  ".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):
    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}  "
            f"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)
    os.makedirs(os.path.dirname(ALL5_PT), 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(N_EEG, 3, BACKBONE).to(DEV)
            net.load_state_dict(torch.load(ALL5_PT, map_location=DEV, weights_only=False),
                                strict=False)
            net.eval()
            log(f"  loaded {ALL5_PT}")
        else:
            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)
            log(f"  all-5 train {len(train)} / val {len(val)}")
            net, best = fit_net(train, val, log, a, check_chance=False)
            torch.save(net.state_dict(), ALL5_PT)
            log(f"  saved {ALL5_PT} best val {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()

    hp_parts = []
    for ds in DS_KEYS:
        for e in ev[ds].values():
            signed = (e["r0"] - e["r1"]).astype(np.float64)
            hp = V4.highpass(signed[:, None], tau=30.0, hop=0.25)[:, 0]
            hp_parts.append(hp * np.where(e["att"] == 0, 1.0, -1.0))
    mu_hp = float(np.concatenate(hp_parts).mean())
    sd_hp = float(np.concatenate(hp_parts).std())

    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)
        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]
            EV_hp[key] = dict(e)
            EV_hp[key]["r0"] = (hp / 2).astype(np.float32)
            EV_hp[key]["r1"] = (-hp / 2).astype(np.float32)
        r_hp = E.score(EV_hp, mu_hp, sd_hp * SCALE, P_STAY, 0.75)
        plain = E.plain(EV)
        ext[ds] = dict(raw={**r, "acc1": plain["acc1"]}, hp30=r_hp)
        log(f"  PRESET EXT {ds}  H {r['H']:.4f} Acc {r['Acc']:.2f} acc1 {plain['acc1']:.3f}")
        log(f"  PRESET EXT_hp30 {ds}  H {r_hp['H']:.4f} Acc {r_hp['Acc']:.2f}")
    return per, ext, mu, sd, mH, mA, hp_per, hp_mH, hp_mA, ev_paths


def soft_fuse_tip_ext(our_ev_paths, mu, sd, log):
    """Soft a=TIP_FUSE_A blend of this clf ↔ Acc tip hard_abs_swmix|t1 (external).

    Natural Acc fuse when tip caches exist. Selection is not re-run on AASD labels;
    report fuse Acc vs pure Acc. Returns dict or None.
    """
    try:
        import sw2_swmix_blend as SM
        import tip_sdur_abs_gate as TIP
    except Exception as e:
        log(f"  tip-fuse import skip: {e!r}")
        return None
    try:
        EV_sw2, _emis = SM.ensure_sw2_external()
        sm_ext = SM.ensure_swmix_external()
        tip_ext = TIP.build_tip_ext_ev(EV_sw2, sm_ext)
    except Exception as e:
        log(f"  tip-fuse tip-build skip: {e!r}")
        return None
    a = float(TIP_FUSE_A)
    out = {}
    for ds in ("AASD", "Carta2026"):
        with open(our_ev_paths[ds], "rb") as fh:
            our = pickle.load(fh)
        tip = tip_ext[ds]
        blended = {}
        for key, e in our.items():
            if key not in tip:
                continue
            # our signed = r0-r1; tip EV uses r0 as signed (r1=0) in SM.blend_ext_ev
            ours = (np.asarray(e["r0"], np.float64) - np.asarray(e["r1"], np.float64))
            tips = np.asarray(tip[key]["r0"], np.float64)
            n = min(len(ours), len(tips))
            d = a * ours[:n] + (1.0 - a) * tips[:n]
            blended[key] = dict(
                r0=(d / 2).astype(np.float32),
                r1=(-d / 2).astype(np.float32),
                att=e["att"][:n],
                t_end=e["t_end"][:n],
                att128=e.get("att128"),
                n128=e.get("n128"),
                subject=e.get("subject"),
            )
        r = E.score(blended, mu, sd * SCALE, P_STAY, 0.75)
        plain = E.plain(blended)
        out[ds] = {**r, "acc1": plain["acc1"]}
        log(f"  TIP_FUSE_a{a:.2f} EXT {ds}  H {r['H']:.4f} Acc {r['Acc']:.2f} "
            f"acc1 {plain['acc1']:.3f}")
    return dict(a=a, external=out)


def write_results(per, ext, mH, mA, hp_per, hp_mH, hp_mA, swap_delta, status="done",
                  chance_note=None, tip_fuse=None, head_name="pure"):
    pub = dict(mean_H=0.7846, mean_Acc=72.14)
    temp = dict(aasd=52.92)
    speech = dict(aasd=52.87)
    aux = dict(aasd=52.93)
    leader = LEADER_AASD
    head = ext["AASD"]["raw"] if status == "done" else None
    if tip_fuse and status == "done":
        tf = tip_fuse["external"]["AASD"]
        if tf["Acc"] > head["Acc"]:
            head = tf
            head_name = f"tip_fuse_a{tip_fuse['a']:.2f}"
    lines = [
        "# results_speech_xattn_dec",
        "",
        "**Multi-head cross-attn decoder: speech-env tokens as KV, EEG latents as Q**.",
        f"SPK-init VLAAI → EEG latents h; causal DW+Linear speech tokens; "
        f"pre-norm MHCA (heads={ATTN_HEADS}, speech_kern={SPEECH_KERN}); "
        "`out_proj` zero-init ⇒ identity @step 0. SPK remap `bb.*`→`bb.bb.*`.",
        "",
        "**Novelty:** ≠ `temp-attn-spk` (temporal self-attn Q=K=V=h); "
        "≠ `speech-resid` (FiLM/additive speech residual before VLAAI); "
        "≠ `aux-env-spk` (aux env MSE); ≠ failed from-scratch `env_xattn`.",
        "",
        f"Swap-test delta={swap_delta:.4f}. Prep `eaP_n5_t60`, sw2, max {MAX_EPOCHS} ep×250, "
        f"batch {BATCH}. HMM p_stay {P_STAY} scale {SCALE}; also hp30.",
        "",
        f"Code: `speech_xattn_dec_lodo.py` / `decoders.SpeechXAttnVLAAI`. "
        f"Checkpoints: `models/speech_xattn_dec/held-*.pt`, `{ALL5_PT}`. JSON: `{OUT_JSON}`.",
        "",
    ]
    if status == "abandoned":
        lines += [
            f"**ABANDONED**: {chance_note}",
            "",
            "## NEXT IDEA",
            "",
            "Speech cross-attn decoder into SPK did not clear chance. Prefer a "
            "qualitatively new Acc frame (not vlaai residual/FiLM/self-attn/cross-attn, "
            "not noise-aug/emis-shift/SSL/Path A/tip PP).",
            "",
        ]
        open(RESULTS_MD, "w").write("\n".join(lines))
        return RESULTS_MD, head_name, None

    lines += [
        "## LODO (preset)",
        "",
        "| Held-out | H | Acc % | hp30 H/Acc | vs published vlaai |",
        "|---|---:|---:|---|---|",
    ]
    for ds in DS_KEYS:
        r, h = per[ds], hp_per[ds]
        lines.append(
            f"| {ds} | {r['H']:.4f} | {r['Acc']:.2f} | {h['H']:.4f}/{h['Acc']:.2f} | "
            f"pub {pub['mean_H']:.4f}/{pub['mean_Acc']:.2f} |")
    lines += [
        f"| **mean** | **{mH:.4f}** | **{mA:.2f}** | **{hp_mH:.4f}/{hp_mA:.2f}** | "
        f"pub {pub['mean_H']:.4f}/{pub['mean_Acc']:.2f}; temp-attn AASD {temp['aasd']}; "
        f"speech-resid {speech['aasd']} |",
        "",
        "## External (preset)",
        "",
        "| Set | H | Acc % | hp30 H/Acc | Acc tip 53.53 | temp-attn | speech-resid | aux-env |",
        "|---|---:|---:|---|---|---|---|---|",
        f"| AASD | {ext['AASD']['raw']['H']:.4f} | {ext['AASD']['raw']['Acc']:.2f} | "
        f"{ext['AASD']['hp30']['H']:.4f}/{ext['AASD']['hp30']['Acc']:.2f} | "
        f"Δ{ext['AASD']['raw']['Acc'] - leader:+.2f} | "
        f"Δ{ext['AASD']['raw']['Acc'] - temp['aasd']:+.2f} | "
        f"Δ{ext['AASD']['raw']['Acc'] - speech['aasd']:+.2f} | "
        f"Δ{ext['AASD']['raw']['Acc'] - aux['aasd']:+.2f} |",
        f"| Carta2026 | {ext['Carta2026']['raw']['H']:.4f} | {ext['Carta2026']['raw']['Acc']:.2f} | "
        f"{ext['Carta2026']['hp30']['H']:.4f}/{ext['Carta2026']['hp30']['Acc']:.2f} | "
        f"— | — | — | — |",
        "",
    ]
    if tip_fuse:
        tf_a = tip_fuse["external"]["AASD"]
        tf_c = tip_fuse["external"]["Carta2026"]
        lines += [
            f"## Tip Acc soft fuse (a={tip_fuse['a']:.2f})",
            "",
            f"| Set | H | Acc % | Δtip |",
            f"|---|---:|---:|---:|",
            f"| AASD | {tf_a['H']:.4f} | {tf_a['Acc']:.2f} | {tf_a['Acc'] - leader:+.2f} |",
            f"| Carta2026 | {tf_c['H']:.4f} | {tf_c['Acc']:.2f} | — |",
            "",
            f"**Headline stream:** `{head_name}` "
            f"(AASD Acc {head['Acc']:.2f}).",
            "",
        ]
    lines += [
        "## Targets",
        "",
        "| Target | Result |",
        "|---|---|",
        f"| AVGC H>=0.85 Acc>=78 | "
        f"{'met' if per['AVGC']['H'] >= 0.85 and per['AVGC']['Acc'] >= 78 else 'miss'} "
        f"({per['AVGC']['H']:.4f}/{per['AVGC']['Acc']:.2f}) |",
        f"| UHD H>=0.85 | "
        f"{'met' if per['UHD']['H'] >= 0.85 else 'miss'} ({per['UHD']['H']:.4f}) |",
        f"| AASD Acc>60 with H>0.63 | "
        f"{'met' if head['Acc'] > 60 and head['H'] > 0.63 else 'miss'} "
        f"({head['H']:.4f}/{head['Acc']:.2f}; "
        f"pure {ext['AASD']['raw']['Acc']:.2f}; hp30 {ext['AASD']['hp30']['Acc']:.2f}) |",
        f"| AASD Acc vs tip 53.53 | Δ {head['Acc'] - leader:+.2f} "
        f"(pure Δ {ext['AASD']['raw']['Acc'] - leader:+.2f}; "
        f"hp30 Δ {ext['AASD']['hp30']['Acc'] - leader:+.2f}) |",
        f"| Acc>60? | "
        f"{'YES' if head['Acc'] > 60 else 'NO'} |",
        f"| Tip moved? | "
        f"{'YES' if head['Acc'] > leader else 'NO'} |",
        "",
        "## NEXT IDEA",
        "",
        "If speech-xattn ≈ temp-attn/speech-resid/aux-env (~52–53 AASD Acc): close "
        "SPK-vlaai attention/FiLM residual family. Prefer a qualitatively new Acc "
        "frame — e.g. causal dual-stream matching head, or subject-invariant "
        "contrastive AAD front-end — not noise-aug / emis-shift / SSL / Path A / "
        "switch-schedule / tip PP.",
        "",
    ]
    open(RESULTS_MD, "w").write("\n".join(lines))
    return RESULTS_MD, head_name, head


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 "| speech-xattn-dec |" in line:
            parts = line.split("|")
            if len(parts) >= 6:
                parts[4] = f" {status} "
                if note:
                    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 update_status_gaps(note):
    path = os.path.join(EXP, "multi_agent_opt", "STATUS_GAPS.md")
    text = open(path).read()
    lines = text.splitlines()
    out = []
    for line in lines:
        if "speech-xattn-dec" in line and (
                line.strip().startswith(("1.", "2.", "3.", "4.", "-")) or "In flight" in line):
            # drop in-flight bullet
            if "speech-xattn-dec" in line and "Recently closed" not in line:
                continue
        out.append(line)
    text2 = "\n".join(out)
    # refresh header
    import re
    text2 = re.sub(
        r"Updated:.*",
        f"Updated: 2026-10-30 (speech-xattn-dec closed).",
        text2, count=1)
    # drop NEXT speech-xattn preference lines
    text2 = re.sub(
        r"\*\*NEXT:\*\* prefer \*\*speech-xattn-dec\*\*[^\n]*\n",
        "**NEXT:** see results_speech_xattn_dec.md NEXT IDEA "
        "(not noise-aug / emis-shift / SSL / Path A / switch-schedule / tip PP).\n",
        text2, count=1)
    if "## Recently closed" in text2 and "speech-xattn-dec" not in text2.split("## Recently closed")[1][:900]:
        text2 = text2.replace(
            "## Recently closed\n",
            f"## Recently closed\n\n- **speech-xattn-dec** — {note}\n",
            1)
    open(path, "w").write(text2 + ("\n" if not text2.endswith("\n") else ""))


def append_already(note):
    path = os.path.join(EXP, "multi_agent_opt", "ALREADY_TRIED.md")
    with open(path, "a") as fh:
        fh.write(f"\n- **speech-xattn-dec** ({note})\n")


def maybe_update_current_best(head, head_name, rpath, log):
    if head is None or head["Acc"] <= LEADER_AASD:
        log(f"  CURRENT_BEST Acc tip unchanged (head Acc {head['Acc'] if head else None})")
        return False
    best = os.path.join(EXP, "multi_agent_opt", "CURRENT_BEST.md")
    txt = open(best).read()
    import re
    txt2 = re.sub(
        r"(\| External Acc \| )[^\|]+(\| AASD \*\*)[0-9.]+",
        rf"\1speech-xattn-dec ({head_name}) \2{head['Acc']:.2f}",
        txt, count=1)
    if txt2 == txt:
        open(best, "a").write(
            f"\n\n### Acc tip UPDATED by speech-xattn-dec\n"
            f"AASD Acc **{head['Acc']:.2f}** via `{head_name}` (was {LEADER_AASD}). "
            f"See `{rpath}`.\n")
    else:
        open(best, "w").write(txt2)
    log(f"  CURRENT_BEST Acc tip moved → {head['Acc']:.2f} via {head_name}")
    return True


def main():
    configure()
    log = log_to(LOG_PATH)
    a = namespace()
    mark_claim("running", "GPU train under flock; results_speech_xattn_dec.md")
    log(f"=== {EXP_NAME} SpeechXAttn→vlaai device {DEV} max_epochs {MAX_EPOCHS} "
        f"min_epochs {MIN_EPOCHS} W={DC.W} FS={FS} batch={BATCH} "
        f"heads={ATTN_HEADS} speech_kern={SPEECH_KERN} ===")
    try:
        swap_delta, swap_delta0, anti_err = swap_and_identity_check(log)
    except RuntimeError as e:
        mark_claim("abandoned", str(e)[:220])
        write_results(None, None, None, None, None, None, None, 0.0,
                      status="abandoned", chance_note=str(e))
        log(f"ABANDONED swap/id: {e}")
        raise SystemExit(2)

    # Memory probe
    try:
        net = DC.DirectNet(N_EEG, 3, BACKBONE).to(DEV)
        load_spk_into_speech_xattn(net, a.init_from, log)
        net.train()
        xb = torch.randn(BATCH, DC.W, N_EEG, device=DEV)
        sb = torch.randn(BATCH, DC.W, 2, 3, device=DEV)
        loss = F.binary_cross_entropy_with_logits(net(xb, sb), torch.zeros(BATCH, device=DEV))
        loss.backward()
        if DEV.type == "cuda":
            log(f"memory probe batch {BATCH} ok; peak "
                f"{torch.cuda.max_memory_allocated() / 1e9:.2f} GB")
        del net, xb, sb, loss
        if DEV.type == "cuda":
            torch.cuda.empty_cache()
    except RuntimeError as e:
        if "out of memory" not in str(e).lower():
            raise
        log(f"OOM at batch {BATCH}; abort so caller can halve once")
        raise SystemExit(2)

    chance_done = any(os.path.exists(fold_file(EXP_NAME, h)) for h in DS_KEYS)
    try:
        for held in DS_KEYS:
            path = fold_file(EXP_NAME, held)
            if os.path.exists(path):
                log(f"  fold held-out={held}: cached {path}")
                chance_done = True
                continue
            run_fold(held, EXP_NAME, log, a, check_chance=not chance_done)
            chance_done = True
    except ChanceStuckError as e:
        log(f"ABANDON: {e}")
        write_results(None, None, None, None, None, None, None, swap_delta,
                      status="abandoned", chance_note=str(e))
        note = str(e)[:220]
        mark_claim("abandoned", note)
        append_already(
            f"`speech_xattn_dec_lodo.py`, `decoders.SpeechXAttnVLAAI`; speech-env KV × "
            f"EEG-latent Q MHCA residual→vlaai_ws SPK; eaP_n5_t60 sw2; ≠ temp-attn-spk/"
            f"speech-resid/aux-env/env_xattn: swapΔ={swap_delta:.4f}; "
            f"abandoned: {note}. `multi_agent_opt/results_speech_xattn_dec.md`.")
        update_status_gaps(f"abandoned ({note[:120]})")
        out = dict(
            experiment=EXP_NAME, status="abandoned", reason=str(e),
            backbone=BACKBONE, attn_heads=ATTN_HEADS, speech_kern=SPEECH_KERN,
            swap_test=dict(delta=swap_delta, zero_eeg_delta=swap_delta0, anti_err=anti_err),
            results_md=RESULTS_MD,
        )
        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)
        log(f"wrote {OUT_JSON} and {RESULTS_MD}")
        return out

    per, ext, mu, sd, mH, mA, hp_per, hp_mH, hp_mA, ev_paths = score_preset(log, a)
    tip_fuse = soft_fuse_tip_ext(ev_paths, mu, sd, log)
    rpath, head_name, head = write_results(
        per, ext, mH, mA, hp_per, hp_mH, hp_mA, swap_delta, tip_fuse=tip_fuse)
    tip_moved = maybe_update_current_best(head, head_name, rpath, log)
    out = dict(
        experiment=EXP_NAME, status="done", decoder="direct_clf", prep="eaP_n5_t60",
        backbone=BACKBONE, attn_heads=ATTN_HEADS, speech_kern=SPEECH_KERN,
        targets=D["targets"], p_switch=0.5, init_from=INIT,
        min_epochs=MIN_EPOCHS, max_epochs=MAX_EPOCHS, window_s=WINDOW_S, batch=BATCH,
        train_seed=D["train_seed"], extra_train="Fuglsang2020", augment=D["augment"],
        swap_test=dict(delta=swap_delta, zero_eeg_delta=swap_delta0, anti_err=anti_err),
        preset=dict(p_stay=P_STAY, scale=SCALE, emis_mu=mu, emis_sd=sd,
                    lodo={d: per[d] for d in DS_KEYS},
                    lodo_hp30={d: hp_per[d] for d in DS_KEYS},
                    external=ext),
        tip_fuse=tip_fuse, head_name=head_name, tip_moved=tip_moved,
        mean_H=mH, mean_Acc=mA, hp30_mean_H=hp_mH, hp30_mean_Acc=hp_mA,
        results_md=rpath,
    )
    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)
    aasd_acc = head["Acc"]
    note = (f"LODO mean H {mH:.4f} Acc {mA:.2f}. "
            f"Head `{head_name}` AASD {head['H']:.4f}/{aasd_acc:.2f} "
            f"(Δ{aasd_acc - LEADER_AASD:+.2f} vs tip 53.53; pure "
            f"{ext['AASD']['raw']['Acc']:.2f}). "
            f"Carta {ext['Carta2026']['raw']['H']:.4f}/{ext['Carta2026']['raw']['Acc']:.2f}. "
            f"Tip moved={'YES' if tip_moved else 'NO'}. `{rpath}`.")
    mark_claim("done", note)
    append_already(
        f"`speech_xattn_dec_lodo.py`, `decoders.SpeechXAttnVLAAI`; speech-env KV × "
        f"EEG-latent Q MHCA residual→vlaai_ws 10s SPK; eaP_n5_t60 sw2; ≠ temp-attn-spk/"
        f"speech-resid/aux-env/env_xattn; swapΔ={swap_delta:.4f}: "
        f"LODO {mH:.4f}/{mA:.2f}; AASD {head['H']:.4f}/{aasd_acc:.2f} "
        f"(Δ{aasd_acc - LEADER_AASD:+.2f} vs tip; head={head_name}); Carta "
        f"{ext['Carta2026']['raw']['Acc']:.2f}. Tip moved={'YES' if tip_moved else 'NO'}. "
        f"`multi_agent_opt/results_speech_xattn_dec.md`.")
    update_status_gaps(
        f"LODO {mH:.4f}/{mA:.2f}; AASD Acc {aasd_acc:.2f} via {head_name} "
        f"(Δ{aasd_acc - LEADER_AASD:+.2f} vs tip 53.53). Acc>60="
        f"{'YES' if aasd_acc > 60 else 'NO'}. Tip moved={'YES' if tip_moved else 'NO'}.")
    log(f"wrote {OUT_JSON} and {rpath}")
    log("=== done ===")
    return out


if __name__ == "__main__":
    main()
