#!/usr/bin/env python3
"""Run from a prepared checkout; provider selection is private environment input.
AI-assisted; verified with the tests and tools listed above.
"""
import os, json, subprocess, pathlib, datetime, sys
side, project, variant = sys.argv[1:]
root=pathlib.Path(os.environ['EVIDENCE_DIR'])
out=root/(datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%S')+'-'+side+'-'+variant)
out.mkdir()
bg='sleep 5; echo EARLY_BG_DONE' if variant=='success' else 'sleep 5; echo EXPECTED_BG_FAILURE; exit 7'
child=f'Use only native terminal tools. Run `{bg}` in the background. Run `sleep 15; echo FOREGROUND_DONE` in the foreground and wait. Never poll, read, or kill the background task. Then reply exactly EARLY_SUCCESS_DONE and end.'
prompt='Call orchestrator_capabilities once. Delegate exactly one asynchronous child to the same provider and model as yourself, with shared isolation, full access and low reasoning. Title the task "Early completion child". Give the child these exact instructions: '+child+' After delegating, end your turn and wait for the automatic completion notification. Upon each completion notification, call task_status for that task and report both notification summary and task_status.summary verbatim. Do not delegate more tasks. Do not mention provider names or model identifiers in your replies.'
(out/'prompt.txt').write_text(prompt)
args=['node','apps/server/src/bin.ts','thread','start','--base-dir','.t3','--provider',os.environ['REPRO_PROVIDER'],'--model',os.environ['REPRO_MODEL'],'--title','Early completion '+variant,'--json',project,prompt]
r=subprocess.run(args,text=True,capture_output=True)
(out/'launch.json').write_text(r.stdout)
(out/'stderr.txt').write_text(r.stderr)
print(r.stdout);print('Output:',out.name)
sys.exit(r.returncode)
