Fix needsThinkingRecovery to require tool loop context

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Badri Narayanan S
2026-01-03 23:24:52 +05:30
parent dc65499c49
commit 12e427e9d5
2 changed files with 12 additions and 11 deletions

View File

@@ -83,7 +83,7 @@ export function convertAnthropicToGoogle(anthropicRequest) {
let processedMessages = messages;
const targetFamily = isClaudeModel ? 'claude' : isGeminiModel ? 'gemini' : null;
if (isThinking && targetFamily && needsThinkingRecovery(messages, targetFamily)) {
if (isThinking && targetFamily && needsThinkingRecovery(messages)) {
logger.debug(`[RequestConverter] Applying thinking recovery for ${targetFamily}`);
processedMessages = closeToolLoopForThinking(messages, targetFamily);
}

View File

@@ -388,23 +388,24 @@ export function analyzeConversationState(messages) {
/**
* Check if conversation needs thinking recovery.
*
* For Gemini: recovery needed when (tool loop OR interrupted tool) AND no valid thinking
* For Claude: recovery needed when no valid compatible thinking (cross-model detection)
* Recovery is only needed when:
* 1. We're in a tool loop or have an interrupted tool, AND
* 2. No valid thinking blocks exist in the current turn
*
* Cross-model signature compatibility is handled by stripInvalidThinkingBlocks
* during recovery (not here).
*
* @param {Array<Object>} messages - Array of messages
* @param {string} targetFamily - Target model family ('claude' or 'gemini')
* @returns {boolean} True if thinking recovery is needed
*/
export function needsThinkingRecovery(messages, targetFamily = null) {
export function needsThinkingRecovery(messages) {
const state = analyzeConversationState(messages);
if (targetFamily === 'claude') {
// Claude: only check if thinking is valid/compatible
return !state.turnHasThinking;
}
// Recovery is only needed in tool loops or interrupted tools
if (!state.inToolLoop && !state.interruptedTool) return false;
// Gemini (default): check tool loop/interrupted AND no thinking
return (state.inToolLoop || state.interruptedTool) && !state.turnHasThinking;
// Need recovery if no valid thinking blocks exist
return !state.turnHasThinking;
}
/**