xonecas.io
64875
Mc

mini-coder / commit f217a60

fix(llm-api): harden GPT history sanitization for OpenAI turns

Strip OpenAI item IDs from replayed GPT history and keep prompts in system messages to avoid tool-protocol leakage patterns across turns.

xonecas2026-03-11 f217a60a2dbc95aba5e2db88c6ef4989467dd69b parent 110a9f7

2 files changed

src/llm-api/turn.test.ts+71−0
@@ -8,6 +8,7 @@ import {
88 isOpenAIGPT,
99 sanitizeGeminiToolMessages,
1010 stripGPTCommentaryFromHistory,
11+ stripOpenAIItemIdsFromHistory,
1112 } from "./turn.ts";
1213
1314 describe("isOpenAIGPT", () => {
@@ -416,6 +417,76 @@ describe("stripGPTCommentaryFromHistory", () => {
416417 });
417418 });
418419
420+describe("stripOpenAIItemIdsFromHistory", () => {
421+ test("removes openai itemId metadata from assistant content parts", () => {
422+ const messages: CoreMessage[] = [
423+ { role: "user", content: "go" },
424+ {
425+ role: "assistant",
426+ content: [
427+ {
428+ type: "text",
429+ text: "thinking",
430+ providerOptions: {
431+ openai: { itemId: "msg_123", phase: "commentary" },
432+ },
433+ },
434+ {
435+ type: "tool-call",
436+ toolCallId: "call_1",
437+ toolName: "read",
438+ input: { path: "README.md" },
439+ providerMetadata: {
440+ openai: { itemId: "fc_456" },
441+ },
442+ },
443+ ],
444+ } as unknown as CoreMessage,
445+ ];
446+
447+ const result = stripOpenAIItemIdsFromHistory(messages, "zen/gpt-5.3-codex");
448+ const assistant = result[1] as {
449+ role: "assistant";
450+ content: Array<Record<string, unknown>>;
451+ };
452+ expect(
453+ (
454+ assistant.content[0]?.providerOptions as {
455+ openai?: { itemId?: string; phase?: string };
456+ }
457+ )?.openai?.itemId,
458+ ).toBeUndefined();
459+ expect(
460+ (
461+ assistant.content[1]?.providerMetadata as {
462+ openai?: { itemId?: string };
463+ }
464+ )?.openai?.itemId,
465+ ).toBeUndefined();
466+ });
467+
468+ test("leaves non-gpt providers untouched", () => {
469+ const messages: CoreMessage[] = [
470+ {
471+ role: "assistant",
472+ content: [
473+ {
474+ type: "text",
475+ text: "hello",
476+ providerOptions: {
477+ openai: { itemId: "msg_123", phase: "final_answer" },
478+ },
479+ },
480+ ],
481+ } as unknown as CoreMessage,
482+ ];
483+
484+ expect(
485+ stripOpenAIItemIdsFromHistory(messages, "anthropic/claude-sonnet-4-5"),
486+ ).toBe(messages);
487+ });
488+});
489+
419490 describe("applyContextPruning", () => {
420491 test("balanced mode prunes stale tool history", () => {
421492 const messages: CoreMessage[] = [
src/llm-api/turn.ts+85−16
@@ -305,6 +305,61 @@ function normalizeMessageProviderOptions(message: CoreMessage): CoreMessage {
305305 } as CoreMessage;
306306 }
307307
308+function stripOpenAIItemIdFromPart(part: unknown): {
309+ part: unknown;
310+ changed: boolean;
311+} {
312+ if (!isRecord(part)) return { part, changed: false };
313+
314+ let changed = false;
315+ const nextPart = { ...part };
316+
317+ const dropItemId = (field: "providerOptions" | "providerMetadata"): void => {
318+ const source = nextPart[field];
319+ if (!isRecord(source)) return;
320+ const openai = source.openai;
321+ if (!isRecord(openai) || !("itemId" in openai)) return;
322+
323+ const nextOpenAI = { ...openai };
324+ delete nextOpenAI.itemId;
325+ nextPart[field] = { ...source, openai: nextOpenAI };
326+ changed = true;
327+ };
328+
329+ dropItemId("providerOptions");
330+ dropItemId("providerMetadata");
331+
332+ return { part: changed ? nextPart : part, changed };
333+}
334+
335+export function stripOpenAIItemIdsFromHistory(
336+ messages: CoreMessage[],
337+ modelString: string,
338+): CoreMessage[] {
339+ if (!isOpenAIGPT(modelString)) return messages;
340+
341+ let mutated = false;
342+ const result = messages.map((message) => {
343+ if (!Array.isArray(message.content)) return message;
344+
345+ let contentMutated = false;
346+ const content = message.content.map((part) => {
347+ const cleaned = stripOpenAIItemIdFromPart(part);
348+ if (cleaned.changed) contentMutated = true;
349+ return cleaned.part;
350+ });
351+
352+ if (!contentMutated) return message;
353+ mutated = true;
354+ return {
355+ ...message,
356+ content: content as CoreMessage["content"],
357+ } as CoreMessage;
358+ });
359+
360+ return mutated ? result : messages;
361+}
362+
308363 /**
309364 * Reads the effective provider-options record for a content part, checking
310365 * both the modern `providerOptions` field and the legacy `providerMetadata`
@@ -469,9 +524,8 @@ export function stripGPTCommentaryFromHistory(
469524 // ─── Main turn function ───────────────────────────────────────────────────────
470525
471526 /**
472- * Returns true when the model string refers to an OpenAI GPT model, which uses
473- * the Responses API and honours the `instructions` provider option as the
474- * authoritative system prompt (rather than a system-role message in `input`).
527+ * Returns true when the model string refers to an OpenAI GPT model routed
528+ * through the OpenAI SDK stack (direct OpenAI or Zen OpenAI).
475529 */
476530 export function isOpenAIGPT(modelString: string): boolean {
477531 const { provider, modelId } = parseModelString(modelString);
@@ -526,11 +580,9 @@ export async function* runTurn(options: {
526580
527581 try {
528582 // OpenAI GPT models use the Responses API (@ai-sdk/openai v3 / ai v6 default),
529- // which honours `instructions` as the authoritative system prompt. Passing it
530- // as a system-role message in `input` works but is treated as a lower-priority
531- // user turn, causing the model to deprioritise the instructions.
532- const useInstructions =
533- systemPrompt !== undefined && isOpenAIGPT(modelString);
583+ // but we keep prompts only in `system` messages for consistency across
584+ // providers and to avoid prompting shape drift on GPT reasoning models.
585+ const useInstructions = false;
534586
535587 const toolCount = Object.keys(toolSet).length;
536588 const thinkingOpts = thinkingEffort
@@ -569,12 +621,20 @@ export async function* runTurn(options: {
569621 logApiEvent("gpt commentary stripped from history", { modelString });
570622 }
571623
624+ const openAIItemIdsStrippedMessages = stripOpenAIItemIdsFromHistory(
625+ gptSanitizedMessages,
626+ modelString,
627+ );
628+ if (openAIItemIdsStrippedMessages !== gptSanitizedMessages) {
629+ logApiEvent("openai item ids stripped from history", { modelString });
630+ }
631+
572632 logApiEvent(
573633 "turn context pre-prune",
574- getMessageDiagnostics(gptSanitizedMessages),
634+ getMessageDiagnostics(openAIItemIdsStrippedMessages),
575635 );
576636 const prunedMessages = applyContextPruning(
577- gptSanitizedMessages,
637+ openAIItemIdsStrippedMessages,
578638 pruningMode,
579639 );
580640 logApiEvent(
@@ -594,16 +654,25 @@ export async function* runTurn(options: {
594654 }
595655
596656 const mergedProviderOptions = {
597- ...(useInstructions
598- ? { openai: { instructions: systemPrompt, store: false } }
599- : {}),
600657 ...(thinkingOpts ?? {}),
601- ...(useInstructions && thinkingOpts?.openai
658+ ...(isOpenAIGPT(modelString)
602659 ? {
603660 openai: {
604- instructions: systemPrompt,
605661 store: false,
606- ...(thinkingOpts.openai as object),
662+ ...(isRecord(thinkingOpts?.openai)
663+ ? (thinkingOpts.openai as object)
664+ : {}),
665+ },
666+ }
667+ : {}),
668+ ...(useInstructions && systemPrompt
669+ ? {
670+ openai: {
671+ ...(isRecord(thinkingOpts?.openai)
672+ ? (thinkingOpts.openai as object)
673+ : {}),
674+ store: false,
675+ instructions: systemPrompt,
607676 },
608677 }
609678 : {}),