If there is an obsolete unit that upgrades to a unit you are building:
A> Increase cost of the unit by 10% when building it, and
B> Generate a 'fair' chance that the hammer-difference will be spent on a spontaneous unit upgrade.
Ie, if you are building a 100 hammer unit, and it is an upgrade to a 50 hammer unit...
It costs 110 hammers to build the unit. When you build the unit, you have a 10/(100-50) = 20% chance to cause a random 50 hammer unit you own to upgrade to the 100 hammer unit.
This isn't the scope of better AI, and in fact breaks down in some mods (where you can upgrade a more expensive unit to a cheaper one, or the same cost, etc).