One of the nice things about diving within recreational dive limits (as anyone in Basic Scuba should be talking about doing) is that, with reasonable dive planning, the situation posited by the OP should be one you will never encounter. After all, the surface is always an option -- and if you've done proper gas planning, you have the gas to get you and your buddy to that surface from anywhere in the dive.
Once you get into overheads and staged decompression diving, these questions get a lot more real. I remember a story from a couple of years ago, where a diver with a significant deco obligation had to decide whether to surface a panicked OOA diver (not his own buddy) at the risk of major DCS, or try to stay underwater and complete his deco obligation. He decided to take the risk to himself, surfaced the OOA diver (who died), and left his buddy . . . who died, and I don't think anyone knows why.
I don't know what I would do, if I were cave diving and encountered a situation where my buddy had an issue that would so delay us that we could not both get out. I would like to say that I would keep trying, and hoping the inexorable math would somehow relent, but honestly, I won't know until that day comes what I will do. I do a lot of dive planning and training to avoid ever facing the issue.