import { WebviewFocusTracker } from "../WebviewFocusTracker" // npx vitest core/webview/__tests__/ClineProvider.sticky-mode.spec.ts import * as vscode from "vscode" import { makeCompositeDisposable } from "../../../test-utils/vscode" import { TelemetryService } from "@roo-code/telemetry" import { ClineProvider } from "../ClineProvider" import { ContextProxy } from "../../config/ContextProxy" import { Task } from "../../task/Task" import { RooCodeEventName, type HistoryItem } from "@roo-code/types" import { providerIdentifiers } from "@roo-code/types/provider-identifiers" vi.mock("vscode", () => ({ ExtensionContext: vi.fn(), OutputChannel: vi.fn(), WebviewView: vi.fn(), Disposable: { from: (...subscriptions: vscode.Disposable[]) => makeCompositeDisposable(...subscriptions) }, Uri: { joinPath: vi.fn(), file: vi.fn(), }, CodeActionKind: { QuickFix: { value: "quickfix" }, RefactorRewrite: { value: "refactor.rewrite" }, }, commands: { executeCommand: vi.fn().mockResolvedValue(undefined), }, window: { showInformationMessage: vi.fn(), showWarningMessage: vi.fn(), showErrorMessage: vi.fn(), onDidChangeActiveTextEditor: vi.fn(() => ({ dispose: vi.fn() })), }, workspace: { getConfiguration: vi.fn().mockReturnValue({ get: vi.fn().mockReturnValue([]), update: vi.fn(), }), onDidChangeConfiguration: vi.fn().mockImplementation(() => { return { dispose: vi.fn(), } }), onDidSaveTextDocument: vi.fn(() => ({ dispose: vi.fn() })), onDidChangeTextDocument: vi.fn(() => ({ dispose: vi.fn() })), onDidOpenTextDocument: vi.fn(() => ({ dispose: vi.fn() })), onDidCloseTextDocument: vi.fn(() => ({ dispose: vi.fn() })), }, env: { uriScheme: "vscode", language: "en", appName: "Visual Studio Code", }, ExtensionMode: { Production: 1, Development: 2, Test: 3, }, version: "1.85.0", })) // Create a counter for unique task IDs. let taskIdCounter = 0 vi.mock("../../task/Task", () => ({ Task: vi.fn().mockImplementation(function (options) { return { taskId: options.taskId || `test-task-id-${++taskIdCounter}`, saveClineMessages: vi.fn(), clineMessages: [], apiConversationHistory: [], overwriteClineMessages: vi.fn(), overwriteApiConversationHistory: vi.fn(), abortTask: vi.fn(), handleWebviewAskResponse: vi.fn(), getTaskNumber: vi.fn().mockReturnValue(0), setTaskNumber: vi.fn(), setParentTask: vi.fn(), setRootTask: vi.fn(), emit: vi.fn(), parentTask: options.parentTask, updateApiConfiguration: vi.fn(), } }), })) vi.mock("../../prompts/sections/custom-instructions") vi.mock("../../../utils/safeWriteJson") vi.mock("../../../api", () => ({ buildApiHandler: vi.fn().mockReturnValue({ getModel: vi.fn().mockReturnValue({ id: "claude-3-sonnet", }), }), })) vi.mock("../../../integrations/workspace/WorkspaceTracker", () => ({ default: vi.fn().mockImplementation(function () { return { initializeFilePaths: vi.fn(), dispose: vi.fn(), } }), })) vi.mock("../../diff/strategies/multi-search-replace", () => ({ MultiSearchReplaceDiffStrategy: vi.fn().mockImplementation(function () { return { getName: () => "test-strategy", applyDiff: vi.fn(), } }), })) vi.mock("@roo-code/cloud", () => ({ CloudService: { hasInstance: vi.fn().mockReturnValue(true), get instance() { return { isAuthenticated: vi.fn().mockReturnValue(false), on: vi.fn(), off: vi.fn(), } }, }, getRooCodeApiUrl: vi.fn().mockReturnValue("https://app.roocode.com"), })) vi.mock("../../../shared/modes", () => ({ modes: [ { slug: "code", name: "Code Mode", roleDefinition: "You are a code assistant", groups: ["read", "edit"], }, { slug: "architect", name: "Architect Mode", roleDefinition: "You are an architect", groups: ["read", "edit"], }, ], getModeBySlug: vi.fn().mockReturnValue({ slug: "code", name: "Code Mode", roleDefinition: "You are a code assistant", groups: ["read", "edit"], }), defaultModeSlug: "code", })) vi.mock("../../prompts/system", () => ({ SYSTEM_PROMPT: vi.fn().mockResolvedValue("mocked system prompt"), codeMode: "code", })) vi.mock("../../../api/providers/fetchers/modelCache", () => ({ getModels: vi.fn().mockResolvedValue({}), flushModels: vi.fn(), getModelsFromCache: vi.fn().mockReturnValue(undefined), })) vi.mock("../../../integrations/misc/extract-text", () => ({ extractTextFromFile: vi.fn().mockResolvedValue("Mock file content"), })) vi.mock("p-wait-for", () => ({ default: vi.fn().mockImplementation(async () => Promise.resolve()), })) vi.mock("fs/promises", () => ({ mkdir: vi.fn().mockResolvedValue(undefined), writeFile: vi.fn().mockResolvedValue(undefined), readFile: vi.fn().mockResolvedValue(""), readdir: vi.fn().mockResolvedValue([]), unlink: vi.fn().mockResolvedValue(undefined), rmdir: vi.fn().mockResolvedValue(undefined), access: vi.fn().mockResolvedValue(undefined), rm: vi.fn().mockResolvedValue(undefined), })) vi.mock("../../../utils/storage", async (importOriginal) => { const actual = await importOriginal() return { ...actual, getStorageBasePath: vi.fn().mockImplementation((defaultPath: string) => { return defaultPath }), getSettingsDirectoryPath: vi.fn().mockResolvedValue("/test/settings/path"), getTaskDirectoryPath: vi.fn().mockResolvedValue("/test/task/path"), } }) vi.mock("@roo-code/telemetry", async (importOriginal) => { const actual = await importOriginal() return { // Keep the real hashDistinctId: getStateToPostToWebview hashes the machine ID // before posting it to the webview (issue #830). ...actual, TelemetryService: { hasInstance: vi.fn().mockReturnValue(true), createInstance: vi.fn(), get instance() { return { trackEvent: vi.fn(), trackError: vi.fn(), setProvider: vi.fn(), captureModeSwitch: vi.fn(), } }, }, } }) describe("ClineProvider - Sticky Mode", () => { let provider: ClineProvider let mockContext: vscode.ExtensionContext let mockOutputChannel: vscode.OutputChannel let mockWebviewView: vscode.WebviewView let mockPostMessage: any async function seedTaskHistory(items: HistoryItem[]) { for (const item of items) { await provider.taskHistoryStore.upsert(item) } } beforeEach(async () => { vi.clearAllMocks() if (!TelemetryService.hasInstance()) { TelemetryService.createInstance([]) } const globalState: Record = { mode: "code", currentApiConfigName: "test-config", } const secrets: Record = {} mockContext = { extensionPath: "/test/path", extensionUri: { fsPath: "/test/path" } as vscode.Uri, globalState: { get: vi.fn().mockImplementation((key: string) => { return globalState[key] }), update: vi.fn().mockImplementation((key: string, value: string | undefined) => { globalState[key] = value return Promise.resolve() }), keys: vi.fn().mockImplementation(() => { return Object.keys(globalState) }), }, secrets: { get: vi.fn().mockImplementation((key: string) => { return secrets[key] }), store: vi.fn().mockImplementation((key: string, value: string | undefined) => { secrets[key] = value return Promise.resolve() }), delete: vi.fn().mockImplementation((key: string) => { delete secrets[key] return Promise.resolve() }), }, workspaceState: { get: vi.fn().mockReturnValue(undefined), update: vi.fn().mockResolvedValue(undefined), keys: vi.fn().mockReturnValue([]), }, subscriptions: [], extension: { packageJSON: { version: "1.0.0" }, }, globalStorageUri: { fsPath: "/test/storage/path", }, } as unknown as vscode.ExtensionContext mockOutputChannel = { appendLine: vi.fn(), clear: vi.fn(), dispose: vi.fn(), } as unknown as vscode.OutputChannel mockPostMessage = vi.fn() mockWebviewView = { webview: { postMessage: mockPostMessage, html: "", options: {}, onDidReceiveMessage: vi.fn(), asWebviewUri: vi.fn(), cspSource: "vscode-webview://test-csp-source", }, visible: true, // Register only: firing the callback here disposes the view before resolveWebviewView finishes, // which sends the later stale-check down the sidebar branch. Tests that need a disposal fire it. onDidDispose: vi.fn().mockImplementation(() => ({ dispose: vi.fn() })), onDidChangeVisibility: vi.fn().mockImplementation(() => { return { dispose: vi.fn() } }), } as unknown as vscode.WebviewView provider = new ClineProvider( mockContext, mockOutputChannel, "sidebar", new ContextProxy(mockContext), new WebviewFocusTracker(), ) // Wait for the async TaskHistoryStore initialization to complete await new Promise((resolve) => setTimeout(resolve, 10)) // Mock getMcpHub method provider.getMcpHub = vi.fn().mockReturnValue({ listTools: vi.fn().mockResolvedValue([]), callTool: vi.fn().mockResolvedValue({ content: [] }), listResources: vi.fn().mockResolvedValue([]), readResource: vi.fn().mockResolvedValue({ contents: [] }), getAllServers: vi.fn().mockReturnValue([]), }) }) describe("handleModeSwitch", () => { beforeEach(async () => { await provider.resolveWebviewView(mockWebviewView) }) it("should save mode to task metadata when switching modes", async () => { // Create a mock task const mockTask = new Task({ provider, apiConfiguration: { apiProvider: providerIdentifiers.openrouter }, }) // Get the actual taskId from the mock const taskId = (mockTask as any).taskId || "test-task-id" // Seed the authoritative file-backed task history. await seedTaskHistory([ { id: taskId, ts: Date.now(), task: "Test task", number: 1, tokensIn: 0, tokensOut: 0, cacheWrites: 0, cacheReads: 0, totalCost: 0, }, ]) // Mock updateTaskHistory to track calls const updateTaskHistorySpy = vi.spyOn(provider, "updateTaskHistory").mockImplementation(() => { return Promise.resolve([]) }) // Add task to provider stack await provider.addClineToStack(mockTask) // Switch mode await provider.handleModeSwitch("architect") // Verify mode was updated in global state expect(mockContext.globalState.update).toHaveBeenCalledWith("mode", "architect") // Verify task history was updated with new mode expect(updateTaskHistorySpy).toHaveBeenCalledWith( expect.objectContaining({ id: taskId, mode: "architect", }), ) }) it("should update task's taskMode property when switching modes", async () => { // Create a mock task with initial mode const mockTask = { taskId: "test-task-id", taskMode: "code", // Initial mode emit: vi.fn(), saveClineMessages: vi.fn(), clineMessages: [], apiConversationHistory: [], updateApiConfiguration: vi.fn(), } // Add task to provider stack await provider.addClineToStack(mockTask as any) // Seed the authoritative file-backed task history. await seedTaskHistory([ { id: mockTask.taskId, ts: Date.now(), task: "Test task", number: 1, tokensIn: 0, tokensOut: 0, cacheWrites: 0, cacheReads: 0, totalCost: 0, }, ]) // Mock updateTaskHistory vi.spyOn(provider, "updateTaskHistory").mockImplementation(() => { return Promise.resolve([]) }) // Switch mode await provider.handleModeSwitch("architect") // Verify task's _taskMode property was updated (using private property) expect((mockTask as any)._taskMode).toBe("architect") // Verify emit was called with taskModeSwitched event expect(mockTask.emit).toHaveBeenCalledWith("taskModeSwitched", mockTask.taskId, "architect") }) it("should update task history with new mode when active task exists", async () => { // Create a mock task with history const mockTask = new Task({ provider, apiConfiguration: { apiProvider: providerIdentifiers.openrouter }, }) // Get the actual taskId from the mock const taskId = (mockTask as any).taskId || "test-task-id" // Seed the authoritative file-backed task history. await seedTaskHistory([ { id: taskId, ts: Date.now(), task: "Test task", number: 1, tokensIn: 0, tokensOut: 0, cacheWrites: 0, cacheReads: 0, totalCost: 0, }, ]) // Mock updateTaskHistory to track calls const updateTaskHistorySpy = vi.spyOn(provider, "updateTaskHistory").mockImplementation(() => { return Promise.resolve([]) }) // Add task to provider stack await provider.addClineToStack(mockTask) // Switch mode await provider.handleModeSwitch("architect") // Verify updateTaskHistory was called with mode in the history item expect(updateTaskHistorySpy).toHaveBeenCalledWith( expect.objectContaining({ id: taskId, mode: "architect", }), ) }) it("does not announce a mode switch whose view-state write failed, and puts the history entry back", async () => { const mockTask = new Task({ provider, apiConfiguration: { apiProvider: providerIdentifiers.openrouter } }) const taskId = mockTask.taskId // Seed the authoritative file-backed task history. await seedTaskHistory([ { id: taskId, ts: Date.now(), task: "Test task", number: 1, mode: "code", tokensIn: 0, tokensOut: 0, cacheWrites: 0, cacheReads: 0, totalCost: 0, }, ]) await provider.addClineToStack(mockTask) mockTask["_taskMode"] = "code" // Only the per-view (viewStates) record fails; the shared write lands and setValue // restores it - the task changes are what this row is about. const originalViewWrite = provider["_saveViewLocalStateFromMutation"] const viewWrite = vi.fn(() => Promise.reject(new Error("view-state record failed")), ) provider["_saveViewLocalStateFromMutation"] = viewWrite const updateHistorySpy = vi.spyOn(provider, "updateTaskHistory") const emitSpy = vi.spyOn(mockTask, "emit") try { await expect(provider.handleModeSwitch("architect", mockTask)).rejects.toThrow( "view-state record failed", ) expect(emitSpy).not.toHaveBeenCalledWith(RooCodeEventName.TaskModeSwitched, taskId, "architect") expect(mockTask["_taskMode"]).toBe("code") expect(updateHistorySpy).toHaveBeenCalledTimes(2) expect(provider.taskHistoryStore.get(taskId)?.mode).toBe("code") } finally { provider["_saveViewLocalStateFromMutation"] = originalViewWrite updateHistorySpy.mockRestore() emitSpy.mockRestore() } }) it("announces the switch only after the view's own mode record has been written", async () => { const mockTask = new Task({ provider, apiConfiguration: { apiProvider: providerIdentifiers.openrouter } }) const taskId = mockTask.taskId // Seed the authoritative file-backed task history. await seedTaskHistory([ { id: taskId, ts: Date.now(), task: "Test task", number: 1, mode: "code", tokensIn: 0, tokensOut: 0, cacheWrites: 0, cacheReads: 0, totalCost: 0, }, ]) await provider.addClineToStack(mockTask) mockTask["_taskMode"] = "code" // Pass-through spy on the durable per-view (viewStates) write, so the ordering of the // announcement relative to that write is what is measured. const originalViewWrite = provider["_saveViewLocalStateFromMutation"] const viewWrite = vi.fn(async (values) => originalViewWrite.call(provider, values), ) provider["_saveViewLocalStateFromMutation"] = viewWrite const emitSpy = vi.spyOn(mockTask, "emit") try { await provider.handleModeSwitch("architect", mockTask) expect(emitSpy).toHaveBeenCalledWith(RooCodeEventName.TaskModeSwitched, taskId, "architect") expect(provider.taskHistoryStore.get(taskId)?.mode).toBe("architect") expect(mockTask["_taskMode"]).toBe("architect") // The switch is announced after the durable writes land, not before them. const announcedAt = emitSpy.mock.calls.findIndex( (call: unknown[]) => call[0] === RooCodeEventName.TaskModeSwitched, ) expect(announcedAt).toBeGreaterThanOrEqual(0) expect(viewWrite.mock.invocationCallOrder[0]).toBeLessThan( emitSpy.mock.invocationCallOrder[announcedAt], ) } finally { provider["_saveViewLocalStateFromMutation"] = originalViewWrite emitSpy.mockRestore() } }) }) describe("persisted view state keys", () => { it("does not resolve a view id that names an Object.prototype key through the chain", async () => { await provider.resolveWebviewView(mockWebviewView) // Seed through the durable write path so later writes land in the same // backing map the assertions read: a frozen get() literal would make every // post-write assertion read the seed no matter what the provider wrote. await provider.contextProxy.setValue("viewStates", { "stable-test-view": { mode: "architect" } }) const states = provider["getPersistedViewStates"]({ fresh: true }) expect(Object.getPrototypeOf(states)).toBeNull() // A view id such as "constructor" must read as an absent entry, not as the // inherited Object function, otherwise the rekey deletes the temporary entry // and never writes it under the stable key. expect(states["constructor"]).toBeUndefined() expect(states["stable-test-view"]).toEqual({ mode: "architect" }) // Registering such an id must not make an inherited member look like a // persisted entry: the lookup still reports an absent entry, so the rekey keeps the // temporary entry instead of deleting it. await provider["setViewStateId"]("constructor") expect(provider["getPersistedViewStates"]({ fresh: true })["constructor"]).toBeUndefined() }) it("does not load a temporary-id entry that an earlier session wrote", async () => { // Temporary names restart from 0 on every host start, so storage can still hold // an entry under this instance's name from a previous session. Seed the entry // through the ContextProxy before the provider is constructed so the ordinary // loadViewState read (which goes through the proxy cache) actually sees it. const tempId = `sidebar-${ClineProvider["nextViewId"]}` const proxy = new ContextProxy(mockContext) await proxy.setValue("viewStates", { [tempId]: { mode: "architect", currentApiConfigName: "stale-profile" }, }) const fresh = new ClineProvider(mockContext, mockOutputChannel, "sidebar", proxy, new WebviewFocusTracker()) expect(fresh.viewId).toBe(tempId) // Await the load itself instead of a fixed timer: the negative // assertion must run after the load has definitely finished. await fresh["loadViewState"]() // The unowned temporary-id entry must not be adopted... expect(fresh["viewLocalState"]).toStrictEqual({}) // ...and the shared values still win, so the stale entry cannot reach getState(). const state = await fresh.getState() expect(state.mode).toBe("code") expect(state.currentApiConfigName).not.toBe("stale-profile") }) it("does not merge a stale temporary-id entry into this instance's first durable write", async () => { // A first write under an unowned temporary id claims the slot: fields left by an // earlier session under the same name must not be adopted (and later re-keyed to // the stable id) by merging into the colliding entry. const tempId = `sidebar-${ClineProvider["nextViewId"]}` const proxy = new ContextProxy(mockContext) await proxy.setValue("viewStates", { [tempId]: { currentApiConfigName: "old-profile", updatedAt: 1 } }) const fresh = new ClineProvider(mockContext, mockOutputChannel, "sidebar", proxy, new WebviewFocusTracker()) expect(fresh.viewId).toBe(tempId) await fresh.saveViewState("mode", "code") const states = fresh["getPersistedViewStates"]({ fresh: true }) expect(states[tempId]).toEqual({ mode: "code", updatedAt: expect.any(Number), claimToken: expect.any(String), }) expect(fresh["viewLocalState"].currentApiConfigName).toBeUndefined() }) it("does not mark the temporary id as authored when the durable write fails", async () => { // Authorship must be recorded only after the write lands: a failed persistence // that still marked the id owned would let a later load adopt an entry that was // never actually stored. const tempId = `sidebar-${ClineProvider["nextViewId"]}` const proxy = new ContextProxy(mockContext) const fresh = new ClineProvider(mockContext, mockOutputChannel, "sidebar", proxy, new WebviewFocusTracker()) expect(fresh.viewId).toBe(tempId) const originalUpdate = mockContext.globalState.update mockContext.globalState.update = vi.fn().mockImplementation((key: string, _value: unknown) => { if (key === "viewStates") { return Promise.reject(new Error("storage unavailable")) } return Promise.resolve() }) await expect(fresh.saveViewState("mode", "code")).rejects.toThrow("storage unavailable") expect(fresh["wroteUnderTemporaryViewStateId"]).toBe(false) mockContext.globalState.update = originalUpdate }) it("does not adopt an entry under the temporary name that this instance never wrote", async () => { await provider.resolveWebviewView(mockWebviewView) // Temporary ids restart from 0 on every host start, so the same name can // belong to a different session. An entry this instance never wrote must not // be adopted as this view's state. // Seed through the durable write path so the assertions below read the // same backing map a re-key adoption would write to: a frozen get() // literal made the negative assertion unable to fail. await provider.contextProxy.setValue("viewStates", { [provider.viewId]: { mode: "architect" } }) await provider["setViewStateId"]("stable-test-view") const states = provider["getPersistedViewStates"]({ fresh: true }) expect(states[provider.viewId]).toEqual({ mode: "architect" }) expect(states["stable-test-view"]).toBeUndefined() }) }) describe("createTaskWithHistoryItem", () => { it("should restore mode from history item when reopening task", async () => { await provider.resolveWebviewView(mockWebviewView) // Create a history item with saved mode const historyItem: HistoryItem = { id: "test-task-id", number: 1, ts: Date.now(), task: "Test task", tokensIn: 100, tokensOut: 200, cacheWrites: 0, cacheReads: 0, totalCost: 0.001, mode: "architect", // Saved mode } // Register a stable view id so the durable per-view write is persisted await provider["setViewStateId"]("stable-test-view") // Initialize task with history item await provider.createTaskWithHistoryItem(historyItem) // Verify mode was restored into the view-local pin (no shared global write) expect(provider["viewLocalState"].mode).toBe("architect") expect(mockContext.globalState.update).not.toHaveBeenCalledWith("mode", "architect") }) it("should use current mode if history item has no saved mode", async () => { await provider.resolveWebviewView(mockWebviewView) // Set current mode mockContext.globalState.get = vi.fn().mockImplementation((key: string) => { if (key === "mode") return "code" return undefined }) // Create a history item without saved mode const historyItem: HistoryItem = { id: "test-task-id", number: 1, ts: Date.now(), task: "Test task", tokensIn: 100, tokensOut: 200, cacheWrites: 0, cacheReads: 0, totalCost: 0.001, // No mode field } // Mock getTaskWithId vi.spyOn(provider, "getTaskWithId").mockResolvedValue({ historyItem, taskDirPath: "/test/path", apiConversationHistoryFilePath: "/test/path/api_history.json", uiMessagesFilePath: "/test/path/ui_messages.json", apiConversationHistory: [], }) // Mock handleModeSwitch to track calls const handleModeSwitchSpy = vi.spyOn(provider, "handleModeSwitch").mockResolvedValue() // Initialize task with history item await provider.createTaskWithHistoryItem(historyItem) // Verify mode was not changed (should use current mode) expect(handleModeSwitchSpy).not.toHaveBeenCalled() }) }) describe("Task metadata persistence", () => { it("should include mode in task metadata when creating history items", async () => { await provider.resolveWebviewView(mockWebviewView) // Set current mode await provider.setValue("mode", "debug") // Create a mock task const mockTask = new Task({ provider, apiConfiguration: { apiProvider: providerIdentifiers.openrouter }, }) // Get the actual taskId from the mock const taskId = (mockTask as any).taskId || "test-task-id" // Seed the authoritative file-backed task history. await seedTaskHistory([ { id: taskId, ts: Date.now(), task: "Test task", number: 1, tokensIn: 0, tokensOut: 0, cacheWrites: 0, cacheReads: 0, totalCost: 0, }, ]) // Mock updateTaskHistory to capture the updated history item let updatedHistoryItem: any vi.spyOn(provider, "updateTaskHistory").mockImplementation((item) => { updatedHistoryItem = item return Promise.resolve([item]) }) // Add task to provider stack await provider.addClineToStack(mockTask) // Trigger a mode switch await provider.handleModeSwitch("debug") // Verify mode was included in the updated history item expect(updatedHistoryItem).toBeDefined() expect(updatedHistoryItem.mode).toBe("debug") }) }) describe("Integration with new_task tool", () => { it("should preserve parent task mode when creating subtasks", async () => { await provider.resolveWebviewView(mockWebviewView) // This test verifies that when using the new_task tool to create a subtask, // the parent task's mode is preserved and not changed by the subtask's mode switch // Set initial mode to architect await provider.setValue("mode", "architect") // Create parent task const parentTask = new Task({ provider, apiConfiguration: { apiProvider: providerIdentifiers.openrouter }, }) // Get the actual taskId from the mock const parentTaskId = (parentTask as any).taskId || "parent-task-id" // Create a simple task history tracking object const taskModes: Record = { [parentTaskId]: "architect", // Parent starts with architect mode } // Read task metadata from the authoritative store's test double. vi.spyOn(provider.taskHistoryStore, "get").mockImplementation((id) => { const mode = taskModes[id] return mode === undefined ? undefined : { id, ts: Date.now(), task: `Task ${id}`, number: 1, tokensIn: 0, tokensOut: 0, totalCost: 0, mode, } }) // Mock updateTaskHistory to track mode changes const updateTaskHistoryMock = vi.spyOn(provider, "updateTaskHistory") updateTaskHistoryMock.mockImplementation((item) => { // The handleModeSwitch method updates the task history for the current task // We should only update the task that matches the item.id if (item.id && item.mode !== undefined) { taskModes[item.id] = item.mode } return Promise.resolve([]) }) // Add parent task to stack await provider.addClineToStack(parentTask) // Create a subtask (simulating new_task tool behavior) const subtask = new Task({ provider, apiConfiguration: { apiProvider: providerIdentifiers.openrouter }, parentTask: parentTask, }) const subtaskId = (subtask as any).taskId || "subtask-id" // Initialize subtask with parent's mode taskModes[subtaskId] = "architect" // Mock getCurrentTask to return the parent task initially const getCurrentTaskMock = vi.spyOn(provider, "getCurrentTask") getCurrentTaskMock.mockReturnValue(parentTask as any) // Add subtask to stack await provider.addClineToStack(subtask) // Now mock getCurrentTask to return the subtask (simulating stack behavior) getCurrentTaskMock.mockReturnValue(subtask as any) // Switch subtask to code mode - this should only affect the subtask await provider.handleModeSwitch("code") // Verify that the parent task's mode is still architect expect(taskModes[parentTaskId]).toBe("architect") // Verify the subtask has code mode expect(taskModes[subtaskId]).toBe("code") }) }) describe("Error handling", () => { it("should handle errors gracefully when saving mode fails", async () => { await provider.resolveWebviewView(mockWebviewView) // Create a mock task that throws on save const mockTask = new Task({ provider, apiConfiguration: { apiProvider: providerIdentifiers.openrouter }, }) vi.spyOn(mockTask as any, "saveClineMessages").mockRejectedValue(new Error("Save failed")) // Add task to provider stack await provider.addClineToStack(mockTask) // Switch mode - should not throw await expect(provider.handleModeSwitch("architect")).resolves.not.toThrow() // Verify mode was still updated in global state expect(mockContext.globalState.update).toHaveBeenCalledWith("mode", "architect") }) it("should handle null/undefined mode gracefully", async () => { await provider.resolveWebviewView(mockWebviewView) // Create a history item with null mode const historyItem: HistoryItem = { id: "test-task-id", number: 1, ts: Date.now(), task: "Test task", tokensIn: 100, tokensOut: 200, cacheWrites: 0, cacheReads: 0, totalCost: 0.001, mode: null as any, // Invalid mode } // Mock getTaskWithId vi.spyOn(provider, "getTaskWithId").mockResolvedValue({ historyItem, taskDirPath: "/test/path", apiConversationHistoryFilePath: "/test/path/api_history.json", uiMessagesFilePath: "/test/path/ui_messages.json", apiConversationHistory: [], }) // Mock handleModeSwitch to track calls const handleModeSwitchSpy = vi.spyOn(provider, "handleModeSwitch").mockResolvedValue() // Initialize task with history item - should not throw await expect(provider.createTaskWithHistoryItem(historyItem)).resolves.not.toThrow() // Verify mode switch was not called with null expect(handleModeSwitchSpy).not.toHaveBeenCalledWith(null) }) it("should restore API configuration when restoring task from history with mode", async () => { // Setup: Configure different API configs for different modes const codeApiConfig = { apiProvider: providerIdentifiers.anthropic, anthropicApiKey: "code-key" } const architectApiConfig = { apiProvider: providerIdentifiers.openai, openAiApiKey: "architect-key" } // Save API configs await provider.upsertProviderProfile("code-config", codeApiConfig) await provider.upsertProviderProfile("architect-config", architectApiConfig) // Get the config IDs const codeConfigId = provider.getProviderProfileEntry("code-config")?.id const architectConfigId = provider.getProviderProfileEntry("architect-config")?.id // Associate configs with modes await provider.providerSettingsManager.setModeConfig("code", codeConfigId!) await provider.providerSettingsManager.setModeConfig("architect", architectConfigId!) // Start in code mode with code config await provider.handleModeSwitch("code") // Create a history item with architect mode const historyItem: HistoryItem = { id: "test-task-id", number: 1, ts: Date.now(), task: "Test task", tokensIn: 100, tokensOut: 200, cacheWrites: 0, cacheReads: 0, totalCost: 0.001, mode: "architect", // Task was created in architect mode } // Restore the task from history await provider.createTaskWithHistoryItem(historyItem) // Verify that the mode was restored into this view's durable pin. The // getState() merge of hydrated per-view values lands with the F1b follow-up. expect(provider["viewLocalState"].mode).toBe("architect") const state = await provider.getState() // Verify that the API configuration was also restored expect(state.currentApiConfigName).toBe("architect-config") expect(state.apiConfiguration.apiProvider).toBe("openai") // The restored mode lives only in this view's buffer, so the restore must not // re-bind the shared mode slot. Before the fix this wrote architect-config onto // the "code" slot, because getState().mode still reported code. expect(await provider.providerSettingsManager.getModeConfigId("code")).toBe(codeConfigId) expect(await provider.providerSettingsManager.getModeConfigId("architect")).toBe(architectConfigId) }) it("should handle mode deletion between sessions", async () => { await provider.resolveWebviewView(mockWebviewView) // Create a history item with a mode that no longer exists const historyItem: HistoryItem = { id: "test-task-id", number: 1, ts: Date.now(), task: "Test task", tokensIn: 100, tokensOut: 200, cacheWrites: 0, cacheReads: 0, totalCost: 0.001, mode: "deleted-mode", // Mode that doesn't exist } // Mock getModeBySlug to return undefined for deleted mode const { getModeBySlug } = await import("../../../shared/modes") vi.mocked(getModeBySlug).mockReturnValue(undefined) // Mock getTaskWithId vi.spyOn(provider, "getTaskWithId").mockResolvedValue({ historyItem, taskDirPath: "/test/path", apiConversationHistoryFilePath: "/test/path/api_history.json", uiMessagesFilePath: "/test/path/ui_messages.json", apiConversationHistory: [], }) // Mock handleModeSwitch to track calls const handleModeSwitchSpy = vi.spyOn(provider, "handleModeSwitch").mockResolvedValue() // Initialize task with history item - should not throw await expect(provider.createTaskWithHistoryItem(historyItem)).resolves.not.toThrow() // Verify mode switch was not called with deleted mode expect(handleModeSwitchSpy).not.toHaveBeenCalledWith("deleted-mode") }) }) describe("Concurrent mode switches", () => { it("should handle concurrent mode switches on the same task", async () => { await provider.resolveWebviewView(mockWebviewView) // Create a mock task const mockTask = { taskId: "test-task-id", _taskMode: "code", emit: vi.fn(), saveClineMessages: vi.fn(), clineMessages: [], apiConversationHistory: [], updateApiConfiguration: vi.fn(), } // Add task to provider stack await provider.addClineToStack(mockTask as any) // Seed the authoritative file-backed task history. await seedTaskHistory([ { id: mockTask.taskId, ts: Date.now(), task: "Test task", number: 1, tokensIn: 0, tokensOut: 0, cacheWrites: 0, cacheReads: 0, totalCost: 0, }, ]) // Mock updateTaskHistory const updateTaskHistorySpy = vi.spyOn(provider, "updateTaskHistory").mockImplementation(() => { return Promise.resolve([]) }) // Clear previous calls to globalState.update vi.mocked(mockContext.globalState.update).mockClear() // Simulate concurrent mode switches const switches = [ provider.handleModeSwitch("architect"), provider.handleModeSwitch("debug"), provider.handleModeSwitch("code"), ] await Promise.all(switches) // Find the last mode update call const modeCalls = vi.mocked(mockContext.globalState.update).mock.calls.filter((call) => call[0] === "mode") const lastModeCall = modeCalls[modeCalls.length - 1] // Verify the last mode switch wins expect(lastModeCall).toEqual(["mode", "code"]) // Verify task history was updated with final mode const lastCall = updateTaskHistorySpy.mock.calls[updateTaskHistorySpy.mock.calls.length - 1] expect(lastCall[0]).toMatchObject({ id: mockTask.taskId, mode: "code", }) }) it("should handle mode switches during task save operations", async () => { await provider.resolveWebviewView(mockWebviewView) // Create a mock task with slow save operation const mockTask = { taskId: "test-task-id", _taskMode: "code", emit: vi.fn(), saveClineMessages: vi.fn().mockImplementation(async () => { // Simulate slow save await new Promise((resolve) => setTimeout(resolve, 100)) }), clineMessages: [], apiConversationHistory: [], updateApiConfiguration: vi.fn(), } // Add task to provider stack await provider.addClineToStack(mockTask as any) // Seed the authoritative file-backed task history. await seedTaskHistory([ { id: mockTask.taskId, ts: Date.now(), task: "Test task", number: 1, tokensIn: 0, tokensOut: 0, cacheWrites: 0, cacheReads: 0, totalCost: 0, mode: "code", }, ]) // Mock updateTaskHistory vi.spyOn(provider, "updateTaskHistory").mockImplementation(() => { return Promise.resolve([]) }) // Start a save operation const savePromise = mockTask.saveClineMessages() // Switch mode during save await provider.handleModeSwitch("architect") // Wait for save to complete await savePromise // Task should have the new mode expect((mockTask as any)._taskMode).toBe("architect") }) }) describe("Mode switch failure scenarios", () => { it("should handle invalid mode gracefully", async () => { await provider.resolveWebviewView(mockWebviewView) // The provider actually does switch to invalid modes // This test should verify that behavior const mockTask = { taskId: "test-task-id", _taskMode: "code", emit: vi.fn(), saveClineMessages: vi.fn(), clineMessages: [], apiConversationHistory: [], updateApiConfiguration: vi.fn(), } // Add task to provider stack await provider.addClineToStack(mockTask as any) // Clear previous calls vi.mocked(mockContext.globalState.update).mockClear() // Try to switch to invalid mode - it will actually switch await provider.handleModeSwitch("invalid-mode" as any) // The mode WILL be updated to invalid-mode (this is the actual behavior) expect(mockContext.globalState.update).toHaveBeenCalledWith("mode", "invalid-mode") }) it("should handle errors during mode switch gracefully", async () => { await provider.resolveWebviewView(mockWebviewView) // Create a mock task that throws on emit only for specific events let emitCallCount = 0 const mockTask = { taskId: "test-task-id", _taskMode: "code", emit: vi.fn().mockImplementation((event) => { emitCallCount++ // Only throw on the second emit call (taskModeSwitched event) // The first call is for TaskFocused in addClineToStack if (emitCallCount === 2 && event === "taskModeSwitched") { throw new Error("Emit failed") } }), saveClineMessages: vi.fn(), clineMessages: [], apiConversationHistory: [], updateApiConfiguration: vi.fn(), } // Add task to provider stack await provider.addClineToStack(mockTask as any) // Seed the authoritative file-backed task history. await seedTaskHistory([ { id: mockTask.taskId, ts: Date.now(), task: "Test task", number: 1, tokensIn: 0, tokensOut: 0, cacheWrites: 0, cacheReads: 0, totalCost: 0, }, ]) // Mock updateTaskHistory vi.spyOn(provider, "updateTaskHistory").mockImplementation(() => { return Promise.resolve([]) }) // Mock console.error to suppress error output const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) // Clear previous mock calls to isolate this test vi.mocked(mockContext.globalState.update).mockClear() // The handleModeSwitch method doesn't catch errors from emit, so it will throw // The error is thrown before the task's mode is updated await expect(provider.handleModeSwitch("architect")).rejects.toThrow("Emit failed") // The announcement now follows the durable writes, so a listener that rejects the switch // leaves the mode write landed - the task's own mode is untouched, asserted below - while // the promise still surfaces the failure. const modeCalls = vi.mocked(mockContext.globalState.update).mock.calls.filter((call) => call[0] === "mode") expect(modeCalls).toEqual([["mode", "architect"]]) // The task's mode should NOT have been updated since the error occurred first expect(mockTask._taskMode).toBe("code") consoleErrorSpy.mockRestore() }) it("should handle updateTaskHistory failures", async () => { await provider.resolveWebviewView(mockWebviewView) // Create a mock task const mockTask = { taskId: "test-task-id", _taskMode: "code", emit: vi.fn(), saveClineMessages: vi.fn(), clineMessages: [], apiConversationHistory: [], updateApiConfiguration: vi.fn(), } // Add task to provider stack await provider.addClineToStack(mockTask as any) // Seed the authoritative file-backed task history. await seedTaskHistory([ { id: mockTask.taskId, ts: Date.now(), task: "Test task", number: 1, tokensIn: 0, tokensOut: 0, cacheWrites: 0, cacheReads: 0, totalCost: 0, }, ]) // Mock updateTaskHistory to throw error vi.spyOn(provider, "updateTaskHistory").mockRejectedValue(new Error("Update failed")) // Mock console.error const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) // The updateTaskHistory failure will cause handleModeSwitch to throw // This is the actual behavior based on the test failure await expect(provider.handleModeSwitch("architect")).rejects.toThrow("Update failed") consoleErrorSpy.mockRestore() }) }) describe("Multiple tasks switching modes simultaneously", () => { it("should handle multiple tasks switching modes independently", async () => { await provider.resolveWebviewView(mockWebviewView) // Create multiple mock tasks const task1 = { taskId: "task-1", _taskMode: "code", emit: vi.fn(), saveClineMessages: vi.fn(), clineMessages: [], apiConversationHistory: [], updateApiConfiguration: vi.fn(), } const task2 = { taskId: "task-2", _taskMode: "architect", emit: vi.fn(), saveClineMessages: vi.fn(), clineMessages: [], apiConversationHistory: [], updateApiConfiguration: vi.fn(), } const task3 = { taskId: "task-3", _taskMode: "debug", emit: vi.fn(), saveClineMessages: vi.fn(), clineMessages: [], apiConversationHistory: [], updateApiConfiguration: vi.fn(), } // Add tasks to provider stack await provider.addClineToStack(task1 as any) await provider.addClineToStack(task2 as any) await provider.addClineToStack(task3 as any) // Seed the authoritative file-backed task history. await seedTaskHistory([ { id: task1.taskId, ts: Date.now(), task: "Task 1", number: 1, tokensIn: 0, tokensOut: 0, cacheWrites: 0, cacheReads: 0, totalCost: 0, mode: "code", }, { id: task2.taskId, ts: Date.now(), task: "Task 2", number: 2, tokensIn: 0, tokensOut: 0, cacheWrites: 0, cacheReads: 0, totalCost: 0, mode: "architect", }, { id: task3.taskId, ts: Date.now(), task: "Task 3", number: 3, tokensIn: 0, tokensOut: 0, cacheWrites: 0, cacheReads: 0, totalCost: 0, mode: "debug", }, ]) // Mock updateTaskHistory vi.spyOn(provider, "updateTaskHistory").mockImplementation(() => { return Promise.resolve([]) }) // Simulate simultaneous mode switches for distinct, explicitly targeted tasks. // These lightweight task doubles only implement the members exercised by this test. const switch1 = provider.handleModeSwitch("architect", task1 as unknown as Task) const switch2 = provider.handleModeSwitch("debug", task2 as unknown as Task) const switch3 = provider.handleModeSwitch("code", task3 as unknown as Task) await Promise.all([switch1, switch2, switch3]) // Verify each task was updated with its new mode expect(task1._taskMode).toBe("architect") expect(task2._taskMode).toBe("debug") expect(task3._taskMode).toBe("code") // Verify emit was called for each task expect(task1.emit).toHaveBeenCalledWith("taskModeSwitched", task1.taskId, "architect") expect(task2.emit).toHaveBeenCalledWith("taskModeSwitched", task2.taskId, "debug") expect(task3.emit).toHaveBeenCalledWith("taskModeSwitched", task3.taskId, "code") }) }) describe("Task initialization timing edge cases", () => { it("should handle mode restoration during slow task initialization", async () => { await provider.resolveWebviewView(mockWebviewView) // Create a history item with saved mode const historyItem: HistoryItem = { id: "test-task-id", number: 1, ts: Date.now(), task: "Test task", tokensIn: 100, tokensOut: 200, cacheWrites: 0, cacheReads: 0, totalCost: 0.001, mode: "architect", } // Mock getTaskWithId to be slow vi.spyOn(provider, "getTaskWithId").mockImplementation(async () => { await new Promise((resolve) => setTimeout(resolve, 100)) return { historyItem, taskDirPath: "/test/path", apiConversationHistoryFilePath: "/test/path/api_history.json", uiMessagesFilePath: "/test/path/ui_messages.json", apiConversationHistory: [], } }) // Clear any previous calls vi.clearAllMocks() // Start initialization const initPromise = provider.createTaskWithHistoryItem(historyItem) // Try to switch mode during initialization await provider.handleModeSwitch("code") // Wait for initialization to complete await initPromise // Check all mode update calls const modeCalls = vi.mocked(mockContext.globalState.update).mock.calls.filter((call) => call[0] === "mode") // Based on the actual behavior, the mode switch to "code" happens and persists // The history mode restoration doesn't override it const lastModeCall = modeCalls[modeCalls.length - 1] expect(lastModeCall).toEqual(["mode", "code"]) }) it("should handle rapid task switches during mode changes", async () => { await provider.resolveWebviewView(mockWebviewView) // Create multiple tasks const tasks = Array.from({ length: 5 }, (_, i) => ({ taskId: `task-${i}`, _taskMode: "code", emit: vi.fn(), saveClineMessages: vi.fn(), clineMessages: [], apiConversationHistory: [], updateApiConfiguration: vi.fn(), })) // Add all tasks to provider for (const task of tasks) { await provider.addClineToStack(task as any) } // Mock getCurrentTask const getCurrentTaskSpy = vi.spyOn(provider, "getCurrentTask") // Rapidly switch between tasks and modes const switches: Promise[] = [] tasks.forEach((task, index) => { getCurrentTaskSpy.mockReturnValue(task as any) const mode = ["architect", "debug", "code"][index % 3] switches.push(provider.handleModeSwitch(mode as any)) }) await Promise.all(switches) // Each task should have been updated tasks.forEach((task) => { expect(task.emit).toHaveBeenCalled() }) }) }) describe("setViewStateId input validation", () => { it("ignores a non-string viewStateId from a crafted message", async () => { const before = provider["viewStateId"] // A crafted postMessage can carry any JSON value despite the declared type. await provider.setViewStateId(42 as unknown as string) expect(provider["viewStateId"]).toBe(before) }) it("ignores an oversized viewStateId", async () => { const before = provider["viewStateId"] await provider.setViewStateId("x".repeat(200)) expect(provider["viewStateId"]).toBe(before) }) it("refuses a viewStateId already claimed by another live view", async () => { const other = new ClineProvider( mockContext, mockOutputChannel, "sidebar", new ContextProxy(mockContext), new WebviewFocusTracker(), ) try { await other.setViewStateId("claimed-by-other-view") await provider.setViewStateId("claimed-by-other-view") expect(provider["viewStateId"]).not.toBe("claimed-by-other-view") expect(other["viewStateId"]).toBe("claimed-by-other-view") } finally { await other.dispose() } }) }) })