#!/usr/bin/env python3
"""Run from either checkout, against its running isolated dev server.
Supply PROOF_MODEL privately. Usage: driver.py initial|resume|final-resume|nested-initial|nested-resume [thread-id]
Restart that checkout's server between initial and resume; keep its T3 home.
Open parent in the web client, expand Lineage, open the original child.
For nested runs open Recovery Child, then Nested Marker.
"""
import json, os, subprocess, sys
from pathlib import Path
phase=sys.argv[1]
cli=["node","apps/server/src/bin.ts"]
project=os.environ["PROOF_PROJECT"]
model=os.environ.get("PROOF_MODEL","")
initial="Spawn a native child agent that replies exactly FIRST_CHILD_MARKER. Wait for it to finish. Retain its agent id and report it. Do not close the child."
resume="Resume the SAME existing native child /root/marker_child using followup_task. Ask it to reply exactly SECOND_CHILD_MARKER. Do not spawn a new child. Wait for completion and report its answer."
if phase.endswith("initial"):
 subprocess.run(cli+["project","add","--base-dir",".t3","--title","Restart recovery proof",project],check=True)
 prompt=(Path(__file__).with_name("nested-initial.txt").read_text() if phase.startswith("nested") else initial)
 args=["thread","start","--base-dir",".t3","--provider","codex","--model",model,"--title","Nested child recovery" if phase.startswith("nested") else "Native child recovery",project,"-","--wait","--json"]
else:
 prompt=(Path(__file__).with_name("nested-resume.txt").read_text() if phase.startswith("nested") else Path(__file__).with_name("final-resume.txt").read_text() if phase=="final-resume" else resume)
 args=["thread","send","--base-dir",".t3",sys.argv[2],"-","--wait","--json"]
subprocess.run(cli+args,input=prompt,text=True,check=True)
