fix: return input reference from downgradeOpenAIReasoningBlocks when unchanged

Address Greptile review feedback:
1. Add anyChanged guard so the function returns the original input
   array when no messages were modified, matching the pattern used
   by downgradeOpenAIFunctionCallReasoningPairs. This preserves the
   early-exit fast path in the stream wrapper.
2. Align call order with google.ts: reasoning blocks first, then
   function-call pairs.
This commit is contained in:
Subash Natarajan
2026-03-27 19:48:03 +05:30
committed by Josh Lehman
parent 02c4851164
commit b08f535e75
2 changed files with 7 additions and 7 deletions

View File

@@ -207,6 +207,7 @@ export function downgradeOpenAIFunctionCallReasoningPairs(
* is incomplete, drop the block to keep history usable.
*/
export function downgradeOpenAIReasoningBlocks(messages: AgentMessage[]): AgentMessage[] {
let anyChanged = false;
const out: AgentMessage[] = [];
for (const msg of messages) {
@@ -259,6 +260,7 @@ export function downgradeOpenAIReasoningBlocks(messages: AgentMessage[]): AgentM
continue;
}
anyChanged = true;
if (nextContent.length === 0) {
continue;
}
@@ -266,5 +268,5 @@ export function downgradeOpenAIReasoningBlocks(messages: AgentMessage[]): AgentM
out.push({ ...assistantMsg, content: nextContent } as AgentMessage);
}
return out;
return anyChanged ? out : messages;
}

View File

@@ -1368,12 +1368,10 @@ export async function runEmbeddedAttempt(
if (!Array.isArray(messages)) {
return inner(model, context, options);
}
const pairSanitized = downgradeOpenAIFunctionCallReasoningPairs(
messages as AgentMessage[],
);
// Also strip orphaned reasoning blocks that lack a required following
// content item — OpenAI rejects these with a 400 error.
const sanitized = downgradeOpenAIReasoningBlocks(pairSanitized);
// Strip orphaned reasoning blocks first, then fix function-call
// pairing — matches the call order in google.ts.
const reasoningSanitized = downgradeOpenAIReasoningBlocks(messages as AgentMessage[]);
const sanitized = downgradeOpenAIFunctionCallReasoningPairs(reasoningSanitized);
if (sanitized === messages) {
return inner(model, context, options);
}