That's more or less what a human player would do so I think you're on the right path here.
I have implemented it in this way. But it needs to be properly tested.
The come out when enemy is weaker-part works well, as can often be seen against aliens.
However, in the test-games a city was rarely attacked by enough forces to trigger the "stay inside"-part.
The reason is: We have the uncoordinated attacking from before but with less units, since everyone keeps more units to defend.
The attackers often-times didn't even dare to walk into the cities radius.
Captures only occured to off-site-cities far away from the defenders home-terriotry or later on with black-holes.
I have started a game where I play myself once again. Economically the improvements of worker-allocation and the bugfix to tile-improvement seem to work well. I'm quite far behind in everything (playing on Hard difficulty).
But usually it was the later stages of the game where I could catch up and pull ahead, so I'll have to see how it goes this time.
Also right now exploring the world doesn't work for some reason. I have to find out why. AI has to make contacts and try and gather goodies too.
On the pro-side: not even bothering with exploration keeps their early unit losses at a very low level.
I usually explore with weak units until the alien aggression starts. The next wave of exploration starts when the explorer can easily manage the xenomorphs or with ships (that can easily manage the carcharodons).
The reason why it didn't explore was a combination of several issues.
The most important one was that the exploration-agent generates a maximum of three targets to explore. But then these targets where checked for their viability. But I had forbidden land units to explore water. So when the top-three priorities where water-tiles it would never explore unless it had ships.
Now I have no longer forbidden it but instead just decrease the value of water-exploration.
Another issue was that with more units assigned to defending, there were less left to do exploring. And without exploring, the AI never saw a reason to make more units until the aliens actually came into the city-radius. And then it prioritized fighting aliens over exploring. I reduced the required defender-count for when an AI had not made contact yet to 1 per defendable location.
I also experimented with reducing it to zero before alien-agression starts, so it would explore with it's starting unit. However, this had resulted in the second city being lost to alien and it seems too dangerous, if another faction finds you first and takes your capital right away.
However, the issue of not taking ruins should be resolved by it and contacts are also made much quicker.
All of that helped me to better understand how units are managed. That first was a big mystery to me but now it becomes clearer and clearer.
Now I finally feel competent enough to making the AIs smarter about how to use their spies. I tried before but had very strange effects. Most likely because I didn't understand how it works.
Did you know that you can click a spy-option and then the target-city even if the spy is not at the city yet? I learned that through how the AI does it. ^^
And if this works, the next step would be implementing better tactics like flanking and moving to tiles that grant an attack-bonus before attacking.