It was happening when all slots were full but no unit even needed disbanding because the unit to be bought is part of the units that are inside of the existing slots.
(This commit contains excessive debugging but I don't want to remove it just yet incase another issue pops up.)
The score for buying army now scales with the cost of that army.
The cost of the units that need to be disbanded in order to hire the new units is subtracted from the army-hiring-score so the AI will prefer hiring armies where the amount of troops to disband is low or non-existent.
Fixed a bug that also disbanded troops when there still were free slots available.
When the AI cannot buy units in a city because all slots are blocked and the units in the slot are cheaper than the units it wants to buy, the AI will now get rid of the units that block that slot in order to be able to buy the better units.