For a long while I tried to design a rule for balancing that would handle this gracefully but I was never able to boil it down to something simple and elegant.
The way I do this at the moment (for form 8949, which I have to file) is to write a custom script.
IIRC I use this script (I have to dig it up once/year when I do my taxes, I can never remember):
Come to think of it, it should be possible to handle this in a general trades report.
Beancount doesn't have a trades report, but I've thought for a long time that this should be part of the booking functionality, and the less intrusive way to provide this functionality would be by having the booking code insert metadata on postings when they get reduced (with a common unique strings to match them up), along with the helper code to pull that information out of the stream of directives. I have something similar as part of a completely unrelated plugin here:
I actually need this functionality at some point, I find it embarrassing that it's missing.
I often find myself wanting to extract the whole list of trades from Beancount (all the information is there).
(I've been prioritizing fixing bugs in the bits and bobs of time I have available here and there.)