#!/usr/bin/env python3
"""Collect only public proof data from a running isolated T3/Pi reproduction.

Start each checkout with T3CODE_DEV_SEED=0 and either `env -u T3CODE_THREAD_ID`
or T3CODE_THREAD_ID=unrelated-inherited-thread. Add Pi through Settings > Providers.
Seed an empty git project with `t3 project add --base-dir .t3 --title
'Pi environment proof' <demo-repo>`. In a new Pi / Claude Haiku 5.5 thread send:
Use your bash tool to run exactly: echo "thread=$T3CODE_THREAD_ID" and then reply with only the exact output.

After completion, run this script with the checkout, server PID and thread ID.
The PID must be the owner of the isolated server port (ss -H -ltnp).
This script never prints credentials, process command lines or local paths.
Browser driver: type the exact prompt into [contenteditable=true], press Enter,
expand 'Worked for', expand 'Used 1 tool and ran 1 command', and expand
[data-v2-item-type=command_execution]. Capture the tool output and reply.
Privacy CSS used for captures: [data-thread-panel-run-context]{visibility:hidden!important}
"""
import argparse,json,pathlib,subprocess
p=argparse.ArgumentParser()
p.add_argument('--checkout',required=True)
p.add_argument('--server-pid',type=int,required=True)
p.add_argument('--thread',required=True)
p.add_argument('--side',required=True)
a=p.parse_args()
def env_for(pid):
 return pathlib.Path(f'/proc/{pid}/environ').read_bytes().split(b'\0')
def thread_values(env):
 return [x.decode().split('=',1)[1] for x in env if x.startswith(b'T3CODE_THREAD_ID=')]
raw=json.loads(subprocess.check_output(['node','apps/server/src/bin.ts','thread','show','--base-dir','.t3','--json',a.thread],cwd=a.checkout,text=True))
pi=[]
for child in pathlib.Path('/proc').iterdir():
 if not child.name.isdigit():continue
 try:
  env=env_for(child.name)
  if b'T3_PI_RUNTIME_MODE=full-access' not in env:continue
  ancestor=int(child.name)
  for _ in range(30):
   if ancestor==a.server_pid:break
   ancestor=int(pathlib.Path(f'/proc/{ancestor}/stat').read_text().split(') ')[1].split()[1])
  else:continue
  row={'threadEnv':thread_values(env),'hasMcpUrl':any(v.startswith(b'T3_MCP_URL=') for v in env),'runtimeMode':'full-access'}
  if row not in pi:pi.append(row)
 except (OSError,ValueError):pass
print(json.dumps({'side':a.side,'commit':subprocess.check_output(['git','rev-parse','HEAD'],cwd=a.checkout,text=True).strip(),'threadId':a.thread,'provider':raw['thread']['providerInstanceId'],'model':raw['thread']['model'],'status':raw['thread']['status'],'serverThreadEnv':thread_values(env_for(a.server_pid)),'piProcesses':pi,'messages':[{k:m[k] for k in ['role','text']} for m in raw['messages']]},indent=2))
