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.

feat(whatsapp): trace how senders are named (#9629)

Wolf Mermelstein8d60255d5298
diff --git a/.sqlx/query-09334be323599739571198e524b1a4510080613b162d5f1aa1df25c74584ecdd.json b/.sqlx/query-09334be323599739571198e524b1a4510080613b162d5f1aa1df25c74584ecdd.json
new file mode 100644
index 0000000000..75622a7130
--- /dev/null
+++ b/.sqlx/query-09334be323599739571198e524b1a4510080613b162d5f1aa1df25c74584ecdd.json
@@ -0,0 +1,37 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO external_chat_person (id, chat_id, user_id, display_name) VALUES ($1, $2, $3, $4)\n ON CONFLICT (id) DO UPDATE SET display_name = COALESCE(excluded.display_name, external_chat_person.display_name),\n user_id = COALESCE(excluded.user_id, external_chat_person.user_id)\n RETURNING xmax = 0 AS \"created!\", display_name IS NOT NULL AS \"named!\", user_id IS NOT NULL AS \"macro_user!\"",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "created!",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 1,
+ "name": "named!",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 2,
+ "name": "macro_user!",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Text",
+ "Text"
+ ]
+ },
+ "nullable": [
+ null,
+ null,
+ null
+ ]
+ },
+ "hash": "09334be323599739571198e524b1a4510080613b162d5f1aa1df25c74584ecdd"
+}
diff --git a/.sqlx/query-a5e2776f18f33db5f852d6d872c4c3a08ccbed90a6b02b77ee4979a6548fa99a.json b/.sqlx/query-a5e2776f18f33db5f852d6d872c4c3a08ccbed90a6b02b77ee4979a6548fa99a.json
deleted file mode 100644
index 4b1f8cd0ad..0000000000
--- a/.sqlx/query-a5e2776f18f33db5f852d6d872c4c3a08ccbed90a6b02b77ee4979a6548fa99a.json
+++ /dev/null
@@ -1,17 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "INSERT INTO external_chat_person (id, chat_id, user_id, display_name) VALUES ($1, $2, $3, $4)\n ON CONFLICT (id) DO UPDATE SET display_name = COALESCE(excluded.display_name, external_chat_person.display_name),\n user_id = COALESCE(excluded.user_id, external_chat_person.user_id)",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Uuid",
- "Uuid",
- "Text",
- "Text"
- ]
- },
- "nullable": []
- },
- "hash": "a5e2776f18f33db5f852d6d872c4c3a08ccbed90a6b02b77ee4979a6548fa99a"
-}
diff --git a/crates/whatsapp_linked/AGENTS.md b/crates/whatsapp_linked/AGENTS.md
index 2bb9450a66..7c374f74c5 100644
--- a/crates/whatsapp_linked/AGENTS.md
+++ b/crates/whatsapp_linked/AGENTS.md
@@ -27,7 +27,9 @@ They share no HTTP: only MacroDB rows and `NOTIFY` (`external_chat`'s
quarantine, deferral and the standalone message write. Senders are resolved
first (`resolve_sender_in`): WhatsApp's from-me flag means the account's own
identity; a LID-only sender takes its phone number from the account's roster
- pair. Only what is still LID-only is quarantined.
+ pair. Only what is still LID-only is quarantined. Each stored sender
+ records a `whatsapp.ingest.person` span (chat kind, LID or not, paired,
+ `message_named`, `person_created`, `person_named`; no names or addresses).
- Structured content (`external_chat::domain::rich::RichContent`: poll, poll
vote, location, contact cards, event, system line) is stored in
`external_chat_message.rich`; `kind = 'system'` rows never notify or count
diff --git a/crates/whatsapp_linked/src/outbound/pg/ingest.rs b/crates/whatsapp_linked/src/outbound/pg/ingest.rs
index 8c51238e35..cd7c8a6b24 100644
--- a/crates/whatsapp_linked/src/outbound/pg/ingest.rs
+++ b/crates/whatsapp_linked/src/outbound/pg/ingest.rs
@@ -152,18 +152,35 @@ pub(super) async fn reserve_in(
.await
.map_err(failure)?;
let person = people.first().copied().unwrap_or_else(Uuid::now_v7);
- sqlx::query!(
+ let stored = sqlx::query!(
r#"INSERT INTO external_chat_person (id, chat_id, user_id, display_name) VALUES ($1, $2, $3, $4)
ON CONFLICT (id) DO UPDATE SET display_name = COALESCE(excluded.display_name, external_chat_person.display_name),
- user_id = COALESCE(excluded.user_id, external_chat_person.user_id)"#,
+ user_id = COALESCE(excluded.user_id, external_chat_person.user_id)
+ RETURNING xmax = 0 AS "created!", display_name IS NOT NULL AS "named!", user_id IS NOT NULL AS "macro_user!""#,
person,
binding.id,
associated_user,
sender.display_name
)
- .execute(&mut **tx)
+ .fetch_one(&mut **tx)
.await
.map_err(failure)?;
+ // The person's shape after this message, never who they are: whether
+ // the row is new and whether it has a name to show.
+ tracing::info_span!(
+ "whatsapp.ingest.person",
+ chat_id = %binding.id,
+ chat_kind = binding.kind.as_ref(),
+ sender_lid = sender.jid.ends_with("@lid"),
+ sender_alternate = sender.alternate_jid.is_some(),
+ message_named = sender.display_name.is_some(),
+ history = message.history,
+ from_me = message.from_me,
+ person_created = stored.created,
+ person_named = stored.named,
+ macro_user = stored.macro_user,
+ )
+ .in_scope(|| {});
for alias in &aliases {
sqlx::query!(
r#"INSERT INTO whatsapp_identity_alias (chat_id, jid, person_id, evidence_account_id, verified_at)
diff --git a/services/whatsapp_session_service/AGENTS.md b/services/whatsapp_session_service/AGENTS.md
index 762584ffb7..1d4dfd0966 100644
--- a/services/whatsapp_session_service/AGENTS.md
+++ b/services/whatsapp_session_service/AGENTS.md
@@ -67,7 +67,11 @@ current, ingests the traffic of added chats (`external_chat`) through `whatsapp_
`events::complete_addresses` fills the other half of a sender's and a
direct chat's phone-number/LID pair from the account's LID store (pairing,
group participant lists, message addressing); `from_me` carries WhatsApp's
- own-message flag. If the LID store can't be read, the event is left
+ own-message flag. `whatsapp.inbound.ingest` records the sender's shape
+ (`chat_kind`, `sender_address`, `sender_alternate`, `sender_named`,
+ `history`; never an address or a name) and `whatsapp.history.page` counts
+ senders `push_named` / `contact_named` (the contact store knows a name) /
+ `unnamed`. If the LID store can't be read, the event is left
unacknowledged (WhatsApp redelivers it) rather than gated without its
other address. History requests name a direct chat by LID
(`events::phone_chat`), as phones key it. Adopted groups stay cached until `whatsapp_account_changed`;
diff --git a/services/whatsapp_session_service/src/domain/inbound.rs b/services/whatsapp_session_service/src/domain/inbound.rs
index aed272f598..bc78cb980a 100644
--- a/services/whatsapp_session_service/src/domain/inbound.rs
+++ b/services/whatsapp_session_service/src/domain/inbound.rs
@@ -179,8 +179,22 @@ impl<Store: InboundStore> InboundSink for InboundGate<Store> {
InboundKind::Left => "left",
},
observed,
+ chat_kind = address_kind(&event.group_jid),
+ history = tracing::field::Empty,
+ sender_address = tracing::field::Empty,
+ sender_alternate = tracing::field::Empty,
+ sender_named = tracing::field::Empty,
outcome = tracing::field::Empty,
);
+ // The sender's shape, never who they are: which address WhatsApp
+ // named them by, whether the other is known, whether a name came.
+ if let InboundKind::Message(message) = &event.kind {
+ let sender = &message.sender;
+ span.record("history", message.history);
+ span.record("sender_address", address_kind(&sender.jid));
+ span.record("sender_alternate", sender.alternate_jid.is_some());
+ span.record("sender_named", sender.display_name.is_some());
+ }
if !observed {
span.record("outcome", "dropped");
tracing::debug!(
@@ -232,3 +246,14 @@ impl<Store: InboundStore> InboundSink for InboundGate<Store> {
}
}
}
+
+/// Which kind of WhatsApp address a JID is, for spans: never the address.
+/// A chat is `group`, or direct by `phone` or `lid`.
+fn address_kind(jid: &str) -> &'static str {
+ match jid.rsplit_once('@').map(|(_, server)| server) {
+ Some("lid") => "lid",
+ Some("s.whatsapp.net") => "phone",
+ Some("g.us") => "group",
+ _ => "other",
+ }
+}
diff --git a/services/whatsapp_session_service/src/outbound/whatsapp.rs b/services/whatsapp_session_service/src/outbound/whatsapp.rs
index ac5cc22e17..955c4ae462 100644
--- a/services/whatsapp_session_service/src/outbound/whatsapp.rs
+++ b/services/whatsapp_session_service/src/outbound/whatsapp.rs
@@ -13,8 +13,8 @@ use tokio_util::sync::CancellationToken;
use tracing::Instrument;
use uuid::Uuid;
use whatsapp_linked::domain::events::{
- ChatUpdate, ClaimedSend, InboundEvent, LookupAnswer, MediaImport, MessageAnchor, OutboundMedia,
- OutgoingContent, SessionScope, Target,
+ ChatUpdate, ClaimedSend, InboundEvent, InboundKind, LookupAnswer, MediaImport, MessageAnchor,
+ OutboundMedia, OutgoingContent, SessionScope, Target,
};
use whatsapp_linked::domain::jid::PhoneNumber;
use whatsapp_linked::domain::models::{GroupInfo, Identity, Participant};
@@ -316,6 +316,9 @@ impl Forwarder {
unreadable = tracing::field::Empty,
skipped = tracing::field::Empty,
skipped_stub_types = tracing::field::Empty,
+ push_named = tracing::field::Empty,
+ contact_named = tracing::field::Empty,
+ unnamed = tracing::field::Empty,
outcome = tracing::field::Empty,
);
async {
@@ -323,6 +326,7 @@ impl Forwarder {
return;
};
let mut stored = 0usize;
+ let mut names = SenderNames::default();
let mut unreadable: Vec<i32> = vec![];
// Messages Macro does not import, by WhatsApp stub type (0: not
// a stub), so a page that stores less than it got is explained.
@@ -356,8 +360,9 @@ impl Forwarder {
let votes = events::history_poll_votes(self.account, &parsed, now);
let mut message_kept = true;
for mut event in std::iter::once(event).chain(votes) {
- message_kept &= self.complete_addresses(&mut event).await
- && Box::pin(self.inbound.accept(&event)).await;
+ let completed = self.complete_addresses(&mut event).await;
+ names.count(self.store.as_ref(), &event).await;
+ message_kept &= completed && Box::pin(self.inbound.accept(&event)).await;
}
if message_kept {
stored += 1;
@@ -367,6 +372,9 @@ impl Forwarder {
}
let span = tracing::Span::current();
span.record("stored", stored);
+ span.record("push_named", names.push);
+ span.record("contact_named", names.contact);
+ span.record("unnamed", names.none);
span.record("unreadable", unreadable.len());
span.record("skipped", skipped.len());
span.record("skipped_stub_types", format!("{skipped:?}"));
@@ -391,6 +399,34 @@ impl Forwarder {
}
}
+/// How the senders of a history page could be named, for its span: by a
+/// WhatsApp name the message carries, by the account's contact store, or
+/// not at all. Counts only; never the names.
+#[derive(Default)]
+struct SenderNames {
+ push: usize,
+ contact: usize,
+ none: usize,
+}
+
+impl SenderNames {
+ async fn count(&mut self, contacts: &dyn ContactStore, event: &InboundEvent) {
+ let InboundKind::Message(message) = &event.kind else {
+ return;
+ };
+ if message.sender.display_name.is_some() {
+ self.push += 1;
+ } else if matches!(
+ events::contact_name(contacts, &message.sender).await,
+ Ok(Some(_))
+ ) {
+ self.contact += 1;
+ } else {
+ self.none += 1;
+ }
+ }
+}
+
impl Forwarder {
/// A poll vote, decrypted with the poll's message secret. `None` when the
/// message is not a vote; a vote that cannot be decrypted (the poll was
diff --git a/services/whatsapp_session_service/src/outbound/whatsapp/events.rs b/services/whatsapp_session_service/src/outbound/whatsapp/events.rs
index e2c586860c..276d8734b8 100644
--- a/services/whatsapp_session_service/src/outbound/whatsapp/events.rs
+++ b/services/whatsapp_session_service/src/outbound/whatsapp/events.rs
@@ -268,6 +268,47 @@ pub async fn complete_sender(
Ok(())
}
+/// What the account's contact store calls someone: the WhatsApp name it
+/// last saw for either address (live messages, push-name history syncs),
+/// else the owner's address-book name, else a verified business name.
+pub async fn contact_name(
+ contacts: &dyn ContactStore,
+ identity: &Identity,
+) -> Result<Option<String>, StoreError> {
+ let mut found = vec![];
+ for jid in [
+ Some(identity.jid.as_str()),
+ identity.alternate_jid.as_deref(),
+ ]
+ .into_iter()
+ .flatten()
+ {
+ if let Ok(jid) = jid.parse::<Jid>()
+ && let Some(contact) = contacts.get_contact(&jid).await?
+ {
+ found.push(contact);
+ }
+ }
+ Ok(best_contact_name(&found))
+}
+
+/// The best name among contact records: own WhatsApp name, then address
+/// book, then business name. `-` is WhatsApp's no-name.
+fn best_contact_name(contacts: &[ContactInfo]) -> Option<String> {
+ let pick = |name: fn(&ContactInfo) -> &str| {
+ contacts
+ .iter()
+ .map(name)
+ .map(str::trim)
+ .find(|name| !name.is_empty() && *name != "-")
+ .map(str::to_owned)
+ };
+ pick(|contact| &contact.push_name)
+ .or_else(|| pick(|contact| &contact.full_name))
+ .or_else(|| pick(|contact| &contact.first_name))
+ .or_else(|| pick(|contact| &contact.business_name))
+}
+
fn person(jid: &Jid) -> Option<String> {
(!jid.is_empty()).then(|| jid.to_non_ad().to_string())
}
diff --git a/services/whatsapp_session_service/src/outbound/whatsapp/events/test/contacts.rs b/services/whatsapp_session_service/src/outbound/whatsapp/events/test/contacts.rs
index 6654d6d49e..d2634d7583 100644
--- a/services/whatsapp_session_service/src/outbound/whatsapp/events/test/contacts.rs
+++ b/services/whatsapp_session_service/src/outbound/whatsapp/events/test/contacts.rs
@@ -14,7 +14,7 @@ use whatsmeow::types::{
ContactInfo, DEFAULT_USER_SERVER, IsOnWhatsAppResponse, VerifiedName, new_jid,
};
-use super::super::{chat_setting, contact_chats, lookup_answer};
+use super::super::{chat_setting, contact_chats, contact_name, lookup_answer};
fn listed(jid: &str, name: &str) -> ChatUpdate {
ChatUpdate {
@@ -162,3 +162,39 @@ async fn a_registered_number_is_its_phone_chat_named_like_any_chat() {
LookupAnswer::NotOnWhatsApp
);
}
+
+#[tokio::test]
+async fn a_contact_name_prefers_their_own_whatsapp_name_for_either_address() {
+ let store = MemoryStore::default();
+ let lid: whatsmeow::types::Jid = "987654321@lid".parse().unwrap();
+ let phone = new_jid("5550001", DEFAULT_USER_SERVER);
+ store
+ .put_contact_name(&phone, "Dana Smith", "Dana")
+ .await
+ .unwrap();
+ let sender = |alternate: Option<String>| whatsapp_linked::domain::models::Identity {
+ jid: lid.to_string(),
+ alternate_jid: alternate,
+ display_name: None,
+ };
+
+ assert_eq!(contact_name(&store, &sender(None)).await.unwrap(), None);
+ assert_eq!(
+ contact_name(&store, &sender(Some(phone.to_string())))
+ .await
+ .unwrap()
+ .as_deref(),
+ Some("Dana Smith"),
+ "the address book, through the paired phone number"
+ );
+ store.put_push_name(&lid, "-").await.unwrap();
+ store.put_push_name(&phone, "dana").await.unwrap();
+ assert_eq!(
+ contact_name(&store, &sender(Some(phone.to_string())))
+ .await
+ .unwrap()
+ .as_deref(),
+ Some("dana"),
+ "their own WhatsApp name wins; `-` is none"
+ );
+}