@@ -848,7 +848,10 @@ describe("ClineProvider - Sticky Mode", () => {
848848 id : "architect-profile-id" ,
849849 apiProvider : providerIdentifiers . openrouter ,
850850 } )
851- provider [ "activateProviderProfileUnlocked" ] = vi . fn ( ) . mockImplementation ( async ( ) => {
851+ provider [ "activateProviderProfileUnlocked" ] = vi . fn ( ) . mockImplementation ( async ( _args , options ) => {
852+ // The real activation flips the committed-switch guard when it writes
853+ // the activated profile; mirror that here.
854+ options ?. onSharedWriteStart ?.( )
852855 await provider . contextProxy . setValue ( "currentApiConfigName" , "architect-profile" )
853856 throw new Error ( "activation step failed" )
854857 } )
@@ -1073,6 +1076,182 @@ describe("ClineProvider - Sticky Mode", () => {
10731076 expect ( setModeConfig ) . toHaveBeenCalledWith ( "architect" , "x-id" )
10741077 } )
10751078
1079+ it ( "reconciles the switch when the mode-linked profile lookup fails" , async ( ) => {
1080+ // Same typed double as the activation-failure rollback test above.
1081+ const mockTask = Object . assign (
1082+ { } as Task ,
1083+ {
1084+ taskId : "test-task-id" ,
1085+ taskMode : "code" ,
1086+ _taskMode : "code" ,
1087+ emit : vi . fn ( ) ,
1088+ saveClineMessages : vi . fn ( ) ,
1089+ clineMessages : [ ] ,
1090+ apiConversationHistory : [ ] ,
1091+ updateApiConfiguration : vi . fn ( ) ,
1092+ } as Partial < Task > ,
1093+ )
1094+ const historyItem : HistoryItem = {
1095+ id : "test-task-id" ,
1096+ ts : Date . now ( ) ,
1097+ task : "Test task" ,
1098+ mode : "code" ,
1099+ number : 1 ,
1100+ tokensIn : 0 ,
1101+ tokensOut : 0 ,
1102+ cacheWrites : 0 ,
1103+ cacheReads : 0 ,
1104+ totalCost : 0 ,
1105+ }
1106+ vi . spyOn ( provider . taskHistoryStore , "get" ) . mockReturnValue ( historyItem )
1107+ const updateTaskHistorySpy = vi . spyOn ( provider , "updateTaskHistory" ) . mockResolvedValue ( [ ] )
1108+ await provider . saveViewState ( "mode" , "code" )
1109+ await provider . contextProxy . setValue ( "mode" , "code" )
1110+
1111+ // The mode's saved profile was deleted: the lookup inside the activation
1112+ // throws before any shared write lands. The committed-switch guard must still
1113+ // be off, so the reconcile restores every mode write.
1114+ vi . spyOn ( provider . providerSettingsManager , "getModeConfigId" ) . mockResolvedValue ( "architect-profile-id" )
1115+ vi . spyOn ( provider . providerSettingsManager , "listConfig" ) . mockResolvedValue ( [
1116+ { id : "architect-profile-id" , name : "architect-profile" , apiProvider : providerIdentifiers . openrouter } ,
1117+ { id : "architect-profile-id" , name : "architect-profile" , apiProvider : providerIdentifiers . openrouter } ,
1118+ ] )
1119+ vi . spyOn ( provider . providerSettingsManager , "getProfile" ) . mockResolvedValue ( {
1120+ name : "architect-profile" ,
1121+ id : "architect-profile-id" ,
1122+ apiProvider : providerIdentifiers . openrouter ,
1123+ } )
1124+ // The activation's own profile lookup throws before any shared write lands
1125+ // (e.g. the profile was deleted): the committed-switch guard must still be
1126+ // off, so the reconcile restores every mode write.
1127+ provider [ "activateProviderProfileUnlocked" ] = vi . fn ( ) . mockRejectedValue ( new Error ( "profile not found" ) )
1128+ await provider . addClineToStack ( mockTask )
1129+
1130+ await expect ( provider [ "handleModeSwitchUnlocked" ] ( "architect" , mockTask ) ) . rejects . toThrow (
1131+ "profile not found" ,
1132+ )
1133+
1134+ expect ( provider . getValue ( "mode" ) ) . toBe ( "code" )
1135+ expect ( provider [ "viewLocalState" ] . mode ) . toBe ( "code" )
1136+ expect ( mockTask [ "_taskMode" ] ) . toBe ( "code" )
1137+ expect ( updateTaskHistorySpy ) . toHaveBeenCalledWith ( expect . objectContaining ( { mode : "code" } ) )
1138+ } )
1139+
1140+ it ( "reports ModeChanged for a switch the catch keeps after activation wrote" , async ( ) => {
1141+ // Same typed double as the activation-failure rollback test above.
1142+ const mockTask = Object . assign (
1143+ { } as Task ,
1144+ {
1145+ taskId : "test-task-id" ,
1146+ taskMode : "code" ,
1147+ _taskMode : "code" ,
1148+ emit : vi . fn ( ) ,
1149+ saveClineMessages : vi . fn ( ) ,
1150+ clineMessages : [ ] ,
1151+ apiConversationHistory : [ ] ,
1152+ updateApiConfiguration : vi . fn ( ) ,
1153+ } as Partial < Task > ,
1154+ )
1155+ const historyItem : HistoryItem = {
1156+ id : "test-task-id" ,
1157+ ts : Date . now ( ) ,
1158+ task : "Test task" ,
1159+ mode : "code" ,
1160+ number : 1 ,
1161+ tokensIn : 0 ,
1162+ tokensOut : 0 ,
1163+ cacheWrites : 0 ,
1164+ cacheReads : 0 ,
1165+ totalCost : 0 ,
1166+ }
1167+ vi . spyOn ( provider . taskHistoryStore , "get" ) . mockReturnValue ( historyItem )
1168+ const updateTaskHistorySpy = vi . spyOn ( provider , "updateTaskHistory" ) . mockResolvedValue ( [ ] )
1169+ await provider . saveViewState ( "mode" , "code" )
1170+ await provider . contextProxy . setValue ( "mode" , "code" )
1171+
1172+ // The activation writes the activated profile and only then rejects: the catch
1173+ // keeps the switch, and listeners plus the webview must still learn the
1174+ // committed mode before the error surfaces.
1175+ vi . spyOn ( provider . providerSettingsManager , "getModeConfigId" ) . mockResolvedValue ( "architect-profile-id" )
1176+ vi . spyOn ( provider . providerSettingsManager , "listConfig" ) . mockResolvedValue ( [
1177+ { id : "architect-profile-id" , name : "architect-profile" , apiProvider : providerIdentifiers . openrouter } ,
1178+ { id : "architect-profile-id" , name : "architect-profile" , apiProvider : providerIdentifiers . openrouter } ,
1179+ ] )
1180+ vi . spyOn ( provider . providerSettingsManager , "getProfile" ) . mockResolvedValue ( {
1181+ name : "architect-profile" ,
1182+ id : "architect-profile-id" ,
1183+ apiProvider : providerIdentifiers . openrouter ,
1184+ } )
1185+ const emitSpy = vi . spyOn ( provider , "emit" )
1186+ const postSpy = vi . spyOn ( provider , "postStateToWebview" ) . mockResolvedValue ( undefined )
1187+ provider [ "activateProviderProfileUnlocked" ] = vi . fn ( ) . mockImplementation ( async ( _args , options ) => {
1188+ options ?. onSharedWriteStart ?.( )
1189+ await provider . contextProxy . setValue ( "currentApiConfigName" , "architect-profile" )
1190+ throw new Error ( "activation step failed" )
1191+ } )
1192+ await provider . addClineToStack ( mockTask )
1193+
1194+ await expect ( provider [ "handleModeSwitchUnlocked" ] ( "architect" , mockTask ) ) . rejects . toThrow (
1195+ "activation step failed" ,
1196+ )
1197+
1198+ expect ( emitSpy ) . toHaveBeenCalledWith ( "modeChanged" , "architect" )
1199+ expect ( postSpy ) . toHaveBeenCalled ( )
1200+ expect ( provider . getValue ( "mode" ) ) . toBe ( "architect" )
1201+ } )
1202+
1203+ it ( "reports the committed mode when the signal aborts after the mode writes" , async ( ) => {
1204+ // Same typed double as the activation-failure rollback test above.
1205+ const mockTask = Object . assign (
1206+ { } as Task ,
1207+ {
1208+ taskId : "test-task-id" ,
1209+ taskMode : "code" ,
1210+ _taskMode : "code" ,
1211+ emit : vi . fn ( ) ,
1212+ saveClineMessages : vi . fn ( ) ,
1213+ clineMessages : [ ] ,
1214+ apiConversationHistory : [ ] ,
1215+ updateApiConfiguration : vi . fn ( ) ,
1216+ } as Partial < Task > ,
1217+ )
1218+ const historyItem : HistoryItem = {
1219+ id : "test-task-id" ,
1220+ ts : Date . now ( ) ,
1221+ task : "Test task" ,
1222+ mode : "code" ,
1223+ number : 1 ,
1224+ tokensIn : 0 ,
1225+ tokensOut : 0 ,
1226+ cacheWrites : 0 ,
1227+ cacheReads : 0 ,
1228+ totalCost : 0 ,
1229+ }
1230+ vi . spyOn ( provider . taskHistoryStore , "get" ) . mockReturnValue ( historyItem )
1231+ const updateTaskHistorySpy = vi . spyOn ( provider , "updateTaskHistory" ) . mockResolvedValue ( [ ] )
1232+ await provider . saveViewState ( "mode" , "code" )
1233+ await provider . contextProxy . setValue ( "mode" , "code" )
1234+
1235+ // The pin and shared mode writes land, then the mutation signal aborts during
1236+ // the activation reads. The activation is skipped, but the committed mode
1237+ // must still reach listeners and the webview through the emit/post tail.
1238+ const emitSpy = vi . spyOn ( provider , "emit" )
1239+ const postSpy = vi . spyOn ( provider , "postStateToWebview" ) . mockResolvedValue ( undefined )
1240+ const controller = new AbortController ( )
1241+ vi . spyOn ( provider . providerSettingsManager , "getModeConfigId" ) . mockImplementation ( async ( ) => {
1242+ controller . abort ( )
1243+ return undefined
1244+ } )
1245+ vi . spyOn ( provider . providerSettingsManager , "listConfig" ) . mockResolvedValue ( [ ] )
1246+ await provider . addClineToStack ( mockTask )
1247+
1248+ await provider [ "handleModeSwitchUnlocked" ] ( "architect" , mockTask , controller . signal )
1249+
1250+ expect ( provider . getValue ( "mode" ) ) . toBe ( "architect" )
1251+ expect ( emitSpy ) . toHaveBeenCalledWith ( "modeChanged" , "architect" )
1252+ expect ( postSpy ) . toHaveBeenCalled ( )
1253+ } )
1254+
10761255 it ( "keeps the cancellation result when the abort rollback write itself fails" , async ( ) => {
10771256 const mockTask = Object . assign (
10781257 { } as Task ,
0 commit comments