Skip to content

Commit e1d6b6c

Browse files
fix(write-to-file): release what a late open() completed after an abort
A cancellation that lands while open() is pending runs the TaskAborted teardown while the call is still in flight, so it can only release what existed at that instant. open() then finished: it created the directories and the empty placeholder and set isEditing + relPath on the provider. This stream's entry was already gone by then, so nothing owned those artifacts any more - they stayed on disk and on the session until a later write to the same path found a session that task never opened. The liveness bail now releases what the late open completed before returning, so the placeholder, the created directories, and the editing session a cancelled stream re-set are cleaned up by the code that observed the late completion.
1 parent 549899c commit e1d6b6c

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

‎src/core/tools/WriteToFileTool.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,13 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> {
802802
// Cancelled while open() was in flight: do not stream partial content into a
803803
// cancelled task's view.
804804
if (!this.isPartialStreamStillLive(task, partialStreamState)) {
805+
// The cancellation teardown ran while open() was still pending, so it could only
806+
// release what existed at that moment. open() then finished: it created the
807+
// directories and the empty placeholder and set isEditing + relPath on the
808+
// provider. This entry is already gone, so nothing else owns those artifacts -
809+
// they would sit there until a later write to the same path found a session this
810+
// task never opened. Release what the late open completed before returning.
811+
await this.cleanupFailedPartialStream(task)
805812
return
806813
}
807814

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,51 @@ describe("writeToFileTool", () => {
662662
expect(writeToFileTool["taskPartialStreamState"].size).toBe(0)
663663
})
664664

665+
it("releases what the late open() completed when the task aborts during it", async () => {
666+
// The cancellation teardown runs while open() is still pending, so it can only release
667+
// what existed at that instant. open() then finishes its side effects: it creates the
668+
// directories and the empty placeholder and sets isEditing + relPath on the provider.
669+
// This stream's entry is already gone by then, so nothing owns those artifacts any
670+
// more - a later write to the same path would find a session this task never opened.
671+
// The cleanup below is therefore the only one that can cover what open() completed.
672+
await executeWriteFileTool({}, { fileExists: false, isPartial: true })
673+
await executeWriteFileTool({}, { fileExists: false, isPartial: true })
674+
expect(writeToFileTool["taskPartialStreamState"].size).toBe(1)
675+
mockCline.diffViewProvider.isEditing = false
676+
mockCline.diffViewProvider.open.mockClear()
677+
mockCline.diffViewProvider.update.mockClear()
678+
mockCline.diffViewProvider.discardUnapprovedStream.mockReset().mockResolvedValue(undefined)
679+
mockCline.diffViewProvider.reset.mockClear()
680+
let releasesAtOpenReturn = -1
681+
let resetsAtOpenReturn = -1
682+
mockCline.diffViewProvider.open.mockImplementationOnce(async () => {
683+
// The cancellation lands mid-open, so the teardown releases this entry and rolls
684+
// back whatever the provider held at that instant. The timed flush lets every
685+
// microtask that release queued run before the counts are sampled, so the sample is
686+
// the whole of what the mid-open teardown did - anything after it is the release of
687+
// what open() itself completed.
688+
writeToFileTool.clearTaskState(mockCline)
689+
await new Promise((resolve) => {
690+
setTimeout(resolve, 0)
691+
})
692+
releasesAtOpenReturn = mockCline.diffViewProvider.discardUnapprovedStream.mock.calls.length
693+
resetsAtOpenReturn = mockCline.diffViewProvider.reset.mock.calls.length
694+
})
695+
696+
await executeWriteFileTool({}, { fileExists: false, isPartial: true })
697+
698+
// Nothing is streamed into the cancelled task's view.
699+
expect(mockCline.diffViewProvider.update).not.toHaveBeenCalled()
700+
expect(writeToFileTool["taskPartialStreamState"].size).toBe(0)
701+
// And the artifacts the late open() created are cleaned AFTER open() returned: both
702+
// counts grew past what the mid-open teardown left behind, which is what tells the two
703+
// releases apart.
704+
expect(mockCline.diffViewProvider.discardUnapprovedStream.mock.calls.length).toBeGreaterThan(
705+
releasesAtOpenReturn,
706+
)
707+
expect(mockCline.diffViewProvider.reset.mock.calls.length).toBeGreaterThan(resetsAtOpenReturn)
708+
})
709+
665710
it("releases the per-task stream state when provider state rejects during a partial delta", async () => {
666711
// provider.getState() looks like a read and is an await into the extension host. It runs
667712
// after this task's entry and its TaskAborted listener were registered, and a rejection

0 commit comments

Comments
 (0)