"""CAIN-A75-058 offline verifier (rules embedded verbatim). CAIN-A75-058 repeated-run framework comparison rules. Standard library only, no CAIN imports: embedded verbatim in the offline verifier.

Repeated runs say more than a demo, but only when the harness, model, prompts, tools and scoring are fixed and published;
otherwise the comparison measures the harness. The review recomputes a framework comparison study:

  harness      the harness hash equals SHA-256 of the canonical task list and rubric hash recomputed here; it was frozen by a
               named human who is not the vendor of any compared framework
  pins         every run used the pinned model id and version, temperature and tool versions, and the frozen harness, and
               ran after the freeze
  coverage     every framework ran every task exactly once in every repetition 1..R, with R >= the declared minimum; no run
               names an unknown framework or task
  results      per framework, the per-repetition pass rate, the mean and the sample standard deviation are recomputed; the
               published mean, standard deviation and run count must match, and the ranking must be by mean, highest first
  separation   a leader is "separated" only when its mean beats the runner-up by more than twice the larger standard error
               (stdev / sqrt(R)); a claimed winner inside that noise is refused
  raw          the published raw-results hash equals SHA-256 of the canonical run list
  claims       every boolean the study asserts matches what this review computed

Verdict: SOUND (the comparison is reproducible as recorded), else UNSOUND with every failing check named. Malformed input is
UNSOUND. It reviews the study record; it does not re-run any framework or judge whether the tasks are representative.
"""
from __future__ import annotations

import hashlib
import json
import math
import re

_ID = re.compile(r"^[A-Za-z0-9_.:\-/@]{1,120}$")
_HEX = re.compile(r"^[0-9a-f]{64}$")


class Bad(ValueError):
    pass


def canon_hash(obj):
    return hashlib.sha256(json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=True).encode()).hexdigest()


def harness_hash(harness):
    return canon_hash({"tasks": harness.get("tasks"), "rubric_sha256": harness.get("rubric_sha256")})


def _num(v, what, lo=0.0, hi=1e15):
    if isinstance(v, bool) or not isinstance(v, (int, float)) or not math.isfinite(v) or not (lo <= v <= hi):
        raise Bad(f"{what} must be a finite number in [{lo}, {hi}]")
    return float(v)


def _id(v, what):
    if not isinstance(v, str) or not _ID.match(v):
        raise Bad(f"{what} must be an id")
    return v


def _int(v, what, lo=1, hi=10000):
    if isinstance(v, bool) or not isinstance(v, int) or not (lo <= v <= hi):
        raise Bad(f"{what} must be an integer in [{lo}, {hi}]")
    return v


