@@ -71,39 +71,42 @@ describe("getReasoningDeltaFromStreamChunk", () => {
7171 });
7272
7373 describe("sanitizeGeminiToolMessages", () => {
74- test("truncates the current Gemini tool turn when any tool call lacks a thought signature", () => {
75- const messages = [
76- { role: "user", content: "older turn" },
74+ const healthyGeminiToolTurn = (userContent: string): CoreMessage[] =>
75+ [
76+ { role: "user", content: userContent },
7777 {
7878 role: "assistant",
7979 content: [
8080 {
8181 type: "tool-call",
82- toolCallId: "old-call",
82+ toolCallId: "healthy-call",
8383 toolName: "read",
8484 input: {},
85- providerOptions: { google: { thoughtSignature: "sig-old" } },
85+ providerOptions: { google: { thoughtSignature: "sig-ok" } },
8686 },
8787 ],
8888 },
8989 {
9090 role: "tool",
91- content: [] as unknown as CoreMessage["content"],
92- },
93- { role: "user", content: "current turn" },
94- {
95- role: "assistant",
9691 content: [
9792 {
98- type: "tool-call",
99- toolCallId: "ok-call",
93+ type: "tool-result",
94+ toolCallId: "healthy-call",
10095 toolName: "read",
101- input: {},
102- providerOptions: { google: { thoughtSignature: "sig-ok" } },
96+ output: { type: "json", value: { ok: true } },
10397 },
98+ ],
99+ },
100+ ] as unknown as CoreMessage[];
101+
102+ const brokenGeminiToolTurn = (toolCallId = "broken-call"): CoreMessage[] =>
103+ [
104+ {
105+ role: "assistant",
106+ content: [
104107 {
105108 type: "tool-call",
106- toolCallId: "broken-call",
109+ toolCallId,
107110 toolName: "replace",
108111 input: {},
109112 },
@@ -114,89 +117,155 @@ describe("sanitizeGeminiToolMessages", () => {
114117 content: [
115118 {
116119 type: "tool-result",
117- toolCallId: "ok-call",
118- toolName: "read",
119- output: { type: "json", value: { ok: true } },
120- },
121- {
122- type: "tool-result",
123- toolCallId: "broken-call",
120+ toolCallId,
124121 toolName: "replace",
125- output: { type: "json", value: { ok: true } },
122+ output: { type: "json", value: { ok: false } },
126123 },
127124 ],
128125 },
129126 ] as unknown as CoreMessage[];
130127
131- const sanitized = sanitizeGeminiToolMessages(
132- messages,
133- "zen/gemini-3.1-pro",
134- true,
135- );
128+ test("valid signed single call preserved", () => {
129+ const messages: CoreMessage[] = [
130+ { role: "user", content: "current turn" },
131+ {
132+ role: "assistant",
133+ content: [
134+ {
135+ type: "tool-call",
136+ toolCallId: "call-1",
137+ toolName: "read",
138+ input: {},
139+ providerOptions: { google: { thoughtSignature: "sig-1" } },
140+ },
141+ ],
142+ } as unknown as CoreMessage,
143+ ];
136144
137- expect(sanitized).toEqual(messages.slice(0, 4));
145+ expect(
146+ sanitizeGeminiToolMessages(messages, "google/gemini-2.5-pro", true),
147+ ).toEqual(messages);
138148 });
139149
140- test("drops earlier broken Gemini tool turns and keeps the latest user turn", () => {
141- const messages = [
142- { role: "user", content: "healthy turn" },
143- { role: "assistant", content: "healthy response" },
144- { role: "user", content: "broken turn" },
150+ test("valid parallel calls with only first signed preserved", () => {
151+ const messages: CoreMessage[] = [
152+ { role: "user", content: "parallel call turn" },
145153 {
146154 role: "assistant",
147155 content: [
148156 {
149157 type: "tool-call",
150- toolCallId: "signed-call",
158+ toolCallId: "signed-anchor",
151159 toolName: "read",
152160 input: {},
153- providerOptions: { google: { thoughtSignature: "sig-1" } },
161+ providerOptions: { google: { thoughtSignature: "sig-anchor" } },
154162 },
155163 {
156164 type: "tool-call",
157- toolCallId: "unsigned-call",
165+ toolCallId: "unsigned-followup",
158166 toolName: "replace",
159167 input: {},
160168 },
161169 ],
162- },
163- {
164- role: "tool",
165- content: [] as unknown as CoreMessage["content"],
166- },
167- { role: "assistant", content: "done" },
168- { role: "user", content: "latest turn" },
170+ } as unknown as CoreMessage,
171+ ];
172+
173+ expect(
174+ sanitizeGeminiToolMessages(messages, "zen/gemini-3.1-pro", true),
175+ ).toEqual(messages);
176+ });
177+
178+ test("legacy providerMetadata recognized and normalized into providerOptions", () => {
179+ const messages = [
180+ { role: "user", content: "current turn" },
169181 {
170182 role: "assistant",
171183 content: [
172184 {
173185 type: "tool-call",
174- toolCallId: "latest-call",
186+ toolCallId: "call-1",
175187 toolName: "read",
176188 input: {},
177- providerOptions: { google: { thoughtSignature: "sig-2" } },
189+ providerMetadata: { google: { thoughtSignature: "sig-legacy" } },
178190 },
179191 ],
180192 },
181193 ] as unknown as CoreMessage[];
182194
195+ const sanitized = sanitizeGeminiToolMessages(
196+ messages,
197+ "google/gemini-2.5-pro",
198+ true,
199+ );
200+ const assistant = sanitized[1] as {
201+ role: "assistant";
202+ content: Array<Record<string, unknown>>;
203+ };
204+ expect(assistant.content[0]?.providerOptions).toEqual({
205+ google: { thoughtSignature: "sig-legacy" },
206+ });
207+ });
208+
209+ test("malformed assistant/tool before current user preserves that user and later messages", () => {
210+ const messages = [
211+ ...healthyGeminiToolTurn("healthy turn"),
212+ ...brokenGeminiToolTurn(),
213+ { role: "user", content: "current prompt" },
214+ { role: "assistant", content: "plain response" },
215+ ] as unknown as CoreMessage[];
216+
183217 expect(
184218 sanitizeGeminiToolMessages(messages, "google/gemini-2.5-pro", true),
185- ).toEqual([...messages.slice(0, 3), ...messages.slice(6)]);
219+ ).toEqual([...messages.slice(0, 3), ...messages.slice(5)]);
186220 });
187221
188- test("copies legacy providerMetadata into providerOptions for Gemini tool calls", () => {
222+ test("malformed latest turn without trailing user still truncates tail from broken assistant onward", () => {
189223 const messages = [
190- { role: "user", content: "current turn" },
224+ ...healthyGeminiToolTurn("healthy turn"),
225+ { role: "user", content: "latest turn" },
226+ ...brokenGeminiToolTurn(),
227+ ] as unknown as CoreMessage[];
228+
229+ expect(
230+ sanitizeGeminiToolMessages(messages, "google/gemini-2.5-pro", true),
231+ ).toEqual(messages.slice(0, 4));
232+ });
233+
234+ test("earlier valid turns remain when latest turn repaired", () => {
235+ const messages = [
236+ { role: "user", content: "turn 1" },
191237 {
192238 role: "assistant",
193239 content: [
194240 {
195241 type: "tool-call",
196- toolCallId: "call-1",
242+ toolCallId: "turn1-call",
243+ toolName: "read",
244+ input: {},
245+ providerOptions: { google: { thoughtSignature: "sig-turn1" } },
246+ },
247+ ],
248+ },
249+ {
250+ role: "tool",
251+ content: [
252+ {
253+ type: "tool-result",
254+ toolCallId: "turn1-call",
197255 toolName: "read",
256+ output: { type: "json", value: { data: 1 } },
257+ },
258+ ],
259+ },
260+ { role: "user", content: "turn 2" },
261+ {
262+ role: "assistant",
263+ content: [
264+ {
265+ type: "tool-call",
266+ toolCallId: "turn2-broken",
267+ toolName: "replace",
198268 input: {},
199- providerMetadata: { google: { thoughtSignature: "sig-1" } },
200269 },
201270 ],
202271 },
@@ -204,20 +273,13 @@ describe("sanitizeGeminiToolMessages", () => {
204273
205274 const sanitized = sanitizeGeminiToolMessages(
206275 messages,
207- "google/gemini-2.5-pro",
276+ "zen/gemini-3.1-pro",
208277 true,
209278 );
210-
211- const assistant = sanitized[1] as {
212- role: "assistant";
213- content: Array<Record<string, unknown>>;
214- };
215- expect(assistant.content[0]?.providerOptions).toEqual({
216- google: { thoughtSignature: "sig-1" },
217- });
279+ expect(sanitized).toEqual(messages.slice(0, 4));
218280 });
219281
220- test("leaves non-Gemini models untouched", () => {
282+ test("non-Gemini untouched", () => {
221283 const messages: CoreMessage[] = [
222284 { role: "user", content: "go" },
223285 {
@@ -230,13 +292,81 @@ describe("sanitizeGeminiToolMessages", () => {
230292 input: {},
231293 },
232294 ],
233- },
295+ } as unknown as CoreMessage,
234296 ];
235297
236298 expect(sanitizeGeminiToolMessages(messages, "openai/gpt-4o", true)).toEqual(
237299 messages,
238300 );
239301 });
302+
303+ test("no Gemini tool-call assistant messages => no-op", () => {
304+ const messages: CoreMessage[] = [
305+ { role: "user", content: "hello" },
306+ { role: "assistant", content: "plain text response" },
307+ { role: "user", content: "next" },
308+ {
309+ role: "tool",
310+ content: [
311+ {
312+ type: "tool-result",
313+ toolCallId: "orphan",
314+ toolName: "read",
315+ output: { type: "text", value: "ok" },
316+ },
317+ ],
318+ } as unknown as CoreMessage,
319+ ];
320+
321+ expect(
322+ sanitizeGeminiToolMessages(messages, "google/gemini-2.5-pro", true),
323+ ).toEqual(messages);
324+ });
325+
326+ test("regression: sanitizer no longer removes up to next user turn for older broken turns", () => {
327+ const messages = [
328+ { role: "user", content: "older broken turn" },
329+ {
330+ role: "assistant",
331+ content: [
332+ {
333+ type: "tool-call",
334+ toolCallId: "older-broken",
335+ toolName: "replace",
336+ input: {},
337+ },
338+ ],
339+ },
340+ {
341+ role: "tool",
342+ content: [
343+ {
344+ type: "tool-result",
345+ toolCallId: "older-broken",
346+ toolName: "replace",
347+ output: { type: "json", value: { stale: true } },
348+ },
349+ ],
350+ },
351+ { role: "user", content: "latest turn" },
352+ {
353+ role: "assistant",
354+ content: [
355+ {
356+ type: "tool-call",
357+ toolCallId: "latest-valid",
358+ toolName: "read",
359+ input: {},
360+ providerOptions: { google: { thoughtSignature: "sig-latest" } },
361+ },
362+ ],
363+ },
364+ ] as unknown as CoreMessage[];
365+
366+ expect(
367+ sanitizeGeminiToolMessages(messages, "google/gemini-2.5-pro", true),
368+ ).toEqual(messages);
369+ });
240370 });
241371
242372 describe("stripGPTCommentaryFromHistory", () => {