Skip to content

Commit 6eb5903

Browse files
committed
fix(google): fail closed on MALFORMED_FUNCTION_CALL without a call part
Gemini usually drops the malformed call upstream, so the final chunk carries only the finishReason and the started-calls guard never fired. The turn surfaced as a clean empty completion instead of an error. MAX_TOKENS with no started call keeps its plain token-limit stop.
1 parent 6a7351b commit 6eb5903

3 files changed

Lines changed: 42 additions & 4 deletions

File tree

‎src/adapters/google-truncation.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,14 @@ export function vertexTruncationErrorMessage(reason?: string): string {
1111
const suffix = reason ? ` (${redactSecretString(reason).slice(0, 160)})` : "";
1212
return `Vertex AI response truncated upstream before the turn completed${suffix}`;
1313
}
14+
15+
/**
16+
* Whether a finished turn must fail closed. A truncation reason arriving mid tool call always
17+
* does. MALFORMED_FUNCTION_CALL fails closed even with zero started calls: the malformed call
18+
* is dropped upstream and usually never materializes as a part, so the turn is incomplete
19+
* despite looking empty. MAX_TOKENS with no started call stays a plain token-limit stop.
20+
*/
21+
export function isVertexTruncatedTurn(finishReason: string | undefined, toolCallsStarted: number): boolean {
22+
if (!isVertexTruncationReason(finishReason)) return false;
23+
return toolCallsStarted > 0 || finishReason === "MALFORMED_FUNCTION_CALL";
24+
}

‎src/adapters/google.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { contentPartsToText, parseDataUrl } from "./image";
1717
import { getVertexAccessToken } from "../lib/gcp-adc";
1818
import { fetchAntigravityWithRetry, fetchVertexWithRetry } from "./google-http";
1919
import { safeAntigravityHttpErrorMessage, safeVertexHttpErrorMessage } from "./google-errors";
20-
import { isVertexTruncationReason, vertexTruncationErrorMessage } from "./google-truncation";
20+
import { isVertexTruncatedTurn, vertexTruncationErrorMessage } from "./google-truncation";
2121
import { ANTIGRAVITY_REQUEST_UA, antigravitySessionId, isLikelyRealThoughtSignature, sanitizeAntigravityClaudeSignatures } from "./google-antigravity-wire";
2222
import { compileGoogleWireBody } from "./google-wire-compiler";
2323
import { identifyRoutedModel } from "./identity";
@@ -596,7 +596,7 @@ export function createGoogleAdapter(provider: OcxProviderConfig): ProviderAdapte
596596
// Fail-closed: a turn cut off mid tool call (MAX_TOKENS / MALFORMED_FUNCTION_CALL) surfaces
597597
// an error instead of a silently-incomplete done. Mirrors kiro-truncation.
598598
if ((provider.googleMode === "vertex" || provider.googleMode === "cloud-code-assist")
599-
&& toolCallsStarted > 0 && isVertexTruncationReason(lastFinishReason)) {
599+
&& isVertexTruncatedTurn(lastFinishReason, toolCallsStarted)) {
600600
yield { type: "error", message: vertexTruncationErrorMessage(lastFinishReason) };
601601
return;
602602
}
@@ -752,7 +752,7 @@ export function createGoogleAdapter(provider: OcxProviderConfig): ProviderAdapte
752752
// Fail-closed truncation, same as the stream path: a non-stream turn cut off mid tool call
753753
// (MAX_TOKENS / MALFORMED_FUNCTION_CALL) surfaces an error instead of a silent done.
754754
if ((provider.googleMode === "vertex" || provider.googleMode === "cloud-code-assist")
755-
&& toolCallsStarted > 0 && isVertexTruncationReason(candidates?.[0]?.finishReason)) {
755+
&& isVertexTruncatedTurn(candidates?.[0]?.finishReason, toolCallsStarted)) {
756756
return finish([{ type: "error", message: vertexTruncationErrorMessage(candidates?.[0]?.finishReason) }]);
757757
}
758758

