xonecas.io
64875
Mc

mini-coder / commit 36d6d43

fix: keep GPT commentary out of session history

xonecas2026-03-14 36d6d4307a418450ade344e425c311de840cf188 parent 8a9b99d

3 files changed

src/agent/agent-helpers.ts+18−1
@@ -1,5 +1,20 @@
11 import type { CoreMessage } from "../llm-api/turn.ts";
22
3+function isRecord(value: unknown): value is Record<string, unknown> {
4+ return value !== null && typeof value === "object";
5+}
6+
7+function isCommentaryTextPart(part: unknown): boolean {
8+ if (!isRecord(part) || part.type !== "text") return false;
9+ const providerData = isRecord(part.providerOptions)
10+ ? part.providerOptions
11+ : isRecord(part.providerMetadata)
12+ ? part.providerMetadata
13+ : null;
14+ if (!providerData || !isRecord(providerData.openai)) return false;
15+ return providerData.openai.phase === "commentary";
16+}
17+
318 export function makeInterruptMessage(reason: "user" | "error"): CoreMessage {
419 const text =
520 reason === "user"
@@ -36,7 +51,9 @@ export function extractAssistantText(newMessages: CoreMessage[]): string {
3651 parts.push(content);
3752 } else if (Array.isArray(content)) {
3853 for (const part of content as Array<{ type?: string; text?: string }>) {
39- if (part?.type === "text" && part.text) parts.push(part.text);
54+ if (part?.type === "text" && part.text && !isCommentaryTextPart(part)) {
55+ parts.push(part.text);
56+ }
4057 }
4158 }
4259 }
src/agent/agent.test.ts+22−0
@@ -66,6 +66,28 @@ describe("extractAssistantText", () => {
6666 ];
6767 expect(extractAssistantText(msgs)).toBe("Tests pass.");
6868 });
69+
70+ test("ignores GPT commentary text parts", () => {
71+ const msgs: CoreMessage[] = [
72+ {
73+ role: "assistant",
74+ content: [
75+ {
76+ type: "text",
77+ text: "to=functions.shell json{...}",
78+ providerOptions: { openai: { phase: "commentary" } },
79+ },
80+ {
81+ type: "text",
82+ text: "Committed the fix.",
83+ providerOptions: { openai: { phase: "final_answer" } },
84+ },
85+ ],
86+ },
87+ ] as unknown as CoreMessage[];
88+
89+ expect(extractAssistantText(msgs)).toBe("Committed the fix.");
90+ });
6991 });
7092
7193 // ─── makeInterruptMessage ─────────────────────────────────────────────────────
src/agent/session-runner.ts+14−9
@@ -1,6 +1,7 @@
11 import * as c from "yoctocolors";
22 import type { ImageAttachment } from "../cli/image-types.ts";
33 import { watchForCancel } from "../cli/input.ts";
4+import { stripGPTCommentaryFromHistory } from "../llm-api/history-transforms.ts";
45 import type { ThinkingEffort } from "../llm-api/providers.ts";
56 import { resolveModel } from "../llm-api/providers.ts";
67 import type { ContextPruningMode, CoreMessage } from "../llm-api/turn.ts";
@@ -290,18 +291,22 @@ export class SessionRunner {
290291 await this.reporter.renderTurn(events, {
291292 showReasoning: this.showReasoning,
292293 });
294+ const sanitizedNewMessages = stripGPTCommentaryFromHistory(
295+ newMessages,
296+ this.currentModel,
297+ );
293298
294- if (newMessages.length > 0) {
295- this.coreHistory.push(...newMessages);
296- this.session.messages.push(...newMessages);
297- // Persistence invariant: save model-authored messages with lossless JSON
298- // serialization and preserve exact shape/ordering (including
299- // providerOptions/providerMetadata thought-signature fields). Never
300- // reconstruct tool-call history during save/load.
301- saveMessages(this.session.id, newMessages, thisTurn);
299+ if (sanitizedNewMessages.length > 0) {
300+ this.coreHistory.push(...sanitizedNewMessages);
301+ this.session.messages.push(...sanitizedNewMessages);
302+ // Persistence invariant: save model-authored messages with stable,
303+ // replay-safe JSON. Provider commentary/tool-call scratchpad text is
304+ // intentionally stripped because persisting it pollutes future turns
305+ // and can reintroduce malformed raw tool-call text into history.
306+ saveMessages(this.session.id, sanitizedNewMessages, thisTurn);
302307 }
303308
304- lastAssistantText = extractAssistantText(newMessages);
309+ lastAssistantText = extractAssistantText(sanitizedNewMessages);
305310 this.totalIn += inputTokens;
306311 this.totalOut += outputTokens;
307312 this.lastContextTokens = contextTokens;