Skip to content

Commit d9e1948

Browse files
PierrunoYTclaude
andcommitted
fix(webview): don't block the first state post on task history loading
getStateToPostToWebview awaited the task history scan even when the caller omitted task history, and webviewDidLaunch always posted the full state, so a large history left the sidebar on a spinner. Await the scan only when history is included; on launch, render without history first and post the full state once the scan completes. Fixes #2095 Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent 16425bc commit d9e1948

6 files changed

Lines changed: 82 additions & 3 deletions

File tree

‎src/core/task-persistence/TaskHistoryStore.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ export class TaskHistoryStore {
9494
*/
9595
public readonly initialized: Promise<void>
9696
private resolveInitialized!: () => void
97+
private _isInitialized = false
98+
99+
/** True once the initial scan has finished, so reads no longer need to wait on `initialized`. */
100+
get isInitialized(): boolean {
101+
return this._isInitialized
102+
}
97103

98104
/** Periodic reconciliation interval in milliseconds. */
99105
private static readonly RECONCILE_INTERVAL_MS = 5 * 60 * 1000
@@ -140,6 +146,7 @@ export class TaskHistoryStore {
140146
this.startPeriodicReconciliation()
141147
} finally {
142148
// Mark initialization as complete so callers awaiting `initialized` can proceed
149+
this._isInitialized = true
143150
this.resolveInitialized()
144151
}
145152
}

‎src/core/task-persistence/__tests__/TaskHistoryStore.spec.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ describe("TaskHistoryStore", () => {
5353
})
5454

