Skip to content

Commit 395dc1e

Browse files
fix(webview): retire confirmed experiment edits from the change set
An experiment stayed in SettingsView's change set after the host confirmed its value, so a later unrelated Save compared this view's cached copy against a newer live value and posted the older copy back over it. A confirmed experiment now leaves the change set and the confirmed value takes its place in the cache. Experiments that are still being edited, or submitted and not yet confirmed, keep their cached value, so a newer unsaved edit is never dropped, and a value the host confirms after a profile change is adopted rather than written back. Adds SettingsView.experimentRetirement.spec.tsx: four tests. Two of them fail at 41bcddb (the stale re-post, and the reverted checkbox after a profile change) and pass here; the other two pin the pending-intent and profile-reset contracts. They also kill the CallExpression mutant on changedExperiments.current.clear() at SettingsView.tsx:264 that the mutation advisory reported as surviving. Answers the review rows at SettingsView.tsx:455 and SettingsView.tsx:268. This push also re-runs this branch's required contexts: the workflow cannot be re-run from the API (403 "Must have admin rights to Repository.").
1 parent 41bcddb commit 395dc1e

2 files changed

Lines changed: 220 additions & 2 deletions

File tree

‎webview-ui/src/components/settings/SettingsView.tsx‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,44 @@ const SettingsView = forwardRef<SettingsViewRef, SettingsViewProps>(({ onDone, t
268268
}, [currentApiConfigName, mode, extensionState])
269269

270270
// Drop a submitted baseline once the extension state confirms that value.
271+
// Retire a submitted baseline once the extension state confirms that value, and retire the
272+
// edit session for that experiment with it: drop the id from the change set and let the
273+
// confirmed live value stand in the cache. Without this, an experiment this view edited and
274+
// saved stays in the change set, so a later unrelated Save compares this view's cached copy
275+
// against a newer live value written elsewhere and posts the older copy back over it.
276+
// Experiments that are still being edited, or submitted and not yet confirmed, keep their
277+
// cached value, so a newer unsaved edit is never dropped.
271278
useEffect(() => {
279+
const live = extensionState.experiments ?? experimentDefault
280+
const confirmed = new Map<ExperimentId, boolean>()
272281
for (const [id, value] of submittedExperiments.current) {
273-
if ((extensionState.experiments?.[id] ?? experimentDefault[id]) === value) {
274-
submittedExperiments.current.delete(id)
282+
const liveValue = live[id] ?? experimentDefault[id]
283+
if (liveValue !== value) {
284+
continue
275285
}
286+
submittedExperiments.current.delete(id)
287+
changedExperiments.current.delete(id)
288+
confirmed.set(id, liveValue)
276289
}
290+
for (const [id, liveValue] of Object.entries(live)) {
291+
const experimentId = id as ExperimentId
292+
if (submittedExperiments.current.has(experimentId) || changedExperiments.current.has(experimentId)) {
293+
continue
294+
}
295+
confirmed.set(experimentId, liveValue)
296+
}
297+
if (confirmed.size === 0) {
298+
return
299+
}
300+
setCachedState((prevState) => {
301+
const experiments = { ...prevState.experiments } as Record<ExperimentId, boolean>
302+
for (const [id, liveValue] of confirmed) {
303+
if (experiments[id] !== liveValue) {
304+
experiments[id] = liveValue
305+
}
306+
}
307+
return { ...prevState, experiments }
308+
})
277309
}, [extensionState.experiments])
278310

279311
// Bust the cache when settings are imported.
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
import type { ExperimentId } from "@roo-code/types"
2+
import { EXPERIMENT_IDS } from "@roo/experiments"
3+
import { act, fireEvent, screen, waitFor } from "@testing-library/react"
4+
import { QueryClient } from "@tanstack/react-query"
5+
import { beforeEach, describe, expect, it, vi } from "vitest"
6+
7+
import { renderWithExtensionState } from "@/utils/test-utils"
8+
import { useExtensionState } from "@src/context/ExtensionStateContext"
9+
import { vscode } from "@src/utils/vscode"
10+
11+
import SettingsView from "../SettingsView"
12+
13+
vi.mock("@src/utils/vscode", () => ({ vscode: { postMessage: vi.fn() } }))
14+
vi.mock("@/utils/TelemetryClient", () => ({ telemetryClient: { capture: vi.fn() } }))
15+
16+
const postMessage = vi.mocked(vscode.postMessage)
17+
18+
const posted = (type: string) =>
19+
postMessage.mock.calls
20+
.map((call) => call[0] as { type: string; updatedSettings?: { experiments?: Record<string, boolean> } })
21+
.filter((message) => message.type === type)
22+
23+
type HostApi = {
24+
setExperimentEnabled: (id: ExperimentId, enabled: boolean) => void
25+
setCurrentApiConfigName: (name: string) => void
26+
}
27+
28+
let hostApi: HostApi | undefined
29+
30+
function HostProbe() {
31+
const state = useExtensionState()
32+
hostApi = {
33+
setExperimentEnabled: state.setExperimentEnabled,
34+
setCurrentApiConfigName: state.setCurrentApiConfigName,
35+
}
36+
return null
37+
}
38+
39+
const IMAGE_LABEL = "settings:experimental.IMAGE_GENERATION.name"
40+
const FOCUS_LABEL = "settings:experimental.PREVENT_FOCUS_DISRUPTION.name"
41+
const IMAGE_ID = EXPERIMENT_IDS.IMAGE_GENERATION as ExperimentId
42+
const FOCUS_ID = EXPERIMENT_IDS.PREVENT_FOCUS_DISRUPTION as ExperimentId
43+
44+
const renderExperimentalTab = () =>
45+
renderWithExtensionState(
46+
<>
47+
<HostProbe />
48+
<SettingsView onDone={vi.fn()} targetSection="experimental" />
49+
</>,
50+
{
51+
queryClient: new QueryClient({ defaultOptions: { queries: { retry: false } } }),
52+
state: { experiments: { [IMAGE_ID]: false } },
53+
},
54+
)
55+
56+
/** The checkbox of the setting whose own label carries the given translation key. */
57+
const settingCheckbox = (labelKey: string) =>
58+
Array.from(document.querySelectorAll<HTMLInputElement>("input[type=checkbox]")).find((element) => {
59+
const label = element.closest("label")
60+
return !!label && String(label.textContent || "").includes(labelKey)
61+
})
62+
63+
const requireCheckbox = (labelKey: string) => {
64+
const checkbox = settingCheckbox(labelKey)
65+
if (!checkbox) {
66+
throw new Error("no checkbox rendered for " + labelKey)
67+
}
68+
return checkbox
69+
}
70+
71+
const toggleExperiment = async (labelKey: string) => {
72+
await act(async () => {
73+
fireEvent.click(requireCheckbox(labelKey))
74+
})
75+
}
76+
77+
const clickSave = async () => {
78+
await waitFor(() => expect(screen.getByTestId("save-button")).toBeEnabled())
79+
fireEvent.click(screen.getByTestId("save-button"))
80+
}
81+
82+
const openExperimentalTab = async () => {
83+
renderExperimentalTab()
84+
await waitFor(() => expect(screen.getByTestId("tab-experimental")).toBeInTheDocument())
85+
fireEvent.click(screen.getByTestId("tab-experimental"))
86+
await waitFor(() => expect(settingCheckbox(IMAGE_LABEL)).toBeTruthy())
87+
}
88+
89+
describe("SettingsView - experiment edits after the host confirms them", () => {
90+
beforeEach(() => {
91+
postMessage.mockClear()
92+
hostApi = undefined
93+
})
94+
95+
it("does not write a confirmed experiment back over a newer value from elsewhere", async () => {
96+
await openExperimentalTab()
97+
98+
// This view edits one experiment and saves it.
99+
await toggleExperiment(IMAGE_LABEL)
100+
await clickSave()
101+
const firstSave = posted("updateSettings").at(-1)
102+
expect(firstSave?.updatedSettings?.experiments).toEqual({ [IMAGE_ID]: true })
103+
104+
// The host confirms that value, and afterwards a different webview turns the same
105+
// experiment off while this view is no longer editing it.
106+
await act(async () => {
107+
hostApi?.setExperimentEnabled(IMAGE_ID, true)
108+
})
109+
await act(async () => {
110+
hostApi?.setExperimentEnabled(IMAGE_ID, false)
111+
})
112+
113+
// An unrelated save must not resurrect this view's copy of that experiment.
114+
await toggleExperiment(FOCUS_LABEL)
115+
await clickSave()
116+
const secondExperiments = posted("updateSettings").at(-1)?.updatedSettings?.experiments ?? {}
117+
expect(secondExperiments).not.toHaveProperty(String(IMAGE_ID))
118+
expect(secondExperiments).toEqual({ [FOCUS_ID]: false })
119+
}, 20000)
120+
121+
it("keeps an experiment intent the host has not confirmed yet", async () => {
122+
await openExperimentalTab()
123+
124+
// Save posts the intent while the host state still reports the old value, so neither the
125+
// submitted baseline nor the local copy may be dropped before that value arrives.
126+
await toggleExperiment(IMAGE_LABEL)
127+
await clickSave()
128+
const firstSave = posted("updateSettings").at(-1)
129+
expect(firstSave?.updatedSettings?.experiments).toEqual({ [IMAGE_ID]: true })
130+
expect(requireCheckbox(IMAGE_LABEL).checked).toBe(true)
131+
132+
await toggleExperiment(FOCUS_LABEL)
133+
await clickSave()
134+
const secondExperiments = posted("updateSettings").at(-1)?.updatedSettings?.experiments ?? {}
135+
expect(secondExperiments).toEqual({ [FOCUS_ID]: false })
136+
expect(requireCheckbox(IMAGE_LABEL).checked).toBe(true)
137+
}, 20000)
138+
139+
it("does not carry a pending experiment edit into another profile's save", async () => {
140+
await openExperimentalTab()
141+
142+
// An experiment is edited but never saved, and then the settings view switches to a
143+
// different api configuration, which resets the edit session.
144+
await toggleExperiment(IMAGE_LABEL)
145+
await waitFor(() => expect(screen.getByTestId("save-button")).toBeEnabled())
146+
await act(async () => {
147+
hostApi?.setCurrentApiConfigName("another-profile")
148+
})
149+
await waitFor(() => expect(screen.getByTestId("save-button")).toBeDisabled())
150+
151+
// The next save belongs to the new session and posts only its own edit.
152+
await toggleExperiment(FOCUS_LABEL)
153+
await clickSave()
154+
const experiments = posted("updateSettings").at(-1)?.updatedSettings?.experiments ?? {}
155+
expect(experiments).not.toHaveProperty(String(IMAGE_ID))
156+
expect(experiments).toEqual({ [FOCUS_ID]: false })
157+
}, 20000)
158+
159+
it("re-adopts a saved experiment when the host answer arrives after a profile change", async () => {
160+
await openExperimentalTab()
161+
162+
// The save is posted, but a profile snapshot arrives before the host answer reaches this
163+
// view, so the cache reverts to the live value and the session resets.
164+
await toggleExperiment(IMAGE_LABEL)
165+
await clickSave()
166+
const firstSave = posted("updateSettings").at(-1)
167+
expect(firstSave?.updatedSettings?.experiments).toEqual({ [IMAGE_ID]: true })
168+
await act(async () => {
169+
hostApi?.setCurrentApiConfigName("another-profile")
170+
})
171+
await waitFor(() => expect(screen.getByTestId("save-button")).toBeDisabled())
172+
expect(requireCheckbox(IMAGE_LABEL).checked).toBe(false)
173+
174+
// The answer to the earlier save then arrives: the persisted value is the saved one, so
175+
// the view has to show it and must not post the reverted value back on a later save.
176+
await act(async () => {
177+
hostApi?.setExperimentEnabled(IMAGE_ID, true)
178+
})
179+
await waitFor(() => expect(requireCheckbox(IMAGE_LABEL).checked).toBe(true))
180+
await toggleExperiment(FOCUS_LABEL)
181+
await clickSave()
182+
const experiments = posted("updateSettings").at(-1)?.updatedSettings?.experiments ?? {}
183+
expect(experiments).not.toHaveProperty(String(IMAGE_ID))
184+
expect(experiments).toEqual({ [FOCUS_ID]: false })
185+
}, 20000)
186+
})

0 commit comments

Comments
 (0)