diff --git a/backend/internal/service/gateway_request.go b/backend/internal/service/gateway_request.go index 213deaafcf..f68d87a5ac 100644 --- a/backend/internal/service/gateway_request.go +++ b/backend/internal/service/gateway_request.go @@ -1495,7 +1495,117 @@ func filterThinkingBlocksInternal(body []byte, alwaysThinking bool) []byte { !bytes.Contains(body, []byte(`"thinking" :`)) { return body } + // Signed thinking history and top-level thinking config defeat the substring + // check on most requests; decide without decoding when nothing is dropped. + if needs, decided := thinkingBlocksNeedFiltering(body, alwaysThinking); decided && !needs { + return body + } + return filterThinkingBlocksDecoded(body, alwaysThinking) +} +// thinkingBlocksNeedFiltering reports whether filterThinkingBlocksDecoded would +// drop a block, mirroring its decisions on borrowed gjson views with +// encoding/json's last-wins rule for duplicate keys. The body is borrowed only +// during this call. A non-object root is left to the decoding path +// (decided=false). Invalid JSON needs no separate scan: the decoding path +// returns it unchanged, so either answer yields the same body. +func thinkingBlocksNeedFiltering(body []byte, alwaysThinking bool) (needs, decided bool) { + view := openAIWSPayloadStringView(body) + root := gjson.Parse(view) + if !root.IsObject() { + return false, false + } + var thinking, messages gjson.Result + root.ForEach(func(key, value gjson.Result) bool { + switch key.String() { + case "thinking": + thinking = value + case "messages": + messages = value + } + return true + }) + + thinkingEnabled := alwaysThinking + if thinking.IsObject() { + var thinkType gjson.Result + thinking.ForEach(func(key, value gjson.Result) bool { + if key.String() == "type" { + thinkType = value + } + return true + }) + if thinkType.Type == gjson.String && (thinkType.Str == "enabled" || thinkType.Str == "adaptive") { + thinkingEnabled = true + } + } + if !messages.IsArray() { + return false, true + } + + messages.ForEach(func(_, msg gjson.Result) bool { + if !msg.IsObject() { + return true + } + var role, content gjson.Result + msg.ForEach(func(key, value gjson.Result) bool { + switch key.String() { + case "role": + role = value + case "content": + content = value + } + return true + }) + if !content.IsArray() { + return true + } + isAssistant := role.Type == gjson.String && role.Str == "assistant" + content.ForEach(func(_, block gjson.Result) bool { + if !block.IsObject() { + return true + } + var blockType, data, signature gjson.Result + hasThinking := false + block.ForEach(func(key, value gjson.Result) bool { + switch key.String() { + case "type": + blockType = value + case "data": + data = value + case "signature": + signature = value + case "thinking": + hasThinking = true + } + return true + }) + typ := "" + if blockType.Type == gjson.String { + typ = blockType.Str + } + switch { + case typ == "thinking" || typ == "redacted_thinking": + if thinkingEnabled && isAssistant { + if alwaysThinking && typ == "redacted_thinking" && data.Type == gjson.String && data.Str != "" { + return true + } + if signature.Type == gjson.String && signature.Str != "" && signature.Str != antigravity.DummyThoughtSignature { + return true + } + } + needs = true + case typ == "" && hasThinking: + needs = true + } + return !needs + }) + return !needs + }) + return needs, true +} + +func filterThinkingBlocksDecoded(body []byte, alwaysThinking bool) []byte { var req map[string]any if err := json.Unmarshal(body, &req); err != nil { return body diff --git a/backend/internal/service/gateway_thinking_filter_precheck_test.go b/backend/internal/service/gateway_thinking_filter_precheck_test.go new file mode 100644 index 0000000000..b7cb36c4d9 --- /dev/null +++ b/backend/internal/service/gateway_thinking_filter_precheck_test.go @@ -0,0 +1,123 @@ +package service + +import ( + "bytes" + "encoding/json" + "strings" + "testing" + + "github.com/Wei-Shaw/sub2api/internal/pkg/antigravity" + "github.com/stretchr/testify/require" +) + +// The precheck may only skip the decoding path when that path would return the +// body unchanged; every case is checked against filterThinkingBlocksDecoded. +func TestThinkingBlocksNeedFilteringMatchesDecodingPath(t *testing.T) { + dummy := antigravity.DummyThoughtSignature + bodies := []string{ + `{"thinking":{"type":"adaptive"},"messages":[{"role":"user","content":"hi"}]}`, + `{"thinking":{"type":"enabled"},"messages":[{"role":"assistant","content":[{"type":"thinking","thinking":"t","signature":"sig"},{"type":"text","text":"a"}]}]}`, + `{"thinking":{"type":"enabled"},"messages":[{"role":"assistant","content":[{"type":"thinking","thinking":"t","signature":""}]}]}`, + `{"thinking":{"type":"enabled"},"messages":[{"role":"assistant","content":[{"type":"thinking","thinking":"t","signature":"` + dummy + `"}]}]}`, + `{"thinking":{"type":"enabled"},"messages":[{"role":"assistant","content":[{"type":"thinking","thinking":"t","signature":7}]}]}`, + `{"thinking":{"type":"enabled"},"messages":[{"role":"user","content":[{"type":"thinking","thinking":"t","signature":"sig"}]}]}`, + `{"thinking":{"type":"disabled"},"messages":[{"role":"assistant","content":[{"type":"thinking","thinking":"t","signature":"sig"}]}]}`, + `{"messages":[{"role":"assistant","content":[{"type":"thinking","thinking":"t","signature":"sig"}]}]}`, + `{"thinking":{"type":"enabled"},"messages":[{"role":"assistant","content":[{"type":"redacted_thinking","data":"d"}]}]}`, + `{"thinking":{"type":"enabled"},"messages":[{"role":"assistant","content":[{"type":"redacted_thinking","data":""}]}]}`, + `{"thinking":{"type":"enabled"},"messages":[{"role":"assistant","content":[{"thinking":"untyped"}]}]}`, + `{"thinking":{"type":"enabled"},"messages":[{"role":"assistant","content":[{"type":7,"thinking":null}]}]}`, + `{"thinking":{"type":"enabled"},"messages":[{"role":"assistant","content":[{"type":"text","text":"\"thinking\": quoted"}]}]}`, + // Duplicate keys: encoding/json keeps the last value. + `{"thinking":{"type":"enabled"},"thinking":{"budget_tokens":1},"messages":[{"role":"assistant","content":[{"type":"thinking","thinking":"t","signature":"sig"}]}]}`, + `{"thinking":{"type":"disabled","type":"enabled"},"messages":[{"role":"assistant","content":[{"type":"thinking","thinking":"t","signature":"sig"}]}]}`, + `{"thinking":{"type":"enabled"},"messages":[{"role":"user","role":"assistant","content":[{"type":"thinking","thinking":"t","signature":"sig"}]}]}`, + `{"thinking":{"type":"enabled"},"messages":[{"role":"assistant","content":[{"type":"thinking","signature":"sig","signature":""}]}]}`, + `{"thinking":{"type":"enabled"},"messages":[{"role":"assistant","content":[{"type":"thinking","signature":"sig"}],"content":"plain"}]}`, + `{"thinking":{"type":"enabled"},"messages":[],"messages":[{"role":"assistant","content":[{"type":"thinking","signature":""}]}]}`, + // Escaped keys and values. + `{"thinking":{"\u0074ype":"enabled"},"messages":[{"role":"assistant","content":[{"\u0074ype":"thinking","thinking":"t","signature":"sig"}]}]}`, + `{"thinking":{"type":"enabled"},"messages":[{"role":"assistant","content":[{"type":"thinking","thinking":"t","\u0073ignature":""}]}]}`, + `{"thinking":{"type":"enabled"},"messages":[{"role":"\u0061ssistant","content":[{"type":"\u0074hinking","thinking":"t","signature":"\u0073ig"}]}]}`, + `{"thinking":{"type":"enabled"},"messages":[{"role":"assistant","content":[{"type":"thinking","thinking":"t","signature":"\u0000"}]}]}`, + // Shapes the decoder rejects or ignores. + `{"thinking":{"type":"enabled"},"messages":"none"}`, + `{"thinking":{"type":"enabled"},"messages":[null,1,{"role":"assistant","content":null},{"role":"assistant","content":[null,"x"]}]}`, + `[{"thinking":{}}]`, + `{"thinking":{"type":"enabled"},"messages":[`, + `{"thinking":{"type":"enabled"}} {}`, + } + for _, body := range bodies { + for _, alwaysThinking := range []bool{false, true} { + decoded := filterThinkingBlocksDecoded([]byte(body), alwaysThinking) + changed := !bytes.Equal(decoded, []byte(body)) + needs, decided := thinkingBlocksNeedFiltering([]byte(body), alwaysThinking) + if decided && json.Valid([]byte(body)) { + require.Equal(t, changed, needs, "alwaysThinking=%v body=%s", alwaysThinking, body) + } + require.Equal(t, string(decoded), string(filterThinkingBlocksInternal([]byte(body), alwaysThinking)), "alwaysThinking=%v body=%s", alwaysThinking, body) + } + } +} + +func signedThinkingHistoryBody() []byte { + var b strings.Builder + _, _ = b.WriteString(`{"model":"claude-sonnet-4-5","thinking":{"type":"adaptive"},"messages":[`) + for i := 0; i < 200; i++ { + if i > 0 { + _ = b.WriteByte(',') + } + _, _ = b.WriteString(`{"role":"user","content":[{"type":"text","text":"` + strings.Repeat("q", 2048) + `"}]},`) + _, _ = b.WriteString(`{"role":"assistant","content":[{"type":"thinking","thinking":"reason","signature":"sig-valid"},{"type":"text","text":"answer"}]}`) + } + _, _ = b.WriteString(`]}`) + return []byte(b.String()) +} + +func TestFilterThinkingBlocksSignedHistorySkipsDecode(t *testing.T) { + body := signedThinkingHistoryBody() + allocs := testing.AllocsPerRun(5, func() { + got := filterThinkingBlocksInternal(body, false) + if &got[0] != &body[0] { + t.Fatal("valid signed history must be returned unchanged") + } + }) + require.Less(t, allocs, float64(16), "allocs: %v", allocs) +} + +func FuzzThinkingBlocksNeedFiltering(f *testing.F) { + f.Add([]byte(`{"thinking":{"type":"enabled"},"messages":[{"role":"assistant","content":[{"type":"thinking","thinking":"t","signature":"sig"}]}]}`), false) + f.Add([]byte(`{"thinking":{"type":"adaptive"},"messages":[{"role":"assistant","content":[{"type":"redacted_thinking","data":"d"},{"thinking":1}]}]}`), true) + f.Add([]byte(`{"messages":[{"role":"user","role":"assistant","content":[{"type":"thinking","signature":"","signature":"s"}]}]}`), true) + f.Fuzz(func(t *testing.T, body []byte, alwaysThinking bool) { + original := append([]byte(nil), body...) + decoded := filterThinkingBlocksDecoded(original, alwaysThinking) + needs, decided := thinkingBlocksNeedFiltering(body, alwaysThinking) + if !bytes.Equal(filterThinkingBlocksInternal(body, alwaysThinking), decoded) { + t.Fatalf("filter output differs from decoding path for %q", body) + } + if decided && json.Valid(original) && needs == bytes.Equal(decoded, original) { + t.Fatalf("precheck needs=%v disagrees with decoding path for %q", needs, body) + } + }) +} + +// Decoded is the previous cost of a request whose signed history needs no +// filtering; Precheck is the current path for the same body. +func BenchmarkFilterThinkingBlocksSignedHistory(b *testing.B) { + body := signedThinkingHistoryBody() + b.Run("Decoded", func(b *testing.B) { + b.ReportAllocs() + b.SetBytes(int64(len(body))) + for i := 0; i < b.N; i++ { + _ = filterThinkingBlocksDecoded(body, false) + } + }) + b.Run("Precheck", func(b *testing.B) { + b.ReportAllocs() + b.SetBytes(int64(len(body))) + for i := 0; i < b.N; i++ { + _ = filterThinkingBlocksInternal(body, false) + } + }) +}