Okay you are definitely not wrong and I can see how this would be bad, but this is computer programming you can add exceptions and rulings that can be consistently checked whether they're true or false. You don't want it checking all the time though as that'll ruin performance needlessly.
So when does the AI check:
1. When it encounters an enemy fleet unrelated to actual player or npc empires such as space amoebas.
2. When it obtains sensor range on a fleet of considerable size of a rival.
3. When it is hostile to another empire and is about to declare war.
4. When war is declared on it.
5. When a galactic crisis happens and it experiences first contact with the select crisis.
6. When it suffers catastrophic loses in its current fleet and needs to rebuild. (Aka check what went wrong)
(I'm fairly certain you could add more to this but let's treat this as the basic outliner. Until any of these happen the AI is told to use the best available technology befitting it's ENERGY, KINETIC or EXPLOSIVE profile. Because lets face it that's what the player does, he uses the best available technology and tries to balance damage to armor and shield. Which I am fairly certain can be done, you should be capable of programming to tell the AI to use a balanced setup of shield vs armor damaging weapons. Anyway moving on.)
Given we don't have a proper fog of war(well we do, but it's the bare bone intel system) and we don't have espionage. I'd use the intel system as I've already given an example of, because you cannot see the enemy fleet until its within your sensor range.
Now, in order to prevent the AI from retrofitting all the time and bankrupting itself out of alloys it should be given different levels of urgency. So I'll attempt to define these levels of urgency. It's worth mentioning, the AI should have a formula or a hard cap where it is forbidden to expend a crazy amount of alloys for no reason as far as fleets are concerned.
AI Logic:
1. Adapt ship design only to main threat, then scale threat of additional parties according to firepower and naval capacity of their currently fielded fleets. (Stops retrofitting just because someone else entered it's space.)
2. Check if you are using PD and the enemy doesn't have weapon systems that are countered by PD and the other way around.
3. Cooldown on upgrading/retrofitting unless the war is over, the threat has passed, AI has lost all ships.
4. Focus on fighting first, upgrade if you return for repairs or are at max two/three jumps away from a shipyard.
Well the more I write about this the more I realize how much an AI overhaul Stellaris needs. It's clear I could go on like this for a while. I'm just wondering if this gets my point across? Sophisticated AI can be made, it's just hard.