Seen Arumba mention something about this and I have had the same problem too.
If you only upgrade half of an underground belt you no longer have a working underground belt :)
So my proposal is:
Underground belt upgrade is skipped if no connected neighbor is also selected or if there is not enough materials to upgrade the underground and its neighbor
One way I can think to handle this would be during the first loop if type = ug belt add it to a secondary table and skip for now. After the main upgrades are done loop through the ug-belt table AND if neighbor is also in the table AND there are enough belts in inventory to update (if alt-selected skip ordering the upgrade if first check fails).
Also not sure if it is but pocket upgrading should be limited to build distance or slightly more.