Since the OP didn't close out this thread with a resolution, I'll post one.
I had a near identical issue as the OP, would bog at idle, in gear while stopped. Drove fine but the idle would never settle in. I replaced all hoses, desmogged, tried A different AFM, adjusted the TPS dozens of times, even tried a different one. Kept getting code 51. I won't bore you with everything I did but I did a lot.
No vacuum leaks yet vac gauge would wobble pretty good and was at around 13-14.
I finally sacked up and adjusted my valves, replaced the valve cover gasket, replaced the PCV and its gasket and... Sweet Mary my problem was solved after I retimed it. Runs smooth as butter and idles down when warm finally. Rock steady 19 vac.
So, the magic bullet I was looking for ended up being more of a shotgun blast but that is what solved my issue. Code gone. On to the next thing.