Skip to content

Commit 416e0ea

Browse files
committed
fix(ollama): persist clamped effort on model switch to keep UI and requests in sync
- Add normalizeReasoningEffortOnModelChange: when the selected model's capability array drops the stored value (e.g. "disable" on qwen3 is absent from gpt-oss), persist the clamped fallback the selector shows so the stored effort, displayed effort, and sent think param agree - Chat selector applies it at the model-switch boundary via a reasoningEffort-only write (soft toggle; enableReasoningEffort untouched) - Cover with unit tests for reasoning-effort util; extend selector spec; refresh chat-toolbar visual baselines
1 parent a31a586 commit 416e0ea

10 files changed

Lines changed: 223 additions & 28 deletions

‎webview-ui/src/components/chat/ReasoningEffortSelector.tsx‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ for models that don't, e.g. gpt-oss); the fallback synthesizes
1616
[disable, low, medium, high] when no model info has loaded yet.
1717
*/
1818

19-
import { useCallback, useMemo, useRef, useState } from "react"
19+
import { useCallback, useEffect, useMemo, useRef, useState } from "react"
2020

2121
import type { ModelInfo } from "@roo-code/types/model"
2222
import { providerIdentifiers } from "@roo-code/types/provider-identifiers"
@@ -31,6 +31,7 @@ import {
3131
getOllamaReasoningModelInfo,
3232
getReasoningEffortSelection,
3333
getReasoningEffortTranslationKey,
34+
normalizeReasoningEffortOnModelChange,
3435
type ReasoningEffortOption,
3536
} from "@src/utils/reasoning-effort"
3637
import { vscode } from "@src/utils/vscode"
@@ -61,10 +62,36 @@ export const ReasoningEffortSelector = ({ disabled = false, triggerClassName = "
6162
return selectedModelInfo
6263
}, [provider, selectedModelInfo])
6364

64-
const { isReasoningEffortSupported, availableOptions, currentReasoningEffort } = getReasoningEffortSelection(
65-
apiConfiguration,
66-
modelInfo,
67-
)
65+
const { isReasoningEffortSupported, availableOptions, currentReasoningEffort, storedReasoningEffort } =
66+
getReasoningEffortSelection(apiConfiguration, modelInfo)
67+
68+
// Normalize the stored reasoning effort at the model-switch boundary. When
69+
// the user switches from a disable-capable model (e.g. qwen3, with
70+
// reasoningEffort: "disable") to one that omits "disable" (gpt-oss →
71+
// ["low","medium","high"]), the stored value falls out of the new capability
72+
// array. getReasoningEffortSelection already clamps the *displayed* effort to
73+
// the fallback, but the *stored* value stays "disable" — so the native
74+
// request mapper would send think: false while the UI shows "Low". This
75+
// effect persists the clamped value so the stored effort, the displayed
76+
// effort, and the request stay in sync. It only writes reasoningEffort (never
77+
// enableReasoningEffort), mirroring the chat selector's soft-toggle contract.
78+
useEffect(() => {
79+
if (!currentApiConfigName || !apiConfiguration) {
80+
return
81+
}
82+
const persisted = normalizeReasoningEffortOnModelChange({ storedReasoningEffort, currentReasoningEffort })
83+
if (persisted === undefined) {
84+
return
85+
}
86+
vscode.postMessage({
87+
type: "upsertApiConfiguration",
88+
text: currentApiConfigName,
89+
apiConfiguration: {
90+
...apiConfiguration,
91+
reasoningEffort: persisted,
92+
},
93+
})
94+
}, [apiConfiguration, currentApiConfigName, storedReasoningEffort, currentReasoningEffort])
6895

