Thank you for your question. The Prophets (as) and the Imams (as) are commanded to abide by the outward nature of the divine law and for that reason they cannot punish someone before a crime even if they know that crime will be committed.
On the other hand, as is clear from the verses concerning Khidhr, he is not ordered to conform to the same rules and so his responsibility differs from that of the Imams (as). Khidrs actions were not independent of the Will of God and that is why he is not blamed in the Qur'an.
May you always be successful