Ps. 139:23 Search me, O God, and know my heart!
Try me and know my thoughts!
24 And see if there be any grievous way in me,
and lead me in the way everlasting!
The entire 139th Psalm is a magnificent, but I'll let you read it on your own.
David is calling here for God to search him, look inside his heart, discern what might be inside his heart, mind and thoughts. This is not some sort of quick, top layer search. He is calling on God to look deep inside and inspect what may be there. David has just said in the previous verses that God even knows what is in his mouth before he speasks, so why would God need to check this? David wants to make sure of his own righteousness. The heart is deceitful and desperately wicked and we can't know it; we need God to show us what is there.
Once God has searched all that is within him , David then requests to know if there is any wicked thing, any thing that might be an offense to God that he doesn't see. David wants his heart to be pure and undefiled and he needs God's help to do it.
After much searching, trying and seeing that there is only good in him, David wants to then be led. David is the king, isn't he supposed to be leading? This shows the humility of David. What better leader than one who is being led by the Savior? We are sheep and sheep are not driven but lead. The Great Shepherd wants to lead us, but will not take up the rear, but leads from the front.
David is excited about the prospect of everlasting purity. He wants God to show him his sin and then lead him in the everlasting way of purity. This, again, makes a case for repentance toward joy. David wanted his sin canceled, but only to be led toward the everlasting, or ancient, way.
Jer 6:16 Thus says the Lord:
“Stand by the roads, and look,
and ask for the ancient paths,
where the good way is; and walk in it,
and find rest for your souls.
This is the way that David wanted to walk in. He wanted to be on the restful road. Oh that our souls would rest in God. Oh that we would be dealing with our sin, and we would ask God to search out our hearts and show us where we need to repent. Then when He has done that work, may He lead us in the everlasting way, the way of rest.
God blesses our humility and loves righteousness. We can't come into His presence without righteousness, and we have none of our own. We must beg Him to make us righteous, to work in our hearts to love righteousness more than the trifles of this world, to seek Him as our best joy. We must not go on in sin, defiling ourselves and defaming the Name of Our Great God. He has called us to be holy, set apart and sanctified for Him, He must do the work, we must be willing to have the work done in our lives. Oh that He would find us willing and begging.