fix(codex): end the app-server turn when the event backlog overflows

A push that overflows the AsyncEventQueue backlog throws from the socket
notification and tool-call callbacks, outside run()'s catch. The error
escaped as an uncaught exception and the consumer waited for a terminal
event that never came. Route those pushes through a guard that pushes a
terminal error, closes the queue and settles the turn.
This commit is contained in:
serenity
2026-11-18 22:07:43 -03:00
committed by diegosouzapw
parent 47a79fcba0
commit 84b3a39598
2 changed files with 62 additions and 15 deletions

View File

@@ -331,11 +331,34 @@ export class CodexAppServerExecutor extends BaseExecutor {
events.close();
markTerminated();
};
const failTurn = (err: unknown) => {
if (terminated) return;
const message = sanitizeErrorMessage(err instanceof Error ? err.message : err);
const status = drainAwareFailureStatus(502);
events.push({
type: "error",
message,
status,
errorType: status === 499 ? "client_disconnected" : "provider_error",
code: status === 499 ? "client_disconnected" : "codex_app_server_turn_failed",
});
events.close();
markTerminated();
};
// Socket callbacks run outside this function's catch: a push that overflows the event
// backlog must still end the turn with an error the consumer sees.
const pushEvent = (event: AdapterEvent) => {
try {
events.push(event);
} catch (err) {
failTurn(err);
}
};
try {
// Setup responses and socket closure can arrive in the same frame batch.
client.onNotification((method, params) => {
if (terminated) return;
const isTerminal = translateNotification(method, params, (event) => events.push(event));
const isTerminal = translateNotification(method, params, pushEvent);
if (isTerminal) {
events.close();
markTerminated();
@@ -394,7 +417,7 @@ export class CodexAppServerExecutor extends BaseExecutor {
const toolParams = (
params && typeof params === "object" ? params : {}
) as DynamicToolCallLike;
translateToolCall(toolParams, (event) => events.push(event));
translateToolCall(toolParams, pushEvent);
// Settle the app-server request so the socket does not stall. The router
// does not have the tool output (the harness will produce it next turn),
// so we report the passthrough as an unsuccessful in-line result and end
@@ -447,19 +470,7 @@ export class CodexAppServerExecutor extends BaseExecutor {
// down the socket mid-turn and the queue never closes (request hangs).
await turnDone;
} catch (err) {
if (!terminated) {
const message = sanitizeErrorMessage(err instanceof Error ? err.message : err);
const status = drainAwareFailureStatus(502);
events.push({
type: "error",
message,
status,
errorType: status === 499 ? "client_disconnected" : "provider_error",
code: status === 499 ? "client_disconnected" : "codex_app_server_turn_failed",
});
events.close();
markTerminated();
}
failTurn(err);
} finally {
client.close();
}

View File

@@ -487,6 +487,42 @@ test("execute (non-stream): a generic turn failure keeps the 200 Responses error
assert.equal(body.error?.code, "codex_app_server_turn_failed");
});
test("execute (non-stream): an event backlog overflow ends the turn with an error instead of hanging", async () => {
const ctrl = makeFakeSocket();
const { fn } = fakeTransport(ctrl);
const executor = new CodexAppServerExecutor({ websocketFn: fn });
ctrl.socket.send = ((originalSend) => (data: string) => {
originalSend(data);
const frame = JSON.parse(data) as Record<string, unknown>;
if (frame.id == null || !frame.method) return;
queueMicrotask(() => {
if (frame.method === "thread/start") {
ctrl.emit({ jsonrpc: "2.0", id: frame.id, result: { threadId: "thr_1" } });
} else if (frame.method === "turn/start") {
ctrl.emit({ jsonrpc: "2.0", id: frame.id, result: {} });
// More events than the queue holds, in one synchronous burst, alternating kinds so
// they cannot coalesce. No terminal notification follows.
for (let i = 0; i < 10_050; i += 1) {
ctrl.emit({
jsonrpc: "2.0",
method: i % 2 ? "item/reasoning/textDelta" : "item/agentMessage/delta",
params: { delta: "x" },
});
}
} else {
ctrl.emit({ jsonrpc: "2.0", id: frame.id, result: {} });
}
});
})(ctrl.socket.send);
const result = await executor.execute(makeExecuteInput({ stream: false }));
const response = "response" in result ? result.response : (result as Response);
const body = (await response.json()) as { status?: string; error?: { code?: string } };
assert.equal(body.status, "failed");
assert.equal(body.error?.code, "codex_app_server_turn_failed");
assert.equal(ctrl.closed, true, "the client socket is torn down once the turn fails");
});
// ── Tool path: INBOUND advertise + OUTBOUND passthrough ──────────────────────
test("dynamicToolWireName: flattens namespaced tools, passes plain ones through", () => {