"""evidential-tip-stack — train-only linear/logistic/evidential stack on frozen tip stems.

Streams (CPU caches only):
  focal  — models/focal_synth_adapt_readout/cache/_tmp_focal__*
  synth  — models/fuse_conf_resid/cache/_tmp_synth__*
  IRM    — models/fuse_conf_resid/cache/_tmp_irm__*

Variants (fit on LODO-train only; leave-AVGC never sees AVGC labels):
  focal_synth_w05   : tip fuse baseline (not selectable)
  equal_mean3       : equal-weight mean of 3 logits (not selectable)
  nnls_simplex      : non-neg simplex weights on logits (NLL)
  logistic_stack    : L2 logistic on 3 logits (+intercept)
  dirichlet_evid    : Dirichlet/evidential softplus evidence + per-stem strengths

≠ closed multi-stem soft-attn (learned EWMA→MLP softmax gates).
≠ causal-ds-temp T / isotonic-ds-fuse / agree-gate / conf-resid / Mixup/SSL/DANN.

Select by mean LODO **H then Acc** @ HMM 0.994/0.8.
Tip to beat: held_T_nll_med 0.8245/75.64 (LODO 0.8844/83.38).

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

import json
import os
import pickle
import time

import numpy as np
from scipy.optimize import minimize
from scipy.special import expit, logit
from sklearn.linear_model import LogisticRegression

import causal_moe_fuse as M
import ensemble_clf_nested as N
import stem_ms96_irm_synth_fuse as SF
from common import DS_KEYS

EXP = os.path.dirname(os.path.abspath(__file__))
FOCAL_CACHE = os.path.join(EXP, "models", "focal_synth_adapt_readout", "cache")
IRM_SYNTH_CACHE = os.path.join(EXP, "models", "fuse_conf_resid", "cache")
CKPT = os.path.join(EXP, "models", "evidential_tip_stack")
LOG = os.path.join(EXP, "experiments", "evidential_tip_stack.log")
OUT = os.path.join(EXP, "results", "research", "evidential_tip_stack.json")
RESULTS_MD = os.path.join(EXP, "multi_agent_opt", "results_evidential_tip_stack.md")

P_STAY = 0.994
SCALE = 0.8
TEST_NAMES = ("AASD", "Carta2026")
STREAMS = ("focal", "synth", "irm")
K = len(STREAMS)
MAX_FIT = 400_000
SEED = 0
PROB_CLIP = (1e-4, 1.0 - 1e-4)
EPS = 1e-8

TIP = dict(
    mean=dict(H=0.8844, Acc=83.38),
    AVGC=dict(H=0.8245, Acc=75.64),
    raw_fuse=dict(H=0.8226, Acc=75.54),
)
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, selectable)
VARIANT_SPECS = [
    ("focal_synth_w05", False),
    ("equal_mean3", False),
    ("nnls_simplex", True),
    ("logistic_stack", True),
    ("dirichlet_evid", 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 require(path):
    if not os.path.isfile(path):
        raise FileNotFoundError(path)
    log_line(f"cache hit {path}")
    return load_pkl(path)


def load_triple(tag: str):
    focal = require(os.path.join(FOCAL_CACHE, f"_tmp_focal__{tag}.pkl"))
    synth = require(os.path.join(IRM_SYNTH_CACHE, f"_tmp_synth__{tag}.pkl"))
    irm = require(os.path.join(IRM_SYNTH_CACHE, f"_tmp_irm__{tag}.pkl"))
    return dict(focal=focal, synth=synth, irm=irm)


def align_keys(*tables):
    keys = set(tables[0])
    for t in tables[1:]:
        keys &= set(t)
    return sorted(keys)


def collect_train(triple, att, train_ds, max_fit=MAX_FIT, rng=None):
    """Return Z (N,K) logits and Y (N,) labels {0,1} where 1 = class0 (att==0)."""
    rng = rng or np.random.default_rng(SEED)
    Zs, Ys = [], []
    for ds in train_ds:
        labels = att[ds]
        tabs = [triple[name][ds] for name in STREAMS]
        for key in align_keys(*tabs):
            if key not in labels:
                continue
            zs = [np.asarray(t[key], np.float64).reshape(-1) for t in tabs]
            n = min(len(z) for z in zs)
            y = (np.asarray(labels[key]) == 0).astype(np.float64).reshape(-1)[:n]
            if n <= 0:
                continue
            Zs.append(np.stack([z[:n] for z in zs], 1))
            Ys.append(y)
    if not Zs:
        raise RuntimeError("no train frames")
    Z = np.concatenate(Zs)
    Y = np.concatenate(Ys)
    if len(Z) > max_fit:
        idx = rng.choice(len(Z), size=max_fit, replace=False)
        Z, Y = Z[idx], Y[idx]
    return Z, Y


def softplus(x):
    x = np.asarray(x, np.float64)
    return np.where(x > 20, x, np.log1p(np.exp(np.clip(x, -40, 40))))


def fit_nnls_simplex(Z, Y):
    """Non-neg simplex weights minimizing frame BCE on logits."""
    y = np.asarray(Y, np.float64)

    def nll(theta):
        # theta unconstrained → simplex via softplus+normalize
        w = softplus(theta) + 1e-6
        w = w / w.sum()
        z = Z @ w
        p = np.clip(expit(z), *PROB_CLIP)
        return float(-(y * np.log(p) + (1.0 - y) * np.log(1.0 - p)).mean())

    # init near equal / tip-ish (focal+synth heavy)
    theta0 = np.array([0.5, 0.5, -1.0], np.float64)
    res = minimize(nll, theta0, method="L-BFGS-B")
    w = softplus(res.x) + 1e-6
    w = w / w.sum()
    return dict(
        w=w.astype(np.float64),
        nll=float(res.fun),
        success=bool(res.success),
        n=int(len(Y)),
    )


def fit_logistic(Z, Y):
    y = np.asarray(Y, np.int64)
    # If degenerate, fall back to equal mean
    if y.min() == y.max():
        return dict(
            coef=np.ones(K) / K, intercept=0.0, C=1.0,
            degenerate=True, n=int(len(Y)),
        )
    clf = LogisticRegression(
        penalty="l2", C=1.0, fit_intercept=True, solver="lbfgs",
        max_iter=500, random_state=SEED,
    )
    clf.fit(Z, y)
    coef = clf.coef_.reshape(-1).astype(np.float64)
    intercept = float(clf.intercept_[0])
    z = Z @ coef + intercept
    p = np.clip(expit(z), *PROB_CLIP)
    nll = float(-(y * np.log(p) + (1.0 - y) * np.log(1.0 - p)).mean())
    return dict(
        coef=coef, intercept=intercept, C=1.0, nll=nll,
        degenerate=False, n=int(len(Y)),
    )


def fit_dirichlet_evid(Z, Y):
    """Per-stem strengths s_k>0; α± = 1 + Σ s_k softplus(±z_k); p=α+/(α++α-)."""
    y = np.asarray(Y, np.float64)
    e_pos = softplus(Z)
    e_neg = softplus(-Z)

    def nll(log_s):
        s = np.exp(np.clip(log_s, -8, 8))
        a_pos = 1.0 + e_pos @ s
        a_neg = 1.0 + e_neg @ s
        p = np.clip(a_pos / (a_pos + a_neg), *PROB_CLIP)
        return float(-(y * np.log(p) + (1.0 - y) * np.log(1.0 - p)).mean())

    log_s0 = np.zeros(K, np.float64)
    res = minimize(nll, log_s0, method="L-BFGS-B")
    s = np.exp(np.clip(res.x, -8, 8))
    return dict(
        s=s.astype(np.float64),
        nll=float(res.fun),
        success=bool(res.success),
        n=int(len(Y)),
    )


def apply_weighted(triple, w):
    out = {}
    ds_set = set(triple["focal"]) & set(triple["synth"]) & set(triple["irm"])
    w = np.asarray(w, np.float64).reshape(-1)
    for ds in ds_set:
        tabs = [triple[name][ds] for name in STREAMS]
        out[ds] = {}
        for key in align_keys(*tabs):
            zs = [np.asarray(t[key], np.float64).reshape(-1) for t in tabs]
            n = min(len(z) for z in zs)
            Z = np.stack([z[:n] for z in zs], 1)
            out[ds][key] = (Z @ w).astype(np.float64)
    return out


def apply_logistic(triple, coef, intercept):
    out = {}
    ds_set = set(triple["focal"]) & set(triple["synth"]) & set(triple["irm"])
    coef = np.asarray(coef, np.float64).reshape(-1)
    for ds in ds_set:
        tabs = [triple[name][ds] for name in STREAMS]
        out[ds] = {}
        for key in align_keys(*tabs):
            zs = [np.asarray(t[key], np.float64).reshape(-1) for t in tabs]
            n = min(len(z) for z in zs)
            Z = np.stack([z[:n] for z in zs], 1)
            out[ds][key] = (Z @ coef + intercept).astype(np.float64)
    return out


def apply_dirichlet(triple, s):
    out = {}
    ds_set = set(triple["focal"]) & set(triple["synth"]) & set(triple["irm"])
    s = np.asarray(s, np.float64).reshape(-1)
    lo, hi = PROB_CLIP
    for ds in ds_set:
        tabs = [triple[name][ds] for name in STREAMS]
        out[ds] = {}
        for key in align_keys(*tabs):
            zs = [np.asarray(t[key], np.float64).reshape(-1) for t in tabs]
            n = min(len(z) for z in zs)
            Z = np.stack([z[:n] for z in zs], 1)
            a_pos = 1.0 + softplus(Z) @ s
            a_neg = 1.0 + softplus(-Z) @ s
            p = np.clip(a_pos / (a_pos + a_neg), lo, hi)
            out[ds][key] = logit(p).astype(np.float64)
    return out


def equal_mean3(triple):
    return apply_weighted(triple, np.ones(K) / K)


def focal_synth_w05(triple):
    out = {}
    for ds in set(triple["focal"]) & set(triple["synth"]):
        out[ds] = {}
        for key in align_keys(triple["focal"][ds], triple["synth"][ds]):
            a = np.asarray(triple["focal"][ds][key], np.float64).reshape(-1)
            b = np.asarray(triple["synth"][ds][key], np.float64).reshape(-1)
            n = min(len(a), len(b))
            out[ds][key] = (0.5 * a[:n] + 0.5 * b[:n]).astype(np.float64)
    return out


def fit_and_apply(name, triple, att, train_ds):
    if name == "focal_synth_w05":
        return focal_synth_w05(triple), dict(mode=name)
    if name == "equal_mean3":
        return equal_mean3(triple), dict(mode=name, w=(np.ones(K) / K).tolist())

    Z, Y = collect_train(triple, att, train_ds)
    if name == "nnls_simplex":
        info = fit_nnls_simplex(Z, Y)
        st = apply_weighted(triple, info["w"])
        return st, dict(
            mode=name, w=info["w"].tolist(), nll=info["nll"],
            success=info["success"], n=info["n"],
        )
    if name == "logistic_stack":
        info = fit_logistic(Z, Y)
        st = apply_logistic(triple, info["coef"], info["intercept"])
        return st, dict(
            mode=name, coef=info["coef"].tolist(),
            intercept=info["intercept"], nll=info.get("nll"),
            degenerate=info["degenerate"], n=info["n"],
        )
    if name == "dirichlet_evid":
        info = fit_dirichlet_evid(Z, Y)
        st = apply_dirichlet(triple, info["s"])
        return st, dict(
            mode=name, s=info["s"].tolist(), nll=info["nll"],
            success=info["success"], n=info["n"],
        )
    raise ValueError(name)


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


def safe_fit_info(info):
    out = {}
    for k, v in info.items():
        if isinstance(v, (int, float, str, bool, type(None))):
            out[k] = v
        elif isinstance(v, list):
            out[k] = [float(x) for x in v]
        elif isinstance(v, np.ndarray):
            out[k] = v.astype(float).tolist()
    return out


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", {})
    tip_improved = dH >= 5e-4  # Acc-alone ≠ tip (isotonic lesson)
    ge = avgc["H"] >= 0.85 and avgc["Acc"] >= 78

    lines = []
    lines.append("# results_evidential_tip_stack\n")
    lines.append(
        "**Claim:** `evidential-tip-stack` — train-only **linear / logistic / "
        "Dirichlet-evidential stacking** over frozen tip stems "
        "(**focal + synth + IRM**). Leave-AVGC fits on non-AVGC train only.\n"
    )
    lines.append(
        "**Distinct from** closed `multi-stem-attn-pool` (learned EWMA→MLP soft-attn "
        "gates), `causal-ds-temp` / `isotonic-ds-fuse` (PP on tip fuse), "
        "agree-gate / conf-resid / fixed-w IRM↔stem.\n"
    )
    lines.append(
        f"**Honesty:** Train labels only. Never AASD/Carta for selection. "
        f"Select mean LODO **H then Acc** @ **{P_STAY}/{SCALE}**.\n"
    )
    lines.append(
        "**Compute:** CPU; reuse focal + fuse_conf_resid IRM/synth caches. "
        "Code: `evidential_tip_stack.py`. "
        "JSON: `results/research/evidential_tip_stack.json`.\n"
    )
    lines.append("## Selection\n")
    lines.append("| Rule | Pick |")
    lines.append("|------|------|")
    lines.append(f"| mean LODO H then Acc (stack variants) | **`{sel}`** |")
    lines.append(
        f"| Tip to beat (held_T_nll_med) | "
        f"**{tip_a['H']:.4f} / {tip_a['Acc']:.2f}** |"
    )
    ranked = results["selection"]["ranked"]
    lines.append(f"\nRanked H: {' → '.join(ranked)}\n")

    lines.append("## LODO (preset)\n")
    lines.append(
        "| Variant | mean H / Acc | AVGC H / Acc | fit@AVGC | selectable |"
    )
    lines.append(
        "|---------|--------------|--------------|----------|------------|"
    )
    for name, selectable in VARIANT_SPECS:
        v = results["variants"][name]
        mark = "**" if name == sel else ""
        fit = v.get("fit", {}).get("AVGC", {})
        if "w" in fit:
            fit_s = "w=" + ",".join(f"{x:.3f}" for x in fit["w"])
        elif "coef" in fit:
            fit_s = "coef=" + ",".join(f"{x:.3f}" for x in fit["coef"])
            fit_s += f" b={fit.get('intercept', 0):+.3f}"
        elif "s" in fit:
            fit_s = "s=" + ",".join(f"{x:.3f}" for x in fit["s"])
        else:
            fit_s = fit.get("mode", "—")
        lines.append(
            f"| {mark}{name}{mark} | {mark}{v['lodo_mean']['H']:.4f} / "
            f"{v['lodo_mean']['Acc']:.2f}{mark} | "
            f"{mark}{v['lodo']['AVGC']['H']:.4f} / {v['lodo']['AVGC']['Acc']:.2f}{mark} | "
            f"{fit_s} | "
            f"{'**SELECTED**' if name == sel else ('yes' if selectable else 'no')} |"
        )
    lines.append(
        f"\nSelected AVGC vs tip **{tip_a['H']:.4f}/{tip_a['Acc']:.2f}**: "
        f"**ΔH {dH:+.4f} / ΔAcc {dA:+.2f}**.\n"
    )

    lines.append("## Selected per-held\n")
    lines.append("| Held | fit summary | H / Acc |")
    lines.append("|------|-------------|---------|")
    for held in DS_KEYS:
        info = prim["fit"].get(held, {})
        sc = prim["lodo"][held]
        if "w" in info:
            fs = "w=" + ",".join(f"{x:.3f}" for x in info["w"])
        elif "coef" in info:
            fs = "coef=" + ",".join(f"{x:.3f}" for x in info["coef"])
        elif "s" in info:
            fs = "s=" + ",".join(f"{x:.3f}" for x in info["s"])
        else:
            fs = info.get("mode", "—")
        lines.append(f"| {held} | {fs} | {sc['H']:.4f} / {sc['Acc']:.2f} |")

    if aasd:
        lines.append("\n## External (bookkeeping; Acc tip not claimed)\n")
        lines.append("| Variant | AASD H / Acc | Carta H / Acc |")
        lines.append("|---------|--------------|---------------|")
        for name in ("focal_synth_w05", sel):
            if name not in results["variants"] or "external" not in results["variants"][name]:
                continue
            v = results["variants"][name]
            a = v["external"]["AASD"]
            c = v["external"]["Carta2026"]
            mark = "**" if name == sel else ""
            lines.append(
                f"| {mark}{name}{mark} | {mark}{a['H']:.4f} / {a['Acc']:.2f}{mark} | "
                f"{c['H']:.4f} / {c['Acc']:.2f} |"
            )

    lines.append("\n## Targets\n")
    lines.append("| Target | Result |")
    lines.append("|--------|--------|")
    lines.append(
        f"| AVGC ≥0.85 / Acc≥78 | **{'HIT' if ge else 'miss'}** "
        f"({avgc['H']:.4f} / {avgc['Acc']:.2f}) |"
    )
    lines.append(
        f"| Tip improved (ΔH≥5e-4 vs 0.8245; Acc alone ≠ tip) | "
        f"{'yes' if tip_improved 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_improved else 'Tip not improved.'}\n"
    )
    lines.append("## Do not repeat\n")
    lines.append(
        "Do not re-grid train-only NNLS/simplex / logistic / Dirichlet-evidential "
        "stacks on frozen focal+synth+IRM tip stem logits at HMM 0.994/0.8. "
        "Distinct from (still closed) multi-stem soft-attn / T / isotonic on tip fuse.\n"
    )
    lines.append("## NEXT IDEA\n")
    if ge:
        lines.append(
            "AVGC bar met — freeze and push AASD Acc on Acc front-end only.\n"
        )
    elif tip_improved:
        lines.append(
            "Stacking helped slightly; prefer qualitatively new **train-time AVGC** "
            "evidence (new stem objective / architecture / domain alignment that "
            "changes logits) — not more decision-layer PP on tip fuse "
            "(no T/iso/stack/attn/agree).\n"
        )
    else:
        lines.append(
            "Decision-layer stacking on frozen tip stems saturated/hurt vs tip. "
            "**CLOSED** stacking family. Prefer qualitatively new **train-time AVGC** "
            "(new stem loss/arch/domain signal that changes logits) — not more PP on "
            "tip fuse: **no T / iso / stack / attn / agree**.\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"evidential-tip-stack CPU p_stay {P_STAY} scale {SCALE} "
        f"tip_AVGC {TIP['AVGC']['H']:.4f}/{TIP['AVGC']['Acc']:.2f}"
    )
    log_line(
        "honesty: train-only stack fit; leave-held never uses held labels; "
        "never AASD/Carta for selection; select H then Acc"
    )
    log_line(
        "≠ multi-stem soft-attn / causal-ds-temp / isotonic-ds-fuse / "
        "agree-gate / conf-resid"
    )

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

    log_line("=== load tip stem caches ===")
    held_trips = {h: load_triple(f"held-{h}") for h in DS_KEYS}
    tip_st = focal_synth_w05(held_trips["AVGC"])
    san = M.score_lodo(tip_st, "AVGC")
    log_line(
        f"sanity focal↔synth w0.5 AVGC H {san['H']:.4f} Acc {san['Acc']:.2f} "
        f"(expect ~{TIP['raw_fuse']['H']:.4f}/{TIP['raw_fuse']['Acc']:.2f})"
    )

    results = dict(
        method=("train-only NNLS/simplex + logistic + Dirichlet-evidential "
                "stack on frozen focal+synth+IRM tip stems"),
        p_stay=P_STAY, scale=SCALE, device="cpu", streams=list(STREAMS),
        honesty=("Train-only stack fit; leave-held without held labels. "
                 "Select mean LODO H then Acc. No AASD/Carta labels."),
        distinct_from=[
            "multi-stem-attn-pool", "causal-ds-temp", "isotonic-ds-fuse",
            "irm-synth-agree-gate", "fuse-conf-resid",
        ],
        baselines=dict(
            tip_held_T_nll_med=TIP, linear_hp30_ext=LINEAR_HP30_EXT,
            sw2_swmix_tip=SW2_SWMIX_TIP,
        ),
        variants={},
    )

    log_line("=== LODO ===")
    lodo_means = {}
    for name, selectable in VARIANT_SPECS:
        t0 = time.time()
        per = {}
        fit_infos = {}
        for held in DS_KEYS:
            train_ds = [d for d in DS_KEYS if d != held]
            trip = held_trips[held]
            adapted, info = fit_and_apply(name, trip, att, train_ds)
            fit_infos[held] = safe_fit_info(info)
            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"),
            )
            extra = ""
            if "w" in info:
                extra = " w=[" + ",".join(f"{x:.3f}" for x in info["w"]) + "]"
            elif "coef" in info:
                extra = " coef=[" + ",".join(f"{x:.3f}" for x in info["coef"]) + "]"
            elif "s" in info:
                extra = " s=[" + ",".join(f"{x:.3f}" for x in info["s"]) + "]"
            log_line(
                f"LODO {name:16s} {held:5s}{extra}  "
                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:16s}  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(
            selectable=selectable, fit=fit_infos, lodo=per,
            lodo_mean=dict(H=mean_h, Acc=mean_a),
        )

    adapt_names = [n for n, sel in VARIANT_SPECS if sel]
    selected = pick_variant(lodo_means, adapt_names)
    ranked = sorted(
        adapt_names,
        key=lambda n: (lodo_means[n]["H"], lodo_means[n]["Acc"]),
        reverse=True,
    )
    log_line(
        f"SELECTED (LODO H then Acc): {selected}  "
        f"H {lodo_means[selected]['H']:.4f} Acc {lodo_means[selected]['Acc']:.2f}"
    )
    log_line(f"ranked: {ranked}")
    results["selection"] = dict(
        criterion="mean LODO H then Acc (stack variants; never AASD/Carta)",
        ranked=ranked, selected=selected,
        excluded_from_select=[n for n, sel in VARIANT_SPECS if not sel],
    )
    results["selected"] = selected

    log_line("=== external (bookkeeping) ===")
    try:
        corp = load_triple("all5_corpus")
        ext_trip = load_triple("all5_external")
        _ext_logits, ext_meta = SF.load_external()
        train_ds_ext = list(DS_KEYS)

        for name, _sel in VARIANT_SPECS:
            # Fit on corpus train only; apply frozen params to external.
            if name == "nnls_simplex":
                Z, Y = collect_train(corp, att, train_ds_ext)
                info = fit_nnls_simplex(Z, Y)
                adapted_corp = apply_weighted(corp, info["w"])
                adapted_test = apply_weighted(ext_trip, info["w"])
                info = dict(mode=name, w=info["w"].tolist(), nll=info["nll"])
            elif name == "logistic_stack":
                Z, Y = collect_train(corp, att, train_ds_ext)
                info = fit_logistic(Z, Y)
                adapted_corp = apply_logistic(corp, info["coef"], info["intercept"])
                adapted_test = apply_logistic(
                    ext_trip, info["coef"], info["intercept"])
                info = dict(
                    mode=name, coef=info["coef"].tolist(),
                    intercept=info["intercept"],
                )
            elif name == "dirichlet_evid":
                Z, Y = collect_train(corp, att, train_ds_ext)
                info = fit_dirichlet_evid(Z, Y)
                adapted_corp = apply_dirichlet(corp, info["s"])
                adapted_test = apply_dirichlet(ext_trip, info["s"])
                info = dict(mode=name, s=info["s"].tolist(), nll=info["nll"])
            elif name == "focal_synth_w05":
                adapted_corp = focal_synth_w05(corp)
                adapted_test = focal_synth_w05(ext_trip)
                info = dict(mode=name)
            elif name == "equal_mean3":
                adapted_corp = equal_mean3(corp)
                adapted_test = equal_mean3(ext_trip)
                info = dict(mode=name)
            else:
                raise ValueError(name)

            mu_e, sd_e = SF.emissions_from(
                adapted_corp, att, datasets=train_ds_ext)
            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"])
                log_line(
                    f"EXT {name:16s} {ds:10s}  "
                    f"H {r['H']:.4f} Acc {r['Acc']:.2f}"
                )
            results["variants"][name]["external"] = ext
            results["variants"][name]["ext_fit"] = safe_fit_info(info)
    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"],
    )
    tip_improved = bool(d_tip["H"] >= 5e-4)
    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_improved=tip_improved,
        tip_moved=bool(abs(d_tip["H"]) >= 5e-4 or abs(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}  "
        f"tip_improved={tip_improved}"
    )

    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()
