Skip to content

Commit 517896e

Browse files
committed
fix: preserve task state when extension host restarts in new window
- Modified ClineProvider.ts to check if tasks exist before clearing them - Only clear task stack on fresh start, not on webview restoration - Added logging to indicate when task state is preserved - Updated webviewMessageHandler to log when restoring active tasks This fixes the issue where prompts were lost after extension host restart in a new window. Fixes #8035
1 parent 2263d86 commit 517896e

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/core/webview/ClineProvider.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ export class ClineProvider
134134
private taskCreationCallback: (task: Task) => void
135135
private taskEventListeners: WeakMap<Task, Array<() => void>> = new WeakMap()
136136
private currentWorkspacePath: string | undefined
137+
private isWebviewRestored: boolean = false // Track if webview is being restored after restart
137138

138139
private recentTasksCache?: string[]
139140
private pendingOperations: Map<string, PendingEditOperation> = new Map()
@@ -686,6 +687,9 @@ export class ClineProvider
686687
}
687688

688689
async resolveWebviewView(webviewView: vscode.WebviewView | vscode.WebviewPanel) {
690+
// Check if we already have a task stack (indicating a restore after extension host restart)
691+
const hasExistingTasks = this.clineStack.length > 0
692+
689693
this.view = webviewView
690694
const inTabMode = "onDidChangeViewState" in webviewView
691695

@@ -695,6 +699,9 @@ export class ClineProvider
695699
setPanel(webviewView, "sidebar")
696700
}
697701

702+
// Mark that we're restoring if we have existing tasks
703+
this.isWebviewRestored = hasExistingTasks
704+
698705
// Initialize out-of-scope variables that need to receive persistent
699706
// global state values.
700707
this.getState().then(
@@ -810,8 +817,17 @@ export class ClineProvider
810817
})
811818
this.webviewDisposables.push(configDisposable)
812819

813-
// If the extension is starting a new session, clear previous task state.
814-
await this.removeClineFromStack()
820+
// Only clear task state if this is a fresh start, not when restoring after extension host restart
821+
// When the extension host restarts in a new window, we want to preserve the task state
822+
if (!hasExistingTasks) {
823+
// This is a fresh start, clear any previous task state
824+
await this.removeClineFromStack()
825+
} else {
826+
// We're restoring after an extension host restart, preserve the task state
827+
this.log("Preserving task state after extension host restart")
828+
// Post the current state to the webview so it can restore the UI
829+
await this.postStateToWebview()
830+
}
815831
}
816832

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

src/core/webview/webviewMessageHandler.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,13 @@ export const webviewMessageHandler = async (
443443
provider.postMessageToWebview({ type: "mcpServers", mcpServers: mcpHub.getAllServers() })
444444
}
445445

446+
// If we have an active task (after extension host restart), notify the webview to restore it
447+
const activeTask = provider.getCurrentTask()
448+
if (activeTask) {
449+
provider.log("Restoring active task after extension host restart")
450+
// The state will be posted by postStateToWebview() above, which includes the current task
451+
}
452+
446453
provider.providerSettingsManager
447454
.listConfig()
448455
.then(async (listApiConfig) => {

0 commit comments

Comments
 (0)