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
Populate missing delivery timestamps before publishing sent email upd… (#7596)
Sean Aye9e50b9599a0b
diff --git a/.sqlx/query-bc5d03a110b267d3f4e4ae2937b95d91a96794ebdbc864ce0d6d8d455996914f.json b/.sqlx/query-c498bd796ed728c8cc5331daaa41673682062bd8b88b803b88b000b22c6f1679.jsonsimilarity index 61%rename from .sqlx/query-bc5d03a110b267d3f4e4ae2937b95d91a96794ebdbc864ce0d6d8d455996914f.jsonrename to .sqlx/query-c498bd796ed728c8cc5331daaa41673682062bd8b88b803b88b000b22c6f1679.jsonindex 49490f6c71..c302fe731e 100644--- a/.sqlx/query-bc5d03a110b267d3f4e4ae2937b95d91a96794ebdbc864ce0d6d8d455996914f.json+++ b/.sqlx/query-c498bd796ed728c8cc5331daaa41673682062bd8b88b803b88b000b22c6f1679.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n UPDATE email_messages\n SET\n provider_id = $1,\n provider_thread_id = $2,\n is_draft = false,\n is_sent = true,\n updated_at = NOW()\n WHERE\n id = $3\n AND link_id = $4\n ",+ "query": "\n UPDATE email_messages\n SET\n provider_id = $1,\n provider_thread_id = $2,\n is_draft = false,\n is_sent = true,\n internal_date_ts = COALESCE(internal_date_ts, NOW()),\n updated_at = NOW()\n WHERE\n id = $3\n AND link_id = $4\n ","describe": {"columns": [],"parameters": {@@ -13,5 +13,5 @@},"nullable": []},- "hash": "bc5d03a110b267d3f4e4ae2937b95d91a96794ebdbc864ce0d6d8d455996914f"+ "hash": "c498bd796ed728c8cc5331daaa41673682062bd8b88b803b88b000b22c6f1679"}diff --git a/crates/email_db_client/fixtures/mark_message_as_sent.sql b/crates/email_db_client/fixtures/mark_message_as_sent.sqlnew file mode 100644index 0000000000..3cf34040c2--- /dev/null+++ b/crates/email_db_client/fixtures/mark_message_as_sent.sql@@ -0,0 +1,35 @@+-- A Macro message already queued for sending: no provider delivery time yet.+-- The old draft/envelope timestamps must not determine its Sent recency.+INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider, is_sync_active)+VALUES ('019a0000-0000-7000-8000-000000000001', 'macro|[email protected]',+ '019a0000-0000-7000-8000-000000000001', '[email protected]', 'GMAIL', true),+ ('019a0000-0000-7000-8000-000000000002', 'macro|[email protected]',+ '019a0000-0000-7000-8000-000000000002', '[email protected]', 'GMAIL', true);++INSERT INTO email_contacts (id, link_id, email_address)+VALUES ('019a0000-0000-7000-8000-000000000301',+ '019a0000-0000-7000-8000-000000000001', '[email protected]');++INSERT INTO email_threads (id, link_id, inbox_visible, is_read)+VALUES ('019a0000-0000-7000-8000-000000000101',+ '019a0000-0000-7000-8000-000000000001', false, true),+ ('019a0000-0000-7000-8000-000000000102',+ '019a0000-0000-7000-8000-000000000001', false, true);++INSERT INTO email_messages (+ id, thread_id, link_id, from_contact_id, subject, is_sent, is_draft,+ internal_date_ts, sent_at, created_at, updated_at+)+VALUES ('019a0000-0000-7000-8000-000000000201',+ '019a0000-0000-7000-8000-000000000101',+ '019a0000-0000-7000-8000-000000000001',+ '019a0000-0000-7000-8000-000000000301',+ 'Sent without provider synchronization', false, false, NULL,+ '2020-01-01T00:00:00Z', '2020-01-01T00:00:00Z', '2020-01-01T00:00:00Z'),+ ('019a0000-0000-7000-8000-000000000202',+ '019a0000-0000-7000-8000-000000000102',+ '019a0000-0000-7000-8000-000000000001',+ '019a0000-0000-7000-8000-000000000301',+ 'Already has a provider timestamp', false, false,+ '2025-01-01T12:00:00Z', '2025-01-01T12:00:00Z',+ '2024-12-01T00:00:00Z', '2024-12-01T00:00:00Z');diff --git a/crates/email_db_client/src/messages/update.rs b/crates/email_db_client/src/messages/update.rsindex b88f15267d..a2ff5d41b3 100644--- a/crates/email_db_client/src/messages/update.rs+++ b/crates/email_db_client/src/messages/update.rs@@ -1,5 +1,8 @@use sqlx::types::Uuid;+#[cfg(test)]+mod test;+/// Update the read status of a single message, scoped to the inbox that owns it.#[tracing::instrument(skip(tx), err)]pub async fn update_message_read_status(@@ -148,7 +151,12 @@ whereOk(updated_count)}-/// Updates draft in database to be sent, and populates with provider IDs+/// Finalize a provider-accepted send and populate its provider IDs.+///+/// Macro messages may not have a provider timestamp until inbox sync runs.+/// Seed their delivery time now so thread metadata and the realtime Mail cache+/// can include them in Sent immediately. Preserve existing provider timestamps+/// and the timestamp from an earlier finalization attempt.#[tracing::instrument(skip(tx), err)]pub async fn mark_message_as_sent(tx: &mut sqlx::PgConnection,@@ -165,6 +173,7 @@ pub async fn mark_message_as_sent(provider_thread_id = $2,is_draft = false,is_sent = true,+ internal_date_ts = COALESCE(internal_date_ts, NOW()),updated_at = NOW()WHEREid = $3diff --git a/crates/email_db_client/src/messages/update/test.rs b/crates/email_db_client/src/messages/update/test.rsnew file mode 100644index 0000000000..33e4951f29--- /dev/null+++ b/crates/email_db_client/src/messages/update/test.rs@@ -0,0 +1,136 @@+use super::mark_message_as_sent;+use crate::messages::get::fetch_messages_metadata;+use crate::threads::get::get_thread_by_id_and_link_id;+use crate::threads::update::update_thread_metadata;+use macro_db_migrator::MACRO_DB_MIGRATIONS;+use models_email::service::message::Message;+use sqlx::{PgConnection, PgPool, types::Uuid};++const LINK: Uuid = Uuid::from_u128(0x019a0000_0000_7000_8000_000000000001);+const OTHER_LINK: Uuid = Uuid::from_u128(0x019a0000_0000_7000_8000_000000000002);+const THREAD: Uuid = Uuid::from_u128(0x019a0000_0000_7000_8000_000000000101);+const PROVIDER_THREAD: Uuid = Uuid::from_u128(0x019a0000_0000_7000_8000_000000000102);+const MESSAGE: Uuid = Uuid::from_u128(0x019a0000_0000_7000_8000_000000000201);+const PROVIDER_MESSAGE: Uuid = Uuid::from_u128(0x019a0000_0000_7000_8000_000000000202);++async fn message(conn: &mut PgConnection, thread: Uuid) -> anyhow::Result<Message> {+ let messages = fetch_messages_metadata(conn, thread).await?;+ assert_eq!(messages.len(), 1);+ Ok(messages.into_iter().next().unwrap())+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("mark_message_as_sent"))+)]+async fn sent_message_has_recency_before_provider_sync(pool: PgPool) -> anyhow::Result<()> {+ let mut tx = pool.begin().await?;+ let pending = message(tx.as_mut(), THREAD).await?;+ assert!(!pending.is_sent);+ assert!(pending.internal_date_ts.is_none());++ // These are the same two steps used by scheduled-delivery finalization,+ // before its transaction commits and the message_sent event is published.+ mark_message_as_sent(tx.as_mut(), "sent-message", "sent-thread", LINK, MESSAGE).await?;+ update_thread_metadata(tx.as_mut(), THREAD, LINK).await?;+ let sent = message(tx.as_mut(), THREAD).await?;+ assert!(sent.is_sent);+ assert!(!sent.is_draft);+ assert_eq!(sent.provider_id.as_deref(), Some("sent-message"));+ assert_eq!(sent.provider_thread_id.as_deref(), Some("sent-thread"));+ assert_eq!(sent.internal_date_ts, Some(sent.updated_at));+ assert!(sent.internal_date_ts > pending.sent_at);+ tx.commit().await?;++ // The Mail cache capsule and server Sent membership both use this field.+ // It must already exist without a subsequent Gmail inbox-sync pass.+ let thread = get_thread_by_id_and_link_id(&pool, THREAD, LINK)+ .await?+ .unwrap();+ assert_eq!(thread.latest_outbound_message_ts, sent.internal_date_ts);+ assert_eq!(thread.latest_non_spam_message_ts, sent.internal_date_ts);+ assert!(thread.latest_inbound_message_ts.is_none());+ assert!(!thread.inbox_visible);++ let mut retry = pool.begin().await?;+ mark_message_as_sent(retry.as_mut(), "sent-message", "sent-thread", LINK, MESSAGE).await?;+ assert_eq!(+ message(retry.as_mut(), THREAD).await?.internal_date_ts,+ sent.internal_date_ts,+ "replaying finalization must not move the message in Sent"+ );+ retry.commit().await?;+ Ok(())+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("mark_message_as_sent"))+)]+async fn sent_message_preserves_provider_timestamp(pool: PgPool) -> anyhow::Result<()> {+ let mut tx = pool.begin().await?;+ let before = message(tx.as_mut(), PROVIDER_THREAD).await?;+ assert!(before.internal_date_ts.is_some());+ mark_message_as_sent(+ tx.as_mut(),+ "provider-message",+ "provider-thread",+ LINK,+ PROVIDER_MESSAGE,+ )+ .await?;+ update_thread_metadata(tx.as_mut(), PROVIDER_THREAD, LINK).await?;+ let sent = message(tx.as_mut(), PROVIDER_THREAD).await?;+ assert!(sent.is_sent);+ assert_eq!(sent.internal_date_ts, before.internal_date_ts);+ tx.commit().await?;++ let thread = get_thread_by_id_and_link_id(&pool, PROVIDER_THREAD, LINK)+ .await?+ .unwrap();+ assert_eq!(thread.latest_outbound_message_ts, before.internal_date_ts);+ Ok(())+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("mark_message_as_sent"))+)]+async fn sent_message_update_is_scoped_to_its_inbox(pool: PgPool) -> anyhow::Result<()> {+ let mut tx = pool.begin().await?;+ mark_message_as_sent(+ tx.as_mut(),+ "wrong-message",+ "wrong-thread",+ OTHER_LINK,+ MESSAGE,+ )+ .await?;+ let unchanged = message(tx.as_mut(), THREAD).await?;+ assert!(!unchanged.is_sent);+ assert!(unchanged.internal_date_ts.is_none());+ assert!(unchanged.provider_id.is_none());+ tx.commit().await?;+ Ok(())+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("mark_message_as_sent"))+)]+async fn failed_finalization_rolls_back_sent_recency(pool: PgPool) -> anyhow::Result<()> {+ let mut tx = pool.begin().await?;+ mark_message_as_sent(tx.as_mut(), "sent-message", "sent-thread", LINK, MESSAGE).await?;+ update_thread_metadata(tx.as_mut(), THREAD, LINK).await?;+ tx.rollback().await?;++ let mut conn = pool.acquire().await?;+ let unchanged = message(&mut conn, THREAD).await?;+ assert!(!unchanged.is_sent);+ assert!(unchanged.internal_date_ts.is_none());+ let thread = get_thread_by_id_and_link_id(&pool, THREAD, LINK)+ .await?+ .unwrap();+ assert!(thread.latest_outbound_message_ts.is_none());+ Ok(())+}diff --git a/docs/AGENT_GUIDE/surfaces.md b/docs/AGENT_GUIDE/surfaces.mdindex 2201d3c987..c77f160b53 100644--- a/docs/AGENT_GUIDE/surfaces.md+++ b/docs/AGENT_GUIDE/surfaces.md@@ -684,7 +684,13 @@ a slow background refresh must not keep the restored editor disabled. A rejectedsend reports failure and restores its original reply editor if it is still mounted.A failure from an older, unmounted editor must not overwrite a newer edited reply.A presentation or refresh error after successful delivery is not a reason to send-again.+again. After the undo window and provider acceptance, send finalization supplies+any missing delivery timestamp before publishing the realtime update. The cached+Sent list must therefore admit the message without waiting for Gmail inbox sync+or an online visit to Sent. Verify by sending from another Mail tab, receiving the+final sent update, then switching offline and opening Sent. Existing provider+timestamps and timestamps from repeated finalization remain unchanged; an unsent+or cancelled draft must not acquire Sent membership.Send and schedule are refused with a notice while the device is offline, while adraft is still syncing (its save was accepted locally but not yet confirmed by the