import json, pathlib, subprocess, sys
side, checkout, home = sys.argv[1:]
root=pathlib.Path(__file__).parent
cases=[('refusal','idle','refusal'),('tokens','idle','max_tokens'),('requests','idle','max_turn_requests'),('fallback','fallback','refusal'),('native','native','refusal'),('claude','claude','max_tokens'),('failedReply','native','error')]
settings_path=pathlib.Path(home)/'userdata/settings.json'
settings=json.loads(settings_path.read_text())
for name,mode,reason in cases:
 settings['providerInstances'][name]={'driver':'acpRegistry','displayName':name+' relay','enabled':True,'config':{'source':'local','commandPath':str(root/'claude-agent-acp'),'commandArgs':[mode,reason],'customModels':['default']}}
settings_path.write_text(json.dumps(settings))
workspace=pathlib.Path(home)/'workspace';workspace.mkdir(exist_ok=True)
(workspace/'README.md').write_text('Synthetic ACP stop reason reproduction.\n')
(workspace/'package.json').write_text('{}\n')
for args in [['init','--quiet','--initial-branch=main'],['add','.'],['-c','user.name=Scenario','-c','user.email=scenario@example.invalid','commit','--quiet','-m','Initial commit']]:subprocess.run(['git',*args],cwd=workspace,check=True)
def cli(args):
 p=subprocess.run(['node','apps/server/src/bin.ts',*args,'--base-dir',home],cwd=checkout,text=True,capture_output=True)
 if p.returncode: raise RuntimeError(p.stderr)
 return p.stdout
cli(['project','add',str(workspace),'--title','ACP stop reasons'])
records=[]
for name,mode,reason in cases:
 result=json.loads(cli(['thread','start',str(workspace),'Prepare fixture','--title',name+' stop','--provider',name,'--model','default','--wait','--json']))
 records.append({'case':name,'mode':mode,'reason':reason,'threadId':result['threadId']})
(root/('state-'+side+'.json')).write_text(json.dumps(records))
print(side+': seeded '+str(len(records))+' synthetic threads')
