@@ -691,6 +691,184 @@ describe("ClineProvider - Sticky Mode", () => {
691691 expect ( provider . getValue ( "mode" ) ) . toBe ( "code" )
692692 } )
693693
694+ it ( "keeps the committed mode switch when the post-activation state refresh fails" , async ( ) => {
695+ // Same typed double as the activation-failure rollback test above.
696+ const mockTask = Object . assign (
697+ { } as Task ,
698+ {
699+ taskId : "test-task-id" ,
700+ taskMode : "code" ,
701+ _taskMode : "code" ,
702+ emit : vi . fn ( ) ,
703+ saveClineMessages : vi . fn ( ) ,
704+ clineMessages : [ ] ,
705+ apiConversationHistory : [ ] ,
706+ updateApiConfiguration : vi . fn ( ) ,
707+ } as Partial < Task > ,
708+ )
709+ const historyItem : HistoryItem = {
710+ id : "test-task-id" ,
711+ ts : Date . now ( ) ,
712+ task : "Test task" ,
713+ mode : "code" ,
714+ number : 1 ,
715+ tokensIn : 0 ,
716+ tokensOut : 0 ,
717+ cacheWrites : 0 ,
718+ cacheReads : 0 ,
719+ totalCost : 0 ,
720+ }
721+ vi . spyOn ( provider . taskHistoryStore , "get" ) . mockReturnValue ( historyItem )
722+ const updateTaskHistorySpy = vi . spyOn ( provider , "updateTaskHistory" ) . mockResolvedValue ( [ ] )
723+ await provider . saveViewState ( "mode" , "code" )
724+ await provider . contextProxy . setValue ( "mode" , "code" )
725+
726+ // The mode-linked profile resolves and its activation lands (writing the
727+ // shared profile like the real activation does); only the trailing state
728+ // post rejects. By then the switch has fully committed - ModeChanged was
729+ // emitted and the activation persisted the new profile - so a UI refresh
730+ // failure must not roll the mode back: that would pair the old mode with
731+ // the new profile, the very mismatch the reconcile exists to prevent.
732+ vi . spyOn ( provider . providerSettingsManager , "getModeConfigId" ) . mockResolvedValue ( "architect-profile-id" )
733+ vi . spyOn ( provider . providerSettingsManager , "listConfig" ) . mockResolvedValue ( [
734+ { id : "architect-profile-id" , name : "architect-profile" , apiProvider : providerIdentifiers . openrouter } ,
735+ ] )
736+ vi . spyOn ( provider . providerSettingsManager , "getProfile" ) . mockResolvedValue ( {
737+ name : "architect-profile" ,
738+ id : "architect-profile-id" ,
739+ apiProvider : providerIdentifiers . openrouter ,
740+ } )
741+ provider [ "activateProviderProfileUnlocked" ] = vi . fn ( ) . mockImplementation ( async ( ) => {
742+ await provider . contextProxy . setValue ( "currentApiConfigName" , "architect-profile" )
743+ } )
744+ vi . spyOn ( provider , "postStateToWebview" ) . mockRejectedValue ( new Error ( "post failed" ) )
745+ await provider . addClineToStack ( mockTask )
746+
747+ await provider [ "handleModeSwitchUnlocked" ] ( "architect" , mockTask )
748+
749+ expect ( provider . getValue ( "mode" ) ) . toBe ( "architect" )
750+ expect ( provider [ "viewLocalState" ] . mode ) . toBe ( "architect" )
751+ expect ( mockTask [ "_taskMode" ] ) . toBe ( "architect" )
752+ expect ( provider . contextProxy . getValue ( "currentApiConfigName" ) ) . toBe ( "architect-profile" )
753+ expect ( updateTaskHistorySpy ) . not . toHaveBeenCalledWith ( expect . objectContaining ( { mode : "code" } ) )
754+ } )
755+
756+ it ( "keeps the switched mode when the mode-linked activation fails after writing" , async ( ) => {
757+ // Same typed double as the activation-failure rollback test above.
758+ const mockTask = Object . assign (
759+ { } as Task ,
760+ {
761+ taskId : "test-task-id" ,
762+ taskMode : "code" ,
763+ _taskMode : "code" ,
764+ emit : vi . fn ( ) ,
765+ saveClineMessages : vi . fn ( ) ,
766+ clineMessages : [ ] ,
767+ apiConversationHistory : [ ] ,
768+ updateApiConfiguration : vi . fn ( ) ,
769+ } as Partial < Task > ,
770+ )
771+ const historyItem : HistoryItem = {
772+ id : "test-task-id" ,
773+ ts : Date . now ( ) ,
774+ task : "Test task" ,
775+ mode : "code" ,
776+ number : 1 ,
777+ tokensIn : 0 ,
778+ tokensOut : 0 ,
779+ cacheWrites : 0 ,
780+ cacheReads : 0 ,
781+ totalCost : 0 ,
782+ }
783+ vi . spyOn ( provider . taskHistoryStore , "get" ) . mockReturnValue ( historyItem )
784+ const updateTaskHistorySpy = vi . spyOn ( provider , "updateTaskHistory" ) . mockResolvedValue ( [ ] )
785+ await provider . saveViewState ( "mode" , "code" )
786+ await provider . contextProxy . setValue ( "mode" , "code" )
787+
788+ // The mode-linked activation lands its shared writes and only then rejects
789+ // (e.g. its own state post or setModeConfig step failing). A mode-only rollback
790+ // would pair the old mode with the profile the activation already switched to,
791+ // the mismatch the reconcile exists to prevent: the switch must stand and the
792+ // error must still surface.
793+ vi . spyOn ( provider . providerSettingsManager , "getModeConfigId" ) . mockResolvedValue ( "architect-profile-id" )
794+ vi . spyOn ( provider . providerSettingsManager , "listConfig" ) . mockResolvedValue ( [
795+ { id : "architect-profile-id" , name : "architect-profile" , apiProvider : providerIdentifiers . openrouter } ,
796+ ] )
797+ vi . spyOn ( provider . providerSettingsManager , "getProfile" ) . mockResolvedValue ( {
798+ name : "architect-profile" ,
799+ id : "architect-profile-id" ,
800+ apiProvider : providerIdentifiers . openrouter ,
801+ } )
802+ provider [ "activateProviderProfileUnlocked" ] = vi . fn ( ) . mockImplementation ( async ( ) => {
803+ await provider . contextProxy . setValue ( "currentApiConfigName" , "architect-profile" )
804+ throw new Error ( "activation step failed" )
805+ } )
806+ await provider . addClineToStack ( mockTask )
807+
808+ await expect ( provider [ "handleModeSwitchUnlocked" ] ( "architect" , mockTask ) ) . rejects . toThrow (
809+ "activation step failed" ,
810+ )
811+
812+ expect ( provider . getValue ( "mode" ) ) . toBe ( "architect" )
813+ expect ( provider [ "viewLocalState" ] . mode ) . toBe ( "architect" )
814+ expect ( mockTask [ "_taskMode" ] ) . toBe ( "architect" )
815+ expect ( provider . contextProxy . getValue ( "currentApiConfigName" ) ) . toBe ( "architect-profile" )
816+ expect ( updateTaskHistorySpy ) . not . toHaveBeenCalledWith ( expect . objectContaining ( { mode : "code" } ) )
817+ } )
818+
819+ it ( "keeps the switched mode when the mutation signal aborted before the failure" , async ( ) => {
820+ // Same typed double as the activation-failure rollback test above.
821+ const mockTask = Object . assign (
822+ { } as Task ,
823+ {
824+ taskId : "test-task-id" ,
825+ taskMode : "code" ,
826+ _taskMode : "code" ,
827+ emit : vi . fn ( ) ,
828+ saveClineMessages : vi . fn ( ) ,
829+ clineMessages : [ ] ,
830+ apiConversationHistory : [ ] ,
831+ updateApiConfiguration : vi . fn ( ) ,
832+ } as Partial < Task > ,
833+ )
834+ const historyItem : HistoryItem = {
835+ id : "test-task-id" ,
836+ ts : Date . now ( ) ,
837+ task : "Test task" ,
838+ mode : "code" ,
839+ number : 1 ,
840+ tokensIn : 0 ,
841+ tokensOut : 0 ,
842+ cacheWrites : 0 ,
843+ cacheReads : 0 ,
844+ totalCost : 0 ,
845+ }
846+ vi . spyOn ( provider . taskHistoryStore , "get" ) . mockReturnValue ( historyItem )
847+ const updateTaskHistorySpy = vi . spyOn ( provider , "updateTaskHistory" ) . mockResolvedValue ( [ ] )
848+ await provider . saveViewState ( "mode" , "code" )
849+ await provider . contextProxy . setValue ( "mode" , "code" )
850+
851+ // The pin and shared writes land; then the mutation timeout aborts this
852+ // switch and a later step rejects. The queue has already advanced past the
853+ // timed-out switch, so a newer mutation may own the mode stores: the late
854+ // rollback must not replay the pre-switch values over them.
855+ const controller = new AbortController ( )
856+ vi . spyOn ( provider . providerSettingsManager , "getModeConfigId" ) . mockImplementation ( async ( ) => {
857+ controller . abort ( )
858+ throw new Error ( "late activation read failure" )
859+ } )
860+ await provider . addClineToStack ( mockTask )
861+
862+ await expect (
863+ provider [ "handleModeSwitchUnlocked" ] ( "architect" , mockTask , controller . signal ) ,
864+ ) . rejects . toThrow ( "late activation read failure" )
865+
866+ expect ( provider . getValue ( "mode" ) ) . toBe ( "architect" )
867+ expect ( provider [ "viewLocalState" ] . mode ) . toBe ( "architect" )
868+ expect ( mockTask [ "_taskMode" ] ) . toBe ( "architect" )
869+ expect ( updateTaskHistorySpy ) . not . toHaveBeenCalledWith ( expect . objectContaining ( { mode : "code" } ) )
870+ } )
871+
694872 it ( "keeps the cancellation result when the abort rollback write itself fails" , async ( ) => {
695873 const mockTask = Object . assign (
696874 { } as Task ,
0 commit comments