#!/usr/bin/env python3
"""Pi background-child proof. Configure first, start an isolated app, then seed.

configure ROOT EXTENSION ORIGINAL_MODELS_JSON
seed CHECKOUT APP_HOME PROJECT_ROOT

Run the fault proxy on 127.0.0.1:18924. Start each app with:
T3CODE_DEV_SEED=0 T3CODE_ENVIRONMENT_LABEL='Pi proof' vp run dev \
  --home-dir APP_HOME --auto-bootstrap-project-from-cwd false
Browser: pair, start recording, run seed, open returned thread directly.
Dismiss the floating error banner (the Provider error row remains), expand the user
message, and close thread details. Observe coordinator 429 and child status; wait
for child SCOUT_OK, then reload and repeat those view controls for the still.
Both sides use 1280x800, light appearance, and the same prompt/model/options.
"""
import json,pathlib,subprocess,sys,os
PROMPT='Use the subagent tool to launch the slowscout agent in the background (async: true) with task: run sleep 75 && echo SCOUT_OK; then briefly tell me it has started.'
mode=sys.argv[1]
if mode=='configure':
 root=pathlib.Path(sys.argv[2]).resolve(); extension=pathlib.Path(sys.argv[3]).resolve()
 agent=root/'pi-home';(agent/'agents').mkdir(parents=True,exist_ok=True)
 models=json.loads(pathlib.Path(sys.argv[4]).read_text())
 models['providers']['anthropic']['baseUrl']='http://127.0.0.1:18924'
 (agent/'models.json').write_text(json.dumps(models));os.chmod(agent/'models.json',0o600)
 (agent/'auth.json').write_text('{}')
 (agent/'settings.json').write_text(json.dumps({'defaultProvider':'anthropic','defaultModel':'claude-haiku-4-5-20251001','packages':[str(extension)],'retry':{'enabled':False},'quietStartup':True}))
 (agent/'agents/slowscout.md').write_text('---\nname: slowscout\ndescription: Run a delayed success check\ntools: bash\nmodel: anthropic/claude-haiku-4-5-20251001\nthinking: off\nsystemPromptMode: replace\ninheritProjectContext: false\ninheritSkills: false\n---\nExecute the exact bash command from the task, then reply only SCOUT_OK.\n')
 wrapper=root/'pi-wrapper'
 wrapper.write_text('#!/bin/sh\nexport PI_CODING_AGENT_DIR="'+str(agent)+'"\nexport PI_SUBAGENTS_TEMP_ROOT="'+str(root/'subagent-temp')+'"\nexport PI_OFFLINE=1\nexec pi "$@"\n');wrapper.chmod(0o755)
 for side in ['base-final','head-final']:
  home=root/side/'userdata';home.mkdir(parents=True,exist_ok=True)
  (home/'settings.json').write_text(json.dumps({'providerInstances':{'pi':{'driver':'pi','displayName':'Pi','config':{'enabled':True,'binaryPath':str(wrapper)}}}},indent=2))
elif mode=='seed':
 checkout,app_home,project_root=map(pathlib.Path,sys.argv[2:5])
 def cli(*args):return subprocess.check_output(['node','apps/server/src/bin.ts',*args],cwd=checkout,text=True)
 added=cli('project','add','--base-dir',str(app_home),'--title','Background scout proof',str(project_root))
 project_id=added.split('project ')[1].split(' ')[0]
 print(cli('thread','start','--base-dir',str(app_home),'--provider','pi','--model','anthropic/claude-haiku-4-5-20251001','--option','thinking=off','--title','Scout proof',project_id,PROMPT,'--json'))
