Repository navigation
feat(platform): let chat search conversations, under assignment privacy - #3006
Merged
Merged
Conversation
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
force-pushed
the
feat/chat-conversations-leg
branch
from
August 18, 2026 17:54
4acc9c5 to
367ac57
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
organizationIdto an RLS-bypassing internal query, because those tables are org-scope-and-role only. Conversations are not. Assignment privacy is narrower than membership, andresolveAgentReadAccessreturnsallowed: truefor 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:rls_rules.tsdelegates 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.tsas well as the new tests. The RLS rule demonstrably runs this code, not a copy of it.Laziness preserved.
rls_rules.tsdeliberately pays its Better Auth team round-trip lazily (rls_rules.lazy_teams.test.ts). A predicate taking a resolvedSetwould 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 —
organizationIdanduserId. Role and team ids are resolved server-side in the query. AnisAdminflag 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_lastMessageAtnewest-first and stops at 300, followingconvex/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.
resolveAgentReadAccessfalls back to the Better Auth component when nomemberMirrorrow 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
conversationMessageshas noby_organizationIdindex, and used that to justify deferring body search. It actually hasby_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.