from driver import *
run=pathlib.Path(sys.argv[1]);c=Client(run);checkout=json.loads((run/'environment.json').read_text())['checkout'];repo=run/'fixture';repo.mkdir(exist_ok=True)
def git(*args,cwd=repo):return subprocess.check_output(['git',*args],cwd=cwd,text=True)
git('init','-b','evidence-root');git('-c','user.name=Evidence','-c','user.email=evidence@example.invalid','commit','--allow-empty','-m','synthetic fixture');git('remote','set-url','origin','https://github.com/pingdotgg/t3code.git') if git('remote').strip() else git('remote','add','origin','https://github.com/pingdotgg/t3code.git')

if not c.http('/api/projects')['projects']:c.http('/api/projects/mutate',dict(type='project.create',commandId=str(uuid.uuid4()),projectId='evidence-project',title='Synthetic PR sweep evidence',workspaceRoot=str(repo)))
c.rpc('server.updateSettings',{'patch':{'providerInstances':{'opencode':{'driver':'opencode','enabled':True,'config':{'enabled':True}}}}})
def create(id,branch='evidence-root'):
 c.command('thread.create',threadId=id,projectId='evidence-project',title=id,modelSelection={'instanceId':'opencode','model':'anthropic/claude-haiku-5-5'},runtimeMode='full-access',interactionMode='default',branch=branch,worktreePath=None)
def send(id,prompt):
 args=['node','src/bin.ts','thread','send','--base-dir',str(run/'home'),'--json','--wait',id,prompt]
 log('provider-start',thread=id,prompt=prompt)
 p=subprocess.run(args,cwd=checkout+'/apps/server');log('provider-end',thread=id,returncode=p.returncode)
 if p.returncode:raise RuntimeError('provider failed')
create('A-parent')
send('A-parent','Use orchestrator_capabilities once. Use T3 delegate_task exactly three times, each with isolation worktree and mode wait, titles W1, W2, W3, task Reply with the single word ok. Same provider and model. Do not edit files, push, open PRs, or run git commands. Await completion and reply done.')
create('B-local-parent')
send('B-local-parent','Use orchestrator_capabilities once. Use T3 delegate_task once, isolation shared, mode wait, title B-local-child, task Reply with the single word ok. Same provider and model. Do not edit files, push, open PRs, or run git commands. Await completion and reply done.')
create('B-native-parent')
send('B-native-parent','Use the native OpenCode task tool exactly once, subagent_type general, description B-native-child, prompt Reply with the single word ok. This is a native projection fixture: do not use T3 delegate_task. Await it and reply done. Do not edit files, push, open PRs, or run git commands.')
create('L-branchless-parent',None)
send('L-branchless-parent','Use orchestrator_capabilities once. Use T3 delegate_task once, isolation shared, mode wait, title L-branchless-child, task Reply with the single word ok. Same provider and model. Do not edit files, push, open PRs, or run git commands. Await completion and reply done.')
# Read full families, including hidden children, through the public API.
threads={}
for root in ['A-parent','B-local-parent','B-native-parent','L-branchless-parent']:
 family=c.rpc('orchestration.getThreadFamily',{'threadId':root});log('family',root=root,value=family)
 for t in family['threads']:threads[t['id']]=t
for t in threads.values():
 if t['title'] in ['W1','W2','W3']:
  name='evidence-'+t['title'];git('branch','-m',name,cwd=pathlib.Path(t['worktreePath']));c.command('thread.metadata.update',threadId=t['id'],branch=name)
child=next(t for t in threads.values() if t['title']=='W1')
c.command('thread.fork',sourceThreadId=child['id'],targetThreadId='A-fork',sourcePoint={'type':'latest_stable'},title='A-fork')
fwt=run/'fork-worktree';git('worktree','add','-b','evidence-fork',str(fwt),'HEAD')
c.command('thread.metadata.update',threadId='A-fork',branch='evidence-fork',worktreePath=str(fwt))
manifest={'threads':threads,'repo':str(repo),'fork':'A-fork'};(run/'seed-manifest.json').write_text(json.dumps(manifest,indent=2))
# Isolate A: the root, child-of-child fork, three worktree delegates.
for root in ['B-local-parent','B-native-parent','L-branchless-parent']:
 for t in c.rpc('orchestration.getThreadFamily',{'threadId':root})['threads']:c.command('thread.archive',threadId=t['id'])
log('seed-complete');c.close()
