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

codexCollabMockPeer.mjs

192 lines6.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`);
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 === "account/read") {
61 write({ id, result: { account: { type: "apiKey" }, requiresOpenaiAuth: false } });
62 return;
63 }
64 if (method === "skills/list" || method === "model/list") {
65 write({ id, result: { data: [] } });
66 return;
67 }
68 if (method === "thread/start") {
69 write({ id, result: fixture.responses.threadStart });
70 return;
71 }
72 if (method === "thread/resume") {
73 if (script.recordRequests) {
74 NodeFS.appendFileSync(
75 `${process.env.T3_CODEX_COLLAB_SCRIPT}.requests`,
76 `${JSON.stringify({ method, params: message.params })}\n`,
77 );
78 }
79 const threadId = message.params?.threadId;
80 const childSnapshot = script.childResumeSnapshots?.[threadId];
81 if (script.resumeRequestMarker) {
82 write({
83 jsonrpc: "2.0",
84 method: "serverRequest/resolved",
85 params: {
86 threadId: script.rootThreadId,
87 requestId: script.resumeRequestMarker,
88 },
89 });
90 }
91 if (childSnapshot?.hang) {
92 return;
93 }
94 if (childSnapshot?.error) {
95 write({ id, error: { code: -32000, message: childSnapshot.error } });
96 return;
97 }
98 if (childSnapshot) {
99 write({
100 id,
101 result: {
102 ...fixture.responses.threadStart,
103 model: childSnapshot.model,
104 reasoningEffort: childSnapshot.reasoningEffort,
105 thread: {
106 ...fixture.responses.threadStart.thread,
107 id: threadId,
108 sessionId: threadId,
109 },
110 },
111 });
112 for (const notification of childSnapshot.notifications ?? []) {
113 write({ jsonrpc: "2.0", method: notification.method, params: notification.params });
114 }
115 return;
116 }
117 write({ id, result: fixture.responses.threadStart });
118 return;
119 }
120 if (method === "turn/start") {
121 const turnId = script.turnIds?.[turnStartCount];
122 const turn = turnId
123 ? { ...fixture.responses.turnStart.turn, id: turnId }
124 : fixture.responses.turnStart.turn;
125 activeTurn = turn;
126 turnStartCount += 1;
127 write({ id, result: { ...fixture.responses.turnStart, turn } });
128 const rootThreadId = script.rootThreadId;
129 if (script.onlyFirstTurnStarts !== true || turnStartCount === 1) {
130 write({
131 jsonrpc: "2.0",
132 method: "turn/started",
133 params: { threadId: rootThreadId, turn },
134 });
135 }
136 for (const notification of script.notifications) {
137 write({ jsonrpc: "2.0", method: notification.method, params: notification.params });
138 }
139 for (const request of script.serverRequests ?? []) {
140 write({ jsonrpc: "2.0", id: request.id, method: request.method, params: request.params });
141 }
142 if (script.holdTurnOpen !== true) {
143 write({
144 jsonrpc: "2.0",
145 method: "turn/completed",
146 params: {
147 threadId: rootThreadId,
148 turn: { ...turn, status: "completed" },
149 },
150 });
151 }
152 return;
153 }
154 if (method === "turn/interrupt") {
155 // Record which thread/turn was interrupted (append-only sidecar file the
156 // test reads) so Stop coverage can assert every live child was reached.
157 // failInterruptFor simulates a dead child whose interrupt errors.
158 const target = message.params?.threadId;
159 NodeFS.appendFileSync(
160 `${process.env.T3_CODEX_COLLAB_SCRIPT}.interrupts`,
161 `${JSON.stringify({ threadId: target, turnId: message.params?.turnId })}\n`,
162 );
163 if (
164 script.expectedActiveTurnId &&
165 message.params?.threadId === script.rootThreadId &&
166 message.params?.turnId !== script.expectedActiveTurnId
167 ) {
168 write({
169 id,
170 error: {
171 code: -32000,
172 message: `expected active turn id ${message.params?.turnId} but found ${script.expectedActiveTurnId}`,
173 },
174 });
175 return;
176 }
177 if (script.failInterruptFor && script.failInterruptFor === target) {
178 write({ id, error: { code: -32000, message: "thread already closed" } });
179 return;
180 }
181 if (script.hangInterruptFor && script.hangInterruptFor === target) {
182 // Never respond: simulates a wedged child whose RPC neither resolves
183 // nor rejects. The runtime's bounded deadline must move on.
184 return;
185 }
186 write({ id, result: {} });
187 return;
188 }
189 if (id !== undefined) {
190 write({ id, result: {} });
191 }
192});