Another thing is that the A.I. has two systems for invading: the planned invasion (takes years to prepare, this is the one your spies can warn for) and the opportunistic invasion. I think the degree of opportunism is influenced by the A.I. personality, but I am not sure.

The A.I. is aware of the presence of your spies and assassins (or at least: it knows when your assassins target one of their agents), and I understand they are unwilling to sign alliances when you have these agents in their territories. I have never seen this confirmed, however.