#!/usr/bin/env python3
"""Synthetic discovery fixtures. Run from either installed app checkout.
Usage: python3 seed.py SCRATCH_ROOT OPENCODE_BINARY T3_HOME
Use a new SCRATCH_ROOT and a fresh T3_HOME for each commit. OpenCode must be 2.2.5.
Start the dev app with T3CODE_DEV_SEED=0 and --home-dir T3_HOME after preparing settings.
Then rerun this script with --add-projects while that app is running.
No model turn is needed or sent.
"""
import json
import pathlib
import subprocess
import sys

root = pathlib.Path(sys.argv[1]).resolve()
binary = pathlib.Path(sys.argv[2]).resolve()
t3_home = pathlib.Path(sys.argv[3]).resolve()
provider_home = root / 'provider-home'
for name in ['amber-audit', 'birch-build', 'cobalt-check', 'delta-design', 'ember-explain', 'fern-format']:
    skill = provider_home / '.agents' / 'skills' / name
    skill.mkdir(parents=True, exist_ok=True)
    (skill / 'SKILL.md').write_text(f'---\nname: {name}\ndescription: Synthetic {name} skill for discovery verification.\n---\nUse this skill for synthetic verification.\n')
config_dir = provider_home / '.config' / 'opencode'
config_dir.mkdir(parents=True, exist_ok=True)
(config_dir / 'opencode.json').write_text(json.dumps({'$schema': 'https://opencode.ai/config.json', 'provider': {'fixture': {'npm': '@ai-sdk/openai-compatible', 'name': 'Discovery fixture', 'options': {'baseURL': 'http://127.0.0.1:9/v1', 'apiKey': 'synthetic-unused'}, 'models': {'discovery': {'name': 'Discovery fixture'}}}}}))
wrapper = root / 'opencode-isolated'
wrapper.write_text(f'#!/bin/sh\nexport HOME="{provider_home}"\nexport XDG_DATA_HOME="$HOME/.local/share"\nexport XDG_CONFIG_HOME="$HOME/.config"\nexport XDG_CACHE_HOME="$HOME/.cache"\nexec "{binary}" "$@"\n')
wrapper.chmod(0o755)
if '--add-projects' not in sys.argv:
    settings_path = t3_home / 'userdata' / 'settings.json'
    settings_path.parent.mkdir(parents=True, exist_ok=True)
    settings = json.loads(settings_path.read_text()) if settings_path.exists() else {}
    settings['providerInstances'] = {name: {'driver': name, 'enabled': False} for name in ['codex','claudeAgent','cursor','grok','pi','antigravity']}
    settings['providerInstances']['opencode'] = {'driver': 'opencode', 'enabled': True, 'config': {'binaryPath': str(wrapper)}}
    settings_path.write_text(json.dumps(settings))
else:
    for i in range(1, 7):
        title = 'Cold garden' + (f' {i}' if i > 1 else '')
        workspace = root / 'workspaces' / title.lower().replace(' ', '-')
        workspace.mkdir(parents=True, exist_ok=True)
        subprocess.run(['git', 'init', '-q', str(workspace)], check=True)
        subprocess.run(['node', 'apps/server/src/bin.ts', 'project', 'add', str(workspace), '--title', title, '--base-dir', str(t3_home)], check=True)

# Browser interaction on each side, light theme, 1280 x 800 CSS viewport:
# 1. New thread -> select each fresh Cold garden project in order.
# 2. Select native OpenCode; type "$" before sending any message.
# 3. Let discovery settle, inspect installed skill names.
# 4. Escape; click composer; End; Backspace; type "$"; inspect again.
# 5. The base's sixth project showed only OpenCode and Report, including reopen
#    about 2 minutes 38 seconds after its first discovery. Head showed all six.
# Both sides used the same synthetic skills; no protocol responses were mocked.
# The base also had four CPU busy workers during project 2; the failure
# on project 6 happened after those workers had stopped.
# Shared recordings show the same close/delete/retype sequence on project 6.
# Captures are cropped to the picker and input to exclude operational metadata.
