Skip to content

Commit 19954d3

Browse files
committed
fix(task): harden set_thinking_effort per review feedback
Address PR review feedback on set_thinking_effort (DTE series 3/5): - Executor: seed the per-task guard history with the task's effective baseline so returning from a changed value to the original baseline is refused as oscillation (A -> B -> A); existing no-op behavior preserved. - Parser: only build nativeArgs when effort AND reason are strings; a non-string payload now fails at parse time and cannot reach the executor. - Gating: a supportsReasoningEffort array that only lists 'disable' no longer exposes the tool (it could apply no level). - i18n: translate the new thinkingEffort chat strings into all 17 non-English webview locales (placeholders preserved). - Tests: regression tests for each change plus branch-coverage for the previously partial lines (non-string args, 'disable'-only capability, baseline oscillation, partial streaming without params, description fallback, capability robustness). All touched patch lines are now fully branch-covered (codecov patch partials resolved). CodeRabbit: #1354
1 parent 0ab4a60 commit 19954d3

24 files changed

Lines changed: 266 additions & 75 deletions

‎src/core/assistant-message/NativeToolCallParser.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,9 @@ export class NativeToolCallParser {
862862
break
863863

864864
case "set_thinking_effort":
865-
if (args.effort !== undefined && args.reason !== undefined) {
865+
// Both values must be strings: a non-string payload is an
866+
// invalid tool call and must not reach the executor.
867+
if (typeof args.effort === "string" && typeof args.reason === "string") {
866868
nativeArgs = {
867869
effort: args.effort,
868870
reason: args.reason,

‎src/core/assistant-message/__tests__/NativeToolCallParser.setThinkingEffort.spec.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,28 @@ describe("NativeToolCallParser — set_thinking_effort", () => {
4848
const result = NativeToolCallParser.parseToolCall(toolCall)
4949
expect(result).toBeNull()
5050
})
51+
52+
it("rejects a non-string reason (no nativeArgs, so the executor is not reached)", () => {
53+
const toolCall = {
54+
id: "toolu_dte_3",
55+
name: "set_thinking_effort" as const,
56+
arguments: JSON.stringify({ effort: "high", reason: {} }),
57+
}
58+
59+
const result = NativeToolCallParser.parseToolCall(toolCall)
60+
expect(result).toBeNull()
61+
})
62+
63+
it("rejects a non-string effort (no nativeArgs, so the executor is not reached)", () => {
64+
const toolCall = {
65+
id: "toolu_dte_4",
66+
name: "set_thinking_effort" as const,
67+
arguments: JSON.stringify({ effort: 123, reason: "escalating" }),
68+
}
69+
70+
const result = NativeToolCallParser.parseToolCall(toolCall)
71+
expect(result).toBeNull()
72+
})
5173
})
5274

5375
describe("processStreamingChunk (partial)", () => {
@@ -66,6 +88,28 @@ describe("NativeToolCallParser — set_thinking_effort", () => {
6688
expect(nativeArgs?.effort).toBe("high")
6789
expect(nativeArgs?.reason).toBe("escalating")
6890
})
91+
92+
it("emits a partial ToolUse carrying only the streamed reason", () => {
93+
const id = "toolu_dte_stream_2"
94+
NativeToolCallParser.startStreamingToolCall(id, "set_thinking_effort")
95+
96+
const result = NativeToolCallParser.processStreamingChunk(id, JSON.stringify({ reason: "escalating" }))
97+
98+
expect(result).not.toBeNull()
99+
const nativeArgs = result?.nativeArgs as { effort?: string; reason?: string } | undefined
100+
expect(nativeArgs?.effort).toBeUndefined()
101+
expect(nativeArgs?.reason).toBe("escalating")
102+
})
103+
104+
it("emits a partial ToolUse without nativeArgs when neither param has streamed yet", () => {
105+
const id = "toolu_dte_stream_3"
106+
NativeToolCallParser.startStreamingToolCall(id, "set_thinking_effort")
107+
108+
const result = NativeToolCallParser.processStreamingChunk(id, JSON.stringify({ other: "value" }))
109+
110+
expect(result).not.toBeNull()
111+
expect((result as { nativeArgs?: unknown }).nativeArgs).toBeUndefined()
112+
})
69113
})
70114

71115
describe("finalizeStreamingToolCall", () => {

‎src/core/assistant-message/__tests__/presentAssistantMessage-setThinkingEffort.spec.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ describe("presentAssistantMessage - set_thinking_effort dispatch", () => {
185185
]
186186

187187
await presentAssistantMessage(asTask())
188+
189+
const handle = vi.mocked(setThinkingEffortTool.handle)
190+
expect(handle).not.toHaveBeenCalled()
188191
})
189192

190193
it("describes a skipped set_thinking_effort block via the tool description when the task already rejected a tool", async () => {
@@ -201,4 +204,26 @@ describe("presentAssistantMessage - set_thinking_effort dispatch", () => {
201204
expect(content).toContain("set_thinking_effort to 'high'")
202205
expect(content).toContain("rejecting")
203206
})
207+
208+
it("describes a set_thinking_effort block without an effort param via the tool description fallback", async () => {
209+
mockTask.didRejectTool = true
210+
mockTask.assistantMessageContent = [
211+
{
212+
type: "tool_use" as const,
213+
id: toolCallId(),
214+
name: "set_thinking_effort",
215+
params: {},
216+
partial: false,
217+
},
218+
]
219+
220+
await presentAssistantMessage(asTask())
221+
222+
const handle = vi.mocked(setThinkingEffortTool.handle)
223+
expect(handle).not.toHaveBeenCalled()
224+
const result = dispatchedToolResult()
225+
expect(result).toBeDefined()
226+
const content = (result as { content?: string }).content
227+
expect(content).toContain("set_thinking_effort to ''")
228+
})
204229
})

‎src/core/prompts/tools/__tests__/filter-thinking-effort.spec.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ describe("isSetThinkingEffortEnabled", () => {
5050
expect(isSetThinkingEffortEnabled({ dynamicThinkingEffort: true }, modelInfo(["low", "high"]))).toBe(true)
5151
expect(isSetThinkingEffortEnabled({ dynamicThinkingEffort: true }, modelInfo(true))).toBe(true)
5252
})
53+
54+
it("is false for a capability array that only lists 'disable' (no settable level)", () => {
55+
expect(isSetThinkingEffortEnabled({ dynamicThinkingEffort: true }, modelInfo(["disable"]))).toBe(false)
56+
})
5357
})
5458

5559
describe("filterNativeToolsForMode set_thinking_effort gate", () => {
@@ -75,6 +79,14 @@ describe("filterNativeToolsForMode set_thinking_effort gate", () => {
7579
expect(toolNames(result)).not.toContain("set_thinking_effort")
7680
})
7781

82+
it("removes the tool for a capability array that only lists 'disable'", () => {
83+
const result = filterNativeToolsForMode(TOOLS, "code", undefined, { dynamicThinkingEffort: true }, undefined, {
84+
modelInfo: modelInfo(["disable"]),
85+
})
86+
expect(toolNames(result)).not.toContain("set_thinking_effort")
87+
expect(toolNames(result)).toContain("execute_command")
88+
})
89+
7890
it("keeps the tool list stable across repeated calls (prompt-cache safety)", () => {
7991
const experiments = { dynamicThinkingEffort: true }
8092
const settings = { modelInfo: modelInfo(["low", "high"]) }
@@ -131,7 +143,9 @@ describe("isToolAllowedInMode — set_thinking_effort gate (prompt-side)", () =>
131143
modelInfo: modelInfo(false),
132144
}),
133145
).toBe(false)
134-
// Other always-available tools remain unconditional.
146+
// Other always-available tools remain unconditional; in particular the
147+
// DTE branch is skipped for them (non-set_thinking_effort path).
135148
expect(isToolAllowedInMode("execute_command", "code", undefined, undefined, undefined, undefined)).toBe(true)
149+
expect(isToolAllowedInMode("switch_mode", "code", undefined, undefined, undefined, undefined)).toBe(true)
136150
})
137151
})

