xonecas.io
64875
Mc

mini-coder / commit ae2aebb

fix(agent): sanitize model-authored history before persisting

xonecas2026-03-15 ae2aebb86c05e4e4ae1a42a9f597659901717724 parent be9a977

3 files changed

src/agent/agent-helpers.ts+12−0
@@ -1,3 +1,7 @@
1+import {
2+ stripOpenAIHistoryTransforms,
3+ stripToolRuntimeInputFields,
4+} from "../llm-api/history-transforms.ts";
15 import type { CoreMessage } from "../llm-api/turn.ts";
26
37 export function makeInterruptMessage(reason: "user" | "error"): CoreMessage {
@@ -27,6 +31,14 @@ export function buildAbortMessages(
2731 return [...partialMessages, partialMsg];
2832 }
2933
34+export function sanitizeModelAuthoredMessages(
35+ newMessages: CoreMessage[],
36+ modelString: string,
37+): CoreMessage[] {
38+ const strippedRuntimeFields = stripToolRuntimeInputFields(newMessages);
39+ return stripOpenAIHistoryTransforms(strippedRuntimeFields, modelString);
40+}
41+
3042 export function extractAssistantText(newMessages: CoreMessage[]): string {
3143 const parts: string[] = [];
3244 for (const msg of newMessages) {
src/agent/agent.test.ts+81−2
@@ -1,6 +1,10 @@
11 import { describe, expect, test } from "bun:test";
22 import type { CoreMessage } from "../llm-api/turn.ts";
3-import { extractAssistantText, makeInterruptMessage } from "./agent-helpers.ts";
3+import {
4+ extractAssistantText,
5+ makeInterruptMessage,
6+ sanitizeModelAuthoredMessages,
7+} from "./agent-helpers.ts";
48
59 // ─── extractAssistantText ─────────────────────────────────────────────────────
610
@@ -54,7 +58,6 @@ describe("extractAssistantText", () => {
5458 });
5559
5660 test("extracts text from array content when last part is a tool call", () => {
57- // Typical agentic pattern: text block followed by a trailing tool call
5861 const msgs: CoreMessage[] = [
5962 {
6063 role: "assistant",
@@ -68,6 +71,82 @@ describe("extractAssistantText", () => {
6871 });
6972 });
7073
74+describe("sanitizeModelAuthoredMessages", () => {
75+ test("strips GPT commentary parts before messages are reused or persisted", () => {
76+ const msgs: CoreMessage[] = [
77+ {
78+ role: "assistant",
79+ content: [
80+ { type: "reasoning", text: "plan" },
81+ {
82+ type: "text",
83+ text: "hidden commentary",
84+ providerOptions: { openai: { phase: "commentary" } },
85+ },
86+ {
87+ type: "text",
88+ text: "final answer",
89+ providerOptions: { openai: { phase: "final_answer" } },
90+ },
91+ ],
92+ } as unknown as CoreMessage,
93+ ];
94+
95+ expect(sanitizeModelAuthoredMessages(msgs, "openai/gpt-5.3-codex")).toEqual(
96+ [
97+ {
98+ role: "assistant",
99+ content: [
100+ { type: "reasoning", text: "plan" },
101+ {
102+ type: "text",
103+ text: "final answer",
104+ providerOptions: { openai: { phase: "final_answer" } },
105+ },
106+ ],
107+ },
108+ ],
109+ );
110+ });
111+
112+ test("strips runtime-only tool fields from persisted tool calls", () => {
113+ const msgs: CoreMessage[] = [
114+ {
115+ role: "assistant",
116+ content: [
117+ {
118+ type: "tool-call",
119+ toolCallId: "1",
120+ toolName: "read",
121+ input: {
122+ path: "TODO.md",
123+ cwd: "/tmp/project",
124+ snapshotCallback: "ignore",
125+ onOutput: "ignore",
126+ },
127+ },
128+ ],
129+ } as unknown as CoreMessage,
130+ ];
131+
132+ expect(
133+ sanitizeModelAuthoredMessages(msgs, "anthropic/claude-sonnet-4-5"),
134+ ).toEqual([
135+ {
136+ role: "assistant",
137+ content: [
138+ {
139+ type: "tool-call",
140+ toolCallId: "1",
141+ toolName: "read",
142+ input: { path: "TODO.md" },
143+ },
144+ ],
145+ },
146+ ]);
147+ });
148+});
149+
71150 // ─── makeInterruptMessage ─────────────────────────────────────────────────────
72151
73152 describe("makeInterruptMessage", () => {
src/agent/session-runner.ts+14−10
@@ -18,7 +18,11 @@ import {
1818 touchActiveSession,
1919 } from "../session/manager.ts";
2020 import { snapshotBeforeEdit } from "../tools/snapshot.ts";
21-import { extractAssistantText, makeInterruptMessage } from "./agent-helpers.ts";
21+import {
22+ extractAssistantText,
23+ makeInterruptMessage,
24+ sanitizeModelAuthoredMessages,
25+} from "./agent-helpers.ts";
2226 import type { AgentReporter } from "./reporter.ts";
2327 import { buildSystemPrompt } from "./system-prompt.ts";
2428 import { undoLastTurn } from "./undo-snapshot.ts";
@@ -290,18 +294,18 @@ export class SessionRunner {
290294 await this.reporter.renderTurn(events, {
291295 showReasoning: this.showReasoning,
292296 });
297+ const historyMessages = sanitizeModelAuthoredMessages(
298+ newMessages,
299+ this.currentModel,
300+ );
293301
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);
302+ if (historyMessages.length > 0) {
303+ this.coreHistory.push(...historyMessages);
304+ this.session.messages.push(...historyMessages);
305+ saveMessages(this.session.id, historyMessages, thisTurn);
302306 }
303307
304- lastAssistantText = extractAssistantText(newMessages);
308+ lastAssistantText = extractAssistantText(historyMessages);
305309 this.totalIn += inputTokens;
306310 this.totalOut += outputTokens;
307311 this.lastContextTokens = contextTokens;