Skip to content

fix Kimi Claude tool-use reasoning replay#3719

Closed
MunMunMiao wants to merge 3 commits into
router-for-me:devfrom
MunMunMiao:fix/kimi-claude-reasoning-content
Closed

fix Kimi Claude tool-use reasoning replay#3719
MunMunMiao wants to merge 3 commits into
router-for-me:devfrom
MunMunMiao:fix/kimi-claude-reasoning-content

Conversation

@MunMunMiao

Copy link
Copy Markdown

Summary

  • Normalize Kimi Claude-source requests before delegating to the Claude executor.
  • Add reasoning_content to Claude-format assistant messages that contain tool_use blocks, using existing reasoning, thinking, or text content as fallback.
  • Apply the same preprocessing to non-streaming, streaming, and count_tokens Kimi Claude-source paths.

Root cause

Kimi Claude-source traffic was delegated directly to ClaudeExecutor, which skipped Kimi's existing tool-message normalizer. When thinking is enabled, Kimi rejects assistant tool-call history that lacks reasoning_content, causing 400 responses such as thinking is enabled but reasoning_content is missing in assistant tool call message.

Testing

  • go test ./internal/runtime/executor -count=1
  • go build -o test-output ./cmd/server && rm test-output
  • go test ./... -count=1

@github-actions github-actions Bot changed the base branch from main to dev June 5, 2026 02:46
@github-actions

github-actions Bot commented Jun 5, 2026

Copy link
Copy Markdown

This pull request targeted main.

The base branch has been automatically changed to dev.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces reasoning normalization for Claude-sourced requests in the Kimi executor, ensuring that assistant tool-use messages are properly patched with reasoning content. The feedback highlights a critical concurrency issue where mutating the shared auth attributes map directly could lead to data races and panics, and suggests cloning the auth object. Additionally, opportunities for optimization and simplification were identified, such as caching the parsed reasoning text to avoid redundant JSON parsing and removing duplicate fallback checks.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +340 to +355
func prepareKimiClaudeSourceRequest(auth *cliproxyauth.Auth, req cliproxyexecutor.Request) (*cliproxyauth.Auth, cliproxyexecutor.Request, error) {
if auth == nil {
auth = &cliproxyauth.Auth{}
}
if auth.Attributes == nil {
auth.Attributes = make(map[string]string)
}
auth.Attributes["base_url"] = kimiauth.KimiAPIBaseURL

body, err := normalizeKimiClaudeToolUseReasoning(req.Payload)
if err != nil {
return auth, req, err
}
req.Payload = body
return auth, req, nil
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Mutating auth.Attributes directly can lead to concurrent map write panics if the auth object is shared or reused across concurrent requests. To prevent data races and side effects, we should clone the auth object and its Attributes map before modifying them.

func prepareKimiClaudeSourceRequest(auth *cliproxyauth.Auth, req cliproxyexecutor.Request) (*cliproxyauth.Auth, cliproxyexecutor.Request, error) {
	var clonedAuth *cliproxyauth.Auth
	if auth != nil {
		cloned := *auth
		clonedAuth = &cloned
	} else {
		clonedAuth = &cliproxyauth.Auth{}
	}

	newAttrs := make(map[string]string, len(clonedAuth.Attributes)+1)
	for k, v := range clonedAuth.Attributes {
		newAttrs[k] = v
	}
	newAttrs["base_url"] = kimiauth.KimiAPIBaseURL
	clonedAuth.Attributes = newAttrs

	body, err := normalizeKimiClaudeToolUseReasoning(req.Payload)
	if err != nil {
		return clonedAuth, req, err
	}
	req.Payload = body
	return clonedAuth, req, nil
}

Comment on lines +377 to +391
if reasoningText := kimiClaudeAssistantReasoningText(msg); reasoningText != "" {
latestReasoning = reasoningText
hasLatestReasoning = true
}

if !hasKimiClaudeToolUse(msg) {
continue
}

reasoning := msg.Get("reasoning_content")
if reasoning.Exists() && strings.TrimSpace(reasoning.String()) != "" {
continue
}

reasoningText := fallbackKimiClaudeAssistantReasoning(msg, hasLatestReasoning, latestReasoning)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

We can optimize the reasoning extraction by storing the result of kimiClaudeAssistantReasoningText(msg) in a local variable. This avoids parsing the content array twice for messages that need patching, and allows us to skip calling fallbackKimiClaudeAssistantReasoning entirely when the current message already has reasoning.

		currentReasoning := kimiClaudeAssistantReasoningText(msg)
		if currentReasoning != "" {
			latestReasoning = currentReasoning
			hasLatestReasoning = true
		}

		if !hasKimiClaudeToolUse(msg) {
			continue
		}

		reasoning := msg.Get("reasoning_content")
		if reasoning.Exists() && strings.TrimSpace(reasoning.String()) != "" {
			continue
		}

		reasoningText := currentReasoning
		if reasoningText == "" {
			reasoningText = fallbackKimiClaudeAssistantReasoning(msg, hasLatestReasoning, latestReasoning)
		}

Comment on lines +444 to +450
func fallbackKimiClaudeAssistantReasoning(msg gjson.Result, hasLatest bool, latest string) string {
if reasoningText := kimiClaudeAssistantReasoningText(msg); reasoningText != "" {
return reasoningText
}
if hasLatest && strings.TrimSpace(latest) != "" {
return latest
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since normalizeKimiClaudeToolUseReasoning now handles the non-empty currentReasoning check directly, we can simplify fallbackKimiClaudeAssistantReasoning by removing the redundant call to kimiClaudeAssistantReasoningText.

Suggested change
func fallbackKimiClaudeAssistantReasoning(msg gjson.Result, hasLatest bool, latest string) string {
if reasoningText := kimiClaudeAssistantReasoningText(msg); reasoningText != "" {
return reasoningText
}
if hasLatest && strings.TrimSpace(latest) != "" {
return latest
}
func fallbackKimiClaudeAssistantReasoning(msg gjson.Result, hasLatest bool, latest string) string {
if hasLatest && strings.TrimSpace(latest) != "" {
return latest
}

@MunMunMiao MunMunMiao marked this pull request as draft June 5, 2026 03:13
@MunMunMiao MunMunMiao force-pushed the fix/kimi-claude-reasoning-content branch from 1070325 to 1360d49 Compare June 5, 2026 03:26
@MunMunMiao MunMunMiao force-pushed the fix/kimi-claude-reasoning-content branch from 1360d49 to bc47ef7 Compare June 5, 2026 03:31
@MunMunMiao MunMunMiao marked this pull request as ready for review June 5, 2026 03:34
@AndersJet

Copy link
Copy Markdown

useful! thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants