149 lines
11 KiB
Python
149 lines
11 KiB
Python
"""B6 learning-only geometric HKS gate, after B1 recovery and serial B7."""
|
|
import argparse
|
|
import fcntl
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import resource
|
|
import sys
|
|
import time
|
|
import traceback
|
|
from collections import Counter
|
|
import joblib
|
|
import numpy as np
|
|
import scipy
|
|
from phase2_fixed_io import BASE,freeze,verify
|
|
from phase2_h1_worker_v2 import checked,sha,save
|
|
from phase2_b6_math import hks,partial_from_triangular,tsqr_update
|
|
from r4bc_guard import supervise
|
|
from s2_recovery_monitoring import disk_usage
|
|
|
|
ROOT=BASE/'artifacts/phase2-b6-gate-2026-11-17'
|
|
def read(p):return json.loads(Path(p).read_text())
|
|
|
|
|
|
def prepare(acceptance):
|
|
a=read(acceptance)
|
|
if a['status']!='PASS':raise ValueError('B6 acceptance missing')
|
|
for p,h in a['code_sha256'].items():checked(BASE/p,h)
|
|
b3=BASE/'artifacts/phase2-b3-gate-2026-11-17/contract.json';old=read(b3)
|
|
spec=BASE/'references/2026-11-17/phase2-design/mold-b6-spec-8f94be12.md'
|
|
checked(spec,'8f94be1294b9b8a020aabaf2f434658b593a3fc78504815c38751e546015ae16')
|
|
names=['phase2_b6_gate','phase2_b6_math','phase2_fixed_io','phase2_h1_worker_v2','phase2_residual_math','r4bc_guard','s2_recovery_supervisor','s2_recovery_monitoring']
|
|
ROOT.mkdir(exist_ok=False)
|
|
save(ROOT/'contract.json',dict(version='B6-volume-Neumann-HKS-v1',approvals=['1794892472.421479','1794893702.921009'],math_review='1794893856.072239',
|
|
parts=old['parts'],baseline=old['baseline_model'],source_sha256={str(b3):sha(b3),str(spec):sha(spec),str(acceptance):sha(acceptance)},
|
|
code_sha256={f'tools/{n}.py':sha(BASE/f'tools/{n}.py') for n in names},numpy=np.__version__,scipy=scipy.__version__,
|
|
operator='volume tet P1 K; lumped mass; all boundaries homogeneous natural Neumann; centered coordinates/Rg; FP64',
|
|
spectrum='all exact physical-component zero modes; first8 positive + full adjacent relative1e-6 boundary cluster; require next gap or exhaustion',
|
|
solver='component-wise dense eigh for n<=256; otherwise projected shift-invert eigsh16 positive Ritz pairs, tol1e-10/maxiter2000/ncv48/fixedseed2026111706; no fallback or retry; unconfirmed boundary missing',
|
|
shift='median whitened stiffness diagonal * 1e-6; invert K+shift*M only on exact zero-mode complement; Rayleigh-Ritz eigenvalues',
|
|
residual_tolerance=1e-5,orthogonality_tolerance=1e-7,zero_tolerance=1e-10,cluster_tolerance=1e-6,times=[.1,1.,10.],
|
|
weights='1/(G * parts_in_component * nodes_in_part); full retained tet node rows',
|
|
partial_correlation='weighted TSQR then SVD projection of y=t_star-A and all3HKS on same original19 columns+intercept; signed Pearson residual r; no refit',
|
|
null_variance='residual norm<=1e-10*max(original_norm,1) is undefined, never filled zero',
|
|
gate='coverage>=40/50 and any abs(r)>=.10 => RETAIN_LEARNING_DESCRIPTION; undefined all => PARK; otherwise PRUNE_THIS_FIXED_HKS; no out-of-sample claim',
|
|
total_cpu_seconds=7200,setup_seconds=60,cpu=24,part_cpu_seconds=110,part_wall_seconds=180,score_cpu_seconds=900,
|
|
rss_bytes=8*1024**3,disk_bytes=5*1024**3,new_models=0,new_decodes=0,gpu=False,retry=False))
|
|
|
|
|
|
def config(root):
|
|
c=read(root/'contract.json')
|
|
for p,h in {**c['code_sha256'],**c['source_sha256']}.items():checked(p,h)
|
|
if (np.__version__,scipy.__version__)!=(c['numpy'],c['scipy']):raise ValueError('Library drift')
|
|
if len(c['parts'])!=50 or len({r['component_id'] for r in c['parts']})!=50:raise ValueError('Original B3 panel differs')
|
|
for r in c['parts']:
|
|
a=next(a for a in r['assignments'] if a['fold']==0)
|
|
if a['role']!='learning' or '100' not in a['learning_pools']:raise PermissionError('Not fold0 learning100')
|
|
return c
|
|
|
|
|
|
def field(root,c,index):
|
|
row=c['parts'][index];dest=root/'parts'/f'{index:02d}';p=row['preparation'];gp=Path(p['preparation_root'])/'geometry.npz'
|
|
checked(gp,p['output_sha256']['geometry.npz'])
|
|
metadata=read(checked(Path(p['preparation_root'])/'result.json',p['result_sha256']))
|
|
try:
|
|
with np.load(gp,allow_pickle=False) as z:
|
|
x=z['points_raw'].copy();tet=z['tetra'].copy();raw=z['raw_node_ids'].copy();mass=z['node_volume_raw'].copy()
|
|
if x.shape!=(len(raw),3) or np.any(np.diff(raw)<=0) or not np.isfinite(mass).all() or np.any(mass<=0):raise ValueError('GEOMETRY_IDENTITY')
|
|
center=np.average(x,axis=0,weights=mass);rg=float(metadata['scales']['Rg'])
|
|
if not np.isfinite(rg) or rg<=0:raise ValueError('INVALID_RG')
|
|
H,report=hks((x-center)/rg,tet)
|
|
np.save(dest/'H.npy',H,allow_pickle=False);np.save(dest/'raw_ids.npy',raw,allow_pickle=False)
|
|
save(dest/'result.json',dict(status='PASS',case=row['case'],component_id=row['component_id'],rg=rg,center=center.tolist(),nodes=len(raw),
|
|
geometry_sha256=p['output_sha256']['geometry.npz'],H_sha256=sha(dest/'H.npy'),raw_ids_sha256=sha(dest/'raw_ids.npy'),labels_read=False,**report))
|
|
except BaseException as exc:
|
|
save(dest/'failure.json',dict(status='MISSING',type=type(exc).__name__,reason=str(exc),traceback=traceback.format_exc(),labels_read=False));raise
|
|
|
|
|
|
def score(root,c):
|
|
verify(root,'fields');manifest=read(root/'fields-manifest.json');dest=root/'score';R=None;access=[]
|
|
entries=manifest['entries'];coverage=len(entries)/50
|
|
if coverage<.8:
|
|
save(dest/'result.json',dict(status='COMPLETE',decision='PARK_COVERAGE',parts=len(entries),coverage=coverage,signed_r=[None]*3,labels_read=False));return
|
|
model=joblib.load(checked(c['baseline']['model_path'],c['baseline']['model_sha256']));groups=Counter(c['parts'][e['index']]['component_id'] for e in entries)
|
|
for e in entries:
|
|
row=c['parts'][e['index']];folder=root/'parts'/f"{e['index']:02d}";result=read(checked(folder/'result.json',e['result_sha256']));cache=row['v5_cache']
|
|
H=np.load(checked(folder/'H.npy',result['H_sha256']),mmap_mode='r',allow_pickle=False)
|
|
raw=np.load(checked(folder/'raw_ids.npy',result['raw_ids_sha256']),allow_pickle=False)
|
|
original=np.load(checked(Path(cache['output'])/'raw_ids.npy',cache['output_sha256']['raw_ids.npy']),allow_pickle=False)
|
|
if not np.array_equal(raw,original):raise ValueError('HKS and A node order differ')
|
|
x=np.load(checked(Path(cache['output'])/'features.npy',cache['output_sha256']['features.npy']),mmap_mode='r',allow_pickle=False)
|
|
access.append(dict(case=row['case'],role='fold0_learning',target_sha256=cache['output_sha256']['target.npy'],nodes=len(raw)));save(dest/'target-access.json',access)
|
|
y=np.load(checked(Path(cache['output'])/'target.npy',cache['output_sha256']['target.npy']),mmap_mode='r',allow_pickle=False)
|
|
if x.shape!=(len(raw),19) or y.shape!=(len(raw),) or H.shape!=(len(raw),3):raise ValueError('HKS residual row mismatch')
|
|
weight=1/(len(groups)*groups[row['component_id']]*len(raw))
|
|
for start in range(0,len(raw),32768):
|
|
stop=min(len(raw),start+32768);xx=np.asarray(x[start:stop],np.float64);pred=np.asarray(model.predict(xx.astype(np.float32)),np.float64)
|
|
block=np.column_stack([np.ones(stop-start),xx,y[start:stop]-pred,H[start:stop]])*np.sqrt(weight)
|
|
if not np.isfinite(block).all():raise ValueError('Nonfinite residual input')
|
|
R=tsqr_update(R,block)
|
|
save(dest/'progress.json',dict(parts=len(access),nodes=sum(a['nodes'] for a in access)))
|
|
result=partial_from_triangular(R);valid=[r for r in result['signed_r'] if r is not None]
|
|
decision='PARK_UNDEFINED' if not valid else 'RETAIN_LEARNING_DESCRIPTION' if any(abs(r)>=.10 for r in valid) else 'PRUNE_THIS_FIXED_HKS'
|
|
np.save(dest/'weighted-tsqr.npy',R,allow_pickle=False)
|
|
save(dest/'result.json',dict(status='COMPLETE',decision=decision,parts=len(entries),components=len(groups),coverage=coverage,**result,
|
|
interpretation='learning fit residual partial correlation; truncated low-frequency geometric heat signature; no out-of-sample or physical thermal claim'))
|
|
|
|
|
|
def run(root):
|
|
c=config(root);os.sched_setaffinity(0,[c['cpu']]);lock=(root/'run.lock').open('a');fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB)
|
|
for folder in ['phase2-h1-labelled-recovery-2026-11-17','phase2-b7-gate-2026-11-17']:
|
|
p=BASE/'artifacts'/folder/'complete.json'
|
|
if not p.exists() or (folder.startswith('phase2-h1') and read(p)['status']!='COMPLETE'):raise RuntimeError('Serial dependency incomplete')
|
|
if (root/'started.json').exists():raise FileExistsError('B6 zero retries')
|
|
start=time.process_time();u0=resource.getrusage(resource.RUSAGE_CHILDREN);charged=0.;records=[];entries=[]
|
|
def total():
|
|
u=resource.getrusage(resource.RUSAGE_CHILDREN);return 60+max(charged,u.ru_utime+u.ru_stime-u0.ru_utime-u0.ru_stime)+time.process_time()-start
|
|
save(root/'started.json',dict(pid=os.getpid(),unix=time.time(),contract_sha256=sha(root/'contract.json')))
|
|
try:
|
|
for i,row in enumerate(c['parts']):
|
|
if total()+c['part_cpu_seconds']+1050>7200:raise RuntimeError('B6 remaining CPU cannot reserve next part and postfit')
|
|
dest=root/'parts'/f'{i:02d}';dest.mkdir(parents=True,exist_ok=False)
|
|
remaining=c['disk_bytes']-disk_usage(root)
|
|
if remaining<=0:raise RuntimeError('B6 disk cap')
|
|
save(root/'live-progress.json',dict(stage='GEOMETRY_ONLY',index=i,parts=len(records),unix=time.time(),cumulative_cpu_seconds=total()))
|
|
g=supervise([sys.executable,'tools/phase2_b6_gate.py','field',str(root),str(i)],dest,dict(cpu_seconds=c['part_cpu_seconds'],wall_seconds=c['part_wall_seconds'],rss_bytes=c['rss_bytes'],disk_bytes=remaining))
|
|
charged+=g['cpu_seconds'];record=dict(index=i,case=row['case'],guard=g);records.append(record)
|
|
if g['monitor_errors']:raise RuntimeError('B6 monitor failure')
|
|
if g['status']!='PASS' and not (dest/'failure.json').exists() and not g['stop_reason']:raise RuntimeError('B6 input/configuration failure before field diagnostics')
|
|
if g['status']=='PASS':entries.append(dict(index=i,case=row['case'],result_sha256=sha(dest/'result.json')))
|
|
save(root/'phase-ledger.json',dict(records=records,cumulative_cpu_seconds=total()))
|
|
save(root/'fields-manifest.json',dict(entries=entries,records=records,all50_terminal=True,labels_read=False))
|
|
freeze(root,'fields',[root/'contract.json',root/'fields-manifest.json',*[BASE/p for p in c['code_sha256']]])
|
|
dest=root/'score';dest.mkdir(exist_ok=False)
|
|
g=supervise([sys.executable,'tools/phase2_b6_gate.py','score',str(root)],dest,dict(cpu_seconds=min(900,7200-total()-120),wall_seconds=1200,rss_bytes=c['rss_bytes'],disk_bytes=c['disk_bytes']-disk_usage(root)))
|
|
charged+=g['cpu_seconds'];records.append(dict(phase='score',guard=g))
|
|
if g['status']!='PASS' or total()>7200 or disk_usage(root)>c['disk_bytes']:raise RuntimeError('B6 scoring/resource failure')
|
|
save(root/'complete.json',dict(status='COMPLETE',cumulative_cpu_seconds=total(),records=records,scientific_gate=read(dest/'result.json')['decision'],retry=False))
|
|
except BaseException as exc:
|
|
save(root/'complete.json',dict(status='STOPPED',reason=type(exc).__name__+': '+str(exc),cumulative_cpu_seconds=total(),records=records,retry=False));raise
|
|
|
|
|
|
if __name__=='__main__':
|
|
p=argparse.ArgumentParser();p.add_argument('mode',choices=['prepare','run','field','score']);p.add_argument('path',type=Path);p.add_argument('index',nargs='?',type=int);a=p.parse_args()
|
|
if a.mode=='prepare':prepare(a.path)
|
|
elif a.mode=='run':run(a.path)
|
|
elif a.mode=='field':field(a.path,config(a.path),a.index)
|
|
else:score(a.path,config(a.path))
|