Skip to content

Commit 1ec2f94

Browse files
committed
fix(reasoning): preserve required model defaults
1 parent 5dc24c0 commit 1ec2f94

7 files changed

Lines changed: 171 additions & 10 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"check-types": "turbo check-types --log-order grouped --output-logs new-only",
1414
"test": "turbo test --log-order grouped --output-logs new-only",
1515
"test:mutation-ci": "node --test scripts/stryker-diff.test.mjs",
16+
"lifecycle:model": "tsx scripts/check-reasoning-defaults.ts",
1617
"lifecycle:model-check": "tsx scripts/check-task-lifecycle.ts && tsx scripts/check-task-store-concurrency.ts",
1718
"test:coverage": "turbo test:coverage --log-order grouped --output-logs new-only",
1819
"format": "turbo format --log-order grouped --output-logs new-only",
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import assert from "node:assert/strict"
2+
3+
type Effort = "disable" | "none" | "minimal" | "low" | "medium" | "high" | "xhigh" | "max"
4+
type Supported = true | readonly Effort[]
5+
6+
interface ModelState {
7+
supported: Supported
8+
required: boolean
9+
modelDefault?: Effort
10+
stored?: Effort
11+
enabled?: boolean
12+
}
13+
14+
const canonicalEfforts = ["low", "medium", "high", "xhigh", "max"] as const
15+
const defaultEfforts = ["low", "medium", "high"] as const
16+
const supportedSets: Supported[] = [
17+
true,
18+
["disable", "low", "high"],
19+
["low", "high"],
20+
["none", "low", "high"],
21+
["low", "medium", "high", "xhigh", "max"],
22+
]
23+
const values = [undefined, "disable", "none", "low", "high", "max"] as const
24+
const enabledValues = [undefined, false, true] as const
25+
26+
function availableOptions(state: ModelState): readonly Effort[] {
27+
return state.supported === true
28+
? state.required
29+
? defaultEfforts
30+
: ["disable", ...defaultEfforts]
31+
: state.supported
32+
}
33+
34+
function supportsEffort(supported: Supported, effort: Effort): boolean {
35+
return supported === true || supported.includes(effort)
36+
}
37+
38+
function resolveSelection(state: ModelState): Effort {
39+
const available = availableOptions(state)
40+
const defaultEffort = state.modelDefault ?? (state.required ? "medium" : "disable")
41+
const raw = state.stored ?? defaultEffort
42+
const fallback = available.includes(defaultEffort) ? defaultEffort : (available[0] ?? raw)
43+
return available.includes(raw) ? raw : fallback
44+
}
45+
46+
function resolveRequest(state: ModelState): Effort | undefined {
47+
const disabled = state.stored === "disable" || state.stored === "none" || state.enabled === false
48+
const canDisable = supportsEffort(state.supported, "disable")
49+
if (disabled && canDisable) return undefined
50+
51+
const candidates = [disabled ? undefined : state.stored, state.modelDefault]
52+
const supported = state.supported
53+
if (supported !== true && !supported.includes("disable")) {
54+
candidates.push(canonicalEfforts.find((effort) => supported.includes(effort)))
55+
}
56+
57+
for (const effort of candidates) {
58+
if (
59+
effort &&
60+
effort !== "disable" &&
61+
effort !== "none" &&
62+
effort !== "minimal" &&
63+
supportsEffort(state.supported, effort)
64+
) {
65+
return effort
66+
}
67+
}
68+
69+
return state.required && state.modelDefault && state.modelDefault !== "none" ? state.modelDefault : undefined
70+
}
71+
72+
let checked = 0
73+
for (const supported of supportedSets) {
74+
for (const required of [false, true]) {
75+
for (const modelDefault of values) {
76+
for (const stored of values) {
77+
for (const enabled of enabledValues) {
78+
const state: ModelState = { supported, required, modelDefault, stored, enabled }
79+
const selection = resolveSelection(state)
80+
const available = availableOptions(state)
81+
assert.ok(available.length === 0 || available.includes(selection), "selection must be supported")
82+
if (!available.includes("disable"))
83+
assert.notEqual(selection, "disable", "required reasoning cannot disable")
84+
85+
const normalized: ModelState = {
86+
...state,
87+
stored: selection,
88+
enabled: required || selection !== "disable",
89+
}
90+
const request = resolveRequest(normalized)
91+
if (selection === "disable") {
92+
assert.equal(request, undefined, "an explicit supported disable must omit reasoning")
93+
} else if (canonicalEfforts.includes(selection as (typeof canonicalEfforts)[number])) {
94+
assert.equal(request, selection, "a normalized effort must reach the request")
95+
}
96+
97+
checked++
98+
}
99+
}
100+
}
101+
}
102+
}
103+
104+
console.log(`Reasoning defaults model check passed (${checked} states)`)

