Skip to content

Commit c2b4b05

Browse files
committed
Diff debugging
1 parent 5fcf6f0 commit c2b4b05

File tree

15 files changed

+147
-53
lines changed

15 files changed

+147
-53
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: 2 additions & 1 deletion
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

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/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: 33 additions & 25 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

@@ -167,15 +168,11 @@ Your search/replace content here
167168
const exactEndIndex = endLine - 1;
168169

169170
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-
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

@@ -198,11 +195,12 @@ Your search/replace content here
198195

199196
if (startLine !== undefined || endLine !== undefined) {
200197
// Convert to 0-based index and add buffer
198+
const BUFFER_LINES = 5;
201199
if (startLine !== undefined) {
202-
searchStartIndex = Math.max(0, startLine - 6);
200+
searchStartIndex = Math.max(0, startLine - (BUFFER_LINES + 1));
203201
}
204202
if (endLine !== undefined) {
205-
searchEndIndex = Math.min(originalLines.length, endLine + 5);
203+
searchEndIndex = Math.min(originalLines.length, endLine + BUFFER_LINES);
206204
}
207205
}
208206

@@ -224,17 +222,27 @@ Your search/replace content here
224222
// Require similarity to meet threshold
225223
if (matchIndex === -1 || bestMatchScore < this.fuzzyThreshold) {
226224
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-
225+
const originalContentSection = startLine !== undefined && endLine !== undefined
226+
? `\n\nOriginal Content:\n${addLineNumbers(
227+
originalLines.slice(
228+
Math.max(0, startLine - 1 - BUFFER_LINES),
229+
Math.min(originalLines.length, endLine + BUFFER_LINES)
230+
).join('\n'),
231+
Math.max(1, startLine - BUFFER_LINES)
232+
)}`
233+
: `\n\nOriginal Content:\n${addLineNumbers(originalLines.join('\n'))}`;
234+
235+
const bestMatchSection = bestMatchContent
236+
? `\n\nBest Match Found:\n${addLineNumbers(bestMatchContent, matchIndex + 1)}`
237+
: `\n\nBest Match Found:\n(no match)`;
238+
239+
const debugInfo = this.debugEnabled ? `\n\nDebug Info:\n- Similarity Score: ${Math.floor(bestMatchScore * 100)}%\n- Required Threshold: ${Math.floor(this.fuzzyThreshold * 100)}%\n- Line Range: lines ${startLine}-${endLine}\n\nSearch Content:\n${searchChunk}${bestMatchSection}${originalContentSection}` : '';
240+
241+
const lineRange = startLine !== undefined || endLine !== undefined ?
242+
` at ${startLine !== undefined ? `start: ${startLine}` : 'start'} to ${endLine !== undefined ? `end: ${endLine}` : 'end'}` : '';
235243
return {
236244
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)}%)`
245+
error: `No sufficiently similar match found${lineRange} (${Math.floor(bestMatchScore * 100)}% similar, needs ${Math.floor(this.fuzzyThreshold * 100)}%)${debugInfo}`
238246
};
239247
}
240248

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { addLineNumbers } from '../extract-text';
2+
3+
describe('addLineNumbers', () => {
4+
it('should add line numbers starting from 1 by default', () => {
5+
const input = 'line 1\nline 2\nline 3';
6+
const expected = '1 | line 1\n2 | line 2\n3 | line 3';
7+
expect(addLineNumbers(input)).toBe(expected);
8+
});
9+
10+
it('should add line numbers starting from specified line number', () => {
11+
const input = 'line 1\nline 2\nline 3';
12+
const expected = '10 | line 1\n11 | line 2\n12 | line 3';
13+
expect(addLineNumbers(input, 10)).toBe(expected);
14+
});
15+
16+
it('should handle empty content', () => {
17+
expect(addLineNumbers('')).toBe('1 | ');
18+
expect(addLineNumbers('', 5)).toBe('5 | ');
19+
});
20+
21+
it('should handle single line content', () => {
22+
expect(addLineNumbers('single line')).toBe('1 | single line');
23+
expect(addLineNumbers('single line', 42)).toBe('42 | single line');
24+
});
25+
26+
it('should pad line numbers based on the highest line number', () => {
27+
const input = 'line 1\nline 2';
28+
// When starting from 99, highest line will be 100, so needs 3 spaces padding
29+
const expected = ' 99 | line 1\n100 | line 2';
30+
expect(addLineNumbers(input, 99)).toBe(expected);
31+
});
32+
});

0 commit comments

Comments
 (0)