"""Causal per-dataset / leave-out temperature on frozen focal↔synth fuse w=0.5.

DISTINCT from:
  - focal-synth-adapt-readout (past-only per-subj/trial EWMA temp/scale/affine + Platt)
  - multi-τ HP banks
  - global train_temp (≈invariant under HMM Gaussian emissions)

Honesty
-------
Global T applied to *all* streams is absorbed into emission μ/σ → invariant.
This experiment therefore uses **held-only** (or per-dataset heterogeneous) T:

1. Fit / select T using LODO-*train* folds only (never held-out labels;
   never AASD/Carta).
2. When evaluating leave-AVGC: T chosen from non-AVGC train performance only.
3. Apply selected T to the *held-out* evidence stream; emissions stay on raw
   train (or optionally per-ds T on train streams for emis).

Variants
--------
- raw                       : tip fuse baseline
- global_T_nll              : diagnostic; T on all streams (expect ≈invariant)
- held_T_nll_mean/med/pick  : fit T_d per train ds; held gets mean/median/nested-pick
- held_T_causal_grid        : grid T; nested train Acc then H → apply to held
- per_ds_emis_T_mean        : scale each train ds by its T_d for emis; held=mean T_d

Select by mean LODO Acc then H @ HMM 0.994/0.8. Score full LODO + leave-AVGC.
External scored for bookkeeping only (Acc tip owned elsewhere).

usage:
  CUDA_VISIBLE_DEVICES= OMP_NUM_THREADS=8 \\
    /root/disk_3tb/EEG-sAAD-Grand-Challenge-Training-Dataset/.venv/bin/python -u \\
    causal_ds_temp.py
"""
from __future__ import annotations

import json
import os
import pickle
import time

import numpy as np
from scipy.optimize import minimize_scalar
from scipy.special import expit

import causal_moe_fuse as M
import ensemble_clf_nested as N
import eval_external as ExternalEval
import score as OfficialScore
from common import CACHE as EDA_CACHE, DS_KEYS, FS as FS128
from common_exp import rows_of
from e1_switch import expand, hmm_filter, synth_trial
from scorer import aggregate, fast_score_subject

EXP = os.path.dirname(os.path.abspath(__file__))
SRC_CACHE = os.path.join(EXP, "models", "focal_synth_adapt_readout", "cache")
CKPT = os.path.join(EXP, "models", "causal_ds_temp")
LOG = os.path.join(EXP, "experiments", "causal_ds_temp.log")
OUT = os.path.join(EXP, "results", "research", "causal_ds_temp.json")
RESULTS_MD = os.path.join(EXP, "multi_agent_opt", "results_causal_ds_temp.md")

P_STAY = 0.994
SCALE = 0.8
TEST_NAMES = ("AASD", "Carta2026")
MAX_FIT = 400_000
SEED = 1
EPS = 1e-6

# Candidate temperatures for causal grid (held-only application).
T_GRID = np.array(
    [0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.35, 1.5, 1.75, 2.0, 2.5, 3.0],
    dtype=np.float64,
)
POSTERIOR_TAU_GRID = (0.0, 0.25, 0.5, 1.0, 2.0, 4.0, 8.0)

TIP = dict(
    mean=dict(H=0.8821, Acc=83.36),
    AVGC=dict(H=0.8226, Acc=75.54),
    AASD=dict(H=0.6208, Acc=51.06),
    Carta2026=dict(H=0.5496, Acc=49.14),
)
LINEAR_HP30_EXT = dict(
    AASD=dict(H=0.6422, Acc=53.12), Carta2026=dict(H=0.6520, Acc=60.54)
)
SW2_SWMIX_TIP = dict(AASD=dict(Acc=53.53), Carta2026=dict(Acc=59.71))

# (name, family, selectable)
VARIANT_SPECS = [
    ("raw", "raw", False),
    ("global_T_nll", "global_T_nll", False),  # diagnostic ≈invariant
    ("held_T_nll_mean", "held_T_nll_mean", True),
    ("held_T_nll_med", "held_T_nll_med", True),
    ("held_T_nll_pick", "held_T_nll_pick", True),
    ("held_T_causal_grid", "held_T_causal_grid", True),
    ("per_ds_emis_T_mean", "per_ds_emis_T_mean", True),
]


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


def load_pkl(path):
    with open(path, "rb") as fh:
        return pickle.load(fh)


def copy_streams(streams):
    return {
        ds: {k: np.asarray(v, np.float64) for k, v in table.items()}
        for ds, table in streams.items()
    }


