@@ -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", () => {