def evaluate(record):
    checks, summary = [], {}

    def c(cid, ok, detail=""):
        checks.append({"id": cid, "ok": bool(ok), "detail": detail})

    try:
        if not isinstance(record, dict) or record.get("schema") != "cain42.a75.frameworkeval.review.v1":
            raise Bad("not a cain42.a75.frameworkeval.review.v1 record")
        h = record.get("harness")
        if not isinstance(h, dict):
            raise Bad("harness must be an object")
        tasks = h.get("tasks")
        if not isinstance(tasks, list) or not tasks:
            raise Bad("harness.tasks must be a non-empty list")
        task_ids = []
        for t in tasks:
            if not isinstance(t, dict):
                raise Bad("a task must be an object")
            task_ids.append(_id(t.get("id"), "task.id"))
        if len(set(task_ids)) != len(task_ids):
            raise Bad("duplicate task id")
        frozen_at = _num(h.get("frozen_at"), "harness.frozen_at")
        hh = harness_hash(h)

        fws = record.get("frameworks")
        if not isinstance(fws, list) or len(fws) < 2:
            raise Bad("frameworks must list at least two frameworks")
        F = {}
        for f in fws:
            if not isinstance(f, dict):
                raise Bad("a framework must be an object")
            fid = _id(f.get("id"), "framework.id")
            if fid in F:
                raise Bad(f"duplicate framework {fid}")
            F[fid] = f
            c(f"framework.{fid}.version", isinstance(f.get("version"), str) and bool(f["version"]), f"version {f.get('version')!r}")

        c("harness.hash", h.get("sha256") == hh, f"stated hash matches recomputed {hh[:12]}")
        c("harness.rubric", isinstance(h.get("rubric_sha256"), str) and bool(_HEX.match(h["rubric_sha256"])), "rubric committed by SHA-256")
        fb = h.get("frozen_by")
        vendors = sorted({f.get("vendor") for f in F.values() if isinstance(f.get("vendor"), str)})
        c("harness.independent", isinstance(fb, str) and fb.startswith("human:") and len(fb) > 6
          and fb not in vendors and not any(isinstance(v, str) and fb.startswith(v + ".") for v in vendors),
          f"frozen by {fb!r}; framework vendors {vendors}")

        pins = record.get("pins") if isinstance(record.get("pins"), dict) else {}
        pm = pins.get("model") if isinstance(pins.get("model"), dict) else {}
        c("pins.model", isinstance(pm.get("id"), str) and bool(pm.get("version")), f"model {pm.get('id')!r} {pm.get('version')!r}")
        temp = pins.get("temperature")
        c("pins.temperature", isinstance(temp, (int, float)) and not isinstance(temp, bool) and math.isfinite(temp),
          f"temperature {temp!r}")
        tools = pins.get("tools") if isinstance(pins.get("tools"), dict) else None
        c("pins.tools", tools is not None and all(isinstance(v, str) and v for v in tools.values()), f"tool versions {tools!r}")

        R = _int(record.get("repetitions"), "repetitions")
        minR = _int(record.get("min_repetitions"), "min_repetitions")
        c("study.repetitions", R >= minR, f"{R} repetitions, minimum {minR}")

        runs = record.get("runs")
        if not isinstance(runs, list) or not runs:
            raise Bad("runs must be a non-empty list")
        seen, drift, early, unknown, bad_out = {}, {"harness": 0, "model": 0, "temperature": 0, "tools": 0}, 0, set(), 0
        for r in runs:
            if not isinstance(r, dict):
                raise Bad("a run must be an object")
            fid, tid, rep = r.get("framework"), r.get("task_id"), r.get("rep")
            if fid not in F or tid not in task_ids or isinstance(rep, bool) or not isinstance(rep, int) or not (1 <= rep <= R):
                unknown.add(f"{fid}/{tid}/{rep}")
                continue
            key = (fid, rep, tid)
            seen[key] = seen.get(key, 0) + 1
            m = r.get("model") if isinstance(r.get("model"), dict) else {}
            drift["harness"] += r.get("harness_sha256") != hh
            drift["model"] += m.get("id") != pm.get("id") or m.get("version") != pm.get("version")
            drift["temperature"] += r.get("temperature") != temp
            drift["tools"] += r.get("tools") != tools
            early += _num(r.get("at"), "run.at") < frozen_at
            bad_out += r.get("outcome") not in ("pass", "fail")
        for k, n in drift.items():
            c(f"runs.same_{k}", n == 0, f"{n} run(s) differ from the pinned {k}")
        c("runs.after_freeze", early == 0, f"{early} run(s) before the harness was frozen")
        c("runs.outcomes", bad_out == 0, f"{bad_out} run(s) with an outcome other than pass/fail")
        c("runs.known", not unknown, f"unknown framework/task/rep {sorted(unknown)[:5]}" if unknown else "every run names a known cell")
        for fid in F:
            missing = sum(1 for rep in range(1, R + 1) for t in task_ids if (fid, rep, t) not in seen)
            dup = sum(1 for rep in range(1, R + 1) for t in task_ids if seen.get((fid, rep, t), 0) > 1)
            c(f"coverage.{fid}", missing == 0 and dup == 0, f"{missing} missing, {dup} duplicated of {R * len(task_ids)} cells")

        stats = {}
        for fid in F:
            rates = []
            for rep in range(1, R + 1):
                cell = [r for r in runs if isinstance(r, dict) and r.get("framework") == fid and r.get("rep") == rep
                        and r.get("task_id") in task_ids]
                rates.append(sum(1 for r in cell if r.get("outcome") == "pass") / len(task_ids))
            mean = sum(rates) / R
            sd = math.sqrt(sum((x - mean) ** 2 for x in rates) / (R - 1)) if R > 1 else 0.0
            stats[fid] = {"mean": round(mean, 4), "stdev": round(sd, 4), "se": round(sd / math.sqrt(R), 4),
                          "runs": sum(1 for r in runs if isinstance(r, dict) and r.get("framework") == fid)}

        pub = record.get("published") if isinstance(record.get("published"), dict) else {}
        pres = pub.get("results") if isinstance(pub.get("results"), dict) else {}
        for fid, s in stats.items():
            p = pres.get(fid) if isinstance(pres.get(fid), dict) else {}
            ok = all(isinstance(p.get(k), (int, float)) and not isinstance(p.get(k), bool) and abs(p[k] - s[k]) <= 1e-4
                     for k in ("mean", "stdev")) and p.get("runs") == s["runs"]
            c(f"published.{fid}", ok, f"stated mean {p.get('mean')!r} sd {p.get('stdev')!r} runs {p.get('runs')!r}; "
                                      f"recomputed {s['mean']} / {s['stdev']} / {s['runs']}")
        order = sorted(stats, key=lambda k: (-stats[k]["mean"], k))
        rank = pub.get("ranking")
        c("published.ranking", isinstance(rank, list) and [stats[k]["mean"] for k in rank if k in stats] ==
          [stats[k]["mean"] for k in order] and sorted(map(str, rank)) == sorted(stats), f"stated {rank!r}, by mean {order}")
        raw = canon_hash(runs)
        c("published.raw_results", pub.get("raw_results_sha256") == raw, f"raw results hash recomputed {raw[:12]}")

        a, b = stats[order[0]], stats[order[1]]
        margin = a["mean"] - b["mean"]
        noise = 2 * max(a["se"], b["se"])
        separated = margin > noise + 1e-9

        def all_ok(pred):
            return all(x["ok"] for x in checks if pred(x["id"]))

        computed = {"harness_frozen": all_ok(lambda i: i.startswith("harness.") or i == "runs.after_freeze"),
                    "versions_pinned": all_ok(lambda i: i.startswith(("pins.", "runs.same_", "framework."))),
                    "repetitions_sufficient": all_ok(lambda i: i == "study.repetitions" or i.startswith("coverage.")),
                    "variance_reported": all_ok(lambda i: i.startswith("published.") and i not in ("published.ranking", "published.raw_results")),
                    "raw_results_published": all_ok(lambda i: i == "published.raw_results"),
                    "leader_separated": separated}
        claims = record.get("claims") if isinstance(record.get("claims"), dict) else {}
        for k in sorted(claims):
            c(f"claims.{k}", k in computed and claims[k] == computed[k], f"claimed {claims[k]!r}, computed {computed.get(k)!r}")
        summary.update({"computed": computed, "stats": stats, "ranking": order, "harness_sha256": hh,
                        "leader": order[0], "margin": round(margin, 4), "noise": round(noise, 4)})
    except Bad as e:
        checks.append({"id": "input.well_formed", "ok": False, "detail": str(e)})
        return "UNSOUND", checks, summary
    except Exception as e:  # noqa: BLE001
        checks.append({"id": "input.error", "ok": False, "detail": f"{type(e).__name__}: {e}"[:200]})
        return "UNSOUND", checks, summary
    return ("SOUND" if all(x["ok"] for x in checks) else "UNSOUND"), checks, summary


