Freeze corrected full-volume ordering diagnostic and one-time custody contract

This commit is contained in:
Frank14f committed 2026-11-15 17:22:20 +08:00
1 parent 6d907acc7c
commit d2adb6bcce
14 files changed
+2599

No files matched your search

+108
View File
@@ -0,0 +1,108 @@
"""One authorized full-node custody decode and D_w^2 graph approximation."""
import argparse
import json
import os
from pathlib import Path
import time
import traceback
from zipfile import ZipFile
from xml.etree import ElementTree as ET
import numpy as np
from audit_samples import decode_array
from freeze_g1_components import sha
from s2_custody_common import checked, read_json
from s2_recovery_supervisor import save
from naive_ordering_common import contract, rows
from naive_ordering_math import cdf_interval, squared_wall_distance, volume_clock_curve
def decode_full(archive, case):
stack=[];count=None;header=None
with ZipFile(archive) as z,z.open(case+'_sim.vtu') as stream:
for event,e in ET.iterparse(stream,events=('start','end')):
if event=='start':
stack.append(e.tag)
if e.tag=='VTKFile':
if e.get('byte_order')!='LittleEndian' or e.get('compressor')!='vtkZLibDataCompressor':
raise ValueError('Unsupported source encoding')
header=e.get('header_type','UInt32')
elif e.tag=='Piece':
if count is not None:raise ValueError('Multiple pieces')
count=int(e.get('NumberOfPoints'))
else:
if e.tag=='DataArray':
if stack[-2]=='PointData' and e.get('Name')=='Fill_time':
values=decode_array(e,header)
if values.shape!=(count,):raise ValueError('Full-node label length')
return values.copy()
e.clear()
stack.pop()
raise ValueError('No Fill_time array')
def run(root, index):
c=contract(root);row=rows(c)[index];prep=row['preparation'];source=Path(prep['preparation_root'])
dest=root/'parts'/f'part-{index:03d}'/'output'
dest.mkdir(exist_ok=False)
start=time.monotonic();cpu=time.process_time()
save(dest/'started.json',dict(pid=os.getpid(),case=row['case'],index=index,unix=time.time(),
contract_sha256=sha(root/'contract.json'),custody_authorization=c['latest_ruling']))
try:
for name in ['geometry.npz','r2.npz','queries.npz']:checked(source/name,prep['output_sha256'][name])
metadata=read_json(checked(source/'result.json',prep['result_sha256']))
if metadata['r2']['columns'][8]!='Dw':raise ValueError('Expected original wall-distance column')
with np.load(source/'geometry.npz',allow_pickle=False) as z:
x=z['points_raw'];edges=z['edges'];mass=z['node_volume_raw'];raw=z['raw_node_ids'];gate=z['gate_location_raw']
with np.load(source/'r2.npz',allow_pickle=False) as z:
dw=z['features'][:,8];df1=z['Df'];vf=z['Vf']
with np.load(source/'queries.npz',allow_pickle=False) as z:
mask=z['eligible_s2'];ids=z['anchor_ids'][mask];query_raw=z['raw_node_ids'][mask];counts=z['multiplicity'][mask]
if not np.array_equal(raw[ids],query_raw):raise ValueError('Query/raw-ID mismatch')
st=Path(c['archive']).stat();a=c['archive_stat']
if (st.st_size,st.st_mtime_ns,st.st_ino)!=(a['bytes'],a['mtime_ns'],a['inode']):raise ValueError('Archive identity changed')
# Exactly once per immutable attempt; save raw bytes even if validation later fails.
full=decode_full(c['archive'],row['case'])
np.savez_compressed(dest/'full-node-labels.npz',seconds=full,retained_tet_raw_ids=raw)
save(dest/'decode-receipt.json',dict(case=row['case'],decoded_arrays=['Fill_time'],decode_count=1,
full_VTU_nodes=len(full),retained_tet_nodes=len(raw),label_sha256=sha(dest/'full-node-labels.npz'),
original_S2_query_decode_preserved=True,custody_authorization=c['latest_ruling']))
if not np.isfinite(full).all() or np.any(full<0) or full.max()<=0:raise ValueError('Invalid full-node Fill_time; preserve raw cache, no retry')
if raw.ndim!=1 or np.any(np.diff(raw)<=0) or raw[0]<0 or raw[-1]>=len(full):raise ValueError('Full-node/tet indexing')
old={v['case']:v for v in read_json(checked(c['label_index'],c['label_index_sha256']))}[row['case']]
with np.load(checked(old['path'],old['sha256']),allow_pickle=False) as z:
if not np.array_equal(z['raw_ids'],query_raw) or not np.array_equal(z['counts'],counts) or not np.array_equal(z['seconds'],full[query_raw]):
raise ValueError('Full decode differs from immutable S2 query cache')
left1,right1=cdf_interval(df1,mass)
difference=float(np.max(np.abs(left1-vf)))
if difference>1e-12:raise ValueError('Existing B0 is not the expected full-volume strict D_w Dijkstra CDF')
save(dest/'progress.json',dict(stage='Dw_squared_graph',cpu_seconds=time.process_time()-cpu))
df2,gate_node,zero_edges=squared_wall_distance(x,edges,dw,gate)
left2,right2=cdf_interval(df2,mass)
leftT,rightT=cdf_interval(full[raw],mass)
np.savez_compressed(dest/'full-domain-maps.npz',raw_ids=raw,truth_left=leftT,truth_right=rightT,
df1_left=left1,df1_right=right1,df2_left=left2,df2_right=right2)
end=float(full.max());tet_end=float(full[raw].max());query_end=float(full[query_raw].max())
positive=float(mass[dw>0].sum()/mass.sum());K=int(counts.sum());alpha=positive*K/metadata['draw_slots']
curve=volume_clock_curve(full[raw],mass,end,prep['t_set'])
save(dest/'volume-clock-curve.json',curve)
save(dest/'result.json',dict(status='COMPLETE',case=row['case'],index=index,component_id=row['component_id'],
t_set=prep['t_set'],full_VTU_T_end=end,full_tet_T_end=tet_end,query_T_max=query_end,
raw_nodes=len(full),tet_nodes=len(raw),zero_mass_excluded_VTU_nodes=len(full)-len(raw),
full_vs_tet_end_difference=end-tet_end,eligible_slots=K,draw_slots=metadata['draw_slots'],
positive_mass_fraction=positive,estimated_alpha=alpha,alpha_is_estimate=True,
DKW_halfwidth_if_fixed_score_independent_PPS=float(np.sqrt(np.log(40)/(2*K))),
baseline_Dw_Vf_max_abs_difference=difference,Dw_solve_reused=True,Dw2_solver='tet-edge Dijkstra approximation',
zero_midpoint_edges_excluded=zero_edges,gate_node=gate_node,
cpu_seconds=time.process_time()-cpu,wall_seconds=time.monotonic()-start,
output_sha256={p.name:sha(p) for p in dest.glob('*') if p.is_file() and p.name not in ['progress.json']},
no_training=True,no_GPU=True,decode_count=1))
return 0
except Exception as exc:
save(dest/'failure.json',dict(type=type(exc).__name__,message=str(exc),traceback=traceback.format_exc(),
cpu_seconds=time.process_time()-cpu,wall_seconds=time.monotonic()-start,retry=False))
raise
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('root',type=Path);p.add_argument('index',type=int);a=p.parse_args()
raise SystemExit(run(a.root,a.index))