Skip to content

Commit 4756833

Browse files
authored
Merge pull request #132 from RooVetGit/debug_diff_enabled
Debug diff enabled
2 parents 12dd9ab + 61b2104 commit 4756833

File tree

16 files changed

+159
-59
lines changed

16 files changed

+159
-59
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Roo Cline Changelog
22

3+
## [2.2.11]
4+
5+
- Added settings checkbox for verbose diff debugging
6+
37
## [2.2.6 - 2.2.10]
48

59
- More fixes to search/replace diffs

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "Roo Cline",
44
"description": "A fork of Cline, an autonomous coding agent, with some added experimental configuration and automation features.",
55
"publisher": "RooVeterinaryInc",
6-
"version": "2.2.10",
6+
"version": "2.2.11",
77
"icon": "assets/icons/rocket.png",
88
"galleryBanner": {
99
"color": "#617A91",

src/core/Cline.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export class Cline {
9797
apiConfiguration: ApiConfiguration,
9898
customInstructions?: string,
9999
diffEnabled?: boolean,
100+
debugDiffEnabled?: boolean,
100101
task?: string,
101102
images?: string[],
102103
historyItem?: HistoryItem,
@@ -109,7 +110,7 @@ export class Cline {
109110
this.diffViewProvider = new DiffViewProvider(cwd)
110111
this.customInstructions = customInstructions
111112
if (diffEnabled && this.api.getModel().id) {
112-
this.diffStrategy = getDiffStrategy(this.api.getModel().id)
113+
this.diffStrategy = getDiffStrategy(this.api.getModel().id, debugDiffEnabled)
113114
}
114115
if (historyItem) {
115116
this.taskId = historyItem.id
@@ -1237,7 +1238,12 @@ export class Cline {
12371238
const originalContent = await fs.readFile(absolutePath, "utf-8")
12381239

12391240
// Apply the diff to the original content
1240-
const diffResult = this.diffStrategy?.applyDiff(originalContent, diffContent) ?? {
1241+
const diffResult = this.diffStrategy?.applyDiff(
1242+
originalContent,
1243+
diffContent,
1244+
parseInt(block.params.start_line ?? ''),
1245+
parseInt(block.params.end_line ?? '')
1246+
) ?? {
12411247
success: false,
12421248
error: "No diff strategy available"
12431249
}

src/core/__tests__/Cline.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ describe('Cline', () => {
278278
mockProvider,
279279
mockApiConfig,
280280
'custom instructions',
281-
false,
281+
false, // diffEnabled
282+
false, // debugDiffEnabled
282283
'test task'
283284
);
284285

src/core/assistant-message/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ export const toolParamNames = [
4444
"question",
4545
"result",
4646
"diff",
47+
"start_line",
48+
"end_line",
4749
] as const
4850

4951
export type ToolParamName = (typeof toolParamNames)[number]

src/core/diff/DiffStrategy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import { SearchReplaceDiffStrategy } from './strategies/search-replace'
66
* @param model The name of the model being used (e.g., 'gpt-4', 'claude-3-opus')
77
* @returns The appropriate diff strategy for the model
88
*/
9-
export function getDiffStrategy(model: string): DiffStrategy {
9+
export function getDiffStrategy(model: string, debugEnabled?: boolean): DiffStrategy {
1010
// For now, return SearchReplaceDiffStrategy for all models (with a fuzzy threshold of 0.9)
1111
// This architecture allows for future optimizations based on model capabilities
12-
return new SearchReplaceDiffStrategy(0.9)
12+
return new SearchReplaceDiffStrategy(0.9, debugEnabled)
1313
}
1414

1515
export type { DiffStrategy }

src/core/diff/strategies/search-replace.ts

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { DiffStrategy, DiffResult } from "../types"
2+
import { addLineNumbers } from "../../../integrations/misc/extract-text"
3+
4+
const BUFFER_LINES = 5; // Number of extra context lines to show before and after matches
25

36
function levenshteinDistance(a: string, b: string): number {
47
const matrix: number[][] = [];
@@ -48,10 +51,12 @@ function getSimilarity(original: string, search: string): number {
4851

4952
export class SearchReplaceDiffStrategy implements DiffStrategy {
5053
private fuzzyThreshold: number;
54+
public debugEnabled: boolean;
5155

52-
constructor(fuzzyThreshold?: number) {
56+
constructor(fuzzyThreshold?: number, debugEnabled?: boolean) {
5357
// Default to exact matching (1.0) unless fuzzy threshold specified
5458
this.fuzzyThreshold = fuzzyThreshold ?? 1.0;
59+
this.debugEnabled = debugEnabled ?? false;
5560
}
5661

5762
getToolDescription(cwd: string): string {
@@ -119,15 +124,11 @@ Your search/replace content here
119124
// Extract the search and replace blocks
120125
const match = diffContent.match(/<<<<<<< SEARCH\n([\s\S]*?)\n=======\n([\s\S]*?)\n>>>>>>> REPLACE/);
121126
if (!match) {
122-
// Log detailed format information
123-
console.log('Invalid Diff Format Debug:', {
124-
expectedFormat: "<<<<<<< SEARCH\\n[search content]\\n=======\\n[replace content]\\n>>>>>>> REPLACE",
125-
tip: "Make sure to include both SEARCH and REPLACE sections with correct markers"
126-
});
127+
const debugInfo = this.debugEnabled ? `\n\nDebug Info:\n- Expected Format: <<<<<<< SEARCH\\n[search content]\\n=======\\n[replace content]\\n>>>>>>> REPLACE\n- Tip: Make sure to include both SEARCH and REPLACE sections with correct markers` : '';
127128

128129
return {
129130
success: false,
130-
error: "Invalid diff format - missing required SEARCH/REPLACE sections"
131+
error: `Invalid diff format - missing required SEARCH/REPLACE sections${debugInfo}`
131132
};
132133
}
133134

@@ -161,21 +162,17 @@ Your search/replace content here
161162
let bestMatchScore = 0;
162163
let bestMatchContent = "";
163164

164-
if (startLine !== undefined && endLine !== undefined) {
165+
if (startLine && endLine) {
165166
// Convert to 0-based index
166167
const exactStartIndex = startLine - 1;
167168
const exactEndIndex = endLine - 1;
168169

169-
if (exactStartIndex < 0 || exactEndIndex >= originalLines.length) {
170-
// Log detailed debug information
171-
console.log('Invalid Line Range Debug:', {
172-
requestedRange: { start: startLine, end: endLine },
173-
fileBounds: { start: 1, end: originalLines.length }
174-
});
175-
170+
if (exactStartIndex < 0 || exactEndIndex >= originalLines.length || exactStartIndex > exactEndIndex) {
171+
const debugInfo = this.debugEnabled ? `\n\nDebug Info:\n- Requested Range: lines ${startLine}-${endLine}\n- File Bounds: lines 1-${originalLines.length}` : '';
172+
176173
return {
177174
success: false,
178-
error: `Line range ${startLine}-${endLine} is invalid (file has ${originalLines.length} lines)`,
175+
error: `Line range ${startLine}-${endLine} is invalid (file has ${originalLines.length} lines)${debugInfo}`,
179176
};
180177
}
181178

@@ -196,13 +193,13 @@ Your search/replace content here
196193
let searchStartIndex = 0;
197194
let searchEndIndex = originalLines.length;
198195

199-
if (startLine !== undefined || endLine !== undefined) {
196+
if (startLine || endLine) {
200197
// Convert to 0-based index and add buffer
201-
if (startLine !== undefined) {
202-
searchStartIndex = Math.max(0, startLine - 6);
198+
if (startLine) {
199+
searchStartIndex = Math.max(0, startLine - (BUFFER_LINES + 1));
203200
}
204-
if (endLine !== undefined) {
205-
searchEndIndex = Math.min(originalLines.length, endLine + 5);
201+
if (endLine) {
202+
searchEndIndex = Math.min(originalLines.length, endLine + BUFFER_LINES);
206203
}
207204
}
208205

@@ -224,17 +221,27 @@ Your search/replace content here
224221
// Require similarity to meet threshold
225222
if (matchIndex === -1 || bestMatchScore < this.fuzzyThreshold) {
226223
const searchChunk = searchLines.join('\n');
227-
// Log detailed debug information to console
228-
console.log('Search/Replace Debug Info:', {
229-
similarity: bestMatchScore,
230-
threshold: this.fuzzyThreshold,
231-
searchContent: searchChunk,
232-
bestMatch: bestMatchContent || undefined
233-
});
234-
224+
const originalContentSection = startLine !== undefined && endLine !== undefined
225+
? `\n\nOriginal Content:\n${addLineNumbers(
226+
originalLines.slice(
227+
Math.max(0, startLine - 1 - BUFFER_LINES),
228+
Math.min(originalLines.length, endLine + BUFFER_LINES)
229+
).join('\n'),
230+
Math.max(1, startLine - BUFFER_LINES)
231+
)}`
232+
: `\n\nOriginal Content:\n${addLineNumbers(originalLines.join('\n'))}`;
233+
234+
const bestMatchSection = bestMatchContent
235+
? `\n\nBest Match Found:\n${addLineNumbers(bestMatchContent, matchIndex + 1)}`
236+
: `\n\nBest Match Found:\n(no match)`;
237+
238+
const debugInfo = this.debugEnabled ? `\n\nDebug Info:\n- Similarity Score: ${Math.floor(bestMatchScore * 100)}%\n- Required Threshold: ${Math.floor(this.fuzzyThreshold * 100)}%\n- Search Range: ${startLine && endLine ? `lines ${startLine}-${endLine}` : 'start to end'}\n\nSearch Content:\n${searchChunk}${bestMatchSection}${originalContentSection}` : '';
239+
240+
const lineRange = startLine || endLine ?
241+
` at ${startLine ? `start: ${startLine}` : 'start'} to ${endLine ? `end: ${endLine}` : 'end'}` : '';
235242
return {
236243
success: false,
237-
error: `No sufficiently similar match found${startLine !== undefined ? ` near lines ${startLine}-${endLine}` : ''} (${Math.round(bestMatchScore * 100)}% similar, needs ${Math.round(this.fuzzyThreshold * 100)}%)`
244+
error: `No sufficiently similar match found${lineRange} (${Math.floor(bestMatchScore * 100)}% similar, needs ${Math.floor(this.fuzzyThreshold * 100)}%)${debugInfo}`
238245
};
239246
}
240247

src/core/diff/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ export type DiffResult =
1313
}};
1414

1515
export interface DiffStrategy {
16+
/**
17+
* Whether to enable detailed debug logging
18+
*/
19+
debugEnabled?: boolean;
20+
1621
/**
1722
* Get the tool description for this diff strategy
1823
* @param cwd The current working directory

src/core/webview/ClineProvider.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ type GlobalStateKey =
6767
| "allowedCommands"
6868
| "soundEnabled"
6969
| "diffEnabled"
70+
| "debugDiffEnabled"
7071
| "alwaysAllowMcp"
7172

7273
export const GlobalFileNames = {
@@ -207,35 +208,39 @@ export class ClineProvider implements vscode.WebviewViewProvider {
207208

208209
async initClineWithTask(task?: string, images?: string[]) {
209210
await this.clearTask()
210-
const {
211-
apiConfiguration,
212-
customInstructions,
211+
const {
212+
apiConfiguration,
213+
customInstructions,
213214
diffEnabled,
215+
debugDiffEnabled,
214216
} = await this.getState()
215217

216218
this.cline = new Cline(
217-
this,
218-
apiConfiguration,
219-
customInstructions,
219+
this,
220+
apiConfiguration,
221+
customInstructions,
220222
diffEnabled,
221-
task,
223+
debugDiffEnabled,
224+
task,
222225
images
223226
)
224227
}
225228

226229
async initClineWithHistoryItem(historyItem: HistoryItem) {
227230
await this.clearTask()
228-
const {
229-
apiConfiguration,
230-
customInstructions,
231+
const {
232+
apiConfiguration,
233+
customInstructions,
231234
diffEnabled,
235+
debugDiffEnabled,
232236
} = await this.getState()
233237

234238
this.cline = new Cline(
235239
this,
236240
apiConfiguration,
237241
customInstructions,
238242
diffEnabled,
243+
debugDiffEnabled,
239244
undefined,
240245
undefined,
241246
historyItem,
@@ -597,6 +602,11 @@ export class ClineProvider implements vscode.WebviewViewProvider {
597602
await this.updateGlobalState("diffEnabled", diffEnabled)
598603
await this.postStateToWebview()
599604
break
605+
case "debugDiffEnabled":
606+
const debugDiffEnabled = message.bool ?? false
607+
await this.updateGlobalState("debugDiffEnabled", debugDiffEnabled)
608+
await this.postStateToWebview()
609+
break
600610
}
601611
},
602612
null,
@@ -923,6 +933,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
923933
alwaysAllowMcp,
924934
soundEnabled,
925935
diffEnabled,
936+
debugDiffEnabled,
926937
taskHistory,
927938
} = await this.getState()
928939

@@ -946,6 +957,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
946957
.sort((a, b) => b.ts - a.ts),
947958
soundEnabled: soundEnabled ?? false,
948959
diffEnabled: diffEnabled ?? false,
960+
debugDiffEnabled: debugDiffEnabled ?? false,
949961
shouldShowAnnouncement: lastShownAnnouncementId !== this.latestAnnouncementId,
950962
allowedCommands,
951963
}
@@ -1040,6 +1052,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
10401052
allowedCommands,
10411053
soundEnabled,
10421054
diffEnabled,
1055+
debugDiffEnabled,
10431056
] = await Promise.all([
10441057
this.getGlobalState("apiProvider") as Promise<ApiProvider | undefined>,
10451058
this.getGlobalState("apiModelId") as Promise<string | undefined>,
@@ -1077,6 +1090,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
10771090
this.getGlobalState("allowedCommands") as Promise<string[] | undefined>,
10781091
this.getGlobalState("soundEnabled") as Promise<boolean | undefined>,
10791092
this.getGlobalState("diffEnabled") as Promise<boolean | undefined>,
1093+
this.getGlobalState("debugDiffEnabled") as Promise<boolean | undefined>,
10801094
])
10811095

10821096
let apiProvider: ApiProvider
@@ -1130,8 +1144,9 @@ export class ClineProvider implements vscode.WebviewViewProvider {
11301144
alwaysAllowMcp: alwaysAllowMcp ?? false,
11311145
taskHistory,
11321146
allowedCommands,
1133-
soundEnabled,
1134-
diffEnabled,
1147+
soundEnabled: soundEnabled ?? false,
1148+
diffEnabled: diffEnabled ?? false,
1149+
debugDiffEnabled: debugDiffEnabled ?? false,
11351150
}
11361151
}
11371152

0 commit comments

Comments
 (0)