"""Manual real-provider retry proof driver. No timer or provider code is modified.
Run from an installed evidence checkout with its isolated dev server running.
Usage: python3 reproduce.py --home .t3 --workspace /path/to/synthetic/project --provider codex --model gpt-6-luna --mode-file /path/to/proxy-mode
Start proxy.py separately: python3 proxy.py PORT MODE_FILE.
Configure that provider instance's HTTPS_PROXY and HTTP_PROXY to http://127.0.0.1:PORT.
Use dedicated provider config homes and existing authorized API credentials; never upload credentials.
For Claude use --provider claudeAgent --model claude-haiku-5-5 --hold-after-retry.
Open each returned thread through /ENVIRONMENT_ID/URL_ENCODED_THREAD_ID in the paired web client.
Record the retrying screen through at least 12 minutes, keeping the same viewport/theme.
"""
import argparse,subprocess,json,time,pathlib,sqlite3,datetime
p=argparse.ArgumentParser();p.add_argument('--home',required=True);p.add_argument('--workspace',required=True);p.add_argument('--provider',required=True);p.add_argument('--model',required=True);p.add_argument('--mode-file',required=True);p.add_argument('--hold-after-retry',action='store_true');a=p.parse_args()
mode=pathlib.Path(a.mode_file)
def cli(*args):
 r=subprocess.run(['node','apps/server/src/bin.ts','thread',*args,'--base-dir',a.home,'--json'],capture_output=True,text=True,check=True)
 return json.loads(r.stdout)
def launch(title):
 return cli('start','--provider',a.provider,'--model',a.model,'--title',title,a.workspace,'Reply with just the word OK.')
mode.write_text('pass');control=launch(a.provider+' control');print('control',control,flush=True)
while True:
 s=cli('show',control['threadId'])
 if s['thread']['status'] not in ['queued','preparing','starting','running','waiting']:break
 time.sleep(1)
print('control outcome',s,flush=True)
if s['thread']['status']!='completed' or not any(m['role']=='assistant' and m['text']=='OK' for m in s['messages']):raise SystemExit('Control failed')
mode.write_text('refuse');fault=launch(a.provider+' continuous provider retry');print('fault',fault,flush=True)
c=sqlite3.connect('file:'+str(pathlib.Path(a.home).resolve()/'userdata/statev2.sqlite')+'?mode=ro',uri=True)
for tick in range(0,14*60,5):
 s=cli('show',fault['threadId']);rows=c.execute("select payload_json from orchestration_v2_projection_turn_items where thread_id=? and json_extract(payload_json,'$.title')='Provider retry'",(fault['threadId'],)).fetchall()
 retries=[{k:j.get(k) for k in ['status','startedAt','updatedAt','completedAt','retry','failure']} for j in [json.loads(r[0]) for r in rows]]
 print(json.dumps({'observedAt':datetime.datetime.now(datetime.timezone.utc).isoformat(),'runStatus':s['thread']['status'],'retries':retries}),flush=True)
 if retries and a.hold_after_retry:mode.write_text('hold')
 time.sleep(5)
