Skip to content

feat(platform): give each mail credential its own sync watermark - #2915

Merged
Israeltheminer merged 2 commits into
mainfrom
feat/mail-sync-credential-watermark
Aug 5, 2026
Merged

Israeltheminer merged 2 commits into
mainfrom
feat/mail-sync-credential-watermark

Conversation

@Israeltheminer

Copy link
Copy Markdown
Contributor

Stacked on #2914 — review that first; this PR's diff is only the credential fields and the query.

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

connectorCredentials gains two optional epoch-ms watermarks:

field meaning
mailSyncInboundSince how far this mailbox's Inbox has been read
mailSyncOutboundSince how far its Sent folder has been read

Optional on purpose — an existing row simply has none, and a mailbox with no watermark reads its newest limit messages on its first pass. No backfill, and adding optional fields is data-safe, so migrations:check passes without a migration (schema snapshot updated).

patchCredentialInternal accepts both so the sync host can advance them, and listActiveCredentialsInternal returns 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 selects encryptedData. Scoped by organizationId through the by_org_connector index.

Nothing calls the query yet. It ships here so the sync change that follows is a diff about sync.

Tests

listActiveCredentialsInternal covered 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.

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.
Base automatically changed from feat/email-attachment-storage to main August 5, 2026 09:06
@Israeltheminer
Israeltheminer merged commit 0fb6596 into main Aug 5, 2026
2 checks passed
@Israeltheminer
Israeltheminer deleted the feat/mail-sync-credential-watermark branch August 5, 2026 09:06
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.

1 participant