#!/usr/bin/env python3
"""Run against each commit's fresh development server.

Start the server with T3CODE_DEV_SEED=0 and four available CPUs (taskset -c 0-3
on Linux). Claude's native workflow then has two execution slots.
Pass the provider's cheapest supported model alias through --model.
Open the returned thread in the web app, open its workflow and Member 3,
record while queued, then keep recording until an active member finishes.
Use identical browser viewport and theme for both commits.
AI-assisted; verified with the tests and tools listed above.
"""
import argparse
import json
from pathlib import Path
import subprocess

p = argparse.ArgumentParser()
p.add_argument('--checkout', type=Path, required=True)
p.add_argument('--home', type=Path, required=True)
p.add_argument('--workspace', type=Path, required=True)
p.add_argument('--model', required=True)
p.add_argument('--prompt-only', action='store_true')
a = p.parse_args()
PROMPT = "Launch native Workflow with this exact script, using the cheapest model alias for agent options, no changes to the member prompt:\nexport const meta = { name: 'Queue demonstration', description: 'Three members sharing two execution slots', phases: [{title:'Work'}] }\nphase('Work')\nreturn await parallel([1,2,3].map(i => () => agent('Perform exactly 60 separate sequential Bash tool calls. Call 1 runs echo 1, call 2 runs echo 2, continuing through call 60 running echo 60. Make each call in a separate response. Do not combine commands, batch calls, write a script, skip numbers, or return early. This is a deliberate tool-sequencing exercise. After all 60 calls, reply Done.', {label:`Member ${i}`, phase:'Work', model:MODEL_LITERAL})))\nDo not read other files first. Reply only Workflow launched after the tool returns.\n"
prompt = PROMPT.replace('MODEL_LITERAL', json.dumps(a.model))
if a.prompt_only:
    print(prompt, end='')
    raise SystemExit

a.workspace.mkdir(parents=True, exist_ok=True)
subprocess.run(['git', 'init', '--quiet', str(a.workspace)], check=True)
subprocess.run(['git', '-C', str(a.workspace), '-c', 'user.name=Evidence',
                '-c', 'user.email=evidence@example.invalid', 'commit',
                '--allow-empty', '-qm', 'Initial'], check=True)
cli = ['node', 'apps/server/src/bin.ts']
subprocess.run(cli + ['project', 'add', str(a.workspace), '--title',
                     'Workflow queue', '--base-dir', str(a.home)],
               cwd=a.checkout, check=True)
subprocess.run(cli + ['thread', 'start', str(a.workspace), '--base-dir',
                     str(a.home), '--provider', 'claudeAgent', '--model',
                     a.model, '--title', 'Queue demonstration', '--json', '-'],
               input=prompt, text=True, cwd=a.checkout, check=True)
