Files
MeltMind/tools/s2_pilot_terminal.py
T

79 lines
4.5 KiB
Python

"""Validate original or merged preparation terminal evidence without rerunning it."""
import argparse
import json
import os
from pathlib import Path
from freeze_g1_components import sha
from s2_e1_supervisor import disk_usage
def continuation_allowed(source,batch):
if batch['status']!='STOPPED' or batch.get('stop_reason')!='FIRST_50_PROJECTION_OVER_CAP':return False
checkpoint=json.loads((source/'first-50-checkpoint.json').read_text())
return (60*3600<checkpoint['projected_worker_cpu_seconds']<=80*3600
and checkpoint['projected_disk_bytes']<=100*2**30
and len(batch['jobs'])==50 and bool(batch['not_run_indices']))
def combine(source,continuation,proof):
roots=[source,continuation];contracts=[];batches=[]
assert len(proof)==2
for root,entry in zip(roots,proof):
assert str(root)==entry['root']
assert sha(root/'contract.json')==entry['contract_sha256']
assert sha(root/'batch-complete.json')==entry['batch_sha256']
contracts.append(json.loads((root/'contract.json').read_text()))
batches.append(json.loads((root/'batch-complete.json').read_text()))
original,later=contracts;before,after=batches
assert len(original['cases'])==499 and continuation_allowed(source,before)
assert after['status']=='ATTEMPTS_COMPLETE' and after['stop_reason'] is None and not after['not_run_indices']
assert later['continuation_of']==proof[0]
assert later['role_manifest_sha256']==original['role_manifest_sha256']
original_cases={s['case']:i for i,s in enumerate(original['cases'])};assert len(original_cases)==499
pending={original['cases'][i]['case'] for i in before['not_run_indices']}
assert {s['case'] for s in later['cases']}==pending and len(later['cases'])==len(pending)
assert sorted(r['index'] for r in after['jobs'])==list(range(len(later['cases'])))
for spec in later['cases']:
assert spec==original['cases'][original_cases[spec['case']]],'Continuation cannot replace or alter a part specification'
jobs=[];seen=set()
for root,c,b in zip(roots,contracts,batches):
for job in b['jobs']:
case=job['case'];assert c['cases'][job['index']]['case']==case
assert case not in seen,'An attempted part may not be retried by continuation'
seen.add(case);assert job['worker']['cpu_seconds']>=0
assert job['worker']['status']!='GUARD_FAILED_ACCOUNTING_UNKNOWN'
jobs.append(dict(job,index=original_cases[case],source_index=job['index'],ready_source_root=str(root)))
assert seen==set(original_cases) and len(jobs)==499
cpu=sum(r['worker']['cpu_seconds'] for r in jobs);used=sum(disk_usage(root) for root in roots)
assert cpu<=80*3600 and used<=100*2**30,'Merged budget exceeded'
return dict(status='ATTEMPTS_COMPLETE',stop_reason=None,not_run_indices=[],jobs=sorted(jobs,key=lambda r:r['index']),
worker_cpu_seconds=cpu,guard_cpu_seconds=sum(b['guard_cpu_seconds'] for b in batches),
failed=sum(r['worker']['status']!='PASS' for r in jobs),disk_bytes=used,
source_provenance=proof,continuation_ruling='1794683049.057259',retry=False,replacement=False,test_labels_read=False)
def terminal_batch(source):
merged=source/'merged-terminal.json'
if not merged.exists():
path=source/'batch-complete.json';return json.loads(path.read_text()),path
m=json.loads(merged.read_text());proof=m['source_provenance']
batch=combine(source,Path(proof[1]['root']),proof)
assert batch['jobs']==m['jobs'] and batch['worker_cpu_seconds']==m['worker_cpu_seconds']
return batch,merged
def merge(source,continuation):
proof=[dict(root=str(root),contract_sha256=sha(root/'contract.json'),batch_sha256=sha(root/'batch-complete.json')) for root in [source,continuation]]
batch=combine(source,continuation,proof)
pending=source/'merged-terminal.json.pending'
with pending.open('x') as f:json.dump(batch,f,indent=2);f.write('\n');f.flush();os.fsync(f.fileno())
# The waiting entry sees either no merged manifest or the complete file.
# link creates the final name atomically and refuses an existing manifest.
os.link(pending,source/'merged-terminal.json');pending.unlink()
return batch
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('source',type=Path);p.add_argument('continuation',type=Path);a=p.parse_args();r=merge(a.source,a.continuation)
print(json.dumps(dict(status=r['status'],terminal=len(r['jobs']),failed=r['failed'],worker_cpu_seconds=r['worker_cpu_seconds'])))