#!/usr/bin/env python3 """Rebuild the held-out test split from the writer/reviewer records. A case is kept only if its contract is valid, the blind reviewer gave the same label as the writer, and the reviewer was confident. Run from the root of the Canary repository (it imports app.py only to validate contracts; no verdict is computed here): python hf/dataset/scripts/assemble_heldout.py hf/dataset/provenance/heldout_writer_reviewer.json /tmp/test.jsonl python hf/dataset/scripts/flatten.py /tmp/test.jsonl """ import collections import json import logging import re import sys JA = re.compile(r"[぀-ヿ㐀-鿿]") def main() -> None: sys.path.insert(0, ".") logging.disable(logging.CRITICAL) import app records = json.load(open(sys.argv[1], encoding="utf-8")) kept, dropped = [], collections.Counter() for rec in records: c, r = rec["writer"], rec["reviewer"] contract = dict(c["contract"]) contract.setdefault("type", c["contract_type"]) if r.get("label") is None: dropped["no review"] += 1 elif contract.get("type") != c["contract_type"]: dropped["type mismatch"] += 1 elif not app.validate_contracts([dict(contract, id="C", severity="critical")])["valid"]: dropped["invalid contract"] += 1 elif c["contract_type"] == "stable" and not isinstance(c["output"], list): dropped["stable without samples list"] += 1 elif r["label"] != c["label"]: dropped["writer and reviewer disagree"] += 1 elif not r.get("confident"): dropped["reviewer not confident"] += 1 else: text = json.dumps([c["output"], c["case"]], ensure_ascii=False) kept.append({ "contract_type": c["contract_type"], "contract": {k: v for k, v in contract.items() if k not in ("id", "severity")}, "output": c["output"], "case": c["case"] or {}, "label": c["label"], "lang": "ja" if JA.search(text) else "en", "category": c["category"], "difficulty": c["difficulty"], "rationale": c["rationale"], "source": f"held-out writer:{rec['group']}, blind reviewer agreed", }) with open(sys.argv[2], "w", encoding="utf-8") as fh: for i, row in enumerate(kept, 1): fh.write(json.dumps({"id": f"test-{i:04d}", **row}, ensure_ascii=False) + "\n") print("records", len(records), "kept", len(kept), "dropped", dict(dropped)) if __name__ == "__main__": main()