import pathlib, subprocess, sys, datetime
checkout=pathlib.Path(sys.argv[1]).resolve()
side=sys.argv[2]
run=pathlib.Path(sys.argv[3]).resolve()/(side+'-'+datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%SZ'))
run.mkdir()
workspace=run/'Recurring-task-demo'
workspace.mkdir()
subprocess.run(['git','init','--quiet',str(workspace)],check=True)
def cli(args):
 p=subprocess.run(['node','apps/server/src/bin.ts',*args,'--base-dir',str(checkout/'.t3')],cwd=checkout,text=True,capture_output=True)
 (run/('cli-'+str(len(list(run.glob('cli-*'))))+'.txt')).write_text(p.stdout+p.stderr)
 print(p.stdout,p.stderr,flush=True)
 p.check_returncode()
 return p.stdout
cli(['project','add',str(workspace),'--title','Recurring task proof'])
prompt='''Call T3 schedule_task exactly once to create a task titled "Fresh-session background wait". Use bindToCurrentThread=true, freshSession=true, schedule={"type":"interval","everyMs":60000}. The scheduled prompt must be exactly: "Run date in the foreground. Then start sleep 90 && echo SLEEP_DONE using Bash with run_in_background=true. Call t3_background_wait with that background task ID and end your turn immediately. When resumed after completion, reply exactly: first occurrence resumed. Do not create any scheduled tasks." After scheduling, reply only: Scheduled every minute in a fresh session. Do not run the scheduled prompt yourself.'''
(run/'prompt.txt').write_text(prompt)
cli(['thread','start',str(workspace),prompt,'--title','Recurring background wait','--provider','claudeAgent','--model','claude-haiku-5-5','--json'])
print('RUN_DIR='+str(run),flush=True)
