Bad intentions, Good results
ויאמר אלהם יוסף אל-תיראו כי התחת אלקים אני: ואתם חשבתם עלי רעה אלקים חשבה לטבה וגו’
Yosef said to [his brothers]: “Do not fear. Am I instead of G-d? You thought to do evil to me, but G-d considered it for the good…”
After Yaakov’s funeral, his sons were worried that Yosef bore a grudge against them for their selling him into slavery. They made up a whole story that Yaakov requested that Yosef forgive them. What was Yosef’s response? He reassured them. He asked rhetorically: “Am I instead of G-d?” He explained that although they had bad intentions by selling him, Hashem was behind the scenes. The whole sale was a way to get Yosef to Egypt, so that he could be promoted to viceroy. With his prestigious position, he was able to secure food for the Egyptian empire despite a devastating famine. This ended up being the salvation for Yaakov’s whole family. So, despite their intentions, it was for the best. What was Yosef stressing by saying that “am I instead of G-d”?
Perhaps he was alluding to a concept taught by our Sages. What happens if a person intends to eat something non-kosher, but they end up eating something kosher? Although they failed to eat non-kosher, they had sinful intent. They aren’t punished, as they didn’t transgress the Torah. However, we are taught that they need Hashem’s forgiveness and atonement for what they did . Although, this innovation seems to contradict another idea. We are taught that except for thoughts of idol worship, we aren’t punished for our sinful thoughts. How do these two ideas work together?
We must simply say that actually acting on these thoughts is worse. It comes out then that there are three levels. Someone simply thinks of sinning but doesn’t carry it out is entirely exempt from punishment. Someone who thinks of sinning and actually transgresses the Torah is punished by the Earthly court. The innovation is that someone who thinks of sinning, tries to carry it out, yet fails, also isn’t punished by the Earthly court. Yet, they still require Hashem’s forgiveness and atonement.
This was Yosef’s intent with his response to his brothers. This case is exactly the same as the one taught by our Sages. The sons of Yaakov had evil intent by selling their brother Yosef. They thought they were following through on that thought. In reality, Hashem engineered it such that this sale would end up being a salvation for their entire family. Yosef was to become the viceroy of Egypt, and this was how it would be fulfilled. Thus, Yosef’s response to his brothers was that they weren’t worthy of his punishment. It was like they intended to eat non-kosher, and accidentally ate kosher. All they require is Hashem’s forgiveness and atonement. Yosef told them that he wasn’t in place of Hashem, so they need not fear retribution from him.
 Based on Maharil Diskin to Genesis 50:19, which is a piece written by his son
 Cf. Targum Onkelos ad. loc.
 Genesis loc. cit., v. 19,20
 This was permissible for the sake of peace (Yevamos 65b, quoted by Rashi ad. loc. v. 16)
 Kiddushin 81b, Nazir 23a, and Sifrei Bamidbar § 153, brought by Rashi to Numbers 30:6
 On a side point, Sifrei loc. cit. bases it on v. 6, which deals with a twelve-year-old girl in her father’s home, and the gemarra bases it on v. 13, which deals with an older married woman. Rav Asher Weiss in Minchas Asher Bamidbar § 67 proves from this Sifrei that the rule of אין ב”ד מענשין פחות מעשרים שנה (Shabbos 89b; Yerushalmi Bikkurim 2:1, brought by Tosafos to Moed Kattan 28a s.v. מה בחמשים; Yerushalmi Sanhedrin 11:5; Bereishis Rabbah 58:1, brought by Rashi to Genesis 23:1; Bamidbar Rabbah and Midrash Tanchuma Korach § 5, brought by Rashi to Numbers 16:27) isn’t to be understood literally, as we see a twelve year old girl needs Hashem to atone her for her sin
 Rav Asher Weiss, in a related shiur from 5773 on parshas Vayechi, proves that this in some way is considered a transgression, as we see from Nazir loc. cit. that there are Rabbinic lashes in such a case, and from Tosafos to Kiddushin 32a s.v. דמחיל ליה that there’s lifnei iver on such a case.
 Kiddushin 39b
 Cf. Yoma 29a, which says הרהורי עבירה קשו מעבירה. See Nefesh HaChaim 1:14 for a deeper understanding of this concept
 Rav Asher Weiss, in the above-mentioned shiur, and in Minchas Asher loc. cit., says the same answer to the question. In Minchas Asher he says there are those who ask this question, and in the shiur he says it’s from the Chasam Sofer on this verse. I found it in Toras Moshe I to Numbers 30:13 [#2], although it says it’s a piece from the sefer Shir Maon, which was written by the Chasam Sofer’s grandson Rav Shimon Sofer. Rav Asher Weiss says that the “Chasam Sofer”, due to this question, says that the gemarra which teaches that we aren’t punished for our thoughts isn’t to be understood literally. There is still a little guilt that is incurred. This reading of the gemarra is hard to understand, as it doesn’t fit with the context of the gemarra, and I personally find the other answer more compelling. In fact, the Shir Maon in his first answer says like this as well
 Cf. Ohr HaChaim to Genesis 50:20. He writes that the brothers were like someone who intended to hand his friend poison, and ended up giving them a cup of wine. Therefore, he concludes that they weren’t guilty of anything. Many commentaries are bothered with this explanation, as it seems to be a direct contradiction to the gemarras cited above. The Beis Yitzchak Yoreh Deah 8:8 and Divrei Yatziv Yoreh Deah 1:67 suggests that perhaps here it is better as they ended up performing a mitzvah, unlike the case that the Sages discuss. The Keli Chemdah ad. loc. § 3, 4 suggests that the brothers didn’t intend to transgress anything, as they held that it was Hashem’s will to punish Yosef. However, Rav Asher Weiss (in the shiur and in Minchas Asher loc. cit.) rejects both of these explanations, as they don’t address the fact that the Ohr HaChaim himself says that intending to poison someone and failing isn’t guilty of anything. Rav Asher Weiss first differentiates between mitzvos between man and G-d, where intent matters most, and interpersonal mitzvos, where the result is the key. He concludes by explaining (with reservation) that the Ohr HaChaim simply meant that it’s not a עבירה גמורה and carries no Earthly punishment