codexwire 0.1.1

A Rust client for the OpenAI Codex app-server: its JSON-RPC protocol, typed
Documentation
#!/usr/bin/env python3
"""A stand-in for `codex app-server`, for tests/spawn.rs.

It writes a line that isn't UTF-8 to stdout and to stderr, then answers
`initialize` like the real server and `fake/info` with what it was started
with (its args, cwd, environment and pid), until its stdin ends.
"""

import json
import os
import sys

if sys.argv[1:2] != ["app-server"]:
    sys.exit("usage: fake_codex app-server [ARGS]")

out = sys.stdout.buffer
err = sys.stderr.buffer
err.write(b"\xff\xfe not utf-8\nfake stderr ok\n")
err.flush()
out.write(b"\xff\xfe not utf-8\n")
out.flush()


def send(msg):
    out.write(json.dumps(msg).encode() + b"\n")
    out.flush()


for line in sys.stdin.buffer:
    msg = json.loads(line)
    if "id" not in msg:
        continue
    if msg["method"] == "initialize":
        name = msg["params"]["clientInfo"]["name"]
        result = {
            "userAgent": f"{name}/0.167.0 (fake) unknown ({name}; 0.1.0)",
            "codexHome": os.environ.get("CODEX_HOME", ""),
            "platformFamily": "unix",
            "platformOs": "linux",
        }
    elif msg["method"] == "fake/info":
        result = {
            "args": sys.argv[2:],
            "cwd": os.getcwd(),
            "env": dict(os.environ),
            "pid": os.getpid(),
        }
    else:
        send({"id": msg["id"], "error": {"code": -32601, "message": "unknown method"}})
        continue
    send({"id": msg["id"], "result": result})