"""Usage: python3 seed-fixture.py <base|head> <checkout> <scratch>.
Run each checkout's desktop first, with a fresh .t3 home and T3CODE_DEV_SEED=0.
Uses only synthetic data and the local replay agent; no paid model runs.
"""
import json, pathlib, re, subprocess, sys
side, checkout, scratch = sys.argv[1:]
root, out = pathlib.Path(checkout).resolve(), pathlib.Path(scratch).resolve()
out.mkdir(parents=True, exist_ok=True)
workspace = out / 'demo'
workspace.mkdir(exist_ok=True)
if not (workspace / '.git').exists():
    subprocess.run(['git', 'init', '-q', '--initial-branch=main', str(workspace)], check=True)
    subprocess.run(['git', '-C', str(workspace), '-c', 'user.name=Scenario', '-c', 'user.email=scenario@example.invalid', 'commit', '--allow-empty', '-qm', 'Initial'], check=True)
def cli(*args):
    return subprocess.check_output(['node', 'apps/server/src/bin.ts', *args, '--base-dir', '.t3'], cwd=root, text=True)
settings_file = root / '.t3/userdata/settings.json'
providers = {d: {'driver': d, 'enabled': False} for d in ['codex', 'claudeAgent', 'cursor', 'grok', 'muse', 'pi', 'opencode', 'antigravity']}
node = subprocess.check_output(['which', 'node'], text=True).strip()
providers['devReplay'] = {'driver': 'acpRegistry', 'displayName': 'Replay (dev)', 'enabled': True, 'config': {'source': 'local', 'commandPath': node, 'commandArgs': [str(root / 'apps/server/scripts/dev-replay-agent.ts')]}}
settings = {'defaultModelSelection': {'instanceId': 'devReplay', 'model': 'multi-turn'}, 'providerInstances': providers}
settings_file.write_text(json.dumps(settings))
cli('project', 'add', str(workspace), '--title', 'History demo')
def thread(title, message, filename):
    result = json.loads(cli('thread', 'start', str(workspace), message, '--title', title, '--provider', 'devReplay', '--model', 'multi-turn', '--wait', '--json'))
    (out / f'{side}-{filename}.json').write_text(json.dumps(result))
    return result['threadId']
b = thread('Thread B', 'Thread B reference destination', 'thread-b')
environment = (root / '.t3/userdata/environment-id').read_text().strip()
agent = (root / 'apps/server/scripts/dev-replay-agent.ts').read_text()
agent = re.sub(r'export const SCENARIOS = \[[\s\S]*?\] as const;', f"export const SCENARIOS = [{{id:'multi-turn', name:'History fixture', file:'./{side}-reference.ndjson'}}] as const;", agent)
agent_file = out / f'{side}-agent.ts'
agent_file.write_text(agent)
entries = [
 {'type': 'expect_outbound', 'frame': {'kind': 'request', 'method': 'session/prompt'}},
 {'type': 'emit_inbound', 'frame': {'kind': 'notification', 'method': 'session/update', 'params': {'sessionId': 'fixture', 'update': {'sessionUpdate': 'agent_message_chunk', 'content': {'type': 'text', 'text': f'Open [Thread B](t3-thread://v2/{environment}/{b}) to continue.'}}}}},
 {'type': 'emit_inbound', 'frame': {'kind': 'response', 'method': 'session/prompt', 'result': {'stopReason': 'end_turn'}}},
]
(out / f'{side}-reference.ndjson').write_text('\n'.join(json.dumps(e) for e in entries))
providers['devReplay']['config']['commandArgs'] = [str(agent_file)]
settings_file.write_text(json.dumps(settings))
thread('Reference A', 'Show the thread reference', 'reference-a')