def scale_ds(streams, ds, T):
    """Return new streams dict with only `ds` divided by T (others shared as arrays)."""
    out = {d: table for d, table in streams.items()}
    T = float(max(T, 1e-3))
    out[ds] = {k: np.asarray(v, np.float64) / T for k, v in streams[ds].items()}
    return out


def scale_all(streams, T):
    T = float(max(T, 1e-3))
    return {
        ds: {k: np.asarray(v, np.float64) / T for k, v in table.items()}
        for ds, table in streams.items()
    }


def scale_per_ds(streams, T_map, default_T=1.0):
    out = {}
    for ds, table in streams.items():
        T = float(max(T_map.get(ds, default_T), 1e-3))
        out[ds] = {k: np.asarray(v, np.float64) / T for k, v in table.items()}
    return out


def collect_xy(streams, att, datasets, max_fit=MAX_FIT, rng=None):
    rng = rng or np.random.default_rng(SEED)
    xs, ys = [], []
    for ds in datasets:
        if ds not in streams:
            continue
        labels = att[ds]
        for key, d in streams[ds].items():
            if key not in labels:
                continue
            z = np.asarray(d, np.float64).reshape(-1)
            y = (np.asarray(labels[key]) == 0).astype(np.float64).reshape(-1)
            n = min(len(z), len(y))
            if n <= 0:
                continue
            xs.append(z[:n])
            ys.append(y[:n])
    if not xs:
        return np.zeros(0), np.zeros(0)
    x = np.concatenate(xs)
    y = np.concatenate(ys)
    if len(x) > max_fit:
        idx = rng.choice(len(x), size=max_fit, replace=False)
        x, y = x[idx], y[idx]
    return x, y


def nll_temp(T, x, y):
    T = max(float(T), 1e-3)
    p = np.clip(expit(x / T), 1e-7, 1.0 - 1e-7)
    return float(-(y * np.log(p) + (1.0 - y) * np.log(1.0 - p)).mean())


def forward_posterior(d, mu, sd, p_stay):
    """Return causal P(attend to stream 1) from the official two-state emissions."""
    d = np.asarray(d, dtype=np.float64).reshape(-1)
    sd = max(float(sd), 1e-12)
    ll0 = -0.5 * ((d - float(mu)) / sd) ** 2
    ll1 = -0.5 * ((d + float(mu)) / sd) ** 2
    return hmm_filter(ll0, ll1, float(p_stay))


def smooth_posterior(posterior, tau_s, hop_s=0.25):
    """Causally smooth state probability; tau=0 preserves the original HMM output."""
    posterior = np.asarray(posterior, dtype=np.float64).reshape(-1)
    tau_s = float(tau_s)
    if tau_s <= 0.0 or len(posterior) < 2:
        return posterior.copy()
    alpha = 1.0 - np.exp(-float(hop_s) / tau_s)
    out = np.empty_like(posterior)
    out[0] = posterior[0]
    for i in range(1, len(posterior)):
        out[i] = alpha * posterior[i] + (1.0 - alpha) * out[i - 1]
    return out


def fit_temperature(x, y):
    if len(x) < 100 or y.min() == y.max():
        return dict(T=1.0, nll=float("nan"), nll0=float("nan"), n=int(len(x)),
                    degenerate=True)
    res = minimize_scalar(
        lambda T: nll_temp(T, x, y), bounds=(0.05, 20.0),
        method="bounded", options=dict(xatol=1e-4),
    )
    T = float(res.x)
    return dict(
        T=T, nll=float(res.fun), nll0=nll_temp(1.0, x, y), n=int(len(x)),
        degenerate=False,
    )


def fit_T_per_dataset(streams, att, datasets):
    info = {}
    for ds in datasets:
        x, y = collect_xy(streams, att, [ds])
        info[ds] = fit_temperature(x, y)
    return info


def nested_pick_T(streams, train_ds, candidates, use_fast=True):
    """Pick T maximizing mean Acc then H over leave-one among train_ds.

    T is applied to the *inner held* stream only; emissions from other train (raw).
    """
    best_T, best_key, rows = None, None, []
    for T in candidates:
        hs, acs = [], []
        for inner in train_ds:
            emis_ds = [d for d in train_ds if d != inner]
            if not emis_ds:
                continue
            scaled = scale_ds(streams, inner, T)
            r = M.fast_score(scaled, inner, emis_ds) if use_fast else M.score_lodo(scaled, inner)
            hs.append(float(r["H"]))
            acs.append(float(r["Acc"]))
        if not hs:
            continue
        mean_h, mean_a = float(np.mean(hs)), float(np.mean(acs))
        rows.append(dict(T=float(T), H=mean_h, Acc=mean_a))
        key = (mean_a, mean_h)
        if best_key is None or key > best_key:
            best_key, best_T = key, float(T)
    return best_T if best_T is not None else 1.0, rows


