@@ -65,10 +65,12 @@ interface MockTask {
6565 recordToolError : ReturnType < typeof vi . fn >
6666 toolRepetitionDetector : { check : ReturnType < typeof vi . fn > }
6767 providerRef : {
68- deref : ( ) => {
69- getState : ReturnType < typeof vi . fn >
70- getMcpHub ?: ( ) => { findServerNameBySanitizedName : ( name : string ) => string | undefined }
71- }
68+ deref : ( ) =>
69+ | {
70+ getState : ReturnType < typeof vi . fn >
71+ getMcpHub ?: ( ) => { findServerNameBySanitizedName : ( name : string ) => string | undefined }
72+ }
73+ | undefined
7274 }
7375 say : ReturnType < typeof vi . fn >
7476 ask : ReturnType < typeof vi . fn >
@@ -319,6 +321,35 @@ describe("presentAssistantMessage - tool usage attribution", () => {
319321 } )
320322 } )
321323
324+ describe ( "undefined provider state" , ( ) => {
325+ // Covers the `state ?? {}` fallback branch (line 347 of presentAssistantMessage.ts).
326+ // When providerRef.deref() returns undefined, state is undefined and the
327+ // destructure falls back to {}, so customModes / experiments / disabledTools
328+ // are all undefined. Tool validation must still use the task-local mode.
329+ it ( "falls back to empty state when provider is unavailable" , async ( ) => {
330+ mockTask . providerRef = { deref : ( ) => undefined }
331+ mockTask . assistantMessageContent = [
332+ {
333+ type : "tool_use" ,
334+ id : "call_no_state" ,
335+ name : "read_file" ,
336+ params : { path : "test.ts" } ,
337+ nativeArgs : { path : "test.ts" } ,
338+ partial : false ,
339+ } ,
340+ ]
341+
342+ await presentAssistantMessage ( mockTask as unknown as Task )
343+
344+ // validateToolUse must still be called with the task-local mode.
345+ const calls = vi . mocked ( validateToolUse ) . mock . calls
346+ expect ( calls . length ) . toBeGreaterThan ( 0 )
347+ expect ( calls [ 0 ] [ 1 ] ) . toBe ( "code" )
348+ // customModes falls back to [] (from the ?? {} path).
349+ expect ( calls [ 0 ] [ 2 ] ) . toEqual ( [ ] )
350+ } )
351+ } )
352+
322353 describe ( "mode delegation regression" , ( ) => {
323354 // Regression for issue #1623.
324355 // Before the fix, validateToolUse received the shared provider mode instead
0 commit comments