@@ -245,12 +245,6 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> {
245245 task . diffViewProvider . editType = fileExists ? "modify" : "create"
246246 }
247247
248- // Create parent directories early for new files to prevent ENOENT errors
249- // in subsequent operations (e.g., diffViewProvider.open, fs.readFile)
250- if ( ! fileExists ) {
251- await createDirectoriesForFile ( absolutePath )
252- }
253-
254248 if ( newContent . startsWith ( "```" ) ) {
255249 newContent = newContent . split ( "\n" ) . slice ( 1 ) . join ( "\n" )
256250 }
@@ -274,7 +268,19 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> {
274268 isProtected : isWriteProtected ,
275269 }
276270
271+ // Tracks whether the user approved the write, so the error path only reverts the
272+ // diff document when the content was never approved (an approved edit is kept in
273+ // the editor so the user can save it manually after a late failure).
274+ let writeApproved = false
275+
277276 try {
277+ // Create parent directories for new files inside the try block so filesystem
278+ // errors (EROFS, EACCES, etc.) route through handleError with proper cleanup
279+ // and consecutive-mistake counting, rather than escaping unhandled.
280+ if ( ! fileExists ) {
281+ await createDirectoriesForFile ( absolutePath )
282+ }
283+
278284 task . consecutiveMistakeCount = 0
279285
280286 const provider = task . providerRef . deref ( )
@@ -308,9 +314,17 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> {
308314 const didApprove = await askApproval ( "tool" , completeMessage , undefined , isWriteProtected )
309315
310316 if ( ! didApprove ) {
317+ // The prevent-focus branch set editType/originalContent on the provider
318+ // before asking. Clear them on denial (no diff document was opened in
319+ // this branch, so reset() is sufficient; the non-prevent-focus denial
320+ // branch resets through revertChanges()), so a later write re-checks
321+ // the file system instead of reusing the stale editType.
322+ await this . resetDiffViewAfterWrite ( task )
311323 return
312324 }
313325
326+ writeApproved = true
327+
314328 await task . diffViewProvider . saveDirectly ( relPath , newContent , false , diagnosticsEnabled , writeDelayMs )
315329 } else {
316330 if ( ! task . diffViewProvider . isEditing ) {
@@ -344,6 +358,8 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> {
344358 return
345359 }
346360
361+ writeApproved = true
362+
347363 await task . diffViewProvider . saveChanges ( diagnosticsEnabled , writeDelayMs )
348364 }
349365
@@ -357,17 +373,35 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> {
357373
358374 pushToolResult ( message )
359375
360- await task . diffViewProvider . reset ( )
361- this . resetPartialState ( )
376+ await this . resetDiffViewAfterWrite ( task )
362377
363378 task . processQueuedMessages ( )
364379
365380 return
366381 } catch ( error ) {
367- await handleError ( "writing file" , error as Error )
368- await task . diffViewProvider . reset ( )
369- this . resetPartialState ( )
382+ // Finalize any open partial tool message so the UI spinner doesn't get stuck.
383+ // The partial ask fired during streaming (handlePartial) or early in execute sets
384+ // partial: true on the webview message; without this, the spinner persists even
385+ // after the error bubble appears.
386+ await this . finalizePartialToolAskAfterFailure ( task )
387+ // The diff cleanup runs in a finally around handleError: the production
388+ // handleError awaits Task.say(), which rejects when the task is aborted, and a
389+ // rejected handleError must not skip restoring the unapproved streamed content.
390+ try {
391+ await handleError ( "writing file" , error as Error )
392+ } finally {
393+ // Before approval the diff document holds unapproved streamed content:
394+ // restore it so a user save cannot persist it. After approval the content
395+ // is the user's accepted edit -- keep it in the editor (dirty) so they can
396+ // save it manually.
397+ if ( ! writeApproved ) {
398+ await this . revertDiffChangesBeforeReset ( task )
399+ }
400+ await this . resetDiffViewAfterWrite ( task )
401+ }
370402 return
403+ } finally {
404+ this . resetTaskPartialState ( task )
371405 }
372406 }
373407
0 commit comments