Anas bin Malik reported,

The Messenger of Allah ﷺ said, "You should not be impressed by anyone until you see how their life ends, for a person may spend a period of his life, or a stretch of his time, doing good deeds, such that if he were to die in that state he would enter Paradise, then he changes and does evil deeds. And a servant may spend a stretch of his time doing evil deeds, such that if he were to die in that state he would enter the Fire, then he changes and does good deeds. When Allah intends good for a servant, He puts him to work before his death."They asked, "Messenger of Allah, how does He put him to work?"He replied, "He grants him success in doing a righteous deed, then takes his soul while he is in that state."

Azami 1047

Sahih

عن أنس

أن رسول الله ﷺ قال: لا عليكم أن لا تعجبوا بأحد حتى تنظروا بم يختم له، فإن العامل يعمل زمانا من عمره، أو برهة من دهره بعمل صالح، لو مات عليه دخل الجنة، ثم يتحول فيعمل عملا سيئا، وإن العبد ليعمل البرهة من دهر بعمل سيء، لو مات عليه دخل النار، ثم يتحول فيعمل عملا صالحا، وإذا أراد الله بعبد خيرا استعمله قبل موته قالوا: يا رسول الله، وكيف يستعمله؟ قال: يوفقه لعمل صالح، ثم يقبضه عليه.

الأعظمي 1047صحيح
Contents
Reference
Commentary
Ayat
Collection
Jami al-Kamil
Book
Faith
Chapter
Belief in the divine decree and predestination
Section
Actions are judged by their endings
Numbering
Jami al-Kamil 1047 (2nd edition)
Azami 1047