‎src/api/providers/__tests__/nanogpt.spec.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,37 @@ describe("NanoGptHandler", () => {
235235
expect(mockCreate.mock.calls[0][0]).toMatchObject({ reasoning_effort: "low" })
236236
})
237237

238+
it.each([
239+
["a stale disable effort", { reasoningEffort: "disable" as const }],
240+
["a stale disabled toggle", { enableReasoningEffort: false }],
241+
])("uses a supported fallback for %s when the model cannot disable reasoning", async (_name, settings) => {
242+
vi.mocked(getModels).mockResolvedValue({
243+
"model:thinking": {
244+
maxTokens: 128000,
245+
contextWindow: 1050000,
246+
supportsPromptCache: false,
247+
supportsReasoningEffort: ["low", "high"],
248+
},
249+
})
250+
251+
await collectStream(
252+
new NanoGptHandler({ nanoGptModelId: "model:thinking", ...settings }).createMessage("sys", messages),
253+
)
254+
255+
expect(mockCreate.mock.calls[0][0]).toMatchObject({ reasoning_effort: "low" })
256+
})
257+
238258
it.each([undefined, true] as const)(
239259
"omits reasoning effort when the disable option is selected and enableReasoningEffort is %s",
240260
async (enableReasoningEffort) => {
261+
vi.mocked(getModels).mockResolvedValue({
262+
"model:thinking": {
263+
maxTokens: 128000,
264+
contextWindow: 1050000,
265+
supportsPromptCache: false,
266+
supportsReasoningEffort: ["disable", "low", "high"],
267+
},
268+
})
241269
await collectStream(
242270
new NanoGptHandler({
243271
nanoGptModelId: "model:thinking",
@@ -263,6 +291,14 @@ describe("NanoGptHandler", () => {
263291
})
264292

265293
it("omits reasoning effort when reasoning is explicitly disabled", async () => {
294+
vi.mocked(getModels).mockResolvedValue({
295+
"model:thinking": {
296+
maxTokens: 128000,
297+
contextWindow: 1050000,
298+
supportsPromptCache: false,
299+
supportsReasoningEffort: ["disable", "low", "high"],
300+
},
301+
})
266302
await collectStream(
267303
new NanoGptHandler({
268304
nanoGptModelId: "model:thinking",

‎src/api/providers/nanogpt.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type NanoGptCachingRequest = { caching?: true }
3232
const NANO_GPT_MERGED_TOOL_RESULT_MODELS = new Set(["meta/muse-spark-1.2-contributor"])
3333

3434
const NANO_GPT_ASTRA_MODEL_IDS = new Set(["openai/gpt-6-astra", "openai/gpt-6-astra-pro"])
35+
const NANO_GPT_REASONING_EFFORTS = ["low", "medium", "high", "xhigh", "max"] as const
3536

3637
function getReasoningEffort(options: ApiHandlerOptions, info: ModelInfo): ReasoningEffortExtended | undefined {
3738
const configured = options.reasoningEffort
@@ -45,13 +46,12 @@ function getReasoningEffort(options: ApiHandlerOptions, info: ModelInfo): Reason
4546

4647
const candidates = [reasoningDisabled ? undefined : configured, info.reasoningEffort]
4748
if (Array.isArray(supported) && !supported.includes("disable")) {
48-
candidates.push(supported.find((effort) => effort !== "none" && effort !== "minimal"))
49+
candidates.push(NANO_GPT_REASONING_EFFORTS.find((effort) => supported.includes(effort)))
4950
}
5051

5152
for (const effort of candidates) {
5253
if (
5354
effort &&
54-
effort !== "disable" &&
5555
effort !== "none" &&
5656
effort !== "minimal" &&
5757
(supported === true || (Array.isArray(supported) && supported.includes(effort)))

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,14 @@ export const ThinkingBudget = ({ apiConfiguration, setApiConfigurationField, mod
114114
? rawReasoningEffort
115115
: fallbackReasoningEffort
116116

117-
// Set default reasoning effort when model supports it and no value is set
117+
// Keep normalized defaults pending so Save persists them to the provider profile.
118118
useEffect(() => {
119119
if (
120120
isReasoningEffortSupported &&
121121
storedReasoningEffort !== currentReasoningEffort &&
122122
currentReasoningEffort !== "disable"
123123
) {
124-
setApiConfigurationField("reasoningEffort", currentReasoningEffort as ReasoningEffortExtended, false)
124+
setApiConfigurationField("reasoningEffort", currentReasoningEffort as ReasoningEffortExtended)
125125
}
126126
}, [isReasoningEffortSupported, storedReasoningEffort, currentReasoningEffort, setApiConfigurationField])
127127

‎webview-ui/src/components/settings/__tests__/SettingsView.change-detection.spec.tsx‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ const mockApiOptions = ({ apiConfiguration, setApiConfigurationField }: any) =>
277277
{provider}
278278
</button>
279279
))}
280+
<button data-testid="set-reasoning-default" onClick={() => setApiConfigurationField("reasoningEffort", "high")}>
281+
Set reasoning default
282+
</button>
280283
</div>
281284
)
282285

@@ -491,6 +494,23 @@ describe("SettingsView - Change Detection Fix", () => {
491494
expect(onDone).toHaveBeenCalled()
492495
}, 10000)
493496

497+
it("persists a normalized reasoning default through Save", async () => {
498+
;(useExtensionState as any).mockReturnValue(createExtensionState())
499+
500+
renderWithExtensionState(<SettingsView onDone={vi.fn()} />, { queryClient })
501+
await waitFor(() => expect(screen.getByTestId("save-button")).toBeDisabled())
502+
503+
fireEvent.click(screen.getByTestId("set-reasoning-default"))
504+
expect(screen.getByTestId("save-button")).toBeEnabled()
505+
506+
fireEvent.click(screen.getByTestId("save-button"))
507+
expect(mockPostMessage).toHaveBeenCalledWith({
508+
type: "upsertApiConfiguration",
509+
text: "default",
510+
apiConfiguration: expect.objectContaining({ reasoningEffort: "high" }),
511+
})
512+
}, 10000)
513+
494514
// These tests are passing for the basic case but failing due to vi.doMock limitations
495515
// The core fix has been verified - when no actual changes are made, no unsaved changes dialog appears
496516

‎webview-ui/src/components/settings/__tests__/ThinkingBudget.spec.tsx‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ describe("ThinkingBudget", () => {
290290

291291
// The select value should be "low" (first item), not "disable"
292292
expect(screen.getByTestId("select")).toHaveAttribute("data-value", "low")
293-
expect(setApiConfigurationField).toHaveBeenCalledWith("reasoningEffort", "low", false)
293+
expect(setApiConfigurationField).toHaveBeenCalledWith("reasoningEffort", "low")
294294
expect(setApiConfigurationField).toHaveBeenCalledWith("enableReasoningEffort", true, false)
295295
})
296296

@@ -310,7 +310,7 @@ describe("ThinkingBudget", () => {
310310
)
311311

312312
expect(screen.getByTestId("select")).toHaveAttribute("data-value", "high")
313-
expect(setApiConfigurationField).toHaveBeenCalledWith("reasoningEffort", "high", false)
313+
expect(setApiConfigurationField).toHaveBeenCalledWith("reasoningEffort", "high")
314314
expect(setApiConfigurationField).toHaveBeenCalledWith("enableReasoningEffort", true, false)
315315
})
316316

@@ -350,7 +350,7 @@ describe("ThinkingBudget", () => {
350350
)
351351

352352
expect(screen.getByTestId("select")).toHaveAttribute("data-value", "max")
353-
expect(setApiConfigurationField).toHaveBeenCalledWith("reasoningEffort", "max", false)
353+
expect(setApiConfigurationField).toHaveBeenCalledWith("reasoningEffort", "max")
354354
})
355355

356356
it("should use the first supported effort when required reasoning has no advertised default", () => {
@@ -369,7 +369,7 @@ describe("ThinkingBudget", () => {
369369
)
370370

371371
expect(screen.getByTestId("select")).toHaveAttribute("data-value", "low")
372-
expect(setApiConfigurationField).toHaveBeenCalledWith("reasoningEffort", "low", false)
372+
expect(setApiConfigurationField).toHaveBeenCalledWith("reasoningEffort", "low")
373373
expect(setApiConfigurationField).toHaveBeenCalledWith("enableReasoningEffort", true, false)
374374
})
375375

@@ -422,9 +422,9 @@ describe("ThinkingBudget", () => {
422422

423423
expect(screen.getByTestId("select")).toHaveAttribute("data-value", expected)
424424
if (expectedWrite) {
425-
expect(setApiConfigurationField).toHaveBeenCalledWith("reasoningEffort", expectedWrite, false)
425+
expect(setApiConfigurationField).toHaveBeenCalledWith("reasoningEffort", expectedWrite)
426426
} else {
427-
expect(setApiConfigurationField).not.toHaveBeenCalledWith("reasoningEffort", expect.anything(), false)
427+
expect(setApiConfigurationField).not.toHaveBeenCalledWith("reasoningEffort", expect.anything())
428428
expect(setApiConfigurationField).not.toHaveBeenCalledWith(
429429
"enableReasoningEffort",
430430
expect.anything(),

0 commit comments

Comments
 (0)