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