"""Real-app evidence driver. Run from an installed checkout with its dev server running.
No database seeding: project add and thread start/send use the real server.
Usage: python driver.py CHECKOUT PROJECT PROVIDER MODEL OUTPUT_DIR prepare|run N [parallel|worktree|forced-wait]
Record the target thread in the browser before invoking run, through its final A/B reply.
The browser masks the workspace identity, composer controls, and reasoning for privacy.
"""
import subprocess,json,sys
from pathlib import Path
root,project,provider,model,out,action,n,*kind=sys.argv[1:]
out=Path(out);out.mkdir(parents=True,exist_ok=True)
kind=kind[0] if kind else 'parallel'
def cli(*args):
 p=subprocess.run(['node','apps/server/src/bin.ts','thread',*args,'--base-dir','.t3','--json'],cwd=root,text=True,capture_output=True)
 if p.returncode:raise RuntimeError(p.stderr)
 return json.loads(p.stdout)
f=out/f'thread-{n}.json'
if action=='prepare':
 d=cli('start',project,'Reply only Ready. Do not mention model or machine identifiers in this conversation.','--title',f'Parallel delegation {n}','--provider',provider,'--model',model,'--wait')
 f.write_text(json.dumps(d));print(d['threadId'])
else:
 d=json.loads(f.read_text())
 prompt='Launch two independent read-only child tasks in parallel using delegate_task. Each child must run sleep 15 and then reply with its letter, A or B. I need both results before your final answer. Choose the delegation mode yourself. Report accurately what has launched and what has completed. Use the same provider as this thread for both children. Do not include provider, model, machine names, or filesystem paths in replies or task titles.'
 if kind=='worktree':prompt+=' Each child must have its own separate prepared checkout. Choose the isolation yourself. Do not edit any files or open a pull request.'
 if kind=='forced-wait':prompt+=' For this control, override the mode choice: submit both delegate_task calls together with mode:"wait", timeoutMs:10000, and distinct clientRequestIds.'
 (out/f'prompt-{n}.txt').write_text(prompt)
 result=cli('send',d['threadId'],prompt,'--wait')
 (out/f'first-return-{n}.json').write_text(json.dumps(result,indent=2))
 print(json.dumps(result))
