Skip to content

Commit c2206c5

Browse files
fix(core,webview): confirm the newest resolved write and restore the deletion suite's members
rt4474947849: ContextProxy.updateGlobalState accepted a resolved write as confirmed only while it still owned the key's generation, so an older write that resolved after a newer write had started left `confirmed` holding the pre-write value, and a later rejection restored something no storage ever held. The success path now accepts any resolved write that is newer than the most recently confirmed generation for the key and mutates the entry in place; the generation check stays on the restore path alone. ContextProxy.globalStateRestore.spec.ts adds the A-resolves / B-rejects case and asserts that both getGlobalState(KEY) and storage read "a". rt4474947862: the deleteApiConfiguration suite in webviewMessageHandler.spec.ts reassigns providerSettingsManager and deleteProviderProfile and never put them back, so the doomed-profile fixture leaked into any later test that reads either member without setting its own value. An afterEach now restores both, matching the snapshot-and-restore the rename and launch suites in the same file already do; the activation-warning case keeps its own suite so this head stays on the leak fix rather than a suite reshuffle.
1 parent 2172fcd commit c2206c5

3 files changed

Lines changed: 50 additions & 10 deletions

File tree

‎src/core/config/ContextProxy.ts‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ export class ContextProxy {
5252
// when this write started. Two writes can be in flight at once, so write B's previous
5353
// cached value may be write A's still-unconfirmed value; restoring that instead would
5454
// leave the cache advertising a value storage never accepted.
55-
private stateWrites = new Map<GlobalStateKey, { generation: number; confirmed: unknown }>()
55+
private stateWrites = new Map<
56+
GlobalStateKey,
57+
{ generation: number; confirmed: unknown; confirmedGeneration: number }
58+
>()
5659
private _isInitialized = false
5760

5861
constructor(context: vscode.ExtensionContext) {
@@ -73,7 +76,7 @@ export class ContextProxy {
7376
this.stateCache[key] = this.originalContext.globalState.get(key)
7477
// Whatever storage handed back is, by definition, the value storage last
7578
// confirmed for this key, so a later rejection can restore it.
76-
this.stateWrites.set(key, { generation: 0, confirmed: this.stateCache[key] })
79+
this.stateWrites.set(key, { generation: 0, confirmedGeneration: 0, confirmed: this.stateCache[key] })
7780
} catch (error) {
7881
logger.error(`Error loading global ${key}: ${error instanceof Error ? error.message : String(error)}`)
7982
}
@@ -382,23 +385,31 @@ export class ContextProxy {
382385
const generation = (prior?.generation ?? 0) + 1
383386
// Carry the last storage-confirmed value forward across the write: the cache's current
384387
// content is not evidence that storage accepted it.
385-
this.stateWrites.set(key, { generation, confirmed: prior?.confirmed })
388+
this.stateWrites.set(key, {
389+
generation,
390+
confirmedGeneration: prior?.confirmedGeneration ?? 0,
391+
confirmed: prior?.confirmed,
392+
})
386393
this.stateCache[key] = value
387394

388395
return Promise.resolve(this.originalContext.globalState.update(key, value))
389396
.then((result) => {
390-
// Storage accepted this value, so it becomes what a later rejection of this
391-
// generation may restore. A newer write that already replaced the entry is
392-
// left alone.
397+
// Storage accepted this value, so it becomes what a later rejection may restore.
398+
// Accept it whenever it is newer than the most recently confirmed generation for
399+
// the key: a write that started after this one may already have resolved, and its
400+
// confirmation must not be lost because this older write finishes last. The entry
401+
// is mutated in place, so an older write's handler sees a newer write's state.
393402
const entry = this.stateWrites.get(key)
394-
if (entry?.generation === generation) {
403+
if (entry && generation > entry.confirmedGeneration) {
395404
entry.confirmed = value
405+
entry.confirmedGeneration = generation
396406
}
397407
return result
398408
})
399409
.catch((error: unknown) => {
400-
// Restore only while this write still owns the cache: a newer write may have
401-
// replaced the value (even with the same value) before this rejection surfaced.
410+
// The generation check belongs on this path only: restore only while this write
411+
// still owns the cache, because a newer write may have replaced the value (even
412+
// with the same value) before this rejection surfaced.
402413
const entry = this.stateWrites.get(key)
403414
if (entry?.generation === generation) {
404415
// The one widening cast here: this map is keyed by GlobalStateKey, so its

‎src/core/config/__tests__/ContextProxy.globalStateRestore.spec.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ vi.mock("vscode", () => ({
2222
describe("ContextProxy globalState cache restore", () => {
2323
const KEY = "currentApiConfigName"
2424

25-
const setup = (persisted: string) => {
25+
const setup = (persisted: string, failOnValues: readonly string[] = []) => {
2626
const store = new Map<string, unknown>([[KEY, persisted]])
2727
let storageWorks = true
2828

@@ -40,6 +40,9 @@ describe("ContextProxy globalState cache restore", () => {
4040
},
4141
setKeysForSync(_keys: readonly string[]): void {},
4242
async update(key: string, value: unknown): Promise<void> {
43+
if (failOnValues.includes(value as string)) {
44+
throw new Error("storage unavailable")
45+
}
4346
if (!storageWorks) {
4447
throw new Error("storage unavailable")
4548
}
@@ -110,6 +113,20 @@ describe("ContextProxy globalState cache restore", () => {
110113
},
111114
)
112115

116+
it("keeps the value an older write confirmed when a newer write rejects", async () => {
117+
// rt4474947849: write A starts, write B starts, A resolves, B rejects. Storage ends
118+
// holding A's value, so after B's rejection the cache has to report the same thing
119+
// rather than the value it held before A was confirmed.
120+
const { proxy, store } = setup("X", ["b"])
121+
const writeA = proxy.updateGlobalState(KEY, "a")
122+
const writeB = proxy.updateGlobalState(KEY, "b")
123+
124+
await expect(Promise.resolve(writeA)).resolves.toBeUndefined()
125+
await expect(Promise.resolve(writeB)).rejects.toThrow("storage unavailable")
126+
127+
expect(proxy.getGlobalState(KEY)).toBe("a")
128+
expect(store.get(KEY)).toBe("a")
129+
})
113130
it("restores the value storage last confirmed rather than the value before the failed write", async () => {
114131
const { proxy, store, setStorageWorks } = setup("X")
115132
await proxy.initialize()

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2273,6 +2273,18 @@ describe("webviewMessageHandler - profile deletion routes through the provider m
22732273
type DeletionProviderFixture = { providerSettingsManager: Partial<Writable<ProviderSettingsManager>> }
22742274
const writable = mockClineProvider as DeletionProviderFixture
22752275

2276+
// This suite reassigns both providerSettingsManager and deleteProviderProfile, the way
2277+
// the rename and launch suites do for their own members, so it has to put them back:
2278+
// otherwise the doomed-profile list below leaks into any later test that reads either
2279+
// member without setting its own value.
2280+
const originalProviderSettingsManager = writable.providerSettingsManager
2281+
const originalDeleteProviderProfile = mockClineProvider.deleteProviderProfile
2282+
2283+
afterEach(() => {
2284+
writable.providerSettingsManager = originalProviderSettingsManager
2285+
mockClineProvider.deleteProviderProfile = originalDeleteProviderProfile
2286+
})
2287+
22762288
it("deletes through ClineProvider so the queue, compensation and sibling re-pin still run", async () => {
22772289
writable.providerSettingsManager = {
22782290
listConfig: vi.fn().mockResolvedValue([

0 commit comments

Comments
 (0)