"""Apply manifest/v2-PREREGISTRATION.md to a screen: keep candidates with 0.15 <= r <= 0.85, cap at 40 (at most 8 ev_contracts; a skill with fewer than 4 kept keeps all), order round-robin by skill. Usage: PYTHONPATH=. .venv/bin/python tools/select_v2.py runs/v2-screen-6x/records.jsonl [--trackio pooled.json] Prints a JSON report; writes nothing. --trackio: {task_id: [solved, episodes]} of v1 first-visit groups. """ import argparse import json from collections import defaultdict SKILLS = ["evidence", "compute", "transform", "repair", "constraints", "contracts"] PREFIX = {"ev": "evidence", "cmp": "compute", "tx": "transform", "fix": "repair", "plan": "constraints"} def skill(task_id: str) -> str: return "contracts" if task_id.startswith("ev_contracts") else PREFIX[task_id.split("_")[0]] def main() -> None: p = argparse.ArgumentParser() p.add_argument("records") p.add_argument("--candidates", default="manifest/v2-candidates.txt") p.add_argument("--trackio", default="") a = p.parse_args() cands = [ln.strip() for ln in open(a.candidates) if ln.strip() and not ln.startswith("#")] order = {t: i for i, t in enumerate(cands)} tally = defaultdict(lambda: [0, 0]) errors = defaultdict(int) for ln in open(a.records): r = json.loads(ln) if r["task_id"] not in order: continue if r.get("error"): errors[r["task_id"]] += 1 continue tally[r["task_id"]][0] += r["reward"] == 1.0 tally[r["task_id"]][1] += 1 if a.trackio: for t, (s, n) in json.load(open(a.trackio)).items(): tally[t][0] += s tally[t][1] += n rows = [] for t in cands: s, n = tally[t] r = s / n if n else None rows.append({"task_id": t, "skill": skill(t), "solved": s, "episodes": n, "rate": r, "errors": errors[t], "kept": r is not None and 0.15 <= r <= 0.85}) kept = sorted((x for x in rows if x["kept"]), key=lambda x: (abs(x["rate"] - 0.5), order[x["task_id"]])) by_skill = defaultdict(list) for x in kept: by_skill[x["skill"]].append(x) chosen = [] if len(kept) > 40: protected = [x for s in SKILLS for x in by_skill[s] if len(by_skill[s]) < 4] chosen = list(protected) n_contracts = sum(x["skill"] == "contracts" for x in chosen) for x in kept: if len(chosen) >= 40: break if x in chosen: continue if x["skill"] == "contracts": if n_contracts >= 8: continue n_contracts += 1 chosen.append(x) else: chosen = kept ids = {x["task_id"] for x in chosen} queues = {s: [x for x in kept if x["task_id"] in ids and x["skill"] == s] for s in SKILLS} final = [] while any(queues.values()): for s in SKILLS: if queues[s]: final.append(queues[s].pop(0)["task_id"]) print(json.dumps({"candidates": len(cands), "kept_in_band": len(kept), "selected": len(final), "freeze_allowed": len(final) >= 24, "per_skill": {s: sum(skill(t) == s for t in final) for s in SKILLS}, "order": final, "rows": rows}, indent=1)) if __name__ == "__main__": main()