Files
MeltMind/tools/freeze_s2_main_roster.py
T

92 lines
4.9 KiB
Python

"""One metadata-only roster across five folds, after the 131 terminal attempts."""
import argparse
import json
from pathlib import Path
from freeze_g1_components import sha
from s2_recovery_supervisor import save
def read(path):
return json.loads(Path(path).read_text())
def validate_preparation(path,expected_sha=None):
path=Path(path)
if expected_sha is not None:assert sha(path/'result.json')==expected_sha
r=read(path/'result.json')
assert r['case']==path.name
assert r['status']=='COMPLETE' and r['label_arrays_decoded']==[]
assert r['eligible_unique']>0 and r['eligible_slots']>0,'No common eligible query'
for name,digest in r['output_sha256'].items():
assert 'label' not in name and sha(path/name)==digest,(path,name)
return dict(preparation_root=str(path),result_sha256=sha(path/'result.json'),output_sha256=r['output_sha256'],
nodes=r['nodes'],t_set=r['scales']['t_set'],eligible_unique=r['eligible_unique'],eligible_slots=r['eligible_slots'])
def assemble(parts,reused,new_jobs,new_sources):
"""Preserve original component IDs and all assignments, including excluded parts."""
assert len({p['case'] for p in parts})==len(parts)
records={p['case']:dict(p,included=False,exclusion_reason='NOT_PREPARED') for p in parts}
assert not (set(reused)&{j['case'] for j in new_jobs})
for case,source in reused.items():
assert source['nodes']==records[case]['nodes']
records[case].update(included=True,exclusion_reason=None,preparation=source,attempt_kind='REUSED_PREVIOUS_PASS')
for job in new_jobs:
case=job['case'];r=records[case];w=job['worker']
if w['status']=='PASS' and case in new_sources:
assert new_sources[case]['nodes']==r['nodes']
r.update(included=True,exclusion_reason=None,preparation=new_sources[case],attempt_kind='NEW_LABEL_FREE_ATTEMPT')
else:
r.update(exclusion_reason=w.get('stop_reason') or w['status'],attempt_kind='NEW_LABEL_FREE_ATTEMPT',terminal_worker=w)
assert set(reused)|{j['case'] for j in new_jobs}==set(records)
rows=[records[p['case']] for p in parts]
counts=[]
for fold in range(5):
for pool in [25,50,100]:
groups={role:[] for role in ['learning','validation','test']}
for p in rows:
a=p['assignments'][fold]
assert a['fold']==fold
if not p['included']:continue
if a['role']=='learning' and str(pool) not in a['learning_pools']:continue
groups[a['role']].append(p)
counts.append(dict(fold=fold,pool=pool,**{role:dict(parts=len(items),nodes=sum(p['nodes'] for p in items),
components=len({p['component_id'] for p in items})) for role,items in groups.items()}))
return rows,counts
def freeze(root):
root=Path(root)
assert not (root/'final-roster.json').exists(),'Never replace a frozen roster'
from run_s2_main_preparation import contract
c=contract(root);batch=read(root/'batch-complete.json')
assert batch['status']=='ATTEMPTS_COMPLETE' and batch['stop_reason'] is None
assert len(batch['jobs'])==131 and not batch['not_run_indices']
assert sorted(j['index'] for j in batch['jobs'])==list(range(131))
sources={}
for j in batch['jobs']:
assert j['case']==c['cases'][j['index']]['case']
guard=root/'jobs'/f"job-{j['index']:04d}"/'guard-result.json'
assert read(guard)['worker']==j['worker']
if j['worker']['status']=='PASS':
sources[j['case']]=validate_preparation(root/'parts'/j['case'])
reuse=read(root/'reuse-493.json')['parts']
old={r['case']:validate_preparation(r['preparation_root'],r['result_sha256']) for r in reuse}
parts=read('artifacts/s2-g1-components-2026-11-15/custodian/part-splits.json')
assert len(parts)==624 and len(old)==493
rows,counts=assemble(parts,old,batch['jobs'],sources)
roster=dict(status='FROZEN',version='S2-main-five-fold-roster-v1',parts=rows,fold_pool_counts=counts,
included=sum(r['included'] for r in rows),excluded=[r['case'] for r in rows if not r['included']],
contract_sha256=sha(root/'contract.json'),batch_sha256=sha(root/'batch-complete.json'),
splits_sha256=sha('artifacts/s2-g1-components-2026-11-15/custodian/part-splits.json'),
labels_decoded=False,models_reused=False,neural_runs_required=90,r2_final_fits_required=15,
test_unsealing='Only unified custody after all90+15 terminal and checkpoints frozen')
save(root/'final-roster.json',roster)
save(root/'roster-receipt.json',dict(status='FROZEN',sha256=sha(root/'final-roster.json'),included=roster['included'],
excluded=roster['excluded'],neural_started=False,r2_started=False,labels_decoded=False))
print(json.dumps(read(root/'roster-receipt.json')),flush=True)
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('root',type=Path);freeze(p.parse_args().root)