Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Settle Codex RPC replies without waiting on notification callbacks. (#1403)

Sina Matian1e2a57e18c7f
diff --git a/src/harness/codex-app-server.ts b/src/harness/codex-app-server.ts
index b02d17a01..9ae310425 100644
--- a/src/harness/codex-app-server.ts
+++ b/src/harness/codex-app-server.ts
@@ -134,6 +134,7 @@ export class CodexAppServer {
private readonly cancelledRequestIds = new Set<JsonRpcId>();
private writeTail = Promise.resolve();
private eventTail = Promise.resolve();
+ private notificationTail = Promise.resolve();
private stderr = "";
private closed = false;
private closeError: Error | null = null;
@@ -297,11 +298,14 @@ export class CodexAppServer {
return;
}
if (!message.method) return;
+ const method = message.method;
if (message.id === undefined) {
- await this.options.onNotification(message.method, message.params);
+ this.notificationTail = this.notificationTail
+ .then(() => this.options.onNotification(method, message.params))
+ .catch((error) => this.failTransport(error));
return;
}
- void this.respond(message.id, message.method, message.params).catch((error) => this.failTransport(error));
+ void this.respond(message.id, method, message.params).catch((error) => this.failTransport(error));
}
private async respond(id: JsonRpcId, method: string, params: unknown): Promise<void> {
diff --git a/test/codex-app-server.test.ts b/test/codex-app-server.test.ts
index f98afea97..3d2b2266a 100644
--- a/test/codex-app-server.test.ts
+++ b/test/codex-app-server.test.ts
@@ -170,6 +170,62 @@ readline.createInterface({ input: process.stdin }).on("line", line => {
});
}
+test("Codex RPC replies settle while a notification callback is still held", { timeout: 3000 }, async (t) => {
+ const dir = mkdtempSync(join(tmpdir(), "qm-codex-notification-queue-"));
+ const binary = join(dir, "codex");
+ writeFileSync(
+ binary,
+ `#!${process.execPath}
+const readline = require("node:readline");
+const send = message => process.stdout.write(JSON.stringify(message) + "\\n");
+readline.createInterface({ input: process.stdin }).on("line", line => {
+ const message = JSON.parse(line);
+ if (message.method === "start") {
+ send({ method: "progress", params: 1 });
+ send({ method: "progress", params: 2 });
+ send({ id: message.id, result: "started" });
+ }
+});
+`,
+ );
+ chmodSync(binary, 0o755);
+ const releaseFirst = Promise.withResolvers<void>();
+ const releaseSecond = Promise.withResolvers<void>();
+ const firstStarted = Promise.withResolvers<void>();
+ const secondStarted = Promise.withResolvers<void>();
+ const notifications: unknown[] = [];
+ const server = new CodexAppServer({
+ binaryPath: binary,
+ cwd: dir,
+ onNotification: async (_method, params) => {
+ notifications.push(params);
+ if (params === 1) {
+ firstStarted.resolve();
+ await releaseFirst.promise;
+ } else {
+ secondStarted.resolve();
+ await releaseSecond.promise;
+ }
+ },
+ onRequest: async () => ({}),
+ });
+ t.after(async () => {
+ releaseFirst.resolve();
+ releaseSecond.resolve();
+ await server.close();
+ rmSync(dir, { recursive: true, force: true });
+ });
+ assert.equal(await server.request("start", {}, AbortSignal.timeout(1000)), "started");
+ await firstStarted.promise;
+ assert.deepEqual(notifications, [1]);
+ assert.equal(server.error(), null);
+ releaseFirst.resolve();
+ await secondStarted.promise;
+ assert.deepEqual(notifications, [1, 2]);
+ releaseSecond.resolve();
+ assert.equal(server.error(), null);
+});
+
test("a waiting tool cannot block another thread's RPC response or tool call", { timeout: 5000 }, async (t) => {
const dir = mkdtempSync(join(tmpdir(), "qm-codex-wait-"));
const binary = join(dir, "codex");