#!/usr/bin/env python3
"""Run from either commit's checkout after configuring its isolated owned OpenCode 2.2.7.
Arguments: workspace-path pid-file t3-home output-directory
The provider wrapper must write its own PID to pid-file before exec'ing OpenCode.
Open the returned thread in the real web client and record until recovery finishes.
"""
import sys, subprocess, pathlib, time, os, signal, json
workspace, pidfile, home, output = sys.argv[1:]
output = pathlib.Path(output) / str(time.time_ns())
output.mkdir(parents=True)
result = subprocess.run([
    'node','apps/server/src/bin.ts','thread','start',workspace,
    'Use your native bash tool to run sleep 15; wait for its result, then reply exactly RECOVERY COMPLETE. Do not use MCP tools or background jobs.',
    '--title','SSE reconnect timeout','--provider','opencode','--model','openai/gpt-6-luna',
    '--base-dir',home,'--json'], capture_output=True, text=True, check=True)
(output/'thread.json').write_text(result.stdout)
print(result.stdout, flush=True)
time.sleep(2)
pid = int(pathlib.Path(pidfile).read_text())
os.kill(pid, signal.SIGSTOP)
(output/'fault.json').write_text(json.dumps({'stoppedAt':time.time(), 'duration':120}))
print('Owned process suspended for 120 seconds', flush=True)
try:
    time.sleep(120)
finally:
    os.kill(pid, signal.SIGCONT)
print('Same owned process resumed', flush=True)
