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

codexCollabMockPeer.mjs

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