Skip to content

Commit ab4940a

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 Zoo-Code-Org#2095 Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent ca05b73 commit ab4940a

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
@@ -2743,8 +2743,11 @@ export class ClineProvider
27432743
}
27442744

27452745
async getStateToPostToWebview({ includeTaskHistory = true }: GetStateOptions = {}): Promise<ExtensionState> {
2746-
// Ensure the store is initialized before reading task history
2747-
await this.taskHistoryStore.initialized
2746+
// Only wait for the store when task history is requested, so a slow scan of a large
2747+
// history cannot keep the webview from receiving its first state.
2748+
if (includeTaskHistory) {
2749+
await this.taskHistoryStore.initialized
2750+
}
27482751

27492752
const {
27502753
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(),
@@ -3053,3 +3055,42 @@ describe("webviewMessageHandler - renameTaskWithId", () => {
30533055
expect(vi.mocked(mockClineProvider.renameTaskWithId).mock.calls).toEqual(expectedCalls)
30543056
})
30553057
})
3058+
3059+
describe("webviewMessageHandler - webviewDidLaunch task history", () => {
3060+
function setupLaunch(taskHistoryStore: { isInitialized: boolean; initialized: Promise<void> }) {
3061+
vi.mocked(mockClineProvider.customModesManager.getCustomModes).mockResolvedValue([])
3062+
const launchProvider = mockClineProvider as unknown as {
3063+
getMcpHub: ReturnType<typeof vi.fn>
3064+
providerSettingsManager: { listConfig: ReturnType<typeof vi.fn> }
3065+
taskHistoryStore: typeof taskHistoryStore
3066+
}
3067+
launchProvider.getMcpHub = vi.fn().mockReturnValue(undefined)
3068+
launchProvider.providerSettingsManager = { listConfig: vi.fn().mockResolvedValue(undefined) }
3069+
launchProvider.taskHistoryStore = taskHistoryStore
3070+
vi.mocked(mockClineProvider.postStateToWebview).mockClear()
3071+
vi.mocked(mockClineProvider.postStateToWebviewWithoutTaskHistory).mockClear()
3072+
}
3073+
3074+
it("posts the full state right away once task history is loaded", async () => {
3075+
setupLaunch({ isInitialized: true, initialized: Promise.resolve() })
3076+
3077+
await webviewMessageHandler(mockClineProvider, { type: "webviewDidLaunch" })
3078+
3079+
expect(mockClineProvider.postStateToWebview).toHaveBeenCalledTimes(1)
3080+
expect(mockClineProvider.postStateToWebviewWithoutTaskHistory).not.toHaveBeenCalled()
3081+
})
3082+
3083+
it("renders without waiting for a slow task history load, then posts the full state", async () => {
3084+
let finishLoading!: () => void
3085+
const initialized = new Promise<void>((resolve) => (finishLoading = resolve))
3086+
setupLaunch({ isInitialized: false, initialized })
3087+
3088+
await webviewMessageHandler(mockClineProvider, { type: "webviewDidLaunch" })
3089+
3090+
expect(mockClineProvider.postStateToWebviewWithoutTaskHistory).toHaveBeenCalledTimes(1)
3091+
expect(mockClineProvider.postStateToWebview).not.toHaveBeenCalled()
3092+
3093+
finishLoading()
3094+
await vi.waitFor(() => expect(mockClineProvider.postStateToWebview).toHaveBeenCalledTimes(1))
3095+
})
3096+
})

‎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)