"""Expand exact-input cached representatives without duplicating billed usage.""" import argparse import json from pathlib import Path BASE = Path(__file__).resolve().parent / 'miriad_public/graph_common_corpus_gpt_20261121/full4685/human511_retrieval_prospective_v1/local_readers_v1/deepseek' def main(): p = argparse.ArgumentParser() p.add_argument('--partial', action='store_true') args = p.parse_args() read = lambda f: [json.loads(s) for s in (BASE / f).open()] aliases = read('exact_input_aliases4088.jsonl') expected = {(r['representative_id'], r['representative_method']): r for r in aliases} representatives = {} failures = 0 for r in read('predictions3928_unique.jsonl'): if r.get('error'): failures += 1 continue key = r['id'], r['method'] assert key in expected and key not in representatives assert r['request_sha256'] == expected[key]['representative_request_sha256'] assert r['model_requested'] == r['model_returned'] == 'deepseek-v4.1-flash' representatives[key] = r if not args.partial: assert len(representatives) == len(expected) == 3928 rows = [] for a in aliases: key = a['representative_id'], a['representative_method'] if key not in representatives: continue original = representatives[key] assert original['request_sha256'] == a['logical_request_sha256'] r = {**original, 'id': a['id'], 'method': a['method'], 'exact_input_representative': list(key)} copied = key != (a['id'], a['method']) r['reused_exact_input_alias'] = copied if copied: r['usage'] = {'prompt_tokens': 0, 'completion_tokens': 0, 'total_tokens': 0, 'exact_input_alias_no_new_call': True} r['elapsed_seconds'] = 0 rows.append(r) assert len({(r['id'], r['method']) for r in rows}) == len(rows) tmp = BASE / 'predictions.jsonl.tmp' tmp.write_text(''.join(json.dumps(r) + '\n' for r in rows)) tmp.replace(BASE / 'predictions.jsonl') result = {'complete': len(rows) == 4088, 'unique_input_successes': len(representatives), 'logical_outputs': len(rows), 'expected_logical_outputs': 4088, 'failed_attempt_records': failures, 'exact_input_alias_copies': sum(r['reused_exact_input_alias'] for r in rows), 'preexisting_private_cache_outputs': sum(r.get('reused_exact_private_cache', False) for r in representatives.values()), 'fresh_successful_provider_calls': sum(not r.get('reused_exact_private_cache', False) and not r.get('usage', {}).get('local_cache_hit', False) for r in representatives.values()), 'new_model_calls_in_materialization': 0, 'clinical_accuracy_established': False, 'limits': 'Exact message/settings aliases are dependent copies, not independent runs. Cached original finish_reason was not retained; do not certify absence of length truncation. Unknown failed-attempt billing not inferred.'} (BASE / 'alias_materialization_audit.json').write_text(json.dumps(result, indent=2) + '\n') print(json.dumps(result)) if __name__ == '__main__': main()