#!/usr/bin/env python3
"""Standalone verifier for the CAIN-42 VAGI Behavior Suite v1 bundle.

Imports no CAIN code. Reads only the bundle:
  commitment.json, tasks_public.json, results.json, rows.jsonl

It recomputes:
  * the suite commitment hash from the published task bodies;
  * every arm's per-domain accepted count and rate from rows.jsonl;
  * that every task appears once per arm (4 arms x 500 tasks = 2,000 rows);
  * that the summary in results.json matches the rows;
  * the >=80% per-domain target for the library arm.

It does NOT prove the tasks are a good benchmark, that the solver is honest,
or that the evidence was independently witnessed. It proves the published
numbers are internally consistent with the published rows.

    python3 verify_offline.py artifacts/vagi_suite
"""
from __future__ import annotations

import hashlib
import json
import sys
from pathlib import Path
from typing import Any, Dict, List, Tuple

DOMAINS = [
    "software_repair", "security_triage", "data_analysis", "business_decision",
    "science_calculation", "planning", "memory_retrieval", "tool_orchestration",
    "recovery", "governance",
]
ARMS = ("none", "fixed", "library", "governed")
TARGET = 0.80


def canon(o: Any) -> bytes:
    return json.dumps(o, sort_keys=True, separators=(",", ":"), ensure_ascii=True).encode()


def sha(o: Any) -> str:
    return hashlib.sha256(canon(o)).hexdigest()


def load(p: Path) -> Any:
    return json.loads(p.read_text(encoding="utf-8"))


def main(argv: List[str]) -> int:
    root = Path(argv[0] if argv else "artifacts/vagi_suite")
    problems: List[str] = []
    checks: List[Tuple[str, bool, str]] = []

    try:
        commit = load(root / "commitment.json")
        tasks = load(root / "tasks_public.json")
        results = load(root / "results.json")
        rows = [json.loads(x) for x in (root / "rows.jsonl").read_text(encoding="utf-8").splitlines() if x.strip()]
    except Exception as e:
        print(f"FAIL: cannot read bundle: {e}")
        return 1

    # 1. commitment
    recomputed = sha(tasks)
    ok = recomputed == commit.get("suite_sha256")
    checks.append(("commitment_suite_hash", ok, f"{recomputed[:16]}… vs {str(commit.get('suite_sha256'))[:16]}…"))
    if not ok:
        problems.append("suite_sha256 does not match the published task bodies")

    ok = len(tasks) == 500 and commit.get("tasks") == 500 and commit.get("domains") == 10
    checks.append(("task_count", ok, f"{len(tasks)} tasks, {commit.get('domains')} domains"))
    if not ok:
        problems.append("expected 500 tasks across 10 domains")

    # 2. row coverage
    ok = len(rows) == 4 * 500
    checks.append(("row_count", ok, f"{len(rows)} rows (expected 2000)"))
    if not ok:
        problems.append("rows.jsonl does not contain 4 arms x 500 tasks")

    for arm in ARMS:
        ids = [r.get("task_id") for r in rows if r.get("arm") == arm]
        ok = len(ids) == 500 and len(set(ids)) == 500
        checks.append((f"arm_{arm}_coverage", ok, f"{len(ids)} rows, {len(set(ids))} unique"))
        if not ok:
            problems.append(f"{arm}: expected 500 unique task rows")

    # 3. recompute every per-domain count
    for arm in ARMS:
        summary = (results.get("arms") or {}).get(arm) or {}
        for d in DOMAINS:
            sub = [r for r in rows if r.get("arm") == arm and r.get("domain") == d]
            accepted = sum(1 for r in sub if r.get("accepted"))
            stated = (summary.get("per_domain") or {}).get(d) or {}
            ok = len(sub) == 50 and stated.get("accepted") == accepted and stated.get("n") == 50
            checks.append((f"{arm}_{d}", ok, f"{accepted}/50 vs stated {stated.get('accepted')}/{stated.get('n')}"))
            if not ok:
                problems.append(f"{arm}/{d}: rows say {accepted}/50, summary says {stated.get('accepted')}/{stated.get('n')}")

    # 4. per-domain target for the library arm
    lib = (results.get("arms") or {}).get("library") or {}
    met = [d for d, v in (lib.get("per_domain") or {}).items() if v.get("met")]
    checks.append(("library_domains_met", len(met) == 10, f"{len(met)}/10 domains >= {TARGET:.0%}"))
    if len(met) != 10:
        problems.append(f"library arm meets the target in only {len(met)}/10 domains")

    # 5. honest limits present
    limits = results.get("honest_limits") or []
    ok = any("not a public benchmark" in x.lower() for x in limits) and any("not agi" in x.lower() for x in limits)
    checks.append(("honest_limits", ok, f"{len(limits)} limit statements"))
    if not ok:
        problems.append("honest_limits must state that this is constructed and not AGI/VAGI")

    for name, ok, detail in checks:
        print(f"{'PASS' if ok else 'FAIL':4s} {name:34s} {detail}")
    print()
    if problems:
        print(f"VERDICT: FAIL ({len(problems)} problem(s))")
        for p in problems:
            print(f"  - {p}")
        return 1
    print(f"VERDICT: PASS — {len(checks)} checks; library {lib.get('accepted')}/{lib.get('n')} "
          f"({(lib.get('rate') or 0)*100:.1f}%), {len(met)}/10 domains met")
    print("Scope: internal consistency only. Constructed tasks; no external verifier; not AGI/VAGI.")
    return 0


if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))
