import json, os, subprocess, sys, time

home = sys.argv[1]
env = dict(os.environ, CODEX_HOME=home)
p = subprocess.Popen(["codex", "app-server"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, env=env, text=True)

def send(msg):
    p.stdin.write(json.dumps(msg) + "\n"); p.stdin.flush()

def recv(id_):
    while True:
        line = p.stdout.readline()
        if not line:
            return None
        m = json.loads(line)
        if m.get("id") == id_:
            return m

send({"id": 1, "method": "initialize", "params": {"clientInfo": {"name": "repro", "version": "0"}}})
print("init", json.dumps(recv(1))[:200])
send({"method": "initialized"})
send({"id": 2, "method": "config/value/write", "params": {"keyPath": "model", "value": "gpt-5.1", "mergeStrategy": "replace"}})
print("value/write", json.dumps(recv(2))[:300])
send({"id": 3, "method": "config/batchWrite", "params": {"edits": [{"keyPath": "model_reasoning_effort", "value": "high", "mergeStrategy": "replace"}]}})
print("batchWrite", json.dumps(recv(3))[:300])
p.terminate(); p.wait()
