Files
MeltMind/tools/prepare_phase3_followup_postfit.py
T

105 lines
5.9 KiB
Python

"""Prepare one candidate review binding with metadata and opaque hash checks."""
from pathlib import Path
import sys
import time
from phase2_h1_worker_v2 import save, sha
from phase2_home_freeze_io import freeze, verify
from phase3_followup_postfit_common import (
BASELINE, GPU, RULING, SEED, TRAINING, budget, config, frozen_file, read,
root_for, roster, training_terminal,
)
from track_a_h2_prepare import local_code_closure
BASE = Path(__file__).resolve().parents[1]
TOOLS = BASE/'tools'
B_TRAIN = TRAINING['b'][0]
def prepare(item):
start = time.process_time()
root = root_for(item)
if root.exists():
raise FileExistsError('Inspect existing binding rather than duplicate it')
train, load_training, _, _ = TRAINING[item]
tc, pilot = load_training(train)
evidence = training_terminal(item, tc)
old = read(BASELINE/'contract.json')
by = roster(old,pilot)
baseline_proof = verify(BASELINE,'predictions')
verify(BASELINE,'terminal')
path = BASELINE/'predictions-manifest.json'
entries = read(frozen_file(baseline_proof,path,sha(path)))['entries']
seed0 = {r['case']:r for r in entries}
for job in tc['jobs']:
for case in job['test']:
e, r = seed0[case], by[case]
if (e['fold'],e['component_id'],e['nodes']) != (job['id'],r['component_id'],r['nodes']):
raise ValueError('Original H2 seed0 identity')
if len(entries)!=552 or set(by)!=set(seed0):
raise ValueError('Complete baseline prediction support')
sources={}
paths = [train/n for n in ('contract.json','training-terminal-git-freeze.json','training-terminal-authorization.json',
'complete.json','models.json','post-terminal-cost.json')]
paths += [BASELINE/n for n in ('contract.json','predictions-manifest.json','predictions-git-freeze.json',
'terminal-git-freeze.json','score/result.json')]
paths += [Path(tc['comparison_contract']), B_TRAIN/'contract.json']
# Include the actual same-domain seed completion when already available.
seed_cost = SEED/'post-terminal-cost.json'
if seed_cost.exists() and read(seed_cost)['within_cap'] is True:
seed_done = read(SEED/'complete.json')
if seed_done['status'] == 'COMPLETE':
t = verify(SEED,'terminal')
p = SEED/'score/result.json'
frozen_file(t,p,sha(p))
paths += [seed_cost, SEED/'complete.json', SEED/'terminal-git-freeze.json',
SEED/'predictions-git-freeze.json', SEED/'predictions-manifest.json', p]
spec_dir = BASE/'references/2026-11-18'/f'phase3-item-{item}'
paths += sorted(spec_dir.glob('*.md'))
for p in paths:
sources[str(p)] = sha(p)
codes=local_code_closure([*TOOLS.glob('phase3_followup_postfit*.py'),Path(__file__),
TOOLS/'test_phase3_followup_postfit.py',*(TOOLS/'s2_models').glob('*.py')])
source_comparison=read(tc['comparison_contract'])
comparison = dict(ruling=RULING,item=item,parts=552,components=204,folds=list(range(5)),source_comparison_sha256=tc['comparison_contract_sha256'],candidate_seed=0,baseline_seed=0,
seed_noise='frozen same-domain seed1 minus seed0 with full 204-pair support',
candidate_contrast=f'{item} minus H2 seed0',bootstrap=2000,bootstrap_seed=2026111408,
query='original unique raw IDs, multiplicity, Rg, hard maximum, no clipping',
new_fits=0,new_decodes=0,target_cache_reads=552,
prospective_gate=source_comparison['old_corpus_gate' if item=='c' else 'test'],
secondary='05344bac full-node final 1 percent volume and peak diagnostics' if item=='c' else None,
context_missingness='report original valid and scientific-missing strata' if item=='b' else None,
full_predictions_frozen_before_target_access=True,fresh_family_confirmation=False)
# b's accepted static ledger is the later phase 3 allocation. Prior and later item
# reservations stay intact. Replace only the current item's cost with its actual
# training including freeze tail and its 7200-second postfit reservation.
reservations=read(B_TRAIN/'contract.json')['phase_reservations']
c=dict(item=item,ruling=RULING,gpu_uuid=GPU,worker_cpus=[2,3],coordinator_cpus=[16,17],
training=evidence,item_gpu_cap=tc['gpu_cap'],phase_reservations=reservations,
jobs=[dict(id=j['id'],expected_parts=len(j['test'])) for j in tc['jobs']],
seed0_predictions=seed0,source_sha256=sources,code_sha256=codes,comparison_contract=str(root/'comparison-contract.json'),
no_retry=True,no_auto_launch=True,new_fits=0,new_decodes=0,
postfit_gpu_cap=7200,postfit_cpu_cap=3600)
budget(c,0,180,5)
root.mkdir()
save(root/'comparison-contract.json',comparison)
c['comparison_contract_sha256']=sha(root/'comparison-contract.json')
save(root/'contract.json',c)
receipt=root/'preparation-receipt.json'
save(receipt,dict(status='PREPARED_PENDING_EXACT_PRE_SCORE_REVIEW',item=item,training=evidence,
contract_sha256=sha(root/'contract.json'),code_files=len(codes),source_files=len(sources),
setup_cpu_seconds=time.process_time()-start,launched=False,model_weights_decoded=False,
prediction_started=False,scoring_started=False,test_targets_read=False))
freeze(root,'implementation',[root/'contract.json',root/'comparison-contract.json',receipt,*map(Path,codes)])
proof=root/'implementation-git-freeze.json'
binding=dict(reviewed_commit=read(proof)['commit'],contract_sha256=sha(root/'contract.json'),
implementation_proof_sha256=sha(proof),comparison_contract_sha256=c['comparison_contract_sha256'],
code_sha256=codes)
save(root/'expected-review-binding.json',binding)
config(root,review=False)
print(root, {k:v for k,v in binding.items() if k!='code_sha256'}, sha(root/'expected-review-binding.json'))
if __name__=='__main__':
prepare(sys.argv[1])