Skip to content

Bug: mail sync re-fetches the boundary message on every poll, forever #2996

Closed

Description

@Israeltheminer

What happens

Every mail sync poll re-fetches the newest already-synced message — its body, and all of its attachment bytes — indefinitely, until newer mail displaces it from the boundary. No new conversation or message is created (dedupe works), so the cost is invisible: wasted connector round-trips per poll, and a fresh blob per attachment per poll.

Cause

An off-by-one at the cursor boundary. The cursor is derived from the newest existing message's own timestamp, and the filter that applies it is inclusive.

convex/conversations/sync_mailbox.ts:154-167 — cursorFromMessage:

const since = message.deliveredAt ?? message.sentAt ?? message._creationTime ?? null;

lib/connectors/natives/imap-smtp.ts — fetchSummaries, after the IMAP SEARCH:

const cursor = query.since;
return summaries
  .filter((message) => cursor === undefined || message.sentAt >= cursor)

>=, and cursor is that message's own sentAt. So the boundary message satisfies its own filter on every poll.

Note this is not the SEARCH SINCE date-granularity issue. SINCE is date-granular, but the millisecond re-filter above narrows the candidate set back down — the comment at imap-smtp.ts:1155 says so explicitly. What survives is precisely the messages at or after the cursor, i.e. the boundary message.

Consequences

This is plausibly the root of the duplicate-blob volume reported under #2985; that issue attributes the re-ingestion to the missing conversation binding, which explains the lack of an idempotency key but not why the same message is fetched again in the first place.

Suggested fix

ConversationSyncCursor already carries the boundary message's identity:

export interface ConversationSyncCursor {
  since: number | null;
  messageId: string | null;   // lib/connectors/natives/platform-conversations.ts:88
}

cursorFromMessage populates messageId, but nothing reads it — the other messageId references in sync_mailbox.ts are on wire summaries, not the cursor.

So exclude the boundary by identity rather than by timestamp, and keep >= so a message sharing the boundary's exact timestamp is still picked up. Flipping to a strict > would fix the loop but silently drop same-timestamp siblings, which is how the inclusive compare was probably arrived at.

Verification

A regression test at the fetchSummaries seam: given a cursor equal to a summary's sentAt and that summary's id, the summary is excluded, while a second summary with the same sentAt and a different id is returned. Mutation-test it — a strict > must make the same-timestamp case fail.

Activity

  1. Israeltheminer commented on Aug 18, 2026

    @Israeltheminer
    ContributorAuthor

    On hold — do not implement yet. The diagnosis above is a code reading and has not been distinguished from the alternative.

    Live observation since re-enabling a 5-minute sync: 100+ new fileMetadata rows, all for a single attachment, all with ragStatus unset (so #2995 is confirmed working — none of them consume an indexing slot).

    That is consistent with the boundary compare described above, but it does not discriminate. Date-granular SEARCH SINCE re-lists every message from the newest one's calendar day, and only messages carrying attachments produce fileMetadata rows — so if the affected message is the only attachment-bearing one in that window, both explanations predict exactly one repeating file.

    The discriminating check: is the affected message the newest inbound message in that mailbox, or merely on the same calendar day as the newest?

    • newest → the boundary compare is the cause, and the fix above is right
    • same day but older → the millisecond re-filter is not doing what this issue claims, and the diagnosis needs revising before any code changes

    A cleaner version of the same experiment: send a test message with a harmless attachment on a different calendar day from the affected one. Under the boundary explanation the test message becomes the new boundary, so it starts duplicating and the older one stops. If both keep duplicating, this issue is wrong about the mechanism.

    Note the useful property — that experiment is also the mitigation, since it moves the boundary off the affected message.

    Separately, and regardless of which explanation holds: fetchBodies materialises attachments before the message dedupe (sync_mailbox.ts:319, the #2985 ordering), so any re-fetch from any cause duplicates. This issue removes the trigger; that ordering removes the class. Both are worth doing, ordering second.

  2. Israeltheminer commented on Aug 18, 2026

    @Israeltheminer
    ContributorAuthor

    Off hold — mechanism confirmed, both in code and live.

    Retracting the discriminating check proposed in the previous comment: it was ill-posed. The millisecond re-filter at imap-smtp.ts:1196 runs regardless of SEARCH SINCE granularity — it is the same line of code either way — so date-granularity only widens the IMAP candidate set before it is narrowed identically. Date-granularity alone cannot admit an already-ingested message. The inclusive compare is the whole cause, and the two explanations were never independent.

    The chain is exact, not inferred:

    • conversations/ingest/build_initial_message.ts:15,25,26 — emailTimestamp = new Date(email.date).getTime(), and both sentAt and deliveredAt are set to that same instant
    • cursorFromMessage (sync_mailbox.ts:154-167) — since = deliveredAt ?? sentAt ?? _creationTime, so for a delivered inbound message the cursor is that message's own timestamp
    • imap-smtp.ts:1196 — .filter((message) => message.sentAt >= cursor)

    Same field, same source, >=. The newest message satisfies its own filter on every poll, necessarily and indefinitely.

    Live confirmation: a test message with one attachment produced 4 fileMetadata rows in roughly 20 minutes on a 5-minute poll — one per poll — and all 4 carry ragStatus unset, so #2995 continues to hold and none of them consume an indexing slot.

    Open question that affects where the fix goes, not whether it is right: mail sync fans out per credential, so each credential plausibly carries its own boundary message. If more than one message is repeating, the exclusion has to be per-cursor rather than global. Worth confirming before implementation.

    The fix suggestion in the description stands — exclude the boundary by the messageId the cursor already carries (and which nothing currently reads), keeping >= so a genuine same-timestamp sibling is still ingested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugpriority: mediumservice: platform

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions