@@ -343,7 +343,11 @@ describe("presentAssistantMessage - Custom Tool Recording", () => {
343343 } )
344344 } )
345345
346- it ( "never marks a protocol tool (attempt_completion) as blocked" , async ( ) => {
346+ it ( "marks a disabled attempt_completion as blocked and answers it with an error tool_result" , async ( ) => {
347+ // An explicit disabledTools entry outranks the always-available class,
348+ // so a disabled attempt_completion reaches the validator like any
349+ // other tool; its rejection must surface as the standard validation-
350+ // error tool_result instead of completing the task.
347351 mockTask . assistantMessageContent = [
348352 {
349353 type : "tool_use" ,
@@ -368,22 +372,103 @@ describe("presentAssistantMessage - Custom Tool Recording", () => {
368372 } ) ,
369373 }
370374
375+ // Mirror the real validator's rejection for a requirement that maps
376+ // to false (validateToolUse.spec pins the predicate itself).
377+ vi . mocked ( validateToolUse ) . mockImplementationOnce ( ( ) => {
378+ throw new Error ( 'Tool "attempt_completion" is not allowed in code mode.' )
379+ } )
380+
381+ await presentAssistantMessage ( mockTask )
382+
383+ const validateToolUseMock = vi . mocked ( validateToolUse )
384+ expect ( validateToolUseMock ) . toHaveBeenCalled ( )
385+ const toolRequirements = validateToolUseMock . mock . calls [ 0 ] [ 3 ]
386+ expect ( toolRequirements ) . toMatchObject ( { attempt_completion : false } )
387+
388+ const errorToolResults = mockTask . userMessageContent . filter ( ( block : unknown ) => {
389+ const b = block as { type ?: string ; is_error ?: boolean }
390+ return b . type === "tool_result" && b . is_error
391+ } )
392+ expect ( errorToolResults ) . toHaveLength ( 1 )
393+ expect ( mockTask . consecutiveMistakeCount ) . toBe ( 1 )
394+
395+ // The completion handler must not run for the rejected call.
396+ const { attemptCompletionTool } = await import ( "../../tools/AttemptCompletionTool" )
397+ expect ( attemptCompletionTool . handle ) . not . toHaveBeenCalled ( )
398+ } )
399+
400+ it ( "treats a model-excluded attempt_completion as blocked and answers it with an error tool_result" , async ( ) => {
401+ // A model excludedTools entry suppresses the protocol tool in the
402+ // effective policy, so the execution gate must see the same
403+ // restriction with disabledTools unset.
404+ mockTask . assistantMessageContent = [
405+ {
406+ type : "tool_use" ,
407+ id : "tool_call_protocol_excluded_123" ,
408+ name : "attempt_completion" ,
409+ params : { } ,
410+ nativeArgs : { } ,
411+ partial : false ,
412+ } ,
413+ ]
414+
415+ mockTask . api . getModel = ( ) => ( { id : "test-model" , info : { excludedTools : [ "attempt_completion" ] } } )
416+
417+ mockTask . providerRef = {
418+ deref : ( ) => ( {
419+ getState : vi . fn ( ) . mockResolvedValue ( {
420+ mode : "code" ,
421+ customModes : [ ] ,
422+ experiments : {
423+ customTools : false ,
424+ } ,
425+ } ) ,
426+ } ) ,
427+ }
428+
429+ // Mirror the real validator's rejection for a requirement that maps
430+ // to false (validateToolUse.spec pins the predicate itself).
431+ vi . mocked ( validateToolUse ) . mockImplementationOnce ( ( ) => {
432+ throw new Error ( 'Tool "attempt_completion" is not allowed in code mode.' )
433+ } )
434+
371435 await presentAssistantMessage ( mockTask )
372436
373437 const validateToolUseMock = vi . mocked ( validateToolUse )
374438 expect ( validateToolUseMock ) . toHaveBeenCalled ( )
375439 const toolRequirements = validateToolUseMock . mock . calls [ 0 ] [ 3 ]
376- // Protocol tools never enter toolRequirements, so the validator cannot
377- // block them even when disabledTools lists them.
378- expect ( toolRequirements ) . not . toHaveProperty ( "attempt_completion" )
440+ expect ( toolRequirements ) . toMatchObject ( { attempt_completion : false } )
379441
380- // With validateToolUse mocked to return normally, the block proceeds
381- // past validation: no validation-error tool_result is pushed.
382442 const errorToolResults = mockTask . userMessageContent . filter ( ( block : unknown ) => {
383443 const b = block as { type ?: string ; is_error ?: boolean }
384444 return b . type === "tool_result" && b . is_error
385445 } )
386- expect ( errorToolResults ) . toEqual ( [ ] )
446+ expect ( errorToolResults ) . toHaveLength ( 1 )
447+ expect ( mockTask . consecutiveMistakeCount ) . toBe ( 1 )
448+
449+ // The completion handler must not run for the rejected call.
450+ const { attemptCompletionTool } = await import ( "../../tools/AttemptCompletionTool" )
451+ expect ( attemptCompletionTool . handle ) . not . toHaveBeenCalled ( )
452+
453+ // Absent model metadata must not derail the requirements build: the
454+ // protocol-tool leg simply sees no exclusions, and the call validates
455+ // normally instead of erroring out.
456+ mockTask . api . getModel = ( ) => undefined
457+ mockTask . currentStreamingContentIndex = 0
458+ mockTask . userMessageContent = [ ]
459+ mockTask . consecutiveMistakeCount = 0
460+ mockTask . didAlreadyUseTool = false
461+ mockTask . didCompleteReadingStream = false
462+
463+ await presentAssistantMessage ( mockTask )
464+
465+ expect ( validateToolUseMock ) . toHaveBeenCalledTimes ( 2 )
466+ expect ( validateToolUseMock . mock . calls [ 1 ] [ 3 ] ) . toEqual ( { } )
467+ expect ( mockTask . consecutiveMistakeCount ) . toBe ( 0 )
468+ const phase2Errors = mockTask . userMessageContent . filter ( ( block : { type ?: string ; is_error ?: boolean } ) => {
469+ return block . type === "tool_result" && block . is_error
470+ } )
471+ expect ( phase2Errors ) . toHaveLength ( 0 )
387472 } )
388473
389474 it ( "still marks ordinary tools (ask_followup_question) as blocked" , async ( ) => {
@@ -416,8 +501,6 @@ describe("presentAssistantMessage - Custom Tool Recording", () => {
416501 const validateToolUseMock = vi . mocked ( validateToolUse )
417502 expect ( validateToolUseMock ) . toHaveBeenCalled ( )
418503 const toolRequirements = validateToolUseMock . mock . calls [ 0 ] [ 3 ]
419- // Control/ordinary tools remain blockable — the inverse of the
420- // protocol-tool guarantee.
421504 expect ( toolRequirements ) . toMatchObject ( {
422505 ask_followup_question : false ,
423506 } )
0 commit comments