Skip to content

Commit 2a2e467

Browse files
committed
1. Added validation for task history and added option for user to remove/keep task in history
1 parent 1f5f668 commit 2a2e467

File tree

3 files changed

+143
-22
lines changed

3 files changed

+143
-22
lines changed

src/core/webview/ClineProvider.ts

Lines changed: 66 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,33 +2169,51 @@ export class ClineProvider implements vscode.WebviewViewProvider {
21692169
}> {
21702170
const history = ((await this.getGlobalState("taskHistory")) as HistoryItem[] | undefined) || []
21712171
const historyItem = history.find((item) => item.id === id)
2172-
if (historyItem) {
2173-
const taskDirPath = path.join(this.contextProxy.globalStorageUri.fsPath, "tasks", id)
2174-
const apiConversationHistoryFilePath = path.join(taskDirPath, GlobalFileNames.apiConversationHistory)
2175-
const uiMessagesFilePath = path.join(taskDirPath, GlobalFileNames.uiMessages)
2176-
const fileExists = await fileExistsAtPath(apiConversationHistoryFilePath)
2177-
if (fileExists) {
2178-
const apiConversationHistory = JSON.parse(await fs.readFile(apiConversationHistoryFilePath, "utf8"))
2179-
return {
2180-
historyItem,
2181-
taskDirPath,
2182-
apiConversationHistoryFilePath,
2183-
uiMessagesFilePath,
2184-
apiConversationHistory,
2185-
}
2186-
}
2172+
if (!historyItem) {
2173+
throw new Error("Task not found in history")
2174+
}
2175+
2176+
const taskDirPath = path.join(this.contextProxy.globalStorageUri.fsPath, "tasks", id)
2177+
const apiConversationHistoryFilePath = path.join(taskDirPath, GlobalFileNames.apiConversationHistory)
2178+
const uiMessagesFilePath = path.join(taskDirPath, GlobalFileNames.uiMessages)
2179+
2180+
const fileExists = await fileExistsAtPath(apiConversationHistoryFilePath)
2181+
if (!fileExists) {
2182+
// Instead of silently deleting, throw a specific error
2183+
throw new Error("TASK_FILES_MISSING")
2184+
}
2185+
2186+
const apiConversationHistory = JSON.parse(await fs.readFile(apiConversationHistoryFilePath, "utf8"))
2187+
return {
2188+
historyItem,
2189+
taskDirPath,
2190+
apiConversationHistoryFilePath,
2191+
uiMessagesFilePath,
2192+
apiConversationHistory,
21872193
}
2188-
// if we tried to get a task that doesn't exist, remove it from state
2189-
// FIXME: this seems to happen sometimes when the json file doesnt save to disk for some reason
2190-
await this.deleteTaskFromState(id)
2191-
throw new Error("Task not found")
21922194
}
21932195

21942196
async showTaskWithId(id: string) {
21952197
if (id !== this.getCurrentCline()?.taskId) {
2196-
// Non-current task.
2197-
const { historyItem } = await this.getTaskWithId(id)
2198-
await this.initClineWithHistoryItem(historyItem) // Clears existing task.
2198+
try {
2199+
const { historyItem } = await this.getTaskWithId(id)
2200+
await this.initClineWithHistoryItem(historyItem)
2201+
} catch (error) {
2202+
if (error.message === "TASK_FILES_MISSING") {
2203+
const response = await vscode.window.showWarningMessage(
2204+
"This task's files are missing. Would you like to remove it from the task list?",
2205+
"Remove",
2206+
"Keep",
2207+
)
2208+
2209+
if (response === "Remove") {
2210+
await this.deleteTaskFromState(id)
2211+
await this.postStateToWebview()
2212+
}
2213+
return
2214+
}
2215+
throw error
2216+
}
21992217
}
22002218

22012219
await this.postMessageToWebview({ type: "action", action: "chatButtonClicked" })
@@ -2659,4 +2677,30 @@ export class ClineProvider implements vscode.WebviewViewProvider {
26592677

26602678
return properties
26612679
}
2680+
2681+
async validateTaskHistory() {
2682+
const history = ((await this.getGlobalState("taskHistory")) as HistoryItem[] | undefined) || []
2683+
const validTasks: HistoryItem[] = []
2684+
2685+
for (const item of history) {
2686+
const taskDirPath = path.join(this.contextProxy.globalStorageUri.fsPath, "tasks", item.id)
2687+
const apiConversationHistoryFilePath = path.join(taskDirPath, GlobalFileNames.apiConversationHistory)
2688+
2689+
if (await fileExistsAtPath(apiConversationHistoryFilePath)) {
2690+
validTasks.push(item)
2691+
}
2692+
}
2693+
2694+
if (validTasks.length !== history.length) {
2695+
await this.updateGlobalState("taskHistory", validTasks)
2696+
await this.postStateToWebview()
2697+
2698+
const removedCount = history.length - validTasks.length
2699+
if (removedCount > 0) {
2700+
await vscode.window.showInformationMessage(
2701+
`Cleaned up ${removedCount} task(s) with missing files from history.`,
2702+
)
2703+
}
2704+
}
2705+
}
26622706
}

src/core/webview/__tests__/ClineProvider.test.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,78 @@ jest.mock("../../contextProxy", () => {
5454
}
5555
})
5656

