Skip to content

Fix: Sort emails by date instead of UID order#98

Merged
Wh1isper merged 1 commit intoai-zerolab:mainfrom
jbkjr:fix-order-by-date
Jan 15, 2026
Merged

Fix: Sort emails by date instead of UID order#98
Wh1isper merged 1 commit intoai-zerolab:mainfrom
jbkjr:fix-order-by-date

Conversation

@jbkjr
Copy link
Contributor

@jbkjr jbkjr commented Jan 14, 2026

Summary

  • Fixes the order parameter in list_emails_metadata to sort by actual email date instead of IMAP UID order
  • UID order doesn't guarantee chronological order on all IMAP servers (e.g., ProtonMail Bridge assigns UIDs in reverse)

Changes

  • Collect all metadata before sorting (instead of sorting UIDs upfront)
  • Sort by the email's Date header
  • Apply pagination after sorting

Test plan

  • Verified syntax is valid
  • Test with ProtonMail Bridge: order=desc should return newest emails first
  • Test with standard IMAP server to ensure no regression

Fixes #97

The order parameter was sorting by IMAP UID order, which doesn't
guarantee chronological order on all servers. ProtonMail Bridge
(and potentially others) assign UIDs in non-chronological order,
causing order=desc to return oldest emails instead of newest.

This fix:
- Fetches metadata for all matching emails
- Sorts by the actual email Date header
- Applies pagination after sorting

Fixes ai-zerolab#97

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@codecov
Copy link

codecov bot commented Jan 14, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Copy link
Member

@Wh1isper Wh1isper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm concerned about potential performance issues when dealing with a large number of files, but we can merge it first.

@Wh1isper Wh1isper merged commit d9e62c6 into ai-zerolab:main Jan 15, 2026
9 checks passed
jbkjr pushed a commit to jbkjr/mcp-email-server that referenced this pull request Jan 15, 2026
Addresses performance concern raised in ai-zerolab#98 review. The previous fix
fetched headers for ALL emails before sorting, causing O(n) network
calls for large mailboxes.

Changes:
- Add SORT capability detection (RFC 5256)
- When SORT supported: server-side sorting, fetch only page headers
- Fallback: batch fetch Date headers, sort, fetch page headers
- Reduces network calls from O(n) to O(2) for any mailbox size

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@jbkjr
Copy link
Contributor Author

jbkjr commented Jan 15, 2026

@Wh1isper Good catch on the performance concern! I've addressed this in #100 which:

  1. Uses IMAP SORT (RFC 5256) when available - the server does the sorting and we only fetch headers for the requested page
  2. Falls back to batch fetching when SORT isn't supported - fetches all Date headers in one call, sorts client-side, then fetches full headers for just the page

This reduces network calls from O(n) to O(1) — for a mailbox with 10,000 emails requesting page 1, we now make 2 IMAP calls instead of 10,000.

@jbkjr jbkjr deleted the fix-order-by-date branch January 15, 2026 08:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: order parameter sorts by UID instead of date, breaks with some IMAP servers

2 participants