From 7a0b7563e9a2e0dd922bed8f32b3fa5c006d6bb3 Mon Sep 17 00:00:00 2001 From: sususu Date: Fri, 16 Oct 2026 01:03:22 +0800 Subject: [PATCH 01/27] feat(signature): recognize Meta Muse reasoning envelopes and gate replay by target - Add InspectMetaReasoningSignature transport-shape validation (canonical unpadded base64url, observed 43e3da0e01 header, 45 + 1024*2^k decoded buckets) and SignatureProviderMeta detection/compatibility wiring - Make Meta Responses sanitization native-only so GPT, foreign, and unknown signatures never replay to Meta, while compat paths preserve Meta envelopes only for Meta-family target models - Preserve recognized Meta signatures through Claude-protocol request translation for muse-* targets and drop them for other targets; never supplement them as Devin sealed signatures - Cover with synthetic-fixture unit tests only; verified same-model, cross-model, and Claude-ingress live replay plus wrong-upstream stripping on an isolated debug CPA --- .../executor/codex_executor_execute.go | 4 +- .../runtime/executor/codex_executor_stream.go | 2 +- .../executor/codex_websockets_stream.go | 2 +- internal/runtime/executor/devin_executor.go | 6 + .../runtime/executor/meta_executor_execute.go | 2 +- .../runtime/executor/meta_executor_test.go | 8 +- .../executor/openai_responses_signature.go | 53 +++++- .../openai_responses_signature_test.go | 54 ++++++ internal/signature/meta_validation.go | 112 ++++++++++++ internal/signature/meta_validation_test.go | 164 ++++++++++++++++++ internal/signature/provider_compatibility.go | 30 +++- .../signature/provider_compatibility_test.go | 2 + .../codex/claude/codex_claude_request.go | 14 ++ .../codex/claude/codex_claude_request_test.go | 39 +++++ 14 files changed, 473 insertions(+), 19 deletions(-) create mode 100644 internal/signature/meta_validation.go create mode 100644 internal/signature/meta_validation_test.go diff --git a/internal/runtime/executor/codex_executor_execute.go b/internal/runtime/executor/codex_executor_execute.go index 86c6752ed59..a760b3607c6 100644 --- a/internal/runtime/executor/codex_executor_execute.go +++ b/internal/runtime/executor/codex_executor_execute.go @@ -76,7 +76,7 @@ func (e *CodexExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, re } else { body = applyCodexSIWCRequestBodyRules(body, hasExplicitInstructions, cliproxyexecutor.Options{KeepTopLevelEffort: thinking.HasThinkingSuffix(req.Model)}) } - body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompat(ctx, "codex executor", body, isCompat) + body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(ctx, "codex executor", body, isCompat, baseModel) body = normalizeCodexParallelToolCalls(body, opts.Headers) body = helps.NormalizeCodexToolSchemas(body) body, optimizeMultiAgentV2 := helps.OptimizeCodexMultiAgentV2RequestForAuth(ctx, opts.Headers, body, e.cfg, auth, isCompat) @@ -261,7 +261,7 @@ func (e *CodexExecutor) executeCompact(ctx context.Context, auth *cliproxyauth.A hasExplicitInstructions := gjson.GetBytes(req.Payload, "instructions").Exists() && strings.TrimSpace(gjson.GetBytes(req.Payload, "instructions").String()) != "" body = applyCodexSIWCCompactBodyRules(body, hasExplicitInstructions, cliproxyexecutor.Options{KeepTopLevelEffort: thinking.HasThinkingSuffix(req.Model)}) } - body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompat(ctx, "codex executor", body, isCompat) + body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(ctx, "codex executor", body, isCompat, baseModel) body = normalizeCodexParallelToolCalls(body, opts.Headers) body = helps.NormalizeCodexToolSchemas(body) body, optimizeMultiAgentV2 := helps.OptimizeCodexMultiAgentV2RequestForAuth(ctx, opts.Headers, body, e.cfg, auth, isCompat) diff --git a/internal/runtime/executor/codex_executor_stream.go b/internal/runtime/executor/codex_executor_stream.go index 03e5ac80a19..b78a480cf28 100644 --- a/internal/runtime/executor/codex_executor_stream.go +++ b/internal/runtime/executor/codex_executor_stream.go @@ -83,7 +83,7 @@ func (e *CodexExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au } else { body = applyCodexSIWCRequestBodyRules(body, hasExplicitInstructions, cliproxyexecutor.Options{KeepTopLevelEffort: thinking.HasThinkingSuffix(req.Model)}) } - body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompat(ctx, "codex executor", body, isCompat) + body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(ctx, "codex executor", body, isCompat, baseModel) body = normalizeCodexParallelToolCalls(body, opts.Headers) body = helps.NormalizeCodexToolSchemas(body) body, optimizeMultiAgentV2 := helps.OptimizeCodexMultiAgentV2RequestForAuth(ctx, opts.Headers, body, e.cfg, auth, isCompat) diff --git a/internal/runtime/executor/codex_websockets_stream.go b/internal/runtime/executor/codex_websockets_stream.go index ce199297414..82bfc3bba36 100644 --- a/internal/runtime/executor/codex_websockets_stream.go +++ b/internal/runtime/executor/codex_websockets_stream.go @@ -803,7 +803,7 @@ func (e *CodexWebsocketsExecutor) prepareCodexWebsocketStream(ctx context.Contex body = ensureImageGenerationTool(body, baseModel, auth, opts.Headers) } } - body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompat(ctx, "codex websockets executor", body, isCompat) + body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(ctx, "codex websockets executor", body, isCompat, baseModel) body = normalizeCodexWebsocketParallelToolCalls(body, opts.Headers) body = helps.NormalizeCodexToolSchemas(body) multiAgentV2Conflict := helps.HasCodexMultiAgentV2NamespaceConflict(body) diff --git a/internal/runtime/executor/devin_executor.go b/internal/runtime/executor/devin_executor.go index 305c705932d..9cb77d8dbab 100644 --- a/internal/runtime/executor/devin_executor.go +++ b/internal/runtime/executor/devin_executor.go @@ -2380,6 +2380,10 @@ func parseSignatureBytes(sigStr string) ([]byte, string) { return []byte(s), "openai" case internalsignature.SignatureProviderGemini: return []byte(s), "gemini" + case internalsignature.SignatureProviderMeta: + // Positively foreign to the Devin backend: never supplement it as a + // sealed signature. + return nil, "" } if strings.HasPrefix(s, "AY") { return []byte(s), "gemini" @@ -2396,6 +2400,8 @@ func parseSignatureBytes(sigStr string) ([]byte, string) { return decoded, "openai" case internalsignature.SignatureProviderGemini: return []byte(s), "gemini" + case internalsignature.SignatureProviderMeta: + return nil, "" } if strings.HasPrefix(decStr, "CAQS") || strings.HasPrefix(decStr, "CAIS") { return decoded, "anthropic" diff --git a/internal/runtime/executor/meta_executor_execute.go b/internal/runtime/executor/meta_executor_execute.go index bbc7cd6b066..67ae5d6a323 100644 --- a/internal/runtime/executor/meta_executor_execute.go +++ b/internal/runtime/executor/meta_executor_execute.go @@ -71,7 +71,7 @@ func (e *MetaExecutor) prepareResponsesRequest(ctx context.Context, req cliproxy return nil, errNormalizePatch } body = normalizeCodexInstructions(body) - body = sanitizeOpenAIResponsesReasoningEncryptedContentKeepForeign(ctx, "meta executor", body) + body = sanitizeOpenAIResponsesReasoningEncryptedContentForMeta(ctx, "meta executor", body) body = helps.SanitizeMetaWebSearchTools(body) body = helps.NormalizeCodexToolIntegerTypes(body, opts.Headers) diff --git a/internal/runtime/executor/meta_executor_test.go b/internal/runtime/executor/meta_executor_test.go index 35e5744aff2..4e58919d845 100644 --- a/internal/runtime/executor/meta_executor_test.go +++ b/internal/runtime/executor/meta_executor_test.go @@ -1609,7 +1609,13 @@ func TestMetaExecutor_Execute_PreservesMuseReasoningEncryptedContent_Issue6450(t }, } - const museEncryptedContent = "Q-PaDg-mock-muse-spark-encrypted-reasoning-payload" + // Shape-valid synthetic Meta envelope (observed header + 1 KiB bucket), + // so this exercises the positively recognized Meta branch end to end + // rather than the unknown-format fallback. No captured value is embedded. + museEncryptedContent := "Q-PaDgE" + for len(museEncryptedContent) < 1426 { + museEncryptedContent += "A" + } reqPayload := []byte(`{"model":"muse-spark-1.3","store":false,"input":[` + `{"id":"rs_1","type":"reasoning","encrypted_content":"` + museEncryptedContent + `","content":[{"type":"reasoning_text","text":"thinking step"}],"summary":[]},` + `{"id":"rs_claude","type":"reasoning","encrypted_content":"` + testClaudeCAISSample + `","summary":[]},` + diff --git a/internal/runtime/executor/openai_responses_signature.go b/internal/runtime/executor/openai_responses_signature.go index e6e03d99d39..9e1e5fa46f2 100644 --- a/internal/runtime/executor/openai_responses_signature.go +++ b/internal/runtime/executor/openai_responses_signature.go @@ -49,18 +49,41 @@ func promoteOpenAIResponsesReasoningTextToSummary(itemRaw string, content gjson. } func sanitizeOpenAIResponsesReasoningEncryptedContent(ctx context.Context, provider string, body []byte) []byte { - return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, false) + return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, false, false) } func sanitizeOpenAIResponsesReasoningEncryptedContentWithCompat(ctx context.Context, provider string, body []byte, isCompat bool) []byte { - return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, isCompat, isCompat) + return sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(ctx, provider, body, isCompat, "") +} + +// sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel is the +// compat variant that knows its target model: a positively recognized Meta +// Muse envelope is preserved only when the compat upstream actually serves +// a Meta-family model (native Meta credentials route to the Meta executor, +// but a cascaded or third-party compat endpoint can serve muse-* models +// through the Codex executor), and then under the same native-only rule as +// the Meta executor. For any other compat target the recognized Meta +// envelope is known-foreign and stripped like other providers'. +func sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(ctx context.Context, provider string, body []byte, isCompat bool, targetModel string) []byte { + keepMeta := isCompat && signature.SignatureProviderFromModelName(targetModel) == signature.SignatureProviderMeta + return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, isCompat, isCompat, keepMeta) } func sanitizeOpenAIResponsesReasoningEncryptedContentKeepForeign(ctx context.Context, provider string, body []byte) []byte { - return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, true) + return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, true, false) +} + +// sanitizeOpenAIResponsesReasoningEncryptedContentForMeta is the Meta-target +// variant and is deliberately native-only: only positively recognized Meta +// Muse envelopes may replay to Meta. GPT, other known-provider, and +// unknown-format values are all stripped, so a wrong signature can never +// reach the Meta upstream through this path. A future Meta envelope revision +// will be stripped until the inspector learns it (fail-closed). +func sanitizeOpenAIResponsesReasoningEncryptedContentForMeta(ctx context.Context, provider string, body []byte) []byte { + return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, true, true) } -func sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx context.Context, provider string, body []byte, isCompat bool, keepForeign bool) []byte { +func sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx context.Context, provider string, body []byte, isCompat bool, keepForeign bool, keepMeta bool) []byte { inputResult := util.GetGJSONBytesNoCopy(body, "input") if !inputResult.Exists() || !inputResult.IsArray() { return body @@ -175,11 +198,25 @@ func sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx context.Con rawSignature := encryptedContent.String() if rawSignature != strings.TrimSpace(rawSignature) { reason = "encrypted_content has leading or trailing whitespace" + } else if keepMeta { + // Meta targets are native-only and never reach the GPT-first gate: + // a GPT, foreign-provider, or unknown-format blob must not replay + // to Meta. Only a positively recognized Meta envelope survives. + if _, err := signature.InspectMetaReasoningSignature(rawSignature); err != nil { + reason = err.Error() + } } else if _, err := signature.InspectGPTReasoningSignature(rawSignature); err != nil { - // When keepForeign is true (compat callers enable it via WithCompat), - // third-party Responses models (such as Muse) expect their own - // unknown-format encrypted_content to be replayed. - if !keepForeign || rawSignature == "" || signature.DetectSignatureProvider(rawSignature) != signature.SignatureProviderUnknown { + // Valid GPT signatures never reach this branch: the GPT-first gate + // above preserves them on non-Meta targets (inherited behavior). + // keepForeign additionally preserves unknown-format blobs a + // third-party Responses target may natively own. Classification + // is skipped entirely when no exemption applies, so strict + // targets pay no detection cost. + keep := false + if rawSignature != "" && keepForeign { + keep = signature.DetectSignatureProvider(rawSignature) == signature.SignatureProviderUnknown + } + if !keep { reason = err.Error() } } diff --git a/internal/runtime/executor/openai_responses_signature_test.go b/internal/runtime/executor/openai_responses_signature_test.go index d27a3056d3b..42953677ba9 100644 --- a/internal/runtime/executor/openai_responses_signature_test.go +++ b/internal/runtime/executor/openai_responses_signature_test.go @@ -260,6 +260,60 @@ func TestSanitizeOpenAIResponsesReasoningEncryptedContentKeepForeign_Issue6450(t } } +func TestSanitizeOpenAIResponsesReasoningEncryptedContentForMeta_PreservesRecognizedMuse(t *testing.T) { + // Synthetic shape-valid Meta envelope: "Q-PaDgE" pins the observed + // five-byte header, and 1426 unpadded base64url chars decode to + // 45 + 1024 bytes. No captured signature is embedded in tests. + metaSig := "Q-PaDgE" + strings.Repeat("A", 1419) + const unknownSig = "Q-PaDg-mock-muse-spark-encrypted-reasoning-payload" + body := []byte(`{"store":false,"input":[` + + `{"id":"rs_meta","type":"reasoning","summary":[],"content":null,"encrypted_content":"` + metaSig + `"},` + + `{"id":"rs_unknown","type":"reasoning","summary":[],"content":null,"encrypted_content":"` + unknownSig + `"},` + + `{"id":"rs_claude","type":"reasoning","summary":[],"content":null,"encrypted_content":"` + testClaudeCAISSample + `"} ` + + `]}`) + + got := sanitizeOpenAIResponsesReasoningEncryptedContentForMeta(context.Background(), "test", body) + + // The Meta target is native-only: the recognized Meta envelope survives, + // while unknown-format and known-foreign values are both stripped so no + // wrong signature can reach the Meta upstream. + if gotSig := gjson.GetBytes(got, "input.0.encrypted_content").String(); gotSig != metaSig { + t.Fatalf("recognized Meta encrypted_content not preserved; body=%s", got) + } + if gjson.GetBytes(got, "input.1.encrypted_content").Exists() { + t.Fatalf("unknown-format encrypted_content should be stripped on Meta target: %s", got) + } + if gjson.GetBytes(got, "input.2.encrypted_content").Exists() { + t.Fatalf("Claude signature should be stripped on Meta target: %s", got) + } + + // The generic keepForeign path has no Meta allowance: once Muse is a + // positively recognized family, its envelope is known-foreign there and + // must be stripped instead of riding the unknown-format exemption. + generic := sanitizeOpenAIResponsesReasoningEncryptedContentKeepForeign(context.Background(), "test", body) + if gjson.GetBytes(generic, "input.0.encrypted_content").Exists() { + t.Fatalf("recognized Meta signature should be stripped by generic keepForeign: %s", generic) + } + if gotSig := gjson.GetBytes(generic, "input.1.encrypted_content").String(); gotSig != unknownSig { + t.Fatalf("unknown-format encrypted_content should remain preserved by generic keepForeign; body=%s", generic) + } + + // The compat path preserves a recognized Meta envelope only when its + // target model is itself a Meta-family model (cascaded/third-party + // compat endpoints serving muse-*); other compat targets strip it. + compatMeta := sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(context.Background(), "test", body, true, "muse-spark-1.3") + if gotSig := gjson.GetBytes(compatMeta, "input.0.encrypted_content").String(); gotSig != metaSig { + t.Fatalf("compat Meta-model target should preserve recognized Meta signature; body=%s", compatMeta) + } + if gjson.GetBytes(compatMeta, "input.1.encrypted_content").Exists() { + t.Fatalf("compat Meta-model target should strip unknown-format signature: %s", compatMeta) + } + compatOther := sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(context.Background(), "test", body, true, "deepseek-v4") + if gjson.GetBytes(compatOther, "input.0.encrypted_content").Exists() { + t.Fatalf("compat non-Meta target should strip recognized Meta signature: %s", compatOther) + } +} + func BenchmarkSanitizeOpenAIResponsesReasoningEncryptedContentLargeNoopPayload(b *testing.B) { body := []byte(`{"store":false,"input":[{"type":"message","role":"user","content":"` + strings.Repeat("x", 8<<20) + `"}]}`) b.ReportAllocs() diff --git a/internal/signature/meta_validation.go b/internal/signature/meta_validation.go new file mode 100644 index 00000000000..7f611ddc245 --- /dev/null +++ b/internal/signature/meta_validation.go @@ -0,0 +1,112 @@ +package signature + +import ( + "bytes" + "encoding/base64" + "fmt" + "strings" + "unicode/utf8" +) + +// MaxMetaReasoningSignatureLen bounds the raw base64url text accepted by +// InspectMetaReasoningSignature. It mirrors the GPT bound; the bucket-length +// rule below is far more restrictive in practice. +const MaxMetaReasoningSignatureLen = 32 * 1024 * 1024 + +const ( + metaReasoningSignatureOverheadLen = 45 + metaReasoningSignatureMinPayload = 1024 +) + +// metaReasoningSignatureHeader is the fixed five-byte prefix observed on every +// captured Meta Muse reasoning encrypted_content value (api.meta.ai, +// 2026-10-15 corpus: 82 values across nine muse-* models). Its semantics are +// unresolved - it may be a format magic, a key/context fingerprint, or a +// versioned identifier ending in 0x01 - and every capture came from one +// account on one day, so a future rotation would fall back to Unknown rather +// than being claimed here on weaker evidence. Cache-prefixed forms +// ("meta#...") are intentionally not recognized: no producer emits them, so +// there is no alias in SignatureProviderFromCachePrefix. +var metaReasoningSignatureHeader = []byte{0x43, 0xe3, 0xda, 0x0e, 0x01} + +// MetaReasoningSignatureInfo describes the transport shape of a Meta Muse +// reasoning encrypted_content envelope. +type MetaReasoningSignatureInfo struct { + DecodedLen int + // PayloadLen is DecodedLen minus the fixed 45-byte envelope overhead. It + // is the power-of-two padded region, not a proven plaintext length. + PayloadLen int + BucketExponent int +} + +func IsValidMetaReasoningSignature(rawSignature string) bool { + _, err := InspectMetaReasoningSignature(rawSignature) + return err == nil +} + +// InspectMetaReasoningSignature validates the outer envelope shape observed +// for Meta Muse reasoning encrypted_content: canonical unpadded base64url, +// the fixed five-byte header above, and a decoded length of exactly +// 45 + 1024*2^k bytes. This is only a transport-shape check; it does not +// prove decryptability or upstream replay acceptance. +func InspectMetaReasoningSignature(rawSignature string) (*MetaReasoningSignatureInfo, error) { + sig := strings.TrimSpace(rawSignature) + if sig == "" { + return nil, fmt.Errorf("empty Meta reasoning signature") + } + if len(sig) > MaxMetaReasoningSignatureLen { + return nil, fmt.Errorf("Meta reasoning signature exceeds maximum length (%d bytes)", MaxMetaReasoningSignatureLen) + } + // The literal prefix is the cheapest discriminator: every observed value + // starts with "Q-PaDg", and probing this validator is on the hot path for + // signatures of every provider. + if !strings.HasPrefix(sig, "Q-PaDg") { + return nil, fmt.Errorf("invalid Meta reasoning signature: expected Q-PaDg prefix") + } + if index, r, ok := firstInvalidMetaReasoningSignatureChar(sig); ok { + return nil, fmt.Errorf("invalid Meta reasoning signature: contains non-base64url character U+%04X at byte %d", r, index) + } + if len(sig)%4 == 1 { + return nil, fmt.Errorf("invalid Meta reasoning signature: base64url length %d cannot decode", len(sig)) + } + // Strict decoding rejects non-canonical trailing bits, so no re-encode + // comparison is needed to prove canonical form. + decoded, err := base64.RawURLEncoding.Strict().DecodeString(sig) + if err != nil { + return nil, fmt.Errorf("invalid Meta reasoning signature: base64url decode failed") + } + if len(decoded) < metaReasoningSignatureOverheadLen+metaReasoningSignatureMinPayload { + return nil, fmt.Errorf("invalid Meta reasoning signature: decoded payload too short") + } + if !bytes.Equal(decoded[:len(metaReasoningSignatureHeader)], metaReasoningSignatureHeader) { + return nil, fmt.Errorf("invalid Meta reasoning signature: unexpected envelope header %x", decoded[:len(metaReasoningSignatureHeader)]) + } + payloadLen := len(decoded) - metaReasoningSignatureOverheadLen + if payloadLen&(payloadLen-1) != 0 { + return nil, fmt.Errorf("invalid Meta reasoning signature: payload length %d is not a power-of-two bucket", payloadLen) + } + exponent := 0 + for p := payloadLen; p > metaReasoningSignatureMinPayload; p >>= 1 { + exponent++ + } + return &MetaReasoningSignatureInfo{ + DecodedLen: len(decoded), + PayloadLen: payloadLen, + BucketExponent: exponent, + }, nil +} + +// metaReasoningSignatureCharSet is the unpadded base64url alphabet. Observed +// Meta values never carry '=' padding, so padding is rejected rather than +// normalized away. +var metaReasoningSignatureCharSet = base64AlphabetSet("-_") + +func firstInvalidMetaReasoningSignatureChar(sig string) (int, rune, bool) { + for index := 0; index < len(sig); index++ { + if !metaReasoningSignatureCharSet[sig[index]] { + r, _ := utf8.DecodeRuneInString(sig[index:]) + return index, r, true + } + } + return 0, 0, false +} diff --git a/internal/signature/meta_validation_test.go b/internal/signature/meta_validation_test.go new file mode 100644 index 00000000000..8b7ff2084be --- /dev/null +++ b/internal/signature/meta_validation_test.go @@ -0,0 +1,164 @@ +package signature + +import ( + "encoding/base64" + "strings" + "testing" +) + +// testMetaReasoningSignature builds a synthetic Meta Muse envelope with the +// observed fixed header and a power-of-two payload bucket. The body bytes are +// deterministic filler; recognition is a transport-shape check, so no real +// captured value is needed (or permitted) here. +func testMetaReasoningSignature(payloadLen int) string { + return base64.RawURLEncoding.EncodeToString(testMetaReasoningSignatureBytes(payloadLen, nil)) +} + +func testMetaReasoningSignatureBytes(payloadLen int, mutate func([]byte)) []byte { + decoded := make([]byte, metaReasoningSignatureOverheadLen+payloadLen) + copy(decoded, metaReasoningSignatureHeader) + for i := len(metaReasoningSignatureHeader); i < len(decoded); i++ { + decoded[i] = byte(i*31 + 7) + } + if mutate != nil { + mutate(decoded) + } + return decoded +} + +func TestInspectMetaReasoningSignature_ValidBuckets(t *testing.T) { + for _, tc := range []struct { + payload int + exponent int + }{ + {1024, 0}, + {2048, 1}, + {4096, 2}, + {32768, 5}, + } { + sig := testMetaReasoningSignature(tc.payload) + info, err := InspectMetaReasoningSignature(sig) + if err != nil { + t.Fatalf("payload %d: InspectMetaReasoningSignature error = %v", tc.payload, err) + } + if info.DecodedLen != tc.payload+metaReasoningSignatureOverheadLen || info.PayloadLen != tc.payload || info.BucketExponent != tc.exponent { + t.Fatalf("payload %d: info = %+v", tc.payload, info) + } + if !IsValidMetaReasoningSignature(sig) { + t.Fatalf("payload %d: IsValidMetaReasoningSignature = false", tc.payload) + } + if got := DetectSignatureProvider(sig); got != SignatureProviderMeta { + t.Fatalf("payload %d: DetectSignatureProvider = %q, want %q", tc.payload, got, SignatureProviderMeta) + } + } +} + +func TestInspectMetaReasoningSignature_RejectsMalformed(t *testing.T) { + valid := testMetaReasoningSignature(1024) + encode := func(payloadLen int, mutate func([]byte)) string { + return base64.RawURLEncoding.EncodeToString(testMetaReasoningSignatureBytes(payloadLen, mutate)) + } + nonCanonical := func() string { + const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" + last := strings.IndexByte(alphabet, valid[len(valid)-1]) + for i := 0; i < len(alphabet); i++ { + if i != last && i&0x30 == last&0x30 { + return valid[:len(valid)-1] + string(alphabet[i]) + } + } + return valid + }() + cases := map[string]string{ + "empty": "", + "mock prefix only": "Q-PaDg-mock-muse-spark-encrypted-reasoning-payload", + "wrong literal start": "R" + valid[1:], + "header byte flipped": encode(1024, func(b []byte) { b[2] ^= 0xff }), + "version byte zero": encode(1024, func(b []byte) { b[4] = 0x00 }), + "payload below min": encode(512, nil), + "payload not power2": encode(1025, nil), + "payload off by one": encode(2047, nil), + "padded encoding": valid + "=", + "standard base64 +": valid[:100] + "+" + valid[101:], + "embedded whitespace": valid[:100] + " " + valid[100:], + "length mod4 is 1": valid[:len(valid)-1], + "non-canonical bits": nonCanonical, + } + for name, sig := range cases { + if _, err := InspectMetaReasoningSignature(sig); err == nil { + t.Errorf("%s: InspectMetaReasoningSignature unexpectedly succeeded", name) + } + if got := DetectSignatureProvider(sig); got == SignatureProviderMeta { + t.Errorf("%s: DetectSignatureProvider = %q, must not claim Meta", name, got) + } + } +} + +func TestMetaSignatureCompatibility(t *testing.T) { + sig := testMetaReasoningSignature(2048) + + decision := DecideSignatureCompatibility(SignatureProviderMeta, sig, SignatureBlockKindUnknown) + if !decision.Compatible || decision.Action != SignatureActionPreserve || decision.NormalizedSignature != sig { + t.Fatalf("Meta target decision = %+v, want preserve exact signature", decision) + } + if got, ok := CompatibleSignatureForProvider(SignatureProviderMeta, sig); !ok || got != sig { + t.Fatalf("CompatibleSignatureForProvider(Meta) = (%q..., %v), want exact signature", got[:16], ok) + } + + gptDecision := DecideSignatureCompatibility(SignatureProviderGPT, sig, SignatureBlockKindUnknown) + if gptDecision.Compatible || gptDecision.Action != SignatureActionDropBlock { + t.Fatalf("GPT target decision = %+v, want drop_block", gptDecision) + } + + foreign := testGPTReasoningSignature() + metaDecision := DecideSignatureCompatibility(SignatureProviderMeta, foreign, SignatureBlockKindUnknown) + if metaDecision.Compatible || metaDecision.DetectedProvider != SignatureProviderGPT || metaDecision.Action != SignatureActionDropBlock { + t.Fatalf("Meta target with GPT signature decision = %+v, want detected GPT + drop_block", metaDecision) + } +} + +func TestSignatureProviderFromModelName_Meta(t *testing.T) { + for _, model := range []string{"muse-wonder-full-1.0-contributor", "muse-spark-1.3", "Muse-Wonder-1.0", "meta/muse-spark-1.2"} { + if got := SignatureProviderFromModelName(model); got != SignatureProviderMeta { + t.Errorf("SignatureProviderFromModelName(%q) = %q, want %q", model, got, SignatureProviderMeta) + } + } + // The match is anchored to the "muse-" model-id boundary so unrelated + // names containing the substring "muse" stay unmapped. + for _, model := range []string{"museum-1", "amuse-model", "meta-llama-4"} { + if got := SignatureProviderFromModelName(model); got != SignatureProviderUnknown { + t.Errorf("SignatureProviderFromModelName(%q) = %q, want %q", model, got, SignatureProviderUnknown) + } + } +} + +func TestMetaSignatureCompatibility_OtherTargets(t *testing.T) { + sig := testMetaReasoningSignature(1024) + for _, tc := range []struct { + target SignatureProvider + action SignatureCompatibilityAction + }{ + {SignatureProviderClaude, SignatureActionDropBlock}, + {SignatureProviderGemini, SignatureActionReplaceWithGeminiBypass}, + {SignatureProviderKimi, SignatureActionDropSignature}, + {SignatureProviderGrok, SignatureActionDropBlock}, + } { + decision := DecideSignatureCompatibility(tc.target, sig, SignatureBlockKindUnknown) + if decision.Compatible || decision.DetectedProvider != SignatureProviderMeta || decision.Action != tc.action { + t.Errorf("target %q: decision = %+v, want detected Meta + action %q", tc.target, decision, tc.action) + } + } +} + +func TestMetaCachePrefixUnsupported(t *testing.T) { + // No producer emits provider-prefixed Meta signatures, so unlike GPT or + // Claude there is deliberately no cache-prefix alias for Meta yet. + sig := testMetaReasoningSignature(1024) + for _, prefixed := range []string{"meta#" + sig, "muse#" + sig} { + if got := DetectSignatureProvider(prefixed); got != SignatureProviderUnknown { + t.Errorf("DetectSignatureProvider(%q...) = %q, want %q", prefixed[:12], got, SignatureProviderUnknown) + } + if _, ok := CompatibleSignatureForProvider(SignatureProviderMeta, prefixed); ok { + t.Errorf("CompatibleSignatureForProvider(Meta, %q...) unexpectedly compatible", prefixed[:12]) + } + } +} diff --git a/internal/signature/provider_compatibility.go b/internal/signature/provider_compatibility.go index f28d86ae272..d31021d2073 100644 --- a/internal/signature/provider_compatibility.go +++ b/internal/signature/provider_compatibility.go @@ -22,6 +22,10 @@ const ( SignatureProviderGrok SignatureProvider = "grok" // SignatureProviderSWE represents Cognition's SWE model family emitting sealed.v1 envelopes. SignatureProviderSWE SignatureProvider = "swe" + // SignatureProviderMeta is identified by the observed Meta Muse envelope: + // a fixed five-byte header plus a power-of-two padded payload length. See + // meta_validation.go for the capture basis and its limits. + SignatureProviderMeta SignatureProvider = "meta" ) type SignatureBlockKind string @@ -78,6 +82,8 @@ func SignatureProviderFromModelName(modelName string) SignatureProvider { return SignatureProviderKimi case strings.Contains(lower, "grok"): return SignatureProviderGrok + case strings.HasPrefix(lower, "muse-") || strings.Contains(lower, "/muse-"): + return SignatureProviderMeta case strings.Contains(lower, "swe-"): return SignatureProviderSWE default: @@ -92,7 +98,7 @@ func SignatureProviderFromModelName(modelName string) SignatureProvider { // // 'C' -> 0x08..0x0b : Claude CAIS (0x08) // 'E' -> 0x10..0x13 : Claude single-layer (0x12), Gemini protobuf_field_2 (0x12) -// 'Q' -> 0x40..0x43 : Antigravity double-layer CAQS (0x43, inner 'C') +// 'Q' -> 0x40..0x43 : Antigravity double-layer CAQS (0x43, inner 'C'), Meta Muse (0x43) // 'R' -> 0x44..0x47 : Claude double-layer R (0x45, inner 'E') // 'g' -> 0x80..0x83 : GPT Fernet reasoning (0x80) // @@ -207,9 +213,11 @@ func DetectSignatureProviderForBlock(rawSignature string, blockKind SignatureBlo // Probes run from the strongest marker to the weakest: // 1. GPT carries the literal "gAAAA" prefix, which pins both the version // byte and the high timestamp bytes. - // 2. Claude CAIS/CAQS carries marker 0x08 with nested container/channel structure. - // 3. Claude single/double-layer carries marker 0x12 plus the same literal. - // 4. Gemini validates wire shape only and has no literal to anchor on, so + // 2. Meta Muse carries a fixed five-byte decoded header plus an exact + // power-of-two padded length, which no other family can collide with. + // 3. Claude CAIS/CAQS carries marker 0x08 with nested container/channel structure. + // 4. Claude single/double-layer carries marker 0x12 plus the same literal. + // 5. Gemini validates wire shape only and has no literal to anchor on, so // it is the weakest judge and goes last. // // This ordering is defense in depth rather than a correctness requirement: @@ -228,6 +236,9 @@ func DetectSignatureProviderForBlock(rawSignature string, blockKind SignatureBlo if IsValidGPTReasoningSignature(sig) { return SignatureProviderGPT } + if IsValidMetaReasoningSignature(sig) { + return SignatureProviderMeta + } if IsValidClaudeCAISSignature(sig) { return SignatureProviderClaude } @@ -316,6 +327,9 @@ func DecideSignatureCompatibilityForModel(targetProvider SignatureProvider, targ case SignatureProviderSWE: decision.Action = SignatureActionDropBlock decision.Reason = "SWE requires sealed.v1 signature from its own backend" + case SignatureProviderMeta: + decision.Action = SignatureActionDropBlock + decision.Reason = "Meta Muse requires its native encrypted_content envelope and has no cross-provider bypass sentinel" case SignatureProviderKimi: // Kimi is the only target that can keep the reasoning text when the // signature does not match. Its Messages endpoint never reads the field @@ -460,6 +474,8 @@ func signatureProviderMatchesTarget(target, detected SignatureProvider) bool { return detected == SignatureProviderGPT case SignatureProviderSWE: return detected == SignatureProviderSWE + case SignatureProviderMeta: + return detected == SignatureProviderMeta case SignatureProviderKimi: return detected == SignatureProviderKimi default: @@ -497,6 +513,10 @@ func normalizeCompatibleSignatureForProvider(targetProvider SignatureProvider, r if strings.HasPrefix(payload, "sealed.v1.") { return payload } + case SignatureProviderMeta: + if IsValidMetaReasoningSignature(payload) { + return payload + } case SignatureProviderKimi: if IsValidKimiThinkingSignature(payload) { return payload @@ -517,7 +537,7 @@ func isRecognizedGeminiProviderSignature(rawSignature string, blockKind Signatur // IsRecognizedReasoningSignature reports whether rawSignature is a structurally valid // reasoning signature or encrypted_content payload from any known provider -// (GPT, Claude, Gemini, Kimi, Grok, Devin). +// (GPT, Claude, Gemini, Kimi, Grok, Devin, Meta). func IsRecognizedReasoningSignature(rawSignature string) bool { sig := strings.TrimSpace(rawSignature) if sig == "" { diff --git a/internal/signature/provider_compatibility_test.go b/internal/signature/provider_compatibility_test.go index 1e5d4c52e53..d66745b3391 100644 --- a/internal/signature/provider_compatibility_test.go +++ b/internal/signature/provider_compatibility_test.go @@ -41,6 +41,7 @@ func TestBase64AlphabetSet_MatchesEncoderAlphabets(t *testing.T) { }{ {"grok unpadded std", grokEncryptedContentCharSet, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"}, {"gpt base64url", gptReasoningSignatureCharSet, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_="}, + {"meta base64url unpadded", metaReasoningSignatureCharSet, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"}, } for _, tc := range cases { allowed := map[byte]bool{} @@ -73,6 +74,7 @@ func replaySafeEnvelopeFixtures(t *testing.T) map[string]struct { "antigravity CAQS": {antigravityCAQSFixture(t), SignatureProviderClaude}, "gemini protobuf field2": {testGeminiThoughtSignatureEnvelope(), SignatureProviderGemini}, "gpt fernet": {testGPTReasoningSignature(), SignatureProviderGPT}, + "meta muse": {testMetaReasoningSignature(1024), SignatureProviderMeta}, } } diff --git a/internal/translator/codex/claude/codex_claude_request.go b/internal/translator/codex/claude/codex_claude_request.go index 6585a6cd4b6..58da492c084 100644 --- a/internal/translator/codex/claude/codex_claude_request.go +++ b/internal/translator/codex/claude/codex_claude_request.go @@ -181,6 +181,10 @@ func convertClaudeRequestToCodex(modelName string, inputRawJSON []byte, _ bool, signature = rawSignature ok = true } + if !ok && codexClaudeTargetAcceptsMetaSignature(modelName) && sigcompat.IsValidMetaReasoningSignature(rawSignature) { + signature = rawSignature + ok = true + } if !ok { if preserveEmptyThinkingBlocks && strings.TrimSpace(rawSignature) == "" { signature = rawSignature @@ -464,6 +468,16 @@ func convertClaudeRequestToCodex(modelName string, inputRawJSON []byte, _ bool, return template, drops.Err() } +// codexClaudeTargetAcceptsMetaSignature reports whether the target model is +// a Meta Muse model, in which case a positively recognized native Meta +// envelope carried in a Claude thinking signature must survive translation +// into a Responses reasoning item so it can replay to Meta. For every other +// target the same blob is foreign and dropped by the caller. +func codexClaudeTargetAcceptsMetaSignature(modelName string) bool { + baseModel := thinking.ParseSuffix(modelName).ModelName + return sigcompat.SignatureProviderFromModelName(baseModel) == sigcompat.SignatureProviderMeta +} + func codexClaudeTargetAcceptsGrokSignature(modelName string) bool { baseModel := strings.ToLower(strings.TrimSpace(thinking.ParseSuffix(modelName).ModelName)) return strings.Contains(baseModel, "grok") diff --git a/internal/translator/codex/claude/codex_claude_request_test.go b/internal/translator/codex/claude/codex_claude_request_test.go index 694542cf57f..efffcd3fb83 100644 --- a/internal/translator/codex/claude/codex_claude_request_test.go +++ b/internal/translator/codex/claude/codex_claude_request_test.go @@ -714,6 +714,45 @@ func TestConvertClaudeRequestToCodex_AssistantGrokSignatureToReasoningItem(t *te } } +func testMetaClaudeSignature() string { + // Shape-valid synthetic Meta envelope; no captured value is embedded. + signature := "Q-PaDgE" + for len(signature) < 1426 { + signature += "A" + } + return signature +} + +func TestConvertClaudeRequestToCodex_AssistantMetaSignatureToReasoningItem(t *testing.T) { + signature := testMetaClaudeSignature() + payload := []byte(`{"model":"muse-spark-1.3","messages":[{"role":"assistant","content":[{"type":"thinking","thinking":"summary","signature":""},{"type":"text","text":"answer"}]},{"role":"user","content":"next"}]}`) + payload, _ = sjson.SetBytes(payload, "messages.0.content.0.signature", signature) + + out, _ := ConvertClaudeRequestToCodex("muse-spark-1.3", payload, false) + reasoning := gjson.GetBytes(out, "input.0") + if reasoning.Get("type").String() != "reasoning" { + t.Fatalf("input.0 type = %q, want reasoning; output=%s", reasoning.Get("type").String(), out) + } + if got := reasoning.Get("encrypted_content").String(); got != signature { + t.Fatalf("encrypted_content = %q, want Meta signature", got) + } +} + +func TestConvertClaudeRequestToCodex_IgnoresMetaSignatureForNonMetaTargets(t *testing.T) { + signature := testMetaClaudeSignature() + payload := []byte(`{"messages":[{"role":"assistant","content":[{"type":"thinking","thinking":"summary","signature":""},{"type":"text","text":"answer"}]},{"role":"user","content":"next"}]}`) + payload, _ = sjson.SetBytes(payload, "messages.0.content.0.signature", signature) + + for _, modelName := range []string{"gpt-5.4", "claude-sonnet-4-6", "grok-4.5"} { + t.Run(modelName, func(t *testing.T) { + out, _ := ConvertClaudeRequestToCodex(modelName, payload, false) + if got := countRequestInputItemsByType(out, "reasoning"); got != 0 { + t.Fatalf("got %d reasoning items for non-Meta target, want 0; output=%s", got, out) + } + }) + } +} + func TestConvertClaudeRequestToCodex_IgnoresGrokSignatureForNonGrokTargets(t *testing.T) { signature := "HmlYdr2aCAqCYP/m9mr8PS6KOsdMs72FGDigmydR+Jsmuv8KX97yWPlbOwmXJgWn0CbHaCacdQD3+n5EvpgLfPNmafS3kdICBjRuDf4bzHy7uBiUhNVhqPtp/ee1y9q4imPE4LYgD1VZ4J+bp9mTeqA1+nC9Oue58CiNEMV9SVaGenCD+aBnVuSTzQhD32Y+68i6HLJW0Dx6ifaRfb8hxYtA/sPM+/FTvAMW11nRho5a2BBSkpnzfqqAz/e/vGJ77/bygpXM823QA9wL9i0X" payload := []byte(`{"messages":[{"role":"assistant","content":[{"type":"thinking","thinking":"summary","signature":""},{"type":"text","text":"answer"}]},{"role":"user","content":"next"}]}`) From 88ac14f38530233507812fd5578e3b6e29895dec Mon Sep 17 00:00:00 2001 From: sususu Date: Fri, 16 Oct 2026 09:18:26 +0800 Subject: [PATCH 02/27] fix(translator): carry reasoning thought signatures through Gemini protocol - Surface Responses reasoning summary as Gemini thought parts and encrypted_content as thoughtSignature in streaming and non-streaming Codex-to-Gemini responses, with an empty carrier part when no summary text exists - Restore client-echoed thoughtSignature values to reasoning items only when the detected signature family matches the target model family, so Meta/GPT/Grok signatures replay to their own upstream and never leak to a wrong one - Verified live on muse-wonder-full-1.0-contributor at high reasoning effort: same-turn signature issuance, next-turn replay retention by hash, early-body tamper forwarded to Meta, and header tamper stripped before upstream --- .../codex/gemini/codex_gemini_request.go | 56 ++++++++++++++ .../codex/gemini/codex_gemini_request_test.go | 51 ++++++++++++ .../codex/gemini/codex_gemini_response.go | 77 ++++++++++++++++++- .../gemini/codex_gemini_response_test.go | 45 +++++++++++ 4 files changed, 226 insertions(+), 3 deletions(-) diff --git a/internal/translator/codex/gemini/codex_gemini_request.go b/internal/translator/codex/gemini/codex_gemini_request.go index 71bdebaceaa..5ffdd7bab90 100644 --- a/internal/translator/codex/gemini/codex_gemini_request.go +++ b/internal/translator/codex/gemini/codex_gemini_request.go @@ -10,6 +10,7 @@ import ( "strconv" "strings" + sigcompat "github.com/router-for-me/CLIProxyAPI/v8/internal/signature" "github.com/router-for-me/CLIProxyAPI/v8/internal/thinking" translatorcommon "github.com/router-for-me/CLIProxyAPI/v8/internal/translator/common" "github.com/router-for-me/CLIProxyAPI/v8/internal/util" @@ -69,6 +70,7 @@ func ConvertGeminiRequestToCodex(modelName string, inputRawJSON []byte, _ bool) // consume them in order when functionResponses arrive. var pendingCallIDs []string callCounter := 0 + emittedThoughtSignatures := map[string]bool{} getGeminiCallID := func(value gjson.Result) string { if callID := strings.TrimSpace(value.Get("id").String()); callID != "" { @@ -140,6 +142,29 @@ func ConvertGeminiRequestToCodex(modelName string, inputRawJSON []byte, _ bool) parr := parts.Array() for j := 0; j < len(parr); j++ { p := parr[j] + + // A thoughtSignature is replayable reasoning state, not visible + // content. Restore it as a reasoning item only when its detected + // family matches this request's target model family, so a Muse + // (or GPT/Grok) signature can never be replayed to a wrong upstream + // through the Gemini ingress. Unmatched signatures fall through to + // the existing hidden-thought drop below. + if role == "assistant" { + if rawSignature := geminiPartThoughtSignature(p); rawSignature != "" && !emittedThoughtSignatures[rawSignature] && geminiThoughtSignatureMatchesCodexTarget(modelName, rawSignature) { + reasoningItem := []byte(`{"type":"reasoning","encrypted_content":"","summary":[]}`) + reasoningItem, _ = sjson.SetBytes(reasoningItem, "encrypted_content", rawSignature) + if translatorcommon.IsGeminiThoughtPart(p) { + if thoughtText := strings.TrimSpace(p.Get("text").String()); thoughtText != "" { + summaryPart := []byte(`{"type":"summary_text","text":""}`) + summaryPart, _ = sjson.SetBytes(summaryPart, "text", thoughtText) + reasoningItem, _ = sjson.SetRawBytes(reasoningItem, "summary", translatorcommon.JoinRawArray([][]byte{summaryPart})) + } + } + inputItems = append(inputItems, reasoningItem) + emittedThoughtSignatures[rawSignature] = true + } + } + if translatorcommon.IsGeminiThoughtPart(p) { continue } @@ -344,6 +369,37 @@ func ConvertGeminiRequestToCodex(modelName string, inputRawJSON []byte, _ bool) return out, nil } +// geminiPartThoughtSignature extracts a part's thought signature under either +// the camelCase or snake_case field spelling. +func geminiPartThoughtSignature(part gjson.Result) string { + if signature := strings.TrimSpace(part.Get("thoughtSignature").String()); signature != "" { + return signature + } + return strings.TrimSpace(part.Get("thought_signature").String()) +} + +// geminiThoughtSignatureMatchesCodexTarget reports whether a client-echoed +// thought signature may be restored as reasoning encrypted_content for the +// Codex-format upstream serving modelName. Positively detected families must +// match the target family exactly (an unmapped target defaults to the Codex +// GPT family, mirroring this translator's purpose). Grok has no detectable +// envelope, so it falls back to its shape inspector keyed on the target. +func geminiThoughtSignatureMatchesCodexTarget(modelName, rawSignature string) bool { + target := sigcompat.SignatureProviderFromModelName(thinking.ParseSuffix(modelName).ModelName) + detected := sigcompat.DetectSignatureProvider(rawSignature) + if detected != sigcompat.SignatureProviderUnknown { + if target == sigcompat.SignatureProviderUnknown { + return detected == sigcompat.SignatureProviderGPT + } + return detected == target + } + if target == sigcompat.SignatureProviderGrok { + _, err := sigcompat.InspectGrokEncryptedContent(rawSignature) + return err == nil + } + return false +} + func setCodexToolChoiceFromGeminiToolConfig(out []byte, functionCallingConfig gjson.Result) []byte { if !functionCallingConfig.Exists() { return out diff --git a/internal/translator/codex/gemini/codex_gemini_request_test.go b/internal/translator/codex/gemini/codex_gemini_request_test.go index 4ac4976a42b..2158afb7982 100644 --- a/internal/translator/codex/gemini/codex_gemini_request_test.go +++ b/internal/translator/codex/gemini/codex_gemini_request_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/tidwall/gjson" + "github.com/tidwall/sjson" ) func TestConvertGeminiRequestToCodex_PreservesCustomCallIDs(t *testing.T) { @@ -116,6 +117,56 @@ func TestConvertGeminiRequestToCodex_DropsHiddenThoughtParts(t *testing.T) { }) } +func testGeminiMetaThoughtSignature() string { + // Shape-valid synthetic Meta envelope; no captured value is embedded. + signature := "Q-PaDgE" + for len(signature) < 1426 { + signature += "A" + } + return signature +} + +func TestConvertGeminiRequestToCodex_PreservesMetaThoughtSignatureForMuseTarget(t *testing.T) { + signature := testGeminiMetaThoughtSignature() + payload := []byte(`{"contents":[{"role":"model","parts":[{"thought":true,"text":"internal reasoning","thoughtSignature":""},{"text":"visible answer"}]},{"role":"user","parts":[{"text":"continue"}]}]}`) + payload, _ = sjson.SetBytes(payload, "contents.0.parts.0.thoughtSignature", signature) + + out, _ := ConvertGeminiRequestToCodex("muse-spark-1.3", payload, false) + input := gjson.GetBytes(out, "input").Array() + if len(input) != 3 { + t.Fatalf("input len = %d, want reasoning+message+user. Output: %s", len(input), string(out)) + } + if got := input[0].Get("type").String(); got != "reasoning" { + t.Fatalf("input.0 type = %q, want reasoning. Output: %s", got, string(out)) + } + if got := input[0].Get("encrypted_content").String(); got != signature { + t.Fatalf("encrypted_content = %q, want Meta signature", got) + } + if got := input[0].Get("summary.0.text").String(); got != "internal reasoning" { + t.Fatalf("summary text = %q, want thought text. Output: %s", got, string(out)) + } + if got := input[1].Get("content.0.text").String(); got != "visible answer" { + t.Fatalf("visible text = %q. Output: %s", got, string(out)) + } +} + +func TestConvertGeminiRequestToCodex_DropsMetaThoughtSignatureForNonMetaTargets(t *testing.T) { + signature := testGeminiMetaThoughtSignature() + payload := []byte(`{"contents":[{"role":"model","parts":[{"thought":true,"text":"internal reasoning","thoughtSignature":""},{"text":"visible answer"}]},{"role":"user","parts":[{"text":"continue"}]}]}`) + payload, _ = sjson.SetBytes(payload, "contents.0.parts.0.thoughtSignature", signature) + + for _, modelName := range []string{"gpt-5.1-codex", "claude-sonnet-4-6", "grok-4.5"} { + t.Run(modelName, func(t *testing.T) { + out, _ := ConvertGeminiRequestToCodex(modelName, payload, false) + for _, item := range gjson.GetBytes(out, "input").Array() { + if item.Get("type").String() == "reasoning" { + t.Fatalf("Meta signature leaked to %s target. Output: %s", modelName, string(out)) + } + } + }) + } +} + func TestConvertGeminiRequestToCodex_DeterministicCallIDs(t *testing.T) { raw := []byte(`{ "contents": [ diff --git a/internal/translator/codex/gemini/codex_gemini_response.go b/internal/translator/codex/gemini/codex_gemini_response.go index ccf3877ac24..78b809926da 100644 --- a/internal/translator/codex/gemini/codex_gemini_response.go +++ b/internal/translator/codex/gemini/codex_gemini_response.go @@ -28,6 +28,10 @@ type ConvertCodexResponseToGeminiParams struct { LastStorageOutput []byte HasOutputTextDelta bool LastImageHashByID map[string][32]byte + // PendingThoughtSignature holds a completed reasoning item's + // encrypted_content until the next visible part can carry it as a + // Gemini thoughtSignature, mirroring native Gemini part placement. + PendingThoughtSignature string } // ConvertCodexResponseToGemini converts Codex streaming response format to Gemini format. @@ -109,6 +113,23 @@ func ConvertCodexResponseToGemini(_ context.Context, modelName string, originalR if typeStr == "response.output_item.done" { itemResult := rootResult.Get("item") itemType := itemResult.Get("type").String() + if itemType == "reasoning" { + signature := strings.TrimSpace(itemResult.Get("encrypted_content").String()) + if signature == "" { + return [][]byte{} + } + if params.PendingThoughtSignature != "" { + // A previous reasoning step never got a visible carrier part; + // flush it as an empty thought part so no signature is lost. + part := []byte(`{"thought":true,"text":"","thoughtSignature":""}`) + part, _ = sjson.SetBytes(part, "thoughtSignature", params.PendingThoughtSignature) + params.PendingThoughtSignature = signature + template = translatorcommon.SetRawArrayItems(template, "candidates.0.content.parts", [][]byte{part}) + return [][]byte{template} + } + params.PendingThoughtSignature = signature + return [][]byte{} + } if itemType == "image_generation_call" { itemID := itemResult.Get("id").String() b64 := itemResult.Get("result").String() @@ -157,6 +178,10 @@ func ConvertCodexResponseToGemini(_ context.Context, modelName string, originalR } } functionCall = setGeminiFunctionCallID(functionCall, itemResult) + if params.PendingThoughtSignature != "" { + functionCall, _ = sjson.SetBytes(functionCall, "thoughtSignature", params.PendingThoughtSignature) + params.PendingThoughtSignature = "" + } template = translatorcommon.SetRawArrayItems(template, "candidates.0.content.parts", [][]byte{functionCall}) template, _ = sjson.SetBytes(template, "candidates.0.finishReason", "STOP") @@ -180,6 +205,10 @@ func ConvertCodexResponseToGemini(_ context.Context, modelName string, originalR params.HasOutputTextDelta = true part := []byte(`{"text":""}`) part, _ = sjson.SetBytes(part, "text", rootResult.Get("delta").String()) + if params.PendingThoughtSignature != "" { + part, _ = sjson.SetBytes(part, "thoughtSignature", params.PendingThoughtSignature) + params.PendingThoughtSignature = "" + } template = translatorcommon.SetRawArrayItems(template, "candidates.0.content.parts", [][]byte{part}) } else if typeStr == "response.output_item.done" { // Fallback: emit final message text when no delta chunks were received itemResult := rootResult.Get("item") @@ -201,6 +230,10 @@ func ConvertCodexResponseToGemini(_ context.Context, modelName string, originalR } part := []byte(`{"text":""}`) part, _ = sjson.SetBytes(part, "text", text) + if params.PendingThoughtSignature != "" { + part, _ = sjson.SetBytes(part, "thoughtSignature", params.PendingThoughtSignature) + params.PendingThoughtSignature = "" + } template, _ = sjson.SetRawBytes(template, "candidates.0.content.parts.-1", part) wroteText = true return true @@ -211,6 +244,12 @@ func ConvertCodexResponseToGemini(_ context.Context, modelName string, originalR } return [][]byte{} } else if typeStr == "response.completed" || typeStr == "response.incomplete" { // Handle response completion with usage metadata + if params.PendingThoughtSignature != "" { + part := []byte(`{"thought":true,"text":"","thoughtSignature":""}`) + part, _ = sjson.SetBytes(part, "thoughtSignature", params.PendingThoughtSignature) + params.PendingThoughtSignature = "" + template = translatorcommon.SetRawArrayItems(template, "candidates.0.content.parts", [][]byte{part}) + } template, _ = sjson.SetBytes(template, "usageMetadata.promptTokenCount", rootResult.Get("response.usage.input_tokens").Int()) template, _ = sjson.SetBytes(template, "usageMetadata.candidatesTokenCount", rootResult.Get("response.usage.output_tokens").Int()) totalTokens := rootResult.Get("response.usage.input_tokens").Int() + rootResult.Get("response.usage.output_tokens").Int() @@ -308,10 +347,42 @@ func ConvertCodexResponseToGeminiNonStream(_ context.Context, modelName string, // Flush any pending function calls before adding non-function content flushPendingFunctionCalls() - // Add thinking content - if content := value.Get("content"); content.Exists() { + // Surface the reasoning summary as thought parts and carry + // the encrypted reasoning state as thoughtSignature on the + // first of them, so a Gemini client can replay it next turn. + // With no summary text, emit an empty thought carrier part; + // dropping the signature here would silently break replay. + signature := strings.TrimSpace(value.Get("encrypted_content").String()) + var thoughtTexts []string + if summary := value.Get("summary"); summary.IsArray() { + for _, entry := range summary.Array() { + if text := entry.Get("text").String(); text != "" { + thoughtTexts = append(thoughtTexts, text) + } + } + } + if content := value.Get("content"); content.IsArray() { + for _, entry := range content.Array() { + if entry.Get("type").String() == "reasoning_text" { + if text := entry.Get("text").String(); text != "" { + thoughtTexts = append(thoughtTexts, text) + } + } + } + } else if content.Type == gjson.String && content.String() != "" { + thoughtTexts = append(thoughtTexts, content.String()) + } + for i, thoughtText := range thoughtTexts { part := []byte(`{"text":"","thought":true}`) - part, _ = sjson.SetBytes(part, "text", content.String()) + part, _ = sjson.SetBytes(part, "text", thoughtText) + if i == 0 && signature != "" { + part, _ = sjson.SetBytes(part, "thoughtSignature", signature) + } + parts = append(parts, part) + } + if len(thoughtTexts) == 0 && signature != "" { + part := []byte(`{"text":"","thought":true,"thoughtSignature":""}`) + part, _ = sjson.SetBytes(part, "thoughtSignature", signature) parts = append(parts, part) } diff --git a/internal/translator/codex/gemini/codex_gemini_response_test.go b/internal/translator/codex/gemini/codex_gemini_response_test.go index 5dda9cd5803..f3d35e9259f 100644 --- a/internal/translator/codex/gemini/codex_gemini_response_test.go +++ b/internal/translator/codex/gemini/codex_gemini_response_test.go @@ -156,6 +156,51 @@ func TestConvertCodexResponseToGemini_StreamPreservesFunctionCallID(t *testing.T } } +func TestConvertCodexResponseToGemini_ReasoningThoughtSignature(t *testing.T) { + ctx := context.Background() + signature := testGeminiMetaThoughtSignature() + + t.Run("non-stream summary carries signature", func(t *testing.T) { + raw := []byte(`{"type":"response.completed","response":{"id":"resp_1","model":"muse-spark-1.3","created_at":1,"usage":{"input_tokens":1,"output_tokens":2},"output":[{"type":"reasoning","encrypted_content":"` + signature + `","summary":[{"type":"summary_text","text":"thinking summary"}]},{"type":"message","content":[{"type":"output_text","text":"answer"}]}]}}`) + out := ConvertCodexResponseToGeminiNonStream(ctx, "muse-spark-1.3", nil, nil, raw, nil) + part := gjson.GetBytes(out, "candidates.0.content.parts.0") + if !part.Get("thought").Bool() || part.Get("text").String() != "thinking summary" { + t.Fatalf("part.0 = %s, want thought summary part; payload=%s", part.Raw, out) + } + if got := part.Get("thoughtSignature").String(); got != signature { + t.Fatalf("thoughtSignature = %q, want Meta signature; payload=%s", got, out) + } + if got := gjson.GetBytes(out, "candidates.0.content.parts.1.text").String(); got != "answer" { + t.Fatalf("part.1 text = %q, want answer; payload=%s", got, out) + } + }) + + t.Run("non-stream empty summary still carries signature", func(t *testing.T) { + raw := []byte(`{"type":"response.completed","response":{"id":"resp_1","model":"muse-spark-1.3","created_at":1,"usage":{"input_tokens":1,"output_tokens":2},"output":[{"type":"reasoning","encrypted_content":"` + signature + `","summary":[]},{"type":"message","content":[{"type":"output_text","text":"answer"}]}]}}`) + out := ConvertCodexResponseToGeminiNonStream(ctx, "muse-spark-1.3", nil, nil, raw, nil) + part := gjson.GetBytes(out, "candidates.0.content.parts.0") + if !part.Get("thought").Bool() || part.Get("thoughtSignature").String() != signature { + t.Fatalf("part.0 = %s, want empty thought carrier with signature; payload=%s", part.Raw, out) + } + }) + + t.Run("stream signature attaches to next text part", func(t *testing.T) { + var param any + done := []byte(`data: {"type":"response.output_item.done","item":{"type":"reasoning","encrypted_content":"` + signature + `","summary":[]}}`) + if out := ConvertCodexResponseToGemini(ctx, "muse-spark-1.3", nil, nil, done, ¶m); len(out) != 0 { + t.Fatalf("reasoning done emitted %d chunks before a visible carrier, want 0", len(out)) + } + out := ConvertCodexResponseToGemini(ctx, "muse-spark-1.3", nil, nil, []byte(`data: {"type":"response.output_text.delta","delta":"answer"}`), ¶m) + if len(out) != 1 { + t.Fatalf("text delta emitted %d chunks, want 1", len(out)) + } + part := gjson.GetBytes(out[0], "candidates.0.content.parts.0") + if part.Get("text").String() != "answer" || part.Get("thoughtSignature").String() != signature { + t.Fatalf("text part = %s, want answer with Meta thoughtSignature", part.Raw) + } + }) +} + func TestConvertCodexResponseToGeminiNonStreamPreservesFunctionCallID(t *testing.T) { ctx := context.Background() originalRequest := []byte(`{"tools":[]}`) From a4660260f894432bc13b5e26b9b569614726cb8c Mon Sep 17 00:00:00 2001 From: sususu Date: Fri, 16 Oct 2026 10:17:13 +0800 Subject: [PATCH 03/27] fix(signature): make Gemini thought-signature carriage SDK-safe and order-preserving - Encode Responses signatures for Gemini thoughtSignature as a versioned carrier in standard padded base64 so official SDK []byte decoding succeeds, and unwrap plus normalize (including cache prefixes) on Gemini ingress - Queue streamed function-call outputs in order and flush on every return path so a pending thought signature can no longer be overwritten or bypassed; deduplicate repeated reasoning done events and never spend a signature on an empty text delta - Resolve signature provider family from the canonical last model-id segment so namespace prefixes cannot outrank the actual model, and reject non-bucket Meta lengths before decoding - Verified live at high reasoning effort: carrier round-trip replay by hash, early-body tamper forwarded to Meta, header tamper stripped before upstream --- internal/signature/meta_validation.go | 26 ++-- internal/signature/meta_validation_test.go | 12 ++ internal/signature/provider_compatibility.go | 22 ++++ .../codex/claude/codex_claude_request.go | 2 +- .../codex/gemini/codex_gemini_request.go | 75 +++++++---- .../codex/gemini/codex_gemini_request_test.go | 46 +++++++ .../codex/gemini/codex_gemini_response.go | 122 +++++++++++++----- .../gemini/codex_gemini_response_test.go | 56 +++++++- 8 files changed, 289 insertions(+), 72 deletions(-) diff --git a/internal/signature/meta_validation.go b/internal/signature/meta_validation.go index 7f611ddc245..f53eb470431 100644 --- a/internal/signature/meta_validation.go +++ b/internal/signature/meta_validation.go @@ -63,28 +63,34 @@ func InspectMetaReasoningSignature(rawSignature string) (*MetaReasoningSignature if !strings.HasPrefix(sig, "Q-PaDg") { return nil, fmt.Errorf("invalid Meta reasoning signature: expected Q-PaDg prefix") } - if index, r, ok := firstInvalidMetaReasoningSignatureChar(sig); ok { - return nil, fmt.Errorf("invalid Meta reasoning signature: contains non-base64url character U+%04X at byte %d", r, index) - } + // Reject by encoded length before scanning or allocating a decoded copy: + // only bucket-sized candidates ever reach the decoder. if len(sig)%4 == 1 { return nil, fmt.Errorf("invalid Meta reasoning signature: base64url length %d cannot decode", len(sig)) } + decodedLen := len(sig) / 4 * 3 + switch len(sig) % 4 { + case 2: + decodedLen++ + case 3: + decodedLen += 2 + } + payloadLen := decodedLen - metaReasoningSignatureOverheadLen + if payloadLen < metaReasoningSignatureMinPayload || payloadLen&(payloadLen-1) != 0 { + return nil, fmt.Errorf("invalid Meta reasoning signature: payload length %d is not a power-of-two bucket", payloadLen) + } + if index, r, ok := firstInvalidMetaReasoningSignatureChar(sig); ok { + return nil, fmt.Errorf("invalid Meta reasoning signature: contains non-base64url character U+%04X at byte %d", r, index) + } // Strict decoding rejects non-canonical trailing bits, so no re-encode // comparison is needed to prove canonical form. decoded, err := base64.RawURLEncoding.Strict().DecodeString(sig) if err != nil { return nil, fmt.Errorf("invalid Meta reasoning signature: base64url decode failed") } - if len(decoded) < metaReasoningSignatureOverheadLen+metaReasoningSignatureMinPayload { - return nil, fmt.Errorf("invalid Meta reasoning signature: decoded payload too short") - } if !bytes.Equal(decoded[:len(metaReasoningSignatureHeader)], metaReasoningSignatureHeader) { return nil, fmt.Errorf("invalid Meta reasoning signature: unexpected envelope header %x", decoded[:len(metaReasoningSignatureHeader)]) } - payloadLen := len(decoded) - metaReasoningSignatureOverheadLen - if payloadLen&(payloadLen-1) != 0 { - return nil, fmt.Errorf("invalid Meta reasoning signature: payload length %d is not a power-of-two bucket", payloadLen) - } exponent := 0 for p := payloadLen; p > metaReasoningSignatureMinPayload; p >>= 1 { exponent++ diff --git a/internal/signature/meta_validation_test.go b/internal/signature/meta_validation_test.go index 8b7ff2084be..52a2fab5b3c 100644 --- a/internal/signature/meta_validation_test.go +++ b/internal/signature/meta_validation_test.go @@ -149,6 +149,18 @@ func TestMetaSignatureCompatibility_OtherTargets(t *testing.T) { } } +func TestSignatureProviderFromModelName_NamespacedTailWins(t *testing.T) { + for model, want := range map[string]SignatureProvider{ + "openai-compatible/muse-spark-1.3": SignatureProviderMeta, + "muse-proxy/gpt-5.4": SignatureProviderGPT, + "openrouter/anthropic/claude-sonnet-4-6": SignatureProviderClaude, + } { + if got := SignatureProviderFromModelName(model); got != want { + t.Errorf("SignatureProviderFromModelName(%q) = %q, want %q", model, got, want) + } + } +} + func TestMetaCachePrefixUnsupported(t *testing.T) { // No producer emits provider-prefixed Meta signatures, so unlike GPT or // Claude there is deliberately no cache-prefix alias for Meta yet. diff --git a/internal/signature/provider_compatibility.go b/internal/signature/provider_compatibility.go index d31021d2073..67ae60e7985 100644 --- a/internal/signature/provider_compatibility.go +++ b/internal/signature/provider_compatibility.go @@ -63,6 +63,28 @@ type SignatureCompatibilityDecision struct { // whose signed history can be safely replayed for that model. func SignatureProviderFromModelName(modelName string) SignatureProvider { lower := strings.ToLower(strings.TrimSpace(modelName)) + if lower == "" { + return SignatureProviderUnknown + } + // A namespaced ID's canonical model is its last path segment, so a + // vendor/router prefix such as "openai-compatible/" must not outrank the + // actual model family ("openai-compatible/muse-spark-1.3" is Meta, not + // GPT). Only when the last segment names no family does the full string + // get the legacy substring treatment. + candidate := lower + if idx := strings.LastIndexAny(lower, "/:"); idx >= 0 && idx < len(lower)-1 { + candidate = lower[idx+1:] + } + if provider := signatureProviderFromModelNameValue(candidate); provider != SignatureProviderUnknown { + return provider + } + if candidate != lower { + return signatureProviderFromModelNameValue(lower) + } + return SignatureProviderUnknown +} + +func signatureProviderFromModelNameValue(lower string) SignatureProvider { switch { case strings.Contains(lower, "claude"): return SignatureProviderClaude diff --git a/internal/translator/codex/claude/codex_claude_request.go b/internal/translator/codex/claude/codex_claude_request.go index 58da492c084..19f0cf18ea7 100644 --- a/internal/translator/codex/claude/codex_claude_request.go +++ b/internal/translator/codex/claude/codex_claude_request.go @@ -182,7 +182,7 @@ func convertClaudeRequestToCodex(modelName string, inputRawJSON []byte, _ bool, ok = true } if !ok && codexClaudeTargetAcceptsMetaSignature(modelName) && sigcompat.IsValidMetaReasoningSignature(rawSignature) { - signature = rawSignature + signature = strings.TrimSpace(rawSignature) ok = true } if !ok { diff --git a/internal/translator/codex/gemini/codex_gemini_request.go b/internal/translator/codex/gemini/codex_gemini_request.go index 5ffdd7bab90..2bbcfb35964 100644 --- a/internal/translator/codex/gemini/codex_gemini_request.go +++ b/internal/translator/codex/gemini/codex_gemini_request.go @@ -6,6 +6,7 @@ package gemini import ( + "encoding/base64" "fmt" "strconv" "strings" @@ -150,18 +151,20 @@ func ConvertGeminiRequestToCodex(modelName string, inputRawJSON []byte, _ bool) // through the Gemini ingress. Unmatched signatures fall through to // the existing hidden-thought drop below. if role == "assistant" { - if rawSignature := geminiPartThoughtSignature(p); rawSignature != "" && !emittedThoughtSignatures[rawSignature] && geminiThoughtSignatureMatchesCodexTarget(modelName, rawSignature) { - reasoningItem := []byte(`{"type":"reasoning","encrypted_content":"","summary":[]}`) - reasoningItem, _ = sjson.SetBytes(reasoningItem, "encrypted_content", rawSignature) - if translatorcommon.IsGeminiThoughtPart(p) { - if thoughtText := strings.TrimSpace(p.Get("text").String()); thoughtText != "" { - summaryPart := []byte(`{"type":"summary_text","text":""}`) - summaryPart, _ = sjson.SetBytes(summaryPart, "text", thoughtText) - reasoningItem, _ = sjson.SetRawBytes(reasoningItem, "summary", translatorcommon.JoinRawArray([][]byte{summaryPart})) + if rawSignature := geminiPartThoughtSignature(p); rawSignature != "" { + if normalizedSignature, ok := normalizedGeminiThoughtSignatureForCodexTarget(modelName, rawSignature); ok && !emittedThoughtSignatures[normalizedSignature] { + reasoningItem := []byte(`{"type":"reasoning","encrypted_content":"","summary":[]}`) + reasoningItem, _ = sjson.SetBytes(reasoningItem, "encrypted_content", normalizedSignature) + if translatorcommon.IsGeminiThoughtPart(p) { + if thoughtText := strings.TrimSpace(p.Get("text").String()); thoughtText != "" { + summaryPart := []byte(`{"type":"summary_text","text":""}`) + summaryPart, _ = sjson.SetBytes(summaryPart, "text", thoughtText) + reasoningItem, _ = sjson.SetRawBytes(reasoningItem, "summary", translatorcommon.JoinRawArray([][]byte{summaryPart})) + } } + inputItems = append(inputItems, reasoningItem) + emittedThoughtSignatures[normalizedSignature] = true } - inputItems = append(inputItems, reasoningItem) - emittedThoughtSignatures[rawSignature] = true } } @@ -378,26 +381,52 @@ func geminiPartThoughtSignature(part gjson.Result) string { return strings.TrimSpace(part.Get("thought_signature").String()) } -// geminiThoughtSignatureMatchesCodexTarget reports whether a client-echoed -// thought signature may be restored as reasoning encrypted_content for the -// Codex-format upstream serving modelName. Positively detected families must -// match the target family exactly (an unmapped target defaults to the Codex -// GPT family, mirroring this translator's purpose). Grok has no detectable -// envelope, so it falls back to its shape inspector keyed on the target. -func geminiThoughtSignatureMatchesCodexTarget(modelName, rawSignature string) bool { +// decodeGeminiCodexThoughtSignature unwraps the carrier written by +// encodeGeminiCodexThoughtSignature. Values without the carrier magic are +// returned unchanged, preserving legacy raw-signature echoes. +func decodeGeminiCodexThoughtSignature(value string) string { + value = strings.TrimSpace(value) + decoded, err := base64.StdEncoding.DecodeString(value) + if err != nil || !strings.HasPrefix(string(decoded), geminiCodexThoughtSignatureCarrierMagic) { + return value + } + return strings.TrimSpace(strings.TrimPrefix(string(decoded), geminiCodexThoughtSignatureCarrierMagic)) +} + +// normalizedGeminiThoughtSignatureForCodexTarget unwraps and normalizes a +// client-echoed thought signature for the Codex-format upstream serving +// modelName, returning the exact value that may be written as reasoning +// encrypted_content. Positively detected families must match the target +// family exactly (an unmapped target defaults to the Codex GPT family, +// mirroring this translator's purpose) and go through the shared +// compatibility normalization, so cache-prefixed values such as "gpt#..." +// are stripped to their native payload instead of being forwarded verbatim. +// Grok has no detectable envelope, so it falls back to its shape inspector +// keyed on the target. +func normalizedGeminiThoughtSignatureForCodexTarget(modelName, rawSignature string) (string, bool) { + rawSignature = decodeGeminiCodexThoughtSignature(rawSignature) + if rawSignature == "" { + return "", false + } target := sigcompat.SignatureProviderFromModelName(thinking.ParseSuffix(modelName).ModelName) detected := sigcompat.DetectSignatureProvider(rawSignature) if detected != sigcompat.SignatureProviderUnknown { - if target == sigcompat.SignatureProviderUnknown { - return detected == sigcompat.SignatureProviderGPT + effectiveTarget := target + if effectiveTarget == sigcompat.SignatureProviderUnknown { + effectiveTarget = sigcompat.SignatureProviderGPT + } + if detected != effectiveTarget { + return "", false } - return detected == target + normalized, ok := sigcompat.CompatibleSignatureForProvider(effectiveTarget, rawSignature) + return normalized, ok && normalized != "" } if target == sigcompat.SignatureProviderGrok { - _, err := sigcompat.InspectGrokEncryptedContent(rawSignature) - return err == nil + if _, err := sigcompat.InspectGrokEncryptedContent(rawSignature); err == nil { + return rawSignature, true + } } - return false + return "", false } func setCodexToolChoiceFromGeminiToolConfig(out []byte, functionCallingConfig gjson.Result) []byte { diff --git a/internal/translator/codex/gemini/codex_gemini_request_test.go b/internal/translator/codex/gemini/codex_gemini_request_test.go index 2158afb7982..1ab0ff7ba9f 100644 --- a/internal/translator/codex/gemini/codex_gemini_request_test.go +++ b/internal/translator/codex/gemini/codex_gemini_request_test.go @@ -1,6 +1,7 @@ package gemini import ( + "encoding/base64" "fmt" "testing" @@ -167,6 +168,51 @@ func TestConvertGeminiRequestToCodex_DropsMetaThoughtSignatureForNonMetaTargets( } } +func testGeminiGPTThoughtSignature() string { + decoded := make([]byte, 89) + decoded[0] = 0x80 + for i := 9; i < len(decoded); i++ { + decoded[i] = byte(i) + } + return base64.RawURLEncoding.EncodeToString(decoded) +} + +func TestConvertGeminiRequestToCodex_NormalizesPrefixedAndCarrierSignatures(t *testing.T) { + gptSignature := testGeminiGPTThoughtSignature() + metaSignature := testGeminiMetaThoughtSignature() + cases := []struct { + name string + model string + echoed string + want string + wantFound bool + }{ + {"gpt cache prefixed", "gpt-5.1-codex", "gpt#" + gptSignature, gptSignature, true}, + {"carrier wrapped gpt prefixed", "gpt-5.1-codex", encodeGeminiCodexThoughtSignature("gpt#" + gptSignature), gptSignature, true}, + {"carrier wrapped meta", "muse-spark-1.3", encodeGeminiCodexThoughtSignature(metaSignature), metaSignature, true}, + {"carrier wrapped meta to gpt target", "gpt-5.1-codex", encodeGeminiCodexThoughtSignature(metaSignature), "", false}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + payload := []byte(`{"contents":[{"role":"model","parts":[{"thought":true,"text":"internal reasoning","thoughtSignature":""},{"text":"visible answer"}]},{"role":"user","parts":[{"text":"continue"}]}]}`) + payload, _ = sjson.SetBytes(payload, "contents.0.parts.0.thoughtSignature", tc.echoed) + out, _ := ConvertGeminiRequestToCodex(tc.model, payload, false) + found := "" + for _, item := range gjson.GetBytes(out, "input").Array() { + if item.Get("type").String() == "reasoning" { + found = item.Get("encrypted_content").String() + } + } + if tc.wantFound && found != tc.want { + t.Fatalf("encrypted_content = %q, want normalized signature; output=%s", found, string(out)) + } + if !tc.wantFound && found != "" { + t.Fatalf("foreign signature leaked as reasoning item; output=%s", string(out)) + } + }) + } +} + func TestConvertGeminiRequestToCodex_DeterministicCallIDs(t *testing.T) { raw := []byte(`{ "contents": [ diff --git a/internal/translator/codex/gemini/codex_gemini_response.go b/internal/translator/codex/gemini/codex_gemini_response.go index 78b809926da..082bc123dad 100644 --- a/internal/translator/codex/gemini/codex_gemini_response.go +++ b/internal/translator/codex/gemini/codex_gemini_response.go @@ -8,6 +8,7 @@ import ( "bytes" "context" "crypto/sha256" + "encoding/base64" "strings" "time" @@ -22,16 +23,22 @@ var ( // ConvertCodexResponseToGeminiParams holds parameters for response conversion. type ConvertCodexResponseToGeminiParams struct { - Model string - CreatedAt int64 - ResponseID string - LastStorageOutput []byte + Model string + CreatedAt int64 + ResponseID string + // StorageOutputs queues completed function-call chunks in stream order + // until the next non-storage event flushes them. A queue (not a single + // slot) so consecutive function calls can never overwrite each other. + StorageOutputs [][]byte HasOutputTextDelta bool LastImageHashByID map[string][32]byte // PendingThoughtSignature holds a completed reasoning item's // encrypted_content until the next visible part can carry it as a // Gemini thoughtSignature, mirroring native Gemini part placement. PendingThoughtSignature string + // SeenReasoningSignatures deduplicates repeated reasoning done events + // by item ID + signature hash within one response stream. + SeenReasoningSignatures map[[32]byte]bool } // ConvertCodexResponseToGemini converts Codex streaming response format to Gemini format. @@ -53,7 +60,6 @@ func ConvertCodexResponseToGemini(_ context.Context, modelName string, originalR Model: modelName, CreatedAt: 0, ResponseID: "", - LastStorageOutput: nil, HasOutputTextDelta: false, LastImageHashByID: make(map[string][32]byte), } @@ -70,6 +76,25 @@ func ConvertCodexResponseToGemini(_ context.Context, modelName string, originalR params := (*param).(*ConvertCodexResponseToGeminiParams) + // flushStorageOutputs drains queued function-call chunks ahead of any + // extra chunks, preserving stream order on every return path. + flushStorageOutputs := func(extra ...[]byte) [][]byte { + outputs := make([][]byte, 0, len(params.StorageOutputs)+len(extra)) + for _, stored := range params.StorageOutputs { + outputs = append(outputs, append([]byte(nil), stored...)) + } + params.StorageOutputs = nil + return append(outputs, extra...) + } + attachPendingThoughtSignature := func(part []byte) []byte { + if params.PendingThoughtSignature == "" { + return part + } + part = setGeminiThoughtSignature(part, params.PendingThoughtSignature) + params.PendingThoughtSignature = "" + return part + } + // Base Gemini response template template := []byte(`{"candidates":[{"content":{"role":"model","parts":[]}}],"usageMetadata":{"trafficType":"PROVISIONED_THROUGHPUT"},"modelVersion":"gemini-2.5-pro","createTime":"2025-08-15T02:52:03.884209Z","responseId":"06CeaPH7NaCU48APvNXDyA4"}`) { @@ -105,8 +130,9 @@ func ConvertCodexResponseToGemini(_ context.Context, modelName string, originalR part := []byte(`{"inlineData":{"data":"","mimeType":""}}`) part, _ = sjson.SetBytes(part, "inlineData.data", b64) part, _ = sjson.SetBytes(part, "inlineData.mimeType", mimeType) + part = attachPendingThoughtSignature(part) template = translatorcommon.SetRawArrayItems(template, "candidates.0.content.parts", [][]byte{part}) - return [][]byte{template} + return flushStorageOutputs(template) } // Handle function call completion @@ -118,14 +144,21 @@ func ConvertCodexResponseToGemini(_ context.Context, modelName string, originalR if signature == "" { return [][]byte{} } + seenKey := sha256.Sum256([]byte(itemResult.Get("id").String() + "\x00" + signature)) + if params.SeenReasoningSignatures == nil { + params.SeenReasoningSignatures = make(map[[32]byte]bool) + } + if params.SeenReasoningSignatures[seenKey] { + return [][]byte{} + } + params.SeenReasoningSignatures[seenKey] = true if params.PendingThoughtSignature != "" { // A previous reasoning step never got a visible carrier part; // flush it as an empty thought part so no signature is lost. - part := []byte(`{"thought":true,"text":"","thoughtSignature":""}`) - part, _ = sjson.SetBytes(part, "thoughtSignature", params.PendingThoughtSignature) + part := setGeminiThoughtSignature([]byte(`{"thought":true,"text":""}`), params.PendingThoughtSignature) params.PendingThoughtSignature = signature template = translatorcommon.SetRawArrayItems(template, "candidates.0.content.parts", [][]byte{part}) - return [][]byte{template} + return flushStorageOutputs(template) } params.PendingThoughtSignature = signature return [][]byte{} @@ -153,8 +186,9 @@ func ConvertCodexResponseToGemini(_ context.Context, modelName string, originalR part := []byte(`{"inlineData":{"data":"","mimeType":""}}`) part, _ = sjson.SetBytes(part, "inlineData.data", b64) part, _ = sjson.SetBytes(part, "inlineData.mimeType", mimeType) + part = attachPendingThoughtSignature(part) template = translatorcommon.SetRawArrayItems(template, "candidates.0.content.parts", [][]byte{part}) - return [][]byte{template} + return flushStorageOutputs(template) } if itemType == "function_call" { // Create function call part @@ -178,15 +212,12 @@ func ConvertCodexResponseToGemini(_ context.Context, modelName string, originalR } } functionCall = setGeminiFunctionCallID(functionCall, itemResult) - if params.PendingThoughtSignature != "" { - functionCall, _ = sjson.SetBytes(functionCall, "thoughtSignature", params.PendingThoughtSignature) - params.PendingThoughtSignature = "" - } + functionCall = attachPendingThoughtSignature(functionCall) template = translatorcommon.SetRawArrayItems(template, "candidates.0.content.parts", [][]byte{functionCall}) template, _ = sjson.SetBytes(template, "candidates.0.finishReason", "STOP") - params.LastStorageOutput = append([]byte(nil), template...) + params.StorageOutputs = append(params.StorageOutputs, append([]byte(nil), template...)) // Use this return to storage message return [][]byte{} @@ -194,6 +225,12 @@ func ConvertCodexResponseToGemini(_ context.Context, modelName string, originalR } if typeStr == "response.created" { // Handle response creation - set model and response ID + // A new response must not inherit per-response streaming state if the + // caller reuses params across responses. + params.StorageOutputs = nil + params.PendingThoughtSignature = "" + params.SeenReasoningSignatures = nil + params.HasOutputTextDelta = false template, _ = sjson.SetBytes(template, "modelVersion", rootResult.Get("response.model").String()) template, _ = sjson.SetBytes(template, "responseId", rootResult.Get("response.id").String()) params.ResponseID = rootResult.Get("response.id").String() @@ -205,9 +242,10 @@ func ConvertCodexResponseToGemini(_ context.Context, modelName string, originalR params.HasOutputTextDelta = true part := []byte(`{"text":""}`) part, _ = sjson.SetBytes(part, "text", rootResult.Get("delta").String()) - if params.PendingThoughtSignature != "" { - part, _ = sjson.SetBytes(part, "thoughtSignature", params.PendingThoughtSignature) - params.PendingThoughtSignature = "" + if rootResult.Get("delta").String() != "" { + // An empty text part may be discarded by clients; never spend the + // only signature copy on one. + part = attachPendingThoughtSignature(part) } template = translatorcommon.SetRawArrayItems(template, "candidates.0.content.parts", [][]byte{part}) } else if typeStr == "response.output_item.done" { // Fallback: emit final message text when no delta chunks were received @@ -230,23 +268,19 @@ func ConvertCodexResponseToGemini(_ context.Context, modelName string, originalR } part := []byte(`{"text":""}`) part, _ = sjson.SetBytes(part, "text", text) - if params.PendingThoughtSignature != "" { - part, _ = sjson.SetBytes(part, "thoughtSignature", params.PendingThoughtSignature) - params.PendingThoughtSignature = "" - } + part = attachPendingThoughtSignature(part) template, _ = sjson.SetRawBytes(template, "candidates.0.content.parts.-1", part) wroteText = true return true }) if wroteText { params.HasOutputTextDelta = true - return [][]byte{template} + return flushStorageOutputs(template) } return [][]byte{} } else if typeStr == "response.completed" || typeStr == "response.incomplete" { // Handle response completion with usage metadata if params.PendingThoughtSignature != "" { - part := []byte(`{"thought":true,"text":"","thoughtSignature":""}`) - part, _ = sjson.SetBytes(part, "thoughtSignature", params.PendingThoughtSignature) + part := setGeminiThoughtSignature([]byte(`{"thought":true,"text":""}`), params.PendingThoughtSignature) params.PendingThoughtSignature = "" template = translatorcommon.SetRawArrayItems(template, "candidates.0.content.parts", [][]byte{part}) } @@ -261,12 +295,7 @@ func ConvertCodexResponseToGemini(_ context.Context, modelName string, originalR return [][]byte{} } - if len(params.LastStorageOutput) > 0 { - stored := append([]byte(nil), params.LastStorageOutput...) - params.LastStorageOutput = nil - return [][]byte{stored, template} - } - return [][]byte{template} + return flushStorageOutputs(template) } // ConvertCodexResponseToGeminiNonStream converts a non-streaming Codex response to a non-streaming Gemini response. @@ -376,13 +405,12 @@ func ConvertCodexResponseToGeminiNonStream(_ context.Context, modelName string, part := []byte(`{"text":"","thought":true}`) part, _ = sjson.SetBytes(part, "text", thoughtText) if i == 0 && signature != "" { - part, _ = sjson.SetBytes(part, "thoughtSignature", signature) + part = setGeminiThoughtSignature(part, signature) } parts = append(parts, part) } if len(thoughtTexts) == 0 && signature != "" { - part := []byte(`{"text":"","thought":true,"thoughtSignature":""}`) - part, _ = sjson.SetBytes(part, "thoughtSignature", signature) + part := setGeminiThoughtSignature([]byte(`{"text":"","thought":true}`), signature) parts = append(parts, part) } @@ -484,6 +512,32 @@ func buildReverseMapFromGeminiOriginal(original []byte) map[string]string { return rev } +// geminiCodexThoughtSignatureCarrierMagic marks a CPA carrier payload inside +// a Gemini thoughtSignature bytes field. The Gemini JSON wire form of a bytes +// field is standard padded base64, while Responses signatures are opaque +// strings that may use the base64url alphabet without padding; writing such a +// string verbatim makes official SDK []byte decoding fail. The carrier therefore +// stores magic+rawSignature as the byte payload and relies on standard base64 +// JSON encoding, which every Gemini client decodes and echoes losslessly. +const geminiCodexThoughtSignatureCarrierMagic = "cpa-codex-gemini-carrier-v1:" + +func encodeGeminiCodexThoughtSignature(rawSignature string) string { + rawSignature = strings.TrimSpace(rawSignature) + if rawSignature == "" { + return "" + } + return base64.StdEncoding.EncodeToString([]byte(geminiCodexThoughtSignatureCarrierMagic + rawSignature)) +} + +func setGeminiThoughtSignature(part []byte, rawSignature string) []byte { + encoded := encodeGeminiCodexThoughtSignature(rawSignature) + if encoded == "" { + return part + } + part, _ = sjson.SetBytes(part, "thoughtSignature", encoded) + return part +} + func setGeminiFunctionCallID(functionCall []byte, item gjson.Result) []byte { if callID := strings.TrimSpace(item.Get("call_id").String()); callID != "" { functionCall, _ = sjson.SetBytes(functionCall, "functionCall.id", callID) diff --git a/internal/translator/codex/gemini/codex_gemini_response_test.go b/internal/translator/codex/gemini/codex_gemini_response_test.go index f3d35e9259f..4f20a40b516 100644 --- a/internal/translator/codex/gemini/codex_gemini_response_test.go +++ b/internal/translator/codex/gemini/codex_gemini_response_test.go @@ -2,6 +2,7 @@ package gemini import ( "context" + "encoding/json" "testing" "github.com/tidwall/gjson" @@ -167,8 +168,19 @@ func TestConvertCodexResponseToGemini_ReasoningThoughtSignature(t *testing.T) { if !part.Get("thought").Bool() || part.Get("text").String() != "thinking summary" { t.Fatalf("part.0 = %s, want thought summary part; payload=%s", part.Raw, out) } - if got := part.Get("thoughtSignature").String(); got != signature { - t.Fatalf("thoughtSignature = %q, want Meta signature; payload=%s", got, out) + if got := part.Get("thoughtSignature").String(); got != encodeGeminiCodexThoughtSignature(signature) { + t.Fatalf("thoughtSignature is not the carrier-encoded Meta signature; payload=%s", out) + } + // The Gemini wire form must survive an official-SDK-style []byte + // JSON decode (standard base64), and unwrap back to the raw signature. + var sdkPart struct { + ThoughtSignature []byte `json:"thoughtSignature"` + } + if err := json.Unmarshal([]byte(part.Raw), &sdkPart); err != nil { + t.Fatalf("SDK []byte decode of thoughtSignature failed: %v", err) + } + if got := decodeGeminiCodexThoughtSignature(part.Get("thoughtSignature").String()); got != signature { + t.Fatalf("carrier round-trip = %q, want raw Meta signature", got) } if got := gjson.GetBytes(out, "candidates.0.content.parts.1.text").String(); got != "answer" { t.Fatalf("part.1 text = %q, want answer; payload=%s", got, out) @@ -179,7 +191,7 @@ func TestConvertCodexResponseToGemini_ReasoningThoughtSignature(t *testing.T) { raw := []byte(`{"type":"response.completed","response":{"id":"resp_1","model":"muse-spark-1.3","created_at":1,"usage":{"input_tokens":1,"output_tokens":2},"output":[{"type":"reasoning","encrypted_content":"` + signature + `","summary":[]},{"type":"message","content":[{"type":"output_text","text":"answer"}]}]}}`) out := ConvertCodexResponseToGeminiNonStream(ctx, "muse-spark-1.3", nil, nil, raw, nil) part := gjson.GetBytes(out, "candidates.0.content.parts.0") - if !part.Get("thought").Bool() || part.Get("thoughtSignature").String() != signature { + if !part.Get("thought").Bool() || part.Get("thoughtSignature").String() != encodeGeminiCodexThoughtSignature(signature) { t.Fatalf("part.0 = %s, want empty thought carrier with signature; payload=%s", part.Raw, out) } }) @@ -195,12 +207,48 @@ func TestConvertCodexResponseToGemini_ReasoningThoughtSignature(t *testing.T) { t.Fatalf("text delta emitted %d chunks, want 1", len(out)) } part := gjson.GetBytes(out[0], "candidates.0.content.parts.0") - if part.Get("text").String() != "answer" || part.Get("thoughtSignature").String() != signature { + if part.Get("text").String() != "answer" || part.Get("thoughtSignature").String() != encodeGeminiCodexThoughtSignature(signature) { t.Fatalf("text part = %s, want answer with Meta thoughtSignature", part.Raw) } }) } +func TestConvertCodexResponseToGemini_StreamMultipleFunctionCallsKeepSignatureAndOrder(t *testing.T) { + ctx := context.Background() + signature := testGeminiMetaThoughtSignature() + var param any + var outputs [][]byte + events := [][]byte{ + []byte(`data: {"type":"response.output_item.done","item":{"type":"reasoning","encrypted_content":"` + signature + `","summary":[]}}`), + []byte(`data: {"type":"response.output_item.done","item":{"type":"function_call","call_id":"call_a","name":"alpha","arguments":"{}"}}`), + []byte(`data: {"type":"response.output_item.done","item":{"type":"function_call","call_id":"call_b","name":"beta","arguments":"{}"}}`), + []byte(`data: {"type":"response.completed","response":{"usage":{"input_tokens":1,"output_tokens":1}}}`), + } + for _, event := range events { + outputs = append(outputs, ConvertCodexResponseToGemini(ctx, "muse-spark-1.3", nil, nil, event, ¶m)...) + } + var names []string + signatureOnAlpha := false + for _, chunk := range outputs { + for _, part := range gjson.GetBytes(chunk, "candidates.0.content.parts").Array() { + name := part.Get("functionCall.name").String() + if name == "" { + continue + } + names = append(names, name) + if name == "alpha" && decodeGeminiCodexThoughtSignature(part.Get("thoughtSignature").String()) == signature { + signatureOnAlpha = true + } + } + } + if len(names) != 2 || names[0] != "alpha" || names[1] != "beta" { + t.Fatalf("function calls = %v, want [alpha beta] in order; outputs=%q", names, outputs) + } + if !signatureOnAlpha { + t.Fatalf("first function call lost the pending Meta thoughtSignature; outputs=%q", outputs) + } +} + func TestConvertCodexResponseToGeminiNonStreamPreservesFunctionCallID(t *testing.T) { ctx := context.Background() originalRequest := []byte(`{"tools":[]}`) From cc5682c563dc4b2f73da226f6146a8d05d12b805 Mon Sep 17 00:00:00 2001 From: sususu Date: Fri, 16 Oct 2026 11:59:27 +0800 Subject: [PATCH 04/27] fix(signature): replay OpenRouter router-suffixed Meta Muse signatures OpenRouter serves meta/muse-* through its Responses API and returns encrypted_content as the native Meta envelope plus "." and a 67-char alphanumeric routing suffix. The strict Meta inspector rejected that form, so it was classified unknown and stripped on every path, including the compat Meta-target path that previously kept unknown blobs. - Accept an optional "."+[A-Za-z0-9]{1,128} router suffix after a valid native envelope and expose it as RouterSuffix; replay stays verbatim - Replace the keepMeta flag with a Meta policy: Meta-family targets on the Codex executor allow routed envelopes regardless of is-compat, while the native Meta executor stays native-only (suffix acceptance by api.meta.ai is unverified) - Live-checked on OpenRouter: native part matches the api.meta.ai header and bucket shape (5/5), suffixed, native-only and suffix-tampered replays are consumed, native-body tamper is silently dropped Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../executor/openai_responses_signature.go | 64 +++++++---- .../openai_responses_signature_test.go | 103 ++++++++++++++++++ internal/signature/meta_validation.go | 58 +++++++++- internal/signature/meta_validation_test.go | 72 ++++++++++++ internal/signature/provider_compatibility.go | 6 +- .../codex/claude/codex_claude_request_test.go | 23 ++++ .../codex/gemini/codex_gemini_request_test.go | 5 + 7 files changed, 307 insertions(+), 24 deletions(-) diff --git a/internal/runtime/executor/openai_responses_signature.go b/internal/runtime/executor/openai_responses_signature.go index 9e1e5fa46f2..475ae7c0a42 100644 --- a/internal/runtime/executor/openai_responses_signature.go +++ b/internal/runtime/executor/openai_responses_signature.go @@ -48,8 +48,19 @@ func promoteOpenAIResponsesReasoningTextToSummary(itemRaw string, content gjson. return sjson.SetRaw(itemRaw, "summary", b.String()) } +type metaSignaturePolicy int + +const ( + // metaSignatureForeign: target is not Meta; a recognized Meta envelope is foreign. + metaSignatureForeign metaSignaturePolicy = iota + // metaSignatureNativeOnly: native Meta upstream (api.meta.ai); only unsuffixed envelopes replay. + metaSignatureNativeOnly + // metaSignatureAllowRouted: Meta-family model served by a router (e.g. OpenRouter meta/muse-*); native or router-suffixed envelopes replay verbatim. + metaSignatureAllowRouted +) + func sanitizeOpenAIResponsesReasoningEncryptedContent(ctx context.Context, provider string, body []byte) []byte { - return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, false, false) + return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, false, metaSignatureForeign) } func sanitizeOpenAIResponsesReasoningEncryptedContentWithCompat(ctx context.Context, provider string, body []byte, isCompat bool) []byte { @@ -58,32 +69,39 @@ func sanitizeOpenAIResponsesReasoningEncryptedContentWithCompat(ctx context.Cont // sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel is the // compat variant that knows its target model: a positively recognized Meta -// Muse envelope is preserved only when the compat upstream actually serves -// a Meta-family model (native Meta credentials route to the Meta executor, -// but a cascaded or third-party compat endpoint can serve muse-* models -// through the Codex executor), and then under the same native-only rule as -// the Meta executor. For any other compat target the recognized Meta -// envelope is known-foreign and stripped like other providers'. +// Muse envelope is preserved whenever the upstream actually serves a +// Meta-family model, whether or not the route is flagged compat (native +// OpenAI never serves muse-* models, but a router key such as an OpenRouter +// codex endpoint can serve meta/muse-* through the Codex executor without +// isCompat). Router-suffixed envelopes replay verbatim there; the native +// Meta executor keeps its own stricter native-only gate. For any other +// target the recognized Meta envelope is known-foreign and stripped like +// other providers'. func sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(ctx context.Context, provider string, body []byte, isCompat bool, targetModel string) []byte { - keepMeta := isCompat && signature.SignatureProviderFromModelName(targetModel) == signature.SignatureProviderMeta - return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, isCompat, isCompat, keepMeta) + metaPolicy := metaSignatureForeign + if signature.SignatureProviderFromModelName(targetModel) == signature.SignatureProviderMeta { + metaPolicy = metaSignatureAllowRouted + } + return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, isCompat, isCompat, metaPolicy) } func sanitizeOpenAIResponsesReasoningEncryptedContentKeepForeign(ctx context.Context, provider string, body []byte) []byte { - return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, true, false) + return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, true, metaSignatureForeign) } // sanitizeOpenAIResponsesReasoningEncryptedContentForMeta is the Meta-target // variant and is deliberately native-only: only positively recognized Meta -// Muse envelopes may replay to Meta. GPT, other known-provider, and -// unknown-format values are all stripped, so a wrong signature can never -// reach the Meta upstream through this path. A future Meta envelope revision -// will be stripped until the inspector learns it (fail-closed). +// Muse envelopes without a router suffix may replay to api.meta.ai, whose +// suffix acceptance is unverified. GPT, other known-provider, +// unknown-format, and router-suffixed values are all stripped, so a wrong +// signature can never reach the Meta upstream through this path. A future +// Meta envelope revision will be stripped until the inspector learns it +// (fail-closed). func sanitizeOpenAIResponsesReasoningEncryptedContentForMeta(ctx context.Context, provider string, body []byte) []byte { - return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, true, true) + return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, true, metaSignatureNativeOnly) } -func sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx context.Context, provider string, body []byte, isCompat bool, keepForeign bool, keepMeta bool) []byte { +func sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx context.Context, provider string, body []byte, isCompat bool, keepForeign bool, metaPolicy metaSignaturePolicy) []byte { inputResult := util.GetGJSONBytesNoCopy(body, "input") if !inputResult.Exists() || !inputResult.IsArray() { return body @@ -198,12 +216,16 @@ func sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx context.Con rawSignature := encryptedContent.String() if rawSignature != strings.TrimSpace(rawSignature) { reason = "encrypted_content has leading or trailing whitespace" - } else if keepMeta { - // Meta targets are native-only and never reach the GPT-first gate: - // a GPT, foreign-provider, or unknown-format blob must not replay - // to Meta. Only a positively recognized Meta envelope survives. - if _, err := signature.InspectMetaReasoningSignature(rawSignature); err != nil { + } else if metaPolicy != metaSignatureForeign { + // Meta targets never reach the GPT-first gate: a GPT, + // foreign-provider, or unknown-format blob must not replay to + // Meta. Only a positively recognized Meta envelope survives, + // and the native Meta upstream additionally refuses the + // router suffix a router such as OpenRouter appends. + if info, err := signature.InspectMetaReasoningSignature(rawSignature); err != nil { reason = err.Error() + } else if metaPolicy == metaSignatureNativeOnly && info.RouterSuffix != "" { + reason = "router-suffixed Meta reasoning signature is not accepted by the native Meta upstream" } } else if _, err := signature.InspectGPTReasoningSignature(rawSignature); err != nil { // Valid GPT signatures never reach this branch: the GPT-first gate diff --git a/internal/runtime/executor/openai_responses_signature_test.go b/internal/runtime/executor/openai_responses_signature_test.go index 42953677ba9..b32a83705df 100644 --- a/internal/runtime/executor/openai_responses_signature_test.go +++ b/internal/runtime/executor/openai_responses_signature_test.go @@ -28,6 +28,20 @@ func validOpenAIResponsesReasoningEncryptedContentForTest() string { return base64.RawURLEncoding.EncodeToString(payload) } +// validMetaReasoningEncryptedContentForTest builds a shape-valid synthetic +// Meta envelope: "Q-PaDgE" pins the observed five-byte header, and 1426 +// unpadded base64url chars decode to 45 + 1024 bytes. No captured signature +// is embedded in tests. +func validMetaReasoningEncryptedContentForTest() string { + return "Q-PaDgE" + strings.Repeat("A", 1419) +} + +// routedMetaReasoningEncryptedContentForTest appends a synthetic +// OpenRouter-style suffix: one '.' plus 67 alphanumeric characters. +func routedMetaReasoningEncryptedContentForTest() string { + return validMetaReasoningEncryptedContentForTest() + "." + strings.Repeat("Ab3", 23)[:67] +} + func TestSanitizeOpenAIResponsesReasoningEncryptedContent_StripsOrphanIDsWhenStoreDisabled(t *testing.T) { valid := validOpenAIResponsesReasoningEncryptedContentForTest() body := []byte(`{"store":false,"input":[` + @@ -314,6 +328,95 @@ func TestSanitizeOpenAIResponsesReasoningEncryptedContentForMeta_PreservesRecogn } } +func TestSanitizeOpenAIResponsesReasoningEncryptedContentForMeta_StripsRouterSuffixed(t *testing.T) { + native := validMetaReasoningEncryptedContentForTest() + routed := routedMetaReasoningEncryptedContentForTest() + body := []byte(`{"store":false,"input":[` + + `{"type":"reasoning","summary":[],"encrypted_content":"` + native + `"},` + + `{"type":"reasoning","summary":[],"encrypted_content":"` + routed + `"}` + + `]}`) + + got := sanitizeOpenAIResponsesReasoningEncryptedContentForMeta(context.Background(), "test", body) + + // The native Meta upstream is fail-closed on the router suffix: the + // unsuffixed envelope survives while the routed form is stripped. + if gotSig := gjson.GetBytes(got, "input.0.encrypted_content").String(); gotSig != native { + t.Fatalf("native Meta encrypted_content not preserved; body=%s", got) + } + if gjson.GetBytes(got, "input.1.encrypted_content").Exists() { + t.Fatalf("router-suffixed Meta encrypted_content should be stripped on native Meta target: %s", got) + } +} + +func TestSanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel_RoutedMetaTarget(t *testing.T) { + native := validMetaReasoningEncryptedContentForTest() + routed := routedMetaReasoningEncryptedContentForTest() + gptSig := validOpenAIResponsesReasoningEncryptedContentForTest() + const unknownSig = "opaque-encrypted-reasoning-token-xyz" + buildBody := func() []byte { + return []byte(`{"store":false,"input":[` + + `{"type":"reasoning","summary":[],"encrypted_content":"` + native + `"},` + + `{"type":"reasoning","summary":[],"encrypted_content":"` + routed + `"},` + + `{"type":"reasoning","summary":[],"encrypted_content":"` + gptSig + `"},` + + `{"type":"reasoning","summary":[],"encrypted_content":"` + unknownSig + `"},` + + `{"type":"reasoning","summary":[],"encrypted_content":"` + testClaudeCAISSample + `"}` + + `]}`) + } + assertRoutedTarget := func(t *testing.T, got []byte) { + t.Helper() + if gotSig := gjson.GetBytes(got, "input.0.encrypted_content").String(); gotSig != native { + t.Fatalf("native Meta encrypted_content not preserved; body=%s", got) + } + if gotSig := gjson.GetBytes(got, "input.1.encrypted_content").String(); gotSig != routed { + t.Fatalf("routed Meta encrypted_content = %q, want verbatim; body=%s", gotSig, got) + } + for index, item := range gjson.GetBytes(got, "input").Array() { + if index < 2 { + continue + } + if item.Get("encrypted_content").Exists() { + t.Fatalf("input[%d] foreign encrypted_content should be stripped on Meta target; body=%s", index, got) + } + } + } + + // A Meta-family model name enables routed replay regardless of the + // compat flag: an OpenRouter codex key without is-compat must still + // replay Muse envelopes. + for _, isCompat := range []bool{false, true} { + got := sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(context.Background(), "test", buildBody(), isCompat, "meta/muse-wonder") + assertRoutedTarget(t, got) + } + + // Namespaced router model IDs resolve to their Meta tail segment. + got := sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(context.Background(), "test", buildBody(), false, "openrouter/meta/muse-wonder") + assertRoutedTarget(t, got) +} + +func TestSanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel_StripsMetaForNonMetaTargets(t *testing.T) { + native := validMetaReasoningEncryptedContentForTest() + routed := routedMetaReasoningEncryptedContentForTest() + body := []byte(`{"store":false,"input":[` + + `{"type":"reasoning","summary":[],"encrypted_content":"` + native + `"},` + + `{"type":"reasoning","summary":[],"encrypted_content":"` + routed + `"}` + + `]}`) + + for _, tc := range []struct { + model string + isCompat bool + }{ + {"gpt-5.4", false}, + {"deepseek-v4", true}, + } { + got := sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(context.Background(), "test", body, tc.isCompat, tc.model) + for index, item := range gjson.GetBytes(got, "input").Array() { + if item.Get("encrypted_content").Exists() { + t.Fatalf("model=%s isCompat=%v: input[%d] Meta encrypted_content should be stripped; body=%s", tc.model, tc.isCompat, index, got) + } + } + } +} + func BenchmarkSanitizeOpenAIResponsesReasoningEncryptedContentLargeNoopPayload(b *testing.B) { body := []byte(`{"store":false,"input":[{"type":"message","role":"user","content":"` + strings.Repeat("x", 8<<20) + `"}]}`) b.ReportAllocs() diff --git a/internal/signature/meta_validation.go b/internal/signature/meta_validation.go index f53eb470431..fe75192e069 100644 --- a/internal/signature/meta_validation.go +++ b/internal/signature/meta_validation.go @@ -16,6 +16,10 @@ const MaxMetaReasoningSignatureLen = 32 * 1024 * 1024 const ( metaReasoningSignatureOverheadLen = 45 metaReasoningSignatureMinPayload = 1024 + // maxMetaRouterSuffixLen bounds the dot-delimited suffix a router such as + // OpenRouter appends after the native envelope. Observed values are well + // under this bound. + maxMetaRouterSuffixLen = 128 ) // metaReasoningSignatureHeader is the fixed five-byte prefix observed on every @@ -37,6 +41,10 @@ type MetaReasoningSignatureInfo struct { // is the power-of-two padded region, not a proven plaintext length. PayloadLen int BucketExponent int + // RouterSuffix is the dot-delimited suffix a router such as OpenRouter + // appends after the native envelope; empty for native values. Replay must + // forward the full value verbatim; never strip the suffix. + RouterSuffix string } func IsValidMetaReasoningSignature(rawSignature string) bool { @@ -47,7 +55,11 @@ func IsValidMetaReasoningSignature(rawSignature string) bool { // InspectMetaReasoningSignature validates the outer envelope shape observed // for Meta Muse reasoning encrypted_content: canonical unpadded base64url, // the fixed five-byte header above, and a decoded length of exactly -// 45 + 1024*2^k bytes. This is only a transport-shape check; it does not +// 45 + 1024*2^k bytes. A router such as OpenRouter may serve the same model +// family and append one "." plus a short alphanumeric routing suffix to the +// native value; that suffix is validated here and reported via +// MetaReasoningSignatureInfo.RouterSuffix, and the full value must be +// replayed verbatim. This is only a transport-shape check; it does not // prove decryptability or upstream replay acceptance. func InspectMetaReasoningSignature(rawSignature string) (*MetaReasoningSignatureInfo, error) { sig := strings.TrimSpace(rawSignature) @@ -57,6 +69,33 @@ func InspectMetaReasoningSignature(rawSignature string) (*MetaReasoningSignature if len(sig) > MaxMetaReasoningSignatureLen { return nil, fmt.Errorf("Meta reasoning signature exceeds maximum length (%d bytes)", MaxMetaReasoningSignatureLen) } + // Reject other providers' signatures before scanning for a router suffix. + if !strings.HasPrefix(sig, "Q-PaDg") { + return nil, fmt.Errorf("invalid Meta reasoning signature: expected Q-PaDg prefix") + } + native := sig + routerSuffix := "" + if head, tail, ok := strings.Cut(sig, "."); ok { + // The native alphabet never contains '.', so the first dot is an + // unambiguous boundary. The suffix is restricted to the observed + // router form: a single run of alphanumeric characters. + if !isValidMetaRouterSuffix(tail) { + return nil, fmt.Errorf("invalid Meta reasoning signature: router suffix must be 1-%d alphanumeric characters", maxMetaRouterSuffixLen) + } + native = head + routerSuffix = tail + } + info, err := inspectNativeMetaReasoningSignature(native) + if err != nil { + return nil, err + } + info.RouterSuffix = routerSuffix + return info, nil +} + +// inspectNativeMetaReasoningSignature validates the native (unsuffixed) +// envelope shape described on InspectMetaReasoningSignature. +func inspectNativeMetaReasoningSignature(sig string) (*MetaReasoningSignatureInfo, error) { // The literal prefix is the cheapest discriminator: every observed value // starts with "Q-PaDg", and probing this validator is on the hot path for // signatures of every provider. @@ -116,3 +155,20 @@ func firstInvalidMetaReasoningSignatureChar(sig string) (int, rune, bool) { } return 0, 0, false } + +// metaRouterSuffixCharSet is the alphanumeric-only alphabet observed on +// router-appended suffixes; base64url's '-'/'_' and any second '.' are all +// rejected so the suffix can never be mistaken for more envelope payload. +var metaRouterSuffixCharSet = base64AlphabetSet("") + +func isValidMetaRouterSuffix(suffix string) bool { + if len(suffix) == 0 || len(suffix) > maxMetaRouterSuffixLen { + return false + } + for index := 0; index < len(suffix); index++ { + if !metaRouterSuffixCharSet[suffix[index]] { + return false + } + } + return true +} diff --git a/internal/signature/meta_validation_test.go b/internal/signature/meta_validation_test.go index 52a2fab5b3c..f850239367f 100644 --- a/internal/signature/meta_validation_test.go +++ b/internal/signature/meta_validation_test.go @@ -26,6 +26,12 @@ func testMetaReasoningSignatureBytes(payloadLen int, mutate func([]byte)) []byte return decoded } +// testMetaRouterSuffix builds a synthetic router suffix in the observed +// OpenRouter shape: a single run of 67 alphanumeric characters. +func testMetaRouterSuffix() string { + return strings.Repeat("Ab3", 23)[:67] +} + func TestInspectMetaReasoningSignature_ValidBuckets(t *testing.T) { for _, tc := range []struct { payload int @@ -93,6 +99,72 @@ func TestInspectMetaReasoningSignature_RejectsMalformed(t *testing.T) { } } +func TestInspectMetaReasoningSignature_RouterSuffixed(t *testing.T) { + native := testMetaReasoningSignature(2048) + suffix := testMetaRouterSuffix() + routed := native + "." + suffix + + info, err := InspectMetaReasoningSignature(routed) + if err != nil { + t.Fatalf("InspectMetaReasoningSignature(routed) error = %v", err) + } + if info.RouterSuffix != suffix { + t.Fatalf("RouterSuffix = %q, want %q", info.RouterSuffix, suffix) + } + nativeInfo, err := InspectMetaReasoningSignature(native) + if err != nil { + t.Fatalf("InspectMetaReasoningSignature(native) error = %v", err) + } + if nativeInfo.RouterSuffix != "" { + t.Fatalf("native RouterSuffix = %q, want empty", nativeInfo.RouterSuffix) + } + if info.DecodedLen != nativeInfo.DecodedLen || info.PayloadLen != nativeInfo.PayloadLen || info.BucketExponent != nativeInfo.BucketExponent { + t.Fatalf("routed info = %+v, want same envelope fields as native %+v", info, nativeInfo) + } + if got := DetectSignatureProvider(routed); got != SignatureProviderMeta { + t.Fatalf("DetectSignatureProvider(routed) = %q, want %q", got, SignatureProviderMeta) + } + if got, ok := CompatibleSignatureForProvider(SignatureProviderMeta, routed); !ok || got != routed { + t.Fatalf("CompatibleSignatureForProvider(Meta, routed) = (%q..., %v), want verbatim routed value", got[:16], ok) + } + + // The routed form is still a foreign envelope to non-Meta targets. + decision := DecideSignatureCompatibility(SignatureProviderGPT, routed, SignatureBlockKindUnknown) + if decision.Compatible || decision.DetectedProvider != SignatureProviderMeta || decision.Action != SignatureActionDropBlock { + t.Fatalf("GPT target decision for routed = %+v, want detected Meta + drop_block", decision) + } +} + +func TestInspectMetaReasoningSignature_RejectsMalformedRouterSuffix(t *testing.T) { + native := testMetaReasoningSignature(1024) + suffix := testMetaRouterSuffix() + encode := func(payloadLen int, mutate func([]byte)) string { + return base64.RawURLEncoding.EncodeToString(testMetaReasoningSignatureBytes(payloadLen, mutate)) + } + cases := map[string]string{ + "trailing dot": native + ".", + "oversized suffix": native + "." + strings.Repeat("a", maxMetaRouterSuffixLen+1), + "suffix with dash": native + "." + suffix[:60] + "-" + suffix[61:], + "suffix with underscore": native + "." + suffix[:60] + "_" + suffix[61:], + "suffix with padding": native + "." + suffix[:60] + "=" + suffix[61:], + "suffix with plus": native + "." + suffix[:60] + "+" + suffix[61:], + "suffix with slash": native + "." + suffix[:60] + "/" + suffix[61:], + "suffix with space": native + "." + suffix[:60] + " " + suffix[61:], + "second dot": native + "." + suffix[:60] + "." + suffix[61:], + "bad native header": encode(1024, func(b []byte) { b[2] ^= 0xff }) + "." + suffix, + "non-bucket native": encode(1025, nil) + "." + suffix, + "suffix only": "." + suffix, + } + for name, sig := range cases { + if _, err := InspectMetaReasoningSignature(sig); err == nil { + t.Errorf("%s: InspectMetaReasoningSignature unexpectedly succeeded", name) + } + if got := DetectSignatureProvider(sig); got == SignatureProviderMeta { + t.Errorf("%s: DetectSignatureProvider = %q, must not claim Meta", name, got) + } + } +} + func TestMetaSignatureCompatibility(t *testing.T) { sig := testMetaReasoningSignature(2048) diff --git a/internal/signature/provider_compatibility.go b/internal/signature/provider_compatibility.go index 67ae60e7985..311d85daf81 100644 --- a/internal/signature/provider_compatibility.go +++ b/internal/signature/provider_compatibility.go @@ -23,8 +23,10 @@ const ( // SignatureProviderSWE represents Cognition's SWE model family emitting sealed.v1 envelopes. SignatureProviderSWE SignatureProvider = "swe" // SignatureProviderMeta is identified by the observed Meta Muse envelope: - // a fixed five-byte header plus a power-of-two padded payload length. See - // meta_validation.go for the capture basis and its limits. + // a fixed five-byte header plus a power-of-two padded payload length, + // optionally followed by a router-appended (e.g. OpenRouter) dot suffix + // that replays verbatim. See meta_validation.go for the capture basis and + // its limits. SignatureProviderMeta SignatureProvider = "meta" ) diff --git a/internal/translator/codex/claude/codex_claude_request_test.go b/internal/translator/codex/claude/codex_claude_request_test.go index efffcd3fb83..9b5211cf10b 100644 --- a/internal/translator/codex/claude/codex_claude_request_test.go +++ b/internal/translator/codex/claude/codex_claude_request_test.go @@ -753,6 +753,29 @@ func TestConvertClaudeRequestToCodex_IgnoresMetaSignatureForNonMetaTargets(t *te } } +func TestConvertClaudeRequestToCodex_AssistantRoutedMetaSignatureToReasoningItem(t *testing.T) { + // OpenRouter serves Meta Muse models and appends a dot-delimited + // alphanumeric suffix to the native envelope; the full value must replay + // verbatim. + signature := testMetaClaudeSignature() + "." + strings.Repeat("Ab3", 23)[:67] + payload := []byte(`{"model":"meta/muse-wonder","messages":[{"role":"assistant","content":[{"type":"thinking","thinking":"summary","signature":""},{"type":"text","text":"answer"}]},{"role":"user","content":"next"}]}`) + payload, _ = sjson.SetBytes(payload, "messages.0.content.0.signature", signature) + + out, _ := ConvertClaudeRequestToCodex("meta/muse-wonder", payload, false) + reasoning := gjson.GetBytes(out, "input.0") + if reasoning.Get("type").String() != "reasoning" { + t.Fatalf("input.0 type = %q, want reasoning; output=%s", reasoning.Get("type").String(), out) + } + if got := reasoning.Get("encrypted_content").String(); got != signature { + t.Fatalf("encrypted_content = %q, want routed Meta signature verbatim", got) + } + + out, _ = ConvertClaudeRequestToCodex("gpt-5.4", payload, false) + if got := countRequestInputItemsByType(out, "reasoning"); got != 0 { + t.Fatalf("got %d reasoning items for non-Meta target, want 0; output=%s", got, out) + } +} + func TestConvertClaudeRequestToCodex_IgnoresGrokSignatureForNonGrokTargets(t *testing.T) { signature := "HmlYdr2aCAqCYP/m9mr8PS6KOsdMs72FGDigmydR+Jsmuv8KX97yWPlbOwmXJgWn0CbHaCacdQD3+n5EvpgLfPNmafS3kdICBjRuDf4bzHy7uBiUhNVhqPtp/ee1y9q4imPE4LYgD1VZ4J+bp9mTeqA1+nC9Oue58CiNEMV9SVaGenCD+aBnVuSTzQhD32Y+68i6HLJW0Dx6ifaRfb8hxYtA/sPM+/FTvAMW11nRho5a2BBSkpnzfqqAz/e/vGJ77/bygpXM823QA9wL9i0X" payload := []byte(`{"messages":[{"role":"assistant","content":[{"type":"thinking","thinking":"summary","signature":""},{"type":"text","text":"answer"}]},{"role":"user","content":"next"}]}`) diff --git a/internal/translator/codex/gemini/codex_gemini_request_test.go b/internal/translator/codex/gemini/codex_gemini_request_test.go index 1ab0ff7ba9f..3b6d9996632 100644 --- a/internal/translator/codex/gemini/codex_gemini_request_test.go +++ b/internal/translator/codex/gemini/codex_gemini_request_test.go @@ -3,6 +3,7 @@ package gemini import ( "encoding/base64" "fmt" + "strings" "testing" "github.com/tidwall/gjson" @@ -180,6 +181,8 @@ func testGeminiGPTThoughtSignature() string { func TestConvertGeminiRequestToCodex_NormalizesPrefixedAndCarrierSignatures(t *testing.T) { gptSignature := testGeminiGPTThoughtSignature() metaSignature := testGeminiMetaThoughtSignature() + // OpenRouter-appended suffix shape: one '.' plus 67 alphanumeric chars. + routedMetaSignature := metaSignature + "." + strings.Repeat("Ab3", 23)[:67] cases := []struct { name string model string @@ -191,6 +194,8 @@ func TestConvertGeminiRequestToCodex_NormalizesPrefixedAndCarrierSignatures(t *t {"carrier wrapped gpt prefixed", "gpt-5.1-codex", encodeGeminiCodexThoughtSignature("gpt#" + gptSignature), gptSignature, true}, {"carrier wrapped meta", "muse-spark-1.3", encodeGeminiCodexThoughtSignature(metaSignature), metaSignature, true}, {"carrier wrapped meta to gpt target", "gpt-5.1-codex", encodeGeminiCodexThoughtSignature(metaSignature), "", false}, + {"carrier wrapped routed meta", "meta/muse-wonder", encodeGeminiCodexThoughtSignature(routedMetaSignature), routedMetaSignature, true}, + {"carrier wrapped routed meta to gpt target", "gpt-5.1-codex", encodeGeminiCodexThoughtSignature(routedMetaSignature), "", false}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { From 772df04b30d5179fa7f5075ddea46485942e5eaa Mon Sep 17 00:00:00 2001 From: sususu Date: Fri, 16 Oct 2026 12:35:04 +0800 Subject: [PATCH 05/27] feat(translator): relay reasoning signatures through the Interactions protocol The Codex-format Interactions translator dropped encrypted_content from responses and ignored client-echoed thought signatures, so Interactions clients could never replay reasoning to Meta Muse (native or OpenRouter) or any other Codex-format upstream. - Emit encrypted_content verbatim as the thought step signature (non-stream) and as a thought_signature step delta (stream), deduplicating repeated reasoning done events - Restore echoed signatures as reasoning encrypted_content only when the detected family matches the target model; request encrypted reasoning via include - Share the family-match/normalization rule with the Gemini translator via translatorcommon.CodexReplaySignatureForTarget Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../codex/gemini/codex_gemini_request.go | 35 +-- .../interactions_codex_request.go | 91 +++++-- .../interactions_codex_response.go | 47 +++- .../interactions/interactions_codex_test.go | 245 +++++++++++++++++- .../common/codex_replay_signature.go | 43 +++ .../common/codex_replay_signature_test.go | 65 +++++ 6 files changed, 462 insertions(+), 64 deletions(-) create mode 100644 internal/translator/common/codex_replay_signature.go create mode 100644 internal/translator/common/codex_replay_signature_test.go diff --git a/internal/translator/codex/gemini/codex_gemini_request.go b/internal/translator/codex/gemini/codex_gemini_request.go index 2bbcfb35964..a1f4749e85b 100644 --- a/internal/translator/codex/gemini/codex_gemini_request.go +++ b/internal/translator/codex/gemini/codex_gemini_request.go @@ -11,7 +11,6 @@ import ( "strconv" "strings" - sigcompat "github.com/router-for-me/CLIProxyAPI/v8/internal/signature" "github.com/router-for-me/CLIProxyAPI/v8/internal/thinking" translatorcommon "github.com/router-for-me/CLIProxyAPI/v8/internal/translator/common" "github.com/router-for-me/CLIProxyAPI/v8/internal/util" @@ -396,37 +395,11 @@ func decodeGeminiCodexThoughtSignature(value string) string { // normalizedGeminiThoughtSignatureForCodexTarget unwraps and normalizes a // client-echoed thought signature for the Codex-format upstream serving // modelName, returning the exact value that may be written as reasoning -// encrypted_content. Positively detected families must match the target -// family exactly (an unmapped target defaults to the Codex GPT family, -// mirroring this translator's purpose) and go through the shared -// compatibility normalization, so cache-prefixed values such as "gpt#..." -// are stripped to their native payload instead of being forwarded verbatim. -// Grok has no detectable envelope, so it falls back to its shape inspector -// keyed on the target. +// encrypted_content. The carrier magic written by +// encodeGeminiCodexThoughtSignature is stripped first; the family match and +// normalization rules live in translatorcommon.CodexReplaySignatureForTarget. func normalizedGeminiThoughtSignatureForCodexTarget(modelName, rawSignature string) (string, bool) { - rawSignature = decodeGeminiCodexThoughtSignature(rawSignature) - if rawSignature == "" { - return "", false - } - target := sigcompat.SignatureProviderFromModelName(thinking.ParseSuffix(modelName).ModelName) - detected := sigcompat.DetectSignatureProvider(rawSignature) - if detected != sigcompat.SignatureProviderUnknown { - effectiveTarget := target - if effectiveTarget == sigcompat.SignatureProviderUnknown { - effectiveTarget = sigcompat.SignatureProviderGPT - } - if detected != effectiveTarget { - return "", false - } - normalized, ok := sigcompat.CompatibleSignatureForProvider(effectiveTarget, rawSignature) - return normalized, ok && normalized != "" - } - if target == sigcompat.SignatureProviderGrok { - if _, err := sigcompat.InspectGrokEncryptedContent(rawSignature); err == nil { - return rawSignature, true - } - } - return "", false + return translatorcommon.CodexReplaySignatureForTarget(modelName, decodeGeminiCodexThoughtSignature(rawSignature)) } func setCodexToolChoiceFromGeminiToolConfig(out []byte, functionCallingConfig gjson.Result) []byte { diff --git a/internal/translator/codex/interactions/interactions_codex_request.go b/internal/translator/codex/interactions/interactions_codex_request.go index c6f5a0b2cc7..4fcd8344061 100644 --- a/internal/translator/codex/interactions/interactions_codex_request.go +++ b/internal/translator/codex/interactions/interactions_codex_request.go @@ -28,10 +28,11 @@ func convertInteractionsRequestToCodex(modelName string, inputRawJSON []byte, st out = copyInteractionsSystemToCodex(out, root) out = copyInteractionsGenerationConfigToCodex(out, root) inputItems := translatorcommon.NewRawArrayItems(root.Get("input.#").Int()) - errInput := appendInteractionsInputToCodex(&inputItems, root.Get("input")) + errInput := appendInteractionsInputToCodex(&inputItems, root.Get("input"), modelName) out = translatorcommon.SetRawArrayItems(out, "input", inputItems) out = copyInteractionsToolsToCodex(out, root) out = copyInteractionsCodexTopLevel(out, root) + out = ensureInteractionsCodexEncryptedContentInclude(out) return out, errInput } @@ -190,7 +191,7 @@ func interactionsCodexReasoningSummary(cfg gjson.Result) string { // appendInteractionsInputToCodex adds the input steps and reports a user turn that // was left with nothing to send because of an attachment Codex cannot carry. -func appendInteractionsInputToCodex(items *[][]byte, input gjson.Result) error { +func appendInteractionsInputToCodex(items *[][]byte, input gjson.Result, modelName string) error { if !input.Exists() { return nil } @@ -201,17 +202,17 @@ func appendInteractionsInputToCodex(items *[][]byte, input gjson.Result) error { var run translatorcommon.UserRun if input.IsArray() { input.ForEach(func(_, step gjson.Result) bool { - appendInteractionsStepToCodex(items, step, "user", &run) + appendInteractionsStepToCodex(items, step, "user", &run, modelName) return true }) } else if steps := input.Get("steps"); steps.Exists() && steps.IsArray() { defaultRole := interactionsCodexStepRole(input, "user") steps.ForEach(func(_, step gjson.Result) bool { - appendInteractionsStepToCodex(items, step, defaultRole, &run) + appendInteractionsStepToCodex(items, step, defaultRole, &run, modelName) return true }) } else { - appendInteractionsStepToCodex(items, input, "user", &run) + appendInteractionsStepToCodex(items, input, "user", &run, modelName) } run.End() return run.Err() @@ -227,7 +228,7 @@ func interactionsCodexUserRun(run *translatorcommon.UserRun, role string) *trans return nil } -func appendInteractionsStepToCodex(items *[][]byte, step gjson.Result, defaultRole string, run *translatorcommon.UserRun) { +func appendInteractionsStepToCodex(items *[][]byte, step gjson.Result, defaultRole string, run *translatorcommon.UserRun, modelName string) { if step.Type == gjson.String { appendInteractionsRoleTextToCodex(items, defaultRole, step.String(), run) return @@ -235,7 +236,7 @@ func appendInteractionsStepToCodex(items *[][]byte, step gjson.Result, defaultRo if steps := step.Get("steps"); steps.Exists() && steps.IsArray() { role := interactionsCodexStepRole(step, defaultRole) steps.ForEach(func(_, nested gjson.Result) bool { - appendInteractionsStepToCodex(items, nested, role, run) + appendInteractionsStepToCodex(items, nested, role, run, modelName) return true }) return @@ -254,7 +255,7 @@ func appendInteractionsStepToCodex(items *[][]byte, step gjson.Result, defaultRo appendInteractionsContentToCodexItem(items, step.Get("content"), "assistant", run) case "thought", "reasoning": run.End() - appendInteractionsThoughtToCodex(items, step) + appendInteractionsThoughtToCodex(items, step, modelName) case "user_input", "message", "": role := interactionsCodexStepRole(step, defaultRole) if content := step.Get("content"); content.Exists() { @@ -426,19 +427,79 @@ func setInteractionsCodexRawIfDifferent(out []byte, path string, value gjson.Res return updated } -func appendInteractionsThoughtToCodex(items *[][]byte, step gjson.Result) { +// ensureInteractionsCodexEncryptedContentInclude makes the outbound request +// ask the upstream for encrypted reasoning content so a signature can come +// back for replay. +func ensureInteractionsCodexEncryptedContentInclude(out []byte) []byte { + const reasoningEncryptedContent = "reasoning.encrypted_content" + include := gjson.GetBytes(out, "include") + if !include.Exists() { + out, _ = sjson.SetRawBytes(out, "include", []byte(`["reasoning.encrypted_content"]`)) + return out + } + if !include.IsArray() { + return out + } + for _, value := range include.Array() { + if value.Type == gjson.String && value.String() == reasoningEncryptedContent { + return out + } + } + out, _ = sjson.SetBytes(out, "include.-1", reasoningEncryptedContent) + return out +} + +// appendInteractionsThoughtToCodex adds a thought step. A client-echoed +// signature that CodexReplaySignatureForTarget accepts for this request's +// target model is restored as Responses reasoning encrypted_content, with +// the thought text carried as a summary; identical signatures already +// emitted in this request are replayed only once. The step id is +// deliberately not copied onto replay items: replay is stateless and the +// executor strips orphan ids. Every other thought step is dropped entirely: +// a reasoning item without accepted encrypted_content carries no replayable +// state, and the legacy text fallback shape is rejected by both the native +// Meta upstream (missing summary) and routers (content expected array). +func appendInteractionsThoughtToCodex(items *[][]byte, step gjson.Result, modelName string) { text := interactionsCodexContentText(step.Get("content")) if text == "" { text = step.Get("text").String() } - item := []byte(`{"type":"reasoning"}`) - if text != "" { - item, _ = sjson.SetBytes(item, "content", text) + if raw := interactionsThoughtStepSignature(step); raw != "" { + if normalized, ok := translatorcommon.CodexReplaySignatureForTarget(modelName, raw); ok && !interactionsCodexSignatureAlreadyEmitted(*items, normalized) { + item := []byte(`{"type":"reasoning","encrypted_content":"","summary":[]}`) + item, _ = sjson.SetBytes(item, "encrypted_content", normalized) + if text != "" { + summaryPart := []byte(`{"type":"summary_text","text":""}`) + summaryPart, _ = sjson.SetBytes(summaryPart, "text", text) + item, _ = sjson.SetRawBytes(item, "summary", translatorcommon.JoinRawArray([][]byte{summaryPart})) + } + *items = append(*items, item) + } } - if id := step.Get("id"); id.Exists() { - item, _ = sjson.SetBytes(item, "id", id.String()) +} + +// interactionsThoughtStepSignature extracts a thought step's signature under +// any of the field spellings seen on the wire. +func interactionsThoughtStepSignature(step gjson.Result) string { + for _, path := range []string{"signature", "thought_signature", "thoughtSignature"} { + if signature := strings.TrimSpace(step.Get(path).String()); signature != "" { + return signature + } } - *items = append(*items, item) + return "" +} + +// interactionsCodexSignatureAlreadyEmitted reports whether an identical +// normalized signature was already written as encrypted_content in this +// request, so a repeated echo is only replayed once. +func interactionsCodexSignatureAlreadyEmitted(items [][]byte, normalized string) bool { + for _, raw := range items { + item := gjson.ParseBytes(raw) + if item.Get("type").String() == "reasoning" && item.Get("encrypted_content").String() == normalized { + return true + } + } + return false } func appendInteractionsTextToCodex(items *[][]byte, role, text string) { diff --git a/internal/translator/codex/interactions/interactions_codex_response.go b/internal/translator/codex/interactions/interactions_codex_response.go index 1f11276533e..34e9845bf4d 100644 --- a/internal/translator/codex/interactions/interactions_codex_response.go +++ b/internal/translator/codex/interactions/interactions_codex_response.go @@ -3,6 +3,7 @@ package interactions import ( "bytes" "context" + "crypto/sha256" "fmt" "strings" "time" @@ -26,6 +27,9 @@ type codexToInteractionsStreamState struct { HasOutputText bool FunctionCallName string FunctionCallID string + // SeenReasoningSignatures dedupes repeated reasoning output_item.done + // events so one signature is emitted as a thought_signature delta once. + SeenReasoningSignatures map[[32]byte]bool } func ConvertCodexResponseToInteractions(ctx context.Context, modelName string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) [][]byte { @@ -332,11 +336,18 @@ func buildCodexMessageItemToInteractions(item gjson.Result) []byte { func buildCodexReasoningItemToInteractions(item gjson.Result) []byte { text := codexReasoningText(item) - if text == "" { + sig := strings.TrimSpace(item.Get("encrypted_content").String()) + if text == "" && sig == "" { return nil } - step := []byte(`{"type":"thought","content":[{"type":"text","text":""}]}`) - step, _ = sjson.SetBytes(step, "content.0.text", text) + step := []byte(`{"type":"thought"}`) + if sig != "" { + step, _ = sjson.SetBytes(step, "signature", sig) + } + if text != "" { + step, _ = sjson.SetRawBytes(step, "content", []byte(`[{"type":"text","text":""}]`)) + step, _ = sjson.SetBytes(step, "content.0.text", text) + } return step } @@ -407,14 +418,30 @@ func appendCodexMessageItemToInteractionsStream(out [][]byte, st *codexToInterac func appendCodexReasoningItemToInteractionsStream(out [][]byte, st *codexToInteractionsStreamState, item gjson.Result) [][]byte { text := codexReasoningText(item) - if text == "" { - return out + sig := strings.TrimSpace(item.Get("encrypted_content").String()) + if text != "" { + out = ensureCodexInteractionsStep(out, st, "thought", item) + delta := []byte(`{"index":0,"delta":{"content":{"text":"","type":"text"},"type":"thought_summary"},"event_type":"step.delta"}`) + delta, _ = sjson.SetBytes(delta, "index", st.ActiveStepIndex) + delta, _ = sjson.SetBytes(delta, "delta.content.text", text) + out = append(out, translatorcommon.SSEEventData("step.delta", delta)) + } + if sig != "" { + key := sha256.Sum256([]byte(item.Get("id").String() + "\x00" + sig)) + if st.SeenReasoningSignatures[key] { + return out + } + if st.SeenReasoningSignatures == nil { + st.SeenReasoningSignatures = make(map[[32]byte]bool) + } + st.SeenReasoningSignatures[key] = true + out = ensureCodexInteractionsStep(out, st, "thought", item) + signatureDelta := []byte(`{"index":0,"delta":{"signature":"","type":"thought_signature"},"event_type":"step.delta"}`) + signatureDelta, _ = sjson.SetBytes(signatureDelta, "index", st.ActiveStepIndex) + signatureDelta, _ = sjson.SetBytes(signatureDelta, "delta.signature", sig) + out = append(out, translatorcommon.SSEEventData("step.delta", signatureDelta)) } - out = ensureCodexInteractionsStep(out, st, "thought", item) - delta := []byte(`{"index":0,"delta":{"content":{"text":"","type":"text"},"type":"thought_summary"},"event_type":"step.delta"}`) - delta, _ = sjson.SetBytes(delta, "index", st.ActiveStepIndex) - delta, _ = sjson.SetBytes(delta, "delta.content.text", text) - return append(out, translatorcommon.SSEEventData("step.delta", delta)) + return out } func appendCodexFunctionCallItemToInteractionsStream(out [][]byte, st *codexToInteractionsStreamState, item gjson.Result) [][]byte { diff --git a/internal/translator/codex/interactions/interactions_codex_test.go b/internal/translator/codex/interactions/interactions_codex_test.go index 67581a1046c..6c8fd4db378 100644 --- a/internal/translator/codex/interactions/interactions_codex_test.go +++ b/internal/translator/codex/interactions/interactions_codex_test.go @@ -3,6 +3,7 @@ package interactions import ( "bytes" "context" + "encoding/base64" "strings" "testing" @@ -17,17 +18,16 @@ func TestConvertInteractionsRequestToCodexWithToolMessagesDirect(t *testing.T) { if got := gjson.GetBytes(out, "input.0.content.0.text").String(); got != "hi" { t.Fatalf("input.0.content.0.text = %q, want hi. Output: %s", got, string(out)) } - if got := gjson.GetBytes(out, "input.1.type").String(); got != "reasoning" { - t.Fatalf("input.1.type = %q, want reasoning. Output: %s", got, string(out)) + // The signatureless thought step is dropped entirely: a reasoning item + // without replayable encrypted_content is rejected by both upstreams. + if got := gjson.GetBytes(out, "input.1.type").String(); got != "function_call" { + t.Fatalf("input.1.type = %q, want function_call. Output: %s", got, string(out)) } - if got := gjson.GetBytes(out, "input.2.type").String(); got != "function_call" { - t.Fatalf("input.2.type = %q, want function_call. Output: %s", got, string(out)) - } - if got := gjson.GetBytes(out, "input.2.call_id").String(); got != "call_1" { + if got := gjson.GetBytes(out, "input.1.call_id").String(); got != "call_1" { t.Fatalf("function_call call_id = %q, want call_1. Output: %s", got, string(out)) } - if got := gjson.GetBytes(out, "input.3.type").String(); got != "function_call_output" { - t.Fatalf("input.3.type = %q, want function_call_output. Output: %s", got, string(out)) + if got := gjson.GetBytes(out, "input.2.type").String(); got != "function_call_output" { + t.Fatalf("input.2.type = %q, want function_call_output. Output: %s", got, string(out)) } if got := gjson.GetBytes(out, "tools.0.name").String(); got != "lookup" { t.Fatalf("tools.0.name = %q, want lookup. Output: %s", got, string(out)) @@ -163,6 +163,235 @@ func TestConvertCodexResponseToInteractionsStreamCompletesAfterSteps(t *testing. } } +// testInteractionsMetaSignature builds a shape-valid synthetic Meta envelope; +// no captured signature is embedded. +func testInteractionsMetaSignature() string { + return "Q-PaDgE" + strings.Repeat("A", 1419) +} + +func testInteractionsGPTSignature() string { + decoded := make([]byte, 89) + decoded[0] = 0x80 + for i := 9; i < len(decoded); i++ { + decoded[i] = byte(i) + } + return base64.RawURLEncoding.EncodeToString(decoded) +} + +func findCodexInteractionsEventPayloads(events [][]byte, eventType string) []gjson.Result { + var out []gjson.Result + for _, event := range events { + for _, line := range bytes.Split(event, []byte("\n")) { + line = bytes.TrimSpace(line) + if !bytes.HasPrefix(line, []byte("data:")) { + continue + } + payload := bytes.TrimSpace(line[len("data:"):]) + if codexInteractionsEventName(codexInteractionsFrameEventName(event), payload) == eventType { + out = append(out, gjson.ParseBytes(payload)) + } + } + } + return out +} + +func TestConvertCodexResponseToInteractionsNonStream_ReasoningSignature(t *testing.T) { + metaSig := testInteractionsMetaSignature() + routedSig := metaSig + "." + strings.Repeat("Ab3", 23)[:67] + raw := []byte(`{"type":"response.completed","response":{"id":"resp_1","output":[` + + `{"type":"reasoning","summary":[{"type":"summary_text","text":"thinking hard"}],"encrypted_content":"` + metaSig + `"},` + + `{"type":"reasoning","summary":[],"encrypted_content":"` + routedSig + `"},` + + `{"type":"reasoning","summary":[]}` + + `]}}`) + + out := ConvertCodexResponseToInteractionsNonStream(context.Background(), "codex-test", nil, nil, raw, nil) + steps := gjson.GetBytes(out, "steps").Array() + if len(steps) != 2 { + t.Fatalf("steps len = %d, want 2 (empty reasoning emits no step). Output: %s", len(steps), string(out)) + } + if got := steps[0].Get("type").String(); got != "thought" { + t.Fatalf("steps.0.type = %q, want thought. Output: %s", got, string(out)) + } + if got := steps[0].Get("signature").String(); got != metaSig { + t.Fatalf("steps.0.signature = %q, want Meta signature verbatim. Output: %s", got, string(out)) + } + if got := steps[0].Get("content.0.text").String(); got != "thinking hard" { + t.Fatalf("steps.0.content.0.text = %q, want thinking hard. Output: %s", got, string(out)) + } + if got := steps[1].Get("signature").String(); got != routedSig { + t.Fatalf("steps.1.signature = %q, want routed Meta signature verbatim. Output: %s", got, string(out)) + } + if steps[1].Get("content").Exists() { + t.Fatalf("steps.1 should carry no content when there is no text. Output: %s", string(out)) + } +} + +func TestConvertCodexResponseToInteractionsStream_ReasoningSignature(t *testing.T) { + metaSig := testInteractionsMetaSignature() + var param any + var events [][]byte + for _, chunk := range [][]byte{ + []byte(`data: {"type":"response.created","response":{"id":"resp_1","model":"codex-test"}}`), + []byte(`data: {"type":"response.output_item.done","item":{"id":"rs_1","type":"reasoning","summary":[],"encrypted_content":"` + metaSig + `"}}`), + // A duplicated done event for the same item must not re-emit the signature. + []byte(`data: {"type":"response.output_item.done","item":{"id":"rs_1","type":"reasoning","summary":[],"encrypted_content":"` + metaSig + `"}}`), + []byte(`data: {"type":"response.output_item.done","item":{"type":"message","content":[{"type":"output_text","text":"answer"}]}}`), + } { + events = append(events, ConvertCodexResponseToInteractions(context.Background(), "codex-test", nil, nil, chunk, ¶m)...) + } + + starts := findCodexInteractionsEventPayloads(events, "step.start") + if len(starts) != 2 { + t.Fatalf("step.start count = %d, want thought + model_output. Events: %q", len(starts), events) + } + if got := starts[0].Get("step.type").String(); got != "thought" { + t.Fatalf("step.start[0] type = %q, want thought", got) + } + if got := starts[1].Get("step.type").String(); got != "model_output" { + t.Fatalf("step.start[1] type = %q, want model_output", got) + } + + signatureDeltas := 0 + for _, delta := range findCodexInteractionsEventPayloads(events, "step.delta") { + if delta.Get("delta.type").String() != "thought_signature" { + continue + } + signatureDeltas++ + if got := delta.Get("delta.signature").String(); got != metaSig { + t.Fatalf("thought_signature delta signature = %q, want Meta signature verbatim", got) + } + } + if signatureDeltas != 1 { + t.Fatalf("thought_signature delta count = %d, want 1 (duplicate suppressed)", signatureDeltas) + } + + names := strings.Join(codexInteractionsEventNames(events), ",") + want := "interaction.created,interaction.status_update,step.start,step.delta,step.stop,step.start,step.delta,step.stop" + if names != want { + t.Fatalf("events = %s, want %s", names, want) + } +} + +func TestConvertInteractionsRequestToCodex_RestoresThoughtSignature(t *testing.T) { + metaSig := testInteractionsMetaSignature() + routedSig := metaSig + "." + strings.Repeat("Ab3", 23)[:67] + for _, tc := range []struct { + name string + model string + sig string + }{ + {"native meta", "muse-spark-1.3", metaSig}, + {"namespaced router model", "meta/muse-wonder", metaSig}, + {"routed meta verbatim", "meta/muse-wonder", routedSig}, + } { + t.Run(tc.name, func(t *testing.T) { + payload := []byte(`{"model":"` + tc.model + `","input":[{"type":"thought","signature":"` + tc.sig + `","content":[{"type":"text","text":"thinking"}]}]}`) + out, _ := ConvertInteractionsRequestToCodex(tc.model, payload, false) + item := gjson.GetBytes(out, "input.0") + if got := item.Get("type").String(); got != "reasoning" { + t.Fatalf("input.0.type = %q, want reasoning. Output: %s", got, string(out)) + } + if got := item.Get("encrypted_content").String(); got != tc.sig { + t.Fatalf("encrypted_content = %q, want signature verbatim. Output: %s", got, string(out)) + } + if got := item.Get("summary.0.text").String(); got != "thinking" { + t.Fatalf("summary.0.text = %q, want thinking. Output: %s", got, string(out)) + } + if item.Get("content").Exists() { + t.Fatalf("replay item must not carry content. Output: %s", string(out)) + } + }) + } +} + +func TestConvertInteractionsRequestToCodex_ThoughtSignatureTargetMismatch(t *testing.T) { + metaSig := testInteractionsMetaSignature() + payload := []byte(`{"model":"gpt-5.4","input":[{"type":"thought","signature":"` + metaSig + `","content":[{"type":"text","text":"thinking"}],"id":"step_1"}]}`) + + // A foreign signature is never replayed and the thought step is dropped + // entirely rather than sent as an unsigned reasoning item. + out, _ := ConvertInteractionsRequestToCodex("gpt-5.4", payload, false) + if got := len(gjson.GetBytes(out, "input").Array()); got != 0 { + t.Fatalf("input len = %d, want 0 (mismatched thought dropped). Output: %s", got, string(out)) + } +} + +func TestConvertInteractionsRequestToCodex_DropsEmptyThoughtSteps(t *testing.T) { + metaSig := testInteractionsMetaSignature() + + // A rejected signature with no text emits no item at all: a bare + // reasoning item carries nothing replayable and is rejected upstream. + out, _ := ConvertInteractionsRequestToCodex("gpt-5.4", []byte(`{"input":[{"type":"user_input","content":[{"type":"text","text":"hi"}]},{"type":"thought","signature":"`+metaSig+`"}]}`), false) + if got := len(gjson.GetBytes(out, "input").Array()); got != 1 { + t.Fatalf("input len = %d, want 1 (thought step dropped). Output: %s", got, string(out)) + } + + // A signatureless, textless thought step emits no item. + out, _ = ConvertInteractionsRequestToCodex("muse-spark-1.3", []byte(`{"input":[{"type":"thought"}]}`), false) + if got := len(gjson.GetBytes(out, "input").Array()); got != 0 { + t.Fatalf("input len = %d, want 0. Output: %s", got, string(out)) + } + + // A duplicate accepted signature with no text emits nothing the second + // time either. + out, _ = ConvertInteractionsRequestToCodex("muse-spark-1.3", []byte(`{"input":[{"type":"thought","signature":"`+metaSig+`"},{"type":"thought","signature":"`+metaSig+`"}]}`), false) + if got := len(gjson.GetBytes(out, "input").Array()); got != 1 { + t.Fatalf("input len = %d, want 1 (duplicate without text dropped). Output: %s", got, string(out)) + } + + // A signatureless thought with text is dropped too, and the message/tool + // items around it keep their order. + out, _ = ConvertInteractionsRequestToCodex("muse-spark-1.3", []byte(`{"input":[{"type":"user_input","content":[{"type":"text","text":"hi"}]},{"type":"thought","content":[{"type":"text","text":"thinking"}]},{"type":"function_call","name":"lookup","call_id":"call_1","arguments":{"q":"x"}}]}`), false) + items := gjson.GetBytes(out, "input").Array() + if len(items) != 2 || items[0].Get("type").String() != "message" || items[1].Get("type").String() != "function_call" { + t.Fatalf("input = %s, want [message, function_call] with the thought dropped", items) + } +} + +func TestConvertInteractionsRequestToCodex_ThoughtSignatureVariants(t *testing.T) { + metaSig := testInteractionsMetaSignature() + gptSig := testInteractionsGPTSignature() + + // A cache-prefixed signature normalizes to its native payload. + out, _ := ConvertInteractionsRequestToCodex("gpt-5.4", []byte(`{"input":[{"type":"thought","signature":"gpt#`+gptSig+`"}]}`), false) + if got := gjson.GetBytes(out, "input.0.encrypted_content").String(); got != gptSig { + t.Fatalf("prefixed GPT signature should normalize to native payload. Output: %s", string(out)) + } + + // The snake_case thought_signature spelling is accepted too. + out, _ = ConvertInteractionsRequestToCodex("muse-spark-1.3", []byte(`{"input":[{"type":"thought","thought_signature":"`+metaSig+`"}]}`), false) + if got := gjson.GetBytes(out, "input.0.encrypted_content").String(); got != metaSig { + t.Fatalf("thought_signature field should be accepted. Output: %s", string(out)) + } + + // An identical signature replays once; the duplicate thought is dropped. + out, _ = ConvertInteractionsRequestToCodex("muse-spark-1.3", []byte(`{"input":[{"type":"thought","signature":"`+metaSig+`","content":[{"type":"text","text":"one"}]},{"type":"thought","signature":"`+metaSig+`","content":[{"type":"text","text":"two"}]}]}`), false) + if got := len(gjson.GetBytes(out, "input").Array()); got != 1 { + t.Fatalf("input len = %d, want 1 (duplicate dropped). Output: %s", got, string(out)) + } + if got := gjson.GetBytes(out, "input.0.encrypted_content").String(); got != metaSig { + t.Fatalf("first thought should carry the signature. Output: %s", string(out)) + } +} + +func TestConvertInteractionsRequestToCodex_IncludeEncryptedContent(t *testing.T) { + out, _ := ConvertInteractionsRequestToCodex("codex-test", []byte(`{"input":"hi"}`), false) + if got := gjson.GetBytes(out, "include.0").String(); got != "reasoning.encrypted_content" { + t.Fatalf("include.0 = %q, want reasoning.encrypted_content. Output: %s", got, string(out)) + } + + out, _ = ConvertInteractionsRequestToCodex("codex-test", []byte(`{"input":"hi","include":["web_search_call.action.sources"]}`), false) + include := gjson.GetBytes(out, "include").Array() + if len(include) != 2 || include[0].String() != "web_search_call.action.sources" || include[1].String() != "reasoning.encrypted_content" { + t.Fatalf("include = %s, want sources + reasoning.encrypted_content. Output: %s", include, string(out)) + } + + out, _ = ConvertInteractionsRequestToCodex("codex-test", []byte(`{"input":"hi","include":["reasoning.encrypted_content"]}`), false) + if include := gjson.GetBytes(out, "include").Array(); len(include) != 1 { + t.Fatalf("include = %s, want unchanged single entry. Output: %s", include, string(out)) + } +} + func findCodexInteractionsEventPayload(events [][]byte, eventType string) []byte { prefix := []byte("data:") for _, event := range events { diff --git a/internal/translator/common/codex_replay_signature.go b/internal/translator/common/codex_replay_signature.go new file mode 100644 index 00000000000..7e154bf3dba --- /dev/null +++ b/internal/translator/common/codex_replay_signature.go @@ -0,0 +1,43 @@ +package common + +import ( + "strings" + + sigcompat "github.com/router-for-me/CLIProxyAPI/v8/internal/signature" + "github.com/router-for-me/CLIProxyAPI/v8/internal/thinking" +) + +// CodexReplaySignatureForTarget returns the exact value that may be written as a +// Responses reasoning encrypted_content for the Codex-format upstream serving +// modelName, or false when rawSignature must not replay there. Positively +// detected families must match the target family exactly (an unmapped target +// defaults to the Codex GPT family, mirroring the Codex translators' purpose) +// and go through the shared compatibility normalization, so cache-prefixed +// values such as "gpt#..." are stripped to their native payload instead of +// being forwarded verbatim. Grok has no detectable envelope, so it falls back +// to its shape inspector keyed on the target. +func CodexReplaySignatureForTarget(modelName, rawSignature string) (string, bool) { + rawSignature = strings.TrimSpace(rawSignature) + if rawSignature == "" { + return "", false + } + target := sigcompat.SignatureProviderFromModelName(thinking.ParseSuffix(modelName).ModelName) + detected := sigcompat.DetectSignatureProvider(rawSignature) + if detected != sigcompat.SignatureProviderUnknown { + effectiveTarget := target + if effectiveTarget == sigcompat.SignatureProviderUnknown { + effectiveTarget = sigcompat.SignatureProviderGPT + } + if detected != effectiveTarget { + return "", false + } + normalized, ok := sigcompat.CompatibleSignatureForProvider(effectiveTarget, rawSignature) + return normalized, ok && normalized != "" + } + if target == sigcompat.SignatureProviderGrok { + if _, err := sigcompat.InspectGrokEncryptedContent(rawSignature); err == nil { + return rawSignature, true + } + } + return "", false +} diff --git a/internal/translator/common/codex_replay_signature_test.go b/internal/translator/common/codex_replay_signature_test.go new file mode 100644 index 00000000000..e5c50afcd3d --- /dev/null +++ b/internal/translator/common/codex_replay_signature_test.go @@ -0,0 +1,65 @@ +package common + +import ( + "encoding/base64" + "strings" + "testing" +) + +// testCodexReplayMetaSignature builds a shape-valid synthetic Meta envelope: +// "Q-PaDgE" pins the observed five-byte header, and 1426 unpadded base64url +// chars decode to 45 + 1024 bytes. No captured signature is embedded. +func testCodexReplayMetaSignature() string { + return "Q-PaDgE" + strings.Repeat("A", 1419) +} + +func testCodexReplayGPTSignature() string { + decoded := make([]byte, 89) + decoded[0] = 0x80 + for i := 9; i < len(decoded); i++ { + decoded[i] = byte(i) + } + return base64.RawURLEncoding.EncodeToString(decoded) +} + +func TestCodexReplaySignatureForTarget(t *testing.T) { + metaSig := testCodexReplayMetaSignature() + routedSig := metaSig + "." + strings.Repeat("Ab3", 23)[:67] + gptSig := testCodexReplayGPTSignature() + + cases := []struct { + name string + model string + sig string + want string + ok bool + }{ + {"native meta on muse target", "muse-spark-1.3", metaSig, metaSig, true}, + {"routed meta on muse target", "meta/muse-wonder", routedSig, routedSig, true}, + {"gpt on gpt target", "gpt-5.4", gptSig, gptSig, true}, + {"gpt cache prefix normalized", "gpt-5.4", "gpt#" + gptSig, gptSig, true}, + {"meta sig on gpt target rejected", "gpt-5.4", metaSig, "", false}, + {"routed meta on gpt target rejected", "gpt-5.4", routedSig, "", false}, + {"gpt sig on meta target rejected", "muse-spark-1.3", gptSig, "", false}, + // An unmapped target defaults to the Codex GPT family. + {"gpt on unmapped target", "deepseek-v4", gptSig, gptSig, true}, + {"meta on unmapped target rejected", "deepseek-v4", metaSig, "", false}, + {"empty signature", "gpt-5.4", "", "", false}, + {"unknown format", "gpt-5.4", "opaque-blob-xyz", "", false}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + got, ok := CodexReplaySignatureForTarget(tc.model, tc.sig) + if ok != tc.ok || got != tc.want { + t.Fatalf("CodexReplaySignatureForTarget(%q, sig) = (%v, %v), want (%v, %v)", tc.model, truncateForTest(got), ok, truncateForTest(tc.want), tc.ok) + } + }) + } +} + +func truncateForTest(s string) string { + if len(s) > 24 { + return s[:24] + "..." + } + return s +} From 674dbe8fd1be5e827803fbbd5e9c6c6ba4f07c42 Mon Sep 17 00:00:00 2001 From: sususu Date: Fri, 16 Oct 2026 18:13:32 +0800 Subject: [PATCH 06/27] fix(signature): support Meta Muse signatures across third-party Responses providers - Gate MetaExecutor signature sanitization by baseURL so custom/third-party meta-api-key endpoints replay router-suffixed Meta Muse envelopes while native api.meta.ai stays native-only - Pass resolved target model (with requested-model fallback for aliased deployments) through Codex, Codex WebSocket, and OpenAI-compat Responses sanitization paths - Broaden Meta Muse model-id boundary matching (muse, meta/muse, muse_*, meta-muse-*) and allow Meta envelopes in the Codex reasoning replay cache - Reuse CodexReplaySignatureForTarget in Claude-to-Codex request translation and avoid duplicating streamed thought summaries on Interactions reasoning done events --- .../cache/codex_reasoning_replay_cache.go | 2 +- .../executor/codex_executor_execute.go | 4 +- .../executor/codex_executor_reasoning.go | 3 +- .../runtime/executor/codex_executor_stream.go | 2 +- .../executor/codex_websockets_execute.go | 2 +- .../executor/codex_websockets_stream.go | 2 +- .../runtime/executor/meta_executor_execute.go | 12 +++-- .../runtime/executor/meta_executor_stream.go | 2 +- .../executor/openai_compat_executor.go | 2 +- .../executor/openai_responses_signature.go | 41 ++++++++++++++++ .../openai_responses_signature_test.go | 13 +++++ internal/signature/meta_validation_test.go | 2 +- internal/signature/provider_compatibility.go | 25 +++++++++- .../codex/claude/codex_claude_request.go | 29 +---------- .../codex/claude/codex_claude_request_test.go | 8 ++++ .../interactions_codex_response.go | 48 +++++++++++-------- .../interactions/interactions_codex_test.go | 18 +++++-- 17 files changed, 148 insertions(+), 67 deletions(-) diff --git a/internal/cache/codex_reasoning_replay_cache.go b/internal/cache/codex_reasoning_replay_cache.go index 7c63b96d30a..d899218821b 100644 --- a/internal/cache/codex_reasoning_replay_cache.go +++ b/internal/cache/codex_reasoning_replay_cache.go @@ -405,7 +405,7 @@ func normalizeCodexReasoningReplayReasoningItem(itemResult gjson.Result) ([]byte if encryptedContent != strings.TrimSpace(encryptedContent) { return nil, false } - if _, err := signature.InspectGPTReasoningSignature(encryptedContent); err != nil { + if !signature.IsValidGPTReasoningSignature(encryptedContent) && !signature.IsValidMetaReasoningSignature(encryptedContent) { return nil, false } diff --git a/internal/runtime/executor/codex_executor_execute.go b/internal/runtime/executor/codex_executor_execute.go index a760b3607c6..124565fc7ad 100644 --- a/internal/runtime/executor/codex_executor_execute.go +++ b/internal/runtime/executor/codex_executor_execute.go @@ -76,7 +76,7 @@ func (e *CodexExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, re } else { body = applyCodexSIWCRequestBodyRules(body, hasExplicitInstructions, cliproxyexecutor.Options{KeepTopLevelEffort: thinking.HasThinkingSuffix(req.Model)}) } - body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(ctx, "codex executor", body, isCompat, baseModel) + body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(ctx, "codex executor", body, isCompat, resolveResponsesSignatureTargetModel(baseModel, req, opts)) body = normalizeCodexParallelToolCalls(body, opts.Headers) body = helps.NormalizeCodexToolSchemas(body) body, optimizeMultiAgentV2 := helps.OptimizeCodexMultiAgentV2RequestForAuth(ctx, opts.Headers, body, e.cfg, auth, isCompat) @@ -261,7 +261,7 @@ func (e *CodexExecutor) executeCompact(ctx context.Context, auth *cliproxyauth.A hasExplicitInstructions := gjson.GetBytes(req.Payload, "instructions").Exists() && strings.TrimSpace(gjson.GetBytes(req.Payload, "instructions").String()) != "" body = applyCodexSIWCCompactBodyRules(body, hasExplicitInstructions, cliproxyexecutor.Options{KeepTopLevelEffort: thinking.HasThinkingSuffix(req.Model)}) } - body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(ctx, "codex executor", body, isCompat, baseModel) + body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(ctx, "codex executor", body, isCompat, resolveResponsesSignatureTargetModel(baseModel, req, opts)) body = normalizeCodexParallelToolCalls(body, opts.Headers) body = helps.NormalizeCodexToolSchemas(body) body, optimizeMultiAgentV2 := helps.OptimizeCodexMultiAgentV2RequestForAuth(ctx, opts.Headers, body, e.cfg, auth, isCompat) diff --git a/internal/runtime/executor/codex_executor_reasoning.go b/internal/runtime/executor/codex_executor_reasoning.go index 7bf5f67e1a1..19f026fd108 100644 --- a/internal/runtime/executor/codex_executor_reasoning.go +++ b/internal/runtime/executor/codex_executor_reasoning.go @@ -199,7 +199,8 @@ func codexInputHasValidReasoningEncryptedContent(body []byte) bool { if encryptedContent.Type != gjson.String { continue } - if _, err := signature.InspectGPTReasoningSignature(encryptedContent.String()); err == nil { + rawSig := encryptedContent.String() + if signature.IsValidGPTReasoningSignature(rawSig) || signature.IsValidMetaReasoningSignature(rawSig) { return true } } diff --git a/internal/runtime/executor/codex_executor_stream.go b/internal/runtime/executor/codex_executor_stream.go index b78a480cf28..28c322669b0 100644 --- a/internal/runtime/executor/codex_executor_stream.go +++ b/internal/runtime/executor/codex_executor_stream.go @@ -83,7 +83,7 @@ func (e *CodexExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au } else { body = applyCodexSIWCRequestBodyRules(body, hasExplicitInstructions, cliproxyexecutor.Options{KeepTopLevelEffort: thinking.HasThinkingSuffix(req.Model)}) } - body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(ctx, "codex executor", body, isCompat, baseModel) + body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(ctx, "codex executor", body, isCompat, resolveResponsesSignatureTargetModel(baseModel, req, opts)) body = normalizeCodexParallelToolCalls(body, opts.Headers) body = helps.NormalizeCodexToolSchemas(body) body, optimizeMultiAgentV2 := helps.OptimizeCodexMultiAgentV2RequestForAuth(ctx, opts.Headers, body, e.cfg, auth, isCompat) diff --git a/internal/runtime/executor/codex_websockets_execute.go b/internal/runtime/executor/codex_websockets_execute.go index a5aef073faf..7f6358ca351 100644 --- a/internal/runtime/executor/codex_websockets_execute.go +++ b/internal/runtime/executor/codex_websockets_execute.go @@ -75,7 +75,7 @@ func (e *CodexWebsocketsExecutor) Execute(ctx context.Context, auth *cliproxyaut body = ensureImageGenerationTool(body, baseModel, auth, opts.Headers) } } - body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompat(ctx, "codex websockets executor", body, isCompat) + body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(ctx, "codex websockets executor", body, isCompat, resolveResponsesSignatureTargetModel(baseModel, req, opts)) body = normalizeCodexWebsocketParallelToolCalls(body, opts.Headers) body = helps.NormalizeCodexToolSchemas(body) multiAgentV2Conflict := helps.HasCodexMultiAgentV2NamespaceConflict(body) diff --git a/internal/runtime/executor/codex_websockets_stream.go b/internal/runtime/executor/codex_websockets_stream.go index 82bfc3bba36..bb5b6849e04 100644 --- a/internal/runtime/executor/codex_websockets_stream.go +++ b/internal/runtime/executor/codex_websockets_stream.go @@ -803,7 +803,7 @@ func (e *CodexWebsocketsExecutor) prepareCodexWebsocketStream(ctx context.Contex body = ensureImageGenerationTool(body, baseModel, auth, opts.Headers) } } - body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(ctx, "codex websockets executor", body, isCompat, baseModel) + body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(ctx, "codex websockets executor", body, isCompat, resolveResponsesSignatureTargetModel(baseModel, req, opts)) body = normalizeCodexWebsocketParallelToolCalls(body, opts.Headers) body = helps.NormalizeCodexToolSchemas(body) multiAgentV2Conflict := helps.HasCodexMultiAgentV2NamespaceConflict(body) diff --git a/internal/runtime/executor/meta_executor_execute.go b/internal/runtime/executor/meta_executor_execute.go index 67ae5d6a323..906c1d76475 100644 --- a/internal/runtime/executor/meta_executor_execute.go +++ b/internal/runtime/executor/meta_executor_execute.go @@ -31,7 +31,7 @@ type metaPreparedRequest struct { body []byte } -func (e *MetaExecutor) prepareResponsesRequest(ctx context.Context, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, stream bool) (*metaPreparedRequest, error) { +func (e *MetaExecutor) prepareResponsesRequest(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, stream bool) (*metaPreparedRequest, error) { baseModel := thinking.ParseSuffix(req.Model).ModelName from := opts.SourceFormat responseFormat := cliproxyexecutor.ResponseFormatOrSource(opts) @@ -71,7 +71,8 @@ func (e *MetaExecutor) prepareResponsesRequest(ctx context.Context, req cliproxy return nil, errNormalizePatch } body = normalizeCodexInstructions(body) - body = sanitizeOpenAIResponsesReasoningEncryptedContentForMeta(ctx, "meta executor", body) + baseURL, _ := metaCreds(auth) + body = sanitizeOpenAIResponsesReasoningEncryptedContentForMetaBaseURL(ctx, "meta executor", body, baseURL) body = helps.SanitizeMetaWebSearchTools(body) body = helps.NormalizeCodexToolIntegerTypes(body, opts.Headers) @@ -98,7 +99,7 @@ func (e *MetaExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, req return resp, errAuth } - prepared, errPrepare := e.prepareResponsesRequest(ctx, req, opts, true) + prepared, errPrepare := e.prepareResponsesRequest(ctx, enriched, req, opts, true) if errPrepare != nil { return resp, errPrepare } @@ -259,10 +260,11 @@ func (e *MetaExecutor) translateMetaCompleted(ctx context.Context, req cliproxye } func (e *MetaExecutor) CountTokens(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { - if _, errAuth := e.ensureAuth(ctx, auth); errAuth != nil { + enriched, errAuth := e.ensureAuth(ctx, auth) + if errAuth != nil { return cliproxyexecutor.Response{}, errAuth } - prepared, errPrepare := e.prepareResponsesRequest(ctx, req, opts, false) + prepared, errPrepare := e.prepareResponsesRequest(ctx, enriched, req, opts, false) if errPrepare != nil { return cliproxyexecutor.Response{}, errPrepare } diff --git a/internal/runtime/executor/meta_executor_stream.go b/internal/runtime/executor/meta_executor_stream.go index a413a810fb9..84cba1b9d7c 100644 --- a/internal/runtime/executor/meta_executor_stream.go +++ b/internal/runtime/executor/meta_executor_stream.go @@ -27,7 +27,7 @@ func (e *MetaExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Aut return nil, errAuth } - prepared, errPrepare := e.prepareResponsesRequest(ctx, req, opts, true) + prepared, errPrepare := e.prepareResponsesRequest(ctx, enriched, req, opts, true) if errPrepare != nil { return nil, errPrepare } diff --git a/internal/runtime/executor/openai_compat_executor.go b/internal/runtime/executor/openai_compat_executor.go index 31ac21db359..7f0286c000b 100644 --- a/internal/runtime/executor/openai_compat_executor.go +++ b/internal/runtime/executor/openai_compat_executor.go @@ -143,7 +143,7 @@ func (e *OpenAICompatExecutor) Execute(ctx context.Context, auth *cliproxyauth.A if updated, errDelete := sjson.DeleteBytes(translated, "stream"); errDelete == nil { translated = updated } - translated = sanitizeOpenAIResponsesReasoningEncryptedContent(ctx, "openai compat executor", translated) + translated = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(ctx, "openai compat executor", translated, true, resolveResponsesSignatureTargetModel(baseModel, req, opts)) } reporter.SetTranslatedReasoningEffort(translated, to.String()) diff --git a/internal/runtime/executor/openai_responses_signature.go b/internal/runtime/executor/openai_responses_signature.go index 475ae7c0a42..2af394edeef 100644 --- a/internal/runtime/executor/openai_responses_signature.go +++ b/internal/runtime/executor/openai_responses_signature.go @@ -3,11 +3,14 @@ package executor import ( "context" "fmt" + "net/url" "strings" "github.com/router-for-me/CLIProxyAPI/v8/internal/runtime/executor/helps" "github.com/router-for-me/CLIProxyAPI/v8/internal/signature" + "github.com/router-for-me/CLIProxyAPI/v8/internal/thinking" "github.com/router-for-me/CLIProxyAPI/v8/internal/util" + cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor" "github.com/tidwall/gjson" "github.com/tidwall/sjson" ) @@ -77,6 +80,19 @@ func sanitizeOpenAIResponsesReasoningEncryptedContentWithCompat(ctx context.Cont // Meta executor keeps its own stricter native-only gate. For any other // target the recognized Meta envelope is known-foreign and stripped like // other providers'. +// resolveResponsesSignatureTargetModel returns the canonical model name to use +// for reasoning signature target checks, falling back to the client-requested +// model when an upstream alias mapping hides the Meta Muse family name. +func resolveResponsesSignatureTargetModel(baseModel string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) string { + if signature.SignatureProviderFromModelName(baseModel) != signature.SignatureProviderUnknown { + return baseModel + } + if requested := thinking.ParseSuffix(helps.PayloadRequestedModel(opts, req.Model)).ModelName; signature.SignatureProviderFromModelName(requested) != signature.SignatureProviderUnknown { + return requested + } + return baseModel +} + func sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(ctx context.Context, provider string, body []byte, isCompat bool, targetModel string) []byte { metaPolicy := metaSignatureForeign if signature.SignatureProviderFromModelName(targetModel) == signature.SignatureProviderMeta { @@ -101,6 +117,31 @@ func sanitizeOpenAIResponsesReasoningEncryptedContentForMeta(ctx context.Context return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, true, metaSignatureNativeOnly) } +// sanitizeOpenAIResponsesReasoningEncryptedContentForMetaBaseURL gates Meta +// reasoning signatures by whether baseURL is the native Meta upstream +// (api.meta.ai, which accepts only unsuffixed envelopes) or a third-party +// Responses provider configured under meta-api-key (which may emit and consume +// router-suffixed Meta Muse envelopes verbatim). +func sanitizeOpenAIResponsesReasoningEncryptedContentForMetaBaseURL(ctx context.Context, provider string, body []byte, baseURL string) []byte { + if isNativeMetaUpstreamURL(baseURL) { + return sanitizeOpenAIResponsesReasoningEncryptedContentForMeta(ctx, provider, body) + } + return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, true, metaSignatureAllowRouted) +} + +func isNativeMetaUpstreamURL(rawURL string) bool { + rawURL = strings.TrimSpace(rawURL) + if rawURL == "" { + return true + } + parsed, err := url.Parse(rawURL) + if err != nil { + return true + } + host := strings.ToLower(strings.TrimSpace(parsed.Hostname())) + return host == "" || host == "api.meta.ai" || strings.HasSuffix(host, ".meta.ai") +} + func sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx context.Context, provider string, body []byte, isCompat bool, keepForeign bool, metaPolicy metaSignaturePolicy) []byte { inputResult := util.GetGJSONBytesNoCopy(body, "input") if !inputResult.Exists() || !inputResult.IsArray() { diff --git a/internal/runtime/executor/openai_responses_signature_test.go b/internal/runtime/executor/openai_responses_signature_test.go index b32a83705df..2ee6f70c358 100644 --- a/internal/runtime/executor/openai_responses_signature_test.go +++ b/internal/runtime/executor/openai_responses_signature_test.go @@ -391,6 +391,19 @@ func TestSanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel_RoutedM // Namespaced router model IDs resolve to their Meta tail segment. got := sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(context.Background(), "test", buildBody(), false, "openrouter/meta/muse-wonder") assertRoutedTarget(t, got) + + // Third-party Meta executor baseURL (non-api.meta.ai) preserves routed Meta + // envelopes while native api.meta.ai strips the router suffix. + routedMetaURL := sanitizeOpenAIResponsesReasoningEncryptedContentForMetaBaseURL(context.Background(), "test", buildBody(), "https://openrouter.ai/api/v1") + assertRoutedTarget(t, routedMetaURL) + + nativeMetaURL := sanitizeOpenAIResponsesReasoningEncryptedContentForMetaBaseURL(context.Background(), "test", buildBody(), "https://api.meta.ai/api/v1") + if gotSig := gjson.GetBytes(nativeMetaURL, "input.0.encrypted_content").String(); gotSig != native { + t.Fatalf("native Meta URL should preserve native envelope; body=%s", nativeMetaURL) + } + if gjson.GetBytes(nativeMetaURL, "input.1.encrypted_content").Exists() { + t.Fatalf("native Meta URL should strip router-suffixed envelope; body=%s", nativeMetaURL) + } } func TestSanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel_StripsMetaForNonMetaTargets(t *testing.T) { diff --git a/internal/signature/meta_validation_test.go b/internal/signature/meta_validation_test.go index f850239367f..29856c95bb1 100644 --- a/internal/signature/meta_validation_test.go +++ b/internal/signature/meta_validation_test.go @@ -189,7 +189,7 @@ func TestMetaSignatureCompatibility(t *testing.T) { } func TestSignatureProviderFromModelName_Meta(t *testing.T) { - for _, model := range []string{"muse-wonder-full-1.0-contributor", "muse-spark-1.3", "Muse-Wonder-1.0", "meta/muse-spark-1.2"} { + for _, model := range []string{"muse-wonder-full-1.0-contributor", "muse-spark-1.3", "Muse-Wonder-1.0", "meta/muse-spark-1.2", "meta/muse", "muse", "meta-muse-spark", "muse_spark_1_3"} { if got := SignatureProviderFromModelName(model); got != SignatureProviderMeta { t.Errorf("SignatureProviderFromModelName(%q) = %q, want %q", model, got, SignatureProviderMeta) } diff --git a/internal/signature/provider_compatibility.go b/internal/signature/provider_compatibility.go index 311d85daf81..95d719a12f5 100644 --- a/internal/signature/provider_compatibility.go +++ b/internal/signature/provider_compatibility.go @@ -106,7 +106,7 @@ func signatureProviderFromModelNameValue(lower string) SignatureProvider { return SignatureProviderKimi case strings.Contains(lower, "grok"): return SignatureProviderGrok - case strings.HasPrefix(lower, "muse-") || strings.Contains(lower, "/muse-"): + case isMetaMuseModelName(lower): return SignatureProviderMeta case strings.Contains(lower, "swe-"): return SignatureProviderSWE @@ -115,6 +115,29 @@ func signatureProviderFromModelNameValue(lower string) SignatureProvider { } } +// isMetaMuseModelName reports whether lower names a Meta Muse model on a +// native or third-party provider ("muse", "muse-*", "muse_*", "meta-muse-*") +// without matching unrelated substrings such as "museum" or "amuse". +func isMetaMuseModelName(lower string) bool { + if lower == "muse" || + strings.HasPrefix(lower, "muse-") || + strings.HasPrefix(lower, "muse_") || + strings.HasSuffix(lower, "/muse") || + strings.Contains(lower, "/muse-") || + strings.Contains(lower, "/muse_") { + return true + } + for _, prefix := range []string{"meta-", "meta_"} { + if idx := strings.Index(lower, prefix+"muse"); idx >= 0 { + rest := lower[idx+len(prefix+"muse"):] + if rest == "" || rest[0] == '-' || rest[0] == '_' || rest[0] == '.' || rest[0] == ':' { + return true + } + } + } + return false +} + // selfDescribingSignatureFirstChars are the base64 first characters that a // self-describing provider envelope can produce. A base64 first character is // exactly the first payload byte shifted right by two, so a single character diff --git a/internal/translator/codex/claude/codex_claude_request.go b/internal/translator/codex/claude/codex_claude_request.go index 19f0cf18ea7..d81713e8319 100644 --- a/internal/translator/codex/claude/codex_claude_request.go +++ b/internal/translator/codex/claude/codex_claude_request.go @@ -175,27 +175,17 @@ func convertClaudeRequestToCodex(modelName string, inputRawJSON []byte, _ bool, } rawSignature := part.Get("signature").String() - signature, ok := sigcompat.CompatibleSignatureForProvider(sigcompat.SignatureProviderGPT, rawSignature) + signature, ok := translatorcommon.CodexReplaySignatureForTarget(modelName, rawSignature) if !ok && preserveEmptyThinkingBlocks && part.Get("signature").Type == gjson.String && strings.TrimSpace(rawSignature) != "" && sigcompat.DetectSignatureProviderForBlock(rawSignature, sigcompat.SignatureBlockKindClaudeThinking) == sigcompat.SignatureProviderUnknown { signature = rawSignature ok = true } - if !ok && codexClaudeTargetAcceptsMetaSignature(modelName) && sigcompat.IsValidMetaReasoningSignature(rawSignature) { - signature = strings.TrimSpace(rawSignature) - ok = true - } if !ok { if preserveEmptyThinkingBlocks && strings.TrimSpace(rawSignature) == "" { signature = rawSignature } else { - if !codexClaudeTargetAcceptsGrokSignature(modelName) { - return - } - if _, err := sigcompat.InspectGrokEncryptedContent(rawSignature); err != nil { - return - } - signature = rawSignature + return } } @@ -468,21 +458,6 @@ func convertClaudeRequestToCodex(modelName string, inputRawJSON []byte, _ bool, return template, drops.Err() } -// codexClaudeTargetAcceptsMetaSignature reports whether the target model is -// a Meta Muse model, in which case a positively recognized native Meta -// envelope carried in a Claude thinking signature must survive translation -// into a Responses reasoning item so it can replay to Meta. For every other -// target the same blob is foreign and dropped by the caller. -func codexClaudeTargetAcceptsMetaSignature(modelName string) bool { - baseModel := thinking.ParseSuffix(modelName).ModelName - return sigcompat.SignatureProviderFromModelName(baseModel) == sigcompat.SignatureProviderMeta -} - -func codexClaudeTargetAcceptsGrokSignature(modelName string) bool { - baseModel := strings.ToLower(strings.TrimSpace(thinking.ParseSuffix(modelName).ModelName)) - return strings.Contains(baseModel, "grok") -} - func normalizeCodexServiceTier(result gjson.Result) string { if !result.Exists() || result.Type != gjson.String { return "" diff --git a/internal/translator/codex/claude/codex_claude_request_test.go b/internal/translator/codex/claude/codex_claude_request_test.go index 9b5211cf10b..6abc910e2a1 100644 --- a/internal/translator/codex/claude/codex_claude_request_test.go +++ b/internal/translator/codex/claude/codex_claude_request_test.go @@ -774,6 +774,14 @@ func TestConvertClaudeRequestToCodex_AssistantRoutedMetaSignatureToReasoningItem if got := countRequestInputItemsByType(out, "reasoning"); got != 0 { t.Fatalf("got %d reasoning items for non-Meta target, want 0; output=%s", got, out) } + + // A foreign GPT signature must not be replayed when the target is a Meta Muse model. + gptPayload := []byte(`{"model":"muse-spark-1.3","messages":[{"role":"assistant","content":[{"type":"thinking","thinking":"summary","signature":""},{"type":"text","text":"answer"}]},{"role":"user","content":"next"}]}`) + gptPayload, _ = sjson.SetBytes(gptPayload, "messages.0.content.0.signature", validCodexReasoningSignature()) + out, _ = ConvertClaudeRequestToCodex("muse-spark-1.3", gptPayload, false) + if got := countRequestInputItemsByType(out, "reasoning"); got != 0 { + t.Fatalf("got %d reasoning items for GPT signature on Meta target, want 0; output=%s", got, out) + } } func TestConvertClaudeRequestToCodex_IgnoresGrokSignatureForNonGrokTargets(t *testing.T) { diff --git a/internal/translator/codex/interactions/interactions_codex_response.go b/internal/translator/codex/interactions/interactions_codex_response.go index 34e9845bf4d..dcc0d382f6b 100644 --- a/internal/translator/codex/interactions/interactions_codex_response.go +++ b/internal/translator/codex/interactions/interactions_codex_response.go @@ -14,19 +14,20 @@ import ( ) type codexToInteractionsStreamState struct { - Started bool - Completed bool - Done bool - ActiveStepOpen bool - ActiveStepType string - ActiveStepIndex int - StepIndex int - ID string - Model string - CreatedAt int64 - HasOutputText bool - FunctionCallName string - FunctionCallID string + Started bool + Completed bool + Done bool + ActiveStepOpen bool + ActiveStepType string + ActiveStepIndex int + StepIndex int + ID string + Model string + CreatedAt int64 + HasOutputText bool + HasThoughtSummary bool + FunctionCallName string + FunctionCallID string // SeenReasoningSignatures dedupes repeated reasoning output_item.done // events so one signature is emitted as a thought_signature delta once. SeenReasoningSignatures map[[32]byte]bool @@ -237,6 +238,7 @@ func codexReasoningDeltaToInteractions(st *codexToInteractionsStreamState, root delta := []byte(`{"index":0,"delta":{"content":{"text":"","type":"text"},"type":"thought_summary"},"event_type":"step.delta"}`) delta, _ = sjson.SetBytes(delta, "index", st.ActiveStepIndex) delta, _ = sjson.SetBytes(delta, "delta.content.text", root.Get("delta").String()) + st.HasThoughtSummary = true return append(out, translatorcommon.SSEEventData("step.delta", delta)) } @@ -284,6 +286,9 @@ func appendCodexInteractionsStepStart(out [][]byte, st *codexToInteractionsStrea st.StepIndex++ st.ActiveStepOpen = true st.ActiveStepType = stepType + if stepType == "thought" { + st.HasThoughtSummary = false + } stepStart := []byte(`{"index":0,"step":{"type":""},"event_type":"step.start"}`) stepStart, _ = sjson.SetBytes(stepStart, "index", st.ActiveStepIndex) stepStart, _ = sjson.SetBytes(stepStart, "step.type", stepType) @@ -419,13 +424,6 @@ func appendCodexMessageItemToInteractionsStream(out [][]byte, st *codexToInterac func appendCodexReasoningItemToInteractionsStream(out [][]byte, st *codexToInteractionsStreamState, item gjson.Result) [][]byte { text := codexReasoningText(item) sig := strings.TrimSpace(item.Get("encrypted_content").String()) - if text != "" { - out = ensureCodexInteractionsStep(out, st, "thought", item) - delta := []byte(`{"index":0,"delta":{"content":{"text":"","type":"text"},"type":"thought_summary"},"event_type":"step.delta"}`) - delta, _ = sjson.SetBytes(delta, "index", st.ActiveStepIndex) - delta, _ = sjson.SetBytes(delta, "delta.content.text", text) - out = append(out, translatorcommon.SSEEventData("step.delta", delta)) - } if sig != "" { key := sha256.Sum256([]byte(item.Get("id").String() + "\x00" + sig)) if st.SeenReasoningSignatures[key] { @@ -435,6 +433,16 @@ func appendCodexReasoningItemToInteractionsStream(out [][]byte, st *codexToInter st.SeenReasoningSignatures = make(map[[32]byte]bool) } st.SeenReasoningSignatures[key] = true + } + if text != "" && !st.HasThoughtSummary { + out = ensureCodexInteractionsStep(out, st, "thought", item) + delta := []byte(`{"index":0,"delta":{"content":{"text":"","type":"text"},"type":"thought_summary"},"event_type":"step.delta"}`) + delta, _ = sjson.SetBytes(delta, "index", st.ActiveStepIndex) + delta, _ = sjson.SetBytes(delta, "delta.content.text", text) + out = append(out, translatorcommon.SSEEventData("step.delta", delta)) + st.HasThoughtSummary = true + } + if sig != "" { out = ensureCodexInteractionsStep(out, st, "thought", item) signatureDelta := []byte(`{"index":0,"delta":{"signature":"","type":"thought_signature"},"event_type":"step.delta"}`) signatureDelta, _ = sjson.SetBytes(signatureDelta, "index", st.ActiveStepIndex) diff --git a/internal/translator/codex/interactions/interactions_codex_test.go b/internal/translator/codex/interactions/interactions_codex_test.go index 6c8fd4db378..95d98e427bf 100644 --- a/internal/translator/codex/interactions/interactions_codex_test.go +++ b/internal/translator/codex/interactions/interactions_codex_test.go @@ -232,9 +232,10 @@ func TestConvertCodexResponseToInteractionsStream_ReasoningSignature(t *testing. var events [][]byte for _, chunk := range [][]byte{ []byte(`data: {"type":"response.created","response":{"id":"resp_1","model":"codex-test"}}`), - []byte(`data: {"type":"response.output_item.done","item":{"id":"rs_1","type":"reasoning","summary":[],"encrypted_content":"` + metaSig + `"}}`), - // A duplicated done event for the same item must not re-emit the signature. - []byte(`data: {"type":"response.output_item.done","item":{"id":"rs_1","type":"reasoning","summary":[],"encrypted_content":"` + metaSig + `"}}`), + []byte(`data: {"type":"response.reasoning_summary_text.delta","delta":"streamed thinking"}`), + []byte(`data: {"type":"response.output_item.done","item":{"id":"rs_1","type":"reasoning","summary":[{"type":"summary_text","text":"streamed thinking"}],"encrypted_content":"` + metaSig + `"}}`), + // A duplicated done event for the same item must not re-emit the signature or summary. + []byte(`data: {"type":"response.output_item.done","item":{"id":"rs_1","type":"reasoning","summary":[{"type":"summary_text","text":"streamed thinking"}],"encrypted_content":"` + metaSig + `"}}`), []byte(`data: {"type":"response.output_item.done","item":{"type":"message","content":[{"type":"output_text","text":"answer"}]}}`), } { events = append(events, ConvertCodexResponseToInteractions(context.Background(), "codex-test", nil, nil, chunk, ¶m)...) @@ -264,9 +265,18 @@ func TestConvertCodexResponseToInteractionsStream_ReasoningSignature(t *testing. if signatureDeltas != 1 { t.Fatalf("thought_signature delta count = %d, want 1 (duplicate suppressed)", signatureDeltas) } + summaryDeltas := 0 + for _, delta := range findCodexInteractionsEventPayloads(events, "step.delta") { + if delta.Get("delta.type").String() == "thought_summary" { + summaryDeltas++ + } + } + if summaryDeltas != 1 { + t.Fatalf("thought_summary delta count = %d, want 1 (output_item.done must not duplicate streamed summary)", summaryDeltas) + } names := strings.Join(codexInteractionsEventNames(events), ",") - want := "interaction.created,interaction.status_update,step.start,step.delta,step.stop,step.start,step.delta,step.stop" + want := "interaction.created,interaction.status_update,step.start,step.delta,step.delta,step.stop,step.start,step.delta,step.stop" if names != want { t.Fatalf("events = %s, want %s", names, want) } From be9fbd942929c14695cce7acdedf6b37de081150 Mon Sep 17 00:00:00 2001 From: sususu Date: Fri, 16 Oct 2026 18:59:17 +0800 Subject: [PATCH 07/27] feat(signature): support router-suffixed GPT/Grok envelopes and multi-family Responses targets - Allow optional dot-delimited alphanumeric router suffixes (such as OpenRouter endpoint_slug metadata) in InspectGPTReasoningSignature and InspectGrokEncryptedContent - Preserve matching target-family signatures (Grok, Gemini, Claude, Kimi, SWE) in sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel when served through Responses routers --- .../executor/openai_responses_signature.go | 36 +++++++++++------ internal/signature/gpt_validation.go | 17 +++++++- internal/signature/gpt_validation_test.go | 27 +++++++++++++ internal/signature/grok_validation.go | 40 +++++++++++++------ internal/signature/grok_validation_test.go | 26 ++++++++++++ .../common/codex_replay_signature_test.go | 1 + 6 files changed, 120 insertions(+), 27 deletions(-) diff --git a/internal/runtime/executor/openai_responses_signature.go b/internal/runtime/executor/openai_responses_signature.go index 2af394edeef..7fff8ee10eb 100644 --- a/internal/runtime/executor/openai_responses_signature.go +++ b/internal/runtime/executor/openai_responses_signature.go @@ -63,7 +63,7 @@ const ( ) func sanitizeOpenAIResponsesReasoningEncryptedContent(ctx context.Context, provider string, body []byte) []byte { - return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, false, metaSignatureForeign) + return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, false, metaSignatureForeign, signature.SignatureProviderUnknown) } func sanitizeOpenAIResponsesReasoningEncryptedContentWithCompat(ctx context.Context, provider string, body []byte, isCompat bool) []byte { @@ -94,15 +94,16 @@ func resolveResponsesSignatureTargetModel(baseModel string, req cliproxyexecutor } func sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(ctx context.Context, provider string, body []byte, isCompat bool, targetModel string) []byte { + targetProvider := signature.SignatureProviderFromModelName(targetModel) metaPolicy := metaSignatureForeign - if signature.SignatureProviderFromModelName(targetModel) == signature.SignatureProviderMeta { + if targetProvider == signature.SignatureProviderMeta { metaPolicy = metaSignatureAllowRouted } - return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, isCompat, isCompat, metaPolicy) + return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, isCompat, isCompat, metaPolicy, targetProvider) } func sanitizeOpenAIResponsesReasoningEncryptedContentKeepForeign(ctx context.Context, provider string, body []byte) []byte { - return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, true, metaSignatureForeign) + return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, true, metaSignatureForeign, signature.SignatureProviderUnknown) } // sanitizeOpenAIResponsesReasoningEncryptedContentForMeta is the Meta-target @@ -114,7 +115,7 @@ func sanitizeOpenAIResponsesReasoningEncryptedContentKeepForeign(ctx context.Con // Meta envelope revision will be stripped until the inspector learns it // (fail-closed). func sanitizeOpenAIResponsesReasoningEncryptedContentForMeta(ctx context.Context, provider string, body []byte) []byte { - return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, true, metaSignatureNativeOnly) + return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, true, metaSignatureNativeOnly, signature.SignatureProviderMeta) } // sanitizeOpenAIResponsesReasoningEncryptedContentForMetaBaseURL gates Meta @@ -126,7 +127,7 @@ func sanitizeOpenAIResponsesReasoningEncryptedContentForMetaBaseURL(ctx context. if isNativeMetaUpstreamURL(baseURL) { return sanitizeOpenAIResponsesReasoningEncryptedContentForMeta(ctx, provider, body) } - return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, true, metaSignatureAllowRouted) + return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, true, metaSignatureAllowRouted, signature.SignatureProviderMeta) } func isNativeMetaUpstreamURL(rawURL string) bool { @@ -142,7 +143,7 @@ func isNativeMetaUpstreamURL(rawURL string) bool { return host == "" || host == "api.meta.ai" || strings.HasSuffix(host, ".meta.ai") } -func sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx context.Context, provider string, body []byte, isCompat bool, keepForeign bool, metaPolicy metaSignaturePolicy) []byte { +func sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx context.Context, provider string, body []byte, isCompat bool, keepForeign bool, metaPolicy metaSignaturePolicy, targetProvider signature.SignatureProvider) []byte { inputResult := util.GetGJSONBytesNoCopy(body, "input") if !inputResult.Exists() || !inputResult.IsArray() { return body @@ -271,13 +272,22 @@ func sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx context.Con } else if _, err := signature.InspectGPTReasoningSignature(rawSignature); err != nil { // Valid GPT signatures never reach this branch: the GPT-first gate // above preserves them on non-Meta targets (inherited behavior). - // keepForeign additionally preserves unknown-format blobs a - // third-party Responses target may natively own. Classification - // is skipped entirely when no exemption applies, so strict - // targets pay no detection cost. + // When the target model belongs to another known family (such as + // Grok or Gemini served through a Responses router like OpenRouter), + // preserve signatures that match that target family. keepForeign + // additionally preserves unknown-format blobs a third-party + // Responses target may natively own. keep := false - if rawSignature != "" && keepForeign { - keep = signature.DetectSignatureProvider(rawSignature) == signature.SignatureProviderUnknown + if rawSignature != "" { + switch targetProvider { + case signature.SignatureProviderGrok: + keep = signature.IsValidGrokEncryptedContent(rawSignature) + case signature.SignatureProviderGemini, signature.SignatureProviderClaude, signature.SignatureProviderKimi, signature.SignatureProviderSWE: + _, keep = signature.CompatibleSignatureForProvider(targetProvider, rawSignature) + } + if !keep && keepForeign { + keep = signature.DetectSignatureProvider(rawSignature) == signature.SignatureProviderUnknown + } } if !keep { reason = err.Error() diff --git a/internal/signature/gpt_validation.go b/internal/signature/gpt_validation.go index a0964275a22..e69da4b8462 100644 --- a/internal/signature/gpt_validation.go +++ b/internal/signature/gpt_validation.go @@ -12,6 +12,9 @@ const MaxGPTReasoningSignatureLen = 32 * 1024 * 1024 type GPTReasoningSignatureInfo struct { DecodedLen int CiphertextLen int + // RouterSuffix is the dot-delimited suffix a router such as OpenRouter + // appends after the native Fernet envelope; empty for native values. + RouterSuffix string } func IsValidGPTReasoningSignature(rawSignature string) bool { @@ -38,11 +41,20 @@ func InspectGPTReasoningSignature(rawSignature string) (*GPTReasoningSignatureIn if !strings.HasPrefix(sig, "gAAAA") { return nil, fmt.Errorf("invalid GPT reasoning signature: expected gAAAA prefix") } - if index, r, ok := firstInvalidGPTReasoningSignatureChar(sig); ok { + native := sig + routerSuffix := "" + if head, tail, ok := strings.Cut(sig, "."); ok { + if !isValidMetaRouterSuffix(tail) { + return nil, fmt.Errorf("invalid GPT reasoning signature: router suffix must be 1-%d alphanumeric characters", maxMetaRouterSuffixLen) + } + native = head + routerSuffix = tail + } + if index, r, ok := firstInvalidGPTReasoningSignatureChar(native); ok { return nil, fmt.Errorf("invalid GPT reasoning signature: contains non-base64url character U+%04X at byte %d", r, index) } - decoded, err := decodeGPTReasoningSignature(sig) + decoded, err := decodeGPTReasoningSignature(native) if err != nil { return nil, err } @@ -61,6 +73,7 @@ func InspectGPTReasoningSignature(rawSignature string) (*GPTReasoningSignatureIn return &GPTReasoningSignatureInfo{ DecodedLen: len(decoded), CiphertextLen: ciphertextLen, + RouterSuffix: routerSuffix, }, nil } diff --git a/internal/signature/gpt_validation_test.go b/internal/signature/gpt_validation_test.go index 21befa8285f..e1e19ca81a3 100644 --- a/internal/signature/gpt_validation_test.go +++ b/internal/signature/gpt_validation_test.go @@ -33,3 +33,30 @@ func TestInspectGPTReasoningSignatureRejectsUnicodeEllipsis(t *testing.T) { t.Fatalf("error = %q, want U+2026 base64url detail", err.Error()) } } + +func TestInspectGPTReasoningSignature_RouterSuffixed(t *testing.T) { + native := testGPTReasoningSignature() + suffix := strings.Repeat("Ab3", 27)[:79] + routed := native + "." + suffix + + info, err := InspectGPTReasoningSignature(routed) + if err != nil { + t.Fatalf("InspectGPTReasoningSignature(routed) error = %v", err) + } + if info.RouterSuffix != suffix { + t.Fatalf("RouterSuffix = %q, want %q", info.RouterSuffix, suffix) + } + if got := DetectSignatureProvider(routed); got != SignatureProviderGPT { + t.Fatalf("DetectSignatureProvider(routed) = %q, want %q", got, SignatureProviderGPT) + } + if got, ok := CompatibleSignatureForProvider(SignatureProviderGPT, routed); !ok || got != routed { + t.Fatalf("CompatibleSignatureForProvider(GPT, routed) = (%q, %v), want verbatim routed value", got, ok) + } + + // Malformed router suffixes must be rejected. + for _, bad := range []string{native + ".", native + ".bad_char", native + ".a.b"} { + if _, err := InspectGPTReasoningSignature(bad); err == nil { + t.Fatalf("InspectGPTReasoningSignature(%q) unexpectedly succeeded", bad) + } + } +} diff --git a/internal/signature/grok_validation.go b/internal/signature/grok_validation.go index 8b424ac3c71..7fdcd2d60e3 100644 --- a/internal/signature/grok_validation.go +++ b/internal/signature/grok_validation.go @@ -29,6 +29,9 @@ const ( type GrokEncryptedContentInfo struct { RawLen int DecodedLen int + // RouterSuffix is the dot-delimited suffix a router such as OpenRouter + // appends after the native Grok ciphertext; empty for native values. + RouterSuffix string } // InspectGrokEncryptedContent validates the transport shape of xAI/Grok @@ -56,12 +59,21 @@ func InspectGrokEncryptedContent(raw string) (*GrokEncryptedContentInfo, error) if strings.Contains(sig, "=") { return nil, fmt.Errorf("invalid Grok encrypted_content: expected unpadded standard base64") } - if index, r, ok := firstInvalidGrokEncryptedContentChar(sig); ok { - return nil, fmt.Errorf("invalid Grok encrypted_content: contains non-base64 character U+%04X at byte %d", r, index) - } if _, _, ok := SplitSignatureProviderPrefix(sig); ok { return nil, fmt.Errorf("invalid Grok encrypted_content: carries another provider's cache prefix") } + native := sig + routerSuffix := "" + if head, tail, ok := strings.Cut(sig, "."); ok { + if !isValidMetaRouterSuffix(tail) { + return nil, fmt.Errorf("invalid Grok encrypted_content: router suffix must be 1-%d alphanumeric characters", maxMetaRouterSuffixLen) + } + native = head + routerSuffix = tail + } + if index, r, ok := firstInvalidGrokEncryptedContentChar(native); ok { + return nil, fmt.Errorf("invalid Grok encrypted_content: contains non-base64 character U+%04X at byte %d", r, index) + } // Foreign-envelope rejection only has to run for the narrow set of base64 // first characters a self-describing envelope can produce. Native xAI // ciphertext is uniformly distributed, so this skips the whole chain for @@ -70,17 +82,20 @@ func InspectGrokEncryptedContent(raw string) (*GrokEncryptedContentInfo, error) // particular is high-entropy standard base64 that drops its padding whenever // the decoded length is a multiple of 3, so the padding gate above does not // exclude it on its own. - if maybeSelfDescribingSignatureEnvelope(sig) { - if strings.HasPrefix(sig, "gAAAA") { + if maybeSelfDescribingSignatureEnvelope(native) { + if strings.HasPrefix(native, "gAAAA") { return nil, fmt.Errorf("Grok encrypted_content looks like GPT/Codex reasoning signature") } - if IsValidClaudeThinkingSignature(sig, ClaudeSignatureValidationOptions{Strict: true}) { + if IsValidMetaReasoningSignature(native) { + return nil, fmt.Errorf("Grok encrypted_content looks like Meta reasoning signature") + } + if IsValidClaudeThinkingSignature(native, ClaudeSignatureValidationOptions{Strict: true}) { return nil, fmt.Errorf("Grok encrypted_content looks like Claude thinking signature") } - if IsValidClaudeCAISSignature(sig) { + if IsValidClaudeCAISSignature(native) { return nil, fmt.Errorf("Grok encrypted_content looks like Claude CAIS thinking signature") } - if _, err := InspectGeminiThoughtSignature(sig, GeminiThoughtSignatureValidationOptions{RequireKnownEnvelope: true}); err == nil { + if _, err := InspectGeminiThoughtSignature(native, GeminiThoughtSignatureValidationOptions{RequireKnownEnvelope: true}); err == nil { return nil, fmt.Errorf("Grok encrypted_content looks like Gemini thoughtSignature") } } @@ -90,11 +105,11 @@ func InspectGrokEncryptedContent(raw string) (*GrokEncryptedContentInfo, error) // length tracks reasoning volume continuously at 1-byte granularity. Neither // observed Kimi length appears anywhere in 1027 catalogued signatures or 215 // native Grok samples, so rejecting them here costs no real Grok traffic. - if IsValidKimiThinkingSignature(sig) { + if IsValidKimiThinkingSignature(native) { return nil, fmt.Errorf("Grok encrypted_content has a Kimi thinking signature length") } - decoded, err := decodeGrokEncryptedContent(sig) + decoded, err := decodeGrokEncryptedContent(native) if err != nil { return nil, err } @@ -105,8 +120,9 @@ func InspectGrokEncryptedContent(raw string) (*GrokEncryptedContentInfo, error) return nil, fmt.Errorf("invalid Grok encrypted_content: decoded payload entropy ratio %.3f below %.3f", entropyRatio, MinGrokEncryptedContentEntropyRatio) } return &GrokEncryptedContentInfo{ - RawLen: len(sig), - DecodedLen: len(decoded), + RawLen: len(sig), + DecodedLen: len(decoded), + RouterSuffix: routerSuffix, }, nil } diff --git a/internal/signature/grok_validation_test.go b/internal/signature/grok_validation_test.go index 1c9a2d72786..6474e1052e0 100644 --- a/internal/signature/grok_validation_test.go +++ b/internal/signature/grok_validation_test.go @@ -248,6 +248,32 @@ func TestByteEntropyRatio_SingleByteReturnsZero(t *testing.T) { } } +func TestInspectGrokEncryptedContent_RouterSuffixed(t *testing.T) { + rawBytes := make([]byte, 135) + for i := range rawBytes { + rawBytes[i] = byte((i*73 + 19) & 0xff) + } + native := base64.RawStdEncoding.EncodeToString(rawBytes) + suffix := strings.Repeat("Ab3", 21)[:62] + routed := native + "." + suffix + + info, err := InspectGrokEncryptedContent(routed) + if err != nil { + t.Fatalf("InspectGrokEncryptedContent(routed) error = %v", err) + } + if info.RouterSuffix != suffix || info.DecodedLen != len(rawBytes) { + t.Fatalf("info = %+v, want DecodedLen=%d RouterSuffix=%q", info, len(rawBytes), suffix) + } + if !IsRecognizedReasoningSignature(routed) { + t.Fatal("IsRecognizedReasoningSignature(routed Grok) = false, want true") + } + for _, bad := range []string{native + ".", native + ".bad-dash", native + ".a.b"} { + if _, err := InspectGrokEncryptedContent(bad); err == nil { + t.Fatalf("InspectGrokEncryptedContent(%q) unexpectedly succeeded", bad) + } + } +} + func testGeminiThoughtSignatureEnvelope() string { payload := []byte{0x01, 0x0c} for i := 0; i < 97; i++ { diff --git a/internal/translator/common/codex_replay_signature_test.go b/internal/translator/common/codex_replay_signature_test.go index e5c50afcd3d..0a9471adc50 100644 --- a/internal/translator/common/codex_replay_signature_test.go +++ b/internal/translator/common/codex_replay_signature_test.go @@ -37,6 +37,7 @@ func TestCodexReplaySignatureForTarget(t *testing.T) { {"native meta on muse target", "muse-spark-1.3", metaSig, metaSig, true}, {"routed meta on muse target", "meta/muse-wonder", routedSig, routedSig, true}, {"gpt on gpt target", "gpt-5.4", gptSig, gptSig, true}, + {"routed gpt on openrouter gpt target", "openrouter/openai/gpt-5.6-luna-pro", gptSig + "." + strings.Repeat("Ab3", 27)[:79], gptSig + "." + strings.Repeat("Ab3", 27)[:79], true}, {"gpt cache prefix normalized", "gpt-5.4", "gpt#" + gptSig, gptSig, true}, {"meta sig on gpt target rejected", "gpt-5.4", metaSig, "", false}, {"routed meta on gpt target rejected", "gpt-5.4", routedSig, "", false}, From a68c7d8ed7c26bb5bbb396a11597497c5817e4a7 Mon Sep 17 00:00:00 2001 From: sususu Date: Fri, 16 Oct 2026 19:09:54 +0800 Subject: [PATCH 08/27] fix(signature): strip OpenRouter private RouterSuffix on official and non-OpenRouter upstreams - Only preserve OpenRouter dot-delimited router suffixes when the upstream baseURL is openrouter.ai / *.openrouter.ai - Strip RouterSuffix down to the native GPT/Grok/Meta envelope when forwarding to official tokens (chatgpt.com, api.openai.com, api.x.ai) or non-OpenRouter third-party providers (while keeping native api.meta.ai fail-closed against cross-caller envelopes) - Strip RouterSuffix before storing GPT signatures in the Codex reasoning replay cache --- .../executor/claude_thinking_replay_test.go | 22 +-- .../executor/codex_executor_execute.go | 13 +- .../executor/codex_executor_reasoning.go | 22 ++- .../runtime/executor/codex_executor_stream.go | 7 +- .../codex_stream_bootstrap_buffering_test.go | 22 +-- .../executor/codex_websockets_execute.go | 7 +- .../executor/codex_websockets_stream.go | 7 +- internal/runtime/executor/devin_executor.go | 18 +++ .../executor/helps/gemini_interactions.go | 86 +++++++++--- .../runtime/executor/meta_executor_execute.go | 6 +- .../executor/openai_compat_executor.go | 12 +- .../executor/openai_responses_signature.go | 118 +++++++++++++--- .../openai_responses_signature_test.go | 126 +++++++++++++++++- .../runtime/executor/xai_executor_execute.go | 2 + .../runtime/executor/xai_executor_request.go | 17 ++- .../runtime/executor/xai_executor_response.go | 21 ++- .../runtime/executor/xai_executor_stream.go | 1 + .../runtime/executor/xai_executor_test.go | 12 ++ .../runtime/executor/xai_reasoning_replay.go | 14 +- .../executor/xai_websockets_executor.go | 1 + internal/signature/grok_validation.go | 3 + .../codex/claude/codex_claude_request.go | 3 +- .../codex/claude/codex_claude_request_test.go | 7 + .../codex/claude/codex_claude_response.go | 81 ++++++----- .../codex/gemini/codex_gemini_request.go | 23 +++- .../codex/gemini/codex_gemini_response.go | 122 ++++++++++++----- .../gemini/codex_gemini_response_test.go | 83 ++++++++++++ .../interactions_codex_request.go | 36 ++--- .../interactions_codex_response.go | 50 ++++--- .../interactions/interactions_codex_test.go | 21 +++ .../common/codex_replay_signature.go | 20 +++ 31 files changed, 783 insertions(+), 200 deletions(-) diff --git a/internal/runtime/executor/claude_thinking_replay_test.go b/internal/runtime/executor/claude_thinking_replay_test.go index 553be9c2b2c..7f04adc21f6 100644 --- a/internal/runtime/executor/claude_thinking_replay_test.go +++ b/internal/runtime/executor/claude_thinking_replay_test.go @@ -33,17 +33,17 @@ func claudeReplayTestAuth(baseURL string) *cliproxyauth.Auth { func claudeReplayTestRequest(payload []byte, sessionID string, isCompat bool, source sdktranslator.Format) (cliproxyexecutor.Request, cliproxyexecutor.Options) { return cliproxyexecutor.Request{ - Model: "claude-synthetic-4772", - Payload: payload, - Metadata: map[string]any{ - claudeReplayResolvedModelInfoKey: ®istry.ModelInfo{IsCompat: isCompat}, - }, - }, cliproxyexecutor.Options{ - SourceFormat: source, - Metadata: map[string]any{ - cliproxyexecutor.ExecutionSessionMetadataKey: sessionID, - }, - } + Model: "claude-synthetic-4772", + Payload: payload, + Metadata: map[string]any{ + claudeReplayResolvedModelInfoKey: ®istry.ModelInfo{IsCompat: isCompat}, + }, + }, cliproxyexecutor.Options{ + SourceFormat: source, + Metadata: map[string]any{ + cliproxyexecutor.ExecutionSessionMetadataKey: sessionID, + }, + } } func TestClaudeThinkingReplayEnabledRequiresCompatClaudeAPIKey(t *testing.T) { diff --git a/internal/runtime/executor/codex_executor_execute.go b/internal/runtime/executor/codex_executor_execute.go index 124565fc7ad..cd3eb06b879 100644 --- a/internal/runtime/executor/codex_executor_execute.go +++ b/internal/runtime/executor/codex_executor_execute.go @@ -48,10 +48,13 @@ func (e *CodexExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, re } originalPayload := originalPayloadSource isCompat := e.resolveCodexModelIsCompat(auth, req, baseModel) - originalTranslated, body, updatesChanged, err := translateCodexRequestPairWithUpdateIntent(from, to, baseModel, originalPayload, req.Payload, false, isCompat) + targetModel := resolveResponsesSignatureTargetModel(baseModel, req, opts) + originalTranslated, body, updatesChanged, err := translateCodexRequestPairWithUpdateIntent(from, to, targetModel, originalPayload, req.Payload, false, isCompat) if err != nil { return resp, err } + originalTranslated = helps.SetStringIfDifferent(originalTranslated, "model", baseModel) + body = helps.SetStringIfDifferent(body, "model", baseModel) body, err = helps.ApplyRequestThinking(body, req, opts, from.String(), to.String(), e.Identifier(), updatesChanged) if err != nil { @@ -76,7 +79,6 @@ func (e *CodexExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, re } else { body = applyCodexSIWCRequestBodyRules(body, hasExplicitInstructions, cliproxyexecutor.Options{KeepTopLevelEffort: thinking.HasThinkingSuffix(req.Model)}) } - body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(ctx, "codex executor", body, isCompat, resolveResponsesSignatureTargetModel(baseModel, req, opts)) body = normalizeCodexParallelToolCalls(body, opts.Headers) body = helps.NormalizeCodexToolSchemas(body) body, optimizeMultiAgentV2 := helps.OptimizeCodexMultiAgentV2RequestForAuth(ctx, opts.Headers, body, e.cfg, auth, isCompat) @@ -84,6 +86,7 @@ func (e *CodexExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, re if errReplay != nil { return resp, errReplay } + body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModelAndBaseURL(ctx, "codex executor", body, isCompat, targetModel, baseURL) url := strings.TrimSuffix(baseURL, "/") + "/responses" httpReq, upstreamBody, err := e.cacheHelper(ctx, from, url, req, body, opts.Headers) @@ -242,10 +245,12 @@ func (e *CodexExecutor) executeCompact(ctx context.Context, auth *cliproxyauth.A } originalPayload := originalPayloadSource isCompat := e.resolveCodexModelIsCompat(auth, req, baseModel) - originalTranslated, body, updatesChanged, err := translateCodexRequestPairWithUpdateIntent(from, to, baseModel, originalPayload, req.Payload, false, isCompat) + targetModel := resolveResponsesSignatureTargetModel(baseModel, req, opts) + originalTranslated, body, updatesChanged, err := translateCodexRequestPairWithUpdateIntent(from, to, targetModel, originalPayload, req.Payload, false, isCompat) if err != nil { return resp, err } + originalTranslated = helps.SetStringIfDifferent(originalTranslated, "model", baseModel) body, err = helps.ApplyRequestThinking(body, req, opts, from.String(), to.String(), e.Identifier(), updatesChanged) if err != nil { @@ -261,7 +266,7 @@ func (e *CodexExecutor) executeCompact(ctx context.Context, auth *cliproxyauth.A hasExplicitInstructions := gjson.GetBytes(req.Payload, "instructions").Exists() && strings.TrimSpace(gjson.GetBytes(req.Payload, "instructions").String()) != "" body = applyCodexSIWCCompactBodyRules(body, hasExplicitInstructions, cliproxyexecutor.Options{KeepTopLevelEffort: thinking.HasThinkingSuffix(req.Model)}) } - body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(ctx, "codex executor", body, isCompat, resolveResponsesSignatureTargetModel(baseModel, req, opts)) + body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModelAndBaseURL(ctx, "codex executor", body, isCompat, resolveResponsesSignatureTargetModel(baseModel, req, opts), baseURL) body = normalizeCodexParallelToolCalls(body, opts.Headers) body = helps.NormalizeCodexToolSchemas(body) body, optimizeMultiAgentV2 := helps.OptimizeCodexMultiAgentV2RequestForAuth(ctx, opts.Headers, body, e.cfg, auth, isCompat) diff --git a/internal/runtime/executor/codex_executor_reasoning.go b/internal/runtime/executor/codex_executor_reasoning.go index 19f026fd108..825b36e9b14 100644 --- a/internal/runtime/executor/codex_executor_reasoning.go +++ b/internal/runtime/executor/codex_executor_reasoning.go @@ -199,8 +199,8 @@ func codexInputHasValidReasoningEncryptedContent(body []byte) bool { if encryptedContent.Type != gjson.String { continue } - rawSig := encryptedContent.String() - if signature.IsValidGPTReasoningSignature(rawSig) || signature.IsValidMetaReasoningSignature(rawSig) { + rawSig := signature.SignaturePayloadWithoutProviderPrefix(encryptedContent.String()) + if signature.IsValidGPTReasoningSignature(rawSig) || signature.IsValidMetaReasoningSignature(rawSig) || signature.IsValidGrokEncryptedContent(rawSig) { return true } } @@ -362,6 +362,20 @@ func codexReasoningReplayTurnAnchorIndex(inputItems []gjson.Result, turn codexRe return 0, false } +func canonicalCodexReasoningReplaySignatureKey(rawSignature string) string { + sig := signature.SignaturePayloadWithoutProviderPrefix(strings.TrimSpace(rawSignature)) + if gptInfo, err := signature.InspectGPTReasoningSignature(sig); err == nil && gptInfo.RouterSuffix != "" { + return stripRouterSuffix(sig, gptInfo.RouterSuffix) + } + if metaInfo, err := signature.InspectMetaReasoningSignature(sig); err == nil && metaInfo.RouterSuffix != "" { + return stripRouterSuffix(sig, metaInfo.RouterSuffix) + } + if grokInfo, err := signature.InspectGrokEncryptedContent(sig); err == nil && grokInfo.RouterSuffix != "" { + return stripRouterSuffix(sig, grokInfo.RouterSuffix) + } + return sig +} + func filterCodexReasoningReplayTurnItems(inputItems []gjson.Result, items [][]byte) [][]byte { existingReasoning := make(map[string]bool) existingCalls := make(map[string]bool) @@ -370,7 +384,7 @@ func filterCodexReasoningReplayTurnItems(inputItems []gjson.Result, items [][]by itemType := strings.TrimSpace(inputItem.Get("type").String()) switch itemType { case "reasoning": - if encryptedContent := strings.TrimSpace(inputItem.Get("encrypted_content").String()); encryptedContent != "" { + if encryptedContent := canonicalCodexReasoningReplaySignatureKey(inputItem.Get("encrypted_content").String()); encryptedContent != "" { existingReasoning[encryptedContent] = true } case "function_call_output", "custom_tool_call_output": @@ -388,7 +402,7 @@ func filterCodexReasoningReplayTurnItems(inputItems []gjson.Result, items [][]by itemResult := gjson.ParseBytes(item) switch strings.TrimSpace(itemResult.Get("type").String()) { case "reasoning": - if existingReasoning[strings.TrimSpace(itemResult.Get("encrypted_content").String())] { + if existingReasoning[canonicalCodexReasoningReplaySignatureKey(itemResult.Get("encrypted_content").String())] { continue } case "function_call", "custom_tool_call": diff --git a/internal/runtime/executor/codex_executor_stream.go b/internal/runtime/executor/codex_executor_stream.go index 28c322669b0..9011eb41df9 100644 --- a/internal/runtime/executor/codex_executor_stream.go +++ b/internal/runtime/executor/codex_executor_stream.go @@ -52,10 +52,13 @@ func (e *CodexExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au } originalPayload := originalPayloadSource isCompat := e.resolveCodexModelIsCompat(auth, req, baseModel) - originalTranslated, body, updatesChanged, err := translateCodexRequestPairWithUpdateIntent(from, to, baseModel, originalPayload, req.Payload, true, isCompat) + targetModel := resolveResponsesSignatureTargetModel(baseModel, req, opts) + originalTranslated, body, updatesChanged, err := translateCodexRequestPairWithUpdateIntent(from, to, targetModel, originalPayload, req.Payload, true, isCompat) if err != nil { return nil, err } + originalTranslated = helps.SetStringIfDifferent(originalTranslated, "model", baseModel) + body = helps.SetStringIfDifferent(body, "model", baseModel) body, err = helps.ApplyRequestThinking(body, req, opts, from.String(), to.String(), e.Identifier(), updatesChanged) if err != nil { @@ -83,7 +86,6 @@ func (e *CodexExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au } else { body = applyCodexSIWCRequestBodyRules(body, hasExplicitInstructions, cliproxyexecutor.Options{KeepTopLevelEffort: thinking.HasThinkingSuffix(req.Model)}) } - body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(ctx, "codex executor", body, isCompat, resolveResponsesSignatureTargetModel(baseModel, req, opts)) body = normalizeCodexParallelToolCalls(body, opts.Headers) body = helps.NormalizeCodexToolSchemas(body) body, optimizeMultiAgentV2 := helps.OptimizeCodexMultiAgentV2RequestForAuth(ctx, opts.Headers, body, e.cfg, auth, isCompat) @@ -91,6 +93,7 @@ func (e *CodexExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au if errReplay != nil { return nil, errReplay } + body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModelAndBaseURL(ctx, "codex executor", body, isCompat, targetModel, baseURL) url := strings.TrimSuffix(baseURL, "/") + "/responses" httpReq, upstreamBody, err := e.cacheHelper(ctx, from, url, req, body, opts.Headers) diff --git a/internal/runtime/executor/codex_stream_bootstrap_buffering_test.go b/internal/runtime/executor/codex_stream_bootstrap_buffering_test.go index f2ff79d9e0a..6a5333f9e6d 100644 --- a/internal/runtime/executor/codex_stream_bootstrap_buffering_test.go +++ b/internal/runtime/executor/codex_stream_bootstrap_buffering_test.go @@ -46,12 +46,12 @@ func codexTestAuth(baseURL string) *cliproxyauth.Auth { func codexTestRequest() (cliproxyexecutor.Request, cliproxyexecutor.Options) { return cliproxyexecutor.Request{ - Model: "gpt-5.6-terra", - Payload: []byte(`{"model":"gpt-5.6-terra","input":"hello"}`), - }, cliproxyexecutor.Options{ - SourceFormat: sdktranslator.FromString("openai-response"), - Stream: true, - } + Model: "gpt-5.6-terra", + Payload: []byte(`{"model":"gpt-5.6-terra","input":"hello"}`), + }, cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FromString("openai-response"), + Stream: true, + } } // codexSSEServer streams the supplied event payloads as an HTTP 200 SSE response. @@ -92,11 +92,11 @@ func codexWebsocketServer(t *testing.T, frames ...string) *httptest.Server { func codexWebsocketRequest() (cliproxyexecutor.Request, cliproxyexecutor.Options) { return cliproxyexecutor.Request{ - Model: "gpt-5.6-terra", - Payload: []byte(`{"model":"gpt-5.6-terra","input":[{"type":"message","role":"user","content":"hello"}]}`), - }, cliproxyexecutor.Options{ - SourceFormat: sdktranslator.FromString("openai-response"), - } + Model: "gpt-5.6-terra", + Payload: []byte(`{"model":"gpt-5.6-terra","input":[{"type":"message","role":"user","content":"hello"}]}`), + }, cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FromString("openai-response"), + } } // drainChunks collects every payload and the first error from a stream result. diff --git a/internal/runtime/executor/codex_websockets_execute.go b/internal/runtime/executor/codex_websockets_execute.go index 7f6358ca351..ace6706ae7e 100644 --- a/internal/runtime/executor/codex_websockets_execute.go +++ b/internal/runtime/executor/codex_websockets_execute.go @@ -53,10 +53,13 @@ func (e *CodexWebsocketsExecutor) Execute(ctx context.Context, auth *cliproxyaut } originalPayload := originalPayloadSource isCompat := e.resolveCodexModelIsCompat(auth, req, baseModel) - originalTranslated, body, updatesChanged, err := translateCodexRequestPairWithUpdateIntent(from, to, baseModel, originalPayload, req.Payload, false, isCompat) + targetModel := resolveResponsesSignatureTargetModel(baseModel, req, opts) + originalTranslated, body, updatesChanged, err := translateCodexRequestPairWithUpdateIntent(from, to, targetModel, originalPayload, req.Payload, false, isCompat) if err != nil { return resp, err } + originalTranslated = helps.SetStringIfDifferent(originalTranslated, "model", baseModel) + body = helps.SetStringIfDifferent(body, "model", baseModel) body, err = helps.ApplyRequestThinking(body, req, opts, from.String(), to.String(), e.Identifier(), updatesChanged) if err != nil { @@ -75,7 +78,6 @@ func (e *CodexWebsocketsExecutor) Execute(ctx context.Context, auth *cliproxyaut body = ensureImageGenerationTool(body, baseModel, auth, opts.Headers) } } - body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(ctx, "codex websockets executor", body, isCompat, resolveResponsesSignatureTargetModel(baseModel, req, opts)) body = normalizeCodexWebsocketParallelToolCalls(body, opts.Headers) body = helps.NormalizeCodexToolSchemas(body) multiAgentV2Conflict := helps.HasCodexMultiAgentV2NamespaceConflict(body) @@ -84,6 +86,7 @@ func (e *CodexWebsocketsExecutor) Execute(ctx context.Context, auth *cliproxyaut if errReplay != nil { return resp, errReplay } + body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModelAndBaseURL(ctx, "codex websockets executor", body, isCompat, targetModel, baseURL) httpURL := strings.TrimSuffix(baseURL, "/") + "/responses" wsURL, err := buildCodexResponsesWebsocketURL(httpURL) diff --git a/internal/runtime/executor/codex_websockets_stream.go b/internal/runtime/executor/codex_websockets_stream.go index bb5b6849e04..c0dbc8732cf 100644 --- a/internal/runtime/executor/codex_websockets_stream.go +++ b/internal/runtime/executor/codex_websockets_stream.go @@ -784,10 +784,13 @@ func (e *CodexWebsocketsExecutor) prepareCodexWebsocketStream(ctx context.Contex } originalPayload := originalPayloadSource isCompat := e.resolveCodexModelIsCompat(auth, req, baseModel) - originalTranslated, body, updatesChanged, err := translateCodexRequestPairWithUpdateIntent(from, to, baseModel, originalPayload, req.Payload, true, isCompat) + targetModel := resolveResponsesSignatureTargetModel(baseModel, req, opts) + originalTranslated, body, updatesChanged, err := translateCodexRequestPairWithUpdateIntent(from, to, targetModel, originalPayload, req.Payload, true, isCompat) if err != nil { return nil, err } + originalTranslated = helps.SetStringIfDifferent(originalTranslated, "model", baseModel) + body = helps.SetStringIfDifferent(body, "model", baseModel) body, err = helps.ApplyRequestThinking(body, req, opts, from.String(), to.String(), e.Identifier(), updatesChanged) if err != nil { @@ -803,7 +806,6 @@ func (e *CodexWebsocketsExecutor) prepareCodexWebsocketStream(ctx context.Contex body = ensureImageGenerationTool(body, baseModel, auth, opts.Headers) } } - body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(ctx, "codex websockets executor", body, isCompat, resolveResponsesSignatureTargetModel(baseModel, req, opts)) body = normalizeCodexWebsocketParallelToolCalls(body, opts.Headers) body = helps.NormalizeCodexToolSchemas(body) multiAgentV2Conflict := helps.HasCodexMultiAgentV2NamespaceConflict(body) @@ -812,6 +814,7 @@ func (e *CodexWebsocketsExecutor) prepareCodexWebsocketStream(ctx context.Contex if errReplay != nil { return nil, errReplay } + body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModelAndBaseURL(ctx, "codex websockets executor", body, isCompat, targetModel, baseURL) httpURL := strings.TrimSuffix(baseURL, "/") + "/responses" wsURL, err := buildCodexResponsesWebsocketURL(httpURL) diff --git a/internal/runtime/executor/devin_executor.go b/internal/runtime/executor/devin_executor.go index 9cb77d8dbab..1b8ec84d9c1 100644 --- a/internal/runtime/executor/devin_executor.go +++ b/internal/runtime/executor/devin_executor.go @@ -2390,6 +2390,22 @@ func parseSignatureBytes(sigStr string) ([]byte, string) { } if decoded, err := base64.StdEncoding.DecodeString(s); err == nil && len(decoded) > 0 { decStr := string(decoded) + if strings.HasPrefix(decStr, "cpa-codex-gemini-carrier-v1:") { + inner := strings.TrimSpace(strings.TrimPrefix(decStr, "cpa-codex-gemini-carrier-v1:")) + if strings.HasPrefix(inner, "sealed.v1.") { + return []byte(inner), "sealed" + } + switch internalsignature.DetectSignatureProvider(inner) { + case internalsignature.SignatureProviderClaude: + return []byte(inner), "anthropic" + case internalsignature.SignatureProviderGPT: + return []byte(inner), "openai" + case internalsignature.SignatureProviderGemini: + return []byte(inner), "gemini" + default: + return nil, "" + } + } if strings.HasPrefix(decStr, "sealed.v1.") { return decoded, "sealed" } @@ -2437,6 +2453,8 @@ func detectSignatureType(sig string) string { return "openai" case internalsignature.SignatureProviderGemini: return "gemini" + case internalsignature.SignatureProviderMeta: + return "" } if strings.HasPrefix(s, "CAQS") || strings.HasPrefix(s, "CAIS") { return "anthropic" diff --git a/internal/runtime/executor/helps/gemini_interactions.go b/internal/runtime/executor/helps/gemini_interactions.go index 86e7d48fbe4..9218ebee605 100644 --- a/internal/runtime/executor/helps/gemini_interactions.go +++ b/internal/runtime/executor/helps/gemini_interactions.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/router-for-me/CLIProxyAPI/v8/internal/config" + "github.com/router-for-me/CLIProxyAPI/v8/internal/signature" cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor" sdktranslator "github.com/router-for-me/CLIProxyAPI/v8/sdk/translator" "github.com/tidwall/gjson" @@ -31,31 +32,86 @@ func SanitizeGeminiInteractionsUnsupportedInputIDs(body []byte) []byte { if !input.IsArray() { return body } - for i, item := range input.Array() { + items := input.Array() + kept := make([]string, 0, len(items)) + changed := false + for _, item := range items { + rawItem := item.Raw stepType := item.Get("type").String() + if stepType == "thought" { + sig := strings.TrimSpace(item.Get("signature").String()) + if sig == "" { + sig = strings.TrimSpace(item.Get("thought_signature").String()) + } + if sig == "" { + sig = strings.TrimSpace(item.Get("thoughtSignature").String()) + } + normalizedSig, ok := signature.CompatibleSignatureForProviderBlock( + signature.SignatureProviderGemini, + sig, + signature.SignatureBlockKindGeminiModelPart, + ) + if !ok || normalizedSig == "" || signature.IsGeminiThoughtSignatureBypass(signature.SignaturePayloadWithoutProviderPrefix(normalizedSig)) { + // Official Gemini Interactions API rejects thought steps without a + // valid Gemini signature (or with foreign/corrupt base64 signatures + // or "content" arrays). + changed = true + continue + } + if normalizedSig != item.Get("signature").String() { + rawItem, _ = sjson.Set(rawItem, "signature", normalizedSig) + changed = true + } + for _, unsupportedField := range []string{"id", "content", "text", "thought_signature", "thoughtSignature"} { + if gjson.Get(rawItem, unsupportedField).Exists() { + rawItem, _ = sjson.Delete(rawItem, unsupportedField) + changed = true + } + } + kept = append(kept, rawItem) + continue + } if stepType == "function_call" { if !item.Get("id").Exists() && item.Get("call_id").Exists() { - body, _ = sjson.SetBytes(body, fmt.Sprintf("input.%d.id", i), item.Get("call_id").String()) + rawItem, _ = sjson.Set(rawItem, "id", item.Get("call_id").String()) + changed = true } if item.Get("call_id").Exists() { - body, _ = sjson.DeleteBytes(body, fmt.Sprintf("input.%d.call_id", i)) - } - } else { - if item.Get("id").Exists() { - body, _ = sjson.DeleteBytes(body, fmt.Sprintf("input.%d.id", i)) + rawItem, _ = sjson.Delete(rawItem, "call_id") + changed = true } + } else if item.Get("id").Exists() { + rawItem, _ = sjson.Delete(rawItem, "id") + changed = true } - content := item.Get("content") - if !content.IsArray() { - continue - } - for j, part := range content.Array() { - if part.Get("id").Exists() { - body, _ = sjson.DeleteBytes(body, fmt.Sprintf("input.%d.content.%d.id", i, j)) + content := gjson.Get(rawItem, "content") + if content.IsArray() { + for j, part := range content.Array() { + if part.Get("id").Exists() { + rawItem, _ = sjson.Delete(rawItem, fmt.Sprintf("content.%d.id", j)) + changed = true + } } } + kept = append(kept, rawItem) + } + if !changed { + return body + } + var b strings.Builder + b.WriteByte('[') + for i, raw := range kept { + if i > 0 { + b.WriteByte(',') + } + b.WriteString(raw) + } + b.WriteByte(']') + updated, err := sjson.SetRawBytes(body, "input", []byte(b.String())) + if err != nil { + return body } - return body + return updated } // TranslateGeminiInteractionsRequestBody translates a request payload to FormatInteractions. diff --git a/internal/runtime/executor/meta_executor_execute.go b/internal/runtime/executor/meta_executor_execute.go index 906c1d76475..0e649a57f3c 100644 --- a/internal/runtime/executor/meta_executor_execute.go +++ b/internal/runtime/executor/meta_executor_execute.go @@ -43,8 +43,10 @@ func (e *MetaExecutor) prepareResponsesRequest(ctx context.Context, auth *clipro } originalPayload := bytes.Clone(originalPayloadSource) isCompat := helps.APIKeyModelIsCompat(req) - originalTranslated := helps.TranslateRequestWithAPIKeyModelCompatibility(ctx, opts.Headers, e.cfg, from, to, baseModel, originalPayload, stream, isCompat) - body, err := helps.TranslateRequestReturningError(ctx, opts.Headers, e.cfg, from, to, baseModel, bytes.Clone(req.Payload), stream, isCompat) + targetModel := resolveMetaSignatureTargetModel(baseModel, req, opts) + originalTranslated := helps.TranslateRequestWithAPIKeyModelCompatibility(ctx, opts.Headers, e.cfg, from, to, targetModel, originalPayload, stream, isCompat) + originalTranslated = helps.SetStringIfDifferent(originalTranslated, "model", baseModel) + body, err := helps.TranslateRequestReturningError(ctx, opts.Headers, e.cfg, from, to, targetModel, bytes.Clone(req.Payload), stream, isCompat) if err != nil { return nil, err } diff --git a/internal/runtime/executor/openai_compat_executor.go b/internal/runtime/executor/openai_compat_executor.go index 7f0286c000b..c47ffc2c06e 100644 --- a/internal/runtime/executor/openai_compat_executor.go +++ b/internal/runtime/executor/openai_compat_executor.go @@ -116,10 +116,13 @@ func (e *OpenAICompatExecutor) Execute(ctx context.Context, auth *cliproxyauth.A } originalPayload := originalPayloadSource isCompat := helps.APIKeyModelIsCompat(req) - originalTranslated, translated, updatesChanged, err := helps.TranslateRequestPairWithAPIKeyModelCompatibilityAndUpdateIntent(ctx, opts.Headers, e.cfg, from, to, baseModel, originalPayload, req.Payload, opts.Stream, isCompat) + targetModel := resolveResponsesSignatureTargetModel(baseModel, req, opts) + originalTranslated, translated, updatesChanged, err := helps.TranslateRequestPairWithAPIKeyModelCompatibilityAndUpdateIntent(ctx, opts.Headers, e.cfg, from, to, targetModel, originalPayload, req.Payload, opts.Stream, isCompat) if err != nil { return resp, err } + originalTranslated = helps.SetStringIfDifferent(originalTranslated, "model", baseModel) + translated = helps.SetStringIfDifferent(translated, "model", baseModel) translated, err = helps.ApplyRequestThinking(translated, req, opts, from.String(), to.String(), e.Identifier(), updatesChanged) if err != nil { @@ -143,7 +146,7 @@ func (e *OpenAICompatExecutor) Execute(ctx context.Context, auth *cliproxyauth.A if updated, errDelete := sjson.DeleteBytes(translated, "stream"); errDelete == nil { translated = updated } - translated = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(ctx, "openai compat executor", translated, true, resolveResponsesSignatureTargetModel(baseModel, req, opts)) + translated = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModelAndBaseURL(ctx, "openai compat executor", translated, true, resolveResponsesSignatureTargetModel(baseModel, req, opts), baseURL) } reporter.SetTranslatedReasoningEffort(translated, to.String()) @@ -343,10 +346,13 @@ func (e *OpenAICompatExecutor) ExecuteStream(ctx context.Context, auth *cliproxy } originalPayload := originalPayloadSource isCompat := helps.APIKeyModelIsCompat(req) - originalTranslated, translated, updatesChanged, err := helps.TranslateRequestPairWithAPIKeyModelCompatibilityAndUpdateIntent(ctx, opts.Headers, e.cfg, from, to, baseModel, originalPayload, req.Payload, true, isCompat) + targetModel := resolveResponsesSignatureTargetModel(baseModel, req, opts) + originalTranslated, translated, updatesChanged, err := helps.TranslateRequestPairWithAPIKeyModelCompatibilityAndUpdateIntent(ctx, opts.Headers, e.cfg, from, to, targetModel, originalPayload, req.Payload, true, isCompat) if err != nil { return nil, err } + originalTranslated = helps.SetStringIfDifferent(originalTranslated, "model", baseModel) + translated = helps.SetStringIfDifferent(translated, "model", baseModel) translated, err = helps.ApplyRequestThinking(translated, req, opts, from.String(), to.String(), e.Identifier(), updatesChanged) if err != nil { diff --git a/internal/runtime/executor/openai_responses_signature.go b/internal/runtime/executor/openai_responses_signature.go index 7fff8ee10eb..59469dd32ce 100644 --- a/internal/runtime/executor/openai_responses_signature.go +++ b/internal/runtime/executor/openai_responses_signature.go @@ -63,7 +63,7 @@ const ( ) func sanitizeOpenAIResponsesReasoningEncryptedContent(ctx context.Context, provider string, body []byte) []byte { - return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, false, metaSignatureForeign, signature.SignatureProviderUnknown) + return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, false, metaSignatureForeign, signature.SignatureProviderUnknown, false) } func sanitizeOpenAIResponsesReasoningEncryptedContentWithCompat(ctx context.Context, provider string, body []byte, isCompat bool) []byte { @@ -87,23 +87,41 @@ func resolveResponsesSignatureTargetModel(baseModel string, req cliproxyexecutor if signature.SignatureProviderFromModelName(baseModel) != signature.SignatureProviderUnknown { return baseModel } - if requested := thinking.ParseSuffix(helps.PayloadRequestedModel(opts, req.Model)).ModelName; signature.SignatureProviderFromModelName(requested) != signature.SignatureProviderUnknown { + requested := thinking.ParseSuffix(helps.PayloadRequestedModel(opts, req.Model)).ModelName + switch signature.SignatureProviderFromModelName(requested) { + case signature.SignatureProviderMeta, signature.SignatureProviderGrok, signature.SignatureProviderGPT: return requested } return baseModel } +func resolveMetaSignatureTargetModel(baseModel string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) string { + targetModel := resolveResponsesSignatureTargetModel(baseModel, req, opts) + if signature.SignatureProviderFromModelName(targetModel) == signature.SignatureProviderMeta { + return targetModel + } + return "muse-" + baseModel +} + func sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(ctx context.Context, provider string, body []byte, isCompat bool, targetModel string) []byte { + return sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModelAndBaseURL(ctx, provider, body, isCompat, targetModel, "") +} + +func sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModelAndBaseURL(ctx context.Context, provider string, body []byte, isCompat bool, targetModel string, baseURL string) []byte { targetProvider := signature.SignatureProviderFromModelName(targetModel) metaPolicy := metaSignatureForeign if targetProvider == signature.SignatureProviderMeta { - metaPolicy = metaSignatureAllowRouted + if strings.TrimSpace(baseURL) != "" && isNativeMetaUpstreamURL(baseURL) { + metaPolicy = metaSignatureNativeOnly + } else { + metaPolicy = metaSignatureAllowRouted + } } - return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, isCompat, isCompat, metaPolicy, targetProvider) + return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, isCompat, isCompat, metaPolicy, targetProvider, isOpenRouterUpstreamURL(baseURL)) } func sanitizeOpenAIResponsesReasoningEncryptedContentKeepForeign(ctx context.Context, provider string, body []byte) []byte { - return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, true, metaSignatureForeign, signature.SignatureProviderUnknown) + return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, true, metaSignatureForeign, signature.SignatureProviderUnknown, false) } // sanitizeOpenAIResponsesReasoningEncryptedContentForMeta is the Meta-target @@ -115,19 +133,20 @@ func sanitizeOpenAIResponsesReasoningEncryptedContentKeepForeign(ctx context.Con // Meta envelope revision will be stripped until the inspector learns it // (fail-closed). func sanitizeOpenAIResponsesReasoningEncryptedContentForMeta(ctx context.Context, provider string, body []byte) []byte { - return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, true, metaSignatureNativeOnly, signature.SignatureProviderMeta) + return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, true, metaSignatureNativeOnly, signature.SignatureProviderMeta, false) } // sanitizeOpenAIResponsesReasoningEncryptedContentForMetaBaseURL gates Meta // reasoning signatures by whether baseURL is the native Meta upstream // (api.meta.ai, which accepts only unsuffixed envelopes) or a third-party // Responses provider configured under meta-api-key (which may emit and consume -// router-suffixed Meta Muse envelopes verbatim). +// router-suffixed Meta Muse envelopes verbatim when targeting OpenRouter, or +// strip the OpenRouter suffix for other providers). func sanitizeOpenAIResponsesReasoningEncryptedContentForMetaBaseURL(ctx context.Context, provider string, body []byte, baseURL string) []byte { if isNativeMetaUpstreamURL(baseURL) { return sanitizeOpenAIResponsesReasoningEncryptedContentForMeta(ctx, provider, body) } - return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, true, metaSignatureAllowRouted, signature.SignatureProviderMeta) + return sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx, provider, body, false, true, metaSignatureAllowRouted, signature.SignatureProviderMeta, isOpenRouterUpstreamURL(baseURL)) } func isNativeMetaUpstreamURL(rawURL string) bool { @@ -143,7 +162,27 @@ func isNativeMetaUpstreamURL(rawURL string) bool { return host == "" || host == "api.meta.ai" || strings.HasSuffix(host, ".meta.ai") } -func sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx context.Context, provider string, body []byte, isCompat bool, keepForeign bool, metaPolicy metaSignaturePolicy, targetProvider signature.SignatureProvider) []byte { +func isOpenRouterUpstreamURL(rawURL string) bool { + rawURL = strings.TrimSpace(rawURL) + if rawURL == "" { + return false + } + parsed, err := url.Parse(rawURL) + if err != nil { + return false + } + host := strings.ToLower(strings.TrimSpace(parsed.Hostname())) + return host == "openrouter.ai" || strings.HasSuffix(host, ".openrouter.ai") +} + +func stripRouterSuffix(rawSignature, suffix string) string { + if suffix == "" || len(rawSignature) <= len(suffix)+1 { + return rawSignature + } + return rawSignature[:len(rawSignature)-len(suffix)-1] +} + +func sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx context.Context, provider string, body []byte, isCompat bool, keepForeign bool, metaPolicy metaSignaturePolicy, targetProvider signature.SignatureProvider, allowRouterSuffix bool) []byte { inputResult := util.GetGJSONBytesNoCopy(body, "input") if !inputResult.Exists() || !inputResult.IsArray() { return body @@ -253,6 +292,7 @@ func sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx context.Con } reason := "" + rewrittenSignature := "" switch encryptedContent.Type { case gjson.String: rawSignature := encryptedContent.String() @@ -266,31 +306,58 @@ func sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx context.Con // router suffix a router such as OpenRouter appends. if info, err := signature.InspectMetaReasoningSignature(rawSignature); err != nil { reason = err.Error() - } else if metaPolicy == metaSignatureNativeOnly && info.RouterSuffix != "" { - reason = "router-suffixed Meta reasoning signature is not accepted by the native Meta upstream" + } else if info.RouterSuffix != "" { + if metaPolicy == metaSignatureNativeOnly { + reason = "router-suffixed Meta reasoning signature is not accepted by the native Meta upstream" + } else if !allowRouterSuffix { + rewrittenSignature = stripRouterSuffix(rawSignature, info.RouterSuffix) + } + } + } else if targetProvider == signature.SignatureProviderUnknown || targetProvider == signature.SignatureProviderGPT { + candidate := rawSignature + hasGPTPrefix := false + if prefixedProvider, unprefixed, ok := signature.SplitSignatureProviderPrefix(rawSignature); ok && prefixedProvider == signature.SignatureProviderGPT { + candidate = unprefixed + hasGPTPrefix = true + } + if gptInfo, err := signature.InspectGPTReasoningSignature(candidate); err == nil { + if gptInfo.RouterSuffix != "" && !allowRouterSuffix { + rewrittenSignature = stripRouterSuffix(candidate, gptInfo.RouterSuffix) + } else if hasGPTPrefix { + rewrittenSignature = candidate + } + } else if !(keepForeign && rawSignature != "" && signature.DetectSignatureProvider(rawSignature) == signature.SignatureProviderUnknown) { + reason = err.Error() } - } else if _, err := signature.InspectGPTReasoningSignature(rawSignature); err != nil { - // Valid GPT signatures never reach this branch: the GPT-first gate - // above preserves them on non-Meta targets (inherited behavior). - // When the target model belongs to another known family (such as - // Grok or Gemini served through a Responses router like OpenRouter), - // preserve signatures that match that target family. keepForeign - // additionally preserves unknown-format blobs a third-party - // Responses target may natively own. + } else { + // Target model belongs to a known non-GPT, non-Meta family (such as + // Grok or Gemini served through a Responses router like OpenRouter). + // Only preserve signatures that match that target family (or + // unknown-format blobs when keepForeign is enabled); foreign GPT or + // other known-provider envelopes are stripped. keep := false if rawSignature != "" { switch targetProvider { case signature.SignatureProviderGrok: - keep = signature.IsValidGrokEncryptedContent(rawSignature) + if grokInfo, errGrok := signature.InspectGrokEncryptedContent(rawSignature); errGrok == nil { + keep = true + if grokInfo.RouterSuffix != "" && !allowRouterSuffix { + rewrittenSignature = stripRouterSuffix(rawSignature, grokInfo.RouterSuffix) + } + } case signature.SignatureProviderGemini, signature.SignatureProviderClaude, signature.SignatureProviderKimi, signature.SignatureProviderSWE: - _, keep = signature.CompatibleSignatureForProvider(targetProvider, rawSignature) + var normalized string + normalized, keep = signature.CompatibleSignatureForProvider(targetProvider, rawSignature) + if keep && normalized != "" && normalized != rawSignature { + rewrittenSignature = normalized + } } if !keep && keepForeign { keep = signature.DetectSignatureProvider(rawSignature) == signature.SignatureProviderUnknown } } if !keep { - reason = err.Error() + reason = fmt.Sprintf("reasoning encrypted_content is not compatible with target provider %s", targetProvider) } } case gjson.Null: @@ -299,6 +366,13 @@ func sanitizeOpenAIResponsesReasoningEncryptedContentWithOptions(ctx context.Con reason = fmt.Sprintf("encrypted_content must be a string, got %s", encryptedContent.Type.String()) } if reason == "" { + if rewrittenSignature != "" { + if updatedItem, errSet := sjson.Set(nextItem, "encrypted_content", rewrittenSignature); errSet == nil { + nextItem = updatedItem + changed = true + helps.LogWithRequestID(ctx).Debugf("%s: stripped router suffix from reasoning encrypted_content at input[%d] item_id=%q", provider, index, itemID) + } + } if !changed { keep(item.Raw) continue diff --git a/internal/runtime/executor/openai_responses_signature_test.go b/internal/runtime/executor/openai_responses_signature_test.go index 2ee6f70c358..15fb4f60793 100644 --- a/internal/runtime/executor/openai_responses_signature_test.go +++ b/internal/runtime/executor/openai_responses_signature_test.go @@ -6,6 +6,8 @@ import ( "strings" "testing" + internalcache "github.com/router-for-me/CLIProxyAPI/v8/internal/cache" + cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor" "github.com/tidwall/gjson" ) @@ -380,20 +382,26 @@ func TestSanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel_RoutedM } } - // A Meta-family model name enables routed replay regardless of the - // compat flag: an OpenRouter codex key without is-compat must still - // replay Muse envelopes. + // A Meta-family model name on an OpenRouter baseURL preserves routed + // envelopes verbatim regardless of the compat flag, while non-OpenRouter + // third-party baseURLs strip OpenRouter's private suffix down to the + // native Meta envelope. for _, isCompat := range []bool{false, true} { - got := sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(context.Background(), "test", buildBody(), isCompat, "meta/muse-wonder") + got := sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModelAndBaseURL(context.Background(), "test", buildBody(), isCompat, "meta/muse-wonder", "https://openrouter.ai/api/v1") assertRoutedTarget(t, got) } // Namespaced router model IDs resolve to their Meta tail segment. - got := sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel(context.Background(), "test", buildBody(), false, "openrouter/meta/muse-wonder") + got := sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModelAndBaseURL(context.Background(), "test", buildBody(), false, "openrouter/meta/muse-wonder", "https://openrouter.ai/api/v1") assertRoutedTarget(t, got) - // Third-party Meta executor baseURL (non-api.meta.ai) preserves routed Meta - // envelopes while native api.meta.ai strips the router suffix. + otherProviderMeta := sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModelAndBaseURL(context.Background(), "test", buildBody(), false, "meta/muse-wonder", "https://example-proxy.internal/v1") + if gotSig := gjson.GetBytes(otherProviderMeta, "input.1.encrypted_content").String(); gotSig != native { + t.Fatalf("non-OpenRouter provider should strip OpenRouter suffix to native Meta envelope, got %q want %q", gotSig, native) + } + + // OpenRouter Meta executor baseURL preserves routed Meta envelopes while + // native api.meta.ai drops router-suffixed envelopes completely. routedMetaURL := sanitizeOpenAIResponsesReasoningEncryptedContentForMetaBaseURL(context.Background(), "test", buildBody(), "https://openrouter.ai/api/v1") assertRoutedTarget(t, routedMetaURL) @@ -430,6 +438,110 @@ func TestSanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModel_StripsM } } +func TestSanitizeOpenAIResponsesReasoningEncryptedContent_StripsRouterSuffixOnNonOpenRouterUpstreams(t *testing.T) { + gptNative := validOpenAIResponsesReasoningEncryptedContentForTest() + suffix := strings.Repeat("Ab3", 27)[:79] + gptRouted := gptNative + "." + suffix + + body := []byte(`{"store":false,"input":[{"type":"reasoning","summary":[],"encrypted_content":"` + gptRouted + `"}]}`) + + // Official Codex / non-OpenRouter upstreams must strip the private OpenRouter suffix. + gotOfficial := sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModelAndBaseURL( + context.Background(), + "test", + body, + false, + "gpt-5.4", + "https://chatgpt.com/backend-api/codex", + ) + if gotSig := gjson.GetBytes(gotOfficial, "input.0.encrypted_content").String(); gotSig != gptNative { + t.Fatalf("official Codex encrypted_content = %q, want stripped native %q", gotSig, gptNative) + } + + // OpenRouter upstream preserves its router suffix verbatim. + gotOpenRouter := sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModelAndBaseURL( + context.Background(), + "test", + body, + false, + "openrouter/openai/o4-mini", + "https://openrouter.ai/api/v1", + ) + if gotSig := gjson.GetBytes(gotOpenRouter, "input.0.encrypted_content").String(); gotSig != gptRouted { + t.Fatalf("OpenRouter encrypted_content = %q, want verbatim %q", gotSig, gptRouted) + } + + // Foreign GPT encrypted_content must be stripped when targeting a non-GPT family (e.g. Grok or Gemini on OpenRouter). + for _, model := range []string{"openrouter/x-ai/grok-4.7", "openrouter/google/gemini-3.8-flash"} { + gotForeign := sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModelAndBaseURL( + context.Background(), + "test", + body, + true, + model, + "https://openrouter.ai/api/v1", + ) + if gjson.GetBytes(gotForeign, "input.0.encrypted_content").Exists() { + t.Fatalf("model=%s: foreign GPT encrypted_content should be stripped; body=%s", model, gotForeign) + } + } + + // Cache-prefixed compatible signatures (e.g. "swe#sealed.v1.sig" or "gpt#...") + // must be rewritten to their normalized unprefixed form in encrypted_content. + prefixedSWEBody := []byte(`{"store":false,"input":[{"type":"reasoning","summary":[],"encrypted_content":"swe#sealed.v1.token"}]}`) + gotSWE := sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModelAndBaseURL( + context.Background(), + "test", + prefixedSWEBody, + true, + "devin/swe-2", + "https://openrouter.ai/api/v1", + ) + if gotSig := gjson.GetBytes(gotSWE, "input.0.encrypted_content").String(); gotSig != "sealed.v1.token" { + t.Fatalf("prefixed SWE encrypted_content = %q, want normalized %q", gotSig, "sealed.v1.token") + } + + // Gemini carrier wrapping a Meta signature must never be misclassified as a + // Devin sealed signature. + metaCarrier := base64.StdEncoding.EncodeToString([]byte("cpa-codex-gemini-carrier-v1:Q-PaDgE" + strings.Repeat("A", 1419))) + if sigBytes, sigType := parseSignatureBytes(metaCarrier); len(sigBytes) != 0 || sigType != "" { + t.Fatalf("parseSignatureBytes(metaCarrier) = (%q, %q), want empty", sigBytes, sigType) + } + + // Replay cache backfill + channel switch: a routed signature cached from + // OpenRouter is preserved on OpenRouter and stripped to native on official Codex. + internalcache.ClearCodexReasoningReplayCache() + t.Cleanup(internalcache.ClearCodexReasoningReplayCache) + const sessionKey = "execution:test-channel-switch" + internalcache.CacheCodexReasoningReplayItem("o4-mini", sessionKey, []byte(`{"type":"reasoning","summary":[],"encrypted_content":"`+gptRouted+`"}`)) + replayReq := cliproxyexecutor.Request{ + Model: "o4-mini", + Payload: []byte(`{"model":"o4-mini","input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"next"}]}]}`), + } + replayOpts := cliproxyexecutor.Options{ + SourceFormat: "claude", + Metadata: map[string]any{ + cliproxyexecutor.ExecutionSessionMetadataKey: "test-channel-switch", + }, + } + injected, _, errReplay := applyCodexReasoningReplayCacheRequired(context.Background(), "claude", replayReq, replayOpts, replayReq.Payload) + if errReplay != nil { + t.Fatalf("applyCodexReasoningReplayCacheRequired error = %v", errReplay) + } + sanitizedOpenRouter := sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModelAndBaseURL( + context.Background(), "test", injected, false, "o4-mini", "https://openrouter.ai/api/v1", + ) + if gotSig := gjson.GetBytes(sanitizedOpenRouter, "input.0.encrypted_content").String(); gotSig != gptRouted { + t.Fatalf("replay cache -> OpenRouter encrypted_content = %q, want routed %q", gotSig, gptRouted) + } + sanitizedOfficial := sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModelAndBaseURL( + context.Background(), "test", injected, false, "o4-mini", "https://chatgpt.com/backend-api/codex", + ) + if gotSig := gjson.GetBytes(sanitizedOfficial, "input.0.encrypted_content").String(); gotSig != gptNative { + t.Fatalf("replay cache -> official Codex encrypted_content = %q, want stripped native %q", gotSig, gptNative) + } +} + func BenchmarkSanitizeOpenAIResponsesReasoningEncryptedContentLargeNoopPayload(b *testing.B) { body := []byte(`{"store":false,"input":[{"type":"message","role":"user","content":"` + strings.Repeat("x", 8<<20) + `"}]}`) b.ReportAllocs() diff --git a/internal/runtime/executor/xai_executor_execute.go b/internal/runtime/executor/xai_executor_execute.go index be546f86473..5d088aa0451 100644 --- a/internal/runtime/executor/xai_executor_execute.go +++ b/internal/runtime/executor/xai_executor_execute.go @@ -47,6 +47,7 @@ func (e *XAIExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, req reporter.SetTranslatedReasoningEffort(prepared.body, e.Identifier()) url := strings.TrimSuffix(baseURL, "/") + "/responses" + prepared.body = sanitizeXAIInputEncryptedContentForBaseURL(prepared.body, baseURL) prepared.body = prepared.finalizePayload(prepared.body) httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(prepared.body)) if err != nil { @@ -212,6 +213,7 @@ func (e *XAIExecutor) executeCompactRequest(ctx context.Context, auth *cliproxya reporter.SetTranslatedReasoningEffort(prepared.body, e.Identifier()) requestURL := strings.TrimSuffix(baseURL, "/") + "/responses/compact" + prepared.body = sanitizeXAIInputEncryptedContentForBaseURL(prepared.body, baseURL) prepared.body = prepared.finalizePayload(prepared.body) httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL, bytes.NewReader(prepared.body)) if err != nil { diff --git a/internal/runtime/executor/xai_executor_request.go b/internal/runtime/executor/xai_executor_request.go index 98f3651d724..edbecb2724a 100644 --- a/internal/runtime/executor/xai_executor_request.go +++ b/internal/runtime/executor/xai_executor_request.go @@ -12,6 +12,7 @@ import ( "github.com/google/uuid" xaiauth "github.com/router-for-me/CLIProxyAPI/v8/internal/auth/xai" "github.com/router-for-me/CLIProxyAPI/v8/internal/runtime/executor/helps" + "github.com/router-for-me/CLIProxyAPI/v8/internal/signature" "github.com/router-for-me/CLIProxyAPI/v8/internal/thinking" "github.com/router-for-me/CLIProxyAPI/v8/internal/util" cliproxyauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth" @@ -58,6 +59,13 @@ type xaiClientToolKey struct { toolType string } +func resolveXAISignatureTargetModel(baseModel string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) string { + if resolved := resolveResponsesSignatureTargetModel(baseModel, req, opts); signature.SignatureProviderFromModelName(resolved) == signature.SignatureProviderGrok { + return resolved + } + return "grok" +} + func (e *XAIExecutor) prepareResponsesRequest(ctx context.Context, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, stream bool) (*xaiPreparedRequest, error) { return e.prepareResponsesRequestTo(ctx, req, opts, stream, sdktranslator.FormatCodex) } @@ -71,12 +79,15 @@ func (e *XAIExecutor) prepareResponsesRequestTo(ctx context.Context, req cliprox originalPayloadSource = opts.OriginalRequest } originalPayload := bytes.Clone(originalPayloadSource) - originalTranslated := helps.TranslateRequestWithAPIKeyModelCompatibilityForExecutor(ctx, opts.Headers, e.cfg, e.Identifier(), from, to, baseModel, originalPayload, stream, helps.APIKeyModelIsCompat(req)) + targetModel := resolveXAISignatureTargetModel(baseModel, req, opts) + originalTranslated := helps.TranslateRequestWithAPIKeyModelCompatibilityForExecutor(ctx, opts.Headers, e.cfg, e.Identifier(), from, to, targetModel, originalPayload, stream, helps.APIKeyModelIsCompat(req)) + originalTranslated = helps.SetStringIfDifferent(originalTranslated, "model", baseModel) originalTranslated = preserveXAIResponsesOutputControls(originalTranslated, originalPayload, from) - body, updatesChanged, err := helps.TranslateRequestWithAPIKeyModelCompatibilityAndUpdateIntentForExecutor(ctx, opts.Headers, e.cfg, e.Identifier(), from, to, baseModel, bytes.Clone(req.Payload), stream, helps.APIKeyModelIsCompat(req)) + body, updatesChanged, err := helps.TranslateRequestWithAPIKeyModelCompatibilityAndUpdateIntentForExecutor(ctx, opts.Headers, e.cfg, e.Identifier(), from, to, targetModel, bytes.Clone(req.Payload), stream, helps.APIKeyModelIsCompat(req)) if err != nil { return nil, err } + body = helps.SetStringIfDifferent(body, "model", baseModel) body = preserveXAIResponsesOutputControls(body, req.Payload, from) body, err = helps.ApplyRequestThinking(body, req, opts, from.String(), e.Identifier(), e.Identifier(), updatesChanged) @@ -143,7 +154,7 @@ func (e *XAIExecutor) prepareResponsesRequestTo(ctx context.Context, req cliprox body = aliasXAIClientWebSearchInput(body, webSearchAlias, namespaceTools) } body = normalizeXAIInputReasoningItems(body) - body = sanitizeXAIInputEncryptedContent(body) + body = sanitizeXAIInputEncryptedContentWithOptions(body, true) body = normalizeCodexInstructions(body) // stop is supported by Chat Completions but not by xAI's Responses API. // Thinking was handled before payload overrides and must not be revalidated here. diff --git a/internal/runtime/executor/xai_executor_response.go b/internal/runtime/executor/xai_executor_response.go index 11fe695b19d..64c0b984bc3 100644 --- a/internal/runtime/executor/xai_executor_response.go +++ b/internal/runtime/executor/xai_executor_response.go @@ -678,6 +678,14 @@ func xaiFunctionParametersNeedSimplification(tool gjson.Result, namespaceName st } func sanitizeXAIInputEncryptedContent(body []byte) []byte { + return sanitizeXAIInputEncryptedContentWithOptions(body, false) +} + +func sanitizeXAIInputEncryptedContentForBaseURL(body []byte, baseURL string) []byte { + return sanitizeXAIInputEncryptedContentWithOptions(body, isOpenRouterUpstreamURL(baseURL)) +} + +func sanitizeXAIInputEncryptedContentWithOptions(body []byte, allowRouterSuffix bool) []byte { input := gjson.GetBytes(body, "input") if !input.Exists() || !input.IsArray() { return body @@ -699,10 +707,14 @@ func sanitizeXAIInputEncryptedContent(body []byte) []byte { continue } reason := "" + rewrittenSig := "" switch encryptedContent.Type { case gjson.String: - if _, err := signature.InspectGrokEncryptedContent(encryptedContent.String()); err != nil { + rawSig := encryptedContent.String() + if info, err := signature.InspectGrokEncryptedContent(rawSig); err != nil { reason = err.Error() + } else if info.RouterSuffix != "" && !allowRouterSuffix { + rewrittenSig = stripRouterSuffix(rawSig, info.RouterSuffix) } case gjson.Null: reason = "encrypted_content is null" @@ -710,6 +722,13 @@ func sanitizeXAIInputEncryptedContent(body []byte) []byte { reason = fmt.Sprintf("encrypted_content must be a string, got %s", encryptedContent.Type.String()) } if reason == "" { + if rewrittenSig != "" { + if next, errSet := sjson.SetBytes([]byte(item.Raw), "encrypted_content", rewrittenSig); errSet == nil { + items = append(items, json.RawMessage(next)) + changed = true + continue + } + } items = append(items, json.RawMessage(item.Raw)) continue } diff --git a/internal/runtime/executor/xai_executor_stream.go b/internal/runtime/executor/xai_executor_stream.go index f6aad351b49..69814311b09 100644 --- a/internal/runtime/executor/xai_executor_stream.go +++ b/internal/runtime/executor/xai_executor_stream.go @@ -41,6 +41,7 @@ func (e *XAIExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Auth reporter.SetTranslatedReasoningEffort(prepared.body, e.Identifier()) url := strings.TrimSuffix(baseURL, "/") + "/responses" + prepared.body = sanitizeXAIInputEncryptedContentForBaseURL(prepared.body, baseURL) prepared.body = prepared.finalizePayload(prepared.body) httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(prepared.body)) if err != nil { diff --git a/internal/runtime/executor/xai_executor_test.go b/internal/runtime/executor/xai_executor_test.go index fc76f3d44ac..1ed1cbb6b6b 100644 --- a/internal/runtime/executor/xai_executor_test.go +++ b/internal/runtime/executor/xai_executor_test.go @@ -5269,6 +5269,18 @@ func TestSanitizeXAIInputEncryptedContent_PreservesValidBlob(t *testing.T) { if gotEnc := gjson.GetBytes(got, "input.0.encrypted_content").String(); gotEnc != sample { t.Fatalf("valid encrypted_content should be preserved, got %q", gotEnc) } + + routedSample := sample + "." + strings.Repeat("Ab3", 21)[:62] + routedBody, _ := sjson.SetBytes(body, "input.0.encrypted_content", routedSample) + gotRouted := sanitizeXAIInputEncryptedContent(routedBody) + if gotEnc := gjson.GetBytes(gotRouted, "input.0.encrypted_content").String(); gotEnc != sample { + t.Fatalf("router suffix should be stripped for native xAI upstream, got %q want %q", gotEnc, sample) + } + + gotOpenRouter := sanitizeXAIInputEncryptedContentForBaseURL(routedBody, "https://openrouter.ai/api/v1") + if gotEnc := gjson.GetBytes(gotOpenRouter, "input.0.encrypted_content").String(); gotEnc != routedSample { + t.Fatalf("router suffix should be preserved for OpenRouter xAI upstream, got %q want %q", gotEnc, routedSample) + } } func TestXAIExecutorReMergesReasoningAfterDroppingInvalidEncryptedContent(t *testing.T) { diff --git a/internal/runtime/executor/xai_reasoning_replay.go b/internal/runtime/executor/xai_reasoning_replay.go index e8e5b2cbdb9..3cb31650c51 100644 --- a/internal/runtime/executor/xai_reasoning_replay.go +++ b/internal/runtime/executor/xai_reasoning_replay.go @@ -8,6 +8,7 @@ import ( internalcache "github.com/router-for-me/CLIProxyAPI/v8/internal/cache" "github.com/router-for-me/CLIProxyAPI/v8/internal/runtime/executor/helps" + "github.com/router-for-me/CLIProxyAPI/v8/internal/signature" "github.com/router-for-me/CLIProxyAPI/v8/internal/thinking" cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor" sdktranslator "github.com/router-for-me/CLIProxyAPI/v8/sdk/translator" @@ -93,8 +94,17 @@ func xaiReasoningReplayEnabledForSource(from sdktranslator.Format) bool { sourceFormatEqual(from, sdktranslator.FormatOpenAIResponse) } +func canonicalXAIReasoningReplaySignatureKey(rawSignature string) string { + sig := signature.SignaturePayloadWithoutProviderPrefix(strings.TrimSpace(rawSignature)) + if info, err := signature.InspectGrokEncryptedContent(sig); err == nil && info.RouterSuffix != "" { + return stripRouterSuffix(sig, info.RouterSuffix) + } + return sig +} + func xaiInputHasReasoningEncryptedContent(inputItems []gjson.Result, encryptedContent string) bool { - if encryptedContent == "" { + canonicalTarget := canonicalXAIReasoningReplaySignatureKey(encryptedContent) + if canonicalTarget == "" { return false } for _, item := range inputItems { @@ -105,7 +115,7 @@ func xaiInputHasReasoningEncryptedContent(inputItems []gjson.Result, encryptedCo if inputEncryptedContent.Type != gjson.String { continue } - if inputEncryptedContent.String() == encryptedContent { + if canonicalXAIReasoningReplaySignatureKey(inputEncryptedContent.String()) == canonicalTarget { return true } } diff --git a/internal/runtime/executor/xai_websockets_executor.go b/internal/runtime/executor/xai_websockets_executor.go index 6fa16db39c9..ba7a54e7c21 100644 --- a/internal/runtime/executor/xai_websockets_executor.go +++ b/internal/runtime/executor/xai_websockets_executor.go @@ -519,6 +519,7 @@ func (e *XAIWebsocketsExecutor) ExecuteStream(ctx context.Context, auth *cliprox } reporter.SetTranslatedReasoningEffort(prepared.body, e.Identifier()) + prepared.body = sanitizeXAIInputEncryptedContentForBaseURL(prepared.body, baseURL) wsHeaders := applyXAIWebsocketHeaders(ctx, http.Header{}, auth, token, prepared.sessionID, opts.Headers) wsReqBody := buildXAIWebsocketRequestBody(prepared.body, prepared.finalizePayload) requestType := strings.TrimSpace(gjson.GetBytes(req.Payload, "type").String()) diff --git a/internal/signature/grok_validation.go b/internal/signature/grok_validation.go index 7fdcd2d60e3..60fdb67de31 100644 --- a/internal/signature/grok_validation.go +++ b/internal/signature/grok_validation.go @@ -113,6 +113,9 @@ func InspectGrokEncryptedContent(raw string) (*GrokEncryptedContentInfo, error) if err != nil { return nil, err } + if strings.HasPrefix(string(decoded), "cpa-codex-gemini-carrier-v1:") { + return nil, fmt.Errorf("invalid Grok encrypted_content: carries Gemini Codex thoughtSignature carrier") + } if len(decoded) < MinGrokEncryptedContentDecodedLen { return nil, fmt.Errorf("invalid Grok encrypted_content: decoded payload too short (%d bytes)", len(decoded)) } diff --git a/internal/translator/codex/claude/codex_claude_request.go b/internal/translator/codex/claude/codex_claude_request.go index d81713e8319..4e45a14fa2d 100644 --- a/internal/translator/codex/claude/codex_claude_request.go +++ b/internal/translator/codex/claude/codex_claude_request.go @@ -175,7 +175,8 @@ func convertClaudeRequestToCodex(modelName string, inputRawJSON []byte, _ bool, } rawSignature := part.Get("signature").String() - signature, ok := translatorcommon.CodexReplaySignatureForTarget(modelName, rawSignature) + signatureTargetModel := translatorcommon.ResolveCodexReplayTargetModel(modelName, rawJSON) + signature, ok := translatorcommon.CodexReplaySignatureForTarget(signatureTargetModel, rawSignature) if !ok && preserveEmptyThinkingBlocks && part.Get("signature").Type == gjson.String && strings.TrimSpace(rawSignature) != "" && sigcompat.DetectSignatureProviderForBlock(rawSignature, sigcompat.SignatureBlockKindClaudeThinking) == sigcompat.SignatureProviderUnknown { signature = rawSignature diff --git a/internal/translator/codex/claude/codex_claude_request_test.go b/internal/translator/codex/claude/codex_claude_request_test.go index 6abc910e2a1..af2c6230f9a 100644 --- a/internal/translator/codex/claude/codex_claude_request_test.go +++ b/internal/translator/codex/claude/codex_claude_request_test.go @@ -782,6 +782,13 @@ func TestConvertClaudeRequestToCodex_AssistantRoutedMetaSignatureToReasoningItem if got := countRequestInputItemsByType(out, "reasoning"); got != 0 { t.Fatalf("got %d reasoning items for GPT signature on Meta target, want 0; output=%s", got, out) } + + // When modelName is an unmapped upstream deployment alias, the client-requested + // Meta Muse model in the payload preserves Meta signatures. + aliasOut, _ := ConvertClaudeRequestToCodex("deployment-123", payload, false) + if got := countRequestInputItemsByType(aliasOut, "reasoning"); got != 1 { + t.Fatalf("got %d reasoning items for aliased Meta target, want 1; output=%s", got, aliasOut) + } } func TestConvertClaudeRequestToCodex_IgnoresGrokSignatureForNonGrokTargets(t *testing.T) { diff --git a/internal/translator/codex/claude/codex_claude_response.go b/internal/translator/codex/claude/codex_claude_response.go index cc144cb1df0..fd7bc2e1846 100644 --- a/internal/translator/codex/claude/codex_claude_response.go +++ b/internal/translator/codex/claude/codex_claude_response.go @@ -120,10 +120,11 @@ func ConvertCodexResponseToClaude(_ context.Context, _ string, originalRequestRa output = append(output, startCodexThinkingBlock(params)...) } params.ThinkingSummarySeen = true - case "response.reasoning_summary_text.delta": + case "response.reasoning_summary_text.delta", "response.reasoning_text.delta": output = append(output, stopCodexTextBlock(params)...) output = append(output, startCodexThinkingBlock(params)...) output = append(output, appendCodexThinkingDelta(params, rootResult.Get("delta").String())...) + params.ThinkingSummarySeen = true case "response.reasoning_summary_part.done": // Intentionally does not close the thinking block: it stays open until // output_item.done delivers the reasoning item's final encrypted_content. @@ -250,6 +251,13 @@ func ConvertCodexResponseToClaude(_ context.Context, _ string, originalRequestRa if signature := itemResult.Get("encrypted_content").String(); signature != "" { params.ThinkingSignature = signature } + if !params.ThinkingSummarySeen { + if doneText := extractCodexReasoningItemText(itemResult); doneText != "" { + output = append(output, startCodexThinkingBlock(params)...) + output = append(output, appendCodexThinkingDelta(params, doneText)...) + params.ThinkingSummarySeen = true + } + } if params.ThinkingSummarySeen { output = append(output, finalizeCodexThinkingBlock(params)...) } else { @@ -385,41 +393,11 @@ func ConvertCodexResponseToClaudeNonStream(_ context.Context, _ string, original output.ForEach(func(_, item gjson.Result) bool { switch item.Get("type").String() { case "reasoning": - thinkingBuilder := strings.Builder{} + thinkingText := extractCodexReasoningItemText(item) signature := item.Get("encrypted_content").String() - if summary := item.Get("summary"); summary.Exists() { - if summary.IsArray() { - summary.ForEach(func(_, part gjson.Result) bool { - if txt := part.Get("text"); txt.Exists() { - thinkingBuilder.WriteString(txt.String()) - } else { - thinkingBuilder.WriteString(part.String()) - } - return true - }) - } else { - thinkingBuilder.WriteString(summary.String()) - } - } - if thinkingBuilder.Len() == 0 { - if content := item.Get("content"); content.Exists() { - if content.IsArray() { - content.ForEach(func(_, part gjson.Result) bool { - if txt := part.Get("text"); txt.Exists() { - thinkingBuilder.WriteString(txt.String()) - } else { - thinkingBuilder.WriteString(part.String()) - } - return true - }) - } else { - thinkingBuilder.WriteString(content.String()) - } - } - } - if thinkingBuilder.Len() > 0 || signature != "" { + if thinkingText != "" || signature != "" { block := []byte(`{"type":"thinking","thinking":""}`) - block, _ = sjson.SetBytes(block, "thinking", thinkingBuilder.String()) + block, _ = sjson.SetBytes(block, "thinking", thinkingText) if signature != "" { block, _ = sjson.SetBytes(block, "signature", signature) } @@ -917,6 +895,41 @@ func stopCodexTextBlock(params *ConvertCodexResponseToClaudeParams) []byte { return translatorcommon.AppendSSEEventBytes(nil, "content_block_stop", template, 2) } +func extractCodexReasoningItemText(item gjson.Result) string { + thinkingBuilder := strings.Builder{} + if summary := item.Get("summary"); summary.Exists() { + if summary.IsArray() { + summary.ForEach(func(_, part gjson.Result) bool { + if txt := part.Get("text"); txt.Exists() { + thinkingBuilder.WriteString(txt.String()) + } else { + thinkingBuilder.WriteString(part.String()) + } + return true + }) + } else { + thinkingBuilder.WriteString(summary.String()) + } + } + if thinkingBuilder.Len() == 0 { + if content := item.Get("content"); content.Exists() { + if content.IsArray() { + content.ForEach(func(_, part gjson.Result) bool { + if txt := part.Get("text"); txt.Exists() { + thinkingBuilder.WriteString(txt.String()) + } else { + thinkingBuilder.WriteString(part.String()) + } + return true + }) + } else { + thinkingBuilder.WriteString(content.String()) + } + } + } + return thinkingBuilder.String() +} + func startCodexThinkingBlock(params *ConvertCodexResponseToClaudeParams) []byte { if params.ThinkingBlockOpen { return nil diff --git a/internal/translator/codex/gemini/codex_gemini_request.go b/internal/translator/codex/gemini/codex_gemini_request.go index a1f4749e85b..2fce6b1d3b7 100644 --- a/internal/translator/codex/gemini/codex_gemini_request.go +++ b/internal/translator/codex/gemini/codex_gemini_request.go @@ -71,6 +71,7 @@ func ConvertGeminiRequestToCodex(modelName string, inputRawJSON []byte, _ bool) var pendingCallIDs []string callCounter := 0 emittedThoughtSignatures := map[string]bool{} + signatureTargetModel := translatorcommon.ResolveCodexReplayTargetModel(modelName, inputRawJSON) getGeminiCallID := func(value gjson.Result) string { if callID := strings.TrimSpace(value.Get("id").String()); callID != "" { @@ -140,6 +141,7 @@ func ConvertGeminiRequestToCodex(modelName string, inputRawJSON []byte, _ bool) continue } parr := parts.Array() + var pendingThoughtSummaryParts [][]byte for j := 0; j < len(parr); j++ { p := parr[j] @@ -151,19 +153,34 @@ func ConvertGeminiRequestToCodex(modelName string, inputRawJSON []byte, _ bool) // the existing hidden-thought drop below. if role == "assistant" { if rawSignature := geminiPartThoughtSignature(p); rawSignature != "" { - if normalizedSignature, ok := normalizedGeminiThoughtSignatureForCodexTarget(modelName, rawSignature); ok && !emittedThoughtSignatures[normalizedSignature] { + if normalizedSignature, ok := normalizedGeminiThoughtSignatureForCodexTarget(signatureTargetModel, rawSignature); ok && !emittedThoughtSignatures[normalizedSignature] { reasoningItem := []byte(`{"type":"reasoning","encrypted_content":"","summary":[]}`) reasoningItem, _ = sjson.SetBytes(reasoningItem, "encrypted_content", normalizedSignature) + summaryParts := pendingThoughtSummaryParts + pendingThoughtSummaryParts = nil if translatorcommon.IsGeminiThoughtPart(p) { - if thoughtText := strings.TrimSpace(p.Get("text").String()); thoughtText != "" { + if thoughtText := p.Get("text").String(); thoughtText != "" { summaryPart := []byte(`{"type":"summary_text","text":""}`) summaryPart, _ = sjson.SetBytes(summaryPart, "text", thoughtText) - reasoningItem, _ = sjson.SetRawBytes(reasoningItem, "summary", translatorcommon.JoinRawArray([][]byte{summaryPart})) + summaryParts = append(summaryParts, summaryPart) } } + if len(summaryParts) > 0 { + reasoningItem, _ = sjson.SetRawBytes(reasoningItem, "summary", translatorcommon.JoinRawArray(summaryParts)) + } inputItems = append(inputItems, reasoningItem) emittedThoughtSignatures[normalizedSignature] = true + } else { + pendingThoughtSummaryParts = nil } + } else if translatorcommon.IsGeminiThoughtPart(p) { + if thoughtText := p.Get("text").String(); thoughtText != "" { + summaryPart := []byte(`{"type":"summary_text","text":""}`) + summaryPart, _ = sjson.SetBytes(summaryPart, "text", thoughtText) + pendingThoughtSummaryParts = append(pendingThoughtSummaryParts, summaryPart) + } + } else { + pendingThoughtSummaryParts = nil } } diff --git a/internal/translator/codex/gemini/codex_gemini_response.go b/internal/translator/codex/gemini/codex_gemini_response.go index 082bc123dad..a6a9049baa9 100644 --- a/internal/translator/codex/gemini/codex_gemini_response.go +++ b/internal/translator/codex/gemini/codex_gemini_response.go @@ -39,6 +39,11 @@ type ConvertCodexResponseToGeminiParams struct { // SeenReasoningSignatures deduplicates repeated reasoning done events // by item ID + signature hash within one response stream. SeenReasoningSignatures map[[32]byte]bool + // HasReasoningDelta and StreamedReasoningItems record whether reasoning + // deltas were already emitted for the current or keyed reasoning item so + // done-only reasoning summaries can fall back cleanly. + HasReasoningDelta bool + StreamedReasoningItems map[string]bool } // ConvertCodexResponseToGemini converts Codex streaming response format to Gemini format. @@ -141,26 +146,49 @@ func ConvertCodexResponseToGemini(_ context.Context, modelName string, originalR itemType := itemResult.Get("type").String() if itemType == "reasoning" { signature := strings.TrimSpace(itemResult.Get("encrypted_content").String()) - if signature == "" { - return [][]byte{} + itemID := strings.TrimSpace(itemResult.Get("id").String()) + hadStreamedReasoning := params.HasReasoningDelta + if itemID != "" && len(params.StreamedReasoningItems) > 0 { + hadStreamedReasoning = params.StreamedReasoningItems[itemID] } - seenKey := sha256.Sum256([]byte(itemResult.Get("id").String() + "\x00" + signature)) - if params.SeenReasoningSignatures == nil { - params.SeenReasoningSignatures = make(map[[32]byte]bool) + params.HasReasoningDelta = false + var thoughtTexts []string + if !hadStreamedReasoning { + thoughtTexts = extractCodexGeminiReasoningTexts(itemResult) } - if params.SeenReasoningSignatures[seenKey] { + if signature == "" && len(thoughtTexts) == 0 { return [][]byte{} } - params.SeenReasoningSignatures[seenKey] = true - if params.PendingThoughtSignature != "" { + if signature != "" { + seenKey := sha256.Sum256([]byte(itemID + "\x00" + signature)) + if params.SeenReasoningSignatures == nil { + params.SeenReasoningSignatures = make(map[[32]byte]bool) + } + if params.SeenReasoningSignatures[seenKey] { + return [][]byte{} + } + params.SeenReasoningSignatures[seenKey] = true + } + var emittedParts [][]byte + if params.PendingThoughtSignature != "" && signature != "" { // A previous reasoning step never got a visible carrier part; // flush it as an empty thought part so no signature is lost. part := setGeminiThoughtSignature([]byte(`{"thought":true,"text":""}`), params.PendingThoughtSignature) + params.PendingThoughtSignature = "" + emittedParts = append(emittedParts, part) + } + for _, thoughtText := range thoughtTexts { + part := []byte(`{"thought":true,"text":""}`) + part, _ = sjson.SetBytes(part, "text", thoughtText) + emittedParts = append(emittedParts, part) + } + if signature != "" { params.PendingThoughtSignature = signature - template = translatorcommon.SetRawArrayItems(template, "candidates.0.content.parts", [][]byte{part}) + } + if len(emittedParts) > 0 { + template = translatorcommon.SetRawArrayItems(template, "candidates.0.content.parts", emittedParts) return flushStorageOutputs(template) } - params.PendingThoughtSignature = signature return [][]byte{} } if itemType == "image_generation_call" { @@ -230,14 +258,39 @@ func ConvertCodexResponseToGemini(_ context.Context, modelName string, originalR params.StorageOutputs = nil params.PendingThoughtSignature = "" params.SeenReasoningSignatures = nil + params.HasReasoningDelta = false + params.StreamedReasoningItems = nil params.HasOutputTextDelta = false template, _ = sjson.SetBytes(template, "modelVersion", rootResult.Get("response.model").String()) template, _ = sjson.SetBytes(template, "responseId", rootResult.Get("response.id").String()) params.ResponseID = rootResult.Get("response.id").String() - } else if typeStr == "response.reasoning_summary_text.delta" { // Handle reasoning/thinking content delta + } else if typeStr == "response.output_item.added" && rootResult.Get("item.type").String() == "reasoning" { + params.HasReasoningDelta = false + if params.PendingThoughtSignature != "" { + part := setGeminiThoughtSignature([]byte(`{"thought":true,"text":""}`), params.PendingThoughtSignature) + params.PendingThoughtSignature = "" + template = translatorcommon.SetRawArrayItems(template, "candidates.0.content.parts", [][]byte{part}) + return flushStorageOutputs(template) + } + return [][]byte{} + } else if typeStr == "response.reasoning_summary_text.delta" || typeStr == "response.reasoning_text.delta" { // Handle reasoning/thinking content delta + params.HasReasoningDelta = true + if itemID := strings.TrimSpace(rootResult.Get("item_id").String()); itemID != "" { + if params.StreamedReasoningItems == nil { + params.StreamedReasoningItems = make(map[string]bool) + } + params.StreamedReasoningItems[itemID] = true + } + var emittedParts [][]byte + if params.PendingThoughtSignature != "" { + carrier := setGeminiThoughtSignature([]byte(`{"thought":true,"text":""}`), params.PendingThoughtSignature) + params.PendingThoughtSignature = "" + emittedParts = append(emittedParts, carrier) + } part := []byte(`{"thought":true,"text":""}`) part, _ = sjson.SetBytes(part, "text", rootResult.Get("delta").String()) - template = translatorcommon.SetRawArrayItems(template, "candidates.0.content.parts", [][]byte{part}) + emittedParts = append(emittedParts, part) + template = translatorcommon.SetRawArrayItems(template, "candidates.0.content.parts", emittedParts) } else if typeStr == "response.output_text.delta" { // Handle regular text content delta params.HasOutputTextDelta = true part := []byte(`{"text":""}`) @@ -382,29 +435,11 @@ func ConvertCodexResponseToGeminiNonStream(_ context.Context, modelName string, // With no summary text, emit an empty thought carrier part; // dropping the signature here would silently break replay. signature := strings.TrimSpace(value.Get("encrypted_content").String()) - var thoughtTexts []string - if summary := value.Get("summary"); summary.IsArray() { - for _, entry := range summary.Array() { - if text := entry.Get("text").String(); text != "" { - thoughtTexts = append(thoughtTexts, text) - } - } - } - if content := value.Get("content"); content.IsArray() { - for _, entry := range content.Array() { - if entry.Get("type").String() == "reasoning_text" { - if text := entry.Get("text").String(); text != "" { - thoughtTexts = append(thoughtTexts, text) - } - } - } - } else if content.Type == gjson.String && content.String() != "" { - thoughtTexts = append(thoughtTexts, content.String()) - } + thoughtTexts := extractCodexGeminiReasoningTexts(value) for i, thoughtText := range thoughtTexts { part := []byte(`{"text":"","thought":true}`) part, _ = sjson.SetBytes(part, "text", thoughtText) - if i == 0 && signature != "" { + if i == len(thoughtTexts)-1 && signature != "" { part = setGeminiThoughtSignature(part, signature) } parts = append(parts, part) @@ -519,6 +554,29 @@ func buildReverseMapFromGeminiOriginal(original []byte) map[string]string { // string verbatim makes official SDK []byte decoding fail. The carrier therefore // stores magic+rawSignature as the byte payload and relies on standard base64 // JSON encoding, which every Gemini client decodes and echoes losslessly. +func extractCodexGeminiReasoningTexts(value gjson.Result) []string { + var thoughtTexts []string + if summary := value.Get("summary"); summary.IsArray() { + for _, entry := range summary.Array() { + if text := entry.Get("text").String(); text != "" { + thoughtTexts = append(thoughtTexts, text) + } + } + } + if content := value.Get("content"); content.IsArray() { + for _, entry := range content.Array() { + if entry.Get("type").String() == "reasoning_text" { + if text := entry.Get("text").String(); text != "" { + thoughtTexts = append(thoughtTexts, text) + } + } + } + } else if content.Type == gjson.String && content.String() != "" { + thoughtTexts = append(thoughtTexts, content.String()) + } + return thoughtTexts +} + const geminiCodexThoughtSignatureCarrierMagic = "cpa-codex-gemini-carrier-v1:" func encodeGeminiCodexThoughtSignature(rawSignature string) string { diff --git a/internal/translator/codex/gemini/codex_gemini_response_test.go b/internal/translator/codex/gemini/codex_gemini_response_test.go index 4f20a40b516..ebded574606 100644 --- a/internal/translator/codex/gemini/codex_gemini_response_test.go +++ b/internal/translator/codex/gemini/codex_gemini_response_test.go @@ -3,6 +3,7 @@ package gemini import ( "context" "encoding/json" + "strings" "testing" "github.com/tidwall/gjson" @@ -249,6 +250,88 @@ func TestConvertCodexResponseToGemini_StreamMultipleFunctionCallsKeepSignatureAn } } +func TestConvertCodexResponseToGeminiNonStream_MultiSummaryRoundTrip(t *testing.T) { + ctx := context.Background() + signature := testGeminiMetaThoughtSignature() + raw := []byte(`{"type":"response.completed","response":{"id":"resp_multi","model":"muse-spark-1.3","created_at":1,"usage":{"input_tokens":1,"output_tokens":2},"output":[{"type":"reasoning","encrypted_content":"` + signature + `","summary":[{"type":"summary_text","text":"step A"},{"type":"summary_text","text":"step B"}]},{"type":"message","content":[{"type":"output_text","text":"final answer"}]}]}}`) + geminiOut := ConvertCodexResponseToGeminiNonStream(ctx, "muse-spark-1.3", nil, nil, raw, nil) + + partsRaw := gjson.GetBytes(geminiOut, "candidates.0.content.parts").Raw + reqPayload := []byte(`{"model":"muse-spark-1.3","contents":[{"role":"user","parts":[{"text":"hi"}]},{"role":"model","parts":` + partsRaw + `},{"role":"user","parts":[{"text":"next"}]}]}`) + codexIn, _ := ConvertGeminiRequestToCodex("deployment-alias", reqPayload, false) + + reasoning := gjson.GetBytes(codexIn, "input.1") + if reasoning.Get("type").String() != "reasoning" || reasoning.Get("encrypted_content").String() != signature { + t.Fatalf("round-trip reasoning item = %s, want Meta signature", reasoning.Raw) + } + summaries := reasoning.Get("summary").Array() + if len(summaries) != 2 || summaries[0].Get("text").String() != "step A" || summaries[1].Get("text").String() != "step B" { + t.Fatalf("round-trip summaries = %s, want [step A, step B]", reasoning.Get("summary").Raw) + } + + // Streaming done-only reasoning item without prior deltas must still emit + // its summary thought parts and attach the signature to the next text delta. + var streamParam any + doneOnly := []byte(`data: {"type":"response.output_item.done","item":{"id":"rs_done_only","type":"reasoning","encrypted_content":"` + signature + `","summary":[{"type":"summary_text","text":"done-only thought"}]}}`) + doneChunks := ConvertCodexResponseToGemini(ctx, "muse-spark-1.3", nil, nil, doneOnly, &streamParam) + if len(doneChunks) != 1 || !gjson.GetBytes(doneChunks[0], "candidates.0.content.parts.0.thought").Bool() || gjson.GetBytes(doneChunks[0], "candidates.0.content.parts.0.text").String() != "done-only thought" { + t.Fatalf("done-only reasoning chunks = %q, want thought summary chunk", doneChunks) + } + textChunks := ConvertCodexResponseToGemini(ctx, "muse-spark-1.3", nil, nil, []byte(`data: {"type":"response.output_text.delta","delta":"visible"}`), &streamParam) + if len(textChunks) != 1 || decodeGeminiCodexThoughtSignature(gjson.GetBytes(textChunks[0], "candidates.0.content.parts.0.thoughtSignature").String()) != signature { + t.Fatalf("text chunk after done-only reasoning = %q, want carrier signature", textChunks) + } + + // Two consecutive streamed reasoning items must flush the first item's + // signature before the second item's thought deltas so each reasoning item + // keeps its own summary on replay. + sig2 := signature + "." + strings.Repeat("Zy9", 21)[:62] + var consecutiveParam any + var streamedParts []string + for _, ev := range [][]byte{ + []byte(`data: {"type":"response.reasoning_summary_text.delta","item_id":"rs_1","delta":"first summary"}`), + []byte(`data: {"type":"response.output_item.done","item":{"id":"rs_1","type":"reasoning","encrypted_content":"` + signature + `","summary":[{"type":"summary_text","text":"first summary"}]}}`), + []byte(`data: {"type":"response.reasoning_summary_text.delta","item_id":"rs_2","delta":"second summary"}`), + []byte(`data: {"type":"response.output_item.done","item":{"id":"rs_2","type":"reasoning","encrypted_content":"` + sig2 + `","summary":[{"type":"summary_text","text":"second summary"}]}}`), + []byte(`data: {"type":"response.output_text.delta","delta":"answer"}`), + } { + for _, chunk := range ConvertCodexResponseToGemini(ctx, "muse-spark-1.3", nil, nil, ev, &consecutiveParam) { + for _, p := range gjson.GetBytes(chunk, "candidates.0.content.parts").Array() { + streamedParts = append(streamedParts, p.Raw) + } + } + } + replayReq := []byte(`{"model":"muse-spark-1.3","contents":[{"role":"user","parts":[{"text":"q"}]},{"role":"model","parts":[` + strings.Join(streamedParts, ",") + `]},{"role":"user","parts":[{"text":"next"}]}]}`) + replayCodex, _ := ConvertGeminiRequestToCodex("muse-spark-1.3", replayReq, false) + if gjson.GetBytes(replayCodex, "input.1.summary.0.text").String() != "first summary" || gjson.GetBytes(replayCodex, "input.2.summary.0.text").String() != "second summary" { + t.Fatalf("replayed consecutive reasoning items = %s, want separate summaries", replayCodex) + } + + // Whitespace-only and space-padded thought chunks must round-trip without + // trimming inter-word spaces. + var wsParam any + var wsParts []string + for _, ev := range [][]byte{ + []byte(`data: {"type":"response.reasoning_summary_text.delta","item_id":"rs_ws","delta":"Hello"}`), + []byte(`data: {"type":"response.reasoning_summary_text.delta","item_id":"rs_ws","delta":" "}`), + []byte(`data: {"type":"response.reasoning_summary_text.delta","item_id":"rs_ws","delta":"world"}`), + []byte(`data: {"type":"response.output_item.done","item":{"id":"rs_ws","type":"reasoning","encrypted_content":"` + signature + `","summary":[]}}`), + []byte(`data: {"type":"response.output_text.delta","delta":"ok"}`), + } { + for _, chunk := range ConvertCodexResponseToGemini(ctx, "muse-spark-1.3", nil, nil, ev, &wsParam) { + for _, p := range gjson.GetBytes(chunk, "candidates.0.content.parts").Array() { + wsParts = append(wsParts, p.Raw) + } + } + } + wsReplayReq := []byte(`{"model":"muse-spark-1.3","contents":[{"role":"user","parts":[{"text":"q"}]},{"role":"model","parts":[` + strings.Join(wsParts, ",") + `]},{"role":"user","parts":[{"text":"next"}]}]}`) + wsReplayCodex, _ := ConvertGeminiRequestToCodex("muse-spark-1.3", wsReplayReq, false) + wsSummaries := gjson.GetBytes(wsReplayCodex, "input.1.summary").Array() + if len(wsSummaries) != 3 || wsSummaries[0].Get("text").String()+wsSummaries[1].Get("text").String()+wsSummaries[2].Get("text").String() != "Hello world" { + t.Fatalf("whitespace thought chunks round-trip = %s, want ['Hello',' ','world']", gjson.GetBytes(wsReplayCodex, "input.1.summary").Raw) + } +} + func TestConvertCodexResponseToGeminiNonStreamPreservesFunctionCallID(t *testing.T) { ctx := context.Background() originalRequest := []byte(`{"tools":[]}`) diff --git a/internal/translator/codex/interactions/interactions_codex_request.go b/internal/translator/codex/interactions/interactions_codex_request.go index 4fcd8344061..47be307a291 100644 --- a/internal/translator/codex/interactions/interactions_codex_request.go +++ b/internal/translator/codex/interactions/interactions_codex_request.go @@ -20,15 +20,16 @@ func ConvertInteractionsRequestToCodex(modelName string, inputRawJSON []byte, st // because its only attachment has no Codex equivalent. func convertInteractionsRequestToCodex(modelName string, inputRawJSON []byte, stream bool) ([]byte, error) { root := gjson.ParseBytes(inputRawJSON) - out := []byte(`{"model":"","instructions":"","input":[]}`) + out := []byte(`{"model":"","instructions":"","store":false,"input":[]}`) out, _ = sjson.SetBytes(out, "model", modelName) if stream || root.Get("stream").Bool() { out, _ = sjson.SetBytes(out, "stream", true) } out = copyInteractionsSystemToCodex(out, root) out = copyInteractionsGenerationConfigToCodex(out, root) + signatureTargetModel := translatorcommon.ResolveCodexReplayTargetModel(modelName, inputRawJSON) inputItems := translatorcommon.NewRawArrayItems(root.Get("input.#").Int()) - errInput := appendInteractionsInputToCodex(&inputItems, root.Get("input"), modelName) + errInput := appendInteractionsInputToCodex(&inputItems, root.Get("input"), signatureTargetModel) out = translatorcommon.SetRawArrayItems(out, "input", inputItems) out = copyInteractionsToolsToCodex(out, root) out = copyInteractionsCodexTopLevel(out, root) @@ -200,19 +201,20 @@ func appendInteractionsInputToCodex(items *[][]byte, input gjson.Result, modelNa return nil } var run translatorcommon.UserRun + emittedSignatures := make(map[string]bool) if input.IsArray() { input.ForEach(func(_, step gjson.Result) bool { - appendInteractionsStepToCodex(items, step, "user", &run, modelName) + appendInteractionsStepToCodex(items, step, "user", &run, modelName, emittedSignatures) return true }) } else if steps := input.Get("steps"); steps.Exists() && steps.IsArray() { defaultRole := interactionsCodexStepRole(input, "user") steps.ForEach(func(_, step gjson.Result) bool { - appendInteractionsStepToCodex(items, step, defaultRole, &run, modelName) + appendInteractionsStepToCodex(items, step, defaultRole, &run, modelName, emittedSignatures) return true }) } else { - appendInteractionsStepToCodex(items, input, "user", &run, modelName) + appendInteractionsStepToCodex(items, input, "user", &run, modelName, emittedSignatures) } run.End() return run.Err() @@ -228,7 +230,7 @@ func interactionsCodexUserRun(run *translatorcommon.UserRun, role string) *trans return nil } -func appendInteractionsStepToCodex(items *[][]byte, step gjson.Result, defaultRole string, run *translatorcommon.UserRun, modelName string) { +func appendInteractionsStepToCodex(items *[][]byte, step gjson.Result, defaultRole string, run *translatorcommon.UserRun, modelName string, emittedSignatures map[string]bool) { if step.Type == gjson.String { appendInteractionsRoleTextToCodex(items, defaultRole, step.String(), run) return @@ -236,7 +238,7 @@ func appendInteractionsStepToCodex(items *[][]byte, step gjson.Result, defaultRo if steps := step.Get("steps"); steps.Exists() && steps.IsArray() { role := interactionsCodexStepRole(step, defaultRole) steps.ForEach(func(_, nested gjson.Result) bool { - appendInteractionsStepToCodex(items, nested, role, run, modelName) + appendInteractionsStepToCodex(items, nested, role, run, modelName, emittedSignatures) return true }) return @@ -255,7 +257,7 @@ func appendInteractionsStepToCodex(items *[][]byte, step gjson.Result, defaultRo appendInteractionsContentToCodexItem(items, step.Get("content"), "assistant", run) case "thought", "reasoning": run.End() - appendInteractionsThoughtToCodex(items, step, modelName) + appendInteractionsThoughtToCodex(items, step, modelName, emittedSignatures) case "user_input", "message", "": role := interactionsCodexStepRole(step, defaultRole) if content := step.Get("content"); content.Exists() { @@ -459,13 +461,14 @@ func ensureInteractionsCodexEncryptedContentInclude(out []byte) []byte { // a reasoning item without accepted encrypted_content carries no replayable // state, and the legacy text fallback shape is rejected by both the native // Meta upstream (missing summary) and routers (content expected array). -func appendInteractionsThoughtToCodex(items *[][]byte, step gjson.Result, modelName string) { +func appendInteractionsThoughtToCodex(items *[][]byte, step gjson.Result, modelName string, emittedSignatures map[string]bool) { text := interactionsCodexContentText(step.Get("content")) if text == "" { text = step.Get("text").String() } if raw := interactionsThoughtStepSignature(step); raw != "" { - if normalized, ok := translatorcommon.CodexReplaySignatureForTarget(modelName, raw); ok && !interactionsCodexSignatureAlreadyEmitted(*items, normalized) { + if normalized, ok := translatorcommon.CodexReplaySignatureForTarget(modelName, raw); ok && !emittedSignatures[normalized] { + emittedSignatures[normalized] = true item := []byte(`{"type":"reasoning","encrypted_content":"","summary":[]}`) item, _ = sjson.SetBytes(item, "encrypted_content", normalized) if text != "" { @@ -489,19 +492,6 @@ func interactionsThoughtStepSignature(step gjson.Result) string { return "" } -// interactionsCodexSignatureAlreadyEmitted reports whether an identical -// normalized signature was already written as encrypted_content in this -// request, so a repeated echo is only replayed once. -func interactionsCodexSignatureAlreadyEmitted(items [][]byte, normalized string) bool { - for _, raw := range items { - item := gjson.ParseBytes(raw) - if item.Get("type").String() == "reasoning" && item.Get("encrypted_content").String() == normalized { - return true - } - } - return false -} - func appendInteractionsTextToCodex(items *[][]byte, role, text string) { part := []byte(`{"type":"","text":""}`) if role == "assistant" { diff --git a/internal/translator/codex/interactions/interactions_codex_response.go b/internal/translator/codex/interactions/interactions_codex_response.go index dcc0d382f6b..9e1a899389e 100644 --- a/internal/translator/codex/interactions/interactions_codex_response.go +++ b/internal/translator/codex/interactions/interactions_codex_response.go @@ -14,20 +14,21 @@ import ( ) type codexToInteractionsStreamState struct { - Started bool - Completed bool - Done bool - ActiveStepOpen bool - ActiveStepType string - ActiveStepIndex int - StepIndex int - ID string - Model string - CreatedAt int64 - HasOutputText bool - HasThoughtSummary bool - FunctionCallName string - FunctionCallID string + Started bool + Completed bool + Done bool + ActiveStepOpen bool + ActiveStepType string + ActiveStepIndex int + StepIndex int + ID string + Model string + CreatedAt int64 + HasOutputText bool + HasThoughtSummary bool + StreamedSummaryItems map[string]bool + FunctionCallName string + FunctionCallID string // SeenReasoningSignatures dedupes repeated reasoning output_item.done // events so one signature is emitted as a thought_signature delta once. SeenReasoningSignatures map[[32]byte]bool @@ -238,6 +239,12 @@ func codexReasoningDeltaToInteractions(st *codexToInteractionsStreamState, root delta := []byte(`{"index":0,"delta":{"content":{"text":"","type":"text"},"type":"thought_summary"},"event_type":"step.delta"}`) delta, _ = sjson.SetBytes(delta, "index", st.ActiveStepIndex) delta, _ = sjson.SetBytes(delta, "delta.content.text", root.Get("delta").String()) + if itemID := strings.TrimSpace(root.Get("item_id").String()); itemID != "" { + if st.StreamedSummaryItems == nil { + st.StreamedSummaryItems = make(map[string]bool) + } + st.StreamedSummaryItems[itemID] = true + } st.HasThoughtSummary = true return append(out, translatorcommon.SSEEventData("step.delta", delta)) } @@ -319,6 +326,9 @@ func appendCodexInteractionsStepStop(out [][]byte, st *codexToInteractionsStream stepStop := []byte(`{"index":0,"event_type":"step.stop"}`) stepStop, _ = sjson.SetBytes(stepStop, "index", st.ActiveStepIndex) out = append(out, translatorcommon.SSEEventData("step.stop", stepStop)) + if st.ActiveStepType == "thought" { + st.HasThoughtSummary = false + } st.ActiveStepOpen = false st.ActiveStepType = "" return out @@ -434,13 +444,17 @@ func appendCodexReasoningItemToInteractionsStream(out [][]byte, st *codexToInter } st.SeenReasoningSignatures[key] = true } - if text != "" && !st.HasThoughtSummary { + itemID := strings.TrimSpace(item.Get("id").String()) + hadStreamedSummary := st.HasThoughtSummary + if itemID != "" && len(st.StreamedSummaryItems) > 0 { + hadStreamedSummary = st.StreamedSummaryItems[itemID] + } + if text != "" && !hadStreamedSummary { out = ensureCodexInteractionsStep(out, st, "thought", item) delta := []byte(`{"index":0,"delta":{"content":{"text":"","type":"text"},"type":"thought_summary"},"event_type":"step.delta"}`) delta, _ = sjson.SetBytes(delta, "index", st.ActiveStepIndex) delta, _ = sjson.SetBytes(delta, "delta.content.text", text) out = append(out, translatorcommon.SSEEventData("step.delta", delta)) - st.HasThoughtSummary = true } if sig != "" { out = ensureCodexInteractionsStep(out, st, "thought", item) @@ -449,6 +463,10 @@ func appendCodexReasoningItemToInteractionsStream(out [][]byte, st *codexToInter signatureDelta, _ = sjson.SetBytes(signatureDelta, "delta.signature", sig) out = append(out, translatorcommon.SSEEventData("step.delta", signatureDelta)) } + // Completing a reasoning item resets the current item's summary flag so a + // subsequent done-only reasoning item is not suppressed by an earlier + // item's streamed deltas. + st.HasThoughtSummary = false return out } diff --git a/internal/translator/codex/interactions/interactions_codex_test.go b/internal/translator/codex/interactions/interactions_codex_test.go index 95d98e427bf..eb85e412fbe 100644 --- a/internal/translator/codex/interactions/interactions_codex_test.go +++ b/internal/translator/codex/interactions/interactions_codex_test.go @@ -280,6 +280,27 @@ func TestConvertCodexResponseToInteractionsStream_ReasoningSignature(t *testing. if names != want { t.Fatalf("events = %s, want %s", names, want) } + + // A second done-only reasoning item after a streamed-delta reasoning item must still emit its summary. + var param2 any + var events2 [][]byte + for _, chunk := range [][]byte{ + []byte(`data: {"type":"response.created","response":{"id":"resp_2","model":"codex-test"}}`), + []byte(`data: {"type":"response.reasoning_summary_text.delta","delta":"first summary"}`), + []byte(`data: {"type":"response.output_item.done","item":{"id":"rs_a","type":"reasoning","summary":[{"type":"summary_text","text":"first summary"}],"encrypted_content":"` + metaSig + `"}}`), + []byte(`data: {"type":"response.output_item.done","item":{"id":"rs_b","type":"reasoning","summary":[{"type":"summary_text","text":"second summary only in done"}]}}`), + } { + events2 = append(events2, ConvertCodexResponseToInteractions(context.Background(), "codex-test", nil, nil, chunk, ¶m2)...) + } + var summaries2 []string + for _, delta := range findCodexInteractionsEventPayloads(events2, "step.delta") { + if delta.Get("delta.type").String() == "thought_summary" { + summaries2 = append(summaries2, delta.Get("delta.content.text").String()) + } + } + if len(summaries2) != 2 || summaries2[0] != "first summary" || summaries2[1] != "second summary only in done" { + t.Fatalf("consecutive reasoning summaries = %v, want [first summary, second summary only in done]", summaries2) + } } func TestConvertInteractionsRequestToCodex_RestoresThoughtSignature(t *testing.T) { diff --git a/internal/translator/common/codex_replay_signature.go b/internal/translator/common/codex_replay_signature.go index 7e154bf3dba..ff290ee8919 100644 --- a/internal/translator/common/codex_replay_signature.go +++ b/internal/translator/common/codex_replay_signature.go @@ -5,8 +5,28 @@ import ( sigcompat "github.com/router-for-me/CLIProxyAPI/v8/internal/signature" "github.com/router-for-me/CLIProxyAPI/v8/internal/thinking" + "github.com/tidwall/gjson" ) +// ResolveCodexReplayTargetModel returns the model name to use for reasoning +// signature family checks, falling back to the client-supplied "model" field in +// rawPayload when modelName is an unmapped upstream deployment alias for a +// Responses-native model family (Meta Muse, Grok, or GPT). +func ResolveCodexReplayTargetModel(modelName string, rawPayload []byte) string { + if sigcompat.SignatureProviderFromModelName(thinking.ParseSuffix(modelName).ModelName) != sigcompat.SignatureProviderUnknown { + return modelName + } + if len(rawPayload) > 0 { + if rawModel := strings.TrimSpace(gjson.GetBytes(rawPayload, "model").String()); rawModel != "" { + switch sigcompat.SignatureProviderFromModelName(thinking.ParseSuffix(rawModel).ModelName) { + case sigcompat.SignatureProviderMeta, sigcompat.SignatureProviderGrok, sigcompat.SignatureProviderGPT: + return rawModel + } + } + } + return modelName +} + // CodexReplaySignatureForTarget returns the exact value that may be written as a // Responses reasoning encrypted_content for the Codex-format upstream serving // modelName, or false when rawSignature must not replay there. Positively From 4337f1f000ffc1bcdfa6f1a05e5b0687842a83aa Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Sun, 18 Oct 2026 21:37:51 +0800 Subject: [PATCH 09/27] fix(logging): escape request path in access log to prevent log injection - Use Go quoting (`%q`) for the request path in the Gin logrus logger format string. - Prevent unescaped line breaks from raw encoded newlines in request paths from splitting log entries. Closes: #6731 --- internal/logging/gin_logger.go | 2 +- internal/logging/gin_logger_test.go | 60 +++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/internal/logging/gin_logger.go b/internal/logging/gin_logger.go index a63d4189443..66c00ee29f1 100644 --- a/internal/logging/gin_logger.go +++ b/internal/logging/gin_logger.go @@ -89,7 +89,7 @@ func GinLogrusLogger() gin.HandlerFunc { if requestID == "" { requestID = "--------" } - logLine := fmt.Sprintf("%3d | %13v | %15s | %-7s \"%s\"", statusCode, latency, clientIP, method, path) + logLine := fmt.Sprintf("%3d | %13v | %15s | %-7s %q", statusCode, latency, clientIP, method, path) if creditsUsed(c) { logLine += " [credits]" } diff --git a/internal/logging/gin_logger_test.go b/internal/logging/gin_logger_test.go index a31a761184d..5561975959b 100644 --- a/internal/logging/gin_logger_test.go +++ b/internal/logging/gin_logger_test.go @@ -4,6 +4,7 @@ import ( "errors" "net/http" "net/http/httptest" + "strings" "testing" "github.com/gin-gonic/gin" @@ -151,6 +152,65 @@ func TestGinLogrusLoggerAddsRequestIDForCodexBackend(t *testing.T) { } } +func TestGinLogrusLoggerEscapesEncodedNewlineInPath(t *testing.T) { + gin.SetMode(gin.TestMode) + logger := log.StandardLogger() + previousHooks := logger.ReplaceHooks(make(log.LevelHooks)) + previousLevel := logger.GetLevel() + hook := logtest.NewLocal(logger) + logger.SetLevel(log.InfoLevel) + t.Cleanup(func() { logger.ReplaceHooks(previousHooks); logger.SetLevel(previousLevel) }) + + engine := gin.New() + engine.Use(GinLogrusLogger()) + + recorder := httptest.NewRecorder() + engine.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/x%0A200%20forged", nil)) + if recorder.Code != http.StatusNotFound { + t.Fatalf("status = %d, want %d", recorder.Code, http.StatusNotFound) + } + entries := hook.AllEntries() + if len(entries) != 1 { + t.Fatalf("log entries = %d, want 1", len(entries)) + } + message := entries[0].Message + if strings.ContainsAny(message, "\r\n") { + t.Errorf("access log message contains a line break: %q", message) + } + if !strings.HasSuffix(message, `GET "/x\n200 forged"`) { + t.Errorf("access log message = %q, want suffix %q", message, `GET "/x\n200 forged"`) + } +} + +func TestGinLogrusLoggerKeepsMaskedQueryReadable(t *testing.T) { + gin.SetMode(gin.TestMode) + logger := log.StandardLogger() + previousHooks := logger.ReplaceHooks(make(log.LevelHooks)) + previousLevel := logger.GetLevel() + hook := logtest.NewLocal(logger) + logger.SetLevel(log.InfoLevel) + t.Cleanup(func() { logger.ReplaceHooks(previousHooks); logger.SetLevel(previousLevel) }) + + engine := gin.New() + engine.Use(GinLogrusLogger()) + engine.GET("/v1/models", func(c *gin.Context) { c.Status(http.StatusOK) }) + + recorder := httptest.NewRecorder() + engine.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/v1/models?key=sk-0123456789cdef&limit=5", nil)) + if recorder.Code != http.StatusOK { + t.Fatalf("status = %d, want %d", recorder.Code, http.StatusOK) + } + entries := hook.AllEntries() + if len(entries) != 1 { + t.Fatalf("log entries = %d, want 1", len(entries)) + } + message := entries[0].Message + wantSuffix := `GET "/v1/models?key=sk-0...cdef&limit=5"` + if !strings.HasSuffix(message, wantSuffix) { + t.Errorf("access log message = %q, want suffix %q", message, wantSuffix) + } +} + func TestGinLogrusLoggerHealthProbeStatus(t *testing.T) { gin.SetMode(gin.TestMode) logger := log.StandardLogger() From c558d3203e897d6d62db7bb6d3735fbffb34233d Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Sun, 18 Oct 2026 22:37:34 +0800 Subject: [PATCH 10/27] feat(util): inline local schema references in Gemini cleaners and preserve literal values - Expand local `$ref` definitions before stripping schema definition containers to prevent nested field loss. - Preserve schema-looking keys inside literal data keywords (`default`, `example`, `examples`, `const`, `enum`). - Add comprehensive test coverage for schema inlining, literal preservation, and wrapped Antigravity tool schemas. Closes: #6732 --- internal/util/gemini_schema.go | 496 ++++++++++++++++++++++++--- internal/util/gemini_schema_test.go | 500 ++++++++++++++++++++++++++++ internal/util/translator.go | 18 +- 3 files changed, 974 insertions(+), 40 deletions(-) diff --git a/internal/util/gemini_schema.go b/internal/util/gemini_schema.go index acfe332c92b..c550eedcd0d 100644 --- a/internal/util/gemini_schema.go +++ b/internal/util/gemini_schema.go @@ -134,10 +134,9 @@ func cleanJSONSchema(jsonStr string, options jsonSchemaCleanOptions) string { // Phase 0: Normalize malformed schemas (e.g. bare property maps and boolean required from MCP tools) jsonStr = normalizeMalformedSchemaObjects(jsonStr, options.addMissingArrayItems) - // Phase 1: Convert and add hints - if options.antigravitySemantics { - jsonStr = inlineLocalRefs(jsonStr) - } + // Phase 1: Convert and add hints. Every cleaner removes $defs/definitions later, so local + // references must be expanded here for every route; otherwise nested fields are lost. + jsonStr = inlineLocalRefsForClean(jsonStr) jsonStr = convertRefsToHints(jsonStr, options.antigravitySemantics) jsonStr = convertConstToEnum(jsonStr) jsonStr = convertEnumValuesToStrings(jsonStr, options.forceEnumStringType) @@ -171,8 +170,8 @@ func cleanJSONSchema(jsonStr string, options jsonSchemaCleanOptions) string { jsonStr = removeKeywords(jsonStr, []string{"title"}) } jsonStr = cleanupRequiredFields(jsonStr) - jsonStr = sanitizeArrayItems(jsonStr) jsonStr = sanitizeObjectProperties(jsonStr) + jsonStr = sanitizeArrayItems(jsonStr, options.addMissingArrayItems) // Phase 4: Add placeholder for empty object schemas (Claude VALIDATED mode requirement) if options.addPlaceholder { jsonStr = addEmptySchemaPlaceholder(jsonStr) @@ -206,10 +205,13 @@ func sanitizeObjectProperties(jsonStr string) string { return jsonStr } -// sanitizeArrayItems ensures that any schema node declaring "items" has "type": "array". +// sanitizeArrayItems ensures that any schema node declaring "items" has "type": "array", and +// (when addMissing is set) that array-typed nodes still missing "items" after reference +// inlining receive the placeholder Phase 0 adds. Phase 0 cannot add it before inlining: on a +// $ref node the placeholder would clobber the referenced definition's real items. // Gemini's protobuf validator enforces a strict field predicate on items ($type == Type.ARRAY); // if type is missing, it is inferred as array; if type is explicitly a non-array, items is removed. -func sanitizeArrayItems(jsonStr string) string { +func sanitizeArrayItems(jsonStr string, addMissing bool) string { paths := findPaths(jsonStr, "items") sortByDepth(paths) for _, p := range paths { @@ -226,6 +228,23 @@ func sanitizeArrayItems(jsonStr string) string { jsonStr, _ = sjson.Delete(jsonStr, p) } } + + if !addMissing { + return jsonStr + } + typePaths := findPaths(jsonStr, "type") + sortByDepth(typePaths) + for _, p := range typePaths { + parentPath := trimSuffix(p, ".type") + if isPropertyDefinition(parentPath) || !isArrayDeclaredType(gjson.Get(jsonStr, p).Value()) { + continue + } + if gjson.Get(jsonStr, joinPath(parentPath, "items")).Exists() || gjson.Get(jsonStr, joinPath(parentPath, "$ref")).Exists() { + continue + } + updated, _ := sjson.SetRawBytes([]byte(jsonStr), joinPath(parentPath, "items"), []byte(`{"type":"string"}`)) + jsonStr = string(updated) + } return jsonStr } @@ -533,8 +552,10 @@ func repairSchemaNode(node map[string]any, addMissingArrayItems bool) (map[strin } // Gemini and Antigravity reject tool array schemas without an items definition, - // and reject tool schemas with items whose type is not ARRAY. - if addMissingArrayItems { + // and reject tool schemas with items whose type is not ARRAY. A node still holding an + // unresolved $ref gets neither a placeholder items nor a synthesized type: both would become + // sibling keywords during inlining and clobber the referenced definition's real values. + if addMissingArrayItems && clone["$ref"] == nil { if isArrayDeclaredType(clone["type"]) { if _, hasItems := clone["items"]; !hasItems { clone["items"] = map[string]any{"type": "string"} @@ -734,17 +755,34 @@ func mergeStringSlices(existing, promoted []string) []string { return res } -// InlineLocalRefs resolves JSON Pointer references against the original schema before definition -// containers are stripped. Each expansion receives its own copy, sibling keywords override the +// InlineLocalRefs resolves JSON Pointer references against the original schema. Every reference +// receives its own copy as long as the expansion budget lasts, sibling keywords override the // referenced definition, and cycles terminate as a typed hint instead of recursing forever. func InlineLocalRefs(jsonStr string) string { return inlineLocalRefs(jsonStr) } -// inlineLocalRefs resolves JSON Pointer references against the original schema before definition -// containers are stripped. Each expansion receives its own copy, sibling keywords override the +// maxInlinedSchemaBytes bounds the total size of schema material copied into the output by local +// reference expansion. Shared definitions fan out once per reference, so a small input can expand +// exponentially; expansions past the budget degrade to a typed hint like cycles do. +const maxInlinedSchemaBytes = 256 * 1024 + +// maxFallbackDescriptionBytes bounds the target description copied into a degraded reference, so a +// large shared description cannot be repeated per reference and defeat the expansion budget. +const maxFallbackDescriptionBytes = 512 + +// inlineLocalRefs resolves JSON Pointer references against the original schema. Every reference +// receives its own copy as long as the expansion budget lasts, sibling keywords override the // referenced definition, and cycles terminate as a typed hint instead of recursing forever. func inlineLocalRefs(jsonStr string) string { + return inlineLocalRefsRooted(jsonStr, nil, false) +} + +// inlineLocalRefsForClean inlines local references for schema cleaning. Cleaning sometimes +// receives the single-key {"schema": ...} carrier used to nest a schema one level down (see Phase +// 0); references inside it must then resolve against the inner schema, not the carrier, or every +// lookup misses. The carrier shape and root keys are otherwise preserved byte-for-byte. +func inlineLocalRefsForClean(jsonStr string) string { if !strings.Contains(jsonStr, `"$ref"`) { return jsonStr } @@ -756,7 +794,43 @@ func inlineLocalRefs(jsonStr string) string { return jsonStr } - resolved := resolveLocalRefs(root, root, make(map[string]bool)) + resolveRoot := root + wrapped := false + if rootMap, ok := root.(map[string]any); ok && len(rootMap) == 1 { + if inner, okInner := rootMap["schema"].(map[string]any); okInner { + resolveRoot = inner + wrapped = true + } + } + if !wrapped { + return inlineLocalRefsRooted(jsonStr, root, true) + } + + state := newRefResolveState() + resolved := resolveSchemaRoot(resolveRoot, state, true) + out, err := json.Marshal(map[string]any{"schema": resolved}) + if err != nil { + return jsonStr + } + return string(out) +} + +func inlineLocalRefsRooted(jsonStr string, parsedRoot any, forClean bool) string { + if !strings.Contains(jsonStr, `"$ref"`) { + return jsonStr + } + + root := parsedRoot + if root == nil { + decoder := json.NewDecoder(strings.NewReader(jsonStr)) + decoder.UseNumber() + if err := decoder.Decode(&root); err != nil { + return jsonStr + } + } + + state := newRefResolveState() + resolved := resolveSchemaRoot(root, state, forClean) out, err := json.Marshal(resolved) if err != nil { return jsonStr @@ -764,43 +838,366 @@ func inlineLocalRefs(jsonStr string) string { return string(out) } -func resolveLocalRefs(root, value any, active map[string]bool) any { +// refResolveState carries the remaining expansion budget of one inlining pass. Every reference +// charges the size of the target it copies (nested references charge their own targets), so +// shared-definition fan-out stops once the budget is spent while ordinary reuse still expands. +type refResolveState struct { + budget int +} + +func newRefResolveState() *refResolveState { + return &refResolveState{budget: maxInlinedSchemaBytes} +} + +// sortedKeys returns map keys in a deterministic order, so budget allocation never depends on +// Go map iteration order. +func sortedKeys[V any](m map[string]V) []string { + keys := make([]string, 0, len(m)) + for key := range m { + keys = append(keys, key) + } + sort.Strings(keys) + return keys +} + +// resolveSchemaRoot resolves references in the schema body before resolving definition +// containers. Definitions only matter through references, so walking them first would spend the +// expansion budget on copies the cleaners delete next (and whose map order is arbitrary), while +// the fields that are actually kept degraded to hints. For the public InlineLocalRefs the +// containers are kept, so they are still resolved afterwards with whatever budget remains; when +// cleaning they are dropped here altogether (references still resolve against the original +// root), so their bytes never reach the intermediate JSON at all. +func resolveSchemaRoot(root any, state *refResolveState, forClean bool) any { + rootMap, ok := root.(map[string]any) + if !ok { + return resolveLocalRefs(root, root, make(map[string]bool), state, false, forClean) + } + + out := make(map[string]any, len(rootMap)) + for _, key := range sortedKeys(rootMap) { + item := rootMap[key] + switch key { + case "$defs", "definitions": + if !forClean { + out[key] = nil // placeholder, resolved after the schema body below + } + case "$ref": + out[key] = item + default: + out[key] = resolveSchemaEntry(rootMap, key, item, make(map[string]bool), state, forClean) + } + } + + // A root-level reference expands into the whole referenced schema; its siblings were resolved + // above and must override the target, mirroring resolveLocalRefs. + if ref, hasRef := rootMap["$ref"].(string); hasRef && strings.HasPrefix(ref, "#/") { + if target, okTarget := resolveJSONPointer(rootMap, ref); okTarget { + if size := targetExpansionSize(target, forClean); size <= state.budget { + state.budget -= size + active := map[string]bool{ref: true} + if targetMap, okMap := resolveLocalRefs(rootMap, target, active, state, false, forClean).(map[string]any); okMap { + merged := make(map[string]any, len(targetMap)+len(out)) + for _, key := range sortedKeys(targetMap) { + merged[key] = targetMap[key] + } + for _, key := range sortedKeys(out) { + if key != "$ref" { + merged[key] = out[key] + } + } + out = merged + } else { + out = resolveRefFallback(rootMap, out, target, ref, make(map[string]bool), state, forClean) + } + } else { + out = resolveRefFallback(rootMap, out, target, ref, make(map[string]bool), state, forClean) + } + delete(out, "$ref") + } + } + + if !forClean { + for _, key := range []string{"$defs", "definitions"} { + if item, exists := rootMap[key]; exists { + out[key] = resolveSchemaEntry(rootMap, key, item, make(map[string]bool), state, false) + } + } + } + return out +} + +// targetExpansionSize estimates what copying a reference target into the output costs. When +// cleaning, definitions nested inside the target are deleted later and never copied, so they are +// excluded; otherwise a small target carrying a large unused definitions block would be degraded +// purely because of material that is about to be thrown away. Name-map and literal context is +// threaded so properties named like a container keyword, and data inside literal values, still +// count in full. +func targetExpansionSize(value any, forClean bool) int { + if !forClean { + return jsonValueSize(value) + } + return cleanExpansionSize(value, false) +} + +func cleanExpansionSize(value any, inNameMap bool) int { + switch node := value.(type) { + case nil: + return 4 + case bool: + return 5 + case json.Number: + return len(node.String()) + case string: + return jsonStringEncodedSize(node) + case []any: + size := 2 + for _, item := range node { + size += cleanExpansionSize(item, false) + 1 + if size > maxInlinedSchemaBytes+1 { + return maxInlinedSchemaBytes + 1 + } + } + return size + case map[string]any: + size := 2 + for key, item := range node { + if !inNameMap && (key == "$defs" || key == "definitions") { + continue + } + child := item + childInNameMap := false + if !inNameMap { + if isLiteralValueKeyword(key) { + size += jsonStringEncodedSize(key) + 1 + jsonValueSize(child) + 1 + if size > maxInlinedSchemaBytes+1 { + return maxInlinedSchemaBytes + 1 + } + continue + } + if _, isNameMap := schemaNameMapKeywords[key]; isNameMap { + childInNameMap = true + } + } + size += jsonStringEncodedSize(key) + 1 + cleanExpansionSize(child, childInNameMap) + 1 + if size > maxInlinedSchemaBytes+1 { + return maxInlinedSchemaBytes + 1 + } + } + return size + default: + return 16 + } +} + +// jsonValueSize estimates the encoded size of a decoded JSON value as encoding/json will emit +// it, including string escaping (notably the six-byte \u00XX forms for <, >, & and control +// characters). It is used to bound ref expansion, so it must not under-count what serialization +// will actually produce. +func jsonValueSize(value any) int { + switch node := value.(type) { + case nil: + return 4 + case bool: + return 5 + case json.Number: + return len(node.String()) + case string: + return jsonStringEncodedSize(node) + case []any: + size := 2 + for _, item := range node { + size += jsonValueSize(item) + 1 + } + return size + case map[string]any: + size := 2 + for key, item := range node { + size += jsonStringEncodedSize(key) + 1 + jsonValueSize(item) + 1 + } + return size + default: + return 16 + } +} + +// jsonStringEncodedSize returns the encoded length of one JSON string including quotes, +// mirroring encoding/json's default HTML escaping. +func jsonStringEncodedSize(s string) int { + size := 2 + for i := 0; i < len(s); i++ { + c := s[i] + switch { + case c < 0x20: + if c == '\n' || c == '\r' || c == '\t' { + size += 2 + } else { + size += 6 + } + case c == '"' || c == '\\': + size += 2 + case c == '<' || c == '>' || c == '&': + size += 6 + case c < 0x80: + size++ + case c == 0xe2 && i+2 < len(s) && s[i+1] == 0x80 && (s[i+2] == 0xa8 || s[i+2] == 0xa9): + size += 6 + i += 2 + default: + size++ + } + } + return size +} + +// literalValueKeywords carry client data rather than subschemas. Cleaning and ref inlining must +// copy their values verbatim: keys inside them that happen to look like schema keywords are data. +var literalValueKeywords = map[string]struct{}{ + "default": {}, + "example": {}, + "examples": {}, + "const": {}, + "enum": {}, +} + +func isLiteralValueKeyword(key string) bool { + _, ok := literalValueKeywords[key] + return ok +} + +func resolveLocalRefs(root, value any, active map[string]bool, state *refResolveState, inNameMap, forClean bool) any { switch node := value.(type) { case []any: out := make([]any, len(node)) for i, item := range node { - out[i] = resolveLocalRefs(root, item, active) + out[i] = resolveLocalRefs(root, item, active, state, false, forClean) } return out case map[string]any: - ref, hasRef := node["$ref"].(string) - if hasRef && strings.HasPrefix(ref, "#/") { - if target, ok := resolveJSONPointer(root, ref); ok { - if active[ref] { - return cyclicRefFallback(node, target, ref) - } - active[ref] = true - resolvedTarget := resolveLocalRefs(root, target, active) - delete(active, ref) - if targetMap, okTarget := resolvedTarget.(map[string]any); okTarget { - out := make(map[string]any, len(targetMap)+len(node)) - for key, item := range targetMap { - out[key] = item + if !inNameMap { + ref, hasRef := node["$ref"].(string) + if hasRef && strings.HasPrefix(ref, "#/") { + if target, ok := resolveJSONPointer(root, ref); ok { + if active[ref] { + return resolveRefFallback(root, node, target, ref, active, state, forClean) + } + // Charge the stored target size and let nested references charge their own + // costs during recursion. The total copied material stays bounded, while an + // over-budget chain still expands its affordable outer levels and only the + // deepest references degrade to hints. + if size := targetExpansionSize(target, forClean); size > state.budget { + return resolveRefFallback(root, node, target, ref, active, state, forClean) + } else { + state.budget -= size } - for key, item := range node { - if key == "$ref" { - continue + active[ref] = true + resolvedTarget := resolveLocalRefs(root, target, active, state, false, forClean) + delete(active, ref) + if targetMap, okTarget := resolvedTarget.(map[string]any); okTarget { + out := make(map[string]any, len(targetMap)+len(node)) + for _, key := range sortedKeys(targetMap) { + if forClean && (key == "$defs" || key == "definitions") { + continue // dropped with every other definitions container when cleaning + } + out[key] = targetMap[key] + } + for _, key := range sortedKeys(node) { + if key == "$ref" { + continue + } + if forClean && (key == "$defs" || key == "definitions") { + continue + } + out[key] = resolveSchemaEntry(root, key, node[key], active, state, forClean) } - out[key] = resolveLocalRefs(root, item, active) + return out } - return out } } } + out := make(map[string]any, len(node)) + for _, key := range sortedKeys(node) { + if forClean && !inNameMap && (key == "$defs" || key == "definitions") { + continue // dropped with every other definitions container when cleaning + } + out[key] = resolveSchemaEntry(root, key, node[key], active, state, forClean) + } + return out + default: + return value + } +} + +// resolveRefFallback degrades a reference that cannot be expanded (cycle or exhausted budget) to +// a typed hint, but still resolves the sibling subschemas on the same node so their own references +// are not left dangling behind the degraded one. Literal values stay verbatim through +// resolveSchemaEntry. The description assembled by cyclicRefFallback is kept afterwards so +// resolved siblings cannot clobber the hint. +func resolveRefFallback(root any, node map[string]any, target any, ref string, active map[string]bool, state *refResolveState, forClean bool) map[string]any { + out := cyclicRefFallback(node, target, ref) + // The target description copied into the fallback is expansion output like any other; charge + // it to the budget. Once the budget is spent, fallbacks carry only the short hint, so a large + // number of degraded references cannot multiply copied descriptions without bound. Cyclic + // fallbacks omit over-long descriptions entirely, and omitted content is not charged. + if targetMap, okMap := target.(map[string]any); okMap { + if description, okDescription := targetMap["description"].(string); okDescription && description != "" && len(description) <= maxFallbackDescriptionBytes { + if _, hasOwn := node["description"]; !hasOwn { + cost := jsonStringEncodedSize(description) + if cost > state.budget { + out["description"] = "See: " + refName(ref) + } else { + state.budget -= cost + } + } + } + } + for _, key := range sortedKeys(node) { + if key == "$ref" || key == "description" { + continue + } + if forClean && (key == "$defs" || key == "definitions") { + delete(out, key) + continue + } + out[key] = resolveSchemaEntry(root, key, node[key], active, state, forClean) + } + return out +} + +// resolveSchemaEntry resolves one map entry during ref inlining. A literal-value keyword's value +// is client data and is deep-copied without resolving refs inside it. Under a name map +// ("properties" and friends), however, a key spelled like a literal keyword is an author-chosen +// property name whose value is a subschema; name-map entries are therefore resolved directly +// here so such property names keep their schema semantics. Nested definition containers outside +// the root still resolve through this path; when cleaning, callers skip them entirely instead. +func resolveSchemaEntry(root any, key string, item any, active map[string]bool, state *refResolveState, forClean bool) any { + if isLiteralValueKeyword(key) { + return deepCopyJSONValue(item) + } + if _, isNameMap := schemaNameMapKeywords[key]; isNameMap { + if nameMap, ok := item.(map[string]any); ok { + out := make(map[string]any, len(nameMap)) + for _, name := range sortedKeys(nameMap) { + out[name] = resolveLocalRefs(root, nameMap[name], active, state, false, forClean) + } + return out + } + } + return resolveLocalRefs(root, item, active, state, false, forClean) +} + +func deepCopyJSONValue(value any) any { + switch node := value.(type) { + case []any: + out := make([]any, len(node)) + for i, item := range node { + out[i] = deepCopyJSONValue(item) + } + return out + case map[string]any: out := make(map[string]any, len(node)) for key, item := range node { - out[key] = resolveLocalRefs(root, item, active) + out[key] = deepCopyJSONValue(item) } return out default: @@ -837,6 +1234,11 @@ func cyclicRefFallback(node map[string]any, target any, ref string) map[string]a if targetMap, ok := target.(map[string]any); ok { for _, key := range []string{"type", "nullable", "description"} { if value, exists := targetMap[key]; exists { + if key == "description" { + if description, okDescription := value.(string); okDescription && len(description) > maxFallbackDescriptionBytes { + continue + } + } out[key] = value } } @@ -870,10 +1272,12 @@ func convertRefsToHints(jsonStr string, preserveSiblings bool) string { sortByDepth(paths) for _, p := range paths { + parentPath := trimSuffix(p, ".$ref") + if isPropertyDefinition(parentPath) { + continue // a property literally named "$ref", not a reference keyword + } refVal := gjson.Get(jsonStr, p).String() defName := refName(refVal) - - parentPath := trimSuffix(p, ".$ref") hint := fmt.Sprintf("See: %s", defName) if !preserveSiblings { if existing := gjson.Get(jsonStr, descriptionPath(parentPath)).String(); existing != "" { @@ -1582,6 +1986,11 @@ func walkForExtensions(value gjson.Result, path string, paths *[]string) { return true } + // Literal values are client data; x-* keys inside them are not schema extensions. + if isLiteralValueKeyword(keyStr) && !isPropertyDefinition(path) { + return true + } + walkForExtensions(val, childPath, paths) return true }) @@ -1698,7 +2107,7 @@ func addEmptySchemaPlaceholder(jsonStr string) string { func findPaths(jsonStr, field string) []string { var paths []string - Walk(gjson.Parse(jsonStr), "", field, &paths) + WalkSchema(gjson.Parse(jsonStr), "", field, &paths) return paths } @@ -1730,6 +2139,12 @@ func walkForFields(value gjson.Result, path string, fields map[string]struct{}, paths[keyStr] = append(paths[keyStr], childPath) } + // Literal values are client data; schema-looking keys inside them are not keywords. + // Under a name map the key is a property name instead, so keep descending there. + if isLiteralValueKeyword(keyStr) && !isPropertyDefinition(path) { + return true + } + walkForFields(val, childPath, fields, paths) return true }) @@ -1767,11 +2182,14 @@ func setRawAt(jsonStr, path, value string) string { } // schemaNameMapKeywords are the schema keywords whose value maps author-chosen names to -// subschemas. A key directly under one of them is a name, never a schema keyword. +// subschemas. A key directly under one of them is a name, never a schema keyword. The Draft-07 +// "dependencies" container qualifies for object (schema) values; array values are property-name +// lists and are never walked as maps by the checks that use this set. var schemaNameMapKeywords = map[string]struct{}{ "properties": {}, "patternProperties": {}, "dependentSchemas": {}, + "dependencies": {}, "$defs": {}, "definitions": {}, } diff --git a/internal/util/gemini_schema_test.go b/internal/util/gemini_schema_test.go index 2dd8721c50c..4da2c6dd39e 100644 --- a/internal/util/gemini_schema_test.go +++ b/internal/util/gemini_schema_test.go @@ -3,6 +3,7 @@ package util import ( "encoding/json" "reflect" + "strconv" "strings" "testing" @@ -3793,3 +3794,502 @@ func TestCleanJSONSchema_RemovesContentMediaTypeKeyword_Issue6699(t *testing.T) }) } } + +// TestIssue6732_GeminiCleanersInlineLocalDefs verifies that the Gemini cleaners expand local +// $defs references before the definition container is stripped, so nested fields survive. +func TestIssue6732_GeminiCleanersInlineLocalDefs(t *testing.T) { + input := `{"type":"object","properties":{"person":{"$ref":"#/$defs/Person"}},"required":["person"],"$defs":{"Person":{"type":"object","properties":{"name":{"type":"string"}},"required":["name"]}}}` + + for name, clean := range map[string]func(string) string{ + "gemini": CleanJSONSchemaForGemini, + "geminiJSONSchema": CleanJSONSchemaForGeminiJSONSchema, + } { + got := clean(input) + parsed := gjson.Parse(got) + if gotType := parsed.Get("properties.person.properties.name.type").String(); gotType != "string" { + t.Errorf("%s: inlined properties.person.properties.name.type = %q, want %q: %s", name, gotType, "string", got) + } + if parsed.Get("$defs").Exists() { + t.Errorf("%s: $defs should be removed after inlining: %s", name, got) + } + if strings.Contains(got, "See: Person") { + t.Errorf("%s: resolvable local ref degraded to a hint: %s", name, got) + } + } +} + +// TestIssue6732_GeminiCleanersInlinePropertyReuse verifies that a Zod-style #/properties/... +// reference is expanded by the Gemini cleaners instead of becoming a description hint. +func TestIssue6732_GeminiCleanersInlinePropertyReuse(t *testing.T) { + input := `{"type":"object","properties":{"billing":{"type":"object","properties":{"zip":{"type":"string"}}},"shipping":{"$ref":"#/properties/billing"}}}` + + for name, clean := range map[string]func(string) string{ + "gemini": CleanJSONSchemaForGemini, + "geminiJSONSchema": CleanJSONSchemaForGeminiJSONSchema, + } { + got := clean(input) + parsed := gjson.Parse(got) + if gotType := parsed.Get("properties.shipping.properties.zip.type").String(); gotType != "string" { + t.Errorf("%s: properties.shipping.properties.zip.type = %q, want %q: %s", name, gotType, "string", got) + } + } +} + +// TestIssue6732_GeminiCleanersReuseSameDefinitionTwice verifies that two properties referencing +// the same definition both keep the referenced fields; bounding expansion must not degrade +// ordinary reuse of a shared definition to a hint. +func TestIssue6732_GeminiCleanersReuseSameDefinitionTwice(t *testing.T) { + input := `{"type":"object","properties":{"billing":{"$ref":"#/$defs/Address"},"shipping":{"$ref":"#/$defs/Address"}},"$defs":{"Address":{"type":"object","properties":{"zip":{"type":"string"}}}}}` + + for name, clean := range map[string]func(string) string{ + "gemini": CleanJSONSchemaForGemini, + "geminiJSONSchema": CleanJSONSchemaForGeminiJSONSchema, + } { + got := clean(input) + parsed := gjson.Parse(got) + for _, field := range []string{"billing", "shipping"} { + if gotType := parsed.Get("properties." + field + ".properties.zip.type").String(); gotType != "string" { + t.Errorf("%s: properties.%s.properties.zip.type = %q, want %q: %s", name, field, gotType, "string", got) + } + } + } +} + +// TestIssue6732_WrappedSchemaResolvesAgainstInnerRoot verifies that a schema wrapped in the +// single-key {"schema": ...} carrier used by the Antigravity executor resolves local refs +// against the inner schema rather than the carrier. +func TestIssue6732_WrappedSchemaResolvesAgainstInnerRoot(t *testing.T) { + inner := `{"type":"object","properties":{"person":{"$ref":"#/$defs/Person"}},"required":["person"],"$defs":{"Person":{"type":"object","properties":{"name":{"type":"string"}},"required":["name"]}}}` + wrapped := `{"schema":` + inner + `}` + + got := CleanJSONSchemaForAntigravityTool(wrapped, false) + parsed := gjson.Parse(got) + if gotType := parsed.Get("schema.properties.person.properties.name.type").String(); gotType != "string" { + t.Errorf("wrapped schema.properties.person.properties.name.type = %q, want %q: %s", gotType, "string", got) + } + if parsed.Get("schema.$defs").Exists() { + t.Errorf("wrapped $defs should be removed after inlining: %s", got) + } + if strings.Contains(got, "See: Person") { + t.Errorf("wrapped resolvable local ref degraded to a hint: %s", got) + } +} + +// TestIssue6732_DefaultLiteralPreservedOnJSONSchemaRoute verifies that the parametersJsonSchema +// route forwards default values verbatim even when they contain schema-looking keys. +func TestIssue6732_DefaultLiteralPreservedOnJSONSchemaRoute(t *testing.T) { + input := `{"type":"object","properties":{"report":{"type":"object","default":{"title":"Report","format":"pdf","id":7,"x-owner":"ops"}}}}` + + got := CleanJSONSchemaForGeminiJSONSchema(input) + def := gjson.Parse(got).Get("properties.report.default") + if !def.Exists() { + t.Fatalf("default literal missing: %s", got) + } + if def.Get("title").String() != "Report" || def.Get("format").String() != "pdf" || def.Get("id").Int() != 7 || def.Get("x-owner").String() != "ops" { + t.Errorf("default literal was rewritten, got %s: %s", def.Raw, got) + } +} + +// TestIssue6732_ExampleLiteralsPreserved verifies that example/examples values keep +// schema-looking keys on the parametersJsonSchema route. +func TestIssue6732_ExampleLiteralsPreserved(t *testing.T) { + input := `{"type":"object","properties":{"a":{"type":"string","example":{"title":"Hi","x-owner":"ops","id":3},"examples":[{"title":"Hi2","x-owner":"ops","id":4}]}}}` + + got := CleanJSONSchemaForGeminiJSONSchema(input) + parsed := gjson.Parse(got) + example := parsed.Get("properties.a.example") + if example.Get("title").String() != "Hi" || example.Get("x-owner").String() != "ops" || example.Get("id").Int() != 3 { + t.Errorf("example literal was rewritten, got %s: %s", example.Raw, got) + } + exampleItem := parsed.Get("properties.a.examples.0") + if exampleItem.Get("title").String() != "Hi2" || exampleItem.Get("x-owner").String() != "ops" || exampleItem.Get("id").Int() != 4 { + t.Errorf("examples literal was rewritten, got %s: %s", exampleItem.Raw, got) + } +} + +// TestIssue6732_InlineLocalRefsLeavesLiteralRefs verifies that the inliner does not resolve +// {"$ref": ...} objects that appear as client data inside a default value. +func TestIssue6732_InlineLocalRefsLeavesLiteralRefs(t *testing.T) { + input := `{"type":"object","properties":{"a":{"type":"object","default":{"$ref":"#/$defs/Person"}}},"$defs":{"Person":{"type":"object","properties":{"name":{"type":"string"}}}}}` + + got := InlineLocalRefs(input) + parsed := gjson.Parse(got) + if ref := parsed.Get("properties.a.default.$ref").String(); ref != "#/$defs/Person" { + t.Errorf("literal $ref inside default = %q, want it preserved verbatim: %s", ref, got) + } + if parsed.Get("properties.a.default.properties").Exists() { + t.Errorf("literal $ref inside default was expanded: %s", got) + } + if !parsed.Get("$defs.Person.properties.name").Exists() { + t.Errorf("InlineLocalRefs must not strip definitions: %s", got) + } +} + +// TestIssue6732_PropertiesNamedDefaultAndTypeRemainSchemas verifies that properties literally +// named "default" and "type" keep their schema semantics: their own keywords are still cleaned +// and their own $ref is still inlined. +func TestIssue6732_PropertiesNamedDefaultAndTypeRemainSchemas(t *testing.T) { + input := `{"type":"object","properties":{"default":{"$ref":"#/$defs/Person"},"type":{"type":"string","title":"Kind"}},"$defs":{"Person":{"type":"object","properties":{"name":{"type":"string"}}}}}` + + got := CleanJSONSchemaForGeminiJSONSchema(input) + parsed := gjson.Parse(got) + if gotType := parsed.Get("properties.default.properties.name.type").String(); gotType != "string" { + t.Errorf("property named \"default\" ref was not inlined, name type = %q: %s", gotType, got) + } + if parsed.Get("properties.type.title").Exists() { + t.Errorf("property named \"type\" schema title should still be cleaned: %s", got) + } +} + +// TestIssue6732_ExpansionBudgetBoundsSharedDefs verifies that inlining a schema whose shared +// definitions fan out exponentially stays bounded instead of expanding without limit, while the +// affordable outer levels are still expanded. +func TestIssue6732_ExpansionBudgetBoundsSharedDefs(t *testing.T) { + input := geminiSchemaDiamondInput() + got := CleanJSONSchemaForGeminiJSONSchema(input) + if len(got) >= 512*1024 { + t.Errorf("diamond schema output = %d bytes, want < %d (input %d bytes)", len(got), 512*1024, len(input)) + } + parsed := gjson.Parse(got) + if !parsed.Get("properties.leaf").Exists() { + t.Errorf("diamond schema lost the top-level property: %s", got) + } + if !parsed.Get("properties.leaf.properties.a.properties.a").Exists() { + t.Errorf("diamond schema outer levels should still be expanded: %s", got) + } + if parsed.Get("$defs").Exists() { + t.Errorf("diamond schema should not retain $defs: %s", got) + } +} + +// TestIssue6732_UnusedDefinitionsDoNotConsumeExpansionBudget verifies that definitions the +// cleaners are about to delete anyway neither consume the expansion budget nor make the kept +// fields depend on map iteration order. +func TestIssue6732_UnusedDefinitionsDoNotConsumeExpansionBudget(t *testing.T) { + var sb strings.Builder + sb.WriteString(`{"type":"object","properties":{"person":{"$ref":"#/$defs/Person"}},"required":["person"],"$defs":{"leaf0":{"type":"string","description":"leaf leaf leaf leaf leaf leaf leaf leaf leaf leaf"},"Person":{"type":"object","properties":{"name":{"type":"string"}},"required":["name"]}`) + for i := range 13 { + target := "leaf0" + if i > 0 { + target = "D" + strconv.Itoa(i-1) + } + sb.WriteString(`,"D` + strconv.Itoa(i) + `":{"type":"object","properties":{"a":{"$ref":"#/$defs/` + target + `"},"b":{"$ref":"#/$defs/` + target + `"}}}`) + } + sb.WriteString(`}}`) + input := sb.String() + + first := CleanJSONSchemaForGeminiJSONSchema(input) + for run := range 30 { + got := CleanJSONSchemaForGeminiJSONSchema(input) + if got != first { + t.Fatalf("run %d: output is not deterministic:\nfirst: %s\ngot: %s", run, first, got) + } + parsed := gjson.Parse(got) + if gotType := parsed.Get("properties.person.properties.name.type").String(); gotType != "string" { + t.Fatalf("run %d: used definition was not inlined, name type = %q: %s", run, gotType, got) + } + if parsed.Get("$defs").Exists() { + t.Fatalf("run %d: $defs should be removed: %s", run, got) + } + } +} + +// TestIssue6732_BudgetFallbackDoesNotCopyLargeDescriptions verifies that references degraded by +// the expansion budget do not each copy a large target description, which would defeat the bound. +func TestIssue6732_BudgetFallbackDoesNotCopyLargeDescriptions(t *testing.T) { + var sb strings.Builder + sb.WriteString(`{"type":"object","properties":{`) + for i := range 200 { + if i > 0 { + sb.WriteByte(',') + } + sb.WriteString(`"p` + strconv.Itoa(i) + `":{"$ref":"#/$defs/Big"}`) + } + sb.WriteString(`},"$defs":{"Big":{"type":"object","description":"` + strings.Repeat("x", 32*1024) + `","properties":{"name":{"type":"string"}}}}}`) + + got := CleanJSONSchemaForGeminiJSONSchema(sb.String()) + if len(got) >= 512*1024 { + t.Errorf("shared large-description output = %d bytes, want < %d", len(got), 512*1024) + } + parsed := gjson.Parse(got) + if !parsed.Get("properties.p0").Exists() || !parsed.Get("properties.p199").Exists() { + t.Errorf("shared large-description schema lost properties: %s", got) + } + if gotType := parsed.Get("properties.p0.properties.name.type").String(); gotType != "string" { + t.Errorf("first affordable expansion should keep its fields, name type = %q: %s", gotType, got) + } + if parsed.Get("$defs").Exists() { + t.Errorf("$defs should be removed: %s", got) + } +} + +// TestIssue6732_NestedUnusedDefinitionsGraphDoesNotConsumeBudget verifies that a self-contained +// reference graph inside a nested definitions container (which cleaning deletes at every level) +// neither spends the expansion budget nor starves kept fields. +func TestIssue6732_NestedUnusedDefinitionsGraphDoesNotConsumeBudget(t *testing.T) { + var sb strings.Builder + sb.WriteString(`{"type":"object","properties":{"person":{"$ref":"#/$defs/Person"},"a":{"type":"object","$defs":{"Leaf":{"type":"string","description":"` + strings.Repeat("x", 1024) + `"}`) + for i := range 256 { + sb.WriteString(`,"U` + strconv.Itoa(i) + `":{"$ref":"#/properties/a/$defs/Leaf"}`) + } + sb.WriteString(`}}},"required":["person"],"$defs":{"Person":{"type":"object","properties":{"name":{"type":"string"}},"required":["name"]}}}`) + + got := CleanJSONSchemaForGeminiJSONSchema(sb.String()) + parsed := gjson.Parse(got) + if gotType := parsed.Get("properties.person.properties.name.type").String(); gotType != "string" { + t.Errorf("nested unused definitions starved the kept field, name type = %q: %s", gotType, got) + } + if parsed.Get("properties.a.$defs").Exists() || parsed.Get("$defs").Exists() { + t.Errorf("nested and root $defs should be removed: %s", got) + } +} + +// TestIssue6732_RefSiblingNameMapReferencesStillExpand verifies that references inside a $ref +// node's own name-map siblings expand when the reference target itself expands. +func TestIssue6732_RefSiblingNameMapReferencesStillExpand(t *testing.T) { + input := `{"type":"object","properties":{"a":{"$ref":"#/$defs/Base","properties":{"x":{"$ref":"#/$defs/S"}}}},"$defs":{"Base":{"type":"object"},"S":{"type":"string"}}}` + + got := CleanJSONSchemaForGeminiJSONSchema(input) + parsed := gjson.Parse(got) + if gotType := parsed.Get("properties.a.properties.x.type").String(); gotType != "string" { + t.Errorf("ref sibling name-map reference lost its type, got %q: %s", gotType, got) + } +} + +// TestIssue6732_CleanInliningStripsDefinitionsFromIntermediate verifies that the cleaning +// inliner's intermediate output does not serialize definition containers (root or carried inside +// expanded targets), so repeated references to a target holding a huge unused definitions block +// cannot multiply that block in the intermediate JSON. +func TestIssue6732_CleanInliningStripsDefinitionsFromIntermediate(t *testing.T) { + person := `{"type":"object","properties":{"name":{"type":"string"}},"$defs":{"Unused":{"type":"string","description":"` + strings.Repeat("x", 280*1024) + `"}}}` + input := `{"type":"object","properties":{"p0":{"$ref":"#/$defs/Person"},"p1":{"$ref":"#/$defs/Person"}},"$defs":{"Person":` + person + `}}` + + got := inlineLocalRefsForClean(input) + if len(got) >= 64*1024 { + t.Errorf("clean inlining intermediate = %d bytes, want < %d", len(got), 64*1024) + } + parsed := gjson.Parse(got) + if parsed.Get("$defs").Exists() { + t.Errorf("intermediate should not carry root $defs: %s", got) + } + if parsed.Get("properties.p0.$defs").Exists() { + t.Errorf("intermediate should not carry target-nested $defs: %s", got) + } + if gotType := parsed.Get("properties.p1.properties.name.type").String(); gotType != "string" { + t.Errorf("second expansion lost its fields, name type = %q: %s", gotType, got) + } +} + +// TestIssue6732_ArrayRefWithExplicitTypeKeepsTargetItems verifies that the placeholder items +// Phase 0 adds to a typed but unresolved $ref node does not clobber the items of the referenced +// definition during inlining. +func TestIssue6732_ArrayRefWithExplicitTypeKeepsTargetItems(t *testing.T) { + input := `{"type":"object","properties":{"rows":{"type":"array","$ref":"#/$defs/Rows"}},"$defs":{"Rows":{"type":"array","items":{"type":"object","properties":{"name":{"type":"string"}}}}}}` + + got := CleanJSONSchemaForGeminiJSONSchema(input) + parsed := gjson.Parse(got) + if gotType := parsed.Get("properties.rows.items.properties.name.type").String(); gotType != "string" { + t.Errorf("array ref target items were clobbered, name type = %q: %s", gotType, got) + } +} + +// TestIssue6732_BudgetFallbackStillResolvesSiblings verifies that when a reference target is too +// large to expand, sibling subschemas on the same node are still resolved instead of being copied +// verbatim with their own references left dangling. +func TestIssue6732_BudgetFallbackStillResolvesSiblings(t *testing.T) { + input := `{"type":"object","properties":{"a":{"$ref":"#/$defs/Big","properties":{"x":{"$ref":"#/$defs/S"}}}},"$defs":{"Big":{"type":"object","description":"` + strings.Repeat("x", 280*1024) + `"},"S":{"type":"string"}}}` + + got := CleanJSONSchemaForGeminiJSONSchema(input) + parsed := gjson.Parse(got) + if gotType := parsed.Get("properties.a.properties.x.type").String(); gotType != "string" { + t.Errorf("sibling reference after budget fallback lost its type, got %q: %s", gotType, got) + } + if strings.Contains(got, `"$ref"`) { + t.Errorf("cleaned schema left a dangling reference: %s", got) + } +} + +// TestIssue6732_InlineLocalRefsDefinitionNamesKeepSchemaSemantics verifies that definitions whose +// names look like schema keywords are treated as schemas by the public inliner: a definition named +// "properties" keeps its literal default intact, and a definition named "default" still expands. +func TestIssue6732_InlineLocalRefsDefinitionNamesKeepSchemaSemantics(t *testing.T) { + input := `{"$defs":{"S":{"type":"string"},"properties":{"type":"object","default":{"$ref":"#/$defs/S"}},"default":{"type":"object","properties":{"x":{"$ref":"#/$defs/S"}}}}}` + + got := InlineLocalRefs(input) + parsed := gjson.Parse(got) + if ref := parsed.Get("$defs.properties.default.$ref").String(); ref != "#/$defs/S" { + t.Errorf("literal default inside definition named \"properties\" was expanded, $ref = %q: %s", ref, got) + } + if gotType := parsed.Get("$defs.default.properties.x.type").String(); gotType != "string" { + t.Errorf("definition named \"default\" was not resolved as a schema, x type = %q: %s", gotType, got) + } +} + +// TestIssue6732_TargetNestedDefinitionsExemptFromBudget verifies that unused definitions nested +// inside a referenced target (which cleaning deletes) do not inflate the target's budget charge. +func TestIssue6732_TargetNestedDefinitionsExemptFromBudget(t *testing.T) { + person := `{"type":"object","properties":{"name":{"type":"string"}},"$defs":{"Unused":{"type":"string","description":"` + strings.Repeat("x", 280*1024) + `"}}}` + cases := map[string]string{ + "propertyRef": `{"type":"object","properties":{"person":{"$ref":"#/$defs/Person"}},"$defs":{"Person":` + person + `}}`, + "rootRef": `{"$ref":"#/$defs/Person","$defs":{"Person":` + person + `}}`, + } + for caseName, input := range cases { + got := CleanJSONSchemaForGeminiJSONSchema(input) + parsed := gjson.Parse(got) + namePath := "properties.person.properties.name.type" + if caseName == "rootRef" { + namePath = "properties.name.type" + } + if gotType := parsed.Get(namePath).String(); gotType != "string" { + t.Errorf("%s: %s = %q, want %q: %s", caseName, namePath, gotType, "string", got) + } + if parsed.Get("$defs").Exists() { + t.Errorf("%s: $defs should be removed: %s", caseName, got) + } + } +} + +// TestIssue6732_ArrayRefWithoutTargetItemsGetsPlaceholder verifies that a typed array whose +// referenced definition carries no items still ends up with the placeholder items Phase 0 would +// have added, and that cleaning twice is stable. +func TestIssue6732_ArrayRefWithoutTargetItemsGetsPlaceholder(t *testing.T) { + input := `{"type":"object","properties":{"rows":{"type":"array","$ref":"#/$defs/Common"}},"$defs":{"Common":{"description":"Rows"}}}` + + first := CleanJSONSchemaForAntigravityTool(input, false) + parsed := gjson.Parse(first) + if gotType := parsed.Get("properties.rows.items.type").String(); gotType != "string" { + t.Errorf("array ref without target items missing placeholder items, got %q: %s", gotType, first) + } + if second := CleanJSONSchemaForAntigravityTool(first, false); second != first { + t.Errorf("cleaning is not idempotent for array refs:\nfirst: %s\nsecond: %s", first, second) + } +} + +// TestIssue6732_SmallTargetFanOutStaysBounded verifies that thousands of references to a small +// target with a near-limit description cannot multiply the description without bound through a +// mix of expansions and fallback copies. +func TestIssue6732_SmallTargetFanOutStaysBounded(t *testing.T) { + var sb strings.Builder + sb.WriteString(`{"type":"object","properties":{`) + for i := range 2000 { + if i > 0 { + sb.WriteByte(',') + } + sb.WriteString(`"p` + strconv.Itoa(i) + `":{"$ref":"#/$defs/S"}`) + } + sb.WriteString(`},"$defs":{"S":{"type":"string","description":"` + strings.Repeat("x", 512) + `"}}}`) + + got := CleanJSONSchemaForGeminiJSONSchema(sb.String()) + if len(got) >= 512*1024 { + t.Errorf("small-target fan-out output = %d bytes, want < %d", len(got), 512*1024) + } +} + +// TestIssue6732_EscapedDescriptionFanOutStaysBounded verifies that budget accounting covers +// HTML-escaped string growth during JSON encoding. +func TestIssue6732_EscapedDescriptionFanOutStaysBounded(t *testing.T) { + input := `{"type":"object","properties":{"p0":{"$ref":"#/$defs/Big"},"p1":{"$ref":"#/$defs/Big"},"p2":{"$ref":"#/$defs/Big"},"p3":{"$ref":"#/$defs/Big"},"p4":{"$ref":"#/$defs/Big"},"p5":{"$ref":"#/$defs/Big"},"p6":{"$ref":"#/$defs/Big"},"p7":{"$ref":"#/$defs/Big"}},"$defs":{"Big":{"type":"object","description":"` + strings.Repeat("<", 32*1024) + `","properties":{"name":{"type":"string"}}}}}` + + got := CleanJSONSchemaForGeminiJSONSchema(input) + if len(got) >= 512*1024 { + t.Errorf("escaped-description fan-out output = %d bytes, want < %d", len(got), 512*1024) + } +} + +// TestIssue6732_DependenciesNameMapKeepsSchemaSemantics verifies that the Draft-07 dependencies +// container behaves as a name map: a dependency keyed "default" holds a schema whose references +// still expand in the public inliner. +func TestIssue6732_DependenciesNameMapKeepsSchemaSemantics(t *testing.T) { + input := `{"type":"object","dependencies":{"default":{"properties":{"x":{"$ref":"#/definitions/S"}}}},"definitions":{"S":{"type":"string"}}}` + + got := InlineLocalRefs(input) + parsed := gjson.Parse(got) + if gotType := parsed.Get("dependencies.default.properties.x.type").String(); gotType != "string" { + t.Errorf("dependency schema named \"default\" was not resolved, x type = %q: %s", gotType, got) + } + if parsed.Get("dependencies.default.properties.x.$ref").Exists() { + t.Errorf("dependency schema reference was left dangling: %s", got) + } +} + +// TestIssue6732_OmittedFallbackDescriptionNotCharged verifies that a target description the +// fallback omits (because it exceeds the copy limit) is not charged to the expansion budget, so +// a following small reference still expands. +func TestIssue6732_OmittedFallbackDescriptionNotCharged(t *testing.T) { + input := `{"type":"object","properties":{"a":{"$ref":"#/$defs/A"},"b":{"$ref":"#/$defs/B"}},"$defs":{"A":{"type":"string","description":"` + strings.Repeat("x", 262142) + `"},"B":{"type":"object","properties":{"name":{"type":"string"}}}}}` + + got := CleanJSONSchemaForGeminiJSONSchema(input) + parsed := gjson.Parse(got) + if gotType := parsed.Get("properties.b.properties.name.type").String(); gotType != "string" { + t.Errorf("small reference after omitted fallback description lost its fields, name type = %q: %s", gotType, got) + } +} + +// TestIssue6732_SynthesizedTypeDoesNotOverrideRefTarget verifies that the type Phase 0 would +// infer from a ref node's sibling items does not clobber the referenced definition's object +// type, and that cleaning the result again is stable. +func TestIssue6732_SynthesizedTypeDoesNotOverrideRefTarget(t *testing.T) { + input := `{"$ref":"#/$defs/O","items":{"type":"integer"},"$defs":{"O":{"type":"object","properties":{"x":{"type":"string"}}}}}` + + first := CleanJSONSchemaForGeminiJSONSchema(input) + parsed := gjson.Parse(first) + if gotType := parsed.Get("type").String(); gotType != "object" { + t.Errorf("root ref target type = %q, want %q: %s", gotType, "object", first) + } + if parsed.Get("items").Exists() { + t.Errorf("object schema should not carry items: %s", first) + } + if gotType := parsed.Get("properties.x.type").String(); gotType != "string" { + t.Errorf("target property lost its type, got %q: %s", gotType, first) + } + if second := CleanJSONSchemaForGeminiJSONSchema(first); second != first { + t.Errorf("cleaning is not idempotent:\nfirst: %s\nsecond: %s", first, second) + } +} + +// TestIssue6732_PropertyNamedRefRemainsProperty verifies that a property literally named "$ref" +// is treated as a property, not as a reference keyword, by the cleaner. +func TestIssue6732_PropertyNamedRefRemainsProperty(t *testing.T) { + input := `{"type":"object","properties":{"$ref":{"type":"string"}}}` + + got := CleanJSONSchemaForGeminiJSONSchema(input) + parsed := gjson.Parse(got) + if gotType := parsed.Get(`properties.\$ref.type`).String(); gotType != "string" { + t.Errorf("property named $ref lost its schema, type = %q: %s", gotType, got) + } + if strings.Contains(got, "See:") { + t.Errorf("property named $ref was converted into a hint: %s", got) + } +} + +// TestIssue6732_PropertyNamedConstRemainsProperty verifies that a property literally named +// "const" holding a schema object is not converted into an enum constraint on its parent. +func TestIssue6732_PropertyNamedConstRemainsProperty(t *testing.T) { + input := `{"type":"object","properties":{"const":{"type":"string"}}}` + + got := CleanJSONSchemaForGeminiJSONSchema(input) + parsed := gjson.Parse(got) + if gotType := parsed.Get(`properties.const.type`).String(); gotType != "string" { + t.Errorf("property named const lost its schema, type = %q: %s", gotType, got) + } + if parsed.Get("enum").Exists() { + t.Errorf("property named const fabricates a root enum: %s", got) + } +} + +// geminiSchemaDiamondInput builds a schema referencing D12, where each Di is an object with two +// properties referencing Di-1 and D0 references a leaf definition twice. Fully expanding it +// produces 2^13 leaf copies. +func geminiSchemaDiamondInput() string { + var sb strings.Builder + sb.WriteString(`{"type":"object","properties":{"leaf":{"$ref":"#/$defs/D12"}},"$defs":{"leaf0":{"type":"string","description":"leaf leaf leaf leaf leaf leaf leaf leaf leaf leaf"}`) + for i := range 13 { + target := "leaf0" + if i > 0 { + target = "D" + strconv.Itoa(i-1) + } + sb.WriteString(`,"D` + strconv.Itoa(i) + `":{"type":"object","properties":{"a":{"$ref":"#/$defs/` + target + `"},"b":{"$ref":"#/$defs/` + target + `"}}}`) + } + sb.WriteString(`}}`) + return sb.String() +} diff --git a/internal/util/translator.go b/internal/util/translator.go index ce4d3505953..945c9867568 100644 --- a/internal/util/translator.go +++ b/internal/util/translator.go @@ -29,6 +29,19 @@ import ( // The function works recursively, building dot-notation paths to each occurrence // of the specified field throughout the JSON structure. func Walk(value gjson.Result, path, field string, paths *[]string) { + walk(value, path, field, paths, false) +} + +// WalkSchema is Walk for JSON Schema documents: it records the field wherever a schema keyword +// may appear, but does not descend into literal values (default, example, examples, const, enum), +// whose contents are client data rather than subschemas. The literal keyword itself is still +// recorded when it is the searched field, and under a property-name map a key spelled like one is +// a property name whose subschema is still walked. +func WalkSchema(value gjson.Result, path, field string, paths *[]string) { + walk(value, path, field, paths, true) +} + +func walk(value gjson.Result, path, field string, paths *[]string, schemaAware bool) { switch value.Type { case gjson.JSON: // For JSON objects and arrays, iterate through each child @@ -49,7 +62,10 @@ func Walk(value gjson.Result, path, field string, paths *[]string) { if keyStr == field { *paths = append(*paths, childPath) } - Walk(val, childPath, field, paths) + if schemaAware && isLiteralValueKeyword(keyStr) && !isPropertyDefinition(path) { + return true + } + walk(val, childPath, field, paths, schemaAware) return true }) case gjson.String, gjson.Number, gjson.True, gjson.False, gjson.Null: From d69962e40ca62c1090e1a06653f3567436876ecd Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Sun, 18 Oct 2026 23:00:32 +0800 Subject: [PATCH 11/27] feat(management): add safe Modify method for auth records with epoch validation and locking - Implement `Modify` method on auth manager to load, clone, edit, and commit records atomically under a per-auth mutation lock. - Refactor auth field patches and status updates to use the new `Modify` API, preventing race conditions and lost concurrent updates. - Add comprehensive test coverage verifying concurrent patch operations, token refreshes, removals, and epoch checks. Closes: #6733 --- .../handlers/management/auth_files_fields.go | 118 +-- .../auth_files_patch_fields_test.go | 721 ++++++++++++++++++ sdk/cliproxy/auth/conductor_lifecycle.go | 47 ++ 3 files changed, 833 insertions(+), 53 deletions(-) diff --git a/internal/api/handlers/management/auth_files_fields.go b/internal/api/handlers/management/auth_files_fields.go index ba2377a84a3..766202a3b8a 100644 --- a/internal/api/handlers/management/auth_files_fields.go +++ b/internal/api/handlers/management/auth_files_fields.go @@ -126,19 +126,21 @@ func (h *Handler) PatchAuthFileStatus(c *gin.Context) { return } - applyAuthDisabledState(targetAuth, *req.Disabled) - updatedAuth, err := h.authManager.Update(ctx, targetAuth) - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("failed to update auth: %v", err)}) + updatedAuth, committed, errModify := h.authManager.Modify(ctx, targetAuth.ID, targetAuth.RegistrationEpoch, func(current *coreauth.Auth) error { + applyAuthDisabledState(current, *req.Disabled) + return nil + }) + if errModify != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("failed to update auth: %v", errModify)}) return } - hookAuth := updatedAuth - if hookAuth == nil { - hookAuth = targetAuth + if !committed || updatedAuth == nil { + c.JSON(http.StatusNotFound, gin.H{"error": "auth file not found"}) + return } locked = false h.authStatusMu.Unlock() - if errHook := h.invokePostAuthPersistHooks(ctx, []*coreauth.Auth{hookAuth}); errHook != nil { + if errHook := h.invokePostAuthPersistHooks(ctx, []*coreauth.Auth{updatedAuth}); errHook != nil { log.Errorf("post-auth persist hook failed for status update on %s: %v", targetAuth.ID, errHook) c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("failed to synchronize auth runtime: %v", errHook)}) return @@ -176,17 +178,18 @@ func (h *Handler) patchPluginVirtualSourceStatus(ctx context.Context, targetAuth !sameAuthFilePath(authAttribute(auth, coreauth.AttributeVirtualSource), sourcePath) { continue } - applyAuthDisabledState(auth, disabled) - auth.UpdatedAt = now - updated, errUpdate := h.authManager.Update(ctx, auth) + updated, committed, errUpdate := h.authManager.Modify(ctx, auth.ID, auth.RegistrationEpoch, func(current *coreauth.Auth) error { + applyAuthDisabledState(current, disabled) + current.UpdatedAt = now + return nil + }) if errUpdate != nil { return nil, fmt.Errorf("failed to update auth %s: %w", auth.ID, errUpdate) } - hookAuth := updated - if hookAuth == nil { - hookAuth = auth + if !committed || updated == nil { + continue } - hookAuths = append(hookAuths, hookAuth) + hookAuths = append(hookAuths, updated) } return hookAuths, nil } @@ -328,6 +331,42 @@ func (h *Handler) PatchAuthFileFields(c *gin.Context) { c.JSON(http.StatusConflict, gin.H{"error": errPluginVirtualAuth.Error()}) return } + + updatedAuth, committed, errModify := h.authManager.Modify(ctx, targetAuth.ID, targetAuth.RegistrationEpoch, func(current *coreauth.Auth) error { + return applyAuthFileFieldsPatch(current, req, requestRetryPatch) + }) + if errModify != nil { + var patchErr *authFileFieldPatchError + if errors.As(errModify, &patchErr) { + c.JSON(http.StatusBadRequest, gin.H{"error": patchErr.message}) + return + } + c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("failed to update auth: %v", errModify)}) + return + } + if !committed || updatedAuth == nil { + c.JSON(http.StatusNotFound, gin.H{"error": "auth file not found"}) + return + } + if h.postAuthPersistHook != nil { + if errHook := h.postAuthPersistHook(ctx, updatedAuth); errHook != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("post-auth persist hook failed: %v", errHook)}) + return + } + } + + c.JSON(http.StatusOK, gin.H{"status": "ok"}) +} + +type authFileFieldPatchError struct { + message string +} + +func (e *authFileFieldPatchError) Error() string { + return e.message +} + +func applyAuthFileFieldsPatch(targetAuth *coreauth.Auth, req map[string]json.RawMessage, requestRetryPatch authFileRequestRetryPatch) error { coreauth.NormalizeCredentialMetadata(targetAuth.Metadata) changed := false @@ -335,18 +374,15 @@ func (h *Handler) PatchAuthFileFields(c *gin.Context) { for key, rawValue := range req { fieldPath := strings.TrimSpace(key) if fieldPath == "" { - c.JSON(http.StatusBadRequest, gin.H{"error": "field name is required"}) - return + return &authFileFieldPatchError{message: "field name is required"} } value, errDecode := decodeAuthFileFieldValue(rawValue) if errDecode != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("invalid field %s", fieldPath)}) - return + return &authFileFieldPatchError{message: fmt.Sprintf("invalid field %s", fieldPath)} } if rootAuthFileField(fieldPath) == "excluded_models" { if fieldPath != "excluded_models" { - c.JSON(http.StatusBadRequest, gin.H{"error": "excluded_models does not support nested fields"}) - return + return &authFileFieldPatchError{message: "excluded_models does not support nested fields"} } if value != nil { models, okModels := value.([]any) @@ -358,8 +394,7 @@ func (h *Handler) PatchAuthFileFields(c *gin.Context) { } } if !validModels { - c.JSON(http.StatusBadRequest, gin.H{"error": "excluded_models must be a string array or null"}) - return + return &authFileFieldPatchError{message: "excluded_models must be a string array or null"} } } } @@ -372,24 +407,20 @@ func (h *Handler) PatchAuthFileFields(c *gin.Context) { delete(targetAuth.Metadata, coreauth.AttributeWeight) } else { if _, okNumber := value.(json.Number); !okNumber { - c.JSON(http.StatusBadRequest, gin.H{"error": "weight must be an integer"}) - return + return &authFileFieldPatchError{message: "weight must be an integer"} } weight, errWeight := credentialweight.ParseValue(value) if errWeight != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": errWeight.Error()}) - return + return &authFileFieldPatchError{message: errWeight.Error()} } targetAuth.Metadata[coreauth.AttributeWeight] = weight } } else if rootAuthFileField(fieldPath) == coreauth.AttributeWeight { - c.JSON(http.StatusBadRequest, gin.H{"error": "weight does not support nested fields"}) - return + return &authFileFieldPatchError{message: "weight does not support nested fields"} } else if fieldPath == "headers" { applyAuthFileHeadersPatch(targetAuth, value) } else if errSet := setAuthFileMetadataValue(targetAuth.Metadata, fieldPath, value); errSet != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": errSet.Error()}) - return + return &authFileFieldPatchError{message: errSet.Error()} } if root := rootAuthFileField(fieldPath); root != "" { touchedRoots[root] = struct{}{} @@ -412,8 +443,7 @@ func (h *Handler) PatchAuthFileFields(c *gin.Context) { if _, touched := touchedRoots["excluded_models"]; touched { excluded, errExcluded := synthesizer.ExtractExcludedModelsFromMetadata(targetAuth.Metadata) if errExcluded != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": errExcluded.Error()}) - return + return &authFileFieldPatchError{message: errExcluded.Error()} } // ApplyAuthExcludedModelsMeta never clears attributes, so drop stale values first. delete(targetAuth.Attributes, "excluded_models") @@ -425,29 +455,11 @@ func (h *Handler) PatchAuthFileFields(c *gin.Context) { } if !changed { - c.JSON(http.StatusBadRequest, gin.H{"error": "no fields to update"}) - return + return &authFileFieldPatchError{message: "no fields to update"} } targetAuth.UpdatedAt = time.Now() - - updatedAuth, err := h.authManager.Update(ctx, targetAuth) - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("failed to update auth: %v", err)}) - return - } - if h.postAuthPersistHook != nil { - hookAuth := updatedAuth - if hookAuth == nil { - hookAuth = targetAuth - } - if errHook := h.postAuthPersistHook(ctx, hookAuth); errHook != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("post-auth persist hook failed: %v", errHook)}) - return - } - } - - c.JSON(http.StatusOK, gin.H{"status": "ok"}) + return nil } func decodeAuthFileFieldValue(raw json.RawMessage) (any, error) { diff --git a/internal/api/handlers/management/auth_files_patch_fields_test.go b/internal/api/handlers/management/auth_files_patch_fields_test.go index a44d1bebee2..4e2331f2830 100644 --- a/internal/api/handlers/management/auth_files_patch_fields_test.go +++ b/internal/api/handlers/management/auth_files_patch_fields_test.go @@ -10,7 +10,9 @@ import ( "os" "path/filepath" "strings" + "sync" "testing" + "testing/synctest" "github.com/gin-gonic/gin" "github.com/router-for-me/CLIProxyAPI/v8/internal/config" @@ -791,3 +793,722 @@ func TestPatchAuthFileFields_IDTokenMissingPlanTypeDefaultsToFree(t *testing.T) t.Fatalf("auth plan_type attribute = %q, want free", got) } } + +type patchRegressionTokenStorage struct { + AccessToken string + RefreshToken string +} + +func (s *patchRegressionTokenStorage) SaveTokenToFile(string) error { return nil } + +type blockingPatchAuthStore struct { + memoryAuthStore + entered chan struct{} + release chan struct{} + once sync.Once + listEntered chan struct{} + listRelease chan struct{} + listOnce sync.Once +} + +func (s *blockingPatchAuthStore) List(ctx context.Context) ([]*coreauth.Auth, error) { + if s.listEntered != nil && s.listRelease != nil { + s.listOnce.Do(func() { + close(s.listEntered) + <-s.listRelease + }) + } + return s.memoryAuthStore.List(ctx) +} + +func (s *blockingPatchAuthStore) Save(ctx context.Context, auth *coreauth.Auth) (string, error) { + if s.entered != nil && s.release != nil { + s.once.Do(func() { + close(s.entered) + <-s.release + }) + } + var cloned *coreauth.Auth + if auth != nil { + cloned = auth.Clone() + } + return s.memoryAuthStore.Save(ctx, cloned) +} + +func (s *blockingPatchAuthStore) savedAuth(id string) *coreauth.Auth { + s.mu.Lock() + defer s.mu.Unlock() + if s.items == nil { + return nil + } + if item := s.items[id]; item != nil { + return item.Clone() + } + return nil +} + +func TestPatchAuthFileFields_KeepsCommitLandedDuringPatch(t *testing.T) { + t.Setenv("MANAGEMENT_PASSWORD", "") + gin.SetMode(gin.TestMode) + + t.Run("concurrent_fields_patch", func(t *testing.T) { + authDir := t.TempDir() + h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: authDir}, nil) + synctest.Test(t, func(t *testing.T) { + store := &blockingPatchAuthStore{} + manager := coreauth.NewManager(store, nil, nil) + h.authManager = manager + base, errRegister := manager.Register(context.Background(), &coreauth.Auth{ + ID: "cred.json", + FileName: "cred.json", + Provider: "codex", + Status: coreauth.StatusActive, + Attributes: map[string]string{ + "path": "/tmp/cred.json", + }, + Metadata: map[string]any{ + "type": "codex", + "access_token": "tok-initial", + "refresh_token": "rt-initial", + }, + }) + if errRegister != nil { + t.Fatalf("Register() error = %v", errRegister) + } + _ = base + store.entered = make(chan struct{}) + store.release = make(chan struct{}) + + firstDone := make(chan int, 1) + go func() { + rec := httptest.NewRecorder() + ctx, _ := gin.CreateTestContext(rec) + req := httptest.NewRequest(http.MethodPatch, "/v8/management/credentials/fields", strings.NewReader(`{"name":"cred.json","prefix":"p1"}`)) + req.Header.Set("Content-Type", "application/json") + ctx.Request = req + h.PatchAuthFileFields(ctx) + firstDone <- rec.Code + }() + <-store.entered + + secondStarted := make(chan struct{}) + secondDone := make(chan int, 1) + go func() { + close(secondStarted) + rec := httptest.NewRecorder() + ctx, _ := gin.CreateTestContext(rec) + req := httptest.NewRequest(http.MethodPatch, "/v8/management/credentials/fields", strings.NewReader(`{"name":"cred.json","note":"second-note"}`)) + req.Header.Set("Content-Type", "application/json") + ctx.Request = req + h.PatchAuthFileFields(ctx) + secondDone <- rec.Code + }() + <-secondStarted + synctest.Wait() + + close(store.release) + if code := <-firstDone; code != http.StatusOK { + t.Fatalf("first PATCH status = %d, want 200", code) + } + if code := <-secondDone; code != http.StatusOK { + t.Fatalf("second PATCH status = %d, want 200", code) + } + + for label, got := range map[string]*coreauth.Auth{ + "runtime": mustGetAuthByID(t, manager, "cred.json"), + "stored": store.savedAuth("cred.json"), + } { + if got == nil { + t.Fatalf("%s auth is nil", label) + } + if got.Prefix != "p1" || got.Metadata["prefix"] != "p1" { + t.Fatalf("%s lost concurrent prefix: prefix=%q metadata.prefix=%#v", label, got.Prefix, got.Metadata["prefix"]) + } + if got.Metadata["note"] != "second-note" || got.Attributes["note"] != "second-note" { + t.Fatalf("%s missing second patch note: metadata.note=%#v attr.note=%q", label, got.Metadata["note"], got.Attributes["note"]) + } + } + }) + }) + + t.Run("concurrent_refresh", func(t *testing.T) { + authDir := t.TempDir() + h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: authDir}, nil) + synctest.Test(t, func(t *testing.T) { + store := &blockingPatchAuthStore{} + manager := coreauth.NewManager(store, nil, nil) + h.authManager = manager + initialStorage := &patchRegressionTokenStorage{AccessToken: "tok-initial", RefreshToken: "rt-initial"} + base, errRegister := manager.Register(context.Background(), &coreauth.Auth{ + ID: "cred.json", + FileName: "cred.json", + Provider: "codex", + Status: coreauth.StatusActive, + Storage: initialStorage, + Attributes: map[string]string{ + "path": "/tmp/cred.json", + }, + Metadata: map[string]any{ + "type": "codex", + "access_token": "tok-initial", + "refresh_token": "rt-initial", + }, + }) + if errRegister != nil { + t.Fatalf("Register() error = %v", errRegister) + } + store.entered = make(chan struct{}) + store.release = make(chan struct{}) + + refreshedStorage := &patchRegressionTokenStorage{AccessToken: "tok-refreshed", RefreshToken: "rt-refreshed"} + refreshed := base.Clone() + refreshed.Storage = refreshedStorage + refreshed.Metadata["access_token"] = "tok-refreshed" + refreshed.Metadata["refresh_token"] = "rt-refreshed" + + refreshDone := make(chan error, 1) + go func() { + _, errRefresh := manager.UpdateRefreshedAuth(context.Background(), base, refreshed) + refreshDone <- errRefresh + }() + <-store.entered + + patchStarted := make(chan struct{}) + patchDone := make(chan int, 1) + go func() { + close(patchStarted) + rec := httptest.NewRecorder() + ctx, _ := gin.CreateTestContext(rec) + req := httptest.NewRequest(http.MethodPatch, "/v8/management/credentials/fields", strings.NewReader(`{"name":"cred.json","prefix":"p1"}`)) + req.Header.Set("Content-Type", "application/json") + ctx.Request = req + h.PatchAuthFileFields(ctx) + patchDone <- rec.Code + }() + <-patchStarted + synctest.Wait() + + close(store.release) + if errRefresh := <-refreshDone; errRefresh != nil { + t.Fatalf("UpdateRefreshedAuth() error = %v", errRefresh) + } + if code := <-patchDone; code != http.StatusOK { + t.Fatalf("PATCH status = %d, want 200", code) + } + + for label, got := range map[string]*coreauth.Auth{ + "runtime": mustGetAuthByID(t, manager, "cred.json"), + "stored": store.savedAuth("cred.json"), + } { + if got == nil { + t.Fatalf("%s auth is nil", label) + } + if got.Prefix != "p1" || got.Metadata["prefix"] != "p1" { + t.Fatalf("%s missing patched prefix: prefix=%q metadata.prefix=%#v", label, got.Prefix, got.Metadata["prefix"]) + } + if got.Metadata["access_token"] != "tok-refreshed" || got.Metadata["refresh_token"] != "rt-refreshed" { + t.Fatalf("%s rolled back refreshed tokens: access_token=%#v refresh_token=%#v", label, got.Metadata["access_token"], got.Metadata["refresh_token"]) + } + gotStorage, okStorage := got.Storage.(*patchRegressionTokenStorage) + if !okStorage || gotStorage == nil || gotStorage.AccessToken != "tok-refreshed" || gotStorage.RefreshToken != "rt-refreshed" { + t.Fatalf("%s rolled back refreshed Storage: %#v", label, got.Storage) + } + if got.CredentialVersion != 2 { + t.Fatalf("%s CredentialVersion = %d, want 2", label, got.CredentialVersion) + } + } + }) + }) + + t.Run("concurrent_remove_answers_404", func(t *testing.T) { + authDir := t.TempDir() + h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: authDir}, nil) + synctest.Test(t, func(t *testing.T) { + store := &blockingPatchAuthStore{} + manager := coreauth.NewManager(store, nil, nil) + h.authManager = manager + base, errRegister := manager.Register(context.Background(), &coreauth.Auth{ + ID: "cred.json", + FileName: "cred.json", + Provider: "codex", + Status: coreauth.StatusActive, + Attributes: map[string]string{ + "path": "/tmp/cred.json", + }, + Metadata: map[string]any{ + "type": "codex", + }, + }) + if errRegister != nil { + t.Fatalf("Register() error = %v", errRegister) + } + store.entered = make(chan struct{}) + store.release = make(chan struct{}) + + hookCalled := false + h.SetPostAuthPersistHook(func(_ context.Context, _ *coreauth.Auth) error { + hookCalled = true + return nil + }) + + saveDone := make(chan error, 1) + go func() { + _, errUpdate := manager.Update(context.Background(), base.Clone()) + saveDone <- errUpdate + }() + <-store.entered + + removeStarted := make(chan struct{}) + removeDone := make(chan struct{}) + go func() { + close(removeStarted) + manager.Remove(context.Background(), "cred.json") + close(removeDone) + }() + <-removeStarted + synctest.Wait() + + patchStarted := make(chan struct{}) + patchDone := make(chan int, 1) + go func() { + close(patchStarted) + rec := httptest.NewRecorder() + ctx, _ := gin.CreateTestContext(rec) + req := httptest.NewRequest(http.MethodPatch, "/v8/management/credentials/fields", strings.NewReader(`{"name":"cred.json","prefix":"p1"}`)) + req.Header.Set("Content-Type", "application/json") + ctx.Request = req + h.PatchAuthFileFields(ctx) + patchDone <- rec.Code + }() + <-patchStarted + synctest.Wait() + + close(store.release) + if errUpdate := <-saveDone; errUpdate != nil { + t.Fatalf("Update() error = %v", errUpdate) + } + <-removeDone + if code := <-patchDone; code != http.StatusNotFound { + t.Fatalf("PATCH status after concurrent Remove = %d, want 404", code) + } + if hookCalled { + t.Fatal("postAuthPersistHook must not run when auth was removed before commit") + } + if _, ok := manager.GetByID("cred.json"); ok { + t.Fatal("removed auth must not be resurrected in manager") + } + }) + }) + + t.Run("concurrent_reregister_preserves_epoch_check", func(t *testing.T) { + authDir := t.TempDir() + h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: authDir}, nil) + synctest.Test(t, func(t *testing.T) { + store := &blockingPatchAuthStore{} + manager := coreauth.NewManager(store, nil, nil) + h.authManager = manager + _, errRegister := manager.Register(context.Background(), &coreauth.Auth{ + ID: "cred.json", + FileName: "cred.json", + Provider: "codex", + Status: coreauth.StatusActive, + Attributes: map[string]string{ + "path": "/tmp/cred.json", + }, + Metadata: map[string]any{ + "type": "codex", + "access_token": "tok-v1", + }, + }) + if errRegister != nil { + t.Fatalf("Register() error = %v", errRegister) + } + // Stage replacement credential in store for Load(). + _, _ = store.memoryAuthStore.Save(context.Background(), &coreauth.Auth{ + ID: "cred.json", + FileName: "cred.json", + Provider: "codex", + Status: coreauth.StatusActive, + Attributes: map[string]string{ + "path": "/tmp/cred.json", + }, + Metadata: map[string]any{ + "type": "codex", + "access_token": "tok-v2", + }, + }) + store.listEntered = make(chan struct{}) + store.listRelease = make(chan struct{}) + + loadDone := make(chan error, 1) + go func() { + loadDone <- manager.Load(context.Background()) + }() + <-store.listEntered + + patchStarted := make(chan struct{}) + patchDone := make(chan int, 1) + go func() { + close(patchStarted) + rec := httptest.NewRecorder() + ctx, _ := gin.CreateTestContext(rec) + req := httptest.NewRequest(http.MethodPatch, "/v8/management/credentials/fields", strings.NewReader(`{"name":"cred.json","prefix":"stale-prefix"}`)) + req.Header.Set("Content-Type", "application/json") + ctx.Request = req + h.PatchAuthFileFields(ctx) + patchDone <- rec.Code + }() + <-patchStarted + synctest.Wait() + + close(store.listRelease) + if errLoad := <-loadDone; errLoad != nil { + t.Fatalf("Load() error = %v", errLoad) + } + if code := <-patchDone; code == http.StatusOK { + t.Fatalf("expected stale-epoch PATCH to be rejected after replacement registration, got 200") + } + + got := mustGetAuthByID(t, manager, "cred.json") + if got.Prefix != "" || got.Metadata["access_token"] != "tok-v2" { + t.Fatalf("stale PATCH landed on replacement registration: prefix=%q access_token=%#v", got.Prefix, got.Metadata["access_token"]) + } + }) + }) +} + +func TestPatchAuthFileStatus_KeepsCommitLandedDuringPatch(t *testing.T) { + t.Setenv("MANAGEMENT_PASSWORD", "") + gin.SetMode(gin.TestMode) + + t.Run("concurrent_fields_patch", func(t *testing.T) { + authDir := t.TempDir() + h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: authDir}, nil) + synctest.Test(t, func(t *testing.T) { + store := &blockingPatchAuthStore{} + manager := coreauth.NewManager(store, nil, nil) + h.authManager = manager + _, errRegister := manager.Register(context.Background(), &coreauth.Auth{ + ID: "cred.json", + FileName: "cred.json", + Provider: "codex", + Status: coreauth.StatusActive, + Attributes: map[string]string{ + "path": "/tmp/cred.json", + }, + Metadata: map[string]any{ + "type": "codex", + "disabled": false, + }, + }) + if errRegister != nil { + t.Fatalf("Register() error = %v", errRegister) + } + store.entered = make(chan struct{}) + store.release = make(chan struct{}) + + fieldsDone := make(chan int, 1) + go func() { + rec := httptest.NewRecorder() + ctx, _ := gin.CreateTestContext(rec) + req := httptest.NewRequest(http.MethodPatch, "/v8/management/credentials/fields", strings.NewReader(`{"name":"cred.json","prefix":"p1"}`)) + req.Header.Set("Content-Type", "application/json") + ctx.Request = req + h.PatchAuthFileFields(ctx) + fieldsDone <- rec.Code + }() + <-store.entered + + statusStarted := make(chan struct{}) + statusDone := make(chan int, 1) + go func() { + close(statusStarted) + rec := httptest.NewRecorder() + ctx, _ := gin.CreateTestContext(rec) + req := httptest.NewRequest(http.MethodPatch, "/v8/management/credentials/status", strings.NewReader(`{"name":"cred.json","disabled":true}`)) + req.Header.Set("Content-Type", "application/json") + ctx.Request = req + h.PatchAuthFileStatus(ctx) + statusDone <- rec.Code + }() + <-statusStarted + synctest.Wait() + + close(store.release) + if code := <-fieldsDone; code != http.StatusOK { + t.Fatalf("fields PATCH status = %d, want 200", code) + } + if code := <-statusDone; code != http.StatusOK { + t.Fatalf("status PATCH status = %d, want 200", code) + } + + for label, got := range map[string]*coreauth.Auth{ + "runtime": mustGetAuthByID(t, manager, "cred.json"), + "stored": store.savedAuth("cred.json"), + } { + if got == nil { + t.Fatalf("%s auth is nil", label) + } + if got.Prefix != "p1" || got.Metadata["prefix"] != "p1" { + t.Fatalf("%s lost concurrent prefix: prefix=%q metadata.prefix=%#v", label, got.Prefix, got.Metadata["prefix"]) + } + if !got.Disabled || got.Status != coreauth.StatusDisabled || got.Metadata["disabled"] != true { + t.Fatalf("%s missing disabled status: disabled=%v status=%v metadata.disabled=%#v", label, got.Disabled, got.Status, got.Metadata["disabled"]) + } + } + }) + }) + + t.Run("concurrent_refresh", func(t *testing.T) { + authDir := t.TempDir() + h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: authDir}, nil) + synctest.Test(t, func(t *testing.T) { + store := &blockingPatchAuthStore{} + manager := coreauth.NewManager(store, nil, nil) + h.authManager = manager + initialStorage := &patchRegressionTokenStorage{AccessToken: "tok-initial", RefreshToken: "rt-initial"} + base, errRegister := manager.Register(context.Background(), &coreauth.Auth{ + ID: "cred.json", + FileName: "cred.json", + Provider: "codex", + Status: coreauth.StatusActive, + Storage: initialStorage, + Attributes: map[string]string{ + "path": "/tmp/cred.json", + }, + Metadata: map[string]any{ + "type": "codex", + "access_token": "tok-initial", + "refresh_token": "rt-initial", + "disabled": false, + }, + }) + if errRegister != nil { + t.Fatalf("Register() error = %v", errRegister) + } + store.entered = make(chan struct{}) + store.release = make(chan struct{}) + + refreshedStorage := &patchRegressionTokenStorage{AccessToken: "tok-refreshed", RefreshToken: "rt-refreshed"} + refreshed := base.Clone() + refreshed.Storage = refreshedStorage + refreshed.Metadata["access_token"] = "tok-refreshed" + refreshed.Metadata["refresh_token"] = "rt-refreshed" + + refreshDone := make(chan error, 1) + go func() { + _, errRefresh := manager.UpdateRefreshedAuth(context.Background(), base, refreshed) + refreshDone <- errRefresh + }() + <-store.entered + + patchStarted := make(chan struct{}) + patchDone := make(chan int, 1) + go func() { + close(patchStarted) + rec := httptest.NewRecorder() + ctx, _ := gin.CreateTestContext(rec) + req := httptest.NewRequest(http.MethodPatch, "/v8/management/credentials/status", strings.NewReader(`{"name":"cred.json","disabled":true}`)) + req.Header.Set("Content-Type", "application/json") + ctx.Request = req + h.PatchAuthFileStatus(ctx) + patchDone <- rec.Code + }() + <-patchStarted + synctest.Wait() + + close(store.release) + if errRefresh := <-refreshDone; errRefresh != nil { + t.Fatalf("UpdateRefreshedAuth() error = %v", errRefresh) + } + if code := <-patchDone; code != http.StatusOK { + t.Fatalf("PATCH status = %d, want 200", code) + } + + for label, got := range map[string]*coreauth.Auth{ + "runtime": mustGetAuthByID(t, manager, "cred.json"), + "stored": store.savedAuth("cred.json"), + } { + if got == nil { + t.Fatalf("%s auth is nil", label) + } + if !got.Disabled || got.Status != coreauth.StatusDisabled || got.Metadata["disabled"] != true { + t.Fatalf("%s missing disabled status: disabled=%v status=%v metadata.disabled=%#v", label, got.Disabled, got.Status, got.Metadata["disabled"]) + } + if got.Metadata["access_token"] != "tok-refreshed" || got.Metadata["refresh_token"] != "rt-refreshed" { + t.Fatalf("%s rolled back refreshed tokens: access_token=%#v refresh_token=%#v", label, got.Metadata["access_token"], got.Metadata["refresh_token"]) + } + gotStorage, okStorage := got.Storage.(*patchRegressionTokenStorage) + if !okStorage || gotStorage == nil || gotStorage.AccessToken != "tok-refreshed" || gotStorage.RefreshToken != "rt-refreshed" { + t.Fatalf("%s rolled back refreshed Storage: %#v", label, got.Storage) + } + if got.CredentialVersion != 2 { + t.Fatalf("%s CredentialVersion = %d, want 2", label, got.CredentialVersion) + } + } + }) + }) + + t.Run("concurrent_remove_answers_404", func(t *testing.T) { + authDir := t.TempDir() + h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: authDir}, nil) + synctest.Test(t, func(t *testing.T) { + store := &blockingPatchAuthStore{} + manager := coreauth.NewManager(store, nil, nil) + h.authManager = manager + base, errRegister := manager.Register(context.Background(), &coreauth.Auth{ + ID: "cred.json", + FileName: "cred.json", + Provider: "codex", + Status: coreauth.StatusActive, + Attributes: map[string]string{ + "path": "/tmp/cred.json", + }, + Metadata: map[string]any{ + "type": "codex", + }, + }) + if errRegister != nil { + t.Fatalf("Register() error = %v", errRegister) + } + store.entered = make(chan struct{}) + store.release = make(chan struct{}) + + hookCalled := false + h.SetPostAuthPersistHook(func(_ context.Context, _ *coreauth.Auth) error { + hookCalled = true + return nil + }) + + saveDone := make(chan error, 1) + go func() { + _, errUpdate := manager.Update(context.Background(), base.Clone()) + saveDone <- errUpdate + }() + <-store.entered + + removeStarted := make(chan struct{}) + removeDone := make(chan struct{}) + go func() { + close(removeStarted) + manager.Remove(context.Background(), "cred.json") + close(removeDone) + }() + <-removeStarted + synctest.Wait() + + patchStarted := make(chan struct{}) + patchDone := make(chan int, 1) + go func() { + close(patchStarted) + rec := httptest.NewRecorder() + ctx, _ := gin.CreateTestContext(rec) + req := httptest.NewRequest(http.MethodPatch, "/v8/management/credentials/status", strings.NewReader(`{"name":"cred.json","disabled":true}`)) + req.Header.Set("Content-Type", "application/json") + ctx.Request = req + h.PatchAuthFileStatus(ctx) + patchDone <- rec.Code + }() + <-patchStarted + synctest.Wait() + + close(store.release) + if errUpdate := <-saveDone; errUpdate != nil { + t.Fatalf("Update() error = %v", errUpdate) + } + <-removeDone + if code := <-patchDone; code != http.StatusNotFound { + t.Fatalf("PATCH status after concurrent Remove = %d, want 404", code) + } + if hookCalled { + t.Fatal("postAuthPersistHook must not run when auth was removed before commit") + } + }) + }) + + t.Run("concurrent_reregister_preserves_epoch_check", func(t *testing.T) { + authDir := t.TempDir() + h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: authDir}, nil) + synctest.Test(t, func(t *testing.T) { + store := &blockingPatchAuthStore{} + manager := coreauth.NewManager(store, nil, nil) + h.authManager = manager + _, errRegister := manager.Register(context.Background(), &coreauth.Auth{ + ID: "cred.json", + FileName: "cred.json", + Provider: "codex", + Status: coreauth.StatusActive, + Attributes: map[string]string{ + "path": "/tmp/cred.json", + }, + Metadata: map[string]any{ + "type": "codex", + "access_token": "tok-v1", + "disabled": false, + }, + }) + if errRegister != nil { + t.Fatalf("Register() error = %v", errRegister) + } + _, _ = store.memoryAuthStore.Save(context.Background(), &coreauth.Auth{ + ID: "cred.json", + FileName: "cred.json", + Provider: "codex", + Status: coreauth.StatusActive, + Attributes: map[string]string{ + "path": "/tmp/cred.json", + }, + Metadata: map[string]any{ + "type": "codex", + "access_token": "tok-v2", + "disabled": false, + }, + }) + store.listEntered = make(chan struct{}) + store.listRelease = make(chan struct{}) + + loadDone := make(chan error, 1) + go func() { + loadDone <- manager.Load(context.Background()) + }() + <-store.listEntered + + patchStarted := make(chan struct{}) + patchDone := make(chan int, 1) + go func() { + close(patchStarted) + rec := httptest.NewRecorder() + ctx, _ := gin.CreateTestContext(rec) + req := httptest.NewRequest(http.MethodPatch, "/v8/management/credentials/status", strings.NewReader(`{"name":"cred.json","disabled":true}`)) + req.Header.Set("Content-Type", "application/json") + ctx.Request = req + h.PatchAuthFileStatus(ctx) + patchDone <- rec.Code + }() + <-patchStarted + synctest.Wait() + + close(store.listRelease) + if errLoad := <-loadDone; errLoad != nil { + t.Fatalf("Load() error = %v", errLoad) + } + if code := <-patchDone; code == http.StatusOK { + t.Fatalf("expected stale-epoch PATCH to be rejected after replacement registration, got 200") + } + + got := mustGetAuthByID(t, manager, "cred.json") + if got.Disabled || got.Metadata["access_token"] != "tok-v2" { + t.Fatalf("stale PATCH landed on replacement registration: disabled=%v access_token=%#v", got.Disabled, got.Metadata["access_token"]) + } + }) + }) +} + +func mustGetAuthByID(t *testing.T, manager *coreauth.Manager, id string) *coreauth.Auth { + t.Helper() + got, ok := manager.GetByID(id) + if !ok || got == nil { + t.Fatalf("expected auth %q in manager", id) + } + return got +} diff --git a/sdk/cliproxy/auth/conductor_lifecycle.go b/sdk/cliproxy/auth/conductor_lifecycle.go index a2952e41a64..852eb5d84eb 100644 --- a/sdk/cliproxy/auth/conductor_lifecycle.go +++ b/sdk/cliproxy/auth/conductor_lifecycle.go @@ -189,6 +189,53 @@ func (m *Manager) Update(ctx context.Context, auth *Auth) (*Auth, error) { return m.updateInternal(ctx, nil, auth, updateModeReplace) } +// Modify loads the latest auth for id while holding the per-auth mutation gate, +// applies edit to a clone of that auth, and commits the result through the +// standard persistence and publication path. It returns (nil, false, nil) if +// the auth is not present when the gate is acquired. +func (m *Manager) Modify(ctx context.Context, id string, expectedEpoch uint64, edit func(*Auth) error) (*Auth, bool, error) { + id = strings.TrimSpace(id) + if m == nil || id == "" || edit == nil { + return nil, false, nil + } + releaseMutation, errMutation := m.lockAuthMutationContext(ctx, id) + if errMutation != nil { + return nil, false, errMutation + } + defer releaseMutation() + + m.mu.RLock() + existing, ok := m.auths[id] + var current *Auth + var currentEpoch uint64 + if ok && existing != nil { + current = existing.Clone() + currentEpoch = existing.RegistrationEpoch + if m.authEpochs != nil && m.authEpochs[id] > currentEpoch { + currentEpoch = m.authEpochs[id] + } + } + m.mu.RUnlock() + if current == nil { + return nil, false, nil + } + if expectedEpoch != 0 && expectedEpoch < currentEpoch { + return nil, false, fmt.Errorf("update auth %s: stale registration epoch %d < %d", id, expectedEpoch, currentEpoch) + } + if errEdit := edit(current); errEdit != nil { + return nil, false, errEdit + } + if expectedEpoch != 0 { + current.RegistrationEpoch = expectedEpoch + } + committedAuth, didCommit, persistCooldown, errUpdate := m.updateInternalLocked(ctx, nil, current, updateModeReplace) + releaseMutation() + if errUpdate == nil && didCommit && committedAuth != nil { + m.finishAuthUpdate(ctx, committedAuth, persistCooldown) + } + return committedAuth, errUpdate == nil && didCommit && committedAuth != nil, errUpdate +} + func (m *Manager) updateInternal(ctx context.Context, base, auth *Auth, mode updateAuthMode) (*Auth, error) { if auth == nil || auth.ID == "" { return nil, nil From 7484a5216a332fe468becb3ad16ced0151b036f1 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Mon, 19 Oct 2026 00:03:26 +0800 Subject: [PATCH 12/27] feat(watcher): protect credentials from unindexed deletion during incomplete or in-place file writes - Track incomplete JSON and unreadable files during auth snapshots, retaining existing auth indexes and preventing premature credential removals during in-place writes. - Buffer and restore active auths during affected provider reloads when auth files are temporarily truncated or unparseable. - Add comprehensive test coverage for in-place file truncation, partial JSON recovery, and provider reload race conditions. Closes: #6734 --- internal/watcher/clients.go | 92 +++-- internal/watcher/dispatcher.go | 175 ++++++++- internal/watcher/dispatcher_snapshot_test.go | 379 ++++++++++++++++++- internal/watcher/synthesizer/context.go | 14 + internal/watcher/synthesizer/file.go | 6 +- internal/watcher/synthesizer/file_test.go | 47 +++ internal/watcher/watcher.go | 71 ++-- internal/watcher/watcher_test.go | 2 +- 8 files changed, 700 insertions(+), 86 deletions(-) diff --git a/internal/watcher/clients.go b/internal/watcher/clients.go index 7c188daca4f..5fb0c1969c2 100644 --- a/internal/watcher/clients.go +++ b/internal/watcher/clients.go @@ -8,6 +8,7 @@ import ( "encoding/hex" "encoding/json" "fmt" + "maps" "os" "path/filepath" "strings" @@ -38,20 +39,24 @@ func (w *Watcher) reloadClients(rescanAuth bool, affectedOAuthProviders []string w.clientsMutex.Lock() if w.currentAuths != nil { filtered := make(map[string]*coreauth.Auth, len(w.currentAuths)) + pendingReload := make(map[string]*coreauth.Auth) for id, auth := range w.currentAuths { if auth == nil { continue } provider := strings.ToLower(strings.TrimSpace(auth.Provider)) if _, match := matchProvider(provider, affectedOAuthProviders); match { + pendingReload[id] = auth continue } filtered[id] = auth } w.currentAuths = filtered + w.pendingProviderReloadAuths = pendingReload log.Debugf("applying oauth-excluded-models to providers %v", affectedOAuthProviders) } else { w.currentAuths = nil + w.pendingProviderReloadAuths = nil } w.clientsMutex.Unlock() } @@ -80,6 +85,7 @@ func (w *Watcher) reloadClients(rescanAuth bool, affectedOAuthProviders []string newAuthContents = make(map[string]*coreauth.Auth) } newFileAuthsByPath := make(map[string]map[string]*coreauth.Auth) + var incompleteRescanPaths map[string]struct{} w.clientsMutex.RLock() parser := w.pluginAuthParser @@ -101,37 +107,74 @@ func (w *Watcher) reloadClients(rescanAuth bool, affectedOAuthProviders []string continue } fullPath := filepath.Join(resolvedAuthDir, name) - if data, errReadFile := os.ReadFile(fullPath); errReadFile == nil && len(data) > 0 { - sum := sha256.Sum256(data) - normalizedPath := w.normalizeAuthPath(fullPath) - newAuthHashes[normalizedPath] = hex.EncodeToString(sum[:]) - // Parse and cache auth content for future diff comparisons (debug only). - if cacheAuthContents { - var auth coreauth.Auth - if errParse := json.Unmarshal(data, &auth); errParse == nil { - newAuthContents[normalizedPath] = &auth - } + data, errReadFile := os.ReadFile(fullPath) + if errReadFile != nil { + continue + } + normalizedPath := w.normalizeAuthPath(fullPath) + if !json.Valid(data) { + if incompleteRescanPaths == nil { + incompleteRescanPaths = make(map[string]struct{}) } - ctx := &synthesizer.SynthesisContext{ - Config: cfg, - AuthDir: resolvedAuthDir, - Now: time.Now(), - IDGenerator: synthesizer.NewStableIDGenerator(), - PluginAuthParser: parser, + incompleteRescanPaths[normalizedPath] = struct{}{} + continue + } + sum := sha256.Sum256(data) + newAuthHashes[normalizedPath] = hex.EncodeToString(sum[:]) + // Parse and cache auth content for future diff comparisons (debug only). + if cacheAuthContents { + var auth coreauth.Auth + if errParse := json.Unmarshal(data, &auth); errParse == nil { + newAuthContents[normalizedPath] = &auth } - generated, errSynthesize := synthesizer.SynthesizeAuthFile(ctx, fullPath, data) - if errSynthesize != nil { - log.WithError(errSynthesize).Warnf("skipping auth file %s", name) - } else if len(generated) > 0 { - if pathAuths := authSliceToMap(generated); len(pathAuths) > 0 { - newFileAuthsByPath[normalizedPath] = authIDSet(pathAuths) - } + } + ctx := &synthesizer.SynthesisContext{ + Config: cfg, + AuthDir: resolvedAuthDir, + Now: time.Now(), + IDGenerator: synthesizer.NewStableIDGenerator(), + PluginAuthParser: parser, + } + generated, errSynthesize := synthesizer.SynthesizeAuthFile(ctx, fullPath, data) + if errSynthesize != nil { + log.WithError(errSynthesize).Warnf("skipping auth file %s", name) + } else if len(generated) > 0 { + if pathAuths := authSliceToMap(generated); len(pathAuths) > 0 { + newFileAuthsByPath[normalizedPath] = authIDSet(pathAuths) } } } } } w.clientsMutex.Lock() + for normalizedPath := range incompleteRescanPaths { + if _, ok := w.lastAuthHashes[normalizedPath]; ok { + newAuthHashes[normalizedPath] = "" + } + if cacheAuthContents && w.lastAuthContents != nil { + if prevContent, ok := w.lastAuthContents[normalizedPath]; ok { + newAuthContents[normalizedPath] = prevContent + } + } + if prevAuths, ok := w.fileAuthsByPath[normalizedPath]; ok && len(prevAuths) > 0 { + newFileAuthsByPath[normalizedPath] = maps.Clone(prevAuths) + } + } + // Keep IDs that are still in currentAuths or pending provider reload indexed + // under their file path until refreshAuthState commits the new state. + for path, prevAuths := range w.fileAuthsByPath { + for id := range prevAuths { + _, stillCurrent := w.currentAuths[id] + _, pendingReload := w.pendingProviderReloadAuths[id] + if !stillCurrent && !pendingReload { + continue + } + if newFileAuthsByPath[path] == nil { + newFileAuthsByPath[path] = make(map[string]*coreauth.Auth) + } + newFileAuthsByPath[path][id] = nil + } + } w.lastAuthHashes = newAuthHashes w.lastAuthContents = newAuthContents w.fileAuthsByPath = newFileAuthsByPath @@ -332,6 +375,9 @@ func (w *Watcher) computePerPathUpdatesLocked(oldByID, newByID map[string]*corea continue } delete(w.currentAuths, id) + if w.pendingProviderReloadAuths != nil { + delete(w.pendingProviderReloadAuths, id) + } updates = append(updates, AuthUpdate{Action: AuthUpdateActionDelete, ID: id}) } w.stampAuthUpdatesLocked(updates) diff --git a/internal/watcher/dispatcher.go b/internal/watcher/dispatcher.go index 29d5a7559fb..dc81f181522 100644 --- a/internal/watcher/dispatcher.go +++ b/internal/watcher/dispatcher.go @@ -6,7 +6,9 @@ import ( "context" "fmt" "maps" + "path/filepath" "reflect" + "strings" "sync" "time" @@ -15,7 +17,7 @@ import ( coreauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth" ) -var snapshotCoreAuthsFunc = snapshotCoreAuths +var snapshotCoreAuthsFunc = snapshotCoreAuthsWithIncomplete func (w *Watcher) setAuthUpdateQueue(queue chan<- AuthUpdate) { w.clientsMutex.Lock() @@ -68,6 +70,9 @@ func (w *Watcher) dispatchRuntimeAuthUpdate(update AuthUpdate) bool { if w.currentAuths != nil { delete(w.currentAuths, id) } + if w.pendingProviderReloadAuths != nil { + delete(w.pendingProviderReloadAuths, id) + } } } updates := []AuthUpdate{update} @@ -144,29 +149,127 @@ func (w *Watcher) refreshAuthState(force bool) { previous := maps.Clone(w.authRevisions) previousFiles := maps.Clone(w.fileObservations) w.clientsMutex.Unlock() - auths := snapshotCoreAuthsFunc(cfg, authDir, parser) + auths, incompleteFiles := snapshotCoreAuthsFunc(cfg, authDir, parser) w.clientsMutex.Lock() w.activeAuthScans-- - // A full scan may finish after a newer file or persisted-auth update. Keep - // those concurrent changes, including deletions, instead of publishing stale data. - changedDuringScan := func(auth *coreauth.Auth) bool { + var incompletePaths map[string]struct{} + if len(incompleteFiles) > 0 { + incompletePaths = make(map[string]struct{}, len(incompleteFiles)*2) + for path := range incompleteFiles { + normalized := w.normalizeAuthPath(path) + if normalized == "" { + continue + } + incompletePaths[normalized] = struct{}{} + var normalizedAbs string + if abs, errAbs := filepath.Abs(normalized); errAbs == nil { + normalizedAbs = w.normalizeAuthPath(abs) + if normalizedAbs != "" { + incompletePaths[normalizedAbs] = struct{}{} + } + } + if previousFiles[normalized] == w.fileObservations[normalized] && + (normalizedAbs == "" || previousFiles[normalizedAbs] == w.fileObservations[normalizedAbs]) { + if _, known := w.lastAuthHashes[normalized]; known { + w.lastAuthHashes[normalized] = "" + } + if normalizedAbs != "" { + if _, known := w.lastAuthHashes[normalizedAbs]; known { + w.lastAuthHashes[normalizedAbs] = "" + } + } + } + } + } + authPath := func(auth *coreauth.Auth) string { path := auth.Attributes[coreauth.AttributePath] if path == "" { path = auth.Attributes[coreauth.AttributeSource] } - normalized := w.normalizeAuthPath(path) + return w.normalizeAuthPath(path) + } + // A full scan may finish after a newer file or persisted-auth update. Keep + // those concurrent changes, including deletions, instead of publishing stale data. + changedDuringScan := func(auth *coreauth.Auth) bool { + normalized := authPath(auth) return previous[auth.ID] != w.authRevisions[auth.ID] || previousFiles[normalized] != w.fileObservations[normalized] } + incompleteDuringScan := func(auth *coreauth.Auth) bool { + if len(incompletePaths) == 0 { + return false + } + normalized := authPath(auth) + if normalized == "" { + return false + } + if _, ok := incompletePaths[normalized]; ok { + return true + } + if abs, errAbs := filepath.Abs(normalized); errAbs == nil { + if _, ok := incompletePaths[w.normalizeAuthPath(abs)]; ok { + return true + } + } + return false + } + isFileBackedAuth := func(auth *coreauth.Auth) bool { + if auth == nil || len(auth.Attributes) == 0 { + return false + } + if auth.Attributes[coreauth.AttributePath] != "" { + return true + } + source := auth.Attributes[coreauth.AttributeSource] + return source != "" && !strings.HasPrefix(source, "config:") + } for index, auth := range auths { - if auth != nil && changedDuringScan(auth) { + if auth != nil && (changedDuringScan(auth) || incompleteDuringScan(auth)) { auths[index] = nil } } + providerReloadAuths := w.pendingProviderReloadAuths + w.pendingProviderReloadAuths = nil + if len(providerReloadAuths) > 0 { + scannedIDs := make(map[string]struct{}, len(auths)) + for _, auth := range auths { + if auth != nil && auth.ID != "" { + scannedIDs[auth.ID] = struct{}{} + } + } + for id, auth := range providerReloadAuths { + if auth == nil || previous[id] != w.authRevisions[id] { + continue + } + if _, exists := w.currentAuths[id]; exists { + continue + } + if incompleteDuringScan(auth) { + if isFileBackedAuth(auth) { + w.indexFileBackedAuthLocked(auth) + } + auths = append(auths, auth.Clone()) + continue + } + if _, scanned := scannedIDs[id]; !scanned { + if w.currentAuths == nil { + w.currentAuths = make(map[string]*coreauth.Auth) + } + w.currentAuths[id] = auth + } + } + } for _, auth := range w.currentAuths { - if auth != nil && changedDuringScan(auth) { - auths = append(auths, auth.Clone()) + if auth == nil { + continue + } + if !changedDuringScan(auth) && !incompleteDuringScan(auth) { + continue + } + if isFileBackedAuth(auth) { + w.indexFileBackedAuthLocked(auth) } + auths = append(auths, auth.Clone()) } if len(w.runtimeAuths) > 0 { for _, a := range w.runtimeAuths { @@ -180,6 +283,39 @@ func (w *Watcher) refreshAuthState(force bool) { w.dispatchAuthUpdates(updates) } +func (w *Watcher) indexFileBackedAuthLocked(auth *coreauth.Auth) { + if auth == nil || auth.ID == "" || len(auth.Attributes) == 0 { + return + } + path := auth.Attributes[coreauth.AttributePath] + if path == "" { + source := auth.Attributes[coreauth.AttributeSource] + if !strings.HasPrefix(source, "config:") { + path = source + } + } + normalized := w.normalizeAuthPath(path) + if normalized == "" { + return + } + if w.fileAuthsByPath == nil { + w.fileAuthsByPath = make(map[string]map[string]*coreauth.Auth) + } + pathAuths := w.fileAuthsByPath[normalized] + if pathAuths == nil { + pathAuths = make(map[string]*coreauth.Auth) + w.fileAuthsByPath[normalized] = pathAuths + } + pathAuths[auth.ID] = nil + if abs, errAbs := filepath.Abs(normalized); errAbs == nil { + if normalizedAbs := w.normalizeAuthPath(abs); normalizedAbs != "" && normalizedAbs != normalized { + if absAuths := w.fileAuthsByPath[normalizedAbs]; absAuths != nil { + absAuths[auth.ID] = nil + } + } + } +} + func (w *Watcher) prepareAuthUpdatesLocked(auths []*coreauth.Auth, force bool) []AuthUpdate { newState := make(map[string]*coreauth.Auth, len(auths)) orderedIDs := make([]string, 0, len(auths)) @@ -190,7 +326,9 @@ func (w *Watcher) prepareAuthUpdatesLocked(auths []*coreauth.Auth, force bool) [ if _, exists := newState[auth.ID]; !exists { orderedIDs = append(orderedIDs, auth.ID) } - newState[auth.ID] = auth.Clone() + cloned := auth.Clone() + newState[auth.ID] = cloned + w.indexFileBackedAuthLocked(cloned) } updates := make([]AuthUpdate, 0, len(newState)+len(w.currentAuths)) for _, id := range orderedIDs { @@ -211,6 +349,16 @@ func (w *Watcher) prepareAuthUpdatesLocked(auths []*coreauth.Auth, force bool) [ updates = append(updates, AuthUpdate{Action: AuthUpdateActionDelete, ID: id}) } } + for path, pathAuths := range w.fileAuthsByPath { + for id := range pathAuths { + if _, ok := newState[id]; !ok { + delete(pathAuths, id) + } + } + if len(pathAuths) == 0 { + delete(w.fileAuthsByPath, path) + } + } w.currentAuths = newState w.stampAuthUpdatesLocked(updates) if w.authQueue == nil { @@ -373,6 +521,11 @@ func normalizeAuth(a *coreauth.Auth) *coreauth.Auth { } func snapshotCoreAuths(cfg *config.Config, authDir string, parser synthesizer.PluginAuthParser) []*coreauth.Auth { + auths, _ := snapshotCoreAuthsWithIncomplete(cfg, authDir, parser) + return auths +} + +func snapshotCoreAuthsWithIncomplete(cfg *config.Config, authDir string, parser synthesizer.PluginAuthParser) ([]*coreauth.Auth, map[string]struct{}) { ctx := &synthesizer.SynthesisContext{ Config: cfg, AuthDir: authDir, @@ -393,5 +546,5 @@ func snapshotCoreAuths(cfg *config.Config, authDir string, parser synthesizer.Pl out = append(out, auths...) } - return out + return out, ctx.IncompleteFiles } diff --git a/internal/watcher/dispatcher_snapshot_test.go b/internal/watcher/dispatcher_snapshot_test.go index fa31a8a07f6..339ae7861fc 100644 --- a/internal/watcher/dispatcher_snapshot_test.go +++ b/internal/watcher/dispatcher_snapshot_test.go @@ -23,11 +23,11 @@ func pauseAuthSnapshot(t *testing.T, w *Watcher, force bool) func() { scanned := make(chan struct{}) resume := make(chan struct{}) finished := make(chan struct{}) - snapshotCoreAuthsFunc = func(cfg *config.Config, authDir string, parser synthesizer.PluginAuthParser) []*coreauth.Auth { - auths := originalSnapshot(cfg, authDir, parser) + snapshotCoreAuthsFunc = func(cfg *config.Config, authDir string, parser synthesizer.PluginAuthParser) ([]*coreauth.Auth, map[string]struct{}) { + auths, incomplete := originalSnapshot(cfg, authDir, parser) close(scanned) <-resume - return auths + return auths, incomplete } var finishOnce sync.Once finish := func() { @@ -130,13 +130,13 @@ func TestRefreshAuthStatePreservesConcurrentRoundTrip(t *testing.T) { w.authQueue = make(chan AuthUpdate, 10) originalSnapshot := snapshotCoreAuthsFunc t.Cleanup(func() { snapshotCoreAuthsFunc = originalSnapshot }) - snapshotCoreAuthsFunc = func(cfg *config.Config, authDir string, parser synthesizer.PluginAuthParser) []*coreauth.Auth { + snapshotCoreAuthsFunc = func(cfg *config.Config, authDir string, parser synthesizer.PluginAuthParser) ([]*coreauth.Auth, map[string]struct{}) { // Deliver two events during the scan, ending at the starting content. if errWrite := os.WriteFile(path, []byte(`{"type":"codex","proxy_url":"http://intermediate.proxy:8080"}`), 0o600); errWrite != nil { t.Fatal(errWrite) } w.addOrUpdateClient(path) - auths := originalSnapshot(cfg, authDir, parser) + auths, incomplete := originalSnapshot(cfg, authDir, parser) if initiallyPresent { if errWrite := os.WriteFile(path, []byte(`{"type":"codex"}`), 0o600); errWrite != nil { t.Fatal(errWrite) @@ -148,7 +148,7 @@ func TestRefreshAuthStatePreservesConcurrentRoundTrip(t *testing.T) { } w.removeClient(path) } - return auths + return auths, incomplete } w.refreshAuthState(false) current := w.currentAuths["account.json"] @@ -190,8 +190,8 @@ func TestRefreshAuthStatePreservesUnchangedFileObservation(t *testing.T) { } originalSnapshot := snapshotCoreAuthsFunc t.Cleanup(func() { snapshotCoreAuthsFunc = originalSnapshot }) - snapshotCoreAuthsFunc = func(cfg *config.Config, authDir string, parser synthesizer.PluginAuthParser) []*coreauth.Auth { - auths := originalSnapshot(cfg, authDir, parser) + snapshotCoreAuthsFunc = func(cfg *config.Config, authDir string, parser synthesizer.PluginAuthParser) ([]*coreauth.Auth, map[string]struct{}) { + auths, incomplete := originalSnapshot(cfg, authDir, parser) latest := original if testCase.formatted { latest = []byte(`{ "type": "codex", "proxy_url": "http://latest.proxy:8080" }`) @@ -204,7 +204,7 @@ func TestRefreshAuthStatePreservesUnchangedFileObservation(t *testing.T) { } else { w.addOrUpdateClient(path) } - return auths + return auths, incomplete } w.refreshAuthState(false) current := w.currentAuths["account.json"] @@ -234,8 +234,8 @@ func TestRefreshAuthStateAppliesHashCachedFileObservation(t *testing.T) { } originalSnapshot := snapshotCoreAuthsFunc t.Cleanup(func() { snapshotCoreAuthsFunc = originalSnapshot }) - snapshotCoreAuthsFunc = func(cfg *config.Config, authDir string, parser synthesizer.PluginAuthParser) []*coreauth.Auth { - auths := originalSnapshot(cfg, authDir, parser) + snapshotCoreAuthsFunc = func(cfg *config.Config, authDir string, parser synthesizer.PluginAuthParser) ([]*coreauth.Auth, map[string]struct{}) { + auths, incomplete := originalSnapshot(cfg, authDir, parser) // reloadClients cached the new hash before publishing its matching auth. if phase == "after_scan" { // Suspend the event after observation, before content processing. @@ -243,7 +243,7 @@ func TestRefreshAuthStateAppliesHashCachedFileObservation(t *testing.T) { } else { w.handleEvent(fsnotify.Event{Name: path, Op: fsnotify.Write}) } - return auths + return auths, incomplete } w.reloadClients(true, nil, false) if phase == "after_scan" { @@ -267,13 +267,13 @@ func TestRefreshAuthStateDoesNotResurrectUnregisteredFile(t *testing.T) { w := &Watcher{authDir: dir, config: &config.Config{AuthDir: dir}, authQueue: make(chan AuthUpdate, 10)} originalSnapshot := snapshotCoreAuthsFunc t.Cleanup(func() { snapshotCoreAuthsFunc = originalSnapshot }) - snapshotCoreAuthsFunc = func(cfg *config.Config, authDir string, parser synthesizer.PluginAuthParser) []*coreauth.Auth { - auths := originalSnapshot(cfg, authDir, parser) + snapshotCoreAuthsFunc = func(cfg *config.Config, authDir string, parser synthesizer.PluginAuthParser) ([]*coreauth.Auth, map[string]struct{}) { + auths, incomplete := originalSnapshot(cfg, authDir, parser) if errRemove := os.Remove(path); errRemove != nil { t.Fatal(errRemove) } w.removeClient(path) - return auths + return auths, incomplete } w.refreshAuthState(false) if w.currentAuths["account.json"] != nil || len(w.pendingOrder) != 0 { @@ -539,3 +539,352 @@ func TestWatcherFileSnapshotReformatAndUnmappedChangeDoNotDropUpdate(t *testing. } }) } + +func TestRefreshAuthStatePreservesIncompleteFileDuringInPlaceWrite(t *testing.T) { + for _, tc := range []struct { + name string + midContent string + fullReload bool + }{ + {name: "truncated_empty_refresh", midContent: ""}, + {name: "partial_json_refresh", midContent: `{"type":"plugin","ids":["b"`}, + {name: "truncated_empty_reload_clients", midContent: "", fullReload: true}, + {name: "partial_json_reload_clients", midContent: `{"type":"plugin","ids":["b"`, fullReload: true}, + } { + t.Run(tc.name, func(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "accounts.json") + w := &Watcher{ + authDir: dir, + config: &config.Config{AuthDir: dir}, + authQueue: make(chan AuthUpdate, 16), + pluginAuthParser: &fileDeleteTestParser{}, + } + if errWrite := os.WriteFile(path, []byte(`{"type":"plugin","ids":["a","b"]}`), 0o600); errWrite != nil { + t.Fatal(errWrite) + } + w.addOrUpdateClient(path) + if _, okBatch := w.nextPendingBatch(context.Background()); !okBatch { + t.Fatal("expected initial add batch") + } + + // Simulate an in-place writer opening with O_WRONLY|O_TRUNC and optionally flushing partial JSON. + file, errOpen := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0o600) + if errOpen != nil { + t.Fatal(errOpen) + } + if tc.midContent != "" { + if _, errWrite := file.WriteString(tc.midContent); errWrite != nil { + _ = file.Close() + t.Fatal(errWrite) + } + } + + if tc.fullReload { + w.reloadClients(true, nil, false) + } else { + w.refreshAuthState(false) + } + + if w.currentAuths["a"] == nil || w.currentAuths["b"] == nil { + _ = file.Close() + t.Fatalf("full scan unregistered credentials during in-place write: currentAuths=%#v, pending=%#v", w.currentAuths, w.pendingUpdates) + } + if len(w.pendingOrder) != 0 { + _ = file.Close() + t.Fatalf("expected no queued deletes during in-place write, got %#v", w.pendingUpdates) + } + + // Finish the in-place write with ["b","c"] (dropping "a" and adding "c"). + if _, errSeek := file.Seek(0, 0); errSeek != nil { + _ = file.Close() + t.Fatal(errSeek) + } + if errTrunc := file.Truncate(0); errTrunc != nil { + _ = file.Close() + t.Fatal(errTrunc) + } + if _, errWrite := file.WriteString(`{"type":"plugin","ids":["b","c"]}`); errWrite != nil { + _ = file.Close() + t.Fatal(errWrite) + } + if errClose := file.Close(); errClose != nil { + t.Fatal(errClose) + } + + w.handleEvent(fsnotify.Event{Name: path, Op: fsnotify.Write}) + + if w.currentAuths["a"] != nil { + t.Fatalf("expected writer's final event to remove dropped child a, got %#v", w.currentAuths["a"]) + } + if w.currentAuths["b"] == nil || w.currentAuths["c"] == nil { + t.Fatalf("expected b and c in currentAuths after write finished, got %#v", w.currentAuths) + } + if gotDelete := w.pendingUpdates["a"]; gotDelete.Action != AuthUpdateActionDelete { + t.Fatalf("expected delete update for dropped child a, got %+v", gotDelete) + } + if gotAdd := w.pendingUpdates["c"]; gotAdd.Action != AuthUpdateActionAdd { + t.Fatalf("expected add update for new child c, got %+v", gotAdd) + } + }) + } + + t.Run("reload_clients_pass_divergence_retains_file_auth_index", func(t *testing.T) { + for _, mode := range []string{"without_early_event", "with_early_truncated_write_event", "final_write_event_during_scan"} { + t.Run(mode, func(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "accounts.json") + w := &Watcher{ + authDir: dir, + config: &config.Config{AuthDir: dir}, + authQueue: make(chan AuthUpdate, 16), + pluginAuthParser: &fileDeleteTestParser{}, + } + if errWrite := os.WriteFile(path, []byte(`{"type":"plugin","ids":["a","b"]}`), 0o600); errWrite != nil { + t.Fatal(errWrite) + } + w.addOrUpdateClient(path) + if _, okBatch := w.nextPendingBatch(context.Background()); !okBatch { + t.Fatal("expected initial add batch") + } + + // reloadClients pass 1 reads complete ["b","c"], then an in-place write truncates + // the file right before refreshAuthState runs snapshotCoreAuthsFunc. + if errWrite := os.WriteFile(path, []byte(`{"type":"plugin","ids":["b","c"]}`), 0o600); errWrite != nil { + t.Fatal(errWrite) + } + originalSnapshot := snapshotCoreAuthsFunc + t.Cleanup(func() { snapshotCoreAuthsFunc = originalSnapshot }) + var file *os.File + snapshotCoreAuthsFunc = func(cfg *config.Config, authDir string, parser synthesizer.PluginAuthParser) ([]*coreauth.Auth, map[string]struct{}) { + var errOpen error + file, errOpen = os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0o600) + if errOpen != nil { + t.Fatal(errOpen) + } + auths, incomplete := originalSnapshot(cfg, authDir, parser) + switch mode { + case "with_early_truncated_write_event": + // An early write event for the truncated file increments fileObservations + // during the scan but returns early without updating fileAuthsByPath. + w.handleEvent(fsnotify.Event{Name: path, Op: fsnotify.Write}) + case "final_write_event_during_scan": + if _, errWrite := file.WriteString(`{"type":"plugin","ids":["b","c"]}`); errWrite != nil { + _ = file.Close() + t.Fatal(errWrite) + } + if errClose := file.Close(); errClose != nil { + t.Fatal(errClose) + } + file = nil + w.handleEvent(fsnotify.Event{Name: path, Op: fsnotify.Write}) + } + return auths, incomplete + } + + w.reloadClients(true, nil, false) + snapshotCoreAuthsFunc = originalSnapshot + + if file != nil { + if w.currentAuths["a"] == nil || w.currentAuths["b"] == nil { + _ = file.Close() + t.Fatalf("expected a and b to survive incomplete refresh scan, got %#v", w.currentAuths) + } + + if _, errWrite := file.WriteString(`{"type":"plugin","ids":["b","c"]}`); errWrite != nil { + _ = file.Close() + t.Fatal(errWrite) + } + if errClose := file.Close(); errClose != nil { + t.Fatal(errClose) + } + + w.handleEvent(fsnotify.Event{Name: path, Op: fsnotify.Write}) + } + + if w.currentAuths["a"] != nil { + t.Fatalf("expected writer's final event to remove retained child a, got %#v", w.currentAuths["a"]) + } + if w.currentAuths["b"] == nil || w.currentAuths["c"] == nil { + t.Fatalf("expected b and c in currentAuths after write finished, got %#v", w.currentAuths) + } + if gotDelete := w.pendingUpdates["a"]; gotDelete.Action != AuthUpdateActionDelete { + t.Fatalf("expected delete update for dropped child a, got %+v", gotDelete) + } + if gotAdd := w.pendingUpdates["c"]; gotAdd.Action != AuthUpdateActionAdd { + t.Fatalf("expected add update for new child c, got %+v", gotAdd) + } + }) + } + }) + + t.Run("affected_oauth_provider_reload_during_in_place_write", func(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "account.json") + w := &Watcher{ + authDir: dir, + config: &config.Config{AuthDir: dir}, + authQueue: make(chan AuthUpdate, 16), + } + if errWrite := os.WriteFile(path, []byte(`{"type":"codex"}`), 0o600); errWrite != nil { + t.Fatal(errWrite) + } + w.addOrUpdateClient(path) + if _, okBatch := w.nextPendingBatch(context.Background()); !okBatch { + t.Fatal("expected initial add batch") + } + + file, errOpen := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0o600) + if errOpen != nil { + t.Fatal(errOpen) + } + _ = file.Close() + + w.reloadClients(false, []string{"codex"}, false) + + if w.currentAuths["account.json"] == nil { + t.Fatalf("expected codex auth to survive incomplete scan during provider reload, got %#v", w.currentAuths) + } + if errRemove := os.Remove(path); errRemove != nil { + t.Fatal(errRemove) + } + w.removeClient(path) + if gotDelete := w.pendingUpdates["account.json"]; gotDelete.Action != AuthUpdateActionDelete { + t.Fatalf("expected removeClient to emit delete after incomplete provider reload, got %+v", gotDelete) + } + }) + + t.Run("affected_oauth_provider_reload_does_not_resurrect_deleted_auth", func(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "account.json") + w := &Watcher{ + authDir: dir, + config: &config.Config{AuthDir: dir}, + authQueue: make(chan AuthUpdate, 16), + } + if errWrite := os.WriteFile(path, []byte(`{"type":"codex"}`), 0o600); errWrite != nil { + t.Fatal(errWrite) + } + w.addOrUpdateClient(path) + w.dispatchRuntimeAuthUpdate(AuthUpdate{ + Action: AuthUpdateActionAdd, + ID: "ws-codex", + Auth: &coreauth.Auth{ID: "ws-codex", Provider: "codex", Status: coreauth.StatusActive}, + }) + if _, okBatch := w.nextPendingBatch(context.Background()); !okBatch { + t.Fatal("expected initial add batch") + } + + file, errOpen := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0o600) + if errOpen != nil { + t.Fatal(errOpen) + } + _ = file.Close() + + originalSnapshot := snapshotCoreAuthsFunc + t.Cleanup(func() { snapshotCoreAuthsFunc = originalSnapshot }) + snapshotCoreAuthsFunc = func(cfg *config.Config, authDir string, parser synthesizer.PluginAuthParser) ([]*coreauth.Auth, map[string]struct{}) { + auths, incomplete := originalSnapshot(cfg, authDir, parser) + w.dispatchRuntimeAuthUpdate(AuthUpdate{Action: AuthUpdateActionDelete, ID: "ws-codex"}) + if errRemove := os.Remove(path); errRemove != nil { + t.Fatal(errRemove) + } + w.removeClient(path) + return auths, incomplete + } + + w.reloadClients(false, []string{"codex"}, false) + + if w.currentAuths["ws-codex"] != nil || w.pendingUpdates["ws-codex"].Action != AuthUpdateActionDelete { + t.Fatalf("provider reload resurrected deleted runtime auth: current=%#v, pending=%+v", w.currentAuths["ws-codex"], w.pendingUpdates["ws-codex"]) + } + if w.currentAuths["account.json"] != nil || w.pendingUpdates["account.json"].Action != AuthUpdateActionDelete { + t.Fatalf("provider reload resurrected deleted file auth: current=%#v, pending=%+v", w.currentAuths["account.json"], w.pendingUpdates["account.json"]) + } + }) + + t.Run("affected_oauth_provider_reload_deletes_removed_or_zero_auth_file", func(t *testing.T) { + for _, mode := range []string{"removed_file", "valid_empty_object"} { + t.Run(mode, func(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "account.json") + w := &Watcher{ + authDir: dir, + config: &config.Config{AuthDir: dir}, + authQueue: make(chan AuthUpdate, 16), + } + if errWrite := os.WriteFile(path, []byte(`{"type":"codex"}`), 0o600); errWrite != nil { + t.Fatal(errWrite) + } + w.addOrUpdateClient(path) + if _, okBatch := w.nextPendingBatch(context.Background()); !okBatch { + t.Fatal("expected initial add batch") + } + + if mode == "removed_file" { + if errRemove := os.Remove(path); errRemove != nil { + t.Fatal(errRemove) + } + } else { + if errWrite := os.WriteFile(path, []byte(`{}`), 0o600); errWrite != nil { + t.Fatal(errWrite) + } + } + + w.reloadClients(false, []string{"codex"}, false) + + if w.currentAuths["account.json"] != nil { + t.Fatalf("expected %s to remove codex auth during provider reload, got %#v", mode, w.currentAuths["account.json"]) + } + if gotDelete := w.pendingUpdates["account.json"]; gotDelete.Action != AuthUpdateActionDelete { + t.Fatalf("expected delete update for %s during provider reload, got %+v", mode, gotDelete) + } + }) + } + }) + + t.Run("control_valid_zero_auth_and_removed_file_still_delete", func(t *testing.T) { + for _, mode := range []string{"valid_empty_object", "valid_plugin_empty", "removed_file"} { + t.Run(mode, func(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "accounts.json") + w := &Watcher{ + authDir: dir, + config: &config.Config{AuthDir: dir}, + authQueue: make(chan AuthUpdate, 16), + pluginAuthParser: &fileDeleteTestParser{}, + } + if errWrite := os.WriteFile(path, []byte(`{"type":"plugin","ids":["a"]}`), 0o600); errWrite != nil { + t.Fatal(errWrite) + } + w.addOrUpdateClient(path) + if _, okBatch := w.nextPendingBatch(context.Background()); !okBatch { + t.Fatal("expected initial add batch") + } + + switch mode { + case "valid_empty_object": + if errWrite := os.WriteFile(path, []byte(`{}`), 0o600); errWrite != nil { + t.Fatal(errWrite) + } + case "valid_plugin_empty": + if errWrite := os.WriteFile(path, []byte(`{"type":"plugin","ids":[]}`), 0o600); errWrite != nil { + t.Fatal(errWrite) + } + case "removed_file": + if errRemove := os.Remove(path); errRemove != nil { + t.Fatal(errRemove) + } + } + + w.refreshAuthState(false) + if w.currentAuths["a"] != nil { + t.Fatalf("expected %s to remove auth a, got %#v", mode, w.currentAuths["a"]) + } + if got := w.pendingUpdates["a"]; got.Action != AuthUpdateActionDelete { + t.Fatalf("expected delete update for %s, got %+v", mode, got) + } + }) + } + }) +} diff --git a/internal/watcher/synthesizer/context.go b/internal/watcher/synthesizer/context.go index 2864327bb12..10d0be2306e 100644 --- a/internal/watcher/synthesizer/context.go +++ b/internal/watcher/synthesizer/context.go @@ -34,4 +34,18 @@ type SynthesisContext struct { PluginAuthParser PluginAuthParser // StrictFileParsing surfaces parse errors instead of silently skipping or falling back. StrictFileParsing bool + // IncompleteFiles records existing auth JSON file paths whose contents were + // empty or not valid JSON during a non-strict directory scan. + IncompleteFiles map[string]struct{} +} + +// RecordIncompleteFile records an existing auth JSON file whose content is not yet complete JSON. +func (c *SynthesisContext) RecordIncompleteFile(path string) { + if c == nil || path == "" { + return + } + if c.IncompleteFiles == nil { + c.IncompleteFiles = make(map[string]struct{}) + } + c.IncompleteFiles[path] = struct{}{} } diff --git a/internal/watcher/synthesizer/file.go b/internal/watcher/synthesizer/file.go index f65d09d4277..0ceb9eb6ef2 100644 --- a/internal/watcher/synthesizer/file.go +++ b/internal/watcher/synthesizer/file.go @@ -49,7 +49,11 @@ func (s *FileSynthesizer) Synthesize(ctx *SynthesisContext) ([]*coreauth.Auth, e } full := filepath.Join(ctx.AuthDir, name) data, errRead := os.ReadFile(full) - if errRead != nil || len(data) == 0 { + if errRead != nil { + continue + } + if !json.Valid(data) { + ctx.RecordIncompleteFile(full) continue } auths, errSynthesize := synthesizeFileAuths(ctx, full, data) diff --git a/internal/watcher/synthesizer/file_test.go b/internal/watcher/synthesizer/file_test.go index 98f465fbe12..bfb65f99fea 100644 --- a/internal/watcher/synthesizer/file_test.go +++ b/internal/watcher/synthesizer/file_test.go @@ -1108,3 +1108,50 @@ func TestSynthesizeAuthFile_CodexPlanType(t *testing.T) { }) } } + +func TestFileSynthesizerRecordsIncompleteFiles(t *testing.T) { + tempDir := t.TempDir() + emptyPath := filepath.Join(tempDir, "empty.json") + partialPath := filepath.Join(tempDir, "partial.json") + validZeroPath := filepath.Join(tempDir, "valid_zero.json") + validAuthPath := filepath.Join(tempDir, "valid_auth.json") + + if errWrite := os.WriteFile(emptyPath, nil, 0o600); errWrite != nil { + t.Fatal(errWrite) + } + if errWrite := os.WriteFile(partialPath, []byte(`{"type":"codex"`), 0o600); errWrite != nil { + t.Fatal(errWrite) + } + if errWrite := os.WriteFile(validZeroPath, []byte(`{}`), 0o600); errWrite != nil { + t.Fatal(errWrite) + } + if errWrite := os.WriteFile(validAuthPath, []byte(`{"type":"codex"}`), 0o600); errWrite != nil { + t.Fatal(errWrite) + } + + ctx := &SynthesisContext{ + Config: &config.Config{}, + AuthDir: tempDir, + Now: time.Now(), + IDGenerator: NewStableIDGenerator(), + } + auths, errSynthesize := NewFileSynthesizer().Synthesize(ctx) + if errSynthesize != nil { + t.Fatalf("Synthesize() error = %v", errSynthesize) + } + if len(auths) != 1 || auths[0].ID != "valid_auth.json" { + t.Fatalf("Synthesize() auths = %#v, want only valid_auth.json", auths) + } + if _, ok := ctx.IncompleteFiles[emptyPath]; !ok { + t.Fatalf("expected empty file %s in IncompleteFiles, got %#v", emptyPath, ctx.IncompleteFiles) + } + if _, ok := ctx.IncompleteFiles[partialPath]; !ok { + t.Fatalf("expected partial file %s in IncompleteFiles, got %#v", partialPath, ctx.IncompleteFiles) + } + if _, ok := ctx.IncompleteFiles[validZeroPath]; ok { + t.Fatalf("did not expect valid zero-auth file %s in IncompleteFiles", validZeroPath) + } + if _, ok := ctx.IncompleteFiles[validAuthPath]; ok { + t.Fatalf("did not expect valid auth file %s in IncompleteFiles", validAuthPath) + } +} diff --git a/internal/watcher/watcher.go b/internal/watcher/watcher.go index a2734deba81..b10e0b84819 100644 --- a/internal/watcher/watcher.go +++ b/internal/watcher/watcher.go @@ -31,41 +31,42 @@ type authDirProvider interface { // Watcher manages file watching for configuration and authentication files type Watcher struct { - configPath string - authDir string - config *config.Config - clientsMutex sync.RWMutex - authRescanMu sync.Mutex - configReloadMu sync.Mutex - configReloadTimer *time.Timer - configApplyMu sync.Mutex // Serializes config file reloads so they apply in file order. - serverUpdateMu sync.Mutex - serverUpdateTimer *time.Timer - serverUpdateLast time.Time - serverUpdatePend bool - stopped atomic.Bool - reloadCallback func(*config.Config) - watcher *fsnotify.Watcher - lastAuthHashes map[string]string - lastAuthContents map[string]*coreauth.Auth - fileAuthsByPath map[string]map[string]*coreauth.Auth - lastRemoveTimes map[string]time.Time - lastConfigHash string - authQueue chan<- AuthUpdate - currentAuths map[string]*coreauth.Auth - authRevisions map[string]uint64 // Includes deletion tombstones; guarded by clientsMutex. - fileObservations map[string]uint64 // Tracks file events even when content is unchanged. - activeAuthScans int // Guarded by clientsMutex. - runtimeAuths map[string]*coreauth.Auth - dispatchMu sync.Mutex - dispatchCond *sync.Cond - pendingUpdates map[string]AuthUpdate - pendingOrder []string - dispatchCancel context.CancelFunc - storePersister storePersister - pluginAuthParser synthesizer.PluginAuthParser - mirroredAuthDir string - oldConfigYaml []byte + configPath string + authDir string + config *config.Config + clientsMutex sync.RWMutex + authRescanMu sync.Mutex + configReloadMu sync.Mutex + configReloadTimer *time.Timer + configApplyMu sync.Mutex // Serializes config file reloads so they apply in file order. + serverUpdateMu sync.Mutex + serverUpdateTimer *time.Timer + serverUpdateLast time.Time + serverUpdatePend bool + stopped atomic.Bool + reloadCallback func(*config.Config) + watcher *fsnotify.Watcher + lastAuthHashes map[string]string + lastAuthContents map[string]*coreauth.Auth + fileAuthsByPath map[string]map[string]*coreauth.Auth + lastRemoveTimes map[string]time.Time + lastConfigHash string + authQueue chan<- AuthUpdate + currentAuths map[string]*coreauth.Auth + pendingProviderReloadAuths map[string]*coreauth.Auth + authRevisions map[string]uint64 // Includes deletion tombstones; guarded by clientsMutex. + fileObservations map[string]uint64 // Tracks file events even when content is unchanged. + activeAuthScans int // Guarded by clientsMutex. + runtimeAuths map[string]*coreauth.Auth + dispatchMu sync.Mutex + dispatchCond *sync.Cond + pendingUpdates map[string]AuthUpdate + pendingOrder []string + dispatchCancel context.CancelFunc + storePersister storePersister + pluginAuthParser synthesizer.PluginAuthParser + mirroredAuthDir string + oldConfigYaml []byte } // AuthUpdateAction represents the type of change detected in auth sources. diff --git a/internal/watcher/watcher_test.go b/internal/watcher/watcher_test.go index b9eafeaa89d..9df65e6b7e2 100644 --- a/internal/watcher/watcher_test.go +++ b/internal/watcher/watcher_test.go @@ -440,7 +440,7 @@ func TestAuthFileEventsDoNotInvokeSnapshotCoreAuths(t *testing.T) { origSnapshot := snapshotCoreAuthsFunc var snapshotCalls int32 - snapshotCoreAuthsFunc = func(cfg *config.Config, authDir string, parser synthesizer.PluginAuthParser) []*coreauth.Auth { + snapshotCoreAuthsFunc = func(cfg *config.Config, authDir string, parser synthesizer.PluginAuthParser) ([]*coreauth.Auth, map[string]struct{}) { atomic.AddInt32(&snapshotCalls, 1) return origSnapshot(cfg, authDir, parser) } From 50cfd2380fc08573e2fc9d08be90b778167e2840 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Mon, 19 Oct 2026 00:23:19 +0800 Subject: [PATCH 13/27] feat(management): add support for static and plugin model definitions retrieval by channel - Implement fallback logic in model definitions handler to query plugin host and global registry for unknown channels. - Add sorting for registry model definitions by provider and ID to ensure consistent responses. Closes: #6736 --- .../handlers/management/model_definitions.go | 34 +++- .../management/model_definitions_test.go | 153 ++++++++++++++++++ 2 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 internal/api/handlers/management/model_definitions_test.go diff --git a/internal/api/handlers/management/model_definitions.go b/internal/api/handlers/management/model_definitions.go index 4e85c72f259..e1bbdfd0073 100644 --- a/internal/api/handlers/management/model_definitions.go +++ b/internal/api/handlers/management/model_definitions.go @@ -2,9 +2,11 @@ package management import ( "net/http" + "sort" "strings" "github.com/gin-gonic/gin" + "github.com/router-for-me/CLIProxyAPI/v8/internal/pluginhost" "github.com/router-for-me/CLIProxyAPI/v8/internal/registry" ) @@ -21,7 +23,10 @@ func (h *Handler) GetStaticModelDefinitions(c *gin.Context) { } models := registry.GetStaticModelDefinitionsByChannel(channel) - if models == nil { + if len(models) == 0 { + models = h.pluginModelDefinitionsByChannel(channel) + } + if len(models) == 0 { c.JSON(http.StatusBadRequest, gin.H{"error": "unknown channel", "channel": channel}) return } @@ -31,3 +36,30 @@ func (h *Handler) GetStaticModelDefinitions(c *gin.Context) { "models": models, }) } + +func (h *Handler) pluginModelDefinitionsByChannel(channel string) []*registry.ModelInfo { + var host *pluginhost.Host + if h != nil { + h.mu.Lock() + host = h.pluginHost + h.mu.Unlock() + } + if host != nil { + if models := host.ModelsForProvider(channel); len(models) > 0 { + return models + } + } + models := registry.GetGlobalRegistry().GetAvailableModelsByProvider(channel) + if len(models) > 1 { + sort.Slice(models, func(i, j int) bool { + if models[i] == nil { + return models[j] != nil + } + if models[j] == nil { + return false + } + return models[i].ID < models[j].ID + }) + } + return models +} diff --git a/internal/api/handlers/management/model_definitions_test.go b/internal/api/handlers/management/model_definitions_test.go new file mode 100644 index 00000000000..73b3ededc01 --- /dev/null +++ b/internal/api/handlers/management/model_definitions_test.go @@ -0,0 +1,153 @@ +package management + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "github.com/gin-gonic/gin" + "github.com/router-for-me/CLIProxyAPI/v8/internal/config" + "github.com/router-for-me/CLIProxyAPI/v8/internal/pluginhost" + "github.com/router-for-me/CLIProxyAPI/v8/internal/registry" + coreauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth" + "github.com/router-for-me/CLIProxyAPI/v8/sdk/pluginapi" +) + +type staticPluginModelProvider struct { + provider string + models []pluginapi.ModelInfo +} + +func (p staticPluginModelProvider) StaticModels(context.Context, pluginapi.StaticModelRequest) (pluginapi.ModelResponse, error) { + return pluginapi.ModelResponse{ + Provider: p.provider, + Models: p.models, + }, nil +} + +func (p staticPluginModelProvider) ModelsForAuth(context.Context, pluginapi.AuthModelRequest) (pluginapi.ModelResponse, error) { + return pluginapi.ModelResponse{}, nil +} + +func invokeGetStaticModelDefinitions(t *testing.T, h *Handler, channel string) (*httptest.ResponseRecorder, struct { + Channel string `json:"channel"` + Error string `json:"error"` + Models []registry.ModelInfo `json:"models"` +}) { + t.Helper() + gin.SetMode(gin.TestMode) + rec := httptest.NewRecorder() + ctx, _ := gin.CreateTestContext(rec) + ctx.Params = gin.Params{{Key: "channel", Value: channel}} + ctx.Request = httptest.NewRequest(http.MethodGet, "/v8/management/routing/model-definitions/"+channel, nil) + h.GetStaticModelDefinitions(ctx) + + var body struct { + Channel string `json:"channel"` + Error string `json:"error"` + Models []registry.ModelInfo `json:"models"` + } + if errUnmarshal := json.Unmarshal(rec.Body.Bytes(), &body); errUnmarshal != nil { + t.Fatalf("unmarshal response body %q: %v", rec.Body.String(), errUnmarshal) + } + return rec, body +} + +func TestGetStaticModelDefinitions_BuiltInChannel(t *testing.T) { + h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: t.TempDir()}, nil) + + rec, body := invokeGetStaticModelDefinitions(t, h, "claude") + if rec.Code != http.StatusOK { + t.Fatalf("status = %d, want %d; body = %s", rec.Code, http.StatusOK, rec.Body.String()) + } + if body.Channel != "claude" { + t.Fatalf("channel = %q, want %q", body.Channel, "claude") + } + if len(body.Models) == 0 { + t.Fatal("expected non-empty models for built-in channel claude") + } +} + +func TestGetStaticModelDefinitions_FallsBackToPluginStaticModels(t *testing.T) { + const provider = "workbuddy" + reg := registry.GetGlobalRegistry() + clientID := "workbuddy-cred-1.json" + reg.RegisterClient(clientID, provider, []*registry.ModelInfo{ + {ID: "workbuddy-from-registry", DisplayName: "WorkBuddy Registry"}, + }) + t.Cleanup(func() { + reg.UnregisterClient(clientID) + }) + + host := pluginhost.New() + pluginID := "workbuddy-plugin" + host.RegisterPluginForTest(pluginID, pluginapi.Plugin{ + Capabilities: pluginapi.Capabilities{ + ModelProvider: staticPluginModelProvider{ + provider: provider, + models: []pluginapi.ModelInfo{ + {ID: "workbuddy-pro", DisplayName: "WorkBuddy Pro"}, + {ID: "workbuddy-lite", DisplayName: "WorkBuddy Lite"}, + }, + }, + }, + }) + host.RegisterModels(t.Context(), reg) + host.RegisterExecutors(coreauth.NewManager(nil, nil, nil), reg) + + h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: t.TempDir()}, nil) + h.SetPluginHost(host) + + rec, body := invokeGetStaticModelDefinitions(t, h, "WorkBuddy") + if rec.Code != http.StatusOK { + t.Fatalf("status = %d, want %d; body = %s", rec.Code, http.StatusOK, rec.Body.String()) + } + if body.Channel != provider { + t.Fatalf("channel = %q, want %q", body.Channel, provider) + } + if len(body.Models) != 2 || body.Models[0].ID != "workbuddy-pro" || body.Models[1].ID != "workbuddy-lite" { + t.Fatalf("models = %#v, want [workbuddy-pro workbuddy-lite]", body.Models) + } +} + +func TestGetStaticModelDefinitions_FallsBackToRegistryModelsForPerAuthPlugin(t *testing.T) { + const provider = "qoderwork" + reg := registry.GetGlobalRegistry() + clientID := "qoderwork-cred-1.json" + reg.RegisterClient(clientID, provider, []*registry.ModelInfo{ + {ID: "qwen3.8-plus-QoderWork", DisplayName: "Qwen 3.8 Plus"}, + {ID: "qwen3.8-flash-QoderWork", DisplayName: "Qwen 3.8 Flash"}, + }) + t.Cleanup(func() { + reg.UnregisterClient(clientID) + }) + + h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: t.TempDir()}, nil) + h.SetPluginHost(pluginhost.New()) + + rec, body := invokeGetStaticModelDefinitions(t, h, "QoderWork") + if rec.Code != http.StatusOK { + t.Fatalf("status = %d, want %d; body = %s", rec.Code, http.StatusOK, rec.Body.String()) + } + if body.Channel != provider { + t.Fatalf("channel = %q, want %q", body.Channel, provider) + } + if len(body.Models) != 2 || body.Models[0].ID != "qwen3.8-flash-QoderWork" || body.Models[1].ID != "qwen3.8-plus-QoderWork" { + t.Fatalf("models = %#v, want [qwen3.8-flash-QoderWork qwen3.8-plus-QoderWork]", body.Models) + } +} + +func TestGetStaticModelDefinitions_RejectsUnknownChannel(t *testing.T) { + h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: t.TempDir()}, nil) + h.SetPluginHost(pluginhost.New()) + + rec, body := invokeGetStaticModelDefinitions(t, h, "nonexistent-provider") + if rec.Code != http.StatusBadRequest { + t.Fatalf("status = %d, want %d; body = %s", rec.Code, http.StatusBadRequest, rec.Body.String()) + } + if body.Error != "unknown channel" || body.Channel != "nonexistent-provider" { + t.Fatalf("body = %#v, want unknown channel error for nonexistent-provider", body) + } +} From 5de9de9ae2430f321d473b88039aef748ec3c522 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Mon, 19 Oct 2026 00:55:46 +0800 Subject: [PATCH 14/27] perf(gemini): pin request payload and resolved tool maps per stream in OpenAI response translators - Cache and reuse initial request JSON and tool name maps across stream chunks to avoid redundant evaluations and prevent map resets. - Add robust state flags to track request and tool map resolution state for Gemini chat and responses translators. Closes: #6741 --- .../gemini_openai_response.go | 27 +++--- .../gemini_openai_response_test.go | 46 ++++++++++ .../gemini_openai-responses_response.go | 41 ++++++--- .../gemini_openai-responses_response_test.go | 89 +++++++++++++++++++ 4 files changed, 181 insertions(+), 22 deletions(-) diff --git a/internal/translator/gemini/openai/chat-completions/gemini_openai_response.go b/internal/translator/gemini/openai/chat-completions/gemini_openai_response.go index 2ff55037a22..c8e4b0185cc 100644 --- a/internal/translator/gemini/openai/chat-completions/gemini_openai_response.go +++ b/internal/translator/gemini/openai/chat-completions/gemini_openai_response.go @@ -24,10 +24,11 @@ import ( type convertGeminiResponseToOpenAIChatParams struct { UnixTimestamp int64 // FunctionIndex tracks tool call indices per candidate index to support multiple candidates. - FunctionIndex map[int]int - SawToolCall map[int]bool - UpstreamFinishReason map[int]string - SanitizedNameMap map[string]string + FunctionIndex map[int]int + SawToolCall map[int]bool + UpstreamFinishReason map[int]string + SanitizedNameMap map[string]string + SanitizedNameMapResolved bool } // functionCallIDCounter provides a process-wide unique counter for function call identifiers. @@ -51,11 +52,12 @@ func ConvertGeminiResponseToOpenAI(_ context.Context, _ string, originalRequestR // Initialize parameters if nil. if *param == nil { *param = &convertGeminiResponseToOpenAIChatParams{ - UnixTimestamp: 0, - FunctionIndex: make(map[int]int), - SawToolCall: make(map[int]bool), - UpstreamFinishReason: make(map[int]string), - SanitizedNameMap: util.SanitizedToolNameMap(originalRequestRawJSON), + UnixTimestamp: 0, + FunctionIndex: make(map[int]int), + SawToolCall: make(map[int]bool), + UpstreamFinishReason: make(map[int]string), + SanitizedNameMap: util.SanitizedToolNameMap(originalRequestRawJSON), + SanitizedNameMapResolved: true, } } @@ -70,8 +72,11 @@ func ConvertGeminiResponseToOpenAI(_ context.Context, _ string, originalRequestR if p.UpstreamFinishReason == nil { p.UpstreamFinishReason = make(map[int]string) } - if p.SanitizedNameMap == nil { - p.SanitizedNameMap = util.SanitizedToolNameMap(originalRequestRawJSON) + if !p.SanitizedNameMapResolved { + if p.SanitizedNameMap == nil { + p.SanitizedNameMap = util.SanitizedToolNameMap(originalRequestRawJSON) + } + p.SanitizedNameMapResolved = true } if bytes.HasPrefix(rawJSON, []byte("data:")) { diff --git a/internal/translator/gemini/openai/chat-completions/gemini_openai_response_test.go b/internal/translator/gemini/openai/chat-completions/gemini_openai_response_test.go index 3f2bb39be22..8e603f2da4b 100644 --- a/internal/translator/gemini/openai/chat-completions/gemini_openai_response_test.go +++ b/internal/translator/gemini/openai/chat-completions/gemini_openai_response_test.go @@ -2,6 +2,7 @@ package chat_completions import ( "context" + "strings" "testing" "github.com/tidwall/gjson" @@ -126,3 +127,48 @@ func TestConvertGeminiResponseToOpenAINonStream_TextPrecedenceOverAudioTranscrip t.Fatalf("expected content to prioritize explicit text, got %v. Output: %s", content.Raw, result) } } + +func TestConvertGeminiResponseToOpenAI_PinsSanitizedNameMapPerStream(t *testing.T) { + ctx := context.Background() + + // 1. Only the first chunk carries the request with a sanitized tool name. + var param any + reqWithTool := []byte(`{"model":"gemini-2.5-flash","tools":[{"name":"mcp/search"}]}`) + chunk1 := []byte(`{"candidates":[{"content":{"parts":[{"text":"thinking"}]}}]}`) + chunk2 := []byte(`{"candidates":[{"content":{"parts":[{"functionCall":{"name":"mcp_search","args":{"q":"test"}}}]}}]}`) + + ConvertGeminiResponseToOpenAI(ctx, "gemini-2.5-flash", reqWithTool, nil, chunk1, ¶m) + out := ConvertGeminiResponseToOpenAI(ctx, "gemini-2.5-flash", nil, nil, chunk2, ¶m) + if len(out) != 1 { + t.Fatalf("expected 1 chunk output, got %d", len(out)) + } + if got := gjson.GetBytes(out[0], "choices.0.delta.tool_calls.0.function.name").String(); got != "mcp/search" { + t.Fatalf("restored tool name = %q, want mcp/search", got) + } + + // 2. When the first chunk's request produces a nil SanitizedNameMap, later chunks must not rebuild it. + var nilMapParam any + reqNoSanitize := []byte(`{"model":"gemini-2.5-flash","tools":[{"name":"plain_tool"}]}`) + ConvertGeminiResponseToOpenAI(ctx, "gemini-2.5-flash", reqNoSanitize, nil, chunk1, &nilMapParam) + outNil := ConvertGeminiResponseToOpenAI(ctx, "gemini-2.5-flash", reqWithTool, nil, chunk2, &nilMapParam) + if len(outNil) != 1 { + t.Fatalf("expected 1 chunk output, got %d", len(outNil)) + } + if got := gjson.GetBytes(outNil[0], "choices.0.delta.tool_calls.0.function.name").String(); got != "mcp_search" { + t.Fatalf("tool name = %q, want mcp_search (must not rebuild nil SanitizedNameMap on later chunks)", got) + } +} + +func BenchmarkConvertGeminiResponseToOpenAI_TextDelta(b *testing.B) { + request := []byte(`{"model":"gemini-2.5-flash","messages":[{"role":"user","content":"` + strings.Repeat("x", 1024*1024) + `"}]}`) + delta := []byte(`{"candidates":[{"content":{"parts":[{"text":"token "}]}}],"modelVersion":"gemini-2.5-flash","responseId":"resp_bench"}`) + + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + var param any + for j := 0; j < 64; j++ { + ConvertGeminiResponseToOpenAI(context.Background(), "gemini-2.5-flash", request, nil, delta, ¶m) + } + } +} diff --git a/internal/translator/gemini/openai/responses/gemini_openai-responses_response.go b/internal/translator/gemini/openai/responses/gemini_openai-responses_response.go index 1b5aa503d3e..20f568dd67c 100644 --- a/internal/translator/gemini/openai/responses/gemini_openai-responses_response.go +++ b/internal/translator/gemini/openai/responses/gemini_openai-responses_response.go @@ -146,6 +146,9 @@ type geminiToResponsesState struct { FuncDone map[int]bool SanitizedNameMap map[string]string ToolIdentityMap map[string]util.ResponsesToolIdentity + RequestJSON []byte + RequestResolved bool + ToolMapsResolved bool FunctionEvidence map[string]*geminiFunctionCallEvidence // web search aggregation @@ -257,7 +260,7 @@ func isUpstreamGeminiRequest(rawJSON []byte) bool { return false } -func determineWebSearchStreamMode(modelName, requestModelName string, originalRequestRawJSON, requestRawJSON []byte) bool { +func determineWebSearchStreamMode(modelName, requestModelName string, originalRequestRawJSON, requestRawJSON, reqJSON []byte) bool { if len(originalRequestRawJSON) > 0 { origRoot := unwrapRequestRoot(gjson.ParseBytes(originalRequestRawJSON)) if !AllowsResponsesWebSearchToolChoice(origRoot) { @@ -273,7 +276,9 @@ func determineWebSearchStreamMode(modelName, requestModelName string, originalRe return hasEffectiveGoogleSearchTool(requestRawJSON) } } - reqJSON := pickRequestJSON(originalRequestRawJSON, requestRawJSON) + if len(reqJSON) == 0 { + reqJSON = pickRequestJSON(originalRequestRawJSON, requestRawJSON) + } if len(reqJSON) > 0 { reqRoot := unwrapRequestRoot(gjson.ParseBytes(reqJSON)) return HasResponsesWebSearchTool(reqRoot) && @@ -285,8 +290,8 @@ func determineWebSearchStreamMode(modelName, requestModelName string, originalRe // ConvertGeminiResponseToOpenAIResponses converts Gemini SSE chunks into OpenAI Responses SSE events. func ConvertGeminiResponseToOpenAIResponses(_ context.Context, modelName string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) [][]byte { - reqJSON := pickRequestJSON(originalRequestRawJSON, requestRawJSON) if *param == nil { + reqJSON := pickRequestJSON(originalRequestRawJSON, requestRawJSON) *param = &geminiToResponsesState{ FuncArgsBuf: make(map[int]*strings.Builder), FuncInputBuf: make(map[int]string), @@ -302,10 +307,18 @@ func ConvertGeminiResponseToOpenAIResponses(_ context.Context, modelName string, SeenReasoningSignatures: make(map[string]bool), SanitizedNameMap: util.SanitizedToolNameMap(originalRequestRawJSON), ToolIdentityMap: util.ResponsesToolReverseIdentityMap(reqJSON), + RequestJSON: reqJSON, + RequestResolved: true, + ToolMapsResolved: true, EmittedAnnotationCount: make(map[int]int), } } st := (*param).(*geminiToResponsesState) + if !st.RequestResolved { + st.RequestJSON = pickRequestJSON(originalRequestRawJSON, requestRawJSON) + st.RequestResolved = true + } + reqJSON := st.RequestJSON if st.FuncArgsBuf == nil { st.FuncArgsBuf = make(map[int]*strings.Builder) } @@ -342,11 +355,14 @@ func ConvertGeminiResponseToOpenAIResponses(_ context.Context, modelName string, if st.SeenReasoningSignatures == nil { st.SeenReasoningSignatures = make(map[string]bool) } - if st.SanitizedNameMap == nil { - st.SanitizedNameMap = util.SanitizedToolNameMap(originalRequestRawJSON) - } - if st.ToolIdentityMap == nil { - st.ToolIdentityMap = util.ResponsesToolReverseIdentityMap(reqJSON) + if !st.ToolMapsResolved { + if st.SanitizedNameMap == nil { + st.SanitizedNameMap = util.SanitizedToolNameMap(originalRequestRawJSON) + } + if st.ToolIdentityMap == nil { + st.ToolIdentityMap = util.ResponsesToolReverseIdentityMap(reqJSON) + } + st.ToolMapsResolved = true } if st.EmittedAnnotationCount == nil { st.EmittedAnnotationCount = make(map[int]int) @@ -785,6 +801,9 @@ func ConvertGeminiResponseToOpenAIResponses(_ context.Context, modelName string, created, _ = sjson.SetBytes(created, "response.id", st.ResponseID) created, _ = sjson.SetBytes(created, "response.created_at", st.CreatedAt) requestModelName := translatorcommon.RequestModelName(originalRequestRawJSON, requestRawJSON) + if requestModelName == "" && len(reqJSON) > 0 { + requestModelName = translatorcommon.RequestModelName(reqJSON, nil) + } if requestModelName == "" { requestModelName = modelName } @@ -804,7 +823,7 @@ func ConvertGeminiResponseToOpenAIResponses(_ context.Context, modelName string, st.Started = true st.NextIndex = 0 - st.WebSearchStreamMode = determineWebSearchStreamMode(modelName, requestModelName, originalRequestRawJSON, requestRawJSON) + st.WebSearchStreamMode = determineWebSearchStreamMode(modelName, requestModelName, originalRequestRawJSON, requestRawJSON, reqJSON) } // Handle groundingMetadata for web search @@ -1423,7 +1442,7 @@ func ConvertGeminiResponseToOpenAIResponses(_ context.Context, modelName string, completed, _ = sjson.SetBytes(completed, "response.id", st.ResponseID) completed, _ = sjson.SetBytes(completed, "response.created_at", st.CreatedAt) - if reqJSON := pickRequestJSON(originalRequestRawJSON, requestRawJSON); len(reqJSON) > 0 { + if len(reqJSON) > 0 { req := unwrapRequestRoot(gjson.ParseBytes(reqJSON)) if v := req.Get("instructions"); v.Exists() { completed, _ = sjson.SetBytes(completed, "response.instructions", v.String()) @@ -1607,7 +1626,7 @@ func ConvertGeminiResponseToOpenAIResponsesNonStream(_ context.Context, _ string resp, _ = sjson.SetBytes(resp, "created_at", createdAt) // Echo request fields when present; fallback model from response modelVersion - if reqJSON := pickRequestJSON(originalRequestRawJSON, requestRawJSON); len(reqJSON) > 0 { + if len(reqJSON) > 0 { req := unwrapRequestRoot(gjson.ParseBytes(reqJSON)) if v := req.Get("instructions"); v.Exists() { resp, _ = sjson.SetBytes(resp, "instructions", v.String()) diff --git a/internal/translator/gemini/openai/responses/gemini_openai-responses_response_test.go b/internal/translator/gemini/openai/responses/gemini_openai-responses_response_test.go index 8ccbbf53cef..fcf86ce94cd 100644 --- a/internal/translator/gemini/openai/responses/gemini_openai-responses_response_test.go +++ b/internal/translator/gemini/openai/responses/gemini_openai-responses_response_test.go @@ -1568,3 +1568,92 @@ func TestConvertGeminiResponseToOpenAIResponses_MessageOutputItemDoneFields(t *t t.Fatalf("missing message response.output_item.done event") } } + +func TestConvertGeminiResponseToOpenAIResponses_PinsRequestPerStream(t *testing.T) { + originalRequest := []byte(`{ + "model":"gemini-2.5-flash", + "instructions":"pinned-instructions", + "input":[{"type":"additional_tools","role":"developer","tools":[ + {"type":"namespace","name":"functions","tools":[{"type":"custom","name":"exec"}]} + ]}] + }`) + chunks := [][]byte{ + []byte(`data: {"candidates":[{"content":{"role":"model","parts":[{"text":"starting"}]}}],"modelVersion":"gemini-2.5-flash","responseId":"resp_pinned"}`), + []byte(`data: {"candidates":[{"content":{"role":"model","parts":[{"functionCall":{"name":"functions__exec","args":{"input":"pwd"}}}]},"finishReason":"STOP"}],"usageMetadata":{"promptTokenCount":10,"candidatesTokenCount":5,"totalTokenCount":15},"modelVersion":"gemini-2.5-flash","responseId":"resp_pinned"}`), + []byte("data: [DONE]"), + } + + var param any + var added, completed gjson.Result + for i, chunk := range chunks { + var requestForChunk []byte + if i == 0 { + requestForChunk = originalRequest + } + for _, output := range ConvertGeminiResponseToOpenAIResponses(context.Background(), "gemini-2.5-flash", requestForChunk, nil, chunk, ¶m) { + event, data := parseSSEEvent(t, output) + switch event { + case "response.output_item.added": + if data.Get("item.type").String() == "custom_tool_call" { + added = data + } + case "response.completed": + completed = data + } + } + } + + if !added.Exists() || !completed.Exists() { + t.Fatalf("missing lifecycle events: added=%v completed=%v", added.Exists(), completed.Exists()) + } + if got := added.Get("item.name").String(); got != "exec" { + t.Fatalf("added name = %q, want exec", got) + } + if got := added.Get("item.namespace").String(); got != "functions" { + t.Fatalf("added namespace = %q, want functions", got) + } + if got := completed.Get("response.instructions").String(); got != "pinned-instructions" { + t.Fatalf("completed instructions = %q, want pinned-instructions", got) + } + + // Verify nil tool maps resolved on the first chunk are not rebuilt on subsequent chunks. + var nilMapParam any + firstReqNoTools := []byte(`{"model":"gemini-2.5-flash","instructions":"pinned-no-tools"}`) + laterReqWithTool := []byte(`{"model":"gemini-2.5-flash","instructions":"later-instructions","tools":[{"type":"function","name":"mcp/search","parameters":{"type":"object"}}]}`) + ConvertGeminiResponseToOpenAIResponses(context.Background(), "gemini-2.5-flash", firstReqNoTools, nil, chunks[0], &nilMapParam) + var funcAdded, noToolsCompleted gjson.Result + laterChunk := []byte(`data: {"candidates":[{"content":{"role":"model","parts":[{"functionCall":{"name":"mcp_search","args":{}}}]},"finishReason":"STOP"}],"usageMetadata":{"promptTokenCount":1,"candidatesTokenCount":1,"totalTokenCount":2},"modelVersion":"gemini-2.5-flash","responseId":"resp_pinned_nil"}`) + for _, output := range ConvertGeminiResponseToOpenAIResponses(context.Background(), "gemini-2.5-flash", laterReqWithTool, nil, laterChunk, &nilMapParam) { + event, data := parseSSEEvent(t, output) + switch event { + case "response.output_item.added": + if data.Get("item.type").String() == "function_call" { + funcAdded = data + } + case "response.completed": + noToolsCompleted = data + } + } + if got := funcAdded.Get("item.name").String(); got != "mcp_search" { + t.Fatalf("nil-map stream tool name = %q, want mcp_search (must not rebuild tool maps on later chunk)", got) + } + if got := noToolsCompleted.Get("response.instructions").String(); got != "pinned-no-tools" { + t.Fatalf("nil-map stream completed instructions = %q, want pinned-no-tools", got) + } +} + +func BenchmarkConvertGeminiResponseToOpenAIResponses_TextDelta(b *testing.B) { + request := []byte(`{"model":"gemini-2.5-flash","instructions":"` + strings.Repeat("x", 1024*1024) + `"}`) + delta := []byte(`data: {"candidates":[{"content":{"role":"model","parts":[{"text":"token "}]} }],"modelVersion":"gemini-2.5-flash","responseId":"resp_bench"}`) + done := []byte(`data: [DONE]`) + + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + var param any + for j := 0; j < 64; j++ { + ConvertGeminiResponseToOpenAIResponses(context.Background(), "gemini-2.5-flash", request, nil, delta, ¶m) + } + ConvertGeminiResponseToOpenAIResponses(context.Background(), "gemini-2.5-flash", request, nil, done, ¶m) + } +} From feb6398456ccaed50d20dfc6353ad050ef714d31 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Mon, 19 Oct 2026 01:41:19 +0800 Subject: [PATCH 15/27] feat(auth): increase Codex proactive refresh lead to 96 hours and improve rejected token handling - Update Codex outer refresh lead time to 96 hours to prevent upstream token invalidation. - Ensure proper token refreshing and background recovery for rejected unexpired JWTs and in-stream 401 errors. Closes: #6742 --- .../runtime/executor/codex_executor_auth.go | 2 +- sdk/auth/codex.go | 2 +- sdk/auth/refresh_registry_test.go | 2 +- sdk/cliproxy/auth/conductor_cooldown.go | 20 +- sdk/cliproxy/auth/conductor_issue6742_test.go | 408 ++++++++++++++++++ sdk/cliproxy/auth/conductor_refresh.go | 18 +- sdk/cliproxy/auth/scheduler.go | 6 + sdk/cliproxy/auth/types.go | 22 +- 8 files changed, 461 insertions(+), 19 deletions(-) create mode 100644 sdk/cliproxy/auth/conductor_issue6742_test.go diff --git a/internal/runtime/executor/codex_executor_auth.go b/internal/runtime/executor/codex_executor_auth.go index 930ab968fc3..687c57217bc 100644 --- a/internal/runtime/executor/codex_executor_auth.go +++ b/internal/runtime/executor/codex_executor_auth.go @@ -253,7 +253,7 @@ func (e *CodexExecutor) refreshLocked(ctx context.Context, auth *cliproxyauth.Au needOuter = false } else if homeEnabled && !hasOuter { hasOuter = true - if outerExp, okExp := auth.OuterExpirationTime(); okExp && !outerExp.IsZero() && !outerExp.After(now.Add(24*time.Hour)) { + if outerExp, okExp := auth.OuterExpirationTime(); okExp && !outerExp.IsZero() && !outerExp.After(now.Add(cliproxyauth.CodexOuterRefreshLead)) { needOuter = true } } diff --git a/sdk/auth/codex.go b/sdk/auth/codex.go index 53b9cab1f21..2d56fa07919 100644 --- a/sdk/auth/codex.go +++ b/sdk/auth/codex.go @@ -32,7 +32,7 @@ func (a *CodexAuthenticator) Provider() string { } func (a *CodexAuthenticator) RefreshLead() *time.Duration { - return new(24 * time.Hour) + return new(coreauth.CodexOuterRefreshLead) } func (a *CodexAuthenticator) Login(ctx context.Context, cfg *config.Config, opts *LoginOptions) (*coreauth.Auth, error) { diff --git a/sdk/auth/refresh_registry_test.go b/sdk/auth/refresh_registry_test.go index 428ce014eab..402ef69aec5 100644 --- a/sdk/auth/refresh_registry_test.go +++ b/sdk/auth/refresh_registry_test.go @@ -12,7 +12,7 @@ func TestProviderRefreshLeads(t *testing.T) { want time.Duration wantNil bool }{ - {name: "codex", authenticator: NewCodexAuthenticator(), want: 24 * time.Hour}, + {name: "codex", authenticator: NewCodexAuthenticator(), want: 96 * time.Hour}, {name: "claude", authenticator: NewClaudeAuthenticator(), want: 4 * time.Hour}, {name: "antigravity", authenticator: NewAntigravityAuthenticator(), want: 30 * time.Minute}, {name: "kimi", authenticator: NewKimiAuthenticator(), want: 5 * time.Minute}, diff --git a/sdk/cliproxy/auth/conductor_cooldown.go b/sdk/cliproxy/auth/conductor_cooldown.go index 7545cfab346..13b10e2ff5b 100644 --- a/sdk/cliproxy/auth/conductor_cooldown.go +++ b/sdk/cliproxy/auth/conductor_cooldown.go @@ -887,6 +887,7 @@ func (m *Manager) MarkResult(ctx context.Context, result Result) { var authSnapshot *Auth cooldownStateChanged := false + scheduleUnauthorizedRefresh := false now := time.Now() releaseMutation := m.lockAuthMutation(result.AuthID) @@ -1294,6 +1295,18 @@ func (m *Manager) MarkResult(ctx context.Context, result Result) { auth.Status = StatusError auth.NextRefreshAfter = time.Time{} auth.NextRetryAfter = time.Time{} + } else if !result.Success && !shouldSkipCredentialCooldown(result.Error) && (statusCodeFromResult(result.Error) == http.StatusUnauthorized || isUnauthorizedError(result.Error)) && authHasRefreshCredential(auth) { + if currentToken := authAccessToken(auth); currentToken != "" { + auth.RejectedAccessToken = currentToken + } + if auth.NextRefreshAfter.IsZero() { + if !auth.LastRefreshedAt.IsZero() && now.Sub(auth.LastRefreshedAt) < refreshFailureBackoff { + auth.NextRefreshAfter = now.Add(refreshFailureBackoff) + } else { + auth.NextRefreshAfter = now + } + } + scheduleUnauthorizedRefresh = true } auth.Generation++ @@ -1316,14 +1329,15 @@ func (m *Manager) MarkResult(ctx context.Context, result Result) { m.mu.Unlock() releaseMutation() if m.scheduler != nil && authSnapshot != nil { + credentialScopedResult := result.CredentialScope || scheduleUnauthorizedRefresh var targetModels []string - if !result.CredentialScope && modelKey != "" { + if !credentialScopedResult && modelKey != "" { targetModels = append(targetModels, modelKey) if routeKey := canonicalModelKey(result.RouteModel); routeKey != "" && routeKey != modelKey { targetModels = append(targetModels, routeKey) } } - m.scheduler.upsertAuthResult(authSnapshot, targetModels, result.CredentialScope) + m.scheduler.upsertAuthResult(authSnapshot, targetModels, credentialScopedResult) } if authSnapshot != nil && cooldownStateChanged { m.persistCooldownStates(context.Background()) @@ -1333,6 +1347,8 @@ func (m *Manager) MarkResult(ctx context.Context, result Result) { if authSnapshot != nil { if _, due := devinQuotaRevalidateAt(authSnapshot, now); due { m.queueRefreshReschedule(result.AuthID) + } else if scheduleUnauthorizedRefresh { + m.queueRefreshReschedule(result.AuthID) } } diff --git a/sdk/cliproxy/auth/conductor_issue6742_test.go b/sdk/cliproxy/auth/conductor_issue6742_test.go new file mode 100644 index 00000000000..e418dee15b0 --- /dev/null +++ b/sdk/cliproxy/auth/conductor_issue6742_test.go @@ -0,0 +1,408 @@ +package auth + +import ( + "context" + "encoding/base64" + "encoding/json" + "errors" + "fmt" + "net/http" + "testing" + "time" + + "github.com/router-for-me/CLIProxyAPI/v8/internal/registry" + cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor" +) + +func makeIssue6742JWT(exp time.Time) string { + header := base64.RawURLEncoding.EncodeToString([]byte(`{"alg":"none","typ":"JWT"}`)) + payloadBytes, _ := json.Marshal(map[string]any{ + "exp": exp.Unix(), + }) + payload := base64.RawURLEncoding.EncodeToString(payloadBytes) + return header + "." + payload + ".sig" +} + +func TestIssue6742_CodexProactiveRefreshBeforeSevenDayInvalidation(t *testing.T) { + issuedAt := time.Date(2026, 9, 26, 14, 39, 9, 0, time.FixedZone("UTC+8", 8*3600)) + jwtExpiry := issuedAt.Add(10 * 24 * time.Hour) // Oct 6 14:39:09 + jwtToken := makeIssue6742JWT(jwtExpiry) + + auth := &Auth{ + ID: "codex-issue6742-a.json", + Provider: "codex", + Status: StatusActive, + LastRefreshedAt: issuedAt, + Metadata: map[string]any{ + "type": "codex", + "access_token": jwtToken, + "refresh_token": "codex-refresh-a", + "last_refresh": issuedAt.Format(time.RFC3339), + "expired": jwtExpiry.Format(time.RFC3339), + }, + } + + mgr := NewManager(nil, nil, nil) + + // At 6 days + 1 hour after issuance (still ~3.9 days before 10-day JWT expiry, + // and before the ~7-day upstream invalidation on Oct 3 16:33:55), proactive refresh must be due. + checkAt := issuedAt.Add(6*24*time.Hour + time.Hour) + dueAt, hasOuter, needOuter := CodexOuterNeedsRefresh(auth, checkAt) + if !hasOuter { + t.Fatal("CodexOuterNeedsRefresh hasRefresh = false, want true") + } + if !needOuter { + t.Fatalf("CodexOuterNeedsRefresh needsRefresh = false at %v (dueAt=%v), want true before 7-day upstream invalidation", checkAt, dueAt) + } + if !mgr.shouldRefresh(auth, checkAt) { + t.Fatalf("mgr.shouldRefresh = false at %v, want true before 7-day upstream invalidation", checkAt) + } + nextAt, okSchedule := nextRefreshCheckAt(issuedAt, auth, 5*time.Second) + if !okSchedule { + t.Fatal("nextRefreshCheckAt ok = false, want true") + } + sevenDayInvalidation := time.Date(2026, 10, 3, 16, 33, 55, 0, time.FixedZone("UTC+8", 8*3600)) + if !nextAt.Before(sevenDayInvalidation) { + t.Fatalf("nextRefreshCheckAt = %v, want before 7-day invalidation at %v", nextAt, sevenDayInvalidation) + } +} + +func TestIssue6742_RejectedUnexpiredJWTRecoversViaBackgroundRefreshAfterTransientFailure(t *testing.T) { + now := time.Now() + // Use 8 days remaining (> 96h proactive refresh lead) so shouldRefresh is false + // before rejection and can only become true via RejectedAccessToken tracking. + staleJWTToken := makeIssue6742JWT(now.Add(8 * 24 * time.Hour)) + freshJWTToken := makeIssue6742JWT(now.Add(10 * 24 * time.Hour)) + + model := "gpt-6-astra" + primary := &Auth{ + ID: "aa-codex-jwt-primary", + Provider: "codex", + Status: StatusActive, + Metadata: map[string]any{ + "type": "codex", + "access_token": staleJWTToken, + "refresh_token": "primary-refresh-token", + "expired": now.Add(8 * 24 * time.Hour).Format(time.RFC3339), + }, + } + + executor := &unauthorizedRefreshExecutor{ + id: "codex", + tokenInvalid: map[string]struct{}{ + staleJWTToken: {}, + }, + refreshErr: errors.New("token refresh request failed: upstream 503 Service Unavailable"), + refreshTokens: map[string]string{ + primary.ID: freshJWTToken, + }, + } + + m := NewManager(nil, &FillFirstSelector{}, nil) + m.RegisterExecutor(executor) + + reg := registry.GetGlobalRegistry() + reg.RegisterClient(primary.ID, "codex", []*registry.ModelInfo{{ID: model}}) + t.Cleanup(func() { + reg.UnregisterClient(primary.ID) + }) + + if _, errRegister := m.Register(context.Background(), primary); errRegister != nil { + t.Fatalf("register primary: %v", errRegister) + } + if m.shouldRefresh(primary, now) { + t.Fatal("precondition failed: shouldRefresh = true before 401 with 8 days remaining, want false") + } + + // 1. Execute hits 401 with invalidated JWT token; on-demand refresh fails transiently. + _, errExec := m.Execute(context.Background(), []string{"codex"}, cliproxyexecutor.Request{Model: model}, cliproxyexecutor.Options{}) + if errExec == nil { + t.Fatal("expected initial Execute to fail when on-demand refresh fails transiently") + } + + afterFail, ok := m.GetByID(primary.ID) + if !ok || afterFail == nil { + t.Fatal("primary auth missing after failure") + } + if afterFail.RejectedAccessToken != staleJWTToken { + t.Fatalf("RejectedAccessToken = %q, want %q for rejected unexpired JWT", afterFail.RejectedAccessToken, staleJWTToken) + } + if afterFail.HasValidAccessToken(now) { + t.Fatal("HasValidAccessToken = true, want false for rejected unexpired JWT") + } + if afterFail.NextRefreshAfter.IsZero() { + t.Fatal("NextRefreshAfter is zero, want scheduled retry backoff") + } + + // 2. When the retry backoff elapses, shouldRefresh must return true even though + // the rejected JWT's nominal exp claim is still 3 days in the future. + retryTime := afterFail.NextRefreshAfter.Add(time.Second) + if !m.shouldRefresh(afterFail, retryTime) { + t.Fatal("shouldRefresh = false at retryTime, want true for rejected unexpired JWT") + } + + // 3. Transient network issue resolves; background auto-refresh loop processes the due auth. + executor.mu.Lock() + executor.refreshErr = nil + executor.mu.Unlock() + + loop := newAuthAutoRefreshLoop(m, 5*time.Second, 1) + loop.handleDueAuth(context.Background(), retryTime, primary.ID) + select { + case job := <-loop.jobs: + if !m.beginRefreshJob(context.Background(), job) { + t.Fatal("beginRefreshJob returned false") + } + if _, errRefresh := m.refreshAuthForRequestAtEpoch(context.Background(), job.id, "", job.registrationEpoch); errRefresh != nil { + t.Fatalf("background refreshAuthForRequestAtEpoch error = %v", errRefresh) + } + m.finishRefreshJob(job, time.Time{}, false) + default: + t.Fatal("expected handleDueAuth to enqueue a refresh job for rejected unexpired JWT") + } + + recovered, ok := m.GetByID(primary.ID) + if !ok || recovered == nil { + t.Fatal("primary auth missing after background refresh") + } + if got := authAccessToken(recovered); got != freshJWTToken { + t.Fatalf("recovered access_token = %q, want %q", got, freshJWTToken) + } + if recovered.Unavailable || recovered.Status != StatusActive { + t.Fatalf("recovered state: Unavailable=%v Status=%s, want false/StatusActive", recovered.Unavailable, recovered.Status) + } + if state := recovered.ModelStates[model]; state != nil && state.Unavailable { + t.Fatalf("model state remained Unavailable after background refresh: %+v", state) + } + + // 4. Subsequent Execute succeeds automatically without manual ForceRefreshAll. + resp, errExec2 := m.Execute(context.Background(), []string{"codex"}, cliproxyexecutor.Request{Model: model}, cliproxyexecutor.Options{}) + if errExec2 != nil { + t.Fatalf("subsequent Execute error = %v, want success", errExec2) + } + if got := string(resp.Payload); got != fmt.Sprintf("%s:%s", primary.ID, freshJWTToken) { + t.Fatalf("subsequent Execute payload = %q, want %q", got, fmt.Sprintf("%s:%s", primary.ID, freshJWTToken)) + } +} + +type inStreamUnauthorizedExecutor struct { + unauthorizedRefreshExecutor +} + +func (e *inStreamUnauthorizedExecutor) ExecuteStream(_ context.Context, auth *Auth, _ cliproxyexecutor.Request, _ cliproxyexecutor.Options) (*cliproxyexecutor.StreamResult, error) { + e.mu.Lock() + e.streamCalls = append(e.streamCalls, auth.ID) + token := authAccessToken(auth) + _, invalid := e.tokenInvalid[token] + e.mu.Unlock() + + ch := make(chan cliproxyexecutor.StreamChunk, 2) + // Emit an initial handshake chunk so readStreamBootstrap commits the stream, + // and then deliver the 401 error in-stream via wrapStreamResult -> MarkResult. + ch <- cliproxyexecutor.StreamChunk{Payload: []byte(`data: {"type":"response.created"}` + "\n\n")} + if invalid { + ch <- cliproxyexecutor.StreamChunk{Err: &Error{ + HTTPStatus: http.StatusUnauthorized, + Code: "auth_unavailable", + Message: `{"error":{"message":"Encountered invalidated oauth token for user, failing request","type":"authentication_error","code":"auth_unavailable"}}`, + }} + } else { + ch <- cliproxyexecutor.StreamChunk{Payload: []byte(auth.ID + ":" + token)} + } + close(ch) + return &cliproxyexecutor.StreamResult{Headers: http.Header{"X-Auth": {auth.ID}}, Chunks: ch}, nil +} + +func TestIssue6742_InStreamUnauthorizedMarksRejectedTokenAndSchedulesRefresh(t *testing.T) { + now := time.Now() + // Use 8 days remaining (> 96h proactive refresh lead) so shouldRefresh is false + // before the in-stream 401 and only becomes true because MarkResult marks RejectedAccessToken. + staleJWTToken := makeIssue6742JWT(now.Add(8 * 24 * time.Hour)) + freshJWTToken := makeIssue6742JWT(now.Add(10 * 24 * time.Hour)) + model := "gpt-5.6-luna" + + primary := &Auth{ + ID: "codex-instream-401", + Provider: "codex", + Status: StatusActive, + Metadata: map[string]any{ + "type": "codex", + "access_token": staleJWTToken, + "refresh_token": "primary-refresh-token", + "expired": now.Add(8 * 24 * time.Hour).Format(time.RFC3339), + }, + } + + executor := &inStreamUnauthorizedExecutor{ + unauthorizedRefreshExecutor: unauthorizedRefreshExecutor{ + id: "codex", + tokenInvalid: map[string]struct{}{ + staleJWTToken: {}, + }, + refreshTokens: map[string]string{ + primary.ID: freshJWTToken, + }, + }, + } + + m := NewManager(nil, &FillFirstSelector{}, nil) + m.RegisterExecutor(executor) + + reg := registry.GetGlobalRegistry() + reg.RegisterClient(primary.ID, "codex", []*registry.ModelInfo{{ID: model}}) + t.Cleanup(func() { + reg.UnregisterClient(primary.ID) + }) + + if _, errRegister := m.Register(context.Background(), primary); errRegister != nil { + t.Fatalf("register primary: %v", errRegister) + } + if m.shouldRefresh(primary, now) { + t.Fatal("precondition failed: shouldRefresh = true before in-stream 401 with 8 days remaining, want false") + } + + stream, errStream := m.ExecuteStream(context.Background(), []string{"codex"}, cliproxyexecutor.Request{Model: model}, cliproxyexecutor.Options{}) + if errStream != nil { + t.Fatalf("ExecuteStream initial error = %v", errStream) + } + for range stream.Chunks { + } + + afterStream, ok := m.GetByID(primary.ID) + if !ok || afterStream == nil { + t.Fatal("primary auth missing after in-stream 401") + } + if afterStream.RejectedAccessToken != staleJWTToken { + t.Fatalf("RejectedAccessToken = %q, want %q after in-stream 401", afterStream.RejectedAccessToken, staleJWTToken) + } + if !m.shouldRefresh(afterStream, time.Now()) { + t.Fatal("shouldRefresh = false after in-stream 401, want true") + } + + // Background refresh loop must pick it up and recover the credential. + loop := newAuthAutoRefreshLoop(m, 5*time.Second, 1) + loop.handleDueAuth(context.Background(), time.Now(), primary.ID) + select { + case job := <-loop.jobs: + if !m.beginRefreshJob(context.Background(), job) { + t.Fatal("beginRefreshJob returned false") + } + if _, errRefresh := m.refreshAuthForRequestAtEpoch(context.Background(), job.id, "", job.registrationEpoch); errRefresh != nil { + t.Fatalf("background refresh error = %v", errRefresh) + } + m.finishRefreshJob(job, time.Time{}, false) + default: + t.Fatal("expected handleDueAuth to enqueue refresh job after in-stream 401") + } + + recovered, ok := m.GetByID(primary.ID) + if !ok || recovered == nil { + t.Fatal("primary auth missing after recovery") + } + if got := authAccessToken(recovered); got != freshJWTToken { + t.Fatalf("recovered access_token = %q, want %q", got, freshJWTToken) + } + if recovered.Unavailable || recovered.Status != StatusActive { + t.Fatalf("recovered Unavailable=%v Status=%s, want false/StatusActive", recovered.Unavailable, recovered.Status) + } + if state := recovered.ModelStates[model]; state != nil && state.Unavailable { + t.Fatalf("model state still Unavailable after recovery: %+v", state) + } +} + +func TestIssue6742_InStreamUnauthorizedBlocksSiblingModelShardUntilRefreshed(t *testing.T) { + now := time.Now() + staleJWTToken := makeIssue6742JWT(now.Add(3 * 24 * time.Hour)) + freshJWTToken := makeIssue6742JWT(now.Add(10 * 24 * time.Hour)) + backupJWTToken := makeIssue6742JWT(now.Add(10 * 24 * time.Hour)) + modelA := "gpt-5.6-luna" + modelB := "gpt-6-astra" + + primary := &Auth{ + ID: "aa-codex-multimodel-primary", + Provider: "codex", + Status: StatusActive, + Metadata: map[string]any{ + "type": "codex", + "access_token": staleJWTToken, + "refresh_token": "primary-refresh-token", + "expired": now.Add(3 * 24 * time.Hour).Format(time.RFC3339), + }, + } + backup := &Auth{ + ID: "bb-codex-multimodel-backup", + Provider: "codex", + Status: StatusActive, + Metadata: map[string]any{ + "type": "codex", + "access_token": backupJWTToken, + "refresh_token": "backup-refresh-token", + "expired": now.Add(10 * 24 * time.Hour).Format(time.RFC3339), + }, + } + + executor := &inStreamUnauthorizedExecutor{ + unauthorizedRefreshExecutor: unauthorizedRefreshExecutor{ + id: "codex", + tokenInvalid: map[string]struct{}{}, + refreshTokens: map[string]string{ + primary.ID: freshJWTToken, + }, + }, + } + + m := NewManager(nil, &FillFirstSelector{}, nil) + m.RegisterExecutor(executor) + + reg := registry.GetGlobalRegistry() + reg.RegisterClient(primary.ID, "codex", []*registry.ModelInfo{{ID: modelA}, {ID: modelB}}) + reg.RegisterClient(backup.ID, "codex", []*registry.ModelInfo{{ID: modelA}, {ID: modelB}}) + t.Cleanup(func() { + reg.UnregisterClient(primary.ID) + reg.UnregisterClient(backup.ID) + }) + + if _, errRegister := m.Register(context.Background(), primary); errRegister != nil { + t.Fatalf("register primary: %v", errRegister) + } + if _, errRegister := m.Register(context.Background(), backup); errRegister != nil { + t.Fatalf("register backup: %v", errRegister) + } + + // Pre-warm the scheduler shard for sibling modelB while primary is healthy. + respWarm, errWarm := m.Execute(context.Background(), []string{"codex"}, cliproxyexecutor.Request{Model: modelB}, cliproxyexecutor.Options{}) + if errWarm != nil { + t.Fatalf("warm-up Execute(modelB) error = %v", errWarm) + } + if got := string(respWarm.Payload); got != primary.ID+":"+staleJWTToken { + t.Fatalf("warm-up Execute(modelB) payload = %q, want primary", got) + } + + // Now upstream invalidates staleJWTToken, and modelA hits an in-stream 401. + executor.mu.Lock() + executor.tokenInvalid[staleJWTToken] = struct{}{} + executor.mu.Unlock() + + stream, errStream := m.ExecuteStream(context.Background(), []string{"codex"}, cliproxyexecutor.Request{Model: modelA}, cliproxyexecutor.Options{}) + if errStream != nil { + t.Fatalf("ExecuteStream(modelA) initial error = %v", errStream) + } + for range stream.Chunks { + } + + // Before background refresh runs, a request for sibling modelB must NOT select primary + // from the pre-warmed modelB scheduler shard with the rejected token; it must route to backup. + execCallsBefore := len(executor.ExecuteCalls()) + respB, errB := m.Execute(context.Background(), []string{"codex"}, cliproxyexecutor.Request{Model: modelB}, cliproxyexecutor.Options{}) + if errB != nil { + t.Fatalf("Execute(modelB) before refresh error = %v, want success via backup", errB) + } + if got := string(respB.Payload); got != backup.ID+":"+backupJWTToken { + t.Fatalf("Execute(modelB) before refresh payload = %q, want backup %q", got, backup.ID+":"+backupJWTToken) + } + for _, id := range executor.ExecuteCalls()[execCallsBefore:] { + if id == primary.ID { + t.Fatalf("sibling modelB selected primary with rejected token before refresh completed") + } + } +} diff --git a/sdk/cliproxy/auth/conductor_refresh.go b/sdk/cliproxy/auth/conductor_refresh.go index 1a1275079d6..caed3ebc100 100644 --- a/sdk/cliproxy/auth/conductor_refresh.go +++ b/sdk/cliproxy/auth/conductor_refresh.go @@ -177,7 +177,7 @@ func (m *Manager) shouldRefresh(a *Auth, now time.Time) bool { return false } if hasExpiry && !expiry.IsZero() { - return time.Until(expiry) <= *lead + return expiry.Sub(now) <= *lead } if !lastRefresh.IsZero() { return now.Sub(lastRefresh) >= *lead @@ -794,15 +794,13 @@ func (m *Manager) markRejectedAccessToken(id, failedAccessToken string) { releaseMutation := m.lockAuthMutation(id) defer releaseMutation() m.mu.Lock() - if current := m.auths[id]; current != nil && authAccessToken(current) == failedAccessToken { - if _, hasExp := current.AccessTokenExpirationTime(); !hasExp { - current.Generation++ - current.UpdatedAt = time.Now() - current.RejectedAccessToken = failedAccessToken - m.auths[id] = current - if m.scheduler != nil { - m.scheduler.upsertAuth(current.Clone()) - } + if current := m.auths[id]; current != nil && authAccessToken(current) == failedAccessToken && current.RejectedAccessToken != failedAccessToken { + current.Generation++ + current.UpdatedAt = time.Now() + current.RejectedAccessToken = failedAccessToken + m.auths[id] = current + if m.scheduler != nil { + m.scheduler.upsertAuth(current.Clone()) } } m.mu.Unlock() diff --git a/sdk/cliproxy/auth/scheduler.go b/sdk/cliproxy/auth/scheduler.go index 39c138e7436..752a52d7d57 100644 --- a/sdk/cliproxy/auth/scheduler.go +++ b/sdk/cliproxy/auth/scheduler.go @@ -891,6 +891,12 @@ func isCredentialBlocked(auth *Auth, supportedModelCount int, now time.Time) boo if auth.Disabled || auth.Status == StatusDisabled { return true } + if hasUnauthorizedAuthFailure(auth) { + return true + } + if exp, ok := auth.AccessTokenExpirationTime(); ok && !exp.IsZero() && !exp.After(now) { + return true + } if auth.Quota.Exceeded && auth.Quota.Reason == "credential_quota" && auth.Quota.NextRecoverAt.After(now) { return true } diff --git a/sdk/cliproxy/auth/types.go b/sdk/cliproxy/auth/types.go index cb815e48483..b09ec308e1a 100644 --- a/sdk/cliproxy/auth/types.go +++ b/sdk/cliproxy/auth/types.go @@ -841,6 +841,11 @@ func (a *Auth) ExpirationTime() (time.Time, bool) { return a.OuterExpirationTime() } +// CodexOuterRefreshLead is the conservative proactive refresh lead time before +// the nominal 10-day Codex OAuth JWT expiration, refreshing tokens well ahead +// of early upstream access-token invalidation. +const CodexOuterRefreshLead = 96 * time.Hour + // CodexSIWCRefreshLead is the minimum remaining validity required before refreshing // a Sign in with ChatGPT (SIWC) token, matching pi's DEFAULT_OAUTH_MINIMUM_VALIDITY_MS (5 minutes). const CodexSIWCRefreshLead = 5 * time.Minute @@ -875,6 +880,11 @@ func CodexOuterNeedsRefresh(a *Auth, now time.Time) (dueAt time.Time, hasRefresh } hasRefresh = true + outerToken := authOuterAccessToken(a) + if outerToken == "" || (a.RejectedAccessToken != "" && a.RejectedAccessToken == outerToken) { + return now, true, true + } + var lastRefresh time.Time if ts, ok := authLastRefreshTimestamp(a); ok && !ts.IsZero() { lastRefresh = ts @@ -910,7 +920,7 @@ func CodexOuterNeedsRefresh(a *Auth, now time.Time) (dueAt time.Time, hasRefresh } lead := ProviderRefreshLead(provider, a.Runtime) if lead == nil && provider == "codex" { - d := 24 * time.Hour + d := CodexOuterRefreshLead lead = &d } if lead == nil { @@ -1165,11 +1175,15 @@ func ProviderRefreshLead(provider string, runtime any) *time.Duration { refreshLeadMu.RLock() factory := refreshLeadFactories[provider] refreshLeadMu.RUnlock() - if factory == nil { + if factory != nil { + if lead := factory(); lead != nil && *lead > 0 { + return lead + } return nil } - if lead := factory(); lead != nil && *lead > 0 { - return lead + if provider == "codex" { + d := CodexOuterRefreshLead + return &d } return nil } From 1ad175b6376ad00004378ab4298a92bcf4db3afc Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Mon, 19 Oct 2026 02:00:22 +0800 Subject: [PATCH 16/27] feat(auth): snapshot plugin scheduler and release host lock during executor registration - Snapshot plugin scheduler and preferences atomically to prevent deadlocks and data races during auth picking. - Release host lock before notifying executor manager during adapter registration to prevent lock contention. Closes: #6744 --- internal/pluginhost/adapters_executors.go | 2 +- internal/pluginhost/adapters_test.go | 52 ++++++++ sdk/cliproxy/auth/conductor_selection.go | 35 +++--- sdk/cliproxy/auth/scheduler_test.go | 146 +++++++++++++++++++--- 4 files changed, 201 insertions(+), 34 deletions(-) diff --git a/internal/pluginhost/adapters_executors.go b/internal/pluginhost/adapters_executors.go index 3f43edf0c6b..552b2600f8c 100644 --- a/internal/pluginhost/adapters_executors.go +++ b/internal/pluginhost/adapters_executors.go @@ -178,6 +178,7 @@ func (h *Host) commitExecutorState(snap *Snapshot, manager executorManager, mode } } h.executorModelClientIDs = nextModelClients + h.mu.Unlock() for _, registration := range registrations { if registration.adapter == nil || registration.provider == "" { @@ -192,7 +193,6 @@ func (h *Host) commitExecutorState(snap *Snapshot, manager executorManager, mode } manager.UnregisterExecutor(provider) } - h.mu.Unlock() if modelRegistry == nil { return diff --git a/internal/pluginhost/adapters_test.go b/internal/pluginhost/adapters_test.go index 43b95b2a74e..f4dd210d5a0 100644 --- a/internal/pluginhost/adapters_test.go +++ b/internal/pluginhost/adapters_test.go @@ -968,6 +968,46 @@ func TestRegisterExecutorsDoesNotUnregisterStaleProviderOwnedExternally(t *testi } } +func TestRegisterExecutorsCommitsManagerChangesOutsideHostLock(t *testing.T) { + manager := newFakeExecutorManager() + exec := &fakeExecutor{identifier: "fallback-provider"} + host := newHostWithRecords(capabilityRecord{ + id: "alpha", + plugin: pluginapi.Plugin{Capabilities: pluginapi.Capabilities{ + Executor: exec, + }}, + }) + host.modelProviders["alpha"] = "provider-a" + + assertUnlocked := func(op string) { + t.Helper() + if !host.mu.TryLock() { + t.Fatalf("%s called while host.mu is held", op) + } + host.mu.Unlock() + } + manager.onExecutor = func(string) { + assertUnlocked("manager.Executor") + } + manager.onRegister = func(coreauth.ProviderExecutor) { + assertUnlocked("manager.RegisterExecutor") + } + manager.onUnregister = func(string) { + assertUnlocked("manager.UnregisterExecutor") + } + + host.RegisterExecutors(manager, nil) + host.modelProviders["alpha"] = "provider-b" + host.RegisterExecutors(manager, nil) + + if manager.registerCalls != 2 { + t.Fatalf("RegisterExecutor calls = %d, want 2", manager.registerCalls) + } + if len(manager.unregisters) != 1 || manager.unregisters[0] != "provider-a" { + t.Fatalf("unregistered providers = %#v, want [provider-a]", manager.unregisters) + } +} + func TestNormalizeRequestChainsByPriority(t *testing.T) { host := newHostWithRecords( capabilityRecord{ @@ -3797,6 +3837,9 @@ type fakeExecutorManager struct { executors map[string]coreauth.ProviderExecutor registerCalls int unregisters []string + onExecutor func(string) + onRegister func(coreauth.ProviderExecutor) + onUnregister func(string) } func newFakeExecutorManager() *fakeExecutorManager { @@ -3806,16 +3849,25 @@ func newFakeExecutorManager() *fakeExecutorManager { } func (m *fakeExecutorManager) Executor(provider string) (coreauth.ProviderExecutor, bool) { + if m.onExecutor != nil { + m.onExecutor(provider) + } executor, okExecutor := m.executors[provider] return executor, okExecutor } func (m *fakeExecutorManager) RegisterExecutor(executor coreauth.ProviderExecutor) { + if m.onRegister != nil { + m.onRegister(executor) + } m.registerCalls++ m.executors[executor.Identifier()] = executor } func (m *fakeExecutorManager) UnregisterExecutor(provider string) { + if m.onUnregister != nil { + m.onUnregister(provider) + } delete(m.executors, provider) m.unregisters = append(m.unregisters, provider) } diff --git a/sdk/cliproxy/auth/conductor_selection.go b/sdk/cliproxy/auth/conductor_selection.go index e2eac43a75c..7cdacb7185f 100644 --- a/sdk/cliproxy/auth/conductor_selection.go +++ b/sdk/cliproxy/auth/conductor_selection.go @@ -43,25 +43,31 @@ func (m *Manager) hasPluginScheduler() bool { return true } -func (m *Manager) pluginSchedulerWantsAcrossPrioritiesLocked() bool { - if m == nil || m.pluginScheduler == nil { +func pluginSchedulerWantsAcrossPriorities(scheduler PluginScheduler) bool { + if scheduler == nil { return false } - if opt, ok := m.pluginScheduler.(PluginSchedulerAcrossPriorities); ok && opt != nil { + if opt, ok := scheduler.(PluginSchedulerAcrossPriorities); ok && opt != nil { return opt.SchedulerWantsAcrossPriorities() } return false } -// PluginSchedulerWantsAcrossPriorities reports whether the configured plugin scheduler -// opted into receiving candidates across all priority tiers. -func (m *Manager) PluginSchedulerWantsAcrossPriorities() bool { +func (m *Manager) snapshotPluginScheduler() (PluginScheduler, bool) { if m == nil { - return false + return nil, false } m.mu.RLock() - defer m.mu.RUnlock() - return m.pluginSchedulerWantsAcrossPrioritiesLocked() + scheduler := m.pluginScheduler + m.mu.RUnlock() + return scheduler, pluginSchedulerWantsAcrossPriorities(scheduler) +} + +// PluginSchedulerWantsAcrossPriorities reports whether the configured plugin scheduler +// opted into receiving candidates across all priority tiers. +func (m *Manager) PluginSchedulerWantsAcrossPriorities() bool { + _, wants := m.snapshotPluginScheduler() + return wants } func isBuiltInSelector(selector Selector) bool { @@ -621,9 +627,8 @@ func (m *Manager) availableAuthsForRouteModelWithPriorityMode(auths []*Auth, pro // the plugin scheduler, plus the candidates handed to the configured selector. Both are equal // unless session affinity or an across-priorities scheduler is active, in which case the selector // or scheduler additionally receives lower priority tiers. -func (m *Manager) availableAuthsForSelector(selector Selector, auths []*Auth, provider, routeModel string, now time.Time) (priorityAuths, selectorAuths []*Auth, err error) { +func (m *Manager) availableAuthsForSelector(selector Selector, auths []*Auth, provider, routeModel string, now time.Time, schedulerAcross bool) (priorityAuths, selectorAuths []*Auth, err error) { _, sessionAffinity := selector.(*SessionAffinitySelector) - schedulerAcross := m.pluginSchedulerWantsAcrossPrioritiesLocked() if !sessionAffinity && !schedulerAcross { priorityAuths, err = m.availableAuthsForRouteModel(auths, provider, routeModel, now) @@ -1821,11 +1826,11 @@ func (m *Manager) pickNextLegacy(ctx context.Context, provider, model string, op pinnedAuthID := pinnedAuthIDFromMetadata(opts.Metadata) eligibility := authSelectionEligibilityForRequest(ctx, opts) + pluginScheduler, schedulerAcross := m.snapshotPluginScheduler() m.mu.RLock() selector := m.selector opts.Metadata[cliproxyexecutor.SessionAffinityModelMetadataKey] = selectionArgForSelector(selector, model) - pluginScheduler := m.pluginScheduler executor, okExecutor := m.executorLocked(provider) if !okExecutor { m.mu.RUnlock() @@ -1874,7 +1879,7 @@ func (m *Manager) pickNextLegacy(ctx context.Context, provider, model string, op return nil, nil, &Error{Code: "auth_not_found", Message: "no auth available"} } now := time.Now() - available, selectorAuths, errAvailable := m.availableAuthsForSelector(selector, candidates, provider, model, now) + available, selectorAuths, errAvailable := m.availableAuthsForSelector(selector, candidates, provider, model, now, schedulerAcross) if errAvailable != nil { m.mu.RUnlock() m.warnLogAuthUnavailable(ctx, []string{provider}, model, opts, tried, errAvailable) @@ -2163,11 +2168,11 @@ func (m *Manager) pickNextMixedLegacy(ctx context.Context, providers []string, m if len(providerSet) == 0 { return nil, nil, "", &Error{Code: "provider_not_found", Message: "no provider supplied"} } + pluginScheduler, schedulerAcross := m.snapshotPluginScheduler() m.mu.RLock() selector := m.selector opts.Metadata[cliproxyexecutor.SessionAffinityModelMetadataKey] = selectionArgForSelector(selector, model) - pluginScheduler := m.pluginScheduler candidates := make([]*Auth, 0, len(m.auths)) modelKey := strings.TrimSpace(model) // Always use base model name (without thinking suffix) for auth matching. @@ -2220,7 +2225,7 @@ func (m *Manager) pickNextMixedLegacy(ctx context.Context, providers []string, m return nil, nil, "", &Error{Code: "auth_not_found", Message: "no auth available"} } now := time.Now() - available, selectorAuths, errAvailable := m.availableAuthsForSelector(selector, candidates, "mixed", model, now) + available, selectorAuths, errAvailable := m.availableAuthsForSelector(selector, candidates, "mixed", model, now, schedulerAcross) if errAvailable != nil { m.mu.RUnlock() m.warnLogAuthUnavailable(ctx, providers, model, opts, tried, errAvailable) diff --git a/sdk/cliproxy/auth/scheduler_test.go b/sdk/cliproxy/auth/scheduler_test.go index f4b64ab729d..9e2cb6dc0d1 100644 --- a/sdk/cliproxy/auth/scheduler_test.go +++ b/sdk/cliproxy/auth/scheduler_test.go @@ -64,16 +64,20 @@ func (schedulerTestExecutor) HttpRequest(ctx context.Context, auth *Auth, req *h } type fakePluginScheduler struct { - acrossPriorities bool - resp pluginapi.SchedulerPickResponse - handled bool - err error - calls int - requests []pluginapi.SchedulerPickRequest - pick func(context.Context, pluginapi.SchedulerPickRequest) (pluginapi.SchedulerPickResponse, bool, error) + acrossPriorities bool + acrossPrioritiesFunc func() bool + resp pluginapi.SchedulerPickResponse + handled bool + err error + calls int + requests []pluginapi.SchedulerPickRequest + pick func(context.Context, pluginapi.SchedulerPickRequest) (pluginapi.SchedulerPickResponse, bool, error) } func (s *fakePluginScheduler) SchedulerWantsAcrossPriorities() bool { + if s.acrossPrioritiesFunc != nil { + return s.acrossPrioritiesFunc() + } return s.acrossPriorities } @@ -1696,11 +1700,21 @@ func TestManagerPluginSchedulerCalledOutsideManagerLock(t *testing.T) { t.Fatalf("Register(auth-a) error = %v", errRegister) } + var currentTest *testing.T + acrossCalls := 0 scheduler := &fakePluginScheduler{ handled: true, + acrossPrioritiesFunc: func() bool { + acrossCalls++ + if !manager.mu.TryLock() { + currentTest.Fatalf("SchedulerWantsAcrossPriorities called while manager lock is held") + } + manager.mu.Unlock() + return true + }, pick: func(ctx context.Context, req pluginapi.SchedulerPickRequest) (pluginapi.SchedulerPickResponse, bool, error) { if !manager.mu.TryLock() { - t.Fatalf("plugin scheduler called while manager lock is held") + currentTest.Fatalf("plugin scheduler called while manager lock is held") } manager.mu.Unlock() return pluginapi.SchedulerPickResponse{Handled: true, AuthID: "auth-a"}, true, nil @@ -1708,18 +1722,114 @@ func TestManagerPluginSchedulerCalledOutsideManagerLock(t *testing.T) { } manager.SetPluginScheduler(scheduler) - got, _, errPick := manager.pickNext(context.Background(), "gemini", "", cliproxyexecutor.Options{}, nil) - if errPick != nil { - t.Fatalf("pickNext() error = %v", errPick) - } - if got == nil { - t.Fatalf("pickNext() auth = nil") + t.Run("PluginSchedulerWantsAcrossPriorities", func(t *testing.T) { + currentTest = t + if !manager.PluginSchedulerWantsAcrossPriorities() { + t.Fatalf("PluginSchedulerWantsAcrossPriorities() = false, want true") + } + }) + + t.Run("pickNext", func(t *testing.T) { + currentTest = t + got, _, errPick := manager.pickNext(context.Background(), "gemini", "", cliproxyexecutor.Options{}, nil) + if errPick != nil { + t.Fatalf("pickNext() error = %v", errPick) + } + if got == nil { + t.Fatalf("pickNext() auth = nil") + } + if got.ID != "auth-a" { + t.Fatalf("pickNext() auth.ID = %q, want auth-a", got.ID) + } + }) + + t.Run("pickNextMixed", func(t *testing.T) { + currentTest = t + gotMixed, _, _, errPickMixed := manager.pickNextMixed(context.Background(), []string{"gemini"}, "", cliproxyexecutor.Options{}, nil) + if errPickMixed != nil { + t.Fatalf("pickNextMixed() error = %v", errPickMixed) + } + if gotMixed == nil { + t.Fatalf("pickNextMixed() auth = nil") + } + if gotMixed.ID != "auth-a" { + t.Fatalf("pickNextMixed() auth.ID = %q, want auth-a", gotMixed.ID) + } + }) + + if scheduler.calls != 2 { + t.Fatalf("scheduler.calls = %d, want %d", scheduler.calls, 2) } - if got.ID != "auth-a" { - t.Fatalf("pickNext() auth.ID = %q, want auth-a", got.ID) + if acrossCalls != 3 { + t.Fatalf("acrossCalls = %d, want %d", acrossCalls, 3) } - if scheduler.calls != 1 { - t.Fatalf("scheduler.calls = %d, want %d", scheduler.calls, 1) +} + +func TestManagerPluginSchedulerSnapshotConsistencyAcrossSwap(t *testing.T) { + for _, mode := range []string{"single", "mixed"} { + t.Run(mode, func(t *testing.T) { + manager := NewManager(nil, &RoundRobinSelector{}, nil) + manager.executors["gemini"] = schedulerTestExecutor{} + if _, errRegister := manager.Register(context.Background(), &Auth{ + ID: "auth-high", + Provider: "gemini", + Attributes: map[string]string{"priority": "10"}, + }); errRegister != nil { + t.Fatalf("Register(auth-high) error = %v", errRegister) + } + if _, errRegister := manager.Register(context.Background(), &Auth{ + ID: "auth-low", + Provider: "gemini", + Attributes: map[string]string{"priority": "1"}, + }); errRegister != nil { + t.Fatalf("Register(auth-low) error = %v", errRegister) + } + + schedulerB := &fakePluginScheduler{ + acrossPriorities: false, + handled: true, + pick: func(ctx context.Context, req pluginapi.SchedulerPickRequest) (pluginapi.SchedulerPickResponse, bool, error) { + for _, candidate := range req.Candidates { + if candidate.ID == "auth-low" { + t.Fatalf("schedulerB (acrossPriorities=false) received low-priority candidate from schedulerA capability") + } + } + return pluginapi.SchedulerPickResponse{Handled: true, AuthID: "auth-high"}, true, nil + }, + } + schedulerA := &fakePluginScheduler{ + handled: true, + acrossPrioritiesFunc: func() bool { + manager.SetPluginScheduler(schedulerB) + return true + }, + pick: func(ctx context.Context, req pluginapi.SchedulerPickRequest) (pluginapi.SchedulerPickResponse, bool, error) { + if len(req.Candidates) != 2 { + t.Fatalf("schedulerA candidates = %d, want 2 across priorities", len(req.Candidates)) + } + return pluginapi.SchedulerPickResponse{Handled: true, AuthID: "auth-low"}, true, nil + }, + } + manager.SetPluginScheduler(schedulerA) + + if mode == "single" { + got, _, errPick := manager.pickNext(context.Background(), "gemini", "", cliproxyexecutor.Options{}, nil) + if errPick != nil { + t.Fatalf("pickNext() error = %v", errPick) + } + if got == nil || got.ID != "auth-low" { + t.Fatalf("pickNext() auth = %#v, want auth-low", got) + } + } else { + got, _, _, errPick := manager.pickNextMixed(context.Background(), []string{"gemini"}, "", cliproxyexecutor.Options{}, nil) + if errPick != nil { + t.Fatalf("pickNextMixed() error = %v", errPick) + } + if got == nil || got.ID != "auth-low" { + t.Fatalf("pickNextMixed() auth = %#v, want auth-low", got) + } + } + }) } } From 904ec710077ed94e1cb068477fd981100fe419a7 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Mon, 19 Oct 2026 02:35:21 +0800 Subject: [PATCH 17/27] feat(auth): enable expired antigravity AI credits negative hint recovery and retry logic - Add freshness validation helper for antigravity credits hints and synchronize the refresh interval constant. - Allow expired negative credits hints to trigger re-validation and clear permanent failure state upon successful recovery. - Include `INSUFFICIENT_G1_CREDITS_BALANCE` in full quota exhaustion decisions and ensure candidates with expired negative hints are re-queried. Closes: #6745 --- .../runtime/executor/antigravity_executor.go | 2 +- .../executor/antigravity_executor_credits.go | 12 +- .../antigravity_executor_credits_test.go | 278 ++++++++++++++++++ sdk/cliproxy/auth/antigravity_credits.go | 10 + sdk/cliproxy/auth/antigravity_credits_test.go | 113 ++++++- .../auth/conductor_credits_candidates_test.go | 23 ++ sdk/cliproxy/auth/conductor_home.go | 5 + 7 files changed, 436 insertions(+), 7 deletions(-) diff --git a/internal/runtime/executor/antigravity_executor.go b/internal/runtime/executor/antigravity_executor.go index 1e052408133..25118cfe66b 100644 --- a/internal/runtime/executor/antigravity_executor.go +++ b/internal/runtime/executor/antigravity_executor.go @@ -42,7 +42,7 @@ const ( antigravityClientSecret = "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" antigravityAuthType = "antigravity" antigravityRequestTokenSafetyWindow = 5 * time.Minute - antigravityCreditsHintRefreshInterval = 10 * time.Minute + antigravityCreditsHintRefreshInterval = cliproxyauth.AntigravityCreditsHintRefreshInterval antigravityCreditsHintRefreshTimeout = 5 * time.Second antigravityShortQuotaCooldownThreshold = 5 * time.Minute antigravityInstantRetryThreshold = 3 * time.Second diff --git a/internal/runtime/executor/antigravity_executor_credits.go b/internal/runtime/executor/antigravity_executor_credits.go index 51645397171..531dca1b4e7 100644 --- a/internal/runtime/executor/antigravity_executor_credits.go +++ b/internal/runtime/executor/antigravity_executor_credits.go @@ -247,7 +247,7 @@ func decideAntigravity429(body []byte) antigravity429Decision { reason := strings.TrimSpace(detail.Get("reason").String()) decision.reason = reason switch { - case strings.EqualFold(reason, "QUOTA_EXHAUSTED"): + case strings.EqualFold(reason, "QUOTA_EXHAUSTED"), strings.EqualFold(reason, "INSUFFICIENT_G1_CREDITS_BALANCE"): decision.kind = antigravity429DecisionFullQuotaExhausted return decision case strings.EqualFold(reason, "RATE_LIMIT_EXCEEDED"): @@ -289,7 +289,13 @@ func clearAntigravityCreditsFailureState(auth *cliproxyauth.Auth) { if auth == nil || strings.TrimSpace(auth.ID) == "" { return } - antigravityCreditsFailureByAuth.Delete(strings.TrimSpace(auth.ID)) + authID := strings.TrimSpace(auth.ID) + _, hadFailure := antigravityCreditsFailureByAuth.LoadAndDelete(authID) + if hint, ok := cliproxyauth.GetAntigravityCreditsHint(authID); ok && hint.Known && !hint.Available { + if hadFailure || !cliproxyauth.IsAntigravityCreditsHintFresh(hint, time.Now()) { + cliproxyauth.SetAntigravityCreditsHint(authID, cliproxyauth.AntigravityCreditsHint{}) + } + } } func markAntigravityCreditsPermanentlyDisabled(auth *cliproxyauth.Auth) { if auth == nil || strings.TrimSpace(auth.ID) == "" || antigravityCoolingDisabled(auth, nil) { @@ -363,7 +369,7 @@ func (e *AntigravityExecutor) maybeRefreshAntigravityCreditsHint(ctx context.Con if authID == "" { return } - if hint, ok := cliproxyauth.GetAntigravityCreditsHint(authID); ok && hint.Known { + if hint, ok := cliproxyauth.GetAntigravityCreditsHint(authID); ok && cliproxyauth.IsAntigravityCreditsHintFresh(hint, time.Now()) { return } if strings.TrimSpace(accessToken) == "" { diff --git a/internal/runtime/executor/antigravity_executor_credits_test.go b/internal/runtime/executor/antigravity_executor_credits_test.go index 8b0bb9eeba1..0fb64b035ab 100644 --- a/internal/runtime/executor/antigravity_executor_credits_test.go +++ b/internal/runtime/executor/antigravity_executor_credits_test.go @@ -10,11 +10,13 @@ import ( "net/http/httptest" "strings" "sync" + "sync/atomic" "testing" "time" "github.com/router-for-me/CLIProxyAPI/v8/internal/config" homekv "github.com/router-for-me/CLIProxyAPI/v8/internal/home" + "github.com/router-for-me/CLIProxyAPI/v8/internal/registry" "github.com/router-for-me/CLIProxyAPI/v8/internal/runtime/executor/helps" cliproxyauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth" cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor" @@ -670,6 +672,282 @@ func TestEnsureAccessToken_WarmTokenLoadsCreditsHint(t *testing.T) { } } +func TestMaybeRefreshAntigravityCreditsHint_RefreshesExpiredHintAndRecoversCredits(t *testing.T) { + resetAntigravityCreditsRetryState() + t.Cleanup(resetAntigravityCreditsRetryState) + + exec := NewAntigravityExecutor(&config.Config{ + QuotaExceeded: config.QuotaExceeded{AntigravityCredits: true}, + }) + auth := &cliproxyauth.Auth{ + ID: fmt.Sprintf("auth-expired-credits-hint-%d", time.Now().UnixNano()), + Metadata: map[string]any{ + "access_token": "token", + "expired": time.Now().Add(1 * time.Hour).Format(time.RFC3339), + }, + } + t.Cleanup(func() { + cliproxyauth.SetAntigravityCreditsHint(auth.ID, cliproxyauth.AntigravityCreditsHint{}) + }) + + markAntigravityCreditsPermanentlyDisabled(auth) + if _, failed := antigravityCreditsFailureByAuth.Load(auth.ID); !failed { + t.Fatal("expected auth to be marked permanently disabled for credits") + } + + var loadCodeAssistCalls int + ctx := context.WithValue(context.Background(), "cliproxy.roundtripper", roundTripperFunc(func(req *http.Request) (*http.Response, error) { + if req.URL.String() != "https://cloudcode-pa.googleapis.com/v1internal:loadCodeAssist" { + t.Fatalf("unexpected request url %s", req.URL.String()) + } + loadCodeAssistCalls++ + return &http.Response{ + StatusCode: http.StatusOK, + Header: make(http.Header), + Body: io.NopCloser(strings.NewReader(`{"paidTier":{"id":"tier-1","availableCredits":[{"creditType":"GOOGLE_ONE_AI","creditAmount":"2516","minimumCreditAmountForUsage":"50"}]}}`)), + }, nil + })) + + // Fresh hint (< antigravityCreditsHintRefreshInterval) must not trigger a refresh. + exec.maybeRefreshAntigravityCreditsHint(ctx, auth, "token") + if loadCodeAssistCalls != 0 { + t.Fatalf("loadCodeAssist calls for fresh hint = %d, want 0", loadCodeAssistCalls) + } + + // Once hint.UpdatedAt is older than antigravityCreditsHintRefreshInterval, + // maybeRefreshAntigravityCreditsHint must re-query loadCodeAssist and recover credits. + cliproxyauth.SetAntigravityCreditsHint(auth.ID, cliproxyauth.AntigravityCreditsHint{ + Known: true, + Available: false, + CreditAmount: 0, + MinCreditAmount: 1, + UpdatedAt: time.Now().Add(-antigravityCreditsHintRefreshInterval - time.Second), + }) + + exec.maybeRefreshAntigravityCreditsHint(ctx, auth, "token") + if value, ok := antigravityCreditsHintRefreshByID.Load(auth.ID); ok { + if state, ok := value.(*antigravityCreditsHintRefreshState); ok && state != nil { + waitForAntigravityCreditsRefresh(t, state) + } + } + + if loadCodeAssistCalls != 1 { + t.Fatalf("loadCodeAssist calls for expired hint = %d, want 1", loadCodeAssistCalls) + } + hint, ok := cliproxyauth.GetAntigravityCreditsHint(auth.ID) + if !ok || !hint.Known || !hint.Available { + t.Fatalf("credits hint after refresh = %#v (ok=%v), want Known=true Available=true", hint, ok) + } + if hint.CreditAmount != 2516 || hint.MinCreditAmount != 50 { + t.Fatalf("hint amounts = (%v, %v), want (2516, 50)", hint.CreditAmount, hint.MinCreditAmount) + } + if _, failed := antigravityCreditsFailureByAuth.Load(auth.ID); failed { + t.Fatal("expected permanently disabled credits state to be cleared after recovery") + } +} + +func TestAntigravityManagerExecuteAndStream_ToppedUpCreditsRecoverDuringQuotaCooldown(t *testing.T) { + for _, stream := range []bool{false, true} { + mode := "execute" + if stream { + mode = "execute_stream" + } + t.Run(mode, func(t *testing.T) { + resetAntigravityCreditsRetryState() + t.Cleanup(resetAntigravityCreditsRetryState) + + const model = "claude-sonnet-4-6" + var toppedUp atomic.Bool + var loadCodeAssistCalls atomic.Int32 + + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + body, _ := io.ReadAll(r.Body) + _ = r.Body.Close() + + if r.URL.Path == "/v1internal:loadCodeAssist" { + callNum := loadCodeAssistCalls.Add(1) + w.Header().Set("Content-Type", "application/json") + if toppedUp.Load() { + _, _ = w.Write([]byte(`{"paidTier":{"id":"tier-1","availableCredits":[{"creditType":"GOOGLE_ONE_AI","creditAmount":"2516","minimumCreditAmountForUsage":"50"}]}}`)) + } else if callNum == 1 { + _, _ = w.Write([]byte(`{"paidTier":{"id":"tier-1","availableCredits":[{"creditType":"GOOGLE_ONE_AI","creditAmount":"100","minimumCreditAmountForUsage":"50"}]}}`)) + } else { + _, _ = w.Write([]byte(`{"paidTier":{"id":"tier-1","availableCredits":[{"creditType":"GOOGLE_ONE_AI","creditAmount":"0","minimumCreditAmountForUsage":"50"}]}}`)) + } + return + } + + hasCreditsFlag := strings.Contains(string(body), `"enabledCreditTypes":["GOOGLE_ONE_AI"]`) + if !hasCreditsFlag { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusTooManyRequests) + _, _ = w.Write([]byte(`{ + "error": { + "code": 429, + "message": "You have exhausted your capacity on this model. Your quota will reset after 1h0m0s.", + "status": "RESOURCE_EXHAUSTED", + "details": [ + {"@type": "type.googleapis.com/google.rpc.ErrorInfo", "reason": "QUOTA_EXHAUSTED"}, + {"@type": "type.googleapis.com/google.rpc.RetryInfo", "retryDelay": "3600s"} + ] + } + }`)) + return + } + + if !toppedUp.Load() { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusTooManyRequests) + _, _ = w.Write([]byte(`{ + "error": { + "code": 429, + "message": "Insufficient Google One AI credits balance.", + "status": "RESOURCE_EXHAUSTED", + "details": [ + {"@type": "type.googleapis.com/google.rpc.ErrorInfo", "reason": "INSUFFICIENT_G1_CREDITS_BALANCE"} + ] + } + }`)) + return + } + + w.Header().Set("Content-Type", "text/event-stream") + _, _ = w.Write([]byte("data: {\"response\":{\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"text\":\"recovered\"}]},\"finishReason\":\"STOP\"}],\"usageMetadata\":{\"promptTokenCount\":1,\"candidatesTokenCount\":1,\"totalTokenCount\":2}}}\n\n")) + })) + defer server.Close() + + cfg := &config.Config{ + QuotaExceeded: config.QuotaExceeded{AntigravityCredits: true}, + } + exec := NewAntigravityExecutor(cfg) + manager := cliproxyauth.NewManager(nil, nil, nil) + manager.SetConfig(cfg) + manager.RegisterExecutor(exec) + + authID := fmt.Sprintf("ag-topup-recovery-%s-%d", mode, time.Now().UnixNano()) + registry.GetGlobalRegistry().RegisterClient(authID, "antigravity", []*registry.ModelInfo{{ID: model}}) + t.Cleanup(func() { + registry.GetGlobalRegistry().UnregisterClient(authID) + cliproxyauth.SetAntigravityCreditsHint(authID, cliproxyauth.AntigravityCreditsHint{}) + }) + + if _, errRegister := manager.Register(context.Background(), &cliproxyauth.Auth{ + ID: authID, + Provider: "antigravity", + Attributes: map[string]string{ + "base_url": server.URL, + }, + Metadata: map[string]any{ + "access_token": "token", + "project_id": "project-1", + "expired": time.Now().Add(1 * time.Hour).Format(time.RFC3339), + }, + }); errRegister != nil { + t.Fatalf("register auth: %v", errRegister) + } + + req := cliproxyexecutor.Request{ + Model: model, + Payload: []byte(`{"request":{"contents":[{"role":"user","parts":[{"text":"hi"}]}]}}`), + } + opts := cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FormatAntigravity, + ResponseFormat: sdktranslator.FormatAntigravity, + } + + // Step 1: Exhaust weekly quota (QUOTA_EXHAUSTED) and credits (INSUFFICIENT_G1_CREDITS_BALANCE). + cliproxyauth.SetAntigravityCreditsHint(authID, cliproxyauth.AntigravityCreditsHint{ + Known: true, + Available: true, + CreditAmount: 100, + MinCreditAmount: 50, + UpdatedAt: time.Now(), + }) + if stream { + if _, errStream := manager.ExecuteStream(context.Background(), []string{"antigravity"}, req, opts); errStream == nil { + t.Fatal("initial ExecuteStream() succeeded, want 429 after quota and credits exhaustion") + } + } else { + if _, errExec := manager.Execute(context.Background(), []string{"antigravity"}, req, opts); errExec == nil { + t.Fatal("initial Execute() succeeded, want 429 after quota and credits exhaustion") + } + } + if value, ok := antigravityCreditsHintRefreshByID.Load(authID); ok { + if state, ok := value.(*antigravityCreditsHintRefreshState); ok && state != nil { + waitForAntigravityCreditsRefresh(t, state) + } + } + if _, failed := antigravityCreditsFailureByAuth.Load(authID); !failed { + t.Fatal("expected auth to be marked permanently disabled after INSUFFICIENT_G1_CREDITS_BALANCE") + } + hint, ok := cliproxyauth.GetAntigravityCreditsHint(authID) + if !ok || !hint.Known || hint.Available { + t.Fatalf("hint after exhaustion = %#v (ok=%v), want Known=true Available=false", hint, ok) + } + + // Step 2: Top up credits and expire the cached hint/throttle window while model remains in 1h quota cooldown. + toppedUp.Store(true) + callsBeforeTopUp := loadCodeAssistCalls.Load() + if value, ok := antigravityCreditsHintRefreshByID.Load(authID); ok { + if state, ok := value.(*antigravityCreditsHintRefreshState); ok && state != nil { + state.mu.Lock() + state.lastAttempt = time.Now().Add(-antigravityCreditsHintRefreshInterval - time.Second) + state.mu.Unlock() + } + } + cliproxyauth.SetAntigravityCreditsHint(authID, cliproxyauth.AntigravityCreditsHint{ + Known: true, + Available: false, + CreditAmount: 0, + MinCreditAmount: 1, + UpdatedAt: time.Now().Add(-antigravityCreditsHintRefreshInterval - time.Second), + }) + + // Step 3: Request recovers via credits fallback while model cooldown is active and triggers loadCodeAssist refresh. + if stream { + streamResult, errStream := manager.ExecuteStream(context.Background(), []string{"antigravity"}, req, opts) + if errStream != nil { + t.Fatalf("ExecuteStream() after top-up error = %v", errStream) + } + var streamed strings.Builder + for chunk := range streamResult.Chunks { + if chunk.Err != nil { + t.Fatalf("unexpected stream chunk error: %v", chunk.Err) + } + streamed.Write(chunk.Payload) + } + if !strings.Contains(streamed.String(), "recovered") { + t.Fatalf("ExecuteStream() payload = %s, want recovered", streamed.String()) + } + } else { + resp, errExec := manager.Execute(context.Background(), []string{"antigravity"}, req, opts) + if errExec != nil { + t.Fatalf("Execute() after top-up error = %v", errExec) + } + if !strings.Contains(string(resp.Payload), "recovered") { + t.Fatalf("Execute() payload = %s, want recovered", string(resp.Payload)) + } + } + + if value, ok := antigravityCreditsHintRefreshByID.Load(authID); ok { + if state, ok := value.(*antigravityCreditsHintRefreshState); ok && state != nil { + waitForAntigravityCreditsRefresh(t, state) + } + } + if loadCodeAssistCalls.Load() <= callsBeforeTopUp { + t.Fatalf("loadCodeAssist calls after top-up = %d, want > %d", loadCodeAssistCalls.Load(), callsBeforeTopUp) + } + hint, ok = cliproxyauth.GetAntigravityCreditsHint(authID) + if !ok || !hint.Known || !hint.Available || hint.CreditAmount != 2516 { + t.Fatalf("hint after recovery = %#v (ok=%v), want Known=true Available=true CreditAmount=2516", hint, ok) + } + if _, failed := antigravityCreditsFailureByAuth.Load(authID); failed { + t.Fatal("expected permanently disabled credits state to be cleared after recovery") + } + }) + } +} + func TestUpdateAntigravityCreditsBalance_LoadCodeAssistUserAgent(t *testing.T) { resetAntigravityCreditsRetryState() t.Cleanup(resetAntigravityCreditsRetryState) diff --git a/sdk/cliproxy/auth/antigravity_credits.go b/sdk/cliproxy/auth/antigravity_credits.go index 06e5d285e8d..29524e2a013 100644 --- a/sdk/cliproxy/auth/antigravity_credits.go +++ b/sdk/cliproxy/auth/antigravity_credits.go @@ -26,6 +26,10 @@ func AntigravityCreditsRequested(ctx context.Context) bool { return v } +// AntigravityCreditsHintRefreshInterval defines how long a cached AI credits hint +// remains fresh before it should be re-validated. +const AntigravityCreditsHintRefreshInterval = 10 * time.Minute + // AntigravityCreditsHint stores the latest known AI credits state for one auth. type AntigravityCreditsHint struct { Known bool @@ -36,6 +40,12 @@ type AntigravityCreditsHint struct { UpdatedAt time.Time } +// IsAntigravityCreditsHintFresh reports whether a known credits hint is still +// within AntigravityCreditsHintRefreshInterval of now. +func IsAntigravityCreditsHintFresh(hint AntigravityCreditsHint, now time.Time) bool { + return hint.Known && !hint.UpdatedAt.IsZero() && now.Sub(hint.UpdatedAt) < AntigravityCreditsHintRefreshInterval +} + var antigravityCreditsHintByAuth sync.Map // SetAntigravityCreditsHint updates the latest known AI credits state for an auth. diff --git a/sdk/cliproxy/auth/antigravity_credits_test.go b/sdk/cliproxy/auth/antigravity_credits_test.go index 5e352b77b02..94a9448f078 100644 --- a/sdk/cliproxy/auth/antigravity_credits_test.go +++ b/sdk/cliproxy/auth/antigravity_credits_test.go @@ -16,13 +16,22 @@ import ( ) type antigravityCreditsFallbackExecutor struct { - streamCreditsRequested []bool + executeCreditsRequested []bool + streamCreditsRequested []bool } func (e *antigravityCreditsFallbackExecutor) Identifier() string { return "antigravity" } -func (e *antigravityCreditsFallbackExecutor) Execute(context.Context, *Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { - return cliproxyexecutor.Response{}, &Error{HTTPStatus: http.StatusNotImplemented, Message: "Execute not implemented"} +func (e *antigravityCreditsFallbackExecutor) Execute(ctx context.Context, _ *Auth, req cliproxyexecutor.Request, _ cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { + creditsRequested := AntigravityCreditsRequested(ctx) + e.executeCreditsRequested = append(e.executeCreditsRequested, creditsRequested) + if !creditsRequested { + return cliproxyexecutor.Response{}, &Error{HTTPStatus: http.StatusTooManyRequests, Message: "quota exhausted"} + } + return cliproxyexecutor.Response{ + Payload: []byte("credits fallback"), + Headers: http.Header{"X-Credits": {req.Model}}, + }, nil } func (e *antigravityCreditsFallbackExecutor) ExecuteStream(ctx context.Context, _ *Auth, req cliproxyexecutor.Request, _ cliproxyexecutor.Options) (*cliproxyexecutor.StreamResult, error) { @@ -266,3 +275,101 @@ func TestIsAuthBlockedForModel_KeepsGeminiBlockedWithoutCreditsBypass(t *testing t.Fatalf("expected gemini model to remain blocked, got blocked=%v reason=%v", blocked, reason) } } + +func TestManagerExecuteAndStream_ExpiredNegativeCreditsHintRecoversDuringModelCooldown(t *testing.T) { + for _, stream := range []bool{false, true} { + name := "execute" + if stream { + name = "execute_stream" + } + t.Run(name, func(t *testing.T) { + authID := "ag-expired-negative-hint-" + name + const model = "claude-sonnet-4-6" + executor := &antigravityCreditsFallbackExecutor{} + manager := NewManager(nil, nil, nil) + manager.SetConfig(&internalconfig.Config{ + QuotaExceeded: internalconfig.QuotaExceeded{AntigravityCredits: true}, + }) + manager.RegisterExecutor(executor) + registry.GetGlobalRegistry().RegisterClient(authID, "antigravity", []*registry.ModelInfo{{ID: model}}) + t.Cleanup(func() { + registry.GetGlobalRegistry().UnregisterClient(authID) + SetAntigravityCreditsHint(authID, AntigravityCreditsHint{}) + }) + + cooldownUntil := time.Now().Add(30 * time.Minute) + if _, errRegister := manager.Register(context.Background(), &Auth{ + ID: authID, + Provider: "antigravity", + ModelStates: map[string]*ModelState{ + model: { + Unavailable: true, + NextRetryAfter: cooldownUntil, + Quota: QuotaState{ + Exceeded: true, + NextRecoverAt: cooldownUntil, + }, + }, + }, + }); errRegister != nil { + t.Fatalf("register auth: %v", errRegister) + } + + // Fresh negative hint must still suppress credits fallback while model is in cooldown. + SetAntigravityCreditsHint(authID, AntigravityCreditsHint{ + Known: true, + Available: false, + UpdatedAt: time.Now(), + }) + if stream { + if _, errStream := manager.ExecuteStream(context.Background(), []string{"antigravity"}, cliproxyexecutor.Request{Model: model}, cliproxyexecutor.Options{}); errStream == nil { + t.Fatal("ExecuteStream() succeeded with fresh negative credits hint, want cooldown error") + } + } else { + if _, errExec := manager.Execute(context.Background(), []string{"antigravity"}, cliproxyexecutor.Request{Model: model}, cliproxyexecutor.Options{}); errExec == nil { + t.Fatal("Execute() succeeded with fresh negative credits hint, want cooldown error") + } + } + if len(executor.executeCreditsRequested) != 0 || len(executor.streamCreditsRequested) != 0 { + t.Fatalf("executor calls during fresh negative hint = (%d, %d), want (0, 0)", len(executor.executeCreditsRequested), len(executor.streamCreditsRequested)) + } + + // Once the negative hint expires, execution must enter credits fallback even while model cooldown is active. + SetAntigravityCreditsHint(authID, AntigravityCreditsHint{ + Known: true, + Available: false, + UpdatedAt: time.Now().Add(-AntigravityCreditsHintRefreshInterval - time.Second), + }) + if stream { + streamResult, errStream := manager.ExecuteStream(context.Background(), []string{"antigravity"}, cliproxyexecutor.Request{Model: model}, cliproxyexecutor.Options{}) + if errStream != nil { + t.Fatalf("ExecuteStream() after negative hint expiry error = %v", errStream) + } + var streamed []byte + for chunk := range streamResult.Chunks { + if chunk.Err != nil { + t.Fatalf("unexpected stream chunk error: %v", chunk.Err) + } + streamed = append(streamed, chunk.Payload...) + } + if string(streamed) != "credits fallback" { + t.Fatalf("ExecuteStream() payload = %q, want %q", string(streamed), "credits fallback") + } + if len(executor.streamCreditsRequested) != 1 || !executor.streamCreditsRequested[0] { + t.Fatalf("streamCreditsRequested = %v, want [true]", executor.streamCreditsRequested) + } + } else { + resp, errExec := manager.Execute(context.Background(), []string{"antigravity"}, cliproxyexecutor.Request{Model: model}, cliproxyexecutor.Options{}) + if errExec != nil { + t.Fatalf("Execute() after negative hint expiry error = %v", errExec) + } + if string(resp.Payload) != "credits fallback" { + t.Fatalf("Execute() payload = %q, want %q", string(resp.Payload), "credits fallback") + } + if len(executor.executeCreditsRequested) != 1 || !executor.executeCreditsRequested[0] { + t.Fatalf("executeCreditsRequested = %v, want [true]", executor.executeCreditsRequested) + } + } + }) + } +} diff --git a/sdk/cliproxy/auth/conductor_credits_candidates_test.go b/sdk/cliproxy/auth/conductor_credits_candidates_test.go index 24109837664..35cd33a64ef 100644 --- a/sdk/cliproxy/auth/conductor_credits_candidates_test.go +++ b/sdk/cliproxy/auth/conductor_credits_candidates_test.go @@ -72,6 +72,29 @@ func TestFindAllAntigravityCreditsCandidateAuths_PrefersKnownCreditsThenUnknown( if pinned[0].auth.ID != "aa-unknown" { t.Fatalf("pinned[0].auth.ID = %q, want %q", pinned[0].auth.ID, "aa-unknown") } + + // Once the negative hint expires (>= 10 minutes), mm-no should be retried as an unknown candidate. + SetAntigravityCreditsHint("mm-no", AntigravityCreditsHint{ + Known: true, + Available: false, + UpdatedAt: time.Now().Add(-10*time.Minute - time.Second), + }) + t.Cleanup(func() { + SetAntigravityCreditsHint("zz-credits", AntigravityCreditsHint{}) + SetAntigravityCreditsHint("mm-no", AntigravityCreditsHint{}) + }) + + afterExpiry, errAfterExpiry := m.findAllAntigravityCreditsCandidateAuths(context.Background(), "claude-sonnet-4-6", opts) + if errAfterExpiry != nil { + t.Fatalf("findAllAntigravityCreditsCandidateAuths(after expiry) error = %v", errAfterExpiry) + } + if len(afterExpiry) != 3 { + t.Fatalf("afterExpiry candidates len = %d, want 3", len(afterExpiry)) + } + if afterExpiry[0].auth.ID != "zz-credits" || afterExpiry[1].auth.ID != "aa-unknown" || afterExpiry[2].auth.ID != "mm-no" { + t.Fatalf("afterExpiry order = [%s, %s, %s], want [zz-credits, aa-unknown, mm-no]", + afterExpiry[0].auth.ID, afterExpiry[1].auth.ID, afterExpiry[2].auth.ID) + } } func TestFindAllAntigravityCreditsCandidateAuths_HomeKVUnavailableReturnsError(t *testing.T) { diff --git a/sdk/cliproxy/auth/conductor_home.go b/sdk/cliproxy/auth/conductor_home.go index d1b1ebce3a7..1810c78d29b 100644 --- a/sdk/cliproxy/auth/conductor_home.go +++ b/sdk/cliproxy/auth/conductor_home.go @@ -1309,6 +1309,7 @@ func (m *Manager) findAllAntigravityCreditsCandidateAuths(ctx context.Context, r } m.mu.RUnlock() + now := time.Now() var known []creditsCandidateEntry var unknown []creditsCandidateEntry for _, candidate := range candidates { @@ -1318,6 +1319,10 @@ func (m *Manager) findAllAntigravityCreditsCandidateAuths(ctx context.Context, r } if okHint && hint.Known { if !hint.Available { + if IsAntigravityCreditsHintFresh(hint, now) { + continue + } + unknown = append(unknown, candidate) continue } known = append(known, candidate) From 030d17ed947adf8f441c853afb6ddc0637f72fe9 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Mon, 19 Oct 2026 08:11:17 +0800 Subject: [PATCH 18/27] perf(openai): cache stream preferences and tool maps per stream in Claude response translator - Cache and reuse resolved stream configuration and tool name maps across stream chunks to avoid redundant request body parsing. - Introduce dedicated state flags in conversion parameters to track resolution status safely. Closes: #6746 --- .../openai/claude/openai_claude_response.go | 29 +++++-- .../claude/openai_claude_response_test.go | 76 +++++++++++++++++++ 2 files changed, 98 insertions(+), 7 deletions(-) diff --git a/internal/translator/openai/claude/openai_claude_response.go b/internal/translator/openai/claude/openai_claude_response.go index d416d41690f..83b2c03e90d 100644 --- a/internal/translator/openai/claude/openai_claude_response.go +++ b/internal/translator/openai/claude/openai_claude_response.go @@ -68,6 +68,14 @@ type ConvertOpenAIResponseToAnthropicParams struct { UsageOutputTokens int64 UsageCachedTokens int64 UsageCacheWriteTokens int64 + // ToolNameMapResolved tracks whether ToolNameMap has been resolved from + // the original request once per stream. + ToolNameMapResolved bool + // StreamResolved and StreamRequested cache whether the original request + // asked for streaming, resolved once per stream so large request bodies + // are not re-parsed on every chunk. + StreamResolved bool + StreamRequested bool } // InterleavedContentChunk stores a buffered chunk of text or thinking arriving while a tool call is open @@ -131,21 +139,28 @@ func ConvertOpenAIResponseToClaude(_ context.Context, _ string, originalRequestR } rawJSON = bytes.TrimSpace(rawJSON[5:]) - if (*param).(*ConvertOpenAIResponseToAnthropicParams).ToolNameMap == nil { - (*param).(*ConvertOpenAIResponseToAnthropicParams).ToolNameMap = util.ToolNameMapFromClaudeRequest(originalRequestRawJSON) + state := (*param).(*ConvertOpenAIResponseToAnthropicParams) + if !state.ToolNameMapResolved { + if state.ToolNameMap == nil { + state.ToolNameMap = util.ToolNameMapFromClaudeRequest(originalRequestRawJSON) + } + state.ToolNameMapResolved = true } // Check if this is the [DONE] marker if bytes.Equal(bytes.TrimSpace(rawJSON), []byte("[DONE]")) { - return convertOpenAIDoneToAnthropic((*param).(*ConvertOpenAIResponseToAnthropicParams)) + return convertOpenAIDoneToAnthropic(state) } - streamResult := gjson.GetBytes(originalRequestRawJSON, "stream") - if !streamResult.Exists() || (streamResult.Exists() && streamResult.Type == gjson.False) { + if !state.StreamResolved { + streamResult := gjson.GetBytes(originalRequestRawJSON, "stream") + state.StreamRequested = streamResult.Exists() && streamResult.Type != gjson.False + state.StreamResolved = true + } + if !state.StreamRequested { return convertOpenAINonStreamingToAnthropic(rawJSON) - } else { - return convertOpenAIStreamingChunkToAnthropic(rawJSON, (*param).(*ConvertOpenAIResponseToAnthropicParams)) } + return convertOpenAIStreamingChunkToAnthropic(rawJSON, state) } func hasValidToolCallArguments(param *ConvertOpenAIResponseToAnthropicParams) bool { diff --git a/internal/translator/openai/claude/openai_claude_response_test.go b/internal/translator/openai/claude/openai_claude_response_test.go index 6dc811238a7..bfd5582c367 100644 --- a/internal/translator/openai/claude/openai_claude_response_test.go +++ b/internal/translator/openai/claude/openai_claude_response_test.go @@ -1468,3 +1468,79 @@ func TestExtractOpenAIUsage(t *testing.T) { }) } } + +const streamFlagTestChunk = `data: {"id":"chatcmpl_1","model":"test-model","created":1,"choices":[{"index":0,"delta":{"content":"hi"},"finish_reason":null}]}` + +func TestConvertOpenAIResponseToClaude_StreamFlagResolvedOncePerStream(t *testing.T) { + var param any + first := ConvertOpenAIResponseToClaude(context.Background(), "test-model", []byte(`{"model":"m","stream":true}`), nil, []byte(streamFlagTestChunk), ¶m) + if !bytes.Contains(bytes.Join(first, nil), []byte("content_block_delta")) { + t.Fatalf("expected streaming events on first chunk, got %q", bytes.Join(first, nil)) + } + + // An empty original request on subsequent chunks would parse as non-streaming if re-read. + second := ConvertOpenAIResponseToClaude(context.Background(), "test-model", nil, nil, []byte(streamFlagTestChunk), ¶m) + if !bytes.Contains(bytes.Join(second, nil), []byte("content_block_delta")) { + t.Fatalf("expected cached stream flag to be reused on subsequent chunks, got %q", bytes.Join(second, nil)) + } +} + +func TestConvertOpenAIResponseToClaude_ToolNameMapResolvedOnceWhenNoTools(t *testing.T) { + var param any + first := ConvertOpenAIResponseToClaude(context.Background(), "test-model", []byte(`{"model":"m","stream":true}`), nil, []byte(streamFlagTestChunk), ¶m) + if !bytes.Contains(bytes.Join(first, nil), []byte("content_block_delta")) { + t.Fatalf("expected streaming events on first chunk, got %q", bytes.Join(first, nil)) + } + + // Even when the first chunk resolved ToolNameMap to nil (no tools in request), + // later chunks must not re-parse originalRequestRawJSON to rebuild ToolNameMap. + toolChunk := `data: {"id":"chatcmpl_1","model":"test-model","created":1,"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"call_1","function":{"name":"bash","arguments":"{}"}}]},"finish_reason":null}]}` + laterReqWithTools := []byte(`{"model":"m","stream":true,"tools":[{"name":"Bash"}]}`) + second := ConvertOpenAIResponseToClaude(context.Background(), "test-model", laterReqWithTools, nil, []byte(toolChunk), ¶m) + joined := bytes.Join(second, nil) + if !bytes.Contains(joined, []byte(`"name":"bash"`)) { + t.Fatalf("expected cached nil ToolNameMap to be reused without re-parsing originalRequestRawJSON, got %q", joined) + } +} + +func TestConvertOpenAIResponseToClaude_NonStreamingRequestStaysNonStreaming(t *testing.T) { + for _, original := range []string{`{"model":"m"}`, `{"model":"m","stream":false}`} { + var param any + out := ConvertOpenAIResponseToClaude(context.Background(), "test-model", []byte(original), nil, []byte(streamFlagTestChunk), ¶m) + joined := bytes.Join(out, nil) + if len(out) != 1 || gjson.GetBytes(joined, "type").String() != "message" { + t.Fatalf("request %s expected a single non-streaming message payload, got %q", original, joined) + } + if bytes.Contains(joined, []byte("content_block_delta")) { + t.Fatalf("request %s must not produce streaming events, got %q", original, joined) + } + + // Subsequent call with stream:true must still use the cached non-streaming decision. + second := ConvertOpenAIResponseToClaude(context.Background(), "test-model", []byte(`{"model":"m","stream":true}`), nil, []byte(streamFlagTestChunk), ¶m) + secondJoined := bytes.Join(second, nil) + if len(second) != 1 || gjson.GetBytes(secondJoined, "type").String() != "message" || bytes.Contains(secondJoined, []byte("content_block_delta")) { + t.Fatalf("request %s expected cached non-streaming flag on second call, got %q", original, secondJoined) + } + } +} + +func BenchmarkConvertOpenAIResponseToClaude_LargeOriginalRequest(b *testing.B) { + var sb strings.Builder + sb.WriteString(`{"model":"m","tools":[`) + for i := 0; i < 60; i++ { + if i > 0 { + sb.WriteByte(',') + } + fmt.Fprintf(&sb, `{"name":"tool_%d","description":%q,"input_schema":{"type":"object","properties":{"a":{"type":"string"}}}}`, i, strings.Repeat("x", 5000)) + } + sb.WriteString(`],"stream":true}`) + original := []byte(sb.String()) + chunk := []byte(streamFlagTestChunk) + + var param any + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + ConvertOpenAIResponseToClaude(context.Background(), "test-model", original, nil, chunk, ¶m) + } +} From 9d453bc0dbeb4649a8793e6dab8ba23f140c173a Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Mon, 19 Oct 2026 09:51:36 +0800 Subject: [PATCH 19/27] feat(cache): add Claude web search result caching and restoration for sessions - Implement LRU cache and helpers for Claude native web search tool results. - Record search results from stream events and responses, and restore them in subsequent requests. - Add configuration options and test coverage for Claude web search result caching. Closes: #6753 --- config.example.yaml | 6 + .../cache/claude_web_search_result_cache.go | 146 ++++++++ .../claude_web_search_result_cache_test.go | 126 +++++++ internal/cache/signature_cache.go | 1 + internal/config/client_test.go | 157 +++++++++ internal/config/sdk_config.go | 215 ++++++++++++ internal/runtime/executor/claude_executor.go | 16 + .../executor/claude_executor_execute.go | 3 + .../executor/claude_executor_stream.go | 3 + .../executor/claude_executor_tokens.go | 8 + ...claude_executor_web_search_results_test.go | 325 ++++++++++++++++++ .../helps/claude_web_search_results.go | 100 ++++++ .../helps/claude_web_search_results_test.go | 85 +++++ internal/watcher/diff/config_diff.go | 9 + internal/watcher/diff/config_diff_test.go | 26 ++ sdk/config/config.go | 1 + 16 files changed, 1227 insertions(+) create mode 100644 internal/cache/claude_web_search_result_cache.go create mode 100644 internal/cache/claude_web_search_result_cache_test.go create mode 100644 internal/runtime/executor/claude_executor_web_search_results_test.go create mode 100644 internal/runtime/executor/helps/claude_web_search_results.go create mode 100644 internal/runtime/executor/helps/claude_web_search_results_test.go diff --git a/config.example.yaml b/config.example.yaml index 9a402263d39..372af6e52a2 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -325,6 +325,12 @@ client: # Default false. Applies to both OAuth and API-key credentials across providers. # Legacy provider paths migrate here; this path wins conflicts, including explicit false. optimize-multi-agent-v2: false + # Cache upstream web search results per session so replayed Codex web_search_call items + # that omit results still retain search results in later turns. + web-search-result-cache: + enabled: false + ttl: "24h" + limit: 1024 # Shared upstream behavior applies to OAuth and API-key credentials. upstream: diff --git a/internal/cache/claude_web_search_result_cache.go b/internal/cache/claude_web_search_result_cache.go new file mode 100644 index 00000000000..a37cd46d66c --- /dev/null +++ b/internal/cache/claude_web_search_result_cache.go @@ -0,0 +1,146 @@ +package cache + +import ( + "bytes" + "container/list" + "crypto/sha256" + "encoding/hex" + "strings" + "sync" + "time" +) + +const ( + defaultClaudeWebSearchResultTTL = 24 * time.Hour + defaultClaudeWebSearchResultLimit = 1024 +) + +type claudeWebSearchResultEntry struct { + key string + content []byte + ttl time.Duration + expiresAt time.Time +} + +var ( + claudeWebSearchResultMu sync.Mutex + claudeWebSearchResultIndex = make(map[string]*list.Element) + claudeWebSearchResultOrder = list.New() + claudeWebSearchResultNow = time.Now +) + +func claudeWebSearchResultKey(sessionKey, toolUseID string) (string, bool) { + sessionKey = strings.TrimSpace(sessionKey) + toolUseID = strings.TrimSpace(toolUseID) + if sessionKey == "" || toolUseID == "" { + return "", false + } + sum := sha256.Sum256([]byte(sessionKey + "\x00" + toolUseID)) + return hex.EncodeToString(sum[:]), true +} + +// CacheClaudeWebSearchResult stores the raw content of one web_search_tool_result +// block for a session. Memory is bounded by the maximum entry count (limit); +// least recently used entries are evicted when the entry limit is exceeded. +func CacheClaudeWebSearchResult(sessionKey, toolUseID string, content []byte, ttl time.Duration, limit int) { + key, ok := claudeWebSearchResultKey(sessionKey, toolUseID) + if !ok || len(content) == 0 { + return + } + if ttl <= 0 { + ttl = defaultClaudeWebSearchResultTTL + } + if limit <= 0 { + limit = defaultClaudeWebSearchResultLimit + } + cacheCleanupOnce.Do(startCacheCleanup) + + claudeWebSearchResultMu.Lock() + defer claudeWebSearchResultMu.Unlock() + + now := claudeWebSearchResultNow() + if element, exists := claudeWebSearchResultIndex[key]; exists { + removeClaudeWebSearchResultLocked(element) + } + if claudeWebSearchResultOrder.Len() >= limit { + purgeExpiredClaudeWebSearchResultLocked(now) + } + entry := &claudeWebSearchResultEntry{ + key: key, + content: bytes.Clone(content), + ttl: ttl, + expiresAt: now.Add(ttl), + } + claudeWebSearchResultIndex[key] = claudeWebSearchResultOrder.PushFront(entry) + for claudeWebSearchResultOrder.Len() > limit { + oldest := claudeWebSearchResultOrder.Back() + if oldest == nil { + break + } + removeClaudeWebSearchResultLocked(oldest) + } +} + +// GetClaudeWebSearchResult returns a copy of the stored content and extends its +// lifetime. Expired entries are removed and reported as missing. +func GetClaudeWebSearchResult(sessionKey, toolUseID string, ttl ...time.Duration) ([]byte, bool) { + key, ok := claudeWebSearchResultKey(sessionKey, toolUseID) + if !ok { + return nil, false + } + claudeWebSearchResultMu.Lock() + defer claudeWebSearchResultMu.Unlock() + + element, exists := claudeWebSearchResultIndex[key] + if !exists { + return nil, false + } + entry := element.Value.(*claudeWebSearchResultEntry) + now := claudeWebSearchResultNow() + if !now.Before(entry.expiresAt) { + removeClaudeWebSearchResultLocked(element) + return nil, false + } + effectiveTTL := entry.ttl + if len(ttl) > 0 && ttl[0] > 0 { + effectiveTTL = ttl[0] + entry.ttl = effectiveTTL + } + if effectiveTTL <= 0 { + effectiveTTL = defaultClaudeWebSearchResultTTL + } + entry.expiresAt = now.Add(effectiveTTL) + claudeWebSearchResultOrder.MoveToFront(element) + return bytes.Clone(entry.content), true +} + +// ClearClaudeWebSearchResultCache removes every stored entry. +func ClearClaudeWebSearchResultCache() { + claudeWebSearchResultMu.Lock() + defer claudeWebSearchResultMu.Unlock() + claudeWebSearchResultIndex = make(map[string]*list.Element) + claudeWebSearchResultOrder.Init() +} + +func purgeExpiredClaudeWebSearchResultCache(now time.Time) { + claudeWebSearchResultMu.Lock() + defer claudeWebSearchResultMu.Unlock() + purgeExpiredClaudeWebSearchResultLocked(now) +} + +func purgeExpiredClaudeWebSearchResultLocked(now time.Time) { + for element := claudeWebSearchResultOrder.Back(); element != nil; { + prev := element.Prev() + entry := element.Value.(*claudeWebSearchResultEntry) + if !now.Before(entry.expiresAt) { + removeClaudeWebSearchResultLocked(element) + } + element = prev + } +} + +func removeClaudeWebSearchResultLocked(element *list.Element) { + entry := element.Value.(*claudeWebSearchResultEntry) + delete(claudeWebSearchResultIndex, entry.key) + claudeWebSearchResultOrder.Remove(element) +} diff --git a/internal/cache/claude_web_search_result_cache_test.go b/internal/cache/claude_web_search_result_cache_test.go new file mode 100644 index 00000000000..472850f8b1c --- /dev/null +++ b/internal/cache/claude_web_search_result_cache_test.go @@ -0,0 +1,126 @@ +package cache + +import ( + "bytes" + "strconv" + "strings" + "testing" + "time" +) + +func useClaudeWebSearchResultClock(t *testing.T, now *time.Time) { + t.Helper() + ClearClaudeWebSearchResultCache() + previous := claudeWebSearchResultNow + claudeWebSearchResultNow = func() time.Time { return *now } + t.Cleanup(func() { + claudeWebSearchResultNow = previous + ClearClaudeWebSearchResultCache() + }) +} + +func TestClaudeWebSearchResultCacheScopesBySessionAndToolUse(t *testing.T) { + now := time.Unix(1_800_000_000, 0) + useClaudeWebSearchResultClock(t, &now) + + CacheClaudeWebSearchResult("session-a", "srvtoolu_1", []byte(`[{"url":"a"}]`), 30*time.Minute, 16) + + got, ok := GetClaudeWebSearchResult("session-a", "srvtoolu_1") + if !ok || string(got) != `[{"url":"a"}]` { + t.Fatalf("Get = %q, %v; want stored content", got, ok) + } + if _, ok := GetClaudeWebSearchResult("session-b", "srvtoolu_1"); ok { + t.Fatal("content leaked to another session") + } + if _, ok := GetClaudeWebSearchResult("session-a", "srvtoolu_2"); ok { + t.Fatal("content returned for another tool use id") + } + if _, ok := GetClaudeWebSearchResult("", "srvtoolu_1"); ok { + t.Fatal("empty session key must not match") + } + got[0] = 'X' + if again, _ := GetClaudeWebSearchResult("session-a", "srvtoolu_1"); !bytes.Equal(again, []byte(`[{"url":"a"}]`)) { + t.Fatalf("caller mutation changed the cache: %q", again) + } +} + +func TestClaudeWebSearchResultCacheExpiresAndSlides(t *testing.T) { + now := time.Unix(1_800_000_000, 0) + useClaudeWebSearchResultClock(t, &now) + + ttl := 30 * time.Minute + CacheClaudeWebSearchResult("s", "srvtoolu_1", []byte(`[1]`), ttl, 16) + now = now.Add(ttl - time.Minute) + if _, ok := GetClaudeWebSearchResult("s", "srvtoolu_1", ttl); !ok { + t.Fatal("entry expired before its TTL") + } + // The read above extended the lifetime. + now = now.Add(ttl - time.Minute) + if _, ok := GetClaudeWebSearchResult("s", "srvtoolu_1", ttl); !ok { + t.Fatal("read did not extend the entry lifetime") + } + now = now.Add(ttl) + if _, ok := GetClaudeWebSearchResult("s", "srvtoolu_1", ttl); ok { + t.Fatal("entry survived past its TTL") + } + if claudeWebSearchResultOrder.Len() != 0 { + t.Fatalf("expired entry not removed: len=%d", claudeWebSearchResultOrder.Len()) + } +} + +func TestClaudeWebSearchResultCacheEnforcesEntryLimitWithoutByteCap(t *testing.T) { + now := time.Unix(1_800_000_000, 0) + useClaudeWebSearchResultClock(t, &now) + + const limit = 4 + longSession := strings.Repeat("s", 4096) + // Large payloads must not be rejected by a byte limit; only the entry limit applies. + largeContent := bytes.Repeat([]byte("a"), 5<<20) + CacheClaudeWebSearchResult("s", "large", largeContent, time.Hour, limit) + if got, ok := GetClaudeWebSearchResult("s", "large"); !ok || len(got) != len(largeContent) { + t.Fatalf("large entry len = %d, ok = %v; want %d, true", len(got), ok, len(largeContent)) + } + + for i := 0; i < limit+10; i++ { + CacheClaudeWebSearchResult(longSession+strconv.Itoa(i), "srvtoolu_"+strconv.Itoa(i), []byte(`[1]`), time.Hour, limit) + } + if got := claudeWebSearchResultOrder.Len(); got != limit { + t.Fatalf("entries = %d, want %d", got, limit) + } + for key := range claudeWebSearchResultIndex { + if len(key) != 64 { + t.Fatalf("stored key len = %d, want 64-char SHA-256 hex", len(key)) + } + } + last := limit + 9 + if _, ok := GetClaudeWebSearchResult(longSession+strconv.Itoa(last), "srvtoolu_"+strconv.Itoa(last)); !ok { + t.Fatal("newest entry was evicted") + } + if _, ok := GetClaudeWebSearchResult(longSession+"0", "srvtoolu_0"); ok { + t.Fatal("oldest entry survived the entry limit") + } + + CacheClaudeWebSearchResult(longSession+strconv.Itoa(last), "srvtoolu_"+strconv.Itoa(last), []byte(`[2]`), time.Hour, limit) + if got, _ := GetClaudeWebSearchResult(longSession+strconv.Itoa(last), "srvtoolu_"+strconv.Itoa(last)); string(got) != `[2]` { + t.Fatalf("overwrite = %q, want [2]", got) + } +} + +func TestClaudeWebSearchResultCachePurgesExpiredBeforeEvictingLiveEntries(t *testing.T) { + now := time.Unix(1_800_000_000, 0) + useClaudeWebSearchResultClock(t, &now) + + CacheClaudeWebSearchResult("s", "short", []byte(`[1]`), 5*time.Minute, 2) + CacheClaudeWebSearchResult("s", "long", []byte(`[2]`), 30*time.Minute, 2) + // Touch "short" so "long" is the LRU tail, then advance past "short"'s TTL. + _, _ = GetClaudeWebSearchResult("s", "short", 5*time.Minute) + now = now.Add(10 * time.Minute) + + CacheClaudeWebSearchResult("s", "new", []byte(`[3]`), 30*time.Minute, 2) + if _, ok := GetClaudeWebSearchResult("s", "long"); !ok { + t.Fatal("live entry was evicted while an expired entry could have been purged") + } + if _, ok := GetClaudeWebSearchResult("s", "new"); !ok { + t.Fatal("new entry missing") + } +} diff --git a/internal/cache/signature_cache.go b/internal/cache/signature_cache.go index 2a6c0a48472..da7186c0a39 100644 --- a/internal/cache/signature_cache.go +++ b/internal/cache/signature_cache.go @@ -113,6 +113,7 @@ func purgeExpiredCaches() { purgeExpiredAntigravityReasoningReplayCache(now) purgeExpiredKimiThinkingReplayCache(now) purgeExpiredClaudeThinkingReplayCache(now) + purgeExpiredClaudeWebSearchResultCache(now) } // CacheSignature stores a thinking signature for a given model group and text. diff --git a/internal/config/client_test.go b/internal/config/client_test.go index c4d6d31f2f7..9dcf08e876d 100644 --- a/internal/config/client_test.go +++ b/internal/config/client_test.go @@ -5,6 +5,7 @@ import ( "os" "path/filepath" "testing" + "time" "gopkg.in/yaml.v3" ) @@ -103,3 +104,159 @@ func TestClientCodexEnableApplyPatchSave(t *testing.T) { } } } + +func TestClientCodexWebSearchResultCache(t *testing.T) { + for _, tc := range []struct { + name string + raw string + want CodexWebSearchResultCacheConfig + wantTTL time.Duration + wantLimit int + }{ + { + name: "omitted", + raw: "config-version: 8\n", + want: CodexWebSearchResultCacheConfig{}, + wantTTL: DefaultCodexWebSearchResultCacheTTL, + wantLimit: DefaultCodexWebSearchResultCacheLimit, + }, + { + name: "enabled with integer seconds ttl", + raw: "client: {codex: {web-search-result-cache: {enabled: true, ttl: 1800, limit: 64}}}\n", + want: CodexWebSearchResultCacheConfig{Enabled: true, TTL: "1800", Limit: 64}, + wantTTL: 30 * time.Minute, + wantLimit: 64, + }, + { + name: "enabled with 30m ttl", + raw: "client: {codex: {web-search-result-cache: {enabled: true, ttl: 30m, limit: 128}}}\n", + want: CodexWebSearchResultCacheConfig{Enabled: true, TTL: "30m", Limit: 128}, + wantTTL: 30 * time.Minute, + wantLimit: 128, + }, + { + name: "disabled with 24h ttl", + raw: "client: {codex: {web-search-result-cache: {enabled: false, ttl: 24h, limit: 1024}}}\n", + want: CodexWebSearchResultCacheConfig{Enabled: false, TTL: "24h", Limit: 1024}, + wantTTL: 24 * time.Hour, + wantLimit: 1024, + }, + { + name: "null fields fall back to defaults", + raw: "client: {codex: {web-search-result-cache: {enabled: null, ttl: null, limit: null}}}\n", + want: CodexWebSearchResultCacheConfig{}, + wantTTL: DefaultCodexWebSearchResultCacheTTL, + wantLimit: DefaultCodexWebSearchResultCacheLimit, + }, + } { + t.Run(tc.name, func(t *testing.T) { + if errValidate := ValidateV8Config([]byte(tc.raw)); errValidate != nil { + t.Fatalf("validate: %v", errValidate) + } + cfg, errParse := ParseConfigBytes([]byte(tc.raw)) + if errParse != nil { + t.Fatalf("parse: %v", errParse) + } + got := cfg.Client.Codex.WebSearchResultCache + if got != tc.want { + t.Fatalf("WebSearchResultCache = %+v, want %+v", got, tc.want) + } + if got.EffectiveTTL() != tc.wantTTL { + t.Fatalf("EffectiveTTL() = %v, want %v", got.EffectiveTTL(), tc.wantTTL) + } + if got.EffectiveLimit() != tc.wantLimit { + t.Fatalf("EffectiveLimit() = %d, want %d", got.EffectiveLimit(), tc.wantLimit) + } + var sdkCfg SDKConfig + if errUnmarshal := yaml.Unmarshal([]byte(tc.raw), &sdkCfg); errUnmarshal != nil { + t.Fatalf("decode SDK config: %v", errUnmarshal) + } + if sdkCfg.Client.Codex.WebSearchResultCache != tc.want { + t.Fatalf("SDK WebSearchResultCache = %+v, want %+v", sdkCfg.Client.Codex.WebSearchResultCache, tc.want) + } + data, errMarshal := json.Marshal(cfg) + if errMarshal != nil { + t.Fatalf("encode JSON: %v", errMarshal) + } + var decoded Config + if errUnmarshal := json.Unmarshal(data, &decoded); errUnmarshal != nil { + t.Fatalf("decode JSON: %v", errUnmarshal) + } + if decoded.Client.Codex.WebSearchResultCache != tc.want { + t.Fatalf("JSON round trip WebSearchResultCache = %+v, want %+v", decoded.Client.Codex.WebSearchResultCache, tc.want) + } + migrated, _, errNormalize := NormalizeConfigLayout([]byte(tc.raw), true) + if errNormalize != nil { + t.Fatalf("normalize: %v", errNormalize) + } + if errValidate := ValidateV8Config(migrated); errValidate != nil { + t.Fatalf("validate normalized config: %v", errValidate) + } + if errUnmarshal := yaml.Unmarshal(migrated, &decoded); errUnmarshal != nil { + t.Fatalf("decode normalized config: %v", errUnmarshal) + } + if decoded.Client.Codex.WebSearchResultCache != tc.want { + t.Fatalf("normalized WebSearchResultCache = %+v, want %+v", decoded.Client.Codex.WebSearchResultCache, tc.want) + } + }) + } + + var fromJSONNumber Config + if errUnmarshal := json.Unmarshal([]byte(`{"client":{"codex":{"web-search-result-cache":{"enabled":true,"ttl":1800,"limit":256}}}}`), &fromJSONNumber); errUnmarshal != nil { + t.Fatalf("unmarshal JSON number ttl: %v", errUnmarshal) + } + if fromJSONNumber.Client.Codex.WebSearchResultCache.EffectiveTTL() != 30*time.Minute || fromJSONNumber.Client.Codex.WebSearchResultCache.EffectiveLimit() != 256 { + t.Fatalf("JSON number ttl config = %+v", fromJSONNumber.Client.Codex.WebSearchResultCache) + } + + for _, raw := range []string{ + "client: {codex: {web-search-result-cache: false}}\n", + "client: {codex: {web-search-result-cache: {unknown: true}}}\n", + "client: {codex: {web-search-result-cache: {enabled: invalid}}}\n", + "client: {codex: {web-search-result-cache: {ttl: invalid}}}\n", + "client: {codex: {web-search-result-cache: {ttl: -30m}}}\n", + "client: {codex: {web-search-result-cache: {ttl: -10}}}\n", + "client: {codex: {web-search-result-cache: {ttl: true}}}\n", + "client: {codex: {web-search-result-cache: {limit: -1}}}\n", + "client: {codex: {web-search-result-cache: {limit: invalid}}}\n", + } { + if errValidate := ValidateV8Config([]byte(raw)); errValidate == nil { + t.Fatalf("accepted invalid config %q", raw) + } + if _, errParse := ParseConfigBytes([]byte(raw)); errParse == nil { + t.Fatalf("ParseConfigBytes accepted invalid config %q", raw) + } + } +} + +func TestClientCodexWebSearchResultCacheSave(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.yaml") + if errWrite := os.WriteFile(path, []byte("config-version: 8\n"), 0600); errWrite != nil { + t.Fatalf("write: %v", errWrite) + } + cfg := &Config{} + for _, want := range []CodexWebSearchResultCacheConfig{ + {Enabled: true, TTL: "30m", Limit: 128}, + {Enabled: false, TTL: "24h", Limit: 1024}, + {Enabled: false, TTL: "", Limit: 0}, + } { + cfg.Client.Codex.WebSearchResultCache = want + if errSave := SaveConfigPreserveComments(path, cfg, true); errSave != nil { + t.Fatalf("save: %v", errSave) + } + data, errRead := os.ReadFile(path) + if errRead != nil { + t.Fatalf("read: %v", errRead) + } + if errValidate := ValidateV8Config(data); errValidate != nil { + t.Fatalf("validate saved config: %v", errValidate) + } + loaded, errLoad := LoadConfig(path) + if errLoad != nil { + t.Fatalf("load: %v", errLoad) + } + if loaded.Client.Codex.WebSearchResultCache != want { + t.Fatalf("saved WebSearchResultCache = %+v, want %+v", loaded.Client.Codex.WebSearchResultCache, want) + } + } +} diff --git a/internal/config/sdk_config.go b/internal/config/sdk_config.go index fa057c08445..1c52dc2b8ef 100644 --- a/internal/config/sdk_config.go +++ b/internal/config/sdk_config.go @@ -5,8 +5,15 @@ package config import ( + "bytes" + "encoding/json" + "fmt" "math" + "strconv" + "strings" "time" + + "gopkg.in/yaml.v3" ) // SDKConfig represents the application's configuration, loaded from a YAML file. @@ -126,6 +133,214 @@ type CodexClientConfig struct { // EnableApplyPatch advertises freeform apply_patch only for supported models. // Default false clears the capability regardless of template metadata. EnableApplyPatch bool `yaml:"enable-apply-patch" json:"enable-apply-patch"` + + // WebSearchResultCache configures caching of upstream web search results for + // Codex Responses clients that drop search results from replayed history. + WebSearchResultCache CodexWebSearchResultCacheConfig `yaml:"web-search-result-cache" json:"web-search-result-cache"` +} + +const ( + // DefaultCodexWebSearchResultCacheTTL is the default lifetime of cached web search results. + DefaultCodexWebSearchResultCacheTTL = 24 * time.Hour + + // DefaultCodexWebSearchResultCacheLimit is the default maximum number of cached web search result entries. + DefaultCodexWebSearchResultCacheLimit = 1024 + + maxCodexWebSearchResultCacheTTLSeconds = int64(math.MaxInt64 / time.Second) +) + +// CodexWebSearchResultCacheConfig configures the session-scoped web search result cache. +type CodexWebSearchResultCacheConfig struct { + // Enabled controls whether web search results are cached and restored for Codex Responses requests. + // Default is false (disabled). + Enabled bool `yaml:"enabled" json:"enabled"` + + // TTL specifies how long cached web search results remain valid (e.g. 1800, "30m", "24h"). + // Pure numbers are interpreted as seconds. Defaults to "24h" when unset. + TTL string `yaml:"ttl" json:"ttl"` + + // Limit specifies the maximum number of entries retained in the web search result cache. + // Defaults to 1024 when unset or zero. + Limit int `yaml:"limit" json:"limit"` +} + +// Validate checks that the configured TTL and entry limit are valid. +func (c CodexWebSearchResultCacheConfig) Validate() error { + if _, err := parseCodexWebSearchResultCacheTTL(c.TTL); err != nil { + return err + } + if c.Limit < 0 { + return fmt.Errorf("client.codex.web-search-result-cache.limit must not be negative") + } + return nil +} + +// EffectiveTTL returns the parsed cache TTL, or DefaultCodexWebSearchResultCacheTTL when unset or zero. +func (c CodexWebSearchResultCacheConfig) EffectiveTTL() time.Duration { + d, err := parseCodexWebSearchResultCacheTTL(c.TTL) + if err != nil || d <= 0 { + return DefaultCodexWebSearchResultCacheTTL + } + return d +} + +// EffectiveLimit returns the configured entry limit, or DefaultCodexWebSearchResultCacheLimit when unset or non-positive. +func (c CodexWebSearchResultCacheConfig) EffectiveLimit() int { + if c.Limit > 0 { + return c.Limit + } + return DefaultCodexWebSearchResultCacheLimit +} + +func (c *CodexWebSearchResultCacheConfig) UnmarshalYAML(value *yaml.Node) error { + if c == nil { + return nil + } + if value == nil || value.Kind == 0 || value.Tag == "!!null" { + *c = CodexWebSearchResultCacheConfig{} + return nil + } + if value.Kind != yaml.MappingNode { + return fmt.Errorf("client.codex.web-search-result-cache must be a mapping") + } + var next CodexWebSearchResultCacheConfig + seen := make(map[string]bool, len(value.Content)/2) + for i := 0; i+1 < len(value.Content); i += 2 { + keyNode := value.Content[i] + valNode := value.Content[i+1] + if keyNode == nil || valNode == nil { + continue + } + if seen[keyNode.Value] { + return fmt.Errorf("client.codex.web-search-result-cache: duplicate field %q", keyNode.Value) + } + seen[keyNode.Value] = true + switch keyNode.Value { + case "enabled": + if valNode.Tag == "!!null" { + next.Enabled = false + continue + } + if valNode.Kind != yaml.ScalarNode || valNode.Tag != "!!bool" { + return fmt.Errorf("client.codex.web-search-result-cache.enabled must be a boolean") + } + var enabled bool + if errDecode := valNode.Decode(&enabled); errDecode != nil { + return fmt.Errorf("client.codex.web-search-result-cache.enabled: %w", errDecode) + } + next.Enabled = enabled + case "ttl": + if valNode.Tag == "!!null" { + next.TTL = "" + continue + } + if valNode.Kind != yaml.ScalarNode || (valNode.Tag != "!!str" && valNode.Tag != "!!int") { + return fmt.Errorf("client.codex.web-search-result-cache.ttl must be a duration string or integer seconds") + } + next.TTL = strings.TrimSpace(valNode.Value) + case "limit": + if valNode.Tag == "!!null" { + next.Limit = 0 + continue + } + if valNode.Kind != yaml.ScalarNode || valNode.Tag != "!!int" { + return fmt.Errorf("client.codex.web-search-result-cache.limit must be an integer") + } + var limit int + if errDecode := valNode.Decode(&limit); errDecode != nil { + return fmt.Errorf("client.codex.web-search-result-cache.limit: %w", errDecode) + } + next.Limit = limit + default: + return fmt.Errorf("client.codex.web-search-result-cache: unknown field %q", keyNode.Value) + } + } + if errValidate := next.Validate(); errValidate != nil { + return errValidate + } + *c = next + return nil +} + +func (c *CodexWebSearchResultCacheConfig) UnmarshalJSON(data []byte) error { + if c == nil { + return nil + } + trimmed := bytes.TrimSpace(data) + if len(trimmed) == 0 || bytes.Equal(trimmed, []byte("null")) { + *c = CodexWebSearchResultCacheConfig{} + return nil + } + var raw struct { + Enabled bool `json:"enabled"` + TTL json.RawMessage `json:"ttl"` + Limit int `json:"limit"` + } + decoder := json.NewDecoder(bytes.NewReader(trimmed)) + decoder.DisallowUnknownFields() + if errDecode := decoder.Decode(&raw); errDecode != nil { + return errDecode + } + ttl, errTTL := parseCodexWebSearchResultCacheTTLJSON(raw.TTL) + if errTTL != nil { + return errTTL + } + next := CodexWebSearchResultCacheConfig{ + Enabled: raw.Enabled, + TTL: ttl, + Limit: raw.Limit, + } + if errValidate := next.Validate(); errValidate != nil { + return errValidate + } + *c = next + return nil +} + +func parseCodexWebSearchResultCacheTTLJSON(raw json.RawMessage) (string, error) { + trimmed := bytes.TrimSpace(raw) + if len(trimmed) == 0 || bytes.Equal(trimmed, []byte("null")) { + return "", nil + } + if trimmed[0] == '"' { + var s string + if errUnmarshal := json.Unmarshal(trimmed, &s); errUnmarshal != nil { + return "", fmt.Errorf("client.codex.web-search-result-cache.ttl: %w", errUnmarshal) + } + return strings.TrimSpace(s), nil + } + var n int64 + if errUnmarshal := json.Unmarshal(trimmed, &n); errUnmarshal != nil { + return "", fmt.Errorf("client.codex.web-search-result-cache.ttl must be a duration string or integer seconds") + } + return strconv.FormatInt(n, 10), nil +} + +func parseCodexWebSearchResultCacheTTL(raw string) (time.Duration, error) { + raw = strings.TrimSpace(raw) + if raw == "" || raw == "0" { + return DefaultCodexWebSearchResultCacheTTL, nil + } + if secs, errParseInt := strconv.ParseInt(raw, 10, 64); errParseInt == nil { + if secs < 0 { + return 0, fmt.Errorf("client.codex.web-search-result-cache.ttl must not be negative") + } + if secs > maxCodexWebSearchResultCacheTTLSeconds { + return 0, fmt.Errorf("client.codex.web-search-result-cache.ttl overflows duration") + } + return time.Duration(secs) * time.Second, nil + } + d, errParseDuration := time.ParseDuration(raw) + if errParseDuration != nil { + return 0, fmt.Errorf("client.codex.web-search-result-cache.ttl %q is invalid: %w", raw, errParseDuration) + } + if d < 0 { + return 0, fmt.Errorf("client.codex.web-search-result-cache.ttl must not be negative") + } + if d == 0 { + return DefaultCodexWebSearchResultCacheTTL, nil + } + return d, nil } // ClaudeCodeConfig configures Claude Code compatibility behavior. diff --git a/internal/runtime/executor/claude_executor.go b/internal/runtime/executor/claude_executor.go index c4b2776a1fa..3643a719cf4 100644 --- a/internal/runtime/executor/claude_executor.go +++ b/internal/runtime/executor/claude_executor.go @@ -7,6 +7,7 @@ import ( "fmt" "net/http" "strings" + "time" "github.com/router-for-me/CLIProxyAPI/v8/internal/config" "github.com/router-for-me/CLIProxyAPI/v8/internal/runtime/executor/helps" @@ -188,6 +189,21 @@ func NewClaudeExecutor(cfg *config.Config) *ClaudeExecutor { func (e *ClaudeExecutor) Identifier() string { return "claude" } +func (e *ClaudeExecutor) claudeWebSearchResultCacheScope(ctx context.Context, from sdktranslator.Format, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, originalPayload []byte) (string, time.Duration, int) { + if e == nil || e.cfg == nil || !e.cfg.Client.Codex.WebSearchResultCache.Enabled { + return "", 0, 0 + } + if !sourceFormatEqual(from, sdktranslator.FormatOpenAIResponse) { + return "", 0, 0 + } + sessionKey := xaiReasoningReplayIsolateSessionKey(ctx, codexReasoningReplaySessionKey(ctx, from, req, opts, originalPayload)) + if sessionKey == "" { + return "", 0, 0 + } + cacheCfg := e.cfg.Client.Codex.WebSearchResultCache + return sessionKey, cacheCfg.EffectiveTTL(), cacheCfg.EffectiveLimit() +} + var _ cliproxyauth.NativeToolSearchSupport = (*ClaudeExecutor)(nil) // SupportsNativeToolSearch reports whether a credential can receive Codex diff --git a/internal/runtime/executor/claude_executor_execute.go b/internal/runtime/executor/claude_executor_execute.go index 928fc392226..b6803635b8a 100644 --- a/internal/runtime/executor/claude_executor_execute.go +++ b/internal/runtime/executor/claude_executor_execute.go @@ -90,6 +90,8 @@ func (e *ClaudeExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, r if err != nil { return resp, err } + webSearchSessionKey, webSearchTTL, webSearchLimit := e.claudeWebSearchResultCacheScope(ctx, from, req, opts, originalPayload) + body = helps.RestoreClaudeWebSearchResults(body, webSearchSessionKey, webSearchTTL) if rebuildMidSystemMessageEnabled(e.cfg, auth) { body = rebuildMidSystemMessagesToTopLevel(body) } @@ -394,6 +396,7 @@ func (e *ClaudeExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, r } } data = e.restoreResponseModel(data, req.Model) + helps.RecordClaudeWebSearchResults(webSearchSessionKey, data, webSearchTTL, webSearchLimit) cacheClaudeThinkingReplayResponse(ctx, replayScope, data) var param any out := sdktranslator.TranslateNonStream( diff --git a/internal/runtime/executor/claude_executor_stream.go b/internal/runtime/executor/claude_executor_stream.go index 74cf32303f7..594012027ed 100644 --- a/internal/runtime/executor/claude_executor_stream.go +++ b/internal/runtime/executor/claude_executor_stream.go @@ -93,6 +93,8 @@ func (e *ClaudeExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A if err != nil { return nil, err } + webSearchSessionKey, webSearchTTL, webSearchLimit := e.claudeWebSearchResultCacheScope(ctx, from, req, opts, originalPayload) + body = helps.RestoreClaudeWebSearchResults(body, webSearchSessionKey, webSearchTTL) if rebuildMidSystemMessageEnabled(e.cfg, auth) { body = rebuildMidSystemMessagesToTopLevel(body) } @@ -474,6 +476,7 @@ func (e *ClaudeExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A return } line = e.restoreResponseModel(restoredLine, req.Model) + helps.RecordClaudeWebSearchResults(webSearchSessionKey, line, webSearchTTL, webSearchLimit) chunks := sdktranslator.TranslateStream( ctx, to, diff --git a/internal/runtime/executor/claude_executor_tokens.go b/internal/runtime/executor/claude_executor_tokens.go index a474ce7c05c..fc5011d260a 100644 --- a/internal/runtime/executor/claude_executor_tokens.go +++ b/internal/runtime/executor/claude_executor_tokens.go @@ -52,6 +52,12 @@ func (e *ClaudeExecutor) CountTokens(ctx context.Context, auth *cliproxyauth.Aut if errThinking != nil { return cliproxyexecutor.Response{}, errThinking } + originalPayload := req.Payload + if len(opts.OriginalRequest) > 0 { + originalPayload = opts.OriginalRequest + } + webSearchSessionKey, webSearchTTL, _ := e.claudeWebSearchResultCacheScope(ctx, from, req, opts, originalPayload) + body = helps.RestoreClaudeWebSearchResults(body, webSearchSessionKey, webSearchTTL) if rebuildMidSystemMessageEnabled(e.cfg, auth) { body = rebuildMidSystemMessagesToTopLevel(body) } @@ -169,6 +175,8 @@ func (e *ClaudeExecutor) countTokensUpstream(ctx context.Context, auth *cliproxy if errThinking != nil { return cliproxyexecutor.Response{}, errThinking } + webSearchSessionKey, webSearchTTL, _ := e.claudeWebSearchResultCacheScope(ctx, from, req, opts, originalPayload) + body = helps.RestoreClaudeWebSearchResults(body, webSearchSessionKey, webSearchTTL) if rebuildMidSystemMessageEnabled(e.cfg, auth) { body = rebuildMidSystemMessagesToTopLevel(body) } diff --git a/internal/runtime/executor/claude_executor_web_search_results_test.go b/internal/runtime/executor/claude_executor_web_search_results_test.go new file mode 100644 index 00000000000..1377b8d6aa2 --- /dev/null +++ b/internal/runtime/executor/claude_executor_web_search_results_test.go @@ -0,0 +1,325 @@ +package executor + +import ( + "context" + "io" + "net/http" + "net/http/httptest" + "strings" + "sync" + "testing" + + "github.com/router-for-me/CLIProxyAPI/v8/internal/cache" + "github.com/router-for-me/CLIProxyAPI/v8/internal/config" + cliproxyauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth" + cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor" + sdktranslator "github.com/router-for-me/CLIProxyAPI/v8/sdk/translator" + "github.com/tidwall/gjson" +) + +const claudeWebSearchExecutorResults = `[{"type":"web_search_result","url":"https://example.com/a","title":"A","encrypted_content":"enc-a","page_age":null},{"type":"web_search_result","url":"https://example.com/b","title":"B","encrypted_content":"enc-b","page_age":null}]` + +// claudeWebSearchUpstream answers the first request with a native web search +// and every later one with plain text, recording each request body. +func claudeWebSearchUpstream(t *testing.T, bodies *[][]byte, mu *sync.Mutex) http.Handler { + t.Helper() + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + body, errRead := io.ReadAll(r.Body) + if errRead != nil { + t.Errorf("read upstream body: %v", errRead) + http.Error(w, "read", http.StatusBadRequest) + return + } + mu.Lock() + *bodies = append(*bodies, body) + mu.Unlock() + + blocks := []string{`{"type":"text","text":"done"}`} + if !strings.Contains(string(body), `"web_search_tool_result"`) { + blocks = []string{ + `{"type":"server_tool_use","id":"srvtoolu_01abc","name":"web_search","input":{"query":"q"}}`, + `{"type":"web_search_tool_result","tool_use_id":"srvtoolu_01abc","content":` + claudeWebSearchExecutorResults + `}`, + `{"type":"text","text":"found two pages"}`, + } + } + if !gjson.GetBytes(body, "stream").Bool() { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"id":"msg_ws","type":"message","role":"assistant","model":"claude-opus-5-5","content":[` + strings.Join(blocks, ",") + `],"stop_reason":"end_turn","usage":{"input_tokens":5,"output_tokens":5}}`)) + return + } + events := []string{ + "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":\"msg_ws\",\"type\":\"message\",\"role\":\"assistant\",\"model\":\"claude-opus-5-5\",\"content\":[],\"usage\":{\"input_tokens\":5,\"output_tokens\":0}}}", + } + for i, block := range blocks { + index := string(rune('0' + i)) + start := block + if strings.Contains(block, `"type":"text"`) { + start = `{"type":"text","text":""}` + } + events = append(events, "event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":"+index+",\"content_block\":"+start+"}") + if strings.Contains(block, `"type":"text"`) { + events = append(events, "event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":"+index+",\"delta\":{\"type\":\"text_delta\",\"text\":"+gjson.Get(block, "text").Raw+"}}") + } + events = append(events, "event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":"+index+"}") + } + events = append(events, + "event: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"end_turn\"},\"usage\":{\"output_tokens\":5}}", + "event: message_stop\ndata: {\"type\":\"message_stop\"}", + ) + w.Header().Set("Content-Type", "text/event-stream") + _, _ = w.Write([]byte(strings.Join(events, "\n\n") + "\n\n")) + }) +} + +type claudeWebSearchExecutorRun struct { + sessionID string + input string +} + +func runClaudeWebSearchScenario(t *testing.T, cfg *config.Config, stream bool, runs []claudeWebSearchExecutorRun) [][]byte { + t.Helper() + cache.ClearClaudeWebSearchResultCache() + t.Cleanup(cache.ClearClaudeWebSearchResultCache) + var ( + bodies [][]byte + mu sync.Mutex + ) + server := httptest.NewServer(claudeWebSearchUpstream(t, &bodies, &mu)) + defer server.Close() + + executor := NewClaudeExecutor(cfg) + auth := &cliproxyauth.Auth{Attributes: map[string]string{"api_key": "key-web-search", "base_url": server.URL}} + streamJSON := "false" + if stream { + streamJSON = "true" + } + for _, run := range runs { + payload := []byte(`{"model":"claude-opus-5-5","stream":` + streamJSON + `,"input":[` + run.input + `]}`) + req := cliproxyexecutor.Request{Model: "claude-opus-5-5", Payload: payload} + opts := cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FormatOpenAIResponse, + ResponseFormat: sdktranslator.FormatOpenAIResponse, + Stream: stream, + OriginalRequest: payload, + } + if run.sessionID != "" { + opts.Metadata = map[string]any{cliproxyexecutor.ExecutionSessionMetadataKey: run.sessionID} + } + if !stream { + if _, errExecute := executor.Execute(context.Background(), auth, req, opts); errExecute != nil { + t.Fatalf("Execute() error = %v", errExecute) + } + continue + } + result, errStream := executor.ExecuteStream(context.Background(), auth, req, opts) + if errStream != nil { + t.Fatalf("ExecuteStream() error = %v", errStream) + } + for chunk := range result.Chunks { + if chunk.Err != nil { + t.Fatalf("chunk error: %v", chunk.Err) + } + } + } + mu.Lock() + defer mu.Unlock() + return append([][]byte(nil), bodies...) +} + +const ( + claudeWebSearchExecutorUser = `{"type":"message","role":"user","content":[{"type":"input_text","text":"search for q"}]}` + // The history as Codex stores it: the web_search_call has no results. + claudeWebSearchExecutorHistory = claudeWebSearchExecutorUser + + `,{"type":"web_search_call","id":"ws_srvtoolu_01abc","status":"completed","action":{"type":"search","query":"q"}},` + + `{"type":"message","role":"assistant","content":[{"type":"output_text","text":"found two pages"}]},` + + `{"type":"message","role":"user","content":[{"type":"input_text","text":"how many results?"}]}` +) + +func enabledClaudeWebSearchCacheConfig() *config.Config { + return &config.Config{ + SDKConfig: config.SDKConfig{ + Client: config.ClientConfig{ + Codex: config.CodexClientConfig{ + WebSearchResultCache: config.CodexWebSearchResultCacheConfig{ + Enabled: true, + TTL: "30m", + Limit: 16, + }, + }, + }, + }, + } +} + +func TestClaudeExecutor_RestoresWebSearchResultsDroppedByResponsesClient(t *testing.T) { + cfg, errParse := config.ParseConfigBytes([]byte("client:\n codex:\n web-search-result-cache:\n enabled: true\n ttl: 30m\n limit: 16\n")) + if errParse != nil { + t.Fatalf("ParseConfigBytes() error = %v", errParse) + } + for _, stream := range []bool{false, true} { + t.Run(map[bool]string{false: "non-stream", true: "stream"}[stream], func(t *testing.T) { + bodies := runClaudeWebSearchScenario(t, cfg, stream, []claudeWebSearchExecutorRun{ + {sessionID: "session-ws", input: claudeWebSearchExecutorUser}, + {sessionID: "session-ws", input: claudeWebSearchExecutorHistory}, + {sessionID: "other-session", input: claudeWebSearchExecutorHistory}, + {sessionID: "", input: claudeWebSearchExecutorHistory}, + }) + if len(bodies) != 4 { + t.Fatalf("upstream requests = %d, want 4", len(bodies)) + } + if got := claudeWebSearchResultContent(t, bodies[1]); got != claudeWebSearchExecutorResults { + t.Fatalf("same session: web_search_tool_result content = %s, want original results", got) + } + if got := claudeWebSearchResultContent(t, bodies[2]); got != "[]" { + t.Fatalf("other session: web_search_tool_result content = %s, want []", got) + } + if got := claudeWebSearchResultContent(t, bodies[3]); got != "[]" { + t.Fatalf("no session identity: web_search_tool_result content = %s, want []", got) + } + }) + } +} + +func TestClaudeExecutor_WebSearchResultCacheDisabledByDefault(t *testing.T) { + for _, stream := range []bool{false, true} { + t.Run(map[bool]string{false: "non-stream", true: "stream"}[stream], func(t *testing.T) { + bodies := runClaudeWebSearchScenario(t, &config.Config{}, stream, []claudeWebSearchExecutorRun{ + {sessionID: "session-ws", input: claudeWebSearchExecutorUser}, + {sessionID: "session-ws", input: claudeWebSearchExecutorHistory}, + }) + if len(bodies) != 2 { + t.Fatalf("upstream requests = %d, want 2", len(bodies)) + } + if got := claudeWebSearchResultContent(t, bodies[1]); got != "[]" { + t.Fatalf("disabled cache: web_search_tool_result content = %s, want []", got) + } + }) + } +} + +// User payload rules stay the final barrier: a filter rule that removes the +// result content must not be undone by the restore. +func TestClaudeExecutor_WebSearchResultRestoreRunsBeforePayloadRules(t *testing.T) { + cfg := enabledClaudeWebSearchCacheConfig() + cfg.Payload = config.PayloadConfig{Filter: []config.PayloadFilterRule{{ + Models: []config.PayloadModelRule{{Name: "claude-opus-5-5"}}, + Params: []string{"messages.1.content.1.content"}, + }}} + for _, stream := range []bool{false, true} { + t.Run(map[bool]string{false: "non-stream", true: "stream"}[stream], func(t *testing.T) { + bodies := runClaudeWebSearchScenario(t, cfg, stream, []claudeWebSearchExecutorRun{ + {sessionID: "session-ws", input: claudeWebSearchExecutorUser}, + {sessionID: "session-ws", input: claudeWebSearchExecutorHistory}, + }) + if len(bodies) != 2 { + t.Fatalf("upstream requests = %d, want 2", len(bodies)) + } + block := gjson.GetBytes(bodies[1], "messages.1.content.1") + if block.Get("type").String() != "web_search_tool_result" { + t.Fatalf("messages.1.content.1 = %s, want the web_search_tool_result block", block.Raw) + } + if content := block.Get("content"); content.Exists() { + t.Fatalf("filtered content came back: %s", content.Raw) + } + }) + } +} + +func TestClaudeExecutor_WebSearchResultCacheHonorsConfiguredLimit(t *testing.T) { + cfg := &config.Config{ + SDKConfig: config.SDKConfig{ + Client: config.ClientConfig{ + Codex: config.CodexClientConfig{ + WebSearchResultCache: config.CodexWebSearchResultCacheConfig{ + Enabled: true, + TTL: "1800", + Limit: 1, + }, + }, + }, + }, + } + bodies := runClaudeWebSearchScenario(t, cfg, false, []claudeWebSearchExecutorRun{ + {sessionID: "session-1", input: claudeWebSearchExecutorUser}, + {sessionID: "session-2", input: claudeWebSearchExecutorUser}, + {sessionID: "session-1", input: claudeWebSearchExecutorHistory}, + {sessionID: "session-2", input: claudeWebSearchExecutorHistory}, + }) + if len(bodies) != 4 { + t.Fatalf("upstream requests = %d, want 4", len(bodies)) + } + // Limit is 1, so session-2's search result evicted session-1's entry. + if got := claudeWebSearchResultContent(t, bodies[2]); got != "[]" { + t.Fatalf("evicted session-1: web_search_tool_result content = %s, want []", got) + } + if got := claudeWebSearchResultContent(t, bodies[3]); got != claudeWebSearchExecutorResults { + t.Fatalf("retained session-2: web_search_tool_result content = %s, want original results", got) + } +} + +func TestClaudeExecutor_CountTokensRestoresWebSearchResultsWhenEnabled(t *testing.T) { + cache.ClearClaudeWebSearchResultCache() + t.Cleanup(cache.ClearClaudeWebSearchResultCache) + + var bodies [][]byte + var mu sync.Mutex + server := httptest.NewServer(claudeWebSearchUpstream(t, &bodies, &mu)) + defer server.Close() + + auth := &cliproxyauth.Auth{Attributes: map[string]string{"api_key": "key-web-search", "base_url": server.URL}} + enabledExec := NewClaudeExecutor(enabledClaudeWebSearchCacheConfig()) + disabledExec := NewClaudeExecutor(&config.Config{}) + + firstPayload := []byte(`{"model":"claude-opus-5-5","stream":false,"input":[` + claudeWebSearchExecutorUser + `]}`) + firstReq := cliproxyexecutor.Request{Model: "claude-opus-5-5", Payload: firstPayload} + firstOpts := cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FormatOpenAIResponse, + ResponseFormat: sdktranslator.FormatOpenAIResponse, + OriginalRequest: firstPayload, + Metadata: map[string]any{cliproxyexecutor.ExecutionSessionMetadataKey: "session-count"}, + } + if _, errExec := enabledExec.Execute(context.Background(), auth, firstReq, firstOpts); errExec != nil { + t.Fatalf("Execute() error = %v", errExec) + } + + historyPayload := []byte(`{"model":"claude-opus-5-5","stream":false,"input":[` + claudeWebSearchExecutorHistory + `]}`) + historyReq := cliproxyexecutor.Request{Model: "claude-opus-5-5", Payload: historyPayload} + historyOpts := cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FormatOpenAIResponse, + ResponseFormat: sdktranslator.FormatClaude, + OriginalRequest: historyPayload, + Metadata: map[string]any{cliproxyexecutor.ExecutionSessionMetadataKey: "session-count"}, + } + enabledCountResp, errEnabledCount := enabledExec.CountTokens(context.Background(), auth, historyReq, historyOpts) + if errEnabledCount != nil { + t.Fatalf("enabled CountTokens() error = %v", errEnabledCount) + } + disabledCountResp, errDisabledCount := disabledExec.CountTokens(context.Background(), auth, historyReq, historyOpts) + if errDisabledCount != nil { + t.Fatalf("disabled CountTokens() error = %v", errDisabledCount) + } + enabledTokens := gjson.GetBytes(enabledCountResp.Payload, "input_tokens").Int() + disabledTokens := gjson.GetBytes(disabledCountResp.Payload, "input_tokens").Int() + if enabledTokens <= disabledTokens { + t.Fatalf("enabled input_tokens (%d) <= disabled input_tokens (%d); want restored search results counted", enabledTokens, disabledTokens) + } +} + +func claudeWebSearchResultContent(t *testing.T, body []byte) string { + t.Helper() + var found string + gjson.GetBytes(body, "messages").ForEach(func(_, message gjson.Result) bool { + message.Get("content").ForEach(func(_, block gjson.Result) bool { + if block.Get("type").String() == "web_search_tool_result" { + found = block.Get("content").Raw + return false + } + return true + }) + return found == "" + }) + if found == "" { + t.Fatalf("no web_search_tool_result block in upstream body: %s", body) + } + return found +} diff --git a/internal/runtime/executor/helps/claude_web_search_results.go b/internal/runtime/executor/helps/claude_web_search_results.go new file mode 100644 index 00000000000..27179ffe6d6 --- /dev/null +++ b/internal/runtime/executor/helps/claude_web_search_results.go @@ -0,0 +1,100 @@ +package helps + +import ( + "bytes" + "fmt" + "strings" + "time" + + "github.com/router-for-me/CLIProxyAPI/v8/internal/cache" + log "github.com/sirupsen/logrus" + "github.com/tidwall/gjson" + "github.com/tidwall/sjson" +) + +var claudeWebSearchToolResultMarker = []byte(`"web_search_tool_result"`) + +// RecordClaudeWebSearchResults stores the web_search_tool_result blocks of a +// Claude response for sessionKey. data is either a complete Messages response +// or SSE text (one or more lines); the result block arrives whole in its +// content_block_start event. +func RecordClaudeWebSearchResults(sessionKey string, data []byte, ttl time.Duration, limit int) { + if strings.TrimSpace(sessionKey) == "" || !bytes.Contains(data, claudeWebSearchToolResultMarker) { + return + } + if content := gjson.GetBytes(data, "content"); content.IsArray() { + content.ForEach(func(_, block gjson.Result) bool { + recordClaudeWebSearchResultBlock(sessionKey, block, ttl, limit) + return true + }) + return + } + for _, line := range bytes.Split(data, []byte("\n")) { + line = bytes.TrimSpace(line) + if !bytes.HasPrefix(line, []byte("data:")) { + continue + } + event := gjson.ParseBytes(bytes.TrimSpace(line[len("data:"):])) + if event.Get("type").String() == "content_block_start" { + recordClaudeWebSearchResultBlock(sessionKey, event.Get("content_block"), ttl, limit) + } + } +} + +func recordClaudeWebSearchResultBlock(sessionKey string, block gjson.Result, ttl time.Duration, limit int) { + if block.Get("type").String() != "web_search_tool_result" { + return + } + content := block.Get("content") + if !content.Exists() || claudeWebSearchResultContentEmpty(content) { + return + } + cache.CacheClaudeWebSearchResult(sessionKey, block.Get("tool_use_id").String(), []byte(content.Raw), ttl, limit) +} + +// RestoreClaudeWebSearchResults fills assistant web_search_tool_result blocks +// whose content is missing or empty with the content stored for sessionKey. +// Blocks that already carry content, and blocks without a stored entry, are +// left unchanged. +func RestoreClaudeWebSearchResults(body []byte, sessionKey string, ttl ...time.Duration) []byte { + if strings.TrimSpace(sessionKey) == "" || !bytes.Contains(body, claudeWebSearchToolResultMarker) { + return body + } + messages := gjson.GetBytes(body, "messages") + if !messages.IsArray() { + return body + } + restored := 0 + for i, message := range messages.Array() { + if message.Get("role").String() != "assistant" { + continue + } + for j, block := range message.Get("content").Array() { + if block.Get("type").String() != "web_search_tool_result" || !claudeWebSearchResultContentEmpty(block.Get("content")) { + continue + } + cached, ok := cache.GetClaudeWebSearchResult(sessionKey, block.Get("tool_use_id").String(), ttl...) + if !ok { + continue + } + updated, errSet := sjson.SetRawBytes(body, fmt.Sprintf("messages.%d.content.%d.content", i, j), cached) + if errSet != nil { + log.Debugf("claude web search results: restore messages.%d.content.%d failed: %v", i, j, errSet) + continue + } + body = updated + restored++ + } + } + if restored > 0 { + log.Debugf("claude web search results: restored %d result block(s) from cache", restored) + } + return body +} + +func claudeWebSearchResultContentEmpty(content gjson.Result) bool { + if !content.Exists() || content.Type == gjson.Null { + return true + } + return content.IsArray() && len(content.Array()) == 0 +} diff --git a/internal/runtime/executor/helps/claude_web_search_results_test.go b/internal/runtime/executor/helps/claude_web_search_results_test.go new file mode 100644 index 00000000000..f100e5679c5 --- /dev/null +++ b/internal/runtime/executor/helps/claude_web_search_results_test.go @@ -0,0 +1,85 @@ +package helps + +import ( + "testing" + "time" + + "github.com/router-for-me/CLIProxyAPI/v8/internal/cache" + "github.com/tidwall/gjson" +) + +const claudeWebSearchTestResults = `[{"type":"web_search_result","url":"https://example.com/a","title":"A","encrypted_content":"enc-a","page_age":null},{"type":"web_search_result","url":"https://example.com/b","title":"B","encrypted_content":"enc-b","page_age":null}]` + +func resetClaudeWebSearchResults(t *testing.T) { + t.Helper() + cache.ClearClaudeWebSearchResultCache() + t.Cleanup(cache.ClearClaudeWebSearchResultCache) +} + +func claudeWebSearchRequestBody(resultContent string) []byte { + return []byte(`{"model":"claude-opus-5-5","messages":[` + + `{"role":"user","content":[{"type":"text","text":"search"}]},` + + `{"role":"assistant","content":[` + + `{"type":"server_tool_use","id":"srvtoolu_1","name":"web_search","input":{"query":"q"}},` + + `{"type":"web_search_tool_result","tool_use_id":"srvtoolu_1","content":` + resultContent + `},` + + `{"type":"text","text":"answer"}]},` + + `{"role":"user","content":[{"type":"text","text":"how many results?"}]}]}`) +} + +func TestRecordClaudeWebSearchResultsFromStreamEventAndRestore(t *testing.T) { + resetClaudeWebSearchResults(t) + event := []byte(`data: {"type":"content_block_start","index":1,"content_block":{"type":"web_search_tool_result","tool_use_id":"srvtoolu_1","content":` + claudeWebSearchTestResults + `}}`) + RecordClaudeWebSearchResults("session-1", event, 30*time.Minute, 16) + + got := RestoreClaudeWebSearchResults(claudeWebSearchRequestBody(`[]`), "session-1", 30*time.Minute) + if raw := gjson.GetBytes(got, "messages.1.content.1.content").Raw; raw != claudeWebSearchTestResults { + t.Fatalf("restored content = %s, want original results", raw) + } + if gjson.GetBytes(got, "messages.1.content.2.text").String() != "answer" { + t.Fatalf("neighbouring blocks changed: %s", got) + } +} + +func TestRecordClaudeWebSearchResultsFromMessageResponse(t *testing.T) { + resetClaudeWebSearchResults(t) + response := []byte(`{"id":"msg_1","type":"message","role":"assistant","content":[` + + `{"type":"server_tool_use","id":"srvtoolu_1","name":"web_search","input":{"query":"q"}},` + + `{"type":"web_search_tool_result","tool_use_id":"srvtoolu_1","content":` + claudeWebSearchTestResults + `}]}`) + RecordClaudeWebSearchResults("session-1", response, 30*time.Minute, 16) + + got := RestoreClaudeWebSearchResults(claudeWebSearchRequestBody(`[]`), "session-1", 30*time.Minute) + if raw := gjson.GetBytes(got, "messages.1.content.1.content").Raw; raw != claudeWebSearchTestResults { + t.Fatalf("restored content = %s, want original results", raw) + } +} + +func TestRestoreClaudeWebSearchResultsLeavesBodyUnchangedWhenNotApplicable(t *testing.T) { + resetClaudeWebSearchResults(t) + RecordClaudeWebSearchResults("session-1", []byte(`data: {"type":"content_block_start","index":1,"content_block":{"type":"web_search_tool_result","tool_use_id":"srvtoolu_1","content":`+claudeWebSearchTestResults+`}}`), 30*time.Minute, 16) + + cases := map[string]struct { + body []byte + sessionKey string + }{ + "other session": {claudeWebSearchRequestBody(`[]`), "session-2"}, + "no session": {claudeWebSearchRequestBody(`[]`), ""}, + "content already present": {claudeWebSearchRequestBody(`[{"type":"web_search_result","url":"https://example.com/z","encrypted_content":"z"}]`), "session-1"}, + "unknown tool use": {[]byte(`{"messages":[{"role":"assistant","content":[{"type":"web_search_tool_result","tool_use_id":"srvtoolu_9","content":[]}]}]}`), "session-1"}, + "user role is ignored": {[]byte(`{"messages":[{"role":"user","content":[{"type":"web_search_tool_result","tool_use_id":"srvtoolu_1","content":[]}]}]}`), "session-1"}, + } + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + if got := RestoreClaudeWebSearchResults(tc.body, tc.sessionKey, 30*time.Minute); string(got) != string(tc.body) { + t.Fatalf("body changed:\n got %s\nwant %s", got, tc.body) + } + }) + } +} + +func TestRecordClaudeWebSearchResultsIgnoresEmptyResults(t *testing.T) { + resetClaudeWebSearchResults(t) + RecordClaudeWebSearchResults("session-1", []byte(`data: {"type":"content_block_start","index":1,"content_block":{"type":"web_search_tool_result","tool_use_id":"srvtoolu_1","content":[]}}`), 30*time.Minute, 16) + if _, ok := cache.GetClaudeWebSearchResult("session-1", "srvtoolu_1"); ok { + t.Fatal("empty result content was cached") + } +} diff --git a/internal/watcher/diff/config_diff.go b/internal/watcher/diff/config_diff.go index ee68350e7f3..cf259219b68 100644 --- a/internal/watcher/diff/config_diff.go +++ b/internal/watcher/diff/config_diff.go @@ -152,6 +152,15 @@ func BuildConfigChangeDetails(oldCfg, newCfg *config.Config) []string { if oldCfg.Client.Codex.OptimizeMultiAgentV2 != newCfg.Client.Codex.OptimizeMultiAgentV2 { changes = append(changes, fmt.Sprintf("client.codex.optimize-multi-agent-v2: %t -> %t", oldCfg.Client.Codex.OptimizeMultiAgentV2, newCfg.Client.Codex.OptimizeMultiAgentV2)) } + if oldCfg.Client.Codex.WebSearchResultCache.Enabled != newCfg.Client.Codex.WebSearchResultCache.Enabled { + changes = append(changes, fmt.Sprintf("client.codex.web-search-result-cache.enabled: %t -> %t", oldCfg.Client.Codex.WebSearchResultCache.Enabled, newCfg.Client.Codex.WebSearchResultCache.Enabled)) + } + if strings.TrimSpace(oldCfg.Client.Codex.WebSearchResultCache.TTL) != strings.TrimSpace(newCfg.Client.Codex.WebSearchResultCache.TTL) { + changes = append(changes, fmt.Sprintf("client.codex.web-search-result-cache.ttl: %s -> %s", strings.TrimSpace(oldCfg.Client.Codex.WebSearchResultCache.TTL), strings.TrimSpace(newCfg.Client.Codex.WebSearchResultCache.TTL))) + } + if oldCfg.Client.Codex.WebSearchResultCache.Limit != newCfg.Client.Codex.WebSearchResultCache.Limit { + changes = append(changes, fmt.Sprintf("client.codex.web-search-result-cache.limit: %d -> %d", oldCfg.Client.Codex.WebSearchResultCache.Limit, newCfg.Client.Codex.WebSearchResultCache.Limit)) + } if oldCfg.Codex.OrphanDelegationCompatibility != newCfg.Codex.OrphanDelegationCompatibility { changes = append(changes, fmt.Sprintf("codex.orphan-delegation-compatibility: %t -> %t", oldCfg.Codex.OrphanDelegationCompatibility, newCfg.Codex.OrphanDelegationCompatibility)) } diff --git a/internal/watcher/diff/config_diff_test.go b/internal/watcher/diff/config_diff_test.go index c5a9c9869b4..4ad39f2f306 100644 --- a/internal/watcher/diff/config_diff_test.go +++ b/internal/watcher/diff/config_diff_test.go @@ -48,6 +48,32 @@ func TestBuildConfigChangeDetailsClientCodexOptimizeMultiAgentV2(t *testing.T) { } } +func TestBuildConfigChangeDetailsClientCodexWebSearchResultCache(t *testing.T) { + oldCfg, newCfg := &config.Config{}, &config.Config{} + newCfg.Client.Codex.WebSearchResultCache = config.CodexWebSearchResultCacheConfig{ + Enabled: true, + TTL: "30m", + Limit: 256, + } + changes := BuildConfigChangeDetails(oldCfg, newCfg) + want := []string{ + "client.codex.web-search-result-cache.enabled: false -> true", + "client.codex.web-search-result-cache.ttl: -> 30m", + "client.codex.web-search-result-cache.limit: 0 -> 256", + } + if len(changes) != len(want) { + t.Fatalf("changes = %v, want %v", changes, want) + } + for i := range want { + if changes[i] != want[i] { + t.Fatalf("changes[%d] = %q, want %q", i, changes[i], want[i]) + } + } + if unchanged := BuildConfigChangeDetails(newCfg, newCfg); len(unchanged) != 0 { + t.Fatalf("unchanged web-search-result-cache produced changes: %v", unchanged) + } +} + func TestBuildConfigChangeDetails(t *testing.T) { oldCfg := &config.Config{ Port: 8080, diff --git a/sdk/config/config.go b/sdk/config/config.go index 47f477d6bec..e584ab82f78 100644 --- a/sdk/config/config.go +++ b/sdk/config/config.go @@ -14,6 +14,7 @@ type ModelCatalogs = internalconfig.ModelCatalogs type ClientConfig = internalconfig.ClientConfig type CodexClientConfig = internalconfig.CodexClientConfig +type CodexWebSearchResultCacheConfig = internalconfig.CodexWebSearchResultCacheConfig type StreamingConfig = internalconfig.StreamingConfig type ClaudeCodeConfig = internalconfig.ClaudeCodeConfig type TLSConfig = internalconfig.TLSConfig From 07feedf7b760d33cc14af9cbe517e4ad6aa98aa0 Mon Sep 17 00:00:00 2001 From: hkfires <10558748+hkfires@users.noreply.github.com> Date: Mon, 19 Oct 2026 10:30:30 +0800 Subject: [PATCH 20/27] fix(api): allow empty home model catalog Return an empty catalog when home returns no model records, e.g. when the client's access scopes have no routes, instead of erroring. Still reject null payloads, null sections, and non-empty sections with no usable model IDs as malformed. --- internal/api/home_models_empty_test.go | 124 +++++++++++++++++++++++++ internal/api/server_routes.go | 13 ++- 2 files changed, 134 insertions(+), 3 deletions(-) create mode 100644 internal/api/home_models_empty_test.go diff --git a/internal/api/home_models_empty_test.go b/internal/api/home_models_empty_test.go new file mode 100644 index 00000000000..83fd9d5ab05 --- /dev/null +++ b/internal/api/home_models_empty_test.go @@ -0,0 +1,124 @@ +package api + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "github.com/gin-gonic/gin" + "github.com/router-for-me/CLIProxyAPI/v8/internal/home" + "github.com/router-for-me/CLIProxyAPI/v8/internal/registry" +) + +func TestDecodeHomeModelsEmptyCatalog(t *testing.T) { + for _, raw := range []string{"{}", " { } ", `{"claude":[],"codex-pro":[]}`} { + entries, errDecode := decodeHomeModels([]byte(raw)) + if errDecode != nil || entries == nil || len(entries) != 0 { + t.Errorf("decodeHomeModels(%q) = %v, %v, want empty list", raw, entries, errDecode) + } + } + for _, raw := range []string{ + "", "null", "[]", "{bad}", `{"claude":[{}]}`, `{"claude":null}`, `{"error":null}`, + `{"error":{"type":"internal_error","message":"unavailable"}}`, + } { + if _, errDecode := decodeHomeModels([]byte(raw)); errDecode == nil { + t.Errorf("decodeHomeModels(%q) accepted malformed or error payload", raw) + } + } +} + +func TestHomeEmptyModelCatalogResponses(t *testing.T) { + server := newTestServer(t) + server.cfg.Home.Enabled = true + engine := gin.New() + engine.GET("/v1/models", server.unifiedModelsHandler(nil, nil)) + engine.GET("/v1beta/models", server.geminiModelsHandler(nil)) + engine.GET("/v1beta/models/*action", server.geminiGetHandler(nil)) + client := newHomeCatalogClient(t, "{}") + previousHome := home.Current() + home.SetCurrent(client) + t.Cleanup(func() { home.SetCurrent(previousHome) }) + registry.GetGlobalRegistry().RegisterClient("empty-catalog-local", "codex", []*registry.ModelInfo{{ID: "local-only-model"}}) + t.Cleanup(func() { registry.GetGlobalRegistry().UnregisterClient("empty-catalog-local") }) + + tests := []struct { + name string + path string + headers map[string]string + field string + }{ + {name: "openai", path: "/v1/models", field: "data"}, + {name: "claude", path: "/v1/models", headers: map[string]string{"Anthropic-Version": "2023-06-01"}, field: "data"}, + {name: "codex", path: "/v1/models?client_version=0.153.4", field: "models"}, + {name: "cpa", path: "/v1/models?client_version=cpa", field: "models"}, + {name: "gemini", path: "/v1beta/models", field: "models"}, + {name: "grok", path: "/v1/models", headers: map[string]string{"User-Agent": "grok-shell"}, field: "data"}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + request := httptest.NewRequest(http.MethodGet, test.path, nil) + request.Header.Set("Authorization", "Bearer scoped-key") + for key, value := range test.headers { + request.Header.Set(key, value) + } + recorder := httptest.NewRecorder() + engine.ServeHTTP(recorder, request) + if recorder.Code != http.StatusOK { + t.Fatalf("status = %d; body=%s", recorder.Code, recorder.Body.String()) + } + var payload map[string]json.RawMessage + if errUnmarshal := json.Unmarshal(recorder.Body.Bytes(), &payload); errUnmarshal != nil { + t.Fatal(errUnmarshal) + } + if string(payload[test.field]) != "[]" { + t.Fatalf("%s = %s, want []; body=%s", test.field, payload[test.field], recorder.Body.String()) + } + }) + } + recorder := httptest.NewRecorder() + engine.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/v1beta/models/inaccessible-model", nil)) + if recorder.Code != http.StatusNotFound { + t.Fatalf("inaccessible model status = %d, want 404; body=%s", recorder.Code, recorder.Body.String()) + } +} + +func TestHomeModelCatalogErrorsRemainErrors(t *testing.T) { + server := newTestServer(t) + server.cfg.Home.Enabled = true + engine := gin.New() + engine.GET("/v1/models", server.unifiedModelsHandler(nil, nil)) + for _, test := range []struct { + name string + payload string + status int + }{ + {name: "missing credentials", payload: `{"error":{"type":"no_credentials","message":"Missing API key"}}`, status: http.StatusUnauthorized}, + {name: "invalid credentials", payload: `{"error":{"type":"invalid_credential","message":"Invalid API key"}}`, status: http.StatusUnauthorized}, + {name: "policy failure", payload: `{"error":{"type":"internal_error","message":"Model access service error"}}`, status: http.StatusBadGateway}, + {name: "empty response", payload: "", status: http.StatusBadGateway}, + {name: "malformed JSON", payload: "{bad}", status: http.StatusBadGateway}, + {name: "null catalog", payload: "null", status: http.StatusBadGateway}, + {name: "null section", payload: `{"claude":null}`, status: http.StatusBadGateway}, + {name: "missing model IDs", payload: `{"claude":[{}]}`, status: http.StatusBadGateway}, + } { + t.Run(test.name, func(t *testing.T) { + client := newHomeCatalogClient(t, test.payload) + previousHome := home.Current() + home.SetCurrent(client) + t.Cleanup(func() { home.SetCurrent(previousHome) }) + recorder := httptest.NewRecorder() + engine.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/v1/models", nil)) + if recorder.Code != test.status { + t.Fatalf("status = %d, want %d; body=%s", recorder.Code, test.status, recorder.Body.String()) + } + var payload map[string]json.RawMessage + if errUnmarshal := json.Unmarshal(recorder.Body.Bytes(), &payload); errUnmarshal != nil { + t.Fatal(errUnmarshal) + } + if _, exists := payload["error"]; !exists { + t.Fatalf("expected error response; body=%s", recorder.Body.String()) + } + }) + } +} diff --git a/internal/api/server_routes.go b/internal/api/server_routes.go index 8113a815d63..abe1108a34b 100644 --- a/internal/api/server_routes.go +++ b/internal/api/server_routes.go @@ -1164,13 +1164,18 @@ func decodeHomeModels(raw []byte) ([]homeModelEntry, error) { if err := json.Unmarshal(raw, &bySection); err != nil { return nil, fmt.Errorf("parse home models payload: %w", err) } - if len(bySection) == 0 { - return nil, fmt.Errorf("home models payload has no sections") + if bySection == nil { + return nil, fmt.Errorf("home models payload is not an object") } indexByID := make(map[string]int) out := make([]homeModelEntry, 0, 256) + hasModelRecords := false for section, models := range bySection { + if models == nil { + return nil, fmt.Errorf("home models section %q is null", section) + } + hasModelRecords = hasModelRecords || len(models) > 0 provider := strings.ToLower(strings.TrimSpace(section)) for _, model := range models { id, _ := model["id"].(string) @@ -1221,7 +1226,9 @@ func decodeHomeModels(raw []byte) ([]homeModelEntry, error) { } sort.Slice(out, func(i, j int) bool { return out[i].id < out[j].id }) - if len(out) == 0 { + // An empty catalog is valid when the client's access scopes have no routes. + // Non-empty sections with no usable model IDs still indicate malformed data. + if len(out) == 0 && hasModelRecords { return nil, fmt.Errorf("home models payload contains no models") } return out, nil From 4d34502f07f607450857739aff71593835ad07a9 Mon Sep 17 00:00:00 2001 From: hkfires <10558748+hkfires@users.noreply.github.com> Date: Mon, 19 Oct 2026 12:11:57 +0800 Subject: [PATCH 21/27] fix(api): reject home models error envelopes Reject any home models payload containing an error key before parsing provider sections. Array-shaped error values were previously treated as model sections, masking upstream errors as valid or empty catalogs instead of returning an error. --- internal/api/home_models_empty_test.go | 6 ++++++ internal/api/server_routes.go | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/internal/api/home_models_empty_test.go b/internal/api/home_models_empty_test.go index 83fd9d5ab05..b4bef17d032 100644 --- a/internal/api/home_models_empty_test.go +++ b/internal/api/home_models_empty_test.go @@ -21,6 +21,8 @@ func TestDecodeHomeModelsEmptyCatalog(t *testing.T) { for _, raw := range []string{ "", "null", "[]", "{bad}", `{"claude":[{}]}`, `{"claude":null}`, `{"error":null}`, `{"error":{"type":"internal_error","message":"unavailable"}}`, + `{"error":[]}`, `{"error":[],"claude":[]}`, + `{"error":[{"id":"not-a-model"}]}`, `{"error":[],"claude":[{"id":"model-a"}]}`, } { if _, errDecode := decodeHomeModels([]byte(raw)); errDecode == nil { t.Errorf("decodeHomeModels(%q) accepted malformed or error payload", raw) @@ -96,6 +98,10 @@ func TestHomeModelCatalogErrorsRemainErrors(t *testing.T) { {name: "missing credentials", payload: `{"error":{"type":"no_credentials","message":"Missing API key"}}`, status: http.StatusUnauthorized}, {name: "invalid credentials", payload: `{"error":{"type":"invalid_credential","message":"Invalid API key"}}`, status: http.StatusUnauthorized}, {name: "policy failure", payload: `{"error":{"type":"internal_error","message":"Model access service error"}}`, status: http.StatusBadGateway}, + {name: "empty array error", payload: `{"error":[]}`, status: http.StatusBadGateway}, + {name: "error alongside empty section", payload: `{"error":[],"claude":[]}`, status: http.StatusBadGateway}, + {name: "error containing model-shaped records", payload: `{"error":[{"id":"not-a-model"}]}`, status: http.StatusBadGateway}, + {name: "error alongside models", payload: `{"error":[],"claude":[{"id":"model-a"}]}`, status: http.StatusBadGateway}, {name: "empty response", payload: "", status: http.StatusBadGateway}, {name: "malformed JSON", payload: "{bad}", status: http.StatusBadGateway}, {name: "null catalog", payload: "null", status: http.StatusBadGateway}, diff --git a/internal/api/server_routes.go b/internal/api/server_routes.go index abe1108a34b..4a9b0a0694c 100644 --- a/internal/api/server_routes.go +++ b/internal/api/server_routes.go @@ -1167,6 +1167,10 @@ func decodeHomeModels(raw []byte) ([]homeModelEntry, error) { if bySection == nil { return nil, fmt.Errorf("home models payload is not an object") } + // The error envelope must never be interpreted as a provider model section. + if _, hasError := bySection["error"]; hasError { + return nil, fmt.Errorf("home models payload contains an error envelope") + } indexByID := make(map[string]int) out := make([]homeModelEntry, 0, 256) From d9451cb65d749f899d85599ae577cb7c82939659 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Mon, 19 Oct 2026 12:56:19 +0800 Subject: [PATCH 22/27] feat(vertex): support partner and open models via endpoints/openapi route - Route Vertex partner/open MaaS models and explicit openapi-enabled credentials through the OpenAI-compatible endpoints/openapi endpoint while reusing service-account OAuth token acquisition and supporting openapi_location overrides. - Register Vertex service-account MaaS models and aliases from credential metadata and OAuth alias rules so partner models are discoverable and routable. - Normalize Vertex OpenAPI model identifiers and reject static API keys against official Google Vertex OpenAPI hosts that require OAuth tokens. Closes: #6763 --- internal/config/vertex_compat.go | 4 +- .../executor/gemini_vertex_executor.go | 81 +++ .../gemini_vertex_executor_openapi_test.go | 607 ++++++++++++++++++ .../runtime/executor/helps/response_model.go | 5 + .../runtime/executor/helps/vertex_openapi.go | 322 ++++++++++ .../executor/helps/vertex_openapi_test.go | 92 +++ .../executor/openai_compat_executor.go | 33 +- internal/watcher/synthesizer/file.go | 86 ++- sdk/cliproxy/auth/classification.go | 3 + sdk/cliproxy/auth/metadata_keys.go | 4 + sdk/cliproxy/auth/types.go | 8 + sdk/cliproxy/service_models.go | 102 +++ .../service_vertex_openapi_models_test.go | 119 ++++ 13 files changed, 1442 insertions(+), 24 deletions(-) create mode 100644 internal/runtime/executor/gemini_vertex_executor_openapi_test.go create mode 100644 internal/runtime/executor/helps/vertex_openapi.go create mode 100644 internal/runtime/executor/helps/vertex_openapi_test.go create mode 100644 sdk/cliproxy/service_vertex_openapi_models_test.go diff --git a/internal/config/vertex_compat.go b/internal/config/vertex_compat.go index bd1cacb0452..9a555456618 100644 --- a/internal/config/vertex_compat.go +++ b/internal/config/vertex_compat.go @@ -113,12 +113,12 @@ func (cfg *Config) SanitizeVertexCompatKeys() { entry.Headers = NormalizeHeaders(entry.Headers) entry.ExcludedModels = NormalizeExcludedModels(entry.ExcludedModels) - // Sanitize models: remove entries without valid alias + // Sanitize models: remove entries without valid model name sanitizedModels := make([]VertexCompatModel, 0, len(entry.Models)) for _, model := range entry.Models { model.Alias = strings.TrimSpace(model.Alias) model.Name = strings.TrimSpace(model.Name) - if model.Alias != "" && model.Name != "" { + if model.Name != "" { sanitizedModels = append(sanitizedModels, model) } } diff --git a/internal/runtime/executor/gemini_vertex_executor.go b/internal/runtime/executor/gemini_vertex_executor.go index c8703719abe..79309c01c97 100644 --- a/internal/runtime/executor/gemini_vertex_executor.go +++ b/internal/runtime/executor/gemini_vertex_executor.go @@ -269,6 +269,9 @@ func (e *GeminiVertexExecutor) Execute(ctx context.Context, auth *cliproxyauth.A if opts.Alt == "responses/compact" { return resp, statusErr{code: http.StatusNotImplemented, msg: "/responses/compact not supported"} } + if helps.ShouldExecuteVertexOpenAPI(auth, req.Model) { + return e.executeOpenAPI(ctx, auth, req, opts) + } if shouldExecuteVertexInteractions(auth, opts) { return e.executeInteractions(ctx, auth, req, opts) } @@ -294,6 +297,9 @@ func (e *GeminiVertexExecutor) ExecuteStream(ctx context.Context, auth *cliproxy if opts.Alt == "responses/compact" { return nil, statusErr{code: http.StatusNotImplemented, msg: "/responses/compact not supported"} } + if helps.ShouldExecuteVertexOpenAPI(auth, req.Model) { + return e.executeOpenAPIStream(ctx, auth, req, opts) + } if shouldExecuteVertexInteractions(auth, opts) { return e.executeInteractionsStream(ctx, auth, req, opts) } @@ -315,6 +321,10 @@ func (e *GeminiVertexExecutor) ExecuteStream(ctx context.Context, auth *cliproxy // CountTokens counts tokens for the given request using the Vertex AI API. func (e *GeminiVertexExecutor) CountTokens(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { + if helps.ShouldExecuteVertexOpenAPI(auth, req.Model) { + normalizedReq, normalizedOpts := normalizeVertexOpenAPIRequest(req, opts) + return NewOpenAICompatExecutor(e.Identifier(), e.cfg).CountTokens(ctx, auth, normalizedReq, normalizedOpts) + } // Try API key authentication first apiKey, baseURL := vertexAPICreds(auth) @@ -331,6 +341,77 @@ func (e *GeminiVertexExecutor) CountTokens(ctx context.Context, auth *cliproxyau return e.countTokensWithAPIKey(ctx, auth, req, opts, apiKey, baseURL) } +func normalizeVertexOpenAPIRequest(req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Request, cliproxyexecutor.Options) { + normalizedModel := helps.NormalizeVertexOpenAPIModel(req.Model) + if normalizedModel == "" || normalizedModel == req.Model { + return req, opts + } + if len(opts.OriginalRequest) == 0 && len(req.Payload) > 0 { + opts.OriginalRequest = req.Payload + } + req.Model = normalizedModel + return req, opts +} + +func hasVertexServiceAccount(auth *cliproxyauth.Auth) bool { + if auth == nil || auth.Metadata == nil { + return false + } + sa, ok := auth.Metadata["service_account"].(map[string]any) + return ok && len(sa) > 0 +} + +func (e *GeminiVertexExecutor) resolveOpenAPICredentials(ctx context.Context, auth *cliproxyauth.Auth) (string, string, error) { + apiKey, baseURL := vertexAPICreds(auth) + if apiKey == "" || hasVertexServiceAccount(auth) { + projectID, location, saJSON, errCreds := vertexCreds(auth) + if errCreds != nil { + return "", "", errCreds + } + token, errTok := vertexAccessToken(ctx, e.cfg, auth, saJSON) + if errTok != nil { + log.Errorf("vertex executor: access token error: %v", errTok) + return "", "", statusErr{code: 500, msg: "internal server error"} + } + if strings.TrimSpace(token) == "" { + return "", "", statusErr{code: http.StatusUnauthorized, msg: "missing access token"} + } + openAPILocation := helps.VertexOpenAPILocation(auth, location) + openAPIBaseURL := helps.VertexServiceAccountOpenAPIBaseURL(helps.VertexAuthBaseURL(auth), vertexAPIVersion, projectID, openAPILocation) + return openAPIBaseURL, token, nil + } + if helps.IsGoogleVertexHost(baseURL) { + return "", "", statusErr{code: http.StatusBadRequest, msg: "vertex endpoints/openapi on aiplatform.googleapis.com requires service account credentials"} + } + openAPILocation := helps.VertexOpenAPILocation(auth, "") + openAPIBaseURL := helps.VertexAPIKeyOpenAPIBaseURL(baseURL, vertexAPIVersion, helps.VertexAuthProjectID(auth), openAPILocation) + return openAPIBaseURL, apiKey, nil +} + +func (e *GeminiVertexExecutor) executeOpenAPI(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (resp cliproxyexecutor.Response, err error) { + req, opts = normalizeVertexOpenAPIRequest(req, opts) + openAPIBaseURL, token, errCreds := e.resolveOpenAPICredentials(ctx, auth) + if errCreds != nil { + baseModel := thinking.ParseSuffix(req.Model).ModelName + reporter := helps.NewExecutorUsageReporter(ctx, e, baseModel, auth) + reporter.TrackFailure(ctx, &errCreds) + return resp, errCreds + } + return NewOpenAICompatExecutor(e.Identifier(), e.cfg).executeWithCredentials(ctx, auth, req, opts, openAPIBaseURL, token, nil) +} + +func (e *GeminiVertexExecutor) executeOpenAPIStream(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (*cliproxyexecutor.StreamResult, error) { + req, opts = normalizeVertexOpenAPIRequest(req, opts) + openAPIBaseURL, token, errCreds := e.resolveOpenAPICredentials(ctx, auth) + if errCreds != nil { + baseModel := thinking.ParseSuffix(req.Model).ModelName + reporter := helps.NewExecutorUsageReporter(ctx, e, baseModel, auth) + reporter.TrackFailure(ctx, &errCreds) + return nil, errCreds + } + return NewOpenAICompatExecutor(e.Identifier(), e.cfg).executeStreamWithCredentials(ctx, auth, req, opts, openAPIBaseURL, token, nil) +} + // Refresh refreshes the authentication credentials (no-op for Vertex). func (e *GeminiVertexExecutor) Refresh(ctx context.Context, auth *cliproxyauth.Auth) (*cliproxyauth.Auth, error) { if refreshed, handled, err := helps.RefreshAuthViaHome(ctx, e.cfg, auth); handled { diff --git a/internal/runtime/executor/gemini_vertex_executor_openapi_test.go b/internal/runtime/executor/gemini_vertex_executor_openapi_test.go new file mode 100644 index 00000000000..fcfa9fd1a0a --- /dev/null +++ b/internal/runtime/executor/gemini_vertex_executor_openapi_test.go @@ -0,0 +1,607 @@ +package executor + +import ( + "context" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "net/url" + "strings" + "sync" + "testing" + + "github.com/gin-gonic/gin" + "github.com/router-for-me/CLIProxyAPI/v8/internal/config" + cliproxyauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth" + cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor" + sdktranslator "github.com/router-for-me/CLIProxyAPI/v8/sdk/translator" + "github.com/tidwall/gjson" +) + +func TestGeminiVertexOpenAPI_ServiceAccount_Execute(t *testing.T) { + var mu sync.Mutex + var gotPath string + var gotMethod string + var gotAuth string + var upstreamBody []byte + + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/token" { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"access_token":"mock-vertex-sa-token","token_type":"Bearer","expires_in":3600}`)) + return + } + mu.Lock() + gotPath = r.URL.Path + gotMethod = r.Method + gotAuth = r.Header.Get("Authorization") + body, _ := io.ReadAll(r.Body) + upstreamBody = body + mu.Unlock() + + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{ + "id":"chatcmpl-vertex-maas-1", + "object":"chat.completion", + "created":1700000000, + "model":"deepseek-ai/deepseek-v4.1-flash-maas", + "choices":[{"index":0,"message":{"role":"assistant","content":"hello from vertex maas"},"finish_reason":"stop"}], + "usage":{"prompt_tokens":8,"completion_tokens":5,"total_tokens":13} + }`)) + })) + defer server.Close() + + targetURL, errParse := url.Parse(server.URL) + if errParse != nil { + t.Fatal(errParse) + } + transport := http.DefaultTransport.(*http.Transport).Clone() + transport.Proxy = nil + t.Cleanup(transport.CloseIdleConnections) + + gin.SetMode(gin.TestMode) + ginCtx, _ := gin.CreateTestContext(httptest.NewRecorder()) + ctx := context.WithValue(context.Background(), "cliproxy.roundtripper", issue6258RedirectTransport{ + target: targetURL, + base: transport, + }) + ctx = context.WithValue(ctx, "gin", ginCtx) + + var sa map[string]any + if errUnmarshal := json.Unmarshal(testVertexServiceAccountJSON(t, server.URL+"/token"), &sa); errUnmarshal != nil { + t.Fatal(errUnmarshal) + } + + auth := &cliproxyauth.Auth{ + ID: "vertex-sa-openapi", + Provider: "vertex", + Metadata: map[string]any{ + "project_id": "proxy-test", + "location": "global", + "email": "vertex-sa@example.iam.gserviceaccount.com", + "service_account": sa, + }, + } + + exec := NewGeminiVertexExecutor(&config.Config{ + SDKConfig: config.SDKConfig{RequestLog: true}, + }) + req := cliproxyexecutor.Request{ + Model: "deepseek-ai/deepseek-v4.1-flash-maas", + Payload: []byte(`{"model":"deepseek-ai/deepseek-v4.1-flash-maas","messages":[{"role":"user","content":"hello maas"}]}`), + } + opts := cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FormatOpenAI, + ResponseFormat: sdktranslator.FormatOpenAI, + } + + resp, errExecute := exec.Execute(ctx, auth, req, opts) + if errExecute != nil { + t.Fatalf("Execute() error = %v", errExecute) + } + + mu.Lock() + defer mu.Unlock() + + expectedPath := "/v1/projects/proxy-test/locations/global/endpoints/openapi/chat/completions" + if gotPath != expectedPath { + t.Fatalf("got path = %q, want %q", gotPath, expectedPath) + } + if gotMethod != http.MethodPost { + t.Fatalf("got method = %q, want %q", gotMethod, http.MethodPost) + } + if gotAuth != "Bearer mock-vertex-sa-token" { + t.Fatalf("got auth = %q, want Bearer mock-vertex-sa-token", gotAuth) + } + if gotModel := gjson.GetBytes(upstreamBody, "model").String(); gotModel != "deepseek-ai/deepseek-v4.1-flash-maas" { + t.Fatalf("got upstream model = %q, want deepseek-ai/deepseek-v4.1-flash-maas; body=%s", gotModel, string(upstreamBody)) + } + if gotMsg := gjson.GetBytes(upstreamBody, "messages.0.content").String(); gotMsg != "hello maas" { + t.Fatalf("got upstream messages.0.content = %q, want hello maas; body=%s", gotMsg, string(upstreamBody)) + } + if gotText := gjson.GetBytes(resp.Payload, "choices.0.message.content").String(); gotText != "hello from vertex maas" { + t.Fatalf("got response content = %q, want hello from vertex maas; payload=%s", gotText, string(resp.Payload)) + } + + if gotType, gotVal := auth.AccountInfo(); gotType != "oauth" || gotVal != "vertex-sa@example.iam.gserviceaccount.com" { + t.Fatalf("auth.AccountInfo() = (%q, %q), want (oauth, vertex-sa@example.iam.gserviceaccount.com)", gotType, gotVal) + } + recordedReq, okReq := ginCtx.Get("API_REQUEST") + if !okReq { + t.Fatal("expected API_REQUEST in gin context") + } + recordedStr := string(recordedReq.([]byte)) + if !strings.Contains(recordedStr, "type=oauth") { + t.Fatalf("expected type=oauth in API_REQUEST log, got: %s", recordedStr) + } + if strings.Contains(recordedStr, "type=api_key") || strings.Contains(recordedStr, "mock-vertex-sa-token") { + t.Fatalf("unexpected api_key auth type or raw token in API_REQUEST log: %s", recordedStr) + } +} + +func TestGeminiVertexOpenAPI_ModelNormalization_PublisherResourceAndExplicitOptIn(t *testing.T) { + var mu sync.Mutex + var upstreamModels []string + + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/token" { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"access_token":"mock-vertex-sa-token","token_type":"Bearer","expires_in":3600}`)) + return + } + body, _ := io.ReadAll(r.Body) + mu.Lock() + upstreamModels = append(upstreamModels, gjson.GetBytes(body, "model").String()) + mu.Unlock() + + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"id":"chatcmpl-norm","object":"chat.completion","choices":[{"index":0,"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}]}`)) + })) + defer server.Close() + + targetURL, errParse := url.Parse(server.URL) + if errParse != nil { + t.Fatal(errParse) + } + transport := http.DefaultTransport.(*http.Transport).Clone() + transport.Proxy = nil + t.Cleanup(transport.CloseIdleConnections) + + ctx := context.WithValue(context.Background(), "cliproxy.roundtripper", issue6258RedirectTransport{ + target: targetURL, + base: transport, + }) + + var sa map[string]any + if errUnmarshal := json.Unmarshal(testVertexServiceAccountJSON(t, server.URL+"/token"), &sa); errUnmarshal != nil { + t.Fatal(errUnmarshal) + } + + exec := NewGeminiVertexExecutor(&config.Config{}) + + // 1. publishers//models/ normalizes to / + saAuth := &cliproxyauth.Auth{ + ID: "vertex-sa-norm-1", + Provider: "vertex", + Metadata: map[string]any{ + "project_id": "proxy-test", + "location": "global", + "service_account": sa, + }, + } + _, errExec1 := exec.Execute(ctx, saAuth, cliproxyexecutor.Request{ + Model: "publishers/deepseek-ai/models/deepseek-v4.1-flash-maas", + Payload: []byte(`{"model":"publishers/deepseek-ai/models/deepseek-v4.1-flash-maas","messages":[{"role":"user","content":"hi"}]}`), + }, cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FormatOpenAI, + ResponseFormat: sdktranslator.FormatOpenAI, + }) + if errExec1 != nil { + t.Fatalf("Execute(publishers/...) error = %v", errExec1) + } + + // 2. Explicit openapi=true with bare model normalizes to google/ + explicitAuth := &cliproxyauth.Auth{ + ID: "vertex-sa-norm-2", + Provider: "vertex", + Metadata: map[string]any{ + "project_id": "proxy-test", + "location": "global", + "openapi": true, + "service_account": sa, + }, + } + _, errExec2 := exec.Execute(ctx, explicitAuth, cliproxyexecutor.Request{ + Model: "gemini-2.5-pro", + Payload: []byte(`{"model":"gemini-2.5-pro","messages":[{"role":"user","content":"hi"}]}`), + }, cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FormatOpenAI, + ResponseFormat: sdktranslator.FormatOpenAI, + }) + if errExec2 != nil { + t.Fatalf("Execute(explicit openapi=true) error = %v", errExec2) + } + + mu.Lock() + defer mu.Unlock() + if len(upstreamModels) != 2 { + t.Fatalf("expected 2 upstream calls, got %d", len(upstreamModels)) + } + if upstreamModels[0] != "deepseek-ai/deepseek-v4.1-flash-maas" { + t.Fatalf("upstreamModels[0] = %q, want deepseek-ai/deepseek-v4.1-flash-maas", upstreamModels[0]) + } + if upstreamModels[1] != "google/gemini-2.5-pro" { + t.Fatalf("upstreamModels[1] = %q, want google/gemini-2.5-pro", upstreamModels[1]) + } +} + +func TestGeminiVertexOpenAPI_ServiceAccount_ExecuteStream_AndOpenAPILocationOverride(t *testing.T) { + var mu sync.Mutex + var gotPath string + var gotAuth string + var upstreamBody []byte + + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/token" { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"access_token":"mock-vertex-sa-token","token_type":"Bearer","expires_in":3600}`)) + return + } + mu.Lock() + gotPath = r.URL.Path + gotAuth = r.Header.Get("Authorization") + body, _ := io.ReadAll(r.Body) + upstreamBody = body + mu.Unlock() + + w.Header().Set("Content-Type", "text/event-stream") + _, _ = w.Write([]byte("data: {\"id\":\"chatcmpl-stream-1\",\"object\":\"chat.completion.chunk\",\"model\":\"qwen/qwen3-coder-480b-a35b-instruct-maas\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"stream ok\"},\"finish_reason\":null}]}\n\n")) + _, _ = w.Write([]byte("data: {\"id\":\"chatcmpl-stream-1\",\"object\":\"chat.completion.chunk\",\"model\":\"qwen/qwen3-coder-480b-a35b-instruct-maas\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}],\"usage\":{\"prompt_tokens\":4,\"completion_tokens\":2,\"total_tokens\":6}}\n\n")) + _, _ = w.Write([]byte("data: [DONE]\n\n")) + })) + defer server.Close() + + targetURL, errParse := url.Parse(server.URL) + if errParse != nil { + t.Fatal(errParse) + } + transport := http.DefaultTransport.(*http.Transport).Clone() + transport.Proxy = nil + t.Cleanup(transport.CloseIdleConnections) + + ctx := context.WithValue(context.Background(), "cliproxy.roundtripper", issue6258RedirectTransport{ + target: targetURL, + base: transport, + }) + + var sa map[string]any + if errUnmarshal := json.Unmarshal(testVertexServiceAccountJSON(t, server.URL+"/token"), &sa); errUnmarshal != nil { + t.Fatal(errUnmarshal) + } + + auth := &cliproxyauth.Auth{ + ID: "vertex-sa-openapi-stream", + Provider: "vertex", + Metadata: map[string]any{ + "project_id": "proxy-test", + "location": "us-central1", + "openapi_location": "global", + "service_account": sa, + }, + } + + exec := NewGeminiVertexExecutor(&config.Config{}) + req := cliproxyexecutor.Request{ + Model: "qwen/qwen3-coder-480b-a35b-instruct-maas", + Payload: []byte(`{"model":"qwen/qwen3-coder-480b-a35b-instruct-maas","messages":[{"role":"user","content":"stream test"}],"stream":true}`), + } + opts := cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FormatOpenAI, + ResponseFormat: sdktranslator.FormatOpenAI, + Stream: true, + } + + stream, errStream := exec.ExecuteStream(ctx, auth, req, opts) + if errStream != nil { + t.Fatalf("ExecuteStream() error = %v", errStream) + } + + var output strings.Builder + for chunk := range stream.Chunks { + if chunk.Err != nil { + t.Fatalf("stream chunk error: %v", chunk.Err) + } + output.Write(chunk.Payload) + } + + mu.Lock() + defer mu.Unlock() + + expectedPath := "/v1/projects/proxy-test/locations/global/endpoints/openapi/chat/completions" + if gotPath != expectedPath { + t.Fatalf("got path = %q, want %q", gotPath, expectedPath) + } + if gotAuth != "Bearer mock-vertex-sa-token" { + t.Fatalf("got auth = %q, want Bearer mock-vertex-sa-token", gotAuth) + } + if !gjson.GetBytes(upstreamBody, "stream_options.include_usage").Bool() { + t.Fatalf("expected stream_options.include_usage=true in upstream body: %s", string(upstreamBody)) + } + if !strings.Contains(output.String(), "stream ok") { + t.Fatalf("expected stream output to contain 'stream ok', got: %s", output.String()) + } +} + +func TestGeminiVertexOpenAPI_PayloadRulesOrdering(t *testing.T) { + var mu sync.Mutex + var nonStreamBody []byte + var streamBody []byte + + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/token" { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"access_token":"mock-vertex-sa-token","token_type":"Bearer","expires_in":3600}`)) + return + } + body, _ := io.ReadAll(r.Body) + if r.Header.Get("Accept") == "text/event-stream" { + mu.Lock() + streamBody = body + mu.Unlock() + w.Header().Set("Content-Type", "text/event-stream") + _, _ = w.Write([]byte("data: {\"id\":\"chatcmpl-1\",\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"ok\"},\"finish_reason\":\"stop\"}]}\n\ndata: [DONE]\n\n")) + return + } + mu.Lock() + nonStreamBody = body + mu.Unlock() + + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"id":"chatcmpl-1","object":"chat.completion","choices":[{"index":0,"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}]}`)) + })) + defer server.Close() + + targetURL, errParse := url.Parse(server.URL) + if errParse != nil { + t.Fatal(errParse) + } + transport := http.DefaultTransport.(*http.Transport).Clone() + transport.Proxy = nil + t.Cleanup(transport.CloseIdleConnections) + + ctx := context.WithValue(context.Background(), "cliproxy.roundtripper", issue6258RedirectTransport{ + target: targetURL, + base: transport, + }) + + var sa map[string]any + if errUnmarshal := json.Unmarshal(testVertexServiceAccountJSON(t, server.URL+"/token"), &sa); errUnmarshal != nil { + t.Fatal(errUnmarshal) + } + + exec := NewGeminiVertexExecutor(&config.Config{ + Payload: config.PayloadConfig{ + Override: []config.PayloadRule{ + { + Models: []config.PayloadModelRule{ + {Name: "deepseek-ai/deepseek-v4.1-flash-maas", Protocol: "openai"}, + }, + Params: map[string]any{ + "temperature": 0.25, + "custom_openapi_param": "final_barrier_applied", + "stream_options.include_usage": false, + }, + }, + }, + Filter: []config.PayloadFilterRule{ + { + Models: []config.PayloadModelRule{ + {Name: "deepseek-ai/deepseek-v4.1-flash-maas", Protocol: "openai"}, + }, + Params: []string{"top_p"}, + }, + }, + }, + }) + auth := &cliproxyauth.Auth{ + ID: "vertex-sa-openapi-rules", + Provider: "vertex", + Metadata: map[string]any{ + "project_id": "proxy-test", + "location": "global", + "service_account": sa, + }, + } + req := cliproxyexecutor.Request{ + Model: "deepseek-ai/deepseek-v4.1-flash-maas", + Payload: []byte(`{"model":"deepseek-ai/deepseek-v4.1-flash-maas","temperature":0.9,"top_p":0.8,"messages":[{"role":"user","content":"hi"}]}`), + } + _, errExecute := exec.Execute(ctx, auth, req, cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FormatOpenAI, + ResponseFormat: sdktranslator.FormatOpenAI, + }) + if errExecute != nil { + t.Fatalf("Execute() error = %v", errExecute) + } + + streamRes, errStream := exec.ExecuteStream(ctx, auth, req, cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FormatOpenAI, + ResponseFormat: sdktranslator.FormatOpenAI, + Stream: true, + }) + if errStream != nil { + t.Fatalf("ExecuteStream() error = %v", errStream) + } + for chunk := range streamRes.Chunks { + if chunk.Err != nil { + t.Fatalf("stream chunk error: %v", chunk.Err) + } + } + + mu.Lock() + defer mu.Unlock() + for name, body := range map[string][]byte{"Execute": nonStreamBody, "ExecuteStream": streamBody} { + if got := gjson.GetBytes(body, "custom_openapi_param").String(); got != "final_barrier_applied" { + t.Fatalf("%s: custom_openapi_param = %q, want final_barrier_applied; body=%s", name, got, string(body)) + } + if got := gjson.GetBytes(body, "temperature").Float(); got != 0.25 { + t.Fatalf("%s: temperature = %v, want 0.25; body=%s", name, got, string(body)) + } + if gjson.GetBytes(body, "top_p").Exists() { + t.Fatalf("%s: expected top_p to be filtered out, got body=%s", name, string(body)) + } + } + if gjson.GetBytes(streamBody, "stream_options.include_usage").Bool() { + t.Fatalf("ExecuteStream: expected payload override stream_options.include_usage=false to win over built-in default, got body=%s", string(streamBody)) + } +} + +func TestGeminiVertexOpenAPI_APIKey_ValidationAndThirdPartyRelay(t *testing.T) { + var mu sync.Mutex + var gotPath string + var gotBearer string + var gotGoogKey string + + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + mu.Lock() + gotPath = r.URL.Path + gotBearer = r.Header.Get("Authorization") + gotGoogKey = r.Header.Get("x-goog-api-key") + mu.Unlock() + + w.Header().Set("Content-Type", "application/json") + if strings.HasSuffix(r.URL.Path, "/chat/completions") { + _, _ = w.Write([]byte(`{"id":"chatcmpl-key","object":"chat.completion","choices":[{"index":0,"message":{"role":"assistant","content":"key openapi ok"},"finish_reason":"stop"}]}`)) + return + } + _, _ = w.Write([]byte(`{"responseId":"resp1","candidates":[{"content":{"parts":[{"text":"google native ok"}]},"finishReason":"STOP"}]}`)) + })) + defer server.Close() + + exec := NewGeminiVertexExecutor(&config.Config{}) + + // 1. Static API key targeting official Google Vertex host is rejected for endpoints/openapi + for _, googleBase := range []string{"", "https://aiplatform.googleapis.com/v1/projects/proxy-test/locations/global"} { + googleKeyAuth := &cliproxyauth.Auth{ + ID: "vertex-apikey-google", + Provider: "vertex", + Attributes: map[string]string{ + "api_key": "static-api-key", + "base_url": googleBase, + }, + } + _, errGoogleKey := exec.Execute(context.Background(), googleKeyAuth, cliproxyexecutor.Request{ + Model: "deepseek-ai/deepseek-v4.1-flash-maas", + Payload: []byte(`{"model":"deepseek-ai/deepseek-v4.1-flash-maas","messages":[{"role":"user","content":"hi"}]}`), + }, cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FormatOpenAI, + ResponseFormat: sdktranslator.FormatOpenAI, + }) + if errGoogleKey == nil || !strings.Contains(errGoogleKey.Error(), "requires service account credentials") { + t.Fatalf("Execute(googleBase=%q) error = %v, want requires service account credentials", googleBase, errGoogleKey) + } + } + + // 2. Third-party compatible gateway with custom base_url routes non-google publisher model to endpoints/openapi/chat/completions + customGatewayAuth := &cliproxyauth.Auth{ + ID: "vertex-apikey-custom-gateway", + Provider: "vertex", + Attributes: map[string]string{ + "api_key": "test-vertex-key", + "base_url": server.URL + "/v1/projects/proxy-test/locations/global", + }, + } + resp, errExecute := exec.Execute(context.Background(), customGatewayAuth, cliproxyexecutor.Request{ + Model: "deepseek-ai/deepseek-v4.1-flash-maas", + Payload: []byte(`{"model":"deepseek-ai/deepseek-v4.1-flash-maas","messages":[{"role":"user","content":"hi"}]}`), + }, cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FormatOpenAI, + ResponseFormat: sdktranslator.FormatOpenAI, + }) + if errExecute != nil { + t.Fatalf("Execute(maas) error = %v", errExecute) + } + mu.Lock() + if gotPath != "/v1/projects/proxy-test/locations/global/endpoints/openapi/chat/completions" { + mu.Unlock() + t.Fatalf("got path = %q, want /v1/projects/proxy-test/locations/global/endpoints/openapi/chat/completions", gotPath) + } + if gotBearer != "Bearer test-vertex-key" { + mu.Unlock() + t.Fatalf("got Authorization = %q, want Bearer test-vertex-key", gotBearer) + } + mu.Unlock() + if gotText := gjson.GetBytes(resp.Payload, "choices.0.message.content").String(); gotText != "key openapi ok" { + t.Fatalf("got response = %q, want key openapi ok", gotText) + } + + // 3. google/ prefix on a relay base URL keeps native generateContent path + relayAuth := &cliproxyauth.Auth{ + ID: "vertex-apikey-relay", + Provider: "vertex", + Attributes: map[string]string{ + "api_key": "test-relay-key", + "base_url": server.URL, + }, + } + _, errRelay := exec.Execute(context.Background(), relayAuth, cliproxyexecutor.Request{ + Model: "google/gemini-2.5-pro", + Payload: []byte(`{"model":"google/gemini-2.5-pro","messages":[{"role":"user","content":"hi relay"}]}`), + }, cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FormatOpenAI, + ResponseFormat: sdktranslator.FormatOpenAI, + }) + if errRelay != nil { + t.Fatalf("Execute(google/gemini-2.5-pro) error = %v", errRelay) + } + mu.Lock() + defer mu.Unlock() + if gotPath != "/v1/publishers/google/models/google/gemini-2.5-pro:generateContent" { + t.Fatalf("got relay path = %q, want /v1/publishers/google/models/google/gemini-2.5-pro:generateContent", gotPath) + } + if gotGoogKey != "test-relay-key" { + t.Fatalf("got x-goog-api-key = %q, want test-relay-key", gotGoogKey) + } +} + +func TestGeminiVertexOpenAPI_CountTokens(t *testing.T) { + var hitUpstream bool + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + hitUpstream = true + w.WriteHeader(http.StatusInternalServerError) + })) + defer server.Close() + + var sa map[string]any + if errUnmarshal := json.Unmarshal(testVertexServiceAccountJSON(t, server.URL+"/token"), &sa); errUnmarshal != nil { + t.Fatal(errUnmarshal) + } + + auth := &cliproxyauth.Auth{ + ID: "vertex-sa-openapi-count", + Provider: "vertex", + Metadata: map[string]any{ + "project_id": "proxy-test", + "location": "global", + "service_account": sa, + }, + } + exec := NewGeminiVertexExecutor(&config.Config{}) + resp, errCount := exec.CountTokens(context.Background(), auth, cliproxyexecutor.Request{ + Model: "deepseek-ai/deepseek-v4.1-flash-maas", + Payload: []byte(`{"contents":[{"role":"user","parts":[{"text":"count these tokens"}]}]}`), + }, cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FormatGemini, + ResponseFormat: sdktranslator.FormatGemini, + }) + if errCount != nil { + t.Fatalf("CountTokens() error = %v", errCount) + } + if hitUpstream { + t.Fatal("expected CountTokens for OpenAPI model to use local tokenizer without hitting upstream") + } + if gjson.GetBytes(resp.Payload, "totalTokens").Int() <= 0 { + t.Fatalf("expected positive totalTokens, got: %s", string(resp.Payload)) + } +} diff --git a/internal/runtime/executor/helps/response_model.go b/internal/runtime/executor/helps/response_model.go index b9c0403a0a5..ec2753d0ceb 100644 --- a/internal/runtime/executor/helps/response_model.go +++ b/internal/runtime/executor/helps/response_model.go @@ -103,6 +103,11 @@ func extractGeminiResponseModelEvent(data []byte) (model string, terminal bool) cand = gjson.GetBytes(data, "response.candidates.0.finishReason") } terminal = cand.Exists() && cand.String() != "" + if !terminal { + objectType := gjson.GetBytes(data, "object").String() + finishReason := gjson.GetBytes(data, "choices.0.finish_reason").String() + terminal = objectType == "chat.completion" || finishReason != "" + } if !terminal { eventType := gjson.GetBytes(data, "event_type").String() if eventType == "" { diff --git a/internal/runtime/executor/helps/vertex_openapi.go b/internal/runtime/executor/helps/vertex_openapi.go new file mode 100644 index 00000000000..29afafbba10 --- /dev/null +++ b/internal/runtime/executor/helps/vertex_openapi.go @@ -0,0 +1,322 @@ +package helps + +import ( + "fmt" + "net/url" + "strings" + + "github.com/router-for-me/CLIProxyAPI/v8/internal/thinking" + cliproxyauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth" +) + +// IsVertexOpenAPIModel reports whether the given model identifier targets a +// non-Google Vertex partner or open model (MaaS) served by endpoints/openapi. +func IsVertexOpenAPIModel(model string) bool { + baseModel := strings.TrimSpace(thinking.ParseSuffix(model).ModelName) + if baseModel == "" { + return false + } + if publisher, modelID, ok := cutVertexPublisherResource(baseModel); ok { + return !strings.EqualFold(publisher, "google") && !strings.Contains(modelID, "/") + } + publisher, modelID, ok := strings.Cut(baseModel, "/") + publisher = strings.TrimSpace(publisher) + modelID = strings.TrimSpace(modelID) + if !ok || publisher == "" || modelID == "" || strings.Contains(modelID, "/") { + return false + } + if strings.EqualFold(publisher, "google") || strings.EqualFold(publisher, "models") || strings.EqualFold(publisher, "publishers") { + return false + } + return true +} + +// NormalizeVertexOpenAPIModel normalizes a Vertex model identifier into the +// "/" form expected by Vertex's endpoints/openapi route while +// preserving any thinking suffix. +func NormalizeVertexOpenAPIModel(model string) string { + trimmed := strings.TrimSpace(model) + if trimmed == "" { + return "" + } + parsed := thinking.ParseSuffix(trimmed) + baseModel := strings.TrimSpace(parsed.ModelName) + if baseModel == "" { + baseModel = trimmed + } + normalizedBase := normalizeVertexOpenAPIBaseModel(baseModel) + if parsed.HasSuffix { + return fmt.Sprintf("%s(%s)", normalizedBase, parsed.RawSuffix) + } + return normalizedBase +} + +func normalizeVertexOpenAPIBaseModel(baseModel string) string { + if publisher, modelID, ok := cutVertexPublisherResource(baseModel); ok { + return strings.ToLower(publisher) + "/" + modelID + } + lower := strings.ToLower(baseModel) + if strings.HasPrefix(lower, "models/") { + modelID := strings.TrimSpace(baseModel[len("models/"):]) + if modelID != "" { + return "google/" + modelID + } + } + if publisher, modelID, ok := strings.Cut(baseModel, "/"); ok { + publisher = strings.TrimSpace(publisher) + modelID = strings.TrimSpace(modelID) + if publisher != "" && modelID != "" { + return publisher + "/" + modelID + } + } + return "google/" + baseModel +} + +func cutVertexPublisherResource(baseModel string) (publisher, modelID string, ok bool) { + lower := strings.ToLower(baseModel) + if !strings.HasPrefix(lower, "publishers/") { + return "", "", false + } + rest := strings.TrimSpace(baseModel[len("publishers/"):]) + pub, tail, found := strings.Cut(rest, "/") + pub = strings.TrimSpace(pub) + tail = strings.TrimSpace(tail) + if !found || pub == "" || tail == "" { + return "", "", false + } + lowerTail := strings.ToLower(tail) + if !strings.HasPrefix(lowerTail, "models/") { + return "", "", false + } + id := strings.TrimSpace(tail[len("models/"):]) + if id == "" { + return "", "", false + } + return pub, id, true +} + +// ShouldExecuteVertexOpenAPI reports whether the request should be routed +// through Vertex AI's OpenAI-compatible endpoints/openapi route. +func ShouldExecuteVertexOpenAPI(auth *cliproxyauth.Auth, model string) bool { + if setting, hasExplicit := vertexOpenAPIExplicitSetting(auth); hasExplicit { + if !setting { + return false + } + lowerModel := strings.ToLower(strings.TrimSpace(thinking.ParseSuffix(model).ModelName)) + if strings.Contains(lowerModel, "imagen") { + return false + } + return true + } + return IsVertexOpenAPIModel(model) +} + +func vertexOpenAPIExplicitSetting(auth *cliproxyauth.Auth) (bool, bool) { + if auth == nil { + return false, false + } + if auth.Attributes != nil { + if v, ok := parseVertexOpenAPIBool(auth.Attributes["openapi"]); ok { + return v, true + } + } + if auth.Metadata != nil { + if v, ok := parseVertexOpenAPIBool(auth.Metadata["openapi"]); ok { + return v, true + } + } + return false, false +} + +func parseVertexOpenAPIBool(raw any) (bool, bool) { + switch v := raw.(type) { + case bool: + return v, true + case string: + trimmed := strings.ToLower(strings.TrimSpace(v)) + switch trimmed { + case "true", "1": + return true, true + case "false", "0": + return false, true + } + } + return false, false +} + +// IsGoogleVertexHost reports whether baseURL is empty or points to Google's +// official aiplatform.googleapis.com domain. +func IsGoogleVertexHost(baseURL string) bool { + trimmed := strings.TrimSpace(baseURL) + if trimmed == "" { + return true + } + parsed, errParse := url.Parse(trimmed) + if errParse != nil || parsed.Host == "" { + lower := strings.ToLower(trimmed) + return strings.Contains(lower, "aiplatform.googleapis.com") + } + host := strings.ToLower(parsed.Hostname()) + return host == "aiplatform.googleapis.com" || strings.HasSuffix(host, "-aiplatform.googleapis.com") || strings.HasSuffix(host, ".aiplatform.googleapis.com") +} + +// VertexOpenAPILocation returns the target Vertex location for OpenAPI requests, +// preferring an explicit openapi_location override on the credential. +func VertexOpenAPILocation(auth *cliproxyauth.Auth, defaultLocation string) string { + if auth != nil { + if auth.Attributes != nil { + for _, key := range []string{"openapi_location", "openapi-location", "location"} { + if v := strings.TrimSpace(auth.Attributes[key]); v != "" { + if key != "location" || strings.TrimSpace(defaultLocation) == "" { + return v + } + } + } + } + if auth.Metadata != nil { + for _, key := range []string{"openapi_location", "openapi-location"} { + if v, ok := auth.Metadata[key].(string); ok && strings.TrimSpace(v) != "" { + return strings.TrimSpace(v) + } + } + if strings.TrimSpace(defaultLocation) == "" { + if v, ok := auth.Metadata["location"].(string); ok && strings.TrimSpace(v) != "" { + return strings.TrimSpace(v) + } + } + } + } + loc := strings.TrimSpace(defaultLocation) + if loc == "" { + return "us-central1" + } + return loc +} + +// VertexAuthBaseURL extracts an optional custom base_url from a Vertex credential. +func VertexAuthBaseURL(auth *cliproxyauth.Auth) string { + if auth == nil { + return "" + } + if auth.Attributes != nil { + if v := strings.TrimSpace(auth.Attributes["base_url"]); v != "" { + return v + } + } + if auth.Metadata != nil { + for _, key := range []string{"base_url", "base-url"} { + if v, ok := auth.Metadata[key].(string); ok && strings.TrimSpace(v) != "" { + return strings.TrimSpace(v) + } + } + } + return "" +} + +// VertexAuthProjectID extracts an optional project_id from a Vertex credential. +func VertexAuthProjectID(auth *cliproxyauth.Auth) string { + if auth == nil || auth.Metadata == nil { + return "" + } + for _, key := range []string{"project_id", "project"} { + if v, ok := auth.Metadata[key].(string); ok && strings.TrimSpace(v) != "" { + return strings.TrimSpace(v) + } + } + return "" +} + +// VertexOpenAPIHostBaseURL returns the Vertex regional or global host URL. +func VertexOpenAPIHostBaseURL(location string) string { + loc := strings.TrimSpace(location) + if loc == "" { + loc = "us-central1" + } else if strings.EqualFold(loc, "global") { + return "https://aiplatform.googleapis.com" + } + return fmt.Sprintf("https://%s-aiplatform.googleapis.com", loc) +} + +// VertexServiceAccountOpenAPIBaseURL builds the OpenAI-compatible base URL for a +// Vertex service-account credential (.../v1/projects/{project}/locations/{location}/endpoints/openapi). +func VertexServiceAccountOpenAPIBaseURL(baseURL, version, projectID, location string) string { + loc := strings.TrimSpace(location) + if loc == "" { + loc = "us-central1" + } + ver := strings.TrimSpace(version) + if ver == "" { + ver = "v1" + } + base := strings.TrimRight(strings.TrimSpace(baseURL), "/") + if base == "" { + base = VertexOpenAPIHostBaseURL(loc) + } + if strings.HasSuffix(base, "/endpoints/openapi") { + return base + } + if idx := strings.Index(base, "/publishers/"); idx >= 0 { + return base[:idx] + "/endpoints/openapi" + } + if strings.Contains(base, "/projects/") && strings.Contains(base, "/locations/") { + return base + "/endpoints/openapi" + } + if !vertexOpenAPIHasVersionSegment(base) { + base = fmt.Sprintf("%s/%s", base, ver) + } + return fmt.Sprintf("%s/projects/%s/locations/%s/endpoints/openapi", base, strings.TrimSpace(projectID), loc) +} + +// VertexAPIKeyOpenAPIBaseURL builds the OpenAI-compatible base URL for a third-party +// Vertex-compatible API-key credential. +func VertexAPIKeyOpenAPIBaseURL(baseURL, version, projectID, location string) string { + ver := strings.TrimSpace(version) + if ver == "" { + ver = "v1" + } + base := strings.TrimRight(strings.TrimSpace(baseURL), "/") + if base == "" { + return "" + } + if strings.HasSuffix(base, "/endpoints/openapi") { + return base + } + if idx := strings.Index(base, "/publishers/"); idx >= 0 { + return base[:idx] + "/endpoints/openapi" + } + if strings.Contains(base, "/projects/") && strings.Contains(base, "/locations/") { + return base + "/endpoints/openapi" + } + if strings.TrimSpace(projectID) != "" { + return VertexServiceAccountOpenAPIBaseURL(base, ver, projectID, location) + } + if !vertexOpenAPIHasVersionSegment(base) { + base = fmt.Sprintf("%s/%s", base, ver) + } + return base + "/endpoints/openapi" +} + +func vertexOpenAPIHasVersionSegment(base string) bool { + if base == "" { + return false + } + if strings.Contains(base, "/v1/") || strings.Contains(base, "/v1beta1/") { + return true + } + seg := base[strings.LastIndexByte(base, '/')+1:] + if len(seg) < 2 || seg[0] != 'v' { + return false + } + hasDigit := false + for i := 1; i < len(seg); i++ { + c := seg[i] + switch { + case c >= '0' && c <= '9': + hasDigit = true + case c == '_' || c == '-' || (c >= 'a' && c <= 'z'): + default: + return false + } + } + return hasDigit +} diff --git a/internal/runtime/executor/helps/vertex_openapi_test.go b/internal/runtime/executor/helps/vertex_openapi_test.go new file mode 100644 index 00000000000..20ed01808ff --- /dev/null +++ b/internal/runtime/executor/helps/vertex_openapi_test.go @@ -0,0 +1,92 @@ +package helps + +import ( + "testing" + + cliproxyauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth" +) + +func TestIsVertexOpenAPIModel(t *testing.T) { + tests := []struct { + model string + want bool + }{ + {"deepseek-ai/deepseek-v4.1-flash-maas", true}, + {"deepseek-ai/deepseek-v4.1-flash-maas(high)", true}, + {"qwen/qwen3-coder-480b-a35b-instruct-maas", true}, + {"xai/grok-4-maas", true}, + {"publishers/deepseek-ai/models/deepseek-v4.1-flash-maas", true}, + {"publishers/google/models/gemini-2.5-pro", false}, + {"publishers/deepseek-ai/deepseek-v4.1-flash-maas", false}, + {"deepseek-ai/extra/segment", false}, + {"google/gemini-2.5-pro", false}, + {"models/gemini-2.5-pro", false}, + {"gemini-2.5-pro", false}, + {"imagen-3.0-generate-002", false}, + {"", false}, + } + for _, tc := range tests { + if got := IsVertexOpenAPIModel(tc.model); got != tc.want { + t.Errorf("IsVertexOpenAPIModel(%q) = %v, want %v", tc.model, got, tc.want) + } + } +} + +func TestNormalizeVertexOpenAPIModel(t *testing.T) { + tests := []struct { + in string + want string + }{ + {"deepseek-ai/deepseek-v4.1-flash-maas", "deepseek-ai/deepseek-v4.1-flash-maas"}, + {"publishers/deepseek-ai/models/deepseek-v4.1-flash-maas", "deepseek-ai/deepseek-v4.1-flash-maas"}, + {"publishers/deepseek-ai/models/deepseek-v4.1-flash-maas(high)", "deepseek-ai/deepseek-v4.1-flash-maas(high)"}, + {"gemini-2.5-pro", "google/gemini-2.5-pro"}, + {"gemini-2.5-pro(8192)", "google/gemini-2.5-pro(8192)"}, + {"models/gemini-2.5-pro", "google/gemini-2.5-pro"}, + {"publishers/google/models/gemini-2.5-pro", "google/gemini-2.5-pro"}, + } + for _, tc := range tests { + if got := NormalizeVertexOpenAPIModel(tc.in); got != tc.want { + t.Errorf("NormalizeVertexOpenAPIModel(%q) = %q, want %q", tc.in, got, tc.want) + } + } +} + +func TestShouldExecuteVertexOpenAPI(t *testing.T) { + if !ShouldExecuteVertexOpenAPI(nil, "deepseek-ai/deepseek-v4.1-flash-maas") { + t.Fatal("expected automatic OpenAPI selection for deepseek-ai/deepseek-v4.1-flash-maas") + } + disabledAuth := &cliproxyauth.Auth{ + Metadata: map[string]any{"openapi": false}, + } + if ShouldExecuteVertexOpenAPI(disabledAuth, "deepseek-ai/deepseek-v4.1-flash-maas") { + t.Fatal("expected explicit openapi=false to disable OpenAPI routing") + } + enabledAuth := &cliproxyauth.Auth{ + Attributes: map[string]string{"openapi": "true"}, + } + if !ShouldExecuteVertexOpenAPI(enabledAuth, "gemini-2.5-pro") { + t.Fatal("expected explicit openapi=true to enable OpenAPI routing") + } +} + +func TestVertexOpenAPIBaseURLBuilders(t *testing.T) { + if got := VertexServiceAccountOpenAPIBaseURL("", "v1", "my-proj", "global"); got != "https://aiplatform.googleapis.com/v1/projects/my-proj/locations/global/endpoints/openapi" { + t.Fatalf("VertexServiceAccountOpenAPIBaseURL(global) = %q", got) + } + if got := VertexServiceAccountOpenAPIBaseURL("", "v1", "my-proj", "us-central1"); got != "https://us-central1-aiplatform.googleapis.com/v1/projects/my-proj/locations/us-central1/endpoints/openapi" { + t.Fatalf("VertexServiceAccountOpenAPIBaseURL(us-central1) = %q", got) + } + if got := VertexAPIKeyOpenAPIBaseURL("https://example.com/v1/projects/my-proj/locations/global", "v1", "", ""); got != "https://example.com/v1/projects/my-proj/locations/global/endpoints/openapi" { + t.Fatalf("VertexAPIKeyOpenAPIBaseURL(project/location URL) = %q", got) + } + if got := VertexAPIKeyOpenAPIBaseURL("https://example.com/api", "v1", "", ""); got != "https://example.com/api/v1/endpoints/openapi" { + t.Fatalf("VertexAPIKeyOpenAPIBaseURL(relay URL) = %q", got) + } + if !IsGoogleVertexHost("") || !IsGoogleVertexHost("https://aiplatform.googleapis.com/v1") || !IsGoogleVertexHost("https://us-central1-aiplatform.googleapis.com") { + t.Fatal("expected Google Vertex hosts to be detected") + } + if IsGoogleVertexHost("https://zenmux.ai/api") { + t.Fatal("expected third-party host not to be detected as Google Vertex host") + } +} diff --git a/internal/runtime/executor/openai_compat_executor.go b/internal/runtime/executor/openai_compat_executor.go index c47ffc2c06e..fc8169ea1aa 100644 --- a/internal/runtime/executor/openai_compat_executor.go +++ b/internal/runtime/executor/openai_compat_executor.go @@ -90,13 +90,19 @@ func (e *OpenAICompatExecutor) Execute(ctx context.Context, auth *cliproxyauth.A if endpointPath := openAICompatImageEndpointPath(opts); endpointPath != "" { return e.executeImages(ctx, auth, req, opts, endpointPath) } + baseURL, apiKey := e.resolveCredentials(auth) + compat := e.resolveCompatConfig(auth, req) + return e.executeWithCredentials(ctx, auth, req, opts, baseURL, apiKey, compat) +} + +func (e *OpenAICompatExecutor) executeWithCredentials(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, baseURL, apiKey string, compat *config.OpenAICompatibility) (resp cliproxyexecutor.Response, err error) { + ctx = helps.EnsureSessionContext(ctx, opts, req.Payload) baseModel := thinking.ParseSuffix(req.Model).ModelName reporter := helps.NewExecutorUsageReporter(ctx, e, baseModel, auth) defer reporter.TrackFailure(ctx, &err) - baseURL, apiKey := e.resolveCredentials(auth) if baseURL == "" { err = statusErr{code: http.StatusUnauthorized, msg: "missing provider baseURL"} return @@ -131,13 +137,13 @@ func (e *OpenAICompatExecutor) Execute(ctx context.Context, auth *cliproxyauth.A requestedModel := helps.PayloadRequestedModel(opts, req.Model) requestPath := helps.PayloadRequestPath(opts) - if helps.ShouldNormalizeOpenAIToolResultsForModel(e.resolveCompatConfig(auth, req), baseModel, requestedModel) { + if helps.ShouldNormalizeOpenAIToolResultsForModel(compat, baseModel, requestedModel) { translated = helps.NormalizeOpenAIToolResultsTextOnly(translated) } if opts.Alt != "responses/compact" { - useMCT := helps.ShouldUseMaxCompletionTokensForModel(e.resolveCompatConfig(auth, req), baseModel, requestedModel) + useMCT := helps.ShouldUseMaxCompletionTokensForModel(compat, baseModel, requestedModel) translated = helps.NormalizeOpenAIMaxTokens(translated, useMCT) - translated, err = e.applyPromptCacheKey(ctx, auth, from, baseModel, req, opts, translated) + translated, err = e.applyPromptCacheKeyWithCompat(ctx, auth, compat, from, baseModel, req, opts, translated) if err != nil { return resp, err } @@ -325,13 +331,19 @@ func (e *OpenAICompatExecutor) ExecuteStream(ctx context.Context, auth *cliproxy if endpointPath := openAICompatImageEndpointPath(opts); endpointPath != "" { return e.executeImagesStream(ctx, auth, req, opts, endpointPath) } + baseURL, apiKey := e.resolveCredentials(auth) + compat := e.resolveCompatConfig(auth, req) + return e.executeStreamWithCredentials(ctx, auth, req, opts, baseURL, apiKey, compat) +} + +func (e *OpenAICompatExecutor) executeStreamWithCredentials(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, baseURL, apiKey string, compat *config.OpenAICompatibility) (_ *cliproxyexecutor.StreamResult, err error) { + ctx = helps.EnsureSessionContext(ctx, opts, req.Payload) baseModel := thinking.ParseSuffix(req.Model).ModelName reporter := helps.NewExecutorUsageReporter(ctx, e, baseModel, auth) defer reporter.TrackFailure(ctx, &err) - baseURL, apiKey := e.resolveCredentials(auth) if baseURL == "" { err = statusErr{code: http.StatusUnauthorized, msg: "missing provider baseURL"} return nil, err @@ -361,13 +373,13 @@ func (e *OpenAICompatExecutor) ExecuteStream(ctx context.Context, auth *cliproxy requestedModel := helps.PayloadRequestedModel(opts, req.Model) requestPath := helps.PayloadRequestPath(opts) - if helps.ShouldNormalizeOpenAIToolResultsForModel(e.resolveCompatConfig(auth, req), baseModel, requestedModel) { + if helps.ShouldNormalizeOpenAIToolResultsForModel(compat, baseModel, requestedModel) { translated = helps.NormalizeOpenAIToolResultsTextOnly(translated) } if opts.Alt != "responses/compact" { - useMCT := helps.ShouldUseMaxCompletionTokensForModel(e.resolveCompatConfig(auth, req), baseModel, requestedModel) + useMCT := helps.ShouldUseMaxCompletionTokensForModel(compat, baseModel, requestedModel) translated = helps.NormalizeOpenAIMaxTokens(translated, useMCT) - translated, err = e.applyPromptCacheKey(ctx, auth, from, baseModel, req, opts, translated) + translated, err = e.applyPromptCacheKeyWithCompat(ctx, auth, compat, from, baseModel, req, opts, translated) if err != nil { return nil, err } @@ -947,7 +959,10 @@ func rewriteOpenAICompatImagesMultipartPayload(payload []byte, model string, bou } func (e *OpenAICompatExecutor) applyPromptCacheKey(ctx context.Context, auth *cliproxyauth.Auth, from sdktranslator.Format, baseModel string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, translated []byte) ([]byte, error) { - compat := e.resolveCompatConfig(auth, req) + return e.applyPromptCacheKeyWithCompat(ctx, auth, e.resolveCompatConfig(auth, req), from, baseModel, req, opts, translated) +} + +func (e *OpenAICompatExecutor) applyPromptCacheKeyWithCompat(ctx context.Context, auth *cliproxyauth.Auth, compat *config.OpenAICompatibility, from sdktranslator.Format, baseModel string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, translated []byte) ([]byte, error) { if compat == nil || !compat.SupportPromptCacheKey { return translated, nil } diff --git a/internal/watcher/synthesizer/file.go b/internal/watcher/synthesizer/file.go index 0ceb9eb6ef2..07ec2f55ac2 100644 --- a/internal/watcher/synthesizer/file.go +++ b/internal/watcher/synthesizer/file.go @@ -240,6 +240,7 @@ func synthesizeFileAuths(ctx *SynthesisContext, fullPath string, data []byte) ([ } coreauth.ApplyCustomHeadersFromMetadata(a) coreauth.SetOAuthModelAliasesAttribute(a, perAccountModelAliases) + applyOpenAPIModelsAttribute(a, metadata) ApplyAuthExcludedModelsMeta(a, perAccountExcluded, "oauth") applyFingerprintProfileAttribute(a, metadata) // For Kimi auth files, preserve domain and base_url attributes. @@ -309,34 +310,93 @@ func compactPluginAuths(auths []*coreauth.Auth) []*coreauth.Auth { // extractOAuthModelAliasesFromMetadata reads per-account model aliases from OAuth JSON metadata. // "model_aliases" is canonical; "model-aliases" remains a legacy alias. +// Entries under "models" that specify both "name" and "alias" are also accepted. func extractOAuthModelAliasesFromMetadata(metadata map[string]any) []config.OAuthModelAlias { if metadata == nil { return nil } - raw, ok := metadata["model_aliases"] - if !ok { - raw, ok = metadata["model-aliases"] - } - if !ok || raw == nil { - return nil - } - data, errMarshal := json.Marshal(raw) - if errMarshal != nil { - return nil + var combined []config.OAuthModelAlias + for _, key := range []string{"model_aliases", "model-aliases", "models"} { + raw, ok := metadata[key] + if !ok || raw == nil { + continue + } + data, errMarshal := json.Marshal(raw) + if errMarshal != nil { + continue + } + var aliases []config.OAuthModelAlias + if errUnmarshal := json.Unmarshal(data, &aliases); errUnmarshal != nil { + continue + } + combined = append(combined, aliases...) } - var aliases []config.OAuthModelAlias - if errUnmarshal := json.Unmarshal(data, &aliases); errUnmarshal != nil { + if len(combined) == 0 { return nil } cfg := config.Config{ OAuthModelAlias: map[string][]config.OAuthModelAlias{ - "auth": aliases, + "auth": combined, }, } cfg.SanitizeOAuthModelAlias() return cfg.OAuthModelAlias["auth"] } +func applyOpenAPIModelsAttribute(auth *coreauth.Auth, metadata map[string]any) { + if auth == nil || metadata == nil { + return + } + var names []string + seen := make(map[string]struct{}) + add := func(raw string) { + trimmed := strings.TrimSpace(raw) + if trimmed == "" { + return + } + key := strings.ToLower(trimmed) + if _, exists := seen[key]; exists { + return + } + seen[key] = struct{}{} + names = append(names, trimmed) + } + for _, key := range []string{"openapi_models", "openapi-models", "models"} { + raw, ok := metadata[key] + if !ok || raw == nil { + continue + } + switch v := raw.(type) { + case string: + for _, part := range strings.Split(v, ",") { + add(part) + } + case []string: + for _, item := range v { + add(item) + } + case []any: + for _, item := range v { + switch elem := item.(type) { + case string: + add(elem) + case map[string]any: + if name, okName := elem["name"].(string); okName { + add(name) + } + } + } + } + } + if len(names) == 0 { + return + } + if auth.Attributes == nil { + auth.Attributes = make(map[string]string) + } + auth.Attributes["openapi_models"] = strings.Join(names, ",") +} + // ExtractExcludedModelsFromMetadata reads per-account excluded models from the OAuth JSON metadata. // "excluded_models" is canonical; "excluded-models" remains a legacy alias. The value must be a // string array; a single string is also accepted for older hand-written files and is split on diff --git a/sdk/cliproxy/auth/classification.go b/sdk/cliproxy/auth/classification.go index f8de9c8c9b9..ced4d0bf8fb 100644 --- a/sdk/cliproxy/auth/classification.go +++ b/sdk/cliproxy/auth/classification.go @@ -125,6 +125,9 @@ func authHasOAuthMetadata(auth *Auth) bool { if siwc, ok := auth.Metadata["siwc"].(map[string]any); ok && len(siwc) > 0 { return true } + if sa, ok := auth.Metadata["service_account"].(map[string]any); ok && len(sa) > 0 { + return true + } return false } diff --git a/sdk/cliproxy/auth/metadata_keys.go b/sdk/cliproxy/auth/metadata_keys.go index e861cfedb3a..ffb662ac2c5 100644 --- a/sdk/cliproxy/auth/metadata_keys.go +++ b/sdk/cliproxy/auth/metadata_keys.go @@ -16,6 +16,10 @@ func CanonicalCredentialMetadataKey(key string) string { return "fingerprint_profile" case "model-aliases": return "model_aliases" + case "openapi-location": + return "openapi_location" + case "openapi-models": + return "openapi_models" case "proxy-url": return "proxy_url" case "request-retry": diff --git a/sdk/cliproxy/auth/types.go b/sdk/cliproxy/auth/types.go index b09ec308e1a..179c6363d5e 100644 --- a/sdk/cliproxy/auth/types.go +++ b/sdk/cliproxy/auth/types.go @@ -772,6 +772,14 @@ func (a *Auth) AccountInfo() (string, string) { return "oauth", email } } + if sa, ok := a.Metadata["service_account"].(map[string]any); ok { + if v, okEmail := sa["client_email"].(string); okEmail { + email := strings.TrimSpace(v) + if email != "" { + return "oauth", email + } + } + } } return "oauth", "" case AuthKindAPIKey: diff --git a/sdk/cliproxy/service_models.go b/sdk/cliproxy/service_models.go index 3b4cfc9b319..7e478af9420 100644 --- a/sdk/cliproxy/service_models.go +++ b/sdk/cliproxy/service_models.go @@ -120,6 +120,8 @@ func (s *Service) registerModelsForAuthWithConfigAndCache(ctx context.Context, c if authKind == "apikey" { excluded = entry.ExcludedModels } + } else { + models = appendVertexServiceAccountModels(cfg, a, models) } models = applyExcludedModels(models, excluded) case "aistudio": @@ -945,6 +947,106 @@ func buildVertexCompatConfigModels(entry *config.VertexCompatKey) []*ModelInfo { return buildConfigModels(entry.Models, "google", "vertex", "vertex") } +func appendVertexServiceAccountModels(cfg *config.Config, a *coreauth.Auth, baseModels []*ModelInfo) []*ModelInfo { + if a == nil { + return baseModels + } + seen := make(map[string]struct{}, len(baseModels)) + for _, model := range baseModels { + if model == nil { + continue + } + if id := strings.ToLower(strings.TrimSpace(model.ID)); id != "" { + seen[id] = struct{}{} + } + } + + var extraNames []string + addName := func(raw string) { + trimmed := strings.TrimSpace(raw) + if trimmed == "" { + return + } + key := strings.ToLower(trimmed) + if _, exists := seen[key]; exists { + return + } + seen[key] = struct{}{} + extraNames = append(extraNames, trimmed) + } + + if a.Attributes != nil { + if raw := strings.TrimSpace(a.Attributes["openapi_models"]); raw != "" { + for _, part := range strings.Split(raw, ",") { + addName(part) + } + } + } + if a.Metadata != nil { + for _, key := range []string{"openapi_models", "openapi-models", "models"} { + raw, ok := a.Metadata[key] + if !ok || raw == nil { + continue + } + switch v := raw.(type) { + case string: + for _, part := range strings.Split(v, ",") { + addName(part) + } + case []string: + for _, item := range v { + addName(item) + } + case []any: + for _, item := range v { + switch elem := item.(type) { + case string: + addName(elem) + case map[string]any: + if name, okName := elem["name"].(string); okName { + addName(name) + } + } + } + } + } + } + + for _, aliasEntry := range oauthModelAliasesForAuth(cfg, "vertex", a.Attributes) { + addName(aliasEntry.Name) + } + + if len(extraNames) == 0 { + return baseModels + } + + now := time.Now().Unix() + out := make([]*ModelInfo, 0, len(baseModels)+len(extraNames)) + out = append(out, baseModels...) + for _, name := range extraNames { + ownedBy := "google" + if publisher, _, ok := strings.Cut(name, "/"); ok && strings.TrimSpace(publisher) != "" { + ownedBy = strings.TrimSpace(publisher) + } + info := &ModelInfo{ + ID: name, + MetadataModelID: name, + Object: "model", + Created: now, + OwnedBy: ownedBy, + Type: "vertex", + DisplayName: name, + UserDefined: true, + Thinking: ®istry.ThinkingSupport{Levels: []string{"low", "medium", "high"}}, + } + if resolved := modelconfig.ResolveModelInfo(name, "vertex", nil); resolved.Thinking != nil { + info.Thinking = resolved.Thinking + } + out = append(out, info) + } + return out +} + func buildGeminiConfigModels(entry *config.GeminiKey) []*ModelInfo { if entry == nil { return nil diff --git a/sdk/cliproxy/service_vertex_openapi_models_test.go b/sdk/cliproxy/service_vertex_openapi_models_test.go new file mode 100644 index 00000000000..134cb85a263 --- /dev/null +++ b/sdk/cliproxy/service_vertex_openapi_models_test.go @@ -0,0 +1,119 @@ +package cliproxy + +import ( + "context" + "testing" + "time" + + "github.com/router-for-me/CLIProxyAPI/v8/internal/config" + "github.com/router-for-me/CLIProxyAPI/v8/internal/watcher/synthesizer" + coreauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth" +) + +func TestRegisterModelsForAuth_VertexServiceAccount_OpenAPIModelsAndAliases(t *testing.T) { + cfg := &config.Config{ + OAuthModelAlias: map[string][]config.OAuthModelAlias{ + "vertex": { + { + Name: "deepseek-ai/deepseek-v4.1-flash-maas", + Alias: "deepseek-flash", + }, + }, + }, + } + cfg.SanitizeOAuthModelAlias() + + manager := coreauth.NewManager(nil, nil, nil) + manager.SetOAuthModelAlias(cfg.OAuthModelAlias) + service := &Service{ + cfg: cfg, + coreManager: manager, + } + + auth := &coreauth.Auth{ + ID: "vertex-sa-maas-test", + Provider: "vertex", + Status: coreauth.StatusActive, + Attributes: map[string]string{ + "auth_kind": "oauth", + }, + Metadata: map[string]any{ + "type": "vertex", + "project_id": "test-proj", + "location": "global", + "openapi_models": []any{ + "qwen/qwen3-coder-480b-a35b-instruct-maas", + }, + }, + } + t.Cleanup(func() { + GlobalModelRegistry().UnregisterClient(auth.ID) + }) + + registered, errReg := service.coreManager.Register(context.Background(), auth) + if errReg != nil { + t.Fatalf("Register auth error: %v", errReg) + } + service.registerModelsForAuth(context.Background(), registered) + + if !GlobalModelRegistry().ClientSupportsModel(auth.ID, "deepseek-flash") { + t.Fatalf("expected Vertex SA auth to support aliased MaaS model 'deepseek-flash'") + } + if !GlobalModelRegistry().ClientSupportsModel(auth.ID, "qwen/qwen3-coder-480b-a35b-instruct-maas") { + t.Fatalf("expected Vertex SA auth to support openapi_models entry 'qwen/qwen3-coder-480b-a35b-instruct-maas'") + } + if !GlobalModelRegistry().ClientSupportsModel(auth.ID, "gemini-2.5-pro") { + t.Fatalf("expected Vertex SA auth to retain native Gemini model 'gemini-2.5-pro'") + } + if gotUpstream := service.coreManager.ResolveExecutionModel(registered, "deepseek-flash"); gotUpstream != "deepseek-ai/deepseek-v4.1-flash-maas" { + t.Fatalf("ResolveExecutionModel(deepseek-flash) = %q, want deepseek-ai/deepseek-v4.1-flash-maas", gotUpstream) + } +} + +func TestSynthesizeAuthFile_VertexModelsAndAliases(t *testing.T) { + rawJSON := []byte(`{ + "type": "vertex", + "project_id": "test-proj", + "location": "global", + "models": [ + {"name": "deepseek-ai/deepseek-v4.1-flash-maas", "alias": "ds-maas"}, + {"name": "xai/grok-4-maas"} + ], + "service_account": {"client_email": "sa@test.iam.gserviceaccount.com"} + }`) + + auths, errSynth := synthesizer.SynthesizeAuthFile(&synthesizer.SynthesisContext{ + Now: time.Unix(1700000000, 0), + }, "/tmp/vertex-test.json", rawJSON) + if errSynth != nil { + t.Fatalf("SynthesizeAuthFile error: %v", errSynth) + } + if len(auths) != 1 { + t.Fatalf("expected 1 synthesized auth, got %d", len(auths)) + } + + service := &Service{ + cfg: &config.Config{}, + coreManager: coreauth.NewManager(nil, nil, nil), + } + auth := auths[0] + t.Cleanup(func() { + GlobalModelRegistry().UnregisterClient(auth.ID) + }) + + registered, errReg := service.coreManager.Register(context.Background(), auth) + if errReg != nil { + t.Fatalf("Register auth error: %v", errReg) + } + service.registerModelsForAuth(context.Background(), registered) + + if !GlobalModelRegistry().ClientSupportsModel(auth.ID, "ds-maas") { + t.Fatalf("expected synthesized Vertex SA auth to support alias 'ds-maas'") + } + if !GlobalModelRegistry().ClientSupportsModel(auth.ID, "xai/grok-4-maas") { + t.Fatalf("expected synthesized Vertex SA auth to support direct model 'xai/grok-4-maas'") + } + if gotUpstream := service.coreManager.ResolveExecutionModel(registered, "ds-maas"); gotUpstream != "deepseek-ai/deepseek-v4.1-flash-maas" { + t.Fatalf("ResolveExecutionModel(ds-maas) = %q, want deepseek-ai/deepseek-v4.1-flash-maas", gotUpstream) + } +} From 084b9ca71737602df7be7a5daaebb975f0fb19ca Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Mon, 19 Oct 2026 12:59:12 +0800 Subject: [PATCH 23/27] feat(claude): strip Claude Code identity on API keys with cloak mode never or uncloak - Strip incoming Claude Code billing attribution, CLI identity system blocks, metadata user IDs, and diagnostics when a non-CLI API key explicitly configures cloak mode as never or uncloak. - Remove Claude Code CLI headers, beta flags, and user-agent strings on uncloaked API key requests while preserving capability betas and credential header overrides. - Keep global disable-claude-cloak-mode passthrough behavior unchanged for credentials without explicit per-key cloak configuration. Closes: #6762 --- config.example.yaml | 2 +- internal/config/config_types.go | 4 +- .../executor/claude_executor_cloaking.go | 93 +++++- .../executor/claude_executor_request.go | 45 ++- .../executor/claude_executor_tokens.go | 2 + .../claude_fingerprint_policy_test.go | 291 ++++++++++++++++++ .../executor/helps/claude_client_detection.go | 5 + 7 files changed, 430 insertions(+), 12 deletions(-) diff --git a/config.example.yaml b/config.example.yaml index 372af6e52a2..1a2f939fcdc 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -728,7 +728,7 @@ upstream: # cloak: # optional: explicitly enable request cloaking for non-Claude-Code clients # mode: "auto" # "auto" (default inside this block): cloak only when client is not Claude Code # # "always": cloak every unconfirmed client; confirmed native Claude Code still passes through -# # "never": never apply cloaking +# # "never" / "uncloak": never apply cloaking; on non-CLI API keys, also strip incoming Claude Code identity/billing attribution # # This "cloak" block applies to this api-keys.claude key entry only. For Claude OAuth # # credentials, set the same options in the auth/token JSON file via "cloak_mode" / # # "cloak_strict_mode" / "cloak_sensitive_words" / "cloak_cache_user_id". diff --git a/internal/config/config_types.go b/internal/config/config_types.go index 5205bf2f2ee..2f412157da9 100644 --- a/internal/config/config_types.go +++ b/internal/config/config_types.go @@ -564,11 +564,11 @@ type PayloadModelRule struct { // CloakConfig configures request cloaking for non-Claude-Code clients. // Cloaking disguises API requests to appear as originating from the official Claude Code CLI. type CloakConfig struct { - // Mode controls cloaking behavior: "auto" (default), "always", or "never". + // Mode controls cloaking behavior: "auto" (default), "always", "never", or "uncloak". // Supplying this CloakConfig explicitly enables cloaking for an unprofiled API key. // - "auto": cloak unless strong request signals identify a verified native entrypoint // - "always": cloak every unconfirmed client; confirmed native Claude Code remains passthrough - // - "never": never apply cloaking + // - "never" / "uncloak": never apply cloaking; on non-CLI API keys, also strip incoming Claude Code identity and billing attribution Mode string `yaml:"mode,omitempty" json:"mode,omitempty"` // StrictMode controls how caller system prompts are handled when cloaking. diff --git a/internal/runtime/executor/claude_executor_cloaking.go b/internal/runtime/executor/claude_executor_cloaking.go index 1f57ee5cb11..42c090762e2 100644 --- a/internal/runtime/executor/claude_executor_cloaking.go +++ b/internal/runtime/executor/claude_executor_cloaking.go @@ -1584,6 +1584,7 @@ type claudeWirePolicy struct { ProfileClaudeCodeCLI bool // request fingerprint looks like Claude Code CLI ConfirmedClaudeCode bool Cloak bool + Uncloak bool } type claudeCloakSettings struct { @@ -1592,6 +1593,79 @@ type claudeCloakSettings struct { cacheUserID bool } +func isClaudeCodeIdentitySystemText(text string) bool { + trimmed := strings.TrimSpace(text) + return trimmed == claudeCodeCLIIdentity || + trimmed == "You are a Claude agent, built on Anthropic's Claude Agent SDK." +} + +func stripClaudeCodeSystemBlocks(payload []byte) []byte { + payload = util.StripClaudeCodeAttributionSystem(payload) + system := gjson.GetBytes(payload, "system") + if !system.Exists() { + return payload + } + if system.Type == gjson.String { + if !isClaudeCodeIdentitySystemText(system.String()) { + return payload + } + if updated, errDelete := sjson.DeleteBytes(payload, "system"); errDelete == nil { + return updated + } + return payload + } + if !system.IsArray() { + return payload + } + kept := make([]string, 0, len(system.Array())) + removed := false + system.ForEach(func(_, block gjson.Result) bool { + if block.Get("type").String() == "text" && isClaudeCodeIdentitySystemText(block.Get("text").String()) { + removed = true + return true + } + if block.Raw != "" { + kept = append(kept, block.Raw) + } + return true + }) + if !removed { + return payload + } + if len(kept) == 0 { + if updated, errDelete := sjson.DeleteBytes(payload, "system"); errDelete == nil { + return updated + } + return payload + } + if updated, errSet := sjson.SetRawBytes(payload, "system", []byte("["+strings.Join(kept, ",")+"]")); errSet == nil { + return updated + } + return payload +} + +func uncloakClaudeCodePayload(payload []byte, confirmedClaudeCode bool) []byte { + payload = stripClaudeCodeSystemBlocks(payload) + if metadata := gjson.GetBytes(payload, "metadata"); metadata.Exists() && metadata.IsObject() { + if userID := metadata.Get("user_id"); userID.Exists() && (confirmedClaudeCode || helps.IsValidUserID(userID.String())) { + if updated, errDelete := sjson.DeleteBytes(payload, "metadata.user_id"); errDelete == nil { + payload = updated + if remaining := gjson.GetBytes(payload, "metadata"); remaining.Exists() && remaining.IsObject() && len(remaining.Map()) == 0 { + if updatedMeta, errDeleteMeta := sjson.DeleteBytes(payload, "metadata"); errDeleteMeta == nil { + payload = updatedMeta + } + } + } + } + } + if diag := gjson.GetBytes(payload, "diagnostics"); diag.Exists() && (confirmedClaudeCode || diag.Get("previous_message_id").Exists()) { + if updated, errDelete := sjson.DeleteBytes(payload, "diagnostics"); errDelete == nil { + payload = updated + } + } + return payload +} + func resolveClaudeWirePolicy(cfg *config.Config, auth *cliproxyauth.Auth, apiKey string, confirmedClaudeCode bool) (claudeWirePolicy, claudeCloakSettings) { if auth != nil && auth.AuthKind() == cliproxyauth.AuthKindAPIKey { cfg = cfg.ForAPIKey() @@ -1608,12 +1682,14 @@ func resolveClaudeWirePolicy(cfg *config.Config, auth *cliproxyauth.Auth, apiKey sensitiveWords: attrWords, cacheUserID: attrCache, } + explicitMode := strings.ToLower(strings.TrimSpace(attrMode)) if attrMode != "" { cloakMode = attrMode } if cloakCfg != nil { if mode := strings.TrimSpace(cloakCfg.Mode); mode != "" { cloakMode = mode + explicitMode = strings.ToLower(mode) } if cloakCfg.StrictMode { settings.strictMode = true @@ -1628,23 +1704,25 @@ func resolveClaudeWirePolicy(cfg *config.Config, auth *cliproxyauth.Auth, apiKey fp := resolveClaudeFingerprintPolicy(cfg, auth, apiKey) cloakConfigured := cloakCfg != nil || attrMode != "" || attrStrict || len(attrWords) > 0 || attrCache + trimmedMode := strings.ToLower(strings.TrimSpace(cloakMode)) + uncloak := !fp.ProfileClaudeCodeCLI && (explicitMode == "never" || explicitMode == "uncloak") policy := claudeWirePolicy{ OAuth: fp.AuthIsOAuthToken, ProfileClaudeCodeCLI: fp.ProfileClaudeCodeCLI, ConfirmedClaudeCode: confirmedClaudeCode, - Cloak: (fp.ProfileClaudeCodeCLI || cloakConfigured) && !confirmedClaudeCode, + Cloak: (fp.ProfileClaudeCodeCLI || cloakConfigured) && !confirmedClaudeCode && !uncloak, + Uncloak: uncloak, } if confirmedClaudeCode { - // Native Claude Code is always a passthrough client. An operator-level - // "always" mode may cloak unknown callers, but must not overwrite a - // strongly confirmed CLI, sdk-cli, or claude-vscode fingerprint. + // Native Claude Code is always a passthrough client unless the + // credential explicitly opts out of Claude Code identity via never/uncloak. policy.Cloak = false return policy, settings } - switch strings.ToLower(strings.TrimSpace(cloakMode)) { + switch trimmedMode { case "always": policy.Cloak = true - case "never": + case "never", "uncloak": policy.Cloak = false default: // Auto applies the CLI cloak only to real Claude OAuth credentials, @@ -1681,6 +1759,9 @@ func applyCloakingInternal( ) ([]byte, bool, error) { policy, settings := resolveClaudeWirePolicy(cfg, auth, apiKey, confirmedClaudeCode) if !policy.Cloak { + if policy.Uncloak { + payload = uncloakClaudeCodePayload(payload, confirmedClaudeCode) + } return payload, false, nil } isExplicit := (len(explicitCacheMode) > 0 && explicitCacheMode[0]) || isExplicitPromptCacheModeContext(ctx) diff --git a/internal/runtime/executor/claude_executor_request.go b/internal/runtime/executor/claude_executor_request.go index 3c92af6b944..dd9f4b58d27 100644 --- a/internal/runtime/executor/claude_executor_request.go +++ b/internal/runtime/executor/claude_executor_request.go @@ -1144,7 +1144,7 @@ func claudeCredentialUsesOAuth(auth *cliproxyauth.Auth, apiKey string) bool { return !hasAPIKeyAttr } -func copyClaudeCallerFingerprintHeaders(dst, src http.Header, confirmedClaudeCode bool) { +func copyClaudeCallerFingerprintHeaders(dst, src http.Header, confirmedClaudeCode, uncloak bool) { if dst == nil || src == nil { return } @@ -1163,6 +1163,21 @@ func copyClaudeCallerFingerprintHeaders(dst, src http.Header, confirmedClaudeCod if !confirmedClaudeCode && (strings.HasPrefix(lowerName, "x-claude-code-") || strings.HasPrefix(lowerName, "x-claude-remote-")) { continue } + if uncloak { + if lowerName == "x-app" || + lowerName == "x-client-request-id" || + lowerName == "x-client-app" || + lowerName == "x-anthropic-additional-protection" || + lowerName == "anthropic-dangerous-direct-browser-access" || + strings.HasPrefix(lowerName, "x-stainless-") || + strings.HasPrefix(lowerName, "x-claude-code-") || + strings.HasPrefix(lowerName, "x-claude-remote-") { + continue + } + if lowerName == "user-agent" && len(values) > 0 && helps.IsClaudeCodeUserAgent(values[0]) { + continue + } + } dst.Del(name) for _, value := range values { dst.Add(name, value) @@ -1221,6 +1236,11 @@ func applyClaudeHeadersWithNativeProfile( useAPIKey := !credentialUsesBearer fp := resolveClaudeFingerprintPolicy(cfg, auth, apiKey) wirePolicy, _ := resolveClaudeWirePolicy(cfg, auth, apiKey, confirmedClaudeCode) + if wirePolicy.Uncloak { + confirmedClaudeCode = false + helperProfile = false + sessionIDs = nil + } applyCLIFingerprint := fp.ProfileClaudeCodeCLI || wirePolicy.Cloak preserveCallerFingerprint := !applyCLIFingerprint && !confirmedClaudeCode useOAuthBetas := fp.UseOAuthBetas @@ -1356,6 +1376,21 @@ func applyClaudeHeadersWithNativeProfile( } } applyBetaHeader := func() { + if wirePolicy.Uncloak { + baseBetas = withoutClaudeBeta(baseBetas, claudeCodeBeta) + baseBetas = withoutClaudeBeta(baseBetas, claudeOAuthBeta) + baseBetas = withoutClaudeBeta(baseBetas, claudeRedactThinkingBeta) + baseBetas = withoutClaudeBeta(baseBetas, claudeAFKModeBeta) + if !gjson.GetBytes(body, "diagnostics").Exists() { + baseBetas = withoutClaudeBeta(baseBetas, claudeCacheDiagnosisBeta) + } + if !gjson.GetBytes(body, "fallback_credit_token").Exists() { + baseBetas = withoutClaudeBeta(baseBetas, claudeFallbackCreditBeta) + } + if !helps.ClaudePayloadHas1hTTL(body) { + baseBetas = withoutClaudeBeta(baseBetas, claudeExtendedCacheTTLBeta) + } + } // Enforce strict native Claude Code 2.1.280 model & turn beta gating: if !claudeRequestSupportsEffort(body, nil) { baseBetas = withoutClaudeBeta(baseBetas, claudeEffortBeta) @@ -1412,7 +1447,7 @@ func applyClaudeHeadersWithNativeProfile( defaultAccept = "text/event-stream" defaultAcceptEncoding = "identity" } - copyClaudeCallerFingerprintHeaders(r.Header, incomingHeaders, confirmedClaudeCode) + copyClaudeCallerFingerprintHeaders(r.Header, incomingHeaders, confirmedClaudeCode, wirePolicy.Uncloak) if hd.ClientRequestID == config.ClaudeClientRequestIDOff { r.Header.Del("X-Client-Request-Id") } @@ -1423,7 +1458,11 @@ func applyClaudeHeadersWithNativeProfile( // sent none must not fall through to Go's transport default // ("Go-http-client/1.1"), which upstreams read as a bot signature. Identify // as CPA instead: honest about the hop, and not a fabricated client. - misc.EnsureHeader(r.Header, incomingHeaders, "User-Agent", "CLIProxyAPI/"+buildinfo.Version) + uaSourceHeaders := incomingHeaders + if wirePolicy.Uncloak && helps.IsClaudeCodeUserAgent(helps.HeaderValueCaseInsensitive(incomingHeaders, "User-Agent")) { + uaSourceHeaders = nil + } + misc.EnsureHeader(r.Header, uaSourceHeaders, "User-Agent", "CLIProxyAPI/"+buildinfo.Version) applyBetaHeader() sessionID := "" for _, candidate := range sessionIDs { diff --git a/internal/runtime/executor/claude_executor_tokens.go b/internal/runtime/executor/claude_executor_tokens.go index fc5011d260a..8d2c9c07730 100644 --- a/internal/runtime/executor/claude_executor_tokens.go +++ b/internal/runtime/executor/claude_executor_tokens.go @@ -200,6 +200,8 @@ func (e *ClaudeExecutor) countTokensUpstream(ctx context.Context, auth *cliproxy if len(settings.sensitiveWords) > 0 { body = helps.ObfuscateSensitiveWords(body, helps.BuildSensitiveWordMatcher(settings.sensitiveWords)) } + } else if policy.Uncloak { + body = uncloakClaudeCodePayload(body, confirmedClaudeCode) } // Keep count_tokens requests compatible with Anthropic cache-control constraints too. diff --git a/internal/runtime/executor/claude_fingerprint_policy_test.go b/internal/runtime/executor/claude_fingerprint_policy_test.go index c56fcc9ff2f..b071dda46bd 100644 --- a/internal/runtime/executor/claude_fingerprint_policy_test.go +++ b/internal/runtime/executor/claude_fingerprint_policy_test.go @@ -1243,3 +1243,294 @@ func claudeFingerprintHeaderValue(headers http.Header, name string) string { } return "" } + +func TestClaudeExecutor_OfficialAPIKeyCloakNeverStripsClaudeCodeIdentity(t *testing.T) { + const sessionID = "11111111-2222-4333-8444-555555555555" + const userID = `{"device_id":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","account_uuid":"11111111-2222-4333-8444-555555555555","session_id":"11111111-2222-4333-8444-555555555555"}` + const billing = "x-anthropic-billing-header: cc_version=2.1.280.d7b; cc_entrypoint=cli; cch=00000; cc_turn_origin=human;" + incoming := http.Header{ + "User-Agent": {"claude-cli/2.1.280 (external, cli)"}, + "X-App": {"cli"}, + "Anthropic-Beta": {"claude-code-20250219,oauth-2025-04-20,interleaved-thinking-2025-05-14,redact-thinking-2026-02-12,thinking-token-count-2026-05-13,context-management-2025-06-27,prompt-caching-scope-2026-01-05,effort-2025-11-24,cache-diagnosis-2026-04-07"}, + "Anthropic-Dangerous-Direct-Browser-Access": {"true"}, + "X-Claude-Code-Session-Id": {sessionID}, + "X-Claude-Code-Request-Class": {"main"}, + "X-Stainless-Package-Version": {"0.112.1"}, + "X-Stainless-Runtime-Version": {"v26.3.0"}, + "X-Stainless-Os": {"MacOS"}, + "X-Stainless-Arch": {"arm64"}, + } + payload := []byte( + `{"model":"claude-opus-4-6","max_tokens":64,"system":[{"type":"text","text":"` + billing + `"},{"type":"text","text":"You are Claude Code, Anthropic's official CLI for Claude.","cache_control":{"type":"ephemeral"}},{"type":"text","text":"You are an interactive CLI tool that helps users with software engineering tasks.","cache_control":{"type":"ephemeral"}}],"messages":[{"role":"user","content":"hello"}],"metadata":{"user_id":` + `"` + strings.ReplaceAll(userID, `"`, `\"`) + `"` + `},"diagnostics":{"previous_message_id":null},"thinking":{"type":"adaptive"}}`, + ) + if detection := helps.DetectClaudeCodeRequest(incoming, payload, false); !detection.Confirmed { + t.Fatal("fixture must be classified as a confirmed native Claude Code request") + } + + for _, tc := range []struct { + name string + mode string + customUA string + stream bool + countTokens bool + wantUserAgent string + wantBetaSubstr string + }{ + { + name: "execute cloak never default user-agent", + mode: "never", + wantUserAgent: "CLIProxyAPI/", + wantBetaSubstr: "interleaved-thinking-2025-05-14", + }, + { + name: "execute cloak never custom user-agent override", + mode: "never", + customUA: "my-api-client/1.0", + wantUserAgent: "my-api-client/1.0", + wantBetaSubstr: "interleaved-thinking-2025-05-14", + }, + { + name: "stream cloak never custom user-agent override", + mode: "never", + customUA: "my-api-client/1.0", + stream: true, + wantUserAgent: "my-api-client/1.0", + wantBetaSubstr: "interleaved-thinking-2025-05-14", + }, + { + name: "execute cloak uncloak mode", + mode: "uncloak", + customUA: "my-api-client/1.0", + wantUserAgent: "my-api-client/1.0", + wantBetaSubstr: "interleaved-thinking-2025-05-14", + }, + { + name: "count_tokens cloak never", + mode: "never", + customUA: "my-api-client/1.0", + countTokens: true, + wantUserAgent: "my-api-client/1.0", + wantBetaSubstr: "token-counting-2024-11-01", + }, + } { + t.Run(tc.name, func(t *testing.T) { + var seenBody []byte + var seenHeaders http.Header + transport := claudeFingerprintRoundTripperFunc(func(req *http.Request) (*http.Response, error) { + seenBody, _ = io.ReadAll(req.Body) + seenHeaders = req.Header.Clone() + if tc.countTokens { + return &http.Response{ + StatusCode: http.StatusOK, + Header: http.Header{"Content-Type": []string{"application/json"}}, + Body: io.NopCloser(strings.NewReader(`{"input_tokens":12}`)), + }, nil + } + if tc.stream { + return &http.Response{ + StatusCode: http.StatusOK, + Header: http.Header{"Content-Type": []string{"text/event-stream"}}, + Body: io.NopCloser(strings.NewReader( + "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":\"msg_1\"}}\n\nevent: message_stop\ndata: {\"type\":\"message_stop\"}\n\n", + )), + }, nil + } + return &http.Response{ + StatusCode: http.StatusOK, + Header: http.Header{"Content-Type": []string{"application/json"}}, + Body: io.NopCloser(strings.NewReader( + `{"id":"msg_1","type":"message","model":"claude-opus-4-6","role":"assistant","content":[{"type":"text","text":"ok"}],"usage":{"input_tokens":1,"output_tokens":1}}`, + )), + }, nil + }) + ctx := context.WithValue( + context.Background(), + "cliproxy.roundtripper", + http.RoundTripper(transport), + ) + + var customHeaders map[string]string + attrs := map[string]string{ + "api_key": "sk-ant-api03-promotional-key", + } + if tc.customUA != "" { + customHeaders = map[string]string{"User-Agent": tc.customUA} + attrs["header:User-Agent"] = tc.customUA + } + cfg := &config.Config{ClaudeKey: []config.ClaudeKey{{ + APIKey: "sk-ant-api03-promotional-key", + Cloak: &config.CloakConfig{Mode: tc.mode}, + Headers: customHeaders, + }}} + auth := &cliproxyauth.Auth{ + ID: "official-promotional-key", + Attributes: attrs, + } + exec := NewClaudeExecutor(cfg) + req := cliproxyexecutor.Request{ + Model: "claude-opus-4-6", + Payload: payload, + } + opts := cliproxyexecutor.Options{ + Stream: tc.stream, + SourceFormat: sdktranslator.FormatClaude, + OriginalRequest: payload, + Headers: incoming.Clone(), + } + + if tc.countTokens { + if _, errCount := exec.CountTokens(ctx, auth, req, opts); errCount != nil { + t.Fatalf("CountTokens() error = %v", errCount) + } + } else if tc.stream { + res, errStream := exec.ExecuteStream(ctx, auth, req, opts) + if errStream != nil { + t.Fatalf("ExecuteStream() error = %v", errStream) + } + for chunk := range res.Chunks { + if chunk.Err != nil { + t.Fatalf("stream chunk error = %v", chunk.Err) + } + } + } else { + if _, errExec := exec.Execute(ctx, auth, req, opts); errExec != nil { + t.Fatalf("Execute() error = %v", errExec) + } + } + + if strings.Contains(string(seenBody), "x-anthropic-billing-header:") || strings.Contains(string(seenBody), "cch=") { + t.Fatalf("upstream body still contains Claude Code billing header: %s", seenBody) + } + if strings.Contains(string(seenBody), claudeCodeCLIIdentity) { + t.Fatalf("upstream body still contains Claude Code CLI identity block: %s", seenBody) + } + if got := gjson.GetBytes(seenBody, "system.#").Int(); got != 1 { + t.Fatalf("system block count = %d, want 1 preserved instruction block: %s", got, seenBody) + } + if got := gjson.GetBytes(seenBody, "system.0.text").String(); got != "You are an interactive CLI tool that helps users with software engineering tasks." { + t.Fatalf("system.0.text = %q, want preserved caller instructions", got) + } + if gjson.GetBytes(seenBody, "metadata").Exists() { + t.Fatalf("metadata = %s, want Claude Code metadata stripped", gjson.GetBytes(seenBody, "metadata").Raw) + } + if gjson.GetBytes(seenBody, "diagnostics").Exists() { + t.Fatalf("diagnostics = %s, want Claude Code diagnostics stripped", gjson.GetBytes(seenBody, "diagnostics").Raw) + } + + ua := claudeFingerprintHeaderValue(seenHeaders, "User-Agent") + if strings.HasPrefix(strings.ToLower(ua), "claude-cli/") { + t.Fatalf("User-Agent = %q, must not forward claude-cli user-agent", ua) + } + if !strings.HasPrefix(ua, tc.wantUserAgent) { + t.Fatalf("User-Agent = %q, want prefix %q", ua, tc.wantUserAgent) + } + for _, forbiddenHdr := range []string{ + "X-App", + "Anthropic-Dangerous-Direct-Browser-Access", + "X-Claude-Code-Session-Id", + "X-Claude-Code-Request-Class", + "X-Stainless-Runtime", + "X-Stainless-Lang", + "X-Stainless-Package-Version", + } { + if got := claudeFingerprintHeaderValue(seenHeaders, forbiddenHdr); got != "" { + t.Fatalf("header %s = %q, want stripped", forbiddenHdr, got) + } + } + + betas := claudeFingerprintHeaderValue(seenHeaders, "Anthropic-Beta") + for _, forbiddenBeta := range []string{ + "claude-code-20250219", + "oauth-2025-04-20", + "redact-thinking-2026-02-12", + "cache-diagnosis-2026-04-07", + } { + if strings.Contains(betas, forbiddenBeta) { + t.Fatalf("Anthropic-Beta = %q, want %q stripped", betas, forbiddenBeta) + } + } + if !strings.Contains(betas, tc.wantBetaSubstr) { + t.Fatalf("Anthropic-Beta = %q, want capability beta %q preserved", betas, tc.wantBetaSubstr) + } + }) + } +} + +func TestClaudeExecutor_GlobalDisableClaudeCloakModePreservesConfirmedCallerIdentityWhenKeyUnconfigured(t *testing.T) { + const sessionID = "11111111-2222-4333-8444-555555555555" + const userID = `{"device_id":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","account_uuid":"11111111-2222-4333-8444-555555555555","session_id":"11111111-2222-4333-8444-555555555555"}` + const billing = "x-anthropic-billing-header: cc_version=2.1.280.d7b; cc_entrypoint=cli; cch=00000; cc_turn_origin=human;" + incoming := http.Header{ + "User-Agent": {"claude-cli/2.1.280 (external, cli)"}, + "X-App": {"cli"}, + "Anthropic-Beta": {"claude-code-20250219,interleaved-thinking-2025-05-14"}, + "Anthropic-Dangerous-Direct-Browser-Access": {"true"}, + "X-Claude-Code-Session-Id": {sessionID}, + "X-Stainless-Package-Version": {"0.112.1"}, + "X-Stainless-Runtime-Version": {"v26.3.0"}, + "X-Stainless-Os": {"MacOS"}, + "X-Stainless-Arch": {"arm64"}, + } + payload := []byte( + `{"model":"claude-opus-4-6","max_tokens":64,"system":[{"type":"text","text":"` + billing + `"},{"type":"text","text":"You are Claude Code, Anthropic's official CLI for Claude.","cache_control":{"type":"ephemeral"}}],"messages":[{"role":"user","content":"hello"}],"metadata":{"user_id":` + `"` + strings.ReplaceAll(userID, `"`, `\"`) + `"` + `}}`, + ) + + var seenBody []byte + var seenHeaders http.Header + transport := claudeFingerprintRoundTripperFunc(func(req *http.Request) (*http.Response, error) { + seenBody, _ = io.ReadAll(req.Body) + seenHeaders = req.Header.Clone() + return &http.Response{ + StatusCode: http.StatusOK, + Header: http.Header{"Content-Type": []string{"application/json"}}, + Body: io.NopCloser(strings.NewReader( + `{"id":"msg_1","type":"message","model":"claude-opus-4-6","role":"assistant","content":[{"type":"text","text":"ok"}],"usage":{"input_tokens":1,"output_tokens":1}}`, + )), + }, nil + }) + ctx := context.WithValue( + context.Background(), + "cliproxy.roundtripper", + http.RoundTripper(transport), + ) + + cfg := &config.Config{ + DisableClaudeCloakMode: true, + ClaudeKey: []config.ClaudeKey{{ + APIKey: "sk-ant-api03-global-disable-passthrough", + }}, + } + auth := &cliproxyauth.Auth{ + ID: "official-global-disable-passthrough", + Attributes: map[string]string{ + "api_key": "sk-ant-api03-global-disable-passthrough", + }, + } + + if _, errExec := NewClaudeExecutor(cfg).Execute(ctx, auth, cliproxyexecutor.Request{ + Model: "claude-opus-4-6", + Payload: payload, + }, cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FormatClaude, + OriginalRequest: payload, + Headers: incoming, + }); errExec != nil { + t.Fatalf("Execute() error = %v", errExec) + } + + if got := gjson.GetBytes(seenBody, "system.0.text").String(); got != billing { + t.Fatalf("system.0.text = %q, want preserved caller billing header %q", got, billing) + } + if got := gjson.GetBytes(seenBody, "system.1.text").String(); got != claudeCodeCLIIdentity { + t.Fatalf("system.1.text = %q, want preserved caller CLI identity", got) + } + if got := gjson.GetBytes(seenBody, "metadata.user_id").String(); got != userID { + t.Fatalf("metadata.user_id = %q, want preserved %q", got, userID) + } + if got := claudeFingerprintHeaderValue(seenHeaders, "X-App"); got != "cli" { + t.Fatalf("X-App = %q, want preserved cli", got) + } + if got := claudeFingerprintHeaderValue(seenHeaders, "Anthropic-Beta"); !strings.Contains(got, "claude-code-20250219") { + t.Fatalf("Anthropic-Beta = %q, want preserved claude-code-20250219", got) + } +} diff --git a/internal/runtime/executor/helps/claude_client_detection.go b/internal/runtime/executor/helps/claude_client_detection.go index 6d5a4c0dfdb..e103f8b9ae0 100644 --- a/internal/runtime/executor/helps/claude_client_detection.go +++ b/internal/runtime/executor/helps/claude_client_detection.go @@ -496,6 +496,11 @@ func claudeJSONObjectHasKeys(raw []byte, want []string) bool { return errClosing == nil && closing == json.Delim('}') && keyIndex == len(want) } +// IsClaudeCodeUserAgent reports whether userAgent identifies a Claude Code CLI client. +func IsClaudeCodeUserAgent(userAgent string) bool { + return claudeCodeUserAgentPattern.MatchString(strings.TrimSpace(userAgent)) +} + func plausibleClaudeCodeUserAgent(userAgent string, cfg *config.Config) bool { userAgent = strings.TrimSpace(userAgent) if !claudeCodeUserAgentPattern.MatchString(userAgent) || !claudeCodeNativeUserAgentPattern.MatchString(userAgent) { From 45ccd9c39d3441e8b6b1a8829eb28dc7b3c6fb1b Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Mon, 19 Oct 2026 17:40:36 +0800 Subject: [PATCH 24/27] fix(openai): preserve model prefix when routing xAI image requests - Pass the client-requested model into xAI image generation and edit handlers and resolve the routing model from registered xAI models instead of re-reading the stripped canonical model from the upstream request payload. - Allow custom xAI credential prefixes in xAI image model validation while preserving built-in prefix fallback when force-model-prefix is disabled. Closes: #6768 --- .../handlers/openai/openai_images_handlers.go | 84 +++++- .../openai/openai_images_handlers_test.go | 285 ++++++++++++++++++ 2 files changed, 355 insertions(+), 14 deletions(-) diff --git a/sdk/api/handlers/openai/openai_images_handlers.go b/sdk/api/handlers/openai/openai_images_handlers.go index 4e177bf15be..10c8a57a3ca 100644 --- a/sdk/api/handlers/openai/openai_images_handlers.go +++ b/sdk/api/handlers/openai/openai_images_handlers.go @@ -230,14 +230,37 @@ func isXAIImagesBaseModel(baseModel string) bool { } } +func registeredXAIImagesModel(model string) (string, bool) { + model = strings.TrimSpace(model) + if model == "" { + return "", false + } + modelRegistry := registry.GetGlobalRegistry() + for _, candidate := range []string{model, strings.ToLower(model)} { + for _, provider := range modelRegistry.GetModelProviders(candidate) { + if strings.EqualFold(strings.TrimSpace(provider), "xai") { + return candidate, true + } + } + } + return "", false +} + func isXAIImagesModel(model string) bool { prefix, baseModel := imagesModelParts(model) if !isXAIImagesBaseModel(baseModel) { return false } - prefix = strings.ToLower(strings.TrimSpace(prefix)) - return prefix == "" || prefix == "xai" || prefix == "x-ai" || prefix == "grok" + trimmedPrefix := strings.ToLower(strings.TrimSpace(prefix)) + if trimmedPrefix == "" || trimmedPrefix == "xai" || trimmedPrefix == "x-ai" || trimmedPrefix == "grok" { + return true + } + if _, ok := registeredXAIImagesModel(prefix + "/" + canonicalXAIImagesModel(model)); ok { + return true + } + _, ok := registeredXAIImagesModel(model) + return ok } func isSupportedImagesModel(model string) bool { @@ -298,6 +321,33 @@ func canonicalXAIImagesModel(model string) string { } } +func (h *OpenAIAPIHandler) routingXAIImagesModel(model string) string { + canonical := canonicalXAIImagesModel(model) + prefix, _ := imagesModelParts(model) + if prefix == "" { + return canonical + } + prefixedModel := prefix + "/" + canonical + if resolved, ok := registeredXAIImagesModel(prefixedModel); ok { + return resolved + } + if resolved, ok := registeredXAIImagesModel(model); ok { + return resolved + } + if h != nil { + if cfg := h.CurrentConfig(); cfg != nil && cfg.ForceModelPrefix { + return prefixedModel + } + } + if h != nil && h.BaseAPIHandler != nil && h.AuthManager != nil && h.AuthManager.HomeEnabled() { + return prefixedModel + } + if _, ok := registeredXAIImagesModel(canonical); !ok { + return prefixedModel + } + return canonical +} + func xaiImagesAspectRatio(raw string, fallback string) string { switch strings.ToLower(strings.TrimSpace(raw)) { case "1:1", "square": @@ -683,7 +733,7 @@ func (h *OpenAIAPIHandler) ImagesGenerations(c *gin.Context) { } if isXAIImagesModel(imageModel) { xaiReq := buildXAIImagesGenerationsRequest(rawJSON, imageModel, responseFormat) - h.handleXAIImages(c, xaiReq, responseFormat, "image_generation", stream) + h.handleXAIImages(c, xaiReq, imageModel, responseFormat, "image_generation", stream) return } if isOpenAICompatImagesModel(imageModel) { @@ -845,7 +895,7 @@ func (h *OpenAIAPIHandler) imagesEditsFromMultipart(c *gin.Context) { quality := strings.TrimSpace(c.PostForm("quality")) n := parseIntField(c.PostForm("n"), 0) xaiReq := buildXAIImagesEditRequest(imageModel, prompt, images, responseFormat, aspectRatio, resolution, quality, n) - h.handleXAIImages(c, xaiReq, responseFormat, "image_edit", stream) + h.handleXAIImages(c, xaiReq, imageModel, responseFormat, "image_edit", stream) return } if isOpenAICompatImagesModel(imageModel) { @@ -984,7 +1034,7 @@ func (h *OpenAIAPIHandler) imagesEditsFromJSON(c *gin.Context) { } aspectRatio, resolution, quality, n := xaiImagesEditOptionsFromJSON(rawJSON) xaiReq := buildXAIImagesEditRequest(imageModel, prompt, images, responseFormat, aspectRatio, resolution, quality, n) - h.handleXAIImages(c, xaiReq, responseFormat, "image_edit", stream) + h.handleXAIImages(c, xaiReq, imageModel, responseFormat, "image_edit", stream) return } if isOpenAICompatImagesModel(imageModel) { @@ -1171,12 +1221,12 @@ func buildImagesAPIResponseFromXAI(payload []byte, responseFormat string) ([]byt return out, nil } -func (h *OpenAIAPIHandler) handleXAIImages(c *gin.Context, xaiReq []byte, responseFormat string, streamPrefix string, stream bool) { +func (h *OpenAIAPIHandler) handleXAIImages(c *gin.Context, xaiReq []byte, imageModel string, responseFormat string, streamPrefix string, stream bool) { if stream { - h.streamXAIImages(c, xaiReq, responseFormat, streamPrefix) + h.streamXAIImages(c, xaiReq, imageModel, responseFormat, streamPrefix) return } - h.collectXAIImages(c, xaiReq, responseFormat) + h.collectXAIImages(c, xaiReq, imageModel, responseFormat) } func (h *OpenAIAPIHandler) handleOpenAICompatImages(c *gin.Context, compatReq []byte, imageModel string, responseFormat string, streamPrefix string, stream bool) { @@ -1463,9 +1513,12 @@ func (h *OpenAIAPIHandler) streamOpenAICompatImages(c *gin.Context, compatReq [] } } -func (h *OpenAIAPIHandler) collectXAIImages(c *gin.Context, xaiReq []byte, responseFormat string) { - model := strings.TrimSpace(gjson.GetBytes(xaiReq, "model").String()) - h.collectImagesWithModel(c, xaiReq, model, responseFormat) +func (h *OpenAIAPIHandler) collectXAIImages(c *gin.Context, xaiReq []byte, imageModel string, responseFormat string) { + model := strings.TrimSpace(imageModel) + if model == "" { + model = strings.TrimSpace(gjson.GetBytes(xaiReq, "model").String()) + } + h.collectImagesWithModel(c, xaiReq, h.routingXAIImagesModel(model), responseFormat) } func (h *OpenAIAPIHandler) collectImagesWithModel(c *gin.Context, imageReq []byte, model string, responseFormat string) { @@ -1500,9 +1553,12 @@ func (h *OpenAIAPIHandler) collectImagesWithModel(c *gin.Context, imageReq []byt cliCancel(nil) } -func (h *OpenAIAPIHandler) streamXAIImages(c *gin.Context, xaiReq []byte, responseFormat string, streamPrefix string) { - model := strings.TrimSpace(gjson.GetBytes(xaiReq, "model").String()) - h.streamImagesWithModel(c, xaiReq, model, responseFormat, streamPrefix) +func (h *OpenAIAPIHandler) streamXAIImages(c *gin.Context, xaiReq []byte, imageModel string, responseFormat string, streamPrefix string) { + model := strings.TrimSpace(imageModel) + if model == "" { + model = strings.TrimSpace(gjson.GetBytes(xaiReq, "model").String()) + } + h.streamImagesWithModel(c, xaiReq, h.routingXAIImagesModel(model), responseFormat, streamPrefix) } func (h *OpenAIAPIHandler) streamImagesWithModel(c *gin.Context, imageReq []byte, model string, responseFormat string, streamPrefix string) { diff --git a/sdk/api/handlers/openai/openai_images_handlers_test.go b/sdk/api/handlers/openai/openai_images_handlers_test.go index d21a31dbf2f..9334acbe6ab 100644 --- a/sdk/api/handlers/openai/openai_images_handlers_test.go +++ b/sdk/api/handlers/openai/openai_images_handlers_test.go @@ -11,6 +11,7 @@ import ( "net/http/httptest" "net/textproto" "strings" + "sync" "testing" "github.com/gin-gonic/gin" @@ -18,6 +19,8 @@ import ( "github.com/router-for-me/CLIProxyAPI/v8/internal/interfaces" "github.com/router-for-me/CLIProxyAPI/v8/internal/registry" "github.com/router-for-me/CLIProxyAPI/v8/sdk/api/handlers" + coreauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth" + coreexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor" sdkconfig "github.com/router-for-me/CLIProxyAPI/v8/sdk/config" "github.com/tidwall/gjson" ) @@ -669,3 +672,285 @@ func TestSSEFrameAccumulatorKeepsMultipleFramesDistinct(t *testing.T) { t.Fatalf("frames were overwritten during buffer compaction: %q", frames) } } + +type xaiImagesCaptureCall struct { + authID string + reqModel string + requestedModel string + payloadModel string +} + +type xaiImagesCaptureExecutor struct { + mu sync.Mutex + calls []xaiImagesCaptureCall +} + +func (e *xaiImagesCaptureExecutor) Identifier() string { return "xai" } + +func (e *xaiImagesCaptureExecutor) Execute(_ context.Context, auth *coreauth.Auth, req coreexecutor.Request, opts coreexecutor.Options) (coreexecutor.Response, error) { + authID := "" + if auth != nil { + authID = auth.ID + } + requestedModel := "" + if opts.Metadata != nil { + if v, ok := opts.Metadata[coreexecutor.RequestedModelMetadataKey].(string); ok { + requestedModel = v + } + } + e.mu.Lock() + e.calls = append(e.calls, xaiImagesCaptureCall{ + authID: authID, + reqModel: req.Model, + requestedModel: requestedModel, + payloadModel: strings.TrimSpace(gjson.GetBytes(req.Payload, "model").String()), + }) + e.mu.Unlock() + return coreexecutor.Response{ + Payload: []byte(`{"created":123,"data":[{"b64_json":"aW1hZ2U=","revised_prompt":"ok","mime_type":"image/png"}]}`), + }, nil +} + +func (e *xaiImagesCaptureExecutor) ExecuteStream(context.Context, *coreauth.Auth, coreexecutor.Request, coreexecutor.Options) (*coreexecutor.StreamResult, error) { + return nil, &coreauth.Error{Code: "not_implemented", Message: "ExecuteStream not implemented"} +} + +func (e *xaiImagesCaptureExecutor) Refresh(_ context.Context, auth *coreauth.Auth) (*coreauth.Auth, error) { + return auth, nil +} + +func (e *xaiImagesCaptureExecutor) CountTokens(context.Context, *coreauth.Auth, coreexecutor.Request, coreexecutor.Options) (coreexecutor.Response, error) { + return coreexecutor.Response{}, &coreauth.Error{Code: "not_implemented", Message: "CountTokens not implemented"} +} + +func (e *xaiImagesCaptureExecutor) HttpRequest(context.Context, *coreauth.Auth, *http.Request) (*http.Response, error) { + return nil, &coreauth.Error{Code: "not_implemented", Message: "HttpRequest not implemented"} +} + +func (e *xaiImagesCaptureExecutor) Calls() []xaiImagesCaptureCall { + e.mu.Lock() + defer e.mu.Unlock() + out := make([]xaiImagesCaptureCall, len(e.calls)) + copy(out, e.calls) + return out +} + +func newXAIImagesRoutingTestHandler(t *testing.T, forceModelPrefix bool, authID string, prefix string, modelIDs []string) (*OpenAIAPIHandler, *xaiImagesCaptureExecutor) { + t.Helper() + + executor := &xaiImagesCaptureExecutor{} + manager := coreauth.NewManager(nil, &coreauth.RoundRobinSelector{}, nil) + manager.RegisterExecutor(executor) + auth := &coreauth.Auth{ + ID: authID, + Provider: "xai", + Prefix: prefix, + Status: coreauth.StatusActive, + } + if _, errRegister := manager.Register(context.Background(), auth); errRegister != nil { + t.Fatalf("manager.Register: %v", errRegister) + } + + models := make([]*registry.ModelInfo, 0, len(modelIDs)) + for _, id := range modelIDs { + models = append(models, ®istry.ModelInfo{ + ID: id, + Object: "model", + OwnedBy: "xai", + Type: "xai", + }) + } + modelRegistry := registry.GetGlobalRegistry() + modelRegistry.RegisterClient(auth.ID, auth.Provider, models) + manager.RefreshSchedulerEntry(auth.ID) + t.Cleanup(func() { + modelRegistry.UnregisterClient(auth.ID) + }) + + base := handlers.NewBaseAPIHandlers(&sdkconfig.SDKConfig{ForceModelPrefix: forceModelPrefix}, manager) + return NewOpenAIAPIHandler(base), executor +} + +func TestXAIImagesRoutesWithPrefixedModelWhenForceModelPrefixEnabled(t *testing.T) { + handler, executor := newXAIImagesRoutingTestHandler(t, true, "xai-prefixed-auth", "xai", []string{ + "xai/" + defaultXAIImagesModel, + "xai/" + xaiImagesQualityModel, + "xai/" + xaiImages20Model, + }) + + t.Run("generations non-stream", func(t *testing.T) { + before := len(executor.Calls()) + body := strings.NewReader(`{"model":"xai/grok-imagine-image-2.0","prompt":"a red square","response_format":"b64_json"}`) + resp := performImagesEndpointRequest(t, imagesGenerationsPath, "application/json", body, handler.ImagesGenerations) + if resp.Code != http.StatusOK { + t.Fatalf("status = %d, want %d: %s", resp.Code, http.StatusOK, resp.Body.String()) + } + if got := gjson.GetBytes(resp.Body.Bytes(), "data.0.b64_json").String(); got != "aW1hZ2U=" { + t.Fatalf("data.0.b64_json = %q, want aW1hZ2U=", got) + } + calls := executor.Calls() + if len(calls) != before+1 { + t.Fatalf("executor calls = %d, want %d", len(calls), before+1) + } + last := calls[len(calls)-1] + if last.authID != "xai-prefixed-auth" { + t.Fatalf("authID = %q, want xai-prefixed-auth", last.authID) + } + if last.requestedModel != "xai/grok-imagine-image-2.0" { + t.Fatalf("requestedModel = %q, want xai/grok-imagine-image-2.0", last.requestedModel) + } + if last.reqModel != xaiImages20Model { + t.Fatalf("req.Model = %q, want %s", last.reqModel, xaiImages20Model) + } + if last.payloadModel != xaiImages20Model { + t.Fatalf("payload model = %q, want %s", last.payloadModel, xaiImages20Model) + } + }) + + t.Run("generations stream", func(t *testing.T) { + before := len(executor.Calls()) + body := strings.NewReader(`{"model":"xai/grok-imagine-image-2.0","prompt":"a red square","response_format":"b64_json","stream":true}`) + resp := performImagesEndpointRequest(t, imagesGenerationsPath, "application/json", body, handler.ImagesGenerations) + if resp.Code != http.StatusOK { + t.Fatalf("status = %d, want %d: %s", resp.Code, http.StatusOK, resp.Body.String()) + } + if !strings.Contains(resp.Body.String(), `"type":"image_generation.completed"`) { + t.Fatalf("stream body = %q, want image_generation.completed event", resp.Body.String()) + } + calls := executor.Calls() + if len(calls) != before+1 { + t.Fatalf("executor calls = %d, want %d", len(calls), before+1) + } + last := calls[len(calls)-1] + if last.requestedModel != "xai/grok-imagine-image-2.0" || last.payloadModel != xaiImages20Model { + t.Fatalf("call = %+v, want requestedModel=xai/grok-imagine-image-2.0 payloadModel=%s", last, xaiImages20Model) + } + }) + + t.Run("edits JSON", func(t *testing.T) { + before := len(executor.Calls()) + body := strings.NewReader(`{"model":"xai/grok-imagine-image-2.0","prompt":"edit this","images":[{"image_url":"data:image/png;base64,AA=="}]}`) + resp := performImagesEndpointRequest(t, imagesEditsPath, "application/json", body, handler.ImagesEdits) + if resp.Code != http.StatusOK { + t.Fatalf("status = %d, want %d: %s", resp.Code, http.StatusOK, resp.Body.String()) + } + calls := executor.Calls() + if len(calls) != before+1 { + t.Fatalf("executor calls = %d, want %d", len(calls), before+1) + } + last := calls[len(calls)-1] + if last.requestedModel != "xai/grok-imagine-image-2.0" || last.payloadModel != xaiImages20Model { + t.Fatalf("call = %+v, want requestedModel=xai/grok-imagine-image-2.0 payloadModel=%s", last, xaiImages20Model) + } + }) + + t.Run("edits multipart", func(t *testing.T) { + before := len(executor.Calls()) + var body bytes.Buffer + writer := multipart.NewWriter(&body) + if err := writer.WriteField("model", "xai/grok-imagine-image-2.0"); err != nil { + t.Fatalf("write model field: %v", err) + } + if err := writer.WriteField("prompt", "edit this"); err != nil { + t.Fatalf("write prompt field: %v", err) + } + header := make(textproto.MIMEHeader) + header.Set("Content-Disposition", multipart.FileContentDisposition("image", "image.png")) + header.Set("Content-Type", "image/png") + part, err := writer.CreatePart(header) + if err != nil { + t.Fatalf("create image part: %v", err) + } + if _, err := part.Write([]byte("png-bytes")); err != nil { + t.Fatalf("write image part: %v", err) + } + if errClose := writer.Close(); errClose != nil { + t.Fatalf("close multipart writer: %v", errClose) + } + + resp := performImagesEndpointRequest(t, imagesEditsPath, writer.FormDataContentType(), &body, handler.ImagesEdits) + if resp.Code != http.StatusOK { + t.Fatalf("status = %d, want %d: %s", resp.Code, http.StatusOK, resp.Body.String()) + } + calls := executor.Calls() + if len(calls) != before+1 { + t.Fatalf("executor calls = %d, want %d", len(calls), before+1) + } + last := calls[len(calls)-1] + if last.requestedModel != "xai/grok-imagine-image-2.0" || last.payloadModel != xaiImages20Model { + t.Fatalf("call = %+v, want requestedModel=xai/grok-imagine-image-2.0 payloadModel=%s", last, xaiImages20Model) + } + }) + + t.Run("case-insensitive prefixed model routes and strips prefix for upstream", func(t *testing.T) { + before := len(executor.Calls()) + body := strings.NewReader(`{"model":"XAI/Grok-Imagine-Image-2.0","prompt":"a red square"}`) + resp := performImagesEndpointRequest(t, imagesGenerationsPath, "application/json", body, handler.ImagesGenerations) + if resp.Code != http.StatusOK { + t.Fatalf("status = %d, want %d: %s", resp.Code, http.StatusOK, resp.Body.String()) + } + calls := executor.Calls() + if len(calls) != before+1 { + t.Fatalf("executor calls = %d, want %d", len(calls), before+1) + } + last := calls[len(calls)-1] + if last.reqModel != xaiImages20Model || last.payloadModel != xaiImages20Model { + t.Fatalf("call = %+v, want reqModel=%s payloadModel=%s", last, xaiImages20Model, xaiImages20Model) + } + }) + + t.Run("unprefixed model rejected when force-model-prefix is enabled", func(t *testing.T) { + before := len(executor.Calls()) + body := strings.NewReader(`{"model":"grok-imagine-image-2.0","prompt":"a red square"}`) + resp := performImagesEndpointRequest(t, imagesGenerationsPath, "application/json", body, handler.ImagesGenerations) + if resp.Code != http.StatusBadRequest { + t.Fatalf("status = %d, want %d: %s", resp.Code, http.StatusBadRequest, resp.Body.String()) + } + if got := gjson.GetBytes(resp.Body.Bytes(), "error.message").String(); got != "unknown provider for model grok-imagine-image-2.0" { + t.Fatalf("error.message = %q, want %q", got, "unknown provider for model grok-imagine-image-2.0") + } + if len(executor.Calls()) != before { + t.Fatalf("expected no executor call for unprefixed model") + } + }) +} + +func TestXAIImagesSupportsBuiltInPrefixFallbackAndCustomCredentialPrefix(t *testing.T) { + t.Run("built-in prefix falls back to unprefixed registered model when force-model-prefix is disabled", func(t *testing.T) { + handler, executor := newXAIImagesRoutingTestHandler(t, false, "xai-unprefixed-auth", "", []string{ + defaultXAIImagesModel, + xaiImagesQualityModel, + xaiImages20Model, + }) + body := strings.NewReader(`{"model":"xai/grok-imagine-image-2.0","prompt":"a red square"}`) + resp := performImagesEndpointRequest(t, imagesGenerationsPath, "application/json", body, handler.ImagesGenerations) + if resp.Code != http.StatusOK { + t.Fatalf("status = %d, want %d: %s", resp.Code, http.StatusOK, resp.Body.String()) + } + calls := executor.Calls() + if len(calls) != 1 { + t.Fatalf("executor calls = %d, want 1", len(calls)) + } + if calls[0].payloadModel != xaiImages20Model { + t.Fatalf("payload model = %q, want %s", calls[0].payloadModel, xaiImages20Model) + } + }) + + t.Run("custom xai credential prefix routes when force-model-prefix is enabled", func(t *testing.T) { + handler, executor := newXAIImagesRoutingTestHandler(t, true, "xai-custom-prefix-auth", "my-xai", []string{ + "my-xai/" + xaiImages20Model, + }) + body := strings.NewReader(`{"model":"my-xai/grok-imagine-image-2.0","prompt":"a red square"}`) + resp := performImagesEndpointRequest(t, imagesGenerationsPath, "application/json", body, handler.ImagesGenerations) + if resp.Code != http.StatusOK { + t.Fatalf("status = %d, want %d: %s", resp.Code, http.StatusOK, resp.Body.String()) + } + calls := executor.Calls() + if len(calls) != 1 { + t.Fatalf("executor calls = %d, want 1", len(calls)) + } + if calls[0].authID != "xai-custom-prefix-auth" || calls[0].requestedModel != "my-xai/grok-imagine-image-2.0" || calls[0].payloadModel != xaiImages20Model { + t.Fatalf("call = %+v, want authID=xai-custom-prefix-auth requestedModel=my-xai/grok-imagine-image-2.0 payloadModel=%s", calls[0], xaiImages20Model) + } + }) +} From 8df4e42ab9ba27f072044cf20bb1b223a61f76bf Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Mon, 19 Oct 2026 18:06:33 +0800 Subject: [PATCH 25/27] fix(codex): advertise direct tool mode and disable responses lite for non-codex routes - Set `tool_mode` to `"direct"` and disable `use_responses_lite` and `supports_reasoning_effort_updates` when normalizing Codex catalog entries for non-template models and non-Codex or mixed-provider routes. - Reset `tool_mode`, `use_responses_lite`, and `supports_reasoning_effort_updates` when synthesizing a fallback `gpt-5.5` baseline template in `fetch_codex_models`. Closes: #6769 --- cmd/fetch_codex_models/main.go | 13 +++ cmd/fetch_codex_models/main_test.go | 14 ++- internal/client/codex/models/models.go | 3 + internal/client/codex/models/models_test.go | 121 ++++++++++++++++++++ 4 files changed, 150 insertions(+), 1 deletion(-) diff --git a/cmd/fetch_codex_models/main.go b/cmd/fetch_codex_models/main.go index 2fc2c2628ac..76ddf4d2168 100644 --- a/cmd/fetch_codex_models/main.go +++ b/cmd/fetch_codex_models/main.go @@ -349,6 +349,19 @@ func ensureBaselineModel(raw []byte) ([]byte, int, error) { return nil, 0, fmt.Errorf("failed to set %s visibility on cloned model: %w", baselineModelSlug, errSet) } } + for _, reset := range []struct { + path string + value any + }{ + {path: "tool_mode", value: "direct"}, + {path: "use_responses_lite", value: false}, + {path: "supports_reasoning_effort_updates", value: false}, + } { + cloned, errSet = sjson.SetBytes(cloned, reset.path, reset.value) + if errSet != nil { + return nil, 0, fmt.Errorf("failed to set %s %s on cloned model: %w", baselineModelSlug, reset.path, errSet) + } + } raw, errSet = sjson.SetRawBytes(raw, "models.-1", cloned) if errSet != nil { return nil, 0, fmt.Errorf("failed to append %s model: %w", baselineModelSlug, errSet) diff --git a/cmd/fetch_codex_models/main_test.go b/cmd/fetch_codex_models/main_test.go index f63a37f510d..ebe2b6a2d4a 100644 --- a/cmd/fetch_codex_models/main_test.go +++ b/cmd/fetch_codex_models/main_test.go @@ -53,7 +53,7 @@ func TestCountModels(t *testing.T) { func TestEnsureBaselineModel(t *testing.T) { t.Run("clones first model as gpt-5.5 when missing", func(t *testing.T) { - raw := []byte(`{"models":[{"slug":"gpt-6.1-sol","display_name":"GPT-6.1-Sol","visibility":"list"},{"slug":"gpt-5.6-sol","visibility":"list"}]}`) + raw := []byte(`{"models":[{"slug":"gpt-6.1-sol","display_name":"GPT-6.1-Sol","visibility":"list","tool_mode":"code_mode_only","use_responses_lite":true,"supports_reasoning_effort_updates":true},{"slug":"gpt-5.6-sol","visibility":"list"}]}`) out, count, errEnsure := ensureBaselineModel(raw) if errEnsure != nil { t.Fatalf("ensureBaselineModel: %v", errEnsure) @@ -64,6 +64,9 @@ func TestEnsureBaselineModel(t *testing.T) { if got := gjson.GetBytes(out, "models.0.slug").String(); got != "gpt-6.1-sol" { t.Fatalf("models[0].slug = %q, want gpt-6.1-sol", got) } + if got := gjson.GetBytes(out, "models.0.tool_mode").String(); got != "code_mode_only" { + t.Fatalf("models[0].tool_mode = %q, want code_mode_only", got) + } if got := gjson.GetBytes(out, "models.2.slug").String(); got != "gpt-5.5" { t.Fatalf("models[2].slug = %q, want gpt-5.5", got) } @@ -73,6 +76,15 @@ func TestEnsureBaselineModel(t *testing.T) { if got := gjson.GetBytes(out, "models.2.visibility").String(); got != "list" { t.Fatalf("models[2].visibility = %q, want list", got) } + if got := gjson.GetBytes(out, "models.2.tool_mode"); got.Type != gjson.String || got.Str != "direct" { + t.Fatalf("models[2].tool_mode = %v (%s), want string \"direct\"", got.Value(), got.Type) + } + if got := gjson.GetBytes(out, "models.2.use_responses_lite"); got.Type != gjson.False { + t.Fatalf("models[2].use_responses_lite = %v (%s), want false", got.Value(), got.Type) + } + if got := gjson.GetBytes(out, "models.2.supports_reasoning_effort_updates"); got.Type != gjson.False { + t.Fatalf("models[2].supports_reasoning_effort_updates = %v (%s), want false", got.Value(), got.Type) + } }) t.Run("clones first model and forces visibility to list when first model is hidden", func(t *testing.T) { diff --git a/internal/client/codex/models/models.go b/internal/client/codex/models/models.go index 595092a9cce..d15faca40e1 100644 --- a/internal/client/codex/models/models.go +++ b/internal/client/codex/models/models.go @@ -570,6 +570,9 @@ func applyCodexClientProviderCapabilities(entry map[string]any, id string, provi // Codex requires these keys to be present; omitting them rejects the entire catalog. func nullCodexClientRequiredOptions(entry map[string]any) { entry["apply_patch_tool_type"] = nil + entry["tool_mode"] = "direct" + entry["use_responses_lite"] = false + entry["supports_reasoning_effort_updates"] = false entry["upgrade"] = nil entry["availability_nux"] = nil } diff --git a/internal/client/codex/models/models_test.go b/internal/client/codex/models/models_test.go index f28cf7078e7..d3b40311818 100644 --- a/internal/client/codex/models/models_test.go +++ b/internal/client/codex/models/models_test.go @@ -1002,6 +1002,15 @@ func TestCodexClientModelsResponse_UnrelatedProviderCannotWidenSearchTool(t *tes if tiers, okTiers := entry["service_tiers"].([]any); !okTiers || len(tiers) != 0 { t.Errorf("service_tiers = %#v, want empty array for non-Codex provider", entry["service_tiers"]) } + if got := stringModelValue(entry, "tool_mode"); got != "direct" { + t.Errorf("tool_mode = %#v, want direct for non-Codex provider", entry["tool_mode"]) + } + if got, ok := entry["use_responses_lite"].(bool); !ok || got { + t.Errorf("use_responses_lite = %#v, want false for non-Codex provider", entry["use_responses_lite"]) + } + if got, ok := entry["supports_reasoning_effort_updates"].(bool); !ok || got { + t.Errorf("supports_reasoning_effort_updates = %#v, want false for non-Codex provider", entry["supports_reasoning_effort_updates"]) + } assertCodexNullableFieldCleared(t, entry, "upgrade") assertCodexNullableFieldCleared(t, entry, "availability_nux") // Reasoning levels must NOT be widened beyond provider's registered capabilities @@ -1098,6 +1107,15 @@ func TestCodexClientModelsResponse_MixedProvidersRestrictProtocolCapabilities(t if tiers, okTiers := entry["service_tiers"].([]any); !okTiers || len(tiers) != 0 { t.Errorf("service_tiers = %#v, want empty array for mixed provider", entry["service_tiers"]) } + if got := stringModelValue(entry, "tool_mode"); got != "direct" { + t.Errorf("tool_mode = %#v, want direct for mixed provider", entry["tool_mode"]) + } + if got, ok := entry["use_responses_lite"].(bool); !ok || got { + t.Errorf("use_responses_lite = %#v, want false for mixed provider", entry["use_responses_lite"]) + } + if got, ok := entry["supports_reasoning_effort_updates"].(bool); !ok || got { + t.Errorf("supports_reasoning_effort_updates = %#v, want false for mixed provider", entry["supports_reasoning_effort_updates"]) + } assertCodexNullableFieldCleared(t, entry, "upgrade") assertCodexNullableFieldCleared(t, entry, "availability_nux") // Reasoning levels must NOT be widened to ultra/max/xhigh regardless of registration order @@ -1733,3 +1751,106 @@ func assertCodexNullableFieldCleared(t *testing.T, entry map[string]any, key str t.Errorf("%s = %#v, want null", key, value) } } + +func TestCodexClientModelsResponse_DoesNotLeakResponsesLiteOrCodeModeToNonCodexRoutes_Issue6769(t *testing.T) { + _, revision := registry.GetCodexClientModelsSnapshot() + codexClientModelTemplatesMu.Lock() + previousLoaded := codexClientModelTemplatesLoaded + previousRevision := codexClientModelTemplatesRevision + previousTemplates := codexClientModelTemplates + previousDefault := codexClientDefaultTemplate + previousErr := codexClientModelTemplatesErr + codexClientModelTemplatesLoaded = false + codexClientModelTemplatesMu.Unlock() + t.Cleanup(func() { + codexClientModelTemplatesMu.Lock() + codexClientModelTemplatesLoaded = previousLoaded + codexClientModelTemplatesRevision = previousRevision + codexClientModelTemplates = previousTemplates + codexClientDefaultTemplate = previousDefault + codexClientModelTemplatesErr = previousErr + codexClientModelTemplatesMu.Unlock() + }) + + // Simulate a post-retirement catalog where the first/default template (gpt-6.1-sol) + // advertises code_mode_only, use_responses_lite, and supports_reasoning_effort_updates. + snapshot := []byte(`{"models":[{ + "slug":"gpt-6.1-sol", + "display_name":"GPT-6.1-Sol", + "description":"Latest workhorse model.", + "base_instructions":"Full instructions", + "minimal_client_version":"0.153.0", + "visibility":"list", + "context_window":500000, + "max_context_window":872000, + "priority":0, + "default_reasoning_level":"low", + "supported_reasoning_levels":[{"effort":"low","description":"Fast"}], + "prefer_websockets":true, + "apply_patch_tool_type":"freeform", + "tool_mode":"code_mode_only", + "use_responses_lite":true, + "supports_reasoning_effort_updates":true, + "supports_search_tool":true, + "service_tiers":[{"id":"priority"}], + "upgrade":null, + "availability_nux":{"message":"nux"} + }]}`) + if _, _, errLoad := loadCodexClientModelTemplatesSnapshot(snapshot, revision); errLoad != nil { + t.Fatalf("loadCodexClientModelTemplatesSnapshot: %v", errLoad) + } + + available := []map[string]any{ + {"id": "gpt-6.1-sol"}, + {"id": "kimi-k3"}, + {"id": "muse-wonder-1.0"}, + } + providers := map[string][]string{ + "gpt-6.1-sol": {"codex"}, + "kimi-k3": {"kimi"}, + "muse-wonder-1.0": {"meta"}, + } + + resp := BuildResponseForClient(available, func(id string) []string { + return providers[id] + }, false, "0.163.0-alpha.10.2") + models, ok := resp["models"].([]map[string]any) + if !ok || len(models) != 3 { + t.Fatalf("models = %#v, want 3 entries", resp["models"]) + } + + bySlug := make(map[string]map[string]any, len(models)) + for _, entry := range models { + bySlug[stringModelValue(entry, "slug")] = entry + } + + native := bySlug["gpt-6.1-sol"] + if native == nil { + t.Fatal("missing gpt-6.1-sol entry") + } + if got := stringModelValue(native, "tool_mode"); got != "code_mode_only" { + t.Errorf("gpt-6.1-sol tool_mode = %#v, want code_mode_only", native["tool_mode"]) + } + if got, ok := native["use_responses_lite"].(bool); !ok || !got { + t.Errorf("gpt-6.1-sol use_responses_lite = %#v, want true", native["use_responses_lite"]) + } + if got, ok := native["supports_reasoning_effort_updates"].(bool); !ok || !got { + t.Errorf("gpt-6.1-sol supports_reasoning_effort_updates = %#v, want true", native["supports_reasoning_effort_updates"]) + } + + for _, slug := range []string{"kimi-k3", "muse-wonder-1.0"} { + entry := bySlug[slug] + if entry == nil { + t.Fatalf("missing %s entry", slug) + } + if got := stringModelValue(entry, "tool_mode"); got != "direct" { + t.Errorf("%s tool_mode = %#v, want direct", slug, entry["tool_mode"]) + } + if got, ok := entry["use_responses_lite"].(bool); !ok || got { + t.Errorf("%s use_responses_lite = %#v, want false", slug, entry["use_responses_lite"]) + } + if got, ok := entry["supports_reasoning_effort_updates"].(bool); !ok || got { + t.Errorf("%s supports_reasoning_effort_updates = %#v, want false", slug, entry["supports_reasoning_effort_updates"]) + } + } +} From e7d4c7316fca3fe0e852866f4e611cb4f2fa0764 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Mon, 19 Oct 2026 19:00:41 +0800 Subject: [PATCH 26/27] fix(codex): reconcile replayed web_search_call history with tool declarations - Inject a web_search tool declaration for standard Codex requests when input replays web_search_call items without a web_search tool so upstream response protection remains available during compaction. - Strip replayed web_search_call items from Responses Lite requests where top-level hosted tool declarations are omitted. Closes: #6770 --- .../executor/codex_executor_compact_test.go | 281 ++++++++++++++++++ .../executor/codex_executor_execute.go | 2 + .../executor/codex_executor_request.go | 75 +++++ .../runtime/executor/codex_executor_stream.go | 1 + .../executor/codex_websockets_execute.go | 1 + .../executor/codex_websockets_stream.go | 1 + 6 files changed, 361 insertions(+) diff --git a/internal/runtime/executor/codex_executor_compact_test.go b/internal/runtime/executor/codex_executor_compact_test.go index f5bb2364777..c374ed98f19 100644 --- a/internal/runtime/executor/codex_executor_compact_test.go +++ b/internal/runtime/executor/codex_executor_compact_test.go @@ -152,3 +152,284 @@ func TestCodexExecutorCompactUseSIWCAppliesSIWCRulesBeforePayloadFinalizer(t *te t.Fatalf("PayloadFinalizer temperature override = %v, want 0.25; body=%s", gotTemp, gotBody) } } + +func TestCodexExecutorReplayedWebSearchCallHistoryEnsuresWebSearchTool_Issue6770(t *testing.T) { + hasWebSearchCallInInput := func(body []byte) bool { + for _, item := range gjson.GetBytes(body, "input").Array() { + if item.Get("type").String() == "web_search_call" { + return true + } + } + return false + } + countWebSearchTools := func(body []byte) int { + count := 0 + for _, tool := range gjson.GetBytes(body, "tools").Array() { + if tool.Get("type").String() == "web_search" { + count++ + } + } + return count + } + + compactionPayloadWithEmptyTools := `{"model":"gpt-6.1-sol","input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"search docs"}]},{"id":"ws_1","type":"web_search_call","status":"completed","action":{"type":"search","query":"openai responses web search"}},{"id":"ws_2","type":"web_search_call","status":"completed","action":{"type":"open_page","url":"https://platform.openai.com/docs"}},{"type":"message","role":"assistant","content":[{"type":"output_text","text":"Found docs."}]},{"type":"message","role":"user","content":[{"type":"input_text","text":"Summarize conversation"}]}],"tools":[],"tool_choice":"auto"}` + + t.Run("ExecuteStream with empty tools injects web_search and avoids 502 response protection error", func(t *testing.T) { + var gotBody []byte + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + body, _ := io.ReadAll(r.Body) + gotBody = body + w.Header().Set("Content-Type", "text/event-stream") + if hasWebSearchCallInInput(body) && countWebSearchTools(body) == 0 { + _, _ = w.Write([]byte("data: {\"type\":\"error\",\"error\":{\"message\":\"response protection is unavailable\",\"type\":\"internal_error\",\"param\":null,\"code\":null}}\n\n")) + return + } + _, _ = w.Write([]byte("data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp_1\",\"object\":\"response\",\"status\":\"completed\",\"output\":[{\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"type\":\"output_text\",\"text\":\"summary\"}]}],\"usage\":{\"input_tokens\":10,\"output_tokens\":5,\"total_tokens\":15}}}\n\n")) + })) + defer server.Close() + + executor := NewCodexExecutor(&config.Config{Codex: config.CodexConfig{StreamBootstrapBuffering: true}}) + auth := &cliproxyauth.Auth{ + Provider: "codex", + Attributes: map[string]string{ + "base_url": server.URL, + "api_key": "test", + "plan_type": "plus", + }, + } + + streamResult, err := executor.ExecuteStream(context.Background(), auth, cliproxyexecutor.Request{ + Model: "gpt-6.1-sol", + Payload: []byte(compactionPayloadWithEmptyTools), + }, cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FromString("openai-response"), + Stream: true, + }) + if err != nil { + t.Fatalf("ExecuteStream() error = %v", err) + } + for chunk := range streamResult.Chunks { + if chunk.Err != nil { + t.Fatalf("stream chunk error = %v", chunk.Err) + } + } + if got := countWebSearchTools(gotBody); got != 1 { + t.Fatalf("web_search tool count = %d, want 1; body=%s", got, gotBody) + } + }) + + t.Run("Execute non-stream with image generation disabled still injects web_search", func(t *testing.T) { + var gotBody []byte + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + body, _ := io.ReadAll(r.Body) + gotBody = body + w.Header().Set("Content-Type", "text/event-stream") + if hasWebSearchCallInInput(body) && countWebSearchTools(body) == 0 { + _, _ = w.Write([]byte("data: {\"type\":\"error\",\"error\":{\"message\":\"response protection is unavailable\",\"type\":\"internal_error\",\"param\":null,\"code\":null}}\n\n")) + return + } + _, _ = w.Write([]byte("data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp_1\",\"object\":\"response\",\"status\":\"completed\",\"output\":[],\"usage\":{\"input_tokens\":10,\"output_tokens\":5,\"total_tokens\":15}}}\n\n")) + })) + defer server.Close() + + executor := NewCodexExecutor(&config.Config{SDKConfig: config.SDKConfig{DisableImageGeneration: config.DisableImageGenerationAll}}) + auth := &cliproxyauth.Auth{ + Provider: "codex", + Attributes: map[string]string{ + "base_url": server.URL, + "api_key": "test", + }, + } + + _, err := executor.Execute(context.Background(), auth, cliproxyexecutor.Request{ + Model: "gpt-6.1-sol", + Payload: []byte(compactionPayloadWithEmptyTools), + }, cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FromString("openai-response"), + }) + if err != nil { + t.Fatalf("Execute() error = %v", err) + } + if got := countWebSearchTools(gotBody); got != 1 { + t.Fatalf("web_search tool count = %d, want 1; body=%s", got, gotBody) + } + if got := len(gjson.GetBytes(gotBody, "tools").Array()); got != 1 { + t.Fatalf("tools len = %d, want 1; body=%s", got, gotBody) + } + }) + + t.Run("Execute compact endpoint injects web_search when input replays web_search_call", func(t *testing.T) { + var gotBody []byte + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + body, _ := io.ReadAll(r.Body) + gotBody = body + if hasWebSearchCallInInput(body) && countWebSearchTools(body) == 0 { + w.WriteHeader(http.StatusBadGateway) + _, _ = w.Write([]byte(`{"error":{"message":"response protection is unavailable","type":"internal_error","param":null,"code":null}}`)) + return + } + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"id":"resp_compact_1","object":"response.compaction","usage":{"input_tokens":10,"output_tokens":2,"total_tokens":12}}`)) + })) + defer server.Close() + + executor := NewCodexExecutor(&config.Config{}) + auth := &cliproxyauth.Auth{ + Provider: "codex", + Attributes: map[string]string{ + "base_url": server.URL, + "api_key": "test", + }, + } + + compactPayload := `{"model":"gpt-6.1-sol","input":[{"type":"message","role":"user","content":"history"},{"id":"ws_1","type":"web_search_call","status":"completed","action":{"type":"search","query":"q"}},{"type":"compaction_trigger"}]}` + _, err := executor.Execute(context.Background(), auth, cliproxyexecutor.Request{ + Model: "gpt-6.1-sol", + Payload: []byte(compactPayload), + }, cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FromString("openai-response"), + Alt: "responses/compact", + }) + if err != nil { + t.Fatalf("Execute compact error = %v", err) + } + if got := countWebSearchTools(gotBody); got != 1 { + t.Fatalf("compact web_search tool count = %d, want 1; body=%s", got, gotBody) + } + }) + + t.Run("prepareCodexWebsocketStream injects web_search when input replays web_search_call", func(t *testing.T) { + wsExecutor := NewCodexWebsocketsExecutor(&config.Config{}) + auth := &cliproxyauth.Auth{ + Provider: "codex", + Attributes: map[string]string{ + "base_url": "http://127.0.0.1:1", + "api_key": "test", + "plan_type": "plus", + }, + } + prepared, err := wsExecutor.prepareCodexWebsocketStream(context.Background(), auth, cliproxyexecutor.Request{ + Model: "gpt-6.1-sol", + Payload: []byte(compactionPayloadWithEmptyTools), + }, cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FromString("openai-response"), + Stream: true, + }) + if err != nil { + t.Fatalf("prepareCodexWebsocketStream() error = %v", err) + } + if got := countWebSearchTools(prepared.clientBody); got != 1 { + t.Fatalf("websocket clientBody web_search tool count = %d, want 1; body=%s", got, prepared.clientBody) + } + }) + + t.Run("existing web_search tool declaration is preserved without duplication", func(t *testing.T) { + payloadWithWebSearch := `{"model":"gpt-6.1-sol","input":[{"id":"ws_1","type":"web_search_call","status":"completed","action":{"type":"search","query":"q"}}],"tools":[{"type":"web_search","external_web_access":true}]}` + wsExecutor := NewCodexWebsocketsExecutor(&config.Config{SDKConfig: config.SDKConfig{DisableImageGeneration: config.DisableImageGenerationAll}}) + auth := &cliproxyauth.Auth{ + Provider: "codex", + Attributes: map[string]string{ + "base_url": "http://127.0.0.1:1", + "api_key": "test", + }, + } + prepared, err := wsExecutor.prepareCodexWebsocketStream(context.Background(), auth, cliproxyexecutor.Request{ + Model: "gpt-6.1-sol", + Payload: []byte(payloadWithWebSearch), + }, cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FromString("openai-response"), + Stream: true, + }) + if err != nil { + t.Fatalf("prepareCodexWebsocketStream() error = %v", err) + } + if got := countWebSearchTools(prepared.clientBody); got != 1 { + t.Fatalf("web_search tool count = %d, want 1; body=%s", got, prepared.clientBody) + } + if !gjson.GetBytes(prepared.clientBody, "tools.0.external_web_access").Bool() { + t.Fatalf("existing web_search options not preserved: %s", prepared.clientBody) + } + }) + + t.Run("Responses Lite request strips replayed web_search_call items without injecting top-level tools", func(t *testing.T) { + litePayload := `{"model":"gpt-6.1-sol","input":[{"type":"additional_tools","role":"developer","tools":[{"type":"namespace","name":"functions","description":"","tools":[{"type":"custom","name":"exec"}]}]},{"type":"message","role":"user","content":[{"type":"input_text","text":"search"}]},{"id":"ws_1","type":"web_search_call","status":"completed","action":{"type":"search","query":"q"}},{"id":"rs_1","type":"reasoning","summary":[]},{"type":"message","role":"assistant","content":[{"type":"output_text","text":"answer"}]},{"type":"compaction_trigger"}],"client_metadata":{"ws_request_header_x_openai_internal_codex_responses_lite":"true"}}` + wsExecutor := NewCodexWebsocketsExecutor(&config.Config{}) + auth := &cliproxyauth.Auth{ + Provider: "codex", + Attributes: map[string]string{ + "base_url": "http://127.0.0.1:1", + "api_key": "test", + }, + } + headers := make(http.Header) + headers.Set(codexResponsesLiteHeader, "true") + + prepared, err := wsExecutor.prepareCodexWebsocketStream(context.Background(), auth, cliproxyexecutor.Request{ + Model: "gpt-6.1-sol", + Payload: []byte(litePayload), + }, cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FromString("openai-response"), + Headers: headers, + Stream: true, + }) + if err != nil { + t.Fatalf("prepareCodexWebsocketStream() error = %v", err) + } + if gjson.GetBytes(prepared.clientBody, "tools").Exists() { + t.Fatalf("Responses Lite request must not inject top-level tools: %s", prepared.clientBody) + } + if hasWebSearchCallInInput(prepared.clientBody) { + t.Fatalf("Responses Lite request must strip replayed web_search_call items: %s", prepared.clientBody) + } + inputItems := gjson.GetBytes(prepared.clientBody, "input").Array() + if len(inputItems) != 5 { + t.Fatalf("input len = %d, want 5; body=%s", len(inputItems), prepared.clientBody) + } + }) + + t.Run("PayloadFinalizer runs after ensureWebSearchToolForHistory", func(t *testing.T) { + var gotBody []byte + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + body, _ := io.ReadAll(r.Body) + gotBody = body + w.Header().Set("Content-Type", "text/event-stream") + _, _ = w.Write([]byte("data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp_1\",\"object\":\"response\",\"status\":\"completed\",\"output\":[],\"usage\":{\"input_tokens\":10,\"output_tokens\":5,\"total_tokens\":15}}}\n\n")) + })) + defer server.Close() + + cfg := &config.Config{ + SDKConfig: config.SDKConfig{ + DisableImageGeneration: config.DisableImageGenerationAll, + }, + Payload: config.PayloadConfig{ + Filter: []config.PayloadFilterRule{ + { + Models: []config.PayloadModelRule{{Name: "gpt-6.1-sol", Protocol: "codex"}}, + Params: []string{"tools"}, + }, + }, + }, + } + executor := NewCodexExecutor(cfg) + auth := &cliproxyauth.Auth{ + Provider: "codex", + Attributes: map[string]string{ + "base_url": server.URL, + "api_key": "test", + }, + } + + _, err := executor.Execute(context.Background(), auth, cliproxyexecutor.Request{ + Model: "gpt-6.1-sol", + Payload: []byte(compactionPayloadWithEmptyTools), + }, cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FromString("openai-response"), + }) + if err != nil { + t.Fatalf("Execute() error = %v", err) + } + if gjson.GetBytes(gotBody, "tools").Exists() { + t.Fatalf("expected user payload filter rule to remove tools after built-in injection, got body=%s", gotBody) + } + }) +} diff --git a/internal/runtime/executor/codex_executor_execute.go b/internal/runtime/executor/codex_executor_execute.go index cd3eb06b879..635fafed33e 100644 --- a/internal/runtime/executor/codex_executor_execute.go +++ b/internal/runtime/executor/codex_executor_execute.go @@ -71,6 +71,7 @@ func (e *CodexExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, re body, _ = sjson.DeleteBytes(body, "stream_options") useSIWC := codexUseSIWC(auth) hasExplicitInstructions := gjson.GetBytes(req.Payload, "instructions").Exists() && strings.TrimSpace(gjson.GetBytes(req.Payload, "instructions").String()) != "" + body = ensureWebSearchToolForHistory(body, opts.Headers) if !useSIWC { body = normalizeCodexInstructions(body, helps.IsNativeCodexRequest(req.Payload, opts)) if e.cfg == nil || e.cfg.DisableImageGeneration == config.DisableImageGenerationOff { @@ -266,6 +267,7 @@ func (e *CodexExecutor) executeCompact(ctx context.Context, auth *cliproxyauth.A hasExplicitInstructions := gjson.GetBytes(req.Payload, "instructions").Exists() && strings.TrimSpace(gjson.GetBytes(req.Payload, "instructions").String()) != "" body = applyCodexSIWCCompactBodyRules(body, hasExplicitInstructions, cliproxyexecutor.Options{KeepTopLevelEffort: thinking.HasThinkingSuffix(req.Model)}) } + body = ensureWebSearchToolForHistory(body, opts.Headers) body = sanitizeOpenAIResponsesReasoningEncryptedContentWithCompatModelAndBaseURL(ctx, "codex executor", body, isCompat, resolveResponsesSignatureTargetModel(baseModel, req, opts), baseURL) body = normalizeCodexParallelToolCalls(body, opts.Headers) body = helps.NormalizeCodexToolSchemas(body) diff --git a/internal/runtime/executor/codex_executor_request.go b/internal/runtime/executor/codex_executor_request.go index bfb6cef70f8..0b5576af49f 100644 --- a/internal/runtime/executor/codex_executor_request.go +++ b/internal/runtime/executor/codex_executor_request.go @@ -404,6 +404,81 @@ func normalizeCodexInstructions(body []byte, nativeRequest ...bool) []byte { var imageGenToolJSON = []byte(`{"type":"image_generation","output_format":"png"}`) var imageGenToolArrayJSON = []byte(`[{"type":"image_generation","output_format":"png"}]`) +var webSearchToolJSON = []byte(`{"type":"web_search"}`) +var webSearchToolArrayJSON = []byte(`[{"type":"web_search"}]`) + +func isCodexWebSearchToolType(toolType string) bool { + switch strings.TrimSpace(toolType) { + case "web_search", "web_search_preview", "web_search_preview_2025_03_11": + return true + default: + return false + } +} + +// ensureWebSearchToolForHistory reconciles replayed web_search_call input items +// with the request's tool declarations so upstream Codex does not fail with +// "response protection is unavailable". Standard Responses requests receive a +// {"type":"web_search"} tool declaration when missing, while Responses Lite +// requests (which omit top-level hosted tools) strip the replayed search items. +func ensureWebSearchToolForHistory(body []byte, headers http.Header) []byte { + if !bytes.Contains(body, []byte("web_search_call")) { + return body + } + input := util.GetGJSONBytesNoCopy(body, "input") + if !input.IsArray() { + return body + } + hasWebSearchCall := false + input.ForEach(func(_, item gjson.Result) bool { + if strings.TrimSpace(item.Get("type").String()) == "web_search_call" { + hasWebSearchCall = true + return false + } + return true + }) + if !hasWebSearchCall { + return body + } + + tools := util.GetGJSONBytesNoCopy(body, "tools") + if tools.IsArray() { + for _, t := range tools.Array() { + if isCodexWebSearchToolType(t.Get("type").String()) { + return body + } + } + } + + if util.IsCodexResponsesLiteRequest(body, headers) { + items := input.Array() + rebuilt := make([]string, 0, len(items)) + for _, item := range items { + if strings.TrimSpace(item.Get("type").String()) == "web_search_call" { + continue + } + rebuilt = append(rebuilt, item.Raw) + } + updated, errSet := sjson.SetRawBytes(body, "input", []byte("["+strings.Join(rebuilt, ",")+"]")) + if errSet != nil { + return body + } + return updated + } + + if !tools.Exists() || !tools.IsArray() { + updated, errSet := sjson.SetRawBytes(body, "tools", webSearchToolArrayJSON) + if errSet != nil { + return body + } + return updated + } + updated, errSet := sjson.SetRawBytes(body, "tools.-1", webSearchToolJSON) + if errSet != nil { + return body + } + return updated +} func isCodexFreePlanAuth(auth *cliproxyauth.Auth) bool { if auth == nil || auth.Attributes == nil { diff --git a/internal/runtime/executor/codex_executor_stream.go b/internal/runtime/executor/codex_executor_stream.go index 9011eb41df9..ec844d9d91c 100644 --- a/internal/runtime/executor/codex_executor_stream.go +++ b/internal/runtime/executor/codex_executor_stream.go @@ -78,6 +78,7 @@ func (e *CodexExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au body = helps.SetStringIfDifferent(body, "model", baseModel) useSIWC := codexUseSIWC(auth) hasExplicitInstructions := gjson.GetBytes(req.Payload, "instructions").Exists() && strings.TrimSpace(gjson.GetBytes(req.Payload, "instructions").String()) != "" + body = ensureWebSearchToolForHistory(body, opts.Headers) if !useSIWC { body = normalizeCodexInstructions(body, preserveNativeOutput) if e.cfg == nil || e.cfg.DisableImageGeneration == config.DisableImageGenerationOff { diff --git a/internal/runtime/executor/codex_websockets_execute.go b/internal/runtime/executor/codex_websockets_execute.go index ace6706ae7e..c8175954dc2 100644 --- a/internal/runtime/executor/codex_websockets_execute.go +++ b/internal/runtime/executor/codex_websockets_execute.go @@ -72,6 +72,7 @@ func (e *CodexWebsocketsExecutor) Execute(ctx context.Context, auth *cliproxyaut body = helps.SetBoolIfDifferent(body, "stream", true) body, _ = sjson.DeleteBytes(body, "prompt_cache_retention") body, _ = sjson.DeleteBytes(body, "safety_identifier") + body = ensureWebSearchToolForHistory(body, opts.Headers) if !useSIWC { body = normalizeCodexInstructions(body, nativeRequest) if e.cfg == nil || e.cfg.DisableImageGeneration == config.DisableImageGenerationOff { diff --git a/internal/runtime/executor/codex_websockets_stream.go b/internal/runtime/executor/codex_websockets_stream.go index c0dbc8732cf..13366bc0cdb 100644 --- a/internal/runtime/executor/codex_websockets_stream.go +++ b/internal/runtime/executor/codex_websockets_stream.go @@ -800,6 +800,7 @@ func (e *CodexWebsocketsExecutor) prepareCodexWebsocketStream(ctx context.Contex hasExplicitInstructions := gjson.GetBytes(req.Payload, "instructions").Exists() && strings.TrimSpace(gjson.GetBytes(req.Payload, "instructions").String()) != "" finalizePayload := helps.NewPayloadFinalizer(e.cfg, "codex-websockets", baseModel, to.String(), "", originalTranslated, req, opts) body = helps.SetStringIfDifferent(body, "model", baseModel) + body = ensureWebSearchToolForHistory(body, opts.Headers) if !useSIWC { body = normalizeCodexInstructions(body, preserveNativeOutput) if e.cfg == nil || e.cfg.DisableImageGeneration == config.DisableImageGenerationOff { From 9adb32ec1b6945db576d185f1105f276fa7da5b1 Mon Sep 17 00:00:00 2001 From: awsldev Date: Mon, 19 Oct 2026 14:57:32 +0000 Subject: [PATCH 27/27] fix(sdk): use v8 imports in executor wrappers --- sdk/exec/antigravity.go | 6 +++--- sdk/exec/codex.go | 4 ++-- sdk/exec/xai.go | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/sdk/exec/antigravity.go b/sdk/exec/antigravity.go index 8d189d55591..ed5bebce13b 100644 --- a/sdk/exec/antigravity.go +++ b/sdk/exec/antigravity.go @@ -3,11 +3,11 @@ package exec import ( - "github.com/router-for-me/CLIProxyAPI/v7/internal/config" - internal "github.com/router-for-me/CLIProxyAPI/v7/internal/runtime/executor" + "github.com/router-for-me/CLIProxyAPI/v8/internal/config" + internal "github.com/router-for-me/CLIProxyAPI/v8/internal/runtime/executor" // Import builtin to register translators (claude -> antigravity, etc.) - _ "github.com/router-for-me/CLIProxyAPI/v7/sdk/translator/builtin" + _ "github.com/router-for-me/CLIProxyAPI/v8/sdk/translator/builtin" ) // AntigravityExecutor is a type alias for the internal AntigravityExecutor. diff --git a/sdk/exec/codex.go b/sdk/exec/codex.go index 904c8f66604..2ceeae04b99 100644 --- a/sdk/exec/codex.go +++ b/sdk/exec/codex.go @@ -5,8 +5,8 @@ package exec import ( - "github.com/router-for-me/CLIProxyAPI/v7/internal/config" - internal "github.com/router-for-me/CLIProxyAPI/v7/internal/runtime/executor" + "github.com/router-for-me/CLIProxyAPI/v8/internal/config" + internal "github.com/router-for-me/CLIProxyAPI/v8/internal/runtime/executor" ) // CodexExecutor is a type alias for the internal CodexExecutor. diff --git a/sdk/exec/xai.go b/sdk/exec/xai.go index cce22605b5a..c1ceb1f4fc8 100644 --- a/sdk/exec/xai.go +++ b/sdk/exec/xai.go @@ -5,8 +5,8 @@ package exec import ( - "github.com/router-for-me/CLIProxyAPI/v7/internal/config" - internal "github.com/router-for-me/CLIProxyAPI/v7/internal/runtime/executor" + "github.com/router-for-me/CLIProxyAPI/v8/internal/config" + internal "github.com/router-for-me/CLIProxyAPI/v8/internal/runtime/executor" ) // XAIExecutor is a type alias for the internal xAI/Grok executor.