Selective pressures that permit the emergence of cooperative behaviour require participating individuals to exhibit a variation in behaviour and those who cooperate result in having a higher fitness in the environment than those who shun cooperation. The selection effect operates on over the whole population and over times that are longer than the life span of an individual.
An individual that is implementing what ultimately will emerge as the winning strategy, may on average be better off, but it can (and it will) be also often worse off.
Depending whose existence you're optimizing for (individual or species) changes the framing of what is the optimal strategy.
An existential risk for a species changes the framing of what is rational to do quite a lot.
Also, multi-species symbiotic relationships are quite common in the animal kingdom. It would be surprising if that weren't true at other scales (and if it were limited to the sci-fi trope of "worker species, soldier species, leader species").