Files
MeltMind/tools/prepare_phase2_e2_postfit.py
T

59 lines
7.2 KiB
Python

"""Read-only input verification and one-time postfit contract construction; no fits."""
import json,time,unittest,io,resource
from pathlib import Path
from collections import Counter
import numpy as np
from phase2_h1_worker_v2 import checked,sha,save
from phase2_fixed_io import BASE,verify
from phase2_e2_postfit import validate_roles
def read(p):return json.loads(Path(p).read_text())
def prepare():
start=time.process_time();geo=BASE/'artifacts/phase2-b13-e2-geometry-2026-11-17';g=read(geo/'contract.json');done=read(geo/'complete.json');manifest=read(geo/'geometry-manifest.json');root=BASE/'artifacts/phase2-b13-e2-postfit-2026-11-17'
if root.exists():raise FileExistsError('One-time E2 postfit preparation; inspect existing root')
if done['status']!='GEOMETRY_COMPLETE_POSTFIT_PENDING' or len(done['records'])!=552 or any(r['guard']['status']!='PASS' for r in done['records']):raise ValueError('Geometry terminal scope')
for p,h in {**g['code_sha256'],**g['source_sha256']}.items():checked(BASE/p,h)
validate_roles(g)
stream=io.StringIO();tests=unittest.defaultTestLoader.loadTestsFromNames(['test_phase2_e2_statistics','test_phase2_e2_postfit']);result=unittest.TextTestRunner(stream=stream,verbosity=2).run(tests)
if not result.wasSuccessful():raise RuntimeError(stream.getvalue())
root.mkdir();save(root/'synthetic-acceptance.json',dict(status='PASS',tests=result.testsRun,output=stream.getvalue(),cpu_seconds_in_setup=time.process_time()-start))
entries=[];geometry={}
for item in manifest['entries']:
i=item['index'];row=g['parts'][i];p=geo/'parts'/f'{i:04d}'/'result.json';r=read(checked(p,item['result_sha256']))
if row['case']!=item['case'] or r['case']!=row['case'] or r['status']!='PREPARATION_COMPLETE':raise ValueError('Geometry manifest identity')
arms={};geometry[row['case']]=r
for arm in ['T','C','G']:
a=r['arms'][arm];f=geo/'parts'/f'{i:04d}'/arm/'extra.npy' if arm!='G' else Path(g['G_sources'][row['case']]['extra']);h=a['extra_sha256'] if arm!='G' else a['source_extra_sha256'];v=np.load(checked(f,h),mmap_mode='r',allow_pickle=False)
if v.shape!=(row['nodes'],2) or (a['valid'] and not np.isfinite(v).all()) or (not a['valid'] and not np.isnan(v).all()):raise ValueError('Geometry validity/readout mismatch')
arms[arm]=dict(path=str(f),sha256=h,valid=a['valid'],descriptor_defined='descriptor' in a)
entries.append(dict(case=row['case'],index=i,geometry_result=str(p),geometry_sha256=item['result_sha256'],arms=arms))
if len(entries)!=552 or len({e['case'] for e in entries})!=552:raise ValueError('Exactly552 geometry entries')
e1=Path(g['G_prediction_manifest']).parent;verify(e1,'predictions');ep=read(e1/'contract.json');Gscore=e1/'score/result.json';gs=read(Gscore);gp=read(checked(g['G_prediction_manifest'],g['G_prediction_manifest_sha256']))
if gs['decision']!='CONFIRMED_FIXED_GENERIC_CONTEXT_INCREMENT' or gs['parts']!=552 or gs['components']!=204 or ep['folds']!=g['folds'] or ep['parts']!=g['parts']:raise ValueError('E1b roles/confirmation changed')
if len(gp['entries'])!=552 or {e['case'] for e in gp['entries']}!={e['case'] for e in entries}:raise ValueError('G prediction roster')
for e in gp['entries']:checked(e['path'],e['sha256'])
gm=[];source={str(geo/n):sha(geo/n) for n in ['contract.json','complete.json','geometry-manifest.json','first-ten-cost-check.json']}
for f in range(5):
d=Path(g['G_model_root'])/str(f);verify(d,'model');r=read(d/'phases/train/result.json');checked(d/'phases/train/model.ubj',r['checkpoint_sha256'])
access=(BASE/'artifacts/phase2-b13-gate-2026-11-17/phases/train/target-access.json') if f==0 else d/'phases/train/target-access.json';a=read(access);rows=g['folds'][f]['rows']['learning']
if [(x['case'],x['nodes'],x['target_sha256']) for x in a]!=[(x['case'],x['nodes'],x['v5_cache']['output_sha256']['target.npy']) for x in rows]:raise ValueError('G original learning identity')
gm.append(dict(fold=f,model_sha256=r['checkpoint_sha256'],learning_parts=len(rows),target_access_sha256=sha(access),old_role_metadata_hardcoding_preserved=True))
for p in [d/'phases/train/result.json',d/'phases/train/model.ubj',access]:source[str(p)]=sha(p)
for p in [Path(g['G_prediction_manifest']),Gscore,e1/'contract.json',BASE/'references/2026-11-17/phase2-design/mold-b13-e2-spec-182e0312.md']:source[str(p)]=sha(p)
coverage={}
for fold in g['folds']:
coverage[str(fold['fold'])]={}
for role,rows in fold['rows'].items():
groups={}
for row in rows:groups.setdefault(row['component_id'],[]).append(row)
combos=Counter(''.join('1' if geometry[r['case']]['arms'][a]['valid'] else '0' for a in ['G','T','C']) for r in rows)
coverage[str(fold['fold'])][role]=dict(parts=len(rows),components=len(groups),validity_order='GTC',validity_combinations={format(i,'03b'):combos[format(i,'03b')] for i in range(8)},arm={a:dict(valid_parts=sum(geometry[r['case']]['arms'][a]['valid'] for r in rows),complete_components=sum(all(geometry[r['case']]['arms'][a]['valid'] for r in rs) for rs in groups.values())) for a in ['G','T','C']})
names=['phase2_e2_postfit','phase2_e2_statistics','prepare_phase2_e2_postfit','test_phase2_e2_statistics','test_phase2_e2_postfit','phase2_local_freeze_io','phase2_fixed_io','phase2_h1_worker_v2','phase2_b1213_features','gre24_models','s2_custody_common','s2_custody_statistics','r4bc_guard','s2_recovery_supervisor','s2_recovery_monitoring','s2_models/objectives']
code={**g['code_sha256'],**{f'tools/{n}.py':sha(BASE/f'tools/{n}.py') for n in names}};doc=BASE/'docs/phase2-b13-e2-postfit-contract-2026-11-17.md';code[str(doc.relative_to(BASE))]=sha(doc)
c=dict(version='E2-postfit-v1',approval='1794907827.062989',prior_review='1794904249.794959',parts=g['parts'],folds=g['folds'],archive=g['archive'],archive_stat=g['archive_stat'],geometry_root=str(geo),G_prediction_manifest=g['G_prediction_manifest'],G_score=str(Gscore),G_models=gm,code_sha256=code,source_sha256=source,total_cpu_seconds=14400,prior_geometry_cpu_seconds=done['cumulative_cpu_seconds'],setup_seconds=60,worker_cpus=[8,10,12,14],job_caps=dict(train=1800,predict=120,score=2400),disk_bytes=12*2**30,new_fits=10,new_geometry=0,new_label_decodes=0,retry=False,gpu=False,postfit_projection=g['postfit_projection'],projected_total_cpu_seconds=done['cumulative_cpu_seconds']+60+g['postfit_projection']['total'],statistics=dict(replicates=2000,seed=2026111408,svd_rcond=1e-12,support_rtol=1e-10,adjusted_interval_max_failed_fraction_exclusive=.05,deployment_gate='raw full-roster T-G only'))
if c['projected_total_cpu_seconds']>14400:raise RuntimeError('POSTFIT_PROJECTION_EXCEEDS_TOTAL')
save(root/'contract.json',c);save(root/'features-manifest.json',dict(entries=entries,all552_terminal=True,new_geometry=False));save(root/'coverage.json',coverage)
save(root/'preparation-audit.json',dict(status='PASS',geometry_guards=552,feature_arrays_verified=1656,G_predictions_verified=552,G_models_verified=5,roles_verified=True,test_parts=552,test_components=204,geometry_prior_counted_once=done['cumulative_cpu_seconds'],source_sha256=source,setup_cpu_seconds=time.process_time()-start,new_label_access=False,new_computation=False,contract_sha256=sha(root/'contract.json')))
print(json.dumps(dict(root=str(root),contract_sha256=sha(root/'contract.json'),setup_cpu_seconds=time.process_time()-start,projected_total=c['projected_total_cpu_seconds'])))
if __name__=='__main__':prepare()