#!/usr/bin/env python3
"""Mesure d'un pilotage de la Lab Strategy Arena par des IA (MCP), recalculée depuis les journaux de la Lab.

Lecture seule. Lit uniquement :
  <lab>/session-journal/<journal>.jsonl          un appel MCP par ligne (agentLabel, toolName, outcome, startedAtMs, facts)
  <lab>/lab-proof-reports/holdout-openings-v1.jsonl   ouvertures de périodes scellées
  <lab>/lab-proof-reports/SAP-*.json             verdicts (reportId, createdAtMs, verdict.code)
  <lab>/portfolio-273a/{declared,cells}.jsonl    hypothèses et verdicts de portefeuille
Ne lit jamais les fichiers de connexion ou de jeton de la Lab (agent-connection-v1.json, agent-control-session.json,
strategyarena_bridge.json) et n'écrit rien dans le dossier de la Lab.

Usage :
  python3 mesurer_pilotage.py --lab <dossier de la Lab> --sessions sessions_17nov.json --out mesures.json
  (--jusqua <ms UTC> pour figer la mesure à une heure donnée)

Rituel : à chaque version de la Lab, copier sessions_17nov.json en sessions_<date>.json, y mettre le journal de la
séance et les étiquettes de chaque IA, relancer ce script, puis outils/construire_public.py.
"""
import argparse
import collections
import datetime
import glob
import json
import os
import sys

INTERDITS = {"agent-connection-v1.json", "agent-control-session.json", "strategyarena_bridge.json"}
APPELS_JUGE = {"promote", "compose_portfolio", "prove_sandbox_script"}
MARGE_MS = 1500


def lire_jsonl(chemin):
    if os.path.basename(chemin) in INTERDITS:
        raise SystemExit(f"refus : {chemin} est un fichier de connexion")
    lignes = []
    with open(chemin, encoding="utf-8") as f:
        for l in f:
            l = l.strip()
            if l:
                try:
                    lignes.append(json.loads(l))
                except ValueError:
                    pass
    return lignes


