@@ -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}
0 commit comments