"""Load versioned paper Judge tasks and keep result scopes explicit.""" import hashlib import json from pathlib import Path J1_PROFILES = {"full": "judge.j1.strategy_appropriateness", "paper-filtered": "judge.j1.paper_filtered_261"} JUDGE_TYPES = {"j1": "J1_strategy_appropriateness", "j2": "J2_strategy_identification"} def load_judge_dataset(data_dir, task, j1_profile="full"): if task not in JUDGE_TYPES or j1_profile not in J1_PROFILES: raise ValueError("Choose paper j1/j2 and an explicit J1 profile") root = Path(data_dir) manifest = json.loads((root / "manifest.json").read_bytes()) key = J1_PROFILES[j1_profile] if task == "j1" else "judge.j2.strategy_identification" catalog = manifest["subsets"] if task == "j1" and j1_profile == "paper-filtered" else manifest["tasks"] entry = catalog[key] if entry["task_type"] != JUDGE_TYPES[task]: raise ValueError("Manifest task identity does not match the paper task") path = (root / entry["path"]).resolve() if root.resolve() not in path.parents: raise ValueError("Dataset path escapes the data directory") raw = path.read_bytes() if hashlib.sha256(raw).hexdigest() != entry["sha256"]: raise ValueError("Dataset hash differs from the release manifest") items = json.loads(raw) if len(items) != entry["record_count"] or len({x["task_id"] for x in items}) != len(items): raise ValueError("Dataset count or unique IDs do not match the release") labels = {"YES", "NO"} if task == "j1" else {"SHORTCUT", "COMPUTATION"} for item in items: if item["task_type"] != JUDGE_TYPES[task] or (item["ground_truth"] not in labels and not (item["ground_truth"] is None and item.get("label_source") == "pending")): raise ValueError("Dataset task type or answer vocabulary mismatch") if not isinstance(item.get("prompt"), str) or not item["prompt"]: raise ValueError("Missing Judge prompt") if task == "j2" and not isinstance(item.get("solution"), str): raise ValueError("Strategy identification requires the supplied solution") metadata = {"release_id": manifest["release_id"], "task_key": key, "sha256": entry["sha256"], "expected_items": len(items), "j1_profile": j1_profile if task == "j1" else None} return items, metadata def validate_existing_results(records, items, metadata): """Refuse silent reuse across different tasks, prompts, labels or subsets.""" lookup = {item["task_id"]: item for item in items} seen = set() for record in records: identifier = record["task_id"] if identifier in seen or identifier not in lookup: raise ValueError("Duplicate or out-of-scope result ID") seen.add(identifier) item = lookup[identifier] if record.get("dataset") != metadata: raise ValueError("Result dataset provenance differs; do not mix historical runs") if any(record.get(k) != item[k] for k in ("task_type", "prompt", "ground_truth")): raise ValueError("Result prompt or reference does not match the selected dataset")