Skip to content

Commit 218f0c8

Browse files
committed
fix(tools): finalize the empty-content write_to_file ask on a refused rollback
handlePartial finalized the partial ask for a refused streaming rollback only inside its `if (newContent)` guard. A stream that asked with an empty content string skipped it: the ask was already on screen, this return is the last thing the delta does, and execute() then exits through its own fail-closed refusal guard without touching that ask - so the spinner and the Save/Reject buttons stay live for a preview that will never be opened. Move the refusal check above the content guard. Non-empty content keeps the identical order and release: resetTaskPartialState drops only this task's stream entry and its abort listener, so the taskRollbackFailures marker survives and execute() still owns the refusal report. The added test seeds a refused rollback for a delta that asks with content "" and asserts the ask is finalized with that same message and that no preview is opened. With the check moved back inside the guard that test fails with `AssertionError: expected "vi.fn()" to be called with arguments: [ StringContaining "\"content\":\"\"" ]` (Number of calls: 0); with it outside the guard core/tools reports 736 passed | 5 skipped (741). No test was skipped and no suppression was added.
1 parent 64a10e3 commit 218f0c8

2 files changed

Lines changed: 40 additions & 10 deletions

File tree

‎src/core/tools/WriteToFileTool.ts‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -634,17 +634,20 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> {
634634
return
635635
}
636636

637+
// A refused rollback left the streamed document dirty: opening a preview here would save
638+
// that unapproved buffer before approval, so this delta stops and execute() owns the refusal
639+
// report. The check sits outside the content guard on purpose: a delta that streamed an
640+
// empty document still has its ask on screen, and that one has to be finalized too.
641+
if (this.taskRollbackFailures.has(this.getPartialStreamFailureKey(task))) {
642+
// The ask above is already on screen and this return is the last thing the delta does:
643+
// finalize it, or the spinner and Save/Reject buttons stay live for a preview that will
644+
// never be opened. execute() owns the refusal report.
645+
await this.finalizePartialToolAskAfterFailure(task, partialMessage)
646+
this.resetTaskPartialState(task)
647+
return
648+
}
649+
637650
if (newContent) {
638-
// A refused rollback left the streamed document dirty: opening it here would save
639-
// that unapproved buffer before approval. Skip the preview; execute() owns the refusal.
640-
if (this.taskRollbackFailures.has(this.getPartialStreamFailureKey(task))) {
641-
// The ask above is already on screen and this return is the last thing the delta
642-
// does: finalize it, or the spinner and Save/Reject buttons stay live for a preview
643-
// that will never be opened. execute() owns the refusal report.
644-
await this.finalizePartialToolAskAfterFailure(task, partialMessage)
645-
this.resetTaskPartialState(task)
646-
return
647-
}
648651
try {
649652
if (!task.diffViewProvider.isEditing) {
650653
await task.diffViewProvider.open(relPath!)

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ describe("WriteToFileTool partial-delta cancellation", () => {
134134
once: MockedFunction<(...args: unknown[]) => unknown>
135135
off: MockedFunction<(...args: unknown[]) => unknown>
136136
ask: MockedFunction<(type: string, text: string, partial?: boolean) => Promise<unknown>>
137+
finalizePartialToolAsk: MockedFunction<(text?: string) => Promise<void>>
137138
providerRef: { deref: () => { getState: () => Promise<Record<string, unknown>> } }
138139
diffViewProvider: {
139140
editType: "modify" | "create" | undefined
@@ -152,6 +153,8 @@ describe("WriteToFileTool partial-delta cancellation", () => {
152153
once: vi.fn(),
153154
off: vi.fn(),
154155
ask: vi.fn().mockResolvedValue(undefined),
156+
finalizePartialToolAsk: vi.fn().mockResolvedValue(undefined),
157+
155158
providerRef: { deref: () => ({ getState: async () => ({ experiments: {} }) }) },
156159
diffViewProvider: {
157160
editType: "create",
@@ -175,6 +178,30 @@ describe("WriteToFileTool partial-delta cancellation", () => {
175178
state.lastSeenPartialPath = "src/demo.ts"
176179
return state
177180
}
181+
// A refused rollback plus an empty streamed document reaches handlePartial with an ask
182+
// already on screen and no preview to open, which used to leave that ask partial forever.
183+
it("finalizes the empty-content partial ask when a rollback was refused", async () => {
184+
const task = buildStreamTask("refused-empty-content", "inst-c6")
185+
seedStablePath(task as unknown as Task)
186+
const failureKey = writeToFileTool["getPartialStreamFailureKey"](task as unknown as Task)
187+
writeToFileTool["taskRollbackFailures"].set(
188+
failureKey,
189+
new Error("EPERM: cannot restore the streamed document"),
190+
)
191+
const emptyContentBlock = {
192+
...partialBlock,
193+
params: { path: "src/demo.ts", content: "" },
194+
}
195+
196+
await writeToFileTool.handlePartial(task as unknown as Task, emptyContentBlock)
197+
198+
expect(task.ask).toHaveBeenCalledWith("tool", expect.stringContaining('"content":""'), true)
199+
expect(task.finalizePartialToolAsk).toHaveBeenCalledWith(expect.stringContaining('"content":""'))
200+
expect(task.diffViewProvider.open).not.toHaveBeenCalled()
201+
expect(task.diffViewProvider.update).not.toHaveBeenCalled()
202+
expect(writeToFileTool["taskPartialStreamState"].size).toBe(0)
203+
writeToFileTool["taskRollbackFailures"].delete(failureKey)
204+
})
178205

179206
it("does not register per-task state for a task that already stopped", async () => {
180207
const task = buildStreamTask("cancel-before-register", "inst-c1")

0 commit comments

Comments
 (0)