#!/usr/bin/env python3
"""Run against an isolated checkout's fresh dev server; credentials stay local.
Set EVIDENCE_CHECKOUT, EVIDENCE_HOME, EVIDENCE_PROJECT, EVIDENCE_PROVIDER,
and EVIDENCE_MODEL. Record the browser before running this script, then open
its printed coordinator thread. Inspect worker and coordinator after 100 s.
"""
import datetime, json, os, pathlib, subprocess
root = pathlib.Path(__file__).resolve().parent
run = root / ('run-' + datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%S%fZ'))
run.mkdir()
prompt = '''Delegate an asynchronous local Claude worker using delegate_task, mode async, isolation shared. Give the worker exactly this task: Use only Monitor to run `sleep 60; echo MONITOR_DONE`. Omit t3_background_wait. End your initial turn with exactly WAITING. When the Monitor ends answer exactly FINAL. Do no other work. Parent: finish your initial turn immediately after delegation with exactly WAITING. On the initial delegated WAITING result answer exactly WAITING. On a later delegated FINAL result answer exactly FINAL. Do not poll, wait, or send follow-up messages to the child. Use only WAITING or FINAL in your responses; do not include runtime identifiers.'''
args = ['node', 'apps/server/src/bin.ts', 'thread', 'start', '--base-dir', os.environ['EVIDENCE_HOME'], '--provider', os.environ['EVIDENCE_PROVIDER'], '--model', os.environ['EVIDENCE_MODEL'], '--title', 'Monitor coordinator', os.environ['EVIDENCE_PROJECT'], prompt, '--json']
p = subprocess.run(args, cwd=os.environ['EVIDENCE_CHECKOUT'], text=True, capture_output=True)
(run/'launch.json').write_text(p.stdout)
if p.returncode:
    raise SystemExit('Launch failed; inspect local output privately.')
print(p.stdout.strip())
