Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Keep the Codex thread/start deadline as a non-retryable timeout. (#1404)

Sina Matian1b615fddde4b
diff --git a/src/harness/codex-harness.ts b/src/harness/codex-harness.ts
index 9e44c7c81..f671fd05d 100644
--- a/src/harness/codex-harness.ts
+++ b/src/harness/codex-harness.ts
@@ -953,9 +953,12 @@ export function createCodexHarness(opts: CodexHarnessOptions = {}): Harness {
}
let started: { thread: { id: string }; model?: string };
try {
- const requestTimeoutMs = deadline ? Math.max(1, deadline - Date.now()) : CODEX_START_TIMEOUT_MS;
+ const requestTimeoutMs = deadline
+ ? Math.max(1, deadline - Date.now())
+ : (opts.appServerStartTimeoutMs ?? CODEX_START_TIMEOUT_MS);
let requestTimer: NodeJS.Timeout | undefined;
const requestAbort = new AbortController();
+ let threadStartTimedOut = false;
started = await awaitSetup(
Promise.race([
rt.server.request(
@@ -966,13 +969,23 @@ export function createCodexHarness(opts: CodexHarnessOptions = {}): Harness {
),
new Promise<never>((_, reject) => {
requestTimer = setTimeout(() => {
+ threadStartTimedOut = true;
requestAbort.abort();
reject(new NonRetryableTurnError("Codex thread/start request timed out"));
}, requestTimeoutMs);
}),
- ]).finally(() => {
- if (requestTimer) clearTimeout(requestTimer);
- }),
+ ])
+ .finally(() => {
+ if (requestTimer) clearTimeout(requestTimer);
+ })
+ .catch((error: unknown) => {
+ if (threadStartTimedOut) {
+ const timeoutError = new NonRetryableTurnError("Codex thread/start request timed out");
+ timeoutError.cause = error;
+ throw timeoutError;
+ }
+ throw error;
+ }),
);
} catch (error) {
return failSetup(error);
diff --git a/test/codex-harness.test.ts b/test/codex-harness.test.ts
index 963267175..1b0db928a 100644
--- a/test/codex-harness.test.ts
+++ b/test/codex-harness.test.ts
@@ -220,6 +220,31 @@ process.stdin.resume();
return path;
}
+function pendingThreadStartCodexBinary(dir: string): string {
+ const path = join(dir, "pending-thread-start-codex");
+ writeFileSync(
+ path,
+ `#!${process.execPath}
+const fs = require("node:fs");
+const readline = require("node:readline");
+const rl = readline.createInterface({ input: process.stdin });
+const send = (value) => process.stdout.write(JSON.stringify(value) + "\\n");
+rl.on("line", (line) => {
+ const msg = JSON.parse(line);
+ if (msg.method === "initialize") return send({ id: msg.id, result: {} });
+ if (msg.method === "initialized") return;
+ if (msg.method === "thread/start") fs.writeFileSync(${JSON.stringify(join(dir, "thread-started"))}, "started");
+});
+process.on("SIGTERM", () => {
+ fs.writeFileSync(${JSON.stringify(join(dir, "closed"))}, "closed");
+ process.exit(0);
+});
+`,
+ );
+ chmodSync(path, 0o755);
+ return path;
+}
+
function pendingTurnStartCodexBinary(dir: string): string {
const path = join(dir, "pending-turn-start-codex");
writeFileSync(
@@ -1119,6 +1144,75 @@ test("cancelling an OAuth startup after spawn closes the provider", async (t) =>
assert.equal(readFileSync(join(dir, "closed"), "utf8"), "closed");
});
+test("Codex classifies a thread/start deadline as a non-retryable timeout", async (t) => {
+ const dir = mkdtempSync(join(tmpdir(), "qm-codex-thread-start-timeout-test-"));
+ const harness = createCodexHarness({
+ binaryPath: pendingThreadStartCodexBinary(dir),
+ env: testHarnessEnv(dir),
+ appServerStartTimeoutMs: 500,
+ turnWallClockMs: 0,
+ });
+ t.after(async () => {
+ await harness.turns.close?.();
+ rmSync(dir, { recursive: true, force: true });
+ });
+ const scope = { kind: "org", id: "test" } as unknown as ScopeId;
+ await assert.rejects(
+ harness.turns.runTurn({
+ session: { id: "thread-start-timeout" } as Session,
+ input: "hi",
+ systemPrompt: "be concise",
+ history: [],
+ tools: {} as HarnessTurnInput["tools"],
+ scopeLabel: scope,
+ orgScopeId: scope,
+ emit: async (entry) =>
+ ({ ...entry, sessionId: "thread-start-timeout", seq: 1, createdAt: Date.now() }) as SessionEntry,
+ recordModelCall: () => {},
+ }),
+ (error: unknown) =>
+ error instanceof NonRetryableTurnError &&
+ /thread\/start request timed out/.test(error.message) &&
+ error.message !== "Codex app-server request cancelled",
+ );
+});
+
+test("cancelling a pending Codex thread/start is not relabeled as a startup timeout", async (t) => {
+ const dir = mkdtempSync(join(tmpdir(), "qm-codex-cancel-thread-start-test-"));
+ const harness = createCodexHarness({
+ binaryPath: pendingThreadStartCodexBinary(dir),
+ env: testHarnessEnv(dir),
+ turnWallClockMs: 3_000,
+ });
+ t.after(async () => {
+ await harness.turns.close?.();
+ rmSync(dir, { recursive: true, force: true });
+ });
+ const cancel = new AbortController();
+ const scope = { kind: "org", id: "test" } as unknown as ScopeId;
+ const turn = harness.turns.runTurn({
+ session: { id: "cancel-thread-start" } as Session,
+ input: "hi",
+ systemPrompt: "be concise",
+ history: [],
+ tools: {} as HarnessTurnInput["tools"],
+ scopeLabel: scope,
+ orgScopeId: scope,
+ cancel: cancel.signal,
+ emit: async (entry) =>
+ ({ ...entry, sessionId: "cancel-thread-start", seq: 1, createdAt: Date.now() }) as SessionEntry,
+ recordModelCall: () => {},
+ });
+ for (let attempt = 0; attempt < 100 && !existsSync(join(dir, "thread-started")); attempt += 1)
+ await new Promise((resolve) => setTimeout(resolve, 10));
+ assert.equal(existsSync(join(dir, "thread-started")), true);
+ cancel.abort();
+ assert.deepEqual(await turn, { reply: "", stopped: true });
+ for (let attempt = 0; attempt < 100 && !existsSync(join(dir, "closed")); attempt += 1)
+ await new Promise((resolve) => setTimeout(resolve, 10));
+ assert.equal(readFileSync(join(dir, "closed"), "utf8"), "closed");
+});
+
test("cancelling a pending Codex turn/start stops and closes the runtime", async (t) => {
const dir = mkdtempSync(join(tmpdir(), "qm-codex-cancel-turn-start-test-"));
const harness = createCodexHarness({