Files
MeltMind/tools/phase2_b76_serial.py
T

53 lines
3.1 KiB
Python

"""One dependency waiter: existing B1-v2 terminal, then B7 then B6 once."""
import fcntl
import json
import os
from pathlib import Path
import subprocess
import sys
import time
import psutil
from phase2_h1_worker_v2 import save,sha,checked
BASE=Path('/home/frank14f/optane/MeltMind')
ROOT=BASE/'artifacts/phase2-b76-serial-2026-11-17'
DEPENDENCY=BASE/'artifacts/phase2-h1-labelled-recovery-2026-11-17'
def read(p):return json.loads(Path(p).read_text())
def live(root):
return [p.pid for p in psutil.process_iter(['pid','cmdline']) if p.pid!=os.getpid() and str(root) in (p.info['cmdline'] or [])]
def run():
c=read(ROOT/'contract.json')
for p,h in c['source_sha256'].items():checked(BASE/p,h)
os.sched_setaffinity(0,[24]);start=time.process_time()
with (ROOT/'run.lock').open('a') as lock:
fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB)
if (ROOT/'started.json').exists():raise FileExistsError('Unique existing chain; no restart')
save(ROOT/'started.json',dict(pid=os.getpid(),unix=time.time(),contract_sha256=sha(ROOT/'contract.json')))
results=[]
try:
wall=time.monotonic()
while not (DEPENDENCY/'complete.json').exists() or live(DEPENDENCY):
save(ROOT/'live-progress.json',dict(stage='WAIT_EXISTING_B1_V2_TERMINAL_AND_EXIT',unix=time.time(),coordinator_cpu_seconds=time.process_time()-start))
if time.monotonic()-wall>43200 or time.process_time()-start>30:raise RuntimeError('Bounded waiter limit')
time.sleep(15)
if read(DEPENDENCY/'complete.json')['status']!='COMPLETE':raise RuntimeError('B1 recovery stopped; no downstream launch')
for name,module in [('phase2-b7-gate-2026-11-17','phase2_b7_gate'),('phase2-b6-gate-2026-11-17','phase2_b6_gate')]:
root=BASE/'artifacts'/name
for p,h in c['source_sha256'].items():checked(BASE/p,h)
if live(root) or (root/'started.json').exists():raise RuntimeError('Dependent gate already attempted; do not duplicate')
save(ROOT/'live-progress.json',dict(stage='RUN_'+module,unix=time.time(),coordinator_cpu_seconds=time.process_time()-start))
with (ROOT/(module+'.log')).open('x') as log:
child=subprocess.run([sys.executable,'tools/'+module+'.py','run',str(root)],stdout=log,stderr=subprocess.STDOUT)
if not (root/'complete.json').exists() or live(root):raise RuntimeError('Missing terminal or live children')
result=read(root/'complete.json');results.append(dict(gate=name,returncode=child.returncode,complete_sha256=sha(root/'complete.json'),status=result['status']))
save(ROOT/'gate-terminals.json',results)
save(ROOT/'complete.json',dict(status='BOTH_GATES_TERMINAL',results=results,coordinator_cpu_seconds=time.process_time()-start,retry=False))
except BaseException as exc:
save(ROOT/'complete.json',dict(status='STOPPED',reason=type(exc).__name__+': '+str(exc),results=results,coordinator_cpu_seconds=time.process_time()-start,retry=False));raise
if __name__=='__main__':run()