ActionQueue.InsertActionSorter leads to data loss and/or duplicate inserts. After sorting the insert actions, it can happen that some actions are missing from the list, and that other actions are duplicated, because the code sorting the batches has a bug which causes batches to get duplicated or lost in the latestBranches list.
I think the problem is in the code swapping the batches in InsertActionSorter#sort:
If this gets executed more than once, it removes a preceding batch but inserts the same new batch every time, instead of the batch it removed.
For example, if we have start with batches A, B and C
This results in batch B being dropped and batch C being duplicated, and this is what happened to us in a production environment.
A workaround is to turn off sorting of inserts.