"""Packed per-UID disposition sidecar for the S3 target/transcript audit. The original worklist and audio stay immutable. Every audited suspect gets an explicit disposition, including `verified_keep` for a false-positive flag. An absent UID is an unaudited clean row and keeps its source text. """ from __future__ import annotations import json from collections import Counter from pathlib import Path import pyarrow.parquet as pq from build_worklist import EXPECTED_ELIGIBLE, OUT, sha ROOT = OUT.parent RESOLUTION_DIR = ROOT / 'target_text_resolution_v1' RESOLUTION_MANIFEST = RESOLUTION_DIR / 'MANIFEST.json' def load_resolution(): spec = json.loads(RESOLUTION_MANIFEST.read_text()) assert spec['status'] == 'PASS' and spec['version'] == 'hvs-s3-target-text-v1' assert spec['source_worklist_sha256'] == sha(OUT / 'MANIFEST.json') assert spec['accepted_rows'] + spec['excluded_rows'] == EXPECTED_ELIGIBLE assert spec['qa_status'] == 'PASS' qa_path = RESOLUTION_DIR / spec['qa_file'] assert qa_path.parent == RESOLUTION_DIR and sha(qa_path) == spec['qa_sha256'] qa = json.loads(qa_path.read_text()) assert qa['status'] == 'PASS' and qa['sampled_rows'] >= 128 render_path = RESOLUTION_DIR / spec['render_qa_file'] assert render_path.parent == RESOLUTION_DIR and sha(render_path) == spec['render_qa_sha256'] render_qa = json.loads(render_path.read_text()) assert render_qa['status'] == 'PASS' and render_qa['sampled_rows'] >= 128 assert spec['unresolved_impossible_rows'] == 0 assert spec['audit_report_sha256'] == sha(ROOT / 'target_text_audit_v1' / 'REPORT.json') path = RESOLUTION_DIR / spec['changes_file'] assert path.parent == RESOLUTION_DIR and sha(path) == spec['changes_sha256'] rows = pq.read_table(path, columns=['uid', 'action', 'text']).to_pylist() assert len(rows) == spec['changed_rows'] changes = {} actions = Counter() for row in rows: uid, action, text = row['uid'], row['action'], row['text'] assert uid and uid not in changes assert action in ('relabel', 'burst_only', 'exclude', 'verified_keep') actions[action] += 1 if action in ('exclude', 'verified_keep'): assert not text else: assert text and text.strip() changes[uid] = (action, text) assert actions['exclude'] == spec['excluded_rows'] assert dict(actions) == spec['action_rows'] return spec, changes def apply_to_rows(rows, changes): kept = [] for row in rows: change = changes.get(row['uid']) if change is None: kept.append(row) elif change[0] == 'verified_keep': kept.append(row) elif change[0] in ('relabel', 'burst_only'): row['_resolved_text'] = change[1] row['_resolution_action'] = change[0] kept.append(row) return kept