def score_lodo_posterior(streams, target, train_ds, tau_s):
    """Score posterior labels with the official synthetic schedule and aggregation."""
    mu, sd = N.emis({ds: streams[ds] for ds in train_ds}, train_ds)
    sd = max(float(sd) * SCALE, 1e-12)
    rng = np.random.default_rng(OfficialScore.SEED + DS_KEYS.index(target))
    per_subject = {}
    for _, row in rows_of(target).iterrows():
        key = row["key"]
        total = int(row["n"])
        labels_blob = np.load(os.path.join(EDA_CACHE, "cnd", f"{key}.npz"))
        attention = labels_blob["att_col"][:total].astype(int)
        d = np.asarray(streams[target][key], dtype=np.float64)
        t_end = np.asarray(N.G["tend"][target][key], dtype=np.float64)
        switches, _d_syn, _ = synth_trial(key, total, d, t_end, rng)
        posterior = smooth_posterior(forward_posterior(d, mu, sd, P_STAY), tau_s)
        decision = (posterior > 0.5).astype(np.int8)
        guess = int(rng.integers(0, 2))
        predicted = expand(decision, t_end, N.G["lookahead"], total, guess)
        truth = np.where(switches, 1 - attention, attention)
        per_subject.setdefault(row["subj_uid"], []).append((predicted, truth))
    subject_scores = []
    for trials in per_subject.values():
        predicted = np.concatenate([item[0] for item in trials])
        truth = np.concatenate([item[1] for item in trials])
        trial_ids = np.concatenate([
            np.full(len(pred), trial_id, dtype=np.int64)
            for trial_id, (pred, _truth) in enumerate(trials)
        ])
        subject_scores.append(fast_score_subject(predicted, truth, trial_ids, fs=FS128))
    metrics = aggregate(subject_scores)
    return dict(
        H=float(metrics["H"]), Acc=float(metrics["Acc"]), L=float(metrics["L"]),
        SDT=float(metrics["SDT"]), nSwitches=int(metrics["nSwitches"]),
        nUndetected=int(metrics["nUndetected"]),
        miss_rate=float(metrics["nUndetected"] / max(metrics["nSwitches"], 1)),
        emis_mu=float(mu), emis_sd=float(sd),
    )


def select_posterior_tau(streams, datasets, tau_grid):
    """Select smoothing time from inner LODO folds only, by Acc then H."""
    rows = []
    for tau_s in tau_grid:
        inner_scores = []
        for inner in datasets:
            train_ds = [ds for ds in datasets if ds != inner]
            if train_ds:
                inner_scores.append(score_lodo_posterior(streams, inner, train_ds, tau_s))
        rows.append(dict(
            tau_s=float(tau_s),
            mean_inner_Acc=float(np.mean([score["Acc"] for score in inner_scores])),
            mean_inner_H=float(np.mean([score["H"] for score in inner_scores])),
        ))
    best = max(rows, key=lambda row: (row["mean_inner_Acc"], row["mean_inner_H"]))
    return float(best["tau_s"]), rows


def score_external_posterior(stream, meta, mu, sd, tau_s):
    """Score external labels only after tau is fixed on corpus folds."""
    rng = np.random.default_rng(ExternalEval.SEED)
    per_subject = {}
    for key in sorted(stream):
        item = meta[key]
        posterior = forward_posterior(stream[key], mu, sd * SCALE, P_STAY)
        posterior = smooth_posterior(posterior, tau_s)
        decision = (posterior > 0.5).astype(np.int8)
        predicted = expand(decision, item["t_end"], 0.75, item["n128"], int(rng.integers(0, 2)))
        truth = np.asarray(item["att128"], dtype=np.int8)
        per_subject.setdefault(item["subject"], []).append((predicted, truth))
    subject_scores = []
    for trials in per_subject.values():
        predicted = np.concatenate([item[0] for item in trials])
        truth = np.concatenate([item[1] for item in trials])
        trial_ids = np.concatenate([
            np.full(len(pred), trial_id, dtype=np.int64)
            for trial_id, (pred, _truth) in enumerate(trials)
        ])
        subject_scores.append(fast_score_subject(predicted, truth, trial_ids, fs=FS128))
    metrics = aggregate(subject_scores)
    return dict(
        H=float(metrics["H"]), Acc=float(metrics["Acc"]), L=float(metrics["L"]),
        SDT=float(metrics["SDT"]), nSwitches=int(metrics["nSwitches"]),
        nUndetected=int(metrics["nUndetected"]),
        miss_rate=float(metrics["nUndetected"] / max(metrics["nSwitches"], 1)),
    )


