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
feat(email): add denormalized is_signal flag on email_threads (#4667)
Evan Hutnik742df7c3bc0d
diff --git a/rust/cloud-storage/.sqlx/query-122b630d54cd1af0141b55934a36ba0b20a940aa0cbadc4b52fba67ee06849a9.json b/rust/cloud-storage/.sqlx/query-122b630d54cd1af0141b55934a36ba0b20a940aa0cbadc4b52fba67ee06849a9.jsonnew file mode 100644index 0000000000..1e7715b2b8--- /dev/null+++ b/rust/cloud-storage/.sqlx/query-122b630d54cd1af0141b55934a36ba0b20a940aa0cbadc4b52fba67ee06849a9.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE email_threads\n SET is_signal = false\n WHERE link_id = $1 AND is_signal\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "122b630d54cd1af0141b55934a36ba0b20a940aa0cbadc4b52fba67ee06849a9"+}diff --git a/rust/cloud-storage/.sqlx/query-53d295231b271d328fab23120f45812ae613e1761ae5dfbc5199221f72c91b8f.json b/rust/cloud-storage/.sqlx/query-53d295231b271d328fab23120f45812ae613e1761ae5dfbc5199221f72c91b8f.jsonnew file mode 100644index 0000000000..330f58a2b0--- /dev/null+++ b/rust/cloud-storage/.sqlx/query-53d295231b271d328fab23120f45812ae613e1761ae5dfbc5199221f72c91b8f.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE email_threads t\n SET is_signal = calc.sig\n FROM (\n SELECT EXISTS (\n SELECT 1\n FROM email_messages m\n WHERE m.thread_id = $1\n AND NOT EXISTS (\n SELECT 1 FROM email_message_labels ml\n JOIN email_labels l ON ml.label_id = l.id\n WHERE ml.message_id = m.id AND l.name = 'TRASH'\n )\n AND (\n (\n EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_address IS NOT NULL\n AND LOWER(ef.email_address) = LOWER(sender_c.email_address)\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = TRUE\n )\n OR EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_domain IS NOT NULL\n AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = TRUE\n AND NOT EXISTS (\n SELECT 1 FROM email_filters ef_addr\n WHERE ef_addr.link_id = m.link_id\n AND ef_addr.email_address IS NOT NULL\n AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)\n AND ef_addr.is_important = FALSE\n )\n )\n )\n OR (\n NOT (\n EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_address IS NOT NULL\n AND LOWER(ef.email_address) = LOWER(sender_c.email_address)\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = FALSE\n )\n OR EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_domain IS NOT NULL\n AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = FALSE\n AND NOT EXISTS (\n SELECT 1 FROM email_filters ef_addr\n WHERE ef_addr.link_id = m.link_id\n AND ef_addr.email_address IS NOT NULL\n AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)\n AND ef_addr.is_important = TRUE\n )\n )\n )\n AND (\n m.is_draft = TRUE\n OR EXISTS (\n SELECT 1 FROM email_message_labels ml\n JOIN email_labels l ON ml.label_id = l.id\n WHERE ml.message_id = m.id\n AND l.name IN ('CATEGORY_PERSONAL', 'SENT', 'DRAFT')\n )\n OR NOT EXISTS (\n SELECT 1 FROM email_message_labels ml\n JOIN email_labels l ON ml.label_id = l.id\n WHERE ml.message_id = m.id\n AND l.name IN ('CATEGORY_UPDATES', 'CATEGORY_PROMOTIONS', 'CATEGORY_SOCIAL', 'CATEGORY_FORUMS')\n )\n )\n )\n )\n ) AS sig\n ) calc\n WHERE t.id = $1\n AND t.is_signal IS DISTINCT FROM calc.sig\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "53d295231b271d328fab23120f45812ae613e1761ae5dfbc5199221f72c91b8f"+}diff --git a/rust/cloud-storage/.sqlx/query-93b0b51a1671eadaab74ba589a0cdb546316dc51e386490f35ce9aaaa9af0ea6.json b/rust/cloud-storage/.sqlx/query-93b0b51a1671eadaab74ba589a0cdb546316dc51e386490f35ce9aaaa9af0ea6.jsonnew file mode 100644index 0000000000..80405fb98b--- /dev/null+++ b/rust/cloud-storage/.sqlx/query-93b0b51a1671eadaab74ba589a0cdb546316dc51e386490f35ce9aaaa9af0ea6.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE email_threads t\n SET is_signal = true\n FROM (\n SELECT DISTINCT m.thread_id\n FROM email_messages m\n WHERE m.link_id = $1\n AND NOT EXISTS (\n SELECT 1 FROM email_message_labels ml\n JOIN email_labels l ON ml.label_id = l.id\n WHERE ml.message_id = m.id AND l.name = 'TRASH'\n )\n AND (\n (\n EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_address IS NOT NULL\n AND LOWER(ef.email_address) = LOWER(sender_c.email_address)\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = TRUE\n )\n OR EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_domain IS NOT NULL\n AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = TRUE\n AND NOT EXISTS (\n SELECT 1 FROM email_filters ef_addr\n WHERE ef_addr.link_id = m.link_id\n AND ef_addr.email_address IS NOT NULL\n AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)\n AND ef_addr.is_important = FALSE\n )\n )\n )\n OR (\n NOT (\n EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_address IS NOT NULL\n AND LOWER(ef.email_address) = LOWER(sender_c.email_address)\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = FALSE\n )\n OR EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_domain IS NOT NULL\n AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = FALSE\n AND NOT EXISTS (\n SELECT 1 FROM email_filters ef_addr\n WHERE ef_addr.link_id = m.link_id\n AND ef_addr.email_address IS NOT NULL\n AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)\n AND ef_addr.is_important = TRUE\n )\n )\n )\n AND (\n m.is_draft = TRUE\n OR EXISTS (\n SELECT 1 FROM email_message_labels ml\n JOIN email_labels l ON ml.label_id = l.id\n WHERE ml.message_id = m.id\n AND l.name IN ('CATEGORY_PERSONAL', 'SENT', 'DRAFT')\n )\n OR NOT EXISTS (\n SELECT 1 FROM email_message_labels ml\n JOIN email_labels l ON ml.label_id = l.id\n WHERE ml.message_id = m.id\n AND l.name IN ('CATEGORY_UPDATES', 'CATEGORY_PROMOTIONS', 'CATEGORY_SOCIAL', 'CATEGORY_FORUMS')\n )\n )\n )\n )\n ) sig\n WHERE t.id = sig.thread_id\n AND NOT t.is_signal\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "93b0b51a1671eadaab74ba589a0cdb546316dc51e386490f35ce9aaaa9af0ea6"+}diff --git a/rust/cloud-storage/.sqlx/query-b291ecfab39f7d8dabb4cc4b5ca58ae16b1a9bb47acd22272ccdb4aa87a88efe.json b/rust/cloud-storage/.sqlx/query-b291ecfab39f7d8dabb4cc4b5ca58ae16b1a9bb47acd22272ccdb4aa87a88efe.jsonnew file mode 100644index 0000000000..615403ffbc--- /dev/null+++ b/rust/cloud-storage/.sqlx/query-b291ecfab39f7d8dabb4cc4b5ca58ae16b1a9bb47acd22272ccdb4aa87a88efe.json@@ -0,0 +1,29 @@+{+ "db_name": "PostgreSQL",+ "query": "DELETE FROM email_filters WHERE id = $1 AND link_id = $2\n RETURNING email_address, email_domain",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "email_address",+ "type_info": "Varchar"+ },+ {+ "ordinal": 1,+ "name": "email_domain",+ "type_info": "Varchar"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid",+ "Uuid"+ ]+ },+ "nullable": [+ true,+ true+ ]+ },+ "hash": "b291ecfab39f7d8dabb4cc4b5ca58ae16b1a9bb47acd22272ccdb4aa87a88efe"+}diff --git a/rust/cloud-storage/.sqlx/query-d52e4f6426d32b333ee46a07031a91f5670946f87411ef0679dd3bf067c1ffb3.json b/rust/cloud-storage/.sqlx/query-d52e4f6426d32b333ee46a07031a91f5670946f87411ef0679dd3bf067c1ffb3.jsonnew file mode 100644index 0000000000..9e6d95a9b6--- /dev/null+++ b/rust/cloud-storage/.sqlx/query-d52e4f6426d32b333ee46a07031a91f5670946f87411ef0679dd3bf067c1ffb3.json@@ -0,0 +1,16 @@+{+ "db_name": "PostgreSQL",+ "query": "\n WITH affected AS MATERIALIZED (\n -- Contact-first candidate scan, MATERIALIZED so the planner can't\n -- hoist the expensive per-thread sig predicate above this\n -- selective filter and evaluate it for the whole link.\n SELECT DISTINCT m.thread_id\n FROM email_contacts c\n JOIN email_messages m ON m.from_contact_id = c.id\n WHERE c.link_id = $1\n AND m.link_id = $1\n AND ($2::text IS NULL OR LOWER(c.email_address) = LOWER($2))\n AND ($3::text IS NULL OR LOWER(SPLIT_PART(c.email_address, '@', 2)) = LOWER($3))\n ),\n -- MATERIALIZED so sig is computed once per thread; inlined, both the\n -- SET and the IS DISTINCT FROM would re-evaluate the predicate.\n calc AS MATERIALIZED (\n SELECT a.thread_id,\n EXISTS (\n SELECT 1\n FROM email_messages m\n WHERE m.thread_id = a.thread_id\n AND NOT EXISTS (\n SELECT 1 FROM email_message_labels ml\n JOIN email_labels l ON ml.label_id = l.id\n WHERE ml.message_id = m.id AND l.name = 'TRASH'\n )\n AND (\n (\n EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_address IS NOT NULL\n AND LOWER(ef.email_address) = LOWER(sender_c.email_address)\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = TRUE\n )\n OR EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_domain IS NOT NULL\n AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = TRUE\n AND NOT EXISTS (\n SELECT 1 FROM email_filters ef_addr\n WHERE ef_addr.link_id = m.link_id\n AND ef_addr.email_address IS NOT NULL\n AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)\n AND ef_addr.is_important = FALSE\n )\n )\n )\n OR (\n NOT (\n EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_address IS NOT NULL\n AND LOWER(ef.email_address) = LOWER(sender_c.email_address)\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = FALSE\n )\n OR EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_domain IS NOT NULL\n AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = FALSE\n AND NOT EXISTS (\n SELECT 1 FROM email_filters ef_addr\n WHERE ef_addr.link_id = m.link_id\n AND ef_addr.email_address IS NOT NULL\n AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)\n AND ef_addr.is_important = TRUE\n )\n )\n )\n AND (\n m.is_draft = TRUE\n OR EXISTS (\n SELECT 1 FROM email_message_labels ml\n JOIN email_labels l ON ml.label_id = l.id\n WHERE ml.message_id = m.id\n AND l.name IN ('CATEGORY_PERSONAL', 'SENT', 'DRAFT')\n )\n OR NOT EXISTS (\n SELECT 1 FROM email_message_labels ml\n JOIN email_labels l ON ml.label_id = l.id\n WHERE ml.message_id = m.id\n AND l.name IN ('CATEGORY_UPDATES', 'CATEGORY_PROMOTIONS', 'CATEGORY_SOCIAL', 'CATEGORY_FORUMS')\n )\n )\n )\n )\n ) AS sig\n FROM affected a\n )\n UPDATE email_threads t\n SET is_signal = calc.sig\n FROM calc\n WHERE t.id = calc.thread_id\n AND t.link_id = $1\n AND t.is_signal IS DISTINCT FROM calc.sig\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "Text",+ "Text"+ ]+ },+ "nullable": []+ },+ "hash": "d52e4f6426d32b333ee46a07031a91f5670946f87411ef0679dd3bf067c1ffb3"+}diff --git a/rust/cloud-storage/.sqlx/query-e70d5acce88125ae894d9d57356559c6e59e236c9650211a52fdc90602318cc3.json b/rust/cloud-storage/.sqlx/query-e70d5acce88125ae894d9d57356559c6e59e236c9650211a52fdc90602318cc3.jsondeleted file mode 100644index 3ace0da282..0000000000--- a/rust/cloud-storage/.sqlx/query-e70d5acce88125ae894d9d57356559c6e59e236c9650211a52fdc90602318cc3.json+++ /dev/null@@ -1,15 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "DELETE FROM email_filters WHERE id = $1 AND link_id = $2",- "describe": {- "columns": [],- "parameters": {- "Left": [- "Uuid",- "Uuid"- ]- },- "nullable": []- },- "hash": "e70d5acce88125ae894d9d57356559c6e59e236c9650211a52fdc90602318cc3"-}diff --git a/rust/cloud-storage/email/fixtures/email_signal_flag.sql b/rust/cloud-storage/email/fixtures/email_signal_flag.sqlnew file mode 100644index 0000000000..3aeafa62d9--- /dev/null+++ b/rust/cloud-storage/email/fixtures/email_signal_flag.sql@@ -0,0 +1,59 @@+-- Fixture for the denormalized email_threads.is_signal flag (email crate):+-- filter-change fan-out resync and the update_thread_metadata piggyback.+--+-- Thread e201 (is_signal=true, correct): unlabeled message from+-- [email protected] — muting/unmuting that sender flips the flag.+-- Thread e202 (is_signal=false, correct): CATEGORY_PROMOTIONS message from+-- [email protected] — marking the domain important flips it to signal.+-- Thread e203 (is_signal=true, stale on purpose): only a CATEGORY_PROMOTIONS+-- message — update_thread_metadata must clear it.+-- Thread e204 (is_signal=true, correct): promotions message + macro draft —+-- discarding the draft via delete_draft_message must flip the flag.+-- Thread e205 (is_signal=true, correct): unlabeled message from+-- [email protected] — same domain as e201's sender but no address+-- exception, for the domain-mute fan-out tests.++INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider, is_sync_active, created_at, updated_at)+VALUES ('00000000-0000-0000-0000-000000000e01', 'macro|[email protected]', '00000000-0000-0000-0000-000000000e01',+ '[email protected]', 'GMAIL', true, NOW(), NOW());++INSERT INTO email_contacts (id, link_id, email_address, created_at, updated_at)+VALUES ('00000000-0000-0000-0000-0000000ce001', '00000000-0000-0000-0000-000000000e01', '[email protected]', NOW(), NOW()),+ ('00000000-0000-0000-0000-0000000ce002', '00000000-0000-0000-0000-000000000e01', '[email protected]', NOW(), NOW()),+ ('00000000-0000-0000-0000-0000000ce003', '00000000-0000-0000-0000-000000000e01', '[email protected]', NOW(), NOW());++INSERT INTO email_labels (id, link_id, provider_label_id, name, created_at)+VALUES ('00000000-0000-0000-0000-0000000be001', '00000000-0000-0000-0000-000000000e01', 'CATEGORY_PROMOTIONS', 'CATEGORY_PROMOTIONS', NOW());++INSERT INTO email_threads (id, link_id, inbox_visible, is_read, is_signal, created_at, updated_at)+VALUES ('00000000-0000-0000-0000-00000000e201', '00000000-0000-0000-0000-000000000e01', true, false, true, NOW(), NOW()),+ ('00000000-0000-0000-0000-00000000e202', '00000000-0000-0000-0000-000000000e01', true, false, false, NOW(), NOW()),+ ('00000000-0000-0000-0000-00000000e203', '00000000-0000-0000-0000-000000000e01', true, false, true, NOW(), NOW()),+ ('00000000-0000-0000-0000-00000000e204', '00000000-0000-0000-0000-000000000e01', true, false, true, NOW(), NOW()),+ ('00000000-0000-0000-0000-00000000e205', '00000000-0000-0000-0000-000000000e01', true, false, true, NOW(), NOW());++INSERT INTO email_messages (id, thread_id, link_id, provider_id, global_id, is_sent, from_contact_id, internal_date_ts,+ has_attachments, is_read, is_starred, is_draft, created_at, updated_at)+VALUES ('00000000-0000-0000-0000-00000000e501', '00000000-0000-0000-0000-00000000e201', '00000000-0000-0000-0000-000000000e01',+ 'provider-msg-e501', 'gid-e501', FALSE, '00000000-0000-0000-0000-0000000ce001', '2025-01-05 10:00:00 +00:00',+ false, false, false, false, NOW(), NOW()),+ ('00000000-0000-0000-0000-00000000e502', '00000000-0000-0000-0000-00000000e202', '00000000-0000-0000-0000-000000000e01',+ 'provider-msg-e502', 'gid-e502', FALSE, '00000000-0000-0000-0000-0000000ce002', '2025-01-05 11:00:00 +00:00',+ false, false, false, false, NOW(), NOW()),+ ('00000000-0000-0000-0000-00000000e503', '00000000-0000-0000-0000-00000000e203', '00000000-0000-0000-0000-000000000e01',+ 'provider-msg-e503', 'gid-e503', FALSE, '00000000-0000-0000-0000-0000000ce002', '2025-01-05 12:00:00 +00:00',+ false, false, false, false, NOW(), NOW()),+ ('00000000-0000-0000-0000-00000000e504', '00000000-0000-0000-0000-00000000e204', '00000000-0000-0000-0000-000000000e01',+ 'provider-msg-e504', 'gid-e504', FALSE, '00000000-0000-0000-0000-0000000ce002', '2025-01-05 13:00:00 +00:00',+ false, false, false, false, NOW(), NOW()),+ ('00000000-0000-0000-0000-00000000e505', '00000000-0000-0000-0000-00000000e204', '00000000-0000-0000-0000-000000000e01',+ NULL, NULL, FALSE, '00000000-0000-0000-0000-0000000ce001', '2025-01-05 14:00:00 +00:00',+ false, false, false, true, NOW(), NOW()),+ ('00000000-0000-0000-0000-00000000e506', '00000000-0000-0000-0000-00000000e205', '00000000-0000-0000-0000-000000000e01',+ 'provider-msg-e506', 'gid-e506', FALSE, '00000000-0000-0000-0000-0000000ce003', '2025-01-05 15:00:00 +00:00',+ false, false, false, false, NOW(), NOW());++INSERT INTO email_message_labels (message_id, label_id)+VALUES ('00000000-0000-0000-0000-00000000e502', '00000000-0000-0000-0000-0000000be001'),+ ('00000000-0000-0000-0000-00000000e503', '00000000-0000-0000-0000-0000000be001'),+ ('00000000-0000-0000-0000-00000000e504', '00000000-0000-0000-0000-0000000be001');diff --git a/rust/cloud-storage/email/src/outbound/email_pg_repo/email_filter.rs b/rust/cloud-storage/email/src/outbound/email_pg_repo/email_filter.rsindex 403f48a2b0..c4ccf75f43 100644--- a/rust/cloud-storage/email/src/outbound/email_pg_repo/email_filter.rs+++ b/rust/cloud-storage/email/src/outbound/email_pg_repo/email_filter.rs@@ -29,9 +29,9 @@ impl From<EmailFilterRow> for EmailFilter {/// Upsert an email filter by address. If a filter for this link+address already/// exists, update its `is_important` value.-#[tracing::instrument(skip(pool), err)]+#[tracing::instrument(skip(conn), err)]pub async fn upsert_email_filter_by_address(- pool: &PgPool,+ conn: &mut sqlx::PgConnection,link_id: Uuid,email_address: &str,is_important: bool,@@ -47,7 +47,7 @@ pub async fn upsert_email_filter_by_address(email_address,is_important,)- .fetch_one(pool)+ .fetch_one(conn).await?;Ok(row.into())@@ -55,9 +55,9 @@ pub async fn upsert_email_filter_by_address(/// Upsert an email filter by domain. If a filter for this link+domain already/// exists, update its `is_important` value.-#[tracing::instrument(skip(pool), err)]+#[tracing::instrument(skip(conn), err)]pub async fn upsert_email_filter_by_domain(- pool: &PgPool,+ conn: &mut sqlx::PgConnection,link_id: Uuid,email_domain: &str,is_important: bool,@@ -73,28 +73,183 @@ pub async fn upsert_email_filter_by_domain(email_domain,is_important,)- .fetch_one(pool)+ .fetch_one(conn).await?;Ok(row.into())}-/// Delete an email filter by its ID, scoped to a link.-#[tracing::instrument(skip(pool), err)]+/// Sender target (address or domain) of a deleted email filter, returned so+/// the caller can resync affected threads' signal flags.+pub struct DeletedEmailFilterTarget {+ pub email_address: Option<String>,+ pub email_domain: Option<String>,+}++/// Delete an email filter by its ID, scoped to a link. Returns the deleted+/// filter's sender target, or `None` if no row matched.+#[tracing::instrument(skip(conn), err)]pub async fn delete_email_filter(- pool: &PgPool,+ conn: &mut sqlx::PgConnection,filter_id: Uuid,link_id: Uuid,-) -> Result<bool, sqlx::Error> {- let result = sqlx::query!(- r#"DELETE FROM email_filters WHERE id = $1 AND link_id = $2"#,+) -> Result<Option<DeletedEmailFilterTarget>, sqlx::Error> {+ let row = sqlx::query!(+ r#"DELETE FROM email_filters WHERE id = $1 AND link_id = $2+ RETURNING email_address, email_domain"#,filter_id,link_id,)- .execute(pool)+ .fetch_optional(conn)+ .await?;++ Ok(row.map(|r| DeletedEmailFilterTarget {+ email_address: r.email_address,+ email_domain: r.email_domain,+ }))+}++/// Recomputes `email_threads.is_signal` for every thread in the link with a+/// message from the given sender address or domain. Called after an+/// email_filters change, since the override feeds the signal heuristic.+/// The recompute mirrors sync_thread_signal_flag (thread.rs) / the+/// Importance(true) predicate in the dynamic query builder.+#[tracing::instrument(skip(conn), err)]+pub async fn resync_signal_flags_for_sender(+ conn: &mut sqlx::PgConnection,+ link_id: Uuid,+ email_address: Option<&str>,+ email_domain: Option<&str>,+) -> Result<u64, sqlx::Error> {+ // The scope filter below ANDs both targets, so passing both would only+ // match contacts satisfying address AND domain at once.+ debug_assert!(+ email_address.is_some() ^ email_domain.is_some(),+ "expected exactly one filter target"+ );++ let result = sqlx::query!(+ r#"+ WITH affected AS MATERIALIZED (+ -- Contact-first candidate scan, MATERIALIZED so the planner can't+ -- hoist the expensive per-thread sig predicate above this+ -- selective filter and evaluate it for the whole link.+ SELECT DISTINCT m.thread_id+ FROM email_contacts c+ JOIN email_messages m ON m.from_contact_id = c.id+ WHERE c.link_id = $1+ AND m.link_id = $1+ AND ($2::text IS NULL OR LOWER(c.email_address) = LOWER($2))+ AND ($3::text IS NULL OR LOWER(SPLIT_PART(c.email_address, '@', 2)) = LOWER($3))+ ),+ -- MATERIALIZED so sig is computed once per thread; inlined, both the+ -- SET and the IS DISTINCT FROM would re-evaluate the predicate.+ calc AS MATERIALIZED (+ SELECT a.thread_id,+ EXISTS (+ SELECT 1+ FROM email_messages m+ WHERE m.thread_id = a.thread_id+ AND NOT EXISTS (+ SELECT 1 FROM email_message_labels ml+ JOIN email_labels l ON ml.label_id = l.id+ WHERE ml.message_id = m.id AND l.name = 'TRASH'+ )+ AND (+ (+ EXISTS (+ SELECT 1+ FROM email_contacts sender_c+ JOIN email_filters ef+ ON ef.link_id = m.link_id+ AND ef.email_address IS NOT NULL+ AND LOWER(ef.email_address) = LOWER(sender_c.email_address)+ WHERE sender_c.id = m.from_contact_id+ AND ef.is_important = TRUE+ )+ OR EXISTS (+ SELECT 1+ FROM email_contacts sender_c+ JOIN email_filters ef+ ON ef.link_id = m.link_id+ AND ef.email_domain IS NOT NULL+ AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))+ WHERE sender_c.id = m.from_contact_id+ AND ef.is_important = TRUE+ AND NOT EXISTS (+ SELECT 1 FROM email_filters ef_addr+ WHERE ef_addr.link_id = m.link_id+ AND ef_addr.email_address IS NOT NULL+ AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)+ AND ef_addr.is_important = FALSE+ )+ )+ )+ OR (+ NOT (+ EXISTS (+ SELECT 1+ FROM email_contacts sender_c+ JOIN email_filters ef+ ON ef.link_id = m.link_id+ AND ef.email_address IS NOT NULL+ AND LOWER(ef.email_address) = LOWER(sender_c.email_address)+ WHERE sender_c.id = m.from_contact_id+ AND ef.is_important = FALSE+ )+ OR EXISTS (+ SELECT 1+ FROM email_contacts sender_c+ JOIN email_filters ef+ ON ef.link_id = m.link_id+ AND ef.email_domain IS NOT NULL+ AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))+ WHERE sender_c.id = m.from_contact_id+ AND ef.is_important = FALSE+ AND NOT EXISTS (+ SELECT 1 FROM email_filters ef_addr+ WHERE ef_addr.link_id = m.link_id+ AND ef_addr.email_address IS NOT NULL+ AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)+ AND ef_addr.is_important = TRUE+ )+ )+ )+ AND (+ m.is_draft = TRUE+ OR EXISTS (+ SELECT 1 FROM email_message_labels ml+ JOIN email_labels l ON ml.label_id = l.id+ WHERE ml.message_id = m.id+ AND l.name IN ('CATEGORY_PERSONAL', 'SENT', 'DRAFT')+ )+ OR NOT EXISTS (+ SELECT 1 FROM email_message_labels ml+ JOIN email_labels l ON ml.label_id = l.id+ WHERE ml.message_id = m.id+ AND l.name IN ('CATEGORY_UPDATES', 'CATEGORY_PROMOTIONS', 'CATEGORY_SOCIAL', 'CATEGORY_FORUMS')+ )+ )+ )+ )+ ) AS sig+ FROM affected a+ )+ UPDATE email_threads t+ SET is_signal = calc.sig+ FROM calc+ WHERE t.id = calc.thread_id+ AND t.link_id = $1+ AND t.is_signal IS DISTINCT FROM calc.sig+ "#,+ link_id,+ email_address,+ email_domain,+ )+ .execute(conn).await?;- Ok(result.rows_affected() > 0)+ Ok(result.rows_affected())}/// List all email filters for a link.diff --git a/rust/cloud-storage/email/src/outbound/email_pg_repo/message.rs b/rust/cloud-storage/email/src/outbound/email_pg_repo/message.rsindex 67f0d64af5..aff54d04c6 100644--- a/rust/cloud-storage/email/src/outbound/email_pg_repo/message.rs+++ b/rust/cloud-storage/email/src/outbound/email_pg_repo/message.rs@@ -380,6 +380,9 @@ pub(crate) async fn delete_draft_message(sqlx::query!("DELETE FROM email_threads WHERE id = $1", thread_db_id).execute(&mut *tx).await?;+ } else {+ // The discarded draft may have been the thread's only signal message.+ super::thread::sync_thread_signal_flag(&mut tx, thread_db_id).await?;}tx.commit().await?;diff --git a/rust/cloud-storage/email/src/outbound/email_pg_repo/mod.rs b/rust/cloud-storage/email/src/outbound/email_pg_repo/mod.rsindex 351adbb53e..12ec663aeb 100644--- a/rust/cloud-storage/email/src/outbound/email_pg_repo/mod.rs+++ b/rust/cloud-storage/email/src/outbound/email_pg_repo/mod.rs@@ -321,31 +321,61 @@ impl EmailRepo for EmailPgRepo {link_id: Uuid,input: UpsertEmailFilterInput,) -> Result<EmailFilter, Self::Err> {- if let Some(address) = &input.email_address {+ // One transaction: the override and the is_signal resync it feeds+ // must land together, or a resync failure leaves stale flags.+ let mut tx = self.pool.begin().await?;++ let filter = if let Some(address) = &input.email_address {email_filter::upsert_email_filter_by_address(- &self.pool,+ &mut tx,link_id,address,input.is_important,)- .await+ .await?} else if let Some(domain) = &input.email_domain {email_filter::upsert_email_filter_by_domain(- &self.pool,+ &mut tx,link_id,domain,input.is_important,)- .await+ .await?} else {unreachable!("UpsertEmailFilterInput must have either email_address or email_domain; validated by service layer")- }+ };++ email_filter::resync_signal_flags_for_sender(+ &mut tx,+ link_id,+ filter.email_address.as_deref(),+ filter.email_domain.as_deref(),+ )+ .await?;++ tx.commit().await?;+ Ok(filter)}async fn delete_email_filter(&self, filter_id: Uuid, link_id: Uuid) -> Result<bool, Self::Err> {- email_filter::delete_email_filter(&self.pool, filter_id, link_id).await+ let mut tx = self.pool.begin().await?;++ let deleted = email_filter::delete_email_filter(&mut tx, filter_id, link_id).await?;++ if let Some(target) = &deleted {+ email_filter::resync_signal_flags_for_sender(+ &mut tx,+ link_id,+ target.email_address.as_deref(),+ target.email_domain.as_deref(),+ )+ .await?;+ }++ tx.commit().await?;+ Ok(deleted.is_some())}async fn list_email_filters(&self, link_id: Uuid) -> Result<Vec<EmailFilter>, Self::Err> {diff --git a/rust/cloud-storage/email/src/outbound/email_pg_repo/test/mod.rs b/rust/cloud-storage/email/src/outbound/email_pg_repo/test/mod.rsindex c277d27a2e..e435d32e92 100644--- a/rust/cloud-storage/email/src/outbound/email_pg_repo/test/mod.rs+++ b/rust/cloud-storage/email/src/outbound/email_pg_repo/test/mod.rs@@ -7,6 +7,7 @@ mod link;mod message;mod preview;mod project_scope_dynamic_query;+mod signal_flag;mod thread;mod thread_labels;diff --git a/rust/cloud-storage/email/src/outbound/email_pg_repo/test/signal_flag.rs b/rust/cloud-storage/email/src/outbound/email_pg_repo/test/signal_flag.rsnew file mode 100644index 0000000000..f27417a402--- /dev/null+++ b/rust/cloud-storage/email/src/outbound/email_pg_repo/test/signal_flag.rs@@ -0,0 +1,201 @@+use super::*;+use crate::domain::models::UpsertEmailFilterInput;++const LINK_ID: &str = "00000000-0000-0000-0000-000000000e01";+const THREAD_PLAIN_SIGNAL: &str = "00000000-0000-0000-0000-00000000e201";+const THREAD_PROMO_NOISE: &str = "00000000-0000-0000-0000-00000000e202";+const THREAD_STALE_SIGNAL: &str = "00000000-0000-0000-0000-00000000e203";++async fn fetch_signal(pool: &Pool<Postgres>, thread_id: &str) -> anyhow::Result<bool> {+ Ok(sqlx::query_scalar!(+ "SELECT is_signal FROM email_threads WHERE id = $1",+ Uuid::parse_str(thread_id)?+ )+ .fetch_one(pool)+ .await?)+}++// Upserting a mute filter for a sender flips their threads to noise via the+// fan-out resync; deleting the filter flips them back.+#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../../fixtures", scripts("email_signal_flag"))+)]+async fn filter_upsert_and_delete_resync_signal_flags(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let repo = EmailPgRepo::new(pool.clone());+ let link_id = Uuid::parse_str(LINK_ID)?;++ // Mute the sender: their unlabeled (signal) thread flips to noise.+ let filter = repo+ .upsert_email_filter(+ link_id,+ UpsertEmailFilterInput {+ email_address: Some("[email protected]".to_string()),+ email_domain: None,+ is_important: false,+ },+ )+ .await?;+ assert!(!fetch_signal(&pool, THREAD_PLAIN_SIGNAL).await?);++ // Deleting the filter restores the heuristic verdict.+ assert!(repo.delete_email_filter(filter.id, link_id).await?);+ assert!(fetch_signal(&pool, THREAD_PLAIN_SIGNAL).await?);++ Ok(())+}++// Marking a whole domain important promotes its category-labeled (noise)+// threads to signal.+#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../../fixtures", scripts("email_signal_flag"))+)]+async fn domain_filter_upsert_promotes_category_thread(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let repo = EmailPgRepo::new(pool.clone());+ let link_id = Uuid::parse_str(LINK_ID)?;++ assert!(!fetch_signal(&pool, THREAD_PROMO_NOISE).await?);++ repo.upsert_email_filter(+ link_id,+ UpsertEmailFilterInput {+ email_address: None,+ email_domain: Some("newsletter.com".to_string()),+ is_important: true,+ },+ )+ .await?;++ assert!(fetch_signal(&pool, THREAD_PROMO_NOISE).await?);+ Ok(())+}++// The email-crate update_thread_metadata port piggybacks the is_signal sync+// (clears a stale flag), matching the email_db_client behavior.+#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../../fixtures", scripts("email_signal_flag"))+)]+async fn update_thread_metadata_syncs_signal_flag(pool: Pool<Postgres>) -> anyhow::Result<()> {+ assert!(fetch_signal(&pool, THREAD_STALE_SIGNAL).await?);++ let mut tx = pool.begin().await?;+ super::super::thread::update_thread_metadata(+ &mut tx,+ Uuid::parse_str(THREAD_STALE_SIGNAL)?,+ Uuid::parse_str(LINK_ID)?,+ )+ .await?;+ tx.commit().await?;++ assert!(!fetch_signal(&pool, THREAD_STALE_SIGNAL).await?);+ Ok(())+}++const THREAD_DRAFT_SIGNAL: &str = "00000000-0000-0000-0000-00000000e204";+const DRAFT_MSG: &str = "00000000-0000-0000-0000-00000000e505";++// Discarding a draft via the new-crate delete_draft_message (no metadata+// recompute on this path) still refreshes is_signal.+#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../../fixtures", scripts("email_signal_flag"))+)]+async fn delete_draft_message_syncs_signal_flag(pool: Pool<Postgres>) -> anyhow::Result<()> {+ // The draft is the thread's only signal message.+ assert!(fetch_signal(&pool, THREAD_DRAFT_SIGNAL).await?);++ let repo = EmailPgRepo::new(pool.clone());+ repo.delete_draft_message(+ Uuid::parse_str(DRAFT_MSG)?,+ Uuid::parse_str(THREAD_DRAFT_SIGNAL)?,+ )+ .await?;++ assert!(!fetch_signal(&pool, THREAD_DRAFT_SIGNAL).await?);+ Ok(())+}++const THREAD_SAME_DOMAIN: &str = "00000000-0000-0000-0000-00000000e205";++// The fan-out resync applies address-beats-domain precedence: muting a+// domain flips all its threads, and an address-important exception flips+// only that sender's threads back.+#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../../fixtures", scripts("email_signal_flag"))+)]+async fn address_exception_beats_domain_mute_via_filter_mutations(+ pool: Pool<Postgres>,+) -> anyhow::Result<()> {+ let repo = EmailPgRepo::new(pool.clone());+ let link_id = Uuid::parse_str(LINK_ID)?;++ repo.upsert_email_filter(+ link_id,+ UpsertEmailFilterInput {+ email_address: None,+ email_domain: Some("example.com".to_string()),+ is_important: false,+ },+ )+ .await?;+ assert!(!fetch_signal(&pool, THREAD_PLAIN_SIGNAL).await?);+ assert!(!fetch_signal(&pool, THREAD_SAME_DOMAIN).await?);++ repo.upsert_email_filter(+ link_id,+ UpsertEmailFilterInput {+ email_address: Some("[email protected]".to_string()),+ email_domain: None,+ is_important: true,+ },+ )+ .await?;+ assert!(fetch_signal(&pool, THREAD_PLAIN_SIGNAL).await?);+ assert!(!fetch_signal(&pool, THREAD_SAME_DOMAIN).await?);++ Ok(())+}++// Deleting a domain mute resyncs the whole domain's threads; a coexisting+// address-important exception keeps its sender signal throughout.+#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../../fixtures", scripts("email_signal_flag"))+)]+async fn deleting_domain_filter_keeps_address_exception(+ pool: Pool<Postgres>,+) -> anyhow::Result<()> {+ let repo = EmailPgRepo::new(pool.clone());+ let link_id = Uuid::parse_str(LINK_ID)?;++ let domain_filter = repo+ .upsert_email_filter(+ link_id,+ UpsertEmailFilterInput {+ email_address: None,+ email_domain: Some("example.com".to_string()),+ is_important: false,+ },+ )+ .await?;+ repo.upsert_email_filter(+ link_id,+ UpsertEmailFilterInput {+ email_address: Some("[email protected]".to_string()),+ email_domain: None,+ is_important: true,+ },+ )+ .await?;+ assert!(fetch_signal(&pool, THREAD_PLAIN_SIGNAL).await?);+ assert!(!fetch_signal(&pool, THREAD_SAME_DOMAIN).await?);++ assert!(repo.delete_email_filter(domain_filter.id, link_id).await?);+ assert!(fetch_signal(&pool, THREAD_PLAIN_SIGNAL).await?);+ assert!(fetch_signal(&pool, THREAD_SAME_DOMAIN).await?);++ Ok(())+}diff --git a/rust/cloud-storage/email/src/outbound/email_pg_repo/thread.rs b/rust/cloud-storage/email/src/outbound/email_pg_repo/thread.rsindex 74b081e353..0720fe206f 100644--- a/rust/cloud-storage/email/src/outbound/email_pg_repo/thread.rs+++ b/rust/cloud-storage/email/src/outbound/email_pg_repo/thread.rs@@ -351,6 +351,120 @@ pub(super) async fn update_thread_metadata().await?;+ sync_thread_signal_flag(tx, thread_db_id).await?;++ Ok(())+}++/// Recomputes the denormalized `email_threads.is_signal` flag: true iff the+/// thread has a non-TRASH message matching the importance heuristic. Exact+/// copy of `email_db_client::threads::update::sync_thread_signal_flag`,+/// mirroring the Importance(true) predicate in the dynamic query builder.+pub(super) async fn sync_thread_signal_flag(+ tx: &mut sqlx::PgConnection,+ thread_db_id: Uuid,+) -> Result<(), sqlx::Error> {+ sqlx::query!(+ r#"+ UPDATE email_threads t+ SET is_signal = calc.sig+ FROM (+ SELECT EXISTS (+ SELECT 1+ FROM email_messages m+ WHERE m.thread_id = $1+ AND NOT EXISTS (+ SELECT 1 FROM email_message_labels ml+ JOIN email_labels l ON ml.label_id = l.id+ WHERE ml.message_id = m.id AND l.name = 'TRASH'+ )+ AND (+ (+ EXISTS (+ SELECT 1+ FROM email_contacts sender_c+ JOIN email_filters ef+ ON ef.link_id = m.link_id+ AND ef.email_address IS NOT NULL+ AND LOWER(ef.email_address) = LOWER(sender_c.email_address)+ WHERE sender_c.id = m.from_contact_id+ AND ef.is_important = TRUE+ )+ OR EXISTS (+ SELECT 1+ FROM email_contacts sender_c+ JOIN email_filters ef+ ON ef.link_id = m.link_id+ AND ef.email_domain IS NOT NULL+ AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))+ WHERE sender_c.id = m.from_contact_id+ AND ef.is_important = TRUE+ AND NOT EXISTS (+ SELECT 1 FROM email_filters ef_addr+ WHERE ef_addr.link_id = m.link_id+ AND ef_addr.email_address IS NOT NULL+ AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)+ AND ef_addr.is_important = FALSE+ )+ )+ )+ OR (+ NOT (+ EXISTS (+ SELECT 1+ FROM email_contacts sender_c+ JOIN email_filters ef+ ON ef.link_id = m.link_id+ AND ef.email_address IS NOT NULL+ AND LOWER(ef.email_address) = LOWER(sender_c.email_address)+ WHERE sender_c.id = m.from_contact_id+ AND ef.is_important = FALSE+ )+ OR EXISTS (+ SELECT 1+ FROM email_contacts sender_c+ JOIN email_filters ef+ ON ef.link_id = m.link_id+ AND ef.email_domain IS NOT NULL+ AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))+ WHERE sender_c.id = m.from_contact_id+ AND ef.is_important = FALSE+ AND NOT EXISTS (+ SELECT 1 FROM email_filters ef_addr+ WHERE ef_addr.link_id = m.link_id+ AND ef_addr.email_address IS NOT NULL+ AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)+ AND ef_addr.is_important = TRUE+ )+ )+ )+ AND (+ m.is_draft = TRUE+ OR EXISTS (+ SELECT 1 FROM email_message_labels ml+ JOIN email_labels l ON ml.label_id = l.id+ WHERE ml.message_id = m.id+ AND l.name IN ('CATEGORY_PERSONAL', 'SENT', 'DRAFT')+ )+ OR NOT EXISTS (+ SELECT 1 FROM email_message_labels ml+ JOIN email_labels l ON ml.label_id = l.id+ WHERE ml.message_id = m.id+ AND l.name IN ('CATEGORY_UPDATES', 'CATEGORY_PROMOTIONS', 'CATEGORY_SOCIAL', 'CATEGORY_FORUMS')+ )+ )+ )+ )+ ) AS sig+ ) calc+ WHERE t.id = $1+ AND t.is_signal IS DISTINCT FROM calc.sig+ "#,+ thread_db_id+ )+ .execute(tx)+ .await?;+Ok(())}diff --git a/rust/cloud-storage/email_db_client/fixtures/sync_thread_signal_flag.sql b/rust/cloud-storage/email_db_client/fixtures/sync_thread_signal_flag.sqlnew file mode 100644index 0000000000..7a5159b185--- /dev/null+++ b/rust/cloud-storage/email_db_client/fixtures/sync_thread_signal_flag.sql@@ -0,0 +1,87 @@+-- Fixture for the denormalized email_threads.is_signal flag.+--+-- Filters: [email protected] is_important=true (address), corp.com+-- is_important=false (domain) — address beats domain.+--+-- Thread d201 (flag=false): message from plain sender, no labels — signal.+-- Thread d202 (flag=true, stale on purpose): only a CATEGORY_PROMOTIONS+-- message — sync should clear to noise.+-- Thread d203 (flag=false): one promotions message + one unlabeled message —+-- any-message semantics makes it signal.+-- Thread d204 (flag=false): only message is TRASH — stays noise.+-- Thread d205 (flag=false): promotions message from [email protected] — address+-- override wins over both the domain override and the depriority label.+-- Thread d206 (flag=false): unlabeled message from [email protected] — domain+-- override (no address exception) forces noise.+-- Thread d207 (flag=false): draft with a promotions label — drafts are signal.+-- Thread d208 (flag=true, correct): promotions message + macro draft — the+-- draft is the only signal message; discarding it must flip the flag.++INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider, is_sync_active, created_at, updated_at)+VALUES ('00000000-0000-0000-0000-000000000d01', 'macro|[email protected]', '00000000-0000-0000-0000-000000000d01',+ '[email protected]', 'GMAIL', true, NOW(), NOW());++INSERT INTO email_contacts (id, link_id, email_address, created_at, updated_at)+VALUES ('00000000-0000-0000-0000-0000000cd001', '00000000-0000-0000-0000-000000000d01', '[email protected]', NOW(), NOW()),+ ('00000000-0000-0000-0000-0000000cd002', '00000000-0000-0000-0000-000000000d01', '[email protected]', NOW(), NOW()),+ ('00000000-0000-0000-0000-0000000cd003', '00000000-0000-0000-0000-000000000d01', '[email protected]', NOW(), NOW()),+ ('00000000-0000-0000-0000-0000000cd004', '00000000-0000-0000-0000-000000000d01', '[email protected]', NOW(), NOW());++INSERT INTO email_filters (id, link_id, email_address, email_domain, is_important, created_at)+VALUES ('00000000-0000-0000-0000-0000000fd001', '00000000-0000-0000-0000-000000000d01', '[email protected]', NULL, true, NOW()),+ ('00000000-0000-0000-0000-0000000fd002', '00000000-0000-0000-0000-000000000d01', NULL, 'corp.com', false, NOW());++INSERT INTO email_labels (id, link_id, provider_label_id, name, created_at)+VALUES ('00000000-0000-0000-0000-0000000bd001', '00000000-0000-0000-0000-000000000d01', 'TRASH', 'TRASH', NOW()),+ ('00000000-0000-0000-0000-0000000bd002', '00000000-0000-0000-0000-000000000d01', 'CATEGORY_PROMOTIONS', 'CATEGORY_PROMOTIONS', NOW());++INSERT INTO email_threads (id, link_id, inbox_visible, is_read, is_signal, created_at, updated_at)+VALUES ('00000000-0000-0000-0000-00000000d201', '00000000-0000-0000-0000-000000000d01', true, false, false, NOW(), NOW()),+ ('00000000-0000-0000-0000-00000000d202', '00000000-0000-0000-0000-000000000d01', true, false, true, NOW(), NOW()),+ ('00000000-0000-0000-0000-00000000d203', '00000000-0000-0000-0000-000000000d01', true, false, false, NOW(), NOW()),+ ('00000000-0000-0000-0000-00000000d204', '00000000-0000-0000-0000-000000000d01', true, false, false, NOW(), NOW()),+ ('00000000-0000-0000-0000-00000000d205', '00000000-0000-0000-0000-000000000d01', true, false, false, NOW(), NOW()),+ ('00000000-0000-0000-0000-00000000d206', '00000000-0000-0000-0000-000000000d01', true, false, false, NOW(), NOW()),+ ('00000000-0000-0000-0000-00000000d207', '00000000-0000-0000-0000-000000000d01', true, false, false, NOW(), NOW()),+ ('00000000-0000-0000-0000-00000000d208', '00000000-0000-0000-0000-000000000d01', true, false, true, NOW(), NOW());++INSERT INTO email_messages (id, thread_id, link_id, provider_id, global_id, is_sent, from_contact_id, internal_date_ts,+ has_attachments, is_read, is_starred, is_draft, created_at, updated_at)+VALUES ('00000000-0000-0000-0000-00000000d501', '00000000-0000-0000-0000-00000000d201', '00000000-0000-0000-0000-000000000d01',+ 'provider-msg-d501', 'gid-d501', FALSE, '00000000-0000-0000-0000-0000000cd001', '2025-01-05 10:00:00 +00:00',+ false, false, false, false, NOW(), NOW()),+ ('00000000-0000-0000-0000-00000000d502', '00000000-0000-0000-0000-00000000d202', '00000000-0000-0000-0000-000000000d01',+ 'provider-msg-d502', 'gid-d502', FALSE, '00000000-0000-0000-0000-0000000cd002', '2025-01-05 11:00:00 +00:00',+ false, false, false, false, NOW(), NOW()),+ ('00000000-0000-0000-0000-00000000d503', '00000000-0000-0000-0000-00000000d203', '00000000-0000-0000-0000-000000000d01',+ 'provider-msg-d503', 'gid-d503', FALSE, '00000000-0000-0000-0000-0000000cd002', '2025-01-05 12:00:00 +00:00',+ false, false, false, false, NOW(), NOW()),+ ('00000000-0000-0000-0000-00000000d504', '00000000-0000-0000-0000-00000000d203', '00000000-0000-0000-0000-000000000d01',+ 'provider-msg-d504', 'gid-d504', FALSE, '00000000-0000-0000-0000-0000000cd001', '2025-01-05 13:00:00 +00:00',+ false, false, false, false, NOW(), NOW()),+ ('00000000-0000-0000-0000-00000000d505', '00000000-0000-0000-0000-00000000d204', '00000000-0000-0000-0000-000000000d01',+ 'provider-msg-d505', 'gid-d505', FALSE, '00000000-0000-0000-0000-0000000cd001', '2025-01-05 14:00:00 +00:00',+ false, false, false, false, NOW(), NOW()),+ ('00000000-0000-0000-0000-00000000d506', '00000000-0000-0000-0000-00000000d205', '00000000-0000-0000-0000-000000000d01',+ 'provider-msg-d506', 'gid-d506', FALSE, '00000000-0000-0000-0000-0000000cd003', '2025-01-05 15:00:00 +00:00',+ false, false, false, false, NOW(), NOW()),+ ('00000000-0000-0000-0000-00000000d507', '00000000-0000-0000-0000-00000000d206', '00000000-0000-0000-0000-000000000d01',+ 'provider-msg-d507', 'gid-d507', FALSE, '00000000-0000-0000-0000-0000000cd004', '2025-01-05 16:00:00 +00:00',+ false, false, false, false, NOW(), NOW()),+ ('00000000-0000-0000-0000-00000000d508', '00000000-0000-0000-0000-00000000d207', '00000000-0000-0000-0000-000000000d01',+ 'provider-msg-d508', 'gid-d508', FALSE, '00000000-0000-0000-0000-0000000cd002', '2025-01-05 17:00:00 +00:00',+ false, false, false, true, NOW(), NOW()),+ ('00000000-0000-0000-0000-00000000d509', '00000000-0000-0000-0000-00000000d208', '00000000-0000-0000-0000-000000000d01',+ 'provider-msg-d509', 'gid-d509', FALSE, '00000000-0000-0000-0000-0000000cd002', '2025-01-05 18:00:00 +00:00',+ false, false, false, false, NOW(), NOW()),+ ('00000000-0000-0000-0000-00000000d510', '00000000-0000-0000-0000-00000000d208', '00000000-0000-0000-0000-000000000d01',+ NULL, NULL, FALSE, '00000000-0000-0000-0000-0000000cd001', '2025-01-05 19:00:00 +00:00',+ false, false, false, true, NOW(), NOW());++INSERT INTO email_message_labels (message_id, label_id)+VALUES ('00000000-0000-0000-0000-00000000d502', '00000000-0000-0000-0000-0000000bd002'),+ ('00000000-0000-0000-0000-00000000d503', '00000000-0000-0000-0000-0000000bd002'),+ ('00000000-0000-0000-0000-00000000d505', '00000000-0000-0000-0000-0000000bd001'),+ ('00000000-0000-0000-0000-00000000d506', '00000000-0000-0000-0000-0000000bd002'),+ ('00000000-0000-0000-0000-00000000d508', '00000000-0000-0000-0000-0000000bd002'),+ ('00000000-0000-0000-0000-00000000d509', '00000000-0000-0000-0000-0000000bd002');diff --git a/rust/cloud-storage/email_db_client/src/messages/delete.rs b/rust/cloud-storage/email_db_client/src/messages/delete.rsindex 18618261e7..71265eff6b 100644--- a/rust/cloud-storage/email_db_client/src/messages/delete.rs+++ b/rust/cloud-storage/email_db_client/src/messages/delete.rs@@ -31,6 +31,13 @@ pub async fn delete_message_with_tx(threads::update::sync_thread_calendar_flag(&mut *tx, message.thread_db_id).await?;}+ // Callers that skip the metadata recompute (draft discard) still need+ // is_signal refreshed — the deleted message may have been the thread's+ // only signal message, and macro drafts get no Gmail echo.+ if !deleted_thread && !update_thread_metadata {+ threads::update::sync_thread_signal_flag(&mut *tx, message.thread_db_id).await?;+ }+if deleted_thread {Ok(Some(message.thread_db_id))} else {diff --git a/rust/cloud-storage/email_db_client/src/threads/insert.rs b/rust/cloud-storage/email_db_client/src/threads/insert.rsindex 7ca666ac3f..c23780df5a 100644--- a/rust/cloud-storage/email_db_client/src/threads/insert.rs+++ b/rust/cloud-storage/email_db_client/src/threads/insert.rs@@ -66,6 +66,11 @@ pub async fn insert_thread_and_messages(update_thread_messages_replying_to(&mut tx, thread_id, link_id).await?;}+ // Messages were inserted with per-message metadata updates disabled and+ // insert_thread precomputes the other thread columns, so is_signal+ // must be synced here or new threads would stay noise.+ super::update::sync_thread_signal_flag(&mut tx, thread_id).await?;+Ok(thread_id)}.await;diff --git a/rust/cloud-storage/email_db_client/src/threads/update.rs b/rust/cloud-storage/email_db_client/src/threads/update.rsindex 09f23869b3..24e8710184 100644--- a/rust/cloud-storage/email_db_client/src/threads/update.rs+++ b/rust/cloud-storage/email_db_client/src/threads/update.rs@@ -170,6 +170,118 @@ pub async fn sync_thread_calendar_flag(Ok(())}+/// Recomputes the denormalized `email_threads.is_signal` flag: true iff the+/// thread has a non-TRASH message matching the importance heuristic. Mirrors+/// the Importance(true) predicate in the email crate's dynamic query builder.+#[tracing::instrument(skip(tx), err)]+pub async fn sync_thread_signal_flag(+ tx: &mut sqlx::PgConnection,+ thread_db_id: Uuid,+) -> anyhow::Result<()> {+ sqlx::query!(+ r#"+ UPDATE email_threads t+ SET is_signal = calc.sig+ FROM (+ SELECT EXISTS (+ SELECT 1+ FROM email_messages m+ WHERE m.thread_id = $1+ AND NOT EXISTS (+ SELECT 1 FROM email_message_labels ml+ JOIN email_labels l ON ml.label_id = l.id+ WHERE ml.message_id = m.id AND l.name = 'TRASH'+ )+ AND (+ (+ EXISTS (+ SELECT 1+ FROM email_contacts sender_c+ JOIN email_filters ef+ ON ef.link_id = m.link_id+ AND ef.email_address IS NOT NULL+ AND LOWER(ef.email_address) = LOWER(sender_c.email_address)+ WHERE sender_c.id = m.from_contact_id+ AND ef.is_important = TRUE+ )+ OR EXISTS (+ SELECT 1+ FROM email_contacts sender_c+ JOIN email_filters ef+ ON ef.link_id = m.link_id+ AND ef.email_domain IS NOT NULL+ AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))+ WHERE sender_c.id = m.from_contact_id+ AND ef.is_important = TRUE+ AND NOT EXISTS (+ SELECT 1 FROM email_filters ef_addr+ WHERE ef_addr.link_id = m.link_id+ AND ef_addr.email_address IS NOT NULL+ AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)+ AND ef_addr.is_important = FALSE+ )+ )+ )+ OR (+ NOT (+ EXISTS (+ SELECT 1+ FROM email_contacts sender_c+ JOIN email_filters ef+ ON ef.link_id = m.link_id+ AND ef.email_address IS NOT NULL+ AND LOWER(ef.email_address) = LOWER(sender_c.email_address)+ WHERE sender_c.id = m.from_contact_id+ AND ef.is_important = FALSE+ )+ OR EXISTS (+ SELECT 1+ FROM email_contacts sender_c+ JOIN email_filters ef+ ON ef.link_id = m.link_id+ AND ef.email_domain IS NOT NULL+ AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))+ WHERE sender_c.id = m.from_contact_id+ AND ef.is_important = FALSE+ AND NOT EXISTS (+ SELECT 1 FROM email_filters ef_addr+ WHERE ef_addr.link_id = m.link_id+ AND ef_addr.email_address IS NOT NULL+ AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)+ AND ef_addr.is_important = TRUE+ )+ )+ )+ AND (+ m.is_draft = TRUE+ OR EXISTS (+ SELECT 1 FROM email_message_labels ml+ JOIN email_labels l ON ml.label_id = l.id+ WHERE ml.message_id = m.id+ AND l.name IN ('CATEGORY_PERSONAL', 'SENT', 'DRAFT')+ )+ OR NOT EXISTS (+ SELECT 1 FROM email_message_labels ml+ JOIN email_labels l ON ml.label_id = l.id+ WHERE ml.message_id = m.id+ AND l.name IN ('CATEGORY_UPDATES', 'CATEGORY_PROMOTIONS', 'CATEGORY_SOCIAL', 'CATEGORY_FORUMS')+ )+ )+ )+ )+ ) AS sig+ ) calc+ WHERE t.id = $1+ AND t.is_signal IS DISTINCT FROM calc.sig+ "#,+ thread_db_id+ )+ .execute(tx)+ .await?;++ Ok(())+}+// Updates a thread's metadata (archived status, latest_timestamps)#[tracing::instrument(skip(tx), err)]pub async fn update_thread_metadata(@@ -243,5 +355,7 @@ pub async fn update_thread_metadata().await?;+ sync_thread_signal_flag(&mut *tx, thread_db_id).await?;+Ok(())}diff --git a/rust/cloud-storage/email_db_client/src/threads/update/test.rs b/rust/cloud-storage/email_db_client/src/threads/update/test.rsindex 7292f5a3da..adc5afddbe 100644--- a/rust/cloud-storage/email_db_client/src/threads/update/test.rs+++ b/rust/cloud-storage/email_db_client/src/threads/update/test.rs@@ -62,3 +62,191 @@ async fn sync_is_stable_when_flag_already_matches(pool: Pool<Postgres>) -> anyhoassert!(fetch_flag(&pool, THREAD_WITH_ICS).await?);Ok(())}++// -- is_signal ---------------------------------------------------------------++const SIG_THREAD_UNLABELED: &str = "00000000-0000-0000-0000-00000000d201";+const SIG_THREAD_STALE_PROMO: &str = "00000000-0000-0000-0000-00000000d202";+const SIG_THREAD_MIXED: &str = "00000000-0000-0000-0000-00000000d203";+const SIG_THREAD_TRASH_ONLY: &str = "00000000-0000-0000-0000-00000000d204";+const SIG_THREAD_VIP_ADDRESS: &str = "00000000-0000-0000-0000-00000000d205";+const SIG_THREAD_DOMAIN_MUTED: &str = "00000000-0000-0000-0000-00000000d206";+const SIG_THREAD_DRAFT: &str = "00000000-0000-0000-0000-00000000d207";++async fn fetch_signal(pool: &Pool<Postgres>, thread_id: &str) -> anyhow::Result<bool> {+ Ok(sqlx::query_scalar!(+ "SELECT is_signal FROM email_threads WHERE id = $1",+ Uuid::parse_str(thread_id)?+ )+ .fetch_one(pool)+ .await?)+}++async fn sync_signal(pool: &Pool<Postgres>, thread_id: &str) -> anyhow::Result<()> {+ let mut conn = pool.acquire().await?;+ sync_thread_signal_flag(&mut conn, Uuid::parse_str(thread_id)?).await+}++// A message with no category labels counts as signal by default.+#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("sync_thread_signal_flag"))+)]+async fn signal_set_for_unlabeled_message(pool: Pool<Postgres>) -> anyhow::Result<()> {+ assert!(!fetch_signal(&pool, SIG_THREAD_UNLABELED).await?);+ sync_signal(&pool, SIG_THREAD_UNLABELED).await?;+ assert!(fetch_signal(&pool, SIG_THREAD_UNLABELED).await?);+ Ok(())+}++// A stale true flag is cleared when the thread's only message carries a+// depriority category label.+#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("sync_thread_signal_flag"))+)]+async fn signal_cleared_for_category_labeled_thread(pool: Pool<Postgres>) -> anyhow::Result<()> {+ // Fixture marks this thread signal even though its only message is+ // CATEGORY_PROMOTIONS.+ assert!(fetch_signal(&pool, SIG_THREAD_STALE_PROMO).await?);+ sync_signal(&pool, SIG_THREAD_STALE_PROMO).await?;+ assert!(!fetch_signal(&pool, SIG_THREAD_STALE_PROMO).await?);+ Ok(())+}++// Any-message semantics: one signal message outweighs noise siblings.+#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("sync_thread_signal_flag"))+)]+async fn signal_set_when_any_message_matches(pool: Pool<Postgres>) -> anyhow::Result<()> {+ // One promotions message + one unlabeled message: the unlabeled one wins.+ sync_signal(&pool, SIG_THREAD_MIXED).await?;+ assert!(fetch_signal(&pool, SIG_THREAD_MIXED).await?);+ Ok(())+}++// TRASH messages are excluded: they can't make a thread signal.+#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("sync_thread_signal_flag"))+)]+async fn signal_ignores_trashed_messages(pool: Pool<Postgres>) -> anyhow::Result<()> {+ // Only message is TRASH: it would otherwise be signal (no category labels).+ sync_signal(&pool, SIG_THREAD_TRASH_ONLY).await?;+ assert!(!fetch_signal(&pool, SIG_THREAD_TRASH_ONLY).await?);+ Ok(())+}++// An important-address filter outranks both a muted-domain filter and a+// depriority category label.+#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("sync_thread_signal_flag"))+)]+async fn signal_address_override_beats_domain_and_labels(+ pool: Pool<Postgres>,+) -> anyhow::Result<()> {+ // [email protected] is important by address even though corp.com is muted by+ // domain and the message carries a depriority label.+ sync_signal(&pool, SIG_THREAD_VIP_ADDRESS).await?;+ assert!(fetch_signal(&pool, SIG_THREAD_VIP_ADDRESS).await?);+ Ok(())+}++// A muted-domain filter forces noise when the sender has no address-level+// exception, even for an otherwise-signal message.+#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("sync_thread_signal_flag"))+)]+async fn signal_domain_override_mutes_unlabeled_message(+ pool: Pool<Postgres>,+) -> anyhow::Result<()> {+ // [email protected] has no address exception, so the domain mute applies.+ sync_signal(&pool, SIG_THREAD_DOMAIN_MUTED).await?;+ assert!(!fetch_signal(&pool, SIG_THREAD_DOMAIN_MUTED).await?);+ Ok(())+}++// Drafts are always signal, even when labeled with a depriority category.+#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("sync_thread_signal_flag"))+)]+async fn signal_set_for_draft_despite_category_label(pool: Pool<Postgres>) -> anyhow::Result<()> {+ sync_signal(&pool, SIG_THREAD_DRAFT).await?;+ assert!(fetch_signal(&pool, SIG_THREAD_DRAFT).await?);+ Ok(())+}++// Discarding a draft via delete_message_with_tx(update_thread_metadata =+// false) — the drafts delete API path — must still refresh is_signal.+#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("sync_thread_signal_flag"))+)]+async fn draft_discard_without_metadata_recompute_syncs_signal_flag(+ pool: Pool<Postgres>,+) -> anyhow::Result<()> {+ const THREAD: &str = "00000000-0000-0000-0000-00000000d208";+ const DRAFT_MSG: &str = "00000000-0000-0000-0000-00000000d510";++ assert!(fetch_signal(&pool, THREAD).await?);++ let now = chrono::Utc::now();+ let draft = models_email::email::service::message::SimpleMessage {+ db_id: Uuid::parse_str(DRAFT_MSG)?,+ provider_id: None,+ thread_db_id: Uuid::parse_str(THREAD)?,+ provider_thread_id: None,+ replying_to_id: None,+ global_id: String::new(),+ link_id: Uuid::parse_str("00000000-0000-0000-0000-000000000d01")?,+ subject: None,+ snippet: None,+ from_contact_id: None,+ provider_history_id: None,+ internal_date_ts: None,+ sent_at: None,+ size_estimate: None,+ is_read: false,+ is_starred: false,+ is_sent: false,+ is_draft: true,+ has_attachments: false,+ headers_json: None,+ created_at: now,+ updated_at: now,+ };++ let mut conn = pool.acquire().await?;+ let deleted_thread =+ crate::messages::delete::delete_message_with_tx(&mut conn, &draft, false).await?;+ assert!(deleted_thread.is_none(), "thread should survive");++ // The remaining message is CATEGORY_PROMOTIONS-only: noise.+ assert!(!fetch_signal(&pool, THREAD).await?);+ Ok(())+}++// update_thread_metadata piggybacks the is_signal sync, so every existing+// metadata-recompute call site keeps the flag fresh.+#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("sync_thread_signal_flag"))+)]+async fn update_thread_metadata_syncs_signal_flag(pool: Pool<Postgres>) -> anyhow::Result<()> {+ assert!(fetch_signal(&pool, SIG_THREAD_STALE_PROMO).await?);++ let mut conn = pool.acquire().await?;+ update_thread_metadata(+ &mut conn,+ Uuid::parse_str(SIG_THREAD_STALE_PROMO)?,+ Uuid::parse_str("00000000-0000-0000-0000-000000000d01")?,+ )+ .await?;++ assert!(!fetch_signal(&pool, SIG_THREAD_STALE_PROMO).await?);+ Ok(())+}diff --git a/rust/cloud-storage/email_service/Cargo.toml b/rust/cloud-storage/email_service/Cargo.tomlindex de78e5c265..8bcf460d69 100644--- a/rust/cloud-storage/email_service/Cargo.toml+++ b/rust/cloud-storage/email_service/Cargo.toml@@ -17,6 +17,10 @@ path = "src/bin/backfill_contacts/backfill_contacts.rs"name = "backfill_calendar_flags"path = "src/bin/backfill_calendar_flags/backfill_calendar_flags.rs"+[[bin]]+name = "backfill_signal_flags"+path = "src/bin/backfill_signal_flags/backfill_signal_flags.rs"+[[bin]]name = "backfill_gmail_attachments"path = "src/bin/backfill_gmail_attachments/backfill_gmail_attachments.rs"diff --git a/rust/cloud-storage/email_service/src/bin/backfill_signal_flags/backfill_signal_flags.rs b/rust/cloud-storage/email_service/src/bin/backfill_signal_flags/backfill_signal_flags.rsnew file mode 100644index 0000000000..5ca8936625--- /dev/null+++ b/rust/cloud-storage/email_service/src/bin/backfill_signal_flags/backfill_signal_flags.rs@@ -0,0 +1,106 @@+//! # Signal Flag Backfill Utility+//!+//! Backfills the denormalized `email_threads.is_signal` column: for each+//! user, flags every thread that has at least one non-TRASH message matching+//! the importance heuristic (email_filters sender overrides, category+//! labels, drafts). By default only ever sets the flag to true — steady-state+//! maintenance (including clearing it) is handled by update_thread_metadata+//! in `email_db_client` and the `email` crate. Set `FULL_RECOMPUTE=true` to+//! also clear stale true flags (full bidirectional recompute per link).+//!+//! ## Required Environment Variables:+//! - `DATABASE_URL`: The connection string for the PostgreSQL database.+//!+//! ## Optional Environment Variables:+//! - `MACRO_IDS`: Comma-separated macro IDs to backfill. When unset, every+//! user with an email link is processed.+//! - `CONCURRENCY`: Number of users processed concurrently (defaults to 1).+//! - `FULL_RECOMPUTE`: When `true`, recomputes both directions instead of+//! the default set-true-only pass (defaults to false).++mod config;+mod process;++use anyhow::Context;+use futures::stream::{self, StreamExt};+use macro_entrypoint::MacroEntrypoint;+use sqlx::postgres::PgPoolOptions;++#[tokio::main]+async fn main() -> anyhow::Result<()> {+ println!("Loading configuration...");+ MacroEntrypoint::default().init();+ let config = config::Config::from_env().context("Failed to load configuration")?;++ println!("Connecting to the database...");+ let db_pool = PgPoolOptions::new()+ .min_connections(1)+ .max_connections(config.concurrency as u32)+ .connect(&config.database_url)+ .await+ .context("Could not connect to db")?;++ let macro_ids: Vec<String> = match &config.macro_ids {+ Some(ids) => ids.split(',').map(|id| id.trim().to_string()).collect(),+ None => process::fetch_all_macro_ids(&db_pool)+ .await+ .context("Failed to fetch macro IDs")?,+ };++ let total_users = macro_ids.len();+ println!(+ "Processing {total_users} macro IDs with concurrency {}",+ config.concurrency+ );++ let full_recompute = config.full_recompute;+ let results: Vec<(String, anyhow::Result<u64>)> = stream::iter(macro_ids)+ .map(|macro_id| {+ let db_pool = db_pool.clone();+ async move {+ let result = process::process_macro_id(&db_pool, ¯o_id, full_recompute).await;+ (macro_id, result)+ }+ })+ .buffer_unordered(config.concurrency)+ .enumerate()+ .map(|(index, (macro_id, result))| {+ match &result {+ Ok(flagged) => println!(+ "=== Completed {macro_id} ({}/{total_users}): flagged {flagged} threads ===",+ index + 1+ ),+ Err(e) => println!(+ "=== Failed {macro_id} ({}/{total_users}): {e:?} ===",+ index + 1+ ),+ }+ (macro_id, result)+ })+ .collect()+ .await;++ let total_flagged: u64 = results.iter().filter_map(|(_, r)| r.as_ref().ok()).sum();+ let failures: Vec<&String> = results+ .iter()+ .filter(|(_, r)| r.is_err())+ .map(|(id, _)| id)+ .collect();++ println!(+ "\n=== All macro IDs processed: {total_flagged} threads flagged, {} failures ===",+ failures.len()+ );+ if !failures.is_empty() {+ anyhow::bail!(+ "{} macro IDs failed ({}); rerun to retry (already-flagged threads are skipped)",+ failures.len(),+ failures+ .iter()+ .map(|s| s.as_str())+ .collect::<Vec<_>>()+ .join(", ")+ );+ }+ Ok(())+}diff --git a/rust/cloud-storage/email_service/src/bin/backfill_signal_flags/config.rs b/rust/cloud-storage/email_service/src/bin/backfill_signal_flags/config.rsnew file mode 100644index 0000000000..4ab048cc6d--- /dev/null+++ b/rust/cloud-storage/email_service/src/bin/backfill_signal_flags/config.rs@@ -0,0 +1,51 @@+use anyhow::Context;+use macro_env_var::env_vars;++/// Holds all configuration loaded from environment variables.+pub struct Config {+ /// Comma-separated macro IDs to backfill. When unset, every user with an+ /// email link is processed.+ pub macro_ids: Option<String>,+ pub database_url: String,+ /// Number of users processed concurrently. Defaults to 1, capped at 50+ /// to bound DB connection usage.+ pub concurrency: usize,+ /// When true, recomputes is_signal in both directions (clears stale true+ /// flags) instead of the default set-true-only pass.+ pub full_recompute: bool,+}++env_vars! {+ struct MacroIds;+ struct DatabaseUrl;+ struct Concurrency;+ struct FullRecompute;+}++impl Config {+ /// Creates a new `Config` instance by reading from environment variables.+ /// Returns an error if any required variable is not set.+ pub fn from_env() -> anyhow::Result<Self> {+ let concurrency = match Concurrency::new().ok() {+ Some(v) => v+ .parse::<usize>()+ .context("CONCURRENCY is not a number")?+ .clamp(1, 50),+ None => 1,+ };++ let full_recompute = match FullRecompute::new().ok() {+ Some(v) => v.parse::<bool>().context("FULL_RECOMPUTE is not a bool")?,+ None => false,+ };++ Ok(Self {+ macro_ids: MacroIds::new().ok().map(|v| v.to_string()),+ database_url: DatabaseUrl::new()+ .context("DATABASE_URL not set")?+ .to_string(),+ concurrency,+ full_recompute,+ })+ }+}diff --git a/rust/cloud-storage/email_service/src/bin/backfill_signal_flags/process.rs b/rust/cloud-storage/email_service/src/bin/backfill_signal_flags/process.rsnew file mode 100644index 0000000000..bcb6d4a55e--- /dev/null+++ b/rust/cloud-storage/email_service/src/bin/backfill_signal_flags/process.rs@@ -0,0 +1,166 @@+use sqlx::types::Uuid;++/// Flags every thread on one of the user's links that has at least one+/// non-TRASH message matching the importance heuristic. One statement per+/// link keeps transactions small and the progress output per-mailbox.+/// With `full_recompute`, stale true flags are also cleared (clear + re-set+/// in one transaction per link). Returns the number of threads flagged.+pub async fn process_macro_id(+ pool: &sqlx::PgPool,+ macro_id: &str,+ full_recompute: bool,+) -> anyhow::Result<u64> {+ let link_ids: Vec<Uuid> =+ sqlx::query_scalar!("SELECT id FROM email_links WHERE macro_id = $1", macro_id)+ .fetch_all(pool)+ .await?;++ if link_ids.is_empty() {+ println!("No email links found for {macro_id}.");+ return Ok(0);+ }++ let mut total_flagged = 0u64;+ for link_id in link_ids {+ let mut tx = pool.begin().await?;++ // Full recompute: clear everything first so the set-true pass below+ // yields exactly the heuristic verdict. Same transaction, so readers+ // never observe the intermediate all-false state.+ if full_recompute {+ let cleared = sqlx::query!(+ r#"+ UPDATE email_threads+ SET is_signal = false+ WHERE link_id = $1 AND is_signal+ "#,+ link_id+ )+ .execute(&mut *tx)+ .await?+ .rows_affected();+ println!("[{macro_id}] link {link_id}: cleared {cleared} flags for recompute");+ }++ // Mirrors sync_thread_signal_flag (email_db_client/threads/update.rs)+ // and the Importance(true) predicate in the dynamic query builder.+ let flagged = sqlx::query!(+ r#"+ UPDATE email_threads t+ SET is_signal = true+ FROM (+ SELECT DISTINCT m.thread_id+ FROM email_messages m+ WHERE m.link_id = $1+ AND NOT EXISTS (+ SELECT 1 FROM email_message_labels ml+ JOIN email_labels l ON ml.label_id = l.id+ WHERE ml.message_id = m.id AND l.name = 'TRASH'+ )+ AND (+ (+ EXISTS (+ SELECT 1+ FROM email_contacts sender_c+ JOIN email_filters ef+ ON ef.link_id = m.link_id+ AND ef.email_address IS NOT NULL+ AND LOWER(ef.email_address) = LOWER(sender_c.email_address)+ WHERE sender_c.id = m.from_contact_id+ AND ef.is_important = TRUE+ )+ OR EXISTS (+ SELECT 1+ FROM email_contacts sender_c+ JOIN email_filters ef+ ON ef.link_id = m.link_id+ AND ef.email_domain IS NOT NULL+ AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))+ WHERE sender_c.id = m.from_contact_id+ AND ef.is_important = TRUE+ AND NOT EXISTS (+ SELECT 1 FROM email_filters ef_addr+ WHERE ef_addr.link_id = m.link_id+ AND ef_addr.email_address IS NOT NULL+ AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)+ AND ef_addr.is_important = FALSE+ )+ )+ )+ OR (+ NOT (+ EXISTS (+ SELECT 1+ FROM email_contacts sender_c+ JOIN email_filters ef+ ON ef.link_id = m.link_id+ AND ef.email_address IS NOT NULL+ AND LOWER(ef.email_address) = LOWER(sender_c.email_address)+ WHERE sender_c.id = m.from_contact_id+ AND ef.is_important = FALSE+ )+ OR EXISTS (+ SELECT 1+ FROM email_contacts sender_c+ JOIN email_filters ef+ ON ef.link_id = m.link_id+ AND ef.email_domain IS NOT NULL+ AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))+ WHERE sender_c.id = m.from_contact_id+ AND ef.is_important = FALSE+ AND NOT EXISTS (+ SELECT 1 FROM email_filters ef_addr+ WHERE ef_addr.link_id = m.link_id+ AND ef_addr.email_address IS NOT NULL+ AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)+ AND ef_addr.is_important = TRUE+ )+ )+ )+ AND (+ m.is_draft = TRUE+ OR EXISTS (+ SELECT 1 FROM email_message_labels ml+ JOIN email_labels l ON ml.label_id = l.id+ WHERE ml.message_id = m.id+ AND l.name IN ('CATEGORY_PERSONAL', 'SENT', 'DRAFT')+ )+ OR NOT EXISTS (+ SELECT 1 FROM email_message_labels ml+ JOIN email_labels l ON ml.label_id = l.id+ WHERE ml.message_id = m.id+ AND l.name IN ('CATEGORY_UPDATES', 'CATEGORY_PROMOTIONS', 'CATEGORY_SOCIAL', 'CATEGORY_FORUMS')+ )+ )+ )+ )+ ) sig+ WHERE t.id = sig.thread_id+ AND NOT t.is_signal+ "#,+ link_id+ )+ .execute(&mut *tx)+ .await?+ .rows_affected();++ tx.commit().await?;++ // Prefix with the user so interleaved concurrent output stays readable.+ println!("[{macro_id}] link {link_id}: flagged {flagged} threads");+ total_flagged += flagged;+ }++ Ok(total_flagged)+}++/// Every macro ID that owns at least one email link. Connected secondary+/// mailboxes carry their own macro_id row in email_links, so iterating these+/// covers every link exactly once.+pub async fn fetch_all_macro_ids(pool: &sqlx::PgPool) -> anyhow::Result<Vec<String>> {+ Ok(+ sqlx::query_scalar!("SELECT DISTINCT macro_id FROM email_links ORDER BY macro_id")+ .fetch_all(pool)+ .await?,+ )+}diff --git a/rust/cloud-storage/macro_db_client/migrations/20260708215614_add_email_threads_is_signal.sql b/rust/cloud-storage/macro_db_client/migrations/20260708215614_add_email_threads_is_signal.sqlnew file mode 100644index 0000000000..72930e1fa1--- /dev/null+++ b/rust/cloud-storage/macro_db_client/migrations/20260708215614_add_email_threads_is_signal.sql@@ -0,0 +1,8 @@+-- Denormalized "thread has a signal (important) message" flag for the+-- Importance soup filter. Deriving it at query time evaluates the per-message+-- importance heuristic (category labels + email_filters sender overrides)+-- twice per preview query. Maintained by update_thread_metadata+-- (email_db_client and the email crate); existing threads are flagged by the+-- backfill_signal_flags util in email_service.+ALTER TABLE email_threads+ ADD COLUMN IF NOT EXISTS is_signal boolean NOT NULL DEFAULT false;diff --git a/rust/cloud-storage/macro_db_client/migrations/20260708215625_email_threads_signal_view_index.sql b/rust/cloud-storage/macro_db_client/migrations/20260708215625_email_threads_signal_view_index.sqlnew file mode 100644index 0000000000..f1d844a404--- /dev/null+++ b/rust/cloud-storage/macro_db_client/migrations/20260708215625_email_threads_signal_view_index.sql@@ -0,0 +1,7 @@+-- no-transaction+-- Matches the Important-tab candidate scan (inbox view sort field + id+-- tiebreak) so per-link ordered scans stop at the LIMIT. Noise rides the+-- existing idx_email_threads_inbox_view_not_null and skips signal rows.+CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_email_threads_signal_view+ ON email_threads (link_id, latest_inbound_message_ts DESC, id DESC)+ WHERE inbox_visible AND is_signal AND latest_inbound_message_ts IS NOT NULL;