From e93012e94e7acfb54d64509b4356ccdeeacc004b Mon Sep 17 00:00:00 2001 From: changjing Date: Mon, 30 Nov 2026 18:18:55 +0800 Subject: [PATCH] fix(antigravity): keep the text-part rule for signature-only parts The Vertex renderer commit let a non-thought part that carries only a thoughtSignature (an empty text part, or a part with no text key) reach the text path. On the Vertex path the text-part signature is dropped since then, so the change has no effect there. On the antigravity platform it added an empty thinking block for such a part: a stream whose signature arrives in an empty final part ended [text, thinking("")], and Claude Code reports an empty result for a turn that ends with a thinking block. main renders [text]. Failure modes this commit guards against: - Antigravity (zero options): "Hello world" followed by an empty final part with a signature, or a part with only a signature, renders an extra empty thinking block (stream with the signature in its own chunk, and non-stream). - Vertex options (marker prefix, text signatures dropped): the output changes. A scratch probe over 8 part shapes (signature-only part before text, after text, after thinking, between text and a call, after a call; signed text; signed image), stream and non-stream, gives the same bytes before and after this commit. With zero options the same probe gives the bytes of main. Changes: - StreamingProcessor and NonStreamingProcessor: the text branch runs again only for a part with text or a thought part, as on main. Tests: - New TestSignatureOnlyTextPartAddsNoBlock: [text] for both tail shapes, stream (two chunks) and non-stream, with zero options and with the Vertex options. The zero-option cases fail on the parent commit. - TestProcessorsKeepSignatureOnlyPartsAndEmptyToolInput loses its two signature-only cases and becomes TestProcessorsKeepEmptyToolInputAndImageSignature. - TestDropTextSignatures: with the drop off, a trailing empty signed part now gives [text]. - TestThinkingSignaturePrefixMarksOnlyThinkingBlocks: the carried text signature now comes from a signed non-empty text part, so the test still checks that the carrier thinking block gets the prefix. Co-Authored-By: Claude Opus 5.5 --- .../pkg/antigravity/response_transformer.go | 4 +- .../pkg/antigravity/signature_part_test.go | 46 ++++++++++++++----- .../pkg/antigravity/stream_transformer.go | 4 +- .../antigravity/text_signature_drop_test.go | 2 +- .../thinking_signature_prefix_test.go | 2 +- 5 files changed, 39 insertions(+), 19 deletions(-) diff --git a/backend/internal/pkg/antigravity/response_transformer.go b/backend/internal/pkg/antigravity/response_transformer.go index 9fdb6e8c4a..8a8efc86c3 100644 --- a/backend/internal/pkg/antigravity/response_transformer.go +++ b/backend/internal/pkg/antigravity/response_transformer.go @@ -229,9 +229,7 @@ func (p *NonStreamingProcessor) processPart(part *GeminiPart) { } // 2. Text 处理 - // 流式响应可能把签名单独放在一个空 text part 里回传,见 - // https://docs.cloud.google.com/vertex-ai/generative-ai/docs/thought-signatures - if part.Text != "" || part.Thought || (signature != "" && part.InlineData == nil) { + if part.Text != "" || part.Thought { if part.Thought { // Thinking part p.flushText() diff --git a/backend/internal/pkg/antigravity/signature_part_test.go b/backend/internal/pkg/antigravity/signature_part_test.go index 02fb73ef56..6d79259349 100644 --- a/backend/internal/pkg/antigravity/signature_part_test.go +++ b/backend/internal/pkg/antigravity/signature_part_test.go @@ -10,23 +10,13 @@ import ( "github.com/stretchr/testify/require" ) -func TestProcessorsKeepSignatureOnlyPartsAndEmptyToolInput(t *testing.T) { +func TestProcessorsKeepEmptyToolInputAndImageSignature(t *testing.T) { tests := []struct { name string parts string wantSignatures []string wantNoArgsCall bool }{ - { - name: "signature in a final empty text part after text", - parts: `[{"text":"done"},{"text":"","thoughtSignature":"SIG_TAIL"}]`, - wantSignatures: []string{"SIG_TAIL"}, - }, - { - name: "signature-only part without a text key", - parts: `[{"text":"done"},{"thoughtSignature":"SIG_BARE"}]`, - wantSignatures: []string{"SIG_BARE"}, - }, { name: "call without args keeps an empty input object", parts: `[{"functionCall":{"name":"TodoRead"},"thoughtSignature":"SIG_CALL"}]`, @@ -84,3 +74,37 @@ func TestProcessorsKeepSignatureOnlyPartsAndEmptyToolInput(t *testing.T) { }) } } + +// 只带签名的 text part(空 text 或没有 text 字段)不产生内容块:回合以文本结束,与不设选项时一致。 +func TestSignatureOnlyTextPartAddsNoBlock(t *testing.T) { + tails := map[string]string{ + "empty text part": `{"text":"","thoughtSignature":"SIG_TAIL"}`, + "part without text": `{"thoughtSignature":"SIG_TAIL"}`, + } + options := map[string]ResponseTransformOptions{ + "zero options": {}, + "vertex options": {ThinkingSignaturePrefix: "vertex-thought-v1:", DropTextSignatures: true}, + } + want := []testBlock{{Type: "text", Text: "Hello world"}} + + for tailName, tail := range tails { + for optionsName, opts := range options { + t.Run("stream/"+optionsName+"/"+tailName, func(t *testing.T) { + processor := NewStreamingProcessor("gemini-4-neon") + processor.SetThinkingSignaturePrefix(opts.ThinkingSignaturePrefix) + processor.SetDropTextSignatures(opts.DropTextSignatures) + out := string(processor.ProcessLine(`data: {"candidates":[{"content":{"role":"model","parts":[{"text":"Hello world"}]}}]}`)) + out += string(processor.ProcessLine(`data: {"candidates":[{"content":{"role":"model","parts":[` + tail + `]},"finishReason":"STOP"}]}`)) + final, _ := processor.Finish() + require.Equal(t, want, streamTestBlocks(t, out+string(final))) + }) + + t.Run("non-stream/"+optionsName+"/"+tailName, func(t *testing.T) { + body := `{"candidates":[{"content":{"role":"model","parts":[{"text":"Hello world"},` + tail + `]},"finishReason":"STOP"}]}` + claudeResp, _, err := TransformGeminiToClaudeWithOptions([]byte(body), "gemini-4-neon", opts) + require.NoError(t, err) + require.Equal(t, want, nonStreamTestBlocks(t, claudeResp)) + }) + } + } +} diff --git a/backend/internal/pkg/antigravity/stream_transformer.go b/backend/internal/pkg/antigravity/stream_transformer.go index 700683800c..e6801c5451 100644 --- a/backend/internal/pkg/antigravity/stream_transformer.go +++ b/backend/internal/pkg/antigravity/stream_transformer.go @@ -290,9 +290,7 @@ func (p *StreamingProcessor) processPart(part *GeminiPart) []byte { } // 2. Text 处理 - // 流式响应可能把签名单独放在一个空 text part 里回传,见 - // https://docs.cloud.google.com/vertex-ai/generative-ai/docs/thought-signatures - if part.Text != "" || part.Thought || (signature != "" && part.InlineData == nil) { + if part.Text != "" || part.Thought { if part.Thought { _, _ = result.Write(p.processThinking(part.Text, signature)) } else { diff --git a/backend/internal/pkg/antigravity/text_signature_drop_test.go b/backend/internal/pkg/antigravity/text_signature_drop_test.go index 922200783b..dd057036bd 100644 --- a/backend/internal/pkg/antigravity/text_signature_drop_test.go +++ b/backend/internal/pkg/antigravity/text_signature_drop_test.go @@ -87,7 +87,7 @@ func TestDropTextSignatures(t *testing.T) { name: "a signature in a trailing empty text part", parts: `{"text":"Answer."},{"text":"","thoughtSignature":"SIG_TAIL"}`, drop: []testBlock{{Type: "text", Text: "Answer."}}, - keep: []testBlock{{Type: "text", Text: "Answer."}, {Type: "thinking", Signature: "SIG_TAIL"}}, + keep: []testBlock{{Type: "text", Text: "Answer."}}, }, { name: "a real thinking block keeps its signature", diff --git a/backend/internal/pkg/antigravity/thinking_signature_prefix_test.go b/backend/internal/pkg/antigravity/thinking_signature_prefix_test.go index 36048a6143..434826bd76 100644 --- a/backend/internal/pkg/antigravity/thinking_signature_prefix_test.go +++ b/backend/internal/pkg/antigravity/thinking_signature_prefix_test.go @@ -14,7 +14,7 @@ func TestThinkingSignaturePrefixMarksOnlyThinkingBlocks(t *testing.T) { const chunk = `{"candidates":[{"content":{"role":"model","parts":[` + `{"text":"plan","thought":true,"thoughtSignature":"SIG_THOUGHT"},` + `{"functionCall":{"name":"Read","args":{}},"thoughtSignature":"SIG_CALL"},` + - `{"text":"","thoughtSignature":"SIG_TAIL"}]},"finishReason":"STOP"}]}` + `{"text":"Done.","thoughtSignature":"SIG_TAIL"}]},"finishReason":"STOP"}]}` for _, prefix := range []string{"", "p:"} { t.Run("stream/prefix="+prefix, func(t *testing.T) {