diff --git a/open-sse/executors/codex.ts b/open-sse/executors/codex.ts index fb9336c784..f595b7f1b8 100644 --- a/open-sse/executors/codex.ts +++ b/open-sse/executors/codex.ts @@ -309,6 +309,7 @@ export function stripStoredItemReferences(body: Record): void { function stripOrphanedCodexFunctionCallOutputs(body: Record): void { if (!Array.isArray(body.input)) return; + const input = body.input; // A previous_response_id delegates history resolution to the upstream // Responses service, so a matching function_call may legitimately live in // that remote response rather than in the local input array. @@ -317,7 +318,7 @@ function stripOrphanedCodexFunctionCallOutputs(body: Record): v const callIds = new Set(); let outputCount = 0; - for (const item of body.input) { + for (const item of input) { if (!item || typeof item !== "object" || Array.isArray(item)) continue; const record = item as Record; @@ -341,9 +342,7 @@ function stripOrphanedCodexFunctionCallOutputs(body: Record): v } if (outputCount === 0) return; - - const before = body.input.length; - body.input = body.input.filter((item) => { + const filteredInput = input.filter((item) => { if (!item || typeof item !== "object" || Array.isArray(item)) return true; const record = item as Record; if (record.type === "function_call_output" && typeof record.call_id === "string") { @@ -352,7 +351,8 @@ function stripOrphanedCodexFunctionCallOutputs(body: Record): v return true; }); - const removedCount = before - body.input.length; + const removedCount = input.length - filteredInput.length; + body.input = filteredInput; if (removedCount > 0) { console.debug( `[Codex] stripOrphanedCodexFunctionCallOutputs: removed ${removedCount} orphaned function_call_output item(s)` diff --git a/tests/unit/codex-orphaned-tool-outputs-2928.test.ts b/tests/unit/codex-orphaned-tool-outputs-2928.test.ts index 2246af4158..1a96a1263b 100644 --- a/tests/unit/codex-orphaned-tool-outputs-2928.test.ts +++ b/tests/unit/codex-orphaned-tool-outputs-2928.test.ts @@ -76,6 +76,7 @@ test("Codex keeps matched outputs and removes orphaned outputs from mixed input" { type: "function_call_output", call_id: "call_orphan", output: "orphaned" }, ]); + assert.equal(result.length, 2); assert.deepEqual(toolOutputs(result), [ { type: "function_call_output", call_id: "call_keep", output: "ok" }, ]);