@@ -12,6 +12,7 @@ import { formatResponse } from "../prompts/responses"
1212import { fileExistsAtPath } from "../../utils/fs"
1313import { RecordSource } from "../context-tracking/FileContextTrackerTypes"
1414import { unescapeHtmlEntities } from "../../utils/text-normalization"
15+ import { EXPERIMENT_IDS , experiments } from "../../shared/experiments"
1516
1617export async function applyDiffToolLegacy (
1718 cline : Task ,
@@ -87,7 +88,7 @@ export async function applyDiffToolLegacy(
8788 return
8889 }
8990
90- let originalContent : string | null = await fs . readFile ( absolutePath , "utf-8" )
91+ const originalContent : string = await fs . readFile ( absolutePath , "utf-8" )
9192
9293 // Apply the diff to the original content
9394 const diffResult = ( await cline . diffStrategy ?. applyDiff (
@@ -99,9 +100,6 @@ export async function applyDiffToolLegacy(
99100 error : "No diff strategy available" ,
100101 }
101102
102- // Release the original content from memory as it's no longer needed
103- originalContent = null
104-
105103 if ( ! diffResult . success ) {
106104 cline . consecutiveMistakeCount ++
107105 const currentCount = ( cline . consecutiveMistakeCountForApplyDiff . get ( relPath ) || 0 ) + 1
@@ -142,40 +140,79 @@ export async function applyDiffToolLegacy(
142140 cline . consecutiveMistakeCount = 0
143141 cline . consecutiveMistakeCountForApplyDiff . delete ( relPath )
144142
145- // Show diff view before asking for approval
146- cline . diffViewProvider . editType = "modify"
147- await cline . diffViewProvider . open ( relPath )
148- await cline . diffViewProvider . update ( diffResult . content , true )
149- cline . diffViewProvider . scrollToFirstDiff ( )
143+ // Check if preventFocusDisruption experiment is enabled
144+ const provider = cline . providerRef . deref ( )
145+ const state = await provider ?. getState ( )
146+ const diagnosticsEnabled = state ?. diagnosticsEnabled ?? true
147+ const writeDelayMs = state ?. writeDelayMs ?? DEFAULT_WRITE_DELAY_MS
148+ const isPreventFocusDisruptionEnabled = experiments . isEnabled (
149+ state ?. experiments ?? { } ,
150+ EXPERIMENT_IDS . PREVENT_FOCUS_DISRUPTION ,
151+ )
150152
151153 // Check if file is write-protected
152154 const isWriteProtected = cline . rooProtectedController ?. isWriteProtected ( relPath ) || false
153155
154- const completeMessage = JSON . stringify ( {
155- ...sharedMessageProps ,
156- diff : diffContent ,
157- isProtected : isWriteProtected ,
158- } satisfies ClineSayTool )
156+ if ( isPreventFocusDisruptionEnabled ) {
157+ // Direct file write without diff view
158+ const completeMessage = JSON . stringify ( {
159+ ...sharedMessageProps ,
160+ diff : diffContent ,
161+ isProtected : isWriteProtected ,
162+ } satisfies ClineSayTool )
159163
160- let toolProgressStatus
164+ let toolProgressStatus
161165
162- if ( cline . diffStrategy && cline . diffStrategy . getProgressStatus ) {
163- toolProgressStatus = cline . diffStrategy . getProgressStatus ( block , diffResult )
164- }
166+ if ( cline . diffStrategy && cline . diffStrategy . getProgressStatus ) {
167+ toolProgressStatus = cline . diffStrategy . getProgressStatus ( block , diffResult )
168+ }
165169
166- const didApprove = await askApproval ( "tool" , completeMessage , toolProgressStatus , isWriteProtected )
170+ const didApprove = await askApproval ( "tool" , completeMessage , toolProgressStatus , isWriteProtected )
167171
168- if ( ! didApprove ) {
169- await cline . diffViewProvider . revertChanges ( ) // Cline likely handles closing the diff view
170- return
171- }
172+ if ( ! didApprove ) {
173+ return
174+ }
172175
173- // Call saveChanges to update the DiffViewProvider properties
174- const provider = cline . providerRef . deref ( )
175- const state = await provider ?. getState ( )
176- const diagnosticsEnabled = state ?. diagnosticsEnabled ?? true
177- const writeDelayMs = state ?. writeDelayMs ?? DEFAULT_WRITE_DELAY_MS
178- await cline . diffViewProvider . saveChanges ( diagnosticsEnabled , writeDelayMs )
176+ // Save directly without showing diff view or opening the file
177+ cline . diffViewProvider . editType = "modify"
178+ cline . diffViewProvider . originalContent = originalContent
179+ await cline . diffViewProvider . saveDirectly (
180+ relPath ,
181+ diffResult . content ,
182+ false ,
183+ diagnosticsEnabled ,
184+ writeDelayMs ,
185+ )
186+ } else {
187+ // Original behavior with diff view
188+ // Show diff view before asking for approval
189+ cline . diffViewProvider . editType = "modify"
190+ await cline . diffViewProvider . open ( relPath )
191+ await cline . diffViewProvider . update ( diffResult . content , true )
192+ cline . diffViewProvider . scrollToFirstDiff ( )
193+
194+ const completeMessage = JSON . stringify ( {
195+ ...sharedMessageProps ,
196+ diff : diffContent ,
197+ isProtected : isWriteProtected ,
198+ } satisfies ClineSayTool )
199+
200+ let toolProgressStatus
201+
202+ if ( cline . diffStrategy && cline . diffStrategy . getProgressStatus ) {
203+ toolProgressStatus = cline . diffStrategy . getProgressStatus ( block , diffResult )
204+ }
205+
206+ const didApprove = await askApproval ( "tool" , completeMessage , toolProgressStatus , isWriteProtected )
207+
208+ if ( ! didApprove ) {
209+ await cline . diffViewProvider . revertChanges ( ) // Cline likely handles closing the diff view
210+ return
211+ }
212+
213+ // Call saveChanges to update the DiffViewProvider properties
214+ await cline . diffViewProvider . saveChanges ( diagnosticsEnabled , writeDelayMs )
215+ }
179216
180217 // Track file edit operation
181218 if ( relPath ) {
0 commit comments