6996
const handleSelect = useCallback(
7097
(option: ReasoningEffortOption) => {

‎webview-ui/src/components/chat/__tests__/ChatTextArea.visual.fixture.tsx‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,13 @@ const TRIGGER_BASE =
3232
"transition-all duration-150 focus:outline-none focus-visible:ring-1 focus-visible:ring-vscode-focusBorder focus-visible:ring-inset " +
3333
"opacity-90 hover:opacity-100 hover:bg-[rgba(255,255,255,0.03)] hover:border-[rgba(255,255,255,0.15)] cursor-pointer"
3434

35-
// The same triggerClassName ChatTextArea passes to each control, tuned for the
36-
// compact toolbar (min-width + ellipsis + flex-shrink so the row overflows
37-
// gracefully at narrow widths). ModeSelector uses flex-shrink-0 in production,
38-
// but the narrow-width snapshot needs to show truncation across the whole row
39-
// (mode truncated, then bits of api-config and reasoning peeking through) — so
40-
// the fixture gives the mode trigger the same shrinkable treatment as its
41-
// siblings to exercise the overflow layout meaningfully.
42-
const MODE_TRIGGER = `${TRIGGER_BASE} min-w-0 text-ellipsis overflow-hidden flex-shrink`
35+
// The same triggerClassName ChatTextArea passes to each control in production.
36+
// ModeSelector gets `text-ellipsis overflow-hidden flex-shrink-0` (no min-w-0),
37+
// so it keeps its full width and does not shrink — matching the production
38+
// layout the narrow-width snapshot must validate. ApiConfigSelector and
39+
// ReasoningEffortSelector get `min-w-[28px] text-ellipsis overflow-hidden
40+
// flex-shrink`, so they shrink/ellipsis first when the row overflows.
41+
const MODE_TRIGGER = `${TRIGGER_BASE} text-ellipsis overflow-hidden flex-shrink-0`
4342
const SHRINK_TRIGGER = `${TRIGGER_BASE} min-w-0 text-ellipsis overflow-hidden flex-shrink`
4443
const AUTO_APPROVE_TRIGGER =
4544
"inline-flex items-center gap-1.5 relative whitespace-nowrap px-1.5 py-1 text-xs " +
@@ -85,6 +84,15 @@ interface ChatToolbarFixtureProps {
8584
* right cluster with `gap-0.5`.
8685
*/
8786
export const ChatToolbarFixture = ({ width }: ChatToolbarFixtureProps) => (
87+
// `i18n` is the production TranslationContext's `typeof i18next`, which we
88+
// cannot satisfy with a real instance in a Playwright CT fixture without
89+
// importing and initializing i18next. The CT config aliases
90+
// `@src/i18n/TranslationContext` to a shim whose `i18n` is `unknown`, but
91+
// `tsc` type-checks against the real module, so the double assertion through
92+
// `unknown` is required to bridge `null` to `typeof i18next`. This is the
93+
// "last resort" double assertion the coding guidelines allow for
94+
// unavoidable casts; the `t` function (the only field the fixture actually
95+
// uses) is provided directly.
8896
<TranslationContext.Provider
8997
value={{
9098
t: (key: string) => translations[key] ?? key,
@@ -96,7 +104,7 @@ export const ChatToolbarFixture = ({ width }: ChatToolbarFixtureProps) => (
96104
style={{ width: `${width}px` }}>
97105
<div className="flex items-center gap-2 min-w-0 overflow-clip flex-1">
98106
<button type="button" data-testid="mode-selector-trigger" className={MODE_TRIGGER}>
99-
<span className="truncate">🛡️ Security Reviewer</span>
107+
<span className="truncate">Security Reviewer</span>
100108
</button>
101109
<button type="button" data-testid="dropdown-trigger" className={SHRINK_TRIGGER}>
102110
<span className="truncate">Ollama-glm</span>

‎webview-ui/src/components/chat/__tests__/ChatTextArea.visual.tsx‎

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,11 @@ const themes = [
2323
name: "dark",
2424
bodyClass: "vscode-dark",
2525
themeId: "Default Dark Modern",
26-
editorBackground: "#1e1e1e",
2726
},
2827
{
2928
name: "light",
3029
bodyClass: "vscode-light",
3130
themeId: "Default Light Modern",
32-
editorBackground: "#ffffff",
3331
},
3432
] as const
3533

@@ -57,21 +55,18 @@ for (const theme of themes) {
5755
document.body.dataset.vscodeThemeId = themeId
5856
}, theme)
5957

58+
// Confirm the theme class was applied before snapshotting. We assert
59+
// only the documentClass (not the resolved --vscode-editor-background
60+
// hex), because the token's exact value varies across Playwright image
61+
// revisions and is not what this baseline guards; the screenshot itself
62+
// proves the theme rendered.
6063
await expect
6164
.poll(() =>
62-
trigger.evaluate((element) => {
63-
const body = element.ownerDocument.body
64-
const styles = getComputedStyle(body)
65-
return {
66-
documentClass: element.ownerDocument.documentElement.className,
67-
editorBackground: styles.getPropertyValue("--vscode-editor-background").trim(),
68-
}
69-
}),
65+
trigger.evaluate((element) => ({
66+
documentClass: element.ownerDocument.documentElement.className,
67+
})),
7068
)
71-
.toEqual({
72-
documentClass: theme.bodyClass,
73-
editorBackground: theme.editorBackground,
74-
})
69+
.toEqual({ documentClass: theme.bodyClass })
7570

7671
await component.evaluate(async () => {
7772
await document.fonts.ready

‎webview-ui/src/components/chat/__tests__/ReasoningEffortSelector.spec.tsx‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,64 @@ describe("ReasoningEffortSelector", () => {
248248
)
249249
})
250250

251+
it("normalizes a stale 'disable' to the clamped fallback on switch from a disable-capable model to gpt-oss", () => {
252+
// Regression: the user saved "disable" on qwen3 (which honors think:
253+
// false) and then selected gpt-oss (which omits "disable" from its
254+
// capability array). The selector's useEffect must persist the clamped
255+
// fallback (gpt-oss's first option "low") so the stored effort matches
256+
// what the UI shows and what the native request mapper sends, instead of
257+
// leaving reasoningEffort: "disable" to map to think: false.
258+
selectedModel.provider = "ollama"
259+
selectedModel.id = "gpt-oss:20b"
260+
selectedModel.info = {
261+
contextWindow: 131072,
262+
supportsPromptCache: true,
263+
supportsReasoningEffort: ["low", "medium", "high"],
264+
}
265+
extensionState.apiConfiguration = {
266+
apiProvider: "ollama",
267+
ollamaModelId: "gpt-oss:20b",
268+
reasoningEffort: "disable",
269+
enableReasoningEffort: true,
270+
}
271+
272+
render(<ReasoningEffortSelector />)
273+
274+
expect(postMessageMock).toHaveBeenCalledWith(
275+
expect.objectContaining({
276+
type: "upsertApiConfiguration",
277+
text: "default",
278+
apiConfiguration: expect.objectContaining({
279+
reasoningEffort: "low",
280+
// enableReasoningEffort is untouched (chat selector is a soft toggle)
281+
enableReasoningEffort: true,
282+
}),
283+
}),
284+
)
285+
})
286+
287+
it("does not persist when the stored effort is still valid for the selected model", () => {
288+
// "low" is valid for gpt-oss, so switching to gpt-oss with a stored "low"
289+
// must not fire a normalization write.
290+
selectedModel.provider = "ollama"
291+
selectedModel.id = "gpt-oss:20b"
292+
selectedModel.info = {
293+
contextWindow: 131072,
294+
supportsPromptCache: true,
295+
supportsReasoningEffort: ["low", "medium", "high"],
296+
}
297+
extensionState.apiConfiguration = {
298+
apiProvider: "ollama",
299+
ollamaModelId: "gpt-oss:20b",
300+
reasoningEffort: "low",
301+
enableReasoningEffort: true,
302+
}
303+
304+
render(<ReasoningEffortSelector />)
305+
306+
expect(postMessageMock).not.toHaveBeenCalled()
307+
})
308+
251309
it("renders for ollama even when no model info has loaded", () => {
252310
// The user expects the chat bar to show a reasoning selector for ollama
253311
// no matter what — this is the primary use case for the chat selector.
-542 Bytes
Loading
-508 Bytes
Loading
-335 Bytes
Loading
-105 Bytes
Loading
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// npx vitest src/utils/__tests__/reasoning-effort.spec.ts
2+
3+
import { describe, expect, it } from "vitest"
4+
5+
import type { ModelInfo, ProviderSettings } from "@roo-code/types"
6+
7+
import { getReasoningEffortSelection, normalizeReasoningEffortOnModelChange } from "@src/utils/reasoning-effort"
8+
9+
// Capability arrays the fetcher advertises per model family (see
10+
// src/api/providers/fetchers/ollama.ts getOllamaThinkingEfforts). gpt-oss
11+
// ignores think: false and accepts only low/medium/high, so its array omits
12+
// "disable" and "max" — unlike disable-capable thinking models (e.g. qwen3 →
13+
// ["disable","low","medium","high","max"]).
14+
const gptOssModel: ModelInfo = {
15+
contextWindow: 131072,
16+
supportsPromptCache: true,
17+
supportsReasoningEffort: ["low", "medium", "high"],
18+
reasoningEffort: "medium",
19+
}
20+
21+
describe("normalizeReasoningEffortOnModelChange", () => {
22+
it("returns undefined when no stored value is set (fresh profile)", () => {
23+
const selection = getReasoningEffortSelection(undefined, gptOssModel)
24+
expect(normalizeReasoningEffortOnModelChange(selection)).toBeUndefined()
25+
})
26+
27+
it("returns undefined when the stored value is still a valid option", () => {
28+
const apiConfiguration = { reasoningEffort: "medium" } as ProviderSettings
29+
const selection = getReasoningEffortSelection(apiConfiguration, gptOssModel)
30+
expect(normalizeReasoningEffortOnModelChange(selection)).toBeUndefined()
31+
})
32+
33+
it("persists the clamped fallback when switching from a disable-capable model to gpt-oss with reasoningEffort: disable", () => {
34+
// Regression: the user saved "disable" on qwen3 (which honors think:
35+
// false), then switched to gpt-oss (which omits "disable" from its
36+
// capability array). getReasoningEffortSelection clamps the *displayed*
37+
// effort to the fallback (gpt-oss's first option "low", since gpt-oss is
38+
// not required-reasoning so the default is "disable" which is itself
39+
// invalid, falling through to availableOptions[0] = "low"), but without
40+
// normalization the *stored* value stays "disable" — so the native
41+
// request mapper would send think: false while the UI shows "Low". The
42+
// helper returns the clamped value to persist so the stored effort, the
43+
// displayed effort, and the request stay in sync.
44+
const apiConfiguration = { reasoningEffort: "disable" } as ProviderSettings
45+
const selection = getReasoningEffortSelection(apiConfiguration, gptOssModel)
46+
47+
// Sanity: the display clamped away from "disable"...
48+
expect(selection.currentReasoningEffort).not.toBe("disable")
49+
// ...and the stored value is no longer valid for gpt-oss's array.
50+
expect(selection.storedReasoningEffort).toBe("disable")
51+
expect(gptOssModel.supportsReasoningEffort).not.toContain("disable")
52+
53+
// The helper returns the clamped fallback ("low") to persist.
54+
expect(normalizeReasoningEffortOnModelChange(selection)).toBe(selection.currentReasoningEffort)
55+
expect(normalizeReasoningEffortOnModelChange(selection)).toBe("low")
56+
})
57+
58+
it("clamps 'max' to the fallback when switching from a max-capable model to gpt-oss", () => {
59+
// Symmetric case: "max" is valid for qwen3 but not gpt-oss. Switching
60+
// models should normalize the stored "max" to gpt-oss's first option.
61+
const apiConfiguration = { reasoningEffort: "max" } as ProviderSettings
62+
const selection = getReasoningEffortSelection(apiConfiguration, gptOssModel)
63+
64+
expect(normalizeReasoningEffortOnModelChange(selection)).toBe("low")
65+
})
66+
67+
it("does not normalize when switching back to a model that still supports the stored value", () => {
68+
// "low" is valid for both qwen3 and gpt-oss, so switching between them
69+
// must not trigger a write.
70+
const apiConfiguration = { reasoningEffort: "low" } as ProviderSettings
71+
const selection = getReasoningEffortSelection(apiConfiguration, gptOssModel)
72+
expect(normalizeReasoningEffortOnModelChange(selection)).toBeUndefined()
73+
})
74+
})

‎webview-ui/src/utils/reasoning-effort.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,39 @@ export function getReasoningEffortSelection(
8282
return { isReasoningEffortSupported, availableOptions, currentReasoningEffort, storedReasoningEffort }
8383
}
8484

85+
/**
86+
* Returns the reasoning-effort value a consumer should persist after the
87+
* selected model's capability array changes, or `undefined` when the stored
88+
* value is still valid for the new capability set (no write needed).
89+
*
90+
* This closes the model-switch gap: when a user saves e.g. "disable" on a
91+
* disable-capable model (qwen3 → ["disable","low","medium","high","max"]) and
92+
* then switches to a model that omits "disable" (gpt-oss →
93+
* ["low","medium","high"]), `getReasoningEffortSelection` clamps the *displayed*
94+
* effort to the fallback ("low") but the *stored* value stays "disable". The
95+
* native request mapper reads the stored value, so it would send `think: false`
96+
* while the UI shows "Low". Persisting the clamped value at the model-switch
97+
* boundary keeps the stored value, the displayed effort, and the request in
98+
* sync. Callers gate the write behind `enableReasoningEffort` / their own
99+
* persistence path so they do not silently flip a user's thinking-off state.
100+
*/
101+
export function normalizeReasoningEffortOnModelChange(
102+
selection: Pick<ReasoningEffortSelection, "storedReasoningEffort" | "currentReasoningEffort">,
103+
): ReasoningEffortOption | undefined {
104+
const { storedReasoningEffort, currentReasoningEffort } = selection
105+
// No stored value (fresh profile) → nothing to normalize.
106+
if (storedReasoningEffort === undefined) {
107+
return undefined
108+
}
109+
// Stored value is still a valid option → keep it (no write).
110+
if (storedReasoningEffort === currentReasoningEffort) {
111+
return undefined
112+
}
113+
// Stored value fell out of the new capability array → persist the clamped
114+
// fallback the UI is already displaying.
115+
return currentReasoningEffort
116+
}
117+
85118
/**
86119
* Builds the `ModelInfo` the Ollama settings page and chat selector feed into
87120
* `getReasoningEffortSelection`. This is the single shared Ollama normalization

0 commit comments

Comments
 (0)