#!/usr/bin/env python """Apply an already-computed contradiction/structural audit to a same-text, different-scoring-model labeled-pairs file (e.g. gsmic_adv_m9 scored with Qwen instead of gpt-4o-mini). The contradiction/structural audit (``audit_contradictions_and_filter.py``) only looks at ``base_text``/``twin_text`` -- it has nothing to do with which model produced ``delta_per_model``/``d_obs``. When a dataset was re-scored with a different backbone model but the underlying distractor-injection is byte-identical (same seed, same pipeline run), the audit verdict for a given twin_id carries over exactly -- no new LLM calls needed for the pairs that exist in both files. Only twin_ids present in the target file but missing from the already-audited source file get a fresh contradiction/structural check. Usage ----- python scripts/audit_crossmodel_reuse.py \\ --target-raw data/gsmic_adv_m9_qwen3p7plus_labeled_pairs.jsonl \\ --source-config gsmic_adv_m9_4omini \\ --out-dir data_audited """ from __future__ import annotations import argparse import json import os import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) sys.path.insert(0, str(Path(__file__).resolve().parent)) ROOT = Path(__file__).resolve().parents[1] def main() -> None: parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument("--target-raw", required=True, help="Path to the un-audited, different-backbone labeled-pairs file.") parser.add_argument("--source-config", required=True, help="Config name of the already-audited same-text dataset, e.g. gsmic_adv_m9_4omini " "(reads its data_audited/_labeled_pairs.jsonl + .audit_report.json).") parser.add_argument("--out-dir", default="data_audited") parser.add_argument("--model", default="gpt-4o-mini") parser.add_argument("--provider", default="openai") parser.add_argument("--concurrency", type=int, default=256) args = parser.parse_args() import noop_verification as noop import contradiction_audit_lib as lib from causal_difficulty.io_utils import read_jsonl, write_jsonl, read_dataset_meta from causal_difficulty.schemas import LabeledPair from causal_difficulty.llm_env import resolve_api_key, resolve_llm_base, resolve_model target_path = Path(args.target_raw) target_pairs = read_jsonl(target_path, LabeledPair) print(f"Loaded {len(target_pairs)} target pairs from {target_path}") out_dir = ROOT / args.out_dir source_kept_path = out_dir / f"{args.source_config}_labeled_pairs.jsonl" source_report_path = out_dir / f"{args.source_config}_labeled_pairs.audit_report.json" source_kept_ids = {p.twin_id for p in read_jsonl(source_kept_path, LabeledPair)} source_report = json.loads(source_report_path.read_text()) source_dropped_ids = {d["twin_id"] for d in source_report["dropped"]} print(f"Source audit ({args.source_config}): {len(source_kept_ids)} kept, " f"{len(source_dropped_ids)} dropped") known_kept: list[LabeledPair] = [] known_dropped_count = 0 needs_fresh: list[LabeledPair] = [] for p in target_pairs: if p.twin_id in source_kept_ids: known_kept.append(p) elif p.twin_id in source_dropped_ids: known_dropped_count += 1 else: needs_fresh.append(p) print(f"{len(known_kept)} inherit 'kept', {known_dropped_count} inherit 'dropped', " f"{len(needs_fresh)} need a fresh audit (twin_id not in source at all)") fresh_kept: list[LabeledPair] = [] fresh_dropped: list[dict] = [] if needs_fresh: is_math = args.source_config.startswith("math_adv") gold_answers = noop._load_gold_answers_math() if is_math else noop._load_gold_answers_gsmic() model = resolve_model(args.provider, args.model) api_key = resolve_api_key() resolve_llm_base() cache_dir = ROOT / "cache" / f"{target_path.stem}_crossmodel_reuse_audit" cache_dir.mkdir(parents=True, exist_ok=True) verdicts = lib.adjudicate_pairs( needs_fresh, gold_answers, model=model, provider=args.provider, api_key=api_key, cache_path=cache_dir / "llm_cache.json", concurrency=args.concurrency, ) for p in needs_fresh: gold = lib.gold_for(p.base_id, gold_answers) flags = lib.structural_flags(p.base_text, p.twin_text, gold) llm = verdicts.get(p.twin_id, {"verdict": "unparsed", "raw": None}) reasons = [] if llm["verdict"] == "no_gold": reasons.append("no_gold_answer") elif llm["verdict"] == "unparsed": reasons.append("unparsed_response") elif llm["verdict"] == "contradiction": reasons.append("llm_contradiction") if flags["any_drop_flag"]: for k in ("entity_collision", "gold_answer_collision", "multi_question_collision"): if flags[k]: reasons.append(k) if reasons: fresh_dropped.append({"base_id": p.base_id, "twin_id": p.twin_id, "reasons": reasons}) else: fresh_kept.append(p) kept = known_kept + fresh_kept n_dropped = known_dropped_count + len(fresh_dropped) print(f"Final: kept={len(kept)} dropped={n_dropped} (of {len(target_pairs)} total)") out_pairs_path = out_dir / target_path.name write_jsonl(out_pairs_path, kept) print(f"Wrote {len(kept)} audited pairs -> {out_pairs_path}") orig_meta = read_dataset_meta(target_path) audited_meta = dict(orig_meta) audited_meta.update({ "audited": True, "audit_script": "scripts/audit_crossmodel_reuse.py", "audit_reused_from": args.source_config, "n_pairs_before_audit": len(target_pairs), "n_pairs_after_audit": len(kept), "n_dropped": n_dropped, "n_inherited_kept": len(known_kept), "n_inherited_dropped": known_dropped_count, "n_fresh_audited": len(needs_fresh), "n_pairs": len(kept), }) meta_out_path = out_dir / f"{out_pairs_path.stem}.meta.json" meta_out_path.write_text(json.dumps(audited_meta, indent=2), encoding="utf-8") print(f"Wrote {meta_out_path}") report_path = out_dir / f"{out_pairs_path.stem}.audit_report.json" report_path.write_text(json.dumps({ "target_raw_path": str(target_path), "reused_from_config": args.source_config, "n_total": len(target_pairs), "n_kept": len(kept), "n_dropped": n_dropped, "fresh_dropped": fresh_dropped, }, indent=2), encoding="utf-8") print(f"Wrote {report_path}") if __name__ == "__main__": main()