import pathlib, subprocess, sys
checkout, home, repo, port = sys.argv[1:]
p = pathlib.Path(repo)
p.mkdir(parents=True)
for name in ['alpha', 'beta']:
    (p / (name + '.ts')).write_text('export const ' + name + ' = 1;\n')
def git(*args): subprocess.run(['git', '-C', repo, *args], check=True, capture_output=True)
git('init', '-q', '-b', 'feature')
git('config', 'user.name', 'Scenario')
git('config', 'user.email', 'scenario@example.invalid')
git('add', '.')
git('commit', '-qm', 'Initial files')
import os
env = dict(os.environ, T3CODE_PORT=port)
def cli(*args):
    return subprocess.run(['node', 'apps/server/src/bin.ts', *args, '--base-dir', home], cwd=checkout, env=env, check=True, capture_output=True, text=True).stdout
cli('project', 'add', repo, '--title', 'Commit demo')
cli('thread', 'start', repo, 'Start', '--title', 'commit repro', '--provider', 'devReplay', '--model', 'multi-turn', '--wait', '--json')
for name in ['alpha', 'beta']:
    with (p / (name + '.ts')).open('a') as f: f.write('// changed\n')
print('Synthetic project and thread ready')
