Yes, the AI can be very irksome in its efforts to hamper your AC effort.
If you leave one or more AI civs strong enough, they can also be serious competitors in the race to build and launch.
Rather than focus with complete single mindedness on research and establishing a sufficient productive capacity to build the spaceship, I also look for opportunities, throughout the game, to peg back the AI civs.
Your post identifies one of the problems about that. The most powerful AI civs are likely to be quite remote - after all, you probably had the opportunity to cripple your near neighbours and at some stage will have felt sufficiently threatened by them to do so.
There is no simple answer to this. If you are going to break off from your space race efforts to mount a major campeign half way across the globe you may as well play blood lust and be done with it. So I just accept that a strong distant civ will be a competitor to the end and try to prepare appropriately.
One thing you
can do is to try very hard to keep the AI diplos and spies at bay. In my experience it is your near neighbours who will be the commoner would-be tech thieves - but they then give away the stolen tech, or swop it, to all and sundry. Especially so after the year 1750. Thus your weedy and unthreatening neighbour assists the more seriously competitive distant civ.
It is my experience that the AI does not go in for ship bourne diplos/spies much. Nevertheless some aircraft or one or two of the longer range ships can try to patrol the waters off your coastal cities to guard against that threat. Some people use missiles for scouting but that seems a bit artificial.
More important is to establish a chain of fortresses between your own cities and those of neighbours. Subject to your military strength, three squares away is better than two for these purposes. Then scout out from the fortresses and try to pick off all AI units which you detect. Again, air power is very helpful in this. Absent aircraft your own spies are good scouts because of their extended line of sight. They can also get your military units through ZOC to destroy the interlopers. The AI does not stack units effectively and is rarely clever about smuggling in a diplo/spy. As far as I know the AI only uses the cheat whereby its military units are allowed diplo/spy functions to bribe cities and units - not to steal tech. Nevertheless probably best to detect and to try to kill off
all approaching units.
I am often tempted, late in the game when I am cash rich, to pick up cheap nearby cities through bribery when they go into revolt. But nowadays I pause to see whether I can keep the diplos/spies away from that city after I acquire it. If not I often give the opportunity the go by.
The benefit of cutting back on tech loss comes in two ways. If you keep your healthy tech lead you get to control the timing as to when the space race starts. Nice, for example, if you have built up a store of caravans to speed up your building programme. Nice, too if you have got several of your cities to the 40/80 shields a turn level.
The second benefit comes from maybe getting to prevent the AI from working on the bigger elements of the ship for a bit if they don't have the requisite tech.
As an alternative, you can adopt the OCC type approach. Give away tech, keep your military might down. The AI is much less bloody with a human opponent who is not supreme.
Finally, a small point. If you think that your productive capacity/skill at building a fast ship ought to keep you in front during the space race, it may pay to get the race under way. In my experience, once the AI competitor turns its attention to building a spaceship, it often offers peace so as to be able to devote its attention to the building programme.
Beware though. Their production advantage makes them awesomely quick builders. I sometimes dread hearing that long sequence of clanky noises that come at the end of each turn when the mongols or some such build a quadrillion structurals in one turn!
Bastards!