One small problem is that top_parent doesn't exist in the back branches, so I had to substitute a much uglier lookup in order to make this work there. I'm surprised that we got away without top_parent for this long TBH, but anyway this fix validates the wisdom of 2f17b5701.
So, pushed with some cosmetic adjustments and the modified back-branch code.
Thanks for the modifying and pushing and the back-patching. I didn't realize how the fix should look like if without top_parent. Thanks to the work in 2f17b5701, it makes life much easier.