(bug #243536) tsort.merge_sorted() should work even with a ghost in mainline.
There is a bit of a discrepency when it comes to ghosts. The revision_graph passed to merge_sorted has had ghosts stripped, because it cannot handle pointing to nodes that aren't present. (Thinks there is a graph cycle.) However, the mainline_history includes the ghost. For now, work around this by ignoring the ghost in the mainline history.