Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Fixed

- Add Codex request-timeout salvage refusal diagnostics and replay coverage for complete `todo_write` calls.
17 changes: 17 additions & 0 deletions packages/ai/src/providers/openai-codex-responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,23 @@ function trySalvageCodexFinalizedToolCalls(
) &&
hasCompleteArguments;
if (!canSalvageFinalizedCall && !canSalvageCompleteArguments) {
logCodexDebug("codex stream close salvage refused", {
toolArgumentCorrelationFailed: runtime.toolArgumentCorrelationFailed,
argumentsComplete: toolCalls.map(toolCall => {
const block = toolCall as ToolCall & { argumentsComplete?: boolean };
return block.argumentsComplete === true;
}),
sourceItemIdPresent: toolCalls.map(toolCall => {
const block = toolCall as ToolCall & { sourceItemId?: string };
return typeof block.sourceItemId === "string";
}),
sourceCallIdPresent: toolCalls.map(toolCall => {
const block = toolCall as ToolCall & { sourceCallId?: string };
return typeof block.sourceCallId === "string";
}),
currentItemType: runtime.currentItem?.type ?? null,
currentBlockType: runtime.currentBlock?.type ?? null,
});
return false;
}

Expand Down
127 changes: 127 additions & 0 deletions packages/ai/test/openai-codex-stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,99 @@ function createProductionCodexToolCallEvents(nestedError: boolean): Record<strin
];
}

type CompleteToolTimeoutReplayVariant = {
argumentsDone: boolean;
outputItemDone: boolean;
newItem: "none" | "message" | "reasoning";
correlateDelta: boolean;
};

function createCompleteToolTimeoutReplayEvents(variant: CompleteToolTimeoutReplayVariant): Record<string, unknown>[] {
const argumentsValue = JSON.stringify({ ops: [] });
const functionCall = {
type: "function_call",
id: "fc_variant",
call_id: "call_variant",
name: "todo_write",
arguments: "",
};
const events: Record<string, unknown>[] = [
{
type: "response.output_item.added",
output_index: 0,
item: {
type: "reasoning",
id: "rs_before_call",
content: [],
summary: [],
},
},
{
type: "response.output_item.done",
output_index: 0,
item: {
type: "reasoning",
id: "rs_before_call",
content: [],
summary: [],
},
},
{
type: "response.output_item.added",
output_index: 1,
item: functionCall,
},
{
type: "response.function_call_arguments.delta",
...(variant.correlateDelta ? { item_id: "fc_variant", output_index: 1 } : {}),
delta: argumentsValue,
},
];
if (variant.argumentsDone) {
events.push({
type: "response.function_call_arguments.done",
...(variant.correlateDelta ? { item_id: "fc_variant", output_index: 1 } : {}),
arguments: argumentsValue,
});
}
if (variant.outputItemDone) {
events.push({
type: "response.output_item.done",
output_index: 1,
item: {
...functionCall,
arguments: argumentsValue,
},
});
}
if (variant.newItem !== "none") {
events.push({
type: "response.output_item.added",
output_index: 2,
item:
variant.newItem === "message"
? {
type: "message",
id: "msg_after_call",
role: "assistant",
content: [],
}
: {
type: "reasoning",
id: "rs_after_call",
content: [],
summary: [],
},
});
}
events.push({
type: "error",
code: "request_timeout",
message: "stream disconnected before completion: stream closed before response.completed (code=request_timeout)",
});
return events;
}

function getRequestSignal(input: string | URL | Request, init: RequestInit | undefined): AbortSignal | undefined {
if (init?.signal) return init.signal;
if (input instanceof Request) return input.signal;
Expand Down Expand Up @@ -491,6 +584,40 @@ describe("openai-codex streaming", () => {
});
});

it.each(
(["none", "message", "reasoning"] as const).flatMap(newItem =>
[false, true].flatMap(argumentsDone =>
[false, true].flatMap(outputItemDone =>
[false, true].map(correlateDelta => ({
argumentsDone,
outputItemDone,
newItem,
correlateDelta,
})),
),
),
),
)("salvages complete todo_write arguments across replay variants (%j)", async (variant: CompleteToolTimeoutReplayVariant) => {
const sse = createCodexErrorSse(createCompleteToolTimeoutReplayEvents(variant));
global.fetch = vi.fn(
async () => new Response(sse, { status: 200, headers: { "content-type": "text/event-stream" } }),
) as unknown as typeof fetch;

const result = await streamOpenAICodexResponses(
{ ...createCodexTestModel("https://chatgpt.com/backend-api"), preferWebsockets: false },
createCodexTestContext(),
{ apiKey: createCodexTestToken() },
).result();

expect(result.stopReason).toBe("toolUse");
expect(result.content).toContainEqual({
type: "toolCall",
id: "call_variant|fc_variant",
name: "todo_write",
arguments: { ops: [] },
});
});

it.each([
false,
true,
Expand Down
Loading