"""Independently reconstruct ordered PPS draws by inverse CDF and audit receipts.""" import argparse from collections import Counter import csv import hashlib import json from pathlib import Path import numpy as np def read(p): return json.loads(Path(p).read_text()) def sha(p): return hashlib.sha256(Path(p).read_bytes()).hexdigest() def main(): parser=argparse.ArgumentParser() parser.add_argument('output',type=Path) parser.add_argument('--precheck',type=Path) parser.add_argument('--report',type=Path) a=parser.parse_args();out=a.output contract=read(out/'draw_contract.json');completion=read(out/'completion.json') assert contract['primary_seed']==2026111400 and contract['bootstrap_seed']==2026111406 assert contract['permutation_seeds']==list(range(2026111401,2026111406)) assert sha(out/'panel.csv')==contract['panel_sha256'] assert sha(out/'historical_count_contract.json')==contract['count_contract_sha256'] assert sha('tools/draw_s1_sampled.py')==contract['code_sha256'] for name,h in contract['document_sha256'].items():assert sha(name)==h,name for name,h in completion['outputs_sha256'].items():assert sha(out/name)==h,name panel=list(csv.DictReader((out/'panel.csv').open())) summary=list(csv.DictReader((out/'summary.csv').open())) assert len(panel)==len(summary)==24 results=[] for p,srow in zip(panel,summary): case=p['case'];dest=out/case;r=read(dest/'receipt.json');old=read(dest/'precheck_result.json') assert sha(dest/'precheck_result.json')==r['precheck_result_sha256'] for name,h in r['output_sha256'].items():assert sha(dest/name)==h,(case,name) with np.load(dest/'inputs.npz',allow_pickle=False) as z:i={k:z[k] for k in z.files} with np.load(dest/'draw.npz',allow_pickle=False) as z:d={k:z[k] for k in z.files} mass=i['node_lumped_mass'];dist=i['boundary_distance_raw'];counts=i['neighbor_counts'];raw=i['raw_node_ids'] candidates=np.flatnonzero(dist>0);prob=mass[candidates]/mass[candidates].sum(dtype=np.float64) assert np.array_equal(candidates,i['candidate_ids']) and np.array_equal(prob,i['candidate_probabilities']) assert np.isfinite(prob).all() and (prob>0).all() and (np.diff(raw)>0).all() assert len(mass)==int(p['tet_nodes'])==old['nodes'] # NumPy's weighted choice consumes 2000 U[0,1) draws and searches the normalized CDF. rng=np.random.Generator(np.random.PCG64(np.random.SeedSequence([2026111400,int(p['numeric_prefix'])]))) cdf=np.cumsum(prob);cdf/=cdf[-1] replay=candidates[np.searchsorted(cdf,rng.random(2000),side='right')] assert np.array_equal(replay,d['draw_ids']) counter=Counter(map(int,replay));unique=np.array(sorted(counter),dtype=np.int64) multiplicity=np.array([counter[int(x)] for x in unique],dtype=np.int64) assert np.array_equal(unique,d['unique_ids']) and np.array_equal(multiplicity,d['multiplicity']) assert np.array_equal(unique[d['draw_to_unique']],replay) assert np.array_equal(raw[replay],d['draw_raw_ids']) and np.array_equal(raw[unique],d['unique_raw_ids']) assert np.array_equal(mass[unique],d['unique_mass']) assert np.array_equal(mass[unique]/mass[candidates].sum(),d['unique_probability']) assert np.array_equal(counts[unique],d['unique_neighbor_counts']) visits=sum(map(int,counts[unique])) assert visits==r['exact_unique_visits']==int(srow['exact_unique_visits']) assert sum(map(int,counts[replay]))==r['repeated_slot_visits_diagnostic'] assert r['unique_drawn']==len(unique) and r['draws']==2000 and int(multiplicity.sum())==2000 assert r['common_eligible_draws'] is None and r['n_ref'] is None and r['fold_roles'] is None assert r['source_key']==p['source_key'] and r['panel_index']==int(p['panel_index']) assert r['radius']==old['radius'] and r['t_part']==old['t_part'] assert r['future_kernel_scenario_0_73us_seconds']==visits*0.73e-6 for key in ['new_distance_queries','new_neighbor_count_queries','new_kernel_evaluations','S1_scoring','training']:assert not r[key] slots=list(csv.DictReader((dest/'draw_slots.csv').open())) assert [(int(x['draw_slot']),int(x['node_id']),int(x['raw_vtu_node_id'])) for x in slots]==list(zip(range(2000),map(int,replay),map(int,raw[replay]))) ucsv=list(csv.DictReader((dest/'unique.csv').open())) for x,node,c in zip(ucsv,unique,multiplicity): assert int(x['node_id'])==node and int(x['raw_vtu_node_id'])==raw[node] assert int(x['multiplicity'])==c and int(x['neighbor_count'])==counts[node] assert float(x['mass'])==mass[node] and float(x['probability'])==mass[node]/mass[candidates].sum() assert len(ucsv)==len(unique) plan=read(dest/'memory_plan.json');ledger=plan['components_bytes'] assert sum(ledger.values())==plan['planning_total_bytes']==r['future_memory_planning_bytes'] expected_ledger=dict(measured_full_count_process_peak=old['peak_rss_bytes'], full_source_tensor_and_construction_allowance=144*old['four_point_sources'], full_raw_tensor_support_and_copy_allowance=160*len(mass), full_query_kernel_scratch_allowance=512*int(counts[unique].max()), sampled_kernel_and_O2_readout_allowance=1024*len(unique), saved_anchor_edge_endpoint_allowance=128*r['directed_anchor_edge_endpoints'], edge_processing_block_allowance=512*4096,allocator_IO_and_unmodelled_reserve=256*2**20) assert ledger==expected_ledger assert sum(map(int,counts))==old['groups']['all']['neighbor_visits'] assert len(candidates)==old['groups']['positive_distance']['nodes'] assert sum(map(int,counts[candidates]))==old['groups']['positive_distance']['neighbor_visits'] if a.precheck: source=a.precheck/case assert sha(source/'result.json')==r['precheck_result_sha256'] for name,h in old['output_sha256'].items():assert sha(source/name)==h with np.load(source/'counts.npz',allow_pickle=False) as z: assert np.array_equal(z['node_lumped_mass'],mass) and np.array_equal(z['neighbor_counts'],counts) assert np.array_equal(np.load(source/'boundary_distance_raw.npy'),dist) with np.load(source/'geometry.npz',allow_pickle=False) as z: assert np.array_equal(z['raw_node_ids'],raw) edges=z['edges'];nodeset=set(map(int,unique)) degree=np.bincount(edges.ravel(),minlength=len(mass)) assert sum(map(int,degree[unique]))==r['directed_anchor_edge_endpoints'] assert sum(int(int(x) in nodeset or int(y) in nodeset) for x,y in edges)==r['incident_undirected_edges'] results.append(dict(case=case,status='PASS',ordered_draw_reproduced_by_inverse_CDF=True, unique_and_multiplicity_reproduced_by_Counter=True,unique=len(unique),exact_visits=visits, input_vectors_compared_to_original_precheck=bool(a.precheck),edge_counts_verified_against_original_topology=bool(a.precheck))) assert sum(r['unique'] for r in results)==completion['total_unique'] assert sum(r['exact_visits'] for r in results)==completion['total_exact_unique_visits'] fc=read(out/'fold_contract.json') assert sha(out/'fold_table.csv')==fc['fold_table_sha256'] assert sha('docs/s1-m2-v1.2-fold-confirmation-2026-11-14.txt')==fc['source_snapshot_sha256'] folds=list(csv.DictReader((out/'fold_table.csv').open())) assert len(folds)==24 and len({p['source_key'] for p in panel})==24 for f,p in zip(folds,panel): idx=int(p['panel_index']);training=[q for q in panel if q['panel_index']!=p['panel_index']] assert int(f['fold_index'])==int(f['test_panel_index'])==idx assert f['test_case']==p['case'] and f['test_source_key']==p['source_key'] assert int(f['test_numeric_prefix'])==int(p['numeric_prefix']) for column,key in [('train_panel_indices','panel_index'),('train_cases','case'),('train_source_keys','source_key'),('train_numeric_prefixes','numeric_prefix')]: assert f[column].split(';')==[q[key] for q in training] assert f['validation_split']=='none' identity_path=out/p['case']/'fold_identity.json';identity=read(identity_path) assert sha(identity_path)==fc['per_part_fold_identity_sha256'][p['case']] assert identity['test_fold']==idx and identity['train_folds']==[j for j in range(24) if j!=idx] assert identity['draw_sha256']==sha(out/p['case']/'draw.npz') assert identity['initial_receipt_sha256']==sha(out/p['case']/'receipt.json') assert identity['source_key']==p['source_key'] and identity['panel_index']==idx assert identity['draw_reused_across_all_roles'] and not identity['validation_folds'] report=dict(status='PASS',parts=results,total_unique=completion['total_unique'], total_exact_unique_visits=completion['total_exact_unique_visits'], scope='Draw replay, integer costs, saved arrays/CSV/receipts/hashes and memory ledger. No O2, S1, bootstrap, permutations, new distances or radius-neighbor queries.', original_precheck_compared=bool(a.precheck),numpy=np.__version__, folds_verified=24,per_part_test_roles=1,per_part_training_roles=23, historical_null_fold_fields_superseded_by_fold_contract=True) if a.report: with a.report.open('x') as f:json.dump(report,f,indent=2);f.write('\n') print(json.dumps({k:v for k,v in report.items() if k!='parts'},indent=2)) if __name__=='__main__':main()