Skip to content

Commit 4b90d96

Browse files
committed
Add detection for already applied diff hunks to prevent duplicate application
1 parent 88fd4b5 commit 4b90d96

4 files changed

Lines changed: 101 additions & 2 deletions

File tree

‎apps/editor/src/commands/apply-chat-response-command/utils/edit-formats/diffs/diff-processor.spec.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@ describe('diff-processor', () => {
7070
}).toThrow(/Failed to apply \d+ hunk\(s\)/)
7171
})
7272

73+
it('applies diff with failing test case 2 correctly', async () => {
74+
const test_case = 'failing-2'
75+
const original = load_test_case_file('', test_case, 'original.txt')
76+
const diff = load_test_case_file('', test_case, 'diff.txt')
77+
78+
expect(() => {
79+
apply_diff({
80+
original_code: original,
81+
diff_patch: diff
82+
})
83+
}).toThrow(/Failed to apply \d+ hunk\(s\)/)
84+
})
85+
7386
it('applies diff with generic test case correctly', async () => {
7487
const test_case = 'generic-1'
7588
const original = load_test_case_file('', test_case, 'original.txt')

‎apps/editor/src/commands/apply-chat-response-command/utils/edit-formats/diffs/diff-processor.ts‎

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type ReplaceLine = {
66
type SearchBlock = {
77
search_lines: string[]
88
replace_lines: ReplaceLine[]
9+
after_search_lines: string[]
910
search_block_start_index: number
1011
actual_original_line_count: number
1112
search_to_original_map: Map<number, number>
@@ -102,6 +103,7 @@ export const apply_diff = (params: {
102103
const search_replace_blocks: SearchBlock[] = []
103104
let search_chunks: string[] = []
104105
let replace_chunks: ReplaceLine[] = []
106+
let after_search_chunks: string[] = []
105107

106108
let inside_replace_block = false
107109
let current_block_has_changes = false
@@ -112,6 +114,7 @@ export const apply_diff = (params: {
112114
search_replace_blocks.push({
113115
search_lines: search_chunks,
114116
replace_lines: replace_chunks,
117+
after_search_lines: after_search_chunks,
115118
search_block_start_index: -1,
116119
actual_original_line_count: 0,
117120
search_to_original_map: new Map()
@@ -120,6 +123,7 @@ export const apply_diff = (params: {
120123
}
121124
search_chunks = []
122125
replace_chunks = []
126+
after_search_chunks = []
123127
inside_replace_block = false
124128
current_block_has_changes = false
125129
}
@@ -150,6 +154,14 @@ export const apply_diff = (params: {
150154
search_chunks.push(line.replace(/^-/, '').replace(/^~/, ''))
151155
}
152156

157+
if (!line.startsWith('-')) {
158+
if (line.startsWith('~nnn')) {
159+
after_search_chunks.push('~nnn')
160+
} else {
161+
after_search_chunks.push(line.replace(/^~/, ''))
162+
}
163+
}
164+
153165
if (line.startsWith('~nnn')) {
154166
replace_chunks.push({
155167
content: line_original.replace(/^~nnn/, '') + '\n',
@@ -174,11 +186,13 @@ export const apply_diff = (params: {
174186
content: line_original.replace(/^\+~nnn/, '') + '\n',
175187
search_index: null
176188
})
189+
after_search_chunks.push('~nnn')
177190
} else {
178191
replace_chunks.push({
179192
content: line_original.replace(/^\+/, '') + '\n',
180193
search_index: null
181194
})
195+
after_search_chunks.push(line.replace(/^\+/, ''))
182196
}
183197
}
184198
}
@@ -190,6 +204,12 @@ export const apply_diff = (params: {
190204
) {
191205
search_chunks.pop()
192206
replace_chunks.pop()
207+
if (
208+
after_search_chunks.length > 0 &&
209+
after_search_chunks[after_search_chunks.length - 1] == '~nnn'
210+
) {
211+
after_search_chunks.pop()
212+
}
193213
}
194214
push_block()
195215
}
@@ -318,8 +338,59 @@ export const apply_diff = (params: {
318338
)
319339
}
320340

321-
const indices = Array.from(matched_info.map.values())
322-
previous_found_index = Math.max(...indices) + 1
341+
// Check if the patch is already applied by matching the after-side
342+
const has_additions = block.replace_lines.some(
343+
(l) => l.search_index === null
344+
)
345+
if (has_additions && block.after_search_lines.length > 0) {
346+
const check_start = Math.max(
347+
0,
348+
matched_info.start_index - block.after_search_lines.length
349+
)
350+
const check_end = Math.min(
351+
original_code_lines_normalized.length - 1,
352+
matched_info.start_index + matched_info.actual_count
353+
)
354+
355+
let already_applied = false
356+
for (let j = check_start; j <= check_end; j++) {
357+
let after_ptr = 0
358+
let orig_ptr = j
359+
360+
while (
361+
after_ptr < block.after_search_lines.length &&
362+
orig_ptr < original_code_lines_normalized.length
363+
) {
364+
const a_val = block.after_search_lines[after_ptr]
365+
const o_val = original_code_lines_normalized[orig_ptr].value
366+
367+
if (a_val === o_val) {
368+
after_ptr++
369+
orig_ptr++
370+
} else if (o_val === '~nnn') {
371+
orig_ptr++
372+
} else if (a_val === '~nnn') {
373+
after_ptr++
374+
} else {
375+
break
376+
}
377+
}
378+
379+
if (after_ptr === block.after_search_lines.length) {
380+
already_applied = true
381+
break
382+
}
383+
}
384+
385+
if (already_applied) {
386+
block.search_block_start_index = -2
387+
}
388+
}
389+
390+
if (block.search_block_start_index !== -2) {
391+
const indices = Array.from(matched_info.map.values())
392+
previous_found_index = Math.max(...indices) + 1
393+
}
323394
} else {
324395
block.search_block_start_index = -2
325396
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--- a/file.ts
2+
+++ b/file.ts
3+
@@ -1,3 +1,5 @@
4+
+import 'start'
5+
+
6+
function main() {
7+
console.log('middle')
8+
}
9+
+console.log('end')
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import 'start'
2+
3+
function main() {
4+
console.log('middle')
5+
}
6+
console.log('end')

0 commit comments

Comments
 (0)