# ---------------------------------------------------------------------------- offline verifier (no CAIN imports)
# Every name below is prefixed _kit_verifier_ / _b64 / _hl / _json / _sys so it cannot shadow a name in the rules above.
import base64 as _b64, hashlib as _hl, json as _json, sys as _sys
_KIT_VERIFIER_GENESIS = _hl.sha256(b"cain42.a75.frameworkeval.genesis").hexdigest()


def _kit_verifier_canon(o):
    return _json.dumps(o, sort_keys=True, separators=(",", ":"), ensure_ascii=True).encode()


def _kit_verifier_sha(o):
    return _hl.sha256(_kit_verifier_canon(o)).hexdigest()


def _kit_verifier_main(argv):
    from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey
    if len(argv) < 2:
        print("usage: verify_offline.py run.json --pinned-key BASE64"); return 2
    run = _json.load(open(argv[1]))
    pin = argv[argv.index("--pinned-key") + 1] if "--pinned-key" in argv else None
    problems, prev, recs = [], _KIT_VERIFIER_GENESIS, run.get("records") or []
    for i, r in enumerate(recs):
        body = {k: v for k, v in r.items() if k not in ("record_hash", "signature_b64")}
        if _kit_verifier_sha(body) != r.get("record_hash"): problems.append(f"record {i}: record_hash does not match")
        if body.get("subject_sha256") != _kit_verifier_sha(body.get("subject")): problems.append(f"record {i}: subject_sha256 does not match")
        if pin and r.get("public_key_b64") != pin: problems.append(f"record {i}: different key than the pinned one")
        try:
            Ed25519PublicKey.from_public_bytes(_b64.b64decode(pin or r.get("public_key_b64"))).verify(
                _b64.b64decode(r.get("signature_b64", "")), str(r.get("record_hash")).encode())
        except Exception:
            problems.append(f"record {i}: signature does not verify")
        if r.get("prev_record_hash") != prev: problems.append(f"record {i}: chain link broken")
        v, checks, _ = evaluate(r.get("subject"))
        if v != r.get("verdict") or checks != r.get("checks"):
            problems.append(f"record {i}: re-running the rules gives {v}, the record says {r.get('verdict')}")
        prev = r.get("record_hash")
    if run.get("run_hash") != _kit_verifier_sha([r.get("record_hash") for r in recs]): problems.append("run_hash does not match")
    for p in problems: print("  -", p)
    if not pin: print("WARNING: no --pinned-key given; the signature was checked against the key inside the file")
    print("VERDICT:", "VERIFIED" if not problems else "TAMPERED OR INVALID", f"(CAIN-A75-058, {len(recs)} record(s), verdicts {run.get('verdicts')})")
    return 0 if not problems else 1


if __name__ == "__main__":
    _sys.exit(_kit_verifier_main(_sys.argv))
