import json, pathlib, subprocess, sys, os
repo,home,port,origin=sys.argv[1:]
env={**os.environ,'T3CODE_PORT':port,'T3CODE_DEV_SEED':'0'}
transcript=[{'type':'expect_outbound','frame':{'kind':'request','method':'session/prompt','params':'<any>'}}, {'type':'emit_inbound','frame':{'kind':'notification','method':'session/update','params':{'sessionId':'dev-replay-session','update':{'sessionUpdate':'plan_update','plan':{'type':'markdown','planId':'contributing-plan','content':'# Plan: add CONTRIBUTING.md\n\n1. Describe how to report bugs.\n2. Explain how to run checks.\n3. Document pull request expectations.'}}}}},{'type':'emit_inbound','frame':{'kind':'response','method':'session/prompt','result':{'stopReason':'end_turn'}}}]
pathlib.Path(repo,'apps/server/scripts/dev-replay/showcase_transcript.ndjson').write_text('\n'.join(map(json.dumps,transcript))+'\n')
def cli(*args):
 p=subprocess.run(['node','apps/server/src/bin.ts',*args,'--base-dir',home],cwd=repo,env=env,text=True,capture_output=True)
 if p.returncode: raise RuntimeError(p.stderr+p.stdout)
 return p.stdout
workspace=pathlib.Path(home,'workspaces','Demo');workspace.mkdir(parents=True,exist_ok=True);(workspace/'README.md').write_text('# Demo\n')
for args in [['init','-q','--initial-branch=main'],['add','.'],['-c','user.name=Evidence','-c','user.email=evidence@example.invalid','commit','-qm','Initial commit']]: subprocess.run(['git',*args],cwd=workspace,check=True)
cli('project','add',str(workspace),'--title','Demo')
thread=cli('thread','start',str(workspace),'Propose adding CONTRIBUTING.md','--title','Plan: add CONTRIBUTING.md','--provider','devReplay','--model','showcase','--wait','--json')
pathlib.Path(home,'thread.json').write_text(thread)
scopes=['orchestration:read','orchestration:operate','settings:write','providers:manage','environment:maintain','preview:operate','diagnostics:read','terminal:read','terminal:operate','source-control:write','filesystem:read','relay:read','relay:write','access:read','access:write']
for label,grant in [('Read-only files',scopes),('Full access',scopes+['filesystem:write'])]:
 flags=[item for scope in grant for item in ['--scope',scope]]
 out=cli('auth','pairing','create','--ttl','1h','--label',label,'--base-url',origin,*flags)
 url=next(line[10:] for line in out.splitlines() if line.startswith('Pair URL: '))
 from urllib.parse import urlencode
 pathlib.Path(home,'restricted.link' if label=='Read-only files' else 'full.link').write_text('t3code-dev://connections/new?'+urlencode({'pairingUrl':url,'autoConnect':'1'}))
print('Seeded Demo and proposed plan; created restricted and full-access pairing links.')