‎src/core/prompts/tools/filter-tools-for-mode.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,10 @@ function hasAnyMcpResources(mcpHub: McpHub, allowedServers?: string[]): boolean
367367
* DTE series 3/5: whether the set_thinking_effort tool should be exposed.
368368
*
369369
* Requires both the dynamicThinkingEffort experiment to be enabled and the
370-
* model to advertise per-request reasoning effort support (a non-empty
371-
* `supportsReasoningEffort` capability array, or boolean/adaptive-class
372-
* support). Evaluated at task start only (prompt-cache safety).
370+
* model to advertise per-request reasoning effort support (a
371+
* `supportsReasoningEffort` capability array with at least one settable
372+
* non-`disable` level, or boolean/adaptive-class support). Evaluated at
373+
* task start only (prompt-cache safety).
373374
*
374375
* @param experiments - Experiment flags from the current state
375376
* @param modelInfo - Current model info (from apiConfiguration)
@@ -384,7 +385,9 @@ export function isSetThinkingEffortEnabled(
384385
}
385386
const capability = modelInfo?.supportsReasoningEffort
386387
if (Array.isArray(capability)) {
387-
return capability.length > 0
388+
// A "disable"-only array exposes a tool that cannot apply any level
389+
// (the executor's clamp would land on "disable" and refuse every call).
390+
return capability.some((effort) => effort !== "disable")
388391
}
389392
return capability === true
390393
}

