The AI will occupy buildings. It does so carefully and usually gets out before the building falls to cannon fire.
The biggest deciding factor I have seen, as to if the AI attacks or defends is its artillery. If it has equal or better artillery it is happy to pound you with it.
If you take out a couple of their guns they attack.
On defense they are always redeploying to avoid being flanked.
It does the best it can, being only dumb AI and only reacts to what the player does.
Bookmarks