Skip to content

Commit c61e97b

Browse files
fix(config): report what landed when the last write standing for a key rejects
CodeRabbit pre-merge check Persistence Integrity on e265d53. The scenario it names held: with persisted S and three writes A, B, C for one key, let C reject first, A land next and B reject last. C is the newest issued, so the mirror correctly fell back to S; A landed but was not the last write running, so it only advanced the rollback record; B rejected while being the last write standing, but it was not the newest issued, so nothing corrected the mirror. Storage held the value A landed and the cache kept reporting S. The rejection handler now takes the result of retireWrite() and restores the newest persisted value in either case: the rejection was the newest write issued for the key, or nothing is in flight for that key any more. Landing behaviour is unchanged. Test: keeps the landed value when the last write standing for the key rejects - the exact three-write order above, asserting the intermediate fallback to S as well as the final value, through getGlobalState() and getValues(). Sweep over the handler: 7 of 8 mutants killed (stale last-standing rejection keeps the stale mirror, newest rejection not rolled back, any rejection rolls the mirror back, landed write recorded only when last standing, stale rejection still retires a write, pre-reset write may land after the reset, landing never repairs the mirror). The survivor is unchanged: dropping writeSequences.clear() from the reset is not observable behind the generation guard. ContextProxy.spec.ts 49/49. Full unit suite 58 failed / 12010 passed, same 15 pre-existing Windows failures. check-types 114 pre-existing errors, none in a touched file. ESLint exits 0 with --max-warnings=0; eslint-suppressions.json untouched.
1 parent e265d53 commit c61e97b

2 files changed

Lines changed: 46 additions & 8 deletions

File tree

‎src/core/config/ContextProxy.ts‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -437,16 +437,17 @@ export class ContextProxy {
437437
throw error
438438
}
439439

440-
retireWrite()
441-
442-
// The newest write for the key is the one storage would have ended up with,
443-
// so its rejection is rolled back to the newest value that actually
444-
// persisted, even while an older write for the same key is still running -
445-
// otherwise readers report a value that was rejected. An older rejection
446-
// leaves the mirror to the newer write that is still in flight.
440+
const wasLastWriteStanding = retireWrite()
441+
442+
// Two cases decide what the mirror holds:
443+
// - this was the newest write issued for the key: storage will never take
444+
// that value, so fall back to the newest value that persisted, even while
445+
// an older write for the same key is still running;
446+
// - nothing is in flight for this key any more: whatever landed and whatever
447+
// failed, the mirror has to equal what storage actually holds.
447448
// The map spans every key, so its value is the union over all keys - this
448449
// write's key is the one being restored.
449-
if (this.writeSequences.get(key) === sequence) {
450+
if (wasLastWriteStanding || this.writeSequences.get(key) === sequence) {
450451
this.stateCache[key] = this.persistedValues.get(key) as GlobalState[K]
451452
}
452453
throw error

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,43 @@ describe("ContextProxy", () => {
330330
// deliberately leaves provider settings such as `apiProvider` out.
331331
expect(proxy.getValues().apiProvider).toBe("openai")
332332
})
333+
334+
it("keeps the landed value when the last write standing for the key rejects", async () => {
335+
await proxy.updateGlobalState("apiProvider", "deepseek")
336+
337+
let settleOldest: (value: undefined) => void = () => {}
338+
let rejectMiddle: (error: Error) => void = () => {}
339+
let rejectNewest: (error: Error) => void = () => {}
340+
mockGlobalState.update.mockImplementationOnce(() => new Promise((resolve) => (settleOldest = resolve)))
341+
const oldest = proxy.updateGlobalState("apiProvider", "openai")
342+
mockGlobalState.update.mockImplementationOnce(
343+
() => new Promise((_resolve, reject) => (rejectMiddle = reject)),
344+
)
345+
const middle = proxy.updateGlobalState("apiProvider", "anthropic")
346+
mockGlobalState.update.mockImplementationOnce(
347+
() => new Promise((_resolve, reject) => (rejectNewest = reject)),
348+
)
349+
const newest = proxy.updateGlobalState("apiProvider", "openrouter")
350+
351+
// The newest write fails first, so the mirror falls back to what storage held
352+
// before these three writes.
353+
rejectNewest(new Error("globalState write failed"))
354+
await expect(newest).rejects.toThrow("globalState write failed")
355+
expect(proxy.getGlobalState("apiProvider")).toBe("deepseek")
356+
357+
// The oldest write lands, so storage now holds its value while the middle one
358+
// is still in flight.
359+
settleOldest(undefined)
360+
await oldest
361+
362+
// The middle write is the last one standing and it fails too: the mirror has
363+
// to report what landed, not what was there before all three writes.
364+
rejectMiddle(new Error("globalState write failed"))
365+
await expect(middle).rejects.toThrow("globalState write failed")
366+
367+
expect(proxy.getGlobalState("apiProvider")).toBe("openai")
368+
expect(proxy.getValues().apiProvider).toBe("openai")
369+
})
333370
})
334371

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

0 commit comments

Comments
 (0)