mirror of
https://github.com/RooVetGit/Roo-Code.git
synced 2026-10-14 04:08:28 +00:00
Part 1 - UI Bug Fix: - ApplyDiffTool: Add finalizePartialToolAskIfNeeded to close partial ask messages on error paths, preventing stuck spinners - ApplyDiffTool: Always show diff_error on first failure (was >= 2) - ApplyDiffTool: Set didToolFailInCurrentTurn on diff apply failure - EditFileTool: Always show diff_error on first failure (was >= 2) Part 2 - Re-anchor Before Edit Experiment: - Add reAnchorBeforeEdit experiment type to packages/types - Add RE_ANCHOR_BEFORE_EDIT to shared experiments config (default off) - When enabled, adds system prompt guideline instructing the model to re-read a file before every edit to reduce hallucination-based failures - Add English localization keys for settings UI - Settings UI auto-renders via existing ExperimentalSettings component
339 lines
11 KiB
TypeScript
339 lines
11 KiB
TypeScript
import path from "path"
|
|
import fs from "fs/promises"
|
|
|
|
import { type ClineSayTool, DEFAULT_WRITE_DELAY_MS } from "@roo-code/types"
|
|
import { TelemetryService } from "@roo-code/telemetry"
|
|
|
|
import { getReadablePath } from "../../utils/path"
|
|
import { isPathOutsideWorkspace } from "../../utils/pathUtils"
|
|
import { Task } from "../task/Task"
|
|
import { formatResponse } from "../prompts/responses"
|
|
import { fileExistsAtPath } from "../../utils/fs"
|
|
import { RecordSource } from "../context-tracking/FileContextTrackerTypes"
|
|
import { unescapeHtmlEntities } from "../../utils/text-normalization"
|
|
import { EXPERIMENT_IDS, experiments } from "../../shared/experiments"
|
|
import { computeDiffStats, sanitizeUnifiedDiff } from "../diff/stats"
|
|
import type { ToolUse } from "../../shared/tools"
|
|
|
|
import { BaseTool, ToolCallbacks } from "./BaseTool"
|
|
|
|
interface ApplyDiffParams {
|
|
path: string
|
|
diff: string
|
|
}
|
|
|
|
export class ApplyDiffTool extends BaseTool<"apply_diff"> {
|
|
readonly name = "apply_diff" as const
|
|
|
|
private didSendPartialToolAsk = false
|
|
private partialToolAskRelPath: string | undefined
|
|
|
|
async execute(params: ApplyDiffParams, task: Task, callbacks: ToolCallbacks): Promise<void> {
|
|
const { askApproval, handleError, pushToolResult } = callbacks
|
|
let { path: relPath, diff: diffContent } = params
|
|
|
|
if (diffContent && !task.api.getModel().id.includes("claude")) {
|
|
diffContent = unescapeHtmlEntities(diffContent)
|
|
}
|
|
|
|
// Finalize any outstanding partial tool ask so the UI spinner doesn't get stuck.
|
|
const finalizePartialToolAskIfNeeded = async (currentRelPath?: string): Promise<void> => {
|
|
if (!this.didSendPartialToolAsk) {
|
|
return
|
|
}
|
|
|
|
if (this.partialToolAskRelPath && currentRelPath && this.partialToolAskRelPath !== currentRelPath) {
|
|
return
|
|
}
|
|
|
|
const sharedMessageProps: ClineSayTool = {
|
|
tool: "appliedDiff",
|
|
path: getReadablePath(task.cwd, currentRelPath ?? relPath ?? ""),
|
|
diff: diffContent,
|
|
}
|
|
|
|
// Finalize the existing partial tool ask row so the UI doesn't get stuck in a spinner state.
|
|
await task.ask("tool", JSON.stringify(sharedMessageProps), false).catch(() => {})
|
|
}
|
|
|
|
try {
|
|
if (!relPath) {
|
|
task.consecutiveMistakeCount++
|
|
task.recordToolError("apply_diff")
|
|
await finalizePartialToolAskIfNeeded()
|
|
pushToolResult(await task.sayAndCreateMissingParamError("apply_diff", "path"))
|
|
return
|
|
}
|
|
|
|
if (!diffContent) {
|
|
task.consecutiveMistakeCount++
|
|
task.recordToolError("apply_diff")
|
|
await finalizePartialToolAskIfNeeded()
|
|
pushToolResult(await task.sayAndCreateMissingParamError("apply_diff", "diff"))
|
|
return
|
|
}
|
|
|
|
const accessAllowed = task.rooIgnoreController?.validateAccess(relPath)
|
|
|
|
if (!accessAllowed) {
|
|
await finalizePartialToolAskIfNeeded(relPath)
|
|
await task.say("rooignore_error", relPath)
|
|
pushToolResult(formatResponse.rooIgnoreError(relPath))
|
|
return
|
|
}
|
|
|
|
const absolutePath = path.resolve(task.cwd, relPath)
|
|
const fileExists = await fileExistsAtPath(absolutePath)
|
|
|
|
if (!fileExists) {
|
|
task.consecutiveMistakeCount++
|
|
task.recordToolError("apply_diff")
|
|
await finalizePartialToolAskIfNeeded(relPath)
|
|
const formattedError = `File does not exist at path: ${absolutePath}\n\n<error_details>\nThe specified file could not be found. Please verify the file path and try again.\n</error_details>`
|
|
await task.say("error", formattedError)
|
|
task.didToolFailInCurrentTurn = true
|
|
pushToolResult(formattedError)
|
|
return
|
|
}
|
|
|
|
const originalContent: string = await fs.readFile(absolutePath, "utf-8")
|
|
|
|
// Apply the diff to the original content
|
|
const diffResult = (await task.diffStrategy?.applyDiff(
|
|
originalContent,
|
|
diffContent,
|
|
parseInt(params.diff.match(/:start_line:(\d+)/)?.[1] ?? ""),
|
|
)) ?? {
|
|
success: false,
|
|
error: "No diff strategy available",
|
|
}
|
|
|
|
if (!diffResult.success) {
|
|
task.consecutiveMistakeCount++
|
|
const currentCount = (task.consecutiveMistakeCountForApplyDiff.get(relPath) || 0) + 1
|
|
task.consecutiveMistakeCountForApplyDiff.set(relPath, currentCount)
|
|
let formattedError = ""
|
|
TelemetryService.instance.captureDiffApplicationError(task.taskId, currentCount)
|
|
|
|
if (diffResult.failParts && diffResult.failParts.length > 0) {
|
|
for (const failPart of diffResult.failParts) {
|
|
if (failPart.success) {
|
|
continue
|
|
}
|
|
|
|
const errorDetails = failPart.details ? JSON.stringify(failPart.details, null, 2) : ""
|
|
|
|
formattedError = `<error_details>\n${
|
|
failPart.error
|
|
}${errorDetails ? `\n\nDetails:\n${errorDetails}` : ""}\n</error_details>`
|
|
}
|
|
} else {
|
|
const errorDetails = diffResult.details ? JSON.stringify(diffResult.details, null, 2) : ""
|
|
|
|
formattedError = `Unable to apply diff to file: ${absolutePath}\n\n<error_details>\n${
|
|
diffResult.error
|
|
}${errorDetails ? `\n\nDetails:\n${errorDetails}` : ""}\n</error_details>`
|
|
}
|
|
|
|
// Always finalize any pending partial ask to stop the spinner.
|
|
await finalizePartialToolAskIfNeeded(relPath)
|
|
|
|
// Always show diff_error on every failure so the user gets immediate visual feedback.
|
|
await task.say("diff_error", formattedError)
|
|
|
|
task.recordToolError("apply_diff", formattedError)
|
|
task.didToolFailInCurrentTurn = true
|
|
|
|
pushToolResult(formattedError)
|
|
return
|
|
}
|
|
|
|
task.consecutiveMistakeCount = 0
|
|
task.consecutiveMistakeCountForApplyDiff.delete(relPath)
|
|
|
|
// Generate backend-unified diff for display in chat/webview
|
|
const unifiedPatchRaw = formatResponse.createPrettyPatch(relPath, originalContent, diffResult.content)
|
|
const unifiedPatch = sanitizeUnifiedDiff(unifiedPatchRaw)
|
|
const diffStats = computeDiffStats(unifiedPatch) || undefined
|
|
|
|
// Check if preventFocusDisruption experiment is enabled
|
|
const provider = task.providerRef.deref()
|
|
const state = await provider?.getState()
|
|
const diagnosticsEnabled = state?.diagnosticsEnabled ?? true
|
|
const writeDelayMs = state?.writeDelayMs ?? DEFAULT_WRITE_DELAY_MS
|
|
const isPreventFocusDisruptionEnabled = experiments.isEnabled(
|
|
state?.experiments ?? {},
|
|
EXPERIMENT_IDS.PREVENT_FOCUS_DISRUPTION,
|
|
)
|
|
|
|
// Check if file is write-protected
|
|
const isWriteProtected = task.rooProtectedController?.isWriteProtected(relPath) || false
|
|
|
|
const sharedMessageProps: ClineSayTool = {
|
|
tool: "appliedDiff",
|
|
path: getReadablePath(task.cwd, relPath),
|
|
diff: diffContent,
|
|
}
|
|
|
|
if (isPreventFocusDisruptionEnabled) {
|
|
// Direct file write without diff view
|
|
const completeMessage = JSON.stringify({
|
|
...sharedMessageProps,
|
|
diff: diffContent,
|
|
content: unifiedPatch,
|
|
originalContent,
|
|
diffStats,
|
|
isProtected: isWriteProtected,
|
|
} satisfies ClineSayTool)
|
|
|
|
let toolProgressStatus
|
|
|
|
if (task.diffStrategy && task.diffStrategy.getProgressStatus) {
|
|
const block: ToolUse<"apply_diff"> = {
|
|
type: "tool_use",
|
|
name: "apply_diff",
|
|
params: { path: relPath, diff: diffContent },
|
|
partial: false,
|
|
}
|
|
toolProgressStatus = task.diffStrategy.getProgressStatus(block, diffResult)
|
|
}
|
|
|
|
const didApprove = await askApproval("tool", completeMessage, toolProgressStatus, isWriteProtected)
|
|
|
|
if (!didApprove) {
|
|
return
|
|
}
|
|
|
|
// Save directly without showing diff view or opening the file
|
|
task.diffViewProvider.editType = "modify"
|
|
task.diffViewProvider.originalContent = originalContent
|
|
await task.diffViewProvider.saveDirectly(
|
|
relPath,
|
|
diffResult.content,
|
|
false,
|
|
diagnosticsEnabled,
|
|
writeDelayMs,
|
|
)
|
|
} else {
|
|
// Original behavior with diff view
|
|
// Show diff view before asking for approval
|
|
task.diffViewProvider.editType = "modify"
|
|
await task.diffViewProvider.open(relPath)
|
|
await task.diffViewProvider.update(diffResult.content, true)
|
|
task.diffViewProvider.scrollToFirstDiff()
|
|
|
|
const completeMessage = JSON.stringify({
|
|
...sharedMessageProps,
|
|
diff: diffContent,
|
|
content: unifiedPatch,
|
|
originalContent,
|
|
diffStats,
|
|
isProtected: isWriteProtected,
|
|
} satisfies ClineSayTool)
|
|
|
|
let toolProgressStatus
|
|
|
|
if (task.diffStrategy && task.diffStrategy.getProgressStatus) {
|
|
const block: ToolUse<"apply_diff"> = {
|
|
type: "tool_use",
|
|
name: "apply_diff",
|
|
params: { path: relPath, diff: diffContent },
|
|
partial: false,
|
|
}
|
|
toolProgressStatus = task.diffStrategy.getProgressStatus(block, diffResult)
|
|
}
|
|
|
|
const didApprove = await askApproval("tool", completeMessage, toolProgressStatus, isWriteProtected)
|
|
|
|
if (!didApprove) {
|
|
await task.diffViewProvider.revertChanges()
|
|
task.processQueuedMessages()
|
|
return
|
|
}
|
|
|
|
// Call saveChanges to update the DiffViewProvider properties
|
|
await task.diffViewProvider.saveChanges(diagnosticsEnabled, writeDelayMs)
|
|
}
|
|
|
|
// Track file edit operation
|
|
if (relPath) {
|
|
await task.fileContextTracker.trackFileContext(relPath, "roo_edited" as RecordSource)
|
|
}
|
|
|
|
// Used to determine if we should wait for busy terminal to update before sending api request
|
|
task.didEditFile = true
|
|
let partFailHint = ""
|
|
|
|
if (diffResult.failParts && diffResult.failParts.length > 0) {
|
|
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`
|
|
}
|
|
|
|
// Get the formatted response message
|
|
const message = await task.diffViewProvider.pushToolWriteResult(task, task.cwd, !fileExists)
|
|
|
|
// Check for single SEARCH/REPLACE block warning
|
|
const searchBlocks = (diffContent.match(/<<<<<<< SEARCH/g) || []).length
|
|
const singleBlockNotice =
|
|
searchBlocks === 1
|
|
? "\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>"
|
|
: ""
|
|
|
|
if (partFailHint) {
|
|
pushToolResult(partFailHint + message + singleBlockNotice)
|
|
} else {
|
|
pushToolResult(message + singleBlockNotice)
|
|
}
|
|
|
|
await task.diffViewProvider.reset()
|
|
this.resetPartialState()
|
|
|
|
// Process any queued messages after file edit completes
|
|
task.processQueuedMessages()
|
|
|
|
return
|
|
} catch (error) {
|
|
await finalizePartialToolAskIfNeeded(relPath)
|
|
await handleError("applying diff", error as Error)
|
|
await task.diffViewProvider.reset()
|
|
task.processQueuedMessages()
|
|
return
|
|
} finally {
|
|
this.didSendPartialToolAsk = false
|
|
this.partialToolAskRelPath = undefined
|
|
this.resetPartialState()
|
|
}
|
|
}
|
|
|
|
override async handlePartial(task: Task, block: ToolUse<"apply_diff">): Promise<void> {
|
|
const relPath: string | undefined = block.params.path
|
|
const diffContent: string | undefined = block.params.diff
|
|
|
|
// Wait for path to stabilize before showing UI (prevents truncated paths)
|
|
if (!this.hasPathStabilized(relPath)) {
|
|
return
|
|
}
|
|
|
|
this.didSendPartialToolAsk = true
|
|
this.partialToolAskRelPath = relPath
|
|
|
|
const sharedMessageProps: ClineSayTool = {
|
|
tool: "appliedDiff",
|
|
path: getReadablePath(task.cwd, relPath),
|
|
diff: diffContent,
|
|
}
|
|
|
|
let toolProgressStatus
|
|
|
|
if (task.diffStrategy && task.diffStrategy.getProgressStatus) {
|
|
toolProgressStatus = task.diffStrategy.getProgressStatus(block)
|
|
}
|
|
|
|
if (toolProgressStatus && Object.keys(toolProgressStatus).length === 0) {
|
|
return
|
|
}
|
|
|
|
await task.ask("tool", JSON.stringify(sharedMessageProps), block.partial, toolProgressStatus).catch(() => {})
|
|
}
|
|
}
|
|
|
|
export const applyDiffTool = new ApplyDiffTool()
|