#!/usr/bin/env python3
"""Live OpenCode 2.3.0 proof. All inputs must refer to isolated synthetic state.
Start the app with T3CODE_DEV_SEED=0 and isolated XDG directories, configure the
opencode provider binary, then run seed on each checkout. Capture the browser
while the child runs; snapshot prints native goal data without host paths.
"""
import argparse, datetime, json, pathlib, sqlite3, subprocess
p=argparse.ArgumentParser()
p.add_argument('action',choices=['seed','snapshot'])
p.add_argument('--checkout',required=True,type=pathlib.Path)
p.add_argument('--project',type=pathlib.Path)
p.add_argument('--thread')
a=p.parse_args()
prompt='/goal Report the word printed by a delegated child task. Step 1: call the T3 delegate_task tool once (same provider and model, no worktree, mode async) with the task "Run the shell command `sleep 240 && echo PINEAPPLE` and reply with its output only." Do not wait for it and do not call task_status. Step 2: end your turn immediately; T3 wakes you with the child result when it completes. Then report the word and finish the goal.'
def cli(*args,input=None):
 return subprocess.check_output(['node','apps/server/src/bin.ts',*args],cwd=a.checkout,input=input,text=True)
if a.action=='seed':
 a.project.mkdir(parents=True,exist_ok=True)
 subprocess.run(['git','init','-q',str(a.project)],check=True)
 (a.project/'README.md').write_text('Synthetic delegated goal demonstration.\n')
 subprocess.run(['git','-C',str(a.project),'add','README.md'],check=True)
 subprocess.run(['git','-C',str(a.project),'-c','user.name=Demo','-c','user.email=demo@example.com','commit','-qm','initial'],check=True)
 cli('project','add','--base-dir','.t3','--title','Delegated goal proof',str(a.project))
 print(cli('thread','start','--base-dir','.t3','--title','Delegated goal — 240 second wait','--provider','opencode','--model','anthropic/claude-haiku-5-5',str(a.project),'-',input=prompt))
else:
 x=json.loads(cli('thread','show','--base-dir','.t3',a.thread,'--json'))
 db=sqlite3.connect('file:'+str(a.checkout/'.t3/userdata/statev2.sqlite')+'?mode=ro',uri=True)
 goals=[json.loads(r[0]).get('goal') for r in db.execute('select payload_json from orchestration_v2_projection_provider_threads where thread_id=?',(a.thread,))]
 print(json.dumps({'capturedAt':datetime.datetime.now(datetime.timezone.utc).isoformat(),'status':x['thread']['status'],'runCount':x['thread']['runCount'],'pendingChildren':len(x['thread']['pendingBackgroundTasks']),'nativeGoals':goals,'messages':x['messages']},indent=2))
