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 no-op guard thread upsert (#5498)
Will Hutchinson88ebb6337882
diff --git a/.sqlx/query-6c022c7b1e5e5faf6f388528436453738aa66c36c46313ca18c7570b406553f6.json b/.sqlx/query-169514afd604067ffe4c814326807107ceab1d2dee771eed2cd3f3b6014eef99.jsonsimilarity index 66%rename from .sqlx/query-6c022c7b1e5e5faf6f388528436453738aa66c36c46313ca18c7570b406553f6.jsonrename to .sqlx/query-169514afd604067ffe4c814326807107ceab1d2dee771eed2cd3f3b6014eef99.jsonindex 2f59f37298..ae700070f8 100644--- a/.sqlx/query-6c022c7b1e5e5faf6f388528436453738aa66c36c46313ca18c7570b406553f6.json+++ b/.sqlx/query-169514afd604067ffe4c814326807107ceab1d2dee771eed2cd3f3b6014eef99.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n UPDATE email_threads\n SET\n inbox_visible = $1,\n is_read = $2,\n latest_inbound_message_ts = $3,\n latest_outbound_message_ts = $4,\n latest_non_spam_message_ts = $5,\n updated_at = NOW()\n WHERE\n id = $6 AND\n link_id = $7\n ",+ "query": "\n UPDATE email_threads\n SET\n inbox_visible = $1,\n is_read = $2,\n latest_inbound_message_ts = $3,\n latest_outbound_message_ts = $4,\n latest_non_spam_message_ts = $5,\n updated_at = NOW()\n WHERE\n id = $6 AND\n link_id = $7 AND\n (inbox_visible, is_read, latest_inbound_message_ts, latest_outbound_message_ts, latest_non_spam_message_ts)\n IS DISTINCT FROM ($1, $2, $3, $4, $5)\n ","describe": {"columns": [],"parameters": {@@ -16,5 +16,5 @@},"nullable": []},- "hash": "6c022c7b1e5e5faf6f388528436453738aa66c36c46313ca18c7570b406553f6"+ "hash": "169514afd604067ffe4c814326807107ceab1d2dee771eed2cd3f3b6014eef99"}diff --git a/.sqlx/query-6a4b0f50cd002b80b9fc97857a51f0fe7e15d7dca88fbfd082c51915aa0f32d5.json b/.sqlx/query-6a4b0f50cd002b80b9fc97857a51f0fe7e15d7dca88fbfd082c51915aa0f32d5.jsonnew file mode 100644index 0000000000..26bcf8b596--- /dev/null+++ b/.sqlx/query-6a4b0f50cd002b80b9fc97857a51f0fe7e15d7dca88fbfd082c51915aa0f32d5.json@@ -0,0 +1,23 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT id FROM email_threads\n WHERE link_id = $1 AND provider_id = $2\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id",+ "type_info": "Uuid"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid",+ "Text"+ ]+ },+ "nullable": [+ false+ ]+ },+ "hash": "6a4b0f50cd002b80b9fc97857a51f0fe7e15d7dca88fbfd082c51915aa0f32d5"+}diff --git a/.sqlx/query-e7e9e2db72c9bf45273b0cfe11057a5dbce790572f2420457e24b0fd498c2024.json b/.sqlx/query-75e7a821188bd178bf9b8c47d7bdc745af3c9a26b436030b46d331cad207f869.jsonsimilarity index 71%rename from .sqlx/query-e7e9e2db72c9bf45273b0cfe11057a5dbce790572f2420457e24b0fd498c2024.jsonrename to .sqlx/query-75e7a821188bd178bf9b8c47d7bdc745af3c9a26b436030b46d331cad207f869.jsonindex 5d51a7025e..ee17d4ce96 100644--- a/.sqlx/query-e7e9e2db72c9bf45273b0cfe11057a5dbce790572f2420457e24b0fd498c2024.json+++ b/.sqlx/query-75e7a821188bd178bf9b8c47d7bdc745af3c9a26b436030b46d331cad207f869.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n INSERT INTO email_threads (id, provider_id, link_id, inbox_visible, is_read,\n latest_inbound_message_ts, latest_outbound_message_ts,\n latest_non_spam_message_ts)\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8)\n ON CONFLICT (link_id, provider_id) WHERE provider_id IS NOT NULL DO UPDATE\n SET\n latest_inbound_message_ts = EXCLUDED.latest_inbound_message_ts,\n updated_at = NOW()\n RETURNING id\n ",+ "query": "\n INSERT INTO email_threads (id, provider_id, link_id, inbox_visible, is_read,\n latest_inbound_message_ts, latest_outbound_message_ts,\n latest_non_spam_message_ts)\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8)\n ON CONFLICT (link_id, provider_id) WHERE provider_id IS NOT NULL DO UPDATE\n SET\n latest_inbound_message_ts = EXCLUDED.latest_inbound_message_ts,\n updated_at = NOW()\n WHERE\n EXCLUDED.latest_inbound_message_ts IS NOT NULL AND\n email_threads.latest_inbound_message_ts IS DISTINCT FROM EXCLUDED.latest_inbound_message_ts\n RETURNING id\n ","describe": {"columns": [{@@ -25,5 +25,5 @@false]},- "hash": "e7e9e2db72c9bf45273b0cfe11057a5dbce790572f2420457e24b0fd498c2024"+ "hash": "75e7a821188bd178bf9b8c47d7bdc745af3c9a26b436030b46d331cad207f869"}diff --git a/.sqlx/query-614ef7fa61ce3e4521e8923ebe706ae249491210d011843f13d67214f65565c3.json b/.sqlx/query-9d126060416b51acfbcb4e9787954d421138c57da8a6efae63652cded88de19f.jsonsimilarity index 72%rename from .sqlx/query-614ef7fa61ce3e4521e8923ebe706ae249491210d011843f13d67214f65565c3.jsonrename to .sqlx/query-9d126060416b51acfbcb4e9787954d421138c57da8a6efae63652cded88de19f.jsonindex 989dd359c2..3931a79110 100644--- a/.sqlx/query-614ef7fa61ce3e4521e8923ebe706ae249491210d011843f13d67214f65565c3.json+++ b/.sqlx/query-9d126060416b51acfbcb4e9787954d421138c57da8a6efae63652cded88de19f.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n INSERT INTO email_threads (id, provider_id, link_id, inbox_visible, is_read, latest_inbound_message_ts,\n latest_outbound_message_ts, latest_non_spam_message_ts)\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8)\n ON CONFLICT (link_id, provider_id) WHERE provider_id IS NOT NULL DO UPDATE\n SET\n latest_inbound_message_ts = EXCLUDED.latest_inbound_message_ts,\n updated_at = NOW()\n RETURNING id\n ",+ "query": "\n INSERT INTO email_threads (id, provider_id, link_id, inbox_visible, is_read, latest_inbound_message_ts,\n latest_outbound_message_ts, latest_non_spam_message_ts)\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8)\n ON CONFLICT (link_id, provider_id) WHERE provider_id IS NOT NULL DO UPDATE\n SET\n latest_inbound_message_ts = EXCLUDED.latest_inbound_message_ts,\n updated_at = NOW()\n WHERE\n EXCLUDED.latest_inbound_message_ts IS NOT NULL AND\n email_threads.latest_inbound_message_ts IS DISTINCT FROM EXCLUDED.latest_inbound_message_ts\n RETURNING id\n ","describe": {"columns": [{@@ -25,5 +25,5 @@false]},- "hash": "614ef7fa61ce3e4521e8923ebe706ae249491210d011843f13d67214f65565c3"+ "hash": "9d126060416b51acfbcb4e9787954d421138c57da8a6efae63652cded88de19f"}diff --git a/crates/email/src/outbound/email_pg_repo/thread.rs b/crates/email/src/outbound/email_pg_repo/thread.rsindex 339c023fcd..18a9f297b0 100644--- a/crates/email/src/outbound/email_pg_repo/thread.rs+++ b/crates/email/src/outbound/email_pg_repo/thread.rs@@ -169,12 +169,16 @@ pub(super) async fn cross_inbox_reply_drafts(}/// Insert a new thread record within a transaction.+///+/// On conflict the update is skipped entirely when it would be a no-op+/// (incoming `latest_inbound_message_ts` is NULL or unchanged), so redundant+/// inserts neither rewrite the row nor wipe a populated timestamp with NULL.pub(super) async fn insert_thread(tx: &mut sqlx::PgConnection,thread: &ThreadRow,link_id: Uuid,) -> Result<Uuid, sqlx::Error> {- let result = sqlx::query!(+ let result = sqlx::query_scalar!(r#"INSERT INTO email_threads (id, provider_id, link_id, inbox_visible, is_read,latest_inbound_message_ts, latest_outbound_message_ts,@@ -184,6 +188,9 @@ pub(super) async fn insert_thread(SETlatest_inbound_message_ts = EXCLUDED.latest_inbound_message_ts,updated_at = NOW()+ WHERE+ EXCLUDED.latest_inbound_message_ts IS NOT NULL AND+ email_threads.latest_inbound_message_ts IS DISTINCT FROM EXCLUDED.latest_inbound_message_tsRETURNING id"#,thread.db_id,@@ -195,10 +202,24 @@ pub(super) async fn insert_thread(thread.latest_outbound_message_ts,thread.latest_non_spam_message_ts,)- .fetch_one(tx)+ .fetch_optional(&mut *tx).await?;- Ok(result.id)+ if let Some(id) = result {+ return Ok(id);+ }++ // The conflicting row already holds this data; fetch its id.+ sqlx::query_scalar!(+ r#"+ SELECT id FROM email_threads+ WHERE link_id = $1 AND provider_id = $2+ "#,+ link_id,+ thread.provider_id,+ )+ .fetch_one(tx)+ .await}// --- Thread metadata update ---@@ -563,7 +584,9 @@ async fn update_db_thread_metadata(updated_at = NOW()WHEREid = $6 AND- link_id = $7+ link_id = $7 AND+ (inbox_visible, is_read, latest_inbound_message_ts, latest_outbound_message_ts, latest_non_spam_message_ts)+ IS DISTINCT FROM ($1, $2, $3, $4, $5)"#,inbox_visible,is_read,diff --git a/crates/email_db_client/src/threads/insert.rs b/crates/email_db_client/src/threads/insert.rsindex 8c3b05aa6c..54c6d0187c 100644--- a/crates/email_db_client/src/threads/insert.rs+++ b/crates/email_db_client/src/threads/insert.rs@@ -8,7 +8,7 @@ use crate::{contacts, messages, parse};use models_email::email::db::address::UpsertedRecipients;use models_email::email::service::thread;use sqlx::types::Uuid;-use sqlx::{Executor, PgPool, Postgres};+use sqlx::{PgConnection, PgPool};use std::collections::HashMap;/// inserts a thread and all of its messages into the database@@ -45,7 +45,7 @@ pub async fn insert_thread_and_messages(let result = async {// Insert thread- let thread_id = insert_thread(&mut *tx, &service_thread, link_id).await?;+ let thread_id = insert_thread(&mut tx, &service_thread, link_id).await?;// Insert all messagesfor mut message in service_thread.messages.clone() {@@ -95,18 +95,20 @@ pub async fn insert_thread_and_messages(}/// inserts a thread object into the database using the provided transaction-#[tracing::instrument(skip(executor, service_thread), err)]-pub async fn insert_thread<'e, E>(- executor: E,+///+/// On conflict the update is skipped entirely when it would be a no-op+/// (incoming `latest_inbound_message_ts` is NULL or unchanged), so redelivered+/// backfill/sync work neither rewrites the row nor wipes a populated+/// timestamp with NULL.+#[tracing::instrument(skip(conn, service_thread), err)]+pub async fn insert_thread(+ conn: &mut PgConnection,service_thread: &thread::Thread,link_id: Uuid,-) -> anyhow::Result<Uuid>-where- E: Executor<'e, Database = Postgres>,-{+) -> anyhow::Result<Uuid> {let db_thread = parse::service_to_db::map_service_thread_to_db(service_thread, link_id);- let result = sqlx::query!(+ let result = sqlx::query_scalar!(r#"INSERT INTO email_threads (id, provider_id, link_id, inbox_visible, is_read, latest_inbound_message_ts,latest_outbound_message_ts, latest_non_spam_message_ts)@@ -115,6 +117,9 @@ whereSETlatest_inbound_message_ts = EXCLUDED.latest_inbound_message_ts,updated_at = NOW()+ WHERE+ EXCLUDED.latest_inbound_message_ts IS NOT NULL AND+ email_threads.latest_inbound_message_ts IS DISTINCT FROM EXCLUDED.latest_inbound_message_tsRETURNING id"#,db_thread.id,@@ -126,22 +131,35 @@ wheredb_thread.latest_outbound_message_ts,db_thread.latest_non_spam_message_ts,)- .fetch_one(executor)+ .fetch_optional(&mut *conn).await?;- Ok(result.id)+ if let Some(id) = result {+ return Ok(id);+ }++ // The conflicting row already holds this data; fetch its id.+ let existing_id = sqlx::query_scalar!(+ r#"+ SELECT id FROM email_threads+ WHERE link_id = $1 AND provider_id = $2+ "#,+ db_thread.link_id,+ db_thread.provider_id,+ )+ .fetch_one(&mut *conn)+ .await?;++ Ok(existing_id)}/// inserts a thread object into the database that has no metadata or rizz-#[tracing::instrument(skip(executor), err)]-pub async fn insert_blank_thread<'e, E>(- executor: E,+#[tracing::instrument(skip(pool), err)]+pub async fn insert_blank_thread(+ pool: &PgPool,thread_provider_id: &str,link_id: Uuid,-) -> anyhow::Result<Uuid>-where- E: Executor<'e, Database = Postgres>,-{+) -> anyhow::Result<Uuid> {let thread = thread::Thread {db_id: macro_uuid::generate_uuid_v7(),provider_id: Some(thread_provider_id.to_string()),@@ -156,5 +174,6 @@ wheremessages: vec![],};- insert_thread(executor, &thread, link_id).await+ let mut conn = pool.acquire().await?;+ insert_thread(&mut conn, &thread, link_id).await}diff --git a/crates/email_db_client/src/threads/insert/test.rs b/crates/email_db_client/src/threads/insert/test.rsindex f40b5f4198..8b19321b9f 100644--- a/crates/email_db_client/src/threads/insert/test.rs+++ b/crates/email_db_client/src/threads/insert/test.rs@@ -24,7 +24,8 @@ async fn provider_thread_upsert_advances_existing_thread_timestamp(conflicting_thread.db_id = Uuid::parse_str("20000000-0000-0000-0000-000000000001")?;conflicting_thread.latest_inbound_message_ts = Some(Utc::now());- let upserted_thread_id = insert_thread(&pool, &conflicting_thread, link_id).await?;+ let upserted_thread_id =+ insert_thread(&mut *pool.acquire().await?, &conflicting_thread, link_id).await?;assert_eq!(upserted_thread_id, existing_thread_id);let thread_after = get_thread_by_id_and_link_id(&pool, existing_thread_id, link_id)@@ -37,3 +38,112 @@ async fn provider_thread_upsert_advances_existing_thread_timestamp(Ok(())}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("links", "threads"))+)]+async fn unchanged_timestamp_upsert_is_noop(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let link_id = Uuid::parse_str("00000000-0000-0000-0000-000000000001")?;+ let existing_thread_id = Uuid::parse_str("10000000-0000-0000-0000-000000000001")?;++ // Seed a populated latest_inbound_message_ts via a real upsert.+ let mut thread = get_thread_by_id_and_link_id(&pool, existing_thread_id, link_id)+ .await?+ .expect("provider thread should exist");+ thread.db_id = Uuid::parse_str("20000000-0000-0000-0000-000000000002")?;+ thread.latest_inbound_message_ts = Some(Utc::now());+ insert_thread(&mut *pool.acquire().await?, &thread, link_id).await?;++ let after_first = get_thread_by_id_and_link_id(&pool, existing_thread_id, link_id)+ .await?+ .expect("provider thread should exist");+ assert!(after_first.latest_inbound_message_ts.is_some());++ tokio::time::sleep(Duration::from_millis(10)).await;++ // Re-upsert with the identical timestamp: must not rewrite the row.+ let mut duplicate = get_thread_by_id_and_link_id(&pool, existing_thread_id, link_id)+ .await?+ .expect("provider thread should exist");+ duplicate.db_id = Uuid::parse_str("20000000-0000-0000-0000-000000000003")?;++ let upserted_thread_id =+ insert_thread(&mut *pool.acquire().await?, &duplicate, link_id).await?;++ assert_eq!(upserted_thread_id, existing_thread_id);+ let after_second = get_thread_by_id_and_link_id(&pool, existing_thread_id, link_id)+ .await?+ .expect("provider thread should still exist");+ assert_eq!(+ after_second.updated_at, after_first.updated_at,+ "no-op upsert must not rewrite the row"+ );+ assert_eq!(+ after_second.latest_inbound_message_ts,+ after_first.latest_inbound_message_ts+ );++ Ok(())+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("links", "threads"))+)]+async fn blank_upsert_preserves_populated_timestamp(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let link_id = Uuid::parse_str("00000000-0000-0000-0000-000000000001")?;+ let existing_thread_id = Uuid::parse_str("10000000-0000-0000-0000-000000000001")?;++ // Seed a populated latest_inbound_message_ts via a real upsert.+ let mut thread = get_thread_by_id_and_link_id(&pool, existing_thread_id, link_id)+ .await?+ .expect("provider thread should exist");+ thread.db_id = Uuid::parse_str("20000000-0000-0000-0000-000000000004")?;+ thread.latest_inbound_message_ts = Some(Utc::now());+ insert_thread(&mut *pool.acquire().await?, &thread, link_id).await?;++ let before = get_thread_by_id_and_link_id(&pool, existing_thread_id, link_id)+ .await?+ .expect("provider thread should exist");+ assert!(before.latest_inbound_message_ts.is_some());++ tokio::time::sleep(Duration::from_millis(10)).await;++ // A redelivered backfill blank insert must not wipe the timestamp or+ // rewrite the row.+ let upserted_thread_id = insert_blank_thread(&pool, "prov1", link_id).await?;++ assert_eq!(upserted_thread_id, existing_thread_id);+ let after = get_thread_by_id_and_link_id(&pool, existing_thread_id, link_id)+ .await?+ .expect("provider thread should still exist");+ assert_eq!(+ after.latest_inbound_message_ts, before.latest_inbound_message_ts,+ "blank upsert must not wipe a populated timestamp"+ );+ assert_eq!(+ after.updated_at, before.updated_at,+ "blank upsert against an existing thread must not rewrite the row"+ );++ Ok(())+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("links", "threads"))+)]+async fn blank_insert_creates_new_thread(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let link_id = Uuid::parse_str("00000000-0000-0000-0000-000000000001")?;++ let thread_id = insert_blank_thread(&pool, "prov-brand-new", link_id).await?;++ let thread = get_thread_by_id_and_link_id(&pool, thread_id, link_id)+ .await?+ .expect("blank thread should be created");+ assert_eq!(thread.provider_id.as_deref(), Some("prov-brand-new"));+ assert_eq!(thread.latest_inbound_message_ts, None);++ Ok(())+}diff --git a/crates/email_db_client/src/threads/update.rs b/crates/email_db_client/src/threads/update.rsindex 24e8710184..f4386c54e3 100644--- a/crates/email_db_client/src/threads/update.rs+++ b/crates/email_db_client/src/threads/update.rs@@ -10,6 +10,10 @@ use models_email::service::message::is_macro_draft;use sqlx::types::Uuid;/// Updates a thread's metadata+///+/// Skips the write entirely when the recomputed values match the stored row,+/// so redundant recomputations (e.g. redelivered backfill work) don't produce+/// dead tuples and WAL for zero semantic change.#[expect(clippy::too_many_arguments, reason = "too annoying to fix right now")]#[tracing::instrument(skip(tx), err)]async fn update_db_thread_metadata(@@ -34,7 +38,9 @@ async fn update_db_thread_metadata(updated_at = NOW()WHEREid = $6 AND- link_id = $7+ link_id = $7 AND+ (inbox_visible, is_read, latest_inbound_message_ts, latest_outbound_message_ts, latest_non_spam_message_ts)+ IS DISTINCT FROM ($1, $2, $3, $4, $5)"#,inbox_visible,is_read,diff --git a/crates/email_db_client/src/threads/update/test.rs b/crates/email_db_client/src/threads/update/test.rsindex a041985b15..2bcf5b2018 100644--- a/crates/email_db_client/src/threads/update/test.rs+++ b/crates/email_db_client/src/threads/update/test.rs@@ -262,3 +262,31 @@ async fn update_thread_metadata_syncs_signal_flag(pool: Pool<Postgres>) -> anyho);Ok(())}++// A recomputation that produces the same values must not rewrite the row.+#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("sync_thread_signal_flag"))+)]+async fn repeated_metadata_recompute_is_noop(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let thread_id = Uuid::parse_str(SIG_THREAD_STALE_PROMO)?;+ let link_id = Uuid::parse_str("00000000-0000-0000-0000-000000000d01")?;++ let mut conn = pool.acquire().await?;+ update_thread_metadata(&mut conn, thread_id, link_id).await?;+ let thread_after_first = get_thread_by_id_and_link_id(&pool, thread_id, link_id)+ .await?+ .expect("thread should exist");++ tokio::time::sleep(Duration::from_millis(10)).await;++ update_thread_metadata(&mut conn, thread_id, link_id).await?;+ let thread_after_second = get_thread_by_id_and_link_id(&pool, thread_id, link_id)+ .await?+ .expect("thread should still exist");+ assert_eq!(+ thread_after_second.updated_at, thread_after_first.updated_at,+ "no-op metadata recompute must not rewrite the row"+ );+ Ok(())+}