Seal the locked 120-pair sample and prepare bounded blind geometry export

This commit is contained in:
Frank14f committed 2026-11-14 19:29:47 +08:00
1 parent 24d2da0fe7
commit 6d61c08686
16 files changed
+483

No files matched your search

+34
View File
@@ -0,0 +1,34 @@
"""One supervised extraction/render task, addressed by sealed-list ordinal."""
from pathlib import Path
from zipfile import ZipFile
import hashlib,json,sys,time,resource
import numpy as np
from g1_registration_cost import geometry_only
from g1_blind_geometry import surface,ply_bytes
from render_g1_blind_surface import render_surface
ROOT=Path('artifacts/s2-g1-blind120-2026-11-14')
def main():
start=time.monotonic();index=int(sys.argv[1]);c=json.loads((ROOT/'extraction-contract.json').read_text())
for p,h in c['source_sha256'].items():
if hashlib.sha256(Path(p).read_bytes()).hexdigest()!=h:raise ValueError('Extraction code changed')
mapping=json.loads((ROOT/'custodian/sampling-and-mapping.json').read_text());case=mapping['unique_cases'][index]
asset=hashlib.sha256(bytes.fromhex(mapping['anonymous_id_secret_hex'])+b'asset\0'+case.encode()).hexdigest()[:32]
out=ROOT/'custodian/geometry'/asset;out.mkdir(parents=True,exist_ok=False)
st=Path('data.zip').stat();verification=json.loads(Path('artifacts/intake-2026-11-13/archive_verification.json').read_text())
if (st.st_size,st.st_mtime_ns,st.st_ino)!=(verification['bytes'],verification['mtime_ns'],verification['inode']):raise ValueError('Archive provenance changed')
entries=json.loads((ROOT/'custodian/geometry-source-index.json').read_text())['cases'];entry=next(e for e in entries if e['case']==case)
usable=[e for e in entry['existing_surface_caches'] if not e['contains_field_labels']]
if usable:
e=usable[0];p=Path(e['path']);stat=p.stat()
if (stat.st_size,stat.st_mtime_ns)!=(e['bytes'],e['mtime_ns']):raise ValueError('Cache changed since inventory')
with np.load(p,allow_pickle=False) as z:x=z[e['surface_coordinate_key']];f=z[e['surface_index_key']]
source_mode='existing geometry cache, coordinate and boundary arrays only'
else:
with ZipFile('data.zip') as z:
with z.open(case+'_sim.vtu') as stream:g=geometry_only(stream)
x=g['points'];f=g['boundary'];source_mode='geometry prefix only, stop before PointData/CellData'
x,f=surface(x,f);(out/'surface.ply').write_bytes(ply_bytes(x,f));render_surface(x,f,out)
receipt={'index':index,'asset_id':asset,'source_mode':source_mode,'vertices':len(x),'triangles':len(f),'files':[{'name':p.name,'bytes':p.stat().st_size,'sha256':hashlib.sha256(p.read_bytes()).hexdigest()} for p in sorted(out.iterdir())],'wall_seconds':time.monotonic()-start,'cpu_seconds':sum(resource.getrusage(resource.RUSAGE_SELF)[:2]),'peak_rss_bytes':resource.getrusage(resource.RUSAGE_SELF).ru_maxrss*1024,'registration':False,'labels_read':False,'geometry_modified':'interior unused vertices omitted; numerical coordinate values and boundary triangles preserved; view centering only in PNG'}
(out/'receipt.json').write_text(json.dumps(receipt,indent=2)+'\n');print(json.dumps({k:receipt[k] for k in ['index','vertices','triangles','wall_seconds','cpu_seconds','peak_rss_bytes']}))
if __name__=='__main__':main()
+55
View File
@@ -0,0 +1,55 @@
"""Geometry-only blind-pair exporter. No dataset access, sampling or source IDs."""
from pathlib import Path
import hashlib,json,re
import numpy as np
UNITS='Original numerical coordinate units; source VTU carries no physical-unit tag. No unit conversion, fitting, registration, scaling or simplification applied.\n'
def surface(points, triangles):
x=np.asarray(points);f=np.asarray(triangles)
if x.ndim!=2 or x.shape[1]!=3 or not len(x) or not np.issubdtype(x.dtype,np.number) or not np.isfinite(x).all():raise ValueError('Invalid coordinates')
if f.ndim!=2 or f.shape[1]!=3 or not len(f) or not np.issubdtype(f.dtype,np.integer):raise ValueError('Invalid triangles')
if f.min()<0 or f.max()>=len(x):raise ValueError('Triangle index outside coordinate table')
if np.any(np.diff(np.sort(f,axis=1),axis=1)==0):raise ValueError('Repeated vertex in triangle')
used=np.unique(f);return x[used].astype(np.float64),np.searchsorted(used,f)
def ply_bytes(x,f):
header=f'ply\nformat ascii 1.0\nelement vertex {len(x)}\nproperty double x\nproperty double y\nproperty double z\nelement face {len(f)}\nproperty list uchar int vertex_indices\nend_header\n'
vertices=''.join(' '.join(format(float(v),'.17g') for v in row)+'\n' for row in x)
faces=''.join('3 '+' '.join(str(int(v)) for v in row)+'\n' for row in f)
return (header+vertices+faces).encode('ascii')
def write_pair(parent, anonymous_id, left, right):
if not re.fullmatch(r'[0-9a-f]{32}',anonymous_id):raise ValueError('Anonymous ID must be opaque 128-bit hex')
if len(left)!=2 or len(right)!=2:raise ValueError('Only coordinates and triangles accepted')
a=surface(*left);b=surface(*right)
payload={'left.ply':ply_bytes(*a),'right.ply':ply_bytes(*b),'units.txt':UNITS.encode('ascii')}
root=Path(parent)/anonymous_id;root.mkdir(parents=True,exist_ok=False)
for name,data in payload.items():(root/name).write_bytes(data)
manifest={'format':'blind-surface-pair-v1','anonymous_pair_id':anonymous_id,
'files':[{'name':name,'bytes':len(data),'sha256':hashlib.sha256(data).hexdigest()} for name,data in sorted(payload.items())]}
(root/'manifest.json').write_text(json.dumps(manifest,indent=2)+'\n')
return root
def validate_pair(root):
root=Path(root);m=json.loads((root/'manifest.json').read_text())
if set(m)!={'format','anonymous_pair_id','files'} or m['format']!='blind-surface-pair-v1':raise ValueError('Unexpected manifest properties')
if not re.fullmatch(r'[0-9a-f]{32}',root.name) or m['anonymous_pair_id']!=root.name:raise ValueError('Identity mismatch')
if {p.name for p in root.iterdir()}!={'left.ply','right.ply','units.txt','manifest.json'}:raise ValueError('Unexpected blind file')
if len(m['files'])!=3 or {r['name'] for r in m['files']}!={'left.ply','right.ply','units.txt'}:raise ValueError('Unexpected payload')
for r in m['files']:
if set(r)!={'name','bytes','sha256'}:raise ValueError('Unexpected payload properties')
data=(root/r['name']).read_bytes()
if len(data)!=r['bytes'] or hashlib.sha256(data).hexdigest()!=r['sha256']:raise ValueError('Payload changed')
if (root/'units.txt').read_text()!=UNITS:raise ValueError('Unexpected units content')
for name in ['left.ply','right.ply']:
lines=(root/name).read_text('ascii').splitlines()
if len(lines)<9:raise ValueError('Truncated PLY')
n=int(lines[2].split()[-1]);nf=int(lines[6].split()[-1])
expected=['ply','format ascii 1.0',f'element vertex {n}','property double x','property double y','property double z',f'element face {nf}','property list uchar int vertex_indices','end_header']
if lines[:9]!=expected or len(lines)!=9+n+nf:raise ValueError('Unexpected PLY fields')
x=np.array([[float(v) for v in row.split()] for row in lines[9:9+n]])
faces=[row.split() for row in lines[9+n:]]
if any(len(row)!=4 or row[0]!='3' for row in faces):raise ValueError('Nontriangle face')
f=np.array([[int(v) for v in row[1:]] for row in faces]);surface(x,f)
return {'status':'GEOMETRY_ONLY_SCHEMA_AND_HASH_PASS','anonymous_pair_id':root.name}
+48
View File
@@ -0,0 +1,48 @@
"""Locked same-key sampling only; identities stay in local custodian directory."""
from pathlib import Path
from itertools import combinations
from collections import Counter
import hashlib,json,os,secrets
import numpy as np
ROOT=Path('artifacts/s2-g1-blind120-2026-11-14')
def save(path,x):path.write_text(json.dumps(x,indent=2,ensure_ascii=False)+'\n')
def main():
ROOT.mkdir(exist_ok=False);sealed=ROOT/'custodian';sealed.mkdir(mode=0o700);public=ROOT/'public';public.mkdir()
old=Path('artifacts/s2-g1-inventory-2026-11-14/custodian')
clusters=json.loads((old/'source_clusters.json').read_text());parts={r['case']:r for r in json.loads((old/'parts.json').read_text())}
rng=np.random.Generator(np.random.PCG64(np.random.SeedSequence([2026111413]).spawn(2)[0]))
selected=[];strata=[];carry=0
for si,(lo,hi,quota) in enumerate([(2,2,30),(3,5,45),(6,10**9,45)]):
groups=sorted((g for g in clusters if lo<=len(g['members'])<=hi),key=lambda g:g['source_key'])
order=rng.permutation(len(groups)).tolist();pools={g['source_key']:list(combinations(sorted(g['members']),2)) for g in groups}
frame_pairs=sum(len(v) for v in pools.values());counts=Counter();rows=[];target=quota+carry
while len(rows)<target:
progress=False
for gi in order:
if len(rows)==target:break
g=groups[gi];key=g['source_key']
if counts[key]>=3 or not pools[key]:continue
pair=pools[key].pop(int(rng.integers(len(pools[key]))));counts[key]+=1;progress=True
rows.append({'a':pair[0],'b':pair[1],'source_key':key,'stratum':si,'frame_pairs_within_key':len(g['members'])*(len(g['members'])-1)//2,'source_cluster_state':g['state'], 'endpoint_exposure':[parts[c]['exposure_state'] for c in pair]})
if not progress:break
carry=target-len(rows)
# Current locked frame has no key shortage inside any round; exact
# marginal pair inclusion = expected key draws / key pair count.
assert not carry and all(len(g['members'])*(len(g['members'])-1)//2>=min(3,(target+len(groups)-1)//len(groups)) for g in groups)
for row in rows:row['marginal_inclusion_probability']=len(rows)/len(groups)/row['frame_pairs_within_key']
selected.extend(rows);strata.append({'stratum':si,'key_size_min':lo,'key_size_max':None if hi==10**9 else hi,'keys':len(groups),'frame_pairs':frame_pairs,'quota':quota,'sampled':len(rows),'sampled_keys':len(counts),'carry_to_next':carry,'key_draw_count_histogram':dict(Counter(counts.values()))})
assert len(selected)==120 and len({(r['a'],r['b']) for r in selected})==120
secret=secrets.token_bytes(32)
for r in selected:
digest=hashlib.sha256(secret+b'pair\0'+r['a'].encode()+b'\0'+r['b'].encode()).hexdigest();r['anonymous_pair_id']=digest[:32]
swap=int(digest[32],16)%2;r['left'],r['right']=(r['b'],r['a']) if swap else (r['a'],r['b'])
r['confirmed_relation']=None;r['development_heldout_assignment']=None
selected.sort(key=lambda r:r['anonymous_pair_id']);unique=sorted({r[k] for r in selected for k in ['a','b']})
save(sealed/'sampling-and-mapping.json',{'seed_sequence':[2026111413],'sampling_spawn_key':[0],'split_spawn_key_reserved':[1],'bit_generator':'PCG64','anonymous_id_secret_hex':secret.hex(),'pairs':selected,'unique_cases':unique,'source_inventory_manifest_sha256':hashlib.sha256((old/'manifest.json').read_bytes()).hexdigest(),'ordering':'keys sorted, pairs lexicographic, one permuted key order per stratum reused each round, rng.integers selects uniformly from remaining sorted pairs','probability':'unconditional marginal within fixed stratum; q/(number_of_keys * pairs_within_key); not realized key count / pairs_within_key'})
save(sealed/'geometry-source-index.json',{'cases':[{'case':c,'existing_surface_caches':parts[c]['surface_caches']} for c in unique]})
for p in sealed.iterdir():p.chmod(0o600)
sealed_manifest=[{'file':p.name,'bytes':p.stat().st_size,'sha256':hashlib.sha256(p.read_bytes()).hexdigest()} for p in sorted(sealed.iterdir())]
summary={'status':'SAMPLED_AND_MAPPING_SEALED_NOT_EXTRACTED_OR_REVIEWED','sampling_ruling':'1794655144.867149','sampled_pairs':120,'sampled_source_keys':len({r['source_key'] for r in selected}),'unique_geometry_cases':len(unique),'strata':strata,'selected_key_exposure_counts':dict(Counter({r['source_key']:r['source_cluster_state'] for r in selected}.values())),'pair_exposure_counts':dict(Counter(r['source_cluster_state'] for r in selected)),'claim':'same-key relationship pool only; not certified natural positives, not independent unexposed or unbiased cross-key validation','unblinding':'Mold labels frozen and committed before Grok verifies seals and identity join','post_review_split':'child1 reserved, not consumed; dominant-class key stratification, rigid<template<variant ties, alternating heldout/development starting heldout; each key at most once per class','sealed_files':sealed_manifest,'code_sha256':hashlib.sha256(Path(__file__).read_bytes()).hexdigest(),'no_geometry_read':True,'no_candidates_or_registration':True}
save(public/'sample-summary-and-seals.json',summary);print(json.dumps(summary,indent=2))
if __name__=='__main__':main()
+39
View File
@@ -0,0 +1,39 @@
"""Uniform CPU-only geometry views; no identity, label or registration inputs."""
import gc
from pathlib import Path
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.collections import PolyCollection
from PIL import Image
def render_surface(x, f, output):
output=Path(output);tri=x[f];center=(x.min(0)+x.max(0))/2;tri=tri-center
normal=np.cross(tri[:,1]-tri[:,0],tri[:,2]-tri[:,0]);length=np.linalg.norm(normal,axis=1)
if np.any(length<=0):raise ValueError('Degenerate triangle in rendered surface')
normal/=length[:,None]
iso=np.array([[1/np.sqrt(2),-1/np.sqrt(2),0],[1/np.sqrt(6),1/np.sqrt(6),-2/np.sqrt(6)],[1/np.sqrt(3)]*3])
matrices=[np.eye(3),np.eye(3)[[0,2,1]],np.eye(3)[[1,2,0]],iso]
titles=['XY view','XZ view','YZ view','Oblique view']
def panel(matrix,title,crop=None):
p=tri@matrix.T;order=np.argsort(p[:,:,2].mean(1),kind='stable');xy=p[order,:,:2]
shade=.3+.65*np.abs((normal@matrix[2])[order]);colors=np.column_stack([.18*shade,.62*shade,.85*shade,np.ones(len(shade))])
fig,ax=plt.subplots(figsize=(6.4,6.4),dpi=110)
ax.add_collection(PolyCollection(xy,facecolors=colors,edgecolors='none',rasterized=True))
low=p[:,:,:2].min(axis=(0,1));high=p[:,:,:2].max(axis=(0,1));span=np.maximum(high-low,1e-12)
if crop is None:lo=low-.04*span;hi=high+.04*span
else:
ix,iy=crop;mid=(low+high)/2;lo=np.array([low[0] if ix==0 else mid[0],low[1] if iy==0 else mid[1]]);hi=np.array([mid[0] if ix==0 else high[0],mid[1] if iy==0 else high[1]])
ax.set_xlim(lo[0],hi[0]);ax.set_ylim(lo[1],hi[1]);ax.set_aspect('equal');ax.set_title(title)
ax.set_xlabel('Original coordinate units, centered for view');ax.set_ylabel('Original coordinate units')
fig.tight_layout();fig.canvas.draw();im=Image.fromarray(np.asarray(fig.canvas.buffer_rgba()).copy()).convert('RGB');plt.close(fig);gc.collect();return im
whole=Image.new('RGB',(1408,1408),'white')
for i,(mat,title) in enumerate(zip(matrices,titles)):
im=panel(mat,title);whole.paste(im,(704*(i%2),704*(i//2)))
whole.save(output/'overview.png')
details=Image.new('RGB',(1408,1408),'white')
for i,crop in enumerate([(0,0),(1,0),(0,1),(1,1)]):
im=panel(iso,f'Oblique quadrant {i+1}; fixed grid, not feature selection',crop);details.paste(im,(704*(i%2),704*(i//2)))
details.save(output/'detail-grid.png')
+34
View File
@@ -0,0 +1,34 @@
"""One sequential, bounded E1 geometry-only queue; never registers pairs."""
from pathlib import Path
import fcntl,json,resource,sys,os
from s2_e1_supervisor import supervise,save
ROOT=Path('artifacts/s2-g1-blind120-2026-11-14')
def main():
os.environ['MPLCONFIGDIR']=str((ROOT/'matplotlib-cache').resolve())
private=ROOT/'custodian';c=json.loads((ROOT/'extraction-contract.json').read_text())
with (private/'batch.lock').open('a') as lock:
fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB)
if (private/'batch-started.json').exists():raise FileExistsError('No silent rerun')
save(private/'batch-started.json',{'parts':c['parts'],'scope':'geometry extraction and uniform views only'})
baseline=c['e1_baseline_charged_seconds'];save(ROOT/'ledger.json',{'cpu_seconds_charged':baseline,'attempts':[{'name':'previous_e1_plus_current_code_reserve','cpu_seconds':baseline,'status':'BASELINE_AND_RESERVE'}]})
receipts=[];last=sum(resource.getrusage(resource.RUSAGE_SELF)[:2])
for index in range(c['parts']):
ledger=json.loads((ROOT/'ledger.json').read_text());phase=ledger['cpu_seconds_charged']-baseline
left=min(c['phase_cpu_seconds']-phase,57600-ledger['cpu_seconds_charged'])
if left<=0:save(private/'batch-stopped.json',{'reason':'CPU budget before launch','completed':len(receipts)});return
limits=dict(total_cpu_seconds=57600,job_cpu_seconds=min(1800,left),job_wall_seconds=1800,tree_rss_bytes=1342177280,disk_bytes=8*1024**3,max_processes=8)
r=supervise([sys.executable,'tools/extract_g1_blind_surface.py',str(index)],ROOT,f'blind-geometry-{index:03}',limits)
now=sum(resource.getrusage(resource.RUSAGE_SELF)[:2]);overhead=now-last;last=now
ledger=json.loads((ROOT/'ledger.json').read_text());ledger['cpu_seconds_charged']+=overhead;ledger['attempts'].append({'name':f'supervisor-self-{index:03}','cpu_seconds':overhead,'status':'MEASURED_SELF_CPU'});save(ROOT/'ledger.json',ledger)
if ledger['cpu_seconds_charged']>=57600 or ledger['cpu_seconds_charged']-baseline>=c['phase_cpu_seconds']:
save(private/'batch-stopped.json',{'reason':'worker plus supervisor budget exhausted','index':index,'e1_total_charged_seconds':ledger['cpu_seconds_charged']});return
receipts.append(r);print(json.dumps({'index':index,'status':r['status'],'worker_cpu_seconds':r['cpu_seconds'],'supervisor_cpu_seconds':overhead,'peak_rss_bytes':r['peak_tree_rss_bytes']}),flush=True)
if r['status']!='PASS':save(private/'batch-stopped.json',{'reason':'first failed extraction, no replacement or automatic retry','index':index});return
if index==4:
measured=ledger['cpu_seconds_charged']-baseline
scenario=max(x['cpu_seconds'] for x in receipts)*c['parts']+(measured-sum(x['cpu_seconds'] for x in receipts))*c['parts']/5
save(private/'first-five-cost-checkpoint.json',{'parts':5,'charged_cpu_seconds':measured,'full_queue_max_sample_scenario_seconds':scenario,'qualification':'geometry/render preparation only; first sorted five not representative or upper bound'})
if scenario>min(c['phase_cpu_seconds'],57600-baseline):
save(private/'batch-stopped.json',{'reason':'first five preparation cost extrapolation exceeds available budget','scenario_seconds':scenario});return
save(private/'batch-complete.json',{'parts':len(receipts),'worker_cpu_seconds':sum(r['cpu_seconds'] for r in receipts),'e1_total_charged_seconds':ledger['cpu_seconds_charged'],'all_pass':True})
if __name__=='__main__':main()
+36
View File
@@ -0,0 +1,36 @@
import json,tempfile,unittest
from pathlib import Path
import numpy as np
from g1_blind_geometry import write_pair,validate_pair
class BlindGeometryTests(unittest.TestCase):
def setUp(self):
self.temp=tempfile.TemporaryDirectory();self.root=Path(self.temp.name)
self.x=np.array([[4.,3.,2.],[5.,3.,2.],[4.,4.,2.],[999.,999.,999.]])
self.f=np.array([[0,1,2]]);self.id='a'*32
def tearDown(self):self.temp.cleanup()
def export(self):return write_pair(self.root,self.id,(self.x,self.f),(self.x*2,self.f))
def test_geometry_only_and_original_scale(self):
p=self.export();self.assertEqual(validate_pair(p)['status'],'GEOMETRY_ONLY_SCHEMA_AND_HASH_PASS')
a=(p/'left.ply').read_text();b=(p/'right.ply').read_text()
self.assertIn('4 3 2\n',a);self.assertIn('8 6 4\n',b);self.assertNotIn('999',a)
def test_named_id_rejected(self):
with self.assertRaises(ValueError):write_pair(self.root,'source-key',(self.x,self.f),(self.x,self.f))
def test_extra_field_and_file_rejected(self):
p=self.export();(p/'labels.json').write_text('{}')
with self.assertRaises(ValueError):validate_pair(p)
def test_manifest_identity_property_rejected(self):
p=self.export();m=json.loads((p/'manifest.json').read_text());m['source_key']='hidden';(p/'manifest.json').write_text(json.dumps(m))
with self.assertRaises(ValueError):validate_pair(p)
def test_changed_payload_rejected(self):
p=self.export();(p/'left.ply').write_text((p/'left.ply').read_text()+'0\n')
with self.assertRaises(ValueError):validate_pair(p)
def test_invalid_right_side_leaves_no_partial_pair(self):
bad=self.x.copy();bad[0,0]=np.nan
with self.assertRaises(ValueError):write_pair(self.root,self.id,(self.x,self.f),(bad,self.f))
self.assertFalse((self.root/self.id).exists())
def test_no_overwrite(self):
self.export()
with self.assertRaises(FileExistsError):self.export()
if __name__=='__main__':unittest.main()