+ /*
+ * if we reach the end of the list without finding a top-level
+ * message, that means the thread is a cycle (or set of cycles)
+ * and any message can be considered top-level. Choose the oldest
+ * message, which happens to be first in our list.
+ */
+ if (first_node) {
+ message = first_node->message;
+ if (_notmuch_message_list_empty(thread->toplevel_list) ||
+ ! _parent_via_in_reply_to (thread, message)) {
+ _notmuch_message_list_add_message (thread->toplevel_list, message);
+ }
+ }
+
+ /* XXX this could be made conditional on messages being inserted
+ * (out of order) in later passes
+ */
+ thread->toplevel_list = _notmuch_message_sort_subtrees (thread, thread->toplevel_list);
+
+