Skip to content

Commit 2c94a81

Browse files
fix(editor,write-to-file): report a refused tab close, and stop a delta that died mid-open
Two rows on PR 1930, one shape each. The abandoned-pre-open callback closes the plain tab it can prove it opened, and nothing awaited that close: a rejection landed in the extension host as an unhandled rejection instead of as a fact about a tab that is still standing. It is reported the way a refused close is reported everywhere else in the class. A partial delta that stops while its own open() is in flight used to fall straight into update() against the preview open() had just installed: content no delta will ever approve, in a view no delta is left to bring to approval. The cancellation guard the flow already asks at every other await boundary now runs here too, and the abandoned stream tears down the preview the same way the diff-view failure path does. Each leg has a case that fails without the source change: the cancellation case at the revert assertion, and the tab case at the log assertion. Reviewed rows: PRRT_kwDOSK-bwM60-lz_ (DiffViewProvider tab close) and PRRT_kwDOSK-bwM606auD (WriteToFileTool open/update boundary).
1 parent 6403f98 commit 2c94a81

4 files changed

Lines changed: 71 additions & 1 deletion

File tree

‎src/core/tools/WriteToFileTool.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,18 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> {
771771
try {
772772
if (!task.diffViewProvider.isEditing) {
773773
await task.diffViewProvider.open(relPath!)
774+
// open() is an await boundary of its own, and the preview it installs is shared state.
775+
// A task that stopped while open() was in flight has no delta left that can approve what
776+
// would be streamed into it, so update() here would write content nobody owns and leave
777+
// the preview dirty for a later save. Undo what open() brought up and release the entry,
778+
// the same teardown the catch below runs for a diff-view failure; both helpers report
779+
// their own failure, and an abandoned stream has no execute() left to report through.
780+
if (this.isStreamCancelled(task)) {
781+
await this.revertDiffChangesBeforeReset(task)
782+
await this.resetDiffViewAfterWrite(task)
783+
this.resetTaskPartialState(task)
784+
return
785+
}
774786
}
775787

776788
await task.diffViewProvider.update(

‎src/core/tools/__tests__/writeToFileTool-partial-state-cleanup.spec.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ describe("WriteToFileTool partial-delta cancellation", () => {
141141
isEditing: boolean
142142
open: MockedFunction<(relPath: string) => Promise<void>>
143143
update: MockedFunction<(content: string, single: boolean) => Promise<void>>
144+
revertChanges: MockedFunction<() => Promise<boolean>>
145+
reset: MockedFunction<() => Promise<void>>
144146
}
145147
}
146148

@@ -161,6 +163,8 @@ describe("WriteToFileTool partial-delta cancellation", () => {
161163
isEditing: true,
162164
open: vi.fn().mockResolvedValue(undefined),
163165
update: vi.fn().mockResolvedValue(undefined),
166+
revertChanges: vi.fn().mockResolvedValue(true),
167+
reset: vi.fn().mockResolvedValue(undefined),
164168
},
165169
})
166170

@@ -288,4 +292,26 @@ describe("WriteToFileTool partial-delta cancellation", () => {
288292
expect(task.diffViewProvider.update).not.toHaveBeenCalled()
289293
expect(writeToFileTool["taskPartialStreamState"].size).toBe(0)
290294
})
295+
296+
it("reverts and closes the preview when the task aborts while open() is in flight", async () => {
297+
const task = buildStreamTask("cancel-in-open", "inst-c5")
298+
seedStablePath(task as unknown as Task)
299+
// No preview yet: this is the delta that calls open() itself, and open() is the await
300+
// boundary that sits between it and update().
301+
task.diffViewProvider.isEditing = false
302+
task.diffViewProvider.open = vi.fn().mockImplementation(async () => {
303+
task.abort = true
304+
})
305+
306+
await writeToFileTool.handlePartial(task as unknown as Task, partialBlock)
307+
308+
expect(task.diffViewProvider.open).toHaveBeenCalledTimes(1)
309+
// The preview open() installed is the thing at risk: the delta that would have streamed
310+
// into it has no approval path left, so update() must not run against it, and the
311+
// preview must not stand there holding content nobody approved.
312+
expect(task.diffViewProvider.update).not.toHaveBeenCalled()
313+
expect(task.diffViewProvider.revertChanges).toHaveBeenCalledTimes(1)
314+
expect(task.diffViewProvider.reset).toHaveBeenCalledTimes(1)
315+
expect(writeToFileTool["taskPartialStreamState"].size).toBe(0)
316+
})
291317
})

‎src/integrations/editor/DiffViewProvider.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1410,7 +1410,12 @@ export class DiffViewProvider {
14101410
orphanedTab.isPinned &&
14111411
!orphanedTab.isDirty
14121412
) {
1413-
void vscode.window.tabGroups.close(orphanedTab)
1413+
// Nothing awaits this close - it is the last thing the callback does - so a rejection
1414+
// here would reach the host as an unhandled rejection instead of as a fact about
1415+
// this tab. Reported the way a refused close is reported everywhere else here.
1416+
void vscode.window.tabGroups.close(orphanedTab).then(undefined, (err: unknown) => {
1417+
console.error(`Failed to close file tab ${orphanedTab.label}`, err)
1418+
})
14141419
}
14151420
return
14161421
}

‎src/integrations/editor/__tests__/DiffViewProvider.spec.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3238,6 +3238,33 @@ describe("DiffViewProvider", () => {
32383238
expect(closedTabs()).toEqual([openedTab])
32393239
})
32403240

3241+
it("reports the failure when the tab a timed-out pre-open left behind refuses to close", async () => {
3242+
const openedTab = makeTab(makeTextTab(`${mockCwd}/mock-recovery-target.ts`), "mock-recovery-target.ts", {
3243+
isPinned: true,
3244+
})
3245+
const { abandon, closedTabs } = wireAbandonedPreopen(openedTab)
3246+
// The close is the last thing the abandoned-pre-open callback does and nothing awaits it, so
3247+
// a rejection has nowhere to go but the host. The tab is still standing afterwards either
3248+
// way, which is the fact a later reader of the log needs.
3249+
const attempts: unknown[] = []
3250+
vi.mocked(vscode.window.tabGroups.close).mockImplementation((tab) => {
3251+
attempts.push(tab)
3252+
return Promise.reject(new Error("close refused: tab held by another editor"))
3253+
})
3254+
const logged: unknown[][] = []
3255+
const errorSpy = vi.spyOn(console, "error").mockImplementation((...args: unknown[]) => {
3256+
logged.push(args)
3257+
})
3258+
try {
3259+
await abandon()
3260+
} finally {
3261+
errorSpy.mockRestore()
3262+
}
3263+
expect(attempts).toEqual([openedTab])
3264+
expect(closedTabs()).toEqual([])
3265+
expect(logged.some((args) => String(args[0]).includes("Failed to close file tab"))).toBe(true)
3266+
})
3267+
32413268
it("leaves a dirty tab alone when the late callback cannot tell whose typing it holds", async () => {
32423269
const openedTab = makeTab(makeTextTab(`${mockCwd}/mock-recovery-target.ts`), "mock-recovery-target.ts", {
32433270
isPinned: true,

0 commit comments

Comments
 (0)