-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat: implement read_file history deduplication (#6279) #6316
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -340,6 +340,140 @@ export class Task extends EventEmitter<ClineEvents> { | |||||||||||||||||||||||||||
| await this.saveApiConversationHistory() | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| public async deduplicateReadFileHistory(): Promise<void> { | ||||||||||||||||||||||||||||
| // Check if the experimental feature is enabled | ||||||||||||||||||||||||||||
| const state = await this.providerRef.deref()?.getState() | ||||||||||||||||||||||||||||
| if (!state?.experiments || !experiments.isEnabled(state.experiments, EXPERIMENT_IDS.READ_FILE_DEDUPLICATION)) { | ||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Track files we've seen and their most recent location | ||||||||||||||||||||||||||||
| const fileLastSeen = new Map<string, { messageIndex: number; blockIndex: number }>() | ||||||||||||||||||||||||||||
| const blocksToRemove = new Map<number, Set<number>>() // messageIndex -> Set of blockIndices to remove | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Iterate through messages in reverse order (newest first) | ||||||||||||||||||||||||||||
| for (let i = this.apiConversationHistory.length - 1; i >= 0; i--) { | ||||||||||||||||||||||||||||
| const message = this.apiConversationHistory[i] | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Only process user messages | ||||||||||||||||||||||||||||
| if (message.role !== "user") continue | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const content = Array.isArray(message.content) ? message.content : [{ type: "text", text: message.content }] | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Track blocks to remove within this message | ||||||||||||||||||||||||||||
| const blockIndicesToRemove = new Set<number>() | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Iterate through blocks in reverse order within the message | ||||||||||||||||||||||||||||
| for (let j = content.length - 1; j >= 0; j--) { | ||||||||||||||||||||||||||||
| const block = content[j] | ||||||||||||||||||||||||||||
| if (block.type !== "text") continue | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const text = block.text | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Check if this is a read_file result | ||||||||||||||||||||||||||||
| if (!text.startsWith("[read_file") || !text.includes("Result:")) continue | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Extract file paths from the result | ||||||||||||||||||||||||||||
| const filePaths = this.extractFilePathsFromReadResult(text) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // For each file path, check if we've seen it before | ||||||||||||||||||||||||||||
| for (const filePath of filePaths) { | ||||||||||||||||||||||||||||
| const lastSeen = fileLastSeen.get(filePath) | ||||||||||||||||||||||||||||
| if (lastSeen) { | ||||||||||||||||||||||||||||
| // We've seen this file before | ||||||||||||||||||||||||||||
| if (lastSeen.messageIndex === i) { | ||||||||||||||||||||||||||||
| // It's in the same message, mark the older block for removal | ||||||||||||||||||||||||||||
| blockIndicesToRemove.add(j) | ||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||
| // It's in a different message, mark this specific block for removal | ||||||||||||||||||||||||||||
| if (!blocksToRemove.has(i)) { | ||||||||||||||||||||||||||||
| blocksToRemove.set(i, new Set()) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| blocksToRemove.get(i)!.add(j) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||
| // First time seeing this file (going backwards), record it | ||||||||||||||||||||||||||||
| fileLastSeen.set(filePath, { messageIndex: i, blockIndex: j }) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // If we have blocks to remove from this message, add them to the map | ||||||||||||||||||||||||||||
| if (blockIndicesToRemove.size > 0) { | ||||||||||||||||||||||||||||
| if (!blocksToRemove.has(i)) { | ||||||||||||||||||||||||||||
| blocksToRemove.set(i, new Set()) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| blockIndicesToRemove.forEach((idx) => blocksToRemove.get(i)!.add(idx)) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Apply the removals | ||||||||||||||||||||||||||||
| if (blocksToRemove.size > 0) { | ||||||||||||||||||||||||||||
| let modified = false | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Create a new conversation history with duplicates removed | ||||||||||||||||||||||||||||
| this.apiConversationHistory = this.apiConversationHistory | ||||||||||||||||||||||||||||
| .map((message, messageIndex) => { | ||||||||||||||||||||||||||||
| const blocksToRemoveForMessage = blocksToRemove.get(messageIndex) | ||||||||||||||||||||||||||||
| if (!blocksToRemoveForMessage || blocksToRemoveForMessage.size === 0) { | ||||||||||||||||||||||||||||
| return message | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // This message has blocks to remove | ||||||||||||||||||||||||||||
| const content = Array.isArray(message.content) | ||||||||||||||||||||||||||||
| ? message.content | ||||||||||||||||||||||||||||
| : [{ type: "text", text: message.content }] | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Check if this is a string content (legacy format) | ||||||||||||||||||||||||||||
| if (!Array.isArray(message.content)) { | ||||||||||||||||||||||||||||
| // For string content, we can only remove the entire message if it's a duplicate | ||||||||||||||||||||||||||||
| if (blocksToRemoveForMessage.has(0)) { | ||||||||||||||||||||||||||||
| modified = true | ||||||||||||||||||||||||||||
| return null | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| return message | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const newContent = content.filter((_, blockIndex) => !blocksToRemoveForMessage.has(blockIndex)) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // If all content was removed, filter out this message entirely | ||||||||||||||||||||||||||||
| if (newContent.length === 0) { | ||||||||||||||||||||||||||||
| modified = true | ||||||||||||||||||||||||||||
| return null | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| modified = true | ||||||||||||||||||||||||||||
| return { ...message, content: newContent } | ||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||
| .filter((message) => message !== null) as ApiMessage[] | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if (modified) { | ||||||||||||||||||||||||||||
| await this.saveApiConversationHistory() | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| private extractFilePathsFromReadResult(text: string): string[] { | ||||||||||||||||||||||||||||
| const paths: string[] = [] | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Match file paths in the XML structure | ||||||||||||||||||||||||||||
| // Handles both single file and multi-file formats | ||||||||||||||||||||||||||||
| const filePathRegex = /<file>\s*<path>([^<]+)<\/path>/g | ||||||||||||||||||||||||||||
| let match | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| while ((match = filePathRegex.exec(text)) !== null) { | ||||||||||||||||||||||||||||
| paths.push(match[1].trim()) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Also handle legacy format where path might be in the header | ||||||||||||||||||||||||||||
| const headerMatch = text.match(/\[read_file for '([^']+)'\]/) | ||||||||||||||||||||||||||||
| if (headerMatch && paths.length === 0) { | ||||||||||||||||||||||||||||
| paths.push(headerMatch[1]) | ||||||||||||||||||||||||||||
|
Comment on lines
+468
to
+471
|
||||||||||||||||||||||||||||
| // Also handle legacy format where path might be in the header | |
| const headerMatch = text.match(/\[read_file for '([^']+)'\]/) | |
| if (headerMatch && paths.length === 0) { | |
| paths.push(headerMatch[1]) | |
| // Also handle legacy format where paths might be in the header | |
| const legacyFormatRegex = /\[read_file for '([^']+?)'(?:, '([^']+?)')*\]/g | |
| let legacyMatch | |
| while ((legacyMatch = legacyFormatRegex.exec(text)) !== null) { | |
| // Extract all file paths from the match | |
| const matchedPaths = legacyMatch[0].match(/'([^']+)'/g)?.map((p) => p.replace(/'/g, "")) | |
| if (matchedPaths) { | |
| paths.push(...matchedPaths) | |
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 regex pattern only matches single-quoted file paths but the test cases show multi-file reads use double quotes and comma separation like "[read_file for 'file1.ts', 'file2.ts']". This will miss file paths in multi-file scenarios.