"""Real-app driver; run from a checkout with dependencies and an isolated dev server.
Copy the companion proof-rpc.ts into apps/server/. No database writes are used.
Usage: python3 reproduce.py <T3 base dir> <empty workspace> <new output dir>
Record the thread in the web client while running; use --pause to wait before resume.
"""
import json, pathlib, re, subprocess, sys, uuid
home, workspace, output = [pathlib.Path(x).resolve() for x in sys.argv[1:4]]
output.mkdir(parents=True, exist_ok=False)
workspace.mkdir(parents=True,exist_ok=True)
(workspace/'notes.txt').write_text('PINEAPPLE\n')
assert not (workspace/'scratch.txt').exists()
def cli(*args):
 return subprocess.check_output(['node','apps/server/src/bin.ts',*args,'--base-dir',str(home)],text=True)
def rpc(method,payload):
 f=output/'rpc-input.json';f.write_text(json.dumps(dict(method=method,payload=payload)))
 return json.loads(subprocess.check_output(['node','apps/server/proof-rpc.ts',str(home),str(f)],text=True))
def dispatch(**payload):
 return rpc('orchestration.dispatchCommand',dict(commandId=str(uuid.uuid4()),**payload))
rpc('server.updateSettings',dict(patch={},providerInstanceMutation=dict(operation='upsert',instanceId='opencode',instance=dict(driver='opencode',enabled=True))))
added=cli('project','add',str(workspace),'--title','Permission history proof')
project=re.search(r'Added project (\S+)',added).group(1)
thread='permission-proof-'+str(uuid.uuid4())
dispatch(type='thread.create',threadId=thread,projectId=project,title='Permission history proof',modelSelection=dict(instanceId='opencode',model='openai/gpt-6-luna'),runtimeMode='approval-required',interactionMode='default',branch=None,worktreePath=None,createdBy='user',creationSource='web')
def send(name,message):
 result=json.loads(cli('thread','send',thread,message,'--wait','--json'))
 (output/(name+'.json')).write_text(json.dumps(result,indent=2))
 return result
send('ready','Reply with just the word READY. Do not use any tools.')
dispatch(type='thread.runtime-mode.set',threadId=thread,runtimeMode='full-access')
initial=send('initial','Use your native task tool to launch exactly one subagent with subagent_type general. Give it this task: Read notes.txt in the current project, then write scratch.txt containing the exact word from notes.txt. Use file tools to do the work, and report which tools you used. Do not do the file work yourself. Report the task_id so we can resume this same child later.')
child=re.search(r'ses_[A-Za-z0-9]+',initial['reply']).group(0)
print('Open thread',thread,'and start its recording. Child:',child,flush=True)
if '--pause' in sys.argv: input('Press Enter to resume the child: ')
send('resumed',f'Resume the same native task child using task_id {child} and subagent_type general. Ask it to retry now: read notes.txt and write scratch.txt containing the exact word from notes.txt, using file tools. Do not create a new child and do not do the file work yourself. Report the tools the child actually used and the result.')
result={'scratchExists':(workspace/'scratch.txt').exists(),'scratch':(workspace/'scratch.txt').read_text() if (workspace/'scratch.txt').exists() else None}
(output/'file-result.json').write_text(json.dumps(result,indent=2));print(result)
