#!/usr/bin/env python3
"""Run against an isolated checkout with `T3CODE_DEV_SEED=0 vp run dev` already running.
Set CODEX_HOME for that server to a scratch home containing config/auth but NO rules.
Usage: reproduce.py CHECKOUT seed EMPTY_WORKSPACE
       reproduce.py CHECKOUT inspect THREAD_ID
For the recorded UI interaction, seed first, pair the browser, open the returned
thread link using the local web origin, and use a 1280x800 light viewport.
Close the sidebar and thread details (privacy), open the model picker to verify
Full access, close it, start recording, type PROMPT, and click Submit message.
Do not answer any approval. On head, expand Worked for to see the command.
The recordings retain normal animations; static captures disable CSS animations,
transitions and caret blinking, then compare screenshots taken 500 ms apart.
"""
import json, pathlib, sqlite3, subprocess, sys
PROMPT = "Execute the shell command `rm -rf ./absent-repro-target` exactly. Then report its exit code."
checkout=pathlib.Path(sys.argv[1]).resolve()
def cli(*args):
    return subprocess.run(["node","apps/server/src/bin.ts",*args],cwd=checkout,text=True,capture_output=True,check=True).stdout
if sys.argv[2]=="seed":
    workspace=pathlib.Path(sys.argv[3]).resolve(); workspace.mkdir(parents=True,exist_ok=True)
    print(cli("project","add","--base-dir",".t3","--title","Command approval proof",str(workspace)))
    print(cli("thread","start","--base-dir",".t3","--title","Full access forced-rm proof","--provider","codex","--model","gpt-6.1-sol","--wait",str(workspace),"Reply Ready only. Do not run any commands yet."))
    print("Browser message:",PROMPT)
elif sys.argv[2]=="inspect":
    thread=sys.argv[3]
    data=json.loads(cli("thread","show","--base-dir",".t3",thread,"--json"))
    db=sqlite3.connect((checkout/".t3/userdata/statev2.sqlite").as_uri()+"?mode=ro",uri=True)
    requests=db.execute("select kind,status from orchestration_v2_projection_runtime_requests where thread_id=?",(thread,)).fetchall()
    print(json.dumps({"commit":subprocess.check_output(["git","rev-parse","HEAD"],cwd=checkout,text=True).strip(),"provider":"codex","codexVersion":"0.169.0","model":data["thread"]["model"],"runtimeMode":data["thread"]["runtimeMode"],"status":data["thread"]["status"],"pendingRequestCount":data["thread"]["pendingRequestCount"],"runtimeRequests":[{"kind":k,"status":s} for k,s in requests],"messages":[{"role":m["role"],"text":m["text"]} for m in data["messages"]]},indent=2))