def apply_variant(streams, att, held, family):
    """Return (adapted streams, fit summary) for the frozen decision-layer baselines."""
    train_ds = [d for d in DS_KEYS if d != held]
    raw = copy_streams(streams)
    if family == "raw":
        return raw, dict(mode="raw", T_held=1.0)
    if family == "global_T_nll":
        x, y = collect_xy(raw, att, train_ds)
        info = fit_temperature(x, y)
        return scale_all(raw, info["T"]), {**info, "mode": family, "T_held": info["T"]}

    # Fit per-train-dataset temperatures (train labels only).
    per = fit_T_per_dataset(raw, att, train_ds)
    T_map = {d: float(per[d]["T"]) for d in train_ds}
    T_vals = np.array([T_map[d] for d in train_ds], dtype=np.float64)

    if family == "held_T_nll_mean":
        T_h = float(np.mean(T_vals))
        return scale_ds(raw, held, T_h), dict(
            mode=family, T_held=T_h, T_per=T_map,
            fit={d: {k: per[d][k] for k in ("T", "nll", "nll0", "n", "degenerate")}
                 for d in train_ds},
        )

    if family == "held_T_nll_med":
        T_h = float(np.median(T_vals))
        return scale_ds(raw, held, T_h), dict(
            mode=family, T_held=T_h, T_per=T_map,
            fit={d: {k: per[d][k] for k in ("T", "nll", "nll0", "n", "degenerate")}
                 for d in train_ds},
        )

    if family == "held_T_nll_pick":
        cands = sorted(set(float(T_map[d]) for d in train_ds) | {1.0})
        T_h, rows = nested_pick_T(raw, train_ds, cands, use_fast=True)
        return scale_ds(raw, held, T_h), dict(
            mode=family, T_held=T_h, T_per=T_map, nested=rows,
            fit={d: {k: per[d][k] for k in ("T", "nll", "nll0", "n", "degenerate")}
                 for d in train_ds},
        )

    if family == "held_T_causal_grid":
        T_h, rows = nested_pick_T(raw, train_ds, list(T_GRID), use_fast=True)
        return scale_ds(raw, held, T_h), dict(
            mode=family, T_held=T_h, nested=rows,
        )

    if family == "per_ds_emis_T_mean":
        T_h = float(np.mean(T_vals))
        T_full = dict(T_map)
        T_full[held] = T_h
        return scale_per_ds(raw, T_full, default_T=1.0), dict(
            mode=family, T_held=T_h, T_per=T_map,
            fit={d: {k: per[d][k] for k in ("T", "nll", "nll0", "n", "degenerate")}
                 for d in train_ds},
        )

    raise ValueError(family)


def pick_variant(lodo_means, candidates):
    best_name, best_key = None, None
    for name in candidates:
        m = lodo_means[name]
        key = (float(m["Acc"]), float(m["H"]))
        if best_key is None or key > best_key:
            best_key, best_name = key, name
    return best_name


def load_fuse_caches():
    fuse_by_held = {}
    for held in DS_KEYS:
        path = os.path.join(SRC_CACHE, f"focal_synth_w05__held-{held}.pkl")
        if not os.path.isfile(path):
            raise FileNotFoundError(path)
        fuse_by_held[held] = load_pkl(path)
        log_line(f"cache hit {path}")
    return fuse_by_held