57+
describe("validateTaskHistory", () => {
58+
let provider: ClineProvider
59+
let mockContext: vscode.ExtensionContext
60+
let mockOutputChannel: vscode.OutputChannel
61+
let mockUpdate: jest.Mock
62+
63+
beforeEach(() => {
64+
// Reset mocks
65+
jest.clearAllMocks()
66+
67+
mockUpdate = jest.fn()
68+
69+
// Setup basic mocks
70+
mockContext = {
71+
globalState: {
72+
get: jest.fn(),
73+
update: mockUpdate,
74+
keys: jest.fn().mockReturnValue([]),
75+
},
76+
secrets: { get: jest.fn(), store: jest.fn(), delete: jest.fn() },
77+
extensionUri: {} as vscode.Uri,
78+
globalStorageUri: { fsPath: "/test/path" },
79+
extension: { packageJSON: { version: "1.0.0" } },
80+
} as unknown as vscode.ExtensionContext
81+
82+
mockOutputChannel = { appendLine: jest.fn() } as unknown as vscode.OutputChannel
83+
provider = new ClineProvider(mockContext, mockOutputChannel)
84+
})
85+
86+
test("should remove tasks with missing files", async () => {
87+
// Mock the global state with some test data
88+
const mockHistory = [
89+
{ id: "task1", ts: Date.now() },
90+
{ id: "task2", ts: Date.now() },
91+
]
92+
93+
// Setup mocks
94+
jest.spyOn(mockContext.globalState, "get").mockReturnValue(mockHistory)
95+
96+
// Mock fileExistsAtPath to only return true for task1
97+
const mockFs = require("../../../utils/fs")
98+
mockFs.fileExistsAtPath = jest.fn().mockImplementation((path) => Promise.resolve(path.includes("task1")))
99+
100+
// Call validateTaskHistory
101+
await provider.validateTaskHistory()
102+
103+
// Verify the results
104+
const expectedHistory = [expect.objectContaining({ id: "task1" })]
105+
106+
expect(mockUpdate).toHaveBeenCalledWith("taskHistory", expect.arrayContaining(expectedHistory))
107+
expect(mockUpdate.mock.calls[0][1].length).toBe(1)
108+
})
109+
110+
test("should handle empty history", async () => {
111+
// Mock empty history
112+
jest.spyOn(mockContext.globalState, "get").mockReturnValue([])
113+
114+
await provider.validateTaskHistory()
115+
116+
expect(mockUpdate).toHaveBeenCalledWith("taskHistory", [])
117+
})
118+
119+
test("should handle null history", async () => {
120+
// Mock null history
121+
jest.spyOn(mockContext.globalState, "get").mockReturnValue(null)
122+
123+
await provider.validateTaskHistory()
124+
125+
expect(mockUpdate).toHaveBeenCalledWith("taskHistory", [])
126+
})
127+
})
128+
57129
// Mock dependencies
58130
jest.mock("vscode")
59131
jest.mock("delay")

src/extension.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ export function activate(context: vscode.ExtensionContext) {
5959
const provider = new ClineProvider(context, outputChannel)
6060
telemetryService.setProvider(provider)
6161

62+
// Validate task history on extension activation
63+
provider.validateTaskHistory().catch((error) => {
64+
outputChannel.appendLine(`Failed to validate task history: ${error}`)
65+
})
66+
6267
context.subscriptions.push(
6368
vscode.window.registerWebviewViewProvider(ClineProvider.sideBarId, provider, {
6469
webviewOptions: { retainContextWhenHidden: true },

0 commit comments

Comments
 (0)