Skip to content

Commit 27a2836

Browse files
fix(webview): exempt keys ContextProxy does not count from the rollback generation guard
_restoreSharedValuesAfterFailedViewWrite gates every key on the per-key write generation the setter captured. ContextProxy counts only cached global-state writes: secrets go through storeSecret and pass-through keys straight to the Memento, and neither advances the map, so those keys are captured with generation 1 and still read 0 afterwards. The guard is never satisfiable for them, which exempts exactly the keys whose value cannot be compared back (a secret is not in getValues, a pass-through key is not cached) from the rollback of their own failed attempt: after a failed per-view write the secret and the durable task history stay at the value the failed attempt wrote while every other key is restored. The filter now skips the generation clause for keys the repo's own predicates say ContextProxy does not count - isSecretStateKey from @roo-code/types and isPassThroughStateKey from ../config/ContextProxy - and keeps it everywhere else. ClineProvider.ts 6456 -> 6464 lines. Tests (three cases, all red before the change). - a secret key (nanoGptApiKey) written alongside a counted key with the per-view write failing: asserts the store moved to the new value and the cache is back to the old one; failed with expected 'new-secret' to be 'old-secret'. storeSecret is fire-and-forget, so the attempted value is asserted by presence, not order. - a pass-through key (taskHistory) in the same shape: asserts the Memento was written the new history and then the old one; received only the new write before the fix. The double delegates to the real store, because replacing it would also silence the read-back the guard compares. - the guard's own case: a concurrent provider re-writes the very value the attempt wrote, so only the generation distinguishes them; asserts the newer write stands. Without the generation clause it reads expected 'architect' to be 'code'. Controls (ClineProvider.ts snapshotted at 8a5977a, restored by content hash after each): drop the exemption (the previous guard) -> the secret and pass-through cases fail and the other three pass; delete the generation clause -> only the same-value case fails, so the guard is still load-bearing for counted keys; delete only the secret arm -> only the secret case fails; delete only the pass-through arm -> only the pass-through case fails. Gates: prettier --check clean; tsc --noEmit -p . clean; eslint --max-warnings=0 clean on both files with no suppression count changed; vitest core/webview + core/config 44 files / 1304 tests green (1301 before, +3 cases). Scope 815 -> 818 extension executable lines (types 8).
1 parent 49c626d commit 27a2836

2 files changed

Lines changed: 156 additions & 2 deletions

File tree

‎src/core/webview/ClineProvider.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import {
7575
modelIdKeys,
7676
providerIdentifiers,
7777
PROVIDER_SETTINGS_KEYS,
78+
isSecretStateKey,
7879
} from "@roo-code/types"
7980
import { RateLimitClock, createRateLimitClock } from "../task/RateLimitClock"
8081
import { TaskRegistry } from "../task/TaskRegistry"
@@ -132,7 +133,7 @@ import { t } from "../../i18n"
132133
import { buildApiHandler } from "../../api"
133134
import { forceFullModelDetailsLoad, hasLoadedFullDetails } from "../../api/providers/fetchers/lmstudio"
134135

135-
import { ContextProxy } from "../config/ContextProxy"
136+
import { ContextProxy, isPassThroughStateKey } from "../config/ContextProxy"
136137
import { withoutInvalidDisplaySettings } from "../config/displaySettings"
137138
import { ProviderSettingsManager, ProviderSettingsNotFoundError } from "../config/ProviderSettingsManager"
138139
import { CustomModesManager } from "../config/CustomModesManager"
@@ -6432,9 +6433,16 @@ export class ClineProvider
64326433

