Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(whatsapp): stop a group added as its first message arrives waiting (#9765)

teo7dc9ce9a0e9c
diff --git a/.sqlx/query-be636082977712afac114f862262656b8fb5911bce2a3a21a1f03fa40b02ef19.json b/.sqlx/query-990ea172a3128ea9d1903fca9f4ca7b492e3258523a9dc3a9477a49c8c3cc463.json
similarity index 81%
rename from .sqlx/query-be636082977712afac114f862262656b8fb5911bce2a3a21a1f03fa40b02ef19.json
rename to .sqlx/query-990ea172a3128ea9d1903fca9f4ca7b492e3258523a9dc3a9477a49c8c3cc463.json
index 3caa8921b3..f9eaeffaa3 100644
--- a/.sqlx/query-be636082977712afac114f862262656b8fb5911bce2a3a21a1f03fa40b02ef19.json
+++ b/.sqlx/query-990ea172a3128ea9d1903fca9f4ca7b492e3258523a9dc3a9477a49c8c3cc463.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "SELECT id FROM whatsapp_linked_account WHERE id = $1 AND user_id = $2 AND generation = $3 AND state = 'connected' FOR SHARE",
+ "query": "SELECT id FROM whatsapp_linked_account WHERE id = $1 AND user_id = $2 AND generation = $3 AND state = 'connected' FOR NO KEY UPDATE",
"describe": {
"columns": [
{
@@ -20,5 +20,5 @@
false
]
},
- "hash": "be636082977712afac114f862262656b8fb5911bce2a3a21a1f03fa40b02ef19"
+ "hash": "990ea172a3128ea9d1903fca9f4ca7b492e3258523a9dc3a9477a49c8c3cc463"
}
diff --git a/crates/whatsapp_linked/AGENTS.md b/crates/whatsapp_linked/AGENTS.md
index 7c374f74c5..30553fdc73 100644
--- a/crates/whatsapp_linked/AGENTS.md
+++ b/crates/whatsapp_linked/AGENTS.md
@@ -99,6 +99,9 @@ They share no HTTP: only MacroDB rows and `NOTIFY` (`external_chat`'s
- Lock accounts before groups, then child rows; lock every group in a batch in UUID order. Pairing expiry locks affected accounts before pairing rows. Media completion takes the group lock before attachment/message writes.
- Every session-service write takes the account row lock at the caller's
`lease_epoch`; `ExternalChatError::Fenced` means stop the account now.
+- Adding a chat takes the account row exclusively, against those share-mode
+ fences: a chat-list update and an add running side by side could otherwise
+ each miss the other's write and leave the chat in `waiting_for_message`.
- `dispatch_started_at` is written before submission. A dispatched send without
an outcome becomes `unknown` on takeover and is never resent; a later echo
with its `allocated_source_id` settles it.
diff --git a/crates/whatsapp_linked/src/outbound/pg/adoptions.rs b/crates/whatsapp_linked/src/outbound/pg/adoptions.rs
index 361b1cc9fa..4b5205b889 100644
--- a/crates/whatsapp_linked/src/outbound/pg/adoptions.rs
+++ b/crates/whatsapp_linked/src/outbound/pg/adoptions.rs
@@ -26,7 +26,12 @@ impl AdoptionRepository for PgWhatsAppRepository {
.execute(&mut *tx)
.await
.map_err(failure)?;
- sqlx::query_scalar!("SELECT id FROM whatsapp_linked_account WHERE id = $1 AND user_id = $2 AND generation = $3 AND state = 'connected' FOR SHARE", account.id,user,account.authorization_generation)
+ // Exclusive against the replica's fence: a chat-list update commits
+ // either before the backfill state below is decided, so it sees the
+ // anchor, or after this chat exists, so the update marks it ready.
+ // In share mode both could miss each other and leave the chat waiting
+ // for a message that had already arrived.
+ sqlx::query_scalar!("SELECT id FROM whatsapp_linked_account WHERE id = $1 AND user_id = $2 AND generation = $3 AND state = 'connected' FOR NO KEY UPDATE", account.id,user,account.authorization_generation)
.fetch_optional(&mut *tx).await.map_err(failure)?.ok_or(ExternalChatError::Disconnected)?;
let existing = sqlx::query_as!(ChatRow,"SELECT id, provider, remote_id, kind, user_id, state, adopted_at FROM external_chat WHERE user_id = $1 AND provider = 'whatsapp' AND remote_id = $2 AND state <> 'detached' FOR UPDATE",user,adoption.jid)
.fetch_optional(&mut *tx).await.map_err(failure)?;
diff --git a/crates/whatsapp_linked/src/outbound/pg/test/concurrency.rs b/crates/whatsapp_linked/src/outbound/pg/test/concurrency.rs
index 0b9b87ede2..ba55a45bf4 100644
--- a/crates/whatsapp_linked/src/outbound/pg/test/concurrency.rs
+++ b/crates/whatsapp_linked/src/outbound/pg/test/concurrency.rs
@@ -1,6 +1,7 @@
use super::*;
use crate::domain::{ports::SendRepository, provider::WhatsAppProvider};
use external_chat::domain::ports::{ChatProvider, SendIntent};
+use std::sync::atomic::{AtomicBool, Ordering};
use tokio::sync::Barrier;
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
@@ -91,3 +92,142 @@ async fn observers_can_refresh_groups_in_opposite_orders(pool: PgPool) {
};
tokio::join!(alice_refresh, bob_refresh);
}
+
+/// A chat-list update carrying `group`'s first message.
+fn first_message(group: &str) -> crate::domain::events::ChatUpdate {
+ crate::domain::events::ChatUpdate {
+ jid: group.to_owned(),
+ kind: ExternalChatKind::Group,
+ name: None,
+ last_activity_at: Some(Utc::now()),
+ archived: None,
+ pinned: None,
+ anchor: Some(MessageAnchor {
+ id: "3EB0FIRST".to_owned(),
+ at: Utc::now(),
+ from_me: false,
+ sender_jid: None,
+ }),
+ create: true,
+ }
+}
+
+async fn backfill_state_of(pool: &PgPool, chat: Uuid) -> Option<String> {
+ sqlx::query_scalar!(
+ "SELECT backfill_state FROM external_chat WHERE id = $1",
+ chat
+ )
+ .fetch_one(pool)
+ .await
+ .unwrap()
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn a_group_added_while_its_first_message_is_recorded_can_import(pool: PgPool) {
+ workspace(&pool).await;
+ let (linked, session, _) = services(&pool);
+ let alice = pair(&linked, &session, ALICE, ALICE_JID).await;
+ session
+ .update_groups(alice, &[group(&[ALICE_JID, CUSTOMER_JID])], true)
+ .await
+ .unwrap();
+
+ // The replica has written the first message and has not committed yet.
+ let mut recording = pool.begin().await.unwrap();
+ let fenced = fence_in(&mut recording, alice).await.unwrap();
+ crate::outbound::pg::chats::update_chats_in(&mut recording, &fenced, &[first_message(GROUP)])
+ .await
+ .unwrap();
+
+ let added = AtomicBool::new(false);
+ let add = async {
+ let binding = linked
+ .chats
+ .add(
+ &person(ALICE),
+ &AddChat {
+ provider: ExternalChatProvider::WhatsApp,
+ remote_id: GROUP.into(),
+ kind: ExternalChatKind::Group,
+ },
+ SendAuthority::Human,
+ )
+ .await
+ .unwrap();
+ added.store(true, Ordering::SeqCst);
+ binding
+ };
+ // Commit once the add has finished without seeing the message, or is
+ // waiting on the replica's transaction.
+ let commit = async {
+ loop {
+ let waiting = sqlx::query_scalar!(
+ r#"SELECT count(*) AS "count!" FROM pg_stat_activity
+ WHERE datname = current_database() AND wait_event_type = 'Lock'"#
+ )
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ if waiting > 0 || added.load(Ordering::SeqCst) {
+ break;
+ }
+ tokio::time::sleep(std::time::Duration::from_millis(5)).await;
+ }
+ recording.commit().await.unwrap();
+ };
+ let (binding, ()) = tokio::join!(add, commit);
+ assert_eq!(
+ backfill_state_of(&pool, binding.id).await.as_deref(),
+ Some("waiting_for_phone"),
+ "the first message makes the chat importable"
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn groups_added_as_their_first_messages_arrive_can_all_import(pool: PgPool) {
+ workspace(&pool).await;
+ let (linked, session, _) = services(&pool);
+ let alice = pair(&linked, &session, ALICE, ALICE_JID).await;
+ let groups: Vec<GroupInfo> = (0..64)
+ .map(|index| {
+ let mut info = group(&[ALICE_JID, CUSTOMER_JID]);
+ info.jid = format!("1203630000{index:08}@g.us");
+ info
+ })
+ .collect();
+ session.update_groups(alice, &groups, true).await.unwrap();
+
+ let barrier = Barrier::new(2);
+ for info in &groups {
+ let add = async {
+ barrier.wait().await;
+ linked
+ .chats
+ .add(
+ &person(ALICE),
+ &AddChat {
+ provider: ExternalChatProvider::WhatsApp,
+ remote_id: info.jid.clone(),
+ kind: ExternalChatKind::Group,
+ },
+ SendAuthority::Human,
+ )
+ .await
+ .unwrap()
+ };
+ let record = async {
+ barrier.wait().await;
+ session
+ .update_chats(alice, &[first_message(&info.jid)])
+ .await
+ .unwrap();
+ };
+ let (binding, ()) = tokio::join!(add, record);
+ assert_eq!(
+ backfill_state_of(&pool, binding.id).await.as_deref(),
+ Some("waiting_for_phone"),
+ "{} missed its first message",
+ info.jid
+ );
+ }
+}