def iso(ms):
    return datetime.datetime.fromtimestamp(ms / 1000, datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")


def ms_de(iso_utc):
    return int(datetime.datetime.strptime(iso_utc, "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=datetime.timezone.utc).timestamp() * 1000)


def session_de(appel, sessions):
    """Étiquette d'agent (exacte ou préfixe), et fenêtre horaire facultative (« apres »/« avant », ISO UTC) quand un même
    client sert à plusieurs pilotages successifs."""
    label = appel.get("agentLabel")
    if not label:
        return None
    t = appel.get("startedAtMs", 0)
    for s in sessions:
        if not (label in s.get("labels", []) or any(label.startswith(p) for p in s.get("labelPrefixes", []))):
            continue
        if s.get("apres") and t < ms_de(s["apres"]):
            continue
        if s.get("avant") and t >= ms_de(s["avant"]):
            continue
        return s["id"]
    return None


def quest(appel):
    f = appel.get("facts") or {}
    return f.get("quest") or {} if isinstance(f, dict) else {}


def mesurer(lab, conf, jusqua=None):
    journal = lire_jsonl(os.path.join(lab, conf["journal"]))
    if jusqua:
        journal = [a for a in journal if a.get("startedAtMs", 0) <= jusqua]
    ouvertures = lire_jsonl(os.path.join(lab, "lab-proof-reports", "holdout-openings-v1.jsonl"))
    rapports = []
    for p in glob.glob(os.path.join(lab, "lab-proof-reports", "SAP-*.json")):
        if p.endswith(".series-244a.json"):
            continue
        try:
            d = json.load(open(p, encoding="utf-8"))
        except (OSError, ValueError):
            continue
        v = d.get("verdict") or {}
        rapports.append({"reportId": d.get("reportId") or os.path.basename(p)[:-5],
                         "createdAtMs": d.get("createdAtMs"), "verdict": v.get("code") if isinstance(v, dict) else v,
                         "marche": (d.get("dataset") or {}).get("label")})
    pf_decl = lire_jsonl(os.path.join(lab, "portfolio-273a", "declared.jsonl")) if os.path.exists(os.path.join(lab, "portfolio-273a", "declared.jsonl")) else []
    pf_trials = lire_jsonl(os.path.join(lab, "portfolio-273a", "trials.jsonl")) if os.path.exists(os.path.join(lab, "portfolio-273a", "trials.jsonl")) else []
    pf_cells = lire_jsonl(os.path.join(lab, "portfolio-273a", "cells.jsonl")) if os.path.exists(os.path.join(lab, "portfolio-273a", "cells.jsonl")) else []

    marche_de_partie = {}
    for a in journal:
        q = quest(a)
        if q.get("game") and q.get("market"):
            marche_de_partie[q["game"]] = q["market"]
    sessions = sorted(conf["sessions"], key=lambda s: s.get("ordre", 0))
    par = collections.defaultdict(list)
    sans = []
    for a in journal:
        sid = session_de(a, sessions)
        (par[sid] if sid else sans).append(a)

    fenetres = {}
    for s in sessions:
        ap = par.get(s["id"], [])
        if ap:
            fenetres[s["id"]] = (min(a["startedAtMs"] for a in ap), max(a.get("endedAtMs", a["startedAtMs"]) for a in ap))

    res = {"schema": "strategyarena.ia_pilotage_mesure.v1", "edition": conf.get("edition"), "labVersion": conf.get("labVersion"),
           "mesureLe": iso(int(datetime.datetime.now(datetime.timezone.utc).timestamp() * 1000)),
           "jusqua": iso(jusqua) if jusqua else None,
           "journalLignes": len(journal), "sessions": [], "nonAttribues": []}

    for s in sessions:
        ap = sorted(par.get(s["id"], []), key=lambda a: a["startedAtMs"])
        if not ap:
            res["sessions"].append({"id": s["id"], "modele": s.get("modele"), "client": s.get("client"), "appels": 0})
            continue
        debut, fin = fenetres[s["id"]]
        duree_min = (fin - debut) / 60000.0
        echecs = [a for a in ap if a.get("outcome") != "response_returned"]
        # appels en parallèle : l'appel commence avant la fin d'un appel précédent de la même session
        fin_max, paralleles = 0, 0
        for a in ap:
            if a["startedAtMs"] < fin_max:
                paralleles += 1
            fin_max = max(fin_max, a.get("endedAtMs", a["startedAtMs"]))
        pauses = sorted((b["startedAtMs"] - a["startedAtMs"]) for a, b in zip(ap, ap[1:]))
        rafale = sum(1 for x in pauses if x < 1000)
        pause_mediane_s = round(pauses[len(pauses) // 2] / 1000.0, 1) if pauses else None
        outils = collections.Counter(a["toolName"] for a in ap)
        echecs_par_outil = collections.Counter(a["toolName"] for a in echecs)

        # essais : réglages évalués par move (trialsThisCall), backtests isolés et preuves isolées
        reglages = sum(int(quest(a).get("trialsThisCall") or 0) for a in ap
                       if a["toolName"] == "move" and a.get("outcome") == "response_returned")
        backtests = sum(1 for a in ap if a["toolName"] == "run_sandbox_backtest" and a.get("outcome") == "response_returned")
        t_essais = [a["startedAtMs"] for a in ap if a.get("outcome") == "response_returned" and (
            (a["toolName"] == "move" and int(quest(a).get("trialsThisCall") or 0) > 0)
            or a["toolName"] in ("run_sandbox_backtest", "prove_sandbox_script", "promote"))]
        t_essais += [r["atMs"] for r in pf_trials if debut - MARGE_MS <= r.get("atMs", 0) <= fin + MARGE_MS]
        moves_essais = [a for a in ap if a["toolName"] == "move" and int(quest(a).get("trialsThisCall") or 0) > 0]
        parties_vues = {quest(a).get("game") for a in ap if a["toolName"] == "board" and quest(a).get("game")}
        parties_essais = {quest(a).get("game") for a in moves_essais if quest(a).get("game")}
        parties_promues = {quest(a).get("game") for a in ap if a["toolName"] == "promote" and quest(a).get("game")}
        pistes = []
        for g in sorted(parties_essais - parties_promues):
            mv = [a for a in moves_essais if quest(a).get("game") == g]
            dg = [quest(a) for a in ap if a["toolName"] == "diagnose" and a.get("outcome") == "response_returned" and quest(a).get("game") == g]
            pistes.append({"marche": marche_de_partie.get(g), "reglagesEvalues": sum(int(quest(a).get("trialsThisCall") or 0) for a in mv),
                           "casesTuees": max([int(quest(a).get("killed") or 0) for a in mv] or [0]),
                           "diagnosticsLus": len(dg),
                           "dernierDiagnostic": ({"alertes": dg[-1].get("flags"), "timingPercentile": dg[-1].get("timingPercentile")} if dg else None),
                           "dernierAppelUtc": iso(max(a["startedAtMs"] for a in ap if quest(a).get("game") == g))})
        hyp = [a for a in ap if a["toolName"] == "discovery" and a.get("outcome") == "response_returned"
               and quest(a).get("step") == "hypothesis"]
        hyp_pf = [d for d in pf_decl if debut - MARGE_MS <= d.get("declaredAtMs", 0) <= fin + MARGE_MS]
        premier_essai = min(t_essais) if t_essais else None
        premiere_hyp = min([a["startedAtMs"] for a in hyp] + [d["declaredAtMs"] for d in hyp_pf]) if (hyp or hyp_pf) else None
        hyp_avant = bool(premiere_hyp is not None and (premier_essai is None or premiere_hyp <= premier_essai))
        sans_hyp = [a for a in ap if a["toolName"] == "move" and int(quest(a).get("trialsThisCall") or 0) > 0
                    and not quest(a).get("hypothesisId") and quest(a).get("game")]
        ids_hyp = sorted({quest(a).get("hypothesisId") for a in hyp if quest(a).get("hypothesisId")} |
                         {d.get("hypothesisId") for d in hyp_pf if d.get("hypothesisId")})

        # ouvertures de périodes scellées : dans la fenêtre de la session, rattachées à un appel juge de la session
        juges = [a for a in ap if a["toolName"] in APPELS_JUGE]
        ouv = []
        for o in ouvertures:
            t = o.get("atMs", 0)
            if not (debut - MARGE_MS <= t <= fin + MARGE_MS):
                continue
            appel = next((a for a in juges if a["startedAtMs"] - MARGE_MS <= t <= a.get("endedAtMs", a["startedAtMs"]) + MARGE_MS), None)
            autre = any(fenetres[k][0] - MARGE_MS <= t <= fenetres[k][1] + MARGE_MS for k in fenetres if k != s["id"])
            ouv.append({"atUtc": iso(t), "origine": o.get("origin"), "appel": appel["toolName"] if appel else None,
                        "attribution": "certaine" if appel and not autre else "incertaine"})

        # verdicts : facts.verdict des appels, rapports SAP créés pendant un appel juge, cellules de portefeuille promues
        verdicts = []
        vus = set()
        for r in rapports:
            t = r.get("createdAtMs") or 0
            appel = next((a for a in juges if a["startedAtMs"] - MARGE_MS <= t <= a.get("endedAtMs", a["startedAtMs"]) + MARGE_MS), None)
            if appel:
                verdicts.append({"reportId": r["reportId"], "verdict": r["verdict"], "appel": appel["toolName"], "atUtc": iso(t),
                                 "marche": r.get("marche")})
                vus.add(r["reportId"])
        for c in pf_cells:
            if c.get("kind273a") == "promoted" and debut - MARGE_MS <= c.get("atMs", 0) <= fin + MARGE_MS and c.get("reportId") not in vus:
                v = c.get("verdict") or {}
                verdicts.append({"reportId": c.get("reportId"), "verdict": v.get("code") if isinstance(v, dict) else v,
                                 "appel": "portefeuille", "atUtc": iso(c["atMs"]), "marche": "portefeuille"})
                vus.add(c.get("reportId"))
        verdicts.sort(key=lambda v: v["atUtc"])
        codes = collections.Counter(v["verdict"] for v in verdicts)

        # écart d'affichage : un appel juge qui dit sealedOpened:false alors qu'une ouverture lui est rattachée
        ecart_sceau = 0
        for a in juges:
            q = quest(a)
            if "sealedOpened" in q and not q.get("sealedOpened"):
                if any(a["startedAtMs"] - MARGE_MS <= o.get("atMs", 0) <= a.get("endedAtMs", a["startedAtMs"]) + MARGE_MS for o in ouvertures):
                    ecart_sceau += 1

        res["sessions"].append({
            "id": s["id"], "modele": s.get("modele"), "client": s.get("client"),
            "debutUtc": iso(debut), "finUtc": iso(fin), "dureeMin": round(duree_min, 1),
            "appels": len(ap), "appelsParMinute": round(len(ap) / duree_min, 1) if duree_min > 0 else None,
            "appelsEnEchec": len(echecs), "appelsEnParallele": paralleles,
            "partAppelsEnParallelePct": round(100.0 * paralleles / len(ap), 1), "ecartMedianEntreAppelsS": pause_mediane_s,
            "appelsAMoinsDUneSecondeDuPrecedent": rafale, "partAppelsEnRafalePct": round(100.0 * rafale / len(ap), 1), "partEchecsPct": round(100.0 * len(echecs) / len(ap), 1),
            "echecsParOutil": dict(echecs_par_outil.most_common()),
            "outilsDistincts": len(outils), "outils": dict(outils.most_common()),
            "hypothesesDeclarees": len(ids_hyp), "hypotheseIds": ids_hyp,
            "hypotheseAvantToutEssai": hyp_avant,
            "partiesRegardees": len(parties_vues), "partiesAvecEssais": len(parties_essais),
            "partiesArreteesSansOuvrirLeurSceau": len(parties_essais - parties_promues),
            "pistesArretees": pistes,
            "movesSansHypothese": len(sans_hyp), "movesAvecEssais": len(moves_essais),
            "reglagesEvaluesParMove": reglages, "backtestsIsoles": backtests,
            "periodesScelleesOuvertes": len(ouv), "ouvertures": ouv,
            "ouverturesAttributionIncertaine": sum(1 for o in ouv if o["attribution"] != "certaine"),
            "verdicts": verdicts, "verdictsParCode": dict(codes),
            "abandonSansOuverture": len(ouv) == 0 and not verdicts,
            "appelsJugeDisantSceauFermeAlorsQuOuvert": ecart_sceau,
        })

    res["nonAttribues"] = [{"atUtc": iso(a["startedAtMs"]), "toolName": a["toolName"], "outcome": a.get("outcome")} for a in sans]
    return res


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--lab", required=True)
    ap.add_argument("--sessions", required=True)
    ap.add_argument("--out", required=True)
    ap.add_argument("--jusqua", type=int, default=None)
    a = ap.parse_args()
    conf = json.load(open(a.sessions, encoding="utf-8"))
    res = mesurer(a.lab, conf, a.jusqua)
    if os.path.abspath(a.out).startswith(os.path.abspath(a.lab)):
        raise SystemExit("refus : la sortie ne doit pas être dans le dossier de la Lab")
    with open(a.out, "w", encoding="utf-8") as f:
        json.dump(res, f, ensure_ascii=False, indent=1)
    for s in res["sessions"]:
        print(f"{s['id']:7} {s.get('modele','')!s:16} appels={s['appels']} "
              + (f"durée={s['dureeMin']} min, {s['appelsParMinute']}/min, échecs={s['partEchecsPct']} %, outils={s['outilsDistincts']}, "
                 f"hyp_avant={s['hypotheseAvantToutEssai']} ({s['hypothesesDeclarees']}), réglages={s['reglagesEvaluesParMove']}, "
                 f"backtests={s['backtestsIsoles']}, sceaux={s['periodesScelleesOuvertes']} (incertains {s['ouverturesAttributionIncertaine']}), "
                 f"verdicts={s['verdictsParCode']}, abandon={s['abandonSansOuverture']}, écart_sceau={s['appelsJugeDisantSceauFermeAlorsQuOuvert']}"
                 if s['appels'] else ""))
    print(f"non attribués : {len(res['nonAttribues'])}")


if __name__ == "__main__":
    sys.exit(main())
