from pathlib import Path
import json,hashlib,shutil,tarfile
H=Path(__file__).resolve().parent;P=H.parent;OLD=P.parent/'parallel-ai-user-weight-owned-flow-20261011';PRIOR=Path('/tmp/cdx-ai-weight-owned-flow-20261011');R=Path('/tmp/cdx-ai-disease-owned-flow-20261011')
assert not R.exists();R.mkdir(mode=0o700);(H/'tools').mkdir(exist_ok=True)
pins=json.loads((OLD/'execution/source-frozen-sha.json').read_text());(R/'source').mkdir(mode=0o700)
for n,sha in pins['files'].items():
 p=PRIOR/'source'/n;assert hashlib.sha256(p.read_bytes()).hexdigest()==sha;q=R/'source'/n;q.parent.mkdir(parents=True,exist_ok=True);shutil.copyfile(p,q)
for q in (H/'source-frozen-sha.json',R/'source-frozen-sha.json'):q.write_text(json.dumps(pins,indent=2)+'\n')
for name in ['native.php','app.php','launch.php','NativePdo.php','observer.php','source-state.php','cleanup-cache.php','cleanup.php','data.php']:
 raw=(OLD/'execution/tools'/name).read_text()
 if name=='app.php':raw=raw.replace('CDXOWNWGT11','CDXOWNDISEASE11').replace('owned-weight.invalid','owned-disease.invalid')
 if name=='launch.php':raw=raw.replace('19001:19002','19011:19012')
 if name=='NativePdo.php':
  raw=raw.replace("$n<1000","$n<600").replace("if(!is_file($runtime.'/owned-actor.json'))","if(!is_file($runtime.'/preflight-ready.json'))")
  raw=raw.replace("['W1_MANUAL','W2_GOAL']","['W1_DISEASE_EDIT']").replace("?100:180","?80:160").replace("?50:90","?60:120").replace("==='W1_MANUAL'","==='W1_DISEASE_EDIT'")
 for q in (H/'tools'/name,R/name):q.write_text(raw)
for lane in ('B','C'):
 d=R/f'seed-{lane}';d.mkdir(mode=0o700);src=PRIOR/f'private-{lane}-final'
 for name in ['prepared.json',f'preflight-{lane}.json','prior-persistence.json','owned-actor.json']:
  shutil.copyfile(src/name,d/name);(d/name).chmod(0o600)
 (d/'weight-baseline.json').write_text(json.dumps(json.loads((src/'final-capture.json').read_text())['rows'],ensure_ascii=False,indent=2)+'\n');(d/'weight-baseline.json').chmod(0o600)
plan=json.loads((P/'machine-plan.json').read_text());plan['status']='ROOT_AUTHORIZED_EXACT_POST_EDIT_AND_POST_INFO';plan['authority']='Root exact actor/0prepare/2HTTP/2I/0UDeleteDDL/600SQL/preflight180/W80B160C/R60B120C/token1/Serve2/UserApi0; R1 POST raw{} replaces unissued GET based actual client';
for q in (H/'execution-plan.json',R/'execution-plan.json'):q.write_text(json.dumps(plan,ensure_ascii=False,indent=2)+'\n')
print(json.dumps({'source_files':6168,'DB':0,'SQL':0,'Auth':0,'HTTP':0,'DML':0}))
