This is a question that stands the test of time. In a perfect world, of course, it is best to sort out these things upfront. But you are dealing with professors who are profoundly imperfect about these things and squeamish about conflicts of interest and the like. If you ask upfront, you may get an evasive answer. That is because professors don't really know. They likely are happy to co-author if you contribute a lot but are not so happy if it turns out you are "just an RA."
This, of course, hints at what the potential right answer is. Professors should offer a contingent agreement. If you do enough, you become a co-author but if you do not, you are just an RA. I was offered this when I was in grad school and didn't end up doing enough and that was fine. The advantage of this is that it creates good incentives. The disadvantage is that "doing enough" is inherently subjective and so the ability of the professor to honour this depends on social skills and we are back where we started. But in economics, the idea of a contingent agreement makes sense so I think you would be well within your rights to ask for it -- even if it is subjective in many dimensions.
I should also point out that if this were done, it would be a good norm. If the market knew that grad student authorship was only granted when the student contributed, then the market will see that as a signal and not infer that it was just the professor being nice or something. It would give value to the co-authorship which is what you want more than the co-authorship itself.