"""One immutable M2 v1.2 PPS draw per panel part; saved-count cost only.""" import argparse import csv import fcntl import hashlib import json import os from pathlib import Path import platform import resource import shutil import time from datetime import datetime, timezone import numpy as np PRECHECK_COMMIT = '81f56afaca38304a4b5f48921927f97669ebbac9' PANEL_SHA = 'c594110b017ad8c62a542e4f634ce2879ccd1c30bd818429e73c0910a57a1e69' COUNT_SHA = '21d11a0d975735ffb4cba355aea8abe0fdfe4f3ef7d7f586444c6ad8113be342' def sha(p): return hashlib.sha256(Path(p).read_bytes()).hexdigest() def now(): return datetime.now(timezone.utc).isoformat() def write(p, obj): with Path(p).open('x') as f: json.dump(obj, f, indent=2, ensure_ascii=False, allow_nan=False) f.write('\n') def main(): a = argparse.ArgumentParser() a.add_argument('--input', type=Path, default=Path('artifacts/s1-panel-precheck-2026-11-14')) a.add_argument('--output', type=Path, default=Path('artifacts/s1-sampled-draw-2026-11-14')) args = a.parse_args() source, out = args.input, args.output assert np.__version__ == '1.26.4', 'Frozen numerical environment required' assert sha(source/'panel.csv') == PANEL_SHA assert sha(source/'count_contract.json') == COUNT_SHA panel = list(csv.DictReader((source/'panel.csv').open())) assert len(panel) == 24 and len({r['source_key'] for r in panel}) == 24 assert [int(r['panel_index']) for r in panel] == list(range(24)) out.mkdir(exist_ok=False) with (out/'execution.lock').open('x') as lock: fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) started = time.monotonic() write(out/'started.json', dict(started_utc=now(), pid=os.getpid(), action='Generate 24 primary draws and sum existing counts; no O2/S1/training', slack_start='https://arcticnimbus.slack.com/archives/C0C78UFKGRM/p1794637933386369', duplicate_check='No existing output directory; exclusive creation and lock; no Python processes seen before start')) docs = ['docs/s1-m2-v1.2-draw-current-source-2026-11-14.md', 'docs/s1-m2-v1.2-draw-go-2026-11-14.txt'] contract = dict(version='M2 v1.2 sampled', frozen_before_first_draw_utc=now(), precheck_commit=PRECHECK_COMMIT, panel_sha256=PANEL_SHA, count_contract_sha256=COUNT_SHA, code_sha256=sha(__file__), code_path='tools/draw_s1_sampled.py', document_sha256={p: sha(p) for p in docs}, numpy=np.__version__, python=platform.python_version(), candidates='All original-pose nodes with d>0, ascending original tetra-submesh ID; raw VTU IDs strictly ascending in same order', distribution='iid with replacement, p=mass[candidates]/np.sum(mass[candidates], dtype=float64)', algorithm='Generator(PCG64(SeedSequence([2026111400, numeric_prefix]))).choice(candidate_ids, size=2000, replace=True, p=probabilities)', draw_count=2000, primary_seed=2026111400, permutation_seeds=list(range(2026111401,2026111406)), bootstrap_seed=2026111406, permutations_executed=False, bootstrap_executed=False, identity='panel_index, case, source_key, numeric_prefix preserved; one draw per part reused across all folds', fold_roles=None, fold_status='Actual fold assignment not specified in current document; must freeze before scoring. Source isolation mandatory.', exact_visits='Python integer sum of saved neighbor_counts[i] over ALL distinct drawn IDs, once per ID, before O2 eligibility', common_eligible_draws=None, n_ref=None, eligibility_status='Unknown until O2; no prefilter except d>0', supersedes='Only historical v1.1 reference/query/rank/shortfall clauses; original panel and count files unchanged', full_source_field='All four-point source positions/masses and original nodal-gradient tensors, full-part R/t_part/R_g retained for future O2', future_per_field_wall_seconds=1800, future_per_field_rss_bytes=1342177280, timing_scenario_seconds_per_visit=0.73e-6, timing_scenario_is_bound=False, visit_alarm=500000000, visit_hard_cap=None, O2_executed=False, S1_scoring=False, training=False) write(out/'draw_contract.json', contract) shutil.copy2(source/'panel.csv', out/'panel.csv') shutil.copy2(source/'count_contract.json', out/'historical_count_contract.json') rows = [] for p in panel: t0 = time.monotonic() case = p['case']; src = source/case; dst = out/case; dst.mkdir() r = json.loads((src/'result.json').read_text()) assert r['status'] == 'COUNT_COMPLETE' for name, h in r['output_sha256'].items(): assert sha(src/name) == h, (case, name) with np.load(src/'counts.npz', allow_pickle=False) as z: mass = z['node_lumped_mass']; counts = z['neighbor_counts']; boundary = z['boundary_mask'] distance = np.load(src/'boundary_distance_raw.npy', allow_pickle=False) with np.load(src/'geometry.npz', allow_pickle=False) as z: raw_ids = z['raw_node_ids']; edges = z['edges'] assert np.array_equal(mass, z['node_volume_raw']) n = len(mass) assert n == int(p['tet_nodes']) == r['nodes'] assert mass.dtype == np.float64 and counts.dtype == np.int64 assert all(v.shape == (n,) for v in [mass, counts, distance, raw_ids, boundary]) assert np.isfinite(mass).all() and (mass > 0).all() assert np.isfinite(distance).all() and (distance >= 0).all() assert (counts >= 0).all() and np.array_equal(boundary, distance == 0) assert (np.diff(raw_ids) > 0).all() ids = np.flatnonzero(distance > 0) positive_mass = mass[ids].sum(dtype=np.float64) probabilities = mass[ids]/positive_mass prefix = int(p['numeric_prefix']) rng = np.random.Generator(np.random.PCG64(np.random.SeedSequence([2026111400, prefix]))) draws = rng.choice(ids, size=2000, replace=True, p=probabilities) unique, inverse, multiplicity = np.unique(draws, return_inverse=True, return_counts=True) assert np.array_equal(unique[inverse], draws) and int(multiplicity.sum()) == 2000 visits = sum(int(counts[i]) for i in unique) selected = np.zeros(n, bool); selected[unique] = True endpoint_mask = selected[edges] incident_edges = int(endpoint_mask.any(axis=1).sum()) directed_endpoints = int(endpoint_mask.sum()) max_neighbors = int(counts[unique].max()) # Full input vectors let a reviewer reproduce every draw and exact visit sum. np.savez_compressed(dst/'inputs.npz', node_lumped_mass=mass, neighbor_counts=counts, boundary_distance_raw=distance, raw_node_ids=raw_ids, candidate_ids=ids, candidate_probabilities=probabilities) np.savez_compressed(dst/'draw.npz', draw_ids=draws, draw_raw_ids=raw_ids[draws], unique_ids=unique, unique_raw_ids=raw_ids[unique], multiplicity=multiplicity, draw_to_unique=inverse, unique_mass=mass[unique], unique_probability=mass[unique]/positive_mass, unique_neighbor_counts=counts[unique]) with (dst/'draw_slots.csv').open('x') as f: w=csv.writer(f,lineterminator='\n');w.writerow(['draw_slot','node_id','raw_vtu_node_id']) w.writerows(zip(range(2000), draws.tolist(), raw_ids[draws].tolist())) with (dst/'unique.csv').open('x') as f: w=csv.writer(f,lineterminator='\n');w.writerow(['node_id','raw_vtu_node_id','multiplicity','mass','probability','neighbor_count']) w.writerows(zip(unique.tolist(),raw_ids[unique].tolist(),multiplicity.tolist(),mass[unique].tolist(),(mass[unique]/positive_mass).tolist(),counts[unique].tolist())) # Explicit conservative planning allowances, not measured RSS or a proven bound. s = int(r['four_point_sources']); u = len(unique) ledger = dict(measured_full_count_process_peak=r['peak_rss_bytes'], full_source_tensor_and_construction_allowance=144*s, full_raw_tensor_support_and_copy_allowance=160*n, full_query_kernel_scratch_allowance=512*max_neighbors, sampled_kernel_and_O2_readout_allowance=1024*u, saved_anchor_edge_endpoint_allowance=128*directed_endpoints, edge_processing_block_allowance=512*4096, allocator_IO_and_unmodelled_reserve=256*2**20) planned_rss=sum(ledger.values()) memory = dict(case=case, components_bytes=ledger, planning_total_bytes=planned_rss, budget_bytes=1342177280, below_budget_in_scenario=planned_rss <= 1342177280, scope='Existing full-source count peak is baseline (positions/masses/tree/topology included). Add full source tensors and one construction copy, full raw field, single-query scratch, sampled node and edge outputs, block temporaries and reserve. Conservative allowances may overlap. Not measured O2 RSS or a guaranteed bound.', implementation_constraints=['One field/process at a time; no parallel cases', 'Keep all source points and full raw field; no source thinning', 'Query one unique anchor at a time; never retain all radius-neighbor lists', 'O2 arrays indexed by unique anchors only; edge processing <=4096 directed endpoints/block', 'Use anchor B only for its incident edge directions; no neighbor-anchor integration', 'Write outputs before supervisor completion; scratch released between anchors']) write(dst/'memory_plan.json',memory) shutil.copy2(src/'result.json', dst/'precheck_result.json') receipt=dict(case=case, source_key=p['source_key'], numeric_prefix=prefix, panel_index=int(p['panel_index']), tertile=int(p['tertile']), status='DRAW_COMPLETE', seed_sequence=[2026111400,prefix], nodes=n, tets=int(p['tet_cells']), full_four_point_sources=s, positive_candidates=len(ids), draws=2000, unique_drawn=u, repeated_slots=2000-u, positive_mass=float(positive_mass), total_mass=float(mass.sum()), positive_mass_fraction=float(positive_mass/mass.sum()), radius=r['radius'], t_part=r['t_part'], exact_unique_visits=visits, repeated_slot_visits_diagnostic=sum(int(counts[i]) for i in draws), min_unique_neighbors=int(counts[unique].min()), max_unique_neighbors=max_neighbors, incident_undirected_edges=incident_edges, directed_anchor_edge_endpoints=directed_endpoints, future_kernel_scenario_0_73us_seconds=visits*0.73e-6, future_kernel_scenario_exceeds_1800s=visits*0.73e-6 > 1800, future_memory_planning_bytes=planned_rss, future_memory_scenario_exceeds_budget=planned_rss>1342177280, visit_alarm_5e8=visits>500000000, common_eligible_draws=None,n_ref=None, fold_roles=None, fold_draw_reuse=True, input_files_sha256=r['output_sha256'], precheck_result_sha256=sha(src/'result.json'), output_sha256={q.name:sha(q) for q in sorted(dst.iterdir()) if q.is_file()}, wall_seconds=time.monotonic()-t0, completed_utc=now(), new_distance_queries=0, new_neighbor_count_queries=0,new_kernel_evaluations=0,S1_scoring=False,training=False) write(dst/'receipt.json',receipt) rows.append({k:receipt[k] for k in ['panel_index','case','source_key','numeric_prefix','tertile','nodes','positive_candidates','draws','unique_drawn','repeated_slots','exact_unique_visits','max_unique_neighbors','incident_undirected_edges','directed_anchor_edge_endpoints','future_kernel_scenario_0_73us_seconds','future_kernel_scenario_exceeds_1800s','future_memory_planning_bytes','future_memory_scenario_exceeds_budget','visit_alarm_5e8','common_eligible_draws','n_ref']}) print(json.dumps(rows[-1]),flush=True) with (out/'summary.csv').open('x') as f: w=csv.DictWriter(f,fieldnames=list(rows[0]),lineterminator='\n');w.writeheader();w.writerows(rows) write(out/'completion.json',dict(status='COMPLETE',parts=len(rows),total_draws=48000, total_unique=sum(r['unique_drawn'] for r in rows),total_exact_unique_visits=sum(r['exact_unique_visits'] for r in rows), wall_seconds=time.monotonic()-started,process_peak_rss_bytes=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss*1024, completed_utc=now(),new_distance_queries=0,new_neighbor_count_queries=0,new_kernel_evaluations=0, S1_scoring=False,training=False,outputs_sha256={str(p.relative_to(out)):sha(p) for p in sorted(out.rglob('*')) if p.is_file() and p.name!='execution.lock'})) if __name__ == '__main__': main()