Skip to content

Commit 7d56f2a

Browse files
author
Bertan Ari
committed
fix(apply-diff): port bounded failure reporting from upstream PR15
Port simurg79/Roo-Code#15. Upstream-commit: d9d96e47006786acbb05d6ef0ee7a927f91f8f12 Retain Zoo diagnostics and telemetry; adapt regression tests to typed mocks and verify repeated-failure reporting. Exclude upstream changeset.
1 parent 58341d8 commit 7d56f2a

4 files changed

Lines changed: 489 additions & 15 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { MultiSearchReplaceDiffStrategy } from "../multi-search-replace"
2+
3+
// Guards the POST-FAILURE "LIKELY CAUSE" hint for mistyped start-line markers.
4+
// The heuristic must never act as an upfront rejection gate.
5+
describe("MultiSearchReplaceDiffStrategy malformed start-line marker hint", () => {
6+
let strategy: MultiSearchReplaceDiffStrategy
7+
8+
beforeEach(() => {
9+
strategy = new MultiSearchReplaceDiffStrategy()
10+
})
11+
12+
const nonMatchingFile = ["alpha", "beta", "gamma", "delta", "epsilon"].join("\n")
13+
14+
function buildDiff(searchBody: string, replaceBody: string): string {
15+
return `<<<<<<< SEARCH\n${searchBody}\n=======\n${replaceBody}\n>>>>>>> REPLACE`
16+
}
17+
18+
function firstFailureError(result: Awaited<ReturnType<MultiSearchReplaceDiffStrategy["applyDiff"]>>): string {
19+
expect(result.success).toBe(false)
20+
const failures = result.failParts ?? []
21+
expect(failures.length).toBeGreaterThan(0)
22+
const failure = failures[0]
23+
if (failure.success || !failure.error) {
24+
throw new Error("Expected a failed block with error details")
25+
}
26+
return failure.error
27+
}
28+
29+
it("adds LIKELY CAUSE when the first search line is a bare ':3' marker and the block fails", async () => {
30+
const diffContent = buildDiff(":3\nnot-in-the-file", "replacement")
31+
const result = await strategy.applyDiff(nonMatchingFile, diffContent)
32+
33+
const errorText = firstFailureError(result)
34+
expect(errorText).toContain("LIKELY CAUSE")
35+
expect(errorText).toContain(":start_line:")
36+
})
37+
38+
const markerVariants = ["start_line:3", ":start-line 3", ": start_line : 3", ":START_LINE:3", "Start_Line: 3"]
39+
40+
it.each(markerVariants)("adds LIKELY CAUSE for the malformed marker variant %j", async (markerVariant) => {
41+
const diffContent = buildDiff(`${markerVariant}\nnot-in-the-file`, "replacement")
42+
const result = await strategy.applyDiff(nonMatchingFile, diffContent)
43+
44+
expect(firstFailureError(result)).toContain("LIKELY CAUSE")
45+
})
46+
47+
it("still applies cleanly when the file genuinely contains a ':3' line (hint is not a gate)", async () => {
48+
const fileWithMarkerLikeLine = ["alpha", ":3", "beta", "gamma"].join("\n")
49+
const diffContent = buildDiff(":3\nbeta", ":3\nBETA")
50+
51+
const result = await strategy.applyDiff(fileWithMarkerLikeLine, diffContent)
52+
53+
expect(result.success).toBe(true)
54+
if (!result.success) {
55+
throw new Error("Expected marker-like file content to match")
56+
}
57+
expect(result.content).toContain("BETA")
58+
expect(result.content).not.toContain("LIKELY CAUSE")
59+
expect(result.failParts ?? []).toHaveLength(0)
60+
})
61+
62+
it("omits the hint for ordinary non-matching content but keeps the file-changed tip", async () => {
63+
const diffContent = buildDiff("completely unrelated content\nsecond line", "replacement")
64+
const result = await strategy.applyDiff(nonMatchingFile, diffContent)
65+
66+
const errorText = firstFailureError(result)
67+
expect(errorText).not.toContain("LIKELY CAUSE")
68+
expect(errorText).toContain("the file content may have changed")
69+
})
70+
71+
it("does not flag the correct ':start_line:3' marker form", async () => {
72+
const diffContent = `<<<<<<< SEARCH\n:start_line:3\n-------\nnot-in-the-file\n=======\nreplacement\n>>>>>>> REPLACE`
73+
const result = await strategy.applyDiff(nonMatchingFile, diffContent)
74+
75+
expect(firstFailureError(result)).not.toContain("LIKELY CAUSE")
76+
})
77+
78+
it("adds LIKELY CAUSE when a well-formed ':start_line:N' line goes unparsed and swallows its separator", async () => {
79+
// Indenting the marker makes the parser miss it, so both it and the "-------" become search text.
80+
const diffContent = buildDiff(" :start_line:3\n-------\nnot-in-the-file", "replacement")
81+
const result = await strategy.applyDiff(nonMatchingFile, diffContent)
82+
83+
const errorText = firstFailureError(result)
84+
expect(errorText).toContain("LIKELY CAUSE")
85+
expect(errorText).toContain('together with the "-------" line after it')
86+
})
87+
})

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,9 +575,32 @@ export class MultiSearchReplaceDiffStrategy implements DiffStrategy {
575575
? distance(normalizeString(searchChunk), normalizeString(bestMatchContent))
576576
: -1
577577

578+
// Only diagnose markers after matching fails: marker-like text can be valid file content.
579+
const hintLines = searchChunk.split("\n")
580+
const firstSearchLine = hintLines[0]?.trim() ?? ""
581+
const separatorSwallowed = hintLines[1]?.trim() === "-------"
582+
const malformedMarkerHint = /^(?::\s*\d{1,9}:?|:?\s*start[_-]?line\s*:?\s*\d{1,9}:?)$/i.test(
583+
firstSearchLine,
584+
)
585+
? `\n- LIKELY CAUSE: the first line of your search content is "${firstSearchLine}", which looks like a malformed start-line marker and was therefore searched for as literal file text${separatorSwallowed ? ', together with the "-------" line after it' : ""}. The only recognized form is ":start_line:<number>" on the line immediately after "<<<<<<< SEARCH", optionally followed by a "-------" line. Remove or correct that line and retry.`
586+
: ""
587+
578588
diffResults.push({
579589
success: false,
580-
error: `No sufficiently similar match found${lineRange} (${Math.floor(bestMatchScore * 100)}% similar, needs ${Math.floor(this.fuzzyThreshold * 100)}%)\n\nDebug Info:\n- Similarity Score: ${Math.floor(bestMatchScore * 100)}%\n- Required Threshold: ${Math.floor(this.fuzzyThreshold * 100)}%\n- Search Range: ${startLine ? `starting at line ${startLine}` : "start to end"}\n- Levenshtein Distance: ${levenDist >= 0 ? `${levenDist} characters` : "N/A"}\n- Search Length: ${searchChunk.length} characters\n- Best Match Length: ${bestMatchContent ? bestMatchContent.length : 0} characters\n- Tried both standard and aggressive line number stripping\n- Tip: Use the read_file tool to get the latest content of the file before attempting to use the apply_diff tool again, as the file content may have changed\n\nSearch Content:\n${searchChunk}${bestMatchSection}${originalContentSection}`,
590+
error:
591+
`No sufficiently similar match found${lineRange} ` +
592+
`(${Math.floor(bestMatchScore * 100)}% similar, needs ${Math.floor(this.fuzzyThreshold * 100)}%)\n\n` +
593+
`Debug Info:\n` +
594+
`- Similarity Score: ${Math.floor(bestMatchScore * 100)}%\n` +
595+
`- Required Threshold: ${Math.floor(this.fuzzyThreshold * 100)}%\n` +
596+
`- Search Range: ${startLine ? `starting at line ${startLine}` : "start to end"}\n` +
597+
`- Levenshtein Distance: ${levenDist >= 0 ? `${levenDist} characters` : "N/A"}\n` +
598+
`- Search Length: ${searchChunk.length} characters\n` +
599+
`- Best Match Length: ${bestMatchContent ? bestMatchContent.length : 0} characters\n` +
600+
`- Tried both standard and aggressive line number stripping${malformedMarkerHint}\n` +
601+
`- Tip: Use the read_file tool to get the latest content of the file before attempting ` +
602+
`to use the apply_diff tool again, as the file content may have changed\n\n` +
603+
`Search Content:\n${searchChunk}${bestMatchSection}${originalContentSection}`,
581604
})
582605
continue
583606
}

