@@ -2326,51 +2326,33 @@ export class ClineProvider extends EventEmitter<ClineProviderEvents> implements
23262326 } > {
23272327 const history = ( ( await this . getGlobalState ( "taskHistory" ) ) as HistoryItem [ ] | undefined ) || [ ]
23282328 const historyItem = history . find ( ( item ) => item . id === id )
2329- if ( ! historyItem ) {
2330- throw new Error ( "Task not found in history" )
2331- }
2332-
2333- const taskDirPath = path . join ( this . contextProxy . globalStorageUri . fsPath , "tasks" , id )
2334- const apiConversationHistoryFilePath = path . join ( taskDirPath , GlobalFileNames . apiConversationHistory )
2335- const uiMessagesFilePath = path . join ( taskDirPath , GlobalFileNames . uiMessages )
2336-
2337- const fileExists = await fileExistsAtPath ( apiConversationHistoryFilePath )
2338- if ( ! fileExists ) {
2339- // Instead of silently deleting, throw a specific error
2340- throw new Error ( "TASK_FILES_MISSING" )
2341- }
2342-
2343- const apiConversationHistory = JSON . parse ( await fs . readFile ( apiConversationHistoryFilePath , "utf8" ) )
2344- return {
2345- historyItem,
2346- taskDirPath,
2347- apiConversationHistoryFilePath,
2348- uiMessagesFilePath,
2349- apiConversationHistory,
2329+ if ( historyItem ) {
2330+ const taskDirPath = path . join ( this . contextProxy . globalStorageUri . fsPath , "tasks" , id )
2331+ const apiConversationHistoryFilePath = path . join ( taskDirPath , GlobalFileNames . apiConversationHistory )
2332+ const uiMessagesFilePath = path . join ( taskDirPath , GlobalFileNames . uiMessages )
2333+ const fileExists = await fileExistsAtPath ( apiConversationHistoryFilePath )
2334+ if ( fileExists ) {
2335+ const apiConversationHistory = JSON . parse ( await fs . readFile ( apiConversationHistoryFilePath , "utf8" ) )
2336+ return {
2337+ historyItem,
2338+ taskDirPath,
2339+ apiConversationHistoryFilePath,
2340+ uiMessagesFilePath,
2341+ apiConversationHistory,
2342+ }
2343+ }
23502344 }
2345+ // if we tried to get a task that doesn't exist, remove it from state
2346+ // FIXME: this seems to happen sometimes when the json file doesnt save to disk for some reason
2347+ await this . deleteTaskFromState ( id )
2348+ throw new Error ( "Task not found" )
23512349 }
23522350
23532351 async showTaskWithId ( id : string ) {
23542352 if ( id !== this . getCurrentCline ( ) ?. taskId ) {
2355- try {
2356- const { historyItem } = await this . getTaskWithId ( id )
2357- await this . initClineWithHistoryItem ( historyItem )
2358- } catch ( error ) {
2359- if ( error . message === "TASK_FILES_MISSING" ) {
2360- const response = await vscode . window . showWarningMessage (
2361- t ( "common:warnings.missing_task_files" ) ,
2362- t ( "common:answers.remove" ) ,
2363- t ( "common:answers.keep" ) ,
2364- )
2365-
2366- if ( response === t ( "common:answers.remove" ) ) {
2367- await this . deleteTaskFromState ( id )
2368- await this . postStateToWebview ( )
2369- }
2370- return
2371- }
2372- throw error
2373- }
2353+ // Non-current task.
2354+ const { historyItem } = await this . getTaskWithId ( id )
2355+ await this . initClineWithHistoryItem ( historyItem ) // Clears existing task.
23742356 }
23752357
23762358 await this . postMessageToWebview ( { type : "action" , action : "chatButtonClicked" } )
@@ -2857,28 +2839,4 @@ export class ClineProvider extends EventEmitter<ClineProviderEvents> implements
28572839
28582840 return properties
28592841 }
2860-
2861- async validateTaskHistory ( ) {
2862- const history = ( ( await this . getGlobalState ( "taskHistory" ) ) as HistoryItem [ ] | undefined ) || [ ]
2863- const validTasks : HistoryItem [ ] = [ ]
2864-
2865- for ( const item of history ) {
2866- const taskDirPath = path . join ( this . contextProxy . globalStorageUri . fsPath , "tasks" , item . id )
2867- const apiConversationHistoryFilePath = path . join ( taskDirPath , GlobalFileNames . apiConversationHistory )
2868-
2869- if ( await fileExistsAtPath ( apiConversationHistoryFilePath ) ) {
2870- validTasks . push ( item )
2871- }
2872- }
2873-
2874- if ( validTasks . length !== history . length ) {
2875- await this . updateGlobalState ( "taskHistory" , validTasks )
2876- await this . postStateToWebview ( )
2877-
2878- const removedCount = history . length - validTasks . length
2879- if ( removedCount > 0 ) {
2880- await vscode . window . showInformationMessage ( t ( "common:info.history_cleanup" , { count : removedCount } ) )
2881- }
2882- }
2883- }
28842842}
0 commit comments