Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
perf(email): cut CRM domain-scoped thread query from ~5.6s to ~1.5s (#4839)
Evan Hutnikcc4751651d59
diff --git a/.sqlx/query-36f475918166109c36d93d8942503e4016b5fbf0eb92a2f49eb22831a6e13818.json b/.sqlx/query-36f475918166109c36d93d8942503e4016b5fbf0eb92a2f49eb22831a6e13818.jsondeleted file mode 100644index 3ba3940499..0000000000--- a/.sqlx/query-36f475918166109c36d93d8942503e4016b5fbf0eb92a2f49eb22831a6e13818.json+++ /dev/null@@ -1,29 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n SELECT c.id, LOWER(c.email_address) AS \"email_lower!\"\n FROM email_contacts c\n JOIN email_links el ON el.id = c.link_id\n JOIN team_user tu ON tu.user_id = el.macro_id\n WHERE tu.team_id = $1\n AND el.is_primary\n AND LOWER(c.email_address) = ANY($2)\n ",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "id",- "type_info": "Uuid"- },- {- "ordinal": 1,- "name": "email_lower!",- "type_info": "Text"- }- ],- "parameters": {- "Left": [- "Uuid",- "TextArray"- ]- },- "nullable": [- false,- null- ]- },- "hash": "36f475918166109c36d93d8942503e4016b5fbf0eb92a2f49eb22831a6e13818"-}diff --git a/.sqlx/query-59494f54fcb77c765e88e7883fcd6171b524642e5c1831885f7b5b885b839dbf.json b/.sqlx/query-59494f54fcb77c765e88e7883fcd6171b524642e5c1831885f7b5b885b839dbf.jsonnew file mode 100644index 0000000000..55e3789abc--- /dev/null+++ b/.sqlx/query-59494f54fcb77c765e88e7883fcd6171b524642e5c1831885f7b5b885b839dbf.json@@ -0,0 +1,22 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT el.id\n FROM email_links el\n JOIN team_user tu ON tu.user_id = el.macro_id\n WHERE tu.team_id = $1 AND el.is_primary\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id",+ "type_info": "Uuid"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": [+ false+ ]+ },+ "hash": "59494f54fcb77c765e88e7883fcd6171b524642e5c1831885f7b5b885b839dbf"+}diff --git a/.sqlx/query-c54744aa091017a10ceba8dc5ebc387f9c4e52b7135d491724fad116a773ab56.json b/.sqlx/query-c54744aa091017a10ceba8dc5ebc387f9c4e52b7135d491724fad116a773ab56.jsondeleted file mode 100644index 28da2d4dd6..0000000000--- a/.sqlx/query-c54744aa091017a10ceba8dc5ebc387f9c4e52b7135d491724fad116a773ab56.json+++ /dev/null@@ -1,22 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n SELECT l.id\n FROM email_labels l\n JOIN email_links el ON el.id = l.link_id\n JOIN team_user tu ON tu.user_id = el.macro_id\n WHERE l.name = 'TRASH' AND tu.team_id = $1\n AND el.is_primary\n ",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "id",- "type_info": "Uuid"- }- ],- "parameters": {- "Left": [- "Uuid"- ]- },- "nullable": [- false- ]- },- "hash": "c54744aa091017a10ceba8dc5ebc387f9c4e52b7135d491724fad116a773ab56"-}diff --git a/crates/email/src/outbound/email_pg_repo/dynamic/filters.rs b/crates/email/src/outbound/email_pg_repo/dynamic/filters.rsindex 9c00b1a369..7151236682 100644--- a/crates/email/src/outbound/email_pg_repo/dynamic/filters.rs+++ b/crates/email/src/outbound/email_pg_repo/dynamic/filters.rs@@ -342,7 +342,7 @@ pub(super) fn wants_address_pushdown(ast: &Expr<EmailLiteral>) -> bool {/// Emits the `AND t.id IN (SELECT thread_id FROM matching_threads)` fragment/// pushed into the candidate-thread WHERE. The CTE itself is built by-/// `build_matching_threads_cte_body` and pasted into the top-level `WITH …`+/// `build_matching_threads_ctes` and pasted into the top-level `WITH …`/// chain. Returns empty when there are no pure-address conjuncts to push.pub(super) fn build_thread_address_filter(ast: &Expr<EmailLiteral>) -> SqlFragment {if !wants_address_pushdown(ast) {@@ -426,68 +426,111 @@ fn flatten_or_tree_of_address_literals(}}-/// Builds one `SELECT m.thread_id FROM …` UNION branch for a single-/// positive address literal. Returns `None` for unresolved Complete emails-/// — the branch can't match anything, so we drop it from the UNION rather-/// than emitting a `WHERE FALSE` branch.-fn build_union_branch(- kind: AddressKind,- email: &Email,- resolved: &ResolvedFilters,- link_scope: Option<&[Uuid]>,-) -> Option<SqlFragment> {- let trash = build_trash_check(resolved);- match (resolved.contact_ids_for(email), email) {- (Some(contact_ids), _) => {- let mut f = match kind {- AddressKind::Sender => {- let mut f = SqlFragment::raw(- "SELECT m.thread_id FROM email_messages m WHERE m.from_contact_id = ANY(",- );- f.extend(SqlFragment::bind_uuid_array(contact_ids.to_vec()));- f.push_raw(")");- f- }- _ => {- let recipient_type = kind.recipient_type_sql().expect("non-sender kind");- let mut f = SqlFragment::raw(- "SELECT m.thread_id FROM email_message_recipients mr \- JOIN email_messages m ON m.id = mr.message_id \- WHERE mr.contact_id = ANY(",- );- f.extend(SqlFragment::bind_uuid_array(contact_ids.to_vec()));- f.push_raw(format!(") AND mr.recipient_type = '{recipient_type}'"));- f- }- };- f.push_raw(" AND ");- f.extend(trash);- Some(f)+/// Which roles (sender / TO / CC / BCC) one address predicate was requested+/// in across the OR-tree. Merging roles lets the CTE emit a single recipient+/// branch per address instead of one per role.+#[derive(Default)]+struct AddressKinds {+ sender: bool,+ to: bool,+ cc: bool,+ bcc: bool,+}++impl AddressKinds {+ fn add(&mut self, kind: AddressKind) {+ match kind {+ AddressKind::Sender => self.sender = true,+ AddressKind::Recipient => self.to = true,+ AddressKind::Cc => self.cc = true,+ AddressKind::Bcc => self.bcc = true,}- (None, Email::Complete(_)) => None,- (None, Email::Partial(s)) => {- let pattern = format!("%{}%", escape_like_pattern(s));- let mut f =- build_union_branch_text_match(kind, "c.email_address ILIKE ", pattern, link_scope);- f.push_raw(" AND ");- f.extend(trash);- Some(f)+ }++ fn any_recipient(&self) -> bool {+ self.to || self.cc || self.bcc+ }++ /// ` AND mr.recipient_type …` for the requested recipient roles. Empty+ /// when all three are present — TO/CC/BCC is the whole enum, so the+ /// filter would be a no-op. Only meaningful when `any_recipient()`.+ fn recipient_type_sql(&self) -> String {+ let mut types = Vec::new();+ if self.to {+ types.push("'TO'");}- (None, Email::Domain(s)) => {- let domain = s.to_ascii_lowercase();- let mut f = build_union_branch_text_match(- kind,- "LOWER(SPLIT_PART(c.email_address, '@', 2)) = ",- domain,- link_scope,- );- f.push_raw(" AND ");- f.extend(trash);- Some(f)+ if self.cc {+ types.push("'CC'");+ }+ if self.bcc {+ types.push("'BCC'");+ }+ match types.len() {+ 3 => String::new(),+ 1 => format!(" AND mr.recipient_type = {}", types[0]),+ _ => format!(" AND mr.recipient_type IN ({})", types.join(", ")),}}}+/// Identity of an address predicate for grouping OR'd literals: two literals+/// with equal keys match the same contact rows, so their branches can share+/// contact-resolution work.+#[derive(PartialEq)]+enum AddressGroupKey<'a> {+ /// Complete email resolved to contact ids.+ Contacts(&'a [Uuid]),+ /// Partial text match (source string of the ILIKE pattern).+ Partial(&'a str),+ /// Bare domain, lowercased.+ Domain(String),+}++/// Returns `None` for unresolved Complete emails — they can't match+/// anything, so the literal is dropped rather than emitting a `WHERE FALSE`+/// branch.+fn address_group_key<'a>(+ email: &'a Email,+ resolved: &'a ResolvedFilters,+) -> Option<AddressGroupKey<'a>> {+ match (resolved.contact_ids_for(email), email) {+ (Some(ids), _) => Some(AddressGroupKey::Contacts(ids)),+ (None, Email::Complete(_)) => None,+ (None, Email::Partial(s)) => Some(AddressGroupKey::Partial(s)),+ (None, Email::Domain(s)) => Some(AddressGroupKey::Domain(s.to_ascii_lowercase())),+ }+}++/// Sender branch over resolved contact ids — probes+/// `idx_email_messages_from_contact_id`.+fn contacts_sender_branch(contact_ids: &[Uuid], resolved: &ResolvedFilters) -> SqlFragment {+ let mut f =+ SqlFragment::raw("SELECT m.thread_id FROM email_messages m WHERE m.from_contact_id = ANY(");+ f.extend(SqlFragment::bind_uuid_array(contact_ids.to_vec()));+ f.push_raw(") AND ");+ f.extend(build_trash_check(resolved));+ f+}++/// One recipient branch covering every requested recipient role — probes+/// `idx_email_message_recipients_contact_id`.+fn contacts_recipient_branch(+ contact_ids: &[Uuid],+ kinds: &AddressKinds,+ resolved: &ResolvedFilters,+) -> SqlFragment {+ let mut f = SqlFragment::raw(+ "SELECT m.thread_id FROM email_message_recipients mr \+ JOIN email_messages m ON m.id = mr.message_id \+ WHERE mr.contact_id = ANY(",+ );+ f.extend(SqlFragment::bind_uuid_array(contact_ids.to_vec()));+ f.push_raw(format!("){}", kinds.recipient_type_sql()));+ f.push_raw(" AND ");+ f.extend(build_trash_check(resolved));+ f+}+/// `AND <alias>.link_id = ANY($links)` when a link scope applies, empty otherwise.fn link_scope_fragment(alias: &str, link_scope: Option<&[Uuid]>) -> SqlFragment {match link_scope {@@ -501,60 +544,117 @@ fn link_scope_fragment(alias: &str, link_scope: Option<&[Uuid]>) -> SqlFragment}}-/// Shared shape for a union-branch SELECT that joins through-/// `email_contacts` with a single bound predicate against `c.*`.+/// Body of a hoisted `matching_contacts_N` CTE: the contact ids matching one+/// text predicate (ILIKE / domain equality), computed once and shared by the+/// sender/recipient branches that reference it. Materialized so Postgres+/// can't inline it back into each branch as a separate `email_contacts` scan.///-/// Text matches (ILIKE / domain equality) are not contact-id-scoped like the-/// Complete branches are, so without `link_scope` they scan matches across-/// every mailbox in the table before the candidate stage intersects with the-/// caller's threads. Scoping both `c` and `m` by the caller's links bounds-/// the CTE to the caller's own mail.-fn build_union_branch_text_match(- kind: AddressKind,+/// Text matches are not contact-id-scoped like the Complete branches are, so+/// without `link_scope` they match contacts across every mailbox in the+/// table before the candidate stage intersects with the caller's threads.+/// Scoping `c` here (and `m` in the branches) bounds the work to the+/// caller's own mail.+fn matching_contacts_cte_body(predicate_prefix: &str,bind_value: String,link_scope: Option<&[Uuid]>,) -> SqlFragment {- match kind {- AddressKind::Sender => {- let mut f = SqlFragment::raw(format!(- "SELECT m.thread_id FROM email_contacts c \- JOIN email_messages m ON m.from_contact_id = c.id \- WHERE {predicate_prefix}"- ));- f.extend(SqlFragment::bind_string(bind_value));- f.extend(link_scope_fragment("c", link_scope));- f.extend(link_scope_fragment("m", link_scope));- f- }- _ => {- let recipient_type = kind.recipient_type_sql().expect("non-sender kind");- let mut f = SqlFragment::raw(format!(- "SELECT m.thread_id FROM email_contacts c \- JOIN email_message_recipients mr ON mr.contact_id = c.id \- JOIN email_messages m ON m.id = mr.message_id \- WHERE {predicate_prefix}"- ));- f.extend(SqlFragment::bind_string(bind_value));- f.push_raw(format!(" AND mr.recipient_type = '{recipient_type}'"));- f.extend(link_scope_fragment("c", link_scope));- f.extend(link_scope_fragment("m", link_scope));- f- }+ let mut f = SqlFragment::raw(format!(+ "SELECT c.id FROM email_contacts c WHERE {predicate_prefix}"+ ));+ f.extend(SqlFragment::bind_string(bind_value));+ f.extend(link_scope_fragment("c", link_scope));+ f+}++/// Sender branch consuming a hoisted `matching_contacts_N` CTE.+fn text_sender_branch(+ cte_name: &str,+ resolved: &ResolvedFilters,+ link_scope: Option<&[Uuid]>,+) -> SqlFragment {+ let mut f = SqlFragment::raw(format!(+ "SELECT m.thread_id FROM {cte_name} c \+ JOIN email_messages m ON m.from_contact_id = c.id \+ WHERE "+ ));+ f.extend(build_trash_check(resolved));+ f.extend(link_scope_fragment("m", link_scope));+ f+}++/// Merged recipient branch consuming a hoisted `matching_contacts_N` CTE.+fn text_recipient_branch(+ cte_name: &str,+ kinds: &AddressKinds,+ resolved: &ResolvedFilters,+ link_scope: Option<&[Uuid]>,+) -> SqlFragment {+ let mut f = SqlFragment::raw(format!(+ "SELECT m.thread_id FROM {cte_name} c \+ JOIN email_message_recipients mr ON mr.contact_id = c.id \+ JOIN email_messages m ON m.id = mr.message_id \+ WHERE "+ ));+ f.extend(build_trash_check(resolved));+ f.push_raw(kinds.recipient_type_sql());+ f.extend(link_scope_fragment("m", link_scope));+ f+}++/// Emits one text-matched group: a hoisted contacts CTE plus the branches+/// that consume it.+fn push_text_group(+ predicate_prefix: &str,+ bind_value: String,+ kinds: &AddressKinds,+ resolved: &ResolvedFilters,+ link_scope: Option<&[Uuid]>,+ contact_ctes: &mut Vec<(String, SqlFragment)>,+ branches: &mut Vec<SqlFragment>,+) {+ let cte_name = format!("matching_contacts_{}", contact_ctes.len());+ contact_ctes.push((+ cte_name.clone(),+ matching_contacts_cte_body(predicate_prefix, bind_value, link_scope),+ ));+ if kinds.sender {+ branches.push(text_sender_branch(&cte_name, resolved, link_scope));}+ if kinds.any_recipient() {+ branches.push(text_recipient_branch(+ &cte_name, kinds, resolved, link_scope,+ ));+ }+}++/// The `matching_threads` CTE plus any hoisted contact-lookup CTEs it+/// depends on. `build_query` emits `contact_ctes` (in order) before+/// `matching_threads` in the `WITH` chain.+pub(super) struct MatchingThreadsCtes {+ /// Hoisted `matching_contacts_N` CTEs as `(name, body)` — one per+ /// distinct text-matched (Partial/Domain) address predicate.+ pub(super) contact_ctes: Vec<(String, SqlFragment)>,+ /// Body of the `matching_threads` CTE itself.+ pub(super) body: SqlFragment,}-/// Builds the body of the `matching_threads` CTE — i.e., everything-/// between `MATERIALIZED (` and `)`. Two shapes:+/// Builds the `matching_threads` CTE (and any hoisted contact CTEs it+/// needs). Two shapes:////// 1. **UNION-of-branches** (preferred): when the candidate filter is a/// single conjunct that's a flat OR-tree of positive single-address-/// literals (e.g. `Sender(X) OR Cc(X) OR Bcc(X) OR Recipient(X)`), each-/// literal becomes its own UNION branch. Each branch is index-driven-/// via `idx_email_messages_from_contact_id` /-/// `idx_email_message_recipients_contact_id`, so total work is-/// proportional to the contact's actual mention count rather than-/// mailbox size.+/// literals (e.g. `Sender(X) OR Cc(X) OR Bcc(X) OR Recipient(X)`),+/// literals are grouped by address predicate and each group becomes at+/// most two UNION branches: a sender branch and one merged recipient+/// branch covering every requested recipient role (with no+/// `recipient_type` filter at all when all three roles are present).+/// Complete-email branches are index-driven via+/// `idx_email_messages_from_contact_id` /+/// `idx_email_message_recipients_contact_id`; Partial/Domain predicates+/// resolve contacts once in a hoisted `matching_contacts_N` CTE (riding+/// the trigram / domain expression index) that both branches consume,+/// instead of re-scanning `email_contacts` per branch./// 2. **Combined predicate**: for everything else (multiple AND conjuncts,/// NOT inside a conjunct, mixed nested operators) we emit a single/// `SELECT DISTINCT m.thread_id FROM email_messages m WHERE …` whose@@ -566,14 +666,15 @@ fn build_union_branch_text_match(////// `link_scope` restricts the CTE's contact/message scans to the given/// links. Only pass it when every candidate thread is known to belong to-/// those links (owned-only, non-team queries) — shared and team candidate-/// selects include threads from other users' links, which the scoped CTE-/// would wrongly filter out.-pub(super) fn build_matching_threads_cte_body(+/// those links: the caller's own links (owned-only queries) or the team's+/// primary links (team-scoped queries). Shared and project candidate+/// selects include threads from arbitrary links, which a scoped CTE would+/// wrongly filter out.+pub(super) fn build_matching_threads_ctes(ast: &Expr<EmailLiteral>,resolved: &ResolvedFilters,link_scope: Option<&[Uuid]>,-) -> Option<SqlFragment> {+) -> Option<MatchingThreadsCtes> {let conjuncts = extract_address_only_conjuncts(ast);if conjuncts.is_empty() {return None;@@ -582,10 +683,62 @@ pub(super) fn build_matching_threads_cte_body(if conjuncts.len() == 1&& let Some(literals) = flatten_or_tree_of_address_literals(conjuncts[0]){- let branches: Vec<SqlFragment> = literals- .into_iter()- .filter_map(|(k, e)| build_union_branch(k, e, resolved, link_scope))- .collect();+ // Group literals by address predicate so one address requested in+ // several roles (the common "this address anywhere" case) shares+ // branches and contact-resolution work.+ let mut groups: Vec<(AddressGroupKey, AddressKinds)> = Vec::new();+ for (kind, email) in literals {+ let Some(key) = address_group_key(email, resolved) else {+ continue;+ };+ match groups.iter_mut().find(|(k, _)| *k == key) {+ Some((_, kinds)) => kinds.add(kind),+ None => {+ let mut kinds = AddressKinds::default();+ kinds.add(kind);+ groups.push((key, kinds));+ }+ }+ }++ let mut contact_ctes: Vec<(String, SqlFragment)> = Vec::new();+ let mut branches: Vec<SqlFragment> = Vec::new();+ for (key, kinds) in &groups {+ match key {+ AddressGroupKey::Contacts(ids) => {+ if kinds.sender {+ branches.push(contacts_sender_branch(ids, resolved));+ }+ if kinds.any_recipient() {+ branches.push(contacts_recipient_branch(ids, kinds, resolved));+ }+ }+ AddressGroupKey::Partial(s) => {+ let pattern = format!("%{}%", escape_like_pattern(s));+ push_text_group(+ "c.email_address ILIKE ",+ pattern,+ kinds,+ resolved,+ link_scope,+ &mut contact_ctes,+ &mut branches,+ );+ }+ AddressGroupKey::Domain(domain) => {+ push_text_group(+ "LOWER(SPLIT_PART(c.email_address, '@', 2)) = ",+ domain.clone(),+ kinds,+ resolved,+ link_scope,+ &mut contact_ctes,+ &mut branches,+ );+ }+ }+ }+if !branches.is_empty() {let mut iter = branches.into_iter();let mut f = iter.next().expect("non-empty checked above");@@ -593,13 +746,17 @@ pub(super) fn build_matching_threads_cte_body(f.push_raw("\n UNION\n ");f.extend(branch);}- return Some(f);+ return Some(MatchingThreadsCtes {+ contact_ctes,+ body: f,+ });}- // All branches were unresolved Complete emails — emit a no-rows+ // All literals were unresolved Complete emails — emit a no-rows// form so the JOIN against matching_threads is empty.- return Some(SqlFragment::raw(- "SELECT NULL::uuid AS thread_id WHERE FALSE",- ));+ return Some(MatchingThreadsCtes {+ contact_ctes: Vec::new(),+ body: SqlFragment::raw("SELECT NULL::uuid AS thread_id WHERE FALSE"),+ });}// Combined-predicate fallback: AND all conjuncts and emit one subquery.@@ -614,7 +771,48 @@ pub(super) fn build_matching_threads_cte_body(f.push_raw(" AND ");f.extend(predicate);f.extend(link_scope_fragment("m", link_scope));- Some(f)+ Some(MatchingThreadsCtes {+ contact_ctes: Vec::new(),+ body: f,+ })+}++#[cfg(test)]+impl MatchingThreadsCtes {+ /// Debug SQL over the hoisted CTEs plus the `matching_threads` body, in+ /// emission order. Bind numbering restarts per fragment.+ pub(super) fn to_debug_sql(&self) -> String {+ let mut out = String::new();+ for (name, body) in &self.contact_ctes {+ out.push_str(name);+ out.push_str(" AS MATERIALIZED (");+ out.push_str(&body.to_debug_sql());+ out.push_str(")\n");+ }+ out.push_str(&self.body.to_debug_sql());+ out+ }++ pub(super) fn has_bind_string(&self, expected: &str) -> bool {+ self.contact_ctes+ .iter()+ .any(|(_, b)| b.has_bind_string(expected))+ || self.body.has_bind_string(expected)+ }++ pub(super) fn has_bind_uuid(&self, expected: &Uuid) -> bool {+ self.contact_ctes+ .iter()+ .any(|(_, b)| b.has_bind_uuid(expected))+ || self.body.has_bind_uuid(expected)+ }++ pub(super) fn has_no_raw_containing(&self, needle: &str) -> bool {+ self.contact_ctes+ .iter()+ .all(|(_, b)| b.has_no_raw_containing(needle))+ && self.body.has_no_raw_containing(needle)+ }}/// Builds thread-level SQL WHERE conditions. Message-level literals map to TRUE.diff --git a/crates/email/src/outbound/email_pg_repo/dynamic/query.rs b/crates/email/src/outbound/email_pg_repo/dynamic/query.rsindex 23a07da1a5..786b49c62c 100644--- a/crates/email/src/outbound/email_pg_repo/dynamic/query.rs+++ b/crates/email/src/outbound/email_pg_repo/dynamic/query.rs@@ -234,7 +234,20 @@ fn push_thread_candidate_select(// mailboxes, whose address differs from the owner's macro_id// email) are excluded. The receipt has already been validated// upstream, so the team_id is trusted here.+ //+ // resolve_filters pre-resolves the team's primary link ids: the+ // ANY probe gives the planner a tight static link set, and the+ // membership subquery revalidates it at execution time so a+ // member or link removed between resolve_filters and this query+ // can't leak threads (same belt-and-suspenders rationale as the+ // crm_enabled EXISTS below). The subquery alone remains as a+ // fallback for resolutions that didn't carry team links.Some(team_id) => {+ if let Some(team_links) = params.resolved.team_link_ids() {+ builder.push("t.link_id = ANY(");+ builder.push_bind(team_links.to_vec());+ builder.push(") AND ");+ }builder.push(r#"t.link_id IN (SELECT el.id@@ -409,33 +422,50 @@ fn build_query(// When the candidate stage pushes a full per-message EXISTS (view-level// message filter), it already enforces "thread has a message the lateral// will surface", so the address-only `matching_threads` CTE is redundant.- let matching_threads_body = if wants_message_exists_pushdown(view) {+ let matching_threads = if wants_message_exists_pushdown(view) {None} else {- // Owned-only, non-team queries can scope the CTE's contact/message- // scans to the caller's links; shared/team/project candidates- // include threads from other links, so scoping there would drop rows.+ // Owned-only queries can scope the CTE's contact/message scans to+ // the links the candidate WHERE will accept: the caller's own links,+ // or every team-member primary link when team-scoped (a thread's+ // messages/contacts share its link_id, so this can't drop rows).+ // Shared/project candidates include threads from arbitrary links,+ // so any scope there would.let link_scope = (matches!(params.shared, SharedEmailFilter::Exclude)- && params.team_id.is_none()&& params.project_scope.is_none())- .then_some(params.link_ids.as_slice());- build_matching_threads_cte_body(email_filter, ¶ms.resolved, link_scope)+ .then(|| match params.team_id {+ Some(_) => params.resolved.team_link_ids(),+ None => Some(params.link_ids.as_slice()),+ })+ .flatten();+ build_matching_threads_ctes(email_filter, ¶ms.resolved, link_scope)};let mut builder = sqlx::QueryBuilder::new("");- if needs_shared_cte || matching_threads_body.is_some() {+ if needs_shared_cte || matching_threads.is_some() {builder.push("\n WITH ");let mut needs_comma = false;if needs_shared_cte {push_shared_cte(&mut builder, ¶ms);needs_comma = true;}- if let Some(body) = matching_threads_body {+ if let Some(ctes) = matching_threads {+ // Hoisted contact-lookup CTEs first — matching_threads' branches+ // reference them by name.+ for (name, body) in ctes.contact_ctes {+ if needs_comma {+ builder.push(",\n ");+ }+ builder.push(format!("{name} AS MATERIALIZED (\n "));+ body.push_into(&mut builder);+ builder.push("\n )");+ needs_comma = true;+ }if needs_comma {builder.push(",\n ");}builder.push("matching_threads AS MATERIALIZED (\n ");- body.push_into(&mut builder);+ ctes.body.push_into(&mut builder);builder.push("\n )");}builder.push("\n ");@@ -714,6 +744,21 @@ pub(super) fn debug_build_query_sql_team_scoped()}+#[cfg(test)]+pub(super) fn debug_build_query_sql_team_scoped_with_resolved(+ view: &PreviewView,+ email_filter: &Expr<EmailLiteral>,+ resolved: ResolvedFilters,+) -> String {+ debug_build_query_sql_inner(+ view,+ email_filter,+ resolved,+ Some(Uuid::nil()),+ SimpleSortMethod::UpdatedAt,+ )+}+#[cfg(test)]pub(super) fn debug_build_query_sql_multi_link(view: &PreviewView,diff --git a/crates/email/src/outbound/email_pg_repo/dynamic/resolve.rs b/crates/email/src/outbound/email_pg_repo/dynamic/resolve.rsindex 569f55d173..b95c47c29c 100644--- a/crates/email/src/outbound/email_pg_repo/dynamic/resolve.rs+++ b/crates/email/src/outbound/email_pg_repo/dynamic/resolve.rs@@ -22,6 +22,11 @@ pub(super) struct ResolvedFilters {/// All TRASH label ids in scope. One id per link with a TRASH label./// Empty when no scope-relevant link has a TRASH label (rare).trash_label_ids: Vec<Uuid>,+ /// Every primary `email_links.id` owned by a member of the query's team.+ /// `Some` only for team-scoped queries. Doubles as the candidate-stage+ /// `t.link_id = ANY(…)` set and (for non-shared, non-project queries)+ /// the `matching_threads` link scope.+ team_link_ids: Option<Vec<Uuid>>,}impl ResolvedFilters {@@ -45,6 +50,11 @@ impl ResolvedFilters {&self.trash_label_ids}+ /// The team's primary link ids, when this resolution was team-scoped.+ pub(super) fn team_link_ids(&self) -> Option<&[Uuid]> {+ self.team_link_ids.as_deref()+ }+/// True iff at least one Complete email address in the AST has any/// matching contact in the resolved scope. Used by `fold_unresolved`.fn has_contact_for(&self, lowered_email: &str) -> bool {@@ -59,9 +69,16 @@ impl ResolvedFilters {Self {contact_ids: HashMap::new(),trash_label_ids: Vec::new(),+ team_link_ids: None,}}+ #[cfg(test)]+ pub(super) fn with_team_links(mut self, ids: Vec<Uuid>) -> Self {+ self.team_link_ids = Some(ids);+ self+ }+#[cfg(test)]pub(super) fn with_contact(mut self, lowered_email: impl Into<String>, id: Uuid) -> Self {self.contact_ids@@ -181,84 +198,62 @@ pub(super) async fn resolve_filters(team_id: Option<Uuid>,ast: &Expr<EmailLiteral>,) -> Result<ResolvedFilters, sqlx::Error> {- let trash_label_ids: Vec<Uuid> = match team_id {- None => {- sqlx::query_scalar!(- r#"- SELECT id- FROM email_labels- WHERE link_id = ANY($1) AND name = 'TRASH'- "#,- link_ids,- )- .fetch_all(pool)- .await?- }- Some(team_id) => {+ // Team scope resolves to a concrete link-id set once, so every+ // downstream lookup (and the main query's candidate WHERE / CTE link+ // scope) is a direct `link_id = ANY($ids)` instead of re-joining+ // email_links/team_user per query.+ let team_link_ids: Option<Vec<Uuid>> = match team_id {+ None => None,+ Some(team_id) => Some(sqlx::query_scalar!(r#"- SELECT l.id- FROM email_labels l- JOIN email_links el ON el.id = l.link_id+ SELECT el.id+ FROM email_links elJOIN team_user tu ON tu.user_id = el.macro_id- WHERE l.name = 'TRASH' AND tu.team_id = $1- AND el.is_primary+ WHERE tu.team_id = $1 AND el.is_primary"#,team_id,).fetch_all(pool)- .await?- }+ .await?,+ ),};+ let scope_link_ids: &[Uuid] = team_link_ids.as_deref().unwrap_or(link_ids);++ let trash_label_ids: Vec<Uuid> = sqlx::query_scalar!(+ r#"+ SELECT id+ FROM email_labels+ WHERE link_id = ANY($1) AND name = 'TRASH'+ "#,+ scope_link_ids,+ )+ .fetch_all(pool)+ .await?;let emails = collect_complete_emails(ast);let mut contact_ids: HashMap<String, Vec<Uuid>> = HashMap::new();if !emails.is_empty() {- // Project each arm's anonymous `Record` into a common `(Uuid, String)`- // shape — `sqlx::query!` generates a distinct struct per call site, so- // the `match` arms can't share a return type otherwise.- let rows: Vec<(Uuid, String)> = match team_id {- None => sqlx::query!(- r#"+ let rows = sqlx::query!(+ r#"SELECT id, LOWER(email_address) AS "email_lower!"FROM email_contactsWHERE link_id = ANY($1) AND LOWER(email_address) = ANY($2)"#,- link_ids,- &emails,- )- .fetch_all(pool)- .await?- .into_iter()- .map(|r| (r.id, r.email_lower))- .collect(),- Some(team_id) => sqlx::query!(- r#"- SELECT c.id, LOWER(c.email_address) AS "email_lower!"- FROM email_contacts c- JOIN email_links el ON el.id = c.link_id- JOIN team_user tu ON tu.user_id = el.macro_id- WHERE tu.team_id = $1- AND el.is_primary- AND LOWER(c.email_address) = ANY($2)- "#,- team_id,- &emails,- )- .fetch_all(pool)- .await?- .into_iter()- .map(|r| (r.id, r.email_lower))- .collect(),- };+ scope_link_ids,+ &emails,+ )+ .fetch_all(pool)+ .await?;- for (id, email_lower) in rows {- contact_ids.entry(email_lower).or_default().push(id);+ for row in rows {+ contact_ids.entry(row.email_lower).or_default().push(row.id);}}Ok(ResolvedFilters {contact_ids,trash_label_ids,+ team_link_ids,})}diff --git a/crates/email/src/outbound/email_pg_repo/dynamic/tests.rs b/crates/email/src/outbound/email_pg_repo/dynamic/tests.rsindex f77c394f95..170bdf1a2b 100644--- a/crates/email/src/outbound/email_pg_repo/dynamic/tests.rs+++ b/crates/email/src/outbound/email_pg_repo/dynamic/tests.rs@@ -1030,7 +1030,7 @@ fn test_matching_threads_cte_body_single_sender_uses_union_form() {let id = Uuid::new_v4();let expr = Expr::Literal(EmailLiteral::Sender(complete("[email protected]")));let resolved = resolved_with(&[("[email protected]", id)]);- let body = build_matching_threads_cte_body(&expr, &resolved, None).expect("body present");+ let body = build_matching_threads_ctes(&expr, &resolved, None).expect("body present");let debug = body.to_debug_sql();// Single sender: one UNION branch (no UNION keyword needed), index probe@@ -1043,10 +1043,12 @@ fn test_matching_threads_cte_body_single_sender_uses_union_form() {}#[test]-fn test_matching_threads_cte_body_or_of_kinds_emits_one_union_branch_per_literal() {+fn test_matching_threads_ctes_or_of_kinds_merges_recipient_roles() {// Sender OR Cc OR Bcc OR Recipient over the same email — the common- // "filter by this address in any role" case. Each leaf becomes its own- // index-driven UNION branch instead of a single OR-laden subquery.+ // "filter by this address in any role" case. Sender keeps its own+ // branch; TO/CC/BCC merge into one recipient branch. All three roles+ // cover the whole recipient_type enum, so no type filter is emitted and+ // the recipients index is probed once instead of three times.let id = Uuid::new_v4();let expr = Expr::or(Expr::or(@@ -1059,20 +1061,38 @@ fn test_matching_threads_cte_body_or_of_kinds_emits_one_union_branch_per_literalExpr::Literal(EmailLiteral::Recipient(complete("[email protected]"))),);let resolved = resolved_with(&[("[email protected]", id)]);- let body = build_matching_threads_cte_body(&expr, &resolved, None).expect("body present");+ let body = build_matching_threads_ctes(&expr, &resolved, None).expect("body present");let debug = body.to_debug_sql();- // Three UNIONs join the four branches.- assert_eq!(debug.matches("UNION").count(), 3);- assert!(debug.contains("recipient_type = 'TO'"));- assert!(debug.contains("recipient_type = 'CC'"));- assert!(debug.contains("recipient_type = 'BCC'"));+ // One UNION joins the two branches (sender + merged recipients).+ assert_eq!(debug.matches("UNION").count(), 1);+ assert!(!debug.contains("recipient_type"));+ assert!(debug.contains("email_message_recipients"));assert!(debug.contains("m.from_contact_id = "));assert!(body.has_bind_uuid(&id));// No correlated `m.thread_id = t.id` — uncorrelated branches.assert!(!debug.contains("m.thread_id = t.id"));}+#[test]+fn test_matching_threads_ctes_recipient_role_subset_keeps_type_filter() {+ // Cc OR Bcc (no TO) must not widen to all recipient roles — the merged+ // branch keeps an IN filter over exactly the requested types.+ let id = Uuid::new_v4();+ let expr = Expr::or(+ Expr::Literal(EmailLiteral::Cc(complete("[email protected]"))),+ Expr::Literal(EmailLiteral::Bcc(complete("[email protected]"))),+ );+ let resolved = resolved_with(&[("[email protected]", id)]);+ let body = build_matching_threads_ctes(&expr, &resolved, None).expect("body present");+ let debug = body.to_debug_sql();++ // Single merged branch — no UNION.+ assert!(!debug.contains("UNION"));+ assert!(debug.contains("mr.recipient_type IN ('CC', 'BCC')"));+ assert!(!debug.contains("recipient_type = "));+}+#[test]fn test_matching_threads_cte_body_skips_unresolved_complete_branches() {// Sender(known) OR Sender(missing) — drops the missing branch from the@@ -1083,7 +1103,7 @@ fn test_matching_threads_cte_body_skips_unresolved_complete_branches() {Expr::Literal(EmailLiteral::Sender(complete("[email protected]"))),);let resolved = resolved_with(&[("[email protected]", id)]);- let body = build_matching_threads_cte_body(&expr, &resolved, None).expect("body present");+ let body = build_matching_threads_ctes(&expr, &resolved, None).expect("body present");let debug = body.to_debug_sql();// Only one branch left → no UNION keyword.@@ -1095,23 +1115,73 @@ fn test_matching_threads_cte_body_skips_unresolved_complete_branches() {#[test]fn test_matching_threads_cte_body_partial_emits_ilike_branch_with_email_contacts_join() {let expr = Expr::Literal(EmailLiteral::Sender(Email::Partial("acme".into())));- let body = build_matching_threads_cte_body(&expr, &ResolvedFilters::empty(), None)- .expect("body present");+ let body =+ build_matching_threads_ctes(&expr, &ResolvedFilters::empty(), None).expect("body present");let debug = body.to_debug_sql();+ // The contact lookup is hoisted into a CTE the branch consumes.+ assert!(debug.contains("matching_contacts_0 AS MATERIALIZED ("));assert!(debug.contains("FROM email_contacts c"));+ assert!(debug.contains("FROM matching_contacts_0 c"));assert!(debug.contains("ILIKE"));assert!(body.has_bind_string("%acme%"));}+#[test]+fn test_matching_threads_ctes_domain_all_roles_share_one_contact_scan() {+ // The CRM ecd shape: one domain across all four roles. email_contacts+ // must be scanned exactly once (the hoisted CTE), feeding a sender+ // branch and one merged recipient branch with no type filter.+ let domain = Email::Domain("acme.com".to_string());+ let expr = Expr::or(+ Expr::or(+ Expr::or(+ Expr::Literal(EmailLiteral::Sender(domain.clone())),+ Expr::Literal(EmailLiteral::Cc(domain.clone())),+ ),+ Expr::Literal(EmailLiteral::Bcc(domain.clone())),+ ),+ Expr::Literal(EmailLiteral::Recipient(domain)),+ );+ let body =+ build_matching_threads_ctes(&expr, &ResolvedFilters::empty(), None).expect("body present");+ let debug = body.to_debug_sql();++ assert_eq!(debug.matches("FROM email_contacts").count(), 1);+ assert_eq!(+ debug.matches("matching_contacts_0 AS MATERIALIZED").count(),+ 1+ );+ assert_eq!(debug.matches("UNION").count(), 1);+ assert!(!debug.contains("recipient_type"));+ assert!(debug.contains("FROM matching_contacts_0 c"));+ assert!(body.has_bind_string("acme.com"));+}++#[test]+fn test_matching_threads_ctes_distinct_domains_get_separate_contact_ctes() {+ let expr = Expr::or(+ Expr::Literal(EmailLiteral::Sender(Email::Domain("acme.com".into()))),+ Expr::Literal(EmailLiteral::Sender(Email::Domain("globex.com".into()))),+ );+ let body =+ build_matching_threads_ctes(&expr, &ResolvedFilters::empty(), None).expect("body present");+ let debug = body.to_debug_sql();++ assert!(debug.contains("matching_contacts_0 AS MATERIALIZED"));+ assert!(debug.contains("matching_contacts_1 AS MATERIALIZED"));+ assert!(body.has_bind_string("acme.com"));+ assert!(body.has_bind_string("globex.com"));+}+#[test]fn test_matching_threads_cte_body_domain_emits_split_part_branch() {// The union-branch path is the candidate-thread pushdown — it has its// own copy of the address-match SQL builder. Domain on this path must// also emit the exact-domain predicate, not ILIKE.let expr = Expr::Literal(EmailLiteral::Sender(Email::Domain("acme.com".into())));- let body = build_matching_threads_cte_body(&expr, &ResolvedFilters::empty(), None)- .expect("body present");+ let body =+ build_matching_threads_ctes(&expr, &ResolvedFilters::empty(), None).expect("body present");let debug = body.to_debug_sql();assert!(debug.contains("FROM email_contacts c"));@@ -1131,8 +1201,8 @@ fn test_matching_threads_cte_body_domain_recipient_kind_uses_split_part() {// union-branch builder. Verify the predicate shape is consistent across// address kinds.let expr = Expr::Literal(EmailLiteral::Recipient(Email::Domain("acme.com".into())));- let body = build_matching_threads_cte_body(&expr, &ResolvedFilters::empty(), None)- .expect("body present");+ let body =+ build_matching_threads_ctes(&expr, &ResolvedFilters::empty(), None).expect("body present");let debug = body.to_debug_sql();assert!(debug.contains("email_message_recipients"));@@ -1145,8 +1215,8 @@ fn test_matching_threads_cte_body_domain_recipient_kind_uses_split_part() {#[test]fn test_matching_threads_cte_body_domain_lowercases_bind_value() {let expr = Expr::Literal(EmailLiteral::Sender(Email::Domain("AcMe.CoM".into())));- let body = build_matching_threads_cte_body(&expr, &ResolvedFilters::empty(), None)- .expect("body present");+ let body =+ build_matching_threads_ctes(&expr, &ResolvedFilters::empty(), None).expect("body present");assert!(body.has_bind_string("acme.com"));assert!(!body.has_bind_string("AcMe.CoM"));@@ -1164,7 +1234,7 @@ fn test_matching_threads_cte_body_and_of_conjuncts_uses_combined_predicate_form(Expr::Literal(EmailLiteral::Recipient(complete("[email protected]"))),);let resolved = resolved_with(&[("[email protected]", sender_id), ("[email protected]", recipient_id)]);- let body = build_matching_threads_cte_body(&expr, &resolved, None).expect("body present");+ let body = build_matching_threads_ctes(&expr, &resolved, None).expect("body present");let debug = body.to_debug_sql();assert!(debug.contains("SELECT DISTINCT m.thread_id"));@@ -1186,7 +1256,7 @@ fn test_matching_threads_cte_body_uses_resolved_trash_label_id() {let resolved = ResolvedFilters::empty().with_contact("[email protected]", contact_id).with_trash(trash_id);- let body = build_matching_threads_cte_body(&expr, &resolved, None).expect("body present");+ let body = build_matching_threads_ctes(&expr, &resolved, None).expect("body present");let debug = body.to_debug_sql();assert!(debug.contains("ml.label_id = "));@@ -1199,7 +1269,7 @@ fn test_matching_threads_cte_body_uses_resolved_trash_label_id() {fn test_matching_threads_cte_body_none_when_no_address_literals() {let id = Uuid::new_v4();let expr = Expr::Literal(EmailLiteral::ThreadId(id));- let body = build_matching_threads_cte_body(&expr, &ResolvedFilters::empty(), None);+ let body = build_matching_threads_ctes(&expr, &ResolvedFilters::empty(), None);assert!(body.is_none());}@@ -1235,6 +1305,123 @@ fn test_full_query_emits_matching_threads_cte_and_in_reference() {);}+#[test]+fn test_full_query_team_scoped_domain_filter_hoists_contact_cte() {+ // Team-scoped CRM domain query (the `ecd` shape): the domain contact+ // lookup must appear exactly once, as a hoisted CTE ahead of+ // matching_threads in the WITH chain — not re-scanned per UNION branch.+ let view = PreviewView::StandardLabel(PreviewViewStandardLabel::All);+ let domain = Email::Domain("acme.com".to_string());+ let expr = Expr::or(+ Expr::or(+ Expr::or(+ Expr::Literal(EmailLiteral::Sender(domain.clone())),+ Expr::Literal(EmailLiteral::Cc(domain.clone())),+ ),+ Expr::Literal(EmailLiteral::Bcc(domain.clone())),+ ),+ Expr::Literal(EmailLiteral::Recipient(domain)),+ );+ let sql = super::query::debug_build_query_sql_team_scoped(&view, &expr);++ let contacts_pos = sql+ .find("matching_contacts_0 AS MATERIALIZED (")+ .expect("hoisted contact CTE missing");+ let threads_pos = sql+ .find("matching_threads AS MATERIALIZED (")+ .expect("matching_threads CTE missing");+ assert!(+ contacts_pos < threads_pos,+ "contact CTE must precede matching_threads: {sql}"+ );+ // One contact scan across the whole WITH chain (the lateral's cheap+ // correlated per-message probes and Step 3's sender join sit after it).+ let with_chain = &sql[..threads_pos];+ assert_eq!(+ with_chain.matches("FROM email_contacts").count(),+ 1,+ "email_contacts must be scanned once in the WITH chain: {sql}"+ );+ let matching_threads_section = &sql[threads_pos..sql.find("SELECT\n").unwrap_or(sql.len())];+ assert!(+ !matching_threads_section.contains("recipient_type"),+ "all-roles domain filter needs no recipient_type filter in matching_threads: {sql}"+ );+}++#[test]+fn test_full_query_team_scoped_uses_resolved_team_links() {+ // With team links pre-resolved, the candidate WHERE probes the link set+ // directly and the matching CTEs are scoped to the same links, instead+ // of matching mail across every mailbox in the table and discarding+ // non-team threads afterwards. The live membership subquery must remain+ // alongside the cached probe — it revalidates the cached ids at+ // execution time so a member removed between resolve_filters and the+ // main query can't leak threads.+ let view = PreviewView::StandardLabel(PreviewViewStandardLabel::All);+ let team_link = Uuid::new_v4();+ let expr = Expr::Literal(EmailLiteral::Sender(Email::Domain("acme.com".into())));+ let resolved = ResolvedFilters::empty().with_team_links(vec![team_link]);+ let sql = super::query::debug_build_query_sql_team_scoped_with_resolved(&view, &expr, resolved);++ assert!(+ sql.contains("t.link_id = ANY("),+ "candidate WHERE must probe the resolved link set: {sql}"+ );+ assert!(+ sql.contains("JOIN team_user tu"),+ "live membership revalidation subquery missing: {sql}"+ );+ assert!(+ sql.contains("c.link_id = ANY("),+ "contact CTE must be scoped to team links: {sql}"+ );+ assert!(+ sql.contains("m.link_id = ANY("),+ "matching_threads branches must be scoped to team links: {sql}"+ );+}++#[test]+fn test_full_query_team_scoped_without_resolved_links_falls_back_to_subquery() {+ let view = PreviewView::StandardLabel(PreviewViewStandardLabel::All);+ let expr = Expr::Literal(EmailLiteral::Sender(Email::Domain("acme.com".into())));+ let sql = super::query::debug_build_query_sql_team_scoped(&view, &expr);++ assert!(+ sql.contains("JOIN team_user tu"),+ "fallback subquery missing: {sql}"+ );+ assert!(+ !sql.contains("c.link_id = ANY("),+ "CTE must stay unscoped without resolved team links: {sql}"+ );+}++#[test]+fn test_full_query_team_scoped_shared_include_leaves_cte_unscoped() {+ // Shared candidates pull threads from arbitrary links via entity_access,+ // so the team-link scope must not be applied to the matching CTEs.+ let view = PreviewView::StandardLabel(PreviewViewStandardLabel::All);+ let expr = Expr::and(+ Expr::Literal(EmailLiteral::Sender(Email::Domain("acme.com".into()))),+ Expr::Literal(EmailLiteral::Shared(+ item_filters::SharedEmailFilter::Include,+ )),+ );+ let resolved = ResolvedFilters::empty().with_team_links(vec![Uuid::new_v4()]);+ let sql = super::query::debug_build_query_sql_team_scoped_with_resolved(&view, &expr, resolved);++ assert!(+ !sql.contains("c.link_id = ANY("),+ "shared-include CTE must stay unscoped: {sql}"+ );+ assert!(+ !sql.contains("m.link_id = ANY("),+ "shared-include CTE must stay unscoped: {sql}"+ );+}+#[test]fn test_build_thread_email_filter_calendar_only_true_uses_thread_flag() {let expr = Expr::Literal(EmailLiteral::CalendarOnly(true));@@ -1406,7 +1593,7 @@ fn test_matching_threads_partial_branch_is_link_scoped_when_scope_given() {// With a scope, both the contact and message sides must be restricted.let links = vec![Uuid::new_v4(), Uuid::new_v4()];let expr = Expr::Literal(EmailLiteral::Sender(Email::Partial("acme".to_string())));- let body = build_matching_threads_cte_body(&expr, &ResolvedFilters::empty(), Some(&links))+ let body = build_matching_threads_ctes(&expr, &ResolvedFilters::empty(), Some(&links)).expect("body present");let debug = body.to_debug_sql();@@ -1421,7 +1608,7 @@ fn test_matching_threads_domain_branch_is_link_scoped_when_scope_given() {let expr = Expr::Literal(EmailLiteral::Recipient(Email::Domain("acme.com".to_string(),)));- let body = build_matching_threads_cte_body(&expr, &ResolvedFilters::empty(), Some(&links))+ let body = build_matching_threads_ctes(&expr, &ResolvedFilters::empty(), Some(&links)).expect("body present");let debug = body.to_debug_sql();@@ -1440,8 +1627,7 @@ fn test_matching_threads_combined_fallback_is_link_scoped_when_scope_given() {Expr::Literal(EmailLiteral::Recipient(complete("[email protected]"))),);let resolved = resolved_with(&[("[email protected]", id)]);- let body =- build_matching_threads_cte_body(&expr, &resolved, Some(&links)).expect("body present");+ let body = build_matching_threads_ctes(&expr, &resolved, Some(&links)).expect("body present");let debug = body.to_debug_sql();assert!(debug.contains("SELECT DISTINCT m.thread_id FROM email_messages m"));@@ -1453,8 +1639,8 @@ fn test_matching_threads_unscoped_without_link_scope() {// Shared/team queries pass None — no link_id predicates may appear,// since candidate threads can live on other users' links.let expr = Expr::Literal(EmailLiteral::Sender(Email::Partial("acme".to_string())));- let body = build_matching_threads_cte_body(&expr, &ResolvedFilters::empty(), None)- .expect("body present");+ let body =+ build_matching_threads_ctes(&expr, &ResolvedFilters::empty(), None).expect("body present");let debug = body.to_debug_sql();assert!(!debug.contains("link_id = ANY("));diff --git a/crates/macro_db_client/migrations/20260714201659_email_messages_thread_root_global_index.sql b/crates/macro_db_client/migrations/20260714201659_email_messages_thread_root_global_index.sqlnew file mode 100644index 0000000000..b2efa8a53c--- /dev/null+++ b/crates/macro_db_client/migrations/20260714201659_email_messages_thread_root_global_index.sql@@ -0,0 +1,9 @@+-- no-transaction+-- Serves the team-scoped dedupe_key lookup: the first non-draft message with+-- a global_id per thread, ORDER BY internal_date_ts ASC NULLS LAST, id ASC+-- LIMIT 1. Default btree ordering (ASC NULLS LAST) matches exactly, so the+-- subquery becomes a single forward index probe instead of a backward scan+-- over idx_email_messages_thread_date_not_draft plus an incremental sort.+CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_email_messages_thread_root_global+ ON email_messages (thread_id, internal_date_ts, id)+ WHERE is_draft = FALSE AND global_id IS NOT NULL;