64336434
for (const key of Object.keys(attempted) as (keyof RooCodeSettings)[]) {
64346435
const generation = generations[key]
6436+
// ContextProxy does not count every key: secrets go through storeSecret and pass-through
6437+
// keys straight to the Memento, and neither advances the per-key write generation, so the
6438+
// expectation this setter recorded for them (generation 1 against a stored 0) can never
6439+
// match and would exempt those keys from the rollback of their own failed attempt.
6440+
const uncountedHere = isSecretStateKey(key) || isPassThroughStateKey(key)
64356441
if (
64366442
this.contextProxy.getValue(key) === attempted[key] &&
6437-
(generation === undefined || this.contextProxy.getStateWriteGeneration(key as never) === generation)
6443+
(generation === undefined ||
6444+
uncountedHere ||
6445+
this.contextProxy.getStateWriteGeneration(key as never) === generation)
64386446
) {
64396447
;(rollback as Record<string, unknown>)[key] = previous[key]
64406448
}

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

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7210,6 +7210,152 @@ describe("ClineProvider", () => {
72107210
mockContext.globalState.update = originalUpdate
72117211
await provider.dispose()
72127212
})
7213+
it("should roll back a secret key that ContextProxy does not track with a write generation", async () => {
7214+
const provider = new ClineProvider(
7215+
mockContext,
7216+
mockOutputChannel,
7217+
"sidebar",
7218+
new ContextProxy(mockContext),
7219+
new WebviewFocusTracker(),
7220+
)
7221+
await provider.contextProxy.setValue("nanoGptApiKey", "old-secret")
7222+
await provider.contextProxy.setValue("mode", "architect")
7223+
const secretStore = vi.spyOn(mockContext.secrets, "store")
7224+
const originalUpdate = mockContext.globalState.update
7225+
mockContext.globalState.update = vi.fn().mockImplementation((key: string) => {
7226+
if (key === "viewStates") {
7227+
return Promise.reject(new Error("storage unavailable"))
7228+
}
7229+
return Promise.resolve()
7230+
})
7231+
7232+
await expect(provider.setValues({ nanoGptApiKey: "new-secret", mode: "code" })).rejects.toThrow(
7233+
"storage unavailable",
7234+
)
7235+
7236+
// ContextProxy routes secret keys through storeSecret, which never advances the per-key
7237+
// write generation the rollback guard compares against. A key it does not count is captured
7238+
// with generation 1 and still reads 0 afterwards, so the guard excludes it and the secret
7239+
// alone stays at the value the failed attempt wrote while every other key is restored.
7240+
// The attempt did move the secret: ContextProxy.storeSecret is fire-and-forget, so
7241+
// only the presence of the attempted value and the restored end state are deterministic.
7242+
expect(secretStore.mock.calls.map(([key, value]) => [key, value])).toContainEqual([
7243+
"nanoGptApiKey",
7244+
"new-secret",
7245+
])
7246+
expect(provider.getValue("mode")).toBe("architect")
7247+
expect(provider.contextProxy.getSecret("nanoGptApiKey")).toBe("old-secret")
7248+
7249+
mockContext.globalState.update = originalUpdate
7250+
await provider.dispose()
7251+
})
7252+
7253+
it("should keep the generation guard for a counted key whose concurrent write carried the same value", async () => {
7254+
const provider = new ClineProvider(
7255+
mockContext,
7256+
mockOutputChannel,
7257+
"sidebar",
7258+
new ContextProxy(mockContext),
7259+
new WebviewFocusTracker(),
7260+
)
7261+
await provider.contextProxy.setValue("mode", "architect")
7262+
await provider.contextProxy.setValue("currentApiConfigName", "current-config")
7263+
const originalUpdate = mockContext.globalState.update
7264+
let rejectViews: (error: Error) => void = () => {}
7265+
let viewWrites = 0
7266+
mockContext.globalState.update = vi.fn().mockImplementation((key: string) => {
7267+
if (key === "viewStates") {
7268+
// The attempt's own per-view write is parked below; the concurrent write's must land.
7269+
viewWrites++
7270+
if (viewWrites === 1) {
7271+
return new Promise<void>((_, reject) => (rejectViews = reject))
7272+
}
7273+
return Promise.resolve()
7274+
}
7275+
return Promise.resolve()
7276+
})
7277+
7278+
const attempt = provider.setValues({ mode: "code", currentApiConfigName: "other-config" })
7279+
await vi.waitFor(() => expect(provider.getValue("mode")).toBe("code"))
7280+
// A concurrent provider re-writes the very value this attempt wrote: the stored value
7281+
// cannot tell the two writes apart, only the per-key generation can.
7282+
await provider.contextProxy.setValue("mode", "code")
7283+
rejectViews(new Error("storage unavailable"))
7284+
await expect(attempt).rejects.toThrow("storage unavailable")
7285+
7286+
// Restoring the captured "architect" here would undo a newer write that this rollback
7287+
// did not make, for every view that reads shared state.
7288+
expect(provider.getValue("mode")).toBe("code")
7289+
expect(provider.getValue("currentApiConfigName")).toBe("current-config")
7290+
7291+
mockContext.globalState.update = originalUpdate
7292+
await provider.dispose()
7293+
})
7294+
7295+
it("should roll back a pass-through key that ContextProxy hands straight to the Memento", async () => {
7296+
const provider = new ClineProvider(
7297+
mockContext,
7298+
mockOutputChannel,
7299+
"sidebar",
7300+
new ContextProxy(mockContext),
7301+
new WebviewFocusTracker(),
7302+
)
7303+
const firstHistory = [
7304+
{
7305+
id: "task-1",
7306+
number: 1,
7307+
ts: 1700000000000,
7308+
task: "first",
7309+
tokensIn: 1,
7310+
tokensOut: 1,
7311+
totalCost: 0,
7312+
},
7313+
]
7314+
const secondHistory = [
7315+
{
7316+
id: "task-2",
7317+
number: 2,
7318+
ts: 1700000000001,
7319+
task: "second",
7320+
tokensIn: 2,
7321+
tokensOut: 2,
7322+
totalCost: 0,
7323+
},
7324+
]
7325+
await provider.contextProxy.setValue("taskHistory", firstHistory as never)
7326+
await provider.contextProxy.setValue("mode", "architect")
7327+
const originalUpdate = mockContext.globalState.update
7328+
// The double must still move the Memento: a pass-through key is read back from there,
7329+
// so replacing the store outright would hide the very write under test.
7330+
const globalStore = vi.fn().mockImplementation((key: string, value: unknown) => {
7331+
if (key === "viewStates") {
7332+
return Promise.reject(new Error("storage unavailable"))
7333+
}
7334+
return originalUpdate(key, value as never)
7335+
})
7336+
mockContext.globalState.update = globalStore
7337+
7338+
await expect(provider.setValues({ taskHistory: secondHistory as never, mode: "code" })).rejects.toThrow(
7339+
"storage unavailable",
7340+
)
7341+
7342+
// taskHistory is a pass-through key: ContextProxy writes it straight to the Memento and
7343+
// never advances its write generation, so a guard that demands one leaves the durable
7344+
// history at the value the failed attempt wrote.
7345+
// taskHistory is a pass-through key: ContextProxy writes it straight to the Memento and
7346+
// never advances its write generation, so a guard that still demands one for it leaves
7347+
// the durable history at the value the failed attempt wrote while every other key is
7348+
// restored. The write sequence is the observable that separates a restored key from
7349+
// one the rollback never touched.
7350+
expect(globalStore.mock.calls.filter(([key]) => key === "taskHistory").map(([, value]) => value)).toEqual([
7351+
secondHistory,
7352+
firstHistory,
7353+
])
7354+
expect(provider.getValue("mode")).toBe("architect")
7355+
7356+
mockContext.globalState.update = originalUpdate
7357+
await provider.dispose()
7358+
})
72137359

72147360
it("should serve this view's restored mode through getState()", async () => {
72157361
const provider = new ClineProvider(

0 commit comments

Comments
 (0)