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

codexCollabMockPeer.mjs

136 lines4.6 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`);
19let turnStartCount = 0;
20let activeTurn;
21​
22const rl = NodeReadline.createInterface({ input: process.stdin });
23rl.on("line", (line) => {
24 let message;
25 try {
26 message = JSON.parse(line);
27 } catch {
28 return;
29 }
30 const { id, method } = message;
31 if (method === undefined && script.serverRequests?.some((request) => request.id === id)) {
32 NodeFS.appendFileSync(
33 `${process.env.T3_CODEX_COLLAB_SCRIPT}.responses`,
34 `${JSON.stringify({ id, result: message.result, error: message.error })}\n`,
35 );
36 if (script.completeTurnOnServerResponse && activeTurn) {
37 write({
38 jsonrpc: "2.0",
39 method: "turn/completed",
40 params: {
41 threadId: script.rootThreadId,
42 turn: { ...activeTurn, status: "completed" },
43 },
44 });
45 }
46 return;
47 }
48 if (method === "initialize") {
49 write({
50 id,
51 result: {
52 userAgent: "t3-collab-mock/0.0.0",
53 codexHome: "/tmp",
54 platformFamily: "unix",
55 platformOs: "linux",
56 },
57 });
58 return;
59 }
60 if (method === "thread/start" || method === "thread/resume") {
61 write({ id, result: fixture.responses.threadStart });
62 return;
63 }
64 if (method === "turn/start") {
65 const turnId = script.turnIds?.[turnStartCount];
66 const turn = turnId
67 ? { ...fixture.responses.turnStart.turn, id: turnId }
68 : fixture.responses.turnStart.turn;
69 activeTurn = turn;
70 turnStartCount += 1;
71 write({ id, result: { ...fixture.responses.turnStart, turn } });
72 const rootThreadId = script.rootThreadId;
73 if (script.onlyFirstTurnStarts !== true || turnStartCount === 1) {
74 write({
75 jsonrpc: "2.0",
76 method: "turn/started",
77 params: { threadId: rootThreadId, turn },
78 });
79 }
80 for (const notification of script.notifications) {
81 write({ jsonrpc: "2.0", method: notification.method, params: notification.params });
82 }
83 for (const request of script.serverRequests ?? []) {
84 write({ jsonrpc: "2.0", id: request.id, method: request.method, params: request.params });
85 }
86 if (script.holdTurnOpen !== true) {
87 write({
88 jsonrpc: "2.0",
89 method: "turn/completed",
90 params: {
91 threadId: rootThreadId,
92 turn: { ...turn, status: "completed" },
93 },
94 });
95 }
96 return;
97 }
98 if (method === "turn/interrupt") {
99 // Record which thread/turn was interrupted (append-only sidecar file the
100 // test reads) so Stop coverage can assert every live child was reached.
101 // failInterruptFor simulates a dead child whose interrupt errors.
102 const target = message.params?.threadId;
103 NodeFS.appendFileSync(
104 `${process.env.T3_CODEX_COLLAB_SCRIPT}.interrupts`,
105 `${JSON.stringify({ threadId: target, turnId: message.params?.turnId })}\n`,
106 );
107 if (
108 script.expectedActiveTurnId &&
109 message.params?.threadId === script.rootThreadId &&
110 message.params?.turnId !== script.expectedActiveTurnId
111 ) {
112 write({
113 id,
114 error: {
115 code: -32000,
116 message: `expected active turn id ${message.params?.turnId} but found ${script.expectedActiveTurnId}`,
117 },
118 });
119 return;
120 }
121 if (script.failInterruptFor && script.failInterruptFor === target) {
122 write({ id, error: { code: -32000, message: "thread already closed" } });
123 return;
124 }
125 if (script.hangInterruptFor && script.hangInterruptFor === target) {
126 // Never respond: simulates a wedged child whose RPC neither resolves
127 // nor rejects. The runtime's bounded deadline must move on.
128 return;
129 }
130 write({ id, result: {} });
131 return;
132 }
133 if (id !== undefined) {
134 write({ id, result: {} });
135 }
136});