Skip to content

Commit 8a5bf2d

Browse files
committed
fix(write_to_file): restore diff document on parse-failure teardown
Close a remaining gap in the parse-failure teardown boundary: streaming may open the diff view with unapproved partial content, and when the final block then fails to parse, execute() never runs, so its error cleanup (revert + reset) never fires -- leaving the unapproved content dirty where a user save could persist it. onParameterParseFailure() now runs the same revert-then-reset document restore (both helpers no-op when no view is open), regardless of whether a streaming error was captured. Adds a regression test for the successful-streaming + parse-failure scenario (document restored, generic parse error reported, per-task state torn down) and extends the streaming-failure parse test to assert the parse path runs the document cleanup in addition to the streaming-failure cleanup.
1 parent 9d75b10 commit 8a5bf2d

2 files changed

Lines changed: 74 additions & 3 deletions

File tree

‎src/core/tools/WriteToFileTool.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,28 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> {
156156
* Tears down the per-task stream state: otherwise the abort listener leaks for
157157
* the task's lifetime, and when a streaming delta had failed, the streamFailed
158158
* guard would suppress the diff preview of every later write_to_file in this
159-
* task. When a streaming delta already hit a fatal filesystem error, that error
160-
* is what the user can act on, so report it with the same "writing file"
161-
* context execute()'s catch uses, and suppress the incidental parse error.
159+
* task. Restores the diff document: streaming may have opened it with
160+
* unapproved partial content, and execute()'s error cleanup (revert + reset)
161+
* never fires on this path, so a user save could persist the content without
162+
* the teardown here. When a streaming delta already hit a fatal filesystem
163+
* error, that error is what the user can act on, so report it with the same
164+
* "writing file" context execute()'s catch uses, and suppress the incidental
165+
* parse error.
162166
*/
163167
override async onParameterParseFailure(task: Task, callbacks: ToolCallbacks, parseError: Error): Promise<boolean> {
164168
const state = this.taskPartialStreamState.get(this.getPartialStreamFailureKey(task))
165169
if (!state) {
166170
return false
167171
}
168172
this.resetTaskPartialState(task)
173+
// Streaming may have opened the diff view with unapproved partial content.
174+
// execute() never runs on this path, so its error cleanup (revert + reset)
175+
// never fires: restore the document here so a user save cannot persist
176+
// content the write never completed (the same invariant the denial and
177+
// streaming-failure paths maintain). Both helpers no-op when no view is
178+
// open.
179+
await this.revertDiffChangesBeforeReset(task)
180+
await this.resetDiffViewAfterWrite(task)
169181
if (!state.streamError) {
170182
return false
171183
}

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,12 @@ describe("writeToFileTool", () => {
758758
expect.stringContaining("parsing write_to_file"),
759759
expect.anything(),
760760
)
761+
// The parse-failure teardown also restores the diff document: the
762+
// streaming failure above already reverted it once (revert + reset),
763+
// and the parse path runs the same cleanup again because execute()
764+
// never runs on this path.
765+
expect(mockCline.diffViewProvider.revertChanges).toHaveBeenCalledTimes(2)
766+
expect(mockCline.diffViewProvider.reset).toHaveBeenCalledTimes(2)
761767
// Per-task state torn down at this boundary: guard cleared, the exact
762768
// registered abort listener detached.
763769
expect(writeToFileTool["taskPartialStreamState"].size).toBe(0)
@@ -774,6 +780,59 @@ describe("writeToFileTool", () => {
774780
consoleErrorSpy.mockRestore()
775781
}
776782
})
783+
784+
it("restores the diff document when the final block fails to parse after successful streaming", async () => {
785+
// Streaming opened the diff view with unapproved partial content (open and
786+
// update both succeeded, so no streaming error was captured). The final block
787+
// then arrives without nativeArgs: execute() never runs, so its error cleanup
788+
// never fires. The parse-failure teardown must still restore the document
789+
// (revert before reset) or a user save could persist content the write never
790+
// completed, and must report the generic parse error (no streaming error to
791+
// surface instead).
792+
let abortListener: (() => void) | undefined
793+
mockCline.once.mockImplementation((event: RooCodeEventName, listener: () => void) => {
794+
if (event === RooCodeEventName.TaskAborted && abortListener === undefined) {
795+
abortListener = listener
796+
}
797+
return mockCline
798+
})
799+
const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {})
800+
try {
801+
// Delta 1 - stabilize path; delta 2 - streams the partial content into the
802+
// diff view (open + update resolve).
803+
await executeWriteFileTool({}, { isPartial: true })
804+
await executeWriteFileTool({}, { isPartial: true })
805+
expect(mockCline.diffViewProvider.open).toHaveBeenCalledTimes(1)
806+
expect(mockCline.diffViewProvider.update).toHaveBeenCalledTimes(1)
807+
808+
const toolUse: ToolUse = {
809+
type: "tool_use",
810+
name: "write_to_file",
811+
params: { path: testFilePath, content: testContent },
812+
nativeArgs: undefined,
813+
partial: false,
814+
}
815+
await writeToFileTool.handle(mockCline, toolUse as ToolUse<"write_to_file">, {
816+
askApproval: mockAskApproval,
817+
handleError: mockHandleError,
818+
pushToolResult: vi.fn(),
819+
})
820+
821+
// No streaming error was captured: the generic parse error is reported.
822+
expect(mockHandleError).toHaveBeenCalledTimes(1)
823+
expect(mockHandleError).toHaveBeenCalledWith("parsing write_to_file args", expect.any(Error))
824+
// The diff document is restored by the parse path itself (no streaming
825+
// failure happened, so this is the only revert + reset in the test).
826+
expect(mockCline.diffViewProvider.revertChanges).toHaveBeenCalledTimes(1)
827+
expect(mockCline.diffViewProvider.reset).toHaveBeenCalledTimes(1)
828+
// Per-task state torn down: guard cleared, exact listener detached.
829+
expect(writeToFileTool["taskPartialStreamState"].size).toBe(0)
830+
expect(abortListener).toBeTypeOf("function")
831+
expect(mockCline.off).toHaveBeenCalledWith(RooCodeEventName.TaskAborted, abortListener)
832+
} finally {
833+
consoleErrorSpy.mockRestore()
834+
}
835+
})
777836
})
778837

779838
describe("path stabilization predicate", () => {

0 commit comments

Comments
 (0)