‎tests/google-vertex-stream.test.ts‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, test } from "bun:test";
22
import { createGoogleAdapter as createGoogleAdapterProduction } from "../src/adapters/google";
3-
import { isVertexTruncationReason, vertexTruncationErrorMessage } from "../src/adapters/google-truncation";
3+
import { isVertexTruncatedTurn, isVertexTruncationReason, vertexTruncationErrorMessage } from "../src/adapters/google-truncation";
44
import { bridgeToResponsesSSE } from "../src/bridge";
55
import type { AdapterEvent, OcxProviderConfig } from "../src/types";
66
import { withTestTranslatorBudget } from "./helpers/translator-budget";
@@ -30,6 +30,14 @@ describe("vertex truncation helpers", () => {
3030
expect(isVertexTruncationReason(undefined)).toBe(false);
3131
expect(vertexTruncationErrorMessage("MAX_TOKENS")).toContain("truncated upstream");
3232
});
33+
34+
test("MALFORMED_FUNCTION_CALL fails closed with zero started calls; MAX_TOKENS does not", () => {
35+
expect(isVertexTruncatedTurn("MALFORMED_FUNCTION_CALL", 0)).toBe(true);
36+
expect(isVertexTruncatedTurn("MAX_TOKENS", 0)).toBe(false);
37+
expect(isVertexTruncatedTurn("MAX_TOKENS", 1)).toBe(true);
38+
expect(isVertexTruncatedTurn("STOP", 5)).toBe(false);
39+
expect(isVertexTruncatedTurn(undefined, 5)).toBe(false);
40+
});
3341
});
3442

3543
describe("vertex parseStream fail-closed truncation", () => {
@@ -77,6 +85,17 @@ describe("vertex parseStream fail-closed truncation", () => {
7785
expect(text).toContain('"incomplete_details":{"reason":"max_output_tokens"}');
7886
});
7987

88+
test("MALFORMED_FUNCTION_CALL with NO emitted call part yields a terminal error, not done", async () => {
89+
// The malformed call is dropped upstream, so the final chunk usually carries only the
90+
// finishReason. Without the guard this surfaced as a clean empty completion.
91+
const events = await collect(vertexProvider, [
92+
{ candidates: [{ finishReason: "MALFORMED_FUNCTION_CALL" }], usageMetadata: { promptTokenCount: 5, candidatesTokenCount: 0 } },
93+
]);
94+
const last = events[events.length - 1];
95+
expect(last.type).toBe("error");
96+
expect(events.some(e => e.type === "done")).toBe(false);
97+
});
98+
8099
test("usage-only final chunk (no candidates) is not dropped", async () => {
81100
const events = await collect(vertexProvider, [
82101
{ candidates: [{ content: { parts: [{ text: "hi" }] } }] },
@@ -98,6 +117,14 @@ describe("vertex parseResponse fail-closed truncation (non-streaming)", () => {
98117
expect(events.some(e => e.type === "done")).toBe(false);
99118
});
100119

120+
test("MALFORMED_FUNCTION_CALL with no call part yields a terminal error, not done", async () => {
121+
const adapter = createGoogleAdapter(vertexProvider);
122+
const body = JSON.stringify({ candidates: [{ finishReason: "MALFORMED_FUNCTION_CALL" }], usageMetadata: { promptTokenCount: 5, candidatesTokenCount: 0 } });
123+
const events = await adapter.parseResponse!(new Response(body, { status: 200 }));
124+
expect(events[events.length - 1].type).toBe("error");
125+
expect(events.some(e => e.type === "done")).toBe(false);
126+
});
127+
101128
test("clean STOP non-stream response yields done", async () => {
102129
const adapter = createGoogleAdapter(vertexProvider);
103130
const body = JSON.stringify({ candidates: [{ content: { parts: [{ text: "ok" }] }, finishReason: "STOP" }], usageMetadata: { promptTokenCount: 2, candidatesTokenCount: 1 } });

0 commit comments

Comments
 (0)