Skip to content

Commit 2a2ba74

Browse files
authored
Merge pull request #1415 from samhvw8/fix/reset-all-state-context-proxy
fix reset all state
2 parents c9e21eb + e21953e commit 2a2ba74

File tree

3 files changed

+99
-8
lines changed

3 files changed

+99
-8
lines changed

src/core/__tests__/contextProxy.test.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,77 @@ describe("ContextProxy", () => {
255255
expect(proxy.getGlobalState("unknownKey")).toBe("some-value")
256256
})
257257
})
258+
259+
describe("resetAllState", () => {
260+
it("should clear all in-memory caches", async () => {
261+
// Setup initial state in caches
262+
await proxy.setValues({
263+
apiModelId: "gpt-4", // global state
264+
openAiApiKey: "test-api-key", // secret
265+
unknownKey: "some-value", // unknown
266+
})
267+
268+
// Verify initial state
269+
expect(proxy.getGlobalState("apiModelId")).toBe("gpt-4")
270+
expect(proxy.getSecret("openAiApiKey")).toBe("test-api-key")
271+
expect(proxy.getGlobalState("unknownKey")).toBe("some-value")
272+
273+
// Reset all state
274+
await proxy.resetAllState()
275+
276+
// Caches should be reinitialized with values from the context
277+
// Since our mock globalState.get returns undefined by default,
278+
// the cache should now contain undefined values
279+
expect(proxy.getGlobalState("apiModelId")).toBeUndefined()
280+
expect(proxy.getGlobalState("unknownKey")).toBeUndefined()
281+
})
282+
283+
it("should update all global state keys to undefined", async () => {
284+
// Setup initial state
285+
await proxy.updateGlobalState("apiModelId", "gpt-4")
286+
await proxy.updateGlobalState("apiProvider", "openai")
287+
288+
// Reset all state
289+
await proxy.resetAllState()
290+
291+
// Should have called update with undefined for each key
292+
for (const key of GLOBAL_STATE_KEYS) {
293+
expect(mockGlobalState.update).toHaveBeenCalledWith(key, undefined)
294+
}
295+
296+
// Total calls should include initial setup + reset operations
297+
const expectedUpdateCalls = 2 + GLOBAL_STATE_KEYS.length
298+
expect(mockGlobalState.update).toHaveBeenCalledTimes(expectedUpdateCalls)
299+
})
300+
301+
it("should delete all secrets", async () => {
302+
// Setup initial secrets
303+
await proxy.storeSecret("apiKey", "test-api-key")
304+
await proxy.storeSecret("openAiApiKey", "test-openai-key")
305+
306+
// Reset all state
307+
await proxy.resetAllState()
308+
309+
// Should have called delete for each key
310+
for (const key of SECRET_KEYS) {
311+
expect(mockSecrets.delete).toHaveBeenCalledWith(key)
312+
}
313+
314+
// Total calls should equal the number of secret keys
315+
expect(mockSecrets.delete).toHaveBeenCalledTimes(SECRET_KEYS.length)
316+
})
317+
318+
it("should reinitialize caches after reset", async () => {
319+
// Spy on initialization methods
320+
const initStateCache = jest.spyOn(proxy as any, "initializeStateCache")
321+
const initSecretCache = jest.spyOn(proxy as any, "initializeSecretCache")
322+
323+
// Reset all state
324+
await proxy.resetAllState()
325+
326+
// Should reinitialize caches
327+
expect(initStateCache).toHaveBeenCalledTimes(1)
328+
expect(initSecretCache).toHaveBeenCalledTimes(1)
329+
})
330+
})
258331
})

src/core/contextProxy.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,29 @@ export class ContextProxy {
129129

130130
return Promise.all(promises)
131131
}
132+
133+
/**
134+
* Resets all global state, secrets, and in-memory caches.
135+
* This clears all data from both the in-memory caches and the VSCode storage.
136+
* @returns A promise that resolves when all reset operations are complete
137+
*/
138+
async resetAllState(): Promise<void> {
139+
// Clear in-memory caches
140+
this.stateCache.clear()
141+
this.secretCache.clear()
142+
143+
// Reset all global state values to undefined
144+
const stateResetPromises = GLOBAL_STATE_KEYS.map((key) =>
145+
this.originalContext.globalState.update(key, undefined),
146+
)
147+
148+
// Delete all secrets
149+
const secretResetPromises = SECRET_KEYS.map((key) => this.originalContext.secrets.delete(key))
150+
151+
// Wait for all reset operations to complete
152+
await Promise.all([...stateResetPromises, ...secretResetPromises])
153+
154+
this.initializeStateCache()
155+
this.initializeSecretCache()
156+
}
132157
}

src/core/webview/ClineProvider.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2432,14 +2432,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
24322432
return
24332433
}
24342434

2435-
for (const key of this.context.globalState.keys()) {
2436-
await this.contextProxy.updateGlobalState(key, undefined)
2437-
}
2438-
2439-
for (const key of SECRET_KEYS) {
2440-
await this.storeSecret(key, undefined)
2441-
}
2442-
2435+
await this.contextProxy.resetAllState()
24432436
await this.configManager.resetAllConfigs()
24442437
await this.customModesManager.resetCustomModes()
24452438
await this.removeClineFromStack()

0 commit comments

Comments
 (0)