import json, os, pathlib, platform, subprocess, sys, tempfile
checkout = pathlib.Path(sys.argv[1]).resolve()
out = pathlib.Path(sys.argv[2])
out.mkdir(parents=True, exist_ok=True)
span = '{"name":"sample.span","durationMs":5,"endTimeUnixNano":"60000000000"}\n'
sha = subprocess.check_output(['git','rev-parse','HEAD'], cwd=checkout, text=True).strip()
lines = [f'Commit: {sha}', f'Environment: Linux {platform.machine()}, Node '+subprocess.check_output(['node','--version'],text=True).strip()+', ordinary non-root user', 'Built CLI: vp run build:bundle in apps/server; node apps/server/dist/bin.mjs', 'Synthetic fixture only; stdout and stderr captured separately.', '']
with tempfile.TemporaryDirectory(prefix='trace-proof-') as tmp:
    root=pathlib.Path(tmp)
    for case in ['directory rotation','permission-denied rotation','absent rotation control','nothing readable']:
        fixture=root / case.replace(' ','-'); fixture.mkdir()
        current=fixture/'trace.ndjson'; rotated=fixture/'trace.ndjson.1'
        if case != 'nothing readable': current.write_text(span)
        if case in ['directory rotation','nothing readable']: rotated.mkdir()
        if case == 'permission-denied rotation':
            rotated.write_text(span); rotated.chmod(0)
        env={**os.environ,'T3CODE_TRACE_FILE':str(current),'T3CODE_TRACE_MAX_FILES':'2','T3CODE_DEV_SEED':'0','NO_COLOR':'1'}
        result=subprocess.run(['node',str(checkout/'apps/server/dist/bin.mjs'),'trace','summary','--base-dir',str(fixture)],env=env,text=True,capture_output=True)
        def clean(s):
            return s.replace(str(fixture),'<fixture>').replace(str(checkout),'<checkout>').replace(str(pathlib.Path.home()),'<home>')
        lines += [f'=== {case} ===','Command: T3CODE_TRACE_FILE=<fixture>/trace.ndjson T3CODE_TRACE_MAX_FILES=2 t3 trace summary --base-dir <fixture>',f'Exit: {result.returncode}','STDOUT:',clean(result.stdout).rstrip() or '(empty)','STDERR:',clean(result.stderr).rstrip() or '(empty)','']
        if case == 'permission-denied rotation': rotated.chmod(0o600)
(out/'transcript.txt').write_text('\n'.join(lines))
print('\n'.join(lines))
