Skip to content

Commit 4b3d007

Browse files
fix(config): stop a discarded write from retiring a current one
CodeRabbit review comment 4454293128 (Functional Correctness, Minor) against ec8850b. It held: the rejection handler called retireWrite() before checking the generation, so a write issued before resetAllState() decremented the in-flight count of writes issued after it. The next landing for that key then saw no writes running and copied its value into the mirror while a newer write was still pending. The generation check now comes first in the rejection handler - a discarded write reports its failure and leaves the current generation bookkeeping alone - which also made the second belongsToCurrentState() inside the restore condition redundant, so that one was removed rather than kept as untestable code. Test (47/47 in ContextProxy.spec.ts): a rejected pre-reset write must not retire one of two newer writes, so the older of the newer writes may not decide the mirror while the newest is still in flight. Sweep over the handler: 5 of 6 mutants killed (stale rejection still retires a write, any rejection rolls the mirror back, a pre-reset write may land after the reset, landing never repairs the mirror, rejection leaves the write in flight). The survivor is unchanged and still disclosed: dropping writeSequences.clear() from resetAllState is not observable because the generation guard already ignores that settlement. Regressions: full unit suite 58 failed / 12008 passed (12007 before), 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 ec8850b commit 4b3d007

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

‎src/core/config/ContextProxy.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,13 @@ export class ContextProxy {
430430
}
431431
},
432432
(error) => {
433+
// A write from a discarded generation is not part of this generation's
434+
// in-flight count. Retiring it would credit a newer write as finished and let
435+
// that write decide the mirror while something newer is still running.
436+
if (!belongsToCurrentState()) {
437+
throw error
438+
}
439+
433440
retireWrite()
434441

435442
// The newest write for the key is the one storage would have ended up with,
@@ -439,7 +446,7 @@ export class ContextProxy {
439446
// leaves the mirror to the newer write that is still in flight.
440447
// The map spans every key, so its value is the union over all keys - this
441448
// write's key is the one being restored.
442-
if (belongsToCurrentState() && this.writeSequences.get(key) === sequence) {
449+
if (this.writeSequences.get(key) === sequence) {
443450
this.stateCache[key] = this.persistedValues.get(key) as GlobalState[K]
444451
}
445452
throw error

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,39 @@ describe("ContextProxy", () => {
679679
await postReset
680680
expect(proxy.getGlobalState("apiProvider")).toBe("anthropic")
681681
})
682+
683+
it("keeps a rejected pre-reset write out of the in-flight count of newer writes", async () => {
684+
let failPreReset: (error: Error) => void = () => {}
685+
mockGlobalState.update.mockImplementationOnce(
686+
() => new Promise((_resolve, reject) => (failPreReset = reject)),
687+
)
688+
const preReset = proxy.updateGlobalState("apiProvider", "openai")
689+
690+
await proxy.resetAllState()
691+
692+
let settleOlder: (value: undefined) => void = () => {}
693+
let settleNewer: (value: undefined) => void = () => {}
694+
mockGlobalState.update.mockImplementationOnce(() => new Promise((resolve) => (settleOlder = resolve)))
695+
const older = proxy.updateGlobalState("apiProvider", "anthropic")
696+
mockGlobalState.update.mockImplementationOnce(() => new Promise((resolve) => (settleNewer = resolve)))
697+
const newer = proxy.updateGlobalState("apiProvider", "openrouter")
698+
699+
// The stale rejection must not retire one of the post-reset writes; if it did,
700+
// the older landing below would look like the last write standing.
701+
failPreReset(new Error("globalState write failed"))
702+
await expect(preReset).rejects.toThrow("globalState write failed")
703+
704+
settleOlder(undefined)
705+
await older
706+
707+
// The newer write is still in flight, so its value is what the mirror holds.
708+
expect(proxy.getGlobalState("apiProvider")).toBe("openrouter")
709+
710+
settleNewer(undefined)
711+
await newer
712+
713+
expect(proxy.getGlobalState("apiProvider")).toBe("openrouter")
714+
})
682715
})
683716

684717
describe("invalid apiProvider migration", () => {

0 commit comments

Comments
 (0)