#!/usr/bin/env python3
"""Run from a dependency-installed T3 Code checkout with its isolated dev server running.
Usage: python3 driver.py <fresh-output-directory>
Start server with T3CODE_DEV_SEED=0; enable Pi in its worktree-local settings.
"""
import json, pathlib, subprocess, sys
out = pathlib.Path(sys.argv[1]).resolve()
parent = out / "parent"
for name in ("a", "b"):
    repo = parent / name
    repo.mkdir(parents=True)
    (repo / "README.md").write_text("Synthetic example repository.\n")
    for args in (("init", "-q", "-b", "main"), ("add", "."),
                 ("-c", "user.name=Example", "-c", "user.email=example@example.invalid", "commit", "-qm", "Initial commit")):
        subprocess.run(["git", *args], cwd=repo, check=True)
def cli(*args):
    return subprocess.check_output(["node", "apps/server/src/bin.ts", *args, "--base-dir", ".t3"], text=True)
(out / "project.txt").write_text(cli("project", "add", str(parent)))
prompt = 'Call the T3 MCP tool delegate_task exactly once with task "Add a line to a/README.md", isolation "worktree", mode "async". First call orchestrator_capabilities if required. Do not use a native subagent. After the call report its exact result and stop. Do not retry, do not edit files yourself, and do not start any other tasks.'
result = cli("thread", "start", "--provider", "pi", "--model", "anthropic/claude-haiku-5-5", "--wait", "--json", "--title", "Multi-repository delegation proof", str(parent), prompt)
(out / "result.json").write_text(result)
thread = json.loads(result)["threadId"]
(out / "thread.json").write_text(cli("thread", "show", thread, "--json", "--messages", "20"))
print(result)
