Skip to content

feat(platform): list emailed attachments by arrival, affordably - #3035

Merged
Israeltheminer merged 3 commits into
mainfrom
fix/mail-attachment-listing-scan
Aug 21, 2026
Merged

Israeltheminer merged 3 commits into
mainfrom
fix/mail-attachment-listing-scan

Conversation

@Israeltheminer

@Israeltheminer Israeltheminer commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

Chat can list the emailed attachments a caller may read, newest arrival first, at a cost that does not grow with the table.

Why

An emailed attachment has no listing surface anywhere in the product. It is not a Document Hub row, so it is absent from the Documents page and from list kind="document", and its only appearance is on its own conversation in the Inbox. Retrieval was the sole way to discover one, which means guessing words inside a file whose name says nothing about why it was sent.

#3033 gave it a listing. This makes that listing correct and affordable, after three shapes that were neither:

  • walking every fileMetadata row under a scan budget spent the budget on rows that can never qualify — measured on a live deployment, 3,671 rows of which 3 carried a conversation — so reachability depended on creation position and the listing emptied as the table grew;
  • walking only the bound rows fixed reachability but still read up to the whole budget to return one page, and above it reported "most recent" from an arbitrary window, because that index orders by conversation rather than by time;
  • leading with conversations bounded the cost but ordered by conversation activity, so a fresh application sorted below an older one whose thread happened to get a reply.

What changed

  • fileMetadata gains mailReceivedAt, set alongside conversationId, and by_organizationId_and_mailReceivedAt. Convex has no partial indexes, so a field present only on bound rows is what makes that index mail-only: a range above undefined walks exactly the attachments that arrived by mail, in arrival order.
  • The reader reads limit rows and stops.
  • The stamp prefers the mail's own date, threaded from the sync, so importing old mail sorts by when it was sent. It falls back to the row's creation time, within minutes on a live poll.
  • The binder writes the stamp whenever it is missing, so a row bound before the field existed is repaired the next time its mail is touched.
  • Migration v0_4_1/05 backfills the rest, because a quiet conversation is never revisited and an unstamped row is invisible to the listing. The field itself needs no migration — a new optional field is data-safe growth, which migrations:check confirms and a snapshot refresh records. down clears only the value up writes, so a stamp taken from a mail's own date survives a rollback.

Risk

An empty list does not mean an empty inbox. The walk checks each attachment against its conversation, so a caller who can read few conversations is rejected on most rows. It stops after 200 examined and reports that it stopped, so the answer is "none of the most recent were visible to you" rather than "there are none". The residual limit: with that bound, a caller who can read very little may see nothing even though older readable attachments exist.

The per-row permission check is not redundant. The rows already come from an org-scoped index, so re-loading each conversation looks removable. It is not: an attachment's conversation id is a reference, not permission to read it. The check applies the live assignment rule through the same predicate the retrieval gate uses, which is also what makes a reassignment move attachments with no re-indexing. Removing it fails a test.

down also clears a fallback stamp. When the sync cannot parse a mail's date it stamps the row's creation time, which is the same value the migration writes — so a rollback cannot tell the two apart and clears both. Harmless: the binder re-stamps the row the next time its mail is touched. A stamp taken from a real mail date is untouched.

Tests

14 at the reader through convex-test: the privacy matrix (admin, team member, individual assignee, disabled role, unidentified caller, foreign conversation), arrival ordering against conversation activity, 700 unbound rows left untouched, truncation reported when the page fills and not when it doesn't, the scan bound reported rather than looking empty, unbound and trashed rows excluded, the indexed flag, and the corpus ref.

4 at the binder for the stamp: from the mail's date, from the creation time when unknown, back-filled on a row bound before the field existed, and left alone when already recorded.

The migration's own ritual — up through the real runner, handler idempotency, digest-equal down — plus the chain and versions suites: 140 tests.

Mutation-tested: each behaviour was deliberately broken in the source, one at a time, to confirm a test catches it. All 11 were caught, including ascending order, walking the wrong index, and never writing the stamp.

Scope

