#!/usr/bin/env python3
"""HLE batch 2 v2: DESIGN PILOT (exploratory, synthetic only, not a gate step).

Purpose: choose the extra planted-fraction rung for the v2 W ladder. Arm P only, level p_Q = 0.60,
f in {0.75, 0.90, 1.00}, 30 datasets each, dataset seeds 20262900+s (disjoint from every gate seed).
Rule fixed before running (PROGRESS.md 15:25): smallest f with P DISQUALIFIED in >= 27/30.
Imports the v1 files read-only (b2_sim.py a4a70f6b..., nulltest_paired.py 2fbbebca...); edits nothing.
"""
import json, os, sys, time, collections
V1 = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "code")
sys.path.insert(0, os.path.abspath(V1))
import b2_sim as S            # v1, unchanged; importing runs the nulltest pin check
import nulltest_paired as P

LEVEL, FS, NDS, SEED0 = 0.60, (0.75, 0.90, 1.00), 30, 20262900

def job(a):
    f, s = a
    items = S.generate(SEED0 + s, LEVEL)
    items, nq = S.plant(items, f, S.PLANT_SEEDS[s % 4])
    rep = P.evaluate_paired(S.make_pair_items(items), "text", "label", "id", None, alpha=0.025)
    wc = rep["worst_cell"]
    return {"f": f, "s": s, "n_planted": nq, "verdict": rep["verdict"], "adj_p": wc["adj_p"],
            "astar": wc["astar_pair"], "worst": "%s/%s" % (wc["null"], wc["stratum"]), "n_perm": rep["n_perm"]}

if __name__ == "__main__":
    out = os.path.join(os.path.dirname(os.path.abspath(__file__)), "pilot_P.jsonl")
    workers = int(sys.argv[1]) if len(sys.argv) > 1 else 14
    import multiprocessing as mp
    jobs = [(f, s) for f in FS for s in range(NDS)]
    t0 = time.time()
    with mp.get_context("fork").Pool(workers) as pool, open(out, "w") as fh:
        for r in pool.imap_unordered(job, jobs):
            fh.write(json.dumps(r, sort_keys=True) + "\n"); fh.flush()
    rows = [json.loads(l) for l in open(out)]
    summ = {}
    for f in FS:
        rr = [r for r in rows if r["f"] == f]
        summ[str(f)] = {"P_disqualified": sum(r["verdict"] == "DISQUALIFIED" for r in rr), "of": len(rr),
                        "astar_mean": round(sum(r["astar"] for r in rr) / len(rr), 4),
                        "worst_cells": dict(collections.Counter(r["worst"] for r in rr))}
    chosen = next((f for f in FS if summ[str(f)]["P_disqualified"] >= 27), None)
    res = {"script_sha256": __import__("hashlib").sha256(open(__file__, "rb").read()).hexdigest(),
           "level": LEVEL, "seeds": "%d+s, s=0..%d" % (SEED0, NDS - 1), "summary": summ,
           "rule": "smallest f with P >= 27/30", "chosen_f": chosen, "wall_s": round(time.time() - t0),
           "finished": time.strftime("%Y-%m-%d %H:%M:%S %Z")}
    json.dump(res, open(os.path.join(os.path.dirname(out), "PILOT_SUMMARY.json"), "w"), indent=1, sort_keys=True)
    print("PILOT DONE", json.dumps(res, sort_keys=True), flush=True)
