The TC lock\unlock is some times caused by noise generated in the electrical system by the alternator. It manifests itself at certain positions on the APPS and interferes with the signal read by the PCM to control the TC lockup. Hence you get the hunting at certain pedal positions.
There are a couple of ways to fix it. One way is order a premade filter kit from Diesel Transmissions Technologies (aka DTT) in British Columbia, Canada.
Here is their link
http://www.dieseltrans.com . The filter will clean up the signal that is used to determine TC lockup and in most cases will solve the problem.
You can also solve the problem by finding the wirre in the harness that runs from the APPS to the PCM and route it in its own loom away from the other wires. If it is noise from the alternator that will most times solve the problem also. If you feel really ambitious you could also build yourself a filter with some components from Radio Shack.
Before spending time and $$ you might want to try a few things and see what happens.
1. Check all grounds for contact and use dielectric grease on them.
2. Check both battteries and make sure they will hold a load.
3. Reset the APPS.
4. Check your harness connections between APPS and PCM.
Some times the above works and problem solved. Other times its on to the next step and either use an external controller or try the filter route. Either way its what happens when you stick low voltage computer equipment under the hood of a diesel truck. Some times things don't work as planned. Good luck.