xonecas.io
64875
Mc

mini-coder / commit b06efc6

fix(llm-api): strip GPT commentary parts from conversation history

GPT-5 reasoning models (gpt-5.3-codex, gpt-5.4, etc.) emit text parts
with providerOptions.openai.phase = 'commentary' before making tool
calls.  When these parts are stored in coreHistory and replayed on the
next turn, the model learns to reproduce tool-call syntax as plain text
instead of issuing real function calls.

Introduce stripGPTCommentaryFromHistory which removes those parts from
assistant messages before the messages are sent back to the API, and
extract getPartProviderOptions as a shared helper so getGeminiThought-
Signature and getOpenAITextPhase both use it without duplication.

xonecas2026-03-11 b06efc65ca0c13832b0495ad73485d65090a039c parent 5be0bb0

3 files changed

TODO.md+2−24
@@ -1,29 +1,5 @@
11 # TODO
22
3-## Gpt codex context window maxinng out
4-
5-I've noticed several sessions with codex spark that error because the context got too big for the model:
6-
7-```
8- ← read node_modules/@ai-sdk/google/src/google-generative-ai-language-model.ts:680+120
9- · node_modules/@ai-sdk/google/src/google-generative-ai-language-model.ts lines 680–799 of 1057 (truncated)
10-⠙ thinking{
11- type: "error",
12- sequence_number: 2,
13- error: {
14- type: "invalid_request_error",
15- code: "context_length_exceeded",
16- message: "Your input exceeds the context window of this model. Please adjust your input and try again.",
17- param: "input",
18- },
19-}
20-✖ [object Object]
21-✖ [object Object]
22-error: script "dev" exited with code 1
23-```
24-
25-The same task with GPT 5.4: `ctx 82.8k/1050.0k 8%` used on 82k in context. I know spark is a smaller context, but I'm suprised by how often we max out the 128k context.
26-
273 ---
284
295 ## LSP Diagnostics (not very important, with tool-hooks and strong linting, this is not as necessary, but we should implement asap)
@@ -80,6 +56,8 @@ error: script "dev" exited with code 130
8056
8157 I was not able to reproduce the same behaviour with opencode using the same provider. Which points to an issue with our code.
8258
59+The issue is more rare, but still happening agfter the fix we tried. Let's investigate further and try to find the real root cause.
60+
8361 ---
8462
8563 ## Deferred fixes
src/llm-api/turn.test.ts+136−0
@@ -7,6 +7,7 @@ import {
77 getReasoningDeltaFromStreamChunk,
88 isOpenAIGPT,
99 sanitizeGeminiToolMessages,
10+ stripGPTCommentaryFromHistory,
1011 } from "./turn.ts";
1112
1213 describe("isOpenAIGPT", () => {
@@ -226,6 +227,141 @@ describe("sanitizeGeminiToolMessages", () => {
226227 });
227228 });
228229
230+describe("stripGPTCommentaryFromHistory", () => {
231+ test("strips commentary text parts from GPT assistant messages", () => {
232+ const messages: CoreMessage[] = [
233+ { role: "user", content: "do the task" },
234+ {
235+ role: "assistant",
236+ content: [
237+ {
238+ type: "text",
239+ text: "I'll call shell: to=functions.shell json{...}",
240+ providerOptions: {
241+ openai: { itemId: "msg-1", phase: "commentary" },
242+ },
243+ },
244+ {
245+ type: "tool-call",
246+ toolCallId: "call-1",
247+ toolName: "shell",
248+ input: { command: "ls" },
249+ },
250+ ],
251+ } as unknown as CoreMessage,
252+ {
253+ role: "tool",
254+ content: [
255+ {
256+ type: "tool-result",
257+ toolCallId: "call-1",
258+ toolName: "shell",
259+ output: { type: "text", value: "file.txt" },
260+ },
261+ ],
262+ } as unknown as CoreMessage,
263+ {
264+ role: "assistant",
265+ content: [
266+ {
267+ type: "text",
268+ text: "Done!",
269+ providerOptions: {
270+ openai: { itemId: "msg-2", phase: "final_answer" },
271+ },
272+ },
273+ ],
274+ } as unknown as CoreMessage,
275+ ];
276+
277+ const result = stripGPTCommentaryFromHistory(
278+ messages,
279+ "openai/gpt-5.3-codex",
280+ );
281+
282+ // commentary part stripped from first assistant message
283+ const firstAssistant = result[1] as {
284+ role: "assistant";
285+ content: Array<Record<string, unknown>>;
286+ };
287+ expect(firstAssistant.content).toHaveLength(1);
288+ expect(firstAssistant.content[0]?.type).toBe("tool-call");
289+
290+ // final_answer text preserved in second assistant message
291+ const secondAssistant = result[3] as {
292+ role: "assistant";
293+ content: Array<Record<string, unknown>>;
294+ };
295+ expect(secondAssistant.content).toHaveLength(1);
296+ expect(secondAssistant.content[0]?.type).toBe("text");
297+ });
298+
299+ test("also strips when phase is in providerMetadata (legacy field)", () => {
300+ const messages: CoreMessage[] = [
301+ {
302+ role: "assistant",
303+ content: [
304+ {
305+ type: "text",
306+ text: "thinking...",
307+ providerMetadata: {
308+ openai: { itemId: "msg-x", phase: "commentary" },
309+ },
310+ },
311+ { type: "text", text: "result", providerOptions: {} },
312+ ],
313+ } as unknown as CoreMessage,
314+ ];
315+
316+ const result = stripGPTCommentaryFromHistory(messages, "zen/gpt-5.4");
317+ const assistant = result[0] as {
318+ role: "assistant";
319+ content: Array<Record<string, unknown>>;
320+ };
321+ expect(assistant.content).toHaveLength(1);
322+ expect(assistant.content[0]?.text).toBe("result");
323+ });
324+
325+ test("leaves non-GPT models untouched", () => {
326+ const messages: CoreMessage[] = [
327+ {
328+ role: "assistant",
329+ content: [
330+ {
331+ type: "text",
332+ text: "thinking...",
333+ providerOptions: { openai: { phase: "commentary" } },
334+ },
335+ ],
336+ } as unknown as CoreMessage,
337+ ];
338+
339+ expect(
340+ stripGPTCommentaryFromHistory(messages, "anthropic/claude-sonnet-4-5"),
341+ ).toBe(messages);
342+ });
343+
344+ test("returns same reference when no commentary parts are present", () => {
345+ const messages: CoreMessage[] = [
346+ { role: "user", content: "hi" },
347+ {
348+ role: "assistant",
349+ content: [
350+ {
351+ type: "text",
352+ text: "hello",
353+ providerOptions: { openai: { phase: "final_answer" } },
354+ },
355+ ],
356+ } as unknown as CoreMessage,
357+ ];
358+
359+ expect(stripGPTCommentaryFromHistory(messages, "openai/gpt-4o")).toBe(
360+ messages,
361+ );
362+ });
363+});
364+
229365 describe("applyContextPruning", () => {
230366 test("balanced mode prunes stale tool history", () => {
231367 const messages: CoreMessage[] = [
src/llm-api/turn.ts+76−8
@@ -305,13 +305,20 @@ function normalizeMessageProviderOptions(message: CoreMessage): CoreMessage {
305305 } as CoreMessage;
306306 }
307307
308-function getGeminiThoughtSignature(part: unknown): string | null {
308+/**
309+ * Reads the effective provider-options record for a content part, checking
310+ * both the modern `providerOptions` field and the legacy `providerMetadata`
311+ * alias (used by some older AI SDK versions and Gemini adapters).
312+ */
313+function getPartProviderOptions(part: unknown): Record<string, unknown> | null {
309314 if (!isRecord(part)) return null;
310- const providerOptions = isRecord(part.providerOptions)
311- ? part.providerOptions
312- : isRecord(part.providerMetadata)
313- ? part.providerMetadata
314- : null;
315+ if (isRecord(part.providerOptions)) return part.providerOptions;
316+ if (isRecord(part.providerMetadata)) return part.providerMetadata;
317+ return null;
318+}
319+
320+function getGeminiThoughtSignature(part: unknown): string | null {
321+ const providerOptions = getPartProviderOptions(part);
315322 if (!providerOptions) return null;
316323
317324 for (const provider of ["google", "vertex"] as const) {
@@ -376,6 +383,59 @@ export function sanitizeGeminiToolMessages(
376383 }
377384 }
378385
386+/**
387+ * Returns the phase of a text content part for OpenAI Responses API models.
388+ * GPT-5 reasoning models emit "commentary" text parts (thinking-out-loud text
389+ * like `to=functions.shell json{...}`) before their actual function calls.
390+ * These are distinct from the "final_answer" phase which is real output.
391+ */
392+function getOpenAITextPhase(part: unknown): string | null {
393+ const providerOptions = getPartProviderOptions(part);
394+ if (!providerOptions) return null;
395+ const openai = providerOptions.openai;
396+ if (!isRecord(openai)) return null;
397+ return typeof openai.phase === "string" ? openai.phase : null;
398+}
399+
400+function isCommentaryTextPart(part: unknown): boolean {
401+ if (!isRecord(part) || part.type !== "text") return false;
402+ return getOpenAITextPhase(part) === "commentary";
403+}
404+
405+/**
406+ * Strip `phase:"commentary"` text parts from assistant messages for GPT
407+ * (Responses API) models.
408+ *
409+ * GPT-5 reasoning models produce commentary parts before tool calls — text
410+ * like "I'll call the shell function: to=functions.shell json{...}". These
411+ * parts are useful during the current turn (the user can see the model
412+ * thinking) but must not leak into persistent conversation history. When fed
413+ * back on subsequent turns the model pattern-matches on them and starts
414+ * reproducing that format as plain text instead of making real function calls,
415+ * resulting in garbled output with repeated `to=functions.<name> json{...}`
416+ * strings.
417+ */
418+export function stripGPTCommentaryFromHistory(
419+ messages: CoreMessage[],
420+ modelString: string,
421+): CoreMessage[] {
422+ if (!isOpenAIGPT(modelString)) return messages;
423+
424+ let mutated = false;
425+ const result = messages.map((message) => {
426+ if (message.role !== "assistant" || !Array.isArray(message.content)) {
427+ return message;
428+ }
429+ const filtered = message.content.filter(
430+ (part) => !isCommentaryTextPart(part),
431+ );
432+ if (filtered.length === message.content.length) return message;
433+ mutated = true;
434+ return { ...message, content: filtered } as CoreMessage;
435+ });
436+ return mutated ? result : messages;
437+}
438+
379439 // ─── Main turn function ───────────────────────────────────────────────────────
380440
381441 /**
@@ -471,12 +531,20 @@ export async function* runTurn(options: {
471531 });
472532 }
473533
534+ const gptSanitizedMessages = stripGPTCommentaryFromHistory(
535+ geminiSanitizedMessages,
536+ modelString,
537+ );
538+ if (gptSanitizedMessages !== geminiSanitizedMessages) {
539+ logApiEvent("gpt commentary stripped from history", { modelString });
540+ }
541+
474542 logApiEvent(
475543 "turn context pre-prune",
476- getMessageDiagnostics(geminiSanitizedMessages),
544+ getMessageDiagnostics(gptSanitizedMessages),
477545 );
478546 const prunedMessages = applyContextPruning(
479- geminiSanitizedMessages,
547+ gptSanitizedMessages,
480548 pruningMode,
481549 );
482550 logApiEvent(