From 552d1ab4bb661f89df4ff267370bf5f578247322 Mon Sep 17 00:00:00 2001 From: r266-tech Date: Wed, 28 Oct 2026 03:54:26 +0800 Subject: [PATCH] fix(apicompat): skip foreign CC Switch reasoning envelopes --- .../pkg/apicompat/anthropic_responses_test.go | 45 +++++++++++++++++++ .../pkg/apicompat/anthropic_to_responses.go | 5 +++ 2 files changed, 50 insertions(+) diff --git a/backend/internal/pkg/apicompat/anthropic_responses_test.go b/backend/internal/pkg/apicompat/anthropic_responses_test.go index 700af63d4d..6755533b00 100644 --- a/backend/internal/pkg/apicompat/anthropic_responses_test.go +++ b/backend/internal/pkg/apicompat/anthropic_responses_test.go @@ -1,6 +1,7 @@ package apicompat import ( + "encoding/base64" "encoding/json" "testing" @@ -197,6 +198,50 @@ func TestAnthropicToResponses_ThinkingSignatureBecomesReasoning(t *testing.T) { assert.Equal(t, "function_call", items[3].Type) } +func TestAnthropicToResponses_ForeignReasoningEnvelopePreservesToolHistory(t *testing.T) { + envelope := "ccswitch-openai-reasoning-v1:" + base64.RawURLEncoding.EncodeToString([]byte( + `{"type":"reasoning","id":"rs_1","summary":[],"encrypted_content":"gAAAA-foreign"}`, + )) + for _, tc := range []struct{ name, signature string }{ + {"valid envelope", envelope}, + {"surrounding whitespace", " \n" + envelope + "\t"}, + {"malformed envelope", "ccswitch-openai-reasoning-v1:not-base64!"}, + } { + t.Run(tc.name, func(t *testing.T) { + blocks, err := json.Marshal([]AnthropicContentBlock{ + {Type: "thinking", Signature: tc.signature}, + {Type: "thinking", Signature: "native-ciphertext"}, + {Type: "text", Text: "Checking now."}, + {Type: "tool_use", ID: "call_1", Name: "lookup", Input: json.RawMessage(`{"key":"value"}`)}, + }) + require.NoError(t, err) + resp, err := AnthropicToResponses(&AnthropicRequest{ + Model: "gpt-6.1-sol", MaxTokens: 1024, + Messages: []AnthropicMessage{ + {Role: "user", Content: json.RawMessage(`"Check this"`)}, + {Role: "assistant", Content: blocks}, + {Role: "user", Content: json.RawMessage(`[{"type":"tool_result","tool_use_id":"call_1","content":"found"}]`)}, + }, + }) + require.NoError(t, err) + var items []ResponsesInputItem + require.NoError(t, json.Unmarshal(resp.Input, &items)) + require.Len(t, items, 5) + require.Equal(t, "reasoning", items[1].Type) + require.Equal(t, "native-ciphertext", items[1].EncryptedContent) + require.Equal(t, "assistant", items[2].Role) + require.JSONEq(t, `[{"type":"output_text","text":"Checking now."}]`, string(items[2].Content)) + require.Equal(t, "function_call", items[3].Type) + require.Equal(t, "call_1", items[3].CallID) + require.Equal(t, "lookup", items[3].Name) + require.JSONEq(t, `{"key":"value"}`, items[3].Arguments) + require.Equal(t, "function_call_output", items[4].Type) + require.Equal(t, "call_1", items[4].CallID) + require.Equal(t, "found", items[4].Output) + }) + } +} + func TestAnthropicToResponses_MaxTokensFloor(t *testing.T) { req := &AnthropicRequest{ Model: "gpt-5.2", diff --git a/backend/internal/pkg/apicompat/anthropic_to_responses.go b/backend/internal/pkg/apicompat/anthropic_to_responses.go index c27e445537..8987cbd6f1 100644 --- a/backend/internal/pkg/apicompat/anthropic_to_responses.go +++ b/backend/internal/pkg/apicompat/anthropic_to_responses.go @@ -302,6 +302,11 @@ func anthropicAssistantToResponses(raw json.RawMessage) ([]ResponsesInputItem, e if sig == "" || strings.HasPrefix(sig, "gAAAA") { continue } + // CC Switch carries a JSON reasoning item in this versioned envelope, + // not provider ciphertext. Ignore the foreign state when resuming directly. + if strings.HasPrefix(sig, "ccswitch-openai-reasoning-v1:") { + continue + } items = append(items, ResponsesInputItem{ Type: "reasoning", EncryptedContent: sig,