@@ -178,6 +178,7 @@ describe("writeToFileTool", () => {
178178 }
179179 mockCline . say = vi . fn ( ) . mockResolvedValue ( undefined )
180180 mockCline . ask = vi . fn ( ) . mockResolvedValue ( undefined )
181+ mockCline . finalizePartialToolAsk = vi . fn ( ) . mockResolvedValue ( undefined )
181182 mockCline . recordToolError = vi . fn ( )
182183 mockCline . sayAndCreateMissingParamError = vi . fn ( ) . mockResolvedValue ( "Missing param error" )
183184
@@ -453,16 +454,104 @@ describe("writeToFileTool", () => {
453454 expect ( mockCline . diffViewProvider . reset ) . toHaveBeenCalled ( )
454455 } )
455456
456- it ( "handles partial streaming errors after path stabilizes" , async ( ) => {
457+ it ( "swallows partial streaming errors instead of surfacing a duplicate error bubble" , async ( ) => {
458+ // The same filesystem operation is retried in execute() once the block completes,
459+ // and that authoritative non-partial path reports the error to the user. Surfacing
460+ // it during streaming too would show the same error twice, so handlePartial must NOT
461+ // route streaming errors through handleError.
457462 mockCline . diffViewProvider . open . mockRejectedValue ( new Error ( "Open failed" ) )
458463
459464 // First call - path not yet stabilized, no error yet
460465 await executeWriteFileTool ( { } , { isPartial : true } )
461466 expect ( mockHandleError ) . not . toHaveBeenCalled ( )
462467
463- // Second call with same path - path is now stabilized, error occurs
468+ // Second call with same path - path is now stabilized, error occurs but is swallowed
464469 await executeWriteFileTool ( { } , { isPartial : true } )
465- expect ( mockHandleError ) . toHaveBeenCalledWith ( "handling partial write_to_file" , expect . any ( Error ) )
470+ expect ( mockHandleError ) . not . toHaveBeenCalled ( )
471+ } )
472+
473+ it ( "finalizes partial tool message and resets diff view when handlePartial open() fails" , async ( ) => {
474+ // Regression test: when diffViewProvider.open() throws during streaming (e.g. EACCES/EROFS
475+ // on a read-only path), the partial tool ask created at the top of handlePartial leaves the
476+ // UI spinner stuck. handlePartial must finalize the partial message and reset the diff view,
477+ // and must NOT surface a duplicate error (execute() reports the authoritative one).
478+ mockCline . diffViewProvider . open . mockRejectedValue (
479+ Object . assign ( new Error ( "EACCES: permission denied, open '/ro/test.py'" ) , { code : "EACCES" } ) ,
480+ )
481+
482+ // First call - path not yet stabilized
483+ await executeWriteFileTool ( { } , { isPartial : true } )
484+ expect ( mockCline . finalizePartialToolAsk ) . not . toHaveBeenCalled ( )
485+
486+ // Second call - path stabilized, open() rejects
487+ await executeWriteFileTool ( { } , { isPartial : true } )
488+
489+ expect ( mockCline . finalizePartialToolAsk ) . toHaveBeenCalled ( )
490+ expect ( mockCline . diffViewProvider . reset ) . toHaveBeenCalled ( )
491+ expect ( mockHandleError ) . not . toHaveBeenCalled ( )
492+ } )
493+
494+ it ( "finalizes partial tool message and resets diff view when handlePartial update() fails" , async ( ) => {
495+ // Same regression as above but for the streaming update() call failing after open() succeeds.
496+ mockCline . diffViewProvider . update . mockRejectedValue (
497+ Object . assign ( new Error ( "EROFS: read-only file system, write '/ro/test.py'" ) , { code : "EROFS" } ) ,
498+ )
499+
500+ // First call - path not yet stabilized
501+ await executeWriteFileTool ( { } , { isPartial : true } )
502+
503+ // Second call - path stabilized, update() rejects
504+ await executeWriteFileTool ( { } , { isPartial : true } )
505+
506+ expect ( mockCline . finalizePartialToolAsk ) . toHaveBeenCalled ( )
507+ expect ( mockCline . diffViewProvider . reset ) . toHaveBeenCalled ( )
508+ expect ( mockHandleError ) . not . toHaveBeenCalled ( )
509+ } )
510+
511+ it ( "does not spawn a new partial tool message on each streaming delta after a failure" , async ( ) => {
512+ // Regression test: after diffViewProvider.open() throws and the partial message is
513+ // finalized + diff view reset, the next streaming delta saw a non-partial last message
514+ // and created a brand new "Zoo wants to edit this file" message -- repeating once per
515+ // delta. After the fix, partialStreamFailed short-circuits subsequent deltas so only
516+ // the single initial partial ask is issued.
517+ mockCline . diffViewProvider . open . mockRejectedValue (
518+ Object . assign ( new Error ( "EROFS: read-only file system, mkdir '/scratch'" ) , { code : "EROFS" } ) ,
519+ )
520+
521+ // Delta 1 - stabilize path (no ask yet)
522+ await executeWriteFileTool ( { } , { fileExists : false , isPartial : true } )
523+ // Delta 2 - path stabilized, ask issued once, open() fails, stream marked failed
524+ await executeWriteFileTool ( { } , { fileExists : false , isPartial : true } )
525+ // Deltas 3..5 - must be short-circuited, no further asks
526+ await executeWriteFileTool ( { } , { fileExists : false , isPartial : true } )
527+ await executeWriteFileTool ( { } , { fileExists : false , isPartial : true } )
528+ await executeWriteFileTool ( { } , { fileExists : false , isPartial : true } )
529+
530+ // Only the single partial ask from delta 2 should have been issued
531+ expect ( mockCline . ask ) . toHaveBeenCalledTimes ( 1 )
532+ // open() must not be retried after the first failure
533+ expect ( mockCline . diffViewProvider . open ) . toHaveBeenCalledTimes ( 1 )
534+ } )
535+
536+ it ( "reports a filesystem error only once across the streaming and execute phases" , async ( ) => {
537+ // Regression test for the double-error UX defect: a single write_to_file call to a
538+ // read-only path failed twice -- once in handlePartial ("handling partial write_to_file")
539+ // and once in execute() ("writing file"). handlePartial now swallows its error so only
540+ // the authoritative execute() error is surfaced.
541+ const erofs = ( ) =>
542+ Object . assign ( new Error ( "EROFS: read-only file system, mkdir '/scratch'" ) , { code : "EROFS" } )
543+ mockCline . diffViewProvider . open . mockRejectedValue ( erofs ( ) )
544+ mockedCreateDirectoriesForFile . mockRejectedValue ( erofs ( ) )
545+
546+ // Streaming phase: stabilize path then fail (swallowed, no handleError)
547+ await executeWriteFileTool ( { } , { fileExists : false , isPartial : true } )
548+ await executeWriteFileTool ( { } , { fileExists : false , isPartial : true } )
549+
550+ // Final phase: execute() reports the single authoritative error
551+ await executeWriteFileTool ( { } , { fileExists : false } )
552+
553+ expect ( mockHandleError ) . toHaveBeenCalledTimes ( 1 )
554+ expect ( mockHandleError ) . toHaveBeenCalledWith ( "writing file" , expect . any ( Error ) )
466555 } )
467556
468557 it . skipIf ( process . platform === "win32" ) (
@@ -509,5 +598,27 @@ describe("writeToFileTool", () => {
509598 expect ( mockCline . diffViewProvider . saveChanges ) . not . toHaveBeenCalled ( )
510599 } ,
511600 )
601+
602+ it . skipIf ( process . platform === "win32" ) (
603+ "finalizes partial tool message on error so the UI spinner does not get stuck" ,
604+ async ( ) => {
605+ // Regression test: when a filesystem error is thrown in execute() the webview
606+ // message created during handlePartial (or the early ask in execute) is stuck in
607+ // partial: true state, showing an indefinite spinner alongside the error bubble.
608+ // The catch block must call finalizePartialToolAsk() to close the spinner without
609+ // blocking for user input.
610+ mockedCreateDirectoriesForFile . mockRejectedValue (
611+ Object . assign ( new Error ( "EACCES: permission denied, mkdir '/ro'" ) , { code : "EACCES" } ) ,
612+ )
613+
614+ await executeWriteFileTool ( { } , { fileExists : false } )
615+
616+ // handleError must still be called
617+ expect ( mockHandleError ) . toHaveBeenCalledWith ( "writing file" , expect . any ( Error ) )
618+
619+ // finalizePartialToolAsk must have been called to dismiss the spinner
620+ expect ( mockCline . finalizePartialToolAsk ) . toHaveBeenCalled ( )
621+ } ,
622+ )
512623 } )
513624} )
0 commit comments