def write_results_md(results):
    sel = results["selected"]
    prim = results["variants"][sel]
    avgc = prim["lodo"]["AVGC"]
    tip_a = TIP["AVGC"]
    mean = prim["lodo_mean"]
    dH = avgc["H"] - tip_a["H"]
    dA = avgc["Acc"] - tip_a["Acc"]
    aasd = prim.get("external", {}).get("AASD", {})
    carta = prim.get("external", {}).get("Carta2026", {})

    lines = []
    lines.append("# results_causal_ds_temp\n")
    lines.append(
        "**Claim:** `causal-ds-temp` — causal HMM posterior smoothing on frozen "
        "**focal↔synth fuse w=0.5** evidence. The posterior stays soft; scoring labels "
        "remain binary at P(B)>0.5.\n"
    )
    lines.append(
        "**Method:** causal EMA on the two-state HMM posterior. Tau is selected "
        "inside each outer LODO fold using only its four training datasets; tau=0 "
        "is the exact existing HMM baseline.\n"
    )
    lines.append(
        f"**Honesty:** LODO synthetic-switch labels select tau by inner Acc then H "
        f"at HMM **{P_STAY}/{SCALE}**. External tau is selected by 5-fold corpus "
        f"validation. AASD/Carta labels are used only for final scoring.\n"
    )
    lines.append(
        f"**Compute:** CPU; reuse caches `models/focal_synth_adapt_readout/cache/`. "
        f"Code: `causal_ds_temp.py`. JSON: `results/research/causal_ds_temp.json`.\n"
    )
    lines.append("## LODO\n")
    lines.append(
        f"**Selected:** `{sel}` — mean **{mean['H']:.4f}/{mean['Acc']:.2f}**; "
        f"raw fuse AVGC **{TIP['AVGC']['H']:.4f}/{TIP['AVGC']['Acc']:.2f}**.\n"
    )
    lines.append("| Outer held-out | tau (s) | H | Acc % | L | SDT (s) | undetected / switches |")
    lines.append("|---|---:|---:|---:|---:|---:|---:|")
    for held in DS_KEYS:
        fit = prim["fit"][held]
        score_row = prim["lodo"][held]
        lines.append(
            f"| {held} | {fit['tau_s']:.2f} | {score_row['H']:.4f} | "
            f"{score_row['Acc']:.2f} | {score_row['L']:.3f} | {score_row['SDT']:.2f} | "
            f"{score_row['nUndetected']} / {score_row['nSwitches']} |"
        )
    lines.append(
        f"| **mean** | — | **{mean['H']:.4f}** | **{mean['Acc']:.2f}** | — | — | — |\n"
    )

    lines.append("### Legacy temperature controls (not selected)\n")
    lines.append("| Variant | mean H / Acc | AVGC H / Acc | selectable in this run |")
    lines.append("|---|---:|---:|---|")
    for name, _fam, selectable in VARIANT_SPECS:
        v = results["variants"][name]
        lines.append(
            f"| `{name}` | {v['lodo_mean']['H']:.4f} / {v['lodo_mean']['Acc']:.2f} | "
            f"{v['lodo']['AVGC']['H']:.4f} / {v['lodo']['AVGC']['Acc']:.2f} | no |"
        )
    lines.append(
        f"\nSelected AVGC vs tip **{tip_a['H']:.4f}/{tip_a['Acc']:.2f}**: "
        f"**ΔH {dH:+.4f} / ΔAcc {dA:+.2f}**.\n"
    )

    if aasd:
        lines.append("\n## External real-switch evaluation\n")
        ext_fit = results.get("external_selection", {})
        lines.append(
            f"Tau **{ext_fit.get('tau_s', float('nan')):.2f} s**, selected on "
            "the five corpus datasets only; external labels were not used for selection.\n"
        )
        lines.append("| Variant | AASD H / Acc | L / SDT (s) | Miss rate | Carta H / Acc | L / SDT (s) | Miss rate |")
        lines.append("|---|---:|---:|---:|---:|---:|---:|")
        for name in ("raw", sel):
            v = results["variants"][name]
            a = v["external"]["AASD"]
            c = v["external"]["Carta2026"]
            mark = "**" if name == sel else ""
            lines.append(
                f"| {mark}`{name}`{mark} | {a['H']:.4f} / {a['Acc']:.2f} | "
                f"{a['L']:.3f} / {a['SDT']:.2f} | {a['miss_rate']:.3f} | "
                f"{c['H']:.4f} / {c['Acc']:.2f} | {c['L']:.3f} / {c['SDT']:.2f} | "
                f"{c['miss_rate']:.3f} |"
            )

    lines.append("\n## Targets\n")
    lines.append("| Target | Result |")
    lines.append("|--------|--------|")
    ge = avgc["H"] >= 0.85 and avgc["Acc"] >= 78
    lines.append(
        f"| AVGC ≥0.85 / Acc≥78 | **{'HIT' if ge else 'miss'}** "
        f"({avgc['H']:.4f} / {avgc['Acc']:.2f}) |"
    )
    tip_moved = abs(dH) >= 0.0005 or abs(dA) >= 0.05
    lines.append(
        f"| Tip moved (\\|ΔH\\|≥5e-4 or \\|ΔAcc\\|≥0.05) | "
        f"{'yes' if tip_moved and (dH > 0 or dA > 0) else 'no / flat-hurt'} |"
    )

    lines.append("\n## Verdict\n")
    lines.append(
        f"Selected **`{sel}`**: LODO mean **{mean['H']:.4f}/{mean['Acc']:.2f}**, "
        f"AVGC **{avgc['H']:.4f}/{avgc['Acc']:.2f}** "
        f"(Δtip {dH:+.4f} H / {dA:+.2f} Acc). "
        f"{'Clears' if ge else 'Does not clear'} AVGC 0.85/78. "
        f"{'Tip moved.' if tip_moved and dH > 0 else 'Tip not improved.'}\n"
    )
    lines.append("## Interpretation\n")
    lines.append(
        "The posterior EMA changes decision persistence only; it does not create "
        "new EEG evidence. A smoother probability trace is useful only if it also "
        "improves held-out switch delay/miss rate without unacceptable Acc loss. "
        "The five LODO corpus evaluations use synthetic switch schedules, not "
        "genuine neural switch events.\n"
    )

    os.makedirs(os.path.dirname(RESULTS_MD), exist_ok=True)
    with open(RESULTS_MD, "w") as fh:
        fh.write("\n".join(lines) + "\n")
    log_line(f"wrote {RESULTS_MD}")


