Remove the strict line bounds check from the diff (#2790)

This commit is contained in:
Matt Rubens 2025-04-19 15:43:27 -04:00 committed by GitHub
parent 2205606118
commit ed102d1850
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 79 additions and 8 deletions

View file

@ -2253,6 +2253,85 @@ function process() {
function two() {
return 2;
}`)
}
})
it("should fail when line range is far outside file bounds", async () => {
const originalContent = `
function one() {
return 1;
}
function two() {
return 2;
}
function three() {
return 3;
}
`.trim()
const diffContent = `test.ts
<<<<<<< SEARCH
:start_line:1000
-------
function three() {
return 3;
}
=======
function three() {
return "three";
}
>>>>>>> REPLACE`
// Line 1000 is way outside the bounds of the file (10 lines)
// and outside of any reasonable buffer range, so it should fail
const result = await strategy.applyDiff(originalContent, diffContent, 1000)
expect(result.success).toBe(false)
})
it("should find match when line range is slightly out of bounds but within buffer zone", async () => {
const originalContent = `
function one() {
return 1;
}
function two() {
return 2;
}
function three() {
return 3;
}
`.trim()
const diffContent = `test.ts
<<<<<<< SEARCH
:start_line:11
-------
function three() {
return 3;
}
=======
function three() {
return "three";
}
>>>>>>> REPLACE`
// File only has 10 lines, but we specify line 11
// It should still find the match since it's within the buffer zone (5 lines)
const result = await strategy.applyDiff(originalContent, diffContent, 11)
expect(result.success).toBe(true)
if (result.success) {
expect(result.content).toBe(`function one() {
return 1;
}
function two() {
return 2;
}
function three() {
return "three";
}`)
}
})

View file

@ -430,14 +430,6 @@ Only use a single line of '=======' between search and replacement content, beca
const searchLen = searchLines.length
const exactEndIndex = exactStartIndex + searchLen - 1
if (exactStartIndex < 0 || exactEndIndex >= resultLines.length) {
diffResults.push({
success: false,
error: `Line range ${startLine}-${startLine + searchLen - 1} is invalid (file has ${resultLines.length} lines)\n\nDebug Info:\n- Requested Range: lines ${startLine}-${startLine + searchLen - 1}\n- File Bounds: lines 1-${resultLines.length}`,
})
continue
}
// Try exact match first
const originalChunk = resultLines.slice(exactStartIndex, exactEndIndex + 1).join("\n")
const similarity = getSimilarity(originalChunk, searchChunk)