Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

codexCollabMockPeer.mjs

93 lines3.2 KB

1// Minimal codex app-server stand-in for runtime-level collab tests.
2// Speaks just enough of the protocol for CodexSessionRuntime to start a
3// session, using REAL captured responses (codexMultiAgentWire.json), then
4// replays a scripted multi-agent notification sequence read from the
5// T3_CODEX_COLLAB_SCRIPT env var (a JSON file path) when the first turn
6// starts. Runs as a plain Node process — stdlib only.
7import * as NodeFS from "node:fs";
8import * as NodeReadline from "node:readline";
9import * as NodePath from "node:path";
10import * as NodeURL from "node:url";
11​
12const here = NodePath.dirname(NodeURL.fileURLToPath(import.meta.url));
13const fixture = JSON.parse(
14 NodeFS.readFileSync(NodePath.join(here, "codexMultiAgentWire.json"), "utf8"),
15);
16const script = JSON.parse(NodeFS.readFileSync(process.env.T3_CODEX_COLLAB_SCRIPT, "utf8"));
17​
18const write = (message) => process.stdout.write(`${JSON.stringify(message)}\n`);
19​
20const rl = NodeReadline.createInterface({ input: process.stdin });
21rl.on("line", (line) => {
22 let message;
23 try {
24 message = JSON.parse(line);
25 } catch {
26 return;
27 }
28 const { id, method } = message;
29 if (method === "initialize") {
30 write({
31 id,
32 result: {
33 userAgent: "t3-collab-mock/0.0.0",
34 codexHome: "/tmp",
35 platformFamily: "unix",
36 platformOs: "linux",
37 },
38 });
39 return;
40 }
41 if (method === "thread/start" || method === "thread/resume") {
42 write({ id, result: fixture.responses.threadStart });
43 return;
44 }
45 if (method === "turn/start") {
46 write({ id, result: fixture.responses.turnStart });
47 const rootThreadId = script.rootThreadId;
48 const turn = fixture.responses.turnStart.turn;
49 write({
50 jsonrpc: "2.0",
51 method: "turn/started",
52 params: { threadId: rootThreadId, turn },
53 });
54 for (const notification of script.notifications) {
55 write({ jsonrpc: "2.0", method: notification.method, params: notification.params });
56 }
57 if (script.holdTurnOpen !== true) {
58 write({
59 jsonrpc: "2.0",
60 method: "turn/completed",
61 params: {
62 threadId: rootThreadId,
63 turn: { ...turn, status: "completed" },
64 },
65 });
66 }
67 return;
68 }
69 if (method === "turn/interrupt") {
70 // Record which thread/turn was interrupted (append-only sidecar file the
71 // test reads) so Stop coverage can assert every live child was reached.
72 // failInterruptFor simulates a dead child whose interrupt errors.
73 const target = message.params?.threadId;
74 NodeFS.appendFileSync(
75 `${process.env.T3_CODEX_COLLAB_SCRIPT}.interrupts`,
76 `${JSON.stringify({ threadId: target, turnId: message.params?.turnId })}\n`,
77 );
78 if (script.failInterruptFor && script.failInterruptFor === target) {
79 write({ id, error: { code: -32000, message: "thread already closed" } });
80 return;
81 }
82 if (script.hangInterruptFor && script.hangInterruptFor === target) {
83 // Never respond: simulates a wedged child whose RPC neither resolves
84 // nor rejects. The runtime's bounded deadline must move on.
85 return;
86 }
87 write({ id, result: {} });
88 return;
89 }
90 if (id !== undefined) {
91 write({ id, result: {} });
92 }
93});