def main():
    os.makedirs(CKPT, exist_ok=True)
    os.makedirs(os.path.dirname(LOG), exist_ok=True)
    open(LOG, "w").close()
    t_all = time.time()

    log_line(
        f"causal-ds-temp CPU p_stay {P_STAY} scale {SCALE} "
        f"posterior_tau_grid={list(POSTERIOR_TAU_GRID)}"
    )
    log_line(
        "honesty: posterior tau selected by nested LODO only; external tau "
        "selected on corpus folds; AASD/Carta labels used only for final scoring"
    )
    log_line(
        "legacy temperature variants are controls, not selected/re-gridded"
    )

    N.load_all()
    att = N.G["att"]

    log_line("=== load tip fuse caches ===")
    fuse_by_held = load_fuse_caches()
    san = M.score_lodo(fuse_by_held["AVGC"], "AVGC")
    log_line(
        f"sanity raw focal↔synth fuse AVGC H {san['H']:.4f} Acc {san['Acc']:.2f} "
        f"(expect ~0.8226/75.54)"
    )

    results = dict(
        method=("causal HMM-posterior smoothing on frozen focal↔synth fuse w=0.5; "
            "legacy temperature variants retained as controls"),
        p_stay=P_STAY, scale=SCALE, device="cpu",
        honesty=("Posterior tau selected from inner LODO datasets only for each outer fold; "
             "external tau selected with five-fold corpus validation. "
             "AASD/Carta labels used only for final scoring."),
        distinct_from=[
            "focal-synth-adapt-readout", "multi-tau-hp", "global train_temp",
        ],
        baselines=dict(
            focal_synth_tip=TIP, linear_hp30_ext=LINEAR_HP30_EXT,
            sw2_swmix_tip=SW2_SWMIX_TIP,
        ),
        T_grid=list(map(float, T_GRID)),
        posterior_tau_grid=list(POSTERIOR_TAU_GRID),
        variants={},
    )

    log_line("=== LODO ===")
    lodo_means = {}
    for name, family, selectable in VARIANT_SPECS:
        t0 = time.time()
        per = {}
        fit_infos = {}
        for held in DS_KEYS:
            streams = fuse_by_held[held]
            adapted, info = apply_variant(streams, att, held, family)
            # JSON-safe fit summary
            safe = {}
            for k, v in info.items():
                if k == "nested":
                    safe[k] = v
                elif k == "fit":
                    safe[k] = v
                elif k == "T_per":
                    safe[k] = v
                elif isinstance(v, (int, float, str, bool, type(None))):
                    safe[k] = v
            fit_infos[held] = safe
            off = M.score_lodo(adapted, held)
            per[held] = dict(
                H=off["H"], Acc=off["Acc"], SDT=off.get("SDT"),
                L=off.get("L"), acc1=off.get("acc1"),
            )
            log_line(
                f"LODO {name:22s} {held:5s}  T={info.get('T_held', 1.0):.4f}  "
                f"H {off['H']:.4f} Acc {off['Acc']:.2f}"
            )
        mean_h = float(np.mean([per[d]["H"] for d in DS_KEYS]))
        mean_a = float(np.mean([per[d]["Acc"] for d in DS_KEYS]))
        log_line(
            f"LODO_MEAN {name:22s}  H {mean_h:.4f} Acc {mean_a:.2f}  "
            f"({time.time() - t0:.0f}s)"
        )
        lodo_means[name] = dict(H=mean_h, Acc=mean_a)
        results["variants"][name] = dict(
            family=family, selectable=selectable,
            fit=fit_infos, lodo=per, lodo_mean=dict(H=mean_h, Acc=mean_a),
        )

    log_line("=== causal posterior EMA (nested tau selection per outer fold) ===")
    posterior_per, posterior_fit = {}, {}
    for held in DS_KEYS:
        train_ds = [ds for ds in DS_KEYS if ds != held]
        streams = fuse_by_held[held]
        tau_s, inner = select_posterior_tau(
            streams, train_ds, POSTERIOR_TAU_GRID)
        off = score_lodo_posterior(streams, held, train_ds, tau_s)
        posterior_per[held] = off
        posterior_fit[held] = dict(tau_s=tau_s, inner=inner)
        log_line(
            f"POSTERIOR {held:5s} tau={tau_s:.2f}s H {off['H']:.4f} "
            f"Acc {off['Acc']:.2f} L {off['L']:.3f} SDT {off['SDT']:.2f}"
        )

    posterior_mean = dict(
        H=float(np.mean([posterior_per[ds]["H"] for ds in DS_KEYS])),
        Acc=float(np.mean([posterior_per[ds]["Acc"] for ds in DS_KEYS])),
    )
    selected = "hmm_posterior_ema"
    lodo_means[selected] = posterior_mean
    results["variants"][selected] = dict(
        family="causal_hmm_posterior_ema", selectable=True,
        fit=posterior_fit, lodo=posterior_per, lodo_mean=posterior_mean,
    )
    log_line(
        f"POSTERIOR_LODO_MEAN H {posterior_mean['H']:.4f} "
        f"Acc {posterior_mean['Acc']:.2f}"
    )
    results["selection"] = dict(
        criterion=("per-outer-fold tau chosen by inner LODO Acc then H; "
                   "no AASD/Carta labels; no cross-outer model selection"),
        ranked=[selected], selected=selected,
        excluded_from_select=[n for n, _f, _sel in VARIANT_SPECS],
    )
    results["selected"] = selected

    # External labels are reserved for the final real-switch evaluation.
    log_line("=== external real-switch evaluation ===")
    try:
        import stem_ms96_irm_synth_fuse as SF
        import ensemble_clf_transformer_v4 as V4

        corp_path = os.path.join(SRC_CACHE, "focal_synth_w05__all5_corpus.pkl")
        ext_path = os.path.join(SRC_CACHE, "focal_synth_w05__all5_external.pkl")
        if not (os.path.isfile(corp_path) and os.path.isfile(ext_path)):
            log_line("WARN: missing all5 corpus/external caches; skip external")
        else:
            corp = load_pkl(corp_path)
            ext_fuse = load_pkl(ext_path)
            _ext_logits, ext_meta = SF.load_external()
            train_ds_ext = list(V4.CORPUS)

            for name, family, _sel in VARIANT_SPECS:
                if family == "raw":
                    info = dict(T_held=1.0)
                    adapted_test = ext_fuse
                    mu_e, sd_e = SF.emissions_from(
                        corp, att, datasets=train_ds_ext)
                elif family == "global_T_nll":
                    x, y = collect_xy(corp, att, train_ds_ext)
                    fit = fit_temperature(x, y)
                    T_h = float(fit["T"])
                    info = dict(T_held=T_h)
                    adapted_test = scale_all(ext_fuse, T_h)
                    mu_e, sd_e = SF.emissions_from(
                        scale_all(corp, T_h), att, datasets=train_ds_ext)
                elif family == "per_ds_emis_T_mean":
                    per = fit_T_per_dataset(corp, att, train_ds_ext)
                    T_map = {d: float(per[d]["T"]) for d in train_ds_ext}
                    T_h = float(np.mean(list(T_map.values())))
                    info = dict(T_held=T_h, T_per=T_map)
                    mu_e, sd_e = SF.emissions_from(
                        scale_per_ds(corp, T_map), att, datasets=train_ds_ext)
                    adapted_test = scale_all(ext_fuse, T_h)
                else:
                    per = fit_T_per_dataset(corp, att, train_ds_ext)
                    T_map = {d: float(per[d]["T"]) for d in train_ds_ext}
                    T_vals = np.array(list(T_map.values()), dtype=np.float64)
                    if family == "held_T_nll_mean":
                        T_h = float(np.mean(T_vals))
                        info = dict(T_held=T_h, T_per=T_map)
                    elif family == "held_T_nll_med":
                        T_h = float(np.median(T_vals))
                        info = dict(T_held=T_h, T_per=T_map)
                    elif family == "held_T_nll_pick":
                        cands = sorted(set(T_map.values()) | {1.0})
                        T_h, rows = nested_pick_T(
                            corp, train_ds_ext, cands, use_fast=True)
                        info = dict(T_held=T_h, T_per=T_map, nested=rows)
                    elif family == "held_T_causal_grid":
                        T_h, rows = nested_pick_T(
                            corp, train_ds_ext, list(T_GRID), use_fast=True)
                        info = dict(T_held=T_h, nested=rows)
                    else:
                        raise ValueError(family)
                    adapted_test = scale_all(ext_fuse, T_h)
                    mu_e, sd_e = SF.emissions_from(
                        corp, att, datasets=train_ds_ext)

                T_h = float(info["T_held"])
                ext = {}
                for ds in TEST_NAMES:
                    r = SF.score_external(
                        adapted_test[ds], ext_meta[ds], mu_e, sd_e)
                    ext[ds] = dict(
                        H=r["H"], Acc=r["Acc"], L=r["L"], SDT=r["SDT"],
                        nSwitches=r["nSwitches"], nUndetected=r["nUndetected"],
                        miss_rate=r["nUndetected"] / max(r["nSwitches"], 1),
                    )
                    log_line(
                        f"EXT {name:22s} {ds:10s}  T={T_h:.4f}  "
                        f"H {r['H']:.4f} Acc {r['Acc']:.2f}"
                    )
                results["variants"][name]["external"] = ext
                results["variants"][name]["emis"] = dict(mu=mu_e, sd=sd_e)
                results["variants"][name]["ext_fit"] = {
                    k: v for k, v in info.items()
                    if isinstance(v, (int, float, str, bool, type(None)))
                    or k in ("T_per",)
                }

            posterior_tau, posterior_inner = select_posterior_tau(
                corp, train_ds_ext, POSTERIOR_TAU_GRID)
            mu_e, sd_e = SF.emissions_from(corp, att, datasets=train_ds_ext)
            external_posterior = {
                ds: score_external_posterior(
                    ext_fuse[ds], ext_meta[ds], mu_e, sd_e, posterior_tau)
                for ds in TEST_NAMES
            }
            results["variants"][selected]["external"] = external_posterior
            results["variants"][selected]["external_fit"] = dict(
                tau_s=posterior_tau, inner=posterior_inner,
                emis_mu=mu_e, emis_sd=sd_e * SCALE,
                selection_data="five corpus datasets only",
            )
            results["external_selection"] = dict(
                tau_s=posterior_tau, inner=posterior_inner,
                selection_data="five corpus datasets only",
            )
            for ds, score_row in external_posterior.items():
                log_line(
                    f"EXT POSTERIOR {ds:10s} tau={posterior_tau:.2f}s "
                    f"H {score_row['H']:.4f} Acc {score_row['Acc']:.2f} "
                    f"L {score_row['L']:.3f} SDT {score_row['SDT']:.2f}"
                )
    except Exception as exc:
        log_line(f"WARN: external scoring failed: {exc!r}")

    prim = results["variants"][selected]
    avgc = prim["lodo"]["AVGC"]
    d_tip = dict(
        H=avgc["H"] - TIP["AVGC"]["H"], Acc=avgc["Acc"] - TIP["AVGC"]["Acc"],
    )
    headline = dict(
        selected=selected,
        lodo_mean=prim["lodo_mean"],
        lodo_AVGC=avgc,
        AVGC_delta_vs_tip=d_tip,
        targets=dict(
            AVGC_ge_085_78=bool(avgc["H"] >= 0.85 and avgc["Acc"] >= 78),
        ),
        tip_moved=bool(abs(d_tip["H"]) >= 5e-4 or abs(d_tip["Acc"]) >= 0.05),
        tip_improved=bool(d_tip["H"] > 5e-4 or d_tip["Acc"] > 0.05),
    )
    if "external" in prim:
        headline["AASD"] = prim["external"]["AASD"]
        headline["Carta2026"] = prim["external"]["Carta2026"]
    results["headline"] = headline
    log_line(
        f"HEADLINE {selected} AVGC H {avgc['H']:.4f} Acc {avgc['Acc']:.2f} "
        f"(Δtip {d_tip['H']:+.4f}/{d_tip['Acc']:+.2f})  "
        f"LODO mean {prim['lodo_mean']['H']:.4f}/{prim['lodo_mean']['Acc']:.2f}"
    )

    os.makedirs(os.path.dirname(OUT), exist_ok=True)
    with open(OUT, "w") as fh:
        json.dump(results, fh, indent=1, default=float)
    with open(os.path.join(CKPT, "summary.json"), "w") as fh:
        json.dump(
            dict(selected=selected, lodo_means=lodo_means, headline=headline),
            fh, indent=1, default=float,
        )
    write_results_md(results)
    log_line(f"wrote {OUT} ({time.time() - t_all:.0f}s)")


if __name__ == "__main__":
    main()
