Skip to content

Commit e9fa2a6

Browse files
committed
fix: persist and restore active task on VS Code restart
- Add currentActiveTaskId to global settings schema - Persist task ID when adding/removing tasks from stack - Restore last active task in resolveWebviewView on startup - Add comprehensive tests for task persistence functionality Fixes #7659
1 parent 966ed76 commit e9fa2a6

File tree

3 files changed

+364
-4
lines changed

3 files changed

+364
-4
lines changed

packages/types/src/global-settings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export const globalSettingsSchema = z.object({
4141
lastShownAnnouncementId: z.string().optional(),
4242
customInstructions: z.string().optional(),
4343
taskHistory: z.array(historyItemSchema).optional(),
44+
currentActiveTaskId: z.string().optional(),
4445

4546
// Image generation settings (experimental) - flattened for simplicity
4647
openRouterImageApiKey: z.string().optional(),

src/core/webview/ClineProvider.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,9 @@ export class ClineProvider
353353
this.clineStack.push(task)
354354
task.emit(RooCodeEventName.TaskFocused)
355355

356+
// Persist the current active task ID
357+
await this.updateGlobalState("currentActiveTaskId", task.taskId)
358+
356359
// Perform special setup provider specific tasks.
357360
await this.performPreparationTasks(task)
358361

@@ -417,6 +420,15 @@ export class ClineProvider
417420
// garbage collected.
418421
task = undefined
419422
}
423+
424+
// Clear the current active task ID if no tasks remain
425+
if (this.clineStack.length === 0) {
426+
await this.updateGlobalState("currentActiveTaskId", undefined)
427+
} else {
428+
// Update to the new top task
429+
const newCurrentTask = this.clineStack[this.clineStack.length - 1]
430+
await this.updateGlobalState("currentActiveTaskId", newCurrentTask.taskId)
431+
}
420432
}
421433

422434
getTaskStackSize(): number {
@@ -725,6 +737,22 @@ export class ClineProvider
725737

726738
// If the extension is starting a new session, clear previous task state.
727739
await this.removeClineFromStack()
740+
741+
// Attempt to restore the last active task if one was persisted
742+
const lastActiveTaskId = this.getGlobalState("currentActiveTaskId")
743+
if (lastActiveTaskId && typeof lastActiveTaskId === "string") {
744+
try {
745+
const { historyItem } = await this.getTaskWithId(lastActiveTaskId)
746+
if (historyItem) {
747+
await this.createTaskWithHistoryItem(historyItem)
748+
this.log(`Restored last active task: ${lastActiveTaskId}`)
749+
}
750+
} catch (error) {
751+
// Task may have been deleted or corrupted, clear the saved ID
752+
this.log(`Failed to restore last active task ${lastActiveTaskId}: ${error}`)
753+
await this.updateGlobalState("currentActiveTaskId", undefined)
754+
}
755+
}
728756
}
729757

730758
public async createTaskWithHistoryItem(historyItem: HistoryItem & { rootTask?: Task; parentTask?: Task }) {

0 commit comments

Comments
 (0)