11// npx vitest run src/core/task/__tests__/Task.runtime-thinking-effort.test.ts
22//
3- // DTE series 2/5 — task -local thinking effort state on Task:
3+ // Task -local thinking effort state on Task:
44// setRuntimeThinkingEffort / getRuntimeThinkingEffort, the in-memory
55// effective-effort handler rebuild (the profile-derived apiConfiguration
66// projection never carries the override).
@@ -11,6 +11,7 @@ import { providerIdentifiers } from "@roo-code/types/provider-identifiers"
1111import { Task } from "../Task"
1212import { ClineProvider } from "../../webview/ClineProvider"
1313import { buildApiHandler } from "../../../api"
14+ import { DeepSeekHandler } from "../../../api/providers/deepseek"
1415import { disposeApiHandler } from "../../../api/dispose-api-handler"
1516import { summarizeConversation } from "../../condense"
1617import { ApiStreamChunk } from "../../../api/transform/stream"
@@ -120,7 +121,7 @@ function getPrivateAccess(task: Task): RuntimeThinkingEffortAccess {
120121
121122const SETTINGS_EFFORT : ReasoningEffortExtended = "low"
122123
123- describe ( "Task runtime thinking effort (DTE series 2/5) " , ( ) => {
124+ describe ( "Task runtime thinking effort override " , ( ) => {
124125 let mockProvider : Record < string , unknown >
125126 let mockApiConfiguration : ProviderSettings
126127 let task : Task
@@ -194,6 +195,10 @@ describe("Task runtime thinking effort (DTE series 2/5)", () => {
194195
195196 describe ( "setRuntimeThinkingEffort" , ( ) => {
196197 it ( "stores effort + source and rebuilds the handler with the override applied" , ( ) => {
198+ // The handler this task owned before the swap must be disposed by the swap:
199+ // handlers that hold host listeners (VS Code LM, GitHub Copilot) leak otherwise.
200+ const handlerBefore = task . api
201+ vi . mocked ( disposeApiHandler ) . mockClear ( )
197202 task . setRuntimeThinkingEffort ( "xhigh" , "test-source" )
198203
199204 expect ( task . getRuntimeThinkingEffort ( ) ) . toEqual ( { effort : "xhigh" , source : "test-source" } )
@@ -207,10 +212,47 @@ describe("Task runtime thinking effort (DTE series 2/5)", () => {
207212 } ) ,
208213 )
209214 // The handler is rebuilt with the override applied (last build call)...
215+ // Exact configuration, not a shape subset: dropping any other profile field
216+ // from the rebuild would fail here.
210217 const lastCall = vi . mocked ( buildApiHandler ) . mock . calls . at ( - 1 )
211- expect ( lastCall ?. [ 0 ] ) . toEqual ( expect . objectContaining ( { reasoningEffort : "xhigh" } ) )
218+ expect ( lastCall ?. [ 0 ] ) . toEqual ( { ... mockApiConfiguration , reasoningEffort : "xhigh" } )
212219 // ...from a fresh copy, not the settings object itself.
213220 expect ( lastCall ?. [ 0 ] ) . not . toBe ( mockApiConfiguration )
221+ const disposedHandlers = vi . mocked ( disposeApiHandler ) . mock . calls . map ( ( [ api ] ) => api )
222+ expect ( disposedHandlers . filter ( ( api ) => api === handlerBefore ) ) . toHaveLength ( 1 )
223+ } )
224+
225+ it ( "hands buildApiHandler a configuration a real provider handler honours" , async ( ) => {
226+ // The mocked builder proves the override reaches buildApiHandler; this feeds
227+ // the configuration it handed over to the REAL builder and checks that an
228+ // actual provider resolves it. deepseek's model-params step reads
229+ // `options.reasoningEffort` and outranks the model's own default ("high"),
230+ // while the profile asks for SETTINGS_EFFORT, so all three values differ.
231+ const { buildApiHandler : buildRealHandler } =
232+ await vi . importActual < typeof import ( "../../../api" ) > ( "../../../api" )
233+ // The real builder routes this configuration to deepseek's handler, whose
234+ // model-params step is what reads the effort.
235+ const deepSeekEffortOf = ( configuration : ProviderSettings ) : ReasoningEffortExtended | undefined => {
236+ const handler = buildRealHandler ( {
237+ ...configuration ,
238+ apiProvider : providerIdentifiers . deepseek ,
239+ apiModelId : "deepseek-v4-flash" ,
240+ } )
241+ if ( ! ( handler instanceof DeepSeekHandler ) ) {
242+ throw new Error ( "the real builder must route this configuration to deepseek's handler" )
243+ }
244+ return handler . getModel ( ) . reasoningEffort
245+ }
246+
247+ expect ( deepSeekEffortOf ( mockApiConfiguration ) ) . toBe ( SETTINGS_EFFORT )
248+
249+ task . setRuntimeThinkingEffort ( "max" , "real-builder" )
250+ const handedConfiguration = vi . mocked ( buildApiHandler ) . mock . calls . at ( - 1 ) ?. [ 0 ] as ProviderSettings
251+ expect ( deepSeekEffortOf ( handedConfiguration ) ) . toBe ( "max" )
252+
253+ task . setRuntimeThinkingEffort ( undefined )
254+ const clearedConfiguration = vi . mocked ( buildApiHandler ) . mock . calls . at ( - 1 ) ?. [ 0 ] as ProviderSettings
255+ expect ( deepSeekEffortOf ( clearedConfiguration ) ) . toBe ( SETTINGS_EFFORT )
214256 } )
215257
216258 it ( "replaces effort + source when re-set while active and restores on clear" , ( ) => {
@@ -221,17 +263,22 @@ describe("Task runtime thinking effort (DTE series 2/5)", () => {
221263 const lastSet = vi . mocked ( buildApiHandler ) . mock . calls . at ( - 1 )
222264 expect ( lastSet ?. [ 0 ] ) . toEqual ( expect . objectContaining ( { reasoningEffort : "medium" } ) )
223265
224- // Clearing rebuilds from the profile projection, not the intermediate effort.
266+ // Clearing rebuilds from the profile projection itself — the same object, not
267+ // a copy carrying the intermediate effort.
225268 task . setRuntimeThinkingEffort ( undefined )
226269 const lastClear = vi . mocked ( buildApiHandler ) . mock . calls . at ( - 1 )
227- expect ( lastClear ?. [ 0 ] ) . toEqual ( expect . objectContaining ( { reasoningEffort : SETTINGS_EFFORT } ) )
270+ expect ( lastClear ?. [ 0 ] ) . toBe ( task . apiConfiguration )
271+ expect ( task . apiConfiguration . reasoningEffort ) . toBe ( SETTINGS_EFFORT )
228272 } )
229273
230274 it ( "restores the settings-derived effort when cleared with undefined" , ( ) => {
231275 task . setRuntimeThinkingEffort ( "max" )
232276 const lastSet = vi . mocked ( buildApiHandler ) . mock . calls . at ( - 1 )
233277 expect ( lastSet ?. [ 0 ] ) . toEqual ( expect . objectContaining ( { reasoningEffort : "max" } ) )
234278
279+ // The handler that served the override must be disposed by the clearing swap.
280+ const handlerUnderOverride = task . api
281+ vi . mocked ( disposeApiHandler ) . mockClear ( )
235282 // A label passed on the clearing call must not survive into source.
236283 task . setRuntimeThinkingEffort ( undefined , "stale-source" )
237284
@@ -246,9 +293,12 @@ describe("Task runtime thinking effort (DTE series 2/5)", () => {
246293 reasoningEffort : SETTINGS_EFFORT ,
247294 } ) ,
248295 )
249- // The handler is rebuilt from the projection.
296+ // The handler is rebuilt from the projection object itself, and the handler
297+ // that served the override is gone.
250298 const lastCall = vi . mocked ( buildApiHandler ) . mock . calls . at ( - 1 )
251- expect ( lastCall ?. [ 0 ] ) . toEqual ( expect . objectContaining ( { reasoningEffort : SETTINGS_EFFORT } ) )
299+ expect ( lastCall ?. [ 0 ] ) . toBe ( task . apiConfiguration )
300+ const disposedHandlers = vi . mocked ( disposeApiHandler ) . mock . calls . map ( ( [ api ] ) => api )
301+ expect ( disposedHandlers . filter ( ( api ) => api === handlerUnderOverride ) ) . toHaveLength ( 1 )
252302 } )
253303
254304 it ( "never lets the override enter apiConfiguration (webview projection + profile save contract)" , ( ) => {
0 commit comments