"""Bounded visible experiments; never touches any process except this GPU1 server."""
import json, os, shlex, subprocess, time, urllib.request
from pathlib import Path
root=Path('/home/lab/bench/ornith/modular/sglang')
tmux=['tmux']
profiles=['mtp-1','mtp-2','mtp-4','dflash-4','dflash-8','dflash-16','standalone']
records=[]
def healthy():
    try:
        with urllib.request.urlopen('http://127.0.0.1:8130/health',timeout=2) as r:return r.status==200
    except Exception:return False
def pane_cmd():return subprocess.check_output(tmux+['display-message','-p','-t','%68','#{pane_current_command}'],text=True).strip()
def launch(profile):
    subprocess.run(['/bin/bash','/home/lab/bench/ornith/sglang/manage.sh','stop'],check=True)
    for _ in range(60):
        if pane_cmd()=='bash' and not healthy():break
        time.sleep(1)
    if pane_cmd()!='bash':raise RuntimeError('Codex pane still busy; no keys sent')
    log=root/'logs'/f'{profile}.log'
    cmd=f'ORNITH_GRAPHS=1 ORNITH_MAX_TOKENS=65536 ORNITH_MEMORY=0.80 bash {root}/launch.sh {profile} 2>&1 | tee {log}'
    subprocess.run(tmux+['send-keys','-t','%68','-l',cmd],check=True)
    subprocess.run(tmux+['send-keys','-t','%68','Enter'],check=True)
    t=time.monotonic()
    while time.monotonic()-t<900:
        if healthy():return True,round(time.monotonic()-t,2)
        if time.monotonic()-t>25 and pane_cmd()=='bash':return False,round(time.monotonic()-t,2)
        time.sleep(3)
    return False,900
for profile in profiles:
    print('\nPROFILE',profile,flush=True)
    ready,startup=launch(profile)
    row={'profile':profile,'ready':ready,'startup_seconds':startup,'target':'AWQ INT4 text','note':'experimental; AWQ arithmetic regression means no production promotion'}
    if ready:
        q=root/'results'/f'quality-{profile}.json'
        rc=subprocess.run(['python3','/home/lab/bench/ornith/modular/shared/quality.py','--base','http://127.0.0.1:8130/v1','--model','ornith-sglang','--out',str(q)]).returncode
        row['quality_exit']=rc
        env={**os.environ,'CUDA_VISIBLE_DEVICES':''}
        cmd=['/home/lab/venvs/sglang/bin/python','/home/lab/bench/ornith/modular/shared/bench.py','--base','http://127.0.0.1:8130/v1','--model','ornith-sglang','--out',str(root/'results'/f'{profile}-4k'),'--lengths','4096','--concurrency','1,3','--repeats','1','--timeout','300','--gpu','GPU-f4a988cb-c310-b618-5a9c-4462d4d33add']
        row['bench_exit']=subprocess.run(cmd,env=env).returncode
    else:
        row['failure_tail']='\n'.join((root/'logs'/f'{profile}.log').read_text(errors='replace').splitlines()[-35:])
    records.append(row); (root/'spec-matrix.json').write_text(json.dumps(records,indent=2));print(json.dumps(row),flush=True)
print('MATRIX COMPLETE; last engine stays running if successful. Parent chooses final validated profile.',flush=True)