Does not index message bodies (#3015). Does not give attachments a Documents-page surface — this is the chat tool only. Does not page; the listing narrows.

Gate: typecheck, oxlint --type-aware, oxfmt, SAST 0 findings, migrations:check, platform 75,598 tests. One unrelated failure in audit_logs/integrity_check.test.ts — the flake filed as #3036; untouched by this diff and 17/17 standalone twice.

The attachment listing walked every `fileMetadata` row newest-first under a
600-row budget, filtering as it went. On a deployment whose table is dominated
by rows that can never qualify, the budget is spent before the walk reaches a
bound attachment, and which attachments are reachable depends on where they sit
in creation order.

Measured on a live deployment: 3,671 `fileMetadata` rows, of which 3 carry a
conversation. The listing reported `truncated: true` for every caller — which
was correct — and the set it returned shrank as the table grew.

The walk now ranges over `by_organizationId_and_conversationId` above
`undefined`, so it touches only rows that carry a conversation: three instead of
six hundred. Convex orders `undefined` below every real value, which is what
makes that bound mean "has a conversation". The scan cap survives as a ceiling
for an organization with very many mail attachments, where it now bounds a walk
over bound rows only.

The index orders by conversation rather than by time, so recency is applied to
the page before the limit is taken.

The regression test seeds 700 unbound rows ahead of three bound ones — above the
budget, and in the position that used to make them unreachable. It fails against
the previous walk and passes against this one.
The listing's cost grew with every attachment the organization had ever
received, and above its budget it was wrong about which attachments those were.

Two shapes failed before this. Walking every `fileMetadata` row under a
600-row budget spent the budget on rows that could never qualify — on one
deployment, 3,671 rows of which 3 carried a conversation — so whether an
attachment was reachable depended on where it sat in creation order. Walking
only the bound rows fixed reachability but still read up to the whole budget to
return one page of twenty, and past that budget it reported "most recent" from
an arbitrary window, because that index orders by conversation rather than by
time.

Conversations now lead. The executor takes the caller's most recently active
readable conversations, and the reader does one exact index lookup per
conversation. Reads scale with the inbox reached, not with the table: one row
read and one lookup per conversation, whatever the attachment history.

Ordering follows the conversations — most recently active mail first, newest
attachment first within a conversation. Global arrival order would need an index
over bound rows keyed by time, which does not exist and would cost a stored
field plus a backfill; the source line says which ordering it is instead of
implying the other.

Each conversation is still re-checked here. The ids arrive from a
privacy-applied reader, and an id that has been through one is still only an
id — a foreign or unreadable conversation contributes nothing.
@Israeltheminer Israeltheminer changed the title fix(platform): reach every emailed attachment, not the newest 600 rows fix(platform): scale the attachment listing to the inbox, not the table Aug 21, 2026
"What has come in recently?" was answered by conversation activity, so a fresh
application sorted below an older one whose thread happened to get a reply.
That ordering was not chosen — it fell out of the shape that fixed the listing's
cost.

`fileMetadata` gains `mailReceivedAt`, set alongside `conversationId`, plus
`by_organizationId_and_mailReceivedAt`. Convex has no partial indexes, so a
field present only on bound rows is what makes that index mail-only: a range
above `undefined` is a walk over exactly the attachments that arrived by mail,
in arrival order. The reader reads `limit` rows and stops, so cost no longer
grows with the table or with the inbox reached.

The stamp prefers the mail's own date, threaded from the sync, so importing old
mail sorts by when it was sent rather than when it was imported. It falls back
to the row's creation time, which is within minutes on a live poll.

The binder writes the stamp whenever it is missing, not only on a fresh
binding, so a row bound before the field existed is repaired the next time its
mail is touched. Migration `v0_4_1/05` closes the rest: a quiet conversation is
never revisited, and without a stamp those attachments stay unlistable for good.

`down` clears the field only where it equals the row's creation time — the exact
value `up` writes — so a stamp taken from the mail's own date survives. The
chain suite caught the blanket version, because clearing a stamp the seeded
world already carried did not restore that world.

The field itself needs no migration: a new optional field is data-safe growth,
which `migrations:check` confirms and a snapshot refresh records.
@Israeltheminer Israeltheminer changed the title fix(platform): scale the attachment listing to the inbox, not the table feat(platform): list emailed attachments by arrival, affordably Aug 21, 2026
@Israeltheminer
Israeltheminer merged commit 53515c4 into main Aug 21, 2026
50 of 51 checks passed
@Israeltheminer
Israeltheminer deleted the fix/mail-attachment-listing-scan branch August 21, 2026 17:37
Israeltheminer added a commit that referenced this pull request Sep 3, 2026
Three legs the port left returning nothing, each under a comment saying no
0.5 table existed. All three tables existed.

kind="website" answered []. Migration 0045 created app.websites with every
column 0.4's reader projected, and listWebsites already existed.

kind="mail-attachment" answered {attachments: [], truncated: false}, while
migration 0037 shipped conversation_id and mail_received_at_ms and the email
binder writes both. truncated:false means "the walk reached the end of the
mail index", so the stub was also making a false claim. The new reader gates
every row on conversationAssignmentAllows against the conversation's current
assignment, resolving the caller's role and team ids rather than taking them
as arguments — a listing that skipped that would serve the whole inbox.

Migration 0064 adds the arrival index. The only existing index leads with
conversation_id, so it orders by conversation, which is the shape #3035
recorded as rejected: a by-arrival page reads most of its budget to return
one row, and above the budget reports "most recent" from an arbitrary window.

The task and project legs also lost their zero-hit listing fallback, leaving
listedSource unreachable. Restored in both.

Adds the reachability gate the chat map never had: every internal ref
reachable from the turn now needs a handler, using the walker the sandbox
bridge already had rather than a second copy. That absence is how #3112's
and #3128's missing handlers shipped.
Israeltheminer added a commit that referenced this pull request Sep 3, 2026
Three legs the port left returning nothing, each under a comment saying no
0.5 table existed. All three tables existed.

kind="website" answered []. Migration 0045 created app.websites with every
column 0.4's reader projected, and listWebsites already existed.

kind="mail-attachment" answered {attachments: [], truncated: false}, while
migration 0037 shipped conversation_id and mail_received_at_ms and the email
binder writes both. truncated:false means "the walk reached the end of the
mail index", so the stub was also making a false claim. The new reader gates
every row on conversationAssignmentAllows against the conversation's current
assignment, resolving the caller's role and team ids rather than taking them
as arguments — a listing that skipped that would serve the whole inbox.

Migration 0070 adds the arrival index. The only existing index leads with
conversation_id, so it orders by conversation, which is the shape #3035
recorded as rejected: a by-arrival page reads most of its budget to return
one row, and above the budget reports "most recent" from an arbitrary window.

The task and project legs also lost their zero-hit listing fallback, leaving
listedSource unreachable. Restored in both.

Adds the reachability gate the chat map never had: every internal ref
reachable from the turn now needs a handler, using the walker the sandbox
bridge already had rather than a second copy. That absence is how #3112's
and #3128's missing handlers shipped.
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