#!/usr/bin/env python3
"""Run from either installed checkout with its fresh dev server already running.
Start server with T3CODE_DEV_SEED=0 and GOOGLE_GEMINI_BASE_URL set to the
available Gemini API endpoint. Uses real agy 1.5.22 and gemini-4-neon.
Usage: python3 reproduce.py OUTPUT_DIRECTORY
The browser follow-up used on both sides is FOLLOW_UP below.
"""
import datetime, json, pathlib, sqlite3, subprocess, sys
out = pathlib.Path(sys.argv[1]) / datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%SZ')
out.mkdir(parents=True)
p = pathlib.Path('.t3/userdata/settings.json')
s = json.loads(p.read_text())
s.setdefault('providerInstances', {})['antigravity'] = {
    'driver': 'antigravity', 'config': {
        'enabled': True, 'authMethod': 'gemini-api-key', 'apiKey': 'proof-dummy'}}
s['mcpServers'] = {
    'better-auth': {'transport': {'type': 'http', 'url': 'https://mcp.better-auth.com/mcp'}},
    'better-auth-stdio': {'transport': {'type': 'stdio', 'command': 'npx',
        'args': ['-y', 'mcp-remote', 'https://mcp.better-auth.com/mcp']}}}
p.write_text(json.dumps(s, indent=2))
cli = ['node', 'apps/server/src/bin.ts']
subprocess.run(cli + ['project', 'add', '--base-dir', '.t3', '--title', 'MCP proof', '.'], check=True)
PROMPT = 'List the exact server names and tool names in your <mcp_servers>. Call search_docs on better-auth-stdio with query "session". Then call list_resources with ServerName better-auth. If better-auth exposes search_docs, call it too with query "session". Report each actual result and error. Do not use shell, browser, or other substitutes.'
FOLLOW_UP = 'Repeat the actual checks now: search_docs on better-auth-stdio for "session", list_resources with ServerName "better-auth", and search_docs on better-auth for "session" if available. Then give a compact table with server, transport, and actual result. Include the exact HTTP error if any. No substitutes.'
r = subprocess.run(cli + ['thread', 'start', '--base-dir', '.t3', '--provider', 'antigravity', '--model', 'gemini-4-neon', '--title', 'MCP transport proof', '--wait', '--json', '.', PROMPT], capture_output=True, text=True)
(out / 'run.json').write_text(r.stdout)
(out / 'error.log').write_text(r.stderr)
thread = json.loads(r.stdout)['threadId']
# Open this thread directly in the paired web client. Collapse sidebar and
# details for privacy; record typing/submitting FOLLOW_UP, then wait for the
# turn to finish. View actual tools by expanding Worked for ... > Used N tools.
# Evidence viewport: 1280x800 CSS pixels, light theme, device scale factor 2.
db = sqlite3.connect('file:.t3/userdata/statev2.sqlite?mode=ro', uri=True)
rows = [json.loads(r[0]) for r in db.execute("select payload_json from orchestration_v2_projection_turn_items where thread_id=? and type='dynamic_tool' order by ordinal", (thread,))]
keys = ['type', 'status', 'title', 'input', 'output', 'startedAt', 'completedAt']
(out / 'tool-results.json').write_text(json.dumps([{k: row[k] for k in keys if k in row} for row in rows], indent=2))
sys.exit(r.returncode)
