"""Causal |logit| hard/soft gate: Acc tip hard_abs_swmix|t1 ↔ clf-switch-dur fuse.

Streams (frozen; CPU caches only):
  tip  = Acc leader hard_abs_swmix|t1 (AASD Acc 53.53) from sw2_swmix_blend
  sdur = SW2-style fuse_general a=0.5: published 4 decoders × clf-switch-dur
         4-seed clfs (AASD Acc 53.20)

Primary rule: when |sdur| ≥ thr use sdur else tip (hard_abs_sdur|t*).
Also thr soft-sigmoid / hard_abs_tip / soft-w tip↔sdur refs.

Select by mean LODO Acc then H @ HMM 0.994/0.8 (never AASD/Carta).
Score AASD + Carta vs tip 53.53.

DISTINCT from SW2↔swmix soft-w re-grid, tip Platt/domain-emis, fuse_general a-grid.

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

import json
import os
import pickle
import time

import numpy as np
from scipy.signal import lfilter
from scipy.special import expit

import ensemble_clf as EC
import ensemble_clf_nested as N
import ensemble_clf_transformer_v4 as V4
import eval_external as E
import fuse_external as FE
import fuse_general as FG
import online_moment_sw2 as OMS
import score as S
import sw2_hp30_blend as SW2
import sw2_swmix_blend as SM
from common import DS_KEYS
from fuse_evidence import LA, OFF

EXP = os.path.dirname(os.path.abspath(__file__))
CACHE_DIR = os.path.join(EXP, "models", "tip_sdur_abs_gate", "cache")
LOG = os.path.join(EXP, "experiments", "tip_sdur_abs_gate.log")
OUT = os.path.join(EXP, "results", "research", "tip_sdur_abs_gate.json")
RESULTS_MD = os.path.join(EXP, "multi_agent_opt", "results_tip_sdur_abs_gate.md")

P_STAY = 0.994
SCALE = 0.8
LOOKAHEAD = float(LA)
A_FUSE = 0.5
TIP_KIND, TIP_THR, TIP_SHARP = OMS.TIP_KIND, OMS.TIP_THR, OMS.TIP_SHARP

PUB_DECS = [
    "vlaai_ws__eaP_n5_t60_aug_cluster_onset_dom_xFUG_ptSPK",
    "vlaai_ws__eaP_n5_t60_aug_cluster_onset_dom_xFUG_ptSPK_s1",
    "vlaai_ws__eaP_n5_t60_aug_cluster_onset_dom_xFUG_ptSPK_s2",
    "vlaai_ws__eaP_n5_t60_aug_cluster_onset_dom_xFUG_ptSPK_s3",
]
SDUR_CLFS = [
    "direct_clf__eaP_n5_t60_w10b3_ptSW2_sdur_aug-sw2",
    "direct_clf__eaP_n5_t60_w10b3_ptSW2_sdur_s1_aug-sw2",
    "direct_clf__eaP_n5_t60_w10b3_ptSW2_sdur_s2_aug-sw2",
    "direct_clf__eaP_n5_t60_w10b3_ptSW2_sdur_s3_aug-sw2",
]
EXT_TAG = "_w10b3_sdur_aug-sw2"
EXT_DECS = [[PUB_DECS[i], i] for i in range(4)]
EXT_CLFS = [[SDUR_CLFS[i], EXT_TAG, i] for i in range(4)]

LINEAR_HP30_EXT = dict(AASD=dict(H=0.6422, Acc=53.12), Carta2026=dict(H=0.6520, Acc=60.54))
TIP_EXT = dict(AASD=dict(H=0.6424, Acc=53.53), Carta2026=dict(H=0.6460, Acc=59.71))
SDUR_EXT = dict(AASD=dict(H=0.6437, Acc=53.20), Carta2026=dict(H=0.6443, Acc=59.62))
SW2_ALONE_EXT = dict(AASD=dict(H=0.6439, Acc=53.46), Carta2026=dict(H=0.6470, Acc=59.60))


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 save_pkl(path, obj):
    os.makedirs(os.path.dirname(path), exist_ok=True)
    with open(path, "wb") as fh:
        pickle.dump(obj, fh, protocol=pickle.HIGHEST_PROTOCOL)


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


def ewma_1d(x, tau_s=4.0, hop=0.25):
    x = np.asarray(x, np.float64)
    alpha = 1.0 - np.exp(-hop / tau_s)
    return lfilter([alpha], [1.0, -(1.0 - alpha)], x)


def gate_series(tip, sdur, kind, thresh, sharp, ewma_tau=4.0):
    """g∈[0,1]; high g → trust tip. Primary: hard_abs_sdur → use sdur when |sdur|≥thr."""
    f = np.asarray(tip, np.float64)
    h = np.asarray(sdur, np.float64)
    n = min(len(f), len(h))
    f, h = f[:n], h[:n]
    if kind == "soft_w":
        return np.full(n, float(thresh), np.float64)
    if kind == "const_tip":
        return np.ones(n, np.float64)
    if kind == "const_sdur":
        return np.zeros(n, np.float64)
    if kind == "hard_abs_sdur":
        # prefer sdur when |sdur| >= thr else tip
        return (np.abs(h) < thresh).astype(np.float64)
    if kind == "hard_abs_tip":
        return (np.abs(f) >= thresh).astype(np.float64)
    if kind == "abs_sdur":
        # high |sdur| → trust sdur (low g)
        return expit(sharp * (thresh - np.abs(h)))
    if kind == "ewma_abs_sdur":
        return expit(sharp * (thresh - ewma_1d(np.abs(h), ewma_tau)))
    if kind == "abs_tip":
        return expit(sharp * (np.abs(f) - thresh))
    if kind == "ewma_abs_tip":
        return expit(sharp * (ewma_1d(np.abs(f), ewma_tau) - thresh))
    if kind == "ewma_abs_either":
        af = ewma_1d(np.abs(f), ewma_tau)
        ah = ewma_1d(np.abs(h), ewma_tau)
        return expit(sharp * (af - ah - thresh))
    if kind == "disagree":
        return expit(sharp * (thresh - np.abs(f - h)))
    if kind == "hard_maxabs":
        # pick stream with larger |logit|
        return (np.abs(f) >= np.abs(h)).astype(np.float64)
    raise ValueError(kind)


def apply_gate_stream(tip_st, sdur_st, kind, thresh, sharp):
    out = {}
    for key in tip_st:
        if key not in sdur_st:
            continue
        f = np.asarray(tip_st[key], np.float64)
        h = np.asarray(sdur_st[key], np.float64)
        n = min(len(f), len(h))
        g = gate_series(f[:n], h[:n], kind, thresh, sharp)
        out[key] = (g * f[:n] + (1.0 - g) * h[:n]).astype(np.float64)
    return out


def apply_gate_all(tip_all, sdur_all, kind, thresh, sharp):
    return {
        ds: apply_gate_stream(tip_all[ds], sdur_all[ds], kind, thresh, sharp)
        for ds in tip_all
        if ds in sdur_all
    }


def candidate_rules():
    rules = []
    # refs (selectable so selector can keep tip if gating hurts)
    for w in (0.0, 0.25, 0.5, 0.75, 1.0):
        rules.append(("soft_w", w, 1.0, True))
    rules += [
        ("const_tip", 0.0, 1.0, True),
        ("const_sdur", 0.0, 1.0, True),
        # PRIMARY: when |sdur|≥thr use sdur else tip
        ("hard_abs_sdur", 0.5, 1.0, True),
        ("hard_abs_sdur", 1.0, 1.0, True),
        ("hard_abs_sdur", 1.5, 1.0, True),
        ("hard_abs_sdur", 2.0, 1.0, True),
        ("hard_abs_tip", 0.5, 1.0, True),
        ("hard_abs_tip", 1.0, 1.0, True),
        ("hard_abs_tip", 1.5, 1.0, True),
        ("hard_maxabs", 0.0, 1.0, True),
        ("abs_sdur", 0.5, 4.0, True),
        ("abs_sdur", 1.0, 4.0, True),
        ("abs_sdur", 1.5, 4.0, True),
        ("ewma_abs_sdur", 0.5, 4.0, True),
        ("ewma_abs_sdur", 1.0, 4.0, True),
        ("ewma_abs_sdur", 1.5, 4.0, True),
        ("abs_tip", 0.5, 4.0, True),
        ("abs_tip", 1.0, 4.0, True),
        ("ewma_abs_tip", 1.0, 4.0, True),
        ("ewma_abs_either", 0.0, 4.0, True),
        ("ewma_abs_either", 0.25, 4.0, True),
        ("disagree", 0.5, 4.0, True),
        ("disagree", 1.0, 4.0, True),
    ]
    return rules


def rule_key(kind, thr, sharp):
    if kind == "soft_w":
        return f"soft_w={thr:g}"
    if kind in ("const_tip", "const_sdur", "hard_maxabs"):
        return kind
    return f"{kind}|t{thr:g}|s{sharp:g}"


def fuse_held(Z, held, a=A_FUSE):
    zd, zc = Z[held]
    return {
        ds: {k: (1.0 - a) * zd[ds][k] + a * zc[ds][k] for k in zd[ds]}
        for ds in DS_KEYS
    }


def ensure_sdur_lodo():
    path = os.path.join(CACHE_DIR, "sdur_lodo_Z_meta.pkl")
    if os.path.isfile(path):
        log_line(f"cache hit {path}")
        return load_pkl(path)
    from common_exp import fold_file

    missing = []
    for name in PUB_DECS + SDUR_CLFS:
        for ds in DS_KEYS:
            p = fold_file(name, ds)
            if not os.path.exists(p):
                missing.append(p)
    if missing:
        raise FileNotFoundError(
            "sdur/dec folds missing:\n" + "\n".join(missing[:16])
        )
    log_line("building sdur fuse_general Z (pub dec × sdur clf) …")
    t0 = time.time()
    Z, meta = FG.build(PUB_DECS, SDUR_CLFS)
    save_pkl(path, (Z, meta))
    log_line(f"sdur LODO cache wrote ({time.time() - t0:.0f}s)")
    return Z, meta


def ensure_sdur_external():
    path = os.path.join(CACHE_DIR, "sdur_external_a05.pkl")
    if os.path.isfile(path):
        log_line(f"cache hit {path}")
        return load_pkl(path)
    res = FE.RES
    missing = []
    for d in EXT_DECS:
        for ds_tag in ("aasd_c1_bestP", "ext_c1_Carta2026_bestP"):
            p = os.path.join(res, f"{ds_tag}_evidence_s{d[1]}.pkl")
            if not os.path.exists(p):
                missing.append(p)
    for c in EXT_CLFS:
        for ds in ("AASD", "Carta2026"):
            p = os.path.join(res, f"ext_clf_{ds}{c[1]}_evidence_s{c[2]}.pkl")
            if not os.path.exists(p):
                missing.append(p)
    if missing:
        raise FileNotFoundError(
            "sdur external pickles missing:\n" + "\n".join(missing[:12])
        )
    log_line("building sdur external fuse a=0.5 …")
    t0 = time.time()
    EV, emis = FE.build_external(
        EXT_DECS, EXT_CLFS, [A_FUSE], ["AASD", "Carta2026"], "_bestP"
    )
    save_pkl(path, (EV, emis))
    log_line(f"sdur external cache wrote ({time.time() - t0:.0f}s)")
    return EV, emis


def build_tip_held(Z_sw2, swmix_full):
    tip_held = {}
    for held in DS_KEYS:
        sw2 = SW2.fuse_sw2_held(Z_sw2, held, a=A_FUSE)
        sm = SM.align_swmix_to_sw2(swmix_full, sw2)
        tip_held[held] = SM.apply_gate_all(sw2, sm, TIP_KIND, TIP_THR, TIP_SHARP)
    return tip_held


def build_sdur_held(Z_sdur):
    return {held: fuse_held(Z_sdur, held, a=A_FUSE) for held in DS_KEYS}


def align_pair(tip_st, sdur_st):
    """Truncate both streams to shared keys/lengths (same fuse grid)."""
    tip_out, sdur_out = {}, {}
    for ds in tip_st:
        if ds not in sdur_st:
            continue
        tip_out[ds], sdur_out[ds] = {}, {}
        for k in tip_st[ds]:
            if k not in sdur_st[ds]:
                continue
            a = np.asarray(tip_st[ds][k], np.float64).reshape(-1)
            b = np.asarray(sdur_st[ds][k], np.float64).reshape(-1)
            n = min(len(a), len(b))
            tip_out[ds][k] = a[:n]
            sdur_out[ds][k] = b[:n]
    return tip_out, sdur_out


def score_lodo_held(stream_all, meta, held):
    return SM.score_lodo_held(stream_all, meta, held)


def mean_lodo(tip_held, sdur_held, meta, kind, thr, sharp):
    hs, acs, per = [], [], {}
    for held in DS_KEYS:
        tip, sdur = align_pair(tip_held[held], sdur_held[held])
        gated = apply_gate_all(tip, sdur, kind, thr, sharp)
        r = score_lodo_held(gated, meta, held)
        hs.append(r["H"])
        acs.append(r["Acc"])
        per[held] = dict(H=r["H"], Acc=r["Acc"], SDT=r["SDT"], L=r.get("L"))
    return float(np.mean(hs)), float(np.mean(acs)), per


def corpus_emis(tip_held, sdur_held, meta, kind, thr, sharp):
    acc = {ds: {} for ds in DS_KEYS}
    counts = {ds: {} for ds in DS_KEYS}
    for held in DS_KEYS:
        tip, sdur = align_pair(tip_held[held], sdur_held[held])
        gated = apply_gate_all(tip, sdur, kind, thr, sharp)
        for ds in DS_KEYS:
            for k, d in gated[ds].items():
                d = np.asarray(d, np.float64)
                if k not in acc[ds]:
                    acc[ds][k] = np.zeros_like(d, np.float64)
                    counts[ds][k] = 0
                n = min(len(acc[ds][k]), len(d))
                acc[ds][k][:n] += d[:n]
                counts[ds][k] += 1
    stream = {
        ds: {k: acc[ds][k] / max(counts[ds][k], 1) for k in acc[ds]}
        for ds in DS_KEYS
    }
    parts = []
    for ds in DS_KEYS:
        for k, d in stream[ds].items():
            att = meta[ds][k][0]
            n = min(len(d), len(att))
            parts.append(
                d[:n] * np.where(np.asarray(att)[:n] == 0, 1.0, -1.0)
            )
    A = np.concatenate(parts)
    return float(A.mean()), float(A.std())


def blend_ext_ev(tip_ev, sdur_ev, kind, thr, sharp):
    out = {}
    for k, e in tip_ev.items():
        if k not in sdur_ev:
            continue
        f = np.asarray(e["r0"], np.float64)
        h = np.asarray(sdur_ev[k]["r0"], np.float64)
        n = min(len(f), len(h))
        g = gate_series(f[:n], h[:n], kind, thr, sharp)
        d = g * f[:n] + (1.0 - g) * h[:n]
        out[k] = dict(
            r0=d,
            r1=np.zeros(n),
            att=e["att"][:n],
            t_end=e["t_end"][:n],
            att128=e["att128"],
            n128=e["n128"],
            subject=e["subject"],
        )
    return out


def build_tip_ext_ev(EV_sw2, sm_ext):
    """hard_abs_swmix|t1 on external SW2 fuse × aligned swmix."""
    out = {}
    for ds in ("AASD", "Carta2026"):
        sw2_ev = EV_sw2[ds][A_FUSE]
        sm_aligned = SM.align_ext_swmix_to_sw2(sm_ext[ds], sw2_ev)
        out[ds] = SM.blend_ext_ev(sw2_ev, sm_aligned, TIP_KIND, TIP_THR, TIP_SHARP)
    return out


def _json_default(o):
    if isinstance(o, (np.floating, np.integer)):
        return float(o)
    if isinstance(o, np.ndarray):
        return o.tolist()
    raise TypeError(type(o))


def write_md(summary):
    sel = summary["selected"]
    aasd = summary["external"]["AASD"]
    carta = summary["external"]["Carta2026"]
    tip_acc = TIP_EXT["AASD"]["Acc"]
    hp_acc = LINEAR_HP30_EXT["AASD"]["Acc"]
    lines = [
        "# results_tip_sdur_abs_gate",
        "",
        "**Claim:** `tip-sdur-abs-gate` — causal hard/soft `|logit|` gate between Acc tip "
        "`hard_abs_swmix|t1` (AASD **53.53**) and clf-switch-dur fuse a=0.5 (AASD **53.20**).",
        "",
        "**Primary rule:** when `|sdur| ≥ thr` use sdur else tip (`hard_abs_sdur|t*`). "
        "Also thr soft-sigmoid, hard_abs_tip, maxabs, soft-w tip↔sdur refs.",
        "",
        "**Distinct from** SW2↔swmix soft-w re-grid, tip Platt/domain-emis, fuse_general a-grid.",
        "",
        f"**Honesty:** CPU caches only (sw2_swmix_blend + clf_switch_dur folds/ext). "
        f"Select by mean LODO Acc then H @ HMM **{P_STAY}/{SCALE}**. Never AASD/Carta for "
        f"selection. Code: `tip_sdur_abs_gate.py`. JSON: `results/research/tip_sdur_abs_gate.json`.",
        "",
        "## Selection",
        "",
        f"**SELECTED `{sel['name']}`** — mean LODO H **{sel['mean_LODO_H']:.4f}** "
        f"Acc **{sel['mean_LODO_Acc']:.2f}**",
        "",
        "| Held-out | H | Acc % |",
        "|---|---:|---:|",
    ]
    for ds in DS_KEYS:
        p = sel["per_ds"][ds]
        lines.append(f"| {ds} | {p['H']:.4f} | {p['Acc']:.2f} |")
    lines += [
        f"| **mean** | **{sel['mean_LODO_H']:.4f}** | **{sel['mean_LODO_Acc']:.2f}** |",
        "",
        "### Variant grid (LODO Acc desc)",
        "",
        "| Rule | mean H | mean Acc | AASD H/Acc | ΔAcc vs tip | Carta H/Acc |",
        "|---|---:|---:|---|---:|---|",
    ]
    ranked = sorted(
        summary["lodo_grid"],
        key=lambda r: (
            0 if r["name"] == sel["name"] else 1,
            -float(r["mean_LODO_Acc"]),
            -float(r["mean_LODO_H"]),
        ),
    )
    for r in ranked:
        mark = " **←**" if r["name"] == sel["name"] else ""
        ex = r.get("external") or {}
        a = ex.get("AASD", {})
        c = ex.get("Carta2026", {})
        if a:
            lines.append(
                f"| `{r['name']}`{mark} | {r['mean_LODO_H']:.4f} | {r['mean_LODO_Acc']:.2f} | "
                f"{a['H']:.4f}/{a['Acc']:.2f} | {a['Acc']-tip_acc:+.2f} | "
                f"{c['H']:.4f}/{c['Acc']:.2f} |"
            )
        else:
            lines.append(
                f"| `{r['name']}`{mark} | {r['mean_LODO_H']:.4f} | {r['mean_LODO_Acc']:.2f} | "
                f"— | — | — |"
            )
    d_tip = aasd["Acc"] - tip_acc
    d_hp = aasd["Acc"] - hp_acc
    d_carta = carta["Acc"] - LINEAR_HP30_EXT["Carta2026"]["Acc"]
    lines += [
        "",
        "## External (preset)",
        "",
        "| Set | selected H/Acc | tip Acc leader | sdur fuse | linear_hp30 |",
        "|---|---|---|---|---|",
    ]
    for ds in ("AASD", "Carta2026"):
        s = summary["external"][ds]
        tip = TIP_EXT[ds]
        sd = SDUR_EXT[ds]
        b = LINEAR_HP30_EXT[ds]
        lines.append(
            f"| {ds} | {s['H']:.4f} / **{s['Acc']:.2f}** | {tip['H']:.4f}/{tip['Acc']:.2f} | "
            f"{sd['H']:.4f}/{sd['Acc']:.2f} | {b['H']:.4f}/{b['Acc']:.2f} |"
        )
    lines += [
        "",
        "## Targets",
        "",
        "| Target | Result |",
        "|---|---|",
        f"| AASD Acc vs tip 53.53 / hp30 53.12 | Acc **{aasd['Acc']:.2f}** "
        f"(Δtip {d_tip:+.2f}; Δhp {d_hp:+.2f}); H {aasd['H']:.4f} |",
        f"| Carta vs hp30 60.54 | Acc **{carta['Acc']:.2f}** (Δ {d_carta:+.2f}); "
        f"H {carta['H']:.4f} |",
        f"| LODO mean | H {sel['mean_LODO_H']:.4f} Acc {sel['mean_LODO_Acc']:.2f} |",
        f"| AASD Acc>60? | {'YES' if aasd['Acc'] > 60 else 'NO'} |",
        "",
        "## Verdict",
        "",
        summary.get("verdict", ""),
        "",
        "## Do not repeat",
        "",
        "Do not re-grid tip `hard_abs_swmix|t1` ↔ clf-switch-dur fuse a=0.5 hard/soft "
        "`|logit|` gates under LODO Acc then H at this HMM. Do not re-grid SW2↔swmix soft-w.",
        "",
        "## NEXT IDEA",
        "",
        summary.get("next_idea", ""),
        "",
    ]
    with open(RESULTS_MD, "w") as fh:
        fh.write("\n".join(lines))


def update_ledger(summary):
    aasd = summary["external"]["AASD"]
    carta = summary["external"]["Carta2026"]
    sel = summary["selected"]
    tip_acc = TIP_EXT["AASD"]["Acc"]
    dacc = aasd["Acc"] - tip_acc
    note = (
        f"Select `{sel['name']}`. LODO {sel['mean_LODO_H']:.4f}/{sel['mean_LODO_Acc']:.2f}. "
        f"AASD {aasd['H']:.4f}/{aasd['Acc']:.2f} (Δtip {dacc:+.2f}). "
        f"Carta {carta['H']:.4f}/{carta['Acc']:.2f}. Acc>60="
        f"{'YES' if aasd['Acc'] > 60 else 'NO'}. `results_tip_sdur_abs_gate.md`."
    )
    # CLAIMS
    claims = os.path.join(EXP, "multi_agent_opt", "CLAIMS.md")
    with open(claims) as fh:
        text = fh.read()
    old = (
        "| 2026-10-30 00:15 +07 | tip-sdur-abs-gate | Causal |logit| hard gate Acc tip "
        "hard_abs_swmix|t1 ↔ clf-switch-dur fuse; LODO Acc then H @0.994/0.8; ≠ SW2↔swmix "
        "soft-w re-grid, ≠ tip PP | running | CPU Acc; tip_sdur_abs_gate.py |"
    )
    new = (
        "| 2026-10-30 00:15 +07 | tip-sdur-abs-gate | Causal |logit| hard gate Acc tip "
        "hard_abs_swmix|t1 ↔ clf-switch-dur fuse; LODO Acc then H @0.994/0.8; ≠ SW2↔swmix "
        f"soft-w re-grid, ≠ tip PP | done | {note} |"
    )
    if old in text:
        text = text.replace(old, new)
        with open(claims, "w") as fh:
            fh.write(text)

    already = os.path.join(EXP, "multi_agent_opt", "ALREADY_TRIED.md")
    bullet = (
        f"- **tip↔sdur abs-gate** (`tip-sdur-abs-gate`; `tip_sdur_abs_gate.py`; causal "
        f"`|logit|` hard/soft gate Acc tip `hard_abs_swmix|t1` ↔ clf-switch-dur fuse a=0.5; "
        f"LODO Acc then H @0.994/0.8; CPU; reuse sw2_swmix_blend + sdur caches; "
        f"**≠** SW2↔swmix soft-w / tip Platt/domain-emis / fuse_general a-grid): "
        f"select→**`{sel['name']}`**. LODO mean **{sel['mean_LODO_H']:.4f}/"
        f"{sel['mean_LODO_Acc']:.2f}**. AASD **{aasd['H']:.4f}/{aasd['Acc']:.2f}** "
        f"(Δtip **{dacc:+.2f}**). Carta **{carta['H']:.4f}/{carta['Acc']:.2f}**. "
        f"Acc>60=**{'YES' if aasd['Acc'] > 60 else 'NO'}**. "
        f"`multi_agent_opt/results_tip_sdur_abs_gate.md`.\n"
    )
    with open(already) as fh:
        at = fh.read()
    if "tip-sdur-abs-gate" not in at and "tip↔sdur abs-gate" not in at:
        with open(already, "a") as fh:
            fh.write("\n" + bullet)

    status = os.path.join(EXP, "multi_agent_opt", "STATUS_GAPS.md")
    with open(status) as fh:
        st = fh.read()
    st = st.replace(
        "2. **tip-sdur-abs-gate** — hard |logit| tip↔switch-dur Acc (CPU) **running**.",
        f"2. ~~tip-sdur-abs-gate~~ — closed Acc **{aasd['Acc']:.2f}** (Δtip {dacc:+.2f}).",
    )
    # also handle if still old string
    st = st.replace(
        "2. **tip-sdur-abs-gate** — hard |logit| tip↔switch-dur Acc (CPU).",
        f"2. ~~tip-sdur-abs-gate~~ — closed Acc **{aasd['Acc']:.2f}** (Δtip {dacc:+.2f}).",
    )
    if "Recently closed" in st and "tip-sdur-abs-gate" not in st.split("## Recently closed")[1][:800]:
        st = st.replace(
            "## Recently closed\n",
            "## Recently closed\n\n"
            f"- **tip-sdur-abs-gate** — Acc **{aasd['Acc']:.2f}** (Δtip {dacc:+.2f}). "
            "Stop tip↔sdur |logit| gates.\n",
        )
    with open(status, "w") as fh:
        fh.write(st)


def main():
    os.makedirs(CACHE_DIR, exist_ok=True)
    os.makedirs(os.path.dirname(LOG), exist_ok=True)
    os.makedirs(os.path.dirname(OUT), exist_ok=True)
    open(LOG, "w").close()
    t_all = time.time()
    log_line(
        f"tip-sdur-abs-gate CPU  p_stay={P_STAY} scale={SCALE} a={A_FUSE} "
        f"tip={TIP_KIND}|t{TIP_THR:g} OFF={OFF}"
    )
    log_line("gate: |sdur|≥thr → sdur else tip (+ soft/hard thr variants)")
    log_line("select mean LODO Acc then H; never AASD/Carta; ≠ SW2↔swmix soft-w")

    try:
        Z_sw2, meta_sw2 = SM.ensure_sw2_lodo()
        swmix_full, la_sm = SM.ensure_swmix_lodo()
        Z_sdur, meta_sdur = ensure_sdur_lodo()
        log_line(f"lookahead LA={LOOKAHEAD} swmix_la={la_sm}")
    except FileNotFoundError as e:
        log_line(f"ABORT: {e}")
        with open(OUT, "w") as fh:
            json.dump(dict(status="abandoned", reason=str(e)), fh, indent=2)
        with open(RESULTS_MD, "w") as fh:
            fh.write(f"# results_tip_sdur_abs_gate\n\n**Status: abandoned**\n\n{e}\n")
        return

    # Prefer SW2 meta (same trial keys); sdur should match
    meta = meta_sw2
    log_line("building tip (hard_abs_swmix|t1) + sdur fuse a=0.5 per held …")
    tip_held = build_tip_held(Z_sw2, swmix_full)
    sdur_held = build_sdur_held(Z_sdur)

    # Sanity
    tip_a, sdur_a = align_pair(tip_held["AVGC"], sdur_held["AVGC"])
    r_tip = score_lodo_held(tip_a, meta, "AVGC")
    r_sdur = score_lodo_held(sdur_a, meta, "AVGC")
    r_hard = score_lodo_held(
        apply_gate_all(tip_a, sdur_a, "hard_abs_sdur", 1.0, 1.0), meta, "AVGC"
    )
    log_line(
        f"sanity AVGC tip H {r_tip['H']:.4f} Acc {r_tip['Acc']:.2f} "
        f"(expect ~0.7159/~67.94)"
    )
    log_line(
        f"sanity AVGC sdur H {r_sdur['H']:.4f} Acc {r_sdur['Acc']:.2f} "
        f"(expect ~fuse switch-dur)"
    )
    log_line(
        f"sanity AVGC hard_abs_sdur|t1 H {r_hard['H']:.4f} Acc {r_hard['Acc']:.2f}"
    )

    rules = candidate_rules()
    rows = []
    best = None
    log_line(f"scoring {len(rules)} rules on mean LODO Acc then H")
    for kind, thr, sharp, selectable in rules:
        name = rule_key(kind, thr, sharp)
        t0 = time.time()
        mean_h, mean_acc, per = mean_lodo(tip_held, sdur_held, meta, kind, thr, sharp)
        row = dict(
            name=name, kind=kind, thr=thr, sharp=sharp, selectable=selectable,
            mean_LODO_H=mean_h, mean_LODO_Acc=mean_acc, per_ds=per,
        )
        rows.append(row)
        log_line(
            f"  LODO {name:28s} H {mean_h:.4f} Acc {mean_acc:.2f} "
            f"({time.time()-t0:.0f}s)"
        )
        if selectable:
            key = (mean_acc, mean_h)
            if best is None or key > best[0]:
                best = (key, row)

    selected = best[1]
    log_line(
        f"SELECTED {selected['name']} mean LODO H {selected['mean_LODO_H']:.4f} "
        f"Acc {selected['mean_LODO_Acc']:.2f}"
    )

    # External
    try:
        EV_sw2, _emis_sw2 = SM.ensure_sw2_external()
        sm_ext = SM.ensure_swmix_external()
        EV_sdur, _emis_sdur = ensure_sdur_external()
    except FileNotFoundError as e:
        log_line(f"ABORT external: {e}")
        summary = dict(
            status="partial", selected=selected, lodo_grid=rows, reason=str(e)
        )
        with open(OUT, "w") as fh:
            json.dump(summary, fh, indent=2, default=_json_default)
        write_md(
            dict(
                selected=selected,
                lodo_grid=rows,
                external={
                    ds: dict(H=float("nan"), Acc=float("nan"))
                    for ds in ("AASD", "Carta2026")
                },
                verdict="Partial: LODO done; external caches missing.",
                next_idea="Restore external SW2/swmix/sdur pickles; rescore AASD/Carta only.",
            )
        )
        return

    tip_ext = build_tip_ext_ev(EV_sw2, sm_ext)
    sdur_ext = {ds: EV_sdur[ds][A_FUSE] for ds in ("AASD", "Carta2026")}

    def score_ext(kind, thr, sharp):
        mu, sd = corpus_emis(tip_held, sdur_held, meta, kind, thr, sharp)
        out = {}
        for ds in ("AASD", "Carta2026"):
            ev_use = blend_ext_ev(tip_ext[ds], sdur_ext[ds], kind, thr, sharp)
            r = E.score(ev_use, mu, sd * SCALE, P_STAY, LOOKAHEAD)
            pl = E.plain(ev_use)
            out[ds] = dict(
                H=r["H"], Acc=r["Acc"], SDT=r["SDT"], L=r["L"],
                acc1=pl["acc1"], nSwitches=r["nSwitches"], nUndetected=r["nUndetected"],
                emis_mu=mu, emis_sd=sd,
            )
        return out

    # Score all rules externally (CPU; cheap vs LODO)
    for row in rows:
        t0 = time.time()
        ex = score_ext(row["kind"], row["thr"], row["sharp"])
        row["external"] = ex
        for ds in ("AASD", "Carta2026"):
            r = ex[ds]
            log_line(
                f"EXT {row['name']:28s} {ds:10s} H {r['H']:.4f} Acc {r['Acc']:.2f} "
                f"({time.time()-t0:.0f}s)"
            )

    sel_ext = selected["external"]
    aasd_acc = sel_ext["AASD"]["Acc"]
    d_tip = aasd_acc - TIP_EXT["AASD"]["Acc"]
    if aasd_acc > 60:
        verdict = "Selected tip↔sdur |logit| gate clears AASD Acc>60."
        next_idea = (
            "Freeze selected gate; probe Carta stability. Avoid soft-w Acc re-grids."
        )
    elif aasd_acc > TIP_EXT["AASD"]["Acc"] + 0.15:
        verdict = (
            f"Selected `{selected['name']}` lifts AASD Acc vs tip 53.53 "
            f"(Δ{d_tip:+.2f}) but does not clear Acc>60."
        )
        next_idea = (
            "Small Acc lift from tip↔sdur |logit| gate — do not re-grid. Prefer a "
            "qualitatively new Acc front-end (non-vlaai / short-hold curriculum) rather "
            "than Acc-stream soft/hard blends among mid-50s leaders."
        )
    else:
        verdict = (
            "Honest LODO Acc/H selector over tip `hard_abs_swmix|t1` ↔ clf-switch-dur "
            "fuse `|logit|` gates does not create AASD Acc>60. Mixing two Acc mid-50s "
            "streams stays mid-50s."
        )
        next_idea = (
            "Do not re-grid tip↔sdur |logit| or SW2↔swmix soft-w. Prefer a qualitatively "
            "new Acc frame that is not tip PP, Acc soft-w, switch-dur/denser p_switch FT, "
            "or frozen-trunk head-swap — e.g. non-vlaai Acc front-end or AASD-like "
            "short-hold curriculum estimated without external labels. Keep tip Acc 53.53 "
            "and linear_hp30 Carta 60.54 as portfolio leaders."
        )

    summary = dict(
        status="done",
        method="tip-sdur-abs-gate",
        selected=selected,
        p_stay=P_STAY,
        scale=SCALE,
        a_fuse=A_FUSE,
        tip=dict(kind=TIP_KIND, thresh=TIP_THR, sharp=TIP_SHARP),
        lodo_grid=rows,
        external=sel_ext,
        baselines=dict(
            tip_ext=TIP_EXT,
            sdur_ext=SDUR_EXT,
            linear_hp30_ext=LINEAR_HP30_EXT,
            sw2_alone_ext=SW2_ALONE_EXT,
        ),
        sanity=dict(AVGC_tip=r_tip, AVGC_sdur=r_sdur, AVGC_hard_t1=r_hard),
        verdict=verdict,
        next_idea=next_idea,
        elapsed_s=time.time() - t_all,
    )
    with open(OUT, "w") as fh:
        json.dump(summary, fh, indent=2, default=_json_default)
    write_md(summary)
    update_ledger(summary)
    log_line(
        f"DONE selected={selected['name']} AASD Acc {aasd_acc:.2f} "
        f"(Δtip {d_tip:+.2f}) Acc>60={'YES' if aasd_acc > 60 else 'NO'} "
        f"in {time.time()-t_all:.0f}s → {OUT}"
    )


if __name__ == "__main__":
    main()
