|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +check_pool_consistency.py — Phase 4 entry gate. |
| 4 | +
|
| 5 | +Asserts UID-set equality between (a) the frozen `FINAL_POOL_LOCK.yaml` |
| 6 | +and (b) the actual round-3 adjudication TSV that feeds extraction. Blocks |
| 7 | +Phase 4 (data extraction) until the two agree. |
| 8 | +
|
| 9 | +Why this gate exists |
| 10 | +==================== |
| 11 | +Cross-project precedent (anonymized): an LLM reporting-quality SR carried |
| 12 | +five documents that disagreed on INCLUDE/EXCLUDE counts. Three EXCLUDE |
| 13 | +rows existed in the downstream extraction sheet without matching INCLUDE |
| 14 | +decisions. The drift traced to a post-freeze adjudication change that |
| 15 | +propagated to the extraction TSV but not the lock — or the other way |
| 16 | +around. Either direction is fatal at peer review. |
| 17 | +
|
| 18 | +The gate fails CLOSED: if the lock and the extraction sheet disagree on |
| 19 | +even one UID, extraction is blocked. |
| 20 | +
|
| 21 | +Inputs |
| 22 | +====== |
| 23 | +
|
| 24 | + --lock PATH FINAL_POOL_LOCK.yaml (Phase 3f.5 artifact) |
| 25 | + --adjudication-tsv PATH round3_adjudication.tsv (Phase 3c artifact) |
| 26 | + --decision-col NAME column holding the decision label |
| 27 | + (default: "round3_decision") |
| 28 | + --uid-col NAME column holding the UID (default: "uid") |
| 29 | + --include-labels LIST decisions counted as INCLUDE |
| 30 | + (default: "INCLUDE,INCLUDE_MIXED") |
| 31 | + --out PATH JSON report (default: qc/pool_consistency.json) |
| 32 | +
|
| 33 | +Output JSON |
| 34 | +=========== |
| 35 | +
|
| 36 | + { |
| 37 | + "submission_safe": false, |
| 38 | + "lock_include_n": 42, |
| 39 | + "tsv_include_n": 43, |
| 40 | + "in_lock_not_tsv": ["UID_007"], |
| 41 | + "in_tsv_not_lock": ["UID_055"], |
| 42 | + "match": false |
| 43 | + } |
| 44 | +
|
| 45 | +Exit codes |
| 46 | +========== |
| 47 | + 0 lock and TSV agree on the UID set |
| 48 | + 1 disagreement (PR T1-5 blocks extraction) |
| 49 | + 2 invocation error (missing files, missing columns) |
| 50 | +
|
| 51 | +Read-only script. No file modification. |
| 52 | +""" |
| 53 | + |
| 54 | +from __future__ import annotations |
| 55 | + |
| 56 | +import argparse |
| 57 | +import csv |
| 58 | +import json |
| 59 | +import sys |
| 60 | +from pathlib import Path |
| 61 | + |
| 62 | + |
| 63 | +def load_lock_uids(lock_path: Path, label_set: list[str]) -> set[str]: |
| 64 | + try: |
| 65 | + import yaml # type: ignore |
| 66 | + except ImportError: |
| 67 | + print( |
| 68 | + "ERROR: PyYAML required for --lock parsing. pip install PyYAML", |
| 69 | + file=sys.stderr, |
| 70 | + ) |
| 71 | + sys.exit(2) |
| 72 | + data = yaml.safe_load(lock_path.read_text(encoding="utf-8")) |
| 73 | + if not isinstance(data, dict): |
| 74 | + print(f"ERROR: lock file not a mapping: {lock_path}", file=sys.stderr) |
| 75 | + sys.exit(2) |
| 76 | + # INCLUDE_MIXED maps to mixed_uids in the lock template. |
| 77 | + uids: set[str] = set() |
| 78 | + if "INCLUDE" in label_set: |
| 79 | + uids.update(str(u) for u in (data.get("include_uids") or [])) |
| 80 | + if "INCLUDE_MIXED" in label_set: |
| 81 | + uids.update(str(u) for u in (data.get("mixed_uids") or [])) |
| 82 | + if "MIXED" in label_set: |
| 83 | + uids.update(str(u) for u in (data.get("mixed_uids") or [])) |
| 84 | + if "EXCLUDE" in label_set: |
| 85 | + uids.update(str(u) for u in (data.get("exclude_uids") or [])) |
| 86 | + return uids |
| 87 | + |
| 88 | + |
| 89 | +def load_tsv_uids( |
| 90 | + tsv_path: Path, |
| 91 | + decision_col: str, |
| 92 | + uid_col: str, |
| 93 | + label_set: set[str], |
| 94 | +) -> set[str]: |
| 95 | + # Allow .tsv or .csv (sniff by extension). |
| 96 | + delim = "," if tsv_path.suffix.lower() == ".csv" else "\t" |
| 97 | + with tsv_path.open("r", encoding="utf-8", newline="") as fh: |
| 98 | + reader = csv.DictReader(fh, delimiter=delim) |
| 99 | + if reader.fieldnames is None: |
| 100 | + print(f"ERROR: empty TSV: {tsv_path}", file=sys.stderr) |
| 101 | + sys.exit(2) |
| 102 | + if uid_col not in reader.fieldnames: |
| 103 | + print( |
| 104 | + f"ERROR: uid column {uid_col!r} not in TSV columns " |
| 105 | + f"{reader.fieldnames!r}", |
| 106 | + file=sys.stderr, |
| 107 | + ) |
| 108 | + sys.exit(2) |
| 109 | + if decision_col not in reader.fieldnames: |
| 110 | + print( |
| 111 | + f"ERROR: decision column {decision_col!r} not in TSV columns " |
| 112 | + f"{reader.fieldnames!r}", |
| 113 | + file=sys.stderr, |
| 114 | + ) |
| 115 | + sys.exit(2) |
| 116 | + uids: set[str] = set() |
| 117 | + for row in reader: |
| 118 | + decision = (row.get(decision_col) or "").strip() |
| 119 | + if decision in label_set: |
| 120 | + uid = (row.get(uid_col) or "").strip() |
| 121 | + if uid: |
| 122 | + uids.add(uid) |
| 123 | + return uids |
| 124 | + |
| 125 | + |
| 126 | +def main(argv: list[str] | None = None) -> int: |
| 127 | + parser = argparse.ArgumentParser( |
| 128 | + description=( |
| 129 | + "Phase 4 entry gate: asserts UID-set equality between the frozen " |
| 130 | + "FINAL_POOL_LOCK.yaml and the round-3 adjudication TSV." |
| 131 | + ) |
| 132 | + ) |
| 133 | + parser.add_argument("--lock", type=Path, required=True) |
| 134 | + parser.add_argument("--adjudication-tsv", type=Path, required=True) |
| 135 | + parser.add_argument("--decision-col", default="round3_decision") |
| 136 | + parser.add_argument("--uid-col", default="uid") |
| 137 | + parser.add_argument( |
| 138 | + "--include-labels", |
| 139 | + default="INCLUDE,INCLUDE_MIXED", |
| 140 | + help="Comma-separated decision labels counted as included.", |
| 141 | + ) |
| 142 | + parser.add_argument("--out", type=Path, default=Path("qc/pool_consistency.json")) |
| 143 | + parser.add_argument("--quiet", action="store_true") |
| 144 | + args = parser.parse_args(argv) |
| 145 | + |
| 146 | + if not args.lock.is_file(): |
| 147 | + print(f"ERROR: lock not found: {args.lock}", file=sys.stderr) |
| 148 | + return 2 |
| 149 | + if not args.adjudication_tsv.is_file(): |
| 150 | + print(f"ERROR: TSV not found: {args.adjudication_tsv}", file=sys.stderr) |
| 151 | + return 2 |
| 152 | + |
| 153 | + labels = [s.strip() for s in args.include_labels.split(",") if s.strip()] |
| 154 | + label_set = set(labels) |
| 155 | + lock_uids = load_lock_uids(args.lock, labels) |
| 156 | + tsv_uids = load_tsv_uids( |
| 157 | + args.adjudication_tsv, args.decision_col, args.uid_col, label_set |
| 158 | + ) |
| 159 | + |
| 160 | + in_lock_only = sorted(lock_uids - tsv_uids) |
| 161 | + in_tsv_only = sorted(tsv_uids - lock_uids) |
| 162 | + match = not in_lock_only and not in_tsv_only |
| 163 | + |
| 164 | + report = { |
| 165 | + "submission_safe": match, |
| 166 | + "match": match, |
| 167 | + "lock_include_n": len(lock_uids), |
| 168 | + "tsv_include_n": len(tsv_uids), |
| 169 | + "in_lock_not_tsv": in_lock_only, |
| 170 | + "in_tsv_not_lock": in_tsv_only, |
| 171 | + "include_labels": labels, |
| 172 | + } |
| 173 | + args.out.parent.mkdir(parents=True, exist_ok=True) |
| 174 | + args.out.write_text(json.dumps(report, indent=2), encoding="utf-8") |
| 175 | + |
| 176 | + if not args.quiet: |
| 177 | + if match: |
| 178 | + print(f"PASS: lock and TSV agree ({len(lock_uids)} UIDs).") |
| 179 | + else: |
| 180 | + print( |
| 181 | + f"FAIL: lock includes {len(lock_uids)} UIDs, TSV includes " |
| 182 | + f"{len(tsv_uids)} UIDs." |
| 183 | + ) |
| 184 | + if in_lock_only: |
| 185 | + print(f" In lock but not TSV ({len(in_lock_only)}):") |
| 186 | + for u in in_lock_only[:10]: |
| 187 | + print(f" - {u}") |
| 188 | + if len(in_lock_only) > 10: |
| 189 | + print(f" ... and {len(in_lock_only) - 10} more") |
| 190 | + if in_tsv_only: |
| 191 | + print(f" In TSV but not lock ({len(in_tsv_only)}):") |
| 192 | + for u in in_tsv_only[:10]: |
| 193 | + print(f" - {u}") |
| 194 | + if len(in_tsv_only) > 10: |
| 195 | + print(f" ... and {len(in_tsv_only) - 10} more") |
| 196 | + |
| 197 | + return 0 if match else 1 |
| 198 | + |
| 199 | + |
| 200 | +if __name__ == "__main__": |
| 201 | + sys.exit(main()) |
0 commit comments