"""Single failed read retest only, exact Root authorization; reuse mature parent tools."""
from pathlib import Path
import json,hashlib,shutil,tarfile,io
HERE=Path(__file__).resolve().parent;PARENT=HERE.parent;RUN=Path('/tmp/cdx-ai-customer-weight-retest-20261011');OLD=Path('/tmp/cdx-ai-api-customer-read12-20261011')
assert not (RUN/'machine-plan.json').exists();RUN.mkdir(mode=0o700,exist_ok=True);(RUN/'source').mkdir(mode=0o700,exist_ok=True);(HERE/'tools').mkdir(exist_ok=True)
pins=json.loads((PARENT/'source-frozen-sha.json').read_text())
for n,h in pins['files'].items():
 src=OLD/'source'/n;assert hashlib.sha256(src.read_bytes()).hexdigest()==h;dst=RUN/'source'/n;dst.parent.mkdir(parents=True,exist_ok=True);shutil.copyfile(src,dst)
shutil.copyfile(PARENT/'source-frozen-sha.json',RUN/'source-frozen-sha.json')
for p in (PARENT/'tools').glob('*.php'):
 raw=p.read_text()
 if p.name=='NativePdo.php':raw=raw.replace("'AI'=>['store'","'AI'=>['staff','store'")
 if p.name=='app.php':raw=raw.replace('CDXCUSTOMERREAD11','CDXCUSTOMER_WEIGHT_RETEST')
 if p.name in ['launch.php','run.php']:raw=raw.replace('18971:18972','18973:18974')
 (RUN/p.name).write_text(raw)
 if p.name in ['NativePdo.php','app.php','launch.php','run.php']:(HERE/'tools'/p.name).write_text(raw)
inherited={p.name:hashlib.sha256(p.read_bytes()).hexdigest() for p in (PARENT/'tools').glob('*.php') if p.name not in ['NativePdo.php','app.php','launch.php','run.php','preflight.php','capture.php']}
for p in (HERE/'tools').glob('*.php'):shutil.copyfile(p,RUN/p.name)
for lane in ['B','C']:
 dst=RUN/f'seed-{lane}';dst.mkdir(mode=0o700);prior=OLD/f'private-{lane}-final'
 for n in ['prepared.json',f'preflight-{lane}.json','prior-persistence.json','sql-budget.jsonl']:
  shutil.copyfile(prior/n,dst/n);(dst/n).chmod(0o600)
 original=json.loads((prior/'expected.json').read_text())['WEIGHT']
 (dst/'expected.json').write_text(json.dumps({'WEIGHT_RETEST':original},ensure_ascii=False,indent=2)+'\n')
 cases=json.loads((prior/'cases.json').read_text());case=cases['WEIGHT'];case['label']='WEIGHT_RETEST';(dst/'cases.json').write_text(json.dumps({'WEIGHT_RETEST':case},ensure_ascii=False,indent=2)+'\n')
 (dst/'budget-baseline.json').write_text(json.dumps({'old_SQL':len((prior/'sql-budget.jsonl').read_text().splitlines()),'old_SQL_SHA':hashlib.sha256((prior/'sql-budget.jsonl').read_bytes()).hexdigest(),'old_HTTP':10,'new_HTTP_cap':1})+'\n')
plan={'authorization':'Root exact failed Weight retest: one GET/side, same actor/tenant10001, date2026-10-11; new token max1/side (cumulative2); new18973/74/caches; add only original yoshop_staff metadata; total cumulative reads≤600;0DMLDDL/SDK; no successful route replay',
 'path':'/api/user.UserWeight/byDate','method':'GET','query_raw':'date=2026-10-11','label':'WEIGHT_RETEST','actors':{'B':44676,'C':1836},'tenant':10001,'HTTP_each':1,'new_token_each':1,'SQL_cumulative_cap':600,'DML':0,'DDL':0,'source_files':6168,'inherited_tool_SHA':inherited,'source_manifest_reference':'../source-frozen-sha.json','unchanged_original_expected':'../oracle_lib.py; parent frozen expected.WEIGHT=[]'}
for dst in [HERE/'machine-plan.json',RUN/'machine-plan.json']:dst.write_text(json.dumps(plan,ensure_ascii=False,indent=2)+'\n')
for lane in ['B','C']:
 entries={p.name:p for p in RUN.iterdir() if p.is_file()};entries.update({p.relative_to(RUN).as_posix():p for p in (RUN/'source').rglob('*') if p.is_file()});entries.update({'private/'+p.name:p for p in (RUN/f'seed-{lane}').iterdir() if p.is_file()});dirs={RUN.name,RUN.name+'/private'}
 for n in entries:dirs.update(str(q) for q in (Path(RUN.name)/n).parents if str(q)!='.')
 target=Path('/tmp/cdx-ai-weight-retest-'+lane+'.tar')
 with tarfile.open(target,'w',format=tarfile.USTAR_FORMAT) as t:
  for n in sorted(dirs):i=tarfile.TarInfo(n);i.type=tarfile.DIRTYPE;i.mode=0o700;t.addfile(i)
  for n,p in sorted(entries.items()):raw=p.read_bytes();i=tarfile.TarInfo(RUN.name+'/'+n);i.size=len(raw);i.mode=0o600;t.addfile(i,io.BytesIO(raw))
 target.chmod(0o600)
print(json.dumps({'source_files':6168,'new_cases_each':1,'SQL':0,'HTTP':0,'Auth':0}))
