#!/usr/bin/env python3
"""Synthetic real-app reproduction. Run with a dev server already running in CHECKOUT.
Pi 1.5.2 must use an isolated PI_CODING_AGENT_DIR with pi-subagents 0.88.1,
and the Pi provider must be enabled with that binary wrapper. No replay/model mock.
Usage: python reproduce.py CHECKOUT SYNTHETIC_REPO
Open the printed thread in the web client and record through both child completions.
"""
import pathlib, subprocess, sys
checkout = pathlib.Path(sys.argv[1]).resolve()
repo = pathlib.Path(sys.argv[2]).resolve()
repo.mkdir(parents=True, exist_ok=True)
(repo / "README.md").write_text("Synthetic project for async Pi subagent UI proof.\n")
for args in [["init", "-q", "-b", "main"], ["add", "."], ["-c", "user.name=Evidence", "-c", "user.email=evidence@example.invalid", "commit", "-qm", "Initial synthetic project"]]:
    subprocess.run(["git", *args], cwd=repo, check=True)
cli = ["node", "apps/server/src/bin.ts"]
subprocess.run([*cli, "project", "add", str(repo), "--title", "Pi async workflow proof", "--base-dir", ".t3"], cwd=checkout, check=True)
prompt = 'Perform this exact UI reproduction. Do not inspect files or run any parent shell commands. In one assistant reply, emit this workflow block and call subagent({workflow: true, async: true}) exactly once. After the launch receipt, immediately reply "Workflow launched; waiting for background results." and end your turn. Do not wait, poll, or call any other tools. When completion notifications arrive, briefly acknowledge the children.\n```js workflow\nconst results = await runs.all([\n {key: "alpha", agent: "scout", model: "anthropic/claude-haiku-5-5", task: "Call bash once with: sleep 40; echo alpha-complete . Then reply alpha complete. Do nothing else."},\n {key: "beta", agent: "scout", model: "anthropic/claude-haiku-5-5", task: "Call bash once with: sleep 70; echo beta-complete . Then reply beta complete. Do nothing else."}\n]);\nreturn results;\n```\n'
subprocess.run([*cli, "thread", "start", str(repo), "-", "--title", "Async workflow: alpha and beta", "--provider", "pi", "--model", "anthropic/claude-haiku-5-5", "--json", "--base-dir", ".t3"], input=prompt, text=True, cwd=checkout, check=True)
