"""Audit extraction transport/structure, not medical truth or task accuracy.""" import ast import hashlib import json from pathlib import Path ROOT = Path(__file__).resolve().parent OUT = ROOT / 'miriad_public/refchecker_question_only_controls_v1' def main(): requests = [json.loads(s) for s in (OUT / 'requests20_local_only.jsonl').open()] rows = [json.loads(s) for s in (OUT / 'luna_extractions.jsonl').open()] by = {(r['id'], r['method']): r for r in rows if not r.get('error')} assert len(by) == len(rows) == len(requests) == 20 extracted = [] for r in requests: assert len(r['messages']) == 1 and r['evidence_layout'] == 'source_free_question_answer' assert r['context_sha256'] == hashlib.sha256(b'').hexdigest() assert hashlib.sha256(json.dumps(r['messages'], sort_keys=True).encode()).hexdigest() == r['messages_sha256'] result = by[r['id'], r['method']] claims, unparsed = [], [] for line in result['output'].splitlines(): if not line.strip(): continue try: triple = ast.literal_eval(line.strip()) assert isinstance(triple, tuple) and len(triple) == 3 assert all(isinstance(v, str) and v for v in triple) claims.append(list(triple)) except (ValueError, SyntaxError, AssertionError): unparsed.append(line) extracted.append({'id': r['id'], 'method': r['method'], 'claims': claims, 'unparsed_lines': unparsed, 'finish_reason': result['finish_reason'], 'authored_refusal_variant': r['method'] == 'abstain'}) short = next(r for r in extracted if (r['id'], r['method']) == ('independent_fact_omission', 'concise')) report = { 'complete': True, 'responses': len(rows), 'source_free_inputs_verified': len(requests), 'fully_parsed_responses': sum(bool(r['claims']) and not r['unparsed_lines'] for r in extracted), 'triples': sum(len(r['claims']) for r in extracted), 'length_truncated_responses': sum(r['finish_reason'] == 'length' for r in extracted), 'refusal_variants_with_triplets': sum(r['authored_refusal_variant'] and bool(r['claims']) for r in extracted), 'provider_logged_tokens': sum(r.get('usage', {}).get('total_tokens', 0) for r in rows), 'parser_deviation': 'ast.literal_eval of complete triple lines; does not execute the official eval-based parser. Unparsed lines retained. Unchanged official question-conditioned prompt, adapted Luna provider, not an official full RAGChecker run.', 'observed_concise_radiation_target_token_absent_from_all_triples': all('meningioma' not in ' '.join(t).casefold() for t in short['claims']), 'semantic_limits': 'Parsing does not certify condition binding or completeness. Concise radiation-category triples omit the question target meningiomas while explicit triples name it; this is a potential relation/target loss requiring separate checking, not an expert error label. All five refusals yield metaclaims, which are not medical assertions supported by a source. Do not count a refusal triplet as a correct medical answer or source entailment.', 'expert_reviews': 0, 'new_qa': 0, 'clinical_or_task_accuracy_established': False, } (OUT / 'extraction_structure_audit.json').write_text(json.dumps(report, indent=2) + '\n') (OUT / 'parsed_claims20_local_only.jsonl').write_text(''.join(json.dumps(r) + '\n' for r in extracted)) print(json.dumps(report)) if __name__ == '__main__': main()