The AI doesn't usually randomly free ships to attack you, generally you have to be involved somehow. The only major exception are attack waves, ships used in attack waves don't exist in game prior to their attack and enter in a free state.
As for AI setting existing defending ships free, there's a few ways for that to happen. Destroying a command center in a system is a sure fire way to free AI ships, the remaining defenders are no longer tethered to their posts and come and attack you. Sometimes AI ships that go too far from their posts to chase your own ships will be set free, generally this is caused by the AI chasing high priority targets, like starships, transports, etc.. Certain AI types, specifically the Tag Teamer, will intercept attacks with all mobile ships in the system, which often causes lots of the ships to break free from their posts.
Lastly, and most dangerously, are cross planet attacks. The AI takes a large portion of the reinforced ships from its planet that match the current AI ship level and set them free all at once. This can result in anywhere from 500 to 4000 ships becoming aggressive all at once. Usually you have a 12 minute warning before such an attack occurs.
It's important to know that free ships don't always rush you. Commonly they'll retreat to AI planets to wait for more attackers, or sometimes they'll just sit around and attack when they think its right (not sure if that's a bug though, sometimes they'll sit around indefinitely!). If you're being attacked by ships, especially early in the game and from one or two specific points repeatedly, it is likely a patrol between two special forces posts. If you scout, you may be able to identify the posts in question and maybe take them out. You can usually tell if it's a patrol because they won't show up as threat prior to entering your system.