Skip to content

Commit 53568a5

Browse files
committed
fix: clear stuck UI spinner and duplicate/repeated errors on write_to_file filesystem failure
When write_to_file hits a filesystem error (EROFS/EACCES) the streaming phase left the "Zoo wants to edit this file" spinner running, surfaced the same error twice (handlePartial + execute), and spawned a new partial tool message on every subsequent streaming delta. - Add Task.finalizePartialToolAsk() to finalize a partial tool ask without blocking on user input, dismissing the spinner. - handlePartial swallows streaming filesystem errors (after finalizing the spinner and resetting the diff view) so only the authoritative execute() error is reported, eliminating the duplicate error bubble. - Track partialStreamFailed so later streaming deltas short-circuit instead of re-attempting and spawning repeated partial tool messages. - Add regression tests for spinner finalization, single-error reporting, and no repeated partial messages.
1 parent 8c45731 commit 53568a5

3 files changed

Lines changed: 178 additions & 10 deletions

File tree

‎src/core/task/Task.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,6 +1730,21 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
17301730
return formatResponse.toolError(formatResponse.missingToolParameterError(paramName))
17311731
}
17321732

1733+
/**
1734+
* Finalize the last partial "tool" ask message without blocking for user input.
1735+
* Call this in error paths where a partial tool message was opened during streaming
1736+
* but execution failed before the normal approval flow could close it, so the webview
1737+
* spinner does not get stuck in a loading state.
1738+
*/
1739+
async finalizePartialToolAsk(): Promise<void> {
1740+
const lastMessage = this.clineMessages.at(-1)
1741+
1742+
if (lastMessage && lastMessage.partial && lastMessage.type === "ask" && lastMessage.ask === "tool") {
1743+
lastMessage.partial = false
1744+
await this.updateClineMessage(lastMessage)
1745+
}
1746+
}
1747+
17331748
// Lifecycle
17341749
// Start / Resume / Abort / Dispose
17351750

‎src/core/tools/WriteToFileTool.ts‎

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ interface WriteToFileParams {
2626
export class WriteToFileTool extends BaseTool<"write_to_file"> {
2727
readonly name = "write_to_file" as const
2828

29+
/**
30+
* Set when a filesystem error aborts diff-view streaming during handlePartial for the
31+
* current tool invocation. Subsequent streaming deltas for the same block then skip the
32+
* doomed open()/update() retry, which would otherwise create a fresh "Zoo wants to edit
33+
* this file" message on every delta. Cleared by resetPartialState() between invocations.
34+
*/
35+
private partialStreamFailed = false
36+
37+
override resetPartialState(): void {
38+
super.resetPartialState()
39+
this.partialStreamFailed = false
40+
}
41+
2942
async execute(params: WriteToFileParams, task: Task, callbacks: ToolCallbacks): Promise<void> {
3043
const { pushToolResult, handleError, askApproval } = callbacks
3144
const relPath = params.path
@@ -187,6 +200,11 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> {
187200

188201
return
189202
} catch (error) {
203+
// Finalize any open partial tool message so the UI spinner doesn't get stuck.
204+
// The partial ask fired during streaming (handlePartial) or early in execute sets
205+
// partial: true on the webview message; without this, the spinner persists even
206+
// after the error bubble appears.
207+
await task.finalizePartialToolAsk()
190208
await handleError("writing file", error as Error)
191209
await task.diffViewProvider.reset()
192210
this.resetPartialState()
@@ -198,6 +216,13 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> {
198216
const relPath: string | undefined = block.params.path
199217
let newContent: string | undefined = block.params.content
200218

219+
// A prior streaming delta for this invocation already hit a fatal filesystem error.
220+
// Skip further streaming work so we don't create a new partial tool message on every
221+
// subsequent delta. execute() will report the error once when the block completes.
222+
if (this.partialStreamFailed) {
223+
return
224+
}
225+
201226
// Wait for path to stabilize before showing UI (prevents truncated paths)
202227
if (!this.hasPathStabilized(relPath) || newContent === undefined) {
203228
return
@@ -240,14 +265,31 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> {
240265
await task.ask("tool", partialMessage, block.partial).catch(() => {})
241266

242267
if (newContent) {
243-
if (!task.diffViewProvider.isEditing) {
244-
await task.diffViewProvider.open(relPath!)
245-
}
268+
try {
269+
if (!task.diffViewProvider.isEditing) {
270+
await task.diffViewProvider.open(relPath!)
271+
}
246272

247-
await task.diffViewProvider.update(
248-
everyLineHasLineNumbers(newContent) ? stripLineNumbers(newContent) : newContent,
249-
false,
250-
)
273+
await task.diffViewProvider.update(
274+
everyLineHasLineNumbers(newContent) ? stripLineNumbers(newContent) : newContent,
275+
false,
276+
)
277+
} catch (error) {
278+
// Opening or updating the diff view can throw on filesystem errors
279+
// (EACCES/EROFS on read-only paths). Finalize the partial tool message
280+
// so the UI spinner doesn't get stuck and reset the diff view. Do NOT
281+
// rethrow: the same filesystem operation is retried in execute() once the
282+
// block completes, and that authoritative non-partial path reports the
283+
// error to the user. Surfacing it here too would show the same error twice.
284+
// Swallowing it here is safe because the agent loop advances naturally when
285+
// the non-partial block arrives (it does not depend on this throw).
286+
console.error(`Error streaming write_to_file diff view:`, error)
287+
// Mark the stream as failed so later deltas don't re-attempt and spawn a new
288+
// partial tool message each time.
289+
this.partialStreamFailed = true
290+
await task.finalizePartialToolAsk()
291+
await task.diffViewProvider.reset()
292+
}
251293
}
252294
}
253295
}

‎src/core/tools/__tests__/writeToFileTool.spec.ts‎

Lines changed: 114 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)