Files
MeltMind/tools/prepare_track_a_postfit_rebind.py
T

89 lines
4.8 KiB
Python

"""Freeze the authorized four-split Track A binding without opening targets or models."""
import fcntl
import time
from pathlib import Path
from phase2_h1_worker_v2 import checked, save, sha
from phase2_home_freeze_io import freeze, verify
from track_a_h2_prepare import local_code_closure
from track_a_postfit_common import config as original_config
from track_a_postfit_rebind_common import (
ORIGINAL, ORIGINAL_POSTFIT, RECOVERY, ROOT, RULING, config, read, scope, terminal,
)
BASE = Path(__file__).resolve().parents[1]
RECORDS = BASE / 'artifacts/track-a-postfit-rebind-2026-11-19'
def source_files():
return [
*(ORIGINAL_POSTFIT / name for name in ('contract.json', 'expected-review-binding.json',
'implementation-git-freeze.json')),
*(ORIGINAL / name for name in ('complete.json', 'infrastructure-stop-git-freeze.json',
'infrastructure-stop-authorization.json')),
*(RECOVERY / name for name in ('contract.json', 'complete.json', 'terminal-ledger.json',
'terminal-git-freeze.json', 'terminal-authorization.json',
'available-models-incomplete.json', 'incomplete-models-git-freeze.json',
'incomplete-models-authorization.json')),
RECOVERY / 'jobs/3/stop.json',
RECOVERY / 'jobs/3/first-epoch-projection.json',
RECOVERY / 'jobs/4/worker/result.json',
*(RECOVERY / f'jobs/{i}/complete.json' for i in (3, 4)),
]
def main():
start = time.process_time()
old, training, _ = original_config(ORIGINAL_POSTFIT)
if ROOT.exists():
raise FileExistsError('The rebind root already exists')
sources = {str(p): sha(p) for p in source_files()}
if set(sources).intersection(old['source_sha256']):
for p in set(sources).intersection(old['source_sha256']):
checked(p, old['source_sha256'][p])
models = terminal(training)
codes = local_code_closure([Path(__file__), *(BASE / 'tools' / name for name in (
'track_a_postfit_rebind.py', 'track_a_postfit_rebind_worker.py',
'track_a_postfit_rebind_guard.py', 'test_track_a_postfit_rebind.py',
))])
contract = dict(old, **scope(old, training), version='track-a-postfit-four-split',
rebind_ruling=RULING, training_evidence=models,
source_sha256={**old['source_sha256'], **sources}, code_sha256=codes)
del contract['all_five_training_terminals_before_prediction']
contract['all_selected_training_terminals_before_prediction'] = True
if (contract['expected_pair_visits'], contract['expected_unique_target_reads']) != (1228, 571):
raise ValueError('Four-split overlap/domain differs from the original metadata')
with Path('/tmp/meltmind-single-writer.lock').open('a') as lock:
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
ROOT.mkdir()
save(ROOT / 'contract.json', contract)
save(ROOT / 'preparation-receipt.json', dict(
status='PREPARED_PENDING_INDEPENDENT_PRE_SCORE_REVIEW', rebind_ruling=RULING,
prior_postfit_implementation=verify(ORIGINAL_POSTFIT, 'implementation')['commit'],
evaluated_splits=[x['id'] for x in contract['jobs']], excluded_split=contract['excluded_split'],
pair_visits=contract['expected_pair_visits'], unique_parts=contract['expected_unique_target_reads'],
training_gpu_seconds_once=models['training_gpu_seconds_once'],
source_pins=len(contract['source_sha256']), code_pins=len(codes),
bootstrap=contract['bootstrap'], opaque_checkpoint_hashes_only=True,
metadata_cpu_seconds=time.process_time()-start, review_adopted=False,
launched=False, predicted=False, scored=False, test_targets_read=False,
))
freeze(ROOT, 'implementation', [ROOT / 'contract.json', ROOT / 'preparation-receipt.json',
*(Path(p) for p in codes), *source_files(),
BASE / 'docs/track-a-four-split-postfit-2026-11-19.md',
RECORDS / 'acceptance.log', RECORDS / 'protective-stops.json'])
proof = verify(ROOT, 'implementation')
frozen, _, _ = config(ROOT)
save(ROOT / 'expected-review-binding.json', dict(
reviewed_commit=proof['commit'], contract_sha256=sha(ROOT / 'contract.json'),
implementation_proof_sha256=sha(ROOT / 'implementation-git-freeze.json'),
comparison_contract_sha256=frozen['comparison_contract_sha256'], code_sha256=codes))
receipt = read(ROOT / 'preparation-receipt.json')
print(dict(receipt, implementation=proof['commit'], contract=sha(ROOT / 'contract.json'),
binding_sha256=sha(ROOT / 'expected-review-binding.json')))
if __name__ == '__main__':
main()