#!/usr/bin/env python3
"""Start a fresh real web app with the launcher-only Muse installation.
Usage: python3 manual-driver.py CHECKOUT SCRATCH
Run separately for each commit. Stop the recorded process group after capture.
"""
import json, os, pathlib, subprocess, sys, time, urllib.request
checkout, scratch = map(pathlib.Path, sys.argv[1:])
run = scratch / str(time.time_ns())
run.mkdir(parents=True)
launcher = run / 'muse'
urllib.request.urlretrieve('https://api.meta.ai/muse-launcher.sh', launcher)
launcher.chmod(0o755)
probe = subprocess.run([str(launcher), '--version'], env={**os.environ, 'MUSE_NO_AUTO_UPDATE':'1'}, capture_output=True, text=True)
(run/'launcher-result.json').write_text(json.dumps({'exitCode':probe.returncode, 'stdout':probe.stdout, 'stderr':probe.stderr}, indent=2))
home = run/'home'
(home/'userdata').mkdir(parents=True)
(home/'userdata/settings.json').write_text(json.dumps({'providerInstances':{name:{'driver':name,'enabled':name=='muse', **({'config':{'binaryPath':str(launcher)}} if name=='muse' else {})} for name in ['codex','claudeAgent','cursor','grok','muse','pi','opencode','antigravity']}}))
log = open(run/'server.log','w')
child = subprocess.Popen(['vp','run','dev','--home-dir',str(home),'--no-seed'], cwd=checkout, env={**os.environ,'T3CODE_DEV_SEED':'0','T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD':'0'}, stdout=log, stderr=subprocess.STDOUT, start_new_session=True)
(run/'pid').write_text(str(child.pid))
print(json.dumps({'run':str(run),'pid':child.pid}), flush=True)