‎src/core/tools/ApplyDiffTool.ts‎

Lines changed: 69 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { RecordSource } from "../context-tracking/FileContextTrackerTypes"
1212
import { unescapeHtmlEntities } from "../../utils/text-normalization"
1313
import { EXPERIMENT_IDS, experiments } from "../../shared/experiments"
1414
import { computeDiffStats, sanitizeUnifiedDiff } from "../diff/stats"
15-
import type { ToolUse } from "../../shared/tools"
15+
import type { DiffResult, ToolUse } from "../../shared/tools"
1616

1717
import { BaseTool, ToolCallbacks } from "./BaseTool"
1818

@@ -21,6 +21,12 @@ interface ApplyDiffParams {
2121
diff: string
2222
}
2323

24+
// Failures embed file content; cap both detail size and the number of summaries.
25+
const DETAILED_APPLY_DIFF_FAILURES = 5
26+
const MAX_DETAILED_FAILURE_CHARACTERS = 4000
27+
const MAX_SUMMARY_FAILURE_CHARACTERS = 200
28+
const MAX_ABBREVIATED_FAILURES = 50
29+
2430
export class ApplyDiffTool extends BaseTool<"apply_diff"> {
2531
readonly name = "apply_diff" as const
2632

@@ -88,17 +94,13 @@ export class ApplyDiffTool extends BaseTool<"apply_diff"> {
8894
TelemetryService.instance.captureDiffApplicationError(task.taskId, currentCount)
8995

9096
if (diffResult.failParts && diffResult.failParts.length > 0) {
91-
for (const failPart of diffResult.failParts) {
92-
if (failPart.success) {
93-
continue
94-
}
95-
96-
const errorDetails = failPart.details ? JSON.stringify(failPart.details, null, 2) : ""
97+
const failedParts = diffResult.failParts.filter((part) => !part.success)
98+
const summary =
99+
failedParts.length > 1
100+
? `${failedParts.length} diff blocks failed to apply. Fix the failures below before retrying.\n\n`
101+
: ""
97102

98-
formattedError = `<error_details>\n${
99-
failPart.error
100-
}${errorDetails ? `\n\nDetails:\n${errorDetails}` : ""}\n</error_details>`
101-
}
103+
formattedError = `<error_details>\n${summary}${this.formatFailedParts(failedParts)}\n</error_details>`
102104
} else {
103105
const errorDetails = diffResult.details ? JSON.stringify(diffResult.details, null, 2) : ""
104106

@@ -231,17 +233,25 @@ export class ApplyDiffTool extends BaseTool<"apply_diff"> {
231233

232234
// Used to determine if we should wait for busy terminal to update before sending api request
233235
task.didEditFile = true
236+
237+
// Literal SEARCH markers inside content can inflate this advisory count.
238+
const searchBlocks = (diffContent.match(/<<<<<<< SEARCH/g) || []).length
239+
const unappliedParts = diffResult.failParts?.filter((part) => !part.success) ?? []
234240
let partFailHint = ""
235241

236-
if (diffResult.failParts && diffResult.failParts.length > 0) {
237-
partFailHint = `But unable to apply all diff parts to file: ${absolutePath}. Use the read_file tool to check the newest file version and re-apply diffs.\n`
242+
if (unappliedParts.length > 0) {
243+
const appliedBlocks = Math.max(0, searchBlocks - unappliedParts.length)
244+
partFailHint =
245+
`Applied ${appliedBlocks} of ${searchBlocks} diff blocks to ${absolutePath}. ` +
246+
`The other ${unappliedParts.length} did NOT apply, so those changes are NOT in the file. ` +
247+
`Fix the failures below and resend ONLY the blocks that failed; the blocks that already applied will no longer match.\n\n` +
248+
`<error_details>\n${this.formatFailedParts(unappliedParts)}\n</error_details>\n\n`
238249
}
239250

240251
// Get the formatted response message
241252
const message = await task.diffViewProvider.pushToolWriteResult(task, task.cwd, !fileExists)
242253

243254
// Check for single SEARCH/REPLACE block warning
244-
const searchBlocks = (diffContent.match(/<<<<<<< SEARCH/g) || []).length
245255
const singleBlockNotice =
246256
searchBlocks === 1
247257
? "\n<notice>Making multiple related changes in a single apply_diff is more efficient. If other changes are needed in this file, please include them as additional SEARCH/REPLACE blocks.</notice>"
@@ -269,6 +279,51 @@ export class ApplyDiffTool extends BaseTool<"apply_diff"> {
269279
}
270280
}
271281

282+
private formatFailedParts(failedParts: Extract<DiffResult, { success: false }>[]): string {
283+
const renderedCount = Math.min(failedParts.length, MAX_ABBREVIATED_FAILURES)
284+
const sections = failedParts.slice(0, renderedCount).map((failPart, index) => {
285+
const label = failedParts.length > 1 ? `--- Diff block ${index + 1} of ${failedParts.length} ---\n` : ""
286+
const error = failPart.error ?? "Unknown apply_diff failure"
287+
288+
if (index >= DETAILED_APPLY_DIFF_FAILURES) {
289+
const summaryLine = error.split("\n")[0]
290+
return `${label}${this.truncate(summaryLine, MAX_SUMMARY_FAILURE_CHARACTERS)}`
291+
}
292+
293+
const errorDetails = failPart.details ? JSON.stringify(failPart.details, null, 2) : ""
294+
const detailedSection = `${label}${error}${errorDetails ? `\n\nDetails:\n${errorDetails}` : ""}`
295+
296+
return this.truncate(detailedSection, MAX_DETAILED_FAILURE_CHARACTERS)
297+
})
298+
299+
const trailers: string[] = []
300+
const abbreviatedRangeStart = DETAILED_APPLY_DIFF_FAILURES + 1
301+
302+
if (renderedCount > DETAILED_APPLY_DIFF_FAILURES) {
303+
trailers.push(
304+
`(Failures ${abbreviatedRangeStart}-${renderedCount} are shown as a single summary line each; retry those blocks individually for full detail.)`,
305+
)
306+
}
307+
308+
const omitted = failedParts.length - renderedCount
309+
310+
if (omitted > 0) {
311+
trailers.push(`(+${omitted} more failures not shown; retry those blocks individually for full detail.)`)
312+
}
313+
314+
return [sections.join("\n\n"), ...trailers].join("\n\n")
315+
}
316+
317+
private truncate(text: string, maxCharacters: number): string {
318+
if (text.length <= maxCharacters) {
319+
return text
320+
}
321+
322+
const omitted = text.length - maxCharacters
323+
324+
return `${text.slice(0, maxCharacters)}\n... [truncated; ${omitted} characters omitted — retry this block alone for full detail]`
325+
}
326+
272327
override async handlePartial(task: Task, block: ToolUse<"apply_diff">): Promise<void> {
273328
const relPath: string | undefined = block.params.path
274329
const diffContent: string | undefined = block.params.diff

0 commit comments

Comments
 (0)