Skip to content

Commit 6536617

Browse files
fix(config): roll the settings cache back when a state write is rejected
Third CodeRabbit round (Persistence Integrity). `ContextProxy.updateGlobalState` wrote the key into `stateCache` before `globalState.update` settled, so a rejected write left an in-memory value that disk never received: readers, `export()`/`import()` and the next restart all report a setting that does not exist. The cache now rolls the key back to the value that is actually on disk and the rejection still reaches the caller; the success path is unchanged, and `Memento.update` is normalized first because it is only specified as a `Thenable` (removing that normalization breaks 46 tests that stub it as a plain object). This is the failure `exportSettings` can now report: it confirms the file it wrote and says the location could not be remembered. Without the rollback, the next dialog would still default to that unremembered location for the rest of the run. Tests: 2 new in `core/config/__tests__/ContextProxy.spec.ts` (38/38). Six of seven hand-written mutants over the new lines are killed (drop the rollback, roll back to undefined, do not capture the previous value, swallow the rejection, skip the cache write on success, drop the normalization); the seventh re-assigns the same value on success and is equivalent. Full unit suite before and after: 58 failed / 11996 passed before, 58 failed / 11998 passed after - the same 15 pre-existing Windows failures (Zoo-Code-Org#2059 among them), no new ones. `eslint --max-warnings=0` exits 0 with unchanged suppression counts for both files.
1 parent bcd0822 commit 6536617

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

‎src/core/config/ContextProxy.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,21 @@ export class ContextProxy {
365365
return this.originalContext.globalState.update(key, value)
366366
}
367367

368+
const previousValue = this.stateCache[key]
368369
this.stateCache[key] = value
369-
return this.originalContext.globalState.update(key, value)
370+
371+
// The cache is globalState's in-memory mirror, so a value that never reached
372+
// disk must not stay in the mirror: readers would report a setting that the
373+
// next restart will not reproduce. Roll the key back to what is on disk and
374+
// let the rejection reach the caller. Memento.update is only specified as a
375+
// Thenable, so its result is normalized before a rejection handler is hung
376+
// off it.
377+
const update = Promise.resolve(this.originalContext.globalState.update(key, value))
378+
379+
return update.then(undefined, (error) => {
380+
this.stateCache[key] = previousValue
381+
throw error
382+
})
370383
}
371384

372385
private getAllGlobalState(): GlobalState {

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,30 @@ describe("ContextProxy", () => {
194194
expect(storedValue).toBe(historyItems)
195195
expect(mockGlobalState.get).toHaveBeenCalledWith("taskHistory")
196196
})
197+
198+
it("keeps the value that is actually on disk when an update is rejected", async () => {
199+
await proxy.updateGlobalState("apiProvider", "deepseek")
200+
mockGlobalState.update.mockRejectedValueOnce(new Error("globalState write failed"))
201+
202+
await expect(proxy.updateGlobalState("apiProvider", "openai")).rejects.toThrow("globalState write failed")
203+
204+
// The rejected value never reached disk, so it must not live in the cache;
205+
// the value that is on disk must survive the failed write next to it.
206+
expect(proxy.getGlobalState("apiProvider")).toBe("deepseek")
207+
})
208+
209+
it("leaves a key unset when its first update is rejected", async () => {
210+
mockGlobalState.update.mockRejectedValueOnce(new Error("globalState write failed"))
211+
212+
await expect(proxy.setValue("lastSettingsExportPath", "/usb/zoo-code-settings.json")).rejects.toThrow(
213+
"globalState write failed",
214+
)
215+
216+
// Nothing was remembered: the export dialog default stays where the user
217+
// last left it instead of pointing at a location that was never stored.
218+
expect(proxy.getValue("lastSettingsExportPath")).toBeUndefined()
219+
expect(mockGlobalState.update).toHaveBeenCalledWith("lastSettingsExportPath", "/usb/zoo-code-settings.json")
220+
})
197221
})
198222

199223
describe("getSecret", () => {

0 commit comments

Comments
 (0)