commit 2feeca740df61f4131c81185a500ff573890c5ac
Author: Timo Sirainen <timo.sirainen@open-xchange.com>
Date:   Mon Jun 29 11:39:17 2026 +0000

    imapc: Fix SORT passthrough returning empty result with search criteria
    
    UID SORT with search criteria (e.g. "UID SORT (REVERSE ARRIVAL) US-ASCII
    NOT DELETED") returned an empty "* SORT" reply over imapc, even though
    "UID SEARCH" with the same criteria returned the expected UIDs.
    
    The SORT reply handler stored the remote UIDs and then mapped them to
    sequences via the local index (mail_index_lookup_seq). The local index
    isn't necessarily populated yet - for example right after SELECT - so
    every lookup failed and all messages were dropped. It also didn't mark
    the search args as matched, so they got re-evaluated against local mail
    that may not be available.
    
    Map the remote UIDs through the live msgmap and mark the args as matched,
    the same way the SEARCH passthrough does.
    
    Broken by 97adde09653753ec9269d34d17a051585bd2ce05

diff --git a/src/lib-storage/index/imapc/imapc-search.c b/src/lib-storage/index/imapc/imapc-search.c
index c51b870e03..583c534074 100644
--- a/src/lib-storage/index/imapc/imapc-search.c
+++ b/src/lib-storage/index/imapc/imapc-search.c
@@ -362,10 +362,26 @@ bool imapc_search_next_update_seq(struct mail_search_context *ctx)
 		return index_storage_search_next_update_seq(ctx);
 
 	if (ictx->sorted) {
+		struct imapc_mailbox *mbox =
+			IMAPC_MAILBOX(ctx->transaction->box);
+		struct imapc_msgmap *msgmap =
+			imapc_client_mailbox_get_msgmap(mbox->client_box);
+		uint32_t rseq;
+
+		/* convert remote UIDs to sequences via the live msgmap, the
+		   same way the SEARCH reply does. The local index may not yet
+		   contain these messages (e.g. just after SELECT), so looking
+		   them up from there would drop them and return nothing. */
 		while (ictx->n < array_count(&ictx->sorted_uids)) {
 			uidp = array_idx(&ictx->sorted_uids, ictx->n++);
-			if (mail_index_lookup_seq(ctx->transaction->view, *uidp, &ctx->seq))
+			if (imapc_msgmap_uid_to_rseq(msgmap, *uidp, &rseq)) {
+				ctx->seq = rseq;
+				ctx->progress_cur = ctx->seq;
+				/* the remote already evaluated the search args -
+				   don't re-evaluate them against local mail. */
+				imapc_search_set_matches(ctx->args->args);
 				return TRUE;
+			}
 		}
 		return FALSE;
 	}
