diff --git a/lib/src/op_walk.rs b/lib/src/op_walk.rs index 3241f1163..bfc078363 100644 --- a/lib/src/op_walk.rs +++ b/lib/src/op_walk.rs @@ -207,13 +207,17 @@ impl PartialOrd for OperationByEndTime { /// Walks `head_ops` and their ancestors in reverse topological order. pub fn walk_ancestors(head_ops: &[Operation]) -> impl Iterator> { + // Emit the latest head first to stabilize the order. + let mut head_ops = head_ops + .iter() + .cloned() + .map(OperationByEndTime) + .collect_vec(); + head_ops.sort_unstable_by(|op1, op2| op1.cmp(op2).reverse()); // Lazily load operations based on timestamp-based heuristic. This works so long // as the operation history is mostly linear. dag_walk::topo_order_reverse_lazy_ok( - head_ops - .iter() - .map(|op| Ok(OperationByEndTime(op.clone()))) - .collect_vec(), + head_ops.into_iter().map(Ok), |OperationByEndTime(op)| op.id().clone(), |OperationByEndTime(op)| op.parents().map_ok(OperationByEndTime).collect_vec(), )