Skip to content

feat(platform): index an emailed attachment for its conversation - #3012

Merged
Israeltheminer merged 4 commits into
mainfrom
feat/index-email-attachments
Aug 19, 2026
Merged

Israeltheminer merged 4 commits into
mainfrom
feat/index-email-attachments

Conversation

@Israeltheminer

@Israeltheminer Israeltheminer commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Turns on what #3011 built. An emailed attachment is now indexed, and it is
readable by whoever can currently read the conversation it arrived on.

Stacked on #3011 — review that one first; this branch contains its two commits.

Where indexing starts

At binding, not at storage. Attachment bytes are drained one message at a time
inside the fetch, before the conversation is resolved, so at storage time there
is nothing to scope the file to. An unscoped corpus row is an org-wide hub
document, which is the outcome this line of work exists to prevent.

bindFileToConversation is the first moment the conversation is known, so that
is where the row is queued and dispatched.

Why in one transaction

Marking a row 'queued' is a promise to dispatch. A queued row that is neither
dispatched nor parked counts against the per-org RAG cap forever, and three of
them starve every real upload — the defect that made email attachments skip
indexing in the first place (#2995).

Binding is best-effort by design: it must not fail a sync that has already landed
the mail. So the promise is not made across that boundary. The mutation queues
and dispatches together, and a poll that cannot bind leaves the file unindexed
rather than half-enqueued.

Only a row that was never indexed is queued. A re-poll of the same mail must not
re-embed; a failed row belongs to the watchdog; audio indexes through its
transcript; a format with no extractor is not queued at all.

Whether the binding moved and whether the file needs indexing are independent
facts. An attachment bound by #3010 already has a conversation and still has no
ragStatus, so returning early on "binding unchanged" would leave it unindexed
for good — nothing else would ever queue it. Such a row is now queued without
rewriting the link it already had.

Why the stamp is read, not passed

indexFileBlob reads the binding from Convex truth rather than taking it as an
argument. Nothing in the dispatch chain carries a conversation id, so a watchdog
retry, a promotion from parked, and a later slice of a large file all pick up the
current binding. A reassignment cannot leave the corpus disagreeing with the
conversation.

An invariant this made visible

Several callers gate on "indexable and not audio". Those two sets turn out to be
disjoint, so the audio half is unreachable — which makes it indistinguishable
from dead code. Rather than delete a guard that may matter later, the extension
set is now exported and a test asserts the disjointness. Add an audio extension
to the indexable set and that test fails, so routing a recording into the text
pipeline becomes a decision instead of an accident.

Proof

Full platform gate green: 75,207 unit tests, type-aware lint, typecheck,
migrations:check.

New coverage: which rows the binder queues and which it only binds (already
completed, failed, running, queued, audio, no-extractor, trashed); that the
dispatch happens with the queue write; that the binder keeps going past a failure
and reports honest counts; that a part with no stored bytes is skipped; the
index-time stamp for a bound blob, an unbound one, and that it is read per
invocation; and the binding query's tenant boundary, driven through convex-test
against the real schema.

14 mutations applied, 14 killed.

Known gap

This converges any attachment on mail a poll touches. It does not reach mail a
poll will never touch again — an attachment stored by an earlier sync, on a
conversation that has since gone quiet, is not revisited, so it stays unindexed.
Draining that backlog needs its own bounded pass and is not part of this change.

The corpus can now hold a row whose reader is decided by a conversation's
current assignment. Nothing sets the new scope yet, so no search result
changes; this is the mechanism an emailed attachment will use.

Why a third scope dimension rather than a stamped team. Conversation
visibility is live: admins see all, unassigned mail is admin-triage only,
otherwise the reader is the assignee or the assigned team. Reassigning the
mail must move its attachments with it. A team stamped at ingest would be
wrong the moment somebody reassigned, and every missed rewrite would hide a
file from its new owner or leave it with the person who handed it off.

So the column carries the question, not the answer. Migration 0008 adds
`conversation_id` with a partial index, following 0006's shape and its
reasoning for an empty down. The hub clause gains `conversation_id IS NULL`,
because a row with every scope column NULL reads as org-wide and an emailed
attachment carries no team and no project. A new disjunct admits
conversation rows, and `filterRetrievableRagFileIds` decides each one with
`conversationAssignmentAllows` — the same predicate the RLS rule uses.

Admitting and re-checking, rather than filtering by a list of readable
conversations: enumerating them is an unbounded walk under assignment
privacy, which is why the chat conversations leg caps its own scan. The
re-check is bounded by the result limit instead, and it fails closed, so a
caller who supplied no identity is served nothing.

The scope now carries the asking user, because the other fields are sets and
"the assignee" cannot be expressed as one.
Adding two fields to the retrieval scope broke a query that had its own copy
of the shape. A closed `v.object()` returns validator throws on an unexpected
field, and a throwing query reads to its caller as "no results" — so a
widened scope presents as knowledge search having gone quiet.

The shape was written out three times: the resolver's returns, the
re-check's args, and the sandbox bridge's returns. Now
`knowledgeAccessScopeValidator` in `documents/access.ts` sits beside the type
it validates, and the re-check spreads its fields to add the chat lane's own
`threadIds`.

Also records why the two session-keyed sandbox lanes carry no identity and so
cannot retrieve an emailed attachment, with a test on it: a bound session has
no person to check an assignment against, and setting the flag there would
hand every skill run the whole inbox.
Chat can now find a file that arrived by email. Binding an attachment to its
conversation also queues it for indexing, and the corpus row is stamped with
that conversation, so the assignment re-check decides who may read it.

Indexing starts at binding time, not at storage time. Attachment bytes are
drained per message before the conversation is resolved, so at storage time
there is nothing to scope the file to — and an unscoped corpus row is an
org-wide hub document, which is what this whole line of work exists to avoid.

The queue write and the dispatch happen in one transaction. Marking a row
`'queued'` is a promise to dispatch, and a queued row that is neither
dispatched nor parked holds an org RAG slot forever — three of them starve
every real upload. That is the defect that made email attachments skip
indexing, so the promise and the dispatch are made together rather than
across the best-effort binding boundary, which is allowed to fail.

Only a row that was never indexed is queued: a re-poll must not re-embed, and
a failed row belongs to the watchdog. Audio is left to its transcript, and a
format with no extractor is not queued at all.

The stamp is read from Convex truth inside the indexing action rather than
carried along the dispatch chain, so a retry, a promotion from parked, or a
later slice of a large file all pick up the current binding. A reassignment
therefore cannot leave the corpus disagreeing with the conversation.

Also exports the RAG-indexable extension set so the "indexable and not audio"
condition several callers share can assert its own premise: that half is
unreachable while the two sets are disjoint, and a test now fails if an audio
extension joins the indexable ones.
An attachment bound before binding started indexing has a conversation and no
`ragStatus`, and the binder returned early on "binding unchanged" — so it
would never be queued, and nothing else would ever queue it.

Whether the binding moved and whether the file needs indexing are independent
facts, and the handler now answers them separately. An already-bound row is
queued without rewriting the link it already had, and reports `'queued'` so
the sync's log counts the indexing work without claiming a new binding.
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