114 lines · 3.8 KB · JavaScript
RawDownload
// Minimal codex app-server stand-in for runtime-level collab tests.// Speaks just enough of the protocol for CodexSessionRuntime to start a// session, using REAL captured responses (codexMultiAgentWire.json), then// replays a scripted multi-agent notification sequence read from the// T3_CODEX_COLLAB_SCRIPT env var (a JSON file path) when the first turn// starts. Runs as a plain Node process — stdlib only.import * as NodeFS from "node:fs";import * as NodeReadline from "node:readline";import * as NodePath from "node:path";import * as NodeURL from "node:url"; const here = NodePath.dirname(NodeURL.fileURLToPath(import.meta.url));const fixture = JSON.parse(  NodeFS.readFileSync(NodePath.join(here, "codexMultiAgentWire.json"), "utf8"),);const script = JSON.parse(NodeFS.readFileSync(process.env.T3_CODEX_COLLAB_SCRIPT, "utf8")); const write = (message) => process.stdout.write(`${JSON.stringify(message)}\n`);let turnStartCount = 0; const rl = NodeReadline.createInterface({ input: process.stdin });rl.on("line", (line) => {  let message;  try {    message = JSON.parse(line);  } catch {    return;  }  const { id, method } = message;  if (method === "initialize") {    write({      id,      result: {        userAgent: "t3-collab-mock/0.0.0",        codexHome: "/tmp",        platformFamily: "unix",        platformOs: "linux",      },    });    return;  }  if (method === "thread/start" || method === "thread/resume") {    write({ id, result: fixture.responses.threadStart });    return;  }  if (method === "turn/start") {    const turnId = script.turnIds?.[turnStartCount];    const turn = turnId      ? { ...fixture.responses.turnStart.turn, id: turnId }      : fixture.responses.turnStart.turn;    turnStartCount += 1;    write({ id, result: { ...fixture.responses.turnStart, turn } });    const rootThreadId = script.rootThreadId;    if (script.onlyFirstTurnStarts !== true || turnStartCount === 1) {      write({        jsonrpc: "2.0",        method: "turn/started",        params: { threadId: rootThreadId, turn },      });    }    for (const notification of script.notifications) {      write({ jsonrpc: "2.0", method: notification.method, params: notification.params });    }    if (script.holdTurnOpen !== true) {      write({        jsonrpc: "2.0",        method: "turn/completed",        params: {          threadId: rootThreadId,          turn: { ...turn, status: "completed" },        },      });    }    return;  }  if (method === "turn/interrupt") {    // Record which thread/turn was interrupted (append-only sidecar file the    // test reads) so Stop coverage can assert every live child was reached.    // failInterruptFor simulates a dead child whose interrupt errors.    const target = message.params?.threadId;    NodeFS.appendFileSync(      `${process.env.T3_CODEX_COLLAB_SCRIPT}.interrupts`,      `${JSON.stringify({ threadId: target, turnId: message.params?.turnId })}\n`,    );    if (      script.expectedActiveTurnId &&      message.params?.threadId === script.rootThreadId &&      message.params?.turnId !== script.expectedActiveTurnId    ) {      write({        id,        error: {          code: -32000,          message: `expected active turn id ${message.params?.turnId} but found ${script.expectedActiveTurnId}`,        },      });      return;    }    if (script.failInterruptFor && script.failInterruptFor === target) {      write({ id, error: { code: -32000, message: "thread already closed" } });      return;    }    if (script.hangInterruptFor && script.hangInterruptFor === target) {      // Never respond: simulates a wedged child whose RPC neither resolves      // nor rejects. The runtime's bounded deadline must move on.      return;    }    write({ id, result: {} });    return;  }  if (id !== undefined) {    write({ id, result: {} });  }});