My tranny did that to me for a while. Basically it would work like clockwork until the first real hill or hard acceleration (or up to 20 minutes if used gently), then it would not go into 4th even when the PCM commanded it. A few hours running on blocks in the shop was no problem and would pop into 4th nice and crisp everytime.
In the end I tore it down to the valve body and found the 3-4 shift valve was badly scratched. Sanding that scratch off with 600 wet sandpaper and gently sanding the bore of the valve cured the problem, but since other valves were not so easily fixed (namely the (%&(*@$ing D-2 shift valve), I had to eventually replace the whole valve body.
Do your symptoms exactly match mine?