Skip to content

Commit 223d3ed

Browse files
PierrunoYTclaude
andcommitted
fix(webview): keep settings posts responsive during history scan and assert staged state
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent ab4940a commit 223d3ed

3 files changed

Lines changed: 94 additions & 2 deletions

File tree

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,30 @@ describe("TaskHistoryStore", () => {
5454

5555
describe("initialize()", () => {
5656
it("reports isInitialized only after the scan completes", async () => {
57+
// Hold the reconciliation scan so the in-flight state can be observed.
58+
let releaseScan!: () => void
59+
const scanGate = new Promise<void>((resolve) => (releaseScan = resolve))
60+
const reconcile = store["reconcile"].bind(store)
61+
store["reconcile"] = async (...args) => {
62+
await scanGate
63+
return reconcile(...args)
64+
}
65+
const settled = vi.fn()
66+
void store.initialized.then(settled)
67+
5768
expect(store.isInitialized).toBe(false)
5869

59-
await store.initialize()
70+
const initializing = store.initialize()
71+
await Promise.resolve()
72+
expect(store.isInitialized).toBe(false)
73+
expect(settled).not.toHaveBeenCalled()
74+
75+
releaseScan()
76+
await initializing
6077

6178
expect(store.isInitialized).toBe(true)
6279
await expect(store.initialized).resolves.toBeUndefined()
80+
expect(settled).toHaveBeenCalledTimes(1)
6381
})
6482

6583
it("initializes from empty state (no index, no task dirs)", async () => {

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { ContextProxy } from "../../config/ContextProxy"
1111
import { Task } from "../../task/Task"
1212
import { ProfileValidator } from "../../../shared/ProfileValidator"
1313
import { ClineProvider } from "../ClineProvider"
14+
import { webviewMessageHandler } from "../webviewMessageHandler"
1415

1516
// Mock setup
1617
vi.mock("p-wait-for", () => ({
@@ -926,6 +927,73 @@ describe("ClineProvider Task History Synchronization", () => {
926927
})
927928
})
928929

930+
describe("state posts while the task history scan is pending", () => {
931+
const postedStates = () =>
932+
findCallsByType(mockPostMessage.mock.calls, "state").map(([message]) => message.state)
933+
934+
// Makes the already-loaded store look like it is still scanning until `release` is called.
935+
function holdTaskHistoryScan() {
936+
const store = provider.taskHistoryStore
937+
let resolveGate!: () => void
938+
const gate = new Promise<void>((resolve) => (resolveGate = resolve))
939+
store["_isInitialized"] = false
940+
Object.defineProperty(store, "initialized", { value: gate })
941+
942+
return () => {
943+
store["_isInitialized"] = true
944+
resolveGate()
945+
}
946+
}
947+
948+
it("launches without history, then posts the full state with history once the scan completes", async () => {
949+
await provider.resolveWebviewView(mockWebviewView)
950+
await provider.updateTaskHistory(createHistoryItem({ id: "launch-task", task: "Launch task" }), {
951+
broadcast: false,
952+
})
953+
// The module-level WorkspaceTracker mock returns undefined; the launch handler chains `.catch` on it.
954+
provider.workspaceTracker!.initializeFilePaths = vi.fn().mockResolvedValue(undefined)
955+
const release = holdTaskHistoryScan()
956+
mockPostMessage.mockClear()
957+
958+
await webviewMessageHandler(provider, { type: "webviewDidLaunch" })
959+
960+
expect(postedStates()).toHaveLength(1)
961+
expect(postedStates()[0]).not.toHaveProperty("taskHistory")
962+
963+
release()
964+
await vi.waitFor(() => expect(postedStates()).toHaveLength(2))
965+
966+
expect(postedStates()[1].taskHistory).toEqual(
967+
expect.arrayContaining([expect.objectContaining({ id: "launch-task" })]),
968+
)
969+
})
970+
971+
it.each([
972+
{ scanComplete: false, expectHistory: false },
973+
{ scanComplete: true, expectHistory: true },
974+
])(
975+
"updateSettings posts state with history: $expectHistory when the scan is complete: $scanComplete",
976+
async ({ scanComplete, expectHistory }) => {
977+
await provider.resolveWebviewView(mockWebviewView)
978+
await provider.updateTaskHistory(createHistoryItem({ id: "settings-task", task: "Settings task" }), {
979+
broadcast: false,
980+
})
981+
if (!scanComplete) {
982+
holdTaskHistoryScan()
983+
}
984+
mockPostMessage.mockClear()
985+
986+
await webviewMessageHandler(provider, {
987+
type: "updateSettings",
988+
updatedSettings: { soundEnabled: true },
989+
})
990+
991+
expect(postedStates()).toHaveLength(1)
992+
expect("taskHistory" in postedStates()[0]).toBe(expectHistory)
993+
},
994+
)
995+
})
996+
929997
describe("taskHistory write lock (mutex)", () => {
930998
it("serializes concurrent updateTaskHistory calls so no entries are lost", async () => {
931999
await provider.resolveWebviewView(mockWebviewView)

‎src/core/webview/webviewMessageHandler.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,13 @@ export const webviewMessageHandler = async (
879879
await provider.contextProxy.setValue(key as keyof RooCodeSettings, newValue)
880880
}
881881

882-
await provider.postStateToWebview()
882+
// While the task history scan runs, a full post would wait on it and stall the
883+
// settings response. The full state follows once the scan completes (see webviewDidLaunch).
884+
if (provider.taskHistoryStore.isInitialized) {
885+
await provider.postStateToWebview()
886+
} else {
887+
await provider.postStateToWebviewWithoutTaskHistory()
888+
}
883889
}
884890

885891
break

0 commit comments

Comments
 (0)