"""Freeze published SW2 vlaai_ws backbone; train a NEW causal GRU (or Conv1d) head.

Loads held-*/all5 SW2 DirectNet checkpoints. Freezes `bb` (vlaai_ws trunk).
Replaces linear head+tw with a causal GRU (or small causal Conv1d) on
penultimate features → frame logits → temporal pool.

DISTINCT from: full FT hardswitch, vlaai identity residuals (SE/temp-MHSA/
speech-FiLM), aux-env/InfoNCE multitask, tip post-process, FIR/Morlet/FBCNet.

Prefer CPU while focal holds the GPU:
  DEVICE=cpu  (default)  — frozen bb forward + head train on CPU
  DEVICE=cuda — under multi_agent_opt/gpu.lock when free

Optional: dump frozen window features once then head-only train
  DUMP_FEATS=1 DEVICE=cpu|cuda  → cache/feats_sw2_feat_gru/
  TRAIN_FROM_CACHE=1 DEVICE=cpu → head train from dumped feats

Score LODO5 + AASD/Carta @ HMM 0.994/0.8 (+hp30). Never uses AASD/Carta labels.

usage:
  GLIMPSE_THR=norm DEVICE=cpu python -u sw2_feat_gru_lodo.py
  # or after flock:
  DEVICE=cuda HEAD=gru python -u sw2_feat_gru_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 as nn
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, DATA_EXP, EXP, FS, fold_file, load_trial, resolve_model, rows_of
from decoders import D, _targets
from e1_switch import draw_switch_times
from run_experiment import load_external, log_to

# Force device early (default CPU so we do not fight focal-stem for the GPU).
DEVICE_STR = os.environ.get("DEVICE", "cpu").lower()
if DEVICE_STR.startswith("cuda") and not torch.cuda.is_available():
    DEVICE_STR = "cpu"
DEV = torch.device(DEVICE_STR)
decoders.DEV = DEV  # DC.logits / windows consumers read decoders.DEV

EXP_NAME = "direct_clf__eaP_n5_t60_w10b3_ptSW2_featgru_aug-sw2"
TAG = "_w10b3_ptSW2_featgru_aug-sw2"
HEAD = os.environ.get("HEAD", "gru").lower()  # gru | conv1d
SEED0 = int(os.environ.get("SEED", "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", "32" if DEV.type == "cpu" else "64"))
STEPS = int(os.environ.get("STEPS_PER_EPOCH", "250"))
LR = float(os.environ.get("LR", "8e-4"))
GRU_HID = int(os.environ.get("GRU_HID", "64"))
CONV_K = int(os.environ.get("CONV_K", "9"))
ABORT_STEPS = int(os.environ.get("ABORT_STEPS", "50"))
ABORT_LOSS = float(os.environ.get("ABORT_LOSS", "0.685"))
P_STAY, SCALE = 0.994, 0.8
LEADER_AASD = 53.53
PREP = "eaP_n5_t60"
N_EEG = 58
DUMP_FEATS = os.environ.get("DUMP_FEATS", "0") == "1"
TRAIN_FROM_CACHE = os.environ.get("TRAIN_FROM_CACHE", "0") == "1"
FEAT_CACHE = os.path.join(EXP, "cache", "feats_sw2_feat_gru")
N_CACHE_TRAIN = int(os.environ.get("N_CACHE_TRAIN", "8000"))
N_CACHE_VAL = int(os.environ.get("N_CACHE_VAL", "1024"))

LOG_PATH = os.path.join(EXP, "experiments", "sw2_feat_gru_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", "sw2_feat_gru")
RESULTS_MD = os.path.join(EXP, "multi_agent_opt", "results_sw2_feat_gru.md")


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


class CausalGRUHead(nn.Module):
    """Causal GRU over (B, T, d) → frame logits (B, T)."""

    def __init__(self, d_in: int, d_hid: int = 64, n_layers: int = 1):
        super().__init__()
        self.gru = nn.GRU(d_in, d_hid, num_layers=n_layers, batch_first=True)
        self.proj = nn.Linear(d_hid, 1)
        for name, p in self.gru.named_parameters():
            if "weight" in name:
                nn.init.xavier_uniform_(p, gain=0.1)
            else:
                nn.init.zeros_(p)
        nn.init.zeros_(self.proj.weight)
        nn.init.zeros_(self.proj.bias)

    def forward(self, f: torch.Tensor) -> torch.Tensor:
        h, _ = self.gru(f)
        return self.proj(h)[..., 0]


class CausalConv1dHead(nn.Module):
    """Small causal Conv1d over time on (B, T, d) → frame logits (B, T)."""

    def __init__(self, d_in: int, d_hid: int = 64, k: int = 9):
        super().__init__()
        self.pad = k - 1
        self.conv = nn.Conv1d(d_in, d_hid, k)
        self.act = nn.GELU()
        self.proj = nn.Conv1d(d_hid, 1, 1)
        nn.init.xavier_uniform_(self.conv.weight, gain=0.1)
        nn.init.zeros_(self.conv.bias)
        nn.init.zeros_(self.proj.weight)
        nn.init.zeros_(self.proj.bias)

    def forward(self, f: torch.Tensor) -> torch.Tensor:
        x = f.transpose(1, 2)  # (B, d, T)
        x = F.pad(x, (self.pad, 0))
        x = self.act(self.conv(x))
        return self.proj(x)[:, 0]  # (B, T)


class FeatGRUDirectNet(nn.Module):
    """Frozen published SW2 backbone + new causal head on penultimate feats."""

    def __init__(self, base: DC.DirectNet, head: str = "gru"):
        super().__init__()
        self.bb = base.bb
        for p in self.bb.parameters():
            p.requires_grad = False
        d = int(base.head.in_features)
        self.d = d
        if head == "conv1d":
            self.head_net = CausalConv1dHead(d, d_hid=GRU_HID, k=CONV_K)
        else:
            self.head_net = CausalGRUHead(d, d_hid=GRU_HID)
        # fresh temporal pool (do not copy published tw — new head)
        self.tw = nn.Parameter(torch.zeros(DC.W))
        self.head_kind = head

    def _bb(self, x):
        y = self.bb(x)
        return y[0] if isinstance(y, (tuple, list)) else y

    def encode(self, E, A, B):
        with torch.no_grad():
            f = self._bb(torch.cat([E, A, B], -1))
        return f.detach()

    def g(self, E, A, B, feat=False):
        f = self.encode(E, A, B)
        h = self.head_net(f)  # (B, W)
        out = (h * torch.softmax(self.tw, 0)).sum(1)
        return (out, f) if feat else out

    def g_from_feat(self, f):
        h = self.head_net(f)
        return (h * torch.softmax(self.tw, 0)).sum(1)

    def forward(self, E, S, feat=False):
        S = (S - S.mean(1, keepdim=True)) / (S.std(1, keepdim=True) + 1e-6)
        if feat:
            a, fa = self.g(E, S[:, :, 0], S[:, :, 1], True)
            b, fb = self.g(E, S[:, :, 1], S[:, :, 0], True)
            return a - b, 0.5 * (fa + fb)
        return self.g(E, S[:, :, 0], S[:, :, 1]) - self.g(E, S[:, :, 1], S[:, :, 0])

    def forward_from_feats(self, fab, fba):
        """fab/fba: (B,W,d) backbone feats for (E,A,B) and (E,B,A)."""
        return self.g_from_feat(fab) - self.g_from_feat(fba)

    def trainable_parameters(self):
        for p in self.parameters():
            if p.requires_grad:
                yield p


def pub_fold_ckpt(held: str, seed: int = 0) -> str:
    if seed == 0:
        name = f"fold_direct_clf__eaP_n5_t60_w10b3_ptSPK_aug-sw2_wts__{held}.pt"
    else:
        name = f"fold_direct_clf__eaP_n5_t60_w10b3_ptSPK_s{seed}_aug-sw2_wts__{held}.pt"
    path = os.path.join(DATA_EXP, "models", name)
    if not os.path.exists(path):
        path = resolve_model(os.path.join("models", name))
    if not os.path.exists(path):
        raise FileNotFoundError(f"published SW2 fold ckpt missing: {name}")
    return path


def pub_all5_ckpt(seed: int = 0) -> str:
    name = f"all5_direct_clf_w10b3sw2P_s{seed}.pt"
    path = os.path.join(DATA_EXP, "models", name)
    if not os.path.exists(path):
        path = resolve_model(os.path.join("models", name))
    if not os.path.exists(path):
        raise FileNotFoundError(f"published SW2 all5 ckpt missing: {name}")
    return path


def load_frozen_featnet(ckpt_path: str, log) -> FeatGRUDirectNet:
    base = DC.DirectNet(N_EEG, 3, "vlaai_ws").to(DEV)
    sd = torch.load(ckpt_path, map_location=DEV, weights_only=False)
    missing, unexpected = base.load_state_dict(sd, strict=False)
    log(f"  loaded SW2 trunk {ckpt_path} (missing {len(missing)} unexpected {len(unexpected)})")
    net = FeatGRUDirectNet(base, head=HEAD).to(DEV)
    net.bb.eval()
    return net


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"] = SEED0
    D["model_blocks"] = 3
    D["max_epochs"] = MAX_EPOCHS
    D["steps_per_epoch"] = STEPS
    D["batch_size"] = BATCH
    D["patience"] = 6
    D["val_windows"] = int(os.environ.get("VAL_WINDOWS", "512"))
    D["lr"] = LR
    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=MIN_EPOCHS,
        prep=PREP, tag=TAG,
    )


def swap_check(log):
    """Antisymmetry + swap-delta on a FeatGRU net with random trunk init (shape smoke)."""
    torch.manual_seed(0)
    configure()
    base = DC.DirectNet(N_EEG, 3, "vlaai_ws").to(DEV)
    # load one real SW2 fold so trunk is the published one
    ckpt = pub_fold_ckpt("AVGC", SEED0)
    base.load_state_dict(torch.load(ckpt, map_location=DEV, weights_only=False), strict=False)
    net = FeatGRUDirectNet(base, head=HEAD).to(DEV).eval()
    with torch.no_grad():
        E = torch.randn(4, DC.W, N_EEG, device=DEV)
        A = torch.randn(4, DC.W, 3, device=DEV)
        B = torch.randn(4, 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())
        # zero-init proj ⇒ head≈0 ⇒ pool≈0 at step 0 (near-zero logits)
        zero_logit = float(y_ab.abs().mean())
        n_train = sum(p.numel() for p in net.trainable_parameters())
        n_frozen = sum(p.numel() for p in net.bb.parameters())
    log(f"swap-test head={HEAD} anti_err {anti_err:.3e} swap_delta {delta:.4f} "
        f"step0_|logit| {zero_logit:.3e} trainable {n_train} frozen_bb {n_frozen} device {DEV}")
    del net, base
    if DEV.type == "cuda":
        torch.cuda.empty_cache()
    if anti_err > 1e-5:
        raise RuntimeError(f"antisymmetry broken: {anti_err}")
    # At zero-init proj, swap delta may be tiny; after a few steps it grows.
    # Require that architecture *can* differ (delta from random GRU weights).
    # Re-check with non-zero proj:
    base = DC.DirectNet(N_EEG, 3, "vlaai_ws").to(DEV)
    base.load_state_dict(torch.load(ckpt, map_location=DEV, weights_only=False), strict=False)
    net = FeatGRUDirectNet(base, head=HEAD).to(DEV).eval()
    with torch.no_grad():
        for p in net.head_net.parameters():
            if p.dim() >= 2:
                nn.init.xavier_uniform_(p, gain=1.0)
        gab = net.g(E.to(DEV), A.to(DEV), B.to(DEV))
        gba = net.g(E.to(DEV), B.to(DEV), A.to(DEV))
        delta2 = float((gab - gba).abs().mean())
    log(f"  post-xavier swap_delta {delta2:.4f}")
    del net, base
    if delta2 < 1e-4:
        raise RuntimeError(f"swap-delta≈0 even after xavier ({delta2:.3e})")
    return delta2, anti_err


def _extract_pair_feats(net, X, S_):
    """Return fab, fba feature tensors for antisymmetric pair."""
    S_n = (S_ - S_.mean(1, keepdim=True)) / (S_.std(1, keepdim=True) + 1e-6)
    fab = net.encode(X, S_n[:, :, 0], S_n[:, :, 1])
    fba = net.encode(X, S_n[:, :, 1], S_n[:, :, 0])
    return fab, fba


def cache_path_for(tag: str) -> str:
    return os.path.join(FEAT_CACHE, f"{tag}_s{SEED0}_{HEAD}.npz")


def dump_fold_feats(net, train, val, tag, log, a):
    os.makedirs(FEAT_CACHE, exist_ok=True)
    seed = D.get("train_seed", CFG["seed"])
    aug = decoders.DNNDec.__new__(decoders.DNNDec)
    aug.W, aug.fut, aug.hop, aug.C = DC.W, DC.FUT, DC.HOP, N_EEG
    # stable but tag-dependent RNG
    tag_salt = abs(hash(tag)) % (2 ** 31)
    rs = np.random.default_rng(seed + 17 + tag_salt)
    net.eval()

    def _pack(trials, n, use_aug):
        X, S_, y = DC.windows(trials, n, rs, aug if use_aug else None, a.p_switch, a.zero_eeg)
        X, S_ = X.to(DEV), S_.to(DEV)
        with torch.no_grad():
            fab, fba = _extract_pair_feats(net, X, S_)
        return fab.cpu().numpy().astype(np.float16), fba.cpu().numpy().astype(np.float16), y.numpy()

    tr_fab, tr_fba, tr_y = [], [], []
    left = N_CACHE_TRAIN
    chunk = min(max(BATCH * 4, 64), 256)
    while left > 0:
        n = min(chunk, left)
        fab, fba, y = _pack(train, n, True)
        tr_fab.append(fab); tr_fba.append(fba); tr_y.append(y)
        left -= n
        done = N_CACHE_TRAIN - left
        if done % 2048 == 0 or left == 0:
            log(f"    dump train {done}/{N_CACHE_TRAIN}")
    va_fab, va_fba, va_y = _pack(val, N_CACHE_VAL, False)
    path = cache_path_for(tag)
    np.savez_compressed(
        path,
        train_fab=np.concatenate(tr_fab), train_fba=np.concatenate(tr_fba), train_y=np.concatenate(tr_y),
        val_fab=va_fab, val_fba=va_fba, val_y=va_y,
        d=net.d, W=DC.W, head=HEAD,
    )
    log(f"  wrote feat cache {path}")
    return path


def fit_from_cache(net, cache_path, log, check_chance=True):
    blob = np.load(cache_path)
    fab_tr = torch.from_numpy(blob["train_fab"].astype(np.float32))
    fba_tr = torch.from_numpy(blob["train_fba"].astype(np.float32))
    y_tr = torch.from_numpy(blob["train_y"].astype(np.float32))
    fab_va = torch.from_numpy(blob["val_fab"].astype(np.float32)).to(DEV)
    fba_va = torch.from_numpy(blob["val_fba"].astype(np.float32)).to(DEV)
    y_va = torch.from_numpy(blob["val_y"].astype(np.float32))
    n = len(y_tr)
    opt = torch.optim.AdamW(list(net.trainable_parameters()), lr=LR, weight_decay=1e-4)
    sch = torch.optim.lr_scheduler.ReduceLROnPlateau(opt, mode="min", factor=0.5, patience=D["lr_patience"])
    rng = np.random.default_rng(D.get("train_seed", 0) + 99)
    best, bad, state = np.inf, 0, None
    early = []
    global_step = 0
    net.bb.eval()
    for ep in range(MAX_EPOCHS):
        t1, tl = time.time(), []
        net.train(); net.bb.eval()
        for _ in range(STEPS):
            idx = rng.integers(0, n, size=BATCH)
            fab = fab_tr[idx].to(DEV)
            fba = fba_tr[idx].to(DEV)
            y = y_tr[idx].to(DEV)
            logit = net.forward_from_feats(fab, fba)
            loss = F.binary_cross_entropy_with_logits(logit, y)
            opt.zero_grad(set_to_none=True)
            loss.backward()
            torch.nn.utils.clip_grad_norm_(list(net.trainable_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 = net.forward_from_feats(fab_va, fba_va).cpu()
        vl = float(F.binary_cross_entropy_with_logits(lv, y_va))
        va = float(((lv > 0).float() == y_va).float().mean())
        if ep >= MIN_EPOCHS:
            sch.step(vl)
        log(f"    epoch {ep:02d} train {np.mean(tl):.4f} val BCE {vl:.4f} acc {va:.3f} "
            f"({time.time() - t1:.0f}s) [cache]")
        if vl < best - 1e-4:
            best, bad, state = vl, 0, {k: v.detach().clone() for k, v in net.state_dict().items()}
        elif ep >= 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 fit_online(net, train, val, log, a, check_chance=True):
    seed = D.get("train_seed", CFG["seed"])
    torch.manual_seed(seed)
    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(list(net.trainable_parameters()), lr=LR, weight_decay=1e-4)
    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
    n_train = sum(p.numel() for p in net.trainable_parameters())
    log(f"  FeatGRU head={HEAD} trainable {n_train} batch {BATCH} lr {LR} device {DEV}")
    for ep in range(MAX_EPOCHS):
        t1, tl = time.time(), []
        net.train(); net.bb.eval()
        for _ in range(STEPS):
            X, S_, y = [z.to(DEV) for z in DC.windows(
                train, BATCH, rs, aug, a.p_switch, a.zero_eeg)]
            # frozen encode + trainable head
            with torch.no_grad():
                fab, fba = _extract_pair_feats(net, X, S_)
            logit = net.forward_from_feats(fab, fba)
            loss = F.binary_cross_entropy_with_logits(logit, y)
            opt.zero_grad(set_to_none=True)
            loss.backward()
            torch.nn.utils.clip_grad_norm_(list(net.trainable_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():
            preds = []
            for i in range(0, len(Xv), 128):
                xb = Xv[i:i + 128].to(DEV)
                sb = Sv[i:i + 128].to(DEV)
                preds.append(net(xb, sb).cpu())
            lv = torch.cat(preds)
        vl = float(F.binary_cross_entropy_with_logits(lv, yv))
        va = float(((lv > 0).float() == yv).float().mean())
        if ep >= MIN_EPOCHS:
            sch.step(vl)
        log(f"    epoch {ep:02d} train {np.mean(tl):.4f} val BCE {vl:.4f} acc {va:.3f} "
            f"({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 >= 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 fit_net(train, val, log, a, held, check_chance=True, init_ckpt=None, cache_tag=None):
    ckpt = init_ckpt or pub_fold_ckpt(held, SEED0)
    net = load_frozen_featnet(ckpt, log)
    tag = cache_tag or f"held-{held}"
    cpath = cache_path_for(tag)
    use_cache = os.environ.get("CACHE_THEN_TRAIN", "1") == "1" or TRAIN_FROM_CACHE or DUMP_FEATS
    if use_cache:
        if not os.path.exists(cpath) or DUMP_FEATS:
            dump_fold_feats(net, train, val, tag, log, a)
        return fit_from_cache(net, cpath, log, check_chance=check_chance)
    return fit_online(net, train, val, log, a, check_chance=check_chance)


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, held, 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="vlaai_ws_frozen",
                head=HEAD, 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 _load_featgru_state(path, log):
    """Rebuild FeatGRUDirectNet and load a saved state_dict (bb + head)."""
    # state has bb.* + head_net.* + tw; need a SW2-shaped base first
    base = DC.DirectNet(N_EEG, 3, "vlaai_ws").to(DEV)
    net = FeatGRUDirectNet(base, head=HEAD).to(DEV)
    sd = torch.load(path, map_location=DEV, weights_only=False)
    missing, unexpected = net.load_state_dict(sd, strict=False)
    log(f"  loaded FeatGRU {path} (missing {len(missing)} unexpected {len(unexpected)})")
    net.eval()
    return net


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 = _load_featgru_state(ALL5_PT, log)
        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, held="AVGC", check_chance=False,
                                init_ckpt=pub_all5_ckpt(SEED0), cache_tag="all5")
            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


def write_results(per, ext, mH, mA, hp_per, hp_mH, hp_mA, swap_delta, status="done",
                  chance_note=None):
    pub = dict(mean_H=0.7846, mean_Acc=72.14)
    leader = LEADER_AASD
    lines = [
        "# results_sw2_feat_gru",
        "",
        "**Freeze published SW2 `vlaai_ws` backbone; train NEW causal "
        f"`{HEAD}` head** on penultimate features → frame logits → pool.",
        "Init held-*/all5 SW2 DirectNet. Trunk frozen. Distinct from full FT "
        "hardswitch, vlaai identity residuals, aux-env/InfoNCE, tip PP, "
        "FIR/Morlet/FBCNet.",
        "",
        f"Swap-test (xavier) delta={swap_delta:.4f}. Prep `eaP_n5_t60`, "
        f"max {MAX_EPOCHS} ep×{STEPS}, batch {BATCH}, device default CPU "
        f"(feat-cache then head). HMM p_stay {P_STAY} scale {SCALE}; also hp30.",
        "",
        f"Code: `sw2_feat_gru_lodo.py`. Checkpoints: `models/sw2_feat_gru/held-*.pt`, "
        f"`{ALL5_PT}`. JSON: `{OUT_JSON}`.",
        "",
    ]
    if status == "abandoned":
        lines += [
            f"**ABANDONED**: {chance_note}",
            "",
            "## NEXT IDEA",
            "",
            "Frozen-SW2 + new GRU/Conv1d head did not clear chance. Prefer a "
            "qualitatively new Acc front-end outside frozen-trunk head swaps "
            "(not hardswitch FT, not vlaai residuals, not tip PP).",
            "",
        ]
        open(RESULTS_MD, "w").write("\n".join(lines))
        return RESULTS_MD

    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} |",
        "",
        "## External (preset)",
        "",
        "| Set | H | Acc % | hp30 H/Acc | Acc tip 53.53 | Δtip | Acc>60? |",
        "|---|---:|---:|---|---|---|---|",
        f"| AASD | {ext['AASD']['raw']['H']:.4f} | {ext['AASD']['raw']['Acc']:.2f} | "
        f"{ext['AASD']['hp30']['H']:.4f}/{ext['AASD']['hp30']['Acc']:.2f} | "
        f"{leader:.2f} | Δ{ext['AASD']['raw']['Acc'] - leader:+.2f} | "
        f"{'YES' if ext['AASD']['raw']['Acc'] > 60 else 'NO'} |",
        f"| Carta2026 | {ext['Carta2026']['raw']['H']:.4f} | {ext['Carta2026']['raw']['Acc']:.2f} | "
        f"{ext['Carta2026']['hp30']['H']:.4f}/{ext['Carta2026']['hp30']['Acc']:.2f} | "
        f"— | — | — |",
        "",
        "## Targets",
        "",
        "| Target | Result |",
        "|---|---|",
        f"| AVGC H>=0.85 Acc>=78 | miss ({per['AVGC']['H']:.4f}/{per['AVGC']['Acc']:.2f}) |",
        f"| UHD H>=0.85 | {'met' if per['UHD']['H'] >= 0.85 else 'miss'} ({per['UHD']['H']:.4f}) |",
        f"| AASD Acc>60 with H>0.63 | "
        f"{'met' if ext['AASD']['raw']['Acc'] > 60 and ext['AASD']['raw']['H'] > 0.63 else 'miss'} "
        f"({ext['AASD']['raw']['H']:.4f}/{ext['AASD']['raw']['Acc']:.2f}) |",
        f"| AASD Acc vs tip 53.53 | Δ {ext['AASD']['raw']['Acc'] - leader:+.2f} |",
        "",
        "## NEXT IDEA",
        "",
    ]
    aasd = ext["AASD"]["raw"]["Acc"]
    if aasd > 60:
        lines.append(
            "AASD Acc>60 hit with frozen-SW2 + new head. Next: multi-seed "
            "ensemble of feat-GRU heads or fuse with tip hard_abs_swmix.")
    elif aasd >= leader - 0.3:
        lines.append(
            "Near tip Acc with frozen trunk + new head. Next: try Conv1d head "
            "ablation if GRU was used (or vice versa), or multi-seed mean of "
            "feat-heads — still no tip PP / hardswitch / residuals.")
    else:
        lines.append(
            "Frozen-SW2 + new GRU/Conv1d head did not beat tip Acc 53.53. "
            "Close the frozen-trunk head-swap family. Prefer non-vlaai Acc "
            "ideas outside tip PP / denser-switch / multitask-on-vlaai.")
    lines.append("")
    open(RESULTS_MD, "w").write("\n".join(lines))
    return RESULTS_MD


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 "| sw2-feat-gru |" 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_done(aasd_acc, aasd_h, note):
    path = os.path.join(EXP, "multi_agent_opt", "STATUS_GAPS.md")
    text = open(path).read()
    # light touch: mark sw2-feat-gru closed in "In flight"
    text2 = text.replace(
        "2. **sw2-feat-gru** — freeze SW2 trunk + new GRU head Acc (queued).",
        f"2. **sw2-feat-gru** — done AASD Acc **{aasd_acc:.2f}** (H {aasd_h:.4f}). {note}",
    )
    if text2 == text:
        text2 = text.replace(
            "2. **sw2-feat-gru** — freeze SW2 trunk + new GRU head Acc (queued).",
            f"2. ~~sw2-feat-gru~~ done Acc {aasd_acc:.2f}.",
        )
    open(path, "w").write(text2)


def main():
    configure()
    log = log_to(LOG_PATH)
    a = namespace()
    mark_claim("running", f"DEVICE={DEV} HEAD={HEAD}; feat-cache then CPU head; results_sw2_feat_gru.md")
    log(f"=== {EXP_NAME} FeatGRU head={HEAD} device {DEV} "
        f"max_epochs {MAX_EPOCHS} batch {BATCH} CACHE_THEN_TRAIN="
        f"{os.environ.get('CACHE_THEN_TRAIN', '1')} ===")
    try:
        swap_delta, _ = swap_check(log)
        for i, held in enumerate(DS_KEYS):
            path = fold_file(EXP_NAME, held)
            if os.path.exists(path):
                log(f"  fold held-out={held}: cached {path}")
                continue
            run_fold(held, EXP_NAME, log, a, check_chance=(i == 0))
        per, ext, mu, sd, mH, mA, hp_per, hp_mH, hp_mA = score_preset(log, a)
        out = dict(
            experiment=EXP_NAME, decoder="direct_clf", prep=PREP,
            targets=D["targets"], p_switch=0.5, head=HEAD, gru_hid=GRU_HID,
            init="published_sw2_held_all5", min_epochs=MIN_EPOCHS,
            max_epochs=MAX_EPOCHS, backbone="vlaai_ws_frozen", window_s=10.0,
            train_seed=D["train_seed"], extra_train="Fuglsang2020",
            augment=D["augment"], swap_delta=swap_delta, device=str(DEV),
            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),
            mean_H=mH, mean_Acc=mA, mean_H_hp30=hp_mH, mean_Acc_hp30=hp_mA,
            aasd_acc=ext["AASD"]["raw"]["Acc"],
            aasd_vs_tip=ext["AASD"]["raw"]["Acc"] - LEADER_AASD,
            acc_gt_60=bool(ext["AASD"]["raw"]["Acc"] > 60),
        )
        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, swap_delta)
        note = (f"LODO mean H {mH:.4f} Acc {mA:.2f}. "
                f"AASD {ext['AASD']['raw']['H']:.4f}/{ext['AASD']['raw']['Acc']:.2f} "
                f"(Δtip {ext['AASD']['raw']['Acc'] - LEADER_AASD:+.2f}; "
                f"Acc>60={'YES' if ext['AASD']['raw']['Acc'] > 60 else 'NO'}). "
                f"Carta {ext['Carta2026']['raw']['H']:.4f}/{ext['Carta2026']['raw']['Acc']:.2f}. "
                f"`{rpath}`.")
        mark_claim("done", note)
        update_status_done(ext["AASD"]["raw"]["Acc"], ext["AASD"]["raw"]["H"],
                           "Close frozen-trunk head-swap if Acc≪60.")
        # ALREADY_TRIED append
        already = os.path.join(EXP, "multi_agent_opt", "ALREADY_TRIED.md")
        with open(already, "a") as fh:
            fh.write(
                f"\n- **sw2-feat-gru** (`sw2_feat_gru_lodo.py`; freeze published SW2 "
                f"vlaai_ws held/all5; NEW causal {HEAD} head on penultimate feats; "
                f"eaP_n5_t60; ≠ hardswitch FT / vlaai residuals / aux-InfoNCE / tip PP / "
                f"FIR-Morlet-FBCNet): LODO {mH:.4f}/{mA:.2f}. "
                f"AASD {ext['AASD']['raw']['H']:.4f}/{ext['AASD']['raw']['Acc']:.2f} "
                f"(Δtip {ext['AASD']['raw']['Acc'] - LEADER_AASD:+.2f}). "
                f"Carta {ext['Carta2026']['raw']['Acc']:.2f}. Acc>60="
                f"{'YES' if ext['AASD']['raw']['Acc'] > 60 else 'NO'}. "
                f"`multi_agent_opt/results_sw2_feat_gru.md`.\n"
            )
        log(f"wrote {OUT_JSON} and {rpath}")
        log("=== done ===")
        print(f"\nAASD Acc={ext['AASD']['raw']['Acc']:.2f} vs tip {LEADER_AASD} "
              f"(Δ{ext['AASD']['raw']['Acc'] - LEADER_AASD:+.2f}); "
              f"Acc>60={'YES' if ext['AASD']['raw']['Acc'] > 60 else 'NO'}")
    except ChanceStuckError as e:
        msg = str(e)
        mark_claim("abandoned", msg[:220])
        write_results({}, {}, 0, 0, {}, 0, 0, 0.0, status="abandoned", chance_note=msg)
        log(f"ABANDONED: {msg}")
        raise SystemExit(2)
    except RuntimeError as e:
        msg = str(e)
        if "swap" in msg.lower() or msg.startswith("ABORT"):
            mark_claim("abandoned", msg[:220])
            write_results({}, {}, 0, 0, {}, 0, 0, 0.0, status="abandoned", chance_note=msg)
            log(f"ABANDONED: {msg}")
            raise SystemExit(2)
        raise


if __name__ == "__main__":
    main()
