From ce026a2cbf1e1d366ddd64fe098e8a0334949a7b Mon Sep 17 00:00:00 2001 From: green_bean <62200636+little-greenbean@users.noreply.github.com> Date: Sun, 15 Nov 2026 13:52:12 +0800 Subject: [PATCH] fix(gemini): retain complete string argument snapshots --- .../gemini_chat_completions_compat_service.go | 2 +- .../service/gemini_messages_compat_service.go | 2 +- .../service/gemini_parallel_tools_test.go | 8 ++++++ .../service/gemini_tool_call_boundary.go | 27 +++++++++---------- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/backend/internal/service/gemini_chat_completions_compat_service.go b/backend/internal/service/gemini_chat_completions_compat_service.go index e89587866d..4626131b2e 100644 --- a/backend/internal/service/gemini_chat_completions_compat_service.go +++ b/backend/internal/service/gemini_chat_completions_compat_service.go @@ -764,7 +764,7 @@ func (s *GeminiMessagesCompatService) handleChatCompletionsStreamingResponseFrom } delta := seenToolJSON.next(argsJSONText) - toolBoundary.observe(fc, seenToolJSON.seen.String()) + toolBoundary.observe(fc) if delta != "" { if emitAnthropicEvent(&apicompat.AnthropicStreamEvent{ Type: "content_block_delta", diff --git a/backend/internal/service/gemini_messages_compat_service.go b/backend/internal/service/gemini_messages_compat_service.go index b0e6b308cd..4aee3e384d 100644 --- a/backend/internal/service/gemini_messages_compat_service.go +++ b/backend/internal/service/gemini_messages_compat_service.go @@ -2428,7 +2428,7 @@ func (s *GeminiMessagesCompatService) handleStreamingResponse(c *gin.Context, re } delta := seenToolJSON.next(argsJSONText) - toolBoundary.observe(fc, seenToolJSON.seen.String()) + toolBoundary.observe(fc) if delta != "" { writeSSE(c.Writer, "content_block_delta", map[string]any{ "type": "content_block_delta", diff --git a/backend/internal/service/gemini_parallel_tools_test.go b/backend/internal/service/gemini_parallel_tools_test.go index 66babba3b2..30145de0d3 100644 --- a/backend/internal/service/gemini_parallel_tools_test.go +++ b/backend/internal/service/gemini_parallel_tools_test.go @@ -37,6 +37,14 @@ func TestGeminiParallelToolStreams(t *testing.T) { {"late_id_then_distinct_same_arguments_call", [][]string{{`{"name":"read_file","args":"{\"path\":"}`}, {`{"name":"read_file","args":"\"a\"}","id":"a"}`}, {`{"name":"read_file","args":{"path":"a"},"id":"b"}`}}, []string{`{"path":"a"}`, `{"path":"a"}`}}, {"anonymous_cumulative_final_string_then_snapshot", [][]string{{`{"name":"read_file","args":"{\"path\":"}`}, {`{"name":"read_file","args":"{\"path\":\"a\"}"}`}, {`{"name":"read_file","args":"{\"path\":\"a\"}"}`}, {`{"name":"read_file","args":{"path":"a"}}`}}, []string{`{"path":"a"}`}}, {"explicit_id_change_overrides_fragment_snapshot", [][]string{{`{"name":"read_file","args":"{\"path\":","id":"a"}`}, {`{"name":"read_file","args":"\"a\"}","id":"a"}`}, {`{"name":"read_file","args":{"path":"a"},"id":"b"}`}}, []string{`{"path":"a"}`, `{"path":"a"}`}}, + {"complete_string_duplicate_no_ids", [][]string{{`{"name":"read_file","args":"{\"path\":\"a\"}"}`}, {`{"name":"read_file","args":"{\"path\":\"a\"}"}`}}, []string{`{"path":"a"}`}}, + {"complete_string_duplicate_first_id_only", [][]string{{`{"name":"read_file","args":"{\"path\":\"a\"}","id":"a"}`}, {`{"name":"read_file","args":"{\"path\":\"a\"}"}`}}, []string{`{"path":"a"}`}}, + {"complete_string_duplicate_final_object_same_id", [][]string{{`{"name":"read_file","args":"{\"path\":\"a\"}","id":"a"}`}, {`{"name":"read_file","args":"{\"path\":\"a\"}"}`}, {`{"name":"read_file","args":{"path":"a"},"id":"a"}`}}, []string{`{"path":"a"}`}}, + {"complete_string_final_object_no_ids", [][]string{{`{"name":"read_file","args":"{\"path\":\"a\"}"}`}, {`{"name":"read_file","args":{"path":"a"}}`}}, []string{`{"path":"a"}`}}, + {"complete_strings_stable_id", [][]string{{`{"name":"read_file","args":"{\"path\":\"a\"}","id":"a"}`}, {`{"name":"read_file","args":"{\"path\":\"a\"}","id":"a"}`}, {`{"name":"read_file","args":{"path":"a"},"id":"a"}`}}, []string{`{"path":"a"}`}}, + {"complete_strings_distinct_ids", [][]string{{`{"name":"read_file","args":"{\"path\":\"a\"}","id":"a"}`}, {`{"name":"read_file","args":"{\"path\":\"a\"}","id":"b"}`}}, []string{`{"path":"a"}`, `{"path":"a"}`}}, + {"complete_string_snapshot_then_identical_native_call", [][]string{{`{"name":"read_file","args":"{\"path\":\"a\"}"}`}, {`{"name":"read_file","args":{"path":"a"}}`}, {`{"name":"read_file","args":{"path":"a"}}`}}, []string{`{"path":"a"}`, `{"path":"a"}`}}, + {"complete_string_late_id_then_new_id", [][]string{{`{"name":"read_file","args":"{\"path\":\"a\"}"}`}, {`{"name":"read_file","args":"{\"path\":\"a\"}","id":"a"}`}, {`{"name":"read_file","args":{"path":"a"},"id":"b"}`}}, []string{`{"path":"a"}`, `{"path":"a"}`}}, {"different_names", [][]string{{`{"name":"read_file","args":{}}`, `{"name":"read_other","args":{}}`}}, []string{`{}`, `{}`}}, } for _, tt := range tests { diff --git a/backend/internal/service/gemini_tool_call_boundary.go b/backend/internal/service/gemini_tool_call_boundary.go index 9b2edd9a6e..5388e27e09 100644 --- a/backend/internal/service/gemini_tool_call_boundary.go +++ b/backend/internal/service/gemini_tool_call_boundary.go @@ -7,13 +7,13 @@ import ( // geminiToolCallBoundary tracks identity separately from the function name. // Native object-valued args without IDs represent independent calls, including -// identical calls. String-valued fragments may end with a cumulative snapshot; -// preserve that snapshot's identity until its object-valued args arrive. -// Without IDs, a terminal snapshot and a new identical call immediately after -// fragments are indistinguishable; prefer the snapshot in that specific case. +// identical calls. String-valued arguments use stream mode even when the first +// chunk is already complete JSON; retain duplicate strings and the terminal +// object snapshot in that call. Without IDs, a terminal snapshot and a new +// identical call after strings are indistinguishable; prefer the snapshot. type geminiToolCallBoundary struct { upstreamID string - fragmented bool + stringArgs bool } func (b *geminiToolCallBoundary) startsNewBlock(openName, seenJSON, argsJSON string, fc map[string]any) bool { @@ -32,24 +32,21 @@ func (b *geminiToolCallBoundary) startsNewBlock(openName, seenJSON, argsJSON str return false } // Keep the existing delta accumulator's duplicate/rewind behavior for a - // fragmented call. Do not deduplicate separate complete native calls. - if b.fragmented && strings.HasPrefix(seenJSON, strings.TrimSuffix(argsJSON, "\u0000")) { + // string-argument call. Do not deduplicate separate complete native calls. + if b.stringArgs && strings.HasPrefix(seenJSON, strings.TrimSuffix(argsJSON, "\u0000")) { return false } return true } -func (b *geminiToolCallBoundary) observe(fc map[string]any, seenJSON string) { +func (b *geminiToolCallBoundary) observe(fc map[string]any) { if b.upstreamID == "" { b.upstreamID, _ = fc["id"].(string) } - if _, isString := fc["args"].(string); isString { - b.fragmented = b.fragmented || !json.Valid([]byte(seenJSON)) - } else { - // The terminal object snapshot has been consumed. A following - // anonymous complete object can now be another identical call. - b.fragmented = false - } + // The representation determines the mode, not whether the first string + // happens to contain complete JSON. An object consumes the final snapshot, + // allowing the next anonymous native object to be an independent call. + _, b.stringArgs = fc["args"].(string) } func geminiToolCallArgsJSON(args any) string {