In bereshis 21:17 it says
And God heard the lad's voice, and an angel of God called to Hagar from heaven, and said to her, "What is troubling you, Hagar? Fear not, for God has heard the lad's voice in the place where he is.
Which the gemarah, Rosh HaShanah 16b, says teaches us that
A person is judged only based on his deeds of that moment, as it is written, ‘G-d has heard the voice of the youth [Yishmael], as he is there.
Rashi, quoting the midrash rabba fills in
For the ministering angels were accusing and saying,“ O Lord of the Universe, for one who is destined to kill Your children with thirst, You are bringing up a well?!” And He answered them, “What is he now, righteous or wicked?” They replied, “Righteous.” He said to them, “According to his present deeds I judge him” (Gen. Rabbah 53:14)
But was Yishmael righteous? 8 Pesukim earlier we find
וַתֵּרֶא שָׂרָה אֶת בֶּן הָגָר הַמִּצְרִית אֲשֶׁר יָלְדָה לְאַבְרָהָם מְצַחֵק Which Rashi, from the midrash, explains as making merry: Heb. מְצַחֵק. An expression of idolatry, as it is said (Exod. 32:6):“and they rose up to make merry” (לְצַחֵק) . Another explanation: An expression of illicit sexual relations, as it is said (below 39:17):“to mock (לְצַחֶק) me.” Another explanation: An expression of murder, as it is said (II Sam. 2:14):“Let the boys get up now and sport (וַיִשַׂחֲקוּ) before us, etc.” - [from Gen. Rabbah 53:11]
So Yishmael engaged in at least one and possible all three of the cardinal sins, which are also capital offenses for a Ben Noach.
Why then is he dubbed "righteous" and not put to death?