5555
describe("initialize()", () => {
56+
it("reports isInitialized only after the scan completes", async () => {
57+
expect(store.isInitialized).toBe(false)
58+
59+
await store.initialize()
60+
61+
expect(store.isInitialized).toBe(true)
62+
await expect(store.initialized).resolves.toBeUndefined()
63+
})
64+
5665
it("initializes from empty state (no index, no task dirs)", async () => {
5766
await store.initialize()
5867
expect(store.getAll()).toEqual([])

‎src/core/webview/ClineProvider.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2716,8 +2716,11 @@ export class ClineProvider
27162716
}
27172717

27182718
async getStateToPostToWebview({ includeTaskHistory = true }: GetStateOptions = {}): Promise<ExtensionState> {
2719-
// Ensure the store is initialized before reading task history
2720-
await this.taskHistoryStore.initialized
2719+
// Only wait for the store when task history is requested, so a slow scan of a large
2720+
// history cannot keep the webview from receiving its first state.
2721+
if (includeTaskHistory) {
2722+
await this.taskHistoryStore.initialized
2723+
}
27212724

27222725
const {
27232726
apiConfiguration,

‎src/core/webview/__tests__/ClineProvider.spec.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2447,6 +2447,16 @@ describe("ClineProvider", () => {
24472447
expect(mockContext.globalState.update).toHaveBeenCalledWith("currentApiConfigName", "test-config")
24482448
})
24492449

2450+
it("builds state without task history while the task history store is still loading", async () => {
2451+
await provider.resolveWebviewView(mockWebviewView)
2452+
// A scan that never finishes must not block a state that omits task history.
2453+
Object.defineProperty(provider.taskHistoryStore, "initialized", { value: new Promise<void>(() => {}) })
2454+
2455+
const state = await provider.getStateToPostToWebview({ includeTaskHistory: false })
2456+
2457+
expect(state.taskHistory).toEqual([])
2458+
})
2459+
24502460
it("saves current config when switching to mode without config", async () => {
24512461
await provider.resolveWebviewView(mockWebviewView)
24522462
const messageHandler = (mockWebviewView.webview.onDidReceiveMessage as any).mock.calls[0][0]

‎src/core/webview/__tests__/webviewMessageHandler.spec.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ const mockClineProvider = {
128128
},
129129
log: vi.fn(),
130130
postStateToWebview: vi.fn(),
131+
postStateToWebviewWithoutTaskHistory: vi.fn(),
132+
taskHistoryStore: { isInitialized: true, initialized: Promise.resolve() },
131133
resolveWebviewThemeFixtureProbe: vi.fn(),
132134
getCurrentTask: vi.fn(),
133135
getTaskWithId: vi.fn(),
@@ -3036,3 +3038,42 @@ describe("updateProfileModel", () => {
30363038
expect(updateProfileModel).not.toHaveBeenCalled()
30373039
})
30383040
})
3041+
3042+
describe("webviewMessageHandler - webviewDidLaunch task history", () => {
3043+
function setupLaunch(taskHistoryStore: { isInitialized: boolean; initialized: Promise<void> }) {
3044+
vi.mocked(mockClineProvider.customModesManager.getCustomModes).mockResolvedValue([])
3045+
const launchProvider = mockClineProvider as unknown as {
3046+
getMcpHub: ReturnType<typeof vi.fn>
3047+
providerSettingsManager: { listConfig: ReturnType<typeof vi.fn> }
3048+
taskHistoryStore: typeof taskHistoryStore
3049+
}
3050+
launchProvider.getMcpHub = vi.fn().mockReturnValue(undefined)
3051+
launchProvider.providerSettingsManager = { listConfig: vi.fn().mockResolvedValue(undefined) }
3052+
launchProvider.taskHistoryStore = taskHistoryStore
3053+
vi.mocked(mockClineProvider.postStateToWebview).mockClear()
3054+
vi.mocked(mockClineProvider.postStateToWebviewWithoutTaskHistory).mockClear()
3055+
}
3056+
3057+
it("posts the full state right away once task history is loaded", async () => {
3058+
setupLaunch({ isInitialized: true, initialized: Promise.resolve() })
3059+
3060+
await webviewMessageHandler(mockClineProvider, { type: "webviewDidLaunch" })
3061+
3062+
expect(mockClineProvider.postStateToWebview).toHaveBeenCalledTimes(1)
3063+
expect(mockClineProvider.postStateToWebviewWithoutTaskHistory).not.toHaveBeenCalled()
3064+
})
3065+
3066+
it("renders without waiting for a slow task history load, then posts the full state", async () => {
3067+
let finishLoading!: () => void
3068+
const initialized = new Promise<void>((resolve) => (finishLoading = resolve))
3069+
setupLaunch({ isInitialized: false, initialized })
3070+
3071+
await webviewMessageHandler(mockClineProvider, { type: "webviewDidLaunch" })
3072+
3073+
expect(mockClineProvider.postStateToWebviewWithoutTaskHistory).toHaveBeenCalledTimes(1)
3074+
expect(mockClineProvider.postStateToWebview).not.toHaveBeenCalled()
3075+
3076+
finishLoading()
3077+
await vi.waitFor(() => expect(mockClineProvider.postStateToWebview).toHaveBeenCalledTimes(1))
3078+
})
3079+
})

‎src/core/webview/webviewMessageHandler.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,16 @@ export const webviewMessageHandler = async (
596596
const customModes = await provider.customModesManager.getCustomModes()
597597
await updateGlobalState("customModes", customModes)
598598

599-
await provider.postStateToWebview()
599+
if (provider.taskHistoryStore.isInitialized) {
600+
await provider.postStateToWebview()
601+
} else {
602+
// A large task history can take a long time to scan. Render the UI now and
603+
// send the full state, including history, once the scan completes.
604+
await provider.postStateToWebviewWithoutTaskHistory()
605+
void provider.taskHistoryStore.initialized
606+
.then(() => provider.postStateToWebview())
607+
.catch((err) => provider.log(`Task history state update error: ${err}`))
608+
}
600609
void provider.workspaceTracker
601610
?.initializeFilePaths()
602611
.catch((err) => provider.log(`Workspace initialization error: ${err}`)) // Don't await.

0 commit comments

Comments
 (0)