"""Post-run disposable reproduction wrapper; never repeats the closed study.
Copy exact first-sealed source and accumulated ledger into a fresh directory.
"""
import argparse,json,pathlib,shutil,subprocess,sys
ROOT=pathlib.Path(__file__).resolve().parent
p=argparse.ArgumentParser();p.add_argument('mode',choices=['smoke','full']);p.add_argument('--out',required=True);a=p.parse_args();target=pathlib.Path(a.out).resolve()
if target.exists():raise SystemExit('Disposable output must not already exist')
f=json.loads((ROOT/'optimization-freeze.json').read_text());target.mkdir(parents=True)
for name in list(f['files'])+['optimization-freeze.json','frozen-status.json','attempts.json']:
    shutil.copyfile(ROOT/name,target/name)
shutil.copyfile(ROOT/'frozen-status.json',target/'status.json')
raise SystemExit(subprocess.run([sys.executable,'run_job.py',a.mode],cwd=target).returncode)
