Skip to content

feat(platform): scope a corpus row to its conversation - #3011

Merged
Israeltheminer merged 3 commits into
mainfrom
feat/conversation-scoped-corpus
Aug 19, 2026
Merged

Israeltheminer merged 3 commits into
mainfrom
feat/conversation-scoped-corpus

Conversation

@Israeltheminer

@Israeltheminer Israeltheminer commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Chat cannot find a file that arrived by email. This adds the mechanism that will
let it, and nothing more: no row carries the new scope yet, so no search result
changes.

The problem with stamping a team

An emailed file should be readable by whoever can read its conversation. That
rule is live, not fixed. Admins see all, unassigned mail is admin-triage only,
otherwise the reader is the assignee or the assigned team. Reassigning the mail
has to move its attachments with it.

A team stamped on the corpus row at ingest is wrong the moment somebody reassigns. Every missed rewrite hides a file from its new owner, or leaves it visible to the person who handed it off.

What 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
section. Retrieval then re-asks the conversation:

  • The hub clause gains conversation_id IS NULL. A row with every scope column NULL reads as org-wide. An emailed attachment carries no team and no project, so without this every indexed one would be org-hub.
  • A new disjunct admits conversation rows.
  • filterRetrievableRagFileIds decides each one with
    conversationAssignmentAllows. The same predicate the RLS rule uses, so
    there is one definition of who may read a conversation.
  • fetch.ts gets the same split, because scope-by-set would read a
    conversation row as hub and serve it.

It admits and then re-checks, rather than filtering on a list of the caller's readable conversations. Enumerating those is an unbounded walk under assignment privacy, which is why the conversations leg caps its own scan. The re-check
is bounded by the result limit instead, and it fails closed.

Two things this needed

The scope now carries the asking user. Its other fields are sets, and "the
assignee" is not expressible as one. Absent identity denies, which is why the two
session-keyed sandbox lanes cannot retrieve an emailed attachment. A session
bound to a project or an org-level run has no person to check an assignment
against. There is a test on that, because the tempting fix is to set the flag and
hand every skill run the whole inbox.

The scope's wire shape was declared three times. The resolver's returns, the
re-check's args, the sandbox bridge's returns. Adding two fields broke the third. A closed returns validator throws on the extra field, so the symptom is a query that reads as "no results". Second commit collapses it to
one knowledgeAccessScopeValidator.

Risk

Both search legs now select conversation_id, so a corpus that has not received migration 0008 lacks a column the code reads. corpus.ts already caught that and returned empty, logging "the corpus is not created yet on this database". That points the operator at an empty corpus, not at a database behind the code.

That is reachable on a self-hosted deployment. pool.ts:133 bootstraps the corpus schema only for an organization's own database; the bundled knowledge database gets its migrations from dbmate at container start (services/db/docker-entrypoint.sh:214). Updating the platform image without restarting the knowledge database leaves the column absent, and every private search returns nothing.

Second commit makes a missing column its own branch. It still returns empty rather than failing a turn, and logs a warning naming the remedy. After deploying, check that private_knowledge.documents has conversation_id; if it does not, restart the knowledge database container.

Proof

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

New coverage: the deciding gate (assignee, assigned team, another team,
unassigned, admin, non-member, disabled role, no identity, cross-org, dangling
reference, folder-scoped search, and that the team lookup stays lazy); the SQL
admit clause on both legs; the fetch-path split; the corpus write path; that
every scope column arrives outside the private_knowledge baseline, since a
ledgered database never re-runs it.

20 mutations applied, 20 killed. The 42703 missing-column path had no test at all before this; it now has three, covering both legs and the message. Two assertions were rewritten because a mutation survived them. One was a redundant identity guard no test could tell from dead code. The other was a substring check over the joined migration SQL that also matched the migration's own prose.

Next

3b turns it on: email attachments stop being skipped for RAG, and the binder
passes the conversation id into indexing. The write path is already here.

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.
A corpus whose table exists but lacks a column the running code selects
returned no results and logged "the corpus is not created yet on this
database". That sends the operator looking for an empty corpus when the real
state is a database behind the code.

It matters from this release on, because both search legs now select
`conversation_id`. The bundled knowledge database applies its dbmate
migrations at container start, and `pool.ts` bootstraps the schema only for an
organization's own database, so a platform image updated without restarting
the knowledge database leaves every private search returning nothing.

A missing column is now its own branch: still empty results rather than a
failed turn, but logged as a warning that names the remedy. The `42703` path
had no test; it has three, covering both legs and the message.
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