Skip to content

feat(platform): let chat search conversations, under assignment privacy - #3006

Merged
Israeltheminer merged 2 commits into
mainfrom
feat/chat-conversations-leg
Aug 18, 2026
Merged

Israeltheminer merged 2 commits into
mainfrom
feat/chat-conversations-leg

Conversation

@Israeltheminer

@Israeltheminer Israeltheminer commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Part of #2992 — the last of the epic's breadth. #3002 has since merged and this branch is rebased onto it, so the diff is now only the conversations leg.

Why this leg could not be copied from its neighbours

Contacts and products safely hand organizationId to an RLS-bypassing internal query, because those tables are org-scope-and-role only. Conversations are not. Assignment privacy is narrower than membership, and resolveAgentReadAccess returns allowed: true for any active member — so the role gate the leg already runs is necessary and nowhere near sufficient. Copying the contacts shape here would have published every organization's entire inbox to every member.

The rule moves somewhere both readers share it

conversationAssignmentAllows (convex/lib/rls/helpers/conversation_assignment.ts) now holds the one definition:

  • admins and owners see everything
  • a conversation with neither an individual nor a team assignee is admin-triage only — an unassigned inbox row is not org-readable
  • otherwise the caller must be the individual assignee, or in the assigned team (the union when both are set)

rls_rules.ts delegates to it instead of keeping its own copy. A second copy is precisely how a reader ends up subtly wider than the rule it is supposed to mirror.

The drift proof is better than a mock. Mutating the shared predicate — making unassigned rows readable, or letting any team pass — fails the existing rls_rules.conversation_scope.test.ts as well as the new tests. The RLS rule demonstrably runs this code, not a copy of it.

Laziness preserved. rls_rules.ts deliberately pays its Better Auth team round-trip lazily (rls_rules.lazy_teams.test.ts). A predicate taking a resolved Set would force that round-trip for every conversation, so the team check is a callback and the caller decides when it fires: the predicate invokes it only for a row whose decision depends on it, so the admin and individual-assignee branches still resolve no teams. There's a test asserting exactly that.

Authority is resolved inside the query

The leg passes identity only — organizationId and userId. Role and team ids are resolved server-side in the query. An isAdmin flag travelling in from an action is one refactor away from being wrong in the direction that leaks, and there's a test asserting no such flag is passed.

Bounded and recency-biased

Conversations carry no text index, so this walks by_org_lastMessageAt newest-first and stops at 300, following convex/chat/search.ts. A filled scan is reported as such — "no matches" and "no matches among what I could reach" are different claims, and only the second is honest here.

Contact names are searched without a read per row: one bounded contacts search resolves the term to a set of ids first, then a conversation matches on subject or correspondent. That path is scope-checked too, so being findable by contact never becomes a second way in — mutation-tested.

Tests

18 new assertions: the predicate's own table, plus a convex-test suite driving the real query path with seeded memberMirror/teamMemberMirror — a plain member sees nothing, an admin sees the unassigned row, an assignee sees only theirs, a team member only their team's, and no cross-org reach.

Mutations, all red: dropping the assignment gate, treating every caller as admin, dropping the org scope from the index walk, letting a contact match bypass scope, resolving teams eagerly, and making unassigned rows readable.

One case deliberately not covered, stated rather than papered over: a user with no membership at all. resolveAgentReadAccess falls back to the Better Auth component when no memberMirror row exists, and that component is not registered in convex-test. That refusal is covered by the access resolver's own tests; this file proves the scope rules only this leg can get wrong.

Plan correction

The plan claimed conversationMessages has no by_organizationId index, and used that to justify deferring body search. It actually has by_organizationId_and_deliveredAt — so a bounded body leg needs no schema change when it's wanted. Body search is still out of scope here, but for scope reasons, not index ones.

Gate: typecheck, oxlint --type-aware, oxfmt, SAST 0 findings, migrations:check (no migration), platform 75,132 tests.

The last leg of the chat epic's breadth, and the one that could not be copied
from its neighbours.

Contacts and products hand `organizationId` to an RLS-bypassing internal query
safely, because those tables are org-scope-and-role only. Conversations are not:
assignment privacy is narrower than membership, and `resolveAgentReadAccess`
returns `allowed: true` for ANY active member — so the role gate the leg already
runs is necessary and nowhere near sufficient. Copying the contacts shape here
would have published every organization's whole inbox to every member.

So the RULE moves somewhere both readers can share it.
`conversationAssignmentAllows` now holds the one definition — admins see all, a
conversation with neither an individual nor a team assignee is admin-triage
only, otherwise the caller must be the assignee or in the assigned team — and
`rls_rules.ts` delegates to it rather than keeping its own copy. A second copy is
how a reader ends up subtly wider than the rule it mirrors.

The team check stays a callback so `rls_rules.ts` keeps paying its Better Auth
round-trip lazily, exactly as `rls_rules.lazy_teams.test.ts` requires; the
predicate calls it only for a row whose decision needs it, so the admin and
individual-assignee branches still resolve no teams.

The leg resolves role and team ids from the caller's identity INSIDE the query.
It is not given an `isAdmin` flag to trust: a flag travelling in from an action
is one refactor away from being wrong in the direction that leaks.

Bounded and recency-biased, following convex/chat/search.ts: conversations carry
no text index, so it walks `by_org_lastMessageAt` newest-first and stops at 300.
A filled scan is reported as such, because "no matches" and "no matches among
what I could reach" are different claims. Contact names are searched without a
read per row — one bounded contacts search resolves the term to a set of ids
first — and that path is scope-checked too, so being findable by correspondent
never becomes a second way in.

Message bodies are not searched yet. Contrary to the note in the plan,
`conversationMessages` does carry `by_organizationId_and_deliveredAt`, so a
bounded body leg needs no schema change when it is wanted.
@Israeltheminer
Israeltheminer force-pushed the feat/chat-conversations-leg branch from 4acc9c5 to 367ac57 Compare August 18, 2026 17:54
@Israeltheminer
Israeltheminer merged commit 1743b13 into main Aug 18, 2026
43 of 45 checks passed
@Israeltheminer
Israeltheminer deleted the feat/chat-conversations-leg branch August 18, 2026 18:25
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