import subprocess,pathlib,json,sys,datetime
root=pathlib.Path(__file__).parent
side,case=sys.argv[1:3]
cfg=json.loads((root/(side+'-workspace.json')).read_text())
cli=['node','apps/server/src/bin.ts']
def run(args):return subprocess.run(args,cwd=cfg['checkout'],capture_output=True,text=True).stdout
commands={'quoted':"echo '<<EOF'; glab mr create --fill --yes\ntrue",'control':"echo 'hello'; glab mr create --fill --yes\ntrue",'comment':"# <<EOF\nglab mr create --fill --yes\ntrue",'heredoc':"cat <<'EOF'\nglab mr create --fill --yes\nEOF\nglab mr view 1 --output json --jq .web_url\ntrue"}
if case=='switch':
 thread=json.loads((root/(side+'-quoted.json')).read_text())['threadId']
 command='git checkout main'
 args=['thread','send',thread]
else:
 subprocess.run(['git','checkout', 'main' if case=='heredoc' else side+'-'+case],cwd=cfg['workspace'],check=True,capture_output=True)
 command=commands[case]
 args=['thread','start',cfg['workspace'],'--title',case.capitalize()+' heredoc proof','--provider','opencode','--model','openai/gpt-6-luna']
prompt='Run exactly one bash tool call with the following command string, preserving all quotes and newlines. Do not split it into separate tool calls. Do not use any T3 tools, and do not manually link a merge request. Reply only Done.\n\n'+command
result=run(cli+args+[prompt,'--wait','--json','--base-dir',cfg['home']])
(root/(side+'-'+case+'.json')).write_text(result)
print(result)
