Skip to content

Commit 7b6835f

Browse files
committed
fix(write_to_file): run diff cleanup when handleError rejects; tighten abort-listener assertion
Address the remaining CodeRabbit findings: - WriteToFileTool.execute(): the catch block awaited handleError before restoring the diff document; the production handleError awaits Task.say(), which rejects when the task is aborted, so a rejected handleError skipped the revert/reset cleanup and could leave unapproved streamed content dirty. The cleanup now runs in a finally around handleError (the !writeApproved condition and revert-before-reset ordering are preserved) and the handleError rejection still propagates. - writeToFileTool.spec.ts: the parse-failure test now captures the registered TaskAborted listener and asserts off() receives that exact reference instead of expect.any(Function). Adds a regression test proving the cleanup runs when handleError rejects.
1 parent a704b15 commit 7b6835f

2 files changed

Lines changed: 52 additions & 9 deletions

File tree

‎src/core/tools/WriteToFileTool.ts‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -387,14 +387,21 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> {
387387
// partial: true on the webview message; without this, the spinner persists even
388388
// after the error bubble appears.
389389
await this.finalizePartialToolAskAfterFailure(task)
390-
await handleError("writing file", error as Error)
391-
// Before approval the diff document holds unapproved streamed content: restore it
392-
// so a user save cannot persist it. After approval the content is the user's
393-
// accepted edit -- keep it in the editor (dirty) so they can save it manually.
394-
if (!writeApproved) {
395-
await this.revertDiffChangesBeforeReset(task)
390+
// The diff cleanup runs in a finally around handleError: the production
391+
// handleError awaits Task.say(), which rejects when the task is aborted, and a
392+
// rejected handleError must not skip restoring the unapproved streamed content.
393+
try {
394+
await handleError("writing file", error as Error)
395+
} finally {
396+
// Before approval the diff document holds unapproved streamed content:
397+
// restore it so a user save cannot persist it. After approval the content
398+
// is the user's accepted edit -- keep it in the editor (dirty) so they can
399+
// save it manually.
400+
if (!writeApproved) {
401+
await this.revertDiffChangesBeforeReset(task)
402+
}
403+
await this.resetDiffViewAfterWrite(task)
396404
}
397-
await this.resetDiffViewAfterWrite(task)
398405
return
399406
} finally {
400407
this.resetTaskPartialState(task)

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

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,15 @@ describe("writeToFileTool", () => {
725725
// blocked by the stale streamFailed guard and no abort listener leaks.
726726
const fsError = new Error("EACCES: permission denied")
727727
mockCline.diffViewProvider.open.mockRejectedValue(fsError)
728+
// Capture the abort listener of the state created by the first deltas: it is the
729+
// exact reference that the parse-failure teardown must detach.
730+
let abortListener: (() => void) | undefined
731+
mockCline.once.mockImplementation((event: RooCodeEventName, listener: () => void) => {
732+
if (event === RooCodeEventName.TaskAborted && abortListener === undefined) {
733+
abortListener = listener
734+
}
735+
return mockCline
736+
})
728737
const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {})
729738
try {
730739
await executeWriteFileTool({}, { isPartial: true })
@@ -749,9 +758,11 @@ describe("writeToFileTool", () => {
749758
expect.stringContaining("parsing write_to_file"),
750759
expect.anything(),
751760
)
752-
// Per-task state torn down at this boundary: guard cleared, listener detached.
761+
// Per-task state torn down at this boundary: guard cleared, the exact
762+
// registered abort listener detached.
753763
expect(writeToFileTool["taskPartialStreamState"].size).toBe(0)
754-
expect(mockCline.off).toHaveBeenCalledWith(RooCodeEventName.TaskAborted, expect.any(Function))
764+
expect(abortListener).toBeTypeOf("function")
765+
expect(mockCline.off).toHaveBeenCalledWith(RooCodeEventName.TaskAborted, abortListener)
755766

756767
// The next stream for the same task must issue a partial ask again (the stale
757768
// streamFailed guard is gone).
@@ -1364,6 +1375,31 @@ describe("writeToFileTool", () => {
13641375
// The write was never approved, so the diff document is reverted before reset
13651376
expect(mockCline.diffViewProvider.revertChanges).toHaveBeenCalledTimes(1)
13661377
})
1378+
1379+
it("runs diff cleanup when handleError rejects", async () => {
1380+
// The production handleError awaits Task.say(), which rejects when the task is
1381+
// aborted. A rejected handleError must not skip the diff cleanup: the unapproved
1382+
// streamed content has to be reverted and the diff view reset, or a user save
1383+
// could persist the failed write. The handleError rejection itself propagates
1384+
// (it is not swallowed by the cleanup).
1385+
mockHandleError.mockRejectedValue(new Error("handleError rejected (aborted task)"))
1386+
mockedCreateDirectoriesForFile.mockRejectedValue(
1387+
Object.assign(new Error("EACCES: permission denied, mkdir '/ro'"), { code: "EACCES" }),
1388+
)
1389+
1390+
await expect(executeWriteFileTool({}, { fileExists: false })).rejects.toThrow(
1391+
"handleError rejected (aborted task)",
1392+
)
1393+
1394+
// handleError was attempted with the write context...
1395+
expect(mockHandleError).toHaveBeenCalledWith("writing file", expect.any(Error))
1396+
// ...and the diff cleanup still ran despite its rejection: the unapproved
1397+
// content is reverted before the diff view reset.
1398+
expect(mockCline.diffViewProvider.revertChanges).toHaveBeenCalledTimes(1)
1399+
expect(mockCline.diffViewProvider.reset).toHaveBeenCalledTimes(1)
1400+
expect(mockCline.diffViewProvider.open).not.toHaveBeenCalled()
1401+
expect(mockCline.diffViewProvider.saveChanges).not.toHaveBeenCalled()
1402+
})
13671403
})
13681404

13691405
describe("prevent focus disruption experiment", () => {

0 commit comments

Comments
 (0)