‎src/core/tools/SetThinkingEffortTool.ts‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import { BaseTool, ToolCallbacks } from "./BaseTool"
1414
* capability, instantly undoable); guardrails replace approval:
1515
* - always a one-line chat notification (success or refusal)
1616
* - escalation cap: max 3 upward changes per task
17-
* - oscillation detection: A -> B -> A ping-pong within a task is refused
17+
* - oscillation detection: A -> B -> A ping-pong within a task (including a
18+
* return to the task baseline) is refused
1819
* - hard clamp to the model capability array
1920
*
2021
* The tool is only exposed when the dynamicThinkingEffort experiment is on
@@ -54,7 +55,10 @@ export const MAX_UPWARD_CHANGES = 3
5455
/** Per-task guardrail state (scoped per Task; see guardState WeakMap). */
5556
interface EffortGuardState {
5657
upwardChanges: number
57-
/** Model-driven applied efforts, most recent last. */
58+
/**
59+
* Applied efforts, most recent last; seeded with the task's effective
60+
* baseline (when defined) so returning to it counts as oscillation.
61+
*/
5862
history: string[]
5963
}
6064

@@ -102,10 +106,16 @@ export class SetThinkingEffortTool extends BaseTool<"set_thinking_effort"> {
102106
*/
103107
private guardState = new WeakMap<Task, EffortGuardState>()
104108

105-
private getGuardState(task: Task): EffortGuardState {
109+
private getGuardState(task: Task, baseline: string | undefined): EffortGuardState {
106110
let state = this.guardState.get(task)
107111
if (!state) {
108-
state = { upwardChanges: 0, history: [] }
112+
state = {
113+
upwardChanges: 0,
114+
// Seed the history with the task's effective baseline so that
115+
// returning from a changed value to the original baseline is
116+
// detected as oscillation (A -> B -> A) instead of re-applied.
117+
history: baseline === undefined ? [] : [baseline],
118+
}
109119
this.guardState.set(task, state)
110120
}
111121
return state
@@ -175,9 +185,11 @@ export class SetThinkingEffortTool extends BaseTool<"set_thinking_effort"> {
175185
task.consecutiveMistakeCount++
176186
task.recordToolError("set_thinking_effort")
177187
task.didToolFailInCurrentTurn = true
178-
const supported = Array.isArray(capability)
179-
? capability.filter((l) => l !== "disable").join(", ")
180-
: "none"
188+
// Invariant: clampToCapability only returns "disable" when the
189+
// capability is a non-empty array containing "disable", so the
190+
// capability is a (non-empty) array here — single documented cast,
191+
// no double assertion.
192+
const supported = (capability as string[]).filter((l) => l !== "disable").join(", ")
181193
pushToolResult(
182194
formatResponse.toolError(
183195
"'" + effort + "' is not supported by the current model. Supported levels: " + supported + ".",
@@ -186,8 +198,8 @@ export class SetThinkingEffortTool extends BaseTool<"set_thinking_effort"> {
186198
return
187199
}
188200

189-
const guard = this.getGuardState(task)
190201
const current = task.getRuntimeThinkingEffort().effort ?? task.apiConfiguration.reasoningEffort
202+
const guard = this.getGuardState(task, current)
191203

192204
// No-op: already at the requested level — confirm without churn.
193205
if (clamped === current) {

0 commit comments

Comments
 (0)