xonecas.io
64875
Mc

mini-coder / commit 11c92d3

fix: harden Gemini tool-call handling

xonecas2026-03-11 11c92d3d0d17717d787b118f025909aa91500d82 parent 1b3ccf7

5 files changed

TODO.md+58−0
@@ -54,6 +54,64 @@ AI_APICallError: Unable to submit request because function call `default_api:rep
5454 https://opencode.ai/zen/v1/models/gemini-3.1-pro:streamGenerateContent?alt=sse
5555 ```
5656
57+## Gpt 5.4 starts printing garbage:
58+
59+```
60+ ← read src/llm-api/turn.test.ts:108+50
61+ · src/llm-api/turn.test.ts lines 108–142 of 142
62+ ✎ replace src/llm-api/turn.test.ts 116:1c–140:39
63+⠴ replace ✔ hook post-replace
64+ ✔ replaced src/llm-api/turn.test.ts
65+ --- src/llm-api/turn.test.ts
66+ +++ src/llm-api/turn.test.ts
67+ @@ -113,11 +113,6 @@
68+ });
69+ });
70+
71+ - test("leaves non-Gemini models untouched", () => {
72+ - const messages = [
73+ - { role: "user", content: "go" },
74+ - {
75+ - role: "assistant",
76+ test("leaves non-Gemini models untouched", () => {
77+ const messages: CoreMessage[] = [
78+ { role: "user", content: "go" },
79+ ← read src/llm-api/turn.test.ts:108+40
80+ · src/llm-api/turn.test.ts lines 108–137 of 137
81+◆ to=functions.shell 彩神争霸大发json
82+{"command":"bun test src/llm-api/turn.test.ts && bun run typecheck","timeout":120000}ո՟иҭanalysis to=functions.shell code
83+{"command":"bun test src/llm-api/turn.test.ts && bun run typecheck","timeout":120000} frontcompatto=functions.shell 手机上天天中彩票json
84+{"command":"bun test src/llm-api/turn.test.ts && bun run typecheck","timeout":120000}({
85+})Oopsto=functions.shell 彩神争霸官方څjson
86+{"command":"bun test src/llm-api/turn.test.ts && bun run typecheck","timeout":120000}We need ensure JSON only.to=functions.shell ส่งเงินบาทไทยjson
87+
88+error: script "dev" exited with code 130
89+```
90+
91+## Gpt codex context window maxinng out
92+
93+I've noticed several sessions with codex spark that error because the context got too big for the model:
94+
95+```
96+ ← read node_modules/@ai-sdk/google/src/google-generative-ai-language-model.ts:680+120
97+ · node_modules/@ai-sdk/google/src/google-generative-ai-language-model.ts lines 680–799 of 1057 (truncated)
98+⠙ thinking{
99+ type: "error",
100+ sequence_number: 2,
101+ error: {
102+ type: "invalid_request_error",
103+ code: "context_length_exceeded",
104+ message: "Your input exceeds the context window of this model. Please adjust your input and try again.",
105+ param: "input",
106+ },
107+}
108+✖ [object Object]
109+✖ [object Object]
110+error: script "dev" exited with code 1
111+```
112+
113+The same task with GPT 5.4: `ctx 82.8k/1050.0k 8%` used on 82k in context. I know spark is a smaller context, but I'm suprised by how often we max out the 128k context.
114+
57115 ## LSP Diagnostics (not very important, with tool-hooks and strong linting, this is not as necessary, but we should implement asap)
58116
59117 We should have a closed loop feedback for LSP diagnostics on edits/reads.
src/llm-api/providers.test.ts+33−0
@@ -0,0 +1,33 @@
1+import { describe, expect, test } from "bun:test";
2+
3+import { shouldDisableGeminiThinkingForTools } from "./providers.ts";
4+
5+describe("shouldDisableGeminiThinkingForTools", () => {
6+ test("enables workaround for affected Gemini versions", () => {
7+ expect(shouldDisableGeminiThinkingForTools("zen/gemini-3.1-pro")).toBe(
8+ true,
9+ );
10+ expect(shouldDisableGeminiThinkingForTools("google/gemini-2.5-pro")).toBe(
11+ true,
12+ );
13+ expect(
14+ shouldDisableGeminiThinkingForTools("google/gemini-2.5-flash-preview"),
15+ ).toBe(true);
16+ });
17+
18+ test("does not enable workaround for non-affected providers", () => {
19+ expect(
20+ shouldDisableGeminiThinkingForTools("anthropic/claude-4-sonnet"),
21+ ).toBe(false);
22+ expect(shouldDisableGeminiThinkingForTools("openai/gpt-4o")).toBe(false);
23+ });
24+
25+ test("does not enable workaround for unrelated Gemini models", () => {
26+ expect(shouldDisableGeminiThinkingForTools("google/gemini-1.5-pro")).toBe(
27+ false,
28+ );
29+ expect(shouldDisableGeminiThinkingForTools("zen/some-other-model")).toBe(
30+ false,
31+ );
32+ });
33+});
src/llm-api/providers.ts+15−1
@@ -200,12 +200,27 @@ function clampEffort(
200200 return ORDER[Math.min(i, m)] as ThinkingEffort;
201201 }
202202
203+const GEMINI_TOOL_CALL_AFFECTED_MODELS = /^gemini-(2\.5|3)/;
204+
205+export function shouldDisableGeminiThinkingForTools(
206+ modelString: string,
207+): boolean {
208+ const { provider, modelId } = parseModelString(modelString);
209+ if (provider !== "google" && provider !== "zen") return false;
210+ return GEMINI_TOOL_CALL_AFFECTED_MODELS.test(modelId);
211+}
212+
203213 export function getThinkingProviderOptions(
204214 modelString: string,
205215 effort: ThinkingEffort,
216+ hasTools = false,
206217 ): Record<string, unknown> | null {
207218 if (!supportsThinking(modelString)) return null;
208219
220+ if (hasTools && shouldDisableGeminiThinkingForTools(modelString)) {
221+ return null;
222+ }
223+
209224 const { provider, modelId } = parseModelString(modelString);
210225
211226 // Anthropic (direct or via Zen routing through @ai-sdk/anthropic)
@@ -218,7 +233,6 @@ export function getThinkingProviderOptions(
218233 /^claude-3-7/.test(modelId) ||
219234 /^claude-sonnet-4/.test(modelId) ||
220235 /^claude-opus-4/.test(modelId);
221-
222236 if (isAdaptive) {
223237 // Adaptive: effort string ("low"|"medium"|"high"|"max")
224238 // xhigh maps to "max" for Opus; clamp to "high" for others.
src/llm-api/turn.test.ts+169−1
@@ -1,5 +1,6 @@
11 import { describe, expect, test } from "bun:test";
2-import { isOpenAIGPT } from "./turn.ts";
2+import type { CoreMessage } from "./turn.ts";
3+import { isOpenAIGPT, sanitizeGeminiToolMessages } from "./turn.ts";
34
45 describe("isOpenAIGPT", () => {
56 test("matches openai/gpt-* models", () => {
@@ -24,3 +25,170 @@ describe("isOpenAIGPT", () => {
2425 expect(isOpenAIGPT("zen/claude-sonnet-4-6")).toBe(false);
2526 });
2627 });
28+
29+describe("sanitizeGeminiToolMessages", () => {
30+ test("truncates the current Gemini tool turn when any tool call lacks a thought signature", () => {
31+ const messages = [
32+ { role: "user", content: "older turn" },
33+ {
34+ role: "assistant",
35+ content: [
36+ {
37+ type: "tool-call",
38+ toolCallId: "old-call",
39+ toolName: "read",
40+ input: {},
41+ providerOptions: { google: { thoughtSignature: "sig-old" } },
42+ },
43+ ],
44+ },
45+ {
46+ role: "tool",
47+ content: [] as unknown as CoreMessage["content"],
48+ },
49+ { role: "user", content: "current turn" },
50+ {
51+ role: "assistant",
52+ content: [
53+ {
54+ type: "tool-call",
55+ toolCallId: "ok-call",
56+ toolName: "read",
57+ input: {},
58+ providerOptions: { google: { thoughtSignature: "sig-ok" } },
59+ },
60+ {
61+ type: "tool-call",
62+ toolCallId: "broken-call",
63+ toolName: "replace",
64+ input: {},
65+ },
66+ ],
67+ },
68+ {
69+ role: "tool",
70+ content: [
71+ {
72+ type: "tool-result",
73+ toolCallId: "ok-call",
74+ toolName: "read",
75+ output: { type: "json", value: { ok: true } },
76+ },
77+ {
78+ type: "tool-result",
79+ toolCallId: "broken-call",
80+ toolName: "replace",
81+ output: { type: "json", value: { ok: true } },
82+ },
83+ ],
84+ },
85+ ] as unknown as CoreMessage[];
86+
87+ const sanitized = sanitizeGeminiToolMessages(
88+ messages,
89+ "zen/gemini-3.1-pro",
90+ true,
91+ );
92+
93+ expect(sanitized).toEqual(messages.slice(0, 4));
94+ });
95+
96+ test("drops earlier broken Gemini tool turns and keeps the latest user turn", () => {
97+ const messages = [
98+ { role: "user", content: "broken turn" },
99+ {
100+ role: "assistant",
101+ content: [
102+ {
103+ type: "tool-call",
104+ toolCallId: "signed-call",
105+ toolName: "read",
106+ input: {},
107+ providerOptions: { google: { thoughtSignature: "sig-1" } },
108+ },
109+ {
110+ type: "tool-call",
111+ toolCallId: "unsigned-call",
112+ toolName: "replace",
113+ input: {},
114+ },
115+ ],
116+ },
117+ {
118+ role: "tool",
119+ content: [] as unknown as CoreMessage["content"],
120+ },
121+ { role: "assistant", content: "done" },
122+ { role: "user", content: "latest turn" },
123+ {
124+ role: "assistant",
125+ content: [
126+ {
127+ type: "tool-call",
128+ toolCallId: "latest-call",
129+ toolName: "read",
130+ input: {},
131+ providerOptions: { google: { thoughtSignature: "sig-2" } },
132+ },
133+ ],
134+ },
135+ ] as unknown as CoreMessage[];
136+
137+ expect(
138+ sanitizeGeminiToolMessages(messages, "google/gemini-2.5-pro", true),
139+ ).toEqual(messages.slice(4));
140+ });
141+
142+ test("copies legacy providerMetadata into providerOptions for Gemini tool calls", () => {
143+ const messages = [
144+ { role: "user", content: "current turn" },
145+ {
146+ role: "assistant",
147+ content: [
148+ {
149+ type: "tool-call",
150+ toolCallId: "call-1",
151+ toolName: "read",
152+ input: {},
153+ providerMetadata: { google: { thoughtSignature: "sig-1" } },
154+ },
155+ ],
156+ },
157+ ] as unknown as CoreMessage[];
158+
159+ const sanitized = sanitizeGeminiToolMessages(
160+ messages,
161+ "google/gemini-2.5-pro",
162+ true,
163+ );
164+
165+ const assistant = sanitized[1] as {
166+ role: "assistant";
167+ content: Array<Record<string, unknown>>;
168+ };
169+ expect(assistant.content[0]?.providerOptions).toEqual({
170+ google: { thoughtSignature: "sig-1" },
171+ });
172+ });
173+
174+ test("leaves non-Gemini models untouched", () => {
175+ const messages: CoreMessage[] = [
176+ { role: "user", content: "go" },
177+ {
178+ role: "assistant",
179+ content: [
180+ {
181+ type: "tool-call",
182+ toolCallId: "call-1",
183+ toolName: "read",
184+ input: {},
185+ },
186+ ],
187+ },
188+ ];
189+
190+ expect(sanitizeGeminiToolMessages(messages, "openai/gpt-4o", true)).toEqual(
191+ messages,
192+ );
193+ });
194+});
src/llm-api/turn.ts+115−2
@@ -4,6 +4,7 @@ import { logApiEvent } from "./api-log.ts";
44 import {
55 getThinkingProviderOptions,
66 parseModelString,
7+ shouldDisableGeminiThinkingForTools,
78 type ThinkingEffort,
89 } from "./providers.ts";
910 import type { ToolDef, TurnEvent } from "./types.ts";
@@ -48,6 +49,105 @@ function toCoreTool(def: ToolDef): ReturnType<typeof dynamicTool> {
4849 });
4950 }
5051
52+function isRecord(value: unknown): value is Record<string, unknown> {
53+ return value !== null && typeof value === "object";
54+}
55+
56+function normalizeProviderOptions<T>(part: T): T {
57+ if (!isRecord(part)) return part;
58+ if (
59+ part.providerOptions !== undefined ||
60+ part.providerMetadata === undefined
61+ ) {
62+ return part;
63+ }
64+ return {
65+ ...part,
66+ providerOptions: part.providerMetadata,
67+ } as T;
68+}
69+
70+function normalizeMessageProviderOptions(message: CoreMessage): CoreMessage {
71+ if (!Array.isArray(message.content)) return message;
72+ return {
73+ ...message,
74+ content: message.content.map((part) =>
75+ normalizeProviderOptions(part),
76+ ) as CoreMessage["content"],
77+ } as CoreMessage;
78+}
79+
80+function getGeminiThoughtSignature(part: unknown): string | null {
81+ if (!isRecord(part)) return null;
82+ const providerOptions = isRecord(part.providerOptions)
83+ ? part.providerOptions
84+ : isRecord(part.providerMetadata)
85+ ? part.providerMetadata
86+ : null;
87+ if (!providerOptions) return null;
88+
89+ for (const provider of ["google", "vertex"] as const) {
90+ const metadata = providerOptions[provider];
91+ if (!isRecord(metadata)) continue;
92+ const signature = metadata.thoughtSignature;
93+ if (typeof signature === "string" && signature.length > 0) {
94+ return signature;
95+ }
96+ }
97+
98+ return null;
99+}
100+
101+function isToolCallPart(part: unknown): part is Record<string, unknown> {
102+ return isRecord(part) && part.type === "tool-call";
103+}
104+
105+function assistantMessageHasUnsignedGeminiToolCall(
106+ message: CoreMessage,
107+): boolean {
108+ if (message.role !== "assistant" || !Array.isArray(message.content)) {
109+ return false;
110+ }
111+
112+ for (const part of message.content) {
113+ if (!isToolCallPart(part)) continue;
114+ if (getGeminiThoughtSignature(part) === null) return true;
115+ }
116+
117+ return false;
118+}
119+
120+export function sanitizeGeminiToolMessages(
121+ messages: CoreMessage[],
122+ modelString: string,
123+ hasTools: boolean,
124+): CoreMessage[] {
125+ if (!hasTools || !shouldDisableGeminiThinkingForTools(modelString)) {
126+ return messages;
127+ }
128+
129+ let sanitized = messages.map((message) =>
130+ normalizeMessageProviderOptions(message),
131+ );
132+
133+ while (true) {
134+ const brokenIndex = sanitized.findIndex((message) =>
135+ assistantMessageHasUnsignedGeminiToolCall(message),
136+ );
137+ if (brokenIndex === -1) return sanitized;
138+
139+ const nextUserIndex = sanitized.findIndex(
140+ (message, index) => index > brokenIndex && message.role === "user",
141+ );
142+ if (nextUserIndex !== -1) {
143+ sanitized = sanitized.slice(nextUserIndex);
144+ continue;
145+ }
146+
147+ return sanitized.slice(0, brokenIndex);
148+ }
149+}
150+
51151 // ─── Main turn function ───────────────────────────────────────────────────────
52152
53153 /**
@@ -112,8 +212,21 @@ export async function* runTurn(options: {
112212
113213 logApiEvent("turn start", { modelString, messageCount: messages.length });
114214
215+ const toolCount = Object.keys(toolSet).length;
216+ const turnMessages = sanitizeGeminiToolMessages(
217+ messages,
218+ modelString,
219+ toolCount > 0,
220+ );
221+ if (turnMessages.length !== messages.length) {
222+ logApiEvent("gemini tool history truncated", {
223+ modelString,
224+ originalMessages: messages.length,
225+ sanitizedMessages: turnMessages.length,
226+ });
227+ }
115228 const thinkingOpts = thinkingEffort
116- ? getThinkingProviderOptions(modelString, thinkingEffort)
229+ ? getThinkingProviderOptions(modelString, thinkingEffort, toolCount > 0)
117230 : null;
118231
119232 const mergedProviderOptions = {
@@ -134,7 +247,7 @@ export async function* runTurn(options: {
134247
135248 const streamOpts: StreamTextOptions = {
136249 model,
137- messages,
250+ messages: turnMessages,
138251 tools: toolSet,
139252 stopWhen: stepCountIs(MAX_STEPS),
140253 onStepFinish: (step: StepResult<ToolSet>) => {