diff --git a/src/__tests__/removeClineFromStack-delegation.spec.ts b/src/__tests__/removeClineFromStack-delegation.spec.ts index 0db228662e..ddbb655b16 100644 --- a/src/__tests__/removeClineFromStack-delegation.spec.ts +++ b/src/__tests__/removeClineFromStack-delegation.spec.ts @@ -5,6 +5,7 @@ import { ClineProvider } from "../core/webview/ClineProvider" import { TaskRegistry } from "../core/task/TaskRegistry" import { PendingActionSettlementError, type Task } from "../core/task/Task" import { makeProviderStub } from "./helpers/provider-stub" +import { writeToFileTool } from "../core/tools/WriteToFileTool" type PrivateClineProviderMethods = { removeClineFromStack: (this: ClineProvider) => ReturnType @@ -202,6 +203,46 @@ describe("ClineProvider failed history restoration cleanup", () => { expect(task.dispose).toHaveBeenCalledOnce() }) + it("releases the tool's per-task state before a directly disposed task loses its listeners", async () => { + const key = "failed-history-task.inst-1" + const order: string[] = [] + const task = { + taskId: "failed-history-task", + instanceId: "inst-1", + emit: vi.fn(), + once: vi.fn(), + off: vi.fn(), + dispose: vi.fn().mockImplementation(() => { + // Dispose removes every listener, so this is the last moment the abort + // cleanup could still have run; the entry must already be gone here. + order.push(writeToFileTool["taskPartialStreamState"].has(key) ? "state-retained" : "state-cleared") + order.push("dispose") + return Promise.resolve() + }), + } as unknown as Task + // Fixture: the state entry the tool would have created during a partial stream. + writeToFileTool["taskPartialStreamState"].set(key, { + lastSeenPartialPath: undefined, + streamFailed: false, + streamError: undefined, + task, + abortCleanup: () => {}, + }) + const taskEventListeners = new Map([[task, [vi.fn()]]]) + const taskRegistry = new TaskRegistry() + taskRegistry.push(task) + const provider = { taskRegistry, taskEventListeners, log: vi.fn() } as unknown as ClineProvider + + await privateClineProvider.cleanupFailedHistoryTask.call( + provider, + task, + new PendingActionSettlementError("settlement failed"), + ) + + expect(order).toEqual(["state-cleared", "dispose"]) + expect(writeToFileTool["taskPartialStreamState"].size).toBe(0) + }) + it("keeps the task active after an unrelated history resume failure", async () => { const cleanupListener = vi.fn() const task = { diff --git a/src/core/assistant-message/__tests__/presentAssistantMessage-missing-native-args.spec.ts b/src/core/assistant-message/__tests__/presentAssistantMessage-missing-native-args.spec.ts new file mode 100644 index 0000000000..17478c3545 --- /dev/null +++ b/src/core/assistant-message/__tests__/presentAssistantMessage-missing-native-args.spec.ts @@ -0,0 +1,292 @@ +// npx vitest src/core/assistant-message/__tests__/presentAssistantMessage-missing-native-args.spec.ts + +import { describe, it, expect, beforeEach, vi } from "vitest" +import { presentAssistantMessage } from "../presentAssistantMessage" +import { DispatchState } from "../../task/dispatchState" +import { type RequestPolicySnapshot } from "../../prompts/tools/effective-tool-policy" +import { isValidToolName, validateToolUse } from "../../tools/validateToolUse" + +// Tool policy is frozen at request build and the presenter refuses to run without the +// snapshot; these tests only exercise the presenter's bypass exits, so the empty policy is +// enough (validateToolUse itself is mocked). +const baseSnapshot: RequestPolicySnapshot = { + disabledTools: [], + customModes: [], +} + +const mockTeardown = vi.hoisted(() => vi.fn()) +const mockWriteHandle = vi.hoisted(() => vi.fn()) + +vi.mock("../../task/Task") +vi.mock("../../tools/validateToolUse", () => ({ + validateToolUse: vi.fn(), + isValidToolName: vi.fn(() => true), +})) +vi.mock("../../tools/WriteToFileTool", () => ({ + writeToFileTool: { handle: mockWriteHandle, teardownAbandonedStream: mockTeardown }, +})) +vi.mock("@roo-code/telemetry", () => ({ + TelemetryService: { + instance: { + captureToolUsage: vi.fn(), + captureConsecutiveMistakeError: vi.fn(), + captureException: vi.fn(), + }, + }, +})) + +type ToolResultBlock = { type: string; tool_use_id: string; content: string; is_error: boolean } + +describe("presentAssistantMessage - finalized block without nativeArgs", () => { + // The presenter reads only a subset of Task. A Record keeps the double honest without + // an `any`; the single cast at the call site is the documented boundary. + let mockTask: Record + let userMessageContent: ToolResultBlock[] + + beforeEach(() => { + mockTeardown.mockReset() + mockWriteHandle.mockReset() + userMessageContent = [] + mockTask = { + taskId: "test-task-id", + instanceId: "test-instance", + abort: false, + // main renamed the dispatcher fields (src/core/task/dispatchState.ts): the presenter reads + // dispatchState and pendingWork, not the old lock/flag pair. + dispatchState: DispatchState.IDLE, + pendingWork: null, + currentStreamingContentIndex: 0, + assistantMessageContent: [ + { + type: "tool_use", + name: "write_to_file", + params: {}, + partial: false, + // Streaming JSON never parsed: Task completes the block with no nativeArgs. + id: "toolu_1", + nativeArgs: undefined, + }, + ], + userMessageContent, + didCompleteReadingStream: false, + // The presenter keeps per-tool-call context in this map (added on main since this + // spec was written); the bypass exits below run after its first use. + activeToolContexts: new Map(), + didRejectTool: false, + didAlreadyUseTool: false, + consecutiveMistakeCount: 0, + consecutiveMistakeLimit: 3, + apiConfiguration: { apiProvider: "test-provider" }, + clineMessages: [], + getTaskMode: vi.fn().mockResolvedValue("code"), + api: { getModel: () => ({ id: "test-model", info: {} }) }, + recordToolUsage: vi.fn(), + recordToolError: vi.fn(), + toolRepetitionDetector: { check: vi.fn().mockReturnValue({ allowExecution: true }) }, + providerRef: { + deref: () => ({ getState: vi.fn().mockResolvedValue({ mode: "code", customModes: [] }) }), + }, + say: vi.fn().mockResolvedValue(undefined), + ask: vi.fn().mockResolvedValue({ response: "yesButtonClicked" }), + pushToolResultToUserContent: vi.fn((toolResult: ToolResultBlock) => { + userMessageContent.push(toolResult) + return true + }), + } + }) + + it("tears the write_to_file stream state down instead of dispatching the tool", async () => { + await presentAssistantMessage( + mockTask as unknown as Parameters[0], + baseSnapshot, + ) + + // The malformed call must not be executed, and exactly one error tool_result is + // emitted for the provider. + expect(isValidToolName).toHaveBeenCalled() + expect(mockWriteHandle).not.toHaveBeenCalled() + expect(mockTask.pushToolResultToUserContent).toHaveBeenCalledTimes(1) + expect(userMessageContent).toEqual([ + expect.objectContaining({ + type: "tool_result", + tool_use_id: "toolu_1", + is_error: true, + content: expect.stringContaining("missing nativeArgs"), + }), + ]) + // The guard bypasses handle(), so the per-task stream state has to be released here: + // otherwise the entry, its TaskAborted listener and any streamed diff view leak into + // the next API request of the same task. + expect(mockTeardown).toHaveBeenCalledTimes(1) + expect(mockTeardown).toHaveBeenCalledWith(mockTask) + }) + + it("tears the stream state down when tool validation rejects the call", async () => { + // A mode file restriction (or any validateToolUse failure) lands here. The block + // streamed partial deltas - handlePartial ran - and this exit bypasses handle(), so + // without the teardown the per-task stream state, its TaskAborted listener and any + // streamed diff view leak into the next API request. + mockTask.assistantMessageContent = [ + { + type: "tool_use", + name: "write_to_file", + params: { path: "restricted.ts", content: "partial model output" }, + partial: false, + id: "toolu_2", + nativeArgs: { path: "restricted.ts", content: "partial model output" }, + }, + ] + vi.mocked(validateToolUse).mockImplementationOnce(() => { + throw new Error("write_to_file is not allowed to write restricted.ts in this mode") + }) + + await presentAssistantMessage( + mockTask as unknown as Parameters[0], + baseSnapshot, + ) + + expect(mockWriteHandle).not.toHaveBeenCalled() + expect(userMessageContent).toEqual([ + expect.objectContaining({ + type: "tool_result", + tool_use_id: "toolu_2", + is_error: true, + content: expect.stringContaining("restricted.ts"), + }), + ]) + expect(mockTeardown).toHaveBeenCalledTimes(1) + expect(mockTeardown).toHaveBeenCalledWith(mockTask) + }) + + it("tears the stream state down when the tool repetition limit stops the call", async () => { + mockTask.assistantMessageContent = [ + { + type: "tool_use", + name: "write_to_file", + params: { path: "same.ts", content: "same content" }, + partial: false, + id: "toolu_3", + nativeArgs: { path: "same.ts", content: "same content" }, + }, + ] + // The detector's result is a tagged union now (allow / soft_block / hard_block): only the + // hard block asks the user and stops the call, so that is the exit under test. + vi.mocked(mockTask.toolRepetitionDetector as unknown as { check: unknown }).check = vi.fn().mockReturnValue({ + action: "hard_block", + askUser: { messageKey: "tool_repetition", messageDetail: "write_to_file" }, + }) + + await presentAssistantMessage( + mockTask as unknown as Parameters[0], + baseSnapshot, + ) + + expect(mockWriteHandle).not.toHaveBeenCalled() + // The repetition exit reports through the local pushToolResult, which wraps the + // message in the tool-error envelope rather than the provider is_error flag. + expect(userMessageContent).toEqual([ + expect.objectContaining({ + type: "tool_result", + tool_use_id: "toolu_3", + content: expect.stringContaining("repetition limit reached"), + }), + ]) + expect(mockTeardown).toHaveBeenCalledTimes(1) + expect(mockTeardown).toHaveBeenCalledWith(mockTask) + }) + + it("leaves the stream state alone at the missing-nativeArgs exit for a tool that opened no diff view", async () => { + // The release at this bypass is scoped to the one tool whose streamed diff view a skipped + // handle() would strand. Applying it to every block name would ask other tools to release + // state they never opened, so the guard has to read the name rather than the shape alone. + mockTask.assistantMessageContent = [ + { + type: "tool_use", + name: "execute_command", + params: { cmd: "ls" }, + partial: false, + id: "toolu_4", + nativeArgs: undefined, + }, + ] + + await presentAssistantMessage( + mockTask as unknown as Parameters[0], + baseSnapshot, + ) + + expect(mockWriteHandle).not.toHaveBeenCalled() + expect(userMessageContent).toEqual([ + expect.objectContaining({ type: "tool_result", tool_use_id: "toolu_4", is_error: true }), + ]) + expect(mockTeardown).not.toHaveBeenCalled() + }) + + it("leaves the stream state alone at the validation exit for a tool that opened no diff view", async () => { + // A refusal lands here whatever the tool. Only write_to_file has a streamed diff view at + // this point, so the release stays scoped to it. + mockTask.assistantMessageContent = [ + { + type: "tool_use", + name: "execute_command", + params: { cmd: "ls" }, + partial: false, + id: "toolu_5", + nativeArgs: { cmd: "ls" }, + }, + ] + vi.mocked(validateToolUse).mockImplementationOnce(() => { + throw new Error("execute_command is not allowed in this mode") + }) + + await presentAssistantMessage( + mockTask as unknown as Parameters[0], + baseSnapshot, + ) + + expect(mockWriteHandle).not.toHaveBeenCalled() + expect(userMessageContent).toEqual([ + expect.objectContaining({ + type: "tool_result", + tool_use_id: "toolu_5", + is_error: true, + content: expect.stringContaining("not allowed"), + }), + ]) + expect(mockTeardown).not.toHaveBeenCalled() + }) + + it("leaves the stream state alone at the repetition exit for a tool that opened no diff view", async () => { + // The repetition stop bypasses handle() for any tool, and the release is still only owed by + // the one that streams a diff view. + mockTask.assistantMessageContent = [ + { + type: "tool_use", + name: "execute_command", + params: { cmd: "ls" }, + partial: false, + id: "toolu_6", + nativeArgs: { cmd: "ls" }, + }, + ] + vi.mocked(mockTask.toolRepetitionDetector as unknown as { check: unknown }).check = vi.fn().mockReturnValue({ + action: "hard_block", + askUser: { messageKey: "tool_repetition", messageDetail: "execute_command" }, + }) + + await presentAssistantMessage( + mockTask as unknown as Parameters[0], + baseSnapshot, + ) + + expect(mockWriteHandle).not.toHaveBeenCalled() + expect(userMessageContent).toEqual([ + expect.objectContaining({ + type: "tool_result", + tool_use_id: "toolu_6", + content: expect.stringContaining("repetition limit reached"), + }), + ]) + expect(mockTeardown).not.toHaveBeenCalled() + }) +}) diff --git a/src/core/assistant-message/presentAssistantMessage.ts b/src/core/assistant-message/presentAssistantMessage.ts index 73d48b2e7c..1bd135bebd 100644 --- a/src/core/assistant-message/presentAssistantMessage.ts +++ b/src/core/assistant-message/presentAssistantMessage.ts @@ -586,6 +586,14 @@ async function presentAssistantMessageBlock(cline: Task, policySnapshot: Request is_error: true, }) + // A write_to_file call that streamed partial deltas leaves per-task stream state + // (and possibly an open diff view) behind, and this guard bypasses handle(), so + // the teardown that execute()/onParameterParseFailure would have run never does. + // Release it here instead of leaking it into the next API request. + if (block.name === "write_to_file") { + await writeToFileTool.teardownAbandonedStream(cline) + } + break } } @@ -797,6 +805,14 @@ async function presentAssistantMessageBlock(cline: Task, policySnapshot: Request error.message, ) + // Same leak as the missing-nativeArgs guard: the block streamed partial + // deltas (handlePartial ran), this exit bypasses handle()/execute(), so the + // per-task stream state and any open diff view must be released here. A + // mode file restriction blocking the target path lands exactly here. + if (block.name === "write_to_file") { + await writeToFileTool.teardownAbandonedStream(cline) + } + break } @@ -873,6 +889,12 @@ async function presentAssistantMessageBlock(cline: Task, policySnapshot: Request `Tool call repetition limit reached for ${block.name}. Please try a different approach.`, ), ) + + // The repetition exit also bypasses handle(): release the stream state and + // diff view the partial deltas created, or they leak into the next request. + if (block.name === "write_to_file") { + await writeToFileTool.teardownAbandonedStream(cline) + } break } } diff --git a/src/core/task/Task.ts b/src/core/task/Task.ts index 77a1333b39..ff1bf9bbfc 100644 --- a/src/core/task/Task.ts +++ b/src/core/task/Task.ts @@ -105,6 +105,7 @@ import { buildNativeToolsArrayWithRestrictions } from "./build-tools" // core modules import { ToolRepetitionDetector } from "../tools/ToolRepetitionDetector" import { restoreTodoListForTask } from "../tools/UpdateTodoListTool" +import { writeToFileTool } from "../tools/WriteToFileTool" import { FileContextTracker } from "../context-tracking/FileContextTracker" import { ObservationRegistry } from "./observationRegistry" import { RooIgnoreController } from "../ignore/RooIgnoreController" @@ -394,6 +395,25 @@ export class Task extends EventEmitter implements TaskLike { * @private */ private taskApiConfigReady: Promise + /** + * Whether taskApiConfigReady has settled. The dispose-time metadata retry needs it only while + * _taskApiConfigName is still undefined: persistTaskMetadata() awaits the promise in that case, + * and teardown must not block on it. A legacy history item legitimately has no stored api + * config while its initialization completed, and that task still needs the retry; once the + * name IS known, persistTaskMetadata() skips the await, so an unsettled promise must not + * suppress the repair either. + */ + private taskApiConfigReadySettled = false + + /** + * Set when the derived metadata / task-history stage of a save failed while the message + * write itself succeeded. Drives one awaited retry in disposeOnce() so a task being torn + * down does not leave its history entry stale; cleared on the next successful metadata + * stage. + * + * @private + */ + private pendingTaskMetadataRepair: boolean = false providerRef: WeakRef private readonly globalStoragePath: string @@ -510,7 +530,14 @@ export class Task extends EventEmitter implements TaskLike { private telemetryMessageCountsBaseline: { user: number; assistant: number } = { user: 0, assistant: 0 } private abortPromise?: Promise private disposalPromise?: Promise - private diffReversionPromise: Promise = Promise.resolve() + // Undefined until a diff view with unapproved content starts being released. The promise is + // the single owner of that release: disposal and the stream abort teardown both await it + // instead of starting a second rollback over the same buffer (see releaseUnapprovedDiffView). + private diffReversionPromise: Promise | undefined + // Set when the release this task owns finished with a failure. The promise stays resolved on purpose: + // abortTaskOnce() documents that abort must always succeed and disposeOnce() awaits it unguarded, so + // a failed release is recorded here and surfaced at the abort site instead of rejected to every waiter. + private diffReversionFailed = false // Checkpoints enableCheckpoints: boolean @@ -805,12 +832,16 @@ export class Task extends EventEmitter implements TaskLike { this._taskApiConfigName = handoffExecutionContext.apiConfigName this.taskModeReady = Promise.resolve() this.taskApiConfigReady = Promise.resolve() + this.taskApiConfigReadySettled = true TelemetryService.instance.captureTaskCreated(this.taskId) } else if (historyItem) { this._taskMode = historyItem.mode || defaultModeSlug this._taskApiConfigName = historyItem.apiConfigName this.taskModeReady = Promise.resolve() this.taskApiConfigReady = Promise.resolve() + // historyItem.apiConfigName is undefined for tasks saved before the api config was + // recorded. That is a completed initialization, not a pending one. + this.taskApiConfigReadySettled = true TelemetryService.instance.captureTaskRestarted(this.taskId) } else { // For new tasks, don't set the mode/apiConfigName yet - wait for async initialization. @@ -818,6 +849,16 @@ export class Task extends EventEmitter implements TaskLike { this._taskApiConfigName = undefined this.taskModeReady = this.initializeTaskMode(provider) this.taskApiConfigReady = this.initializeTaskApiConfigName(provider) + // Observe settlement without replacing the promise: a rejection must still reach + // every other awaiter of taskApiConfigReady. + void this.taskApiConfigReady.then( + () => { + this.taskApiConfigReadySettled = true + }, + () => { + this.taskApiConfigReadySettled = true + }, + ) TelemetryService.instance.captureTaskCreated(this.taskId) } @@ -1846,11 +1887,13 @@ export class Task extends EventEmitter implements TaskLike { /** * Persist the message array, then refresh the derived metadata / task-history entries. * - * The returned boolean reflects the message write only: `saveTaskMessages` failure - * leaves the on-disk record stale, so callers gating UI updates on durable state must - * skip them. Metadata / task-history stage failures are logged and swallowed — the - * message array is already persisted, and the next save recomputes and re-emits the - * metadata. + * The two stages report separately on purpose. The returned boolean reflects the MESSAGE + * WRITE: `saveTaskMessages` failure leaves the on-disk record stale, so callers gating + * UI updates on durable state must skip them. A metadata / task-history failure does NOT + * turn that result false — the message array really is persisted, and reporting it as + * failed would make callers skip a webview update that is safe. It is not silently + * dropped either: `persistTaskMetadata()` records the failure and `disposeOnce()` retries + * it once, awaited, before the task stops. * * `merge` (default `true`) is passed through to `saveTaskMessages`: the in-memory * snapshot is merged with the on-disk record. `overwriteClineMessages` passes `false` @@ -1869,6 +1912,20 @@ export class Task extends EventEmitter implements TaskLike { return false } + await this.persistTaskMetadata() + + return true + } + + /** + * Recompute the derived task metadata and write the task-history entry for messages that + * are already on disk. Returns whether THIS stage succeeded — deliberately separate from + * `saveClineMessages()` so a metadata failure is never reported as a failed message write. + * + * A failure is recorded in `pendingTaskMetadataRepair` so `disposeOnce()` gets one awaited + * retry before shutdown; any later `saveClineMessages()` also recomputes it from scratch. + */ + private async persistTaskMetadata(): Promise { try { if (this._taskApiConfigName === undefined) { await this.taskApiConfigReady @@ -1895,16 +1952,27 @@ export class Task extends EventEmitter implements TaskLike { this.debouncedEmitTokenUsage(tokenUsage, this.toolUsage) const provider = this.providerRef.deref() - const existingStatus = provider?.taskHistoryStore.get(this.taskId)?.status - await provider?.updateTaskHistory(existingStatus ? { ...historyItem, status: existingStatus } : historyItem) + // A released provider makes the two calls below optional-chain no-ops, so without this + // guard the stage reports success while persisting nothing and clears the repair flag - + // the history entry then stays behind the messages that ARE on disk and nothing retries + // it. Treat it as the failed stage it is and leave the flag set for disposeOnce(). + if (!provider) { + this.pendingTaskMetadataRepair = true + console.error("Failed to save task metadata: the task has no provider to persist task history.") + return false + } + const existingStatus = provider.taskHistoryStore.get(this.taskId)?.status + await provider.updateTaskHistory(existingStatus ? { ...historyItem, status: existingStatus } : historyItem) + this.pendingTaskMetadataRepair = false + return true } catch (error) { - // The message array was persisted above; a metadata or task-history failure must - // not mask that write (see the method docs). The next saveClineMessages() call - // recomputes and re-emits the metadata update. + // The message array is already durable; a metadata or task-history failure must not + // mask that write (see saveClineMessages). Record it so the retry in disposeOnce() + // runs even if no further save happens before the task is torn down. + this.pendingTaskMetadataRepair = true console.error("Failed to save task metadata:", error) + return false } - - return true } private findMessageByTimestamp(ts: number): ClineMessage | undefined { @@ -3687,6 +3755,20 @@ export class Task extends EventEmitter implements TaskLike { // Reversion affects the user's workspace and must finish before the // final task state is saved. Artifact deletion is drained separately. await this.diffReversionPromise + if (this.diffReversionFailed) { + // The release this abort waited for failed. Report it the way a failed rollback is reported + // elsewhere - through the error say path - instead of letting the abort complete as if the + // workspace had been restored. + await this.say( + "error", + "The diff view could not be released after the stream was aborted, so the editor may still show content this task never approved.", + ).catch((sayError) => { + console.error( + `Error reporting the failed diff release for ${this.taskId}.${this.instanceId}:`, + sayError, + ) + }) + } } catch (error) { console.error(`Error during task ${this.taskId}.${this.instanceId} disposal:`, error) // Don't rethrow - we want abort to always succeed @@ -3771,6 +3853,12 @@ export class Task extends EventEmitter implements TaskLike { // Remove all event listeners to prevent memory leaks. try { + // The per-task write_to_file stream state lives in a tool singleton and is + // normally released by the TaskAborted listener registered with it. removeAllListeners + // above drops that listener, so a task disposed directly - without an abort - would + // leave the singleton holding this task, its provider, and a stream that can never + // advance again. Release it here, where every disposal path passes. + writeToFileTool.clearTaskState(this) this.removeAllListeners() } catch (error) { console.error("Error removing event listeners:", error) @@ -3818,18 +3906,88 @@ export class Task extends EventEmitter implements TaskLike { } try { - // If we're not streaming then `abortStream` won't be called. + // If we're not streaming then `abortStream` won't be called; when it already ran, it + // started the one release this task may perform and isEditing is already false. if (this.isStreaming && this.diffViewProvider.isEditing) { - this.diffReversionPromise = this.diffViewProvider.revertChanges().catch(console.error) + void this.releaseUnapprovedDiffView() } } catch (error) { console.error("Error reverting diff changes:", error) } + // A save whose metadata / task-history stage failed leaves the history entry + // behind the messages that are already on disk. Give that stage one awaited chance to + // catch up before the task stops serving. Deliberately last: everything synchronous above + // has to happen before the first yield, because abortTaskOnce() awaits diffReversionPromise + // right after void dispose(), an abandoned stream can reach its finally while this await is + // in flight and leave the revert decision below false, and direct dispose() callers rely on + // the abort flag being set synchronously. Skipped only when the retry could block: while + // taskApiConfigReady has not settled AND _taskApiConfigName is still undefined, + // persistTaskMetadata() awaits that promise and teardown must not wait on it. Once the name + // is known the await is skipped inside persistTaskMetadata(), so the repair is safe to run + // and skipping it would leave the history entry stale for a task that could repair it. + if ( + this.pendingTaskMetadataRepair && + (this.taskApiConfigReadySettled || this._taskApiConfigName !== undefined) + ) { + await this.persistTaskMetadata() + } + await pendingCleanup await this.diffReversionPromise } + /** + * Release a diff view whose content this task never approved. + * + * Both disposal and the stream abort teardown reach an abandoned diff view, and each used + * to start its own rollback. That left two defects: abortStream() reverted unconditionally, + * and revertChanges()'s create branch SAVES the dirty buffer - the partial model output + * nobody approved - before deleting the file, so a failed delete left it on disk; and a + * release one path had already started could be repeated by the other over the same view. + * + * The promise is the single owner of the release. It is assigned synchronously, before any + * await, because abortTaskOnce() and waitForDiffReversion() await it immediately after the + * trigger; a later caller gets that same promise instead of a second rollback. + * + * The edit type selects the mechanism. A modify's restore is the content already on disk, + * so revertChanges() puts the editor back where the stream started. A create has no prior + * content: discardUnapprovedStream() empties the buffer first, so the only bytes that can + * ever reach the placeholder are none. + * + * Cleared at the start of the next API request, because each request's diff view is a new + * session that has to be able to release itself. + */ + private releaseUnapprovedDiffView(): Promise { + if (this.diffReversionPromise) { + return this.diffReversionPromise + } + if (!this.diffViewProvider.isEditing) { + return Promise.resolve() + } + const releaseUnapprovedEdit = + this.diffViewProvider.editType === "modify" + ? this.diffViewProvider.revertChanges() + : this.diffViewProvider.discardUnapprovedStream() + this.diffReversionPromise = releaseUnapprovedEdit.catch((error) => { + // A failed release must not reject the waiters (see diffReversionFailed): record it so the site + // that reports the outcome can say the rollback did not happen instead of reporting it done. + this.diffReversionFailed = true + console.error(error) + }) + return this.diffReversionPromise + } + + /** + * The diff teardown this task started, for a continuation that observes its own + * stream released mid-await. Single-owner by construction: the in-flight + * handlePartial() waits for the discard disposal or the abort teardown already started + * instead of running an independent one over the same buffer. + */ + public async waitForDiffReversion(): Promise { + await this.diffReversionPromise + } + // Subtasks // Spawn / Wait / Complete @@ -4188,9 +4346,12 @@ export class Task extends EventEmitter implements TaskLike { } const abortStream = async (cancelReason: ClineApiReqCancelReason, streamingFailedMessage?: string) => { - if (this.diffViewProvider.isEditing) { - await this.diffViewProvider.revertChanges() // closes diff view - } + // A cancelled or failed stream never reached approval, so its diff view holds + // content the user never approved. Same release as disposal: the edit type + // decides the mechanism (a create must be discarded, never reverted - that + // SAVES the dirty buffer), and a release disposal already started is awaited + // instead of rolled back a second time over the same view. + await this.releaseUnapprovedDiffView() // if last message is a partial we need to update and save it const lastMessage = this.clineMessages.at(-1) @@ -4239,6 +4400,13 @@ export class Task extends EventEmitter implements TaskLike { const nativeToolCallParserScope = NativeToolCallParser.createScope() await this.diffViewProvider.reset() + // A new request opens a new diff-view session: the previous stream's release (if it + // had one) is over, and this session's abandoned view has to be able to release + // itself instead of awaiting the settled promise of the one before. + this.diffReversionPromise = undefined + // The failure record belongs to the same release session: the next request has to be able to report + // its own failure rather than inherit this one. + this.diffReversionFailed = false await this.safeEnsureModelFetched() diff --git a/src/core/task/__tests__/Task.dispose.test.ts b/src/core/task/__tests__/Task.dispose.test.ts index 7a83f16b8d..4c2e547fc2 100644 --- a/src/core/task/__tests__/Task.dispose.test.ts +++ b/src/core/task/__tests__/Task.dispose.test.ts @@ -1,3 +1,4 @@ +import { writeToFileTool } from "../../tools/WriteToFileTool" import path from "node:path" import { type ProviderSettings, RooCodeEventName } from "@roo-code/types" @@ -246,8 +247,11 @@ describe("Task dispose method", () => { resolveCleanup = resolve }), ) + // A modify: disposal reverts it. Disposal discards a create instead (see the + // create/modify pair in Task.spec.ts), and these tests are about the reversion promise. task.isStreaming = true task.diffViewProvider.isEditing = true + task.diffViewProvider.editType = "modify" const revertChangesSpy = vi.spyOn(task.diffViewProvider, "revertChanges").mockReturnValue( new Promise((resolve) => { resolveReversion = resolve @@ -281,8 +285,11 @@ describe("Task dispose method", () => { const reversion = new Promise((resolve) => { resolveReversion = resolve }) + // A modify: disposal reverts it. Disposal discards a create instead (see the + // create/modify pair in Task.spec.ts), and these tests are about the reversion promise. task.isStreaming = true task.diffViewProvider.isEditing = true + task.diffViewProvider.editType = "modify" const revertChangesSpy = vi.spyOn(task.diffViewProvider, "revertChanges").mockReturnValue(reversion) const disposal = task.dispose() @@ -303,8 +310,11 @@ describe("Task dispose method", () => { test("should log rejected diff reversion and continue final abort persistence", async () => { const reversionError = new Error("reversion failed") const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + // A modify: disposal reverts it. Disposal discards a create instead (see the + // create/modify pair in Task.spec.ts), and these tests are about the reversion promise. task.isStreaming = true task.diffViewProvider.isEditing = true + task.diffViewProvider.editType = "modify" vi.spyOn(task.diffViewProvider, "revertChanges").mockRejectedValue(reversionError) const saveMessages = vi.fn().mockResolvedValue(true) Object.defineProperty(task, "saveClineMessages", { value: saveMessages }) @@ -513,4 +523,32 @@ describe("Task.run() idempotency", () => { await t.dispose() startTaskSpy.mockRestore() }) + + test("dispose() releases the write_to_file stream state this task registered", async () => { + // The per-task stream state lives in a tool singleton and is normally released by the + // TaskAborted listener registered with it. dispose() removes every listener, so a task + // disposed directly - the path ClineProvider.cleanupFailedHistoryTask() takes - would + // leave the singleton holding this task, its provider, and a stream that can never + // advance again. + // Spied before construction: a constructor that fails can start the disposal itself, + // and that path has to release the state too. The release is captured in a local + // rather than read back off the spy, because mockRestore() clears the spy's call + // history and the assertion below runs after it. + const released: unknown[] = [] + const clearTaskState = vi.spyOn(writeToFileTool, "clearTaskState").mockImplementation((disposed) => { + released.push(disposed) + }) + const t = new Task({ + provider: mockProvider as unknown as ClineProvider, + apiConfiguration: mockApiConfiguration, + task: "hello", + startTask: false, + }) + try { + await t.dispose() + } finally { + clearTaskState.mockRestore() + } + expect(released).toEqual([t]) + }) }) diff --git a/src/core/task/__tests__/Task.spec.ts b/src/core/task/__tests__/Task.spec.ts index 765dd1b2b6..702464eee9 100644 --- a/src/core/task/__tests__/Task.spec.ts +++ b/src/core/task/__tests__/Task.spec.ts @@ -1560,6 +1560,79 @@ describe("Cline", () => { }, ) + it("marks the api-config wait settled for a delegated child handed an explicit context", async () => { + // The dispose-time metadata repair keys off this flag to decide whether the api config is + // still being worked out. A child that arrives with its profile named has nothing pending, + // so the synchronous branch has to settle the wait itself; leaving it pending would make + // every delegated child read as an unfinished initialization for its whole life. + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + task: "delegated child", + startTask: false, + handoffExecutionContext: { + mode: "ask", + apiConfigName: "parent-local-profile", + apiConfiguration: mockApiConfig, + }, + }) + + // Bracket access for the private field: this branch settles it synchronously, and the + // profile it hands the task is the one the context carried. + expect(task["taskApiConfigReadySettled"]).toBe(true) + await expect(task.getTaskApiConfigName()).resolves.toBe("parent-local-profile") + }) + + it("marks the api-config wait settled for a task reopened from a history entry", async () => { + // A reopened task carries whatever api config name was saved with it - including none, + // for entries written before the name was recorded - and its initialization is complete + // the moment the constructor returns. The dispose-time repair reads this flag, so a + // reopened task must not spend its life reading as a pending initialization. + const historyItem = { + id: "settled-history", + number: 2, + task: "history", + ts: Date.now(), + tokensIn: 0, + tokensOut: 0, + totalCost: 0, + } + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + historyItem, + startTask: false, + }) + + expect(task["taskApiConfigReadySettled"]).toBe(true) + }) + + it("settles the api-config wait when the initialization rejects, and keeps the rejection reachable", async () => { + // The constructor observes settlement on a side channel rather than replacing the promise, + // so a rejection has to settle the flag too: otherwise a task whose lookup failed stays + // marked as still initializing forever, and the dispose-time repair keeps waiting on a + // promise that already failed. The rejection itself must still reach every other awaiter, + // which is why the observation is a then(f, r) beside the promise rather than a catch on it. + // The prototype is reached through unknown rather than any: only the private member this + // row replaces, and the swap is restored below. + const proto = Task.prototype as unknown as { + initializeTaskApiConfigName: () => Promise + } + const initSpy = vi + .spyOn(proto, "initializeTaskApiConfigName") + .mockRejectedValueOnce(new Error("api config lookup refused")) + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + task: "failed init", + startTask: false, + }) + + await expect(task["taskApiConfigReady"]).rejects.toThrow("api config lookup refused") + + expect(task["taskApiConfigReadySettled"]).toBe(true) + initSpy.mockRestore() + }) it("keeps history-task initialization distinct from delegated-child initialization", async () => { const captureTaskRestarted = vi.spyOn(TelemetryService.instance, "captureTaskRestarted") const historyItem = { diff --git a/src/core/task/__tests__/Task.teardown.spec.ts b/src/core/task/__tests__/Task.teardown.spec.ts new file mode 100644 index 0000000000..0522ca3165 --- /dev/null +++ b/src/core/task/__tests__/Task.teardown.spec.ts @@ -0,0 +1,1174 @@ +// npx vitest core/task/__tests__/Task.teardown.spec.ts +// Setup prelude, the Cline-provider beforeEach and the console-error spies are copied verbatim +// from Task.spec.ts; the 13 tests below are byte-identical to the 430 lines this unit used to +// insert into Task.spec.ts, so Task.spec.ts returns to main's own content. + +import { WebviewFocusTracker } from "../../webview/WebviewFocusTracker" +// npx vitest core/task/__tests__/Task.spec.ts + +import * as fsReal from "fs" +import * as os from "os" +import * as path from "path" + +import * as vscode from "vscode" +import { Anthropic } from "@anthropic-ai/sdk" + +import { + providerIdentifiers, + type ClineMessage, + type GlobalState, + type ProviderSettings, + type ModelInfo, +} from "@roo-code/types" +import { TelemetryService } from "@roo-code/telemetry" + +import { Task } from "../Task" +import { ClineProvider } from "../../webview/ClineProvider" +import { ApiStreamChunk } from "../../../api/transform/stream" +import { ContextProxy } from "../../config/ContextProxy" +import type { ApiMessage } from "../../task-persistence" +import type { RequestPolicySnapshot } from "../../prompts/tools/effective-tool-policy" +import { asyncStreamFrom } from "../../../test-utils/stream" + +type TaskTestAccess = { + getSystemPrompt: (requestState: ProviderState | undefined, requestModelInfo?: ModelInfo) => Promise + handleContextWindowExceededError: (requestModelInfo: ModelInfo) => Promise + getEnabledMcpToolsCount: () => Promise<{ enabledToolCount: number; enabledServerCount: number }> + initiateTaskLoop: (userContent: Anthropic.Messages.ContentBlockParam[]) => Promise + startTask: (task?: string, images?: string[]) => Promise + resumeTaskFromHistory: () => Promise + presentAssistantMessageSafe: () => void + hasCompleteToolResultsForCurrentTurn: () => boolean + addToClineMessages: (message: import("@roo-code/types").ClineMessage) => Promise + updateClineMessage: (message: import("@roo-code/types").ClineMessage) => Promise + saveClineMessages: () => Promise + safeEnsureModelFetched: () => Promise + backoffAndAnnounce: (retryAttempt: number, error: unknown) => Promise + requestPolicySnapshot?: RequestPolicySnapshot + getFilesReadByRooSafely: (context: string) => Promise + addToApiConversationHistory: (message: unknown, reasoning?: string) => Promise + resetAssistantMessagePersistence: () => void + buildCleanConversationHistory: ( + messages: ApiMessage[], + requestModelInfo: ModelInfo, + ) => Array<{ role: string; content: unknown } | { type: "reasoning"; encrypted_content: string }> + attemptApiRequestWithActiveApi: () => AsyncGenerator + condenseContextWithActiveApi: () => Promise + retiredApis: Set +} + +type ProviderState = Awaited> +function getTaskTestAccess(task: Task): TaskTestAccess { + return task as unknown as TaskTestAccess +} + +// Mock delay before any imports that might use it +vi.mock("delay", () => ({ + __esModule: true, + default: vi.fn().mockResolvedValue(undefined), +})) + +vi.mock("uuid", async (importOriginal) => { + const actual = await importOriginal() + return { + ...actual, + v7: vi.fn(() => "00000000-0000-7000-8000-000000000000"), + } +}) + +vi.mock("execa", () => ({ + execa: vi.fn(), +})) + +vi.mock("fs/promises", async (importOriginal) => { + const actual = await importOriginal() + const mockFunctions = { + mkdir: vi.fn().mockResolvedValue(undefined), + writeFile: vi.fn().mockResolvedValue(undefined), + readFile: vi.fn().mockImplementation((filePath) => { + if (filePath.includes("ui_messages.json")) { + return Promise.resolve(JSON.stringify(mockMessages)) + } + if (filePath.includes("api_conversation_history.json")) { + return Promise.resolve( + JSON.stringify([ + { + role: "user", + content: [{ type: "text", text: "historical task" }], + ts: Date.now(), + }, + { + role: "assistant", + content: [{ type: "text", text: "I'll help you with that task." }], + ts: Date.now(), + }, + ]), + ) + } + return Promise.resolve("[]") + }), + unlink: vi.fn().mockResolvedValue(undefined), + rmdir: vi.fn().mockResolvedValue(undefined), + stat: vi.fn().mockRejectedValue({ code: "ENOENT" }), + readdir: vi.fn().mockResolvedValue([]), + } + + return { + ...actual, + ...mockFunctions, + default: mockFunctions, + } +}) + +vi.mock("p-wait-for", () => ({ + default: vi.fn().mockImplementation(async () => Promise.resolve()), +})) + +// Task tests do not exercise indexing; keep workspace resolution and its cache out of this suite. +vi.mock("../../../services/code-index/code-index-manager-registry", () => ({ + CodeIndexManagerRegistry: { + getOrCreate: vi.fn().mockReturnValue(undefined), + getAllInstances: vi.fn().mockReturnValue([]), + disposeAll: vi.fn(), + }, +})) + +vi.mock("vscode", () => { + const mockDisposable = { dispose: vi.fn() } + const mockEventEmitter = { event: vi.fn(), fire: vi.fn() } + const mockTextDocument = { uri: { fsPath: "/mock/workspace/path/file.ts" } } + const mockTextEditor = { document: mockTextDocument } + const mockTab = { input: { uri: { fsPath: "/mock/workspace/path/file.ts" } } } + const mockTabGroup = { tabs: [mockTab] } + + return { + TabInputTextDiff: vi.fn(), + CodeActionKind: { + QuickFix: { value: "quickfix" }, + RefactorRewrite: { value: "refactor.rewrite" }, + }, + window: { + createTextEditorDecorationType: vi.fn().mockReturnValue({ + dispose: vi.fn(), + }), + visibleTextEditors: [mockTextEditor], + tabGroups: { + all: [mockTabGroup], + close: vi.fn(), + onDidChangeTabs: vi.fn(() => ({ dispose: vi.fn() })), + }, + showErrorMessage: vi.fn(), + }, + workspace: { + workspaceFolders: [ + { + uri: { fsPath: "/mock/workspace/path" }, + name: "mock-workspace", + index: 0, + }, + ], + createFileSystemWatcher: vi.fn(() => ({ + onDidCreate: vi.fn(() => mockDisposable), + onDidDelete: vi.fn(() => mockDisposable), + onDidChange: vi.fn(() => mockDisposable), + dispose: vi.fn(), + })), + fs: { + stat: vi.fn().mockResolvedValue({ type: 1 }), // FileType.File = 1 + }, + onDidSaveTextDocument: vi.fn(() => mockDisposable), + getConfiguration: vi.fn(() => ({ get: (_key: string, defaultValue: unknown) => defaultValue })), + }, + env: { + uriScheme: "vscode", + language: "en", + }, + EventEmitter: vi.fn().mockImplementation(function () { + return mockEventEmitter + }), + Disposable: { + from: vi.fn(), + }, + TabInputText: vi.fn(), + } +}) + +vi.mock("../../mentions", () => ({ + parseMentions: vi.fn().mockImplementation((text) => { + return Promise.resolve({ text: `processed: ${text}`, mode: undefined, contentBlocks: [] }) + }), + openMention: vi.fn(), + getLatestTerminalOutput: vi.fn(), +})) + +vi.mock("../../../integrations/misc/extract-text", () => ({ + extractTextFromFile: vi.fn().mockResolvedValue("Mock file content"), +})) + +vi.mock("../../environment/getEnvironmentDetails", () => ({ + getEnvironmentDetails: vi.fn().mockResolvedValue(""), +})) + +vi.mock("../../mentions/processUserContentMentions", async (importOriginal) => { + const actual = await importOriginal() + return { + ...actual, + processUserContentMentions: vi.fn().mockImplementation(actual.processUserContentMentions), + } +}) + +vi.mock("../../ignore/RooIgnoreController") + +vi.mock("../../../i18n", () => { + return { + t: (key: string, args?: Record) => { + if (key === "tools:missingToolParameterWithPath") { + return `${args?.toolName}|${args?.relPath}|${args?.paramName}` + } + if (key === "tools:missingToolParameter") { + return `${args?.toolName}|${args?.paramName}` + } + return key + }, + } +}) + +vi.mock("../../condense", async (importOriginal) => { + const actual = await importOriginal() + return { + ...actual, + summarizeConversation: vi.fn().mockResolvedValue({ + messages: [{ role: "user", content: [{ type: "text", text: "continued" }], ts: Date.now() }], + summary: "summary", + cost: 0, + newContextTokens: 1, + }), + } +}) + +vi.mock("../../prompts/system", async (importOriginal) => { + const actual = await importOriginal() + return { + ...actual, + SYSTEM_PROMPT: vi.fn(actual.SYSTEM_PROMPT), + } +}) + +// Mock storagePathManager to prevent dynamic import issues. +vi.mock("../../../utils/storage", () => ({ + getTaskDirectoryPath: vi + .fn() + .mockImplementation((globalStoragePath, taskId) => Promise.resolve(`${globalStoragePath}/tasks/${taskId}`)), + getSettingsDirectoryPath: vi + .fn() + .mockImplementation((globalStoragePath) => Promise.resolve(`${globalStoragePath}/settings`)), +})) + +vi.mock("../../../utils/fs", () => ({ + fileExistsAtPath: vi.fn().mockImplementation((filePath) => { + return filePath.includes("ui_messages.json") || filePath.includes("api_conversation_history.json") + }), +})) + +const mockMessages = [ + { + ts: Date.now(), + type: "say", + say: "text", + text: "historical task", + }, +] + +// Model-info stand-in for tests that stub safeEnsureModelFetched and only need +// the settled snapshot to be a defined ModelInfo. +const stubModelInfo: ModelInfo = { + contextWindow: 200_000, + maxTokens: 4096, + supportsPromptCache: true, +} + +describe("Task teardown, dispose-time metadata repair and diff-view release", () => { + let mockProvider: ClineProvider + let mockApiConfig: ProviderSettings + let mockOutputChannel: vscode.OutputChannel + let mockExtensionContext: vscode.ExtensionContext + + beforeEach(async () => { + if (!TelemetryService.hasInstance()) { + TelemetryService.createInstance([]) + } + + // Setup mock extension context + const storageUri = { + fsPath: path.join(os.tmpdir(), "test-storage"), + } + + mockExtensionContext = { + globalState: { + get: vi.fn().mockImplementation((key: keyof GlobalState) => { + if (key === "taskHistory") { + return [ + { + id: "123", + number: 0, + ts: Date.now(), + task: "historical task", + tokensIn: 100, + tokensOut: 200, + cacheWrites: 0, + cacheReads: 0, + totalCost: 0.001, + }, + ] + } + + return undefined + }), + update: vi.fn().mockImplementation((_key, _value) => Promise.resolve()), + keys: vi.fn().mockReturnValue([]), + }, + globalStorageUri: storageUri, + workspaceState: { + get: vi.fn().mockImplementation((_key) => undefined), + update: vi.fn().mockImplementation((_key, _value) => Promise.resolve()), + keys: vi.fn().mockReturnValue([]), + }, + secrets: { + get: vi.fn().mockImplementation((_key) => Promise.resolve(undefined)), + store: vi.fn().mockImplementation((_key, _value) => Promise.resolve()), + delete: vi.fn().mockImplementation((_key) => Promise.resolve()), + }, + extensionUri: { + fsPath: "/mock/extension/path", + }, + extension: { + packageJSON: { + version: "1.0.0", + }, + }, + } as unknown as vscode.ExtensionContext + + // Setup mock output channel + mockOutputChannel = { + name: "test-output", + appendLine: vi.fn(), + append: vi.fn(), + replace: vi.fn(), + clear: vi.fn(), + show: vi.fn(), + hide: vi.fn(), + dispose: vi.fn(), + } + + // Setup mock provider with output channel + mockProvider = new ClineProvider( + mockExtensionContext, + mockOutputChannel, + "sidebar", + new ContextProxy(mockExtensionContext), + new WebviewFocusTracker(), + ) + + // Setup mock API configuration + mockApiConfig = { + apiProvider: providerIdentifiers.anthropic, + apiModelId: "claude-3-5-sonnet-20241022", + apiKey: "test-api-key", // Add API key to mock config + } + + // Mock provider methods + mockProvider.postMessageToWebview = vi.fn().mockResolvedValue(undefined) + mockProvider.postStateToWebview = vi.fn().mockResolvedValue(undefined) + mockProvider.postStateToWebviewWithoutTaskHistory = vi.fn().mockResolvedValue(undefined) + mockProvider.postStateToWebviewThrottled = vi.fn().mockResolvedValue(undefined) + mockProvider.flushPostStateToWebviewThrottled = vi.fn().mockResolvedValue(undefined) + mockProvider.getTaskWithId = vi.fn().mockImplementation(async (id) => ({ + historyItem: { + id, + ts: Date.now(), + task: "historical task", + tokensIn: 100, + tokensOut: 200, + cacheWrites: 0, + cacheReads: 0, + totalCost: 0.001, + }, + taskDirPath: "/mock/storage/path/tasks/123", + apiConversationHistoryFilePath: "/mock/storage/path/tasks/123/api_conversation_history.json", + uiMessagesFilePath: "/mock/storage/path/tasks/123/ui_messages.json", + apiConversationHistory: [ + { + role: "user", + content: [{ type: "text", text: "historical task" }], + ts: Date.now(), + }, + { + role: "assistant", + content: [{ type: "text", text: "I'll help you with that task." }], + ts: Date.now(), + }, + ], + })) + }) + + describe("early-return and teardown cleanup", () => { + let consoleErrorSpy: ReturnType + + beforeEach(() => { + consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + }) + + afterEach(() => { + consoleErrorSpy.mockRestore() + vi.restoreAllMocks() + }) + + it("retries the failed metadata / task-history stage once, awaited, during dispose", async () => { + // A metadata or task-history failure is reported separately from the message write, + // but it must not simply be dropped: the failure is recorded and dispose() awaits + // one retry before the task stops serving, so a task torn down after a partially + // successful save does not leave its history entry behind the messages that are + // already on disk. + const taskDir = path.join(os.tmpdir(), "test-storage", "tasks", "00000000-0000-7000-8000-000000000000") + fsReal.mkdirSync(taskDir, { recursive: true }) + // The retry is held open by a gate. Ungated it settles the moment it starts, so the + // call-count assertions below would pass whether or not dispose() awaited it. Holding + // it open is what lets the test observe disposal still waiting on the retry. + let releaseRetry: (() => void) | undefined + const retryGate = new Promise((resolve) => { + releaseRetry = resolve + }) + const historySpy = vi + .spyOn(mockProvider, "updateTaskHistory") + .mockRejectedValueOnce(new Error("history stage unavailable")) + .mockImplementation(async () => { + await retryGate + return [] + }) + + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + task: "test task", + startTask: false, + }) + + // The message write succeeded, so the save still reports success even though the + // metadata stage failed - and the failure is logged, not swallowed. + await expect(getTaskTestAccess(task).saveClineMessages()).resolves.toBe(true) + expect(historySpy).toHaveBeenCalledTimes(1) + expect(consoleErrorSpy).toHaveBeenCalledWith("Failed to save task metadata:", expect.any(Error)) + + let disposalSettled = false + const disposalFinished = task.dispose().then(() => { + disposalSettled = true + }) + + try { + // Drain until disposal settles or the cap is reached. The gate is the only thing holding the + // retry open, so a disposal that awaits the retry cannot settle inside this loop; a disposal + // that dropped the awaited call runs the loop out having settled on its own. + for (let tick = 0; tick < 2000 && !disposalSettled; tick++) { + await new Promise((wake) => setImmediate(wake)) + } + expect(historySpy).toHaveBeenCalledTimes(2) + // dispose() has not resolved while the retry it awaits is still open. Turning the + // awaited call into a floating one fails here instead of going unnoticed. + expect(disposalSettled).toBe(false) + } finally { + releaseRetry?.() + } + + await disposalFinished + + // dispose() awaited the retry: by the time it resolves the metadata stage has run + // again with the recomputed history item for this task. + expect(disposalSettled).toBe(true) + expect(historySpy).toHaveBeenCalledTimes(2) + // The history record is keyed by `id` (the task id), not a taskId field. + expect(historySpy.mock.calls[1][0]).toEqual(expect.objectContaining({ id: task.taskId })) + + historySpy.mockRestore() + }) + + it("still retries for a task whose stored api config is legitimately absent", async () => { + // Tasks saved before the api config was recorded have apiConfigName undefined. Their + // initialization has completed, so the dispose-time repair must still run: gating it on + // the value rather than on the settled promise drops the retry for every such task. + const taskDir = path.join(os.tmpdir(), "test-storage", "tasks", "00000000-0000-7000-8000-000000000000") + fsReal.mkdirSync(taskDir, { recursive: true }) + const historySpy = vi + .spyOn(mockProvider, "updateTaskHistory") + .mockRejectedValueOnce(new Error("history stage unavailable")) + + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + task: "test task", + startTask: false, + }) + + // Bracket access for the private fields: wait for the async api-config + // initialization to settle, then drop the name - the shape of a task loaded from a + // history entry saved before the api config was recorded. + await task["taskApiConfigReady"] + task["_taskApiConfigName"] = undefined + expect(task["taskApiConfigReadySettled"]).toBe(true) + + await expect(getTaskTestAccess(task).saveClineMessages()).resolves.toBe(true) + expect(historySpy).toHaveBeenCalledTimes(1) + + await task.dispose() + + expect(historySpy).toHaveBeenCalledTimes(2) + historySpy.mockRestore() + }) + + it("treats a released provider as a failed metadata stage instead of a silent success", async () => { + // providerRef.deref() returns undefined once the provider is gone. Without the guard the + // two history calls optional-chain to nothing, persistTaskMetadata() returns true and + // CLEARS pendingTaskMetadataRepair: the history entry then stays behind the messages that + // are already on disk and disposeOnce() has nothing left to retry on. + const taskDir = path.join(os.tmpdir(), "test-storage", "tasks", "00000000-0000-7000-8000-000000000000") + fsReal.mkdirSync(taskDir, { recursive: true }) + const historySpy = vi.spyOn(mockProvider, "updateTaskHistory").mockResolvedValue([]) + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + task: "test task", + startTask: false, + }) + // Bracket access for the private field: the flag must start clear so the assertion below + // proves the failed stage SET it again rather than merely leaving it alone. + await task["taskApiConfigReady"] + task["pendingTaskMetadataRepair"] = false + Object.defineProperty(task, "providerRef", { + value: { deref: vi.fn().mockReturnValue(undefined) }, + configurable: true, + }) + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + + const persisted = await task["persistTaskMetadata"]() + + expect(persisted).toBe(false) + expect(historySpy).not.toHaveBeenCalled() + expect(task["pendingTaskMetadataRepair"]).toBe(true) + expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("no provider to persist task history")) + errorSpy.mockRestore() + historySpy.mockRestore() + }) + + it("clears the repair flag and reports success when the metadata stage writes the history entry", async () => { + // The success side of the stage the row above covers the failure of. The history write + // lands, the flag disposeOnce() keys off is cleared rather than left alone, and the stage + // reports success for itself - deliberately separate from the message write's own result, + // which saveClineMessages() reports on its own. + const taskDir = path.join(os.tmpdir(), "test-storage", "tasks", "00000000-0000-7000-8000-000000000000") + fsReal.mkdirSync(taskDir, { recursive: true }) + const historySpy = vi.spyOn(mockProvider, "updateTaskHistory").mockResolvedValue([]) + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + task: "test task", + startTask: false, + }) + // Bracket access for the private field: set the flag first so the assertion below proves + // the stage CLEARED it rather than merely never having set it. + await task["taskApiConfigReady"] + task["pendingTaskMetadataRepair"] = true + + const persisted = await task["persistTaskMetadata"]() + + expect(persisted).toBe(true) + expect(historySpy).toHaveBeenCalledTimes(1) + expect(task["pendingTaskMetadataRepair"]).toBe(false) + historySpy.mockRestore() + }) + + it("finishes the synchronous teardown before the dispose-time metadata retry", async () => { + // The retry has to sit after disposeOnce()'s synchronous section, not in front of it: + // abortTaskOnce() awaits diffReversionPromise right after void dispose(), an abandoned + // stream can reach its finally while the retry is in flight and leave the revert decision + // below false, and direct dispose() callers rely on the abort flag being set + // synchronously. A task-history write that never settles turns any of those three + // regressions into an observable hang. + const taskDir = path.join(os.tmpdir(), "test-storage", "tasks", "00000000-0000-7000-8000-000000000000") + fsReal.mkdirSync(taskDir, { recursive: true }) + let releaseHistory: (() => void) | undefined + const historyGate = new Promise((resolve) => { + releaseHistory = resolve + }) + const historySpy = vi + .spyOn(mockProvider, "updateTaskHistory") + .mockImplementation(() => historyGate.then(() => [])) + + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + task: "test task", + startTask: false, + }) + // Let the async api-config initialization settle first: the retry is deliberately + // skipped while it is pending, which would hide the ordering under test. + await task["taskApiConfigReady"] + task["pendingTaskMetadataRepair"] = true + task.isStreaming = true + task.diffViewProvider.isEditing = true + // A modify: disposal reverts it. The edit type decides which release path disposal takes + // (see the create/modify disposal pair below), and this test is about the ordering. + task.diffViewProvider.editType = "modify" + const revertSpy = vi.spyOn(task.diffViewProvider, "revertChanges").mockResolvedValue(undefined) + + const disposing = task.dispose() + // No await before this one: dispose() sets the abort flag synchronously, so a retry moved + // in front of the teardown would park dispose() on the gated task-history write first and + // this would read false. + expect(task.abort).toBe(true) + + await new Promise((resolve) => setImmediate(resolve)) + + // The teardown completed without waiting for the task-history write. + expect(task.abort).toBe(true) + expect(revertSpy).toHaveBeenCalledTimes(1) + // Bracket access for the private field: abortTaskOnce() awaits this promise right after + // void dispose(), so it has to exist once the teardown has run. + expect(task["diffReversionPromise"]).toBeDefined() + // And the retry has actually started: historySpy is gated, so without this assertion the + // test cannot tell "the teardown ran first" from "the retry never ran at all". + expect(historySpy).toHaveBeenCalledTimes(1) + + releaseHistory?.() + await disposing + + expect(historySpy).toHaveBeenCalledTimes(1) + historySpy.mockRestore() + revertSpy.mockRestore() + }) + + it("discards a cancelled create preview instead of saving it through revertChanges", async () => { + // revertChanges() for a create SAVES the dirty buffer - the partial content the model + // streamed and nobody approved - before deleting the file, so a failed delete leaves + // unapproved bytes on disk. A cancelled stream never reached approval, so disposal has to + // take the discard path, which empties the buffer first. + const taskDir = path.join(os.tmpdir(), "test-storage", "tasks", "00000000-0000-7000-8000-000000000000") + fsReal.mkdirSync(taskDir, { recursive: true }) + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + task: "test task", + startTask: false, + }) + await task["taskApiConfigReady"] + task.isStreaming = true + task.diffViewProvider.isEditing = true + task.diffViewProvider.editType = "create" + const revertSpy = vi.spyOn(task.diffViewProvider, "revertChanges").mockResolvedValue(undefined) + const discardSpy = vi.spyOn(task.diffViewProvider, "discardUnapprovedStream").mockResolvedValue(undefined) + + await task.dispose() + + expect(discardSpy).toHaveBeenCalledTimes(1) + expect(revertSpy).not.toHaveBeenCalled() + revertSpy.mockRestore() + discardSpy.mockRestore() + }) + + it("still reverts a cancelled modify preview, whose restore is the content already on disk", async () => { + // The pair of the test above: a modify has prior content, and disposal keeps reverting it + // so the editor shows what was on disk before the stream started. + const taskDir = path.join(os.tmpdir(), "test-storage", "tasks", "00000000-0000-7000-8000-000000000000") + fsReal.mkdirSync(taskDir, { recursive: true }) + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + task: "test task", + startTask: false, + }) + await task["taskApiConfigReady"] + task.isStreaming = true + task.diffViewProvider.isEditing = true + task.diffViewProvider.editType = "modify" + const revertSpy = vi.spyOn(task.diffViewProvider, "revertChanges").mockResolvedValue(undefined) + const discardSpy = vi.spyOn(task.diffViewProvider, "discardUnapprovedStream").mockResolvedValue(undefined) + + await task.dispose() + + expect(revertSpy).toHaveBeenCalledTimes(1) + expect(discardSpy).not.toHaveBeenCalled() + revertSpy.mockRestore() + discardSpy.mockRestore() + }) + + it("discards a create preview from the stream abort path, not only from disposal", async () => { + // A cancelled stream never reached approval. abortStream() used to revert unconditionally, + // and revertChanges()'s create branch SAVES the dirty buffer - the partial model output - + // before deleting the file, so a failed delete left unapproved bytes on disk. The abort + // path now shares disposal's release, so this pins it to the discard. + const taskDir = path.join(os.tmpdir(), "test-storage", "tasks", "00000000-0000-7000-8000-000000000000") + fsReal.mkdirSync(taskDir, { recursive: true }) + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + task: "abort release create", + startTask: false, + }) + vi.spyOn(task.diffViewProvider, "reset").mockResolvedValue(undefined) + vi.spyOn(getTaskTestAccess(task), "safeEnsureModelFetched").mockResolvedValue(stubModelInfo) + // The streaming loop checks this.abort right after the presenter hands off a chunk, so the + // cancel lands mid-stream and reaches abortStream(-"user_cancelled") with the view open. + vi.spyOn(getTaskTestAccess(task), "presentAssistantMessageSafe").mockImplementation(() => { + // Task.abort is a getter over the run-state latch; the method the tree declares is + // abortTask(), which latches the abort synchronously and returns the shared promise. + void task.abortTask() + }) + vi.spyOn(task, "attemptApiRequest").mockImplementation(() => + asyncStreamFrom([{ type: "text", text: "partial model output" }]), + ) + const revertSpy = vi.spyOn(task.diffViewProvider, "revertChanges").mockResolvedValue(undefined) + const discardSpy = vi.spyOn(task.diffViewProvider, "discardUnapprovedStream").mockResolvedValue(undefined) + task.diffViewProvider.isEditing = true + task.diffViewProvider.editType = "create" + // Isolation: disposal's own release trigger is gated on isStreaming, so clearing that flag + // on the way into dispose() leaves the stream-abort path as the only one left that can start + // a release here. Without the swap the two paths cannot be told apart - abortTaskOnce() voids + // dispose(), and the call count below could be reporting either one of them. + const disposeReal = task.dispose.bind(task) + let disposeCalls = 0 + const disposeSpy = vi.spyOn(task, "dispose").mockImplementation(() => { + disposeCalls++ + task.isStreaming = false + return disposeReal.call(task) + }) + + await task.recursivelyMakeClineRequests([{ type: "text", text: "cancel this stream" }]).catch(() => {}) + + // Disposal really ran, so the swap above was exercised rather than vacuous, and every entry + // into it had its release trigger disabled: the release counted below is the abort path's own. + expect(disposeCalls).toBeGreaterThan(0) + const sharedRelease = task["diffReversionPromise"] + expect(sharedRelease).toBeDefined() + // It had already settled before this line, so disposal never got the chance to start it. + await sharedRelease + expect(discardSpy).toHaveBeenCalledTimes(1) + expect(revertSpy).not.toHaveBeenCalled() + revertSpy.mockRestore() + discardSpy.mockRestore() + disposeSpy.mockRestore() + }) + + it("reverts a modify preview from the stream abort path, whose restore is the content on disk", async () => { + // The pair of the test above: a modify has prior content, so the shared release keeps + // reverting it and shows what was on disk before the stream started. + const taskDir = path.join(os.tmpdir(), "test-storage", "tasks", "00000000-0000-7000-8000-000000000000") + fsReal.mkdirSync(taskDir, { recursive: true }) + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + task: "abort release modify", + startTask: false, + }) + vi.spyOn(task.diffViewProvider, "reset").mockResolvedValue(undefined) + vi.spyOn(getTaskTestAccess(task), "safeEnsureModelFetched").mockResolvedValue(stubModelInfo) + vi.spyOn(getTaskTestAccess(task), "presentAssistantMessageSafe").mockImplementation(() => { + // Task.abort is a getter over the run-state latch; the method the tree declares is + // abortTask(), which latches the abort synchronously and returns the shared promise. + void task.abortTask() + }) + vi.spyOn(task, "attemptApiRequest").mockImplementation(() => + asyncStreamFrom([{ type: "text", text: "partial model output" }]), + ) + const revertSpy = vi.spyOn(task.diffViewProvider, "revertChanges").mockResolvedValue(undefined) + const discardSpy = vi.spyOn(task.diffViewProvider, "discardUnapprovedStream").mockResolvedValue(undefined) + task.diffViewProvider.isEditing = true + task.diffViewProvider.editType = "modify" + // Isolation: disposal's own release trigger is gated on isStreaming, so clearing that flag + // on the way into dispose() leaves the stream-abort path as the only one left that can start + // a release here. Without the swap the two paths cannot be told apart - abortTaskOnce() voids + // dispose(), and the call count below could be reporting either one of them. + const disposeReal = task.dispose.bind(task) + let disposeCalls = 0 + const disposeSpy = vi.spyOn(task, "dispose").mockImplementation(() => { + disposeCalls++ + task.isStreaming = false + return disposeReal.call(task) + }) + + await task.recursivelyMakeClineRequests([{ type: "text", text: "cancel this stream" }]).catch(() => {}) + + // Disposal really ran, so the swap above was exercised rather than vacuous, and every entry + // into it had its release trigger disabled: the release counted below is the abort path's own. + expect(disposeCalls).toBeGreaterThan(0) + const sharedRelease = task["diffReversionPromise"] + expect(sharedRelease).toBeDefined() + // It had already settled before this line, so disposal never got the chance to start it. + await sharedRelease + expect(revertSpy).toHaveBeenCalledTimes(1) + expect(discardSpy).not.toHaveBeenCalled() + revertSpy.mockRestore() + discardSpy.mockRestore() + disposeSpy.mockRestore() + }) + + it("surfaces a failed diff release instead of completing the abort as if the workspace were restored", async () => { + // The ask: a release that failed must not be reported as a completed rollback. Recording and surfacing + // is the chosen shape - propagating it would make dispose() itself begin rejecting on the path whose + // own comment says abort must always succeed - so the proof is that the abort still completes AND the + // user is told the rollback did not happen. + const taskDir = path.join(os.tmpdir(), "test-storage", "tasks", "00000000-0000-7000-8000-000000000009") + fsReal.mkdirSync(taskDir, { recursive: true }) + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + task: "abort release failure", + startTask: false, + }) + vi.spyOn(task.diffViewProvider, "reset").mockResolvedValue(undefined) + vi.spyOn(getTaskTestAccess(task), "safeEnsureModelFetched").mockResolvedValue(stubModelInfo) + vi.spyOn(getTaskTestAccess(task), "presentAssistantMessageSafe").mockImplementation(() => { + void task.abortTask() + }) + vi.spyOn(task, "attemptApiRequest").mockImplementation(() => + asyncStreamFrom([{ type: "text", text: "partial model output" }]), + ) + vi.spyOn(task.diffViewProvider, "revertChanges").mockResolvedValue(undefined) + const discardSpy = vi + .spyOn(task.diffViewProvider, "discardUnapprovedStream") + .mockRejectedValue(new Error("placeholder could not be removed")) + // Observed, not replaced: replacing say() would change the streaming path this test runs through, + // so the spy keeps the real implementation and only records the calls. + const saySpy = vi.spyOn(task, "say") + task.diffViewProvider.isEditing = true + task.diffViewProvider.editType = "create" + // Isolation as in the two tests above: disposal's own trigger is disabled on the way in, so the + // release whose failure is reported here can only be the stream-abort path's. + const disposeReal = task.dispose.bind(task) + let disposeCalls = 0 + const disposeSpy = vi.spyOn(task, "dispose").mockImplementation(() => { + disposeCalls++ + task.isStreaming = false + return disposeReal.call(task) + }) + + await task.recursivelyMakeClineRequests([{ type: "text", text: "cancel this stream" }]).catch(() => {}) + // abortTask() returns the shared abort promise, so awaiting it again cannot start a second abort; + // it only guarantees abortTaskOnce() has finished before the assertions below. + await task.abortTask() + + expect(disposeCalls).toBeGreaterThan(0) + expect(discardSpy).toHaveBeenCalledTimes(1) + // The failure is recorded rather than thrown, which is what lets the abort path finish. + expect(task["diffReversionFailed"]).toBe(true) + // The promise itself stays resolved: that is the half of the ruling this keeps abort semantics + // alone for, and awaiting it here fails the test if the release promise ever rejects. + const sharedRelease = task["diffReversionPromise"] + expect(sharedRelease).toBeDefined() + await sharedRelease + // Exactly one error say carries this release failure; a second would mean the abort reported the + // same hazard twice. + const releaseFailureSays = saySpy.mock.calls.filter( + (call: unknown[]) => call[0] === "error" && String(call[1]).includes("diff view could not be released"), + ) + expect(releaseFailureSays.length).toBe(1) + saySpy.mockRestore() + discardSpy.mockRestore() + disposeSpy.mockRestore() + }) + + it("reports the release failure through the log when the error say itself fails", async () => { + // The surfacing block wraps its own say in a catch, so that a failed report does not turn the abort into + // a rejection either. That fallback only runs when say rejects, so it needs its own case: the first + // failing-release test leaves say with its real implementation and never reaches it. + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + task: "abort release failure, failing report", + startTask: false, + }) + // These two cases are about the release record, not about persistence, and every Task in this file + // lands in the same task directory (uuid v7 is mocked to one constant), so a persisting test + // leaves files that the next run reads back. Mocking the two write stages keeps the directory as + // the run found it. + vi.spyOn(getTaskTestAccess(task), "saveClineMessages").mockResolvedValue(true) + vi.spyOn(getTaskTestAccess(task), "addToApiConversationHistory").mockResolvedValue(undefined) + vi.spyOn(task.diffViewProvider, "reset").mockResolvedValue(undefined) + vi.spyOn(getTaskTestAccess(task), "safeEnsureModelFetched").mockResolvedValue(stubModelInfo) + vi.spyOn(getTaskTestAccess(task), "presentAssistantMessageSafe").mockImplementation(() => { + void task.abortTask() + }) + vi.spyOn(task, "attemptApiRequest").mockImplementation(() => + asyncStreamFrom([{ type: "text", text: "partial model output" }]), + ) + vi.spyOn(task.diffViewProvider, "revertChanges").mockResolvedValue(undefined) + const discardSpy = vi + .spyOn(task.diffViewProvider, "discardUnapprovedStream") + .mockRejectedValue(new Error("placeholder could not be removed")) + // Only the release-failure report rejects. Every other say keeps the real implementation, because + // replacing say() outright changes the streaming path this test runs through. + const sayReal = task.say.bind(task) + const saySpy = vi + .spyOn(task, "say") + .mockImplementation((...args) => + args[0] === "error" && String(args[1] ?? "").includes("diff view could not be released") + ? Promise.reject(new Error("the error say itself failed")) + : sayReal(...args), + ) + const errorLogSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + task.diffViewProvider.isEditing = true + task.diffViewProvider.editType = "create" + const disposeReal = task.dispose.bind(task) + let disposeCalls = 0 + const disposeSpy = vi.spyOn(task, "dispose").mockImplementation(() => { + disposeCalls++ + task.isStreaming = false + return disposeReal.call(task) + }) + + await task.recursivelyMakeClineRequests([{ type: "text", text: "cancel this stream" }]).catch(() => {}) + await task.abortTask() + + expect(disposeCalls).toBeGreaterThan(0) + expect(discardSpy).toHaveBeenCalledTimes(1) + expect(task["diffReversionFailed"]).toBe(true) + // The report was attempted once and failed. The abort still ran to the end of its try block, which is + // what the awaited abortTask() above proves, and the report's own failure went to the log rather than + // out through abortTask(). + expect( + saySpy.mock.calls.filter( + (call: unknown[]) => + call[0] === "error" && String(call[1]).includes("diff view could not be released"), + ).length, + ).toBe(1) + const reportedFailures = errorLogSpy.mock.calls.filter((call: unknown[]) => + String(call[0]).includes("Error reporting the failed diff release"), + ) + expect(reportedFailures.length).toBe(1) + expect(String(reportedFailures[0][1])).toContain("the error say itself failed") + saySpy.mockRestore() + discardSpy.mockRestore() + disposeSpy.mockRestore() + errorLogSpy.mockRestore() + }) + + it("clears the release-failure record when the failed stream is retried", async () => { + // The product"s own way back into a request after a release failed is the mid-stream failure retry: + // the stream throws, the handler releases the diff view (which fails here), and the same content is + // pushed back onto the request stack, so the next iteration runs the reset block again after the + // failure is on the books. A user cancel cannot do this - the run-state abort latch is one-way + // (latches.abort is set by abortRequested and never cleared), and a new request throws at its own + // abort check - so this case rides the retry path instead of a second message. + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + task: "failed release, then a stream retry", + startTask: false, + }) + // These two cases are about the release record, not about persistence, and every Task in this file + // lands in the same task directory (uuid v7 is mocked to one constant), so a persisting test + // leaves files that the next run reads back. Mocking the two write stages keeps the directory as + // the run found it. + vi.spyOn(getTaskTestAccess(task), "saveClineMessages").mockResolvedValue(true) + vi.spyOn(getTaskTestAccess(task), "addToApiConversationHistory").mockResolvedValue(undefined) + // The request path awaits diffViewProvider.reset() immediately before the reset block, so it has to + // resolve; the release inside the failure handler is what fails. + vi.spyOn(task.diffViewProvider, "reset").mockResolvedValue(undefined) + vi.spyOn(task.diffViewProvider, "revertChanges").mockResolvedValue(undefined) + const discardSpy = vi + .spyOn(task.diffViewProvider, "discardUnapprovedStream") + .mockRejectedValue(new Error("placeholder could not be removed")) + // safeEnsureModelFetched is awaited directly after the reset block, so each call is a read of the + // record at exactly the point where the reset has just run, together with how many releases have + // happened by then. + const recordAtModelFetch: { failed: boolean; releases: number }[] = [] + vi.spyOn(getTaskTestAccess(task), "safeEnsureModelFetched").mockImplementation(async () => { + recordAtModelFetch.push({ + failed: task["diffReversionFailed"], + releases: discardSpy.mock.calls.length, + }) + return stubModelInfo + }) + // First call: one chunk, then the provider stream breaks, which is what routes the request through + // abortStream on the streaming_failed path rather than through a user cancel. + // Second call: a clean stream, cancelled at its first presentation so the run ends promptly. + let streamCalls = 0 + vi.spyOn(task, "attemptApiRequest").mockImplementation(() => { + streamCalls++ + if (streamCalls === 1) { + return (async function* (): AsyncGenerator { + yield { type: "text", text: "partial model output" } + throw new Error("provider stream broke mid-request") + })() + } + return asyncStreamFrom([{ type: "text", text: "second attempt" }]) + }) + let presentCalls = 0 + vi.spyOn(getTaskTestAccess(task), "presentAssistantMessageSafe").mockImplementation(() => { + presentCalls++ + if (presentCalls >= 2) { + void task.abortTask() + } + }) + vi.spyOn(getTaskTestAccess(task), "backoffAndAnnounce").mockResolvedValue(undefined) + task.diffViewProvider.isEditing = true + task.diffViewProvider.editType = "create" + + await task.recursivelyMakeClineRequests([{ type: "text", text: "stream that fails" }]).catch(() => {}) + + // Two model fetches means the retry really re-entered the request path, and the first row shows that a + // failed release was already on the books before it did. + expect(recordAtModelFetch.length).toBeGreaterThanOrEqual(2) + expect(recordAtModelFetch[0]).toEqual({ failed: false, releases: 0 }) + // The pin: at the retry"s model fetch - the first point after the reset block - the record is + // clear although exactly one failed release has run. Flip the reset from false to true and this row + // reads { failed: true } instead. + expect(recordAtModelFetch[1]).toEqual({ failed: false, releases: 1 }) + // Exactly one release ran and it failed, and the retry opened no new one: the settled promise and its + // record were dropped together, so the session that follows starts clean and can report its own + // failure instead of inheriting this one. + expect(discardSpy).toHaveBeenCalledTimes(1) + expect(task["diffReversionFailed"]).toBe(false) + expect(task["diffReversionPromise"]).toBeUndefined() + discardSpy.mockRestore() + }) + + it("starts one release per diff view and hands every later caller the same promise", async () => { + // Single owner: a release one path already started must not be repeated by the other over + // the same buffer (a second revertChanges() could roll back content the first release + // already restored). The promise is stored on the first call and returned afterwards. + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + task: "single release owner", + startTask: false, + }) + await task["taskApiConfigReady"] + task.diffViewProvider.isEditing = true + task.diffViewProvider.editType = "create" + let releaseDiscard: (() => void) | undefined + const discardGate = new Promise((resolve) => { + releaseDiscard = resolve + }) + const discardSpy = vi + .spyOn(task.diffViewProvider, "discardUnapprovedStream") + .mockImplementation(() => discardGate) + + const first = task["releaseUnapprovedDiffView"]() + const second = task["releaseUnapprovedDiffView"]() + + expect(second).toBe(first) + expect(discardSpy).toHaveBeenCalledTimes(1) + + releaseDiscard?.() + await first + discardSpy.mockRestore() + }) + + it("does not start a release when no diff view is open", async () => { + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + task: "no diff view to release", + startTask: false, + }) + await task["taskApiConfigReady"] + const discardSpy = vi.spyOn(task.diffViewProvider, "discardUnapprovedStream").mockResolvedValue(undefined) + const revertSpy = vi.spyOn(task.diffViewProvider, "revertChanges").mockResolvedValue(undefined) + + const release = task["releaseUnapprovedDiffView"]() + await release + + expect(revertSpy).not.toHaveBeenCalled() + expect(discardSpy).not.toHaveBeenCalled() + revertSpy.mockRestore() + discardSpy.mockRestore() + }) + + it("does not roll the same view back again when disposal follows the stream teardown", async () => { + // The interleaving the single-owner promise exists for: the abort teardown released the + // view, then disposal runs while isEditing is still true (the mocked release does not reset + // the provider) and must await that release instead of starting a second one. + const taskDir = path.join(os.tmpdir(), "test-storage", "tasks", "00000000-0000-7000-8000-000000000000") + fsReal.mkdirSync(taskDir, { recursive: true }) + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + task: "dispose after abort release", + startTask: false, + }) + await task["taskApiConfigReady"] + task.isStreaming = true + task.diffViewProvider.isEditing = true + task.diffViewProvider.editType = "create" + const discardSpy = vi.spyOn(task.diffViewProvider, "discardUnapprovedStream").mockResolvedValue(undefined) + const revertSpy = vi.spyOn(task.diffViewProvider, "revertChanges").mockResolvedValue(undefined) + + await task["releaseUnapprovedDiffView"]() + await task.dispose() + + expect(discardSpy).toHaveBeenCalledTimes(1) + expect(revertSpy).not.toHaveBeenCalled() + discardSpy.mockRestore() + revertSpy.mockRestore() + }) + + it("does not block teardown when a pending metadata repair has an unsettled api-config init", async () => { + // persistTaskMetadata() awaits taskApiConfigReady. For a task whose async api-config + // initialization never settles, an unconditional dispose-time retry would hang the + // teardown, so the retry is skipped until that promise settles. + const taskDir = path.join(os.tmpdir(), "test-storage", "tasks", "00000000-0000-7000-8000-000000000000") + fsReal.mkdirSync(taskDir, { recursive: true }) + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + task: "test task", + startTask: false, + }) + + // Bracket access for the private fields: this is the pathological construction where + // the async api-config initialization never resolves, which no public API can produce. + task["_taskApiConfigName"] = undefined + task["taskApiConfigReady"] = new Promise(() => {}) + task["pendingTaskMetadataRepair"] = true + + const outcome = await Promise.race([ + task.dispose().then(() => "disposed"), + new Promise((resolve) => setTimeout(() => resolve("timed out"), 2000)), + ]) + + expect(outcome).toBe("disposed") + }) + + it("runs the dispose-time metadata retry when the api-config name is already known even if init never settles", async () => { + // persistTaskMetadata() only awaits taskApiConfigReady while _taskApiConfigName is still + // undefined. Once the name is known (a handoff, a resumed history item, or an explicit + // setTaskApiConfigName), the retry cannot block teardown - so skipping it because the + // promise has not settled leaves the history entry stale for a task that was perfectly + // able to repair it. + const taskDir = path.join(os.tmpdir(), "test-storage", "tasks", "00000000-0000-7000-8000-000000000000") + fsReal.mkdirSync(taskDir, { recursive: true }) + const historySpy = vi.spyOn(mockProvider, "updateTaskHistory").mockResolvedValue([]) + const task = new Task({ + provider: mockProvider, + apiConfiguration: mockApiConfig, + task: "test task", + startTask: false, + }) + + // Bracket access for the private fields: the pathological combination - a name that is + // already resolved while the initialization promise never settles - is not reachable + // through the public API. + task["_taskApiConfigName"] = "my-profile" + task["taskApiConfigReady"] = new Promise(() => {}) + task["taskApiConfigReadySettled"] = false + task["pendingTaskMetadataRepair"] = true + + const outcome = await Promise.race([ + task.dispose().then(() => "disposed"), + new Promise((resolve) => setTimeout(() => resolve("timed out"), 2000)), + ]) + + expect(outcome).toBe("disposed") + expect(historySpy).toHaveBeenCalledTimes(1) + historySpy.mockRestore() + }) + }) +}) diff --git a/src/core/tools/BaseTool.ts b/src/core/tools/BaseTool.ts index 1e73a8285a..827011fba0 100644 --- a/src/core/tools/BaseTool.ts +++ b/src/core/tools/BaseTool.ts @@ -155,9 +155,23 @@ export abstract class BaseTool { throw new Error("Tool call is missing native arguments (nativeArgs).") } } catch (error) { - console.error(`Error parsing parameters:`, error) - const errorMessage = `Failed to parse ${this.name} parameters: ${error instanceof Error ? error.message : String(error)}` - await callbacks.handleError(`parsing ${this.name} args`, new Error(errorMessage)) + const parseError = error instanceof Error ? error : new Error(String(error)) + console.error(`Error parsing parameters:`, parseError) + // Final args could not be parsed (e.g. the model's tool call was truncated + // mid-JSON by the output token limit), so execute() will never run. If a + // streaming delta already opened a partial "tool" ask (partial: true), + // finalize it here or the webview spinner stays stuck indefinitely. + await task.finalizePartialToolAsk().catch((finalizeError) => { + console.error(`Error finalizing ${this.name} partial tool ask:`, finalizeError) + }) + // execute() never runs on this path, so tools that keep per-task state + // outside execute() (streaming failure marks, abort listeners) get their + // one remaining teardown boundary here. + const reportedStreamingFailure = await this.onParameterParseFailure(task, callbacks, parseError) + if (!reportedStreamingFailure) { + const errorMessage = `Failed to parse ${this.name} parameters: ${parseError.message}` + await callbacks.handleError(`parsing ${this.name} args`, new Error(errorMessage)) + } // Note: handleError already emits a tool_result via formatResponse.toolError in the caller. // Do NOT call pushToolResult here to avoid duplicate tool_result payloads. return @@ -166,4 +180,27 @@ export abstract class BaseTool { // Execute with typed parameters await this.execute(params, task, callbacks) } + + /** + * Teardown boundary for the native-argument parse-failure path in handle(). + * + * When nativeArgs are missing or malformed, execute() never runs, so per-task + * state a tool registered outside execute() (streaming failure marks, abort + * listeners) is never torn down there. Streaming tools override this to tear + * that state down and, when a streaming delta already failed, to report the + * captured streaming error instead of the generic parse error. + * + * @param _task - Task instance + * @param _callbacks - Tool execution callbacks + * @param _parseError - The native-argument parse error + * @returns true when the override already reported the failure to the user, + * so handle() suppresses the generic parse error + */ + protected async onParameterParseFailure( + _task: Task, + _callbacks: ToolCallbacks, + _parseError: Error, + ): Promise { + return false + } } diff --git a/src/core/tools/WriteToFileTool.ts b/src/core/tools/WriteToFileTool.ts index 0c5c80abb9..dc7cae7adc 100644 --- a/src/core/tools/WriteToFileTool.ts +++ b/src/core/tools/WriteToFileTool.ts @@ -1,7 +1,7 @@ import path from "path" import fs from "fs/promises" -import { type ClineSayTool, DEFAULT_WRITE_DELAY_MS } from "@roo-code/types" +import { type ClineSayTool, DEFAULT_WRITE_DELAY_MS, RooCodeEventName } from "@roo-code/types" import { Task } from "../task/Task" import { formatResponse } from "../prompts/responses" @@ -22,9 +22,347 @@ interface WriteToFileParams { content: string } +/** + * Per-task partial-streaming state tracked by WriteToFileTool. + */ +interface TaskPartialStreamState { + /** Last path seen during streaming; undefined until the first delta. */ + lastSeenPartialPath: string | undefined + /** True once a streaming delta hit a fatal filesystem error. */ + streamFailed: boolean + /** The original filesystem error of the failed streaming delta, reported once + * by onParameterParseFailure() when the final block fails to parse (so + * execute() never runs and would never report it). */ + streamError: Error | undefined + /** The task that owns this state; target for abort-listener deregistration. */ + task: Task + /** TaskAborted listener that tears this state down; registered once per task. */ + abortCleanup: () => void +} + export class WriteToFileTool extends BaseTool<"write_to_file"> { readonly name = "write_to_file" as const + /** + * Per-task partial-streaming state, keyed by task id (taskId + instanceId). + * + * All per-task fields live in one object per task so that resetTaskPartialState() / + * resetPartialState() cannot clear a subset of them and leak the rest (abort + * listener, failure mark, path-stabilization entry) for an abandoned stream. + * + * This deliberately diverges from the sibling streaming tools (ApplyDiffTool, + * EditFileTool, SearchReplaceTool, EditTool), which rely on BaseTool's singleton + * lastSeenPartialPath / resetPartialState and keep no failure state. The divergence is + * intentional, for two reasons: + * + * 1. Only this tool's handlePartial performs failure-prone streaming work + * (diffViewProvider.open/update, which can throw EACCES/EROFS); the siblings only + * send a task.ask preview. Without per-task failure tracking, every later delta for + * a failed path would re-attempt the failing operation and re-spawn a partial tool + * message. + * + * 2. The tool instance is a module-level singleton shared by every task, including + * tasks from different ClineProvider instances (e.g. sidebar and tab-panel + * providers, which activate independently). A single provider streams at most one + * task at a time — TaskScheduler gates task.run() at maxConcurrency=1 and + * delegation disposes the parent before the child starts — so per-task keying is + * reachable specifically across providers, where two providers can stream + * write_to_file concurrently through this same singleton. + * + * Lifting this per-task keying into BaseTool for all streaming tools is a follow-up + * (separate PR); it is deliberately not done here. + */ + private taskPartialStreamState = new Map() + + private getPartialStreamFailureKey(task: Task): string { + return `${task.taskId}.${task.instanceId}` + } + + /** + * Get this task's partial stream state, creating it on first use and registering the + * TaskAborted teardown listener exactly once per task. + */ + private getTaskPartialStreamState(task: Task): TaskPartialStreamState { + const key = this.getPartialStreamFailureKey(task) + const existing = this.taskPartialStreamState.get(key) + if (existing) { + return existing + } + + const state: TaskPartialStreamState = { + lastSeenPartialPath: undefined, + streamFailed: false, + streamError: undefined, + task, + abortCleanup: () => this.resetTaskPartialState(task), + } + this.taskPartialStreamState.set(key, state) + task.once(RooCodeEventName.TaskAborted, state.abortCleanup) + return state + } + + private hasPathStabilizedForTask(state: TaskPartialStreamState, partialPath: string | undefined): boolean { + // Stryker disable next-line ConditionalExpression: equivalent mutant: when state.lastSeenPartialPath is undefined the second clause matches only an undefined partialPath and the return's `!!partialPath` rejects it, so both branches return false + const pathHasStabilized = state.lastSeenPartialPath !== undefined && state.lastSeenPartialPath === partialPath + state.lastSeenPartialPath = partialPath + return pathHasStabilized && !!partialPath + } + + /** + * Clear a task's partial-stream state from a disposal path that does not abort first. + * Task.dispose() removes every listener, so a task disposed directly (for example + * ClineProvider.cleanupFailedHistoryTask()) never fires the TaskAborted cleanup and + * this singleton would keep the disposed task and its diff-view provider. + */ + public clearTaskState(task: Task): void { + this.resetTaskPartialState(task) + } + + private resetTaskPartialState(task: Task): void { + const key = this.getPartialStreamFailureKey(task) + const state = this.taskPartialStreamState.get(key) + if (!state) { + return + } + state.task.off(RooCodeEventName.TaskAborted, state.abortCleanup) + this.taskPartialStreamState.delete(key) + } + + /** + * Whether this task's partial stream is still the live one. handlePartial() awaits + * provider state, a filesystem probe and task.ask() before it touches the diff view; a + * cancellation during any of those awaits runs the TaskAborted teardown (or a direct + * clearTaskState), which deletes this entry. Continuing would re-open a diff view and + * re-ask for a task the user already cancelled, resurrecting the state the teardown + * released. Identity, not presence: a re-created entry for the same key belongs to a new + * stream, and this one must not write into it. + */ + private isPartialStreamStillLive(task: Task, state: TaskPartialStreamState): boolean { + return this.taskPartialStreamState.get(this.getPartialStreamFailureKey(task)) === state + } + + private async resetDiffViewAfterWrite(task: Task): Promise { + await task.diffViewProvider.reset().catch((resetError) => { + console.error("Error resetting write_to_file diff view:", resetError) + }) + } + + /** + * Restore the diff editor document to its pre-streaming state and close the view. + * + * reset() clears the provider's state but leaves the diff document dirty with the + * streamed content; a user save would then persist a write the task never completed + * (denied or failed before approval). Must run BEFORE resetDiffViewAfterWrite(), + * since reset() clears the state the rollback relies on. No-op when no diff view is + * open. Failures are logged and reported through the return value: the caller must + * not treat the teardown as complete when this returns false, because the document + * may still hold the unapproved content, but the remaining cleanup (reset, per-task + * state teardown) still runs so the task is not left half-torn-down. + */ + private async revertDiffChangesBeforeReset(task: Task): Promise { + // Every caller of this restores content the user never approved, so neither edit + // type may go through revertChanges(): it SAVES. Its create branch saves the dirty + // buffer - unapproved partial model output - before deleting the file, so a failed + // delete leaves that content on disk; its modify branch restores the original + // content and saves it, which for a .rooignore-denied path is a write the policy + // forbids and for any abandoned stream a write the user never approved. + return this.releaseAbandonedDiffView(task) + } + + private async finalizePartialToolAskAfterFailure(task: Task, text?: string): Promise { + await task.finalizePartialToolAsk(text).catch((finalizeError) => { + console.error("Error finalizing write_to_file partial tool ask:", finalizeError) + }) + } + + /** + * Surface a failed rollback to the user. The hazard has to be visible in the chat, + * not only in the console: the editor may still hold content the task never + * approved and a save of it would land an unauthorized write. A failing say must + * not abort the teardown - a retained streaming error still has to reach the user + * through handleError - so its failure is logged only, matching the other cleanup + * helpers in this file. + */ + private async reportRevertFailure(task: Task): Promise { + await task + .say( + "error", + "write_to_file: the diff editor could not be restored after the failed tool call, so it may still show unapproved content. Do not save that editor.", + ) + .catch((sayError) => { + console.error("Error reporting write_to_file rollback failure:", sayError) + }) + } + + /** + * Cleanup for a failed partial stream: restore the diff document, close the view, + * and - when the restore itself failed - tell the user the editor may still hold + * unapproved content, the same way the parse-failure teardown does. Without the + * report, a failed rollback here is invisible: the stream error that triggered the + * cleanup is a different failure and is reported elsewhere. + */ + private async cleanupFailedPartialStream(task: Task): Promise { + const reverted = await this.revertDiffChangesBeforeReset(task) + await this.resetDiffViewAfterWrite(task) + if (!reverted) { + await this.reportRevertFailure(task) + } + } + + /** + * Teardown boundary for the handle() parse-failure path, where execute() never + * runs and therefore its finally (resetTaskPartialState) never runs either. + * + * Tears down the per-task stream state: otherwise the abort listener leaks for + * the task's lifetime, and when a streaming delta had failed, the streamFailed + * guard would suppress the diff preview of every later write_to_file in this + * task. Restores the diff document: streaming may have opened it with + * unapproved partial content, and execute()'s error cleanup (revert + reset) + * never fires on this path, so a user save could persist the content without + * the teardown here. When a streaming delta already hit a fatal filesystem + * error, that error is what the user can act on, so report it with the same + * "writing file" context execute()'s catch uses, and suppress the incidental + * parse error. + */ + override async onParameterParseFailure(task: Task, callbacks: ToolCallbacks, parseError: Error): Promise { + const state = this.taskPartialStreamState.get(this.getPartialStreamFailureKey(task)) + if (!state) { + return false + } + // Streaming may have opened the diff view with unapproved partial content. + // execute() never runs on this path, so its error cleanup (revert + reset) + // never fires: restore the document here so a user save cannot persist + // content the write never completed (the same invariant the denial and + // streaming-failure paths maintain). Both helpers no-op when no view is open. + // The revert runs BEFORE the per-task state is torn down: when it fails, the + // document can still hold that unapproved content, and the recovery state has + // to exist while the outcome is decided and reported. + const reverted = await this.revertDiffChangesBeforeReset(task) + this.resetTaskPartialState(task) + await this.resetDiffViewAfterWrite(task) + if (!reverted) { + // Do not report a completed teardown: the editor may still show content this + // task never approved, and saving it would land a write the user never + // authorized. The user has to be able to tell that from the UI. + await this.reportRevertFailure(task) + } + if (!state.streamError) { + return false + } + void parseError + await callbacks.handleError("writing file", state.streamError) + return true + } + + /** + * Release the diff view of an abandoned, failed, or denied stream without writing to + * the target file. Neither edit type may go through revertChanges(), because that + * method saves: its create branch persists the dirty buffer (unapproved partial model + * output) before deleting the file, and its modify branch writes the restored original + * content back to a file the user never approved - a write the rooignore policy + * forbids outright for a denied path. discardUnapprovedStream() restores the buffer in + * memory only and removes the artifacts this edit created. + */ + private async releaseAbandonedDiffView(task: Task): Promise { + try { + await task.diffViewProvider.discardUnapprovedStream() + return true + } catch (releaseError) { + console.error("Error releasing the abandoned write_to_file diff view:", releaseError) + return false + } + } + + /** + * Whether an in-flight open() left anything this cleanup can reclaim: the directories it created, the + * placeholder file it wrote, or the editor it opened. Read through bracket notation because the + * provider keeps these private (AGENTS.md). + */ + private hasPublishedDiffArtifacts(task: Task): boolean { + const provider = task.diffViewProvider + // A test double may carry none of these fields, so every read is optional-chained. + return Boolean( + provider["placeholderPath"] || provider["activeDiffEditor"] || (provider["createdDirs"]?.length ?? 0) > 0, + ) // test-double tolerant: fields may be absent + } + + /** + * Own the diff view that open() publishes AFTER cancellation already released this + * stream. The TaskAborted teardown only knows about the view that existed when it ran; + * a create that completes afterwards leaves its placeholder and the directories open() + * made on disk with no owner, and a modify leaves a dirty preview - nothing else in the + * stream path will touch them, because execute() never runs for a cancelled stream. + * + * Idempotent by construction: the discard is a no-op once reset() has cleared the + * provider (no relPath / activeDiffEditor), so a second settle of the same open() - or + * a later delta that also observes the release - cannot roll back twice or report the + * hazard twice. + */ + private async discardDiffViewOpenedAfterRelease(task: Task): Promise { + // The gate cannot be the editing flag: this method exists for the view open() published AFTER the + // cancellation release had already run, and open() creates the directories and the empty placeholder + // before it assigns the editor (DiffViewProvider.open, discardUnapprovedStream), so a create whose + // editor never landed leaves artifacts with the flag never set. Gate on what the open actually + // published: discardUnapprovedStream() clears all three when it runs, so a second observation of the + // same release still finds nothing to do. + if (!this.hasPublishedDiffArtifacts(task)) { + return + } + const released = await this.releaseAbandonedDiffView(task) + await this.resetDiffViewAfterWrite(task) + if (!released) { + // The buffer could not be restored, so the editor may still show content this task + // never approved; the cancellation itself reports its own outcome, not this hazard. + await this.reportRevertFailure(task) + } + } + + /** + * Teardown for the presenter's missing-nativeArgs guard. A finalized write_to_file + * block whose streamed JSON never parsed never reaches handle(): the presenter emits + * the tool_result and breaks, so neither execute()'s finally nor + * onParameterParseFailure() runs. Without this the per-task stream state (map entry, + * TaskAborted listener, and a diff view that streaming may have opened with unapproved + * partial content) outlives the call and leaks into the next API request: a stale path + * makes the next write's first delta look stabilized, and a retained streamFailed flag + * suppresses that write's preview. + * + * Silent for the malformed call itself: the guard has already pushed the tool_result + * the provider waits for, so reporting the call again would double-report it. A FAILED + * ROLLBACK is the exception - the editor may still hold content this task never + * approved, and that hazard has to be visible in the chat exactly as the parse-failure + * and failed-stream teardowns report it. + * + * The open partial ask is finalized here for the same reason: streaming may have created + * it (partial: true), and this exit never reaches the approval or error flow that closes + * asks, so leaving it open keeps the webview spinner running for a call already reported. + */ + async teardownAbandonedStream(task: Task): Promise { + if (!this.taskPartialStreamState.has(this.getPartialStreamFailureKey(task))) { + return + } + + // A streamed delta may already have opened the partial `tool` ask (partial: true). This + // exit bypasses handle()/execute(), so nothing else would finalize it and the webview + // spinner would keep running for a call that has already been reported as failed. The + // missing-parameter, rooignore and parse-failure exits finalize the same ask. + await this.finalizePartialToolAskAfterFailure(task) + const released = await this.releaseAbandonedDiffView(task) + this.resetTaskPartialState(task) + await this.resetDiffViewAfterWrite(task) + if (!released) { + await this.reportRevertFailure(task) + } + } + + override resetPartialState(): void { + super.resetPartialState() + for (const state of this.taskPartialStreamState.values()) { + state.task.off(RooCodeEventName.TaskAborted, state.abortCleanup) + } + this.taskPartialStreamState.clear() + } + async execute(params: WriteToFileParams, task: Task, callbacks: ToolCallbacks): Promise { const { pushToolResult, handleError, askApproval } = callbacks const relPath = params.path @@ -34,7 +372,13 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> { task.consecutiveMistakeCount++ task.recordToolError("write_to_file") pushToolResult(await task.sayAndCreateMissingParamError("write_to_file", "path")) - await task.diffViewProvider.reset() + // handlePartial() has no missing-parameter guard, so streaming deltas for a + // stabilized path may already have created a partial `tool` ask (partial: true) + // before execute() saw the malformed payload. Finalize it so the UI spinner + // does not stay stuck, mirroring the rooignore and execute-error cleanups. + await this.finalizePartialToolAskAfterFailure(task) + await this.cleanupFailedPartialStream(task) + this.resetTaskPartialState(task) return } @@ -42,7 +386,11 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> { task.consecutiveMistakeCount++ task.recordToolError("write_to_file") pushToolResult(await task.sayAndCreateMissingParamError("write_to_file", "content")) - await task.diffViewProvider.reset() + // Same partial-ask cleanup as the missing-`path` branch above: a partial `tool` + // ask created during streaming would otherwise stay open (partial: true). + await this.finalizePartialToolAskAfterFailure(task) + await this.cleanupFailedPartialStream(task) + this.resetTaskPartialState(task) return } @@ -51,6 +399,18 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> { if (!accessAllowed) { await task.say("rooignore_error", relPath) pushToolResult(formatResponse.rooIgnoreError(relPath)) + // handlePartial() has no rooignore guard, so streaming deltas for this denied + // path may already have created a partial `tool` ask (partial: true) and opened + // the diff view before execute() reached the access check. Denying here without + // cleanup would leave the UI spinner stuck (partial: true), the diff view open + // with the denied content still dirty in the editor, and this task's per-task + // stream state leaked. Perform the same cleanup the try/finally path does + // before returning. + await this.finalizePartialToolAskAfterFailure(task) + // The write was denied before approval: restore the document so a user save + // cannot persist the streamed content. + await this.cleanupFailedPartialStream(task) + this.resetTaskPartialState(task) return } @@ -66,12 +426,6 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> { task.diffViewProvider.editType = fileExists ? "modify" : "create" } - // Create parent directories early for new files to prevent ENOENT errors - // in subsequent operations (e.g., diffViewProvider.open, fs.readFile) - if (!fileExists) { - await createDirectoriesForFile(absolutePath) - } - if (newContent.startsWith("```")) { newContent = newContent.split("\n").slice(1).join("\n") } @@ -95,7 +449,19 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> { isProtected: isWriteProtected, } + // Tracks whether the user approved the write, so the error path only reverts the + // diff document when the content was never approved (an approved edit is kept in + // the editor so the user can save it manually after a late failure). + let writeApproved = false + try { + // Create parent directories for new files inside the try block so filesystem + // errors (EROFS, EACCES, etc.) route through handleError with proper cleanup + // and consecutive-mistake counting, rather than escaping unhandled. + if (!fileExists) { + await createDirectoriesForFile(absolutePath) + } + task.consecutiveMistakeCount = 0 const provider = task.providerRef.deref() @@ -129,9 +495,17 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> { const didApprove = await askApproval("tool", completeMessage, undefined, isWriteProtected) if (!didApprove) { + // The prevent-focus branch set editType/originalContent on the provider + // before asking. Clear them on denial (no diff document was opened in + // this branch, so reset() is sufficient; the non-prevent-focus denial + // branch resets through revertChanges()), so a later write re-checks + // the file system instead of reusing the stale editType. + await this.resetDiffViewAfterWrite(task) return } + writeApproved = true + await task.diffViewProvider.saveDirectly(relPath, newContent, false, diagnosticsEnabled, writeDelayMs) } else { if (!task.diffViewProvider.isEditing) { @@ -164,6 +538,8 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> { return } + writeApproved = true + await task.diffViewProvider.saveChanges(diagnosticsEnabled, writeDelayMs) } @@ -177,17 +553,41 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> { pushToolResult(message) - await task.diffViewProvider.reset() - this.resetPartialState() + await this.resetDiffViewAfterWrite(task) task.processQueuedMessages() return } catch (error) { - await handleError("writing file", error as Error) - await task.diffViewProvider.reset() - this.resetPartialState() + // Finalize any open partial tool message so the UI spinner doesn't get stuck. + // The partial ask fired during streaming (handlePartial) or early in execute sets + // partial: true on the webview message; without this, the spinner persists even + // after the error bubble appears. + await this.finalizePartialToolAskAfterFailure(task) + // The diff cleanup runs in a finally around handleError: the production + // handleError awaits Task.say(), which rejects when the task is aborted, and a + // rejected handleError must not skip restoring the unapproved streamed content. + try { + await handleError("writing file", error as Error) + } finally { + // Before approval the diff document holds unapproved streamed content: + // restore it so a user save cannot persist it. After approval the content + // is the user's accepted edit -- keep it in the editor (dirty) so they can + // save it manually. + let reverted = true + if (!writeApproved) { + reverted = await this.revertDiffChangesBeforeReset(task) + } + await this.resetDiffViewAfterWrite(task) + if (!reverted) { + // The restore failed, so the editor still holds content this task never approved. + // The error the user sees on this path is the write failure, not this one. + await this.reportRevertFailure(task) + } + } return + } finally { + this.resetTaskPartialState(task) } } @@ -195,62 +595,168 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> { const relPath: string | undefined = block.params.path const newContent: string | undefined = block.params.content - // Wait for path to stabilize before showing UI (prevents truncated paths) - if (!this.hasPathStabilized(relPath) || newContent === undefined) { + const partialStreamFailureKey = this.getPartialStreamFailureKey(task) + + // A prior streaming delta for this task already hit a fatal filesystem error. + // Skip further streaming work so we don't create a new partial tool message on every + // subsequent delta. execute() will report the error once when the block completes. + if (this.taskPartialStreamState.get(partialStreamFailureKey)?.streamFailed) { return } - const provider = task.providerRef.deref() - const state = await provider?.getState() - const isPreventFocusDisruptionEnabled = experiments.isEnabled( - state?.experiments ?? {}, - EXPERIMENT_IDS.PREVENT_FOCUS_DISRUPTION, - ) + // Get (or create) this task's state; registers the TaskAborted teardown listener + // once, so abandoned streams are torn down even if execute() never runs. + const partialStreamState = this.getTaskPartialStreamState(task) - if (isPreventFocusDisruptionEnabled) { + // Wait for path to stabilize before showing UI (prevents truncated paths) + if (!this.hasPathStabilizedForTask(partialStreamState, relPath) || newContent === undefined) { return } - // relPath is guaranteed non-null after hasPathStabilized - let fileExists: boolean - const absolutePath = path.resolve(task.cwd, relPath!) + // Hoisted out of the guarded setup: the diff-view catch further down finalizes the + // same partial ask, so the message has to stay in scope once the try block ends. + let partialMessage: string | undefined - if (task.diffViewProvider.editType !== undefined) { - fileExists = task.diffViewProvider.editType === "modify" - } else { - fileExists = await fileExistsAtPath(absolutePath) - task.diffViewProvider.editType = fileExists ? "modify" : "create" - } + try { + // Everything from here to the diff view is setup that can fail before + // execute() ever runs; the catch below owns the teardown for that window. + const provider = task.providerRef.deref() + const state = await provider?.getState() - // Create parent directories early for new files to prevent ENOENT errors - // in subsequent operations (e.g., diffViewProvider.open) - if (!fileExists) { - await createDirectoriesForFile(absolutePath) - } + // Cancelled while provider state was in flight: the teardown already + // released this task's stream state. + if (!this.isPartialStreamStillLive(task, partialStreamState)) { + return + } + const isPreventFocusDisruptionEnabled = experiments.isEnabled( + state?.experiments ?? {}, + EXPERIMENT_IDS.PREVENT_FOCUS_DISRUPTION, + ) - const isWriteProtected = task.rooProtectedController?.isWriteProtected(relPath!) || false - const isOutsideWorkspace = isPathOutsideWorkspace(absolutePath) + if (isPreventFocusDisruptionEnabled) { + // The preview is suppressed for this stream: release the entry registered above so the + // abort listener and any failure mark do not outlive a delta that never shows a diff + // view and never reaches execute()'s teardown. + super.resetPartialState() + this.resetTaskPartialState(task) + return + } - const sharedMessageProps: ClineSayTool = { - tool: fileExists ? "editedExistingFile" : "newFileCreated", - path: getReadablePath(task.cwd, relPath!), - content: newContent || "", - isOutsideWorkspace, - isProtected: isWriteProtected, - } + // relPath is guaranteed non-null after hasPathStabilized + let fileExists: boolean + const absolutePath = path.resolve(task.cwd, relPath!) - const partialMessage = JSON.stringify(sharedMessageProps) - await task.ask("tool", partialMessage, block.partial).catch(() => {}) + if (task.diffViewProvider.editType !== undefined) { + fileExists = task.diffViewProvider.editType === "modify" + } else { + fileExists = await fileExistsAtPath(absolutePath) + if (!this.isPartialStreamStillLive(task, partialStreamState)) { + return + } + task.diffViewProvider.editType = fileExists ? "modify" : "create" + } - if (newContent) { - if (!task.diffViewProvider.isEditing) { - await task.diffViewProvider.open(relPath!) + const isWriteProtected = task.rooProtectedController?.isWriteProtected(relPath!) || false + const isOutsideWorkspace = isPathOutsideWorkspace(absolutePath) + + const sharedMessageProps: ClineSayTool = { + tool: fileExists ? "editedExistingFile" : "newFileCreated", + path: getReadablePath(task.cwd, relPath!), + content: newContent || "", + isOutsideWorkspace, + isProtected: isWriteProtected, } - await task.diffViewProvider.update( - everyLineHasLineNumbers(newContent) ? stripLineNumbers(newContent) : newContent, - false, - ) + partialMessage = JSON.stringify(sharedMessageProps) + await task.ask("tool", partialMessage, block.partial).catch(() => {}) + + if (!this.isPartialStreamStillLive(task, partialStreamState)) { + return + } + } catch (error) { + // Unexpected failure in the pre-streaming setup (provider state, the filesystem probe, the + // partial ask): this delta never reaches the diff view or execute(), so nothing else + // releases what the registration above acquired. Drop this task's entry and its + // TaskAborted listener, then rethrow - BaseTool.handle() still reports the error once. + this.resetTaskPartialState(task) + throw error + } + + if (newContent) { + try { + if (!task.diffViewProvider.isEditing) { + await task.diffViewProvider.open(relPath!) + } + + // Cancellation may land while open() is in flight: its abort handler has + // already torn the stream down (and may have reverted or closed this very + // diff view), so streaming the partial content into it now would resurrect a + // view for a task that no longer exists. The view open() just published is now + // this method's responsibility - the teardown ran before it existed. + if (!this.isPartialStreamStillLive(task, partialStreamState)) { + await this.discardDiffViewOpenedAfterRelease(task) + return + } + + await task.diffViewProvider.update( + everyLineHasLineNumbers(newContent) ? stripLineNumbers(newContent) : newContent, + false, + ) + + // Cancellation can land while update() is in flight. The view it streamed into + // already existed when the TaskAborted teardown ran, so that teardown owns it: + // starting a second discard here would roll the same buffer back twice and report + // the hazard twice. Wait for the task's own reversion instead, then drop the + // provider references this released stream still points at. Without the re-check + // the continuation returns as if it still owned a live stream. + if (!this.isPartialStreamStillLive(task, partialStreamState)) { + await task.waitForDiffReversion() + await this.resetDiffViewAfterWrite(task) + return + } + } catch (error) { + // A cancellation that lands while open() or update() is in flight runs the + // TaskAborted teardown - which releases this task's stream state, reverts or + // closes this very diff view, and reports the failure itself - and it can also + // reject the call in flight. Marking the already-released state failed, finalizing + // the ask, or running the failed-stream cleanup a second time would resurrect UI + // and roll back twice for a task the user cancelled, so the teardown owns the + // outcome here. + if (!this.isPartialStreamStillLive(task, partialStreamState)) { + console.error(`Error streaming write_to_file diff view:`, error) + // The teardown ran while this call was in flight, so it could not have released the + // view open() had already published (or left half-published). Discard it here. + await this.discardDiffViewOpenedAfterRelease(task) + return + } + + // Opening or updating the diff view can throw on filesystem errors + // (EACCES/EROFS on read-only paths). Finalize the partial tool message + // so the UI spinner doesn't get stuck and reset the diff view. Do NOT + // rethrow: the same filesystem operation is retried in execute() once the + // block completes, and that authoritative non-partial path reports the + // error to the user. Surfacing it here too would show the same error twice. + // Swallowing it here is safe because the agent loop advances naturally when + // the non-partial block arrives (it does not depend on this throw). + console.error(`Error streaming write_to_file diff view:`, error) + // Mark the stream as failed so later deltas don't re-attempt and spawn a new + // partial tool message each time. Retain the original error: if the final + // block later fails to parse, execute() never runs and only + // onParameterParseFailure() can report this failure to the user. + partialStreamState.streamFailed = true + partialStreamState.streamError = error instanceof Error ? error : new Error(String(error)) + // The ask only exists once the setup above assigned its message; before that there + // is nothing to finalize. + if (partialMessage !== undefined) { + await this.finalizePartialToolAskAfterFailure(task, partialMessage) + } + // The write was never approved: restore the document so a user save cannot + // persist the failed streamed content (reset() alone leaves it dirty), and + // surface the hazard if that restore itself failed. The stream error is + // reported by the authoritative non-partial path in execute(); the rollback + // hazard is a different failure and nothing else in this path says so. + await this.cleanupFailedPartialStream(task) + } } } } diff --git a/src/core/tools/__tests__/writeToFileTool-partial-state-cleanup.spec.ts b/src/core/tools/__tests__/writeToFileTool-partial-state-cleanup.spec.ts new file mode 100644 index 0000000000..0464b55360 --- /dev/null +++ b/src/core/tools/__tests__/writeToFileTool-partial-state-cleanup.spec.ts @@ -0,0 +1,252 @@ +// npx vitest run core/tools/__tests__/writeToFileTool-partial-state-cleanup.spec.ts + +import { RooCodeEventName } from "@roo-code/types" +import { vi, type MockedFunction } from "vitest" + +import { type Task } from "../../task/Task" +import { writeToFileTool } from "../WriteToFileTool" + +// The cleanup primitives only read these members, so a structural double is enough; +// the double assertion is the repo's existing pattern for private-method tests +// (see src/__tests__/removeClineFromStack-delegation.spec.ts). +interface CleanupTask { + taskId: string + instanceId: string + once: MockedFunction<(...args: unknown[]) => unknown> + off: MockedFunction<(...args: unknown[]) => unknown> + diffViewProvider: { + reset: MockedFunction<() => Promise> + revertChanges: MockedFunction<() => Promise> + discardUnapprovedStream: MockedFunction<() => Promise> + editType?: string + } + finalizePartialToolAsk: MockedFunction<() => Promise> + say: MockedFunction<(...args: unknown[]) => Promise> +} + +function buildTask(taskId: string, instanceId: string): Task { + const task: CleanupTask = { + taskId, + instanceId, + once: vi.fn(), + off: vi.fn(), + diffViewProvider: { + reset: vi.fn().mockResolvedValue(undefined), + revertChanges: vi.fn().mockResolvedValue(undefined), + discardUnapprovedStream: vi.fn().mockResolvedValue(undefined), + }, + finalizePartialToolAsk: vi.fn().mockResolvedValue(undefined), + say: vi.fn().mockResolvedValue(undefined), + } + return task as unknown as Task +} + +describe("WriteToFileTool per-task partial-state cleanup", () => { + afterEach(() => { + writeToFileTool["taskPartialStreamState"].clear() + vi.restoreAllMocks() + }) + + it("releases the task state and deregisters the abort listener", async () => { + const task = buildTask("cleanup-task", "inst-1") + const state = writeToFileTool["getTaskPartialStreamState"](task) + expect(writeToFileTool["taskPartialStreamState"].size).toBe(1) + + writeToFileTool.clearTaskState(task) + + expect(writeToFileTool["taskPartialStreamState"].size).toBe(0) + expect((task as unknown as CleanupTask).off).toHaveBeenCalledWith( + RooCodeEventName.TaskAborted, + state.abortCleanup, + ) + }) + + it("is a no-op for a task that never streamed", async () => { + const task = buildTask("never-streamed", "inst-2") + + writeToFileTool.clearTaskState(task) + + expect(writeToFileTool["taskPartialStreamState"].size).toBe(0) + expect((task as unknown as CleanupTask).off).not.toHaveBeenCalled() + }) + + it("logs and continues when resetting the diff view fails", async () => { + const task = buildTask("reset-fails", "inst-3") + const t = task as unknown as CleanupTask + t.diffViewProvider.reset = vi.fn().mockRejectedValue(new Error("reset failed")) + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + + await writeToFileTool["resetDiffViewAfterWrite"](task) + + expect(errorSpy).toHaveBeenCalledWith("Error resetting write_to_file diff view:", expect.any(Error)) + }) + + it("logs and continues when reverting the diff document fails", async () => { + const task = buildTask("revert-fails", "inst-4") + const t = task as unknown as CleanupTask + t.diffViewProvider.discardUnapprovedStream = vi.fn().mockRejectedValue(new Error("revert failed")) + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + + await writeToFileTool["revertDiffChangesBeforeReset"](task) + + expect(errorSpy).toHaveBeenCalledWith( + "Error releasing the abandoned write_to_file diff view:", + expect.any(Error), + ) + }) + + it("reverts while the recovery state exists and reports the hazard when the revert fails", async () => { + const task = buildTask("revert-fails-teardown", "inst-6") + const t = task as unknown as CleanupTask + let stateSizeDuringRevert = -1 + t.diffViewProvider.discardUnapprovedStream = vi.fn(async () => { + // The recovery state must still be present while the rollback runs. + stateSizeDuringRevert = writeToFileTool["taskPartialStreamState"].size + throw new Error("revert failed") + }) + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + // Seed the per-task stream state so this teardown boundary is taken at all. + writeToFileTool["getTaskPartialStreamState"](task) + expect(writeToFileTool["taskPartialStreamState"].size).toBe(1) + + const handled = await writeToFileTool["onParameterParseFailure"]( + task, + // Only handleError is reached when no streaming error was recorded; the + // structural double is the existing pattern in this file. + { handleError: vi.fn().mockResolvedValue(undefined) } as unknown as Parameters< + (typeof writeToFileTool)["onParameterParseFailure"] + >[1], + new Error("parameter parse failed"), + ) + + // A failed rollback is not a completed teardown: the user is told the editor may + // still hold content the task never approved. + expect(t.say).toHaveBeenCalledWith("error", expect.stringContaining("unapproved")) + expect(stateSizeDuringRevert).toBe(1) + // The teardown still finished. + expect(writeToFileTool["taskPartialStreamState"].size).toBe(0) + expect(t.diffViewProvider.reset).toHaveBeenCalled() + expect(handled).toBe(false) + errorSpy.mockRestore() + }) + + it("reports the rollback hazard AND the retained streaming error, and returns true", async () => { + const task = buildTask("revert-fails-with-stream-error", "inst-7") + const t = task as unknown as CleanupTask + t.diffViewProvider.discardUnapprovedStream = vi.fn().mockRejectedValue(new Error("revert failed")) + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + const state = writeToFileTool["getTaskPartialStreamState"](task) + const streamError = new Error("filesystem failure while streaming") + state.streamFailed = true + state.streamError = streamError + const handleError = vi.fn().mockResolvedValue(undefined) + + const handled = await writeToFileTool["onParameterParseFailure"]( + task, + { handleError } as unknown as Parameters<(typeof writeToFileTool)["onParameterParseFailure"]>[1], + new Error("parameter parse failed"), + ) + + // Both reports happen: the rollback hazard and the error the user can act on. + expect(t.say).toHaveBeenCalledWith("error", expect.stringContaining("unapproved")) + expect(handleError).toHaveBeenCalledWith("writing file", streamError) + expect(handled).toBe(true) + errorSpy.mockRestore() + }) + + it("still reports the streaming error when the rollback warning itself fails", async () => { + const task = buildTask("rollback-warning-fails", "inst-8") + const t = task as unknown as CleanupTask + t.diffViewProvider.discardUnapprovedStream = vi.fn().mockRejectedValue(new Error("revert failed")) + t.say = vi.fn().mockRejectedValue(new Error("say failed")) + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + const state = writeToFileTool["getTaskPartialStreamState"](task) + const streamError = new Error("filesystem failure while streaming") + state.streamFailed = true + state.streamError = streamError + const handleError = vi.fn().mockResolvedValue(undefined) + + const handled = await writeToFileTool["onParameterParseFailure"]( + task, + { handleError } as unknown as Parameters<(typeof writeToFileTool)["onParameterParseFailure"]>[1], + new Error("parameter parse failed"), + ) + + // A failing report must not abort the teardown: the streaming error still lands. + expect(errorSpy).toHaveBeenCalledWith("Error reporting write_to_file rollback failure:", expect.any(Error)) + expect(handleError).toHaveBeenCalledWith("writing file", streamError) + expect(handled).toBe(true) + errorSpy.mockRestore() + }) + + it("surfaces the rollback hazard from the failed-stream cleanup as well", async () => { + const task = buildTask("failed-stream-cleanup", "inst-9") + const t = task as unknown as CleanupTask + t.diffViewProvider.discardUnapprovedStream = vi.fn().mockRejectedValue(new Error("revert failed")) + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + + await writeToFileTool["cleanupFailedPartialStream"](task) + + // Same contract as the parse-failure teardown: a failed restore is reported. + expect(t.say).toHaveBeenCalledWith("error", expect.stringContaining("unapproved")) + expect(t.diffViewProvider.reset).toHaveBeenCalled() + errorSpy.mockRestore() + }) + + it("stays silent when the failed-stream cleanup restores the buffer", async () => { + // The hazard report is conditional on the rollback failing. A cleanup that always + // reported "could not be restored" would pass the test above and still spam a false + // warning on every failed stream, so the success path needs its own negative case. + const task = buildTask("failed-stream-cleanup-ok", "inst-12") + const t = task as unknown as CleanupTask + + await writeToFileTool["cleanupFailedPartialStream"](task) + + expect(t.diffViewProvider.discardUnapprovedStream).toHaveBeenCalledTimes(1) + expect(t.diffViewProvider.reset).toHaveBeenCalledTimes(1) + expect(t.say).not.toHaveBeenCalled() + }) + + it("logs and continues when finalizing the open partial ask fails", async () => { + const task = buildTask("finalize-fails", "inst-5") + const t = task as unknown as CleanupTask + t.finalizePartialToolAsk = vi.fn().mockRejectedValue(new Error("finalize failed")) + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + + await writeToFileTool["finalizePartialToolAskAfterFailure"](task, "partial text") + + expect(errorSpy).toHaveBeenCalledWith("Error finalizing write_to_file partial tool ask:", expect.any(Error)) + }) + + it("routes a new-file rollback through the discard path, never through revertChanges", async () => { + // Pre-approval cleanup restores content the user never approved. revertChanges()'s + // new-file branch SAVES that buffer before deleting the file, so a failed delete + // leaves unapproved model output on disk (for a .rooignore-denied path that is a + // write the policy forbids). The discard path never persists it. + const task = buildTask("new-file-rollback", "inst-10") + const t = task as unknown as CleanupTask + t.diffViewProvider.editType = "create" + + const rolledBack = await writeToFileTool["revertDiffChangesBeforeReset"](task) + + expect(t.diffViewProvider.discardUnapprovedStream).toHaveBeenCalledTimes(1) + expect(t.diffViewProvider.revertChanges).not.toHaveBeenCalled() + expect(rolledBack).toBe(true) + }) + + it("routes a modify rollback through the discard path as well, because revertChanges() saves", async () => { + // A modify edit does have prior content on disk, but revertChanges() restores it by + // WRITING: applyEdit plus document.save(). Every caller of this helper restores content + // the user never approved, and for a .rooignore-denied path that write is one the policy + // forbids outright - so both edit types take the in-memory discard. + const task = buildTask("modify-rollback", "inst-11") + const t = task as unknown as CleanupTask + t.diffViewProvider.editType = "modify" + + const rolledBack = await writeToFileTool["revertDiffChangesBeforeReset"](task) + + expect(t.diffViewProvider.discardUnapprovedStream).toHaveBeenCalledTimes(1) + expect(t.diffViewProvider.revertChanges).not.toHaveBeenCalled() + expect(rolledBack).toBe(true) + }) +}) diff --git a/src/core/tools/__tests__/writeToFileTool.spec.ts b/src/core/tools/__tests__/writeToFileTool.spec.ts index 7cb9d01585..16605f774a 100644 --- a/src/core/tools/__tests__/writeToFileTool.spec.ts +++ b/src/core/tools/__tests__/writeToFileTool.spec.ts @@ -1,5 +1,9 @@ +import * as vscode from "vscode" +import fs from "fs/promises" +import os from "os" import * as path from "path" +import { RooCodeEventName } from "@roo-code/types" import type { MockedFunction } from "vitest" import { fileExistsAtPath, createDirectoriesForFile } from "../../../utils/fs" @@ -7,6 +11,7 @@ import { isPathOutsideWorkspace } from "../../../utils/pathUtils" import { getReadablePath } from "../../../utils/path" import { unescapeHtmlEntities } from "../../../utils/text-normalization" import { everyLineHasLineNumbers, stripLineNumbers } from "../../../integrations/misc/extract-text" +import { DiffViewProvider } from "../../../integrations/editor/DiffViewProvider" import { ToolUse, ToolResponse, AskApproval, HandleError, PushToolResult } from "../../../shared/tools" import { writeToFileTool } from "../WriteToFileTool" @@ -45,6 +50,10 @@ vi.mock("../../../utils/pathUtils", () => ({ vi.mock("../../../utils/path", () => ({ getReadablePath: vi.fn().mockReturnValue("test/path.txt"), + // The real DiffViewProvider compares file system paths while closing tabs; the late-open test + // below is the only consumer, and a plain case-insensitive compare is all that path matching is + // used for there. + arePathsEqual: vi.fn((a: string, b: string) => String(a).toLowerCase() === String(b).toLowerCase()), })) vi.mock("../../../utils/text-normalization", () => ({ @@ -66,15 +75,69 @@ vi.mock("../../../integrations/misc/extract-text", () => ({ }), })) +// The members beyond showWarningMessage/openExternal/Uri.parse exist for the one test below that +// drives the real DiffViewProvider through open(); no other test in this file reads them, and they +// are doubles, not a partial extension host. vi.mock("vscode", () => ({ window: { showWarningMessage: vi.fn().mockResolvedValue(undefined), + showTextDocument: vi.fn().mockResolvedValue(undefined), + createTextEditorDecorationType: vi.fn(() => ({ dispose: vi.fn() })), + visibleTextEditors: [], + tabGroups: { + all: [], + close: vi.fn().mockResolvedValue(true), + activeTabGroup: { activeTab: undefined }, + }, + onDidChangeActiveTextEditor: vi.fn(() => ({ dispose: vi.fn() })), + onDidChangeTextEditorSelection: vi.fn(() => ({ dispose: vi.fn() })), + onDidChangeTextEditorVisibleRanges: vi.fn(() => ({ dispose: vi.fn() })), + onDidChangeVisibleTextEditors: vi.fn(() => ({ dispose: vi.fn() })), + }, + workspace: { + textDocuments: [], + applyEdit: vi.fn().mockResolvedValue(true), + openTextDocument: vi.fn().mockResolvedValue({}), + onDidOpenTextDocument: vi.fn(() => ({ dispose: vi.fn() })), + }, + commands: { + executeCommand: vi.fn().mockResolvedValue(undefined), + }, + languages: { + getDiagnostics: vi.fn(() => []), }, env: { openExternal: vi.fn(), }, + Position: class { + constructor( + public line: number, + public character: number, + ) {} + }, + Range: class { + constructor( + public start: unknown, + public end: unknown, + ) {} + }, + Selection: class { + constructor( + public anchor: unknown, + public active: unknown, + ) {} + }, + WorkspaceEdit: class { + replace() {} + delete() {} + }, + TextEditorSelectionChangeKind: { Keyboard: 1, Mouse: 2, Command: 3 }, + TabInputText: class {}, + TabInputTextDiff: class {}, + ViewColumn: { One: 1 }, Uri: { parse: vi.fn(), + file: vi.fn((p: string) => ({ fsPath: p, scheme: "file" })), }, })) @@ -96,6 +159,19 @@ describe("writeToFileTool", () => { const testContent = "Line 1\nLine 2\nLine 3" const testContentWithMarkdown = "```javascript\nLine 1\nLine 2\n```" + // The exact payload handlePartial() streams as the partial `tool` ask for the default + // test scenario (new file, readable path, in-workspace, not write-protected). + // finalizePartialToolAsk() no-ops on a text mismatch, so finalize assertions must + // match this exactly: a weaker matcher (e.g. expect.any(String), which a relPath also + // satisfies) would pass a mutant that passes the wrong text and leaves the spinner stuck. + const expectedPartialToolMessage = JSON.stringify({ + tool: "newFileCreated", + path: "test/path.txt", + content: testContent, + isOutsideWorkspace: false, + isProtected: false, + }) + // Mocked functions with correct types const mockedFileExistsAtPath = fileExistsAtPath as MockedFunction const mockedCreateDirectoriesForFile = createDirectoriesForFile as MockedFunction @@ -118,6 +194,9 @@ describe("writeToFileTool", () => { mockedPathResolve.mockReturnValue(absoluteFilePath) mockedFileExistsAtPath.mockResolvedValue(false) + // vi.clearAllMocks() keeps the last mock implementation; reset the factory default here + // so no test depends on declaration order or an earlier test's rejection. + mockedCreateDirectoriesForFile.mockResolvedValue([]) mockedIsPathOutsideWorkspace.mockReturnValue(false) mockedGetReadablePath.mockReturnValue("test/path.txt") mockedUnescapeHtmlEntities.mockImplementation((content) => { @@ -128,6 +207,8 @@ describe("writeToFileTool", () => { return content }) + mockCline.taskId = "task-1" + mockCline.instanceId = "instance-1" mockCline.cwd = "/" mockCline.consecutiveMistakeCount = 0 mockCline.didEditFile = false @@ -151,6 +232,8 @@ describe("writeToFileTool", () => { update: vi.fn().mockResolvedValue(undefined), reset: vi.fn().mockResolvedValue(undefined), revertChanges: vi.fn().mockResolvedValue(undefined), + discardUnapprovedStream: vi.fn().mockResolvedValue(undefined), + saveDirectly: vi.fn().mockResolvedValue(undefined), saveChanges: vi.fn().mockResolvedValue({ newProblemsMessage: "", userEdits: null, @@ -186,8 +269,12 @@ describe("writeToFileTool", () => { } mockCline.say = vi.fn().mockResolvedValue(undefined) mockCline.ask = vi.fn().mockResolvedValue(undefined) + mockCline.once = vi.fn() + mockCline.off = vi.fn() + mockCline.finalizePartialToolAsk = vi.fn().mockResolvedValue(undefined) mockCline.recordToolError = vi.fn() mockCline.sayAndCreateMissingParamError = vi.fn().mockResolvedValue("Missing param error") + mockCline.processQueuedMessages = vi.fn() mockAskApproval = vi.fn().mockResolvedValue(true) mockHandleError = vi.fn().mockResolvedValue(undefined) @@ -224,9 +311,12 @@ describe("writeToFileTool", () => { ...params, }, nativeArgs: { - path: (params.path ?? testFilePath) as any, - content: (params.content ?? testContent) as any, - }, + // The missing-parameter tests inject `undefined` where + // NativeToolArgs["write_to_file"] declares `string`, so one assertion through + // unknown is what models the malformed payload - no `any` needed. + path: Object.prototype.hasOwnProperty.call(params, "path") ? params.path : testFilePath, + content: Object.prototype.hasOwnProperty.call(params, "content") ? params.content : testContent, + } as unknown as ToolUse<"write_to_file">["nativeArgs"], partial: isPartial, } @@ -251,6 +341,689 @@ describe("writeToFileTool", () => { expect(mockCline.rooIgnoreController.validateAccess).toHaveBeenCalledWith(testFilePath) expect(mockCline.diffViewProvider.open).toHaveBeenCalledWith(testFilePath) }) + + it("finalizes the partial ask and clears per-task state when rooignore denies access", async () => { + // handlePartial() has no rooignore guard, so streaming deltas for a denied path + // still create a partial `tool` ask (partial: true) and open the diff view before + // execute() reaches the access check. The denial must clean up all of that: + // finalize the partial ask (spinner does not stick), revert the diff document so a + // user save cannot persist the denied content, reset the diff view (reset failures + // swallowed), and clear the per-task stream state (abort listener + entries). + const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + try { + let abortCleanup: (() => void) | undefined + mockCline.once.mockImplementation((event: RooCodeEventName, listener: () => void) => { + if (event === RooCodeEventName.TaskAborted) { + abortCleanup = listener + } + return mockCline + }) + // Record the relative order of discardUnapprovedStream() and reset(): vitest mocks expose + // no invocationCallOrder, so the ordering assertion uses this sequence. + const diffViewCallOrder: string[] = [] + mockCline.diffViewProvider.discardUnapprovedStream.mockImplementation(async () => { + diffViewCallOrder.push("revert") + }) + mockCline.diffViewProvider.reset.mockImplementation(async () => { + diffViewCallOrder.push("reset") + throw new Error("reset failed") + }) + + // Stream two deltas so the path stabilizes: handlePartial registers the abort + // cleanup and opens the partial ask + diff view for the (soon denied) path. + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + expect(mockCline.ask).toHaveBeenCalledTimes(1) + expect(mockCline.diffViewProvider.open).toHaveBeenCalledTimes(1) + expect(abortCleanup).toBeTypeOf("function") + + // The completed block now reaches the access check, which denies the path. + await executeWriteFileTool({}, { fileExists: false, accessAllowed: false }) + + expect(mockCline.say).toHaveBeenCalledWith("rooignore_error", testFilePath) + // The denial finalizes without a text match: any open partial tool ask is closed. + expect(mockCline.finalizePartialToolAsk).toHaveBeenCalledWith(undefined) + // The denied write's streamed content must be reverted from the diff document + // BEFORE reset() clears the state discardUnapprovedStream() relies on. + expect(diffViewCallOrder).toEqual(["revert", "reset"]) + expect(mockHandleError).not.toHaveBeenCalled() + expect(consoleErrorSpy).toHaveBeenCalledWith( + "Error resetting write_to_file diff view:", + expect.any(Error), + ) + expect(mockCline.off).toHaveBeenCalledWith(RooCodeEventName.TaskAborted, abortCleanup) + } finally { + consoleErrorSpy.mockRestore() + } + }) + }) + + describe("missing-parameter early-return cleanup", () => { + // handlePartial() has no missing-parameter guard: two partial streaming calls + // stabilize the path and open the partial `tool` ask + diff view. This establishes + // the "partial ask is open" precondition for the missing-parameter branches below. + async function streamPartialAsk() { + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + expect(mockCline.ask).toHaveBeenCalledTimes(1) + } + + it("finalizes the partial ask when content is missing after partial streaming", async () => { + // Streaming deltas create a partial `tool` ask (partial: true), then the completed + // payload is missing `content`. The missing-parameter branch must finalize the ask + // (the spinner must not stick) and still perform the same diff-view revert / reset + // and per-task-state cleanup as the other early-return paths. + // Record the relative order of discardUnapprovedStream() and reset(): vitest mocks expose + // no invocationCallOrder, so the ordering assertion uses this sequence. The + // revert mock awaits a deferred so the test proves the branch AWAITs + // discardUnapprovedStream() before reset(): with the revert still pending, reset() must + // not have run yet. + const diffViewCallOrder: string[] = [] + let resolveRevert: () => void = () => {} + const revertDeferred = new Promise((resolve) => { + resolveRevert = resolve + }) + mockCline.diffViewProvider.discardUnapprovedStream.mockImplementation(async () => { + diffViewCallOrder.push("revert") + await revertDeferred + }) + mockCline.diffViewProvider.reset.mockImplementation(async () => { + diffViewCallOrder.push("reset") + }) + let abortCleanup: (() => void) | undefined + mockCline.once.mockImplementation((event: RooCodeEventName, listener: () => void) => { + if (event === RooCodeEventName.TaskAborted) { + abortCleanup = listener + } + return mockCline + }) + await streamPartialAsk() + + // The missing-parameter branch awaits discardUnapprovedStream() before reset(): with the + // deferred revert still pending, reset() must not have run yet. + const executePromise = executeWriteFileTool({ content: undefined }, { fileExists: false }) + await new Promise((resolve) => setTimeout(resolve, 0)) + + expect(mockCline.diffViewProvider.discardUnapprovedStream).toHaveBeenCalledTimes(1) + expect(mockCline.diffViewProvider.reset).not.toHaveBeenCalled() + + resolveRevert() + await executePromise + + expect(mockCline.sayAndCreateMissingParamError).toHaveBeenCalledWith("write_to_file", "content") + // The missing-parameter path finalizes without a text match: any open partial ask is closed. + expect(mockCline.finalizePartialToolAsk).toHaveBeenCalledWith(undefined) + // The streamed content of the failed write must be reverted from the diff + // document before reset() clears the state discardUnapprovedStream() relies on. + expect(diffViewCallOrder).toEqual(["revert", "reset"]) + expect(mockHandleError).not.toHaveBeenCalled() + // The per-task stream state must be gone, not just the diff view: the entry and its + // TaskAborted listener would otherwise live for the rest of the task. + expect(writeToFileTool["taskPartialStreamState"].size).toBe(0) + expect(mockCline.off).toHaveBeenCalledWith(RooCodeEventName.TaskAborted, abortCleanup) + }) + + it("finalizes the partial ask when path is missing after partial streaming", async () => { + // Same scenario with the `path` field missing: the missing-`path` branch must run + // the identical partial-ask + diff-view + per-task-state cleanup. As in the + // content-missing test above, the revert mock awaits a deferred so the test + // proves the branch AWAITs discardUnapprovedStream() before reset(): with the revert + // still pending, reset() must not have run yet. + const diffViewCallOrder: string[] = [] + let resolveRevert: () => void = () => {} + const revertDeferred = new Promise((resolve) => { + resolveRevert = resolve + }) + mockCline.diffViewProvider.discardUnapprovedStream.mockImplementation(async () => { + diffViewCallOrder.push("revert") + await revertDeferred + }) + mockCline.diffViewProvider.reset.mockImplementation(async () => { + diffViewCallOrder.push("reset") + }) + let abortCleanup: (() => void) | undefined + mockCline.once.mockImplementation((event: RooCodeEventName, listener: () => void) => { + if (event === RooCodeEventName.TaskAborted) { + abortCleanup = listener + } + return mockCline + }) + await streamPartialAsk() + + // The missing-parameter branch awaits discardUnapprovedStream() before reset(): with the + // deferred revert still pending, reset() must not have run yet. + const executePromise = executeWriteFileTool({ path: undefined }, { fileExists: false }) + await new Promise((resolve) => setTimeout(resolve, 0)) + + expect(mockCline.diffViewProvider.discardUnapprovedStream).toHaveBeenCalledTimes(1) + expect(mockCline.diffViewProvider.reset).not.toHaveBeenCalled() + + resolveRevert() + await executePromise + + expect(mockCline.sayAndCreateMissingParamError).toHaveBeenCalledWith("write_to_file", "path") + expect(mockCline.finalizePartialToolAsk).toHaveBeenCalledWith(undefined) + // The diff document must be reverted before reset() clears the state + // discardUnapprovedStream() relies on. + expect(diffViewCallOrder).toEqual(["revert", "reset"]) + expect(mockHandleError).not.toHaveBeenCalled() + // The per-task stream state must be gone, not just the diff view: the entry and its + // TaskAborted listener would otherwise live for the rest of the task. + expect(writeToFileTool["taskPartialStreamState"].size).toBe(0) + expect(mockCline.off).toHaveBeenCalledWith(RooCodeEventName.TaskAborted, abortCleanup) + }) + }) + + describe("abandoned-stream teardown (presenter missing-nativeArgs guard)", () => { + it("releases the per-task stream state when the finalized block never reaches handle()", async () => { + // Streaming JSON that never parses: Task marks the block complete with nativeArgs + // undefined, presentAssistantMessage pushes the error tool_result and breaks, so + // neither execute()'s finally nor onParameterParseFailure() runs. The presenter + // routes the abandoned block here instead; without it the entry, the TaskAborted + // listener and the streamed diff view survive into the next API request. + let abortCleanup: (() => void) | undefined + mockCline.once.mockImplementation((event: RooCodeEventName, listener: () => void) => { + if (event === RooCodeEventName.TaskAborted) { + abortCleanup = listener + } + return mockCline + }) + const diffViewCallOrder: string[] = [] + mockCline.diffViewProvider.discardUnapprovedStream = vi.fn(async () => { + diffViewCallOrder.push("discard") + }) + mockCline.diffViewProvider.revertChanges.mockImplementation(async () => { + diffViewCallOrder.push("revert") + }) + mockCline.diffViewProvider.reset.mockImplementation(async () => { + diffViewCallOrder.push("reset") + }) + + // Two deltas stabilize the path: the entry, the listener, the partial ask and the + // diff view all exist before the block is finalized. + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + expect(writeToFileTool["taskPartialStreamState"].size).toBe(1) + expect(abortCleanup).toBeTypeOf("function") + + await writeToFileTool.teardownAbandonedStream(mockCline) + + // The streamed content is reverted before the view is reset, and the state is gone. + // A new-file stream was never approved: the buffer must be discarded rather than + // reverted, because revertChanges() would SAVE the partial content before deleting + // the file. The view is still reset and the state released. + expect(diffViewCallOrder).toEqual(["discard", "reset"]) + expect(mockCline.diffViewProvider.revertChanges).not.toHaveBeenCalled() + expect(writeToFileTool["taskPartialStreamState"].size).toBe(0) + expect(mockCline.off).toHaveBeenCalledWith(RooCodeEventName.TaskAborted, abortCleanup) + // The partial `tool` ask the deltas opened is finalized: this exit bypasses + // handle()/execute(), so nothing else closes it and the webview spinner would keep + // running for a call already reported as failed. + expect(mockCline.finalizePartialToolAsk).toHaveBeenCalledTimes(1) + // The malformed call itself is not re-reported (the guard already emitted the + // tool_result), and a successful rollback has no hazard to surface. + expect(mockHandleError).not.toHaveBeenCalled() + expect(mockCline.sayAndCreateMissingParamError).not.toHaveBeenCalled() + expect(mockCline.say).not.toHaveBeenCalledWith("error", expect.stringContaining("could not be restored")) + }) + + it("reports the rollback hazard when the streamed content cannot be reverted", async () => { + // Same contract as cleanupFailedPartialStream: the state is still released, but the + // user has to see that the editor may still hold unapproved content. + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + expect(writeToFileTool["taskPartialStreamState"].size).toBe(1) + mockCline.diffViewProvider.discardUnapprovedStream = vi.fn(async () => { + throw new Error("discard failed") + }) + + await writeToFileTool.teardownAbandonedStream(mockCline) + + expect(writeToFileTool["taskPartialStreamState"].size).toBe(0) + expect(mockCline.diffViewProvider.reset).toHaveBeenCalled() + expect(mockCline.say).toHaveBeenCalledWith("error", expect.stringContaining("could not be restored")) + }) + + it("discards a modify teardown too, because revertChanges() would save it", async () => { + // revertChanges() restores the original content and SAVES it. For a stream the user + // never approved that is a write they never asked for - and for a rooignore-denied + // path a write the policy forbids - so both edit types take the in-memory discard. + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + mockCline.diffViewProvider.editType = "modify" + mockCline.diffViewProvider.discardUnapprovedStream = vi.fn().mockResolvedValue(undefined) + mockCline.diffViewProvider.revertChanges.mockClear() + + await writeToFileTool.teardownAbandonedStream(mockCline) + + expect(mockCline.diffViewProvider.discardUnapprovedStream).toHaveBeenCalledTimes(1) + expect(mockCline.diffViewProvider.revertChanges).not.toHaveBeenCalled() + expect(writeToFileTool["taskPartialStreamState"].size).toBe(0) + }) + + it("stops before touching the diff view when the stream state is released during an in-flight await", async () => { + // A cancellation while task.ask() is in flight runs the TaskAborted teardown. The + // delta that was already in flight must not then re-open the diff view for a task + // the user cancelled - that resurrects the state the teardown just released. + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + expect(writeToFileTool["taskPartialStreamState"].size).toBe(1) + mockCline.diffViewProvider.open.mockClear() + mockCline.diffViewProvider.update.mockClear() + mockCline.ask.mockImplementation(async () => { + writeToFileTool.clearTaskState(mockCline) + }) + + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + + expect(mockCline.diffViewProvider.open).not.toHaveBeenCalled() + expect(mockCline.diffViewProvider.update).not.toHaveBeenCalled() + expect(writeToFileTool["taskPartialStreamState"].size).toBe(0) + }) + + it("stops before updating the diff view when the task is cancelled while open() is in flight", async () => { + // open() is the first provider await after the ask. If TaskAborted lands while + // it is in flight, the teardown has already released this task's stream state + // (and may have reverted or closed this very view), so the in-flight delta must + // not stream partial content into a view for a task the user cancelled. + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + expect(writeToFileTool["taskPartialStreamState"].size).toBe(1) + expect(mockCline.diffViewProvider.open).toHaveBeenCalledTimes(1) + mockCline.diffViewProvider.open.mockClear() + mockCline.diffViewProvider.update.mockClear() + mockCline.diffViewProvider.open.mockImplementationOnce(async () => { + writeToFileTool.clearTaskState(mockCline) + }) + + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + + expect(mockCline.diffViewProvider.open).toHaveBeenCalledTimes(1) + expect(mockCline.diffViewProvider.update).not.toHaveBeenCalled() + expect(writeToFileTool["taskPartialStreamState"].size).toBe(0) + }) + + it("waits for the task's own discard instead of starting a second one when the abort lands during update()", async () => { + // The view update() streams into already existed when the TaskAborted teardown ran, so + // that teardown owns it. Without a re-check after the await the continuation returns as + // if it still owned a live stream; with an independent discard it would roll the same + // buffer back twice and report the hazard twice. + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + expect(writeToFileTool["taskPartialStreamState"].size).toBe(1) + mockCline.diffViewProvider.isEditing = true + mockCline.diffViewProvider.update.mockClear() + mockCline.diffViewProvider.discardUnapprovedStream.mockClear() + mockCline.diffViewProvider.revertChanges.mockClear() + mockCline.diffViewProvider.reset.mockClear() + mockCline.waitForDiffReversion = vi.fn().mockResolvedValue(undefined) + // The cancellation lands inside update(), the last provider await of the delta. + mockCline.diffViewProvider.update.mockImplementationOnce(async () => { + writeToFileTool.clearTaskState(mockCline) + }) + + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + + expect(mockCline.diffViewProvider.update).toHaveBeenCalledTimes(1) + // Single owner: the continuation waits for the discard the disposal already started. + expect(mockCline.waitForDiffReversion).toHaveBeenCalledTimes(1) + expect(mockCline.diffViewProvider.discardUnapprovedStream).not.toHaveBeenCalled() + expect(mockCline.diffViewProvider.revertChanges).not.toHaveBeenCalled() + // It still drops the provider references the released stream points at. + expect(mockCline.diffViewProvider.reset).toHaveBeenCalledTimes(1) + expect(writeToFileTool["taskPartialStreamState"].size).toBe(0) + }) + it("discards the diff view that open() publishes after the stream state was released", async () => { + // The TaskAborted teardown can only release the view that existed when it ran. When open() + // completes AFTER the release it publishes a view nobody owns any more: for a create that + // is the placeholder plus the directories open() wrote to disk, and execute() never runs + // for a cancelled stream, so nothing else would ever remove them. + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + mockCline.diffViewProvider.open.mockClear() + mockCline.diffViewProvider.update.mockClear() + mockCline.diffViewProvider.discardUnapprovedStream.mockClear() + mockCline.diffViewProvider.revertChanges.mockClear() + mockCline.diffViewProvider.reset.mockClear() + // open() is only called when no view is open yet, so the flag starts clear and open() + // itself publishes the view - the real ordering, where the cancellation lands inside the + // window open() is in flight. + mockCline.diffViewProvider.isEditing = false + mockCline.diffViewProvider.open.mockImplementationOnce(async () => { + // What a create's open() publishes before it ever assigns the editor: the directories it made + // and the empty placeholder. The late cleanup below gates on these, not on the editing flag. + mockCline.diffViewProvider["createdDirs"] = ["mock-dir"] + mockCline.diffViewProvider["placeholderPath"] = "mock-file" + mockCline.diffViewProvider.isEditing = true + writeToFileTool.clearTaskState(mockCline) + }) + + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + + expect(mockCline.diffViewProvider.update).not.toHaveBeenCalled() + // The delta that lost the race owns the view open() just published, and releasing it must + // never go through revertChanges(): that saves. + expect(mockCline.diffViewProvider.discardUnapprovedStream).toHaveBeenCalledTimes(1) + expect(mockCline.diffViewProvider.revertChanges).not.toHaveBeenCalled() + expect(mockCline.diffViewProvider.reset).toHaveBeenCalledTimes(1) + expect(writeToFileTool["taskPartialStreamState"].size).toBe(0) + }) + it("reclaims the placeholder and directories an in-flight open published with no editor and no editing flag", async () => { + // The ask: the late cleanup must not skip what open() published. open() creates the directories and + // the empty placeholder BEFORE it assigns the editor, so an open that lands after the cancellation + // release can leave them behind with isEditing never set, and execute() never runs for a cancelled + // stream, so nothing else would ever remove them. + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + mockCline.diffViewProvider.open.mockClear() + mockCline.diffViewProvider.discardUnapprovedStream.mockClear() + mockCline.diffViewProvider.revertChanges.mockClear() + mockCline.diffViewProvider.reset.mockClear() + mockCline.say.mockClear() + mockCline.diffViewProvider.isEditing = false + // The artifacts the in-flight open published, with no editor and no editing flag. + mockCline.diffViewProvider["createdDirs"] = ["mock-dir"] + mockCline.diffViewProvider["placeholderPath"] = "mock-file" + mockCline.diffViewProvider["activeDiffEditor"] = undefined + mockCline.diffViewProvider.open.mockImplementationOnce(async () => { + // The cancellation teardown ran inside the window open() was in flight, which is what clears + // the stream state out from under the delta. + writeToFileTool.clearTaskState(mockCline) + }) + + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + + // The late cleanup read what open published instead of the editing flag, and reclaimed it once. + expect(mockCline.diffViewProvider.discardUnapprovedStream).toHaveBeenCalledTimes(1) + expect(mockCline.diffViewProvider.revertChanges).not.toHaveBeenCalled() + expect(mockCline.diffViewProvider.reset).toHaveBeenCalledTimes(1) + // A clean release reports no hazard. + expect(mockCline.say).not.toHaveBeenCalled() + expect(writeToFileTool["taskPartialStreamState"].size).toBe(0) + }) + + it("reclaims the diff view an in-flight open published as only the placeholder file", async () => { + // Single-artifact case: the all-three test above passes even if two of the presence check's three operands + // stop working, because any one truthy field is enough to run the cleanup. Each of the three artifacts needs + // its own case so that the placeholder path alone is what the gate is actually reading. + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + mockCline.diffViewProvider.open.mockClear() + mockCline.diffViewProvider.discardUnapprovedStream.mockClear() + mockCline.diffViewProvider.revertChanges.mockClear() + mockCline.diffViewProvider.reset.mockClear() + mockCline.say.mockClear() + mockCline.diffViewProvider.isEditing = false + mockCline.diffViewProvider["placeholderPath"] = "mock-file" + mockCline.diffViewProvider["activeDiffEditor"] = undefined + mockCline.diffViewProvider["createdDirs"] = [] + mockCline.diffViewProvider.open.mockImplementationOnce(async () => { + // The cancellation teardown ran inside the window open() was in flight, which is what clears + // the stream state out from under the delta. + writeToFileTool.clearTaskState(mockCline) + }) + + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + + expect(mockCline.diffViewProvider.discardUnapprovedStream).toHaveBeenCalledTimes(1) + expect(mockCline.diffViewProvider.revertChanges).not.toHaveBeenCalled() + expect(mockCline.diffViewProvider.reset).toHaveBeenCalledTimes(1) + expect(mockCline.say).not.toHaveBeenCalled() + expect(writeToFileTool["taskPartialStreamState"].size).toBe(0) + }) + + it("reclaims the diff view an in-flight open published as only the diff editor", async () => { + // Single-artifact case: the all-three test above passes even if two of the presence check's three operands + // stop working, because any one truthy field is enough to run the cleanup. Each of the three artifacts needs + // its own case so that the editor reference alone is what the gate is actually reading. + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + mockCline.diffViewProvider.open.mockClear() + mockCline.diffViewProvider.discardUnapprovedStream.mockClear() + mockCline.diffViewProvider.revertChanges.mockClear() + mockCline.diffViewProvider.reset.mockClear() + mockCline.say.mockClear() + mockCline.diffViewProvider.isEditing = false + mockCline.diffViewProvider["placeholderPath"] = undefined + mockCline.diffViewProvider["activeDiffEditor"] = { hide: vi.fn() } + mockCline.diffViewProvider["createdDirs"] = [] + mockCline.diffViewProvider.open.mockImplementationOnce(async () => { + // The cancellation teardown ran inside the window open() was in flight, which is what clears + // the stream state out from under the delta. + writeToFileTool.clearTaskState(mockCline) + }) + + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + + expect(mockCline.diffViewProvider.discardUnapprovedStream).toHaveBeenCalledTimes(1) + expect(mockCline.diffViewProvider.revertChanges).not.toHaveBeenCalled() + expect(mockCline.diffViewProvider.reset).toHaveBeenCalledTimes(1) + expect(mockCline.say).not.toHaveBeenCalled() + expect(writeToFileTool["taskPartialStreamState"].size).toBe(0) + }) + + it("reclaims the diff view an in-flight open published as only the directories it created", async () => { + // Single-artifact case: the all-three test above passes even if two of the presence check's three operands + // stop working, because any one truthy field is enough to run the cleanup. Each of the three artifacts needs + // its own case so that the created-directories list alone is what the gate is actually reading. + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + mockCline.diffViewProvider.open.mockClear() + mockCline.diffViewProvider.discardUnapprovedStream.mockClear() + mockCline.diffViewProvider.revertChanges.mockClear() + mockCline.diffViewProvider.reset.mockClear() + mockCline.say.mockClear() + mockCline.diffViewProvider.isEditing = false + mockCline.diffViewProvider["placeholderPath"] = undefined + mockCline.diffViewProvider["activeDiffEditor"] = undefined + mockCline.diffViewProvider["createdDirs"] = ["mock-dir"] + mockCline.diffViewProvider.open.mockImplementationOnce(async () => { + // The cancellation teardown ran inside the window open() was in flight, which is what clears + // the stream state out from under the delta. + writeToFileTool.clearTaskState(mockCline) + }) + + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + + expect(mockCline.diffViewProvider.discardUnapprovedStream).toHaveBeenCalledTimes(1) + expect(mockCline.diffViewProvider.revertChanges).not.toHaveBeenCalled() + expect(mockCline.diffViewProvider.reset).toHaveBeenCalledTimes(1) + expect(mockCline.say).not.toHaveBeenCalled() + expect(writeToFileTool["taskPartialStreamState"].size).toBe(0) + }) + + it("reclaims nothing when an in-flight open published no artifact at all", async () => { + // Control on top of the three single-artifact cases: with nothing published the cleanup must stay out of the + // way, and it must stay out of the way without reading a length off a field the provider double does not + // carry. The three fields are deleted rather than set to undefined, because the real DiffViewProvider + // declares them as fields that may hold nothing at all (placeholderPath is optional, and a double that + // never went through open() has no such property), which is what the optional chain is there for. + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + mockCline.diffViewProvider.open.mockClear() + mockCline.diffViewProvider.discardUnapprovedStream.mockClear() + mockCline.diffViewProvider.revertChanges.mockClear() + mockCline.diffViewProvider.reset.mockClear() + mockCline.say.mockClear() + mockCline.diffViewProvider.isEditing = false + Reflect.deleteProperty(mockCline.diffViewProvider, "placeholderPath") + Reflect.deleteProperty(mockCline.diffViewProvider, "activeDiffEditor") + Reflect.deleteProperty(mockCline.diffViewProvider, "createdDirs") + mockCline.diffViewProvider.open.mockImplementationOnce(async () => { + // The cancellation teardown ran inside the window open() was in flight, which is what clears + // the stream state out from under the delta. + writeToFileTool.clearTaskState(mockCline) + }) + + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + + expect(mockCline.diffViewProvider.discardUnapprovedStream).not.toHaveBeenCalled() + expect(mockCline.diffViewProvider.revertChanges).not.toHaveBeenCalled() + expect(mockCline.diffViewProvider.reset).not.toHaveBeenCalled() + expect(mockCline.say).not.toHaveBeenCalled() + expect(writeToFileTool["taskPartialStreamState"].size).toBe(0) + }) + + it("leaves no listener, controller or editor behind when a cancelled stream races a real open()", async () => { + // The second test the review asks for, at the tool layer. The provider is the real + // DiffViewProvider over a real temporary directory, the tool calls the real open(), and the + // placeholder is a real file. Only the innermost openDiffEditor() is deferred, because a real + // diff document needs the extension host, and the editor handed back afterwards is a structural + // double: this is a real call path over doubles, not a real extension host. + const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "zoo-late-open-")) + const relPath = "late-open.txt" + const absolutePath = path.join(tempDir, relPath) + try { + // The shared setup pins path.resolve to one fixed path; the real provider needs the + // temporary directory instead, so restore the plain join behaviour for this test. + mockedPathResolve.mockImplementation((...args: string[]) => args.join(path.sep)) + const provider = new DiffViewProvider(tempDir, mockCline) + mockCline.diffViewProvider = provider + let editorCallStarted = false + let handOverEditor: (editor: vscode.TextEditor) => void = () => {} + const pendingEditorCall = new Promise((resolve) => { + handOverEditor = resolve + }) + // Bracket access rather than a cast: openDiffEditor is private, and deferring it is the + // only way to hold open() inside the await the cancellation is racing. The flag is set on + // entry: open() has already written the placeholder by then, so the flag - not isEditing, + // which open() sets eight lines earlier - is what proves the call is parked. + provider["openDiffEditor"] = async () => { + editorCallStarted = true + return pendingEditorCall + } + + const running = executeWriteFileTool({ path: relPath }, { fileExists: false }) + for (let tick = 0; tick < 200 && !editorCallStarted; tick++) { + await new Promise((resolve) => setTimeout(resolve, 0)) + } + expect(editorCallStarted).toBe(true) + + // The tool is inside open() and parked on the deferred editor call. The placeholder is a + // real empty file in the real directory, and nothing has been published yet. + expect(provider.isEditing).toBe(true) + expect(await fs.readFile(absolutePath, "utf-8")).toBe("") + expect(vscode.window.onDidChangeTextEditorSelection).not.toHaveBeenCalled() + + // The release the abort path performs while open() is still parked. + await provider.discardUnapprovedStream() + handOverEditor({ + document: { uri: { scheme: "file", fsPath: absolutePath }, isDirty: false, lineCount: 0 }, + } as unknown as vscode.TextEditor) + await running + + // Nothing survived the late publish: no listener was registered, no decoration type was + // created, the editor reference is gone, and the streaming session is closed. + expect(vscode.window.onDidChangeActiveTextEditor).not.toHaveBeenCalled() + expect(vscode.window.onDidChangeTextEditorSelection).not.toHaveBeenCalled() + expect(vscode.window.onDidChangeTextEditorVisibleRanges).not.toHaveBeenCalled() + expect(vscode.window.createTextEditorDecorationType).not.toHaveBeenCalled() + expect(provider["activeDiffEditor"]).toBeUndefined() + expect(provider["activeLineController"]).toBeUndefined() + expect(provider["fadedOverlayController"]).toBeUndefined() + expect(provider["deferredScrollTimer"]).toBeUndefined() + expect(provider.isEditing).toBe(false) + // The discard removed the real placeholder open() wrote, so nothing of this aborted + // session is left on disk either. + const placeholderState = await fs.readFile(absolutePath, "utf-8").then( + () => "present", + // The cast reads the error code off a rejected fs call, the same way the provider reads + // it at the unlink site it replaces. + (error: unknown) => + (error as NodeJS.ErrnoException)?.code === "ENOENT" ? "removed" : "other error", + ) + expect(placeholderState).toBe("removed") + expect(writeToFileTool["taskPartialStreamState"].size).toBe(0) + } finally { + await fs.rm(tempDir, { recursive: true, force: true }) + } + }) + + it("does not roll back twice when a released stream is observed a second time", async () => { + // Idempotency: the discard is a no-op once reset() has cleared the provider, so a second + // observation of the same release cannot roll back again or report a hazard that never + // happened. reset() is what clears isEditing, which is the guard's only input. + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + mockCline.diffViewProvider.open.mockClear() + mockCline.diffViewProvider.discardUnapprovedStream.mockClear() + // Bound to the real cleanup instead of to clearing this test performs by hand. The double keeps its + // editor and filesystem boundaries - no editor is installed, so the discard never reaches the buffer + // and tab work - and the field clearing a second observation depends on is the clearing + // DiffViewProvider.discardUnapprovedStream() itself performs. Take a clear out of that method and + // this row stops passing, which is what the double's own clearing could not do. + mockCline.diffViewProvider.discardUnapprovedStream.mockImplementation(async () => { + await DiffViewProvider.prototype.discardUnapprovedStream.call(mockCline.diffViewProvider) + }) + mockCline.diffViewProvider.reset.mockClear() + mockCline.say.mockClear() + mockCline.diffViewProvider.isEditing = false + mockCline.diffViewProvider.reset.mockImplementation(async () => { + mockCline.diffViewProvider.isEditing = false + }) + mockCline.diffViewProvider.open.mockImplementationOnce(async () => { + // Same: what a create's open() publishes before the editor lands. The first cleanup reclaims these, + // and reclaiming them is what leaves nothing for the second observation. + mockCline.diffViewProvider["createdDirs"] = ["mock-dir"] + mockCline.diffViewProvider["placeholderPath"] = "mock-file" + // A real open() also knows which file it opened and in which workspace - the constructor and + // open() publish both before any cleanup can run - and the production cleanup resolves the + // path it is about to remove from them, so the double carries them for it. + mockCline.diffViewProvider["relPath"] = "mock-file" + mockCline.diffViewProvider["cwd"] = process.cwd() + mockCline.diffViewProvider.isEditing = true + writeToFileTool.clearTaskState(mockCline) + }) + + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + // A later settle observes the same release with nothing left to release. + await writeToFileTool["discardDiffViewOpenedAfterRelease"](mockCline) + + expect(mockCline.diffViewProvider.discardUnapprovedStream).toHaveBeenCalledTimes(1) + expect(mockCline.say).not.toHaveBeenCalled() + }) + it("does not finalize the ask or roll back twice when open() rejects after a cancellation", async () => { + // A cancellation during open() releases the stream state through the TaskAborted + // teardown (which also reverts or closes this diff view) AND rejects the call in + // flight. The catch used to mark the released state failed, finalize the ask and run + // the failed-stream cleanup again - a second rollback and a fresh ask row for a task + // the user already cancelled. + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + expect(writeToFileTool["taskPartialStreamState"].size).toBe(1) + mockCline.diffViewProvider.open.mockClear() + mockCline.diffViewProvider.update.mockClear() + mockCline.diffViewProvider.revertChanges.mockClear() + mockCline.diffViewProvider.discardUnapprovedStream.mockClear() + mockCline.ask.mockClear() + mockCline.finalizePartialToolAsk.mockClear() + mockCline.diffViewProvider.open.mockImplementationOnce(async () => { + writeToFileTool.clearTaskState(mockCline) + throw new Error("EACCES: permission denied, open mock-file") + }) + + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + + expect(mockCline.diffViewProvider.update).not.toHaveBeenCalled() + // Only the partial ask this delta issued: no finalize on top of it. Asserted on the + // finalize mock itself - finalizePartialToolAskAfterFailure() calls finalizePartialToolAsk, + // not ask(), so the ask count alone cannot see a regression there. + expect(mockCline.ask).toHaveBeenCalledTimes(1) + expect(mockCline.finalizePartialToolAsk).not.toHaveBeenCalled() + // The teardown that already ran owns the rollback: no second one from the catch. + expect(mockCline.diffViewProvider.discardUnapprovedStream).not.toHaveBeenCalled() + expect(mockCline.diffViewProvider.revertChanges).not.toHaveBeenCalled() + expect(writeToFileTool["taskPartialStreamState"].size).toBe(0) + }) + it("does nothing when the task has no stream state to release", async () => { + await writeToFileTool.teardownAbandonedStream(mockCline) + // The guard comes first: no delta registered state, so there is no ask it could have + // opened either - finalizing here would close an unrelated partial ask. + expect(mockCline.finalizePartialToolAsk).not.toHaveBeenCalled() + expect(mockCline.diffViewProvider.revertChanges).not.toHaveBeenCalled() + expect(mockCline.diffViewProvider.reset).not.toHaveBeenCalled() + expect(mockCline.off).not.toHaveBeenCalled() + }) }) describe("file existence detection", () => { @@ -288,15 +1061,16 @@ describe("writeToFileTool", () => { ) it.skipIf(process.platform === "win32")( - "creates parent directories when path has stabilized (partial)", + "does not create directories in handlePartial -- only execute() creates them", async () => { - // First call - path not yet stabilized + // First call - path not yet stabilized, early return await executeWriteFileTool({}, { fileExists: false, isPartial: true }) expect(mockedCreateDirectoriesForFile).not.toHaveBeenCalled() - // Second call with same path - path is now stabilized + // Second call with same path - path stabilized, handlePartial runs but + // must NOT call createDirectoriesForFile (directory creation belongs in execute) await executeWriteFileTool({}, { fileExists: false, isPartial: true }) - expect(mockedCreateDirectoriesForFile).toHaveBeenCalledWith(absoluteFilePath) + expect(mockedCreateDirectoriesForFile).not.toHaveBeenCalled() }, ) @@ -393,6 +1167,25 @@ describe("writeToFileTool", () => { // Should process normally without issues expect(mockCline.consecutiveMistakeCount).toBe(0) }) + + it("does not report a successful write as failed when final diff reset rejects", async () => { + const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + try { + mockCline.diffViewProvider.reset.mockRejectedValue(new Error("reset failed")) + + await executeWriteFileTool({}, { fileExists: false }) + + expect(mockHandleError).not.toHaveBeenCalled() + expect(mockPushToolResult).toHaveBeenCalledWith("Tool result message") + expect(mockCline.didEditFile).toBe(true) + expect(consoleErrorSpy).toHaveBeenCalledWith( + "Error resetting write_to_file diff view:", + expect.any(Error), + ) + } finally { + consoleErrorSpy.mockRestore() + } + }) }) describe("partial block handling", () => { @@ -420,6 +1213,315 @@ describe("writeToFileTool", () => { expect(mockCline.diffViewProvider.open).toHaveBeenCalledWith(testFilePath) expect(mockCline.diffViewProvider.update).toHaveBeenCalledWith(testContent, false) }) + it("does not share path stabilization between tasks with the same path", async () => { + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + expect(mockCline.ask).not.toHaveBeenCalled() + + mockCline.taskId = "task-2" + mockCline.instanceId = "instance-2" + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + expect(mockCline.ask).not.toHaveBeenCalled() + + mockCline.taskId = "task-1" + mockCline.instanceId = "instance-1" + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + expect(mockCline.ask).toHaveBeenCalledTimes(1) + + mockCline.taskId = "task-2" + mockCline.instanceId = "instance-2" + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + expect(mockCline.ask).toHaveBeenCalledTimes(2) + }) + + it("cleans per-task partial state when the task aborts before execute finalization", async () => { + let abortCleanup: (() => void) | undefined + mockCline.once.mockImplementation((event: RooCodeEventName, listener: () => void) => { + if (event === RooCodeEventName.TaskAborted) { + abortCleanup = listener + } + return mockCline + }) + + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + expect(mockCline.ask).toHaveBeenCalledTimes(1) + expect(mockCline.once).toHaveBeenCalledWith(RooCodeEventName.TaskAborted, expect.any(Function)) + + abortCleanup?.() + expect(mockCline.off).toHaveBeenCalledWith(RooCodeEventName.TaskAborted, abortCleanup) + + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + expect(mockCline.ask).toHaveBeenCalledTimes(1) + + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + expect(mockCline.ask).toHaveBeenCalledTimes(2) + }) + + it("does not treat a changed path between deltas as stabilized", async () => { + // Delta 1 streams "alpha.txt"; delta 2 streams "beta.txt" for the same task. The path changed + // between deltas, so it must not count as stabilized and no partial `tool` ask may be issued for + // the still-changing second path. + await executeWriteFileTool({ path: "alpha.txt" }, { isPartial: true }) + await executeWriteFileTool({ path: "beta.txt" }, { isPartial: true }) + + expect(mockCline.ask).not.toHaveBeenCalled() + expect(mockCline.diffViewProvider.open).not.toHaveBeenCalled() + }) + + it("does not issue a partial ask when content is undefined after path stabilization", async () => { + // Delta 1 stabilizes the path. Delta 2 repeats it but carries no content yet: the + // `newContent === undefined` clause must short-circuit the ask even though the path itself has + // stabilized. + await executeWriteFileTool({}, { isPartial: true }) + await executeWriteFileTool({ content: undefined }, { isPartial: true }) + + expect(mockCline.ask).not.toHaveBeenCalled() + expect(mockCline.diffViewProvider.update).not.toHaveBeenCalled() + }) + + it("does not reopen an already open diff view during streaming", async () => { + // The diff view is already open for this task (isEditing). A stabilized delta must still update + // the streamed content but must not call open() again -- reopening would discard the view's + // current state. + mockCline.diffViewProvider.isEditing = true + + await executeWriteFileTool({}, { isPartial: true }) + await executeWriteFileTool({}, { isPartial: true }) + + expect(mockCline.ask).toHaveBeenCalledTimes(1) + expect(mockCline.diffViewProvider.open).not.toHaveBeenCalled() + expect(mockCline.diffViewProvider.update).toHaveBeenCalledWith(testContent, false) + }) + + it("logs the streaming diff view failure with the write_to_file context", async () => { + // The catch arm logs a context-specific message before swallowing the error (execute() reports + // the authoritative one). The message must keep the write_to_file context so the log is + // actionable. + mockCline.diffViewProvider.open.mockRejectedValue(new Error("EACCES: permission denied")) + const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + try { + await executeWriteFileTool({}, { isPartial: true }) + await executeWriteFileTool({}, { isPartial: true }) + + expect(consoleErrorSpy).toHaveBeenCalledWith( + "Error streaming write_to_file diff view:", + expect.anything(), + ) + } finally { + consoleErrorSpy.mockRestore() + } + }) + + it("reports the captured streaming error instead of the parse error when the final block fails to parse, and clears the per-task state", async () => { + // A streaming delta fails with a filesystem error (streamFailed + streamError are + // captured). The final block then arrives without nativeArgs, so execute() never + // runs: the parse-failure teardown boundary must report the original filesystem + // error under the "writing file" context (not the incidental parse error) and tear + // down the per-task state, so the next write_to_file stream in this task is not + // blocked by the stale streamFailed guard and no abort listener leaks. + const fsError = new Error("EACCES: permission denied") + mockCline.diffViewProvider.open.mockRejectedValue(fsError) + // Capture the abort listener of the state created by the first deltas: it is the + // exact reference that the parse-failure teardown must detach. + let abortListener: (() => void) | undefined + mockCline.once.mockImplementation((event: RooCodeEventName, listener: () => void) => { + if (event === RooCodeEventName.TaskAborted && abortListener === undefined) { + abortListener = listener + } + return mockCline + }) + const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + try { + await executeWriteFileTool({}, { isPartial: true }) + await executeWriteFileTool({}, { isPartial: true }) + + const toolUse: ToolUse = { + type: "tool_use", + name: "write_to_file", + params: { path: testFilePath, content: testContent }, + nativeArgs: undefined, + partial: false, + } + await writeToFileTool.handle(mockCline, toolUse as ToolUse<"write_to_file">, { + askApproval: mockAskApproval, + handleError: mockHandleError, + pushToolResult: vi.fn(), + toolCallId: "toolu_handle_error", + }) + + expect(mockHandleError).toHaveBeenCalledTimes(1) + expect(mockHandleError).toHaveBeenCalledWith("writing file", fsError) + expect(mockHandleError).not.toHaveBeenCalledWith( + expect.stringContaining("parsing write_to_file"), + expect.anything(), + ) + // The parse-failure teardown also restores the diff document: the + // streaming failure above already reverted it once (revert + reset), + // and the parse path runs the same cleanup again because execute() + // never runs on this path. + expect(mockCline.diffViewProvider.discardUnapprovedStream).toHaveBeenCalledTimes(2) + expect(mockCline.diffViewProvider.reset).toHaveBeenCalledTimes(2) + // Per-task state torn down at this boundary: guard cleared, the exact + // registered abort listener detached. + expect(writeToFileTool["taskPartialStreamState"].size).toBe(0) + expect(abortListener).toBeTypeOf("function") + expect(mockCline.off).toHaveBeenCalledWith(RooCodeEventName.TaskAborted, abortListener) + + // The next stream for the same task must issue a partial ask again (the stale + // streamFailed guard is gone). + mockCline.diffViewProvider.open.mockResolvedValue(undefined) + await executeWriteFileTool({}, { isPartial: true }) + await executeWriteFileTool({}, { isPartial: true }) + expect(mockCline.ask).toHaveBeenCalledTimes(2) + } finally { + consoleErrorSpy.mockRestore() + } + }) + + it("restores the diff document when the final block fails to parse after successful streaming", async () => { + // Streaming opened the diff view with unapproved partial content (open and + // update both succeeded, so no streaming error was captured). The final block + // then arrives without nativeArgs: execute() never runs, so its error cleanup + // never fires. The parse-failure teardown must still restore the document + // (revert before reset) or a user save could persist content the write never + // completed, and must report the generic parse error (no streaming error to + // surface instead). + let abortListener: (() => void) | undefined + mockCline.once.mockImplementation((event: RooCodeEventName, listener: () => void) => { + if (event === RooCodeEventName.TaskAborted && abortListener === undefined) { + abortListener = listener + } + return mockCline + }) + const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + try { + // Delta 1 - stabilize path; delta 2 - streams the partial content into the + // diff view (open + update resolve). + await executeWriteFileTool({}, { isPartial: true }) + await executeWriteFileTool({}, { isPartial: true }) + expect(mockCline.diffViewProvider.open).toHaveBeenCalledTimes(1) + expect(mockCline.diffViewProvider.update).toHaveBeenCalledTimes(1) + + const toolUse: ToolUse = { + type: "tool_use", + name: "write_to_file", + params: { path: testFilePath, content: testContent }, + nativeArgs: undefined, + partial: false, + } + await writeToFileTool.handle(mockCline, toolUse as ToolUse<"write_to_file">, { + askApproval: mockAskApproval, + handleError: mockHandleError, + pushToolResult: vi.fn(), + toolCallId: "toolu_parse_error", + }) + + // No streaming error was captured: the generic parse error is reported. + expect(mockHandleError).toHaveBeenCalledTimes(1) + expect(mockHandleError).toHaveBeenCalledWith("parsing write_to_file args", expect.any(Error)) + // The diff document is restored by the parse path itself (no streaming + // failure happened, so this is the only revert + reset in the test). + expect(mockCline.diffViewProvider.discardUnapprovedStream).toHaveBeenCalledTimes(1) + expect(mockCline.diffViewProvider.reset).toHaveBeenCalledTimes(1) + // Per-task state torn down: guard cleared, exact listener detached. + expect(writeToFileTool["taskPartialStreamState"].size).toBe(0) + expect(abortListener).toBeTypeOf("function") + expect(mockCline.off).toHaveBeenCalledWith(RooCodeEventName.TaskAborted, abortListener) + } finally { + consoleErrorSpy.mockRestore() + } + }) + + it("releases the per-task stream state when provider state rejects during a partial delta", async () => { + // handlePartial() registers the entry and the TaskAborted listener, then awaits + // provider.getState(). A rejection there never reaches the diff view or execute(), so + // nothing else releases what the registration acquired: the entry and its listener would + // survive for the task's lifetime. The error still has to surface (BaseTool reports it). + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + expect(writeToFileTool["taskPartialStreamState"].size).toBe(1) + mockCline.providerRef.deref.mockReturnValue({ + getState: vi.fn().mockRejectedValue(new Error("provider state unavailable")), + }) + + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + + expect(writeToFileTool["taskPartialStreamState"].size).toBe(0) + const abortListener = mockCline.once.mock.calls.find( + ([event]: unknown[]) => event === RooCodeEventName.TaskAborted, + )?.[1] + expect(abortListener).toBeInstanceOf(Function) + expect(mockCline.off).toHaveBeenCalledWith(RooCodeEventName.TaskAborted, abortListener) + expect(mockHandleError).toHaveBeenCalledWith( + "handling partial write_to_file", + expect.objectContaining({ message: "provider state unavailable" }), + ) + }) + }) + + describe("path stabilization predicate", () => { + // The predicate is exercised directly (it is private) because not all of its branches are + // observable through handlePartial(): an undefined path reaches the same early return either + // way, so the clause-by-clause behavior must be pinned at the predicate level. + function makeState(lastSeenPartialPath: string | undefined) { + return { + lastSeenPartialPath, + streamFailed: false, + streamError: undefined, + task: mockCline, + abortCleanup: () => {}, + } + } + + it("reports a first delta as not stabilized and records the seen path", () => { + const state = makeState(undefined) + + expect(writeToFileTool["hasPathStabilizedForTask"](state, "a.txt")).toBe(false) + expect(state.lastSeenPartialPath).toBe("a.txt") + }) + + it("reports a repeated path as stabilized", () => { + const state = makeState("a.txt") + + expect(writeToFileTool["hasPathStabilizedForTask"](state, "a.txt")).toBe(true) + }) + + it("reports a changed path as not stabilized", () => { + const state = makeState("a.txt") + + expect(writeToFileTool["hasPathStabilizedForTask"](state, "b.txt")).toBe(false) + expect(state.lastSeenPartialPath).toBe("b.txt") + }) + }) + + describe("resetPartialState", () => { + it("resets the base partial path and detaches every task's abort listener", async () => { + let abortCleanup: (() => void) | undefined + mockCline.once.mockImplementation((event: RooCodeEventName, listener: () => void) => { + if (event === RooCodeEventName.TaskAborted) { + abortCleanup = listener + } + return mockCline + }) + + // Seed one per-task state with an abort listener attached. + await executeWriteFileTool({}, { isPartial: true }) + await executeWriteFileTool({}, { isPartial: true }) + expect(mockCline.ask).toHaveBeenCalledTimes(1) + expect(abortCleanup).toBeTypeOf("function") + + // The base-class singleton field is reset by super.resetPartialState(). + writeToFileTool["lastSeenPartialPath"] = "stale-path" + writeToFileTool.resetPartialState() + + expect(writeToFileTool["lastSeenPartialPath"]).toBeUndefined() + expect(mockCline.off).toHaveBeenCalledWith(RooCodeEventName.TaskAborted, abortCleanup) + + // The per-task map was cleared too: a fresh delta sequence starts un-stabilized, so no + // second partial ask is issued. + await executeWriteFileTool({}, { isPartial: true }) + expect(mockCline.ask).toHaveBeenCalledTimes(1) + }) }) describe("user interaction", () => { @@ -461,16 +1563,679 @@ describe("writeToFileTool", () => { expect(mockCline.diffViewProvider.reset).toHaveBeenCalled() }) - it("handles partial streaming errors after path stabilizes", async () => { + it("uses safe reset and clears partial state when path is missing", async () => { + const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + try { + let abortCleanup: (() => void) | undefined + mockCline.once.mockImplementation((event: RooCodeEventName, listener: () => void) => { + if (event === RooCodeEventName.TaskAborted) { + abortCleanup = listener + } + return mockCline + }) + mockCline.diffViewProvider.reset.mockRejectedValue(new Error("reset failed")) + + await executeWriteFileTool({}, { isPartial: true }) + await executeWriteFileTool({ path: "" }) + + expect(mockCline.recordToolError).toHaveBeenCalledWith("write_to_file") + expect(mockPushToolResult).toHaveBeenCalledWith("Missing param error") + expect(mockHandleError).not.toHaveBeenCalled() + expect(consoleErrorSpy).toHaveBeenCalledWith( + "Error resetting write_to_file diff view:", + expect.any(Error), + ) + expect(mockCline.off).toHaveBeenCalledWith(RooCodeEventName.TaskAborted, abortCleanup) + } finally { + consoleErrorSpy.mockRestore() + } + }) + + it("uses safe reset and clears partial state when content is missing", async () => { + const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + try { + let abortCleanup: (() => void) | undefined + mockCline.once.mockImplementation((event: RooCodeEventName, listener: () => void) => { + if (event === RooCodeEventName.TaskAborted) { + abortCleanup = listener + } + return mockCline + }) + mockCline.diffViewProvider.reset.mockRejectedValue(new Error("reset failed")) + + await executeWriteFileTool({}, { isPartial: true }) + await executeWriteFileTool({ content: undefined }) + + expect(mockCline.recordToolError).toHaveBeenCalledWith("write_to_file") + expect(mockPushToolResult).toHaveBeenCalledWith("Missing param error") + expect(mockHandleError).not.toHaveBeenCalled() + expect(consoleErrorSpy).toHaveBeenCalledWith( + "Error resetting write_to_file diff view:", + expect.any(Error), + ) + expect(mockCline.off).toHaveBeenCalledWith(RooCodeEventName.TaskAborted, abortCleanup) + } finally { + consoleErrorSpy.mockRestore() + } + }) + + it("swallows partial streaming errors instead of surfacing a duplicate error bubble", async () => { + // The same filesystem operation is retried in execute() once the block completes, + // and that authoritative non-partial path reports the error to the user. Surfacing + // it during streaming too would show the same error twice, so handlePartial must NOT + // route streaming errors through handleError. mockCline.diffViewProvider.open.mockRejectedValue(new Error("Open failed")) // First call - path not yet stabilized, no error yet await executeWriteFileTool({}, { isPartial: true }) expect(mockHandleError).not.toHaveBeenCalled() - // Second call with same path - path is now stabilized, error occurs + // Second call with same path - path is now stabilized, error occurs but is swallowed await executeWriteFileTool({}, { isPartial: true }) - expect(mockHandleError).toHaveBeenCalledWith("handling partial write_to_file", expect.any(Error)) + expect(mockHandleError).not.toHaveBeenCalled() + }) + + it("finalizes partial tool message and resets diff view when handlePartial open() fails", async () => { + // Regression test: when diffViewProvider.open() throws during streaming (e.g. EACCES/EROFS + // on a read-only path), the partial tool ask created at the top of handlePartial leaves the + // UI spinner stuck. handlePartial must finalize the partial message and reset the diff view, + // and must NOT surface a duplicate error (execute() reports the authoritative one). + mockCline.diffViewProvider.open.mockRejectedValue( + Object.assign(new Error("EACCES: permission denied, open '/ro/test.py'"), { code: "EACCES" }), + ) + // Record the relative order of discardUnapprovedStream() and reset() (vitest mocks expose + // no invocationCallOrder). + const diffViewCallOrder: string[] = [] + mockCline.diffViewProvider.discardUnapprovedStream.mockImplementation(async () => { + diffViewCallOrder.push("revert") + }) + mockCline.diffViewProvider.reset.mockImplementation(async () => { + diffViewCallOrder.push("reset") + }) + + // First call - path not yet stabilized + await executeWriteFileTool({}, { isPartial: true }) + expect(mockCline.finalizePartialToolAsk).not.toHaveBeenCalled() + + // Second call - path stabilized, open() rejects + await executeWriteFileTool({}, { isPartial: true }) + + // Exact streamed payload: finalizePartialToolAsk() no-ops on a text mismatch, so + // a wrong argument (e.g. relPath) would leave the spinner stuck. + expect(mockCline.finalizePartialToolAsk).toHaveBeenCalledWith(expectedPartialToolMessage) + // The failed write's streamed content must be reverted before reset() clears the + // state discardUnapprovedStream() relies on. + expect(diffViewCallOrder).toEqual(["revert", "reset"]) + expect(mockHandleError).not.toHaveBeenCalled() + }) + + it("finalizes partial tool message and resets diff view when handlePartial update() fails", async () => { + // Same regression as above but for the streaming update() call failing after open() succeeds. + mockCline.diffViewProvider.update.mockRejectedValue( + Object.assign(new Error("EROFS: read-only file system, write '/ro/test.py'"), { code: "EROFS" }), + ) + // Record the relative order of discardUnapprovedStream() and reset() (vitest mocks expose + // no invocationCallOrder). + const diffViewCallOrder: string[] = [] + mockCline.diffViewProvider.discardUnapprovedStream.mockImplementation(async () => { + diffViewCallOrder.push("revert") + }) + mockCline.diffViewProvider.reset.mockImplementation(async () => { + diffViewCallOrder.push("reset") + }) + + // First call - path not yet stabilized + await executeWriteFileTool({}, { isPartial: true }) + + // Second call - path stabilized, update() rejects + await executeWriteFileTool({}, { isPartial: true }) + + // Exact streamed payload: finalizePartialToolAsk() no-ops on a text mismatch, so + // a wrong argument (e.g. relPath) would leave the spinner stuck. + expect(mockCline.finalizePartialToolAsk).toHaveBeenCalledWith(expectedPartialToolMessage) + // The failed write's streamed content must be reverted before reset() clears the + // state discardUnapprovedStream() relies on. + expect(diffViewCallOrder).toEqual(["revert", "reset"]) + expect(mockHandleError).not.toHaveBeenCalled() + }) + + it("does not spawn a new partial tool message on each streaming delta after a failure", async () => { + // Regression test: after diffViewProvider.open() throws and the partial message is + // finalized + diff view reset, the next streaming delta saw a non-partial last message + // and created a brand new "Zoo wants to edit this file" message -- repeating once per + // delta. After the fix, partialStreamFailed short-circuits subsequent deltas so only + // the single initial partial ask is issued. + mockCline.diffViewProvider.open.mockRejectedValue( + Object.assign(new Error("EROFS: read-only file system, mkdir '/scratch'"), { code: "EROFS" }), + ) + + // Delta 1 - stabilize path (no ask yet) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + // Delta 2 - path stabilized, ask issued once, open() fails, stream marked failed + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + // Deltas 3..5 - must be short-circuited, no further asks + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + + // Only the single partial ask from delta 2 should have been issued + expect(mockCline.ask).toHaveBeenCalledTimes(1) + // open() must not be retried after the first failure + expect(mockCline.diffViewProvider.open).toHaveBeenCalledTimes(1) + }) + + it("finalizes any open partial tool ask when final args cannot be parsed", async () => { + // Regression test: a write_to_file block whose final args fail to parse (e.g. the + // tool call was truncated mid-JSON by the output token limit) never reaches + // execute(). A streaming delta for that block may already have opened a partial + // `tool` ask (partial: true) -- BaseTool.handle must finalize it, otherwise the + // UI spinner stays stuck even though the parse error bubble was shown. + // Delta 1 - stabilize path (no ask yet) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + // Delta 2 - path stabilized, partial ask issued once + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + expect(mockCline.ask).toHaveBeenCalledTimes(1) + expect(mockCline.finalizePartialToolAsk).not.toHaveBeenCalled() + + // Final block arrives but its native args cannot be parsed, so execute() is skipped. + const toolUse: ToolUse = { + type: "tool_use", + name: "write_to_file", + params: { + path: testFilePath, + content: testContent, + }, + partial: false, + } + await writeToFileTool.handle(mockCline, toolUse as ToolUse<"write_to_file">, { + askApproval: mockAskApproval, + handleError: mockHandleError, + pushToolResult: mockPushToolResult, + toolCallId: "toolu_finalize_ask", + }) + + // The parse error is still reported, and the open partial ask is finalized first. + // No argument: BaseTool.handle() calls finalizePartialToolAsk() with no text, so a + // mutation passing wrong text would leave findLast() unmatched and the spinner + // stuck. (toHaveBeenCalledWith(undefined) does not match a no-arg call under + // vitest's matcher semantics: [] is not equal to [undefined].) + expect(mockCline.finalizePartialToolAsk).toHaveBeenCalledTimes(1) + expect(mockCline.finalizePartialToolAsk).toHaveBeenCalledWith() + expect(mockHandleError).toHaveBeenCalledWith("parsing write_to_file args", expect.any(Error)) + }) + + it("continues parse failure cleanup when finalizing the partial ask fails", async () => { + // Pins the .catch arm on task.finalizePartialToolAsk() in BaseTool.handle(): when the + // final args cannot be parsed and finalizing the open partial ask also fails, the + // failure must only be logged so the parse error is still reported to the user. + const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + try { + mockCline.finalizePartialToolAsk.mockRejectedValue(new Error("finalize failed")) + + // Final block arrives but its native args cannot be parsed, so execute() is skipped. + const toolUse: ToolUse = { + type: "tool_use", + name: "write_to_file", + params: { + path: testFilePath, + content: testContent, + }, + partial: false, + } + await writeToFileTool.handle(mockCline, toolUse as ToolUse<"write_to_file">, { + askApproval: mockAskApproval, + handleError: mockHandleError, + pushToolResult: vi.fn(), + toolCallId: "toolu_finalize_ask_rejects", + }) + + // Same no-argument contract as the sibling test above: the finalize call in + // BaseTool.handle() carries no text. + expect(mockCline.finalizePartialToolAsk).toHaveBeenCalledTimes(1) + expect(mockCline.finalizePartialToolAsk).toHaveBeenCalledWith() + expect(consoleErrorSpy).toHaveBeenCalledWith( + "Error finalizing write_to_file partial tool ask:", + expect.any(Error), + ) + // The parse error is still reported despite the failed finalization. + expect(mockHandleError).toHaveBeenCalledWith("parsing write_to_file args", expect.any(Error)) + } finally { + consoleErrorSpy.mockRestore() + } + }) + + it("reports a filesystem error only once across the streaming and execute phases", async () => { + // Regression test for the double-error UX defect: a single write_to_file call to a + // read-only path failed twice -- once in handlePartial ("handling partial write_to_file") + // and once in execute() ("writing file"). handlePartial now swallows its error so only + // the authoritative execute() error is surfaced. + const erofs = () => + Object.assign(new Error("EROFS: read-only file system, mkdir '/scratch'"), { code: "EROFS" }) + mockCline.diffViewProvider.open.mockRejectedValue(erofs()) + mockedCreateDirectoriesForFile.mockRejectedValue(erofs()) + + // Streaming phase: stabilize path then fail (swallowed, no handleError) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + + // Final phase: execute() reports the single authoritative error + await executeWriteFileTool({}, { fileExists: false }) + + expect(mockHandleError).toHaveBeenCalledTimes(1) + expect(mockHandleError).toHaveBeenCalledWith("writing file", expect.any(Error)) + }) + + it("does not reset consecutive mistake count when directory creation fails", async () => { + mockCline.consecutiveMistakeCount = 3 + mockedCreateDirectoriesForFile.mockRejectedValue( + Object.assign(new Error("EACCES: permission denied, mkdir '/ro'"), { code: "EACCES" }), + ) + + await executeWriteFileTool({}, { fileExists: false }) + + expect(mockHandleError).toHaveBeenCalledWith("writing file", expect.any(Error)) + expect(mockCline.consecutiveMistakeCount).toBe(3) + }) + + it("reverts the diff document when the write fails before approval", async () => { + // Regression test for the dirty-diff leak: streaming already opened the diff view + // with unapproved content, and the write then failed before the user could approve + // it. reset() alone left the diff document dirty with the streamed content -- a + // user save in the editor would persist a write the task never completed. The + // error path must revert the document (like the approval-denied path does) before + // resetting the provider state. + mockedCreateDirectoriesForFile.mockRejectedValue( + Object.assign(new Error("EACCES: permission denied, mkdir '/ro'"), { code: "EACCES" }), + ) + // Record the relative order of discardUnapprovedStream() and reset() (vitest mocks expose + // no invocationCallOrder). + const diffViewCallOrder: string[] = [] + mockCline.diffViewProvider.discardUnapprovedStream.mockImplementation(async () => { + diffViewCallOrder.push("revert") + }) + mockCline.diffViewProvider.reset.mockImplementation(async () => { + diffViewCallOrder.push("reset") + }) + + // Stream two deltas so the diff view is open with the unapproved content... + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + // ...then the completed block fails before approval + await executeWriteFileTool({}, { fileExists: false }) + + expect(mockHandleError).toHaveBeenCalledWith("writing file", expect.any(Error)) + expect(diffViewCallOrder).toEqual(["revert", "reset"]) + }) + + it("continues cleanup when reverting the diff document fails before approval", async () => { + // Pins the .catch arm on discardUnapprovedStream() in revertDiffChangesBeforeReset(): a failed + // revert (e.g. the diff view was already closed) must only be logged so the + // remaining cleanup (diff view reset + per-task state teardown) always completes. + const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + try { + let abortCleanup: (() => void) | undefined + mockCline.once.mockImplementation((event: RooCodeEventName, listener: () => void) => { + if (event === RooCodeEventName.TaskAborted) { + abortCleanup = listener + } + return mockCline + }) + mockedCreateDirectoriesForFile.mockRejectedValue( + Object.assign(new Error("EACCES: permission denied, mkdir '/ro'"), { code: "EACCES" }), + ) + mockCline.diffViewProvider.discardUnapprovedStream.mockRejectedValue(new Error("revert failed")) + + // Stream two deltas so the diff view opens with the unapproved content... + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + // ...then the completed block fails before approval and the revert fails too. + await executeWriteFileTool({}, { fileExists: false }) + + expect(mockHandleError).toHaveBeenCalledWith("writing file", expect.any(Error)) + expect(consoleErrorSpy).toHaveBeenCalledWith( + "Error releasing the abandoned write_to_file diff view:", + expect.any(Error), + ) + // The diff view is still reset and the per-task stream state still torn down. + expect(mockCline.diffViewProvider.reset).toHaveBeenCalled() + // The reference that was registered must be the one removed. + expect(abortCleanup).toBeTypeOf("function") + expect(mockCline.off).toHaveBeenCalledWith(RooCodeEventName.TaskAborted, abortCleanup) + } finally { + consoleErrorSpy.mockRestore() + } + }) + + it("keeps approved diff content in the editor when saving fails after approval", async () => { + // The reverse of the previous test: once the user approved the write, the diff + // content is their accepted edit. A late failure (e.g. saveChanges rejecting) + // must NOT revert it -- the document stays dirty so the user can save it manually. + mockCline.diffViewProvider.saveChanges.mockRejectedValueOnce(new Error("save failed")) + + await executeWriteFileTool({}, { fileExists: false }) + + expect(mockHandleError).toHaveBeenCalledWith("writing file", expect.any(Error)) + expect(mockCline.diffViewProvider.saveChanges).toHaveBeenCalled() + expect(mockCline.diffViewProvider.revertChanges).not.toHaveBeenCalled() + // With fileExists false the rollback route is discardUnapprovedStream(), so asserting + // only revertChanges here would pass even if the writeApproved guard were dropped and + // the approved edit discarded. Assert the route that can actually fire. + expect(mockCline.diffViewProvider.discardUnapprovedStream).not.toHaveBeenCalled() + expect(mockCline.diffViewProvider.reset).toHaveBeenCalled() + }) + + it("continues execute error cleanup when finalizing partial ask fails", async () => { + const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + try { + mockedCreateDirectoriesForFile.mockRejectedValue( + Object.assign(new Error("EACCES: permission denied, mkdir '/ro'"), { code: "EACCES" }), + ) + mockCline.finalizePartialToolAsk.mockRejectedValue(new Error("finalize failed")) + + await executeWriteFileTool({}, { fileExists: false }) + + // The execute error path finalizes without a text match: any open partial + // tool ask is closed. + expect(mockCline.finalizePartialToolAsk).toHaveBeenCalledWith(undefined) + expect(mockHandleError).toHaveBeenCalledWith("writing file", expect.any(Error)) + expect(mockCline.diffViewProvider.discardUnapprovedStream).toHaveBeenCalledTimes(1) + expect(mockCline.diffViewProvider.reset).toHaveBeenCalled() + expect(consoleErrorSpy).toHaveBeenCalledWith( + "Error finalizing write_to_file partial tool ask:", + expect.any(Error), + ) + } finally { + consoleErrorSpy.mockRestore() + } + }) + + it("keeps partial stream failures isolated per task", async () => { + mockCline.diffViewProvider.open.mockRejectedValueOnce( + Object.assign(new Error("EROFS: read-only file system, mkdir '/task-a'"), { code: "EROFS" }), + ) + + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + expect(mockCline.ask).toHaveBeenCalledTimes(1) + + mockCline.taskId = "task-2" + mockCline.instanceId = "instance-2" + mockCline.diffViewProvider.open.mockResolvedValue(undefined) + mockCline.diffViewProvider.update.mockResolvedValue(undefined) + mockCline.diffViewProvider.editType = undefined + + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + expect(mockCline.ask).toHaveBeenCalledTimes(1) + + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + + expect(mockCline.ask).toHaveBeenCalledTimes(2) + expect(mockCline.diffViewProvider.open).toHaveBeenCalledTimes(2) + }) + + it("swallows diff view reset errors during partial failure cleanup", async () => { + const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + try { + mockCline.diffViewProvider.open.mockRejectedValue( + Object.assign(new Error("EROFS: read-only file system, mkdir '/scratch'"), { code: "EROFS" }), + ) + mockCline.diffViewProvider.reset.mockRejectedValue(new Error("reset failed")) + + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + + expect(mockCline.finalizePartialToolAsk).toHaveBeenCalledWith(expectedPartialToolMessage) + expect(mockCline.diffViewProvider.discardUnapprovedStream).toHaveBeenCalledTimes(1) + expect(mockCline.diffViewProvider.reset).toHaveBeenCalled() + expect(mockHandleError).not.toHaveBeenCalled() + expect(consoleErrorSpy).toHaveBeenCalledWith( + "Error resetting write_to_file diff view:", + expect.any(Error), + ) + } finally { + consoleErrorSpy.mockRestore() + } + }) + + it("continues partial failure cleanup when finalizing partial ask fails", async () => { + const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + try { + mockCline.diffViewProvider.open.mockRejectedValue( + Object.assign(new Error("EROFS: read-only file system, mkdir '/scratch'"), { code: "EROFS" }), + ) + mockCline.finalizePartialToolAsk.mockRejectedValue(new Error("finalize failed")) + + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + + expect(mockCline.finalizePartialToolAsk).toHaveBeenCalledWith(expectedPartialToolMessage) + expect(mockCline.diffViewProvider.discardUnapprovedStream).toHaveBeenCalledTimes(1) + expect(mockCline.diffViewProvider.reset).toHaveBeenCalled() + expect(mockHandleError).not.toHaveBeenCalled() + expect(consoleErrorSpy).toHaveBeenCalledWith( + "Error finalizing write_to_file partial tool ask:", + expect.any(Error), + ) + } finally { + consoleErrorSpy.mockRestore() + } + }) + + it("EROFS in handlePartial does not stall agent loop -- createDirectoriesForFile is not called", async () => { + // Regression test: before the fix, createDirectoriesForFile was called in handlePartial + // with no .catch() guard. An EROFS throw escaped to BaseTool.handle(), which called + // handleError but did not set didRejectTool/didAlreadyUseTool, so the advancement gate + // in presentAssistantMessage was never reached and the agent loop stalled permanently. + // After the fix the call is removed entirely -- handlePartial never touches the filesystem. + mockedCreateDirectoriesForFile.mockRejectedValue( + Object.assign(new Error("EROFS: read-only file system, mkdir '/scratch'"), { code: "EROFS" }), + ) + + // First call -- path not yet stabilized, returns early + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + expect(mockHandleError).not.toHaveBeenCalled() + + // Second call -- path stabilized; createDirectoriesForFile must NOT be called from + // handlePartial, so the mock rejection must not trigger and handleError must not be called + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + expect(mockedCreateDirectoriesForFile).not.toHaveBeenCalled() + expect(mockHandleError).not.toHaveBeenCalled() + }) + + it("EROFS in execute() routes through handleError with cleanup rather than escaping unhandled", async () => { + // Regression test: before the fix, createDirectoriesForFile in execute() sat outside + // the try block (lines 70-74), so an EROFS error escaped the catch at line 188 entirely. + // After the fix the call is inside the try block, so filesystem errors are caught and + // routed through handleError with proper diffViewProvider.reset() cleanup. + mockedCreateDirectoriesForFile.mockRejectedValue( + Object.assign(new Error("EROFS: read-only file system, mkdir '/scratch'"), { code: "EROFS" }), + ) + + await executeWriteFileTool({}, { fileExists: false }) + + expect(mockHandleError).toHaveBeenCalledWith("writing file", expect.any(Error)) + expect(mockCline.diffViewProvider.reset).toHaveBeenCalled() + // The tool must not have proceeded to open or save + expect(mockCline.diffViewProvider.open).not.toHaveBeenCalled() + expect(mockCline.diffViewProvider.saveChanges).not.toHaveBeenCalled() + }) + + it("finalizes partial tool message on error so the UI spinner does not get stuck", async () => { + // Regression test: when a filesystem error is thrown in execute() the webview + // message created during handlePartial (or the early ask in execute) is stuck in + // partial: true state, showing an indefinite spinner alongside the error bubble. + // The catch block must call finalizePartialToolAsk() to close the spinner without + // blocking for user input. + mockedCreateDirectoriesForFile.mockRejectedValue( + Object.assign(new Error("EACCES: permission denied, mkdir '/ro'"), { code: "EACCES" }), + ) + + await executeWriteFileTool({}, { fileExists: false }) + + // handleError must still be called + expect(mockHandleError).toHaveBeenCalledWith("writing file", expect.any(Error)) + + // finalizePartialToolAsk must have been called (no text: the execute error + // path closes whichever partial tool ask is open) to dismiss the spinner + expect(mockCline.finalizePartialToolAsk).toHaveBeenCalledWith(undefined) + // The write was never approved, so the diff document is reverted before reset + expect(mockCline.diffViewProvider.discardUnapprovedStream).toHaveBeenCalledTimes(1) + }) + + it("runs diff cleanup when handleError rejects", async () => { + // The production handleError awaits Task.say(), which rejects when the task is + // aborted. A rejected handleError must not skip the diff cleanup: the unapproved + // streamed content has to be reverted and the diff view reset, or a user save + // could persist the failed write. The handleError rejection itself propagates + // (it is not swallowed by the cleanup). As in the missing-parameter tests + // above, the revert mock awaits a deferred so the test proves the cleanup + // AWAITs discardUnapprovedStream() before reset(): with the revert still pending, + // reset() must not have run yet. + mockHandleError.mockRejectedValue(new Error("handleError rejected (aborted task)")) + mockedCreateDirectoriesForFile.mockRejectedValue( + Object.assign(new Error("EACCES: permission denied, mkdir '/ro'"), { code: "EACCES" }), + ) + const diffViewCallOrder: string[] = [] + let resolveRevert: () => void = () => {} + const revertDeferred = new Promise((resolve) => { + resolveRevert = resolve + }) + mockCline.diffViewProvider.discardUnapprovedStream.mockImplementation(async () => { + diffViewCallOrder.push("revert") + await revertDeferred + }) + mockCline.diffViewProvider.reset.mockImplementation(async () => { + diffViewCallOrder.push("reset") + }) + + const executePromise = executeWriteFileTool({}, { fileExists: false }) + await new Promise((resolve) => setTimeout(resolve, 0)) + + // handleError was attempted with the write context and the cleanup has reached + // the deferred revert... + expect(mockHandleError).toHaveBeenCalledWith("writing file", expect.any(Error)) + expect(mockCline.diffViewProvider.discardUnapprovedStream).toHaveBeenCalledTimes(1) + // ...and while the revert is still pending, reset() must not have run yet. + expect(mockCline.diffViewProvider.reset).not.toHaveBeenCalled() + + resolveRevert() + await expect(executePromise).rejects.toThrow("handleError rejected (aborted task)") + + // The unapproved content is reverted before the diff view reset. + expect(diffViewCallOrder).toEqual(["revert", "reset"]) + expect(mockCline.diffViewProvider.open).not.toHaveBeenCalled() + expect(mockCline.diffViewProvider.saveChanges).not.toHaveBeenCalled() + }) + }) + + describe("prevent focus disruption experiment", () => { + /** + * Enable the PREVENT_FOCUS_DISRUPTION experiment for the current task: the experiment + * branches in execute()/handlePartial() read it from the provider state they fetch. + */ + function enablePreventFocusDisruption(): void { + mockCline.providerRef = { + deref: vi.fn().mockReturnValue({ + getState: vi.fn().mockResolvedValue({ + diagnosticsEnabled: true, + writeDelayMs: 1000, + experiments: { preventFocusDisruption: true }, + }), + }), + } + } + + it("saves through saveDirectly without diff editor interaction when the experiment is enabled", async () => { + enablePreventFocusDisruption() + + await executeWriteFileTool({}, { fileExists: false }) + + expect(mockCline.diffViewProvider.saveDirectly).toHaveBeenCalledWith( + testFilePath, + testContent, + false, + true, + 1000, + ) + expect(mockCline.diffViewProvider.saveChanges).not.toHaveBeenCalled() + expect(mockCline.ask).not.toHaveBeenCalled() + expect(mockCline.didEditFile).toBe(true) + expect(toolResult).toBe("Tool result message") + }) + + it("keeps approved diff content when saveDirectly fails after approval", async () => { + // The experiment branch stamps writeApproved before saveDirectly, so a late failure + // must NOT revert the document (the user approved the edit and can save it + // manually) but must still finalize the partial ask and reset the diff view. + const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + try { + enablePreventFocusDisruption() + mockCline.diffViewProvider.saveDirectly.mockRejectedValue(new Error("save failed")) + + await executeWriteFileTool({}, { fileExists: false }) + + expect(mockHandleError).toHaveBeenCalledWith("writing file", expect.any(Error)) + expect(mockCline.finalizePartialToolAsk).toHaveBeenCalledWith(undefined) + expect(mockCline.diffViewProvider.saveDirectly).toHaveBeenCalled() + expect(mockCline.diffViewProvider.revertChanges).not.toHaveBeenCalled() + // Same reason as the saveChanges case: with fileExists false the rollback route is + // discardUnapprovedStream(), so this is the assertion that can actually fail. + expect(mockCline.diffViewProvider.discardUnapprovedStream).not.toHaveBeenCalled() + expect(mockCline.diffViewProvider.reset).toHaveBeenCalled() + expect(mockCline.didEditFile).toBe(false) + } finally { + consoleErrorSpy.mockRestore() + } + }) + + it("skips streaming diff view work when the experiment is enabled", async () => { + // With the experiment enabled the tool preview is embedded in the complete message + // built in execute(), so handlePartial must not open or update the diff view while + // streaming. + enablePreventFocusDisruption() + + // Delta 1 - stabilize path; delta 2 - path stabilized but the experiment short-circuits + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + + expect(mockCline.ask).not.toHaveBeenCalled() + expect(mockCline.diffViewProvider.open).not.toHaveBeenCalled() + expect(mockCline.diffViewProvider.update).not.toHaveBeenCalled() + }) + + it("releases the per-task stream state when prevent-focus-disruption skips the partial preview", async () => { + // Delta 1 only pins the path, so the entry is still live afterwards (the stream is in + // flight). Delta 2 reaches the experiment check and returns without ever showing a + // preview: nothing else would release the entry or detach the TaskAborted listener. + enablePreventFocusDisruption() + + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + expect(writeToFileTool["taskPartialStreamState"].size).toBe(1) + + await executeWriteFileTool({}, { fileExists: false, isPartial: true }) + + expect(mockCline.ask).not.toHaveBeenCalled() + expect(writeToFileTool["taskPartialStreamState"].size).toBe(0) + expect(mockCline.off).toHaveBeenCalledWith(RooCodeEventName.TaskAborted, expect.any(Function)) + }) + + it("clears the provider state when prevent-focus approval is denied", async () => { + // The prevent-focus branch stamps editType/originalContent on the provider before + // asking. On denial nothing was approved and no diff document was opened, so the + // provider state must be cleared (reset) to make a later write re-check the file + // system instead of reusing the stale editType. The non-prevent-focus denial branch + // resets through revertChanges(); this branch must not call it (no document to + // revert). + enablePreventFocusDisruption() + mockAskApproval.mockResolvedValue(false) + + await executeWriteFileTool({}, { fileExists: false }) + + expect(mockAskApproval).toHaveBeenCalledTimes(1) + expect(mockCline.diffViewProvider.saveDirectly).not.toHaveBeenCalled() + expect(mockCline.diffViewProvider.reset).toHaveBeenCalledTimes(1) + expect(mockCline.diffViewProvider.revertChanges).not.toHaveBeenCalled() + expect(mockCline.didEditFile).toBe(false) }) }) }) diff --git a/src/core/webview/ClineProvider.ts b/src/core/webview/ClineProvider.ts index 678b93a136..9b9ceb81ec 100644 --- a/src/core/webview/ClineProvider.ts +++ b/src/core/webview/ClineProvider.ts @@ -78,6 +78,7 @@ import { } from "@roo-code/types" import { RateLimitClock, createRateLimitClock } from "../task/RateLimitClock" import { TaskRegistry } from "../task/TaskRegistry" +import { writeToFileTool } from "../tools/WriteToFileTool" import { TaskScheduler } from "../task/TaskScheduler" import { getEffectiveTaskApiConfiguration, @@ -787,6 +788,11 @@ export class ClineProvider removeTaskEventListeners(this.taskEventListeners, task) + // Dispose removes all listeners, so the tool's per-task state must be released + // while the task is still here; otherwise the singleton keeps the disposed task + // and its diff-view provider. + writeToFileTool.clearTaskState(task) + try { await task.dispose() } catch (error) { diff --git a/src/eslint-suppressions.json b/src/eslint-suppressions.json index d5b845997a..0da463fcd4 100644 --- a/src/eslint-suppressions.json +++ b/src/eslint-suppressions.json @@ -891,7 +891,7 @@ }, "core/tools/__tests__/writeToFileTool.spec.ts": { "@typescript-eslint/no-explicit-any": { - "count": 5 + "count": 3 } }, "core/tools/helpers/toolResultFormatting.ts": { diff --git a/src/integrations/editor/DiffViewProvider.ts b/src/integrations/editor/DiffViewProvider.ts index 3c1d33ae28..78c2884ef4 100644 --- a/src/integrations/editor/DiffViewProvider.ts +++ b/src/integrations/editor/DiffViewProvider.ts @@ -33,6 +33,20 @@ export class DiffViewProvider { isEditing = false originalContent: string | undefined private createdDirs: string[] = [] + /** + * Absolute path of the empty placeholder open() created for a new-file edit, or undefined + * when no placeholder is outstanding. reset() does NOT clear relPath, and saveDirectly() + * sets relPath for a file that was written with approval, so relPath alone cannot tell a + * caller that a file on disk belongs to the abandoned edit. Only this field justifies an + * unlink. + */ + private placeholderPath: string | undefined + /** + * The exact content open() wrote at placeholderPath, recorded so the abandoned cleanup can + * tell its own empty placeholder from content something else put in the same path between + * open() and the discard. Cleared wherever placeholderPath is cleared. + */ + private placeholderContent: string | undefined private documentWasOpen = false // Tracks whether the target file's tab was pinned before the diff session. // Closing the tab to open the diff drops VS Code's pin state, so we restore @@ -132,6 +146,13 @@ export class DiffViewProvider { // Make sure the file exists before we open it. if (!fileExists) { await fs.writeFile(absolutePath, "") + // From here until the placeholder is removed, THIS edit owns that path. Set after + // the write succeeds, so a failed write does not claim ownership of a file we did + // not create. + this.placeholderPath = absolutePath + // Record what was written, not only where: the placeholder is empty by construction, and the + // abandoned cleanup below removes a file only while it still holds exactly this. + this.placeholderContent = "" } // If the file was already open, close it (must happen after showing the @@ -171,6 +192,28 @@ export class DiffViewProvider { this.snapshotPreviewTabs = this.captureUnrelatedPreviewTabs(absolutePath) this.activeDiffEditor = await this.openDiffEditor() + // A release can run while the await above is still in flight: an abort or a denied stream + // reaches discardUnapprovedStream() and reset(), and both drop isEditing along with the + // artifact fields. Everything published below belongs to a session that is already over, + // so the late editor is closed through the same helper the release paths use, the + // listeners are never registered, the deferred scroll is cancelled, and open() returns + // before creating the decoration controllers - so there are no controllers to dispose on + // this path. isEditing is the signal because it is what both release paths clear; it is + // not generation-exact. A second open() that legitimately started while this await was + // parked sets isEditing again and would let this late editor publish, and telling those + // two apart needs the per-session counter the review asks about, which this change + // leaves as an open question instead of inventing one here. + if (!this.isEditing) { + // The plain text tab openDiffEditor() pre-opened through showTextDocument() is not a + // diff tab, so standing down from the diff tabs alone would leave it behind on a file this + // session has already given up owning: close it before the editor reference is dropped. + await this.closeFileTab(absolutePath) + this.activeDiffEditor = undefined + this.disposeActiveEditorListener() + this.cancelDeferredScroll() + await this.closeAllDiffViews() + return + } this.fadedOverlayController = new DecorationController("fadedOverlay", this.activeDiffEditor) this.activeLineController = new DecorationController("activeLine", this.activeDiffEditor) // Apply faded overlay to all lines initially. @@ -336,6 +379,10 @@ export class DiffViewProvider { } const absolutePath = path.resolve(this.cwd, this.relPath) + // The write below is the approved one: whatever placeholder open() created at this path + // becomes real content, so it is no longer an artifact this edit may remove. + this.placeholderPath = undefined + this.placeholderContent = undefined const updatedDocument = this.activeDiffEditor.document const editedContent = updatedDocument.getText() @@ -513,6 +560,218 @@ export class DiffViewProvider { return JSON.stringify(result) } + /** + * Release a diff view whose content was never approved: an abandoned partial stream, + * a failed stream, or a write the user was never asked to approve (rooignore denial, + * validation failure). Deliberately NOT the same as revertChanges(), because + * revertChanges() SAVES and neither abandoned case may write to disk: + * + * - create: its new-file branch saves a dirty buffer as-is before deleting the file, + * which writes partial model output the task never approved - and leaves it on disk + * if the delete then fails. + * - modify: its branch restores the original content and saves it. That is a write to + * a file the user never approved, and for a rooignore-denial path a write the + * policy forbids outright. + * + * Here the target file is never written. A create buffer is emptied FIRST, so the + * only bytes that can ever reach the placeholder are none and the tab is clean enough + * to close without a prompt; a modify buffer is restored to the content already on + * disk in memory only, so the file itself is untouched and the tab (now showing the + * original content, still marked dirty) is left for the user rather than force-closed + * over any edits they may have typed into the preview. The placeholder plus the + * directories this edit created are removed either way. + */ + async discardUnapprovedStream(): Promise { + if (!this.relPath) { + return + } + + const absolutePath = path.resolve(this.cwd, this.relPath) + // Snapshot the directories this edit created BEFORE the editor work below, and + // clear the field so nothing else can act on them twice. If an await below + // rejects, the caller runs reset(), which drops relPath/createdDirs - this is + // then the only chance to remove what the abandoned edit left on disk. + const createdDirs = this.createdDirs + this.createdDirs = [] + // Same reason for the placeholder and its recorded content: an await below may reject and + // the caller then runs reset(), which drops both fields too. + const placeholderPath = this.placeholderPath + const placeholderContent = this.placeholderContent + this.placeholderPath = undefined + this.placeholderContent = undefined + + // The placeholder's identity has to be settled before the buffer work below rather than after it: + // that work saves an emptied create buffer into this very path, so a file that stopped holding + // what open() wrote has to be spared the save as well as the unlink - checked after the save, the + // read would only find what the save wrote, and the content that arrived in between would already + // be gone. ENOENT still means there is nothing left to remove. A read that fails for any other + // reason proves nothing about the file, so the discard then touches neither its content nor its + // existence, and the failure is recorded exactly as the cleanup catch below records one it could + // not finish, so the caller still sees a cleanup failure instead of a silent success. + let cleanupFailure: unknown + let placeholderAlreadyGone = false + let placeholderStillMatches = true + let placeholderReadFailed = false + if (placeholderPath && placeholderContent !== undefined) { + try { + placeholderStillMatches = (await fs.readFile(placeholderPath, "utf-8")) === placeholderContent + } catch (error) { + if ((error as NodeJS.ErrnoException)?.code === "ENOENT") { + placeholderAlreadyGone = true + } else { + placeholderReadFailed = true + cleanupFailure = error + console.error("Error removing abandoned write_to_file artifacts:", error) + } + } + } + + let editorFailure: unknown + // open() creates the directories and the empty placeholder BEFORE it assigns + // activeDiffEditor (openDiffEditor() can reject on its 10s timeout or a failed + // vscode.diff call), so an abandoned create can leave artifacts on disk with no + // editor at all. Only the buffer and tab work needs the editor; the artifact + // cleanup below runs either way. + if (this.activeDiffEditor) { + const document = this.activeDiffEditor.document + try { + this.disposeActiveEditorListener() + this.cancelDeferredScroll() + await this.closeAllDiffViews() + + if (document.isDirty) { + // Restore the buffer to what this edit started from - the empty placeholder for a + // create, the content already on disk for a modify. A failed applyEdit leaves the + // unapproved streamed content in the buffer, and saving then would persist exactly + // what this method exists to discard, so the save is conditional and the failure is + // surfaced to the caller as a rollback hazard. + const edit = new vscode.WorkspaceEdit() + const fullRange = new vscode.Range( + document.positionAt(0), + document.positionAt(document.getText().length), + ) + const restoredContent = + this.editType === "modify" ? this.stripAllBOMs(this.originalContent ?? "") : "" + edit.replace(document.uri, fullRange, restoredContent) + const applied = await vscode.workspace.applyEdit(edit) + if (!applied) { + editorFailure = new Error( + `Could not restore the diff editor buffer for ${this.relPath}; it may still hold unapproved content.`, + ) + } else if ( + this.editType !== "modify" && + placeholderStillMatches && + !placeholderAlreadyGone && + !placeholderReadFailed + ) { + // Only the emptied placeholder is ever saved: this edit created that file, and closing + // a dirty tab would prompt. A modify is never saved here - a modify's restore is an + // in-memory revert, and saving it would write to a file the user never approved (see the + // method comment). The placeholder is not saved once something else has written to it, + // or once a read proved nothing about it either, or once a read proved the file is gone: + // dropping the empty buffer onto that + // path would overwrite bytes nobody approved, which is the very reason the unlink below + // is guarded, and checking it after the save would be too late - the save would already + // have written over the content the read was looking for. + await document.save() + } + } + + await this.closeFileTab(absolutePath) + } catch (error) { + // Do NOT stop here: the placeholder and the created directories still have + // to go. The original failure is re-thrown once the artifacts are dealt with, + // so the caller still reports the rollback hazard instead of a silent success. + editorFailure = error + } + } + + try { + // Only a placeholder THIS edit created may be removed. relPath survives reset(), and + // saveDirectly() sets it for a file that was written with approval, so unlinking + // absolutePath unconditionally can delete content the user approved. + if (placeholderPath) { + // Remove the placeholder only while it still holds what open() wrote there. Content can + // land in that path between open() and this discard - the user typing into the preview, a + // formatter, a later edit - and unlinking it here would delete bytes nobody approved. The + // read that answers that question ran above, before any buffer work, so the same finding + // can stop the save. A placeholder with no recorded content is a path a caller installed + // without open() having written it, and it keeps the unconditional behaviour it always had. + if (!placeholderAlreadyGone && !placeholderReadFailed) { + if (placeholderStillMatches) { + // The read that set this flag ran before every await below, so a formatter, an extension or a + // later edit that wrote to this path in that window would still see the flag true and be + // unlinked. Re-reading immediately before the unlink narrows that window; plain fs calls + // cannot make the check and the delete atomic, so it does not close it. A file that is no + // longer there is still treated as already removed, and a path with no recorded content + // keeps the unconditional behaviour it always had: there is nothing to compare against. + if (placeholderContent !== undefined) { + const contentNow = await fs.readFile(placeholderPath, "utf-8").catch((error: unknown) => { + if ((error as NodeJS.ErrnoException)?.code === "ENOENT") { + return undefined + } + throw error + }) + if (contentNow !== undefined && contentNow !== placeholderContent) { + throw new Error( + `Kept ${placeholderPath}: it no longer holds the empty placeholder this edit wrote.`, + ) + } + } + await fs.unlink(placeholderPath).catch((error: unknown) => { + // open() creates the placeholder; if it is already gone there is nothing + // left to remove and the discard did its job. + if ((error as NodeJS.ErrnoException)?.code !== "ENOENT") { + throw error + } + }) + } else { + // Kept on disk and reported through the catch below, which is the channel this site + // already uses for a cleanup it could not finish, and surfaced to the caller as a + // cleanup failure exactly the way an unlink failure is. No new reporting channel is + // introduced here; the file does not have one to name. + throw new Error( + `Kept ${placeholderPath}: it no longer holds the empty placeholder this edit wrote.`, + ) + } + } + } + + // Remove only the directories this edit created, in reverse order. + for (let i = createdDirs.length - 1; i >= 0; i--) { + await fs.rmdir(createdDirs[i]).catch((error: unknown) => { + if ((error as NodeJS.ErrnoException)?.code !== "ENOENT") { + throw error + } + }) + } + } catch (error) { + cleanupFailure = error + console.error("Error removing abandoned write_to_file artifacts:", error) + } + + // The stream session is over whichever way this returns. Task.dispose() runs no reset() + // after the discard, so leaving isEditing true with the editor still referenced would + // keep a disposed task pointing at a live diff view - and the idempotency guard in + // WriteToFileTool.discardDiffViewOpenedAfterRelease() reads isEditing to decide whether + // a late continuation still has work to do. reset() itself is not called here: it closes + // the diff views again, and the callers that do run it still do. + this.isEditing = false + this.editType = undefined + this.activeDiffEditor = undefined + this.originalContent = undefined + this.streamedLines = [] + this.userTouchedDocument = false + this.userTouchedDiffEditor = false + + if (editorFailure) { + throw editorFailure instanceof Error ? editorFailure : new Error(String(editorFailure)) + } + if (cleanupFailure) { + throw cleanupFailure instanceof Error ? cleanupFailure : new Error(String(cleanupFailure)) + } + } + async revertChanges(): Promise { if (!this.relPath || !this.activeDiffEditor) { return @@ -537,6 +796,8 @@ export class DiffViewProvider { // opened tab before deleting it from disk. await this.closeFileTab(absolutePath) await fs.unlink(absolutePath) + this.placeholderPath = undefined + this.placeholderContent = undefined // Remove only the directories we created, in reverse order. for (let i = this.createdDirs.length - 1; i >= 0; i--) { @@ -1114,6 +1375,8 @@ export class DiffViewProvider { this.isEditing = false this.originalContent = undefined this.createdDirs = [] + this.placeholderPath = undefined + this.placeholderContent = undefined this.documentWasOpen = false this.documentWasPinned = false this.activeDiffEditor = undefined diff --git a/src/integrations/editor/__tests__/DiffViewProvider.spec.ts b/src/integrations/editor/__tests__/DiffViewProvider.spec.ts index e94a66e1db..423ff4c473 100644 --- a/src/integrations/editor/__tests__/DiffViewProvider.spec.ts +++ b/src/integrations/editor/__tests__/DiffViewProvider.spec.ts @@ -1,3 +1,4 @@ +import * as fs from "fs/promises" import { DiffViewProvider, DIFF_VIEW_URI_SCHEME, DIFF_VIEW_LABEL_CHANGES } from "../DiffViewProvider" import { DecorationController } from "../DecorationController" import type { Task } from "../../../core/task/Task" @@ -17,6 +18,10 @@ vi.mock("delay", () => ({ vi.mock("fs/promises", () => ({ readFile: vi.fn().mockResolvedValue("file content"), writeFile: vi.fn().mockResolvedValue(undefined), + // The abandoned-stream discard removes the placeholder file and the directories it + // created, so the discard path needs these two. + unlink: vi.fn().mockResolvedValue(undefined), + rmdir: vi.fn().mockResolvedValue(undefined), access: vi.fn().mockResolvedValue(undefined), })) @@ -1775,5 +1780,1407 @@ describe("DiffViewProvider", () => { expect(closeFileTab).not.toHaveBeenCalled() expect(vscode.window.showTextDocument).toHaveBeenCalled() }) + // An abandoned partial stream was never approved. revertChanges() saves a dirty + // new-file buffer before deleting the file, which would put partial model output on + // disk (and leave it there if the delete fails), so the discard path blanks the + // buffer first and only then saves the empty placeholder before removing it. + describe("discardUnapprovedStream method", () => { + const makeAbandonedDocument = (callOrder: string[], isDirty = true) => ({ + isDirty, + getText: () => "partial model output", + positionAt: (offset: number) => ({ line: 0, character: offset }), + uri: { fsPath: mockTargetPath, path: mockTargetPath }, + save: vi.fn(async () => { + callOrder.push("save") + }), + }) + + const openAbandonedView = (document: unknown, createdDirs: string[], callOrder: string[]) => + Object.assign(diffViewProvider, { + relPath: "mock-target-file.ts", + activeDiffEditor: { document }, + editType: "create", + createdDirs, + // open() records the placeholder it wrote; the discard may only delete what it owns. + placeholderPath: `${mockCwd}/mock-target-file.ts`, + closeAllDiffViews: vi.fn(async () => { + callOrder.push("closeDiffViews") + }), + closeFileTab: vi.fn(async () => { + callOrder.push("closeFileTab") + }), + }) + + it("leaves no session behind for a task that was disposed while the discard ran", async () => { + // Task.dispose() starts this discard and runs no reset() afterwards, so the discard + // itself has to end the session. isEditing and the editor reference are what a late + // handlePartial() continuation reads to decide whether it still owns a view, and a + // disposed task must not keep either - nor the streamed lines it just discarded. + const callOrder: string[] = [] + const document = makeAbandonedDocument(callOrder) + openAbandonedView(document, [], callOrder) + diffViewProvider.isEditing = true + diffViewProvider["streamedLines"] = ["partial model output"] + vi.mocked(vscode.workspace.applyEdit).mockResolvedValue(true) + + await diffViewProvider.discardUnapprovedStream() + + expect(diffViewProvider.isEditing).toBe(false) + expect(diffViewProvider.editType).toBeUndefined() + expect(diffViewProvider["activeDiffEditor"]).toBeUndefined() + expect(diffViewProvider["streamedLines"]).toEqual([]) + }) + + it("never persists the unapproved buffer: blanks it with an empty replacement, then deletes the placeholder", async () => { + const callOrder: string[] = [] + const document = makeAbandonedDocument(callOrder) + openAbandonedView(document, [], callOrder) + vi.mocked(vscode.workspace.applyEdit).mockImplementation(async () => { + callOrder.push("applyEdit") + return true + }) + + await diffViewProvider.discardUnapprovedStream() + + expect(callOrder).toEqual(["closeDiffViews", "applyEdit", "save", "closeFileTab"]) + // The only content the discard may write is an empty buffer, and it must be + // written to THIS document before the save that would otherwise persist the + // partial model output. + expect(mockWorkspaceEdit.replace).toHaveBeenCalledTimes(1) + const [replacedUri, , replacedText] = mockWorkspaceEdit.replace.mock.calls[0] + expect(replacedUri).toEqual(document.uri) + // The replacement covers the whole buffer: the range is built from + // positionAt(0) to positionAt(getText().length), so nothing can survive it. + expect(vi.mocked(vscode.Range)).toHaveBeenCalledWith( + { line: 0, character: 0 }, + { line: 0, character: "partial model output".length }, + ) + expect(replacedText).toBe("") + expect(mockWorkspaceEdit.delete).not.toHaveBeenCalled() + // The placeholder for THIS relPath is what gets removed. + expect(fs.unlink).toHaveBeenCalledWith(expect.stringContaining("mock-target-file.ts")) + }) + + it("blanks, saves and removes a dirty create whose placeholder still holds what open() wrote", async () => { + // The accept path of the save gate with a dirty buffer. The row above reaches the gate only + // with a clean buffer, which never enters the restore branch at all, and the rows below + // cover a placeholder somebody else rewrote or removed, so nothing covered the ordinary + // case: a dirty create whose placeholder still holds the empty content open() wrote. The + // guard agrees, so the emptied buffer is saved onto the file this edit owns and the file is + // then removed. + const relPath = "dirty-matching-placeholder.ts" + const fsPath = `${mockCwd}/${relPath}` + const mockEditor = { + document: { + uri: { fsPath, scheme: "file" }, + getText: vi.fn().mockReturnValue("partial model output"), + positionAt: (offset: number) => ({ line: 0, character: offset }), + isDirty: true, + save: vi.fn().mockResolvedValue(undefined), + lineCount: 0, + }, + selection: { active: { line: 0, character: 0 }, anchor: { line: 0, character: 0 } }, + edit: vi.fn().mockResolvedValue(true), + revealRange: vi.fn(), + } + // Structural double routed through unknown rather than any: only the members the discard + // touches, the same double shape the neighbouring rows use. + const editor = mockEditor as unknown as vscode.TextEditor + vi.mocked(vscode.window).visibleTextEditors = [editor] + vi.mocked(vscode.window.showTextDocument).mockResolvedValue(editor) + vi.mocked(vscode.workspace.onDidOpenTextDocument).mockImplementation((callback) => { + setTimeout(() => callback({ uri: { fsPath, scheme: "file" } } as vscode.TextDocument), 0) + return { dispose: vi.fn() } + }) + vi.mocked(vscode.window.onDidChangeVisibleTextEditors).mockReturnValue({ dispose: vi.fn() }) + vi.mocked(vscode.window.onDidChangeTextEditorVisibleRanges).mockReturnValue({ dispose: vi.fn() }) + vi.mocked(vscode.languages.getDiagnostics).mockReturnValue([]) + diffViewProvider.editType = "create" + vi.mocked(fs.readFile).mockResolvedValue("") + + await diffViewProvider.open(relPath) + expect(fs.writeFile).toHaveBeenCalledWith(fsPath, "") + vi.mocked(vscode.workspace.applyEdit).mockResolvedValue(true) + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + + await expect(diffViewProvider.discardUnapprovedStream()).resolves.toBeUndefined() + + // Emptied with one replacement, saved once onto the path this edit owns, and then removed: + // the guard read agreed twice, so neither the keep branch nor the failure branch ran. + expect(mockWorkspaceEdit.replace).toHaveBeenCalledTimes(1) + expect(editor.document.save).toHaveBeenCalledTimes(1) + expect(fs.unlink).toHaveBeenCalledWith(fsPath) + expect(errorSpy).not.toHaveBeenCalledWith( + "Error removing abandoned write_to_file artifacts:", + expect.anything(), + ) + }) + + it("keeps an editor that arrives after the deferred diff command from publishing into a released session", async () => { + // The reviewer named this ordering: a release can reach the provider while open() is still + // parked inside openDiffEditor(), before the vscode.diff command settles. This row drives the + // real open() against a mocked executeCommand("vscode.diff") that resolves only when the + // test releases it, and leaves openDiffEditor() itself alone because it is private. What it + // pins is open() reacting to a session that was released before that command settled, on the + // create path where open() wrote an empty placeholder - not the extension host that would + // really raise these events. + const callOrder: string[] = [] + const lateDocument = makeTextDocument({ + uri: makeUri(`${mockCwd}/test.txt`), + isDirty: true, + getText: () => "partial model output", + }) + const lateEditor = makeTextEditor({ document: lateDocument }) + let resolveDiffCommand: () => void = () => {} + const diffCommandSettled = new Promise((resolve) => { + resolveDiffCommand = resolve + }) + // openDiffEditor() registers these listeners before it invokes the diff command, so the + // doubles hand the callbacks back instead of being stubbed away. + let onDidOpenTextDocument: ((document: vscode.TextDocument) => unknown) | undefined + // openDiffEditor() hands back a disposable for each of the two listeners it registers + // before the deferred diff command, and the question this row has to answer is whether the + // document callback releases them. Both are kept as distinct objects here - the + // visible-editors fallback is registered against its own double rather than the module + // mock's shared one, so neither assertion can satisfy the other. + const documentListenerDisposable = { dispose: vi.fn() } + const visibleEditorsListenerDisposable = { dispose: vi.fn() } + vi.mocked(vscode.workspace.onDidOpenTextDocument).mockImplementation((callback) => { + onDidOpenTextDocument = callback + return documentListenerDisposable + }) + vi.mocked(vscode.window.onDidChangeVisibleTextEditors).mockImplementation( + () => visibleEditorsListenerDisposable, + ) + // The three listeners open() registers after the editor lands stay on the module mock's + // own disposable double, so registering them is still observable from outside. + Object.defineProperty(vscode.window.tabGroups, "all", { value: [], configurable: true }) + vi.mocked(vscode.window).visibleTextEditors = [] + vi.mocked(vscode.window.showTextDocument).mockResolvedValue(lateEditor) + vi.mocked(vscode.commands.executeCommand).mockImplementation(async (command) => { + callOrder.push(String(command)) + // Only this test lets the diff command settle, which is what defers the diff editor. + await diffCommandSettled + callOrder.push("diffCommandSettled") + }) + // A denied write_to_file empties the buffer; the replace is what would be saved. + vi.mocked(vscode.workspace.applyEdit).mockImplementation(async () => { + callOrder.push("applyEdit") + return true + }) + Object.assign(diffViewProvider, { + editType: "create" as const, + // Every step the released-session stand-down reaches for is recorded, so the order below + // names the whole of it rather than re-testing what each helper does: the plain tab close, + // the listener release, the deferred-scroll cancel and the diff-tab close are four separate + // calls, and dropping any one of them changes this array. + closeAllDiffViews: vi.fn(async () => { + callOrder.push("closeDiffViews") + }), + closeFileTab: vi.fn(async () => { + callOrder.push("closeFileTab") + }), + disposeActiveEditorListener: vi.fn(() => { + callOrder.push("disposeActiveEditorListener") + }), + cancelDeferredScroll: vi.fn(() => { + callOrder.push("cancelDeferredScroll") + }), + }) + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + vi.mocked(fs.readFile).mockResolvedValue("content the user saved after open()") + vi.mocked(fs.unlink).mockResolvedValue(undefined) + const openPromise = diffViewProvider.open("test.txt") + // The streamed text is what the deny discards; open() owns the placeholder it wrote. + diffViewProvider["streamedLines"] = ["partial model output"] + // Let open() run up to the point where it invokes the diff command and stop it there: + // this macrotask queues behind every already-pending microtask, and the command + // double stays unresolved, so open() is parked exactly where the reviewer named. + await new Promise((resolve) => setTimeout(resolve, 0)) + // open() publishes its editor only when the await below it resolves, so the field + // starts empty for this scenario rather than carrying the double the beforeEach installs. + diffViewProvider["activeDiffEditor"] = undefined + // open() is parked on the diff command, so it has published no editor yet. + expect(callOrder).toEqual(["vscode.diff"]) + expect(diffViewProvider["activeDiffEditor"]).toBeUndefined() + expect(vscode.commands.executeCommand).toHaveBeenCalledWith( + "vscode.diff", + expect.anything(), + expect.anything(), + expect.anything(), + { preserveFocus: true }, + ) + // The release runs while open() is still awaiting that command. + const released = diffViewProvider.discardUnapprovedStream() + await expect(released).rejects.toThrow(/no longer holds the empty placeholder/) + // The release refused before any closing: with no editor published it has nothing to work + // from, so it neither empties the buffer nor removes the placeholder it no longer owns. + expect(callOrder).toEqual(["vscode.diff"]) + expect(vscode.workspace.applyEdit).not.toHaveBeenCalled() + expect(lateDocument.save).not.toHaveBeenCalled() + // Then the host catches up: the command settles and the editor finally appears. + resolveDiffCommand() + vi.mocked(vscode.window).visibleTextEditors = [lateEditor] + if (typeof onDidOpenTextDocument !== "function") { + throw new Error("open() never registered the document listener it uses to await the diff editor") + } + await onDidOpenTextDocument(lateDocument) + await openPromise + // The document callback is what resolves openDiffEditor(), and releasing the two temporary + // listeners is part of that resolution: a subscription that survived the race would leave + // one of these two dispose calls uncalled. + expect(documentListenerDisposable.dispose).toHaveBeenCalledTimes(1) + expect(visibleEditorsListenerDisposable.dispose).toHaveBeenCalledTimes(1) + // The late editor never published into the released session. + expect(diffViewProvider["activeDiffEditor"]).toBeUndefined() + expect(DecorationController).not.toHaveBeenCalled() + expect(vscode.window.onDidChangeActiveTextEditor).not.toHaveBeenCalled() + expect(vscode.window.onDidChangeTextEditorSelection).not.toHaveBeenCalled() + expect(vscode.window.onDidChangeTextEditorVisibleRanges).not.toHaveBeenCalled() + expect(fs.unlink).not.toHaveBeenCalled() + // Two closings follow the release and both belong to open(), reached through the same + // helpers the release paths use: first the plain text tab open() pre-opened, then the diff + // tabs. The two helpers are doubles above, so this row names the order of the closings and + // the parked-open row below is the one that names the tab types. The diff command settling + // is recorded before either, and the release that already ran must not run again. + expect(callOrder).toEqual([ + "vscode.diff", + "diffCommandSettled", + "closeFileTab", + "disposeActiveEditorListener", + "cancelDeferredScroll", + "closeDiffViews", + ]) + // Leave the document-open double as the module mock defined it, for the rows after + // this one, since a mockImplementation here outlives the test that set it. + vi.mocked(vscode.workspace.onDidOpenTextDocument).mockImplementation(() => ({ dispose: vi.fn() })) + errorSpy.mockRestore() + }) + it("does not save the emptied buffer over content that arrived in the placeholder after open()", async () => { + // The reviewer's ordering ask, at the layer that would have caught it. The tests above all + // run with a dirty buffer and no recorded placeholder content, so open() wrote nothing this + // discard can recognise as its own and the identity check is skipped; this unit's own tests + // all set isDirty to false. Dirty plus a recorded identity is the combination none of them + // covered, and it is the case where an emptied buffer used to be saved onto the path before + // anything noticed that the path no longer held what open() wrote. Here the save is the only + // write this document can make, so a save that never happens is the whole claim. + const callOrder: string[] = [] + const document = makeAbandonedDocument(callOrder) + openAbandonedView(document, [], callOrder) + diffViewProvider["placeholderContent"] = "" + vi.mocked(vscode.workspace.applyEdit).mockImplementation(async () => { + callOrder.push("applyEdit") + return true + }) + // Somebody else's content is what now holds the placeholder's path. + vi.mocked(fs.readFile).mockResolvedValue("content the user saved after open()") + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + + await expect(diffViewProvider.discardUnapprovedStream()).rejects.toThrow( + /no longer holds the empty placeholder/, + ) + + // The buffer is still blanked in memory and the tab is still closed - only the write is + // withheld. With the identity check running after the buffer work, this list would read + // closeDiffViews, applyEdit, save, closeFileTab. + expect(callOrder).toEqual(["closeDiffViews", "applyEdit", "closeFileTab"]) + expect(document.save).not.toHaveBeenCalled() + expect(mockWorkspaceEdit.replace).toHaveBeenCalledTimes(1) + // The file is kept, so nothing removed it either. + expect(fs.unlink).not.toHaveBeenCalled() + expect(errorSpy).toHaveBeenCalledWith( + "Error removing abandoned write_to_file artifacts:", + expect.objectContaining({ + message: expect.stringContaining("no longer holds the empty placeholder"), + }), + ) + expect(diffViewProvider["placeholderPath"]).toBeUndefined() + expect(diffViewProvider["placeholderContent"]).toBeUndefined() + }) + + it("restores a modify buffer in memory and never saves the target file", async () => { + // revertChanges() restores a modify by applyEdit + document.save(). For a stream the + // user never approved that save is a write they never asked for, and for a + // .rooignore-denied path a write the policy forbids outright, so the discard restores + // the buffer and leaves the file on disk untouched. + const callOrder: string[] = [] + const document = makeAbandonedDocument(callOrder) + openAbandonedView(document, [], callOrder) + Object.assign(diffViewProvider, { + editType: "modify", + originalContent: "original content", + // A modify owns no placeholder: only open() of a create writes one. + placeholderPath: undefined, + }) + vi.mocked(vscode.workspace.applyEdit).mockImplementation(async () => { + callOrder.push("applyEdit") + return true + }) + + await diffViewProvider.discardUnapprovedStream() + + expect(callOrder).toEqual(["closeDiffViews", "applyEdit", "closeFileTab"]) + const [replacedUri, , replacedText] = mockWorkspaceEdit.replace.mock.calls[0] + expect(replacedUri).toEqual(document.uri) + expect(replacedText).toBe("original content") + // The replacement covers the whole buffer, so no streamed content survives it. + expect(vi.mocked(vscode.Range)).toHaveBeenCalledWith( + { line: 0, character: 0 }, + { line: 0, character: "partial model output".length }, + ) + expect(document.save).not.toHaveBeenCalled() + // Nothing is unlinked for a modify: saveChanges() may have written this very file with + // the user's approval, and this edit created no directories. + expect(fs.unlink).not.toHaveBeenCalled() + expect(fs.rmdir).not.toHaveBeenCalled() + }) + + it("does not save and reports the hazard when the buffer restore fails to apply", async () => { + // applyEdit resolving false leaves the unapproved partial content in the buffer. Saving + // then would persist exactly what this method exists to discard, so the save is skipped + // and the caller is told the rollback did not happen. + const callOrder: string[] = [] + const document = makeAbandonedDocument(callOrder) + openAbandonedView(document, [], callOrder) + vi.mocked(vscode.workspace.applyEdit).mockResolvedValue(false) + + await expect(diffViewProvider.discardUnapprovedStream()).rejects.toThrow( + /could not restore the diff editor buffer/i, + ) + + expect(document.save).not.toHaveBeenCalled() + // The artifacts still have to go: the caller runs reset() next, which drops + // placeholderPath/createdDirs and leaves nothing else able to remove them. + expect(fs.unlink).toHaveBeenCalledWith(expect.stringContaining("mock-target-file.ts")) + }) + + it("removes the directories this edit created in reverse order, after the placeholder unlink", async () => { + const callOrder: string[] = [] + const document = makeAbandonedDocument(callOrder) + const createdDirs = [`${mockCwd}/mock-dir`, `${mockCwd}/mock-dir/nested`] + openAbandonedView(document, createdDirs, callOrder) + vi.mocked(vscode.workspace.applyEdit).mockResolvedValue(true) + + await diffViewProvider.discardUnapprovedStream() + + expect(fs.unlink).toHaveBeenCalledTimes(1) + expect(fs.rmdir).toHaveBeenCalledTimes(createdDirs.length) + // Reverse order, so a parent is never removed before the child inside it, and + // only the directories this edit created - never a pre-existing one. + expect(vi.mocked(fs.rmdir).mock.calls.map((c) => c[0])).toEqual([...createdDirs].reverse()) + const unlinkOrder = vi.mocked(fs.unlink).mock.invocationCallOrder[0] + for (const order of vi.mocked(fs.rmdir).mock.invocationCallOrder) { + expect(order).toBeGreaterThan(unlinkOrder) + } + }) + + it("does not save when the abandoned buffer is already clean", async () => { + const callOrder: string[] = [] + const document = makeAbandonedDocument(callOrder, false) + openAbandonedView(document, [], callOrder) + + await diffViewProvider.discardUnapprovedStream() + + expect(vscode.workspace.applyEdit).not.toHaveBeenCalled() + expect(document.save).not.toHaveBeenCalled() + expect(mockWorkspaceEdit.replace).not.toHaveBeenCalled() + expect(fs.unlink).toHaveBeenCalledTimes(1) + }) + + it("still removes the placeholder and the created directories when the editor work rejects, and reports the failure", async () => { + const callOrder: string[] = [] + const document = makeAbandonedDocument(callOrder) + const createdDirs = [`${mockCwd}/mock-dir`] + openAbandonedView(document, createdDirs, callOrder) + vi.mocked(vscode.workspace.applyEdit).mockRejectedValue(new Error("applyEdit rejected")) + + // The caller (releaseAbandonedDiffView) treats a throw as "rollback hazard", so + // the failure must still surface - but only after the artifacts are gone, because + // the caller then runs reset(), which drops relPath/createdDirs and leaves + // nothing else able to remove them. + await expect(diffViewProvider.discardUnapprovedStream()).rejects.toThrow("applyEdit rejected") + + expect(fs.unlink).toHaveBeenCalledWith(expect.stringContaining("mock-target-file.ts")) + expect(fs.rmdir).toHaveBeenCalledWith(`${mockCwd}/mock-dir`) + }) + + it("removes the placeholder and created directories even when open() failed before activeDiffEditor was assigned", async () => { + // open() creates the directories (line 130) and the empty placeholder (line 134) + // BEFORE openDiffEditor() assigns activeDiffEditor (line 173). If that call rejects + // - the 10s timeout or a failed vscode.diff - an abandoned create therefore has + // artifacts on disk and no editor, and the early return must not skip the cleanup. + Object.assign(diffViewProvider, { + relPath: "mock-target-file.ts", + activeDiffEditor: undefined, + editType: "create", + createdDirs: [`${mockCwd}/mock-dir`], + // open() had already written the placeholder before openDiffEditor() rejected, + // so this edit owns the path. + placeholderPath: `${mockCwd}/mock-target-file.ts`, + }) + + await diffViewProvider.discardUnapprovedStream() + + expect(fs.unlink).toHaveBeenCalledWith(expect.stringContaining("mock-target-file.ts")) + expect(fs.rmdir).toHaveBeenCalledWith(`${mockCwd}/mock-dir`) + // Nothing editor-side ran, and nothing threw: the artifacts are the whole job here. + expect(vscode.workspace.applyEdit).not.toHaveBeenCalled() + // Bracket access for the private field: the snapshot must be consumed, so a second + // discard cannot try to remove the same directories again. + expect(diffViewProvider["createdDirs"]).toEqual([]) + }) + + it("logs and rejects when the placeholder unlink fails for a reason other than ENOENT", async () => { + const callOrder: string[] = [] + const document = makeAbandonedDocument(callOrder) + openAbandonedView(document, [], callOrder) + vi.mocked(vscode.workspace.applyEdit).mockResolvedValue(true) + const permissionError = Object.assign(new Error("EPERM: operation not permitted"), { code: "EPERM" }) + vi.mocked(fs.unlink).mockRejectedValueOnce(permissionError) + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + + await expect(diffViewProvider.discardUnapprovedStream()).rejects.toThrow( + "EPERM: operation not permitted", + ) + + expect(errorSpy).toHaveBeenCalledWith( + "Error removing abandoned write_to_file artifacts:", + permissionError, + ) + }) + + it("logs and rejects when removing a created directory fails for a reason other than ENOENT", async () => { + const callOrder: string[] = [] + const document = makeAbandonedDocument(callOrder) + openAbandonedView(document, [`${mockCwd}/mock-dir`], callOrder) + vi.mocked(vscode.workspace.applyEdit).mockResolvedValue(true) + vi.mocked(fs.unlink).mockResolvedValue(undefined) + const notEmpty = Object.assign(new Error("ENOTEMPTY: directory not empty"), { code: "ENOTEMPTY" }) + vi.mocked(fs.rmdir).mockRejectedValueOnce(notEmpty) + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + + await expect(diffViewProvider.discardUnapprovedStream()).rejects.toThrow( + "ENOTEMPTY: directory not empty", + ) + + expect(errorSpy).toHaveBeenCalledWith("Error removing abandoned write_to_file artifacts:", notEmpty) + }) + + it("tolerates an already-deleted placeholder and directories (ENOENT) without throwing", async () => { + const callOrder: string[] = [] + const document = makeAbandonedDocument(callOrder) + // Two created directories: the tolerance has to hold for every one of them, so the + // rejection is keyed on the path rather than on which call comes first. + const dirA = `${mockCwd}/mock-dir-a` + const dirB = `${mockCwd}/nested/mock-dir-b` + openAbandonedView(document, [dirA, dirB], callOrder) + vi.mocked(vscode.workspace.applyEdit).mockResolvedValue(true) + const enoent = Object.assign(new Error("ENOENT"), { code: "ENOENT" }) + // A single unlink call in this flow, so the placeholder stays Once-based. + vi.mocked(fs.unlink).mockRejectedValueOnce(enoent) + vi.mocked(fs.rmdir).mockImplementation(async (dir) => { + if (dir === dirA || dir === dirB) { + throw enoent + } + }) + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + + await expect(diffViewProvider.discardUnapprovedStream()).resolves.toBeUndefined() + + // Both directories were attempted; the assertion is on the paths, not the order. + expect(fs.rmdir).toHaveBeenCalledWith(dirA) + expect(fs.rmdir).toHaveBeenCalledWith(dirB) + expect(errorSpy).not.toHaveBeenCalledWith( + "Error removing abandoned write_to_file artifacts:", + expect.anything(), + ) + + // beforeEach only clears call data, not implementations, so put the default back. + vi.mocked(fs.rmdir).mockResolvedValue(undefined) + }) + + it("reports the editor failure rather than the cleanup failure when both happen", async () => { + const callOrder: string[] = [] + const document = makeAbandonedDocument(callOrder) + openAbandonedView(document, [`${mockCwd}/mock-dir`], callOrder) + vi.mocked(vscode.workspace.applyEdit).mockRejectedValue(new Error("applyEdit rejected")) + vi.mocked(fs.unlink).mockRejectedValueOnce(Object.assign(new Error("EPERM"), { code: "EPERM" })) + vi.spyOn(console, "error").mockImplementation(() => {}) + + // The caller reports a rollback hazard from the thrown error, so the ORIGINAL failure + // is what must surface; the cleanup failure is still logged for the operator. + await expect(diffViewProvider.discardUnapprovedStream()).rejects.toThrow("applyEdit rejected") + }) + + it("leaves a file this edit never created a placeholder for on disk", async () => { + // reset() does not clear relPath, and saveDirectly() sets it for an approved write. + // Without ownership tracking the discard would unlink that approved file: an approved + // write_to_file to A, then a new write_to_file whose block exits through a rollback + // path before open() ever ran, still sees relPath === A. + const callOrder: string[] = [] + const document = makeAbandonedDocument(callOrder) + Object.assign(diffViewProvider, { + relPath: "previously-approved-file.ts", + activeDiffEditor: { document }, + editType: undefined, + createdDirs: [], + placeholderPath: undefined, + }) + vi.mocked(vscode.workspace.applyEdit).mockResolvedValue(true) + + await expect(diffViewProvider.discardUnapprovedStream()).resolves.toBeUndefined() + + expect(fs.unlink).not.toHaveBeenCalled() + expect(fs.rmdir).not.toHaveBeenCalled() + // The buffer is still blanked and the tab closed: the unapproved content is gone + // without touching a file the user approved. + expect(mockWorkspaceEdit.replace).toHaveBeenCalledTimes(1) + expect(mockWorkspaceEdit.replace.mock.calls[0][2]).toBe("") + }) + + it("closes a diff editor that open() publishes after a release already ran, and registers nothing behind it", async () => { + // The review's late-open scenario, driven through the real open(): the editor call inside + // open() is still pending while the abort path releases the view, and only then resolves. + // open() itself is the production method; the innermost openDiffEditor() call is deferred + // and only vscode and fs are doubles. + const relPath = "late-open.ts" + const fsPath = `${mockCwd}/${relPath}` + let handOverEditor: (editor: vscode.TextEditor) => void = () => {} + const pendingEditorCall = new Promise((resolve) => { + handOverEditor = resolve + }) + const lateEditor = { + document: { + uri: { fsPath, scheme: "file" }, + lineCount: 0, + getText: vi.fn().mockReturnValue(""), + isDirty: false, + save: vi.fn().mockResolvedValue(undefined), + }, + selection: { active: { line: 0, character: 0 }, anchor: { line: 0, character: 0 } }, + visibleRanges: [], + edit: vi.fn().mockResolvedValue(true), + revealRange: vi.fn(), + } + // Structural double routed through unknown rather than any: it carries only the members + // the late teardown touches. + const lateEditorDouble = lateEditor as unknown as vscode.TextEditor + const lateDiffTab = makeTab(makeDiffTabInput("cline-diff", fsPath), "diff tab") + // The other tab the awaited call puts on screen: openDiffEditor() pre-opens the file as a + // plain text document before it runs the diff command, and a plain text tab is not a + // diff tab, so closeAllDiffViews() cannot reach it and only closeFileTab() can. + const latePlainTab = makeTab(makeTextTab(fsPath), "plain tab") + + // The shared setup already carries an editor and two decoration controllers, so their + // identities are captured: open() would have replaced both controllers and assigned the + // editor it awaited, and unchanged identities prove it published neither. + const editorBefore = diffViewProvider["activeDiffEditor"] + const overlayControllerBefore = diffViewProvider["fadedOverlayController"] + const lineControllerBefore = diffViewProvider["activeLineController"] + + // Bracket access rather than a cast: openDiffEditor is private, and deferring it is the + // only way to hold open() inside the await the release is racing. + diffViewProvider["openDiffEditor"] = async () => pendingEditorCall + vi.mocked(fs.readFile).mockResolvedValue("") + diffViewProvider.editType = "create" + + const opening = diffViewProvider.open(relPath) + await new Promise((resolve) => setTimeout(resolve, 0)) + + // open() is parked on the editor call, so it has published nothing yet: the editor is + // still the one the setup installed, and no listener has been registered. + expect(diffViewProvider["activeDiffEditor"]).toBe(editorBefore) + expect(vscode.window.onDidChangeActiveTextEditor).not.toHaveBeenCalled() + + // The release the abort path performs, while open() is still parked. + await diffViewProvider.discardUnapprovedStream() + expect(diffViewProvider.isEditing).toBe(false) + + // The diff tab the awaited call put on screen, as the tab group would show it now. This + // spec installs tabGroups.all through a getter, so the late tab is installed the same way, + // and close() is answered with a promise because closeAllDiffViews() awaits it. + Object.defineProperty(vscode.window.tabGroups, "all", { + get: (): vscode.TabGroup[] => [ + { + isActive: true, + viewColumn: vscode.ViewColumn.One, + activeTab: lateDiffTab, + tabs: [latePlainTab, lateDiffTab], + }, + ], + configurable: true, + }) + vi.mocked(vscode.window.tabGroups.close).mockResolvedValue(true) + handOverEditor(lateEditorDouble) + await opening + + // The late editor is closed through the helper the release paths already use, and nothing + // was published behind it: no listeners registered, no deferred scroll pending, and the + // editor reference dropped so a later update() cannot act on it. + expect(vscode.window.tabGroups.close).toHaveBeenCalledWith(lateDiffTab) + // Both tab types are closed before ownership of the file is dropped, and only two + // helpers can close them: the diff tab is closeAllDiffViews() and the plain text tab is + // closeFileTab(), so this assertion is the one that answers a guard without the close. + expect(vscode.window.tabGroups.close).toHaveBeenCalledWith(latePlainTab) + expect(vscode.window.onDidChangeActiveTextEditor).not.toHaveBeenCalled() + expect(vscode.window.onDidChangeTextEditorSelection).not.toHaveBeenCalled() + expect(vscode.window.onDidChangeTextEditorVisibleRanges).not.toHaveBeenCalled() + expect(diffViewProvider["activeEditorListener"]).toBeUndefined() + expect(diffViewProvider["diffEditorSelectionListener"]).toBeUndefined() + expect(diffViewProvider["diffScrollListener"]).toBeUndefined() + expect(diffViewProvider["deferredScrollTimer"]).toBeUndefined() + expect(diffViewProvider["activeDiffEditor"]).toBeUndefined() + // Same controller objects as before open() ran: the late route returned before creating + // the pair open() normally installs, so there was nothing on this path to dispose. + expect(diffViewProvider["fadedOverlayController"]).toBe(overlayControllerBefore) + expect(diffViewProvider["activeLineController"]).toBe(lineControllerBefore) + expect(diffViewProvider.isEditing).toBe(false) + + // Leave the tab-group getter as this spec found it so a later test does not see the tab + // this test installed. + Object.defineProperty(vscode.window.tabGroups, "all", { value: [], configurable: true }) + }) + + it("keeps a placeholder that no longer holds the content open() wrote, and reports it as a cleanup failure", async () => { + // The persistence ask, at the public-method layer: open() writes an empty placeholder and + // records what it wrote; if something else puts content in that path before the discard, + // the discard leaves the file alone. This spec's fs is a double, so the content is the + // answer the double gives the guard's read - what the test pins is that a mismatch keeps + // the file, writes nothing over it, and reports through the channel this site already uses. + const relPath = "kept-placeholder.ts" + const fsPath = `${mockCwd}/${relPath}` + const mockEditor = { + document: { + uri: { fsPath, scheme: "file" }, + getText: vi.fn().mockReturnValue(""), + isDirty: false, + save: vi.fn().mockResolvedValue(undefined), + lineCount: 0, + }, + selection: { active: { line: 0, character: 0 }, anchor: { line: 0, character: 0 } }, + edit: vi.fn().mockResolvedValue(true), + revealRange: vi.fn(), + } + // Structural double routed through unknown rather than any: it carries only the members + // open() touches, the same double shape the placeholder test below uses. + const editor = mockEditor as unknown as vscode.TextEditor + vi.mocked(vscode.window).visibleTextEditors = [editor] + vi.mocked(vscode.window.showTextDocument).mockResolvedValue(editor) + vi.mocked(vscode.workspace.onDidOpenTextDocument).mockImplementation((callback) => { + setTimeout(() => callback({ uri: { fsPath, scheme: "file" } } as vscode.TextDocument), 0) + return { dispose: vi.fn() } + }) + vi.mocked(vscode.window.onDidChangeVisibleTextEditors).mockReturnValue({ dispose: vi.fn() }) + vi.mocked(vscode.window.onDidChangeTextEditorVisibleRanges).mockReturnValue({ dispose: vi.fn() }) + vi.mocked(vscode.languages.getDiagnostics).mockReturnValue([]) + diffViewProvider.editType = "create" + vi.mocked(fs.readFile).mockResolvedValue("") + + await diffViewProvider.open(relPath) + expect(fs.writeFile).toHaveBeenCalledWith(fsPath, "") + + // Content lands in the placeholder between open() and the discard. + vi.mocked(fs.readFile).mockResolvedValue("content the user typed into the preview\n") + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + + await expect(diffViewProvider.discardUnapprovedStream()).rejects.toThrow( + /no longer holds the empty placeholder/, + ) + + expect(fs.unlink).not.toHaveBeenCalledWith(fsPath) + // Nothing wrote over it: the only write to that path in this test is the one open() did. + expect(fs.writeFile).toHaveBeenCalledTimes(1) + // The kept file is reported through the console.error call this cleanup site already + // makes for a cleanup it could not finish, with the error that names it. + expect(errorSpy).toHaveBeenCalledWith( + "Error removing abandoned write_to_file artifacts:", + expect.any(Error), + ) + // The edit drops its claim on the path even though the file stays: a later discard has + // nothing to remove and does not report the same kept file twice. + expect(diffViewProvider["placeholderPath"]).toBeUndefined() + expect(diffViewProvider["placeholderContent"]).toBeUndefined() + }) + + it("keeps the placeholder when content arrives between the guard read and the unlink", async () => { + // The guard's read is the first read the discard makes and the re-check immediately before the + // unlink is the second. The first answer has to let the unlink through and the second has to stop + // it, so the two reads are mocked in that order and nothing else in the flow reads this path. + const relPath = "rewritten-after-guard-placeholder.ts" + const fsPath = `${mockCwd}/${relPath}` + const mockEditor = { + document: { + uri: { fsPath, scheme: "file" }, + getText: vi.fn().mockReturnValue(""), + isDirty: false, + save: vi.fn().mockResolvedValue(undefined), + lineCount: 0, + }, + selection: { active: { line: 0, character: 0 }, anchor: { line: 0, character: 0 } }, + edit: vi.fn().mockResolvedValue(true), + revealRange: vi.fn(), + } + // Structural double routed through unknown rather than any: only the members the discard + // touches, the same double shape the row above uses. + const editor = mockEditor as unknown as vscode.TextEditor + vi.mocked(vscode.window).visibleTextEditors = [editor] + vi.mocked(vscode.window.showTextDocument).mockResolvedValue(editor) + vi.mocked(vscode.workspace.onDidOpenTextDocument).mockImplementation((callback) => { + setTimeout(() => callback({ uri: { fsPath, scheme: "file" } } as vscode.TextDocument), 0) + return { dispose: vi.fn() } + }) + vi.mocked(vscode.window.onDidChangeVisibleTextEditors).mockReturnValue({ dispose: vi.fn() }) + vi.mocked(vscode.window.onDidChangeTextEditorVisibleRanges).mockReturnValue({ dispose: vi.fn() }) + vi.mocked(vscode.languages.getDiagnostics).mockReturnValue([]) + diffViewProvider.editType = "create" + vi.mocked(fs.readFile).mockResolvedValue("") + + await diffViewProvider.open(relPath) + expect(fs.writeFile).toHaveBeenCalledWith(fsPath, "") + + // Read one: still the empty placeholder open() wrote. Read two: a formatter has rewritten it. + vi.mocked(fs.readFile).mockResolvedValueOnce("").mockResolvedValueOnce("content a formatter rewrote\n") + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + + await expect(diffViewProvider.discardUnapprovedStream()).rejects.toThrow( + /no longer holds the empty placeholder/, + ) + + // Nothing was removed and nothing was written over the file that arrived: the only write to + // that path is still the empty placeholder open() made, and the kept file is reported through + // the console.error call this cleanup site already makes for a cleanup it could not finish. + expect(fs.unlink).not.toHaveBeenCalledWith(fsPath) + expect(fs.writeFile).toHaveBeenCalledTimes(1) + expect(errorSpy).toHaveBeenCalledWith( + "Error removing abandoned write_to_file artifacts:", + expect.any(Error), + ) + }) + it("treats a placeholder that vanished between the guard read and the unlink as already removed", async () => { + // The re-check must not turn a file that disappeared into a reported failure: this site already + // tolerates an unlink that finds nothing, and a file that is gone before the unlink is the same + // case arrived one await earlier. + const relPath = "vanished-after-guard-placeholder.ts" + const fsPath = `${mockCwd}/${relPath}` + const mockEditor = { + document: { + uri: { fsPath, scheme: "file" }, + getText: vi.fn().mockReturnValue(""), + isDirty: false, + save: vi.fn().mockResolvedValue(undefined), + lineCount: 0, + }, + selection: { active: { line: 0, character: 0 }, anchor: { line: 0, character: 0 } }, + edit: vi.fn().mockResolvedValue(true), + revealRange: vi.fn(), + } + // Structural double routed through unknown rather than any: only the members the discard + // touches, the same double shape the row above uses. + const editor = mockEditor as unknown as vscode.TextEditor + vi.mocked(vscode.window).visibleTextEditors = [editor] + vi.mocked(vscode.window.showTextDocument).mockResolvedValue(editor) + vi.mocked(vscode.workspace.onDidOpenTextDocument).mockImplementation((callback) => { + setTimeout(() => callback({ uri: { fsPath, scheme: "file" } } as vscode.TextDocument), 0) + return { dispose: vi.fn() } + }) + vi.mocked(vscode.window.onDidChangeVisibleTextEditors).mockReturnValue({ dispose: vi.fn() }) + vi.mocked(vscode.window.onDidChangeTextEditorVisibleRanges).mockReturnValue({ dispose: vi.fn() }) + vi.mocked(vscode.languages.getDiagnostics).mockReturnValue([]) + diffViewProvider.editType = "create" + vi.mocked(fs.readFile).mockResolvedValue("") + + await diffViewProvider.open(relPath) + expect(fs.writeFile).toHaveBeenCalledWith(fsPath, "") + + // Read one: still the placeholder open() wrote. Read two: the file is gone, so the re-check + // takes its own ENOENT branch, and the unlink that follows finds nothing either. Neither + // tolerance is allowed to turn into a reported failure. + const enoent = Object.assign(new Error("ENOENT: no such file or directory"), { code: "ENOENT" }) + vi.mocked(fs.readFile).mockResolvedValueOnce("").mockRejectedValueOnce(enoent) + vi.mocked(fs.unlink).mockRejectedValueOnce(enoent) + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + + await expect(diffViewProvider.discardUnapprovedStream()).resolves.toBeUndefined() + + expect(fs.unlink).toHaveBeenCalledWith(fsPath) + expect(fs.writeFile).toHaveBeenCalledTimes(1) + expect(errorSpy).not.toHaveBeenCalledWith( + "Error removing abandoned write_to_file artifacts:", + expect.anything(), + ) + }) + + it("reports the failure when the re-check's own read fails for a reason other than the file being gone", async () => { + // The re-check tolerates the file having vanished and rethrows anything else, because a read that + // proved nothing about the file does not license removing it. Every earlier row let this read + // succeed, so its failure path was never executed at all. + + const relPath = "denied-recheck-placeholder.ts" + const fsPath = `${mockCwd}/${relPath}` + const mockEditor = { + document: { + uri: { fsPath, scheme: "file" }, + getText: vi.fn().mockReturnValue(""), + isDirty: false, + save: vi.fn().mockResolvedValue(undefined), + lineCount: 0, + }, + selection: { active: { line: 0, character: 0 }, anchor: { line: 0, character: 0 } }, + edit: vi.fn().mockResolvedValue(true), + revealRange: vi.fn(), + } + // Structural double routed through unknown rather than any: only the members the discard + // touches, the same double shape the neighbouring rows use. + const editor = mockEditor as unknown as vscode.TextEditor + vi.mocked(vscode.window).visibleTextEditors = [editor] + vi.mocked(vscode.window.showTextDocument).mockResolvedValue(editor) + vi.mocked(vscode.workspace.onDidOpenTextDocument).mockImplementation((callback) => { + setTimeout(() => callback({ uri: { fsPath, scheme: "file" } } as vscode.TextDocument), 0) + return { dispose: vi.fn() } + }) + vi.mocked(vscode.window.onDidChangeVisibleTextEditors).mockReturnValue({ dispose: vi.fn() }) + vi.mocked(vscode.window.onDidChangeTextEditorVisibleRanges).mockReturnValue({ dispose: vi.fn() }) + vi.mocked(vscode.languages.getDiagnostics).mockReturnValue([]) + diffViewProvider.editType = "create" + vi.mocked(fs.readFile).mockResolvedValue("") + + await diffViewProvider.open(relPath) + expect(fs.writeFile).toHaveBeenCalledWith(fsPath, "") + + // Read one still agrees; read two is refused for a reason that says nothing about the file. + const denied: NodeJS.ErrnoException = new Error("EACCES: permission denied, read") + denied.code = "EACCES" + vi.mocked(fs.readFile).mockResolvedValueOnce("").mockRejectedValue(denied) + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + + await expect(diffViewProvider.discardUnapprovedStream()).rejects.toThrow(/permission denied, read/) + expect(fs.unlink).not.toHaveBeenCalledWith(fsPath) + expect(fs.writeFile).toHaveBeenCalledTimes(1) + expect(errorSpy).toHaveBeenCalledWith( + "Error removing abandoned write_to_file artifacts:", + expect.objectContaining({ code: "EACCES" }), + ) + }) + + it("treats a re-check that rejected with no error object as a cleanup it could not finish", async () => { + // A promise can reject with no value at all. The optional chain on this catch is what keeps that + // from becoming a crash: the re-check then proves nothing, the file is left alone, and the discard + // resolves with the report this site already makes, the same way it does when the guard read is + // the one that says nothing. + + const relPath = "reasonless-recheck-placeholder.ts" + const fsPath = `${mockCwd}/${relPath}` + const mockEditor = { + document: { + uri: { fsPath, scheme: "file" }, + getText: vi.fn().mockReturnValue(""), + isDirty: false, + save: vi.fn().mockResolvedValue(undefined), + lineCount: 0, + }, + selection: { active: { line: 0, character: 0 }, anchor: { line: 0, character: 0 } }, + edit: vi.fn().mockResolvedValue(true), + revealRange: vi.fn(), + } + // Structural double routed through unknown rather than any: only the members the discard + // touches, the same double shape the neighbouring rows use. + const editor = mockEditor as unknown as vscode.TextEditor + vi.mocked(vscode.window).visibleTextEditors = [editor] + vi.mocked(vscode.window.showTextDocument).mockResolvedValue(editor) + vi.mocked(vscode.workspace.onDidOpenTextDocument).mockImplementation((callback) => { + setTimeout(() => callback({ uri: { fsPath, scheme: "file" } } as vscode.TextDocument), 0) + return { dispose: vi.fn() } + }) + vi.mocked(vscode.window.onDidChangeVisibleTextEditors).mockReturnValue({ dispose: vi.fn() }) + vi.mocked(vscode.window.onDidChangeTextEditorVisibleRanges).mockReturnValue({ dispose: vi.fn() }) + vi.mocked(vscode.languages.getDiagnostics).mockReturnValue([]) + diffViewProvider.editType = "create" + vi.mocked(fs.readFile).mockResolvedValue("") + + await diffViewProvider.open(relPath) + expect(fs.writeFile).toHaveBeenCalledWith(fsPath, "") + + // Read one agrees; read two rejects with nothing in the rejection. + vi.mocked(fs.readFile).mockResolvedValueOnce("").mockRejectedValue(undefined) + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + + await expect(diffViewProvider.discardUnapprovedStream()).resolves.toBeUndefined() + expect(errorSpy).toHaveBeenCalledWith("Error removing abandoned write_to_file artifacts:", undefined) + expect(fs.unlink).not.toHaveBeenCalledWith(fsPath) + expect(fs.writeFile).toHaveBeenCalledTimes(1) + }) + + it("keeps the file when the guard read found foreign content even though a later read finds it back", async () => { + // The guard's answer is the decisive one: it ran before the awaited buffer work, and a re-read that + // finds the placeholder content again after a read that found something else does not undo that + // finding. The discard refuses both the save and the removal and reports the kept file, rather + // than letting the second answer overwrite the first. + + const relPath = "rewritten-then-restored-placeholder.ts" + const fsPath = `${mockCwd}/${relPath}` + const mockEditor = { + document: { + uri: { fsPath, scheme: "file" }, + getText: vi.fn().mockReturnValue(""), + isDirty: false, + save: vi.fn().mockResolvedValue(undefined), + lineCount: 0, + }, + selection: { active: { line: 0, character: 0 }, anchor: { line: 0, character: 0 } }, + edit: vi.fn().mockResolvedValue(true), + revealRange: vi.fn(), + } + // Structural double routed through unknown rather than any: only the members the discard + // touches, the same double shape the neighbouring rows use. + const editor = mockEditor as unknown as vscode.TextEditor + vi.mocked(vscode.window).visibleTextEditors = [editor] + vi.mocked(vscode.window.showTextDocument).mockResolvedValue(editor) + vi.mocked(vscode.workspace.onDidOpenTextDocument).mockImplementation((callback) => { + setTimeout(() => callback({ uri: { fsPath, scheme: "file" } } as vscode.TextDocument), 0) + return { dispose: vi.fn() } + }) + vi.mocked(vscode.window.onDidChangeVisibleTextEditors).mockReturnValue({ dispose: vi.fn() }) + vi.mocked(vscode.window.onDidChangeTextEditorVisibleRanges).mockReturnValue({ dispose: vi.fn() }) + vi.mocked(vscode.languages.getDiagnostics).mockReturnValue([]) + diffViewProvider.editType = "create" + vi.mocked(fs.readFile).mockResolvedValue("") + + await diffViewProvider.open(relPath) + expect(fs.writeFile).toHaveBeenCalledWith(fsPath, "") + + // The guard read is the only read this path makes: its answer is decisive, so the row queues + // exactly the one value it uses rather than a second that would outlive the test. + vi.mocked(fs.readFile).mockResolvedValueOnce("content a formatter rewrote\n") + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + + await expect(diffViewProvider.discardUnapprovedStream()).rejects.toThrow( + /no longer holds the empty placeholder/, + ) + expect(fs.unlink).not.toHaveBeenCalledWith(fsPath) + expect(fs.writeFile).toHaveBeenCalledTimes(1) + expect(errorSpy).toHaveBeenCalledWith( + "Error removing abandoned write_to_file artifacts:", + expect.any(Error), + ) + }) + + it("does not touch a file the guard read could not read even when a later read would have agreed", async () => { + // A guard read that failed for a reason other than the file being gone is the case where the + // discard knows nothing about the file, and it has to stop there: entering the unlink path anyway + // because a later read happened to agree would remove a file the method never proved anything + // about. The failure is reported the way this site reports every cleanup it could not finish. + + const relPath = "unreadable-then-readable-placeholder.ts" + const fsPath = `${mockCwd}/${relPath}` + const mockEditor = { + document: { + uri: { fsPath, scheme: "file" }, + getText: vi.fn().mockReturnValue(""), + isDirty: false, + save: vi.fn().mockResolvedValue(undefined), + lineCount: 0, + }, + selection: { active: { line: 0, character: 0 }, anchor: { line: 0, character: 0 } }, + edit: vi.fn().mockResolvedValue(true), + revealRange: vi.fn(), + } + // Structural double routed through unknown rather than any: only the members the discard + // touches, the same double shape the neighbouring rows use. + const editor = mockEditor as unknown as vscode.TextEditor + vi.mocked(vscode.window).visibleTextEditors = [editor] + vi.mocked(vscode.window.showTextDocument).mockResolvedValue(editor) + vi.mocked(vscode.workspace.onDidOpenTextDocument).mockImplementation((callback) => { + setTimeout(() => callback({ uri: { fsPath, scheme: "file" } } as vscode.TextDocument), 0) + return { dispose: vi.fn() } + }) + vi.mocked(vscode.window.onDidChangeVisibleTextEditors).mockReturnValue({ dispose: vi.fn() }) + vi.mocked(vscode.window.onDidChangeTextEditorVisibleRanges).mockReturnValue({ dispose: vi.fn() }) + vi.mocked(vscode.languages.getDiagnostics).mockReturnValue([]) + diffViewProvider.editType = "create" + vi.mocked(fs.readFile).mockResolvedValue("") + + await diffViewProvider.open(relPath) + expect(fs.writeFile).toHaveBeenCalledWith(fsPath, "") + + const denied: NodeJS.ErrnoException = new Error("EACCES: permission denied, read") + denied.code = "EACCES" + // The guard read is refused for a reason that says nothing about the file, and it is the last + // read the discard makes here: a stopped cleanup never gets to agree with anything. + // The failure is queued rather than made the default, so the row leaves nothing behind in + // either direction - and a reading that did go on to read again would read the plain + // default above and remove the file this reading proved nothing about. + vi.mocked(fs.readFile).mockRejectedValueOnce(denied) + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + + await expect(diffViewProvider.discardUnapprovedStream()).rejects.toThrow(/permission denied, read/) + expect(fs.unlink).not.toHaveBeenCalledWith(fsPath) + expect(fs.writeFile).toHaveBeenCalledTimes(1) + expect(errorSpy).toHaveBeenCalledWith( + "Error removing abandoned write_to_file artifacts:", + expect.objectContaining({ code: "EACCES" }), + ) + }) + + it("reports an unlink that rejected with no error object instead of throwing past the discard", async () => { + // The optional chain on this catch is what makes a rejection carrying nothing a cleanup that could + // not be finished rather than a crash inside the catch: the discard reports it through the channel + // this site already has and returns, the way it returns when the guard read is the one that carried + // nothing. The row above this file's other ENOENT rows covers an unlink failure that does carry a + // code; this one covers the rejection with no object to carry one, which is what the chain is for. + + const relPath = "reasonless-unlink-placeholder.ts" + const fsPath = `${mockCwd}/${relPath}` + const mockEditor = { + document: { + uri: { fsPath, scheme: "file" }, + getText: vi.fn().mockReturnValue(""), + isDirty: false, + save: vi.fn().mockResolvedValue(undefined), + lineCount: 0, + }, + selection: { active: { line: 0, character: 0 }, anchor: { line: 0, character: 0 } }, + edit: vi.fn().mockResolvedValue(true), + revealRange: vi.fn(), + } + // Structural double routed through unknown rather than any: only the members the discard + // touches, the same double shape the neighbouring rows use. + const editor = mockEditor as unknown as vscode.TextEditor + vi.mocked(vscode.window).visibleTextEditors = [editor] + vi.mocked(vscode.window.showTextDocument).mockResolvedValue(editor) + vi.mocked(vscode.workspace.onDidOpenTextDocument).mockImplementation((callback) => { + setTimeout(() => callback({ uri: { fsPath, scheme: "file" } } as vscode.TextDocument), 0) + return { dispose: vi.fn() } + }) + vi.mocked(vscode.window.onDidChangeVisibleTextEditors).mockReturnValue({ dispose: vi.fn() }) + vi.mocked(vscode.window.onDidChangeTextEditorVisibleRanges).mockReturnValue({ dispose: vi.fn() }) + vi.mocked(vscode.languages.getDiagnostics).mockReturnValue([]) + diffViewProvider.editType = "create" + vi.mocked(fs.readFile).mockResolvedValue("") + + await diffViewProvider.open(relPath) + expect(fs.writeFile).toHaveBeenCalledWith(fsPath, "") + + // Both reads agree, so the removal is attempted, and it rejects with nothing in the rejection. + vi.mocked(fs.unlink).mockRejectedValueOnce(undefined) + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + + await expect(diffViewProvider.discardUnapprovedStream()).resolves.toBeUndefined() + + // Reported, not swallowed: the file was not removed and nobody proved it was gone, so the + // discard says so on the channel it already uses and leaves the failure unrecorded rather than + // rethrowing a value that carries no message. + expect(fs.unlink).toHaveBeenCalledWith(fsPath) + expect(errorSpy).toHaveBeenCalledWith("Error removing abandoned write_to_file artifacts:", undefined) + expect(fs.writeFile).toHaveBeenCalledTimes(1) + }) + + it("tolerates a placeholder somebody else already deleted instead of reporting a cleanup failure", async () => { + // The guard reads the file before removing it, so a placeholder that is already gone is not + // a failure: there is nothing left to remove. That is the ENOENT tolerance this site had + // before the read was added, kept on the new path rather than lost to it. + const relPath = "already-gone-placeholder.ts" + const fsPath = `${mockCwd}/${relPath}` + const mockEditor = { + document: { + uri: { fsPath, scheme: "file" }, + getText: vi.fn().mockReturnValue(""), + isDirty: false, + save: vi.fn().mockResolvedValue(undefined), + lineCount: 0, + }, + selection: { active: { line: 0, character: 0 }, anchor: { line: 0, character: 0 } }, + edit: vi.fn().mockResolvedValue(true), + revealRange: vi.fn(), + } + // Structural double routed through unknown rather than any: only the members open() touches. + const editor = mockEditor as unknown as vscode.TextEditor + vi.mocked(vscode.window).visibleTextEditors = [editor] + vi.mocked(vscode.window.showTextDocument).mockResolvedValue(editor) + vi.mocked(vscode.workspace.onDidOpenTextDocument).mockImplementation((callback) => { + setTimeout(() => callback({ uri: { fsPath, scheme: "file" } } as vscode.TextDocument), 0) + return { dispose: vi.fn() } + }) + vi.mocked(vscode.window.onDidChangeVisibleTextEditors).mockReturnValue({ dispose: vi.fn() }) + vi.mocked(vscode.window.onDidChangeTextEditorVisibleRanges).mockReturnValue({ dispose: vi.fn() }) + vi.mocked(vscode.languages.getDiagnostics).mockReturnValue([]) + diffViewProvider.editType = "create" + vi.mocked(fs.readFile).mockResolvedValue("") + + await diffViewProvider.open(relPath) + expect(fs.writeFile).toHaveBeenCalledWith(fsPath, "") + + // Somebody else removed the file before the discard ran. + const missing: NodeJS.ErrnoException = new Error("ENOENT: no such file, already removed") + missing.code = "ENOENT" + vi.mocked(fs.readFile).mockRejectedValue(missing) + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + + await expect(diffViewProvider.discardUnapprovedStream()).resolves.toBeUndefined() + + expect(fs.unlink).not.toHaveBeenCalledWith(fsPath) + expect(errorSpy).not.toHaveBeenCalledWith( + "Error removing abandoned write_to_file artifacts:", + expect.anything(), + ) + }) + + it("does not save the emptied buffer when the guard read proved the placeholder is gone", async () => { + // The same ENOENT the unlink block already tolerates. A placeholder that is not there has + // nothing to save onto, and dropping the emptied create buffer onto that path recreates the + // empty file the discard just declined to remove. The buffer is still blanked, because the + // unapproved bytes must not survive in the editor either. + const relPath = "gone-before-save-placeholder.ts" + const fsPath = `${mockCwd}/${relPath}` + const mockEditor = { + document: { + uri: { fsPath, scheme: "file" }, + getText: vi.fn().mockReturnValue("partial model output"), + positionAt: (offset: number) => ({ line: 0, character: offset }), + isDirty: true, + save: vi.fn().mockResolvedValue(undefined), + lineCount: 0, + }, + selection: { active: { line: 0, character: 0 }, anchor: { line: 0, character: 0 } }, + edit: vi.fn().mockResolvedValue(true), + revealRange: vi.fn(), + } + // Structural double routed through unknown rather than any: only the members the discard + // touches, the same double shape the row above uses. + const editor = mockEditor as unknown as vscode.TextEditor + vi.mocked(vscode.window).visibleTextEditors = [editor] + vi.mocked(vscode.window.showTextDocument).mockResolvedValue(editor) + vi.mocked(vscode.workspace.onDidOpenTextDocument).mockImplementation((callback) => { + setTimeout(() => callback({ uri: { fsPath, scheme: "file" } } as vscode.TextDocument), 0) + return { dispose: vi.fn() } + }) + vi.mocked(vscode.window.onDidChangeVisibleTextEditors).mockReturnValue({ dispose: vi.fn() }) + vi.mocked(vscode.window.onDidChangeTextEditorVisibleRanges).mockReturnValue({ dispose: vi.fn() }) + vi.mocked(vscode.languages.getDiagnostics).mockReturnValue([]) + diffViewProvider.editType = "create" + vi.mocked(fs.readFile).mockResolvedValue("") + + await diffViewProvider.open(relPath) + expect(fs.writeFile).toHaveBeenCalledWith(fsPath, "") + + // Somebody else removed the file before the discard ran: the guard read reports ENOENT and + // leaves the recorded match untouched, which is what the save gate now has to notice. + const missing: NodeJS.ErrnoException = new Error("ENOENT: no such file, already removed") + missing.code = "ENOENT" + vi.mocked(fs.readFile).mockRejectedValue(missing) + vi.mocked(vscode.workspace.applyEdit).mockResolvedValue(true) + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + + await expect(diffViewProvider.discardUnapprovedStream()).resolves.toBeUndefined() + + // Blanked, not written: the replace ran, the save did not, and the only write to that path + // is still the empty placeholder open() made. Nothing was removed and nothing was reported. + expect(mockWorkspaceEdit.replace).toHaveBeenCalledTimes(1) + expect(editor.document.save).not.toHaveBeenCalled() + expect(fs.writeFile).toHaveBeenCalledTimes(1) + expect(fs.unlink).not.toHaveBeenCalledWith(fsPath) + expect(errorSpy).not.toHaveBeenCalledWith( + "Error removing abandoned write_to_file artifacts:", + expect.anything(), + ) + }) + + it("keeps the placeholder when the guard's read fails for a reason that does not say it is gone", async () => { + // The read that guards the unlink can fail for reasons that say nothing about the file being + // gone - a permission refusal, for example. Only ENOENT means there was nothing left to + // remove, so anything else is rethrown: the discard keeps the file and reports it through + // the console.error call this cleanup site already makes, and the method rejects with that + // error the way it rejects for an unlink it could not finish. + const relPath = "unreadable-placeholder.ts" + const fsPath = `${mockCwd}/${relPath}` + const mockEditor = { + document: { + uri: { fsPath, scheme: "file" }, + getText: vi.fn().mockReturnValue(""), + isDirty: false, + save: vi.fn().mockResolvedValue(undefined), + lineCount: 0, + }, + selection: { active: { line: 0, character: 0 }, anchor: { line: 0, character: 0 } }, + edit: vi.fn().mockResolvedValue(true), + revealRange: vi.fn(), + } + // Structural double routed through unknown rather than any: only the members open() touches. + const editor = mockEditor as unknown as vscode.TextEditor + vi.mocked(vscode.window).visibleTextEditors = [editor] + vi.mocked(vscode.window.showTextDocument).mockResolvedValue(editor) + vi.mocked(vscode.workspace.onDidOpenTextDocument).mockImplementation((callback) => { + setTimeout(() => callback({ uri: { fsPath, scheme: "file" } } as vscode.TextDocument), 0) + return { dispose: vi.fn() } + }) + vi.mocked(vscode.window.onDidChangeVisibleTextEditors).mockReturnValue({ dispose: vi.fn() }) + vi.mocked(vscode.window.onDidChangeTextEditorVisibleRanges).mockReturnValue({ dispose: vi.fn() }) + vi.mocked(vscode.languages.getDiagnostics).mockReturnValue([]) + diffViewProvider.editType = "create" + vi.mocked(fs.readFile).mockResolvedValue("") + + await diffViewProvider.open(relPath) + expect(fs.writeFile).toHaveBeenCalledWith(fsPath, "") + + // The guard's read now fails for a reason that is not 'already gone'. + const denied: NodeJS.ErrnoException = new Error("EACCES: permission denied, read") + denied.code = "EACCES" + vi.mocked(fs.readFile).mockRejectedValue(denied) + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + + await expect(diffViewProvider.discardUnapprovedStream()).rejects.toThrow(/permission denied, read/) + + // A read that said nothing about the file being gone does not license removing it. + expect(fs.unlink).not.toHaveBeenCalledWith(fsPath) + expect(fs.writeFile).toHaveBeenCalledTimes(1) + expect(errorSpy).toHaveBeenCalledWith( + "Error removing abandoned write_to_file artifacts:", + expect.objectContaining({ code: "EACCES" }), + ) + // The edit drops its claim on the path with the failure, so a later discard does not + // report the same kept file twice. + expect(diffViewProvider["placeholderPath"]).toBeUndefined() + expect(diffViewProvider["placeholderContent"]).toBeUndefined() + }) + + it("reports a guard read that rejected with no error object and touches nothing", async () => { + // The optional chain on the error binding is the whole difference between "this read proved + // nothing about the file" and "this read blew up": a promise can reject with no value at all, + // and reaching .code on that would throw inside the catch and escape the discard as some other + // failure - no cleanup report from this site, and no answer about the file. The row above + // covers a rejection that does carry a code; this one covers a rejection with no object to + // carry one, which is the case the optional chain exists for. + const relPath = "reasonless-rejection-placeholder.ts" + const fsPath = `${mockCwd}/${relPath}` + const mockEditor = { + document: { + uri: { fsPath, scheme: "file" }, + getText: vi.fn().mockReturnValue(""), + isDirty: false, + save: vi.fn().mockResolvedValue(undefined), + lineCount: 0, + }, + selection: { active: { line: 0, character: 0 }, anchor: { line: 0, character: 0 } }, + edit: vi.fn().mockResolvedValue(true), + revealRange: vi.fn(), + } + // Structural double routed through unknown rather than any: only the members the discard + // touches, the same double shape the neighbouring rows use. + const editor = mockEditor as unknown as vscode.TextEditor + vi.mocked(vscode.window).visibleTextEditors = [editor] + vi.mocked(vscode.window.showTextDocument).mockResolvedValue(editor) + vi.mocked(vscode.workspace.onDidOpenTextDocument).mockImplementation((callback) => { + setTimeout(() => callback({ uri: { fsPath, scheme: "file" } } as vscode.TextDocument), 0) + return { dispose: vi.fn() } + }) + vi.mocked(vscode.window.onDidChangeVisibleTextEditors).mockReturnValue({ dispose: vi.fn() }) + vi.mocked(vscode.window.onDidChangeTextEditorVisibleRanges).mockReturnValue({ dispose: vi.fn() }) + vi.mocked(vscode.languages.getDiagnostics).mockReturnValue([]) + diffViewProvider.editType = "create" + vi.mocked(fs.readFile).mockResolvedValue("") + + await diffViewProvider.open(relPath) + expect(fs.writeFile).toHaveBeenCalledWith(fsPath, "") + + // The guard read now rejects with nothing in the rejection: no code to read, so the catch + // can only conclude that the read proved nothing. + vi.mocked(fs.readFile).mockRejectedValue(undefined) + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) + + await expect(diffViewProvider.discardUnapprovedStream()).resolves.toBeUndefined() + + // Reported through the channel this cleanup site already has, with the file left exactly as it + // was found and no second write to it. + expect(errorSpy).toHaveBeenCalledWith("Error removing abandoned write_to_file artifacts:", undefined) + expect(fs.unlink).not.toHaveBeenCalledWith(fsPath) + expect(fs.writeFile).toHaveBeenCalledTimes(1) + }) + + it("removes the placeholder that a new-file open() wrote, driven through the public methods", async () => { + // The ownership lifecycle lives at the public-method layer, so exercise it there: + // open() writes the empty placeholder and records it, and the discard of an abandoned + // create removes exactly that path. + const relPath = "owned-placeholder.ts" + const fsPath = `${mockCwd}/${relPath}` + const mockEditor = { + document: { + uri: { fsPath, scheme: "file" }, + getText: vi.fn().mockReturnValue(""), + isDirty: false, + save: vi.fn().mockResolvedValue(undefined), + lineCount: 0, + }, + selection: { active: { line: 0, character: 0 }, anchor: { line: 0, character: 0 } }, + edit: vi.fn().mockResolvedValue(true), + revealRange: vi.fn(), + } + // Structural double for the mocked editor: the mock only implements the members + // open() touches, so it is routed through unknown rather than any. + const editor = mockEditor as unknown as vscode.TextEditor + vi.mocked(vscode.window).visibleTextEditors = [editor] + vi.mocked(vscode.window.showTextDocument).mockResolvedValue(editor) + vi.mocked(vscode.workspace.onDidOpenTextDocument).mockImplementation((callback) => { + setTimeout(() => callback({ uri: { fsPath, scheme: "file" } } as vscode.TextDocument), 0) + return { dispose: vi.fn() } + }) + vi.mocked(vscode.window.onDidChangeVisibleTextEditors).mockReturnValue({ dispose: vi.fn() }) + vi.mocked(vscode.window.onDidChangeTextEditorVisibleRanges).mockReturnValue({ dispose: vi.fn() }) + vi.mocked(vscode.languages.getDiagnostics).mockReturnValue([]) + diffViewProvider.editType = "create" + // The discard reads the placeholder back before unlinking it, so the double has to answer + // with the empty content open() wrote; the test above covers the mismatching answer. + vi.mocked(fs.readFile).mockResolvedValue("") + + await diffViewProvider.open(relPath) + + // open() created the placeholder, and this edit now owns it. + expect(fs.writeFile).toHaveBeenCalledWith(fsPath, "") + + await diffViewProvider.discardUnapprovedStream() + + expect(fs.unlink).toHaveBeenCalledWith(fsPath) + }) + + it("does not remove the file once saveChanges() has approved the content", async () => { + // saveChanges() turns the placeholder into approved content and drops this edit's + // claim on it, so a later abandoned cleanup must leave the approved file on disk. + const relPath = "approved-by-save.ts" + const fsPath = `${mockCwd}/${relPath}` + Object.assign(diffViewProvider, { + relPath, + newContent: "approved content", + editType: "create", + createdDirs: [], + placeholderPath: fsPath, + preDiagnostics: [], + activeDiffEditor: { + document: { + uri: { fsPath, scheme: "file" }, + getText: vi.fn().mockReturnValue("approved content"), + isDirty: false, + save: vi.fn().mockResolvedValue(undefined), + }, + }, + closeAllDiffViews: vi.fn().mockResolvedValue(undefined), + closeFileTab: vi.fn().mockResolvedValue(undefined), + }) + vi.mocked(vscode.languages.getDiagnostics).mockReturnValue([]) + vi.mocked(vscode.window.showTextDocument).mockResolvedValue(undefined as never) + vi.mocked(vscode.workspace.applyEdit).mockResolvedValue(true) + + await diffViewProvider.saveChanges(false, 0) + + await diffViewProvider.discardUnapprovedStream() + + expect(fs.unlink).not.toHaveBeenCalled() + }) + + it("does nothing when no abandoned view is open", async () => { + Object.assign(diffViewProvider, { relPath: undefined, activeDiffEditor: undefined }) + + await diffViewProvider.discardUnapprovedStream() + + expect(fs.unlink).not.toHaveBeenCalled() + }) + }) }) })