Genes don't know. Natural Selection is truly results oriented. If the actions of a being with an "altruistic" gene makeup add to the survivability of his own or closely-related genes, then those genes will proliferate. In the case of fish, if a particular population is able to produce more offspring than nearby populations because its members are predisposed by their genes to pick up extra eggs when they don't have any of their own, then the next generation of the species will have a larger representation of those genes that predispose the fish to take on extra eggs, and a smaller representation (as a percentage) of the genes of more selfish fish that won't raise anyone else's eggs. And the next generation, the representation of the altruistic genes will increase, and so on, until perhaps the entire species bears the genes related to the predisposition.
A note on "just so" stories: They are seized upon by opponents of evolutionary theory as representing holes in the theory, since they cannot be proved. But they do not exist to bolster the theory (clearly only actual observed evidence can do this), but to rebut the objections that sceptics raise against the theory: that no one knows how certain transitions happened (thus they must have happened by some other process, aka a Miracle).
We can't always (barring the invention of a time machine or recovery of fossil evidence that, because of sampling, we may never be likely to find) go back and discover the fine details of the full story of how certain traits arose, how certain speciation events happened, or how certain stages in chemical evolution took place. That is not a scientific criticism of the theory, because a theory does not have to explain all events, but has to be the best explanation of all events by scientific method in order to be accepted. (A quick example: we can't analyze supernovae that exploded before recorded history, but that does not invalidate the theories constructed about the life cycles of stars that were made on the basis of more recent observations that have been made over far less than the span of almost any single star's life span.) But when that criticism is offered: "It is impossible for mutation and natural selection to explain the altruism of some male Banggai Cardinalfish who will take on eggs fertiliized by other males", it is only fair to accept that any plausible explanation is sufficient to rebut it. In other words, my scenario is not intended to explain the altruism of Banggai Cardinalfish (although it might

), but to rebut the allegation that mutation and natural selection can't account for it. Clearly, they can, even if this scenario doesn't represent the real sequence of events that produced their altruism.
To put it briefly, "just so" stories are not meant to fill out natural history, but to rebut unfair and unscientific criticisms of the theory of evolution. Those who use them to attack the theory of evolution are being doubly dishonest, because they don't have a better scientific explanation to offer, and they are making arguments from silence rather than attacking the observed evidence that overwhelmingly supports the theory.
Thanks,
Brad