From a6c1d8c42f317fc7d915d104dad525806822fcc6 Mon Sep 17 00:00:00 2001 From: Eric Wheeler Date: Wed, 16 Jul 2025 14:29:55 -0700 Subject: [PATCH] fix: reduce redundant context in apply_diff error messages When apply_diff fails to find a match, the error message previously included: - The search content that was just sent (redundant since model already has it) - A large snippet of the original file (noisy and context-consuming) This change streamlines the error output to only show the "Best Match Found" section, which provides the most actionable information for debugging while conserving valuable context window space. The best match section shows exactly what was found in the file that was closest to the search pattern, enabling targeted corrections without re-reading the entire file. Fixes: #5795 Signed-off-by: Eric Wheeler --- .../diff/strategies/multi-file-search-replace.ts | 16 +--------------- src/core/diff/strategies/multi-search-replace.ts | 15 +-------------- 2 files changed, 2 insertions(+), 29 deletions(-) diff --git a/src/core/diff/strategies/multi-file-search-replace.ts b/src/core/diff/strategies/multi-file-search-replace.ts index d875d723a15..7d53b50450f 100644 --- a/src/core/diff/strategies/multi-file-search-replace.ts +++ b/src/core/diff/strategies/multi-file-search-replace.ts @@ -614,20 +614,6 @@ Each file requires its own path, start_line, and diff elements. searchLines = aggressiveSearchLines replaceLines = replaceContent ? replaceContent.split(/\r?\n/) : [] } else { - // No match found with either method - const originalContentSection = - startLine !== undefined && endLine !== undefined - ? `\n\nOriginal Content:\n${addLineNumbers( - resultLines - .slice( - Math.max(0, startLine - 1 - this.bufferLines), - Math.min(resultLines.length, endLine + this.bufferLines), - ) - .join("\n"), - Math.max(1, startLine - this.bufferLines), - )}` - : `\n\nOriginal Content:\n${addLineNumbers(resultLines.join("\n"))}` - const bestMatchSection = bestMatchContent ? `\n\nBest Match Found:\n${addLineNumbers(bestMatchContent, matchIndex + 1)}` : `\n\nBest Match Found:\n(no match)` @@ -644,7 +630,7 @@ Each file requires its own path, start_line, and diff elements. bestMatchScore * 100, )}%\n- Required Threshold: ${Math.floor(this.fuzzyThreshold * 100)}%\n- Search Range: ${ startLine ? `starting at line ${startLine}` : "start to end" - }\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}`, + }\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${bestMatchSection}`, }) continue } diff --git a/src/core/diff/strategies/multi-search-replace.ts b/src/core/diff/strategies/multi-search-replace.ts index b90ef4072d5..d9f704f84d2 100644 --- a/src/core/diff/strategies/multi-search-replace.ts +++ b/src/core/diff/strategies/multi-search-replace.ts @@ -521,19 +521,6 @@ Only use a single line of '=======' between search and replacement content, beca replaceLines = replaceContent ? replaceContent.split(/\r?\n/) : [] } else { // No match found with either method - const originalContentSection = - startLine !== undefined && endLine !== undefined - ? `\n\nOriginal Content:\n${addLineNumbers( - resultLines - .slice( - Math.max(0, startLine - 1 - this.bufferLines), - Math.min(resultLines.length, endLine + this.bufferLines), - ) - .join("\n"), - Math.max(1, startLine - this.bufferLines), - )}` - : `\n\nOriginal Content:\n${addLineNumbers(resultLines.join("\n"))}` - const bestMatchSection = bestMatchContent ? `\n\nBest Match Found:\n${addLineNumbers(bestMatchContent, matchIndex + 1)}` : `\n\nBest Match Found:\n(no match)` @@ -542,7 +529,7 @@ Only use a single line of '=======' between search and replacement content, beca diffResults.push({ success: false, - 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- 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}`, + 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- 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${bestMatchSection}`, }) continue }