Repository navigation
feat(platform): give each mail credential its own sync watermark - #2915
Merged
Merged
Conversation
Mail arriving over IMAP lost its attachments. The connector returned body text and headers only, so a message with a CV or an invoice landed in the Inbox as prose with no trace of the file. Pull attachment parts out of the parsed MIME in `imap-smtp.get_message` and carry them on the email object: metadata always, plus base64 bytes when the part fits under the inline cap. Sync then drains those bytes into the org's blob storage, records the file in `fileMetadata`, and rewrites the attachment to a `storageId` + download URL before ingest — so what reaches a conversation is a reference the Inbox can open, not a payload it has to carry. Bytes are drained per message rather than per page: holding a whole page of base64 in one action would scale resident memory with `limit`. The cap is 5 MiB raw (~6.7 MiB encoded), well inside the Convex function-payload ceiling; a larger part is listed as metadata only rather than risking the pass. `contentBase64` is wire-only. `attachmentsForMetadata` strips it at every ingest sink, because the message validator is a strict object that would reject the row and the metadata records — untyped JSON — would otherwise swell by the whole attachment.
Mail sync tracks how far it has read with one cursor per connector, derived from the newest message already in the Inbox. That works while a connector has one mailbox behind it and breaks as soon as it has two: a mailbox connected later inherits the other one's position and never sees anything older than it. Put the position on the credential row instead. `mailSyncInboundSince` and `mailSyncOutboundSince` are optional epoch-ms watermarks, so an existing row simply has none yet and reads its newest messages on the first pass — no backfill, and adding the fields is data-safe. `listActiveCredentialsInternal` is what a fan-out iterates: active rows for one connector, default first then by name so the order is stable, metadata only with no ciphertext. Nothing calls it yet; the sync host that does follows.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Groundwork for syncing every mailbox on a connector rather than only its default. The host that fans out follows in the next PR; this one moves where the sync position lives.
Why the position has to move
Today the cursor is derived per connector, from the newest delivered message already in the Inbox. With one mailbox behind a connector that is exactly right. With two it is wrong in a way that loses mail: a second IMAP mailbox connected next month inherits the first mailbox's position and never sees anything older than it. There is nowhere to record "Recruitment has read up to here, General Support up to there," because there is only one cursor for both.
The change
connectorCredentialsgains two optional epoch-ms watermarks:mailSyncInboundSincemailSyncOutboundSinceOptional on purpose — an existing row simply has none, and a mailbox with no watermark reads its newest
limitmessages on its first pass. No backfill, and adding optional fields is data-safe, somigrations:checkpasses without a migration (schema snapshot updated).patchCredentialInternalaccepts both so the sync host can advance them, andlistActiveCredentialsInternalreturns what a fan-out iterates: active rows for one connector, default first then by name so the order is stable across passes, metadata only — the projection never selectsencryptedData. Scoped byorganizationIdthrough theby_org_connectorindex.Nothing calls the query yet. It ships here so the sync change that follows is a diff about sync.
Tests
listActiveCredentialsInternalcovered for the filter (disabled rows excluded), the ordering (default first), connector scoping, watermark round-trip through the patch mutation, and the absence of ciphertext in the projection.