"""Run in either installed commit checkout, with its dev server already running.
The server must be started with T3CODE_DEV_SEED=0 and CODEX_HOME pointing to
an isolated Codex config declaring envprobe.py as a user stdio MCP server.
"""
import argparse,json,subprocess,datetime,pathlib
p=argparse.ArgumentParser(); p.add_argument('--thread');p.add_argument('--out',required=True);a=p.parse_args()
out=pathlib.Path(a.out)/datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%SZ');out.mkdir(parents=True)
prompt='Call envprobe whoami, then run echo SHELL_T3CODE_THREAD_ID=$T3CODE_THREAD_ID. Report both exact outputs. Do no other work.'
def cli(*args):
 r=subprocess.run(['node','apps/server/src/bin.ts',*args],text=True,capture_output=True,check=True)
 return r.stdout
if a.thread:
 result=cli('thread','send','--base-dir','.t3',a.thread,prompt,'--json')
 (out/'send.json').write_text(result);print(result)
else:
 cli('project','add',str(pathlib.Path.cwd()),'--title','MCP thread identity proof','--base-dir','.t3')
 for i in [1,2]:
  result=cli('thread','start','--base-dir','.t3','--provider','codex','--model','gpt-6.1-sol','--title',f'Thread {i} identity',str(pathlib.Path.cwd()),prompt,'--json')
  (out/f'thread{i}.json').write_text(result);print(result)
