Skip to content

Commit 7cf1937

Browse files
fix(webview): discard a view-state load that a reset cleared underneath
resetState() bumps viewStatesResetGeneration and then clears every live instance's viewLocalState. loadViewState() awaits the mode list and the profile lookup before it applies what it read, and its only discard check compares viewStateId, which a reset does not touch. A load that started before a reset therefore wrote its pre-reset mode, profile name and apiConfiguration back into a buffer the reset had just emptied, and getValues() served them afterwards. The load now captures the generation the durable write queue already uses for the same race and refuses the load if it changed while the lookups were in flight, leaving the existing view-id check untouched. A load that begins after the bump captures the new generation and is unaffected. Test: it("discards a load that a reset cleared underneath it instead of applying pre-reset selections"). Registering the stable id starts the load and the test holds it inside providerSettingsManager.getProfile, then performs the two moves resetState() itself performs on a live instance - bump the generation, clear viewLocalState - resolves the lookup and awaits the load. It asserts on the buffer and on getValues(), not on call counts. It uses its own view id and profile name because every case in this file shares one mockContext and therefore one persisted viewStates slot: reusing another case's id made that case fail on a claim token it no longer owned. Measured with five runs of vitest run webview/__tests__/ClineProvider.spec.ts in this worktree, and the lines below are from those runs, which are the same runs that produced the numstat quoted at the end: HEAD Test Files 1 passed (1) Tests 389 passed (389) (exit 0) case, production at HEAD Test Files 1 failed (1) Tests 1 failed | 389 passed (390), AssertionError: expected { apiConfiguration: {} } to not have property "apiConfiguration" case + generation check Test Files 1 passed (1) Tests 390 passed (390) (exit 0) CONTROL check reverted, Test Files 1 failed (1) Tests 1 failed | 389 passed (390), AssertionError: expected { apiConfiguration: {} } to not have property "apiConfiguration" case kept; the revert is git checkout -- of the one file, so the case is what differs between the second and fourth lines. final shapes after Test Files 1 passed (1) Tests 390 passed (390) (exit 0) Not closed here, measured and reported: a load that begins after the bump but reads the persisted map before resetAllState() wipes it still applies what it read, because it captured the post-bump generation. Closing that needs cancelling loads at clear time rather than a token captured at load start, which this change does not attempt. Scope: the fix adds one discard and one log line. It introduces no new error, refusal, option or setting, so there is no new observable beyond the discard the case asserts. Gates: tsc --noEmit -p . exit 0. eslint --max-warnings=0 exit 0 on both files. eslint --prune-suppressions leaves ClineProvider.ts {"@typescript-eslint/no-explicit-any":{"count":7}} | spec {"@typescript-eslint/no-explicit-any":{"count":195}} exactly as recorded and the suppressions file is not in this commit. prettier --check exits 0 on the copies committed here and 0/0 (file/spec) on the HEAD copies fed through the same invocation, so neither file changes format. 11 added / 0 deleted in ClineProvider.ts and 37 added / 0 deleted in the spec, from the git diff --numstat that fed this message.
1 parent d746c4a commit 7cf1937

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

‎src/core/webview/ClineProvider.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,9 @@ export class ClineProvider
10071007
private async loadViewState(): Promise<void> {
10081008
// A newer id registered while this load is in flight must win.
10091009
const loadedForViewId = this.viewStateId
1010+
// A reset clears this view's buffer and advances the generation without touching the id,
1011+
// so the id alone cannot tell a load that started before the reset from one started after it.
1012+
const loadedForResetGeneration = ClineProvider.viewStatesResetGeneration
10101013

10111014
// Before registration the key is the session-local name, which can be a stale entry
10121015
// from another session: load only entries this instance authored.
@@ -1046,6 +1049,14 @@ export class ClineProvider
10461049
}
10471050
}
10481051

1052+
if (ClineProvider.viewStatesResetGeneration !== loadedForResetGeneration) {
1053+
// The reset emptied this buffer while the load was awaiting modes and profile data; applying
1054+
// what it read would put back selections a reload would drop.
1055+
this.log(
1056+
`[loadViewState] Discarding pre-reset load for view id ${loadedForViewId}: state was reset during the load`,
1057+
)
1058+
return
1059+
}
10491060
if (this.viewStateId !== loadedForViewId) {
10501061
this.log(`[loadViewState] Discarding stale state for superseded view id ${loadedForViewId}`)
10511062
return

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6743,6 +6743,43 @@ describe("ClineProvider", () => {
67436743

67446744
await provider.dispose()
67456745
})
6746+
it("discards a load that a reset cleared underneath it instead of applying pre-reset selections", async () => {
6747+
const provider = new ClineProvider(
6748+
mockContext,
6749+
mockOutputChannel,
6750+
"sidebar",
6751+
new ContextProxy(mockContext),
6752+
new WebviewFocusTracker(),
6753+
)
6754+
const logSpy = vi.spyOn(provider, "log")
6755+
const { getProfile: getProfileSpy, resolveProfile } = stallProviderSettingsProfile(provider)
6756+
// @ts-ignore - Replace customModesManager with a test double (no custom modes).
6757+
provider.customModesManager = { getCustomModes: vi.fn().mockResolvedValue([]), dispose: vi.fn() }
6758+
// Its own view id and profile name, so this case neither reads nor writes another case's slot.
6759+
await provider.saveViewState("mode", "architect")
6760+
await provider.saveViewState("currentApiConfigName", "reset-race-profile")
6761+
// Registering the stable id starts the load of the entry this instance authored, and the load
6762+
// stalls inside the profile lookup - the await a reset most often lands in while a view opens.
6763+
const settling = provider["setViewStateId"]("reset-race-view")
6764+
await vi.waitFor(() => expect(getProfileSpy).toHaveBeenCalledTimes(1))
6765+
6766+
// The reset's own two moves, taken from resetState(): bump the generation that already guards
6767+
// the durable write queue, then clear this instance's buffer as the live-instance loop does.
6768+
ClineProvider["viewStatesResetGeneration"] += 1
6769+
provider["viewLocalState"] = {}
6770+
resolveProfile({ name: "reset-race-profile" })
6771+
await settling
6772+
6773+
// The load started before the reset and its view id never changed, so an id-only guard lets it
6774+
// write the pre-reset selections back into a buffer the reset had just emptied.
6775+
expect(provider["viewLocalState"]).not.toHaveProperty("apiConfiguration")
6776+
expect(provider["viewLocalState"].mode).not.toBe("architect")
6777+
expect(provider["viewLocalState"].currentApiConfigName).not.toBe("reset-race-profile")
6778+
expect(provider.getValues().mode).not.toBe("architect")
6779+
expect(provider.getValues().currentApiConfigName).not.toBe("reset-race-profile")
6780+
expect(logSpy).toHaveBeenCalledWith(expect.stringContaining("state was reset during the load"))
6781+
await provider.dispose()
6782+
})
67466783
})
67476784

67486785
describe("persisted view state pruning", () => {

0 commit comments

Comments
 (0)