import os, sys, sqlite3, subprocess, time, threading, json
from pathlib import Path
root = Path(sys.argv[1]).resolve()
output = Path(__file__).parent
side = "base" if root.name.endswith("-base") else "head"
sha = subprocess.check_output(["git", "-C", str(root), "rev-parse", "HEAD"], text=True).strip()
started = time.monotonic()
recording = open(output / (side + ".cast"), "w")
recording.write(json.dumps({"version": 2, "width": 110, "height": 25, "title": "Real MCP dispatch refusal: " + side}) + "\n")
def emit(text):
    recording.write(json.dumps([round(time.monotonic()-started,3), "o", text.replace("\n", "\r\n") + "\r\n"]) + "\n")
    recording.flush()
emit("T3 Code real server | OpenCode ACP | openai/gpt-5.6-luna")
emit("Commit: " + sha)
db = sqlite3.connect(root / ".t3/userdata/statev2.sqlite", check_same_thread=False)
db.execute("BEGIN IMMEDIATE")
emit("SQLite: acquired external 30-second write lock; reads remain available.")
def release():
    time.sleep(30)
    db.rollback()
thread = threading.Thread(target=release)
thread.start()
time.sleep(1)
emit('MCP request: create_threads {"threads":[{"prompt":"Say hi"}]}')
t = time.monotonic()
r = subprocess.run(["node", str(root / "apps/server/src/bin.ts"), "acp-mcp-call", "create_threads", json.dumps({"threads":[{"prompt":"Say hi"}], "clientRequestId":"proof-recorded-2"})], capture_output=True, text=True)
elapsed = time.monotonic()-t
payload = json.loads(r.stdout)
error = json.loads(payload["content"][0]["text"])
emit("Reply after %.2f seconds | isError=%s | code=%s" % (elapsed, payload.get("isError"), error["code"]))
import textwrap
emit("\n".join(textwrap.wrap(error["message"], 105)))
(output / (side + "-reply.json")).write_text(json.dumps({"commit":sha,"elapsedSeconds":elapsed,"result":payload}, indent=2))
thread.join()
emit("External write lock released. Capture complete.")
recording.close()
print(r.stdout)
