"""Capture a real external OpenCode session; redact operational metadata.
Usage: OPENCODE_PASSWORD=... python3 capture-instructions.py URL SESSION DIRECTORY COMMIT OUTPUT_PREFIX
Run the same command for each T3 checkout after a completed external-provider turn.
"""
import base64, json, os, re, sys, urllib.parse, urllib.request
url, session, directory, commit, output = sys.argv[1:]
headers = {'Authorization': 'Basic ' + base64.b64encode(('opencode:' + os.environ['OPENCODE_PASSWORD']).encode()).decode()}
query = '?' + urllib.parse.urlencode({'location[directory]': directory})
def get(path):
    return json.load(urllib.request.urlopen(urllib.request.Request(url + path + query, headers=headers)))
mcp = get('/api/mcp')['data']
entries = get('/api/experimental/session/' + session + '/instructions/entries')['data']
value = next(x['value'] for x in entries if x['key'] == 't3-code')
value = re.sub(r'<runtime_info>.*?</runtime_info>', '<runtime_info>[runtime attribution redacted]</runtime_info>', value, flags=re.S)
text = '\n'.join(['REAL SERVER RESPONSE — external OpenCode 2.2.7', 'T3 commit: ' + commit, 'Fresh T3 database; isolated OpenCode XDG state', '', 'GET /api/mcp (thread directory)', json.dumps({'data': mcp}), '', 'GET /api/experimental/session/<session>/instructions/entries', 'Entry key: t3-code', '', value, '', 'PR instruction block present: ' + str('<pull_request_linking>' in value), 'Runtime information retained: ' + str('<runtime_info>' in value)])
open(output + '.txt', 'w').write(text + '\n')
print(text)
