-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: preserve thinking sections during consecutive tool uses (#8065) #8067
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- Add isConsecutiveToolUse() method to Task class to detect when assistant is making consecutive tool calls without user intervention - Conditionally preserve <thinking> tags in presentAssistantMessage when consecutive tool use is detected - This fixes issue #8065 where reasoning models were losing context between tool uses The solution checks the conversation history to determine if the current message follows another assistant message (consecutive tool use) or a user message (new interaction). When consecutive tool use is detected, thinking sections are preserved to maintain context for reasoning models.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewing my own code is like debugging in a mirror - everything looks backwards but the bugs are still mine.
| * @returns true if the last non-system message was from the assistant (consecutive tool use), | ||
| * false if it was from the user or if there are no previous messages | ||
| */ | ||
| public isConsecutiveToolUse(): boolean { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new isConsecutiveToolUse() method lacks unit tests. Given that this is critical functionality determining when thinking sections are preserved, we should add comprehensive test coverage to ensure it correctly identifies consecutive tool uses vs user interventions.
| if (block.type === "text") { | ||
| const text = block.text.trim() | ||
| // Check if this is just environment details or tool results | ||
| return ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The text pattern matching here (environment_details:, [Tool Use:, Result:) could be fragile if these patterns change. Consider extracting these as constants or using a more robust method to identify system-generated content vs actual user input.
| content = content.replace(/\s?<\/thinking>/g, "") | ||
| // Check if we should preserve thinking sections | ||
| // Preserve thinking sections during consecutive tool uses (no user messages in between) | ||
| const shouldPreserveThinking = cline.isConsecutiveToolUse() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider renaming shouldPreserveThinking to something more descriptive like isConsecutiveToolUseWithoutUserInput to better convey what this variable represents.
Summary
This PR fixes issue #8065 where reasoning models were experiencing "amnesia" and losing context between consecutive tool uses because thinking sections were being stripped out.
Problem
When using reasoning models (like Qwen3-235B-A22B-Thinking-2507), the thinking sections were being removed from assistant messages in all cases. This caused the model to lose context and start reasoning from scratch on each consecutive tool use, even though these thinking sections should be preserved during tool chains.
Solution
The fix introduces intelligent detection of consecutive tool usage:
isConsecutiveToolUse()method to the Task class that analyzes conversation historypresentAssistantMessage.tsto conditionally preserve<thinking>tags when consecutive tool use is detectedChanges
isConsecutiveToolUse()method to detect when the assistant is making consecutive tool calls without user interventionTesting
Impact
This change allows reasoning models to maintain their thought process across multiple tool uses, significantly improving their ability to handle complex multi-step tasks.
Fixes #8065
Important
Fixes issue #8065 by preserving
<thinking>sections during consecutive tool uses in reasoning models.<thinking>sections in assistant messages during consecutive tool uses.isConsecutiveToolUse()inTaskclass to detect consecutive tool uses without user intervention.presentAssistantMessage.ts: Modifies logic to conditionally remove<thinking>tags based on consecutive tool use detection.Task.ts: AddsisConsecutiveToolUse()method to analyze conversation history for consecutive tool use detection.This description was created by
for c5efea5. You can customize this summary. It will automatically update as commits are pushed.