diff --git a/internal/translator/openai/claude/openai_claude_response.go b/internal/translator/openai/claude/openai_claude_response.go index bdd0dfc6..11c17d36 100644 --- a/internal/translator/openai/claude/openai_claude_response.go +++ b/internal/translator/openai/claude/openai_claude_response.go @@ -8,6 +8,7 @@ package claude import ( "bytes" "context" + "fmt" "sort" "strings" @@ -304,14 +305,8 @@ func convertOpenAIStreamingChunkToAnthropic(rawJSON []byte, param *ConvertOpenAI if !param.ContentBlocksStopped { for _, index := range toolCallAccumulatorIndexes(param.ToolCallsAccumulator) { accumulator := param.ToolCallsAccumulator[index] - if !accumulator.StartEmitted { - // Belated emit for streams that supplied a valid name but - // never sent an id. SanitizeClaudeToolID("") produces the - // expected stable synthetic toolu__ ID shape. - if accumulator.Name == "" { - continue - } - emitToolUseStart(param, index, accumulator, &results) + if !emitBelatedToolUseStart(param, index, accumulator, &results) { + continue } blockIndex := param.toolContentBlockIndex(index) @@ -377,13 +372,8 @@ func convertOpenAIDoneToAnthropic(param *ConvertOpenAIResponseToAnthropicParams) if !param.ContentBlocksStopped { for _, index := range toolCallAccumulatorIndexes(param.ToolCallsAccumulator) { accumulator := param.ToolCallsAccumulator[index] - if !accumulator.StartEmitted { - // Belated emit at [DONE]; same behavior as the finish_reason - // path for name-but-no-id streams. - if accumulator.Name == "" { - continue - } - emitToolUseStart(param, index, accumulator, &results) + if !emitBelatedToolUseStart(param, index, accumulator, &results) { + continue } blockIndex := param.toolContentBlockIndex(index) @@ -591,6 +581,29 @@ func emitToolUseStart(param *ConvertOpenAIResponseToAnthropicParams, openAIToolI param.SawToolCall = true } +// emitBelatedToolUseStart finalizes a tool_use block that never received a +// mid-stream start. Some OpenAI-compatible providers leave function.name empty +// for the whole stream; dropping those calls loses tool_use for Claude Code and +// can trigger retry loops. When name is still empty but the call has an id +// and/or arguments, synthesize tool_ instead of silently discarding it. +// Returns false when the accumulator has no usable tool-call signal. +func emitBelatedToolUseStart(param *ConvertOpenAIResponseToAnthropicParams, openAIToolIndex int, accumulator *ToolCallAccumulator, results *[][]byte) bool { + if accumulator == nil { + return false + } + if accumulator.StartEmitted { + return true + } + if accumulator.Name == "" && accumulator.ID == "" && accumulator.Arguments.Len() == 0 { + return false + } + if accumulator.Name == "" { + accumulator.Name = fmt.Sprintf("tool_%d", openAIToolIndex) + } + emitToolUseStart(param, openAIToolIndex, accumulator, results) + return true +} + func toolCallAccumulatorIndexes(accumulators map[int]*ToolCallAccumulator) []int { indexes := make([]int, 0, len(accumulators)) for index := range accumulators { diff --git a/internal/translator/openai/claude/openai_claude_response_test.go b/internal/translator/openai/claude/openai_claude_response_test.go index f5b063b2..5382c4ed 100644 --- a/internal/translator/openai/claude/openai_claude_response_test.go +++ b/internal/translator/openai/claude/openai_claude_response_test.go @@ -163,17 +163,24 @@ func TestStreamingTool_EmptyNameThroughout(t *testing.T) { `{"id":"c1","model":"m","choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}`, ) - if got := len(toolUseStarts(events)); got != 0 { - t.Fatalf("expected zero tool_use content_block_start, got %d (events=%+v)", got, events) + starts := toolUseStarts(events) + if len(starts) != 1 { + t.Fatalf("expected one tool_use content_block_start with synthetic name, got %d (events=%+v)", len(starts), events) } - if got := countByType(events, "content_block_delta"); got != 0 { - t.Fatalf("expected zero content_block_delta when start was suppressed, got %d", got) + if name := gjson.Get(starts[0].Payload, "content_block.name").String(); name != "tool_0" { + t.Fatalf("announced tool name = %q, want %q", name, "tool_0") } - if got := countByType(events, "content_block_stop"); got != 0 { - t.Fatalf("expected zero content_block_stop when start was suppressed, got %d", got) + if id := gjson.Get(starts[0].Payload, "content_block.id").String(); id != "call_a" { + t.Fatalf("announced tool id = %q, want %q", id, "call_a") } - if got := lastStopReason(events); got == "tool_use" { - t.Fatalf("stop_reason must not be tool_use when zero tool_use blocks were emitted; got %q", got) + if got := countByType(events, "content_block_delta"); got != 1 { + t.Fatalf("expected one content_block_delta for accumulated args, got %d", got) + } + if got := countByType(events, "content_block_stop"); got != 1 { + t.Fatalf("expected one content_block_stop, got %d", got) + } + if got := lastStopReason(events); got != "tool_use" { + t.Fatalf("stop_reason = %q, want %q", got, "tool_use") } } @@ -182,11 +189,21 @@ func TestStreamingTool_NullName(t *testing.T) { `{"id":"c1","model":"m","choices":[{"index":0,"delta":{"role":"assistant","tool_calls":[{"index":0,"id":"call_a","function":{"name":null,"arguments":""}}]}}]}`, `{"id":"c1","model":"m","choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}`, ) - if got := len(toolUseStarts(events)); got != 0 { - t.Fatalf("null name must not produce a tool_use start; got %d", got) + starts := toolUseStarts(events) + if len(starts) != 1 { + t.Fatalf("null name with id should belated-emit synthetic tool name; got %d", len(starts)) + } + if name := gjson.Get(starts[0].Payload, "content_block.name").String(); name != "tool_0" { + t.Fatalf("announced tool name = %q, want %q", name, "tool_0") } - if got := countByType(events, "content_block_stop"); got != 0 { - t.Fatalf("null name must not produce content_block_stop; got %d", got) + if id := gjson.Get(starts[0].Payload, "content_block.id").String(); id != "call_a" { + t.Fatalf("announced tool id = %q, want %q", id, "call_a") + } + if got := countByType(events, "content_block_stop"); got != 1 { + t.Fatalf("expected one content_block_stop, got %d", got) + } + if got := lastStopReason(events); got != "tool_use" { + t.Fatalf("stop_reason = %q, want %q", got, "tool_use") } } @@ -195,8 +212,12 @@ func TestStreamingTool_NonStringName(t *testing.T) { `{"id":"c1","model":"m","choices":[{"index":0,"delta":{"role":"assistant","tool_calls":[{"index":0,"id":"call_a","function":{"name":123,"arguments":""}}]}}]}`, `{"id":"c1","model":"m","choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}`, ) - if got := len(toolUseStarts(events)); got != 0 { - t.Fatalf("non-string name must not produce a tool_use start; got %d", got) + starts := toolUseStarts(events) + if len(starts) != 1 { + t.Fatalf("non-string name with id should belated-emit synthetic tool name; got %d", len(starts)) + } + if name := gjson.Get(starts[0].Payload, "content_block.name").String(); name != "tool_0" { + t.Fatalf("announced tool name = %q, want %q", name, "tool_0") } } @@ -220,10 +241,10 @@ func TestStreamingTool_RepeatedName(t *testing.T) { } } -func TestStreamingTool_MixedSuppressedAndValid(t *testing.T) { +func TestStreamingTool_MixedEmptyNameAndValid(t *testing.T) { events := runStream(t, streamReq, `{"id":"c1","model":"m","choices":[{"index":0,"delta":{"role":"assistant","tool_calls":[ - {"index":0,"id":"call_skip","function":{"name":"","arguments":""}}, + {"index":0,"id":"call_empty","function":{"name":"","arguments":""}}, {"index":1,"id":"call_real","function":{"name":"do_it","arguments":""}} ]}}]}`, `{"id":"c1","model":"m","choices":[{"index":0,"delta":{"tool_calls":[ @@ -233,16 +254,36 @@ func TestStreamingTool_MixedSuppressedAndValid(t *testing.T) { ) starts := toolUseStarts(events) - if len(starts) != 1 { - t.Fatalf("expected exactly one tool_use start, got %d", len(starts)) + if len(starts) != 2 { + t.Fatalf("expected two tool_use starts (valid mid-stream + synthetic empty-name), got %d", len(starts)) } - if got := countByType(events, "content_block_stop"); got != 1 { - t.Fatalf("expected exactly one content_block_stop, got %d", got) + // Valid name+id is emitted mid-stream first; empty-name is belated at finish. + if name := gjson.Get(starts[0].Payload, "content_block.name").String(); name != "do_it" { + t.Fatalf("first tool name = %q, want %q", name, "do_it") + } + if name := gjson.Get(starts[1].Payload, "content_block.name").String(); name != "tool_0" { + t.Fatalf("second tool name = %q, want %q", name, "tool_0") + } + if got := countByType(events, "content_block_stop"); got != 2 { + t.Fatalf("expected two content_block_stop events, got %d", got) } indices := blockIndices(events) - if len(indices) == 0 || indices[0] != 0 { - t.Fatalf("first content_block_start index must be 0, got %v", indices) + if len(indices) < 2 || indices[0] != 0 || indices[1] != 1 { + t.Fatalf("content_block_start indices must be [0,1], got %v", indices) + } +} + +func TestStreamingTool_EmptyNameWithoutSignalIsSuppressed(t *testing.T) { + events := runStream(t, streamReq, + `{"id":"c1","model":"m","choices":[{"index":0,"delta":{"role":"assistant","tool_calls":[{"index":0,"function":{"name":"","arguments":""}}]}}]}`, + `{"id":"c1","model":"m","choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}`, + ) + if got := len(toolUseStarts(events)); got != 0 { + t.Fatalf("empty name without id/args must stay suppressed; got %d", got) + } + if got := lastStopReason(events); got == "tool_use" { + t.Fatalf("stop_reason must not be tool_use when zero tool_use blocks were emitted; got %q", got) } } @@ -371,10 +412,10 @@ func TestStreamingTool_LateIDAfterFinalization(t *testing.T) { } } -func TestStreamingTool_StopReasonMixedSuppressedAndValid(t *testing.T) { +func TestStreamingTool_StopReasonMixedEmptyNameAndValid(t *testing.T) { events := runStream(t, streamReq, `{"id":"c1","model":"m","choices":[{"index":0,"delta":{"role":"assistant","tool_calls":[ - {"index":0,"id":"call_skip","function":{"name":"","arguments":""}}, + {"index":0,"id":"call_empty","function":{"name":"","arguments":""}}, {"index":1,"id":"call_real","function":{"name":"do_it","arguments":"{}"}} ]}}]}`, `{"id":"c1","model":"m","choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}`, @@ -382,4 +423,28 @@ func TestStreamingTool_StopReasonMixedSuppressedAndValid(t *testing.T) { if got := lastStopReason(events); got != "tool_use" { t.Fatalf("stop_reason = %q, want %q", got, "tool_use") } + if got := len(toolUseStarts(events)); got != 2 { + t.Fatalf("expected two tool_use starts, got %d", got) + } +} + +func TestStreamingTool_EmptyNameArgsOnlyNoID(t *testing.T) { + events := runStream(t, streamReq, + `{"id":"c1","model":"m","choices":[{"index":0,"delta":{"role":"assistant","tool_calls":[{"index":0,"function":{"name":"","arguments":"{\"q\":\"x\"}"}}]}}]}`, + `{"id":"c1","model":"m","choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}`, + ) + starts := toolUseStarts(events) + if len(starts) != 1 { + t.Fatalf("expected one belated tool_use start for empty-name args-only call, got %d", len(starts)) + } + if name := gjson.Get(starts[0].Payload, "content_block.name").String(); name != "tool_0" { + t.Fatalf("announced tool name = %q, want %q", name, "tool_0") + } + id := gjson.Get(starts[0].Payload, "content_block.id").String() + if !strings.HasPrefix(id, "toolu_") { + t.Fatalf("synthetic id should match toolu__, got %q", id) + } + if got := lastStopReason(events); got != "tool_use" { + t.Fatalf("stop_reason = %q, want %q", got, "tool_use") + } } -- knotmirror 0.0.1 From 4b3cc55cdc93b81dda20798a95d6a3c70160e9a6 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Sat, 8 Aug 2026 04:53:34 +0800 Subject: [PATCH 02/17] fix(cliproxy): centralize client error status mapping and apply context cancellation/deadline HTTP codes Closes: #4601 --- internal/api/server_routes.go | 24 ++--- internal/client/codex/live/live.go | 22 ++--- internal/client/codex/live/sideband.go | 3 +- internal/clienterror/client_error.go | 39 ++++++++ internal/clienterror/client_error_test.go | 91 +++++++++++++++++++ .../runtime/executor/helps/usage_helpers.go | 12 +-- .../executor/helps/usage_helpers_test.go | 36 ++++++++ .../handlers/handlers_error_response_test.go | 69 ++++++++++++++ sdk/api/handlers/handlers_errors.go | 11 +-- sdk/api/handlers/handlers_execution.go | 7 +- .../handlers/openai/openai_images_handlers.go | 7 +- .../openai_responses_websocket_forward.go | 9 +- .../handlers/openai/openai_videos_handlers.go | 11 ++- sdk/cliproxy/auth/conductor_cooldown.go | 5 +- .../connection_lifecycle_cooldown_test.go | 10 ++ 15 files changed, 297 insertions(+), 59 deletions(-) diff --git a/internal/api/server_routes.go b/internal/api/server_routes.go index 3003e2ce..e008e01f 100644 --- a/internal/api/server_routes.go +++ b/internal/api/server_routes.go @@ -18,6 +18,7 @@ import ( codexlive "github.com/router-for-me/CLIProxyAPI/v7/internal/client/codex/live" codexmodels "github.com/router-for-me/CLIProxyAPI/v7/internal/client/codex/models" "github.com/router-for-me/CLIProxyAPI/v7/internal/client/grokbuild" + "github.com/router-for-me/CLIProxyAPI/v7/internal/clienterror" "github.com/router-for-me/CLIProxyAPI/v7/internal/home" "github.com/router-for-me/CLIProxyAPI/v7/internal/logging" "github.com/router-for-me/CLIProxyAPI/v7/internal/registry" @@ -269,7 +270,7 @@ func (s *Server) codexAlphaSearch(c *gin.Context) { body, err := io.ReadAll(io.LimitReader(c.Request.Body, 16<<20)) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "Failed to read search request"}) + c.JSON(clienterror.HTTPStatusFromErrorOr(err, http.StatusBadRequest), gin.H{"error": "Failed to read search request"}) return } @@ -288,7 +289,7 @@ func (s *Server) codexAlphaSearch(c *gin.Context) { selectionModel, errRoute := s.codexAlphaSearchSelectionModel(ctx, c, body, strings.TrimSpace(routing.Model)) if errRoute != nil { log.WithError(errRoute).Warn("codex alpha search: model router returned an unsupported target") - c.JSON(http.StatusServiceUnavailable, gin.H{"error": errRoute.Error()}) + c.JSON(clienterror.HTTPStatusFromErrorOr(errRoute, http.StatusServiceUnavailable), gin.H{"error": errRoute.Error()}) return } selectionOpts := coreexecutor.Options{Headers: selectionHeaders, OriginalRequest: body} @@ -303,10 +304,7 @@ func (s *Server) codexAlphaSearch(c *gin.Context) { selected, err = s.handlers.AuthManager.SelectAuthWithCredentialPolicy(ctx, "codex", selectionModel, auth.CredentialPolicyCodexAlphaSearchV1, selectionOpts) } if err != nil { - status := http.StatusServiceUnavailable - if statusError, ok := err.(interface{ StatusCode() int }); ok && statusError.StatusCode() > 0 { - status = statusError.StatusCode() - } + status := clienterror.HTTPStatusFromErrorOr(err, http.StatusServiceUnavailable) for _, value := range auth.SafeResponseHeaders(err).Values("Retry-After") { c.Writer.Header().Add("Retry-After", value) } @@ -384,7 +382,7 @@ func (s *Server) codexAlphaSearch(c *gin.Context) { if selection != nil { selection.End("attempt_canceled") } - c.JSON(http.StatusRequestTimeout, gin.H{"error": errCtx.Error()}) + c.JSON(clienterror.HTTPStatusFromErrorOr(errCtx, http.StatusRequestTimeout), gin.H{"error": errCtx.Error()}) return } resp, err := performRequest(selected) @@ -400,7 +398,7 @@ func (s *Server) codexAlphaSearch(c *gin.Context) { selection.End("request_failed") } helps.RecordAPIResponseError(ctx, s.cfg, err) - c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()}) + c.JSON(clienterror.HTTPStatusFromErrorOr(err, http.StatusBadGateway), gin.H{"error": err.Error()}) return } if selection != nil && resp.StatusCode == http.StatusUnauthorized { @@ -413,11 +411,7 @@ func (s *Server) codexAlphaSearch(c *gin.Context) { refreshed, didRefresh, errRefresh := s.handlers.AuthManager.RefreshHomeSelectionAfterUnauthorized(ctx, selection, selected) if errRefresh != nil { selection.End("refresh_failed") - status := http.StatusServiceUnavailable - if statusError, ok := errRefresh.(interface{ StatusCode() int }); ok && statusError.StatusCode() > 0 { - status = statusError.StatusCode() - } - c.JSON(status, gin.H{"error": errRefresh.Error()}) + c.JSON(clienterror.HTTPStatusFromErrorOr(errRefresh, http.StatusServiceUnavailable), gin.H{"error": errRefresh.Error()}) return } if !didRefresh || refreshed == nil { @@ -436,7 +430,7 @@ func (s *Server) codexAlphaSearch(c *gin.Context) { } selection.End("retry_failed") helps.RecordAPIResponseError(ctx, s.cfg, err) - c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()}) + c.JSON(clienterror.HTTPStatusFromErrorOr(err, http.StatusBadGateway), gin.H{"error": err.Error()}) return } if resp.StatusCode == http.StatusUnauthorized { @@ -464,7 +458,7 @@ func (s *Server) codexAlphaSearch(c *gin.Context) { upstreamBody, err := io.ReadAll(io.LimitReader(resp.Body, 32<<20)) if err != nil { helps.RecordAPIResponseError(ctx, s.cfg, err) - c.JSON(http.StatusBadGateway, gin.H{"error": "Failed to read Codex search response"}) + c.JSON(clienterror.HTTPStatusFromErrorOr(err, http.StatusBadGateway), gin.H{"error": "Failed to read Codex search response"}) return } helps.AppendAPIResponseChunk(ctx, s.cfg, upstreamBody) diff --git a/internal/client/codex/live/live.go b/internal/client/codex/live/live.go index 9e8b155d..ac36682e 100644 --- a/internal/client/codex/live/live.go +++ b/internal/client/codex/live/live.go @@ -17,6 +17,7 @@ import ( "sync" "github.com/gin-gonic/gin" + "github.com/router-for-me/CLIProxyAPI/v7/internal/clienterror" "github.com/router-for-me/CLIProxyAPI/v7/internal/config" "github.com/router-for-me/CLIProxyAPI/v7/internal/logging" "github.com/router-for-me/CLIProxyAPI/v7/internal/runtime/executor/helps" @@ -175,7 +176,7 @@ func (h *Handler) Handle(c *gin.Context) { body, errRead := readBody(c.Request.Body) if errRead != nil { - status := http.StatusBadRequest + status := clienterror.HTTPStatusFromErrorOr(errRead, http.StatusBadRequest) if errors.Is(errRead, errBodyTooLarge) { status = http.StatusRequestEntityTooLarge } @@ -246,7 +247,7 @@ func (h *Handler) Handle(c *gin.Context) { authIndex: selectedIndex, }) if errSDP != nil { - c.JSON(http.StatusBadGateway, gin.H{"error": errSDP.Error()}) + c.JSON(clienterror.HTTPStatusFromErrorOr(errSDP, http.StatusBadGateway), gin.H{"error": errSDP.Error()}) return } defer func() { @@ -291,7 +292,7 @@ func (h *Handler) Handle(c *gin.Context) { if selection != nil { selection.End("attempt_canceled") } - c.JSON(http.StatusRequestTimeout, gin.H{"error": errContext.Error()}) + c.JSON(clienterror.HTTPStatusFromErrorOr(errContext, http.StatusRequestTimeout), gin.H{"error": errContext.Error()}) return } resp, errRequest := performRequest(selected) @@ -300,7 +301,7 @@ func (h *Handler) Handle(c *gin.Context) { selection.End("request_failed") } helps.RecordAPIResponseError(ctx, runtimeConfig, errRequest) - c.JSON(http.StatusBadGateway, gin.H{"error": errRequest.Error()}) + c.JSON(clienterror.HTTPStatusFromErrorOr(errRequest, http.StatusBadGateway), gin.H{"error": errRequest.Error()}) return } if selection != nil && resp.StatusCode == http.StatusUnauthorized { @@ -327,7 +328,7 @@ func (h *Handler) Handle(c *gin.Context) { if errRequest != nil { selection.End("retry_failed") helps.RecordAPIResponseError(ctx, runtimeConfig, errRequest) - c.JSON(http.StatusBadGateway, gin.H{"error": errRequest.Error()}) + c.JSON(clienterror.HTTPStatusFromErrorOr(errRequest, http.StatusBadGateway), gin.H{"error": errRequest.Error()}) return } if resp.StatusCode == http.StatusUnauthorized { @@ -361,10 +362,12 @@ func (h *Handler) Handle(c *gin.Context) { if errResponse != nil { helps.RecordAPIResponseError(ctx, runtimeConfig, errResponse) message := "Failed to read Codex live response" + status := clienterror.HTTPStatusFromErrorOr(errResponse, http.StatusBadGateway) if errors.Is(errResponse, errBodyTooLarge) { message = "Codex live response body too large" + status = http.StatusBadGateway } - c.JSON(http.StatusBadGateway, gin.H{"error": message}) + c.JSON(status, gin.H{"error": message}) return } helps.AppendAPIResponseChunk(ctx, runtimeConfig, responseBody) @@ -389,7 +392,7 @@ func (h *Handler) Handle(c *gin.Context) { } downstreamAnswer, errAnswer := mediaSession.AcceptUpstreamAnswer(ctx, upstreamAnswer) if errAnswer != nil { - c.JSON(http.StatusBadGateway, gin.H{"error": errAnswer.Error()}) + c.JSON(clienterror.HTTPStatusFromErrorOr(errAnswer, http.StatusBadGateway), gin.H{"error": errAnswer.Error()}) return } responseBodyToWrite = []byte(downstreamAnswer) @@ -718,10 +721,7 @@ func writeResponseHeaders(destination, source http.Header) { } func writeSelectionError(c *gin.Context, err error) { - status := http.StatusServiceUnavailable - if statusError, ok := err.(interface{ StatusCode() int }); ok && statusError.StatusCode() > 0 { - status = statusError.StatusCode() - } + status := clienterror.HTTPStatusFromErrorOr(err, http.StatusServiceUnavailable) for _, value := range auth.SafeResponseHeaders(err).Values("Retry-After") { c.Writer.Header().Add("Retry-After", value) } diff --git a/internal/client/codex/live/sideband.go b/internal/client/codex/live/sideband.go index fe3b6155..cdbbbd5b 100644 --- a/internal/client/codex/live/sideband.go +++ b/internal/client/codex/live/sideband.go @@ -14,6 +14,7 @@ import ( "github.com/gin-gonic/gin" "github.com/gorilla/websocket" + "github.com/router-for-me/CLIProxyAPI/v7/internal/clienterror" "github.com/router-for-me/CLIProxyAPI/v7/internal/config" "github.com/router-for-me/CLIProxyAPI/v7/internal/logging" "github.com/router-for-me/CLIProxyAPI/v7/internal/runtime/executor/helps" @@ -550,7 +551,7 @@ func callIDFromLocation(location string) string { } func handleSidebandDialError(c *gin.Context, ctx context.Context, cfg *config.Config, response *http.Response, errDial error) { - status := http.StatusBadGateway + status := clienterror.HTTPStatusFromErrorOr(errDial, http.StatusBadGateway) if response != nil { if response.StatusCode > 0 { status = response.StatusCode diff --git a/internal/clienterror/client_error.go b/internal/clienterror/client_error.go index ac575e9c..3a2f532d 100644 --- a/internal/clienterror/client_error.go +++ b/internal/clienterror/client_error.go @@ -2,6 +2,7 @@ package clienterror import ( + "context" "encoding/json" "errors" "net/http" @@ -10,6 +11,10 @@ import ( "github.com/tidwall/gjson" ) +// StatusClientClosedRequest is the nginx-style status used when the client +// aborts the request before the proxy finishes (context.Canceled). +const StatusClientClosedRequest = 499 + var requestFaultCodes = map[string]struct{}{ "cyber_policy": {}, "context_length_exceeded": {}, @@ -29,6 +34,40 @@ var requestFaultTypes = map[string]struct{}{ "invalid_prompt": {}, } +// HTTPStatusFromError extracts an HTTP status from err. +// Explicit StatusCode() values win. Otherwise context.Canceled maps to 499 +// and context.DeadlineExceeded maps to 504. Returns 0 when unknown. +func HTTPStatusFromError(err error) int { + if err == nil { + return 0 + } + type statusCoder interface { + StatusCode() int + } + var sc statusCoder + if errors.As(err, &sc) && sc != nil { + if code := sc.StatusCode(); code > 0 { + return code + } + } + if errors.Is(err, context.Canceled) { + return StatusClientClosedRequest + } + if errors.Is(err, context.DeadlineExceeded) { + return http.StatusGatewayTimeout + } + return 0 +} + +// HTTPStatusFromErrorOr is like HTTPStatusFromError but returns fallback when +// the error does not carry a known status. +func HTTPStatusFromErrorOr(err error, fallback int) int { + if code := HTTPStatusFromError(err); code > 0 { + return code + } + return fallback +} + // IsRequestFault reports whether an upstream failure is caused by the request // and therefore must not rotate or penalize credentials. func IsRequestFault(status int, err error) bool { diff --git a/internal/clienterror/client_error_test.go b/internal/clienterror/client_error_test.go index 2582b6f9..db17df39 100644 --- a/internal/clienterror/client_error_test.go +++ b/internal/clienterror/client_error_test.go @@ -1,8 +1,11 @@ package clienterror import ( + "context" "errors" + "fmt" "net/http" + "net/url" "testing" ) @@ -14,6 +17,94 @@ type statusError struct { func (e statusError) Error() string { return e.body } func (e statusError) StatusCode() int { return e.status } +func TestHTTPStatusFromError(t *testing.T) { + tests := []struct { + name string + err error + want int + }{ + {name: "nil", err: nil, want: 0}, + {name: "plain error", err: errors.New("boom"), want: 0}, + {name: "context canceled", err: context.Canceled, want: StatusClientClosedRequest}, + {name: "context deadline exceeded", err: context.DeadlineExceeded, want: http.StatusGatewayTimeout}, + { + name: "url error wraps canceled", + err: &url.Error{Op: "Post", URL: "https://example.com", Err: context.Canceled}, + want: StatusClientClosedRequest, + }, + { + name: "url error wraps deadline", + err: &url.Error{Op: "Post", URL: "https://example.com", Err: context.DeadlineExceeded}, + want: http.StatusGatewayTimeout, + }, + { + name: "fmt wrap canceled", + err: fmt.Errorf("upstream: %w", context.Canceled), + want: StatusClientClosedRequest, + }, + { + name: "explicit status code wins", + err: statusError{status: http.StatusTooManyRequests, body: "rate limited"}, + want: http.StatusTooManyRequests, + }, + { + name: "explicit status wins over canceled unwrap", + err: statusAndUnwrapError{ + status: http.StatusTooManyRequests, + body: "rate limited", + cause: context.Canceled, + }, + want: http.StatusTooManyRequests, + }, + { + name: "zero status code falls through to canceled unwrap", + err: statusAndUnwrapError{ + status: 0, + body: "canceled", + cause: context.Canceled, + }, + want: StatusClientClosedRequest, + }, + { + name: "zero status code without unwrap stays unknown", + err: statusError{status: 0, body: context.Canceled.Error()}, + want: 0, + }, + { + name: "wrapped status code via errors.As", + err: fmt.Errorf("execute failed: %w", statusError{status: http.StatusUnauthorized, body: "unauthorized"}), + want: http.StatusUnauthorized, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + if got := HTTPStatusFromError(tc.err); got != tc.want { + t.Fatalf("HTTPStatusFromError() = %d, want %d", got, tc.want) + } + }) + } + + if got := HTTPStatusFromErrorOr(errors.New("boom"), http.StatusBadGateway); got != http.StatusBadGateway { + t.Fatalf("HTTPStatusFromErrorOr(plain) = %d, want %d", got, http.StatusBadGateway) + } + if got := HTTPStatusFromErrorOr(context.Canceled, http.StatusBadGateway); got != StatusClientClosedRequest { + t.Fatalf("HTTPStatusFromErrorOr(canceled) = %d, want %d", got, StatusClientClosedRequest) + } +} + +type statusAndUnwrapError struct { + status int + body string + cause error +} + +func (e statusAndUnwrapError) Error() string { return e.body } +func (e statusAndUnwrapError) StatusCode() int { + return e.status +} +func (e statusAndUnwrapError) Unwrap() error { return e.cause } + func TestIsRequestFaultStructuredIdentifiers(t *testing.T) { for _, code := range []string{ "cyber_policy", diff --git a/internal/runtime/executor/helps/usage_helpers.go b/internal/runtime/executor/helps/usage_helpers.go index ec305c88..fa355bd6 100644 --- a/internal/runtime/executor/helps/usage_helpers.go +++ b/internal/runtime/executor/helps/usage_helpers.go @@ -3,7 +3,6 @@ package helps import ( "bytes" "context" - "errors" "fmt" "io" "net/http" @@ -13,6 +12,7 @@ import ( "time" "github.com/gin-gonic/gin" + "github.com/router-for-me/CLIProxyAPI/v7/internal/clienterror" internallogging "github.com/router-for-me/CLIProxyAPI/v7/internal/logging" "github.com/router-for-me/CLIProxyAPI/v7/internal/thinking" cliproxyauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" @@ -306,14 +306,10 @@ func failFromErrors(errs ...error) usage.Failure { if err == nil { continue } - fail := usage.Failure{ - Body: strings.TrimSpace(err.Error()), + return usage.Failure{ + Body: strings.TrimSpace(err.Error()), + StatusCode: clienterror.HTTPStatusFromError(err), } - var se interface{ StatusCode() int } - if errors.As(err, &se) && se != nil { - fail.StatusCode = se.StatusCode() - } - return fail } return usage.Failure{} } diff --git a/internal/runtime/executor/helps/usage_helpers_test.go b/internal/runtime/executor/helps/usage_helpers_test.go index aa41f636..93a5ab13 100644 --- a/internal/runtime/executor/helps/usage_helpers_test.go +++ b/internal/runtime/executor/helps/usage_helpers_test.go @@ -2,12 +2,15 @@ package helps import ( "context" + "errors" "io" "net/http" + "net/url" "strings" "testing" "time" + "github.com/router-for-me/CLIProxyAPI/v7/internal/clienterror" "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/usage" ) @@ -660,6 +663,39 @@ func TestUsageReporterBuildAdditionalModelRecordSkipsZeroTokens(t *testing.T) { } } +func TestFailFromErrorsMapsContextStatuses(t *testing.T) { + tests := []struct { + name string + err error + want int + }{ + {name: "canceled", err: context.Canceled, want: clienterror.StatusClientClosedRequest}, + {name: "deadline", err: context.DeadlineExceeded, want: http.StatusGatewayTimeout}, + { + name: "url error wraps canceled", + err: &url.Error{Op: "Post", URL: "https://example.com", Err: context.Canceled}, + want: clienterror.StatusClientClosedRequest, + }, + {name: "plain error", err: errors.New("boom"), want: 0}, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + fail := failFromErrors(tc.err) + if fail.StatusCode != tc.want { + t.Fatalf("StatusCode = %d, want %d; body=%q", fail.StatusCode, tc.want, fail.Body) + } + if strings.TrimSpace(fail.Body) == "" { + t.Fatalf("expected non-empty failure body") + } + }) + } + + if fail := failFromErrors(nil, nil); fail.StatusCode != 0 || fail.Body != "" { + t.Fatalf("failFromErrors(nil) = %+v, want empty failure", fail) + } +} + type roundTripFunc func(*http.Request) (*http.Response, error) func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { diff --git a/sdk/api/handlers/handlers_error_response_test.go b/sdk/api/handlers/handlers_error_response_test.go index 561d98d6..c5253901 100644 --- a/sdk/api/handlers/handlers_error_response_test.go +++ b/sdk/api/handlers/handlers_error_response_test.go @@ -1,15 +1,18 @@ package handlers import ( + "context" "errors" "net/http" "net/http/httptest" + "net/url" "reflect" "strings" "testing" "time" "github.com/gin-gonic/gin" + "github.com/router-for-me/CLIProxyAPI/v7/internal/clienterror" "github.com/router-for-me/CLIProxyAPI/v7/internal/interfaces" coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" sdkconfig "github.com/router-for-me/CLIProxyAPI/v7/sdk/config" @@ -209,3 +212,69 @@ func TestEnrichAuthSelectionError_IgnoresOtherErrors(t *testing.T) { t.Fatalf("expected original error to be returned unchanged") } } + +func TestExecutionErrorMessageMapsContextStatuses(t *testing.T) { + tests := []struct { + name string + err error + want int + }{ + {name: "canceled", err: context.Canceled, want: clienterror.StatusClientClosedRequest}, + {name: "deadline", err: context.DeadlineExceeded, want: http.StatusGatewayTimeout}, + { + name: "url error wraps canceled", + err: &url.Error{Op: "Post", URL: "https://example.com", Err: context.Canceled}, + want: clienterror.StatusClientClosedRequest, + }, + {name: "plain error defaults to 500", err: errors.New("boom"), want: http.StatusInternalServerError}, + { + name: "explicit status wins", + err: &coreauth.Error{Code: "rate_limited", Message: "slow down", HTTPStatus: http.StatusTooManyRequests}, + want: http.StatusTooManyRequests, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + msg := executionErrorMessage(tc.err) + if msg == nil { + t.Fatalf("executionErrorMessage() returned nil") + } + if msg.StatusCode != tc.want { + t.Fatalf("StatusCode = %d, want %d", msg.StatusCode, tc.want) + } + if msg.Error != tc.err { + t.Fatalf("Error = %v, want original %v", msg.Error, tc.err) + } + }) + } +} + +func TestStatusFromErrorMapsContextStatuses(t *testing.T) { + if got := statusFromError(context.Canceled); got != clienterror.StatusClientClosedRequest { + t.Fatalf("statusFromError(canceled) = %d, want %d", got, clienterror.StatusClientClosedRequest) + } + if got := statusFromError(context.DeadlineExceeded); got != http.StatusGatewayTimeout { + t.Fatalf("statusFromError(deadline) = %d, want %d", got, http.StatusGatewayTimeout) + } + if got := statusFromError(&url.Error{Op: "Post", URL: "https://example.com", Err: context.Canceled}); got != clienterror.StatusClientClosedRequest { + t.Fatalf("statusFromError(url canceled) = %d, want %d", got, clienterror.StatusClientClosedRequest) + } + if got := statusFromError(errors.New("boom")); got != 0 { + t.Fatalf("statusFromError(plain) = %d, want 0", got) + } +} + +func TestWriteErrorResponse_ContextCanceledUses499(t *testing.T) { + gin.SetMode(gin.TestMode) + recorder := httptest.NewRecorder() + c, _ := gin.CreateTestContext(recorder) + c.Request = httptest.NewRequest(http.MethodPost, "/v1/chat/completions", nil) + + handler := NewBaseAPIHandlers(nil, nil) + handler.WriteErrorResponse(c, executionErrorMessage(context.Canceled)) + + if recorder.Code != clienterror.StatusClientClosedRequest { + t.Fatalf("status = %d, want %d", recorder.Code, clienterror.StatusClientClosedRequest) + } +} diff --git a/sdk/api/handlers/handlers_errors.go b/sdk/api/handlers/handlers_errors.go index 1dc05913..57df50e0 100644 --- a/sdk/api/handlers/handlers_errors.go +++ b/sdk/api/handlers/handlers_errors.go @@ -8,21 +8,14 @@ import ( "strings" "github.com/gin-gonic/gin" + "github.com/router-for-me/CLIProxyAPI/v7/internal/clienterror" "github.com/router-for-me/CLIProxyAPI/v7/internal/interfaces" coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" "golang.org/x/net/context" ) func statusFromError(err error) int { - if err == nil { - return 0 - } - if se, ok := err.(interface{ StatusCode() int }); ok && se != nil { - if code := se.StatusCode(); code > 0 { - return code - } - } - return 0 + return clienterror.HTTPStatusFromError(err) } func isAuthSelectionUnavailable(err error) bool { diff --git a/sdk/api/handlers/handlers_execution.go b/sdk/api/handlers/handlers_execution.go index b2deb73f..994bd33a 100644 --- a/sdk/api/handlers/handlers_execution.go +++ b/sdk/api/handlers/handlers_execution.go @@ -5,6 +5,7 @@ import ( "fmt" "net/http" + "github.com/router-for-me/CLIProxyAPI/v7/internal/clienterror" "github.com/router-for-me/CLIProxyAPI/v7/internal/interfaces" coreexecutor "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/executor" "github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginapi" @@ -307,10 +308,8 @@ func executionErrorMessage(err error) *interfaces.ErrorMessage { } } status := http.StatusInternalServerError - if se, ok := err.(interface{ StatusCode() int }); ok && se != nil { - if code := se.StatusCode(); code > 0 { - status = code - } + if code := clienterror.HTTPStatusFromError(err); code > 0 { + status = code } var addon http.Header if he, ok := err.(interface{ Headers() http.Header }); ok && he != nil { diff --git a/sdk/api/handlers/openai/openai_images_handlers.go b/sdk/api/handlers/openai/openai_images_handlers.go index 7f65bca1..961a4276 100644 --- a/sdk/api/handlers/openai/openai_images_handlers.go +++ b/sdk/api/handlers/openai/openai_images_handlers.go @@ -15,6 +15,7 @@ import ( "time" "github.com/gin-gonic/gin" + "github.com/router-for-me/CLIProxyAPI/v7/internal/clienterror" internalconfig "github.com/router-for-me/CLIProxyAPI/v7/internal/config" "github.com/router-for-me/CLIProxyAPI/v7/internal/interfaces" "github.com/router-for-me/CLIProxyAPI/v7/internal/registry" @@ -1609,7 +1610,11 @@ func collectImagesFromResponsesStream(ctx context.Context, data <-chan []byte, e for { select { case <-ctx.Done(): - return nil, &interfaces.ErrorMessage{StatusCode: http.StatusRequestTimeout, Error: ctx.Err()} + errCtx := ctx.Err() + return nil, &interfaces.ErrorMessage{ + StatusCode: clienterror.HTTPStatusFromErrorOr(errCtx, http.StatusRequestTimeout), + Error: errCtx, + } case errMsg, ok := <-errs: if ok && errMsg != nil { return nil, errMsg diff --git a/sdk/api/handlers/openai/openai_responses_websocket_forward.go b/sdk/api/handlers/openai/openai_responses_websocket_forward.go index 775f5700..49603edb 100644 --- a/sdk/api/handlers/openai/openai_responses_websocket_forward.go +++ b/sdk/api/handlers/openai/openai_responses_websocket_forward.go @@ -188,13 +188,10 @@ func responsesWebsocketErrorStatus(errMsg *interfaces.ErrorMessage) int { if errMsg == nil { return 0 } - status := errMsg.StatusCode - if status <= 0 && errMsg.Error != nil { - if se, ok := errMsg.Error.(interface{ StatusCode() int }); ok && se != nil { - status = se.StatusCode() - } + if errMsg.StatusCode > 0 { + return errMsg.StatusCode } - return status + return clienterror.HTTPStatusFromError(errMsg.Error) } // shouldExposeResponsesUpstreamError reports whether a terminal upstream error diff --git a/sdk/api/handlers/openai/openai_videos_handlers.go b/sdk/api/handlers/openai/openai_videos_handlers.go index 6f9e1c3a..1748eaa6 100644 --- a/sdk/api/handlers/openai/openai_videos_handlers.go +++ b/sdk/api/handlers/openai/openai_videos_handlers.go @@ -14,6 +14,7 @@ import ( "github.com/gin-gonic/gin" "github.com/google/uuid" + "github.com/router-for-me/CLIProxyAPI/v7/internal/clienterror" "github.com/router-for-me/CLIProxyAPI/v7/internal/config" "github.com/router-for-me/CLIProxyAPI/v7/internal/interfaces" "github.com/router-for-me/CLIProxyAPI/v7/internal/runtime/executor/helps" @@ -891,7 +892,10 @@ func (h *OpenAIAPIHandler) VideosContent(c *gin.Context) { func (h *OpenAIAPIHandler) writeVideoContentFromURL(c *gin.Context, contentURL string) error { req, err := http.NewRequestWithContext(c.Request.Context(), http.MethodGet, contentURL, nil) if err != nil { - errMsg := &interfaces.ErrorMessage{StatusCode: http.StatusBadGateway, Error: err} + errMsg := &interfaces.ErrorMessage{ + StatusCode: clienterror.HTTPStatusFromErrorOr(err, http.StatusBadGateway), + Error: err, + } h.WriteErrorResponse(c, errMsg) return err } @@ -899,7 +903,10 @@ func (h *OpenAIAPIHandler) writeVideoContentFromURL(c *gin.Context, contentURL s httpClient := h.videoContentHTTPClient(c) resp, err := httpClient.Do(req) if err != nil { - errMsg := &interfaces.ErrorMessage{StatusCode: http.StatusBadGateway, Error: err} + errMsg := &interfaces.ErrorMessage{ + StatusCode: clienterror.HTTPStatusFromErrorOr(err, http.StatusBadGateway), + Error: err, + } h.WriteErrorResponse(c, errMsg) return err } diff --git a/sdk/cliproxy/auth/conductor_cooldown.go b/sdk/cliproxy/auth/conductor_cooldown.go index c7c911c4..5c9acdcd 100644 --- a/sdk/cliproxy/auth/conductor_cooldown.go +++ b/sdk/cliproxy/auth/conductor_cooldown.go @@ -1273,7 +1273,8 @@ func isConnectionLifecycleError(err error) bool { if statusCodeFromError(err) != 0 { return false } - if errors.Is(err, context.Canceled) || errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) { + // Client abort and request-scoped timeouts are not credential faults. + if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) || errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) { return true } return isConnectionLifecycleMessage(err.Error()) @@ -1300,7 +1301,7 @@ func isConnectionLifecycleMessage(message string) bool { return false } switch lower { - case "context canceled", "eof", "unexpected eof": + case "context canceled", "context deadline exceeded", "eof", "unexpected eof": return true } // gorilla/websocket CloseError.Error() and common wrappers. diff --git a/sdk/cliproxy/auth/connection_lifecycle_cooldown_test.go b/sdk/cliproxy/auth/connection_lifecycle_cooldown_test.go index 94e53082..d6e77570 100644 --- a/sdk/cliproxy/auth/connection_lifecycle_cooldown_test.go +++ b/sdk/cliproxy/auth/connection_lifecycle_cooldown_test.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "net/http" + "net/url" "testing" "time" @@ -29,9 +30,14 @@ func TestManager_MarkResult_ConnectionLifecycleDoesNotCooldown(t *testing.T) { {name: "websocket 1001", err: &Error{Message: "websocket: close 1001 (going away)"}}, {name: "websocket 1006", err: &Error{Message: "websocket: close 1006 (abnormal closure): unexpected EOF"}}, {name: "context canceled", err: &Error{Message: "context canceled"}}, + {name: "context deadline exceeded", err: &Error{Message: "context deadline exceeded"}}, {name: "unexpected EOF", err: &Error{Message: "unexpected EOF"}}, {name: "plain EOF", err: &Error{Message: "EOF"}}, {name: "wrapped unexpected EOF", err: &Error{Message: "read tcp 127.0.0.1:1->127.0.0.1:2: unexpected EOF"}}, + {name: "typed canceled", err: resultErrorFromError(context.Canceled)}, + {name: "typed deadline", err: resultErrorFromError(context.DeadlineExceeded)}, + {name: "url canceled", err: resultErrorFromError(&url.Error{Op: "Post", URL: "https://example.com", Err: context.Canceled})}, + {name: "url deadline", err: resultErrorFromError(&url.Error{Op: "Post", URL: "https://example.com", Err: context.DeadlineExceeded})}, } for _, tc := range cases { @@ -202,8 +208,11 @@ func TestManager_MarkResult_NonLifecycleStillCooldowns(t *testing.T) { func TestResultErrorFromError_ConnectionLifecycleDoesNotBecomeRequestScoped(t *testing.T) { cases := []error{ context.Canceled, + context.DeadlineExceeded, io.EOF, io.ErrUnexpectedEOF, + &url.Error{Op: "Post", URL: "https://example.com", Err: context.Canceled}, + &url.Error{Op: "Post", URL: "https://example.com", Err: context.DeadlineExceeded}, &websocket.CloseError{Code: websocket.CloseNormalClosure, Text: "normal"}, &websocket.CloseError{Code: websocket.CloseGoingAway, Text: "bye"}, &websocket.CloseError{Code: websocket.CloseAbnormalClosure, Text: "unexpected EOF"}, @@ -211,6 +220,7 @@ func TestResultErrorFromError_ConnectionLifecycleDoesNotBecomeRequestScoped(t *t fmt.Errorf("wrap: %w", io.ErrUnexpectedEOF), errors.New("websocket: close 1000 (normal)"), errors.New("websocket: close 1006 (abnormal closure): unexpected EOF"), + errors.New("context deadline exceeded"), errors.New("unexpected EOF"), } for _, err := range cases { -- knotmirror 0.0.1 From 36936340a33ad17906fd0c12022098278902e04a Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Sat, 8 Aug 2026 05:08:42 +0800 Subject: [PATCH 03/17] fix(kimi): canonicalize K2.7 Code model aliases to official Kimi-For-Coding IDs Closes: #4605 --- internal/runtime/executor/kimi_executor.go | 16 +++++++++++++--- internal/runtime/executor/kimi_executor_test.go | 14 ++++++++++++++ .../executor/kimi_thinking_replay_test.go | 12 +++++++----- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/internal/runtime/executor/kimi_executor.go b/internal/runtime/executor/kimi_executor.go index 6593a94b..e434d193 100644 --- a/internal/runtime/executor/kimi_executor.go +++ b/internal/runtime/executor/kimi_executor.go @@ -807,14 +807,24 @@ func stripKimiPrefix(model string) string { // It strips the CLIProxyAPI "kimi-" prefix and any Claude Code "[1m]" context // suffix while preserving a trailing thinking suffix (e.g. "(1024)"), so that // the upstream API receives IDs such as "k3(1024)" instead of "kimi-k3[1m](1024)". +// K2.7 Code aliases are remapped to the official Kimi Code model IDs before +// generic prefix stripping, so already-canonical IDs stay idempotent. func normalizeKimiUpstreamModel(model string) string { model = strings.TrimSpace(model) parsed := thinking.ParseSuffix(model) - base := parsed.ModelName - if strings.HasSuffix(strings.ToLower(base), "[1m]") { + base := strings.ToLower(strings.TrimSpace(parsed.ModelName)) + if strings.HasSuffix(base, "[1m]") { base = base[:len(base)-len("[1m]")] } - normalized := strings.ToLower(stripKimiPrefix(strings.TrimSpace(base))) + var normalized string + switch base { + case "kimi-k2.7-code", "k2.7-code", "kimi-for-coding", "for-coding": + normalized = "kimi-for-coding" + case "kimi-k2.7-code-highspeed", "k2.7-code-highspeed", "kimi-for-coding-highspeed", "for-coding-highspeed": + normalized = "kimi-for-coding-highspeed" + default: + normalized = stripKimiPrefix(base) + } if parsed.HasSuffix { return normalized + "(" + parsed.RawSuffix + ")" } diff --git a/internal/runtime/executor/kimi_executor_test.go b/internal/runtime/executor/kimi_executor_test.go index c9e79d92..dea518d4 100644 --- a/internal/runtime/executor/kimi_executor_test.go +++ b/internal/runtime/executor/kimi_executor_test.go @@ -528,6 +528,20 @@ func TestNormalizeKimiUpstreamModel(t *testing.T) { {"kimi-k3[1m](1024)", "k3(1024)"}, {"kimi-k2.6(high)", "k2.6(high)"}, {"kimi-k2.6[1m](high)", "k2.6(high)"}, + {"kimi-k2.7-code", "kimi-for-coding"}, + {"kimi-k2.7-code-highspeed", "kimi-for-coding-highspeed"}, + {"Kimi-K2.7-Code", "kimi-for-coding"}, + {"kimi-k2.7-code-highspeed(high)", "kimi-for-coding-highspeed(high)"}, + {"kimi-k2.7-code[1m](high)", "kimi-for-coding(high)"}, + {"k2.7-code", "kimi-for-coding"}, + {"k2.7-code-highspeed", "kimi-for-coding-highspeed"}, + {"kimi-for-coding", "kimi-for-coding"}, + {"kimi-for-coding-highspeed", "kimi-for-coding-highspeed"}, + {"Kimi-For-Coding", "kimi-for-coding"}, + {"kimi-for-coding-highspeed(high)", "kimi-for-coding-highspeed(high)"}, + {"kimi-for-coding[1m]", "kimi-for-coding"}, + {"for-coding", "kimi-for-coding"}, + {"for-coding-highspeed", "kimi-for-coding-highspeed"}, } for _, c := range cases { diff --git a/internal/runtime/executor/kimi_thinking_replay_test.go b/internal/runtime/executor/kimi_thinking_replay_test.go index 380589c1..2301427c 100644 --- a/internal/runtime/executor/kimi_thinking_replay_test.go +++ b/internal/runtime/executor/kimi_thinking_replay_test.go @@ -30,8 +30,10 @@ func TestKimiThinkingReplayModelFamily(t *testing.T) { {model: "kimi-k3", want: "k3"}, {model: "k3-256k", want: "k3"}, {model: "kimi-k3-256k(high)", want: "k3"}, - {model: "kimi-k2.7-code", want: "k2.7-code"}, - {model: "kimi-k2.7-code-highspeed", want: "k2.7-code-highspeed"}, + {model: "kimi-k2.7-code", want: "kimi-for-coding"}, + {model: "kimi-k2.7-code-highspeed", want: "kimi-for-coding-highspeed"}, + {model: "kimi-for-coding", want: "kimi-for-coding"}, + {model: "kimi-for-coding-highspeed(high)", want: "kimi-for-coding-highspeed"}, } for _, tc := range cases { t.Run(tc.model, func(t *testing.T) { @@ -89,7 +91,7 @@ func TestPrepareKimiThinkingReplayRequestSharesOnlyK3Variants(t *testing.T) { if !internalcache.CacheKimiThinkingReplayBestEffort(context.Background(), "k3", "execution:"+sessionID, []byte(cached)) { t.Fatal("failed to seed K3 thinking replay cache") } - if !internalcache.CacheKimiThinkingReplayBestEffort(context.Background(), "k2.7-code", "execution:"+sessionID, []byte(cached)) { + if !internalcache.CacheKimiThinkingReplayBestEffort(context.Background(), "kimi-for-coding", "execution:"+sessionID, []byte(cached)) { t.Fatal("failed to seed K2.7 Code thinking replay cache") } @@ -110,8 +112,8 @@ func TestPrepareKimiThinkingReplayRequestSharesOnlyK3Variants(t *testing.T) { k27Payload := []byte(`{"model":"kimi-k2.7-code-highspeed","messages":[{"role":"assistant","content":[{"type":"tool_use","id":"toolu_1","name":"Read","input":{"path":"README.md"}}]}]}`) preparedK27, scopeK27 := prepareKimiThinkingReplayRequest(context.Background(), cliproxyexecutor.Request{Model: "kimi-k2.7-code-highspeed", Payload: k27Payload}, opts) - if scopeK27.modelFamily != "k2.7-code-highspeed" { - t.Fatalf("K2.7 replay family = %q, want k2.7-code-highspeed", scopeK27.modelFamily) + if scopeK27.modelFamily != "kimi-for-coding-highspeed" { + t.Fatalf("K2.7 replay family = %q, want kimi-for-coding-highspeed", scopeK27.modelFamily) } if gjson.GetBytes(preparedK27.Payload, "messages.0.content.0.signature").Exists() { t.Fatalf("K2.7 variants must remain isolated: %s", preparedK27.Payload) -- knotmirror 0.0.1 From e64cdbf5591dd5dd7c9c86704bdf26e0663501b7 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Sat, 8 Aug 2026 05:36:20 +0800 Subject: [PATCH 04/17] fix(codex): resolve credential-aware model before forwarding API-key alpha search requests Closes: #4714 --- internal/api/server_routes.go | 46 ++++++++- internal/api/server_test.go | 130 ++++++++++++++++++++++++++ sdk/cliproxy/auth/conductor_models.go | 18 ++++ 3 files changed, 192 insertions(+), 2 deletions(-) diff --git a/internal/api/server_routes.go b/internal/api/server_routes.go index e008e01f..19af6260 100644 --- a/internal/api/server_routes.go +++ b/internal/api/server_routes.go @@ -252,6 +252,38 @@ func sanitizeCodexAlphaSearchBody(body []byte) []byte { return sanitizedBody } +// rewriteCodexAlphaSearchModel replaces the top-level model field with the +// credential-resolved upstream model before the request is forwarded. +func rewriteCodexAlphaSearchModel(body []byte, upstreamModel string) []byte { + upstreamModel = strings.TrimSpace(upstreamModel) + if upstreamModel == "" { + return body + } + + var payload map[string]json.RawMessage + if errUnmarshal := json.Unmarshal(body, &payload); errUnmarshal != nil || payload == nil { + return body + } + if _, exists := payload["model"]; !exists { + return body + } + + modelJSON, errMarshalModel := json.Marshal(upstreamModel) + if errMarshalModel != nil { + return body + } + if string(payload["model"]) == string(modelJSON) { + return body + } + + payload["model"] = modelJSON + rewrittenBody, errMarshal := json.Marshal(payload) + if errMarshal != nil { + return body + } + return rewrittenBody +} + func homeSelectionAttemptContext(ctx context.Context, selection *auth.HomeDispatchSelection) (context.Context, func(), error) { if selection == nil { return nil, func() {}, errors.New("Home dispatch selection is nil") @@ -343,12 +375,19 @@ func (s *Server) codexAlphaSearch(c *gin.Context) { } errMissingBaseURL := errors.New("Codex Alpha Search API key base URL unavailable") + routeModel := strings.TrimSpace(selectionModel) + if routeModel == "" { + routeModel = strings.TrimSpace(routing.Model) + } performRequest := func(current *auth.Auth) (*http.Response, error) { headers := baseHeaders.Clone() if accountID, ok := current.Metadata["account_id"].(string); ok && strings.TrimSpace(accountID) != "" { headers.Set("Chatgpt-Account-Id", accountID) } upstreamURL := "https://chatgpt.com/backend-api/codex/alpha/search" + requestBody := upstreamRequestBody + // API-key Alpha Search reuses normal credential-aware model resolution so + // CPA routing prefixes and model aliases are not forwarded upstream. if current.AuthKind() == auth.AuthKindAPIKey { baseURL := "" if current.Attributes != nil { @@ -358,8 +397,11 @@ func (s *Server) codexAlphaSearch(c *gin.Context) { return nil, errMissingBaseURL } upstreamURL = strings.TrimRight(baseURL, "/") + "/alpha/search" + if upstreamModel := s.handlers.AuthManager.ResolveExecutionModel(current, routeModel); upstreamModel != "" { + requestBody = rewriteCodexAlphaSearchModel(upstreamRequestBody, upstreamModel) + } } - req, errRequest := s.handlers.AuthManager.NewHttpRequest(ctx, current, http.MethodPost, upstreamURL, upstreamRequestBody, headers) + req, errRequest := s.handlers.AuthManager.NewHttpRequest(ctx, current, http.MethodPost, upstreamURL, requestBody, headers) if errRequest != nil { return nil, errRequest } @@ -368,7 +410,7 @@ func (s *Server) codexAlphaSearch(c *gin.Context) { URL: upstreamURL, Method: http.MethodPost, Headers: req.Header.Clone(), - Body: upstreamRequestBody, + Body: requestBody, Provider: "codex", AuthID: current.ID, AuthLabel: current.Label, diff --git a/internal/api/server_test.go b/internal/api/server_test.go index 14613d28..e8a885cb 100644 --- a/internal/api/server_test.go +++ b/internal/api/server_test.go @@ -904,6 +904,136 @@ func TestCodexAlphaSearchOptInAPIKeyUsesConfiguredEndpoint(t *testing.T) { } } +func TestCodexAlphaSearchOptInAPIKeyStripsCredentialPrefix(t *testing.T) { + server := newTestServer(t) + executor := &codexSearchCaptureExecutor{} + server.handlers.AuthManager.RegisterExecutor(executor) + credential := &auth.Auth{ + ID: "codex-alpha-api-key-prefix", + Provider: "codex", + Prefix: "vendor", + Status: auth.StatusActive, + Attributes: map[string]string{ + auth.AttributeAPIKey: "codex-alpha-key", + auth.AttributeCodexAlphaSearch: "true", + "base_url": "https://codex.example.com/v1", + }, + } + if _, errRegister := server.handlers.AuthManager.Register(context.Background(), credential); errRegister != nil { + t.Fatalf("register Codex API key: %v", errRegister) + } + registry.GetGlobalRegistry().RegisterClient(credential.ID, credential.Provider, []*registry.ModelInfo{{ID: "vendor/gpt-5.6-sol"}}) + t.Cleanup(func() { + registry.GetGlobalRegistry().UnregisterClient(credential.ID) + }) + + payload := `{"id":"00000000-0000-4000-8000-000000000003","model":"vendor/gpt-5.6-sol","commands":{"search_query":[{"q":"Go programming language official website"}]}}` + req := httptest.NewRequest(http.MethodPost, "/v1/alpha/search", strings.NewReader(payload)) + req.Header.Set("Authorization", "Bearer test-key") + rr := httptest.NewRecorder() + server.engine.ServeHTTP(rr, req) + + if rr.Code != http.StatusOK { + t.Fatalf("status = %d, want %d; body=%s", rr.Code, http.StatusOK, rr.Body.String()) + } + if executor.request == nil { + t.Fatal("Codex executor did not receive a request") + } + var upstreamBody map[string]json.RawMessage + if errUnmarshal := json.Unmarshal(executor.body, &upstreamBody); errUnmarshal != nil { + t.Fatalf("unmarshal upstream body: %v", errUnmarshal) + } + var upstreamModel string + if errUnmarshal := json.Unmarshal(upstreamBody["model"], &upstreamModel); errUnmarshal != nil { + t.Fatalf("unmarshal upstream model: %v", errUnmarshal) + } + if upstreamModel != "gpt-5.6-sol" { + t.Fatalf("upstream model = %q, want gpt-5.6-sol", upstreamModel) + } +} + +func TestCodexAlphaSearchOptInAPIKeyResolvesModelAlias(t *testing.T) { + server := newTestServer(t) + executor := &codexSearchCaptureExecutor{} + server.handlers.AuthManager.RegisterExecutor(executor) + server.handlers.AuthManager.SetConfig(&proxyconfig.Config{ + CodexKey: []proxyconfig.CodexKey{{ + APIKey: "codex-alpha-key", + Prefix: "vendor", + BaseURL: "https://codex.example.com/v1", + AlphaSearch: true, + Models: []proxyconfig.CodexModel{{ + Name: "gpt-5.6-sol", + Alias: "sol-alias", + }}, + }}, + }) + credential := &auth.Auth{ + ID: "codex-alpha-api-key-alias", + Provider: "codex", + Prefix: "vendor", + Status: auth.StatusActive, + Attributes: map[string]string{ + auth.AttributeAPIKey: "codex-alpha-key", + auth.AttributeCodexAlphaSearch: "true", + "base_url": "https://codex.example.com/v1", + }, + } + if _, errRegister := server.handlers.AuthManager.Register(context.Background(), credential); errRegister != nil { + t.Fatalf("register Codex API key: %v", errRegister) + } + registry.GetGlobalRegistry().RegisterClient(credential.ID, credential.Provider, []*registry.ModelInfo{{ID: "vendor/sol-alias"}}) + t.Cleanup(func() { + registry.GetGlobalRegistry().UnregisterClient(credential.ID) + }) + + payload := `{"model":"vendor/sol-alias","commands":{"search_query":[{"q":"golang"}]}}` + req := httptest.NewRequest(http.MethodPost, "/v1/alpha/search", strings.NewReader(payload)) + req.Header.Set("Authorization", "Bearer test-key") + rr := httptest.NewRecorder() + server.engine.ServeHTTP(rr, req) + + if rr.Code != http.StatusOK { + t.Fatalf("status = %d, want %d; body=%s", rr.Code, http.StatusOK, rr.Body.String()) + } + if executor.request == nil { + t.Fatal("Codex executor did not receive a request") + } + var upstreamBody map[string]json.RawMessage + if errUnmarshal := json.Unmarshal(executor.body, &upstreamBody); errUnmarshal != nil { + t.Fatalf("unmarshal upstream body: %v", errUnmarshal) + } + var upstreamModel string + if errUnmarshal := json.Unmarshal(upstreamBody["model"], &upstreamModel); errUnmarshal != nil { + t.Fatalf("unmarshal upstream model: %v", errUnmarshal) + } + if upstreamModel != "gpt-5.6-sol" { + t.Fatalf("upstream model = %q, want gpt-5.6-sol", upstreamModel) + } +} + +func TestRewriteCodexAlphaSearchModel(t *testing.T) { + original := []byte(`{"id":"search-1","model":"vendor/gpt-5.6-sol","commands":{"search_query":[{"q":"golang"}]}}`) + rewritten := rewriteCodexAlphaSearchModel(original, "gpt-5.6-sol") + var payload map[string]json.RawMessage + if errUnmarshal := json.Unmarshal(rewritten, &payload); errUnmarshal != nil { + t.Fatalf("unmarshal rewritten body: %v", errUnmarshal) + } + var model string + if errUnmarshal := json.Unmarshal(payload["model"], &model); errUnmarshal != nil { + t.Fatalf("unmarshal rewritten model: %v", errUnmarshal) + } + if model != "gpt-5.6-sol" { + t.Fatalf("model = %q, want gpt-5.6-sol", model) + } + if _, exists := payload["commands"]; !exists { + t.Fatal("commands field was dropped") + } + if string(rewriteCodexAlphaSearchModel([]byte(`{"query":"x"}`), "gpt-5.6-sol")) != `{"query":"x"}` { + t.Fatal("body without model should remain unchanged") + } +} + func TestCodexAlphaSearchOptInAPIKeyWithoutBaseURLFailsClosed(t *testing.T) { server := newTestServer(t) executor := &codexSearchCaptureExecutor{} diff --git a/sdk/cliproxy/auth/conductor_models.go b/sdk/cliproxy/auth/conductor_models.go index 69c58111..50788157 100644 --- a/sdk/cliproxy/auth/conductor_models.go +++ b/sdk/cliproxy/auth/conductor_models.go @@ -182,6 +182,24 @@ func (m *Manager) executionModelCandidates(auth *Auth, routeModel string) []stri return []string{resolved} } +// ResolveExecutionModel returns the credential-aware upstream model used by +// normal execution. It strips auth prefixes, applies configured aliases, and +// prefers Home-dispatched upstream models when present. +func (m *Manager) ResolveExecutionModel(auth *Auth, routeModel string) string { + routeModel = strings.TrimSpace(routeModel) + if m == nil { + return routeModel + } + candidates := m.executionModelCandidates(auth, routeModel) + if len(candidates) == 0 { + return routeModel + } + if resolved := strings.TrimSpace(candidates[0]); resolved != "" { + return resolved + } + return routeModel +} + func (m *Manager) selectionModelForAuth(auth *Auth, routeModel string) string { requestedModel := rewriteModelForAuth(routeModel, auth) if strings.TrimSpace(requestedModel) == "" { -- knotmirror 0.0.1 From 01a21b77f4dc126b92809e746cfd4ed251e69e13 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Sat, 8 Aug 2026 06:08:23 +0800 Subject: [PATCH 05/17] fix(cliproxy): delegate OpenAI-compatible OAuth refresh to plugin auth providers - Added a plugin refresh-compat executor wrapper that forwards normal OpenAI-compat execution paths while routing `Refresh` to plugin `AuthProvider`/Home refresh logic. - Updated refresh lookup to use the effective executor key from auth metadata so namespaced compatibility providers can resolve their refresh executors correctly. - Changed OpenAI-compat registration to wrap built-in executors with the plugin-refresh wrapper when a matching plugin auth provider exists, while preserving bare executors otherwise. - Made `OpenAICompatExecutor.Refresh` fail fast for OAuth-style credentials (with refresh tokens) instead of silently returning unchanged auth. Closes: #4719 --- .../plugin_refresh_compat_executor.go | 154 +++++++++++++++ .../plugin_refresh_compat_executor_test.go | 176 ++++++++++++++++++ .../executor/openai_compat_executor.go | 25 +++ sdk/cliproxy/auth/conductor_refresh.go | 4 +- .../conductor_refresh_executor_key_test.go | 77 ++++++++ sdk/cliproxy/service_executors.go | 123 ++++++++++-- .../service_plugin_refresh_executor_test.go | 164 ++++++++++++++++ 7 files changed, 708 insertions(+), 15 deletions(-) create mode 100644 internal/pluginhost/plugin_refresh_compat_executor.go create mode 100644 internal/pluginhost/plugin_refresh_compat_executor_test.go create mode 100644 sdk/cliproxy/auth/conductor_refresh_executor_key_test.go create mode 100644 sdk/cliproxy/service_plugin_refresh_executor_test.go diff --git a/internal/pluginhost/plugin_refresh_compat_executor.go b/internal/pluginhost/plugin_refresh_compat_executor.go new file mode 100644 index 00000000..b5296c68 --- /dev/null +++ b/internal/pluginhost/plugin_refresh_compat_executor.go @@ -0,0 +1,154 @@ +package pluginhost + +import ( + "context" + "fmt" + "net/http" + "strings" + + "github.com/router-for-me/CLIProxyAPI/v7/internal/config" + "github.com/router-for-me/CLIProxyAPI/v7/internal/runtime/executor/helps" + coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" + cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/executor" +) + +// pluginRefreshCompatExecutor keeps native OpenAI-compat inference while +// routing credential refresh to a plugin AuthProvider. +// +// Plugins often set Attributes["base_url"] so host routing uses the built-in +// OpenAI-compat executor. That binding previously swallowed refresh because +// OpenAICompatExecutor.Refresh is a no-op for non-Home providers. This wrapper +// preserves native Execute* paths and delegates Refresh to Host.RefreshAuth. +type pluginRefreshCompatExecutor struct { + inner coreauth.ProviderExecutor + host *Host + cfg *config.Config + provider string +} + +// NewPluginRefreshCompatExecutor wraps a native provider executor so Refresh is +// handled by the plugin AuthProvider for the same provider key. +func NewPluginRefreshCompatExecutor(inner coreauth.ProviderExecutor, host *Host, cfg *config.Config) coreauth.ProviderExecutor { + if inner == nil { + return nil + } + provider := strings.ToLower(strings.TrimSpace(inner.Identifier())) + return &pluginRefreshCompatExecutor{ + inner: inner, + host: host, + cfg: cfg, + provider: provider, + } +} + +// IsPluginRefreshCompatExecutor reports whether executor is a plugin-refresh wrapper. +func IsPluginRefreshCompatExecutor(executor coreauth.ProviderExecutor) bool { + _, ok := executor.(*pluginRefreshCompatExecutor) + return ok +} + +// UnwrapPluginRefreshCompatExecutor returns the inner native executor when executor +// is a plugin-refresh wrapper. +func UnwrapPluginRefreshCompatExecutor(executor coreauth.ProviderExecutor) (coreauth.ProviderExecutor, bool) { + wrapper, ok := executor.(*pluginRefreshCompatExecutor) + if !ok || wrapper == nil || wrapper.inner == nil { + return nil, false + } + return wrapper.inner, true +} + +func (e *pluginRefreshCompatExecutor) Identifier() string { + if e == nil { + return "" + } + if e.provider != "" { + return e.provider + } + if e.inner != nil { + return e.inner.Identifier() + } + return "" +} + +func (e *pluginRefreshCompatExecutor) Execute(ctx context.Context, auth *coreauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { + if e == nil || e.inner == nil { + return cliproxyexecutor.Response{}, fmt.Errorf("plugin refresh compat executor is unavailable") + } + return e.inner.Execute(ctx, auth, req, opts) +} + +func (e *pluginRefreshCompatExecutor) ExecuteStream(ctx context.Context, auth *coreauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (*cliproxyexecutor.StreamResult, error) { + if e == nil || e.inner == nil { + return nil, fmt.Errorf("plugin refresh compat executor is unavailable") + } + return e.inner.ExecuteStream(ctx, auth, req, opts) +} + +func (e *pluginRefreshCompatExecutor) CountTokens(ctx context.Context, auth *coreauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { + if e == nil || e.inner == nil { + return cliproxyexecutor.Response{}, fmt.Errorf("plugin refresh compat executor is unavailable") + } + return e.inner.CountTokens(ctx, auth, req, opts) +} + +func (e *pluginRefreshCompatExecutor) HttpRequest(ctx context.Context, auth *coreauth.Auth, req *http.Request) (*http.Response, error) { + if e == nil || e.inner == nil { + return nil, fmt.Errorf("plugin refresh compat executor is unavailable") + } + return e.inner.HttpRequest(ctx, auth, req) +} + +// PrepareRequest forwards credential injection to the inner executor when supported. +func (e *pluginRefreshCompatExecutor) PrepareRequest(req *http.Request, auth *coreauth.Auth) error { + if e == nil || e.inner == nil { + return fmt.Errorf("plugin refresh compat executor is unavailable") + } + preparer, ok := e.inner.(interface { + PrepareRequest(*http.Request, *coreauth.Auth) error + }) + if !ok || preparer == nil { + return nil + } + return preparer.PrepareRequest(req, auth) +} + +func (e *pluginRefreshCompatExecutor) Refresh(ctx context.Context, auth *coreauth.Auth) (*coreauth.Auth, error) { + if e == nil { + return nil, fmt.Errorf("plugin refresh compat executor is unavailable") + } + if ctx == nil { + ctx = context.Background() + } + if refreshed, handled, errHome := helps.RefreshAuthViaHome(ctx, e.cfg, auth); handled { + return refreshed, errHome + } + if e.host != nil { + if refreshed, handled, errRefresh := e.host.RefreshAuth(ctx, auth); handled { + return refreshed, errRefresh + } + } + if authHasRefreshToken(auth) { + provider := e.Identifier() + if provider == "" && auth != nil { + provider = strings.TrimSpace(auth.Provider) + } + return nil, fmt.Errorf("plugin auth provider refresh is unavailable for provider %s", provider) + } + if auth == nil { + return nil, nil + } + return auth.Clone(), nil +} + +func authHasRefreshToken(auth *coreauth.Auth) bool { + if auth == nil || auth.Metadata == nil { + return false + } + if token, _ := auth.Metadata["refresh_token"].(string); strings.TrimSpace(token) != "" { + return true + } + if token, _ := auth.Metadata["refreshToken"].(string); strings.TrimSpace(token) != "" { + return true + } + return false +} diff --git a/internal/pluginhost/plugin_refresh_compat_executor_test.go b/internal/pluginhost/plugin_refresh_compat_executor_test.go new file mode 100644 index 00000000..2e6fe3b9 --- /dev/null +++ b/internal/pluginhost/plugin_refresh_compat_executor_test.go @@ -0,0 +1,176 @@ +package pluginhost + +import ( + "context" + "net/http" + "strings" + "testing" + + "github.com/router-for-me/CLIProxyAPI/v7/internal/config" + coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" + cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/executor" + "github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginapi" +) + +type stubCompatExecutor struct { + id string + executeCalls int + refreshCalls int +} + +func (e *stubCompatExecutor) Identifier() string { return e.id } + +func (e *stubCompatExecutor) Execute(context.Context, *coreauth.Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { + e.executeCalls++ + return cliproxyexecutor.Response{Payload: []byte(`{"ok":true}`)}, nil +} + +func (e *stubCompatExecutor) ExecuteStream(context.Context, *coreauth.Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (*cliproxyexecutor.StreamResult, error) { + return &cliproxyexecutor.StreamResult{}, nil +} + +func (e *stubCompatExecutor) Refresh(_ context.Context, auth *coreauth.Auth) (*coreauth.Auth, error) { + e.refreshCalls++ + return auth, nil +} + +func (e *stubCompatExecutor) CountTokens(context.Context, *coreauth.Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { + return cliproxyexecutor.Response{}, nil +} + +func (e *stubCompatExecutor) HttpRequest(context.Context, *coreauth.Auth, *http.Request) (*http.Response, error) { + return nil, nil +} + +func (e *stubCompatExecutor) PrepareRequest(*http.Request, *coreauth.Auth) error { + return nil +} + +func TestPluginRefreshCompatExecutorDelegatesExecuteAndRefresh(t *testing.T) { + refreshCalls := 0 + host := newHostWithRecords(capabilityRecord{ + id: "auth-plugin", + plugin: pluginapi.Plugin{ + Capabilities: pluginapi.Capabilities{ + AuthProvider: fakeAuthProvider{ + identifier: "plugin-provider", + refreshAuth: func(ctx context.Context, req pluginapi.AuthRefreshRequest) (pluginapi.AuthRefreshResponse, error) { + refreshCalls++ + if req.AuthID != "auth-1" || req.AuthProvider != "plugin-provider" { + t.Fatalf("RefreshAuth request = %#v", req) + } + return pluginapi.AuthRefreshResponse{ + Auth: pluginapi.AuthData{ + ID: "auth-1", + Provider: "plugin-provider", + Metadata: map[string]any{ + "access_token": "new-token", + "refresh_token": "refresh-1", + }, + Attributes: map[string]string{ + "base_url": "https://compat.example.com/v1", + }, + }, + }, nil + }, + }, + }, + }, + }) + + inner := &stubCompatExecutor{id: "plugin-provider"} + wrapped := NewPluginRefreshCompatExecutor(inner, host, &config.Config{}) + if wrapped == nil { + t.Fatal("NewPluginRefreshCompatExecutor() = nil") + } + if !IsPluginRefreshCompatExecutor(wrapped) { + t.Fatal("IsPluginRefreshCompatExecutor() = false, want true") + } + if got, ok := UnwrapPluginRefreshCompatExecutor(wrapped); !ok || got != inner { + t.Fatalf("UnwrapPluginRefreshCompatExecutor() = (%T, %v), want inner", got, ok) + } + if wrapped.Identifier() != "plugin-provider" { + t.Fatalf("Identifier() = %q, want plugin-provider", wrapped.Identifier()) + } + + auth := &coreauth.Auth{ + ID: "auth-1", + Provider: "plugin-provider", + Metadata: map[string]any{ + "access_token": "old-token", + "refresh_token": "refresh-1", + }, + Attributes: map[string]string{ + "base_url": "https://compat.example.com/v1", + }, + } + + if _, errExecute := wrapped.Execute(context.Background(), auth, cliproxyexecutor.Request{}, cliproxyexecutor.Options{}); errExecute != nil { + t.Fatalf("Execute() error = %v", errExecute) + } + if inner.executeCalls != 1 { + t.Fatalf("inner Execute calls = %d, want 1", inner.executeCalls) + } + + refreshed, errRefresh := wrapped.Refresh(context.Background(), auth) + if errRefresh != nil { + t.Fatalf("Refresh() error = %v", errRefresh) + } + if refreshCalls != 1 { + t.Fatalf("plugin RefreshAuth calls = %d, want 1", refreshCalls) + } + if inner.refreshCalls != 0 { + t.Fatalf("inner Refresh calls = %d, want 0", inner.refreshCalls) + } + if refreshed == nil || refreshed.Metadata["access_token"] != "new-token" { + t.Fatalf("Refresh() auth = %#v, want updated access_token", refreshed) + } + if refreshed.Attributes["base_url"] != "https://compat.example.com/v1" { + t.Fatalf("Refresh() base_url = %q, want preserved", refreshed.Attributes["base_url"]) + } +} + +func TestPluginRefreshCompatExecutorErrorsWhenRefreshUnavailable(t *testing.T) { + inner := &stubCompatExecutor{id: "plugin-provider"} + wrapped := NewPluginRefreshCompatExecutor(inner, New(), &config.Config{}) + auth := &coreauth.Auth{ + ID: "auth-1", + Provider: "plugin-provider", + Metadata: map[string]any{ + "access_token": "old-token", + "refresh_token": "refresh-1", + }, + } + + _, errRefresh := wrapped.Refresh(context.Background(), auth) + if errRefresh == nil { + t.Fatal("Refresh() error = nil, want unavailable plugin refresh error") + } + if !strings.Contains(errRefresh.Error(), "plugin auth provider refresh is unavailable") { + t.Fatalf("Refresh() error = %v, want unavailable message", errRefresh) + } + if inner.refreshCalls != 0 { + t.Fatalf("inner Refresh calls = %d, want 0", inner.refreshCalls) + } +} + +func TestPluginRefreshCompatExecutorNoOpForAPIKeyAuth(t *testing.T) { + inner := &stubCompatExecutor{id: "plugin-provider"} + wrapped := NewPluginRefreshCompatExecutor(inner, New(), &config.Config{}) + auth := &coreauth.Auth{ + ID: "auth-1", + Provider: "plugin-provider", + Attributes: map[string]string{ + "api_key": "sk-test", + "base_url": "https://compat.example.com/v1", + }, + } + + refreshed, errRefresh := wrapped.Refresh(context.Background(), auth) + if errRefresh != nil { + t.Fatalf("Refresh() error = %v", errRefresh) + } + if refreshed == nil || refreshed.Attributes["api_key"] != "sk-test" { + t.Fatalf("Refresh() auth = %#v, want unchanged api key auth", refreshed) + } +} diff --git a/internal/runtime/executor/openai_compat_executor.go b/internal/runtime/executor/openai_compat_executor.go index f425eeac..1b6a038a 100644 --- a/internal/runtime/executor/openai_compat_executor.go +++ b/internal/runtime/executor/openai_compat_executor.go @@ -643,14 +643,39 @@ func (e *OpenAICompatExecutor) CountTokens(ctx context.Context, auth *cliproxyau } // Refresh is a no-op for API-key based compatibility providers. +// OAuth-style credentials with a refresh token cannot be rotated here; callers +// that need plugin/Home refresh must bind a refresh-capable executor instead. func (e *OpenAICompatExecutor) Refresh(ctx context.Context, auth *cliproxyauth.Auth) (*cliproxyauth.Auth, error) { log.Debugf("openai compat executor: refresh called") if refreshed, handled, err := helps.RefreshAuthViaHome(ctx, e.cfg, auth); handled { return refreshed, err } + if openAICompatAuthHasRefreshToken(auth) { + provider := "" + if e != nil { + provider = e.Identifier() + } + if provider == "" && auth != nil { + provider = strings.TrimSpace(auth.Provider) + } + return nil, fmt.Errorf("openai compat executor cannot refresh oauth credentials for provider %s", provider) + } return auth, nil } +func openAICompatAuthHasRefreshToken(auth *cliproxyauth.Auth) bool { + if auth == nil || auth.Metadata == nil { + return false + } + if token, _ := auth.Metadata["refresh_token"].(string); strings.TrimSpace(token) != "" { + return true + } + if token, _ := auth.Metadata["refreshToken"].(string); strings.TrimSpace(token) != "" { + return true + } + return false +} + func openAICompatImageEndpointPath(opts cliproxyexecutor.Options) string { if opts.SourceFormat.String() != openAICompatImageHandlerType { return "" diff --git a/sdk/cliproxy/auth/conductor_refresh.go b/sdk/cliproxy/auth/conductor_refresh.go index 045e88fd..06a2a27a 100644 --- a/sdk/cliproxy/auth/conductor_refresh.go +++ b/sdk/cliproxy/auth/conductor_refresh.go @@ -512,7 +512,9 @@ func (m *Manager) refreshAuthForRequest(ctx context.Context, id, failedAccessTok auth := m.auths[id] var exec ProviderExecutor if auth != nil { - exec = m.executors[auth.Provider] + // Use the same effective provider key as request execution so OpenAI-compat + // auths registered under namespaced keys still resolve for refresh. + exec = m.executors[executorKeyFromAuth(auth)] } m.mu.RUnlock() if auth == nil || exec == nil { diff --git a/sdk/cliproxy/auth/conductor_refresh_executor_key_test.go b/sdk/cliproxy/auth/conductor_refresh_executor_key_test.go new file mode 100644 index 00000000..e3239901 --- /dev/null +++ b/sdk/cliproxy/auth/conductor_refresh_executor_key_test.go @@ -0,0 +1,77 @@ +package auth + +import ( + "context" + "net/http" + "sync/atomic" + "testing" + + cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/executor" +) + +type countingRefreshExecutor struct { + id string + refreshCalls atomic.Int32 +} + +func (e *countingRefreshExecutor) Identifier() string { return e.id } + +func (e *countingRefreshExecutor) Execute(context.Context, *Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { + return cliproxyexecutor.Response{}, nil +} + +func (e *countingRefreshExecutor) ExecuteStream(context.Context, *Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (*cliproxyexecutor.StreamResult, error) { + return nil, nil +} + +func (e *countingRefreshExecutor) Refresh(_ context.Context, auth *Auth) (*Auth, error) { + e.refreshCalls.Add(1) + if auth.Metadata == nil { + auth.Metadata = make(map[string]any) + } + auth.Metadata["access_token"] = "refreshed-token" + return auth, nil +} + +func (e *countingRefreshExecutor) CountTokens(context.Context, *Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { + return cliproxyexecutor.Response{}, nil +} + +func (e *countingRefreshExecutor) HttpRequest(context.Context, *Auth, *http.Request) (*http.Response, error) { + return nil, nil +} + +func TestRefreshAuthForRequest_UsesExecutorKeyFromAuth(t *testing.T) { + ctx := context.Background() + manager := NewManager(nil, &RoundRobinSelector{}, nil) + executor := &countingRefreshExecutor{id: "openai-compatible-custom"} + manager.RegisterExecutor(executor) + + auth := &Auth{ + ID: "compat-oauth", + Provider: "plugin-provider", + Attributes: map[string]string{ + "compat_name": "custom", + "provider_key": "custom", + "base_url": "https://compat.example.com/v1", + }, + Metadata: map[string]any{ + "access_token": "old-token", + "refresh_token": "refresh-1", + }, + } + if _, errRegister := manager.Register(ctx, auth); errRegister != nil { + t.Fatalf("register auth: %v", errRegister) + } + + refreshed, errRefresh := manager.refreshAuthForRequest(ctx, auth.ID, "old-token") + if errRefresh != nil { + t.Fatalf("refreshAuthForRequest() error = %v", errRefresh) + } + if executor.refreshCalls.Load() != 1 { + t.Fatalf("refresh calls = %d, want 1", executor.refreshCalls.Load()) + } + if refreshed == nil || refreshed.Metadata["access_token"] != "refreshed-token" { + t.Fatalf("refreshed auth = %#v, want updated access_token", refreshed) + } +} diff --git a/sdk/cliproxy/service_executors.go b/sdk/cliproxy/service_executors.go index 371ed6a9..0213ee4f 100644 --- a/sdk/cliproxy/service_executors.go +++ b/sdk/cliproxy/service_executors.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/router-for-me/CLIProxyAPI/v7/internal/constant" + "github.com/router-for-me/CLIProxyAPI/v7/internal/pluginhost" "github.com/router-for-me/CLIProxyAPI/v7/internal/runtime/executor" "github.com/router-for-me/CLIProxyAPI/v7/internal/util" coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" @@ -17,6 +18,11 @@ type openAICompatibilityRegistrationCache struct { byIndex map[int]*openAICompatibilityRegistrationEntry } +// pluginHostHasAuthProvider is overridable in tests to avoid loading real plugins. +var pluginHostHasAuthProvider = func(host *pluginhost.Host, provider string) bool { + return host != nil && host.HasAuthProvider(provider) +} + type openAICompatibilityRegistrationEntry struct { providerKey string models []*ModelInfo @@ -140,10 +146,13 @@ func (s *Service) unregisterOpenAICompatExecutor(providerKey string) { if !okExecutor || existing == nil { return } - if _, okOpenAICompat := existing.(*executor.OpenAICompatExecutor); !okOpenAICompat { + if _, okOpenAICompat := existing.(*executor.OpenAICompatExecutor); okOpenAICompat { + s.coreManager.UnregisterExecutor(providerKey) return } - s.coreManager.UnregisterExecutor(providerKey) + if pluginhost.IsPluginRefreshCompatExecutor(existing) { + s.coreManager.UnregisterExecutor(providerKey) + } } func (s *Service) ensureExecutorsForAuth(a *coreauth.Auth) { @@ -262,14 +271,7 @@ func (s *Service) registerExecutorForAuth(a *coreauth.Auth, forceReplace bool) { if compatProviderKey == "" { compatProviderKey = "openai-compatibility" } - if !forceReplace { - if existingExecutor, hasExecutor := s.coreManager.Executor(compatProviderKey); hasExecutor { - if _, isOpenAICompatExecutor := existingExecutor.(*executor.OpenAICompatExecutor); isOpenAICompatExecutor { - return - } - } - } - s.coreManager.RegisterExecutor(executor.NewOpenAICompatExecutor(compatProviderKey, cfg)) + s.registerOpenAICompatProviderExecutor(compatProviderKey, a, cfg, forceReplace, false) return } switch strings.ToLower(a.Provider) { @@ -312,14 +314,107 @@ func (s *Service) registerExecutorForAuth(a *coreauth.Auth, forceReplace bool) { s.unregisterOpenAICompatExecutor(providerKey) return } - if !forceReplace { - if existingExecutor, hasExecutor := s.coreManager.Executor(providerKey); hasExecutor && - (s.pluginHost == nil || !s.pluginHost.OwnsExecutor(existingExecutor)) { + // Keep native OpenAI-compat inference for base_url routing, but delegate + // OAuth refresh to the plugin AuthProvider when one is registered. + s.registerOpenAICompatProviderExecutor(providerKey, a, cfg, forceReplace, true) + } +} + +// registerOpenAICompatProviderExecutor binds a native OpenAI-compat executor, optionally +// wrapping it so plugin AuthProvider refresh remains available. +// When respectNonOwned is true, an existing non-owned executor is preserved unless it is a +// bare OpenAI-compat executor that should be upgraded to the plugin-refresh wrapper. +func (s *Service) registerOpenAICompatProviderExecutor(providerKey string, auth *coreauth.Auth, cfg *config.Config, forceReplace bool, respectNonOwned bool) { + if s == nil || s.coreManager == nil { + return + } + providerKey = strings.ToLower(strings.TrimSpace(providerKey)) + if providerKey == "" { + providerKey = "openai-compatibility" + } + compatExecutor := executor.NewOpenAICompatExecutor(providerKey, cfg) + nextExecutor := s.wrapOpenAICompatIfPluginAuth(compatExecutor, auth, cfg) + if !forceReplace { + if existingExecutor, hasExecutor := s.coreManager.Executor(providerKey); hasExecutor { + if shouldKeepExistingOpenAICompatExecutor(s, existingExecutor, nextExecutor, respectNonOwned) { + return + } + } + } + s.coreManager.RegisterExecutor(nextExecutor) +} + +func (s *Service) wrapOpenAICompatIfPluginAuth(compatExecutor *executor.OpenAICompatExecutor, auth *coreauth.Auth, cfg *config.Config) coreauth.ProviderExecutor { + if compatExecutor == nil { + return nil + } + for _, candidate := range pluginAuthProviderLookupKeys(auth, compatExecutor.Identifier()) { + if pluginHostHasAuthProvider(s.pluginHost, candidate) { + return pluginhost.NewPluginRefreshCompatExecutor(compatExecutor, s.pluginHost, cfg) + } + } + return compatExecutor +} + +func pluginAuthProviderLookupKeys(auth *coreauth.Auth, fallback string) []string { + keys := make([]string, 0, 4) + add := func(value string) { + value = strings.ToLower(strings.TrimSpace(value)) + if value == "" { + return + } + for _, existing := range keys { + if existing == value { return } } - s.coreManager.RegisterExecutor(executor.NewOpenAICompatExecutor(providerKey, cfg)) + keys = append(keys, value) + } + if auth != nil { + add(auth.Provider) + if auth.Attributes != nil { + add(auth.Attributes["provider_key"]) + add(auth.Attributes["compat_name"]) + } + } + add(fallback) + return keys +} + +func shouldKeepExistingOpenAICompatExecutor(s *Service, existing, next coreauth.ProviderExecutor, respectNonOwned bool) bool { + if existing == nil || next == nil { + return false + } + if shouldUpgradeOpenAICompatToPluginRefresh(existing, next) { + return false + } + if pluginhost.IsPluginRefreshCompatExecutor(existing) && pluginhost.IsPluginRefreshCompatExecutor(next) { + return true + } + _, existingBare := existing.(*executor.OpenAICompatExecutor) + _, nextBare := next.(*executor.OpenAICompatExecutor) + if existingBare && nextBare { + return true + } + if !respectNonOwned { + // Historical openai-compatibility path only short-circuits bare native executors. + return existingBare + } + if s != nil && s.pluginHost != nil && s.pluginHost.OwnsExecutor(existing) { + return false + } + return true +} + +func shouldUpgradeOpenAICompatToPluginRefresh(existing, next coreauth.ProviderExecutor) bool { + if existing == nil || next == nil { + return false + } + if !pluginhost.IsPluginRefreshCompatExecutor(next) { + return false } + _, bareOpenAICompat := existing.(*executor.OpenAICompatExecutor) + return bareOpenAICompat } func (s *Service) registerResolvedModelsForAuth(a *coreauth.Auth, providerKey string, models []*ModelInfo) { diff --git a/sdk/cliproxy/service_plugin_refresh_executor_test.go b/sdk/cliproxy/service_plugin_refresh_executor_test.go new file mode 100644 index 00000000..33c47858 --- /dev/null +++ b/sdk/cliproxy/service_plugin_refresh_executor_test.go @@ -0,0 +1,164 @@ +package cliproxy + +import ( + "testing" + + "github.com/router-for-me/CLIProxyAPI/v7/internal/pluginhost" + runtimeexecutor "github.com/router-for-me/CLIProxyAPI/v7/internal/runtime/executor" + coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" + "github.com/router-for-me/CLIProxyAPI/v7/sdk/config" +) + +func TestRegisterExecutorForAuth_PluginAuthProviderWrapsOpenAICompatRefresh(t *testing.T) { + oldHasAuthProvider := pluginHostHasAuthProvider + pluginHostHasAuthProvider = func(host *pluginhost.Host, provider string) bool { + return host != nil && provider == "plugin-provider" + } + t.Cleanup(func() { + pluginHostHasAuthProvider = oldHasAuthProvider + }) + + service := &Service{ + cfg: &config.Config{}, + coreManager: coreauth.NewManager(nil, nil, nil), + pluginHost: pluginhost.New(), + } + + auth := &coreauth.Auth{ + ID: "plugin-auth-1", + Provider: "plugin-provider", + Attributes: map[string]string{ + "base_url": "https://compat.example.com/v1", + "api_key": "expired-token", + }, + Metadata: map[string]any{ + "access_token": "expired-token", + "refresh_token": "refresh-1", + }, + } + + service.registerExecutorForAuth(auth, true) + + resolved, ok := service.coreManager.Executor("plugin-provider") + if !ok || resolved == nil { + t.Fatal("expected executor for plugin-provider") + } + if !pluginhost.IsPluginRefreshCompatExecutor(resolved) { + t.Fatalf("executor type = %T, want plugin refresh compat wrapper", resolved) + } + inner, okInner := pluginhost.UnwrapPluginRefreshCompatExecutor(resolved) + if !okInner { + t.Fatal("expected unwrap of plugin refresh compat executor") + } + if _, okOpenAICompat := inner.(*runtimeexecutor.OpenAICompatExecutor); !okOpenAICompat { + t.Fatalf("inner executor type = %T, want *executor.OpenAICompatExecutor", inner) + } + + // Upgrading from bare OpenAICompat without forceReplace should still wrap. + service.coreManager.RegisterExecutor(runtimeexecutor.NewOpenAICompatExecutor("plugin-provider", service.cfg)) + service.registerExecutorForAuth(auth, false) + resolved, ok = service.coreManager.Executor("plugin-provider") + if !ok || !pluginhost.IsPluginRefreshCompatExecutor(resolved) { + t.Fatalf("upgrade path executor type = %T, want plugin refresh compat wrapper", resolved) + } +} + +func TestRegisterExecutorForAuth_OpenAICompatWithoutPluginAuthProviderStaysBare(t *testing.T) { + service := &Service{ + cfg: &config.Config{}, + coreManager: coreauth.NewManager(nil, nil, nil), + pluginHost: pluginhost.New(), + } + auth := &coreauth.Auth{ + ID: "compat-auth-1", + Provider: "custom-compat", + Attributes: map[string]string{ + "base_url": "https://compat.example.com/v1", + "api_key": "sk-test", + }, + } + + service.registerExecutorForAuth(auth, true) + + resolved, ok := service.coreManager.Executor("custom-compat") + if !ok || resolved == nil { + t.Fatal("expected executor for custom-compat") + } + if pluginhost.IsPluginRefreshCompatExecutor(resolved) { + t.Fatal("did not expect plugin refresh wrapper without AuthProvider") + } + if _, okOpenAICompat := resolved.(*runtimeexecutor.OpenAICompatExecutor); !okOpenAICompat { + t.Fatalf("executor type = %T, want *executor.OpenAICompatExecutor", resolved) + } +} + +func TestRegisterExecutorForAuth_OpenAICompatInfoPathAlsoWrapsPluginRefresh(t *testing.T) { + oldHasAuthProvider := pluginHostHasAuthProvider + pluginHostHasAuthProvider = func(host *pluginhost.Host, provider string) bool { + return host != nil && provider == "plugin-provider" + } + t.Cleanup(func() { + pluginHostHasAuthProvider = oldHasAuthProvider + }) + + service := &Service{ + cfg: &config.Config{}, + coreManager: coreauth.NewManager(nil, nil, nil), + pluginHost: pluginhost.New(), + } + auth := &coreauth.Auth{ + ID: "plugin-auth-compat", + Provider: "plugin-provider", + Attributes: map[string]string{ + "base_url": "https://compat.example.com/v1", + "compat_name": "custom", + "provider_key": "custom", + }, + Metadata: map[string]any{ + "access_token": "expired-token", + "refresh_token": "refresh-1", + }, + } + + service.registerExecutorForAuth(auth, true) + + resolved, ok := service.coreManager.Executor("openai-compatible-custom") + if !ok || resolved == nil { + t.Fatal("expected executor for openai-compatible-custom") + } + if !pluginhost.IsPluginRefreshCompatExecutor(resolved) { + t.Fatalf("executor type = %T, want plugin refresh compat wrapper", resolved) + } +} + +func TestUnregisterOpenAICompatExecutorRemovesPluginRefreshWrapper(t *testing.T) { + oldHasAuthProvider := pluginHostHasAuthProvider + pluginHostHasAuthProvider = func(host *pluginhost.Host, provider string) bool { + return host != nil && provider == "plugin-provider" + } + t.Cleanup(func() { + pluginHostHasAuthProvider = oldHasAuthProvider + }) + + service := &Service{ + cfg: &config.Config{}, + coreManager: coreauth.NewManager(nil, nil, nil), + pluginHost: pluginhost.New(), + } + auth := &coreauth.Auth{ + ID: "plugin-auth-1", + Provider: "plugin-provider", + Attributes: map[string]string{ + "base_url": "https://compat.example.com/v1", + }, + } + service.registerExecutorForAuth(auth, true) + if _, ok := service.coreManager.Executor("plugin-provider"); !ok { + t.Fatal("expected wrapper before unregister") + } + + service.unregisterOpenAICompatExecutor("plugin-provider") + if _, ok := service.coreManager.Executor("plugin-provider"); ok { + t.Fatal("expected plugin-provider executor to be removed") + } +} -- knotmirror 0.0.1 From 9829bd9d3e51b6365ecfce923454fb69f73881d9 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Sat, 8 Aug 2026 06:32:09 +0800 Subject: [PATCH 06/17] fix(cliproxy): stop non-streaming keep-alive after OpenAI handler execution Closes: #4782 --- sdk/api/handlers/openai/openai_handlers.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sdk/api/handlers/openai/openai_handlers.go b/sdk/api/handlers/openai/openai_handlers.go index cdb3c6c2..84b32d57 100644 --- a/sdk/api/handlers/openai/openai_handlers.go +++ b/sdk/api/handlers/openai/openai_handlers.go @@ -436,7 +436,9 @@ func (h *OpenAIAPIHandler) handleNonStreamingResponse(c *gin.Context, rawJSON [] modelName := gjson.GetBytes(rawJSON, "model").String() cliCtx, cliCancel := h.GetContextWithCancel(h, c, context.Background()) + stopKeepAlive := h.StartNonStreamingKeepAlive(c, cliCtx) resp, upstreamHeaders, errMsg := h.ExecuteWithAuthManager(cliCtx, h.HandlerType(), modelName, rawJSON, h.GetAlt(c)) + stopKeepAlive() if errMsg != nil { h.WriteErrorResponse(c, errMsg) cliCancel(errMsg.Error) -- knotmirror 0.0.1 From aff2095abd3b6feb632a717b53f8421b07af4ea3 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Sat, 8 Aug 2026 06:42:13 +0800 Subject: [PATCH 07/17] fix(codex): deduplicate Responses tools when converting to OpenAI Chat Completions - Merge `tools` and `additional_tools` through a single canonical traversal and emit each Chat Completions tool name once (first declaration wins). - Rework custom-tool and single-tool classification to use merged declaration identity, preventing collisions from duplicate definitions. - Make reverse qualified-name resolution consistent with the same merge order so split calls map to the tool that actually survived conversion. Closes: #4788 --- .../openai_openai-responses_request.go | 21 +- .../openai_openai-responses_request_test.go | 275 +++++++++++++++++ .../openai_openai-responses_tools.go | 290 +++++++++--------- 3 files changed, 425 insertions(+), 161 deletions(-) diff --git a/internal/translator/openai/openai/responses/openai_openai-responses_request.go b/internal/translator/openai/openai/responses/openai_openai-responses_request.go index a329d4e3..7ab2ced9 100644 --- a/internal/translator/openai/openai/responses/openai_openai-responses_request.go +++ b/internal/translator/openai/openai/responses/openai_openai-responses_request.go @@ -311,25 +311,8 @@ func ConvertOpenAIResponsesRequestToOpenAIChatCompletions(modelName string, inpu // "additional_tools" input item instead of the top-level "tools" field, // so merge both sources. var chatCompletionsTools []interface{} - appendChatTools := func(tools gjson.Result) { - if !tools.Exists() || !tools.IsArray() { - return - } - tools.ForEach(func(_, tool gjson.Result) bool { - for _, chatTool := range convertResponsesToolToOpenAIChatTools(tool) { - chatCompletionsTools = append(chatCompletionsTools, gjson.ParseBytes(chatTool).Value()) - } - return true - }) - } - appendChatTools(root.Get("tools")) - if input := root.Get("input"); input.Exists() && input.IsArray() { - input.ForEach(func(_, item gjson.Result) bool { - if item.Get("type").String() == "additional_tools" { - appendChatTools(item.Get("tools")) - } - return true - }) + for _, chatTool := range mergeResponsesRequestChatTools(root) { + chatCompletionsTools = append(chatCompletionsTools, gjson.ParseBytes(chatTool).Value()) } if len(chatCompletionsTools) > 0 { out, _ = sjson.SetBytes(out, "tools", chatCompletionsTools) diff --git a/internal/translator/openai/openai/responses/openai_openai-responses_request_test.go b/internal/translator/openai/openai/responses/openai_openai-responses_request_test.go index 749185a0..48fdcc91 100644 --- a/internal/translator/openai/openai/responses/openai_openai-responses_request_test.go +++ b/internal/translator/openai/openai/responses/openai_openai-responses_request_test.go @@ -695,3 +695,278 @@ func TestConvertOpenAIResponsesRequestToOpenAIChatCompletions_NormalizesInputIma }) } } + +func TestConvertOpenAIResponsesRequestToOpenAIChatCompletions_DeduplicatesToolsAcrossAdditionalTools(t *testing.T) { + raw := []byte(`{ + "input": [ + {"role":"user","content":"What time is it?"}, + { + "type":"additional_tools", + "tools":[ + {"type":"function","name":"get_time","description":"copy from additional_tools","parameters":{"type":"object","properties":{"tz":{"type":"string"}}}} + ] + } + ], + "tools": [ + {"type":"function","name":"get_time","description":"authoritative top-level definition","parameters":{"type":"object","properties":{"timezone":{"type":"string"}}}} + ] + }`) + t.Logf("input json:\n%s", prettyJSONForTest(raw)) + + out := ConvertOpenAIResponsesRequestToOpenAIChatCompletions("deepseek-v4-flash", raw, false) + t.Logf("output json:\n%s", prettyJSONForTest(out)) + + if got := gjson.GetBytes(out, "tools.#").Int(); got != 1 { + t.Fatalf("tools count = %d, want 1; output=%s", got, out) + } + if got := gjson.GetBytes(out, "tools.0.function.name").String(); got != "get_time" { + t.Fatalf("tools.0.function.name = %q, want get_time; output=%s", got, out) + } + if got := gjson.GetBytes(out, "tools.0.function.description").String(); got != "authoritative top-level definition" { + t.Fatalf("tools.0.function.description = %q, want the top-level definition to win; output=%s", got, out) + } + if got := gjson.GetBytes(out, "tools.0.function.parameters.properties.timezone.type").String(); got != "string" { + t.Fatalf("tools.0.function.parameters should come from the top-level definition; output=%s", out) + } +} + +func TestConvertOpenAIResponsesRequestToOpenAIChatCompletions_DeduplicatesNamespaceQualifiedCollision(t *testing.T) { + raw := []byte(`{ + "input": [ + {"role":"user","content":"Patch the file."} + ], + "tools": [ + {"type":"function","name":"editor__apply_patch","parameters":{"type":"object"}}, + { + "type":"namespace", + "name":"editor", + "tools":[{"type":"function","name":"apply_patch","parameters":{"type":"object"}}] + } + ] + }`) + t.Logf("input json:\n%s", prettyJSONForTest(raw)) + + out := ConvertOpenAIResponsesRequestToOpenAIChatCompletions("deepseek-v4-flash", raw, false) + t.Logf("output json:\n%s", prettyJSONForTest(out)) + + if got := gjson.GetBytes(out, "tools.#").Int(); got != 1 { + t.Fatalf("tools count = %d, want 1; output=%s", got, out) + } + if got := gjson.GetBytes(out, "tools.0.function.name").String(); got != "editor__apply_patch" { + t.Fatalf("tools.0.function.name = %q, want editor__apply_patch; output=%s", got, out) + } +} + +func TestConvertOpenAIResponsesRequestToOpenAIChatCompletions_KeepsDistinctToolsFromBothSources(t *testing.T) { + raw := []byte(`{ + "input": [ + {"role":"user","content":"Do the thing."}, + { + "type":"additional_tools", + "tools":[ + {"type":"function","name":"get_date","parameters":{"type":"object"}}, + {"type":"function","name":"get_time","parameters":{"type":"object"}} + ] + } + ], + "tools": [ + {"type":"function","name":"get_time","parameters":{"type":"object"}}, + {"type":"function","name":"get_weather","parameters":{"type":"object"}} + ] + }`) + t.Logf("input json:\n%s", prettyJSONForTest(raw)) + + out := ConvertOpenAIResponsesRequestToOpenAIChatCompletions("deepseek-v4-flash", raw, false) + t.Logf("output json:\n%s", prettyJSONForTest(out)) + + want := []string{"get_time", "get_weather", "get_date"} + if got := gjson.GetBytes(out, "tools.#").Int(); got != int64(len(want)) { + t.Fatalf("tools count = %d, want %d; output=%s", got, len(want), out) + } + for i, wantName := range want { + got := gjson.GetBytes(out, fmt.Sprintf("tools.%d.function.name", i)).String() + if got != wantName { + t.Fatalf("tools.%d.function.name = %q, want %q; output=%s", i, got, wantName, out) + } + } +} + +func TestResponsesSingleCustomToolName_CountsDeduplicatedTools(t *testing.T) { + raw := []byte(`{ + "input": [ + {"role":"user","content":"Patch the file."}, + { + "type":"additional_tools", + "tools":[{"type":"custom","name":"apply_patch","description":"copy"}] + } + ], + "tools": [ + {"type":"custom","name":"apply_patch","description":"authoritative"} + ] + }`) + + name, ok := responsesSingleCustomToolName(raw) + if !ok { + t.Fatalf("responsesSingleCustomToolName ok = false, want true when the only tool is duplicated across both sources") + } + if name != "apply_patch" { + t.Fatalf("responsesSingleCustomToolName name = %q, want apply_patch", name) + } +} + +func TestSplitResponsesQualifiedFunctionCallFromRequest_FirstDeclarationWins(t *testing.T) { + flatFirst := []byte(`{ + "tools": [ + {"type":"function","name":"editor__apply_patch","parameters":{"type":"object"}}, + {"type":"namespace","name":"editor","tools":[{"type":"function","name":"apply_patch","parameters":{"type":"object"}}]} + ] + }`) + namespaceFirst := []byte(`{ + "tools": [ + {"type":"namespace","name":"editor","tools":[{"type":"function","name":"apply_patch","parameters":{"type":"object"}}]}, + {"type":"function","name":"editor__apply_patch","parameters":{"type":"object"}} + ] + }`) + namespaceOnly := []byte(`{ + "tools": [ + {"type":"namespace","name":"mcp__github","tools":[{"type":"function","name":"get_me","parameters":{"type":"object"}}]} + ] + }`) + + tests := []struct { + name string + raw []byte + qualified string + wantName string + wantNamespace string + }{ + // The flat tool is the one that survives merging, so it must stay flat. + {"flat declared first", flatFirst, "editor__apply_patch", "editor__apply_patch", ""}, + // The namespace child survives here, so the call splits back into it. + {"namespace declared first", namespaceFirst, "editor__apply_patch", "apply_patch", "editor"}, + // No collision: unchanged behaviour. + {"namespace only", namespaceOnly, "mcp__github__get_me", "get_me", "mcp__github"}, + // Unknown name falls through untouched. + {"unknown name", flatFirst, "something_else", "something_else", ""}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotName, gotNamespace := splitResponsesQualifiedFunctionCallFromRequest(tt.raw, tt.qualified) + if gotName != tt.wantName || gotNamespace != tt.wantNamespace { + t.Fatalf("split(%q) = (%q, %q), want (%q, %q)", + tt.qualified, gotName, gotNamespace, tt.wantName, tt.wantNamespace) + } + }) + } +} + +func TestSplitResponsesQualifiedFunctionCallFromRequest_MatchesMergedToolIdentity(t *testing.T) { + // Whatever survives the merge must be what reverse translation reports. + raw := []byte(`{ + "tools": [ + {"type":"function","name":"editor__apply_patch","parameters":{"type":"object"}}, + {"type":"namespace","name":"editor","tools":[{"type":"function","name":"apply_patch","parameters":{"type":"object"}}]} + ] + }`) + + merged := mergeResponsesRequestChatTools(gjson.ParseBytes(raw)) + if len(merged) != 1 { + t.Fatalf("merged tool count = %d, want 1", len(merged)) + } + emitted := gjson.GetBytes(merged[0], "function.name").String() + + name, namespace := splitResponsesQualifiedFunctionCallFromRequest(raw, emitted) + if namespace != "" { + t.Fatalf("emitted tool %q came from a flat declaration, but split reported namespace %q", emitted, namespace) + } + if name != emitted { + t.Fatalf("split(%q) name = %q, want %q", emitted, name, emitted) + } +} + +func TestResponsesCustomToolNames_FollowsMergedDeclaration(t *testing.T) { + // Declarations delivered through the two channels may differ in type: a + // top-level function and an "additional_tools" custom tool can flatten to + // the same Chat Completions name. Only the winner may decide whether the + // tool is freeform, otherwise a plain function call comes back as a + // custom_tool_call with unwrapped arguments. + functionFirst := []byte(`{ + "input": [ + {"type":"additional_tools","tools":[{"type":"custom","name":"exec","description":"copy"}]} + ], + "tools": [ + {"type":"function","name":"exec","parameters":{"type":"object"}} + ] + }`) + customFirst := []byte(`{ + "input": [ + {"type":"additional_tools","tools":[{"type":"function","name":"exec","parameters":{"type":"object"}}]} + ], + "tools": [ + {"type":"custom","name":"exec","description":"authoritative"} + ] + }`) + + tests := []struct { + name string + raw []byte + wantCustom bool + }{ + {name: "function declaration wins", raw: functionFirst, wantCustom: false}, + {name: "custom declaration wins", raw: customFirst, wantCustom: true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + merged := mergeResponsesRequestChatTools(gjson.ParseBytes(tt.raw)) + if len(merged) != 1 { + t.Fatalf("merged tool count = %d, want 1", len(merged)) + } + // Freeform tools are the ones converted to the single-string shape. + mergedIsCustom := gjson.GetBytes(merged[0], "function.parameters.properties.input").Exists() + if mergedIsCustom != tt.wantCustom { + t.Fatalf("merged tool custom = %v, want %v", mergedIsCustom, tt.wantCustom) + } + + if _, isCustom := responsesCustomToolNames(tt.raw)["exec"]; isCustom != tt.wantCustom { + t.Fatalf("responsesCustomToolNames classified exec as custom = %v, want %v", isCustom, tt.wantCustom) + } + + name, ok := responsesSingleCustomToolName(tt.raw) + if ok != tt.wantCustom { + t.Fatalf("responsesSingleCustomToolName ok = %v, want %v", ok, tt.wantCustom) + } + if ok && name != "exec" { + t.Fatalf("responsesSingleCustomToolName name = %q, want exec", name) + } + }) + } +} + +func TestResponsesCustomToolNames_OnlyReportsMergedTools(t *testing.T) { + // Nested namespaces are not converted, so their children never reach the + // upstream request and must not be classified as freeform tools either. + raw := []byte(`{ + "tools": [ + {"type":"namespace","name":"outer","tools":[ + {"type":"namespace","name":"inner","tools":[{"type":"custom","name":"buried"}]}, + {"type":"custom","name":"reachable"} + ]} + ] + }`) + + mergedNames := make(map[string]struct{}) + for _, chatTool := range mergeResponsesRequestChatTools(gjson.ParseBytes(raw)) { + mergedNames[gjson.GetBytes(chatTool, "function.name").String()] = struct{}{} + } + if _, ok := mergedNames["outer__reachable"]; !ok { + t.Fatalf("merged tool names = %v, want outer__reachable", mergedNames) + } + + for name := range responsesCustomToolNames(raw) { + if _, ok := mergedNames[name]; !ok { + t.Fatalf("responsesCustomToolNames reported %q, which the merge never emits", name) + } + } +} diff --git a/internal/translator/openai/openai/responses/openai_openai-responses_tools.go b/internal/translator/openai/openai/responses/openai_openai-responses_tools.go index d4a9007b..bfdb4c45 100644 --- a/internal/translator/openai/openai/responses/openai_openai-responses_tools.go +++ b/internal/translator/openai/openai/responses/openai_openai-responses_tools.go @@ -7,23 +7,112 @@ import ( "github.com/tidwall/sjson" ) -func convertResponsesToolToOpenAIChatTools(tool gjson.Result) [][]byte { - toolType := strings.TrimSpace(tool.Get("type").String()) - switch toolType { - case "", "function": - if tJSON, ok := convertResponsesFunctionToolToOpenAIChat(tool, ""); ok { - return [][]byte{tJSON} +// responsesToolDeclaration is one Responses tool declaration paired with the +// Chat Completions function name it produces. Namespace children carry both +// their declared name and the owning namespace, so reverse translation can +// restore the split identity. +type responsesToolDeclaration struct { + tool gjson.Result + chatName string + localName string + namespace string + custom bool +} + +// walkResponsesToolDeclarations visits the tool declarations of a Responses +// request in one canonical order: the top-level "tools" field first, then +// Codex Desktop (Responses Lite) "additional_tools" input items, namespace +// children in declaration order. Declarations that produce no Chat Completions +// tool are skipped. Visiting stops early once visit returns false. +// +// Request conversion, reverse name resolution and freeform tool classification +// all traverse through here, so they cannot disagree about which declaration +// backs a given Chat Completions tool name. +func walkResponsesToolDeclarations(root gjson.Result, visit func(responsesToolDeclaration) bool) { + proceed := true + emit := func(tool gjson.Result, namespaceName string) { + if !proceed { + return + } + var custom bool + switch strings.TrimSpace(tool.Get("type").String()) { + case "", "function": + case "custom": + custom = true + default: + return } - case "namespace": - return convertResponsesNamespaceToolToOpenAIChat(tool) - case "custom": - if tJSON, ok := convertResponsesCustomToolToOpenAIChat(tool, ""); ok { - return [][]byte{tJSON} + localName := responsesToolName(tool) + if localName == "" { + return } - default: - return nil + proceed = visit(responsesToolDeclaration{ + tool: tool, + chatName: qualifyResponsesNamespaceToolName(namespaceName, localName), + localName: localName, + namespace: namespaceName, + custom: custom, + }) + } + scan := func(tools gjson.Result) { + if !proceed || !tools.Exists() || !tools.IsArray() { + return + } + tools.ForEach(func(_, tool gjson.Result) bool { + if strings.TrimSpace(tool.Get("type").String()) == "namespace" { + if children := tool.Get("tools"); children.Exists() && children.IsArray() { + namespaceName := strings.TrimSpace(tool.Get("name").String()) + children.ForEach(func(_, child gjson.Result) bool { + emit(child, namespaceName) + return proceed + }) + } + return proceed + } + emit(tool, "") + return proceed + }) + } + + scan(root.Get("tools")) + if input := root.Get("input"); input.Exists() && input.IsArray() { + input.ForEach(func(_, item gjson.Result) bool { + if item.Get("type").String() == "additional_tools" { + scan(item.Get("tools")) + } + return proceed + }) } - return nil +} + +// mergeResponsesRequestChatTools converts every tool declaration in a Responses +// request into Chat Completions form, merging the top-level "tools" field with +// Codex Desktop (Responses Lite) "additional_tools" input items. +// +// Codex clients may deliver the same tool through both channels, and namespace +// qualification can collapse distinct declarations onto one Chat Completions +// name, so entries are deduplicated by function name. The first occurrence +// wins, which keeps the top-level "tools" definition authoritative over the +// "additional_tools" copy. Chat Completions requires tool names to be unique; +// strict upstreams reject the whole request otherwise. +func mergeResponsesRequestChatTools(root gjson.Result) [][]byte { + var merged [][]byte + seenToolNames := make(map[string]struct{}) + walkResponsesToolDeclarations(root, func(declaration responsesToolDeclaration) bool { + if _, duplicate := seenToolNames[declaration.chatName]; duplicate { + return true + } + convert := convertResponsesFunctionToolToOpenAIChat + if declaration.custom { + convert = convertResponsesCustomToolToOpenAIChat + } + if chatTool, ok := convert(declaration.tool, declaration.chatName); ok { + seenToolNames[declaration.chatName] = struct{}{} + merged = append(merged, chatTool) + } + return true + }) + return merged } // convertResponsesCustomToolToOpenAIChat maps a Responses freeform ("custom") @@ -45,32 +134,6 @@ func convertResponsesCustomToolToOpenAIChat(tool gjson.Result, overrideName stri return chatTool, true } -func convertResponsesNamespaceToolToOpenAIChat(tool gjson.Result) [][]byte { - namespaceName := strings.TrimSpace(tool.Get("name").String()) - children := tool.Get("tools") - if !children.Exists() || !children.IsArray() { - return nil - } - - var out [][]byte - children.ForEach(func(_, child gjson.Result) bool { - childName := responsesToolName(child) - qualifiedName := qualifyResponsesNamespaceToolName(namespaceName, childName) - switch strings.TrimSpace(child.Get("type").String()) { - case "", "function": - if tJSON, ok := convertResponsesFunctionToolToOpenAIChat(child, qualifiedName); ok { - out = append(out, tJSON) - } - case "custom": - if tJSON, ok := convertResponsesCustomToolToOpenAIChat(child, qualifiedName); ok { - out = append(out, tJSON) - } - } - return true - }) - return out -} - func convertResponsesFunctionToolToOpenAIChat(tool gjson.Result, overrideName string) ([]byte, bool) { name := strings.TrimSpace(overrideName) if name == "" { @@ -147,43 +210,28 @@ func responsesToolOutputText(output gjson.Result) string { return "" } -// responsesCustomToolNames collects the names of freeform ("custom") tools -// declared in the original Responses request, both in the top-level "tools" -// field and in Codex Desktop "additional_tools" input items. Namespace child -// names use the qualified Chat Completions form. +// responsesCustomToolNames collects the Chat Completions names of the freeform +// ("custom") tools that survive the merge, so response translation only unwraps +// freeform arguments for calls whose winning declaration really was freeform. +// +// Declaration types may differ across the two delivery channels: a top-level +// function and an "additional_tools" custom tool can flatten to the same name. +// Classification therefore follows the same first-wins rule as the merge — +// a discarded custom declaration must not turn a surviving ordinary function +// into a custom_tool_call. func responsesCustomToolNames(requestRawJSON []byte) map[string]struct{} { names := make(map[string]struct{}) - var collect func(gjson.Result, string) - collect = func(tools gjson.Result, namespaceName string) { - if !tools.Exists() || !tools.IsArray() { - return - } - tools.ForEach(func(_, tool gjson.Result) bool { - switch strings.TrimSpace(tool.Get("type").String()) { - case "custom": - name := responsesToolName(tool) - if namespaceName != "" { - name = qualifyResponsesNamespaceToolName(namespaceName, name) - } - if name != "" { - names[name] = struct{}{} - } - case "namespace": - collect(tool.Get("tools"), strings.TrimSpace(tool.Get("name").String())) - } + seenToolNames := make(map[string]struct{}) + walkResponsesToolDeclarations(gjson.ParseBytes(requestRawJSON), func(declaration responsesToolDeclaration) bool { + if _, duplicate := seenToolNames[declaration.chatName]; duplicate { return true - }) - } - root := gjson.ParseBytes(requestRawJSON) - collect(root.Get("tools"), "") - if input := root.Get("input"); input.Exists() && input.IsArray() { - input.ForEach(func(_, item gjson.Result) bool { - if item.Get("type").String() == "additional_tools" { - collect(item.Get("tools"), "") - } - return true - }) - } + } + seenToolNames[declaration.chatName] = struct{}{} + if declaration.custom { + names[declaration.chatName] = struct{}{} + } + return true + }) return names } @@ -193,27 +241,10 @@ func responsesSingleCustomToolName(requestRawJSON []byte) (string, bool) { return "", false } - toolCount := 0 - collect := func(tools gjson.Result) { - if !tools.Exists() || !tools.IsArray() { - return - } - tools.ForEach(func(_, tool gjson.Result) bool { - toolCount += len(convertResponsesToolToOpenAIChatTools(tool)) - return true - }) - } - - root := gjson.ParseBytes(requestRawJSON) - collect(root.Get("tools")) - if input := root.Get("input"); input.Exists() && input.IsArray() { - input.ForEach(func(_, item gjson.Result) bool { - if item.Get("type").String() == "additional_tools" { - collect(item.Get("tools")) - } - return true - }) - } + // Count the tools actually emitted, which are deduplicated by name, so a + // tool delivered through both "tools" and "additional_tools" still counts + // once and freeform unwrapping stays enabled. + toolCount := len(mergeResponsesRequestChatTools(gjson.ParseBytes(requestRawJSON))) for name := range customToolNames { return name, toolCount == 1 } @@ -247,60 +278,35 @@ func qualifyResponsesNamespaceToolName(namespaceName, childName string) string { return namespaceName + "__" + childName } +// resolveResponsesQualifiedToolIdentity maps an emitted Chat Completions +// function name back to the Responses declaration that produced it. +// +// Declarations are walked in the same order mergeResponsesRequestChatTools +// uses, and the first one producing the name wins, so reverse translation +// reports the identity of the declaration that actually survived the merge. A +// flat top-level tool named "editor__apply_patch" therefore stays flat even +// when a later namespace declares a child qualifying to the same name. +func resolveResponsesQualifiedToolIdentity(root gjson.Result, qualifiedName string) (name, namespace string, found bool) { + walkResponsesToolDeclarations(root, func(declaration responsesToolDeclaration) bool { + if declaration.chatName != qualifiedName { + return true + } + name, namespace, found = declaration.localName, declaration.namespace, true + return false + }) + return name, namespace, found +} + func splitResponsesQualifiedFunctionCallFromRequest(requestRawJSON []byte, qualifiedName string) (name, namespace string) { qualifiedName = strings.TrimSpace(qualifiedName) if qualifiedName == "" { return "", "" } - var bestNamespace string - var bestChild string - collect := func(tools gjson.Result) { - if !tools.Exists() || !tools.IsArray() { - return - } - tools.ForEach(func(_, tool gjson.Result) bool { - if strings.TrimSpace(tool.Get("type").String()) != "namespace" { - return true - } - namespaceName := strings.TrimSpace(tool.Get("name").String()) - if namespaceName == "" { - return true - } - children := tool.Get("tools") - if !children.Exists() || !children.IsArray() { - return true - } - children.ForEach(func(_, child gjson.Result) bool { - childName := responsesToolName(child) - if childName == "" { - return true - } - if qualifyResponsesNamespaceToolName(namespaceName, childName) == qualifiedName { - bestNamespace = namespaceName - bestChild = childName - } - return true - }) - return true - }) - } - - root := gjson.ParseBytes(requestRawJSON) - collect(root.Get("tools")) - if input := root.Get("input"); input.Exists() && input.IsArray() { - input.ForEach(func(_, item gjson.Result) bool { - if item.Get("type").String() == "additional_tools" { - collect(item.Get("tools")) - } - return true - }) - } - - if bestNamespace == "" || bestChild == "" { - return qualifiedName, "" + if resolvedName, resolvedNamespace, ok := resolveResponsesQualifiedToolIdentity(gjson.ParseBytes(requestRawJSON), qualifiedName); ok { + return resolvedName, resolvedNamespace } - return bestChild, bestNamespace + return qualifiedName, "" } func pickRequestJSON(originalRequestRawJSON, requestRawJSON []byte) []byte { -- knotmirror 0.0.1 From 8558f44329af49a6b2c2785b9e1ea28a80f5bbcb Mon Sep 17 00:00:00 2001 From: sususu Date: Fri, 7 Aug 2026 16:38:45 +0800 Subject: [PATCH 08/17] feat(signature): identify Kimi thinking signatures by fixed size Kimi Messages responses carry a thinking signature that no existing validator claims, so replayed Kimi history resolved to the residual unknown class and lost its provider policy. Kimi exposes no self-describing envelope: a per-offset scan over the observed corpus finds no magic prefix, version byte or key id, so the GPT, Claude and Gemini probes have nothing to anchor on. The one stable signal is raw length, which is fixed per protocol mode at 12946 characters for non-streaming and 4340 for streaming, and is independent of the content it accompanies. This is not bucketing of a variable payload: a response whose thinking text grew from 6 to 14803 characters emits a byte-identical length, and a non-streaming response carrying a single thinking token still emits the full 12946. Because size is the only signal, the probe runs after every self-describing envelope check has declined. A length coincidence then cannot capture another provider's signature, and future drift in Kimi's sizes costs Kimi its own identification instead of corrupting a neighbouring family. Replaying the cross-provider catalog confirms the existing families are unaffected. The envelope pre-filter now gates only the envelope probes rather than returning early. Kimi's base64 is uniformly distributed and starts with one of "CERg" about 6% of the time, so an early return dropped those payloads on whichever side of the gate they happened to land. A Kimi target drops only the signature rather than the whole block: the upstream returns 200 for a mutated, truncated, non-base64 or entirely absent signature, because reasoning continuity there travels in OpenAI-style reasoning_content. Dropping the block would discard recoverable thinking text for no upstream benefit. Contract tests synthesize payloads from a seeded PRNG, since length and entropy need no captured traffic. Corpus-backed tests validate against real harvested samples and skip when the corpus is absent, matching how the Grok and Gemini native corpora are already handled. --- internal/signature/grok_validation.go | 9 + internal/signature/kimi_validation.go | 161 ++++++++ internal/signature/kimi_validation_test.go | 370 +++++++++++++++++++ internal/signature/provider_compatibility.go | 63 +++- 4 files changed, 589 insertions(+), 14 deletions(-) create mode 100644 internal/signature/kimi_validation.go create mode 100644 internal/signature/kimi_validation_test.go diff --git a/internal/signature/grok_validation.go b/internal/signature/grok_validation.go index 41e7b07d..ba09ad94 100644 --- a/internal/signature/grok_validation.go +++ b/internal/signature/grok_validation.go @@ -81,6 +81,15 @@ func InspectGrokEncryptedContent(raw string) (*GrokEncryptedContentInfo, error) return nil, fmt.Errorf("Grok encrypted_content looks like Gemini thoughtSignature") } } + // Kimi emits no envelope either, so the pre-filter above cannot narrow it and + // this check has to run unconditionally. Length is the only separator the two + // families have: Kimi is fixed at two code-path constants while xAI payload + // 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) { + return nil, fmt.Errorf("Grok encrypted_content has a Kimi thinking signature length") + } decoded, err := decodeGrokEncryptedContent(sig) if err != nil { diff --git a/internal/signature/kimi_validation.go b/internal/signature/kimi_validation.go new file mode 100644 index 00000000..c7c600b8 --- /dev/null +++ b/internal/signature/kimi_validation.go @@ -0,0 +1,161 @@ +package signature + +import ( + "encoding/base64" + "fmt" + "strings" +) + +// Kimi thinking signatures carry no self-describing envelope. Every byte is +// indistinguishable from uniform random data: a per-offset scan over 44 samples +// x 9709 bytes found zero positions below a 4-sigma floor, so there is no magic +// prefix, version byte, key id or timestamp to anchor on the way GPT (Fernet), +// Claude (CAIS/protobuf) and Gemini (Tink envelope) all provide. +// +// What Kimi does expose is size. The raw signature length is fixed per protocol +// mode and completely independent of the content it accompanies: +// +// non-streaming : 12946 characters (9709 bytes) +// streaming : 4340 characters (3255 bytes) +// +// This is not quantization of a variable payload into buckets. There is no +// bucketing behaviour at all: a response whose thinking text grew from 6 to +// 14,803 characters (thinking_tokens 1 -> 6188, output_tokens 29 -> 2709) emits +// a byte-identical signature length, and a non-streaming response carrying a +// single thinking token still emits the full 12946. The two values are code-path +// constants, not size classes. +// +// Empirical basis for treating the pair as complete: +// - All 8 Kimi models exposed upstream (k2, k2.5, k2.6, k2-thinking, k2.7-code, +// k2.7-code-highspeed, k3, k3-256k) x streaming/non-streaming = 16 combinations, +// no exceptions. +// - Additional paths that produced no third value: thinking budget 128..12000, +// absent thinking field, max_tokens truncation mid-thinking, non-English +// prompts, 135k-character inputs, multi-turn replay of signed history, tool +// calls, tool_result continuation, and the interleaved-thinking beta header. +// - Two independent collection paths agree: CPA request logs (57 unique samples) +// and the mitmproxy harvest in +// .agents/skills/cpa-signature-catalog-and-collection/data/signatures/kimi/ +// (61 unique samples) both yield exactly {4340, 12946}. +// +// Cross-family safety: across 1027 catalog signatures plus 215 native Grok +// samples, no Claude, Gemini, GPT or Grok value lands on either length. The +// nearest miss is a 4344-character GPT token, which the gAAAA probe claims long +// before this check runs. +// +// Fragility this check accepts, and why it still runs last: the length pair is +// an observed regularity, not a protocol contract. Kimi never reads the field +// back - replaying an empty string, a single character, non-base64 text or a +// mutated blob all return 200, and omitting the signature entirely also returns +// 200, because reasoning continuity on that endpoint travels in OpenAI-style +// reasoning_content instead. A gateway change could therefore move these values +// without any client-visible error. Running the self-describing validators first +// bounds the damage: a drift only costs Kimi its own identification and cannot +// mislabel another provider's signature. +const ( + // KimiThinkingSignatureNonStreamingLen is the raw character length Kimi emits + // for non-streaming Messages responses. + KimiThinkingSignatureNonStreamingLen = 12946 + // KimiThinkingSignatureStreamingLen is the raw character length Kimi emits in + // the streaming signature_delta event. + KimiThinkingSignatureStreamingLen = 4340 +) + +// KimiThinkingSignatureMode records which upstream code path produced a +// signature. It is derived from length alone and carries no decoded content. +type KimiThinkingSignatureMode string + +const ( + KimiThinkingSignatureModeNonStreaming KimiThinkingSignatureMode = "non_streaming" + KimiThinkingSignatureModeStreaming KimiThinkingSignatureMode = "streaming" +) + +// kimiThinkingSignatureLens maps every accepted raw length to the mode that +// produces it. Keeping this as a package-level map rather than inline constants +// leaves room for a calibration pass to register a newly observed length without +// touching the probe itself. +var kimiThinkingSignatureLens = map[int]KimiThinkingSignatureMode{ + KimiThinkingSignatureNonStreamingLen: KimiThinkingSignatureModeNonStreaming, + KimiThinkingSignatureStreamingLen: KimiThinkingSignatureModeStreaming, +} + +// MinKimiThinkingSignatureEntropyRatio keeps a same-length attacker-supplied +// filler from claiming the family. Native samples sit at 0.997+ against the +// sample-size ceiling, so this floor has multiple sigma of headroom while still +// rejecting padded or repetitive input. +const MinKimiThinkingSignatureEntropyRatio = 0.85 + +// KimiThinkingSignatureInfo describes an accepted Kimi thinking signature. +type KimiThinkingSignatureInfo struct { + RawLen int + DecodedLen int + Mode KimiThinkingSignatureMode +} + +// InspectKimiThinkingSignature validates the transport shape of a Kimi Messages +// thinking signature. +// +// Unlike the Claude, Gemini and GPT validators this proves nothing about the +// payload: it reports that the value has the size and character class Kimi +// produces. Because size is the only available signal, this probe must run after +// every self-describing envelope check has declined, so that a Claude, Gemini or +// GPT signature can never be captured by a length coincidence. +func InspectKimiThinkingSignature(raw string) (*KimiThinkingSignatureInfo, error) { + sig := strings.TrimSpace(raw) + if sig == "" { + return nil, fmt.Errorf("empty Kimi thinking signature") + } + if sig != raw { + return nil, fmt.Errorf("Kimi thinking signature has leading or trailing whitespace") + } + mode, ok := kimiThinkingSignatureLens[len(sig)] + if !ok { + return nil, fmt.Errorf("invalid Kimi thinking signature: unexpected length %d", len(sig)) + } + if strings.Contains(sig, "=") { + return nil, fmt.Errorf("invalid Kimi thinking signature: expected unpadded standard base64") + } + if index, r, ok := firstInvalidGrokEncryptedContentChar(sig); ok { + return nil, fmt.Errorf("invalid Kimi thinking signature: contains non-base64 character U+%04X at byte %d", r, index) + } + if _, _, ok := SplitSignatureProviderPrefix(sig); ok { + return nil, fmt.Errorf("invalid Kimi thinking signature: carries another provider's cache prefix") + } + // Defense in depth. DetectSignatureProviderForBlock already runs the + // self-describing probes first, but this validator is exported and callers + // may reach it directly, so a foreign envelope of coincidentally matching + // length must not be accepted here either. + if maybeSelfDescribingSignatureEnvelope(sig) { + if strings.HasPrefix(sig, "gAAAA") { + return nil, fmt.Errorf("Kimi thinking signature looks like GPT/Codex reasoning signature") + } + if IsValidClaudeCAISSignature(sig) { + return nil, fmt.Errorf("Kimi thinking signature looks like Claude CAIS thinking signature") + } + if IsValidClaudeThinkingSignature(sig, ClaudeSignatureValidationOptions{Strict: true}) { + return nil, fmt.Errorf("Kimi thinking signature looks like Claude thinking signature") + } + if IsValidGeminiThoughtSignature(sig, GeminiThoughtSignatureValidationOptions{RequireKnownEnvelope: true}) { + return nil, fmt.Errorf("Kimi thinking signature looks like Gemini thoughtSignature") + } + } + decoded, err := base64.RawStdEncoding.DecodeString(sig) + if err != nil { + return nil, fmt.Errorf("invalid Kimi thinking signature: base64 decode failed: %w", err) + } + if entropyRatio := byteEntropyRatio(decoded); entropyRatio < MinKimiThinkingSignatureEntropyRatio { + return nil, fmt.Errorf("invalid Kimi thinking signature: decoded payload entropy ratio %.3f below %.3f", entropyRatio, MinKimiThinkingSignatureEntropyRatio) + } + return &KimiThinkingSignatureInfo{ + RawLen: len(sig), + DecodedLen: len(decoded), + Mode: mode, + }, nil +} + +// IsValidKimiThinkingSignature reports whether raw has the transport shape of a +// Kimi thinking signature. +func IsValidKimiThinkingSignature(raw string) bool { + _, err := InspectKimiThinkingSignature(raw) + return err == nil +} diff --git a/internal/signature/kimi_validation_test.go b/internal/signature/kimi_validation_test.go new file mode 100644 index 00000000..304fc4fe --- /dev/null +++ b/internal/signature/kimi_validation_test.go @@ -0,0 +1,370 @@ +package signature + +import ( + "encoding/base64" + "encoding/json" + "math/rand" + "os" + "path/filepath" + "runtime" + "strings" + "testing" +) + +// kimiSignatureCorpusPath locates the harvested Kimi signature corpus. The +// corpus lives with the collection skill that produced it and is not tracked in +// this repository, matching how the Grok and Gemini native corpora are handled: +// tests that need real traffic skip when it is absent rather than committing +// captured payloads. +func kimiSignatureCorpusPath() (string, bool) { + _, file, _, ok := runtime.Caller(0) + if !ok { + return "", false + } + repo := filepath.Clean(filepath.Join(filepath.Dir(file), "..", "..")) + path := filepath.Join(repo, ".agents", "skills", "cpa-signature-catalog-and-collection", "data", "signatures", "kimi", "samples.json") + if _, err := os.Stat(path); err != nil { + return path, false + } + return path, true +} + +// masterSignatureCatalogPath locates the cross-provider signature catalog from +// the same collection skill. +func masterSignatureCatalogPath() (string, bool) { + _, file, _, ok := runtime.Caller(0) + if !ok { + return "", false + } + repo := filepath.Clean(filepath.Join(filepath.Dir(file), "..", "..")) + path := filepath.Join(repo, ".agents", "skills", "cpa-signature-catalog-and-collection", "data", "master_signatures_catalog.json") + if _, err := os.Stat(path); err != nil { + return path, false + } + return path, true +} + +const kimiCorpusSkipReason = "kimi signature corpus missing; see .agents/skills/cpa-signature-catalog-and-collection" + +func loadKimiCorpus(t *testing.T) []string { + t.Helper() + path, ok := kimiSignatureCorpusPath() + if !ok { + t.Skip(kimiCorpusSkipReason) + } + raw, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read kimi corpus: %v", err) + } + var doc struct { + Samples []struct { + Signature string `json:"signature"` + } `json:"samples"` + } + if err := json.Unmarshal(raw, &doc); err != nil { + t.Fatalf("parse kimi corpus: %v", err) + } + out := make([]string, 0, len(doc.Samples)) + for _, sample := range doc.Samples { + if sample.Signature != "" { + out = append(out, sample.Signature) + } + } + if len(out) == 0 { + t.Skip(kimiCorpusSkipReason) + } + return out +} + +// synthesizeKimiSignature builds a signature-shaped payload of the requested +// decoded size from a seeded PRNG. Kimi identification rests entirely on raw +// length plus the payload being high-entropy unpadded base64, and none of that +// requires captured traffic, so the contract tests below run everywhere instead +// of depending on a local corpus. +func synthesizeKimiSignature(t *testing.T, decodedLen int, seed int64) string { + t.Helper() + buf := make([]byte, decodedLen) + prng := rand.New(rand.NewSource(seed)) + if _, err := prng.Read(buf); err != nil { + t.Fatalf("synthesize payload: %v", err) + } + return base64.RawStdEncoding.EncodeToString(buf) +} + +// TestKimiThinkingSignatureLengths_MatchDecodedSizes pins the arithmetic that +// makes the two constants reachable at all: unpadded base64 of 9709 and 3255 +// bytes is exactly 12946 and 4340 characters. A future edit that changes one +// constant without the other would otherwise produce a length no real payload +// can have. +func TestKimiThinkingSignatureLengths_MatchDecodedSizes(t *testing.T) { + tests := []struct { + name string + decodedLen int + wantRawLen int + }{ + {"non streaming", 9709, KimiThinkingSignatureNonStreamingLen}, + {"streaming", 3255, KimiThinkingSignatureStreamingLen}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + sig := synthesizeKimiSignature(t, tc.decodedLen, 1) + if len(sig) != tc.wantRawLen { + t.Fatalf("raw length = %d, want %d", len(sig), tc.wantRawLen) + } + info, err := InspectKimiThinkingSignature(sig) + if err != nil { + t.Fatalf("synthesized payload rejected: %v", err) + } + if info.DecodedLen != tc.decodedLen { + t.Errorf("DecodedLen = %d, want %d", info.DecodedLen, tc.decodedLen) + } + }) + } +} + +func TestInspectKimiThinkingSignature_ReportsMode(t *testing.T) { + tests := []struct { + name string + decodedLen int + wantMode KimiThinkingSignatureMode + }{ + {"non streaming", 9709, KimiThinkingSignatureModeNonStreaming}, + {"streaming", 3255, KimiThinkingSignatureModeStreaming}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + info, err := InspectKimiThinkingSignature(synthesizeKimiSignature(t, tc.decodedLen, 7)) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if info.Mode != tc.wantMode { + t.Errorf("Mode = %q, want %q", info.Mode, tc.wantMode) + } + }) + } +} + +// TestInspectKimiThinkingSignature_RejectsNeighbouringLengths is the core +// negative test for a size-only probe: one character in either direction must +// fall out of the family. +func TestInspectKimiThinkingSignature_RejectsNeighbouringLengths(t *testing.T) { + for _, decodedLen := range []int{9709, 3255} { + native := synthesizeKimiSignature(t, decodedLen, 3) + for _, tc := range []struct { + name string + sig string + }{ + {"one character short", native[:len(native)-1]}, + {"one character long", native + "A"}, + } { + t.Run(tc.name, func(t *testing.T) { + if IsValidKimiThinkingSignature(tc.sig) { + t.Errorf("length %d accepted as Kimi signature", len(tc.sig)) + } + }) + } + } +} + +func TestInspectKimiThinkingSignature_RejectsMalformedInput(t *testing.T) { + native := synthesizeKimiSignature(t, 3255, 5) + tests := []struct { + name string + sig string + }{ + {"empty", ""}, + {"whitespace only", " "}, + {"leading whitespace", " " + native}, + {"trailing whitespace", native + " "}, + {"padded base64", native[:len(native)-2] + "=="}, + {"non base64 character", native[:len(native)-1] + "!"}, + {"provider cache prefix", "claude#" + native}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + if IsValidKimiThinkingSignature(tc.sig) { + t.Errorf("malformed input accepted as Kimi signature") + } + }) + } +} + +// TestInspectKimiThinkingSignature_RejectsLowEntropyFiller pins the one attack a +// length check alone cannot survive: a caller that knows the constant and pads +// to it with structured bytes. +func TestInspectKimiThinkingSignature_RejectsLowEntropyFiller(t *testing.T) { + for _, length := range []int{KimiThinkingSignatureStreamingLen, KimiThinkingSignatureNonStreamingLen} { + if IsValidKimiThinkingSignature(strings.Repeat("A", length)) { + t.Errorf("repeated-character filler of length %d accepted as Kimi signature", length) + } + } +} + +// TestInspectKimiThinkingSignature_RejectsSelfDescribingEnvelope guards the +// exported entry point. DetectSignatureProviderForBlock already runs the +// envelope probes first, but callers can reach this validator directly, so a +// foreign envelope must not be accepted here on length alone. +func TestInspectKimiThinkingSignature_RejectsSelfDescribingEnvelope(t *testing.T) { + if IsValidKimiThinkingSignature(observedFable5Sample) { + t.Errorf("Claude CAIS sample accepted as Kimi signature") + } +} + +// TestDetectSignatureProvider_KimiRunsAfterEnvelopeProbes pins the ordering +// invariant. Kimi's base64 is uniformly distributed, so roughly 6% of real +// signatures start with one of the "CERg" envelope characters; those must still +// resolve to Kimi after the envelope probes decline, and a real envelope must +// never be captured by the size probe. +func TestDetectSignatureProvider_KimiRunsAfterEnvelopeProbes(t *testing.T) { + if got := DetectSignatureProvider(observedFable5Sample); got != SignatureProviderClaude { + t.Fatalf("DetectSignatureProvider = %q, want %q for a Claude CAIS sample", got, SignatureProviderClaude) + } + + var checked int + for seed := int64(0); seed < 200 && checked < 3; seed++ { + sig := synthesizeKimiSignature(t, 3255, seed) + if !maybeSelfDescribingSignatureEnvelope(sig) { + continue + } + checked++ + if got := DetectSignatureProvider(sig); got != SignatureProviderKimi { + t.Fatalf("DetectSignatureProvider = %q, want %q for an envelope-prefixed Kimi payload", got, SignatureProviderKimi) + } + } + if checked == 0 { + t.Skip("no synthesized payload landed on an envelope first character") + } +} + +func TestInspectGrokEncryptedContent_RejectsKimiLengths(t *testing.T) { + for _, decodedLen := range []int{9709, 3255} { + sig := synthesizeKimiSignature(t, decodedLen, 11) + if IsValidGrokEncryptedContent(sig) { + t.Errorf("Kimi-length payload (%d bytes) accepted as Grok encrypted_content", decodedLen) + } + } +} + +func TestSignatureProviderFromModelName_Kimi(t *testing.T) { + tests := []struct { + model string + want SignatureProvider + }{ + {"kimi-k3", SignatureProviderKimi}, + {"kimi-k3-256k", SignatureProviderKimi}, + {"kimi-k2.7-code-highspeed", SignatureProviderKimi}, + {"k3", SignatureProviderKimi}, + {"k2-thinking", SignatureProviderKimi}, + {"moonshot-v1-128k", SignatureProviderKimi}, + {"claude-opus-5", SignatureProviderClaude}, + {"gemini-3.6-flash", SignatureProviderGemini}, + {"gpt-5.6-sol", SignatureProviderGPT}, + } + for _, tc := range tests { + t.Run(tc.model, func(t *testing.T) { + if got := SignatureProviderFromModelName(tc.model); got != tc.want { + t.Errorf("SignatureProviderFromModelName(%q) = %q, want %q", tc.model, got, tc.want) + } + }) + } +} + +// TestDecideSignatureCompatibility_KimiDropsSignatureNotBlock encodes the +// measured upstream behaviour: Kimi returns 200 for a mutated, truncated, +// non-base64 or entirely absent thinking signature, so a foreign signature costs +// the field rather than the reasoning text. +func TestDecideSignatureCompatibility_KimiDropsSignatureNotBlock(t *testing.T) { + decision := DecideSignatureCompatibility(SignatureProviderKimi, observedFable5Sample, SignatureBlockKindClaudeThinking) + if decision.Compatible { + t.Fatalf("Claude signature reported compatible with a Kimi target") + } + if decision.Action != SignatureActionDropSignature { + t.Errorf("Action = %q, want %q", decision.Action, SignatureActionDropSignature) + } +} + +func TestDecideSignatureCompatibility_KimiPreservesNativeSignature(t *testing.T) { + native := synthesizeKimiSignature(t, 9709, 13) + decision := DecideSignatureCompatibility(SignatureProviderKimi, native, SignatureBlockKindClaudeThinking) + if !decision.Compatible { + t.Fatalf("Kimi-shaped signature reported incompatible with a Kimi target: %s", decision.Reason) + } + if decision.Action != SignatureActionPreserve { + t.Errorf("Action = %q, want %q", decision.Action, SignatureActionPreserve) + } + if decision.NormalizedSignature != native { + t.Errorf("NormalizedSignature was rewritten for a Kimi signature") + } +} + +// TestInspectKimiThinkingSignature_NativeCorpus validates the synthesized +// contract above against real harvested traffic when the corpus is available. +func TestInspectKimiThinkingSignature_NativeCorpus(t *testing.T) { + modes := map[KimiThinkingSignatureMode]int{} + for _, sig := range loadKimiCorpus(t) { + info, err := InspectKimiThinkingSignature(sig) + if err != nil { + t.Fatalf("native Kimi signature (len %d) rejected: %v", len(sig), err) + } + if got := DetectSignatureProvider(sig); got != SignatureProviderKimi { + t.Fatalf("DetectSignatureProvider = %q, want %q", got, SignatureProviderKimi) + } + modes[info.Mode]++ + } + if modes[KimiThinkingSignatureModeNonStreaming] == 0 || modes[KimiThinkingSignatureModeStreaming] == 0 { + t.Fatalf("corpus does not cover both modes: %v", modes) + } +} + +// TestDetectSignatureProvider_KimiProbeDoesNotDisturbCatalog replays the whole +// cross-provider catalog to prove the size probe changed nothing for the +// self-describing families and never claims a Grok payload. +func TestDetectSignatureProvider_KimiProbeDoesNotDisturbCatalog(t *testing.T) { + path, ok := masterSignatureCatalogPath() + if !ok { + t.Skip("signature catalog missing; see .agents/skills/cpa-signature-catalog-and-collection") + } + raw, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read signature catalog: %v", err) + } + var doc struct { + Records []struct { + FullSignature string `json:"full_signature"` + ClaimedProvider string `json:"claimed_provider"` + } `json:"records"` + } + if err := json.Unmarshal(raw, &doc); err != nil { + t.Fatalf("parse signature catalog: %v", err) + } + + matrix := map[string]map[SignatureProvider]int{} + for _, record := range doc.Records { + if record.FullSignature == "" { + continue + } + detected := DetectSignatureProvider(record.FullSignature) + if matrix[record.ClaimedProvider] == nil { + matrix[record.ClaimedProvider] = map[SignatureProvider]int{} + } + matrix[record.ClaimedProvider][detected]++ + } + if len(matrix) == 0 { + t.Skip("signature catalog has no usable records") + } + + for claimed, row := range matrix { + t.Logf("%-8s -> %v", claimed, row) + if captured := row[SignatureProviderKimi]; captured > 0 { + t.Errorf("%d %s signatures captured by the Kimi size probe", captured, claimed) + } + } + // xAI stays in the residual class by contract: its ciphertext carries no + // envelope and no fixed length, so any positive claim would also capture + // unrelated opaque payloads. + for detected, count := range matrix["grok"] { + if detected != SignatureProviderUnknown { + t.Errorf("%d grok signatures classified as %q, want %q", count, detected, SignatureProviderUnknown) + } + } +} diff --git a/internal/signature/provider_compatibility.go b/internal/signature/provider_compatibility.go index 437868e0..79d7530a 100644 --- a/internal/signature/provider_compatibility.go +++ b/internal/signature/provider_compatibility.go @@ -10,6 +10,9 @@ const ( SignatureProviderGemini SignatureProvider = "gemini" SignatureProviderGeminiBypass SignatureProvider = "gemini_bypass" SignatureProviderGPT SignatureProvider = "gpt" + // SignatureProviderKimi is identified by fixed signature size rather than by + // an envelope. See kimi_validation.go for the empirical basis and its limits. + SignatureProviderKimi SignatureProvider = "kimi" ) type SignatureBlockKind string @@ -59,6 +62,11 @@ func SignatureProviderFromModelName(modelName string) SignatureProvider { strings.HasPrefix(lower, "o3"), strings.HasPrefix(lower, "o4"): return SignatureProviderGPT + case strings.Contains(lower, "kimi"), + strings.Contains(lower, "moonshot"), + strings.HasPrefix(lower, "k2"), + strings.HasPrefix(lower, "k3"): + return SignatureProviderKimi default: return SignatureProviderUnknown } @@ -170,10 +178,6 @@ func DetectSignatureProviderForBlock(rawSignature string, blockKind SignatureBlo if IsGeminiThoughtSignatureBypass(sig) { return SignatureProviderGeminiBypass } - if !maybeSelfDescribingSignatureEnvelope(sig) { - return SignatureProviderUnknown - } - // 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. @@ -188,17 +192,33 @@ func DetectSignatureProviderForBlock(rawSignature string, blockKind SignatureBlo // separable in either order. TestGeminiEnvelopeNeverClaimsClaudeSignatures // pins that invariant so a looser Gemini envelope check cannot make the // order silently start mattering. - if IsValidGPTReasoningSignature(sig) { - return SignatureProviderGPT - } - if IsValidClaudeCAISSignature(sig) { - return SignatureProviderClaude - } - if IsValidClaudeThinkingSignature(sig, ClaudeSignatureValidationOptions{Strict: true}) { - return SignatureProviderClaude + // + // The envelope pre-filter gates only the envelope probes. A blob that cannot + // be an envelope skips straight to the size probe below rather than returning + // early, because Kimi's uniformly distributed base64 starts with one of + // "CERg" about 6% of the time and would otherwise be dropped by whichever + // side of the gate it happened to land on. + if maybeSelfDescribingSignatureEnvelope(sig) { + if IsValidGPTReasoningSignature(sig) { + return SignatureProviderGPT + } + if IsValidClaudeCAISSignature(sig) { + return SignatureProviderClaude + } + if IsValidClaudeThinkingSignature(sig, ClaudeSignatureValidationOptions{Strict: true}) { + return SignatureProviderClaude + } + if isRecognizedGeminiProviderSignature(sig, blockKind) { + return SignatureProviderGemini + } } - if isRecognizedGeminiProviderSignature(sig, blockKind) { - return SignatureProviderGemini + // Kimi carries no envelope, so it can only be claimed once every + // self-describing probe above has declined. Ordering it last means a length + // coincidence can never capture another provider's signature, and a future + // drift in Kimi's sizes costs Kimi its own identification rather than + // corrupting a neighbouring family. + if IsValidKimiThinkingSignature(sig) { + return SignatureProviderKimi } return SignatureProviderUnknown } @@ -254,6 +274,15 @@ func DecideSignatureCompatibilityForModel(targetProvider SignatureProvider, targ case SignatureProviderGPT: decision.Action = SignatureActionDropBlock decision.Reason = "GPT reasoning encrypted_content cannot be synthesized from another provider signature" + 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 + // back: a mutated, truncated, non-base64 or absent signature all return + // 200, because reasoning continuity there travels in OpenAI-style + // reasoning_content instead. Dropping the block would discard recoverable + // thinking text for no upstream benefit, so drop only the signature. + decision.Action = SignatureActionDropSignature + decision.Reason = "Kimi does not validate replayed thinking signatures, so the block survives without one" default: decision.Action = SignatureActionNoCompatibleReplacement decision.Reason = "unknown target provider" @@ -372,6 +401,8 @@ func signatureProviderMatchesTarget(target, detected SignatureProvider) bool { return detected == SignatureProviderClaude case SignatureProviderGPT: return detected == SignatureProviderGPT + case SignatureProviderKimi: + return detected == SignatureProviderKimi default: return false } @@ -400,6 +431,10 @@ func normalizeCompatibleSignatureForProvider(targetProvider SignatureProvider, r if IsValidGPTReasoningSignature(payload) { return payload } + case SignatureProviderKimi: + if IsValidKimiThinkingSignature(payload) { + return payload + } } return "" } -- knotmirror 0.0.1 From 4abb0e66026ce905025465bf0e5d32ea266abb0d Mon Sep 17 00:00:00 2001 From: sususu Date: Fri, 7 Aug 2026 16:39:07 +0800 Subject: [PATCH 09/17] feat(signature): add Grok as a target-only provider family xAI had no SignatureProvider value, so a Grok target could not be named in a compatibility decision and fell through to the unknown-target branch, which reports no compatible replacement rather than the correct policy. SignatureProviderGrok is deliberately target-only and DetectSignatureProvider never returns it. xAI ciphertext carries no envelope, no version byte and no fixed length, and is statistically indistinguishable from uniform random bytes, so any positive detection rule would also claim every other opaque high-entropy payload. Callers establish an xAI target from the model or route, then use InspectGrokEncryptedContent as a replay-safety shape check. A test replays the native corpus to pin that detection keeps returning unknown. A Grok target drops the whole block when the signature does not match, in contrast to Kimi: xAI decrypts encrypted_content on replay and rejects foreign or mutated blobs with 400, so the block cannot survive by shedding just its signature. Also correct the recorded floor for MinGrokEncryptedContentDecodedLen. The comment claimed the shortest observed native payload was exactly 50 bytes, which read like a protocol minimum; a later capture from grok-4.5 and grok-composer-2.5-fast reached 43 and 48 bytes. The observed minimum keeps sliding, which is the argument for keeping the constant well below it and letting the entropy check filter. The value is unchanged. --- internal/signature/grok_validation.go | 15 +++--- internal/signature/grok_validation_test.go | 57 ++++++++++++++++++++ internal/signature/provider_compatibility.go | 18 +++++++ 3 files changed, 84 insertions(+), 6 deletions(-) diff --git a/internal/signature/grok_validation.go b/internal/signature/grok_validation.go index ba09ad94..8b424ac3 100644 --- a/internal/signature/grok_validation.go +++ b/internal/signature/grok_validation.go @@ -11,12 +11,15 @@ import ( const ( // MaxGrokEncryptedContentLen is a transport safety cap for opaque replay blobs. MaxGrokEncryptedContentLen = 8 * 1024 * 1024 - // MinGrokEncryptedContentDecodedLen is a deliberately loose floor. The - // shortest observed native payload is exactly 50 bytes (grok-composer-2.5-fast), - // and those samples share no structure, so 50 is a sampling artifact rather - // than a protocol minimum. Sitting on the observed floor would silently reject - // a future shorter payload and surface as lost reasoning context, so keep - // headroom here and let the entropy check do the real filtering. + // MinGrokEncryptedContentDecodedLen is a deliberately loose floor, and the + // headroom has already proven necessary. An earlier corpus of 207 samples put + // the shortest native payload at exactly 50 bytes, with several samples piled + // on that value, which read like a protocol floor; a later 215-sample capture + // from grok-4.5 and grok-composer-2.5-fast reached 43 and 48 bytes and moved + // it. Both corpora agree there is no structure to anchor on, so the observed + // minimum is a sampling artifact that keeps sliding, and sitting on it would + // silently reject a future shorter payload as lost reasoning context. Keep the + // floor low and let the entropy check do the real filtering. MinGrokEncryptedContentDecodedLen = 32 // MinGrokEncryptedContentEntropyRatio rejects obvious non-ciphertext payloads. // Native samples are >= 0.892 against the sample-size entropy ceiling. diff --git a/internal/signature/grok_validation_test.go b/internal/signature/grok_validation_test.go index ca3383d8..1c9a2d72 100644 --- a/internal/signature/grok_validation_test.go +++ b/internal/signature/grok_validation_test.go @@ -333,3 +333,60 @@ func grokEncryptedContentSamplesPath() (string, bool) { } return path, true } + +func TestSignatureProviderFromModelName_Grok(t *testing.T) { + for _, model := range []string{"grok-4.5", "grok-4.5-build", "grok-composer-2.5-fast", "grok-code-fast-1"} { + t.Run(model, func(t *testing.T) { + if got := SignatureProviderFromModelName(model); got != SignatureProviderGrok { + t.Errorf("SignatureProviderFromModelName(%q) = %q, want %q", model, got, SignatureProviderGrok) + } + }) + } +} + +// TestDetectSignatureProvider_NeverClassifiesGrok pins the contract that xAI is +// a target-only family. Its ciphertext carries no envelope, no version byte and +// no fixed length, so a positive detection rule would necessarily also claim +// unrelated opaque payloads. Callers establish an xAI target from provenance and +// then use InspectGrokEncryptedContent as a replay-safety check. +func TestDetectSignatureProvider_NeverClassifiesGrok(t *testing.T) { + path, ok := grokEncryptedContentSamplesPath() + if !ok { + t.Skip("grok encrypted_content corpus missing; run docs/native-prompt-capture/scripts/harvest-grok-encrypted-content.sh") + } + raw, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read grok corpus: %v", err) + } + var samples []string + if err := json.Unmarshal(raw, &samples); err != nil { + var wrapped struct { + Samples []string `json:"samples"` + } + if err := json.Unmarshal(raw, &wrapped); err != nil { + t.Fatalf("parse grok corpus: %v", err) + } + samples = wrapped.Samples + } + if len(samples) == 0 { + t.Skip("grok encrypted_content corpus is empty") + } + for _, sig := range samples { + if got := DetectSignatureProvider(sig); got != SignatureProviderUnknown { + t.Fatalf("DetectSignatureProvider = %q, want %q for native encrypted_content", got, SignatureProviderUnknown) + } + } +} + +// TestDecideSignatureCompatibility_GrokDropsBlock contrasts with the Kimi +// policy: xAI decrypts the blob and answers 400 for foreign or mutated input, so +// an incompatible block cannot survive by shedding just its signature. +func TestDecideSignatureCompatibility_GrokDropsBlock(t *testing.T) { + decision := DecideSignatureCompatibility(SignatureProviderGrok, observedFable5Sample, SignatureBlockKindUnknown) + if decision.Compatible { + t.Fatalf("Claude signature reported compatible with a Grok target") + } + if decision.Action != SignatureActionDropBlock { + t.Errorf("Action = %q, want %q", decision.Action, SignatureActionDropBlock) + } +} diff --git a/internal/signature/provider_compatibility.go b/internal/signature/provider_compatibility.go index 79d7530a..2eba2cc1 100644 --- a/internal/signature/provider_compatibility.go +++ b/internal/signature/provider_compatibility.go @@ -13,6 +13,13 @@ const ( // SignatureProviderKimi is identified by fixed signature size rather than by // an envelope. See kimi_validation.go for the empirical basis and its limits. SignatureProviderKimi SignatureProvider = "kimi" + // SignatureProviderGrok is a target-only family. DetectSignatureProvider never + // returns it: xAI emits no envelope, no version byte and no fixed length, and + // its ciphertext is statistically indistinguishable from uniform random bytes, + // so any positive claim would also capture every other opaque blob. Grok + // handling is provenance-first - establish the target from the model or route, + // then use InspectGrokEncryptedContent as a replay-safety shape check. + SignatureProviderGrok SignatureProvider = "grok" ) type SignatureBlockKind string @@ -67,6 +74,8 @@ func SignatureProviderFromModelName(modelName string) SignatureProvider { strings.HasPrefix(lower, "k2"), strings.HasPrefix(lower, "k3"): return SignatureProviderKimi + case strings.Contains(lower, "grok"): + return SignatureProviderGrok default: return SignatureProviderUnknown } @@ -283,6 +292,12 @@ func DecideSignatureCompatibilityForModel(targetProvider SignatureProvider, targ // thinking text for no upstream benefit, so drop only the signature. decision.Action = SignatureActionDropSignature decision.Reason = "Kimi does not validate replayed thinking signatures, so the block survives without one" + case SignatureProviderGrok: + // xAI decrypts encrypted_content and rejects the request with 400 + // "Could not decrypt" when the blob is foreign or mutated, so a + // non-matching value has to leave with the block. + decision.Action = SignatureActionDropBlock + decision.Reason = "xAI verifies encrypted_content on replay and rejects foreign or mutated blobs" default: decision.Action = SignatureActionNoCompatibleReplacement decision.Reason = "unknown target provider" @@ -404,6 +419,9 @@ func signatureProviderMatchesTarget(target, detected SignatureProvider) bool { case SignatureProviderKimi: return detected == SignatureProviderKimi default: + // SignatureProviderGrok is deliberately absent. Detection never yields it, + // so a Grok target must decide replay safety from provenance plus + // InspectGrokEncryptedContent rather than from a detected-provider match. return false } } -- knotmirror 0.0.1 From 197f520426374e514218ed155933ac546c98d345 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Sat, 8 Aug 2026 23:24:49 +0800 Subject: [PATCH 10/17] fix(codex): normalize `custom_tool_call` IDs with `ctc_` prefix during Codex input sanitization --- internal/runtime/executor/helps/codex_input_ids.go | 11 +++++++---- .../runtime/executor/helps/codex_input_ids_test.go | 10 ++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/internal/runtime/executor/helps/codex_input_ids.go b/internal/runtime/executor/helps/codex_input_ids.go index 1ee2bac4..05c011e9 100644 --- a/internal/runtime/executor/helps/codex_input_ids.go +++ b/internal/runtime/executor/helps/codex_input_ids.go @@ -12,10 +12,11 @@ import ( ) const ( - codexInputItemIDLimit = 64 - codexMessageItemIDPrefix = "msg" - codexReasoningItemIDPrefix = "rs" - codexFunctionCallItemIDPrefix = "fc" + codexInputItemIDLimit = 64 + codexMessageItemIDPrefix = "msg" + codexReasoningItemIDPrefix = "rs" + codexFunctionCallItemIDPrefix = "fc" + codexCustomToolCallItemIDPrefix = "ctc" ) // SanitizeCodexInputItemIDs normalizes supported input item IDs for Codex, removes encrypted @@ -103,6 +104,8 @@ func normalizeCodexInputItemID(item gjson.Result, id string) string { prefix = codexReasoningItemIDPrefix case "function_call": prefix = codexFunctionCallItemIDPrefix + case "custom_tool_call": + prefix = codexCustomToolCallItemIDPrefix default: return id } diff --git a/internal/runtime/executor/helps/codex_input_ids_test.go b/internal/runtime/executor/helps/codex_input_ids_test.go index c9f4434e..ba9cb92f 100644 --- a/internal/runtime/executor/helps/codex_input_ids_test.go +++ b/internal/runtime/executor/helps/codex_input_ids_test.go @@ -94,6 +94,16 @@ func TestSanitizeCodexInputItemIDsNormalizesResponseItemIDs(t *testing.T) { } } +func TestSanitizeCodexInputItemIDsNormalizesCustomToolCallIDs(t *testing.T) { + const invalidID = "item_44e13caebc1ddf25f1337cbe" + body := []byte(`{"input":[{"type":"custom_tool_call","id":"` + invalidID + `","call_id":"call-1","name":"lookup","input":"{}"}]}`) + + got := SanitizeCodexInputItemIDs(body) + if actual := gjson.GetBytes(got, "input.0.id").String(); actual != "ctc_"+invalidID { + t.Fatalf("custom_tool_call ID = %q, want ctc-prefixed ID", actual) + } +} + func TestSanitizeCodexInputItemIDsDropsOverlongEncryptedReasoningItem(t *testing.T) { longReasoningID := "rs_" + strings.Repeat("a", 64) shortReasoningID := "rs_" + strings.Repeat("b", 48) -- knotmirror 0.0.1 From ea8882bf26b2a84f4c1880f36126fef40baf4d7e Mon Sep 17 00:00:00 2001 From: sususu Date: Sat, 8 Aug 2026 10:53:58 +0800 Subject: [PATCH 11/17] Add a no-copy GJSON parse helper gjson.ParseBytes copies the entire document before returning a result, which is prohibitive for multi-megabyte requests. ParseGJSONBytesNoCopy mirrors the existing GetGJSONBytesNoCopy contract: the returned result references the caller's bytes, so callers must neither retain it nor mutate the buffer while it is in use. --- internal/util/gjson.go | 11 +++++++++++ internal/util/gjson_test.go | 30 +++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/internal/util/gjson.go b/internal/util/gjson.go index cd6fd7c7..840cf768 100644 --- a/internal/util/gjson.go +++ b/internal/util/gjson.go @@ -14,3 +14,14 @@ func GetGJSONBytesNoCopy(data []byte, path string) gjson.Result { } return gjson.Get(unsafe.String(unsafe.SliceData(data), len(data)), path) } + +// ParseGJSONBytesNoCopy parses data into a GJSON result that references data +// directly. gjson.ParseBytes copies the whole document, which is prohibitive +// for multi-megabyte payloads. Callers must not retain the result or mutate +// data while using it. +func ParseGJSONBytesNoCopy(data []byte) gjson.Result { + if len(data) == 0 { + return gjson.Result{} + } + return gjson.Parse(unsafe.String(unsafe.SliceData(data), len(data))) +} diff --git a/internal/util/gjson_test.go b/internal/util/gjson_test.go index 8ce36958..b0f03b31 100644 --- a/internal/util/gjson_test.go +++ b/internal/util/gjson_test.go @@ -1,6 +1,9 @@ package util -import "testing" +import ( + "testing" + "unsafe" +) func TestGetGJSONBytesNoCopy(t *testing.T) { input := []byte(`{"request":{"contents":[{"role":"user"}]}}`) @@ -15,3 +18,28 @@ func TestGetGJSONBytesNoCopyEmptyInput(t *testing.T) { t.Fatalf("empty input result = %s, want missing", result.Raw) } } + +func TestParseGJSONBytesNoCopy(t *testing.T) { + input := []byte(`{"request":{"contents":[{"role":"user"}]}}`) + root := ParseGJSONBytesNoCopy(input) + if !root.IsObject() || root.Get("request.contents.0.role").String() != "user" { + t.Fatalf("parsed root = %s, want user content array", root.Raw) + } +} + +func TestParseGJSONBytesNoCopyReferencesInput(t *testing.T) { + input := []byte(`{"contents":[{"role":"user"}]}`) + root := ParseGJSONBytesNoCopy(input) + if len(root.Raw) != len(input) { + t.Fatalf("raw length = %d, want %d", len(root.Raw), len(input)) + } + if unsafe.StringData(root.Raw) != unsafe.SliceData(input) { + t.Fatal("parsed result copied the input instead of referencing it") + } +} + +func TestParseGJSONBytesNoCopyEmptyInput(t *testing.T) { + if result := ParseGJSONBytesNoCopy(nil); result.Exists() { + t.Fatalf("empty input result = %s, want missing", result.Raw) + } +} -- knotmirror 0.0.1 From b7a441522a904270afcb41761c81b8682884406c Mon Sep 17 00:00:00 2001 From: sususu Date: Sat, 8 Aug 2026 10:53:58 +0800 Subject: [PATCH 12/17] Reduce Antigravity request amplification for large payloads A single 8 MiB inlineData request allocated about 480 MB end to end. The Gemini-to-Antigravity translator was responsible for the largest share: - The entry point converted the payload between string and []byte, so the whole request was copied before any rewrite happened. - The contents array was rebuilt part by part even when every role was already valid and no function name needed rewriting. - The envelope was built by splicing the payload into the template first and then setting the model, which copied the payload a second time. - fixCLIToolResponse took and returned a string, so even its no-op path copied the request twice. Keep the payload as []byte throughout, pre-scan for roles and function names before deciding to rebuild, fill the small envelope before splicing the payload in once, and switch fixCLIToolResponse to []byte with a fast path that returns the input unchanged. Benchmarked on an 8 MiB inlineData request: 84.0 MB/op to 16.8 MB/op and 44.9 ms/op to 39.3 ms/op. Outputs were compared byte for byte against the previous implementation across 5166 generated and hand-built requests. --- .../gemini/antigravity_gemini_request.go | 120 ++++++++++++------ .../gemini/antigravity_gemini_request_test.go | 24 ++-- .../gemini/noop_optimization_test.go | 47 ++++++- 3 files changed, 135 insertions(+), 56 deletions(-) diff --git a/internal/translator/antigravity/gemini/antigravity_gemini_request.go b/internal/translator/antigravity/gemini/antigravity_gemini_request.go index 925cfa6d..ff388157 100644 --- a/internal/translator/antigravity/gemini/antigravity_gemini_request.go +++ b/internal/translator/antigravity/gemini/antigravity_gemini_request.go @@ -39,32 +39,32 @@ import ( func ConvertGeminiRequestToAntigravity(modelName string, inputRawJSON []byte, _ bool) []byte { rawJSON := inputRawJSON functionNameMap := util.SanitizedFunctionNameMap(inputRawJSON) - template := `{"project":"","request":{},"model":""}` - templateBytes, _ := sjson.SetRawBytes([]byte(template), "request", rawJSON) - templateBytes, _ = sjson.SetBytes(templateBytes, "model", modelName) - template = string(templateBytes) - if gjson.Get(template, "request.model").Exists() { - template, _ = sjson.Delete(template, "request.model") + // Keep the envelope in []byte form. Round-tripping through string copies the + // entire request, which dominates allocations for large inline data. Fill the + // small envelope fields first so the payload is only spliced in once. + envelope, _ := sjson.SetBytes([]byte(`{"project":"","request":{},"model":""}`), "model", modelName) + rawJSON, _ = sjson.SetRawBytes(envelope, "request", rawJSON) + if util.GetGJSONBytesNoCopy(rawJSON, "request.model").Exists() { + rawJSON, _ = sjson.DeleteBytes(rawJSON, "request.model") } - template, errFixCLIToolResponse := fixCLIToolResponse(template) + fixedJSON, errFixCLIToolResponse := fixCLIToolResponse(rawJSON) if errFixCLIToolResponse != nil { return []byte{} } + rawJSON = fixedJSON - systemInstructionResult := gjson.Get(template, "request.system_instruction") - if systemInstructionResult.Exists() { - templateBytes, _ = sjson.SetRawBytes([]byte(template), "request.systemInstruction", []byte(systemInstructionResult.Raw)) - template = string(templateBytes) - template, _ = sjson.Delete(template, "request.system_instruction") + if systemInstructionResult := util.GetGJSONBytesNoCopy(rawJSON, "request.system_instruction"); systemInstructionResult.Exists() { + rawJSON, _ = sjson.SetRawBytes(rawJSON, "request.systemInstruction", []byte(systemInstructionResult.Raw)) + rawJSON, _ = sjson.DeleteBytes(rawJSON, "request.system_instruction") } - rawJSON = []byte(template) - // Normalize roles in request.contents: default to valid values if missing/invalid - contents := gjson.GetBytes(rawJSON, "request.contents") - if contents.IsArray() { + // Normalize roles in request.contents: default to valid values if missing/invalid. + // The contents array is only materialized when a role actually changes; copying + // every content up front duplicates the whole payload for large inline data. + contents := util.GetGJSONBytesNoCopy(rawJSON, "request.contents") + if contents.IsArray() && geminiContentRolesNeedNormalization(contents) { contentItems := translatorcommon.NewRawArrayItems(contents.Get("#").Int()) - rolesChanged := false previousRole := "" contents.ForEach(func(_, value gjson.Result) bool { role := value.Get("role").String() @@ -76,18 +76,15 @@ func ConvertGeminiRequestToAntigravity(modelName string, inputRawJSON []byte, _ role = "model" } content, _ = sjson.SetBytes(content, "role", role) - rolesChanged = true } previousRole = role contentItems = append(contentItems, content) return true }) - if rolesChanged { - rawJSON, _ = sjson.SetRawBytes(rawJSON, "request.contents", translatorcommon.JoinRawArray(contentItems)) - } + rawJSON, _ = sjson.SetRawBytes(rawJSON, "request.contents", translatorcommon.JoinRawArray(contentItems)) } - toolsResult := gjson.GetBytes(rawJSON, "request.tools") + toolsResult := util.GetGJSONBytesNoCopy(rawJSON, "request.tools") if toolsResult.IsArray() { seenFunctionNames := make(map[string]struct{}) toolsChanged := false @@ -158,8 +155,23 @@ func ConvertGeminiRequestToAntigravity(modelName string, inputRawJSON []byte, _ return common.AttachDefaultSafetySettings(rawJSON, "request.safetySettings") } +// geminiContentRolesNeedNormalization reports whether any content role is missing +// or invalid and therefore requires rebuilding the contents array. +func geminiContentRolesNeedNormalization(contents gjson.Result) bool { + needsNormalization := false + contents.ForEach(func(_, value gjson.Result) bool { + role := value.Get("role").String() + if role != "user" && role != "model" { + needsNormalization = true + return false + } + return true + }) + return needsNormalization +} + func removeEmptyGeminiFunctionTools(rawJSON []byte) []byte { - tools := gjson.GetBytes(rawJSON, "request.tools") + tools := util.GetGJSONBytesNoCopy(rawJSON, "request.tools") if tools.IsArray() && len(tools.Array()) == 0 { rawJSON, _ = sjson.DeleteBytes(rawJSON, "request.tools") return rawJSON @@ -175,7 +187,7 @@ func removeEmptyGeminiFunctionTools(rawJSON []byte) []byte { changed = true } } - if len(gjson.ParseBytes(toolJSON).Map()) == 0 { + if len(util.ParseGJSONBytesNoCopy(toolJSON).Map()) == 0 { changed = true continue } @@ -193,8 +205,36 @@ func removeEmptyGeminiFunctionTools(rawJSON []byte) []byte { return rawJSON } +// geminiFunctionNameFields lists the part fields that can carry a function name. +var geminiFunctionNameFields = []string{"functionCall", "functionResponse", "function_call", "function_response"} + +// geminiFunctionNamesNeedRewrite reports whether any part carries a function name +// that must be remapped or coerced to a string. +func geminiFunctionNamesNeedRewrite(contents gjson.Result, functionNameMap map[string]string) bool { + needsRewrite := false + contents.ForEach(func(_, content gjson.Result) bool { + content.Get("parts").ForEach(func(_, part gjson.Result) bool { + for _, field := range geminiFunctionNameFields { + nameResult := part.Get(field + ".name") + name := nameResult.String() + if name == "" { + continue + } + if nameResult.Type == gjson.String && util.MapSanitizedFunctionName(functionNameMap, name) == name { + continue + } + needsRewrite = true + return false + } + return true + }) + return !needsRewrite + }) + return needsRewrite +} + func rewriteGeminiFunctionNames(rawJSON []byte, functionNameMap map[string]string) []byte { - contents := gjson.GetBytes(rawJSON, "request.contents") + contents := util.GetGJSONBytesNoCopy(rawJSON, "request.contents") canBatchContents := contents.IsArray() if canBatchContents { contents.ForEach(func(_, content gjson.Result) bool { @@ -206,8 +246,9 @@ func rewriteGeminiFunctionNames(rawJSON []byte, functionNameMap map[string]strin return true }) } - if canBatchContents { - contentsChanged := false + // Rebuilding the contents array copies every content and part, so only pay for + // it once a name actually needs rewriting. + if canBatchContents && geminiFunctionNamesNeedRewrite(contents, functionNameMap) { contentItems := translatorcommon.NewRawArrayItems(contents.Get("#").Int()) contents.ForEach(func(_, content gjson.Result) bool { contentJSON := []byte(content.Raw) @@ -215,7 +256,7 @@ func rewriteGeminiFunctionNames(rawJSON []byte, functionNameMap map[string]strin partItems := make([][]byte, 0, 4) content.Get("parts").ForEach(func(_, part gjson.Result) bool { partJSON := []byte(part.Raw) - for _, field := range []string{"functionCall", "functionResponse", "function_call", "function_response"} { + for _, field := range geminiFunctionNameFields { nameResult := part.Get(field + ".name") name := nameResult.String() if name == "" { @@ -233,18 +274,15 @@ func rewriteGeminiFunctionNames(rawJSON []byte, functionNameMap map[string]strin }) if partsChanged { contentJSON, _ = sjson.SetRawBytes(contentJSON, "parts", translatorcommon.JoinRawArray(partItems)) - contentsChanged = true } contentItems = append(contentItems, contentJSON) return true }) - if contentsChanged { - rawJSON, _ = sjson.SetRawBytes(rawJSON, "request.contents", translatorcommon.JoinRawArray(contentItems)) - } - } else { + rawJSON, _ = sjson.SetRawBytes(rawJSON, "request.contents", translatorcommon.JoinRawArray(contentItems)) + } else if !canBatchContents { for contentIndex, content := range contents.Array() { for partIndex, part := range content.Get("parts").Array() { - for _, field := range []string{"functionCall", "functionResponse", "function_call", "function_response"} { + for _, field := range geminiFunctionNameFields { nameResult := part.Get(field + ".name") name := nameResult.String() if name == "" { @@ -265,7 +303,7 @@ func rewriteGeminiFunctionNames(rawJSON []byte, functionNameMap map[string]strin "request.toolConfig.functionCallingConfig.allowedFunctionNames", "request.tool_config.function_calling_config.allowed_function_names", } { - allowedNames := gjson.GetBytes(rawJSON, allowedPath) + allowedNames := util.GetGJSONBytesNoCopy(rawJSON, allowedPath) if allowedNames.IsArray() { namesChanged := false nameItems := make([][]byte, 0, 4) @@ -551,9 +589,11 @@ func parseFunctionResponseRaw(response gjson.Result, fallbackName string) string // Returns: // - string: The processed JSON string with grouped function calls and responses // - error: An error if the processing fails -func fixCLIToolResponse(input string) (string, error) { - // Parse the input JSON to extract the conversation structure - parsed := gjson.Parse(input) +func fixCLIToolResponse(input []byte) ([]byte, error) { + // Parse the input JSON to extract the conversation structure. + // The parsed result references input directly; input must not be mutated + // while the result and its raw slices are still in use. + parsed := util.ParseGJSONBytesNoCopy(input) // Extract the contents array which contains the conversation messages contents := parsed.Get("request.contents") @@ -690,7 +730,7 @@ func fixCLIToolResponse(input string) (string, error) { } // Update the original JSON with the new contents - result, _ := sjson.SetRawBytes([]byte(input), "request.contents", translatorcommon.JoinRawArray(contentItems)) + result, _ := sjson.SetRawBytes(input, "request.contents", translatorcommon.JoinRawArray(contentItems)) - return string(result), nil + return result, nil } diff --git a/internal/translator/antigravity/gemini/antigravity_gemini_request_test.go b/internal/translator/antigravity/gemini/antigravity_gemini_request_test.go index a10e2c9a..11136a3f 100644 --- a/internal/translator/antigravity/gemini/antigravity_gemini_request_test.go +++ b/internal/translator/antigravity/gemini/antigravity_gemini_request_test.go @@ -332,13 +332,13 @@ func TestFixCLIToolResponse_PreservesFunctionResponseParts(t *testing.T) { } }` - result, err := fixCLIToolResponse(input) + result, err := fixCLIToolResponse([]byte(input)) if err != nil { t.Fatalf("fixCLIToolResponse failed: %v", err) } // Find the function response content (role=function) - contents := gjson.Get(result, "request.contents").Array() + contents := gjson.GetBytes(result, "request.contents").Array() var funcContent gjson.Result for _, c := range contents { if c.Get("role").String() == "function" { @@ -396,12 +396,12 @@ func TestFixCLIToolResponse_BackfillsEmptyFunctionResponseName(t *testing.T) { } }` - result, err := fixCLIToolResponse(input) + result, err := fixCLIToolResponse([]byte(input)) if err != nil { t.Fatalf("fixCLIToolResponse failed: %v", err) } - contents := gjson.Get(result, "request.contents").Array() + contents := gjson.GetBytes(result, "request.contents").Array() var funcContent gjson.Result for _, c := range contents { if c.Get("role").String() == "function" { @@ -443,12 +443,12 @@ func TestFixCLIToolResponse_BackfillsMultipleEmptyNames(t *testing.T) { } }` - result, err := fixCLIToolResponse(input) + result, err := fixCLIToolResponse([]byte(input)) if err != nil { t.Fatalf("fixCLIToolResponse failed: %v", err) } - contents := gjson.Get(result, "request.contents").Array() + contents := gjson.GetBytes(result, "request.contents").Array() var funcContent gjson.Result for _, c := range contents { if c.Get("role").String() == "function" { @@ -497,12 +497,12 @@ func TestFixCLIToolResponse_PreservesExistingName(t *testing.T) { } }` - result, err := fixCLIToolResponse(input) + result, err := fixCLIToolResponse([]byte(input)) if err != nil { t.Fatalf("fixCLIToolResponse failed: %v", err) } - contents := gjson.Get(result, "request.contents").Array() + contents := gjson.GetBytes(result, "request.contents").Array() var funcContent gjson.Result for _, c := range contents { if c.Get("role").String() == "function" { @@ -543,12 +543,12 @@ func TestFixCLIToolResponse_MoreResponsesThanCalls(t *testing.T) { } }` - result, err := fixCLIToolResponse(input) + result, err := fixCLIToolResponse([]byte(input)) if err != nil { t.Fatalf("fixCLIToolResponse failed: %v", err) } - contents := gjson.Get(result, "request.contents").Array() + contents := gjson.GetBytes(result, "request.contents").Array() var funcContent gjson.Result for _, c := range contents { if c.Get("role").String() == "function" { @@ -601,12 +601,12 @@ func TestFixCLIToolResponse_MultipleGroupsFIFO(t *testing.T) { } }` - result, err := fixCLIToolResponse(input) + result, err := fixCLIToolResponse([]byte(input)) if err != nil { t.Fatalf("fixCLIToolResponse failed: %v", err) } - contents := gjson.Get(result, "request.contents").Array() + contents := gjson.GetBytes(result, "request.contents").Array() var funcContents []gjson.Result for _, c := range contents { if c.Get("role").String() == "function" { diff --git a/internal/translator/antigravity/gemini/noop_optimization_test.go b/internal/translator/antigravity/gemini/noop_optimization_test.go index 16ea1317..a5e33f64 100644 --- a/internal/translator/antigravity/gemini/noop_optimization_test.go +++ b/internal/translator/antigravity/gemini/noop_optimization_test.go @@ -1,6 +1,8 @@ package gemini import ( + "runtime" + "strings" "testing" "github.com/tidwall/gjson" @@ -50,25 +52,62 @@ func TestRewriteGeminiFunctionNamesNormalizesNonStringNames(t *testing.T) { } func TestFixCLIToolResponseReusesHistoryWithoutFunctionResponses(t *testing.T) { - input := `{"request":{"contents":[{"role":"user","parts":[{"text":"hello"}]},{"role":"model","parts":[{"text":"world"}]}]}}` + input := []byte(`{"request":{"contents":[{"role":"user","parts":[{"text":"hello"}]},{"role":"model","parts":[{"text":"world"}]}]}}`) output, errFix := fixCLIToolResponse(input) if errFix != nil { t.Fatalf("fixCLIToolResponse returned an error: %v", errFix) } - if output != input { + if string(output) != string(input) { t.Fatalf("history changed:\n got: %s\nwant: %s", output, input) } + if &output[0] != &input[0] { + t.Fatal("history without function responses caused a payload copy") + } } func TestFixCLIToolResponsePreservesObjectNormalization(t *testing.T) { - input := `{"request":{"contents":{"first":{"role":"user","parts":[{"text":"hello"}]}}}}` + input := []byte(`{"request":{"contents":{"first":{"role":"user","parts":[{"text":"hello"}]}}}}`) output, errFix := fixCLIToolResponse(input) if errFix != nil { t.Fatalf("fixCLIToolResponse returned an error: %v", errFix) } - if !gjson.Get(output, "request.contents").IsArray() { + if !gjson.GetBytes(output, "request.contents").IsArray() { t.Fatalf("contents should be normalized to an array: %s", output) } } + +// TestConvertGeminiRequestToAntigravityBoundsLargePayloadCopies keeps the number of +// full-payload copies bounded for large inline data. The assertions run directly in +// the test (not inside testing.Benchmark) so a regression fails loudly instead of +// being swallowed by a discarded benchmark result. +func TestConvertGeminiRequestToAntigravityBoundsLargePayloadCopies(t *testing.T) { + const inlineDataSize = 4 << 20 + input := []byte(`{"contents":[{"role":"user","parts":[{"inlineData":{"mimeType":"image/png","data":"` + + strings.Repeat("A", inlineDataSize) + `"}},{"text":"describe"}]}]}`) + + var before, after runtime.MemStats + runtime.GC() + runtime.ReadMemStats(&before) + output := ConvertGeminiRequestToAntigravity("gemini-3-flash", input, false) + runtime.ReadMemStats(&after) + + if got := gjson.GetBytes(output, "request.contents.0.parts.0.inlineData.data").String(); len(got) != inlineDataSize { + t.Fatalf("inline data length = %d, want %d", len(got), inlineDataSize) + } + if got := gjson.GetBytes(output, "model").String(); got != "gemini-3-flash" { + t.Fatalf("model = %q, want gemini-3-flash", got) + } + if got := gjson.GetBytes(output, "request.safetySettings"); !got.IsArray() { + t.Fatalf("request.safetySettings = %s, want array", got.Raw) + } + + // Wrapping the request in the Antigravity envelope and setting the model each + // allocate one payload-sized buffer; everything beyond that is a regression. + const allowedCopies = 3 + if allocated := after.TotalAlloc - before.TotalAlloc; allocated > allowedCopies*inlineDataSize { + t.Fatalf("conversion allocated %d bytes for a %d byte payload, want at most %d", + allocated, inlineDataSize, allowedCopies*inlineDataSize) + } +} -- knotmirror 0.0.1 From 99929209840ba6821925a821daf79361bcbc0595 Mon Sep 17 00:00:00 2001 From: sususu Date: Sat, 8 Aug 2026 10:53:58 +0800 Subject: [PATCH 13/17] Avoid copying large payloads in Antigravity reads gjson.GetBytes copies the matched subtree before returning it, so every read of request.contents on a multi-megabyte request allocated another copy of it. The reasoning replay, executor, signature validation and session identity paths all read whole arrays this way, and hasExplicitSession parsed the entire body with gjson.ParseBytes on every request. Use the no-copy helpers for these reads. Each call site only reads from the result and writes through sjson, which allocates a new buffer, so the payload stays immutable while the results are alive. --- .../runtime/executor/antigravity_executor.go | 11 +++--- .../executor/antigravity_reasoning_replay.go | 38 +++++++++---------- internal/signature/gemini_validation.go | 5 ++- sdk/cliproxy/session/identity.go | 5 ++- 4 files changed, 32 insertions(+), 27 deletions(-) diff --git a/internal/runtime/executor/antigravity_executor.go b/internal/runtime/executor/antigravity_executor.go index 02e01b95..4e9c59a6 100644 --- a/internal/runtime/executor/antigravity_executor.go +++ b/internal/runtime/executor/antigravity_executor.go @@ -18,6 +18,7 @@ import ( "github.com/router-for-me/CLIProxyAPI/v7/internal/runtime/executor/helps" internalsignature "github.com/router-for-me/CLIProxyAPI/v7/internal/signature" antigravityclaude "github.com/router-for-me/CLIProxyAPI/v7/internal/translator/antigravity/claude" + "github.com/router-for-me/CLIProxyAPI/v7/internal/util" cliproxyauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" sdktranslator "github.com/router-for-me/CLIProxyAPI/v7/sdk/translator" log "github.com/sirupsen/logrus" @@ -133,7 +134,7 @@ func sanitizeAntigravityGeminiRequestSignatures(modelName string, rawJSON []byte func normalizeAntigravityGeminiFunctionResponseRoles(rawJSON []byte) []byte { rawJSON = repairAntigravityGeminiFunctionResponseNames(rawJSON) - contents := gjson.GetBytes(rawJSON, "request.contents") + contents := util.GetGJSONBytesNoCopy(rawJSON, "request.contents") if !contents.IsArray() { return rawJSON } @@ -219,7 +220,7 @@ func normalizeAntigravityGeminiFunctionResponseRoles(rawJSON []byte) []byte { } func repairAntigravityGeminiFunctionResponseNames(rawJSON []byte) []byte { - contents := gjson.GetBytes(rawJSON, "request.contents") + contents := util.GetGJSONBytesNoCopy(rawJSON, "request.contents") if !contents.IsArray() { return rawJSON } @@ -301,7 +302,7 @@ func validateAntigravityRequestSignatures(ctx context.Context, modelName string, } func hasAntigravityClaudeTypedWebSearchTool(payload []byte) bool { - tools := gjson.GetBytes(payload, "tools") + tools := util.GetGJSONBytesNoCopy(payload, "tools") if !tools.IsArray() { return false } @@ -315,7 +316,7 @@ func hasAntigravityClaudeTypedWebSearchTool(payload []byte) bool { } func hasAntigravityGoogleSearchTool(payload []byte) bool { - tools := gjson.GetBytes(payload, "request.tools") + tools := util.GetGJSONBytesNoCopy(payload, "request.tools") if !tools.IsArray() { return false } @@ -341,7 +342,7 @@ func (e *AntigravityExecutor) resolveWebSearchGroundingURLs(ctx context.Context, } func countClaudeThinkingBlocks(rawJSON []byte) int { - messages := gjson.GetBytes(rawJSON, "messages") + messages := util.GetGJSONBytesNoCopy(rawJSON, "messages") if !messages.IsArray() { return 0 } diff --git a/internal/runtime/executor/antigravity_reasoning_replay.go b/internal/runtime/executor/antigravity_reasoning_replay.go index 51733113..3c2ea61e 100644 --- a/internal/runtime/executor/antigravity_reasoning_replay.go +++ b/internal/runtime/executor/antigravity_reasoning_replay.go @@ -37,7 +37,7 @@ func antigravityReplayLogKey(value string) string { // Claude-facing provenance IDs are still present in a Gemini-shaped payload. func antigravityCountClaudeToolProvenanceIDs(payload []byte) int { count := 0 - contents := gjson.GetBytes(payload, "request.contents") + contents := util.GetGJSONBytesNoCopy(payload, "request.contents") if !contents.IsArray() { return 0 } @@ -139,7 +139,7 @@ func antigravityReasoningReplayClientSessionKey(ctx context.Context, req cliprox } func antigravityClaudeReplaySystemLane(payload []byte) string { - system := gjson.GetBytes(payload, "system") + system := util.GetGJSONBytesNoCopy(payload, "system") if !system.Exists() { return "" } @@ -191,7 +191,7 @@ func antigravityReplaySessionIDFromPayload(payload []byte) string { } func antigravityReasoningReplayPendingModelContentIndex(payload []byte) (contentIndex int, basePartIndex int) { - contents := gjson.GetBytes(payload, "request.contents") + contents := util.GetGJSONBytesNoCopy(payload, "request.contents") if !contents.IsArray() { return 0, 0 } @@ -221,7 +221,7 @@ func antigravityReasoningReplayPendingModelContentIndex(payload []byte) (content } func antigravityReasoningReplayResolveContentIndex(payload []byte, cached int) int { - contents := gjson.GetBytes(payload, "request.contents") + contents := util.GetGJSONBytesNoCopy(payload, "request.contents") if !contents.IsArray() { return cached } @@ -408,7 +408,7 @@ func filterAntigravityReasoningReplayItemsForRequestWithSchemas(payload []byte, func antigravityExistingToolCallKeys(payload []byte) map[string]bool { existing := make(map[string]bool) - contents := gjson.GetBytes(payload, "request.contents") + contents := util.GetGJSONBytesNoCopy(payload, "request.contents") if !contents.IsArray() { return existing } @@ -512,7 +512,7 @@ func antigravityFunctionResponseContentIndex(payload []byte, callID string) (int if callID == "" { return -1, false } - contents := gjson.GetBytes(payload, "request.contents") + contents := util.GetGJSONBytesNoCopy(payload, "request.contents") if !contents.IsArray() { return -1, false } @@ -539,7 +539,7 @@ func restoreAntigravityFunctionResponseReplayIdentity(payload []byte, currentID, return payload } out := payload - contents := gjson.GetBytes(out, "request.contents") + contents := util.GetGJSONBytesNoCopy(out, "request.contents") contents.ForEach(func(contentKey, content gjson.Result) bool { content.Get("parts").ForEach(func(partKey, part gjson.Result) bool { response := part.Get("functionResponse") @@ -566,7 +566,7 @@ func antigravityFunctionCallPartLocation(payload []byte, callID string) (content if callID == "" { return -1, -1, false } - contents := gjson.GetBytes(payload, "request.contents") + contents := util.GetGJSONBytesNoCopy(payload, "request.contents") if !contents.IsArray() { return -1, -1, false } @@ -627,7 +627,7 @@ func antigravityFunctionCallPartLocationForReplayWithSchemas(payload []byte, ite name, ci, pi, util.IsGeminiClaudeToolUseID(candidateID)) return -1, -1, false } - contents := gjson.GetBytes(payload, "request.contents") + contents := util.GetGJSONBytesNoCopy(payload, "request.contents") if !contents.IsArray() { return -1, -1, false } @@ -705,7 +705,7 @@ func antigravityFunctionCallProvenanceLocation(payload []byte, itemResult gjson. } func insertAntigravityModelFunctionCallBeforeContent(payload []byte, beforeIndex int, name, callID, thoughtSig string, args gjson.Result) ([]byte, bool) { - contents := gjson.GetBytes(payload, "request.contents") + contents := util.GetGJSONBytesNoCopy(payload, "request.contents") if !contents.IsArray() { return payload, false } @@ -838,7 +838,7 @@ func antigravityReplayPartOccurrence(parts []gjson.Result, targetPartIndex int, } func antigravityReplayContextFingerprint(payload []byte, beforeContentIndex int) string { - contents := gjson.GetBytes(payload, "request.contents") + contents := util.GetGJSONBytesNoCopy(payload, "request.contents") if !contents.IsArray() || beforeContentIndex < 0 { return "" } @@ -887,7 +887,7 @@ func antigravityReplayToolSchemasFromRequests(rawRequests ...[]byte) map[string] continue } nameMap := util.SanitizedFunctionNameMap(raw) - tools := gjson.GetBytes(raw, "tools") + tools := util.GetGJSONBytesNoCopy(raw, "tools") if !tools.IsArray() { continue } @@ -996,7 +996,7 @@ func antigravityFunctionCallMatchesReplayItem(functionCall, itemResult gjson.Res } func antigravityPayloadHasClaudeToolProvenanceID(payload []byte) bool { - contents := gjson.GetBytes(payload, "request.contents") + contents := util.GetGJSONBytesNoCopy(payload, "request.contents") if !contents.IsArray() { return false } @@ -1034,7 +1034,7 @@ func antigravitySyntheticToolCallID(reservedID string) string { // antigravityRepairUnsignedFirstFunctionCalls. Every other part is left alone, // preserving the native "1 signed + N unsigned" parallel-call shape. func degradeAntigravityClaudeToolProvenanceIDs(payload []byte) ([]byte, int) { - contents := gjson.GetBytes(payload, "request.contents") + contents := util.GetGJSONBytesNoCopy(payload, "request.contents") if !contents.IsArray() { return payload, 0 } @@ -1079,7 +1079,7 @@ func degradeAntigravityClaudeToolProvenanceIDs(payload []byte) ([]byte, int) { // context deliberately declines to replay one. Only a missing signature is filled // in here, so native signatures are never touched. func antigravityRepairUnsignedFirstFunctionCalls(payload []byte) []byte { - contents := gjson.GetBytes(payload, "request.contents") + contents := util.GetGJSONBytesNoCopy(payload, "request.contents") if !contents.IsArray() { return payload } @@ -1134,7 +1134,7 @@ func antigravitySetReplayItemContextHash(item []byte, payload []byte, contentInd func antigravityThoughtSignatureReplayPartPath(payload []byte, itemResult gjson.Result) (string, bool) { ci := int(itemResult.Get("contentIndex").Int()) - contents := gjson.GetBytes(payload, "request.contents") + contents := util.GetGJSONBytesNoCopy(payload, "request.contents") if !contents.IsArray() { return "", false } @@ -1309,7 +1309,7 @@ func antigravityFunctionResponsesCanRestoreID(payload []byte, currentID, nativeN if currentID == "" { return true } - contents := gjson.GetBytes(payload, "request.contents") + contents := util.GetGJSONBytesNoCopy(payload, "request.contents") if !contents.IsArray() { return false } @@ -1372,7 +1372,7 @@ func restoreAntigravityNativeFunctionCallReplay(payload []byte, contentIndex, pa out, _ = sjson.SetBytes(out, partPath+".thoughtSignature", signature) } if currentID != "" && nativeID != "" && currentID != nativeID { - contents := gjson.GetBytes(out, "request.contents") + contents := util.GetGJSONBytesNoCopy(out, "request.contents") contents.ForEach(func(contentKey, content gjson.Result) bool { content.Get("parts").ForEach(func(partKey, part gjson.Result) bool { response := part.Get("functionResponse") @@ -1581,7 +1581,7 @@ func newAntigravityReasoningReplayAccumulator(scope antigravityReasoningReplaySc } func antigravityReasoningReplayItemsFromRequest(payload []byte) [][]byte { - contents := gjson.GetBytes(payload, "request.contents") + contents := util.GetGJSONBytesNoCopy(payload, "request.contents") if !contents.IsArray() { return nil } diff --git a/internal/signature/gemini_validation.go b/internal/signature/gemini_validation.go index ad8ce940..c6ac5332 100644 --- a/internal/signature/gemini_validation.go +++ b/internal/signature/gemini_validation.go @@ -72,6 +72,7 @@ import ( "fmt" "strings" + "github.com/router-for-me/CLIProxyAPI/v7/internal/util" "github.com/tidwall/gjson" "google.golang.org/protobuf/encoding/protowire" ) @@ -502,8 +503,8 @@ func isASCIIUUIDBytes(decoded []byte) bool { } func geminiContents(inputRawJSON []byte) (gjson.Result, string) { - if contents := gjson.GetBytes(inputRawJSON, "contents"); contents.Exists() { + if contents := util.GetGJSONBytesNoCopy(inputRawJSON, "contents"); contents.Exists() { return contents, "contents" } - return gjson.GetBytes(inputRawJSON, "request.contents"), "request.contents" + return util.GetGJSONBytesNoCopy(inputRawJSON, "request.contents"), "request.contents" } diff --git a/sdk/cliproxy/session/identity.go b/sdk/cliproxy/session/identity.go index 696e0372..0105f0dd 100644 --- a/sdk/cliproxy/session/identity.go +++ b/sdk/cliproxy/session/identity.go @@ -11,6 +11,7 @@ import ( "strings" "unicode" + "github.com/router-for-me/CLIProxyAPI/v7/internal/util" cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/executor" sdktranslator "github.com/router-for-me/CLIProxyAPI/v7/sdk/translator" "github.com/tidwall/gjson" @@ -140,7 +141,9 @@ func hasExplicitSession(headers map[string][]string, payload []byte) bool { if len(payload) == 0 { return false } - root := gjson.ParseBytes(payload) + // Parsing without copying matters here: this runs on every request and the + // payload can be multiple megabytes. + root := util.ParseGJSONBytesNoCopy(payload) for _, path := range []string{"session_id", "sessionId", "conversation_id", "prompt_cache_key"} { if NormalizeExplicitID(root.Get(path).String()) != "" { return true -- knotmirror 0.0.1 From 1737596e020a2812ba6a6ed32ec820eba0d45473 Mon Sep 17 00:00:00 2001 From: sususu Date: Sat, 8 Aug 2026 10:53:58 +0800 Subject: [PATCH 14/17] Make the payload-reuse guards able to fail TestConvertGeminiRequestToGeminiReusesLargeNormalizedPayload asserted the reuse invariant with b.Fatal inside testing.Benchmark. A failing benchmark aborts before completing an iteration and returns a zero BenchmarkResult, so AllocedBytesPerOp reported 0 and the allocation check passed exactly when the payload was being copied. Injecting a full copy into the translator left the test green. Assert the invariant with t.Fatal and reject a zero-iteration benchmark result; both injected regressions now fail the test. Add two static guards for the no-copy invariant. Results handed out by the no-copy helpers alias the caller's buffer, and mutating those bytes corrupts already-derived values in a way the race detector cannot see. One guard rejects in-place sjson options, the other keeps the set of in-place byte writes small and documented: each reviewed site records why it cannot corrupt a no-copy result, and a new site fails the test until it is justified. --- .../gemini/gemini_gemini_request_test.go | 19 ++- internal/util/nocopy_invariant_test.go | 148 ++++++++++++++++++ 2 files changed, 162 insertions(+), 5 deletions(-) create mode 100644 internal/util/nocopy_invariant_test.go diff --git a/internal/translator/gemini/gemini/gemini_gemini_request_test.go b/internal/translator/gemini/gemini/gemini_gemini_request_test.go index 75c5679c..f5402ef0 100644 --- a/internal/translator/gemini/gemini/gemini_gemini_request_test.go +++ b/internal/translator/gemini/gemini/gemini_gemini_request_test.go @@ -14,17 +14,26 @@ var largeInlineDataBenchmarkOutput []byte func TestConvertGeminiRequestToGeminiReusesLargeNormalizedPayload(t *testing.T) { input := largeInlineDataGeminiRequest(true) + // Assert the reuse invariant with t.Fatal rather than inside testing.Benchmark: + // a failing benchmark aborts before any iteration completes and yields a zero + // BenchmarkResult, so AllocedBytesPerOp would report 0 and silently satisfy the + // allocation check below exactly when the payload is being copied. + output := ConvertGeminiRequestToGemini("gemini-test", input, false) + if &output[0] != &input[0] { + t.Fatal("normalized request should reuse the input payload") + } + largeInlineDataBenchmarkOutput = output + result := testing.Benchmark(func(b *testing.B) { b.ReportAllocs() for b.Loop() { - output := ConvertGeminiRequestToGemini("gemini-test", input, false) - if &output[0] != &input[0] { - b.Fatal("normalized request should reuse the input payload") - } - largeInlineDataBenchmarkOutput = output + largeInlineDataBenchmarkOutput = ConvertGeminiRequestToGemini("gemini-test", input, false) } }) + if result.N == 0 { + t.Fatal("allocation benchmark did not complete an iteration") + } if allocated := result.AllocedBytesPerOp(); allocated >= 1<<20 { t.Fatalf("normalized 20 MiB inlineData request allocated %d bytes/op, want less than 1 MiB", allocated) } diff --git a/internal/util/nocopy_invariant_test.go b/internal/util/nocopy_invariant_test.go new file mode 100644 index 00000000..92137350 --- /dev/null +++ b/internal/util/nocopy_invariant_test.go @@ -0,0 +1,148 @@ +package util + +import ( + "os" + "path/filepath" + "regexp" + "strings" + "testing" +) + +// inPlaceSJSONTokens are the sjson knobs that let a write reuse the caller's +// backing array instead of allocating a new one. +var inPlaceSJSONTokens = []string{"ReplaceInPlace", "Optimistic"} + +// inPlaceSJSONAllowlist holds files that are allowed to opt into in-place +// sjson writes. A file may only be added here once it is proven that no +// no-copy GJSON result (GetGJSONBytesNoCopy / ParseGJSONBytesNoCopy) derived +// from the same buffer can still be alive at that point. +var inPlaceSJSONAllowlist = map[string]struct{}{} + +// forEachSourceFile visits every non-test Go file in the repository. +func forEachSourceFile(t *testing.T, root string, visit func(rel string, data []byte)) { + t.Helper() + err := filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error { + if err != nil { + return err + } + if d.IsDir() { + switch d.Name() { + case ".git", "vendor", "node_modules", "testdata": + return filepath.SkipDir + } + return nil + } + if !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") { + return nil + } + rel, errRel := filepath.Rel(root, path) + if errRel != nil { + return errRel + } + data, errRead := os.ReadFile(path) + if errRead != nil { + return errRead + } + visit(filepath.ToSlash(rel), data) + return nil + }) + if err != nil { + t.Fatalf("walk repository: %v", err) + } +} + +// TestNoInPlaceSJSONWrites protects the invariant that request payload buffers +// stay immutable for their whole lifetime. +// +// GetGJSONBytesNoCopy and ParseGJSONBytesNoCopy hand out gjson.Result values +// whose Raw and Str alias the caller's []byte. Go strings must never change, +// so any in-place mutation of that buffer turns already-derived results into +// silently wrong data: re-parsing sees the new bytes, and strings that were +// used as map keys keep a hash computed from the old ones. The race detector +// cannot see this, and normal tests rarely trigger it, so the invariant is +// enforced statically here instead. +func TestNoInPlaceSJSONWrites(t *testing.T) { + root := repoRoot(t) + var offenders []string + forEachSourceFile(t, root, func(rel string, data []byte) { + if _, allowed := inPlaceSJSONAllowlist[rel]; allowed { + return + } + for _, token := range inPlaceSJSONTokens { + if strings.Contains(string(data), token) { + offenders = append(offenders, rel+" uses "+token) + } + } + }) + if len(offenders) > 0 { + t.Fatalf("in-place sjson writes would corrupt no-copy GJSON results that alias the same buffer:\n %s\n"+ + "Either keep the default (allocating) sjson call, or prove no no-copy result derived from that buffer is still alive and add the file to inPlaceSJSONAllowlist.", + strings.Join(offenders, "\n ")) + } +} + +// inPlaceByteWritePatterns match the realistic ways Go code overwrites bytes +// of an existing buffer: copying into a slice expression, or zeroing elements +// in a loop. They do not catch every possible form, so they are a tripwire for +// new code rather than a proof of absence. +var inPlaceByteWritePatterns = []*regexp.Regexp{ + regexp.MustCompile(`\bcopy\([a-zA-Z_][A-Za-z0-9_.]*\[`), + regexp.MustCompile(`^\s*[a-zA-Z_][A-Za-z0-9_.]*\[[a-zA-Z0-9_]+\] = 0$`), +} + +// inPlaceByteWriteAllowlist enumerates the reviewed in-place byte writes. Each +// entry states why the write cannot corrupt a no-copy GJSON result: either the +// buffer is private to the writer, or every reader copies out before the write. +var inPlaceByteWriteAllowlist = map[string]string{ + "internal/runtime/executor/claude_signing.go": "writes CCH digits into bytes.Clone(body); the caller's body is never touched", + "internal/runtime/executor/claude_executor_cloaking.go": "shifts []string headers to prepend a block; no byte of any payload is rewritten", + "internal/runtime/executor/claude_executor_request.go": "shifts []string headers to insert a part; no byte of any payload is rewritten", + "internal/runtime/executor/helps/claude_mcp_alias.go": "copies an HMAC sum into a local fixed-size digest array", + "internal/client/codex/live/tcp_proxy.go": "copies header and payload into a freshly allocated frame", + "internal/home/client.go": "zeroes a secret buffer after json.Unmarshal has copied every value out", + "internal/pluginstore/auth.go": "zeroes a locally built credential buffer after base64 encoding copied it out", + "internal/wsrelay/manager.go": "rewrites a locally allocated random-id buffer", +} + +// TestInPlaceByteWritesAreReviewed keeps the set of in-place byte writes small +// and justified. A new hit means the author must prove that no no-copy GJSON +// result derived from that buffer can still be alive, then document it here. +func TestInPlaceByteWritesAreReviewed(t *testing.T) { + root := repoRoot(t) + var offenders []string + forEachSourceFile(t, root, func(rel string, data []byte) { + if _, allowed := inPlaceByteWriteAllowlist[rel]; allowed { + return + } + for _, line := range strings.Split(string(data), "\n") { + for _, pattern := range inPlaceByteWritePatterns { + if pattern.MatchString(line) { + offenders = append(offenders, rel+": "+strings.TrimSpace(line)) + } + } + } + }) + if len(offenders) > 0 { + t.Fatalf("unreviewed in-place byte write(s):\n %s\n"+ + "Prove that no no-copy GJSON result derived from that buffer is still alive, then add the file to inPlaceByteWriteAllowlist with the reason.", + strings.Join(offenders, "\n ")) + } +} + +func repoRoot(t *testing.T) string { + t.Helper() + dir, err := os.Getwd() + if err != nil { + t.Fatalf("getwd: %v", err) + } + for { + if _, errStat := os.Stat(filepath.Join(dir, "go.mod")); errStat == nil { + return dir + } + parent := filepath.Dir(dir) + if parent == dir { + t.Fatal("go.mod not found above working directory") + } + dir = parent + } +} -- knotmirror 0.0.1 From b921b5d03264aa34094ef8bf19ef60163d34ef8f Mon Sep 17 00:00:00 2001 From: sususu Date: Sat, 8 Aug 2026 23:16:40 +0800 Subject: [PATCH 15/17] Review: tighten the in-place byte write guard Reviewing the guard against itself surfaced two defects. The allowlist was keyed by file, so a new in-place write inside an already reviewed file passed silently, and internal/wsrelay/manager.go was listed without ever matching a pattern. Record the reviewed occurrence count per file and reject any deviation in either direction: an added write in a reviewed file now fails, and a stale entry whose writes disappeared fails too. Drop the entry that never matched. --- internal/util/nocopy_invariant_test.go | 62 ++++++++++++++++---------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/internal/util/nocopy_invariant_test.go b/internal/util/nocopy_invariant_test.go index 92137350..5bdfd3d4 100644 --- a/internal/util/nocopy_invariant_test.go +++ b/internal/util/nocopy_invariant_test.go @@ -90,42 +90,58 @@ var inPlaceByteWritePatterns = []*regexp.Regexp{ regexp.MustCompile(`^\s*[a-zA-Z_][A-Za-z0-9_.]*\[[a-zA-Z0-9_]+\] = 0$`), } -// inPlaceByteWriteAllowlist enumerates the reviewed in-place byte writes. Each -// entry states why the write cannot corrupt a no-copy GJSON result: either the -// buffer is private to the writer, or every reader copies out before the write. -var inPlaceByteWriteAllowlist = map[string]string{ - "internal/runtime/executor/claude_signing.go": "writes CCH digits into bytes.Clone(body); the caller's body is never touched", - "internal/runtime/executor/claude_executor_cloaking.go": "shifts []string headers to prepend a block; no byte of any payload is rewritten", - "internal/runtime/executor/claude_executor_request.go": "shifts []string headers to insert a part; no byte of any payload is rewritten", - "internal/runtime/executor/helps/claude_mcp_alias.go": "copies an HMAC sum into a local fixed-size digest array", - "internal/client/codex/live/tcp_proxy.go": "copies header and payload into a freshly allocated frame", - "internal/home/client.go": "zeroes a secret buffer after json.Unmarshal has copied every value out", - "internal/pluginstore/auth.go": "zeroes a locally built credential buffer after base64 encoding copied it out", - "internal/wsrelay/manager.go": "rewrites a locally allocated random-id buffer", +// reviewedInPlaceByteWrites records the reviewed in-place byte writes per file. +// The count is part of the contract: a new write inside an already reviewed file +// must be reviewed too, so the count must be updated deliberately. Each reason +// states why the write cannot corrupt a no-copy GJSON result, either because the +// buffer is private to the writer or because every reader copies out first. +type reviewedInPlaceByteWrite struct { + count int + reason string +} + +var reviewedInPlaceByteWrites = map[string]reviewedInPlaceByteWrite{ + "internal/runtime/executor/claude_signing.go": {2, "writes CCH digits into bytes.Clone(body); the caller's body is never touched"}, + "internal/runtime/executor/claude_executor_cloaking.go": {1, "shifts []string headers to prepend a block; no byte of any payload is rewritten"}, + "internal/runtime/executor/claude_executor_request.go": {2, "shifts []string headers to insert a part; no byte of any payload is rewritten"}, + "internal/runtime/executor/helps/claude_mcp_alias.go": {1, "copies an HMAC sum into a local fixed-size digest array"}, + "internal/client/codex/live/tcp_proxy.go": {1, "copies header and payload into a freshly allocated frame"}, + "internal/home/client.go": {1, "zeroes a secret buffer after json.Unmarshal has copied every value out"}, + "internal/pluginstore/auth.go": {1, "zeroes a locally built credential buffer after base64 encoding copied it out"}, } // TestInPlaceByteWritesAreReviewed keeps the set of in-place byte writes small -// and justified. A new hit means the author must prove that no no-copy GJSON -// result derived from that buffer can still be alive, then document it here. +// and justified. Any change to the set, including a new write in an already +// reviewed file, fails until the author proves that no no-copy GJSON result +// derived from that buffer can still be alive and records it above. func TestInPlaceByteWritesAreReviewed(t *testing.T) { root := repoRoot(t) - var offenders []string + found := make(map[string][]string) forEachSourceFile(t, root, func(rel string, data []byte) { - if _, allowed := inPlaceByteWriteAllowlist[rel]; allowed { - return - } for _, line := range strings.Split(string(data), "\n") { for _, pattern := range inPlaceByteWritePatterns { if pattern.MatchString(line) { - offenders = append(offenders, rel+": "+strings.TrimSpace(line)) + found[rel] = append(found[rel], strings.TrimSpace(line)) } } } }) - if len(offenders) > 0 { - t.Fatalf("unreviewed in-place byte write(s):\n %s\n"+ - "Prove that no no-copy GJSON result derived from that buffer is still alive, then add the file to inPlaceByteWriteAllowlist with the reason.", - strings.Join(offenders, "\n ")) + for rel, lines := range found { + reviewed, ok := reviewedInPlaceByteWrites[rel] + if !ok { + t.Errorf("unreviewed in-place byte write in %s:\n %s\nProve that no no-copy GJSON result derived from that buffer is still alive, then record it in reviewedInPlaceByteWrites.", + rel, strings.Join(lines, "\n ")) + continue + } + if len(lines) != reviewed.count { + t.Errorf("%s has %d in-place byte write(s), reviewed %d (%s):\n %s", + rel, len(lines), reviewed.count, reviewed.reason, strings.Join(lines, "\n ")) + } + } + for rel := range reviewedInPlaceByteWrites { + if _, ok := found[rel]; !ok { + t.Errorf("stale entry in reviewedInPlaceByteWrites: %s no longer contains an in-place byte write", rel) + } } } -- knotmirror 0.0.1 From 3522e481aa7baa68f47cb712d7c0f90919b69bf0 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Sun, 9 Aug 2026 03:47:01 +0800 Subject: [PATCH 16/17] fix(openai): emit `response.failed` stream errors for Codex requests Closes: #4854 --- .../optimize_multi_agent_v2.go | 7 +- .../openai/openai_responses_handlers.go | 23 ++++++ ...ai_responses_handlers_stream_error_test.go | 37 ++++++++++ .../handlers/openai_responses_stream_error.go | 71 +++++++++++++++++++ .../openai_responses_stream_error_test.go | 42 +++++++++++ 5 files changed, 179 insertions(+), 1 deletion(-) diff --git a/internal/client/codex/optimize-multi-agent-v2/optimize_multi_agent_v2.go b/internal/client/codex/optimize-multi-agent-v2/optimize_multi_agent_v2.go index fbfde007..80774883 100644 --- a/internal/client/codex/optimize-multi-agent-v2/optimize_multi_agent_v2.go +++ b/internal/client/codex/optimize-multi-agent-v2/optimize_multi_agent_v2.go @@ -166,7 +166,8 @@ func headerValueCaseInsensitive(headers http.Header, name string) string { return "" } -func isCodexMultiAgentClient(userAgent string) bool { +// IsCodexClientUserAgent reports whether a request uses an official Codex client identity. +func IsCodexClientUserAgent(userAgent string) bool { userAgent = strings.TrimSpace(userAgent) return strings.HasPrefix(userAgent, "Codex Desktop/") || strings.HasPrefix(userAgent, "codex-tui/") || @@ -174,6 +175,10 @@ func isCodexMultiAgentClient(userAgent string) bool { strings.HasPrefix(userAgent, "codex_cli_rs/") } +func isCodexMultiAgentClient(userAgent string) bool { + return IsCodexClientUserAgent(userAgent) +} + func codexSpawnAgentModelsForRequest(ctx context.Context, headers http.Header, homeEnabled bool) []codexSpawnAgentModel { availableModels := registry.GetGlobalRegistry().GetAvailableModels("openai") if homeEnabled { diff --git a/sdk/api/handlers/openai/openai_responses_handlers.go b/sdk/api/handlers/openai/openai_responses_handlers.go index ba78a447..405d47f0 100644 --- a/sdk/api/handlers/openai/openai_responses_handlers.go +++ b/sdk/api/handlers/openai/openai_responses_handlers.go @@ -14,6 +14,7 @@ import ( "io" "net/http" "sort" + "strings" "github.com/gin-gonic/gin" "github.com/router-for-me/CLIProxyAPI/v7/internal/client/codex/optimize-multi-agent-v2" @@ -573,6 +574,23 @@ func (h *OpenAIResponsesAPIHandler) handleStreamingResponse(c *gin.Context, rawJ } } +// isCodexResponsesClientRequest limits the alternate terminal event to official Codex clients. +func isCodexResponsesClientRequest(c *gin.Context) bool { + if c == nil || c.Request == nil { + return false + } + if multiagentv2.IsCodexClientUserAgent(c.GetHeader("User-Agent")) { + return true + } + + switch originator := strings.ToLower(strings.TrimSpace(c.GetHeader("Originator"))); originator { + case "codex desktop", "codex-tui", "codex_cli_rs": + return true + default: + return strings.HasPrefix(originator, "codex desktop/") || strings.HasPrefix(originator, "codex-tui/") || strings.HasPrefix(originator, "codex_cli_rs/") + } +} + func (h *OpenAIResponsesAPIHandler) forwardResponsesStream(c *gin.Context, flusher http.Flusher, cancel func(error), data <-chan []byte, errs <-chan *interfaces.ErrorMessage, framer *responsesSSEFramer) { if framer == nil { framer = &responsesSSEFramer{} @@ -594,6 +612,11 @@ func (h *OpenAIResponsesAPIHandler) forwardResponsesStream(c *gin.Context, flush if errMsg.Error != nil && errMsg.Error.Error() != "" { errText = errMsg.Error.Error() } + if isCodexResponsesClientRequest(c) { + chunk := handlers.BuildOpenAIResponsesStreamFailedChunk(status, errText, 0) + _, _ = fmt.Fprintf(c.Writer, "\nevent: response.failed\ndata: %s\n\n", string(chunk)) + return + } chunk := handlers.BuildOpenAIResponsesStreamErrorChunk(status, errText, 0) _, _ = fmt.Fprintf(c.Writer, "\nevent: error\ndata: %s\n\n", string(chunk)) }, diff --git a/sdk/api/handlers/openai/openai_responses_handlers_stream_error_test.go b/sdk/api/handlers/openai/openai_responses_handlers_stream_error_test.go index f416dcd0..038ced45 100644 --- a/sdk/api/handlers/openai/openai_responses_handlers_stream_error_test.go +++ b/sdk/api/handlers/openai/openai_responses_handlers_stream_error_test.go @@ -88,3 +88,40 @@ func TestForwardResponsesStreamExposesOnlyClientErrors(t *testing.T) { }) } } + +func TestForwardResponsesStreamUsesResponseFailedForCodex(t *testing.T) { + gin.SetMode(gin.TestMode) + + base := handlers.NewBaseAPIHandlers(&sdkconfig.SDKConfig{}, nil) + h := NewOpenAIResponsesAPIHandler(base) + + recorder := httptest.NewRecorder() + c, _ := gin.CreateTestContext(recorder) + c.Request = httptest.NewRequest(http.MethodPost, "/v1/responses", nil) + c.Request.Header.Set("User-Agent", "Codex Desktop/26.803.41515") + + flusher, ok := c.Writer.(http.Flusher) + if !ok { + t.Fatal("expected gin writer to implement http.Flusher") + } + + data := make(chan []byte) + errs := make(chan *interfaces.ErrorMessage, 1) + errs <- &interfaces.ErrorMessage{ + StatusCode: http.StatusBadRequest, + Error: errors.New(`{"error":{"type":"invalid_request","code":"cyber_policy","message":"blocked"}}`), + } + close(errs) + + h.forwardResponsesStream(c, flusher, func(error) {}, data, errs, nil) + body := recorder.Body.String() + if !strings.Contains(body, "event: response.failed") { + t.Fatalf("missing response.failed event: %q", body) + } + if strings.Contains(body, "event: error") { + t.Fatalf("unexpected legacy error event for Codex: %q", body) + } + if !strings.Contains(body, `"type":"invalid_request"`) || !strings.Contains(body, `"code":"cyber_policy"`) { + t.Fatalf("missing nested Codex error detail: %q", body) + } +} diff --git a/sdk/api/handlers/openai_responses_stream_error.go b/sdk/api/handlers/openai_responses_stream_error.go index e7760bd0..a3c3c7e3 100644 --- a/sdk/api/handlers/openai_responses_stream_error.go +++ b/sdk/api/handlers/openai_responses_stream_error.go @@ -14,6 +14,17 @@ type openAIResponsesStreamErrorChunk struct { SequenceNumber int `json:"sequence_number"` } +type openAIResponsesStreamFailedChunk struct { + Type string `json:"type"` + SequenceNumber int `json:"sequence_number"` + Response openAIResponsesStreamFailedResponse `json:"response"` +} + +type openAIResponsesStreamFailedResponse struct { + Status string `json:"status"` + Error map[string]any `json:"error"` +} + func openAIResponsesStreamErrorCode(status int) string { switch status { case http.StatusUnauthorized: @@ -117,3 +128,63 @@ func BuildOpenAIResponsesStreamErrorChunk(status int, errText string, sequenceNu } return []byte(`{"type":"error","code":"internal_server_error","message":"internal error","sequence_number":0}`) } + +func openAIResponsesStreamFailedErrorDetail(status int, errText, code, message string) map[string]any { + var payload map[string]any + if errUnmarshal := json.Unmarshal([]byte(strings.TrimSpace(errText)), &payload); errUnmarshal == nil { + if errorDetail, ok := payload["error"].(map[string]any); ok { + return errorDetail + } + if response, ok := payload["response"].(map[string]any); ok { + if errorDetail, ok := response["error"].(map[string]any); ok { + return errorDetail + } + } + } + + errorType := "invalid_request_error" + if status >= http.StatusInternalServerError { + errorType = "server_error" + } + return map[string]any{ + "type": errorType, + "code": code, + "message": message, + } +} + +// BuildOpenAIResponsesStreamFailedChunk builds the terminal Responses event used by official Codex clients. +// It is intentionally separate from BuildOpenAIResponsesStreamErrorChunk so existing clients keep the legacy shape. +func BuildOpenAIResponsesStreamFailedChunk(status int, errText string, sequenceNumber int) []byte { + if status <= 0 { + status = http.StatusInternalServerError + } + if sequenceNumber < 0 { + sequenceNumber = 0 + } + + legacyChunk := BuildOpenAIResponsesStreamErrorChunk(status, errText, sequenceNumber) + var legacyPayload openAIResponsesStreamErrorChunk + if errUnmarshal := json.Unmarshal(legacyChunk, &legacyPayload); errUnmarshal != nil { + legacyPayload.Code = openAIResponsesStreamErrorCode(status) + legacyPayload.Message = http.StatusText(status) + legacyPayload.SequenceNumber = sequenceNumber + } + if sequenceNumber == 0 && legacyPayload.SequenceNumber > 0 { + sequenceNumber = legacyPayload.SequenceNumber + } + + data, errMarshal := json.Marshal(openAIResponsesStreamFailedChunk{ + Type: "response.failed", + SequenceNumber: sequenceNumber, + Response: openAIResponsesStreamFailedResponse{ + Status: "failed", + Error: openAIResponsesStreamFailedErrorDetail(status, errText, legacyPayload.Code, legacyPayload.Message), + }, + }) + if errMarshal == nil { + return data + } + + return []byte(`{"type":"response.failed","sequence_number":0,"response":{"status":"failed","error":{"type":"server_error","code":"internal_server_error","message":"internal error"}}}`) +} diff --git a/sdk/api/handlers/openai_responses_stream_error_test.go b/sdk/api/handlers/openai_responses_stream_error_test.go index 90b2c667..c6dfd25e 100644 --- a/sdk/api/handlers/openai_responses_stream_error_test.go +++ b/sdk/api/handlers/openai_responses_stream_error_test.go @@ -46,3 +46,45 @@ func TestBuildOpenAIResponsesStreamErrorChunkExtractsHTTPErrorBody(t *testing.T) t.Fatalf("message = %v, want %q", payload["message"], "oops") } } + +func TestBuildOpenAIResponsesStreamFailedChunkPreservesNestedError(t *testing.T) { + chunk := BuildOpenAIResponsesStreamFailedChunk( + http.StatusBadRequest, + `{"error":{"type":"invalid_request","code":"cyber_policy","message":"blocked","param":null}}`, + 0, + ) + + var payload struct { + Type string `json:"type"` + SequenceNumber int `json:"sequence_number"` + Response struct { + Status string `json:"status"` + Error struct { + Type string `json:"type"` + Code string `json:"code"` + Message string `json:"message"` + } `json:"error"` + } `json:"response"` + } + if err := json.Unmarshal(chunk, &payload); err != nil { + t.Fatalf("unmarshal: %v", err) + } + if payload.Type != "response.failed" { + t.Fatalf("type = %q, want %q", payload.Type, "response.failed") + } + if payload.SequenceNumber != 0 { + t.Fatalf("sequence_number = %d, want 0", payload.SequenceNumber) + } + if payload.Response.Status != "failed" { + t.Fatalf("response.status = %q, want %q", payload.Response.Status, "failed") + } + if payload.Response.Error.Type != "invalid_request" { + t.Fatalf("response.error.type = %q, want %q", payload.Response.Error.Type, "invalid_request") + } + if payload.Response.Error.Code != "cyber_policy" { + t.Fatalf("response.error.code = %q, want %q", payload.Response.Error.Code, "cyber_policy") + } + if payload.Response.Error.Message != "blocked" { + t.Fatalf("response.error.message = %q, want %q", payload.Response.Error.Message, "blocked") + } +} -- knotmirror 0.0.1 From 2e6b1d83f6c304a102aa33c1faf0a4f94d0d331e Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Sun, 9 Aug 2026 04:36:39 +0800 Subject: [PATCH 17/17] fix(claude): add Claude-compatible thinking replay persistence for multi-turn sessions - Add Claude replay enablement and scope derivation for API-key compatible Claude requests with isolated model/session keys. - Restore cached signed reasoning blocks into outbound requests and persist completed assistant turns after execution, including stream handling. - Introduce a dedicated Claude thinking-replay cache with local + home-KV paths, CAS-based snapshot replace/clear, TTL/size limits, and bounded eviction/expiration management. Closes: #4856 --- .../cache/claude_thinking_replay_cache.go | 482 ++++++++++++++++++ .../claude_thinking_replay_cache_test.go | 89 ++++ internal/cache/signature_cache.go | 1 + internal/config/config_types.go | 3 +- .../executor/claude_executor_execute.go | 10 + .../executor/claude_executor_stream.go | 15 +- .../executor/claude_thinking_replay.go | 140 +++++ .../executor/claude_thinking_replay_test.go | 374 ++++++++++++++ .../runtime/executor/kimi_thinking_replay.go | 13 +- 9 files changed, 1122 insertions(+), 5 deletions(-) create mode 100644 internal/cache/claude_thinking_replay_cache.go create mode 100644 internal/cache/claude_thinking_replay_cache_test.go create mode 100644 internal/runtime/executor/claude_thinking_replay.go create mode 100644 internal/runtime/executor/claude_thinking_replay_test.go diff --git a/internal/cache/claude_thinking_replay_cache.go b/internal/cache/claude_thinking_replay_cache.go new file mode 100644 index 00000000..6ca146f7 --- /dev/null +++ b/internal/cache/claude_thinking_replay_cache.go @@ -0,0 +1,482 @@ +package cache + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "sort" + "strings" + "sync" + "time" + + "github.com/google/uuid" + homekv "github.com/router-for-me/CLIProxyAPI/v7/internal/home" + log "github.com/sirupsen/logrus" + "github.com/tidwall/gjson" +) + +const ( + // ClaudeThinkingReplayCacheTTL limits how long signed assistant turns stay replayable. + ClaudeThinkingReplayCacheTTL = 1 * time.Hour + + // ClaudeThinkingReplayCacheMaxEntries bounds process memory used by Claude replay continuity. + ClaudeThinkingReplayCacheMaxEntries = 10240 + + // ClaudeThinkingReplayCacheEvictBatchSize leaves headroom after reaching capacity. + ClaudeThinkingReplayCacheEvictBatchSize = 128 + + // ClaudeThinkingReplayCacheMaxBytesPerSession bounds all cached assistant turns for one session. + ClaudeThinkingReplayCacheMaxBytesPerSession = 8 << 20 + + // ClaudeThinkingReplayCacheMaxTurnsPerSession bounds the number of assistant turns per session. + ClaudeThinkingReplayCacheMaxTurnsPerSession = 64 + + // ClaudeThinkingReplayCacheMaxBlocksPerTurn prevents pathological content arrays. + ClaudeThinkingReplayCacheMaxBlocksPerTurn = 512 + + // ClaudeThinkingReplayCacheMaxTotalBytes bounds aggregate in-process Claude replay content. + ClaudeThinkingReplayCacheMaxTotalBytes = 256 << 20 + + claudeThinkingReplayCacheMaxSerializedBytes = ClaudeThinkingReplayCacheMaxBytesPerSession + 1024 +) + +type claudeThinkingReplayEntry struct { + Contents [][]byte + Timestamp time.Time + Generation string + Deleted bool +} + +// ClaudeThinkingReplaySnapshot identifies the exact replay generation read for one request. +type ClaudeThinkingReplaySnapshot = KimiThinkingReplaySnapshot + +type claudeThinkingReplayHomeValue struct { + Generation string `json:"generation"` + Deleted bool `json:"deleted,omitempty"` + Contents []json.RawMessage `json:"contents,omitempty"` +} + +var ( + claudeThinkingReplayMu sync.Mutex + claudeThinkingReplayEntries = make(map[string]claudeThinkingReplayEntry) + claudeThinkingReplayTotalBytes int +) + +var currentClaudeThinkingReplayKVClient = func() (kimiThinkingReplayKVClient, bool, error) { + return homekv.CurrentKVClient() +} + +// CacheClaudeThinkingReplayBestEffort stores one complete signed assistant content array. +func CacheClaudeThinkingReplayBestEffort(ctx context.Context, modelFamily, sessionKey string, content []byte) bool { + key := claudeThinkingReplayCacheKey(modelFamily, sessionKey) + if key == "" || !validClaudeThinkingReplayContent(content) { + return false + } + if ctx == nil { + ctx = context.Background() + } + contents := [][]byte{append([]byte(nil), content...)} + generation := uuid.NewString() + if client, homeMode, errClient := currentClaudeThinkingReplayKVClient(); homeMode { + if errClient != nil { + log.Errorf("home kv best-effort Claude thinking replay set failed: %v", errClient) + return false + } + raw, errMarshal := marshalClaudeThinkingReplayHomeValue(generation, false, contents) + if errMarshal != nil { + log.Errorf("home kv best-effort Claude thinking replay set failed: %v", errMarshal) + return false + } + written, errSet := client.KVSet(ctx, claudeThinkingReplayKVKey(modelFamily, sessionKey), raw, homekv.KVSetOptions{EX: ClaudeThinkingReplayCacheTTL}) + if errSet != nil { + log.Errorf("home kv best-effort Claude thinking replay set failed: %v", errSet) + return false + } + return written + } + + storeClaudeThinkingReplayLocal(key, contents, generation, false, time.Now()) + return true +} + +// GetClaudeThinkingReplayRequired retrieves all cached assistant turns for request-time replay. +func GetClaudeThinkingReplayRequired(ctx context.Context, modelFamily, sessionKey string) ([][]byte, bool, error) { + contents, _, found, errGet := GetClaudeThinkingReplayWithSnapshotRequired(ctx, modelFamily, sessionKey) + return contents, found, errGet +} + +// GetClaudeThinkingReplayWithSnapshotRequired retrieves replay content and the exact cache state read. +func GetClaudeThinkingReplayWithSnapshotRequired(ctx context.Context, modelFamily, sessionKey string) ([][]byte, ClaudeThinkingReplaySnapshot, bool, error) { + key := claudeThinkingReplayCacheKey(modelFamily, sessionKey) + if key == "" { + return nil, ClaudeThinkingReplaySnapshot{}, false, nil + } + if ctx == nil { + ctx = context.Background() + } + client, homeMode, errClient := currentClaudeThinkingReplayKVClient() + if homeMode { + if errClient != nil { + return nil, ClaudeThinkingReplaySnapshot{loaded: true}, false, errClient + } + kvKey := claudeThinkingReplayKVKey(modelFamily, sessionKey) + raw, errRead := readOrReserveClaudeThinkingReplayHomeValue(ctx, client, kvKey) + if errRead != nil { + return nil, ClaudeThinkingReplaySnapshot{loaded: true}, false, errRead + } + snapshot := ClaudeThinkingReplaySnapshot{raw: append([]byte(nil), raw...), loaded: true, found: true} + contents, generation, deleted, okDecode := decodeClaudeThinkingReplayHomeValue(raw) + if !okDecode { + return nil, snapshot, false, fmt.Errorf("invalid Claude thinking replay content") + } + snapshot.generation = generation + if _, errExpire := client.KVExpire(ctx, kvKey, ClaudeThinkingReplayCacheTTL); errExpire != nil { + log.Warnf("home kv Claude thinking replay expire failed: %v", errExpire) + } + if deleted { + return nil, snapshot, false, nil + } + return cloneClaudeThinkingReplayContents(contents), snapshot, len(contents) > 0, nil + } + + cacheCleanupOnce.Do(startCacheCleanup) + now := time.Now() + claudeThinkingReplayMu.Lock() + defer claudeThinkingReplayMu.Unlock() + entry, ok := claudeThinkingReplayEntries[key] + if !ok || now.Sub(entry.Timestamp) > ClaudeThinkingReplayCacheTTL { + if ok { + claudeThinkingReplayTotalBytes -= claudeThinkingReplayEntryBytes(entry.Contents) + delete(claudeThinkingReplayEntries, key) + } + entry = reserveClaudeThinkingReplayLocalLocked(key, now) + } + entry.Timestamp = now + claudeThinkingReplayEntries[key] = entry + snapshot := ClaudeThinkingReplaySnapshot{generation: entry.Generation, loaded: true, found: true} + if entry.Deleted { + return nil, snapshot, false, nil + } + return cloneClaudeThinkingReplayContents(entry.Contents), snapshot, len(entry.Contents) > 0, nil +} + +// ReplaceClaudeThinkingReplayIfUnchanged appends a completed assistant turn only if the request snapshot is current. +func ReplaceClaudeThinkingReplayIfUnchanged(ctx context.Context, modelFamily, sessionKey string, snapshot ClaudeThinkingReplaySnapshot, content []byte) (bool, error) { + key := claudeThinkingReplayCacheKey(modelFamily, sessionKey) + if key == "" || !validClaudeThinkingReplayContent(content) { + return false, nil + } + if ctx == nil { + ctx = context.Background() + } + if !snapshot.loaded { + return CacheClaudeThinkingReplayBestEffort(ctx, modelFamily, sessionKey, content), nil + } + client, homeMode, errClient := currentClaudeThinkingReplayKVClient() + if homeMode { + if errClient != nil { + return false, errClient + } + contents, _, deleted, okDecode := decodeClaudeThinkingReplayHomeValue(snapshot.raw) + if !okDecode { + return false, fmt.Errorf("invalid Claude thinking replay snapshot") + } + if deleted { + contents = nil + } + contents = appendClaudeThinkingReplayContent(contents, content) + generation := uuid.NewString() + raw, errMarshal := marshalClaudeThinkingReplayHomeValue(generation, false, contents) + if errMarshal != nil { + return false, errMarshal + } + return client.KVCompareAndSwap(ctx, claudeThinkingReplayKVKey(modelFamily, sessionKey), snapshot.raw, snapshot.found, raw, ClaudeThinkingReplayCacheTTL) + } + + claudeThinkingReplayMu.Lock() + defer claudeThinkingReplayMu.Unlock() + entry, found := claudeThinkingReplayEntries[key] + if found != snapshot.found || (found && entry.Generation != snapshot.generation) { + return false, nil + } + contents := appendClaudeThinkingReplayContent(entry.Contents, content) + claudeThinkingReplayTotalBytes -= claudeThinkingReplayEntryBytes(entry.Contents) + claudeThinkingReplayTotalBytes += claudeThinkingReplayEntryBytes(contents) + claudeThinkingReplayEntries[key] = claudeThinkingReplayEntry{ + Contents: contents, + Timestamp: time.Now(), + Generation: uuid.NewString(), + } + enforceClaudeThinkingReplayLimitsLocked() + return true, nil +} + +// DeleteClaudeThinkingReplayIfUnchanged clears replay state only if the request snapshot is current. +func DeleteClaudeThinkingReplayIfUnchanged(ctx context.Context, modelFamily, sessionKey string, snapshot ClaudeThinkingReplaySnapshot) (bool, error) { + key := claudeThinkingReplayCacheKey(modelFamily, sessionKey) + if key == "" { + return false, nil + } + if ctx == nil { + ctx = context.Background() + } + if !snapshot.loaded { + return true, DeleteClaudeThinkingReplayRequired(ctx, modelFamily, sessionKey) + } + generation := uuid.NewString() + client, homeMode, errClient := currentClaudeThinkingReplayKVClient() + if homeMode { + if errClient != nil { + return false, errClient + } + tombstone, errMarshal := marshalClaudeThinkingReplayHomeValue(generation, true, nil) + if errMarshal != nil { + return false, errMarshal + } + return client.KVCompareAndSwap(ctx, claudeThinkingReplayKVKey(modelFamily, sessionKey), snapshot.raw, snapshot.found, tombstone, ClaudeThinkingReplayCacheTTL) + } + + claudeThinkingReplayMu.Lock() + defer claudeThinkingReplayMu.Unlock() + entry, found := claudeThinkingReplayEntries[key] + if found != snapshot.found || (found && entry.Generation != snapshot.generation) { + return false, nil + } + claudeThinkingReplayTotalBytes -= claudeThinkingReplayEntryBytes(entry.Contents) + claudeThinkingReplayEntries[key] = claudeThinkingReplayEntry{Timestamp: time.Now(), Generation: generation, Deleted: true} + return true, nil +} + +// DeleteClaudeThinkingReplayRequired removes stale replay state unconditionally. +func DeleteClaudeThinkingReplayRequired(ctx context.Context, modelFamily, sessionKey string) error { + key := claudeThinkingReplayCacheKey(modelFamily, sessionKey) + if key == "" { + return nil + } + if ctx == nil { + ctx = context.Background() + } + client, homeMode, errClient := currentClaudeThinkingReplayKVClient() + if homeMode { + if errClient != nil { + return errClient + } + _, errDelete := client.KVDel(ctx, claudeThinkingReplayKVKey(modelFamily, sessionKey)) + return errDelete + } + claudeThinkingReplayMu.Lock() + if entry, found := claudeThinkingReplayEntries[key]; found { + claudeThinkingReplayTotalBytes -= claudeThinkingReplayEntryBytes(entry.Contents) + delete(claudeThinkingReplayEntries, key) + } + claudeThinkingReplayMu.Unlock() + return nil +} + +// ClearClaudeThinkingReplayCache clears only Claude replay state. +func ClearClaudeThinkingReplayCache() { + claudeThinkingReplayMu.Lock() + claudeThinkingReplayEntries = make(map[string]claudeThinkingReplayEntry) + claudeThinkingReplayTotalBytes = 0 + claudeThinkingReplayMu.Unlock() +} + +func readOrReserveClaudeThinkingReplayHomeValue(ctx context.Context, client kimiThinkingReplayKVClient, key string) ([]byte, error) { + for attempt := 0; attempt < 4; attempt++ { + raw, found, errGet := client.KVGet(ctx, key) + if errGet != nil { + return nil, errGet + } + if found { + if len(raw) > claudeThinkingReplayCacheMaxSerializedBytes { + return nil, fmt.Errorf("Claude thinking replay value exceeds size limit") + } + return raw, nil + } + tombstone, errMarshal := marshalClaudeThinkingReplayHomeValue(uuid.NewString(), true, nil) + if errMarshal != nil { + return nil, errMarshal + } + swapped, errReserve := client.KVCompareAndSwap(ctx, key, nil, false, tombstone, ClaudeThinkingReplayCacheTTL) + if errReserve != nil { + return nil, errReserve + } + if swapped { + return tombstone, nil + } + } + return nil, fmt.Errorf("could not reserve absent Claude thinking replay state") +} + +func marshalClaudeThinkingReplayHomeValue(generation string, deleted bool, contents [][]byte) ([]byte, error) { + value := claudeThinkingReplayHomeValue{Generation: generation, Deleted: deleted} + if !deleted { + value.Contents = make([]json.RawMessage, 0, len(contents)) + for _, content := range contents { + value.Contents = append(value.Contents, json.RawMessage(append([]byte(nil), content...))) + } + } + return json.Marshal(value) +} + +func decodeClaudeThinkingReplayHomeValue(raw []byte) ([][]byte, string, bool, bool) { + if len(raw) == 0 || len(raw) > claudeThinkingReplayCacheMaxSerializedBytes || !gjson.ValidBytes(raw) { + return nil, "", false, false + } + var value claudeThinkingReplayHomeValue + if errUnmarshal := json.Unmarshal(raw, &value); errUnmarshal != nil || strings.TrimSpace(value.Generation) == "" { + return nil, "", false, false + } + if value.Deleted { + return nil, value.Generation, true, true + } + contents := make([][]byte, 0, len(value.Contents)) + for _, content := range value.Contents { + if !validClaudeThinkingReplayContent(content) { + return nil, "", false, false + } + contents = append(contents, append([]byte(nil), content...)) + } + if len(contents) == 0 { + return nil, "", false, false + } + return contents, value.Generation, false, true +} + +func reserveClaudeThinkingReplayLocalLocked(key string, now time.Time) claudeThinkingReplayEntry { + entry := claudeThinkingReplayEntry{Timestamp: now, Generation: uuid.NewString(), Deleted: true} + claudeThinkingReplayEntries[key] = entry + enforceClaudeThinkingReplayLimitsLocked() + return entry +} + +func storeClaudeThinkingReplayLocal(key string, contents [][]byte, generation string, deleted bool, now time.Time) { + cacheCleanupOnce.Do(startCacheCleanup) + claudeThinkingReplayMu.Lock() + defer claudeThinkingReplayMu.Unlock() + if previous, found := claudeThinkingReplayEntries[key]; found { + claudeThinkingReplayTotalBytes -= claudeThinkingReplayEntryBytes(previous.Contents) + } + cloned := cloneClaudeThinkingReplayContents(contents) + claudeThinkingReplayTotalBytes += claudeThinkingReplayEntryBytes(cloned) + claudeThinkingReplayEntries[key] = claudeThinkingReplayEntry{Contents: cloned, Timestamp: now, Generation: generation, Deleted: deleted} + enforceClaudeThinkingReplayLimitsLocked() +} + +func appendClaudeThinkingReplayContent(contents [][]byte, content []byte) [][]byte { + cloned := cloneClaudeThinkingReplayContents(contents) + for _, existing := range cloned { + if claudeThinkingReplayJSONEqual(existing, content) { + return cloned + } + } + cloned = append(cloned, append([]byte(nil), content...)) + for len(cloned) > ClaudeThinkingReplayCacheMaxTurnsPerSession || claudeThinkingReplayEntryBytes(cloned) > ClaudeThinkingReplayCacheMaxBytesPerSession { + if len(cloned) == 0 { + break + } + cloned = cloned[1:] + } + return cloned +} + +func cloneClaudeThinkingReplayContents(contents [][]byte) [][]byte { + cloned := make([][]byte, 0, len(contents)) + for _, content := range contents { + cloned = append(cloned, append([]byte(nil), content...)) + } + return cloned +} + +func claudeThinkingReplayEntryBytes(contents [][]byte) int { + total := 0 + for _, content := range contents { + total += len(content) + } + return total +} + +func claudeThinkingReplayCacheKey(modelFamily, sessionKey string) string { + modelFamily = strings.TrimSpace(modelFamily) + sessionKey = strings.TrimSpace(sessionKey) + if modelFamily == "" || sessionKey == "" { + return "" + } + return strings.Join([]string{"claude-thinking-replay", modelFamily, sessionKey}, "\x00") +} + +func claudeThinkingReplayKVKey(modelFamily, sessionKey string) string { + return "cpa:claude:thinking-replay:" + homekv.HashKeyPart(strings.TrimSpace(modelFamily)) + ":" + homekv.HashKeyPart(strings.TrimSpace(sessionKey)) +} + +func validClaudeThinkingReplayContent(content []byte) bool { + if len(content) == 0 || len(content) > ClaudeThinkingReplayCacheMaxBytesPerSession || !gjson.ValidBytes(content) { + return false + } + root := gjson.ParseBytes(content) + return root.IsArray() && len(root.Array()) > 0 && len(root.Array()) <= ClaudeThinkingReplayCacheMaxBlocksPerTurn +} + +func claudeThinkingReplayJSONEqual(left, right []byte) bool { + leftCanonical, leftOK := claudeThinkingReplayCanonicalJSON(left) + rightCanonical, rightOK := claudeThinkingReplayCanonicalJSON(right) + return leftOK && rightOK && bytes.Equal(leftCanonical, rightCanonical) +} + +func claudeThinkingReplayCanonicalJSON(raw []byte) ([]byte, bool) { + decoder := json.NewDecoder(bytes.NewReader(raw)) + decoder.UseNumber() + var value any + if errDecode := decoder.Decode(&value); errDecode != nil { + return nil, false + } + canonical, errMarshal := json.Marshal(value) + return canonical, errMarshal == nil +} + +func enforceClaudeThinkingReplayLimitsLocked() { + for len(claudeThinkingReplayEntries) > ClaudeThinkingReplayCacheMaxEntries || claudeThinkingReplayTotalBytes > ClaudeThinkingReplayCacheMaxTotalBytes { + if len(claudeThinkingReplayEntries) == 0 { + claudeThinkingReplayTotalBytes = 0 + return + } + evictOldestClaudeThinkingReplayEntriesLocked(ClaudeThinkingReplayCacheEvictBatchSize) + } +} + +func evictOldestClaudeThinkingReplayEntriesLocked(count int) { + if count <= 0 || len(claudeThinkingReplayEntries) == 0 { + return + } + type candidate struct { + key string + timestamp time.Time + } + candidates := make([]candidate, 0, len(claudeThinkingReplayEntries)) + for key, entry := range claudeThinkingReplayEntries { + candidates = append(candidates, candidate{key: key, timestamp: entry.Timestamp}) + } + sort.Slice(candidates, func(i, j int) bool { + return candidates[i].timestamp.Before(candidates[j].timestamp) + }) + if count > len(candidates) { + count = len(candidates) + } + for i := 0; i < count; i++ { + entry := claudeThinkingReplayEntries[candidates[i].key] + claudeThinkingReplayTotalBytes -= claudeThinkingReplayEntryBytes(entry.Contents) + delete(claudeThinkingReplayEntries, candidates[i].key) + } +} + +func purgeExpiredClaudeThinkingReplayCache(now time.Time) { + claudeThinkingReplayMu.Lock() + for key, entry := range claudeThinkingReplayEntries { + if now.Sub(entry.Timestamp) > ClaudeThinkingReplayCacheTTL { + claudeThinkingReplayTotalBytes -= claudeThinkingReplayEntryBytes(entry.Contents) + delete(claudeThinkingReplayEntries, key) + } + } + claudeThinkingReplayMu.Unlock() +} diff --git a/internal/cache/claude_thinking_replay_cache_test.go b/internal/cache/claude_thinking_replay_cache_test.go new file mode 100644 index 00000000..c4ee7c10 --- /dev/null +++ b/internal/cache/claude_thinking_replay_cache_test.go @@ -0,0 +1,89 @@ +package cache + +import ( + "bytes" + "context" + "testing" +) + +func useFakeClaudeThinkingReplayKVClient(t *testing.T, client *fakeKimiThinkingReplayKVClient) { + t.Helper() + previous := currentClaudeThinkingReplayKVClient + currentClaudeThinkingReplayKVClient = func() (kimiThinkingReplayKVClient, bool, error) { + return client, true, nil + } + t.Cleanup(func() { + currentClaudeThinkingReplayKVClient = previous + }) +} + +func TestClaudeThinkingReplayAppendsAssistantTurns(t *testing.T) { + client := newFakeKimiThinkingReplayKVClient() + useFakeClaudeThinkingReplayKVClient(t, client) + + const modelFamily = "claude:auth:model" + const sessionKey = "execution:multi-turn" + first := []byte(`[{"type":"thinking","thinking":"first","signature":"sig-1"},{"type":"tool_use","id":"toolu-1","name":"Read","input":{"path":"one"}}]`) + second := []byte(`[{"type":"thinking","thinking":"second","signature":"sig-2"},{"type":"tool_use","id":"toolu-2","name":"Read","input":{"path":"two"}}]`) + + if !CacheClaudeThinkingReplayBestEffort(context.Background(), modelFamily, sessionKey, first) { + t.Fatal("failed to seed first Claude replay turn") + } + _, snapshot, found, errGet := GetClaudeThinkingReplayWithSnapshotRequired(context.Background(), modelFamily, sessionKey) + if errGet != nil || !found { + t.Fatalf("initial Claude replay read = found %v, error %v", found, errGet) + } + replaced, errReplace := ReplaceClaudeThinkingReplayIfUnchanged(context.Background(), modelFamily, sessionKey, snapshot, second) + if errReplace != nil || !replaced { + t.Fatalf("append Claude replay turn = replaced %v, error %v", replaced, errReplace) + } + + contents, found, errGet := GetClaudeThinkingReplayRequired(context.Background(), modelFamily, sessionKey) + if errGet != nil || !found || len(contents) != 2 { + t.Fatalf("Claude replay contents = %d, found %v, error %v; want two turns", len(contents), found, errGet) + } + if !bytes.Equal(contents[0], first) || !bytes.Equal(contents[1], second) { + t.Fatalf("Claude replay contents lost ordering: got %s / %s", contents[0], contents[1]) + } +} + +func TestClaudeThinkingReplayClearDoesNotClearKimiState(t *testing.T) { + previousClaudeClient := currentClaudeThinkingReplayKVClient + previousKimiClient := currentKimiThinkingReplayKVClient + currentClaudeThinkingReplayKVClient = func() (kimiThinkingReplayKVClient, bool, error) { + return nil, false, nil + } + currentKimiThinkingReplayKVClient = func() (kimiThinkingReplayKVClient, bool, error) { + return nil, false, nil + } + t.Cleanup(func() { + currentClaudeThinkingReplayKVClient = previousClaudeClient + currentKimiThinkingReplayKVClient = previousKimiClient + }) + ClearClaudeThinkingReplayCache() + ClearKimiThinkingReplayCache() + t.Cleanup(ClearClaudeThinkingReplayCache) + t.Cleanup(ClearKimiThinkingReplayCache) + + const modelFamily = "shared-model" + const sessionKey = "execution:shared-session" + kimiContent := []byte(`[{"type":"thinking","signature":"kimi"}]`) + claudeContent := []byte(`[{"type":"thinking","signature":"claude"}]`) + if !CacheKimiThinkingReplayBestEffort(context.Background(), modelFamily, sessionKey, kimiContent) { + t.Fatal("failed to seed Kimi replay state") + } + if !CacheClaudeThinkingReplayBestEffort(context.Background(), modelFamily, sessionKey, claudeContent) { + t.Fatal("failed to seed Claude replay state") + } + + ClearClaudeThinkingReplayCache() + + gotKimi, foundKimi, errKimi := GetKimiThinkingReplayRequired(context.Background(), modelFamily, sessionKey) + if errKimi != nil || !foundKimi || !bytes.Equal(gotKimi, kimiContent) { + t.Fatalf("Kimi replay after Claude clear = %s, found %v, error %v; want preserved state", gotKimi, foundKimi, errKimi) + } + gotClaude, foundClaude, errClaude := GetClaudeThinkingReplayRequired(context.Background(), modelFamily, sessionKey) + if errClaude != nil || foundClaude || len(gotClaude) != 0 { + t.Fatalf("Claude replay after Claude clear = %d turns, found %v, error %v; want cleared state", len(gotClaude), foundClaude, errClaude) + } +} diff --git a/internal/cache/signature_cache.go b/internal/cache/signature_cache.go index e9630d5c..3ca339bc 100644 --- a/internal/cache/signature_cache.go +++ b/internal/cache/signature_cache.go @@ -112,6 +112,7 @@ func purgeExpiredCaches() { purgeExpiredXAIReasoningReplayCache(now) purgeExpiredAntigravityReasoningReplayCache(now) purgeExpiredKimiThinkingReplayCache(now) + purgeExpiredClaudeThinkingReplayCache(now) } // CacheSignature stores a thinking signature for a given model group and text. diff --git a/internal/config/config_types.go b/internal/config/config_types.go index 134109fd..599bd123 100644 --- a/internal/config/config_types.go +++ b/internal/config/config_types.go @@ -388,7 +388,8 @@ type ClaudeModel struct { // ForceMapping rewrites upstream response model fields back to Alias. ForceMapping bool `yaml:"force-mapping,omitempty" json:"force-mapping,omitempty"` - // IsCompat preserves thinking blocks with empty signatures for compatible upstreams. + // IsCompat preserves thinking blocks with empty signatures for compatible upstreams + // and enables provider-aware signed-thinking replay for Claude-compatible API-key models. // Default false keeps the normal signature validation behavior. IsCompat bool `yaml:"is-compat,omitempty" json:"is-compat,omitempty"` diff --git a/internal/runtime/executor/claude_executor_execute.go b/internal/runtime/executor/claude_executor_execute.go index c8436e48..64cc1cb7 100644 --- a/internal/runtime/executor/claude_executor_execute.go +++ b/internal/runtime/executor/claude_executor_execute.go @@ -36,6 +36,15 @@ func (e *ClaudeExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, r from := opts.SourceFormat responseFormat := cliproxyexecutor.ResponseFormatOrSource(opts) to := sdktranslator.FromString("claude") + var replayScope claudeThinkingReplayScope + if claudeThinkingReplayEnabled(auth, req, opts) { + req, replayScope = prepareClaudeThinkingReplayRequest(ctx, auth, req, opts) + } + defer func() { + if err != nil && replayScope.replayApplied && shouldClearKimiThinkingReplayAfterError(err) { + clearClaudeThinkingReplayContent(ctx, replayScope) + } + }() // Use an upstream stream whenever the downstream response needs translation // from Claude events. Native Claude responses use the JSON response path. upstreamStream := responseFormat != to @@ -240,6 +249,7 @@ func (e *ClaudeExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, r data = restoreClaudeOAuthToolNamesFromResponse(data, oauthToolNamesReverseMap) } data = e.restoreResponseModel(data, req.Model) + cacheClaudeThinkingReplayResponse(ctx, replayScope, data) var param any out := sdktranslator.TranslateNonStream( ctx, diff --git a/internal/runtime/executor/claude_executor_stream.go b/internal/runtime/executor/claude_executor_stream.go index 93caef89..99cfaff9 100644 --- a/internal/runtime/executor/claude_executor_stream.go +++ b/internal/runtime/executor/claude_executor_stream.go @@ -43,6 +43,15 @@ func (e *ClaudeExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A from := opts.SourceFormat responseFormat := cliproxyexecutor.ResponseFormatOrSource(opts) to := sdktranslator.FromString("claude") + var replayScope claudeThinkingReplayScope + if claudeThinkingReplayEnabled(auth, req, opts) { + req, replayScope = prepareClaudeThinkingReplayRequest(ctx, auth, req, opts) + } + defer func() { + if err != nil && replayScope.replayApplied && shouldClearKimiThinkingReplayAfterError(err) { + clearClaudeThinkingReplayContent(ctx, replayScope) + } + }() originalPayloadSource := req.Payload if len(opts.OriginalRequest) > 0 { originalPayloadSource = opts.OriginalRequest @@ -339,7 +348,11 @@ func (e *ClaudeExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A commitClaudeDiagnostics(diagnosticsState, upstreamMessageID) } }() - return &cliproxyexecutor.StreamResult{Headers: httpResp.Header.Clone(), Chunks: out}, nil + result := &cliproxyexecutor.StreamResult{Headers: httpResp.Header.Clone(), Chunks: out} + if replayScope.valid() { + result = wrapClaudeThinkingReplayStream(ctx, result, replayScope) + } + return result, nil } func validateClaudeStreamingResponse(data []byte) error { diff --git a/internal/runtime/executor/claude_thinking_replay.go b/internal/runtime/executor/claude_thinking_replay.go new file mode 100644 index 00000000..936a9a33 --- /dev/null +++ b/internal/runtime/executor/claude_thinking_replay.go @@ -0,0 +1,140 @@ +package executor + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "strings" + + internalcache "github.com/router-for-me/CLIProxyAPI/v7/internal/cache" + "github.com/router-for-me/CLIProxyAPI/v7/internal/runtime/executor/helps" + "github.com/router-for-me/CLIProxyAPI/v7/internal/thinking" + cliproxyauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" + cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/executor" + sdktranslator "github.com/router-for-me/CLIProxyAPI/v7/sdk/translator" + log "github.com/sirupsen/logrus" + "github.com/tidwall/gjson" +) + +// claudeThinkingReplayScope reuses the bounded replay state shape shared with Kimi. +type claudeThinkingReplayScope = kimiThinkingReplayScope + +func claudeThinkingReplayEnabled(auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) bool { + if auth == nil || !sourceFormatEqual(opts.SourceFormat, sdktranslator.FormatClaude) { + return false + } + if !strings.EqualFold(strings.TrimSpace(auth.Provider), "claude") || auth.AuthKind() != cliproxyauth.AuthKindAPIKey { + return false + } + if !helps.APIKeyModelIsCompat(req) { + return false + } + apiKey, _ := claudeCreds(auth) + return strings.TrimSpace(apiKey) != "" && !isClaudeOAuthToken(apiKey) +} + +// A missing session identity intentionally disables replay instead of sharing hidden reasoning across callers. +func claudeThinkingReplayScopeFromRequest(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) claudeThinkingReplayScope { + sessionKey := codexReasoningReplaySessionKey(ctx, sdktranslator.FormatClaude, req, opts, req.Payload) + sessionKey = xaiReasoningReplayIsolateSessionKey(ctx, sessionKey) + return claudeThinkingReplayScope{ + modelFamily: claudeThinkingReplayModelFamily(auth, req.Model), + sessionKey: sessionKey, + } +} + +func claudeThinkingReplayModelFamily(auth *cliproxyauth.Auth, model string) string { + baseModel := thinking.ParseSuffix(strings.TrimSpace(model)).ModelName + if baseModel == "" { + return "" + } + identity := "" + if auth != nil { + identity = strings.TrimSpace(auth.ID) + if identity == "" { + apiKey, baseURL := claudeCreds(auth) + identity = strings.TrimSpace(baseURL) + if identity == "" { + identity = strings.TrimSpace(apiKey) + } + } + } + if identity == "" { + return "claude:" + baseModel + } + sum := sha256.Sum256([]byte(identity)) + return "claude:" + hex.EncodeToString(sum[:8]) + ":" + baseModel +} + +func prepareClaudeThinkingReplayRequest(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Request, claudeThinkingReplayScope) { + scope := claudeThinkingReplayScopeFromRequest(ctx, auth, req, opts) + if !scope.valid() { + return req, scope + } + contents, snapshot, found, errGet := internalcache.GetClaudeThinkingReplayWithSnapshotRequired(ctx, scope.modelFamily, scope.sessionKey) + scope.snapshot = snapshot + scope.cacheReady = errGet == nil + if errGet != nil { + log.Warnf("claude compatible thinking replay cache read failed: %v", errGet) + return req, scope + } + if !found { + return req, scope + } + updated, restored := restoreClaudeThinkingReplayContents(req.Payload, contents) + if restored { + req.Payload = updated + scope.replayApplied = true + } + return req, scope +} + +func restoreClaudeThinkingReplayContents(body []byte, cachedContents [][]byte) ([]byte, bool) { + updated := body + restored := false + for _, cachedContent := range cachedContents { + var restoredTurn bool + updated, restoredTurn = restoreKimiThinkingReplayContent(updated, cachedContent) + restored = restored || restoredTurn + } + return updated, restored +} + +func cacheClaudeThinkingReplayResponse(ctx context.Context, scope claudeThinkingReplayScope, response []byte) { + content := gjson.GetBytes(response, "content") + if content.IsArray() { + cacheClaudeThinkingReplayContent(ctx, scope, []byte(content.Raw)) + return + } + accumulator := newKimiThinkingReplayStreamAccumulator() + accumulator.observe(response) + if content, completed := accumulator.content(); completed { + cacheClaudeThinkingReplayContent(ctx, scope, content) + } +} + +func cacheClaudeThinkingReplayContent(ctx context.Context, scope claudeThinkingReplayScope, content []byte) { + if !scope.valid() || !scope.cacheReady { + return + } + if kimiThinkingReplayContentIsReplayable(content) { + if _, errReplace := internalcache.ReplaceClaudeThinkingReplayIfUnchanged(ctx, scope.modelFamily, scope.sessionKey, scope.snapshot, content); errReplace != nil { + log.Warnf("claude compatible thinking replay cache replace failed: %v", errReplace) + } + return + } + clearClaudeThinkingReplayContent(ctx, scope) +} + +func clearClaudeThinkingReplayContent(ctx context.Context, scope claudeThinkingReplayScope) { + if !scope.valid() || !scope.cacheReady { + return + } + if _, errDelete := internalcache.DeleteClaudeThinkingReplayIfUnchanged(ctx, scope.modelFamily, scope.sessionKey, scope.snapshot); errDelete != nil { + log.Warnf("claude compatible thinking replay cache delete failed: %v", errDelete) + } +} + +func wrapClaudeThinkingReplayStream(ctx context.Context, result *cliproxyexecutor.StreamResult, scope claudeThinkingReplayScope) *cliproxyexecutor.StreamResult { + return wrapThinkingReplayStream(ctx, result, scope, cacheClaudeThinkingReplayContent, clearClaudeThinkingReplayContent) +} diff --git a/internal/runtime/executor/claude_thinking_replay_test.go b/internal/runtime/executor/claude_thinking_replay_test.go new file mode 100644 index 00000000..14c92856 --- /dev/null +++ b/internal/runtime/executor/claude_thinking_replay_test.go @@ -0,0 +1,374 @@ +package executor + +import ( + "bytes" + "context" + "io" + "net/http" + "net/http/httptest" + "sync" + "testing" + + internalcache "github.com/router-for-me/CLIProxyAPI/v7/internal/cache" + "github.com/router-for-me/CLIProxyAPI/v7/internal/registry" + cliproxyauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" + cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/executor" + sdktranslator "github.com/router-for-me/CLIProxyAPI/v7/sdk/translator" + "github.com/tidwall/gjson" +) + +const claudeReplayResolvedModelInfoKey = "cliproxy.resolved_api_key_model_info" + +func claudeReplayTestAuth(baseURL string) *cliproxyauth.Auth { + return &cliproxyauth.Auth{ + ID: "claude-replay-auth", + Provider: "claude", + Attributes: map[string]string{ + cliproxyauth.AttributeAPIKey: "key-claude-replay", + cliproxyauth.AttributeAuthKind: cliproxyauth.AuthKindAPIKey, + "base_url": baseURL, + }, + } +} + +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, + }, + } +} + +func TestClaudeThinkingReplayEnabledRequiresCompatClaudeAPIKey(t *testing.T) { + baseRequest, baseOptions := claudeReplayTestRequest([]byte(`{"messages":[]}`), "scope", true, sdktranslator.FormatClaude) + baseAuth := claudeReplayTestAuth("http://127.0.0.1") + + tests := []struct { + name string + auth *cliproxyauth.Auth + request cliproxyexecutor.Request + options cliproxyexecutor.Options + wantEnable bool + }{ + { + name: "compat Claude API key", + auth: baseAuth, + request: baseRequest, + options: baseOptions, + wantEnable: true, + }, + { + name: "non compat model", + auth: baseAuth, + request: func() cliproxyexecutor.Request { + request, _ := claudeReplayTestRequest([]byte(`{"messages":[]}`), "scope-non-compat", false, sdktranslator.FormatClaude) + return request + }(), + options: baseOptions, + wantEnable: false, + }, + { + name: "OAuth credential", + auth: func() *cliproxyauth.Auth { + auth := baseAuth.Clone() + auth.Attributes[cliproxyauth.AttributeAuthKind] = cliproxyauth.AuthKindOAuth + auth.Attributes[cliproxyauth.AttributeAPIKey] = "sk-ant-oat-replay" + return auth + }(), + request: baseRequest, + options: baseOptions, + wantEnable: false, + }, + { + name: "other provider", + auth: func() *cliproxyauth.Auth { + auth := baseAuth.Clone() + auth.Provider = "kimi" + return auth + }(), + request: baseRequest, + options: baseOptions, + wantEnable: false, + }, + { + name: "OpenAI source format", + auth: baseAuth, + request: baseRequest, + options: func() cliproxyexecutor.Options { + options := baseOptions + options.SourceFormat = sdktranslator.FormatOpenAI + return options + }(), + wantEnable: false, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + if got := claudeThinkingReplayEnabled(test.auth, test.request, test.options); got != test.wantEnable { + t.Fatalf("claudeThinkingReplayEnabled() = %v, want %v", got, test.wantEnable) + } + }) + } +} + +func TestClaudeExecutorCompatThinkingReplayRestoresOmittedBlock(t *testing.T) { + internalcacheClearClaudeThinkingReplay(t) + + var mu sync.Mutex + var requestBodies [][]byte + callCount := 0 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + body, errRead := io.ReadAll(r.Body) + if errRead != nil { + t.Errorf("read request body: %v", errRead) + return + } + mu.Lock() + requestBodies = append(requestBodies, bytes.Clone(body)) + callCount++ + call := callCount + mu.Unlock() + + w.Header().Set("Content-Type", "application/json") + if call == 1 { + _, _ = w.Write([]byte(`{"id":"msg-1","type":"message","role":"assistant","model":"claude-synthetic-4772","content":[{"type":"thinking","thinking":"provider reasoning","signature":"EgI="},{"type":"tool_use","id":"toolu_1","name":"Read","input":{"path":"README.md"}}],"stop_reason":"tool_use"}`)) + return + } + _, _ = w.Write([]byte(`{"id":"msg-2","type":"message","role":"assistant","model":"claude-synthetic-4772","content":[{"type":"text","text":"done"}],"stop_reason":"end_turn"}`)) + })) + defer server.Close() + + executor := NewClaudeExecutor(nil) + auth := claudeReplayTestAuth(server.URL) + firstPayload := []byte(`{"messages":[{"role":"user","content":"inspect"}]}`) + firstRequest, firstOptions := claudeReplayTestRequest(firstPayload, "nonstream-replay", true, sdktranslator.FormatClaude) + if _, errExecute := executor.Execute(context.Background(), auth, firstRequest, firstOptions); errExecute != nil { + t.Fatalf("first Execute() error = %v", errExecute) + } + + secondPayload := []byte(`{"messages":[{"role":"user","content":"inspect"},{"role":"assistant","content":[{"type":"tool_use","id":"toolu_1","name":"Read","input":{"path":"README.md"}}]},{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu_1","content":"ok"}]}]}`) + secondRequest, secondOptions := claudeReplayTestRequest(secondPayload, "nonstream-replay", true, sdktranslator.FormatClaude) + if _, errExecute := executor.Execute(context.Background(), auth, secondRequest, secondOptions); errExecute != nil { + t.Fatalf("second Execute() error = %v", errExecute) + } + + mu.Lock() + defer mu.Unlock() + if len(requestBodies) != 2 { + t.Fatalf("upstream request count = %d, want 2", len(requestBodies)) + } + content := gjson.GetBytes(requestBodies[1], "messages.1.content").Array() + if len(content) != 2 { + t.Fatalf("second assistant content = %s, want thinking and tool_use", gjson.GetBytes(requestBodies[1], "messages.1.content").Raw) + } + if got := content[0].Get("type").String(); got != "thinking" { + t.Fatalf("restored first content type = %q, want thinking", got) + } + if got := content[0].Get("signature").String(); got != "EgI=" { + t.Fatalf("restored signature = %q, want EgI=", got) + } +} + +func TestClaudeExecutorCompatThinkingReplayRestoresOmittedBlockInStream(t *testing.T) { + internalcacheClearClaudeThinkingReplay(t) + + var mu sync.Mutex + var requestBodies [][]byte + callCount := 0 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + body, errRead := io.ReadAll(r.Body) + if errRead != nil { + t.Errorf("read request body: %v", errRead) + return + } + mu.Lock() + requestBodies = append(requestBodies, bytes.Clone(body)) + callCount++ + call := callCount + mu.Unlock() + + w.Header().Set("Content-Type", "text/event-stream") + if call == 1 { + _, _ = w.Write([]byte(claudeReplayThinkingStream())) + return + } + _, _ = w.Write([]byte("event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":\"msg-2\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[]}}\n\n" + + "event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n")) + })) + defer server.Close() + + executor := NewClaudeExecutor(nil) + auth := claudeReplayTestAuth(server.URL) + firstPayload := []byte(`{"messages":[{"role":"user","content":"inspect"}]}`) + firstRequest, firstOptions := claudeReplayTestRequest(firstPayload, "stream-replay", true, sdktranslator.FormatClaude) + firstResult, errExecute := executor.ExecuteStream(context.Background(), auth, firstRequest, firstOptions) + if errExecute != nil { + t.Fatalf("first ExecuteStream() error = %v", errExecute) + } + for chunk := range firstResult.Chunks { + if chunk.Err != nil { + t.Fatalf("first stream error: %v", chunk.Err) + } + } + + secondPayload := []byte(`{"messages":[{"role":"user","content":"inspect"},{"role":"assistant","content":[{"type":"tool_use","id":"toolu_1","name":"Read","input":{"path":"README.md"}}]},{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu_1","content":"ok"}]}]}`) + secondRequest, secondOptions := claudeReplayTestRequest(secondPayload, "stream-replay", true, sdktranslator.FormatClaude) + secondResult, errExecute := executor.ExecuteStream(context.Background(), auth, secondRequest, secondOptions) + if errExecute != nil { + t.Fatalf("second ExecuteStream() error = %v", errExecute) + } + for chunk := range secondResult.Chunks { + if chunk.Err != nil { + t.Fatalf("second stream error: %v", chunk.Err) + } + } + + mu.Lock() + defer mu.Unlock() + if len(requestBodies) != 2 { + t.Fatalf("upstream request count = %d, want 2", len(requestBodies)) + } + content := gjson.GetBytes(requestBodies[1], "messages.1.content").Array() + if len(content) != 2 || content[0].Get("type").String() != "thinking" { + t.Fatalf("second streamed assistant content = %s, want restored thinking and tool_use", gjson.GetBytes(requestBodies[1], "messages.1.content").Raw) + } + if got := content[0].Get("signature").String(); got != "EgI=" { + t.Fatalf("restored streamed signature = %q, want EgI=", got) + } +} + +func claudeReplayThinkingStream() string { + return "event: message_start\n" + + "data: {\"type\":\"message_start\",\"message\":{\"id\":\"msg-1\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[]}}\n\n" + + "event: content_block_start\n" + + "data: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"thinking\",\"thinking\":\"\",\"signature\":\"\"}}\n\n" + + "event: content_block_delta\n" + + "data: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"thinking_delta\",\"thinking\":\"provider reasoning\"}}\n\n" + + "event: content_block_delta\n" + + "data: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"signature_delta\",\"signature\":\"EgI=\"}}\n\n" + + "event: content_block_stop\n" + + "data: {\"type\":\"content_block_stop\",\"index\":0}\n\n" + + "event: content_block_start\n" + + "data: {\"type\":\"content_block_start\",\"index\":1,\"content_block\":{\"type\":\"tool_use\",\"id\":\"toolu_1\",\"name\":\"Read\",\"input\":{}}}\n\n" + + "event: content_block_delta\n" + + "data: {\"type\":\"content_block_delta\",\"index\":1,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"path\\\":\\\"README.md\\\"}\"}}\n\n" + + "event: content_block_stop\n" + + "data: {\"type\":\"content_block_stop\",\"index\":1}\n\n" + + "event: message_stop\n" + + "data: {\"type\":\"message_stop\"}\n\n" +} + +func TestClaudeExecutorCompatThinkingReplayClearsAfterUpstreamBadRequest(t *testing.T) { + internalcacheClearClaudeThinkingReplay(t) + + callCount := 0 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + callCount++ + if callCount == 1 { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"id":"msg-1","type":"message","role":"assistant","model":"claude-synthetic-4772","content":[{"type":"thinking","thinking":"reasoning","signature":"EgI="},{"type":"tool_use","id":"toolu-1","name":"Read","input":{"path":"README.md"}}],"stop_reason":"tool_use"}`)) + return + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusBadRequest) + _, _ = w.Write([]byte(`{"type":"error","error":{"type":"invalid_request_error","message":"invalid thinking signature"}}`)) + })) + defer server.Close() + + executor := NewClaudeExecutor(nil) + auth := claudeReplayTestAuth(server.URL) + firstRequest, firstOptions := claudeReplayTestRequest([]byte(`{"messages":[{"role":"user","content":"inspect"}]}`), "bad-request-replay", true, sdktranslator.FormatClaude) + if _, errExecute := executor.Execute(context.Background(), auth, firstRequest, firstOptions); errExecute != nil { + t.Fatalf("first Execute() error = %v", errExecute) + } + + secondPayload := []byte(`{"messages":[{"role":"user","content":"inspect"},{"role":"assistant","content":[{"type":"tool_use","id":"toolu-1","name":"Read","input":{"path":"README.md"}}]},{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu-1","content":"ok"}]}]}`) + secondRequest, secondOptions := claudeReplayTestRequest(secondPayload, "bad-request-replay", true, sdktranslator.FormatClaude) + if _, errExecute := executor.Execute(context.Background(), auth, secondRequest, secondOptions); errExecute == nil { + t.Fatal("second Execute() error = nil, want upstream bad request") + } + + scope := claudeThinkingReplayScopeFromRequest(context.Background(), auth, firstRequest, firstOptions) + _, found, errGet := internalcache.GetClaudeThinkingReplayRequired(context.Background(), scope.modelFamily, scope.sessionKey) + if errGet != nil || found { + t.Fatalf("replay after upstream bad request = found %v, error %v; want cleared state", found, errGet) + } +} + +func TestClaudeExecutorCompatThinkingReplayRestoresMultipleOmittedBlocks(t *testing.T) { + internalcacheClearClaudeThinkingReplay(t) + + var mu sync.Mutex + var requestBodies [][]byte + callCount := 0 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + body, errRead := io.ReadAll(r.Body) + if errRead != nil { + t.Errorf("read request body: %v", errRead) + return + } + mu.Lock() + requestBodies = append(requestBodies, bytes.Clone(body)) + callCount++ + call := callCount + mu.Unlock() + + w.Header().Set("Content-Type", "application/json") + switch call { + case 1: + _, _ = w.Write([]byte(`{"id":"msg-1","type":"message","role":"assistant","model":"claude-synthetic-4772","content":[{"type":"thinking","thinking":"first","signature":"EgI="},{"type":"tool_use","id":"toolu-1","name":"Read","input":{"path":"one"}}],"stop_reason":"tool_use"}`)) + case 2: + _, _ = w.Write([]byte(`{"id":"msg-2","type":"message","role":"assistant","model":"claude-synthetic-4772","content":[{"type":"thinking","thinking":"second","signature":"EgM="},{"type":"tool_use","id":"toolu-2","name":"Read","input":{"path":"two"}}],"stop_reason":"tool_use"}`)) + default: + _, _ = w.Write([]byte(`{"id":"msg-3","type":"message","role":"assistant","model":"claude-synthetic-4772","content":[{"type":"text","text":"done"}],"stop_reason":"end_turn"}`)) + } + })) + defer server.Close() + + executor := NewClaudeExecutor(nil) + auth := claudeReplayTestAuth(server.URL) + firstRequest, firstOptions := claudeReplayTestRequest([]byte(`{"messages":[{"role":"user","content":"inspect"}]}`), "multi-turn-replay", true, sdktranslator.FormatClaude) + if _, errExecute := executor.Execute(context.Background(), auth, firstRequest, firstOptions); errExecute != nil { + t.Fatalf("first Execute() error = %v", errExecute) + } + + secondPayload := []byte(`{"messages":[{"role":"user","content":"inspect"},{"role":"assistant","content":[{"type":"tool_use","id":"toolu-1","name":"Read","input":{"path":"one"}}]},{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu-1","content":"one result"}]}]}`) + secondRequest, secondOptions := claudeReplayTestRequest(secondPayload, "multi-turn-replay", true, sdktranslator.FormatClaude) + if _, errExecute := executor.Execute(context.Background(), auth, secondRequest, secondOptions); errExecute != nil { + t.Fatalf("second Execute() error = %v", errExecute) + } + + thirdPayload := []byte(`{"messages":[{"role":"user","content":"inspect"},{"role":"assistant","content":[{"type":"tool_use","id":"toolu-1","name":"Read","input":{"path":"one"}}]},{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu-1","content":"one result"}]},{"role":"assistant","content":[{"type":"tool_use","id":"toolu-2","name":"Read","input":{"path":"two"}}]},{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu-2","content":"two result"}]}]}`) + thirdRequest, thirdOptions := claudeReplayTestRequest(thirdPayload, "multi-turn-replay", true, sdktranslator.FormatClaude) + if _, errExecute := executor.Execute(context.Background(), auth, thirdRequest, thirdOptions); errExecute != nil { + t.Fatalf("third Execute() error = %v", errExecute) + } + + mu.Lock() + defer mu.Unlock() + if len(requestBodies) != 3 { + t.Fatalf("upstream request count = %d, want 3", len(requestBodies)) + } + firstContent := gjson.GetBytes(requestBodies[2], "messages.1.content").Array() + secondContent := gjson.GetBytes(requestBodies[2], "messages.3.content").Array() + if len(firstContent) != 2 || firstContent[0].Get("type").String() != "thinking" || firstContent[0].Get("signature").String() != "EgI=" { + t.Fatalf("first omitted turn was not restored: %s", gjson.GetBytes(requestBodies[2], "messages.1.content").Raw) + } + if len(secondContent) != 2 || secondContent[0].Get("type").String() != "thinking" || secondContent[0].Get("signature").String() != "EgM=" { + t.Fatalf("second omitted turn was not restored: %s", gjson.GetBytes(requestBodies[2], "messages.3.content").Raw) + } +} + +func internalcacheClearClaudeThinkingReplay(t *testing.T) { + t.Helper() + internalcache.ClearClaudeThinkingReplayCache() + t.Cleanup(internalcache.ClearClaudeThinkingReplayCache) +} diff --git a/internal/runtime/executor/kimi_thinking_replay.go b/internal/runtime/executor/kimi_thinking_replay.go index a670c2d7..563ef297 100644 --- a/internal/runtime/executor/kimi_thinking_replay.go +++ b/internal/runtime/executor/kimi_thinking_replay.go @@ -441,7 +441,10 @@ func (a *kimiThinkingReplayStreamAccumulator) content() ([]byte, bool) { return content, true } -func wrapKimiThinkingReplayStream(ctx context.Context, result *cliproxyexecutor.StreamResult, scope kimiThinkingReplayScope) *cliproxyexecutor.StreamResult { +type thinkingReplayContentCacheFunc func(context.Context, kimiThinkingReplayScope, []byte) +type thinkingReplayContentClearFunc func(context.Context, kimiThinkingReplayScope) + +func wrapThinkingReplayStream(ctx context.Context, result *cliproxyexecutor.StreamResult, scope kimiThinkingReplayScope, cacheContent thinkingReplayContentCacheFunc, clearContent thinkingReplayContentClearFunc) *cliproxyexecutor.StreamResult { if result == nil || !scope.valid() { return result } @@ -466,12 +469,16 @@ func wrapKimiThinkingReplayStream(ctx context.Context, result *cliproxyexecutor. return } if content, completed := accumulator.content(); completed { - cacheKimiThinkingReplayContent(ctx, scope, content) + cacheContent(ctx, scope, content) return } if accumulator.upstreamError && scope.replayApplied { - clearKimiThinkingReplayContent(ctx, scope) + clearContent(ctx, scope) } }() return &cliproxyexecutor.StreamResult{Headers: result.Headers.Clone(), Chunks: out} } + +func wrapKimiThinkingReplayStream(ctx context.Context, result *cliproxyexecutor.StreamResult, scope kimiThinkingReplayScope) *cliproxyexecutor.StreamResult { + return wrapThinkingReplayStream(ctx, result, scope, cacheKimiThinkingReplayContent, clearKimiThinkingReplayContent) +}