Files
MeltMind/tools/phase2_h2_s2_prepare.py
T

52 lines
7.6 KiB
Python

"""Metadata and hash preparation; no target arrays, GPU workload or launch."""
import json,time,shutil
from pathlib import Path
import psutil
from phase2_fixed_io import BASE
from phase2_h1_worker_v2 import save,sha,checked
from phase2_home_freeze_io import freeze,verify
from phase2_h2_prepare import closure
from phase2_h2_s2_common import config,execution_command
def read(p):return json.loads(Path(p).read_text())
def main():
root=Path('/home/frank14f/MeltMind-runs/phase2-h2-s2-2026-11-17')
if root.exists():raise FileExistsError('Existing S2 root; inspect, do not duplicate')
active=[p.info for p in psutil.process_iter(['pid','cmdline']) if execution_command(p.info['cmdline'] or [])]
if active:raise RuntimeError('Existing H2 task')
s1=Path('/home/frank14f/MeltMind-runs/phase2-h2-s1-postfit-2026-11-17');verify(s1,'terminal');verify(s1,'predictions');old=read(s1/'contract.json');cost=read(s1/'post-terminal-cost.json');done=read(s1/'complete.json');pilotpath=BASE/'artifacts/phase2-h2-pilot-2026-11-17/contract.json';pilot=read(pilotpath);aroot=BASE/'artifacts/phase2-b13-e1b-2026-11-17';a=read(aroot/'contract.json');h=read(BASE/'artifacts/phase2-h1-execution-2026-11-17/contract.json')
if done['status']!='COMPLETE' or not done['s2_eligible_by_original_gate'] or not cost['within_cap'] or cost['cumulative_h2_gpu_seconds']!=18858.21490871499:raise ValueError('Original S1 gate or ledger')
pred=read(s1/'predictions-manifest.json')['entries']
if len(pred)!=121:raise ValueError('Original121 predictions')
for e in pred:checked(e['path'],e['sha256'])
test=[case for f in pilot['folds'] for case in f['test']];by={r['case']:r for r in a['parts']}
if len(test)!=552 or len(set(test))!=552 or len({by[k]['component_id'] for k in test})!=204:raise ValueError('Fixed five-fold552/204')
for f in pilot['folds']:
af=a['folds'][f['fold']];hj=next(j for j in h['jobs'] if j['protocol']=='grouped' and j['pool']==100 and j['fold']==f['fold'])
if {r['case'] for r in af['rows']['learning']}!=set(f['learning']) or {r['case'] for r in af['rows']['test']}!=set(f['test']) or hj['baseline_model']!=af['A_baseline']:raise ValueError('Original H2/A model or fold role mismatch')
groups={role:{by[k]['component_id'] for k in f[role]} for role in ['learning','validation','test']}
if groups['test']&(groups['learning']|groups['validation']) or groups['learning']&groups['validation']:raise ValueError('Component leakage')
originaltrain=BASE/'artifacts/phase2-h2-s1-2026-11-17';history=[originaltrain/'worker/history'/f'{e:03d}.json' for e in range(10,201,10)];valmax=max(read(p)['validation_seconds'] for p in history)
uuids=['GPU-e8853ae7-a302-6a17-bcfa-aa767e9ed20c','GPU-f1c9fd5a-e917-56e0-21fb-24035ba44a36','GPU-b8c11c01-c91f-aa0b-abf6-dad35085cc76','GPU-8b71b032-1b75-2891-935c-ce106a53034a'];traincap=144000-cost['cumulative_h2_gpu_seconds']-2400;jobs=[];f0=pilot['folds'][0]
for fold in range(1,5):
f=pilot['folds'][fold];ratio=max(f['validation_nodes']/f0['validation_nodes'],f['validation_edges']/f0['validation_edges'],len(f['validation'])/len(f0['validation']))
jobs.append(dict(fold=fold,gpu_uuid=uuids[fold-1],worker_cpus=[2*(fold-1),2*(fold-1)+1],guard_cpus=[fold+7],gpu_seconds=traincap/4,cpu_seconds=traincap/4,setup_gpu_seconds=60,learning_parts=len(f['learning']),learning_nodes=f['learning_nodes'],validation_nodes=f['validation_nodes'],validation_seconds_bound=valmax*ratio,rss_bytes=16*2**30,vram_bytes=15*2**30,disk_bytes=20*2**30))
sources={str(BASE/p):h for p,h in read(originaltrain/'contract.json')['source_sha256'].items()}
for p in [s1/'contract.json',s1/'complete.json',s1/'post-terminal-cost.json',s1/'score/result.json',s1/'predictions-manifest.json',s1/'terminal-git-freeze.json',s1/'terminal-authorization.json',s1/'predictions-git-freeze.json',s1/'predictions-authorization.json',Path(old['checkpoint']),pilotpath,aroot/'contract.json',aroot/'predictions-manifest.json',aroot/'score/result.json',*history]:sources[str(p)]=sha(p)
for f in pilot['folds']:sources[f['clock_path']]=f['clock_sha256']
for p,digest in sources.items():checked(p,digest)
codes={str(BASE/p):h for p,h in closure(['phase2_h2_s2_common','phase2_h2_s2_worker','phase2_h2_s2_guard','phase2_h2_s2_queue','phase2_h2_s2_prepare','test_phase2_h2_s2']).items()}
for p,h in read(originaltrain/'contract.json')['code_sha256'].items():checked(BASE/p,h);codes[str(BASE/p)]=h
doc=BASE/'docs/phase2-h2-s2-execution-contract-2026-11-17.md';codes[str(doc)]=sha(doc)
root.mkdir();fold0=dict(checkpoint=old['checkpoint'],checkpoint_sha256=old['checkpoint_sha256'],prediction_manifest=str(s1/'predictions-manifest.json'),prediction_manifest_sha256=sha(s1/'predictions-manifest.json'),score=str(s1/'score/result.json'),score_sha256=sha(s1/'score/result.json'),terminal=verify(s1,'terminal')['commit'],no_reexecution=True)
c=dict(version='H2-S2-four-fold-training-v1',approval='1794923791.467409',correction='1794923933.456449',epochs=200,seed=0,new_fits=4,s1_root=str(s1),pilot_contract=str(pilotpath),fold0_reuse=fold0,prior_gpu_seconds=cost['cumulative_h2_gpu_seconds'],total_h2_gpu_seconds=144000,prediction_gpu_reserve=2400,train_gpu_cap=traincap,postfit_cpu_cap=3600,jobs=jobs,coordinator_cpus=[12,13],aggregate_rss_bytes=64*2**30,disk_bytes=80*2**30,source_sha256=sources,code_sha256=codes,scoring=dict(parts=552,components=204,bootstrap_replicates=2000,bootstrap_seed=2026111408,confirmation='pooled field H2-A CI upper<0 with complete552/204',B13_and_last_and_tau='report_only',new_exact_semantic_PASS_required=True,S1_PASS_reuse=False,fold0_score_hash_reuse=True),stop_after='TRAIN_COMPLETE_POSTFIT_PENDING',no_retry=True,data_sufficiency_started=False)
save(root/'contract.json',c)
for i in [1,2]:shutil.copyfile('/tmp/h2-s2-synthetic-attempt'+str(i)+'.log',root/('synthetic-attempt'+str(i)+'.log'))
if not (root/'synthetic-attempt2.log').read_text().rstrip().endswith('OK'):raise ValueError('Synthetic acceptance absent')
save(root/'acceptance.json',dict(status='PASS',tests=16,elapsed_seconds=.051,command='python -m unittest tools.test_phase2_h2_s2 -v',actual_log_sha256=sha(root/'synthetic-attempt2.log'),first_fixture_failure_retained=True,prelaunch_contract_path_fixed=True,scientific_workloads=0,scope='CPU synthetic/AST/role/budget and two guard lifecycle tests with fake supervisor/GPU probe; no real CUDA or targets',unix=time.time()))
gate=Path('/home/frank14f/MeltMind-runs/phase2-gate-outcome-calibration-v2-2026-11-17');g=read(gate/'contract.json');gprog=read(gate/'live-progress.json');gatepins={p:sha(BASE/p)==h for p,h in g['code_sha256'].items()};assert all(gatepins.values())
save(root/'preparation-audit.json',dict(status='PREPARED_NOT_LAUNCHED',unix=time.time(),h2_active_processes=active,original_fold0_predictions_hash_checked=121,original_A_roles_models_checked=5,test_partition552_204=True,test_target_values_read=False,validation_timing_source_max=valmax,gate_progress=gprog,gate_original_total_cpu=72000,gate_geometry_cpu=68400,gate_pins_verified=all(gatepins.values()),gate_95000_ruling='1794919865.091919 /1794920747.468449; only after original budget stop and exit; all terminal hashes/old CPU once, no retries, new postfit identity review',gate_modified=False))
freeze(root,'implementation',[root/'contract.json',root/'acceptance.json',root/'preparation-audit.json',root/'synthetic-attempt1.log',root/'synthetic-attempt2.log',*[Path(p) for p in codes]]);config(root)
print(json.dumps(dict(root=str(root),implementation=verify(root,'implementation')['commit'],contract_sha256=sha(root/'contract.json'),code_files=len(codes),jobs=[dict(fold=j['fold'],learning_parts=j['learning_parts'],gpu_seconds=j['gpu_seconds']) for j in jobs],launched=False)))
if __name__=='__main__':main()