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
fix(crm): stop mailbox backfills from queueing on one CRM domain lock (#9290)
teob4e6afc38801
diff --git a/.sqlx/query-1443d3a29f699b4ef3cf30c9591aa26ce5dc85b6603605b4eaa8d532d0cfff82.json b/.sqlx/query-1443d3a29f699b4ef3cf30c9591aa26ce5dc85b6603605b4eaa8d532d0cfff82.jsonnew file mode 100644index 0000000000..916da4a992--- /dev/null+++ b/.sqlx/query-1443d3a29f699b4ef3cf30c9591aa26ce5dc85b6603605b4eaa8d532d0cfff82.json@@ -0,0 +1,29 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT\n (NOT $8 AND NOT EXISTS (\n SELECT 1\n FROM crm_companies c\n JOIN crm_domains d ON d.company_id = c.id\n WHERE c.team_id = $1\n AND d.team_id = $1\n AND LOWER(d.domain) = $3\n ))\n OR EXISTS (\n SELECT 1\n FROM crm_companies c\n JOIN crm_domains d ON d.company_id = c.id\n JOIN crm_contacts ct ON ct.company_id = c.id\n JOIN crm_contact_sources s ON s.contact_id = ct.id\n WHERE c.team_id = $1\n AND d.team_id = $1\n AND LOWER(d.domain) = $3\n AND ct.email = $4\n AND s.link_id = $2\n AND (ct.name IS NOT NULL OR $5::text IS NULL)\n AND c.last_interaction >= $7\n AND ct.last_interaction >= $7\n AND (NOT $8 OR (c.first_interaction <= $6 AND ct.first_interaction <= $6))\n FOR KEY SHARE OF s\n ) AS \"changes_nothing!\"\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "changes_nothing!",+ "type_info": "Bool"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid",+ "Uuid",+ "Text",+ "Text",+ "Text",+ "Timestamptz",+ "Timestamptz",+ "Bool"+ ]+ },+ "nullable": [+ null+ ]+ },+ "hash": "1443d3a29f699b4ef3cf30c9591aa26ce5dc85b6603605b4eaa8d532d0cfff82"+}diff --git a/crates/crm/src/outbound/companies_repo.rs b/crates/crm/src/outbound/companies_repo.rsindex c086a24364..29ff8b32fb 100644--- a/crates/crm/src/outbound/companies_repo.rs+++ b/crates/crm/src/outbound/companies_repo.rs@@ -136,6 +136,73 @@ impl CompaniesRepositoryImpl {Ok(row.map(|r| (r.id, r.hidden)))}+ /// Whether `populate_contact` would leave every row as it is: a+ /// received message from a domain with no company, or a contact whose+ /// company, contact and source rows already cover this interaction.+ /// A mailbox backfill enqueues a populate per message, so most repeat a+ /// contact the CRM already has; checking before the `(team, domain)`+ /// lock keeps them from queueing behind one another on a busy domain.+ /// `FOR KEY SHARE` waits for a depopulate deleting the source, so a+ /// skip can't leave a teardown standing that the locked path would+ /// have undone.+ #[expect(+ clippy::too_many_arguments,+ reason = "mirrors populate_contact's arguments"+ )]+ async fn populate_changes_nothing(+ &self,+ team_id: &uuid::Uuid,+ link_id: &uuid::Uuid,+ normalized_domain: &str,+ normalized_email: &str,+ name: Option<&str>,+ first_at: DateTime<Utc>,+ last_at: DateTime<Utc>,+ is_sent: bool,+ ) -> Result<bool, CrmError> {+ sqlx::query_scalar!(+ r#"+ SELECT+ (NOT $8 AND NOT EXISTS (+ SELECT 1+ FROM crm_companies c+ JOIN crm_domains d ON d.company_id = c.id+ WHERE c.team_id = $1+ AND d.team_id = $1+ AND LOWER(d.domain) = $3+ ))+ OR EXISTS (+ SELECT 1+ FROM crm_companies c+ JOIN crm_domains d ON d.company_id = c.id+ JOIN crm_contacts ct ON ct.company_id = c.id+ JOIN crm_contact_sources s ON s.contact_id = ct.id+ WHERE c.team_id = $1+ AND d.team_id = $1+ AND LOWER(d.domain) = $3+ AND ct.email = $4+ AND s.link_id = $2+ AND (ct.name IS NOT NULL OR $5::text IS NULL)+ AND c.last_interaction >= $7+ AND ct.last_interaction >= $7+ AND (NOT $8 OR (c.first_interaction <= $6 AND ct.first_interaction <= $6))+ FOR KEY SHARE OF s+ ) AS "changes_nothing!"+ "#,+ team_id,+ link_id,+ normalized_domain,+ normalized_email,+ name,+ first_at,+ last_at,+ is_sent,+ )+ .fetch_one(&self.pool)+ .await+ .map_err(|e| CrmError::StorageLayerError(e.into()))+ }+/// Merge an interaction range into a company row./// `last_interaction` always bumps via GREATEST; `first_interaction`/// only LEAST-merges on `is_sent = true` — received-direction@@ -276,7 +343,7 @@ impl CompaniesRepository for CompaniesRepositoryImpl {self.query_activity(scope).await}- #[tracing::instrument(skip(self), err)]+ #[tracing::instrument(skip(self), fields(skipped = tracing::field::Empty), err)]#[allow(clippy::too_many_arguments)]async fn populate_contact(&self,@@ -292,6 +359,23 @@ impl CompaniesRepository for CompaniesRepositoryImpl {let normalized_domain = domain.to_ascii_lowercase();let normalized_email = email.to_ascii_lowercase();+ let skipped = self+ .populate_changes_nothing(+ team_id,+ link_id,+ &normalized_domain,+ &normalized_email,+ name,+ first_at,+ last_at,+ is_sent,+ )+ .await?;+ tracing::Span::current().record("skipped", skipped);+ if skipped {+ return Ok(());+ }+let mut tx = self.pool.begin()