Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

fix: frame Codex JSON-RPC on LF without splitting Unicode strings (#1034)

Joshua France5a1c0b902b33
diff --git a/src/harness/codex-app-server.ts b/src/harness/codex-app-server.ts
index e65f3182e..00c1afe61 100644
--- a/src/harness/codex-app-server.ts
+++ b/src/harness/codex-app-server.ts
@@ -1,5 +1,4 @@
import { spawn, type ChildProcess } from "node:child_process";
-import { createInterface } from "node:readline";
import { errMessage } from "../util/errors.ts";
export class CodexRpcError extends Error {
@@ -151,14 +150,24 @@ export class CodexAppServer {
this.processClosed = new Promise<void>((resolve) => {
resolveProcessClosed = resolve;
});
- const lines = createInterface({ input: this.process.stdout! });
- lines.on("line", (line) => {
+ let pendingOutput = "";
+ const receiveLine = (line: string) => {
this.eventTail = this.eventTail
.then(() => this.receive(line))
.catch((error) => {
this.failAll(error instanceof Error ? error : new Error(String(error)));
this.process.kill("SIGTERM");
});
+ };
+ this.process.stdout!.setEncoding("utf8");
+ this.process.stdout!.on("data", (chunk: string) => {
+ const lines = (pendingOutput + chunk).split("\n");
+ pendingOutput = lines.pop()!;
+ for (const line of lines) receiveLine(line);
+ });
+ this.process.stdout!.on("end", () => {
+ if (pendingOutput) receiveLine(pendingOutput);
+ pendingOutput = "";
});
this.process.stderr?.on("data", (chunk: Buffer) => {
this.stderr = `${this.stderr}${chunk.toString()}`.slice(-16_384);
diff --git a/test/codex-app-server.test.ts b/test/codex-app-server.test.ts
new file mode 100644
index 000000000..95dcc0dc1
--- /dev/null
+++ b/test/codex-app-server.test.ts
@@ -0,0 +1,58 @@
+import test from "node:test";
+import assert from "node:assert/strict";
+import { chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { CodexAppServer } from "../src/harness/codex-app-server.ts";
+
+const cases = [
+ { name: "U+2028 inside strings", text: "before\u2028after", ending: "\n", fragmented: false },
+ { name: "U+2029 inside strings", text: "before\u2029after", ending: "\n", fragmented: false },
+ { name: "split UTF-8 and CRLF frames", text: '界🙂\u2028\u2029\n\r\\quoted"', ending: "\r\n", fragmented: true },
+ { name: "an unterminated final frame at EOF", text: "before\u2028after", ending: "", fragmented: true },
+];
+
+for (const { name, text, ending, fragmented } of cases) {
+ test(`Codex JSON-RPC preserves ${name}`, async (t) => {
+ const dir = mkdtempSync(join(tmpdir(), "qm-codex-framing-"));
+ const binary = join(dir, "codex");
+ writeFileSync(
+ binary,
+ `#!${process.execPath}
+const readline = require("node:readline");
+readline.createInterface({ input: process.stdin }).on("line", line => {
+ const request = JSON.parse(line);
+ const text = ${JSON.stringify(text)};
+ const data = Buffer.from(
+ JSON.stringify({ method: "rawResponseItem/completed", params: { output: text } }) + "\\n" +
+ JSON.stringify({ id: request.id, result: { text } }) + ${JSON.stringify(ending)}
+ );
+ const split = ${fragmented} ? data.indexOf(Buffer.from(JSON.stringify(text).slice(1, -1))) + 1 : data.length;
+ process.stdout.write(data.subarray(0, split));
+ setTimeout(() => {
+ process.stdout.write(data.subarray(split));
+ if (${ending === ""}) process.stdout.end();
+ }, 20);
+});
+`,
+ );
+ chmodSync(binary, 0o755);
+ const notifications: unknown[] = [];
+ const server = new CodexAppServer({
+ binaryPath: binary,
+ cwd: dir,
+ onNotification: (_method, params) => {
+ notifications.push(params);
+ },
+ onRequest: async () => ({}),
+ });
+ t.after(async () => {
+ await server.close();
+ rmSync(dir, { recursive: true, force: true });
+ });
+ assert.deepEqual(await server.request("test"), { text });
+ assert.deepEqual(notifications, [{ output: text }]);
+ if (ending) assert.deepEqual(await server.request("test"), { text });
+ assert.equal(server.error(), null);
+ });
+}