I sat in a class recently and listened to an Orthodox rabbi expound on the convoluted path that had to be taken in the past and will presumably be taken in the future to get to Moshiach. I may not convey all of the ideas correctly but to summarize:
Some of the scandalous or questionable events that took place in the Bible were necessary to happen in what would seem like an improper way in order to prevent the Sitra Achra from stopping or slowing the progress of history and thus delaying Moshiach. Some examples of this would be Lot and his daughters (leading to Moab thus leading to Ruth and thus King David), Tamar and Yehudah (leading to Peretz and thus to King David), Rahab of Jericho, David and Bathsheba. I think there were other examples, but that is what I remember.
I do not think this rabbi made up this intricate thought process himself. I would like to know what earlier published sources expounded similar reasons for how history occurred and how human weakness and sin were either transformed for good or were used as a cloak for the real mission. I have some theological problems with this line of thought since it implies that another power besides G-d and human free will was at work (the Sitra Achra actively trying to prevent Moshiach), but I want to refrain from judging these ideas too harshly until I read the original sources which may present the ideas differently than this particular rabbi did. So, what earlier rabbis, Jewish philosophers, or mystics presented ideas like this, and what works would be useful to study to understand these ideas better?