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
perf(channels): read notified thread pages from the user's notifications (#7604)
teo17328d16c56b
diff --git a/crates/channels/src/outbound/pg_channels_repo.rs b/crates/channels/src/outbound/pg_channels_repo.rsindex d18d7e58b3..bc2e315b23 100644--- a/crates/channels/src/outbound/pg_channels_repo.rs+++ b/crates/channels/src/outbound/pg_channels_repo.rs@@ -732,16 +732,21 @@ fn push_channel_thread_filter_expr(push_channel_thread_notification_filter_expr(builder,user_id,- match state {- item_filters::NotificationState::Unseen => "un.state = 'unseen'",- item_filters::NotificationState::Seen => "un.state = 'seen'",- item_filters::NotificationState::Done => "un.state = 'done'",- },+ notification_state_predicate(*state),);}}}+#[cfg(feature = "list")]+fn notification_state_predicate(state: item_filters::NotificationState) -> &'static str {+ match state {+ item_filters::NotificationState::Unseen => "un.state = 'unseen'",+ item_filters::NotificationState::Seen => "un.state = 'seen'",+ item_filters::NotificationState::Done => "un.state = 'done'",+ }+}+/// A user is a participant of a thread when they are still an active member of the/// channel AND they sent the root message or any reply, or were @-mentioned anywhere/// in the thread.@@ -827,6 +832,252 @@ fn push_channel_thread_notification_filter_expr(builder.push(")");}+#[cfg(feature = "list")]+const CHANNEL_MESSAGE_ID_PATTERN: &str =+ r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$";++/// Positive notification states, plus predicates that were ANDed onto them.+///+/// `Unsafe` is a NOT, or an OR that mixes a notification with anything else.+/// Those trees are not "the user's notified roots" and stay on the channel scan.+#[cfg(feature = "list")]+#[derive(Debug)]+enum ThreadNotifPlan<'a> {+ Absent,+ Ready {+ ids_sql: String,+ residuals: Vec<&'a Expr<ChannelThreadLiteral>>,+ },+ Unsafe,+}++#[cfg(feature = "list")]+fn channel_thread_expr_mentions_notification(expr: &Expr<ChannelThreadLiteral>) -> bool {+ match expr {+ Expr::Literal(ChannelThreadLiteral::NotificationState(_)) => true,+ Expr::Literal(_) => false,+ Expr::Not(inner) => channel_thread_expr_mentions_notification(inner),+ Expr::And(left, right) | Expr::Or(left, right) => {+ channel_thread_expr_mentions_notification(left)+ || channel_thread_expr_mentions_notification(right)+ }+ }+}++/// Root ids of channel-message notifications in `state_predicate`.+///+/// `$1` is the requesting user, the first bind `build_channel_thread_notification_query`+/// pushes. Each arm fences the user's rows and the notification lookup with+/// `OFFSET 0`. Without the inner fence Postgres hashes every `channel_message`+/// notification in the table when that type's statistics are too low. AND is+/// `INTERSECT` because two states can be witnessed by different notification+/// rows. OR is `UNION`.+#[cfg(feature = "list")]+fn channel_message_notification_ids_sql(state_predicate: &str) -> String {+ format!(+ r#"SELECT DISTINCT n.secondary_event_item_id::uuid AS root_id,+ n.event_item_id AS channel_id+ FROM (+ SELECT un.notification_id+ FROM user_notification un+ WHERE un.user_id = $1+ AND un.deleted_at IS NULL+ AND {state_predicate}+ OFFSET 0+ ) un+ JOIN LATERAL (+ SELECT n.secondary_event_item_id, n.event_item_id+ FROM notification n+ WHERE n.id = un.notification_id+ AND n.event_item_type = 'channel'+ AND n.secondary_event_item_type = 'channel_message'+ AND n.secondary_event_item_id ~ '{CHANNEL_MESSAGE_ID_PATTERN}'+ OFFSET 0+ ) n ON TRUE"#+ )+}++#[cfg(feature = "list")]+fn classify_channel_thread_notification(expr: &Expr<ChannelThreadLiteral>) -> ThreadNotifPlan<'_> {+ match expr {+ Expr::Literal(ChannelThreadLiteral::NotificationState(state)) => ThreadNotifPlan::Ready {+ ids_sql: channel_message_notification_ids_sql(notification_state_predicate(*state)),+ residuals: Vec::new(),+ },+ Expr::Literal(_) => ThreadNotifPlan::Absent,+ Expr::Not(inner) => {+ if channel_thread_expr_mentions_notification(inner) {+ ThreadNotifPlan::Unsafe+ } else {+ ThreadNotifPlan::Absent+ }+ }+ Expr::Or(left, right) => {+ match (+ classify_channel_thread_notification(left),+ classify_channel_thread_notification(right),+ ) {+ (+ ThreadNotifPlan::Ready {+ ids_sql: left_ids,+ residuals: left_residuals,+ },+ ThreadNotifPlan::Ready {+ ids_sql: right_ids,+ residuals: right_residuals,+ },+ ) if left_residuals.is_empty() && right_residuals.is_empty() => {+ ThreadNotifPlan::Ready {+ ids_sql: format!("({left_ids}) UNION ({right_ids})"),+ residuals: Vec::new(),+ }+ }+ (ThreadNotifPlan::Absent, ThreadNotifPlan::Absent) => ThreadNotifPlan::Absent,+ _ => ThreadNotifPlan::Unsafe,+ }+ }+ Expr::And(left, right) => {+ match (+ classify_channel_thread_notification(left),+ classify_channel_thread_notification(right),+ ) {+ (ThreadNotifPlan::Unsafe, _) | (_, ThreadNotifPlan::Unsafe) => {+ ThreadNotifPlan::Unsafe+ }+ (ThreadNotifPlan::Absent, ThreadNotifPlan::Absent) => ThreadNotifPlan::Absent,+ (+ ThreadNotifPlan::Ready {+ ids_sql,+ mut residuals,+ },+ ThreadNotifPlan::Absent,+ ) => {+ residuals.push(right);+ ThreadNotifPlan::Ready { ids_sql, residuals }+ }+ (+ ThreadNotifPlan::Absent,+ ThreadNotifPlan::Ready {+ ids_sql,+ mut residuals,+ },+ ) => {+ residuals.push(left);+ ThreadNotifPlan::Ready { ids_sql, residuals }+ }+ (+ ThreadNotifPlan::Ready {+ ids_sql: left_ids,+ mut residuals,+ },+ ThreadNotifPlan::Ready {+ ids_sql: right_ids,+ residuals: right_residuals,+ },+ ) => {+ residuals.extend(right_residuals);+ ThreadNotifPlan::Ready {+ ids_sql: format!("({left_ids}) INTERSECT ({right_ids})"),+ residuals,+ }+ }+ }+ }+ }+}++#[cfg(feature = "list")]+fn push_channel_thread_keyset(+ builder: &mut QueryBuilder<'static, Postgres>,+ sort_method_str: String,+ cursor_timestamp: Option<DateTime<Utc>>,+ cursor_id_str: Option<String>,+ query_limit: Option<i64>,+) {+ builder.push(" AND (");+ builder.push_bind(cursor_timestamp);+ builder.push("::timestamptz IS NULL OR (");+ push_channel_thread_sort_expr(builder, sort_method_str.clone());+ builder.push(", m.id::text) < (");+ builder.push_bind(cursor_timestamp);+ builder.push(", ");+ builder.push_bind(cursor_id_str);+ builder.push(")) ORDER BY ");+ push_channel_thread_sort_expr(builder, sort_method_str);+ builder.push(" DESC, m.id::text DESC LIMIT ");+ builder.push_bind(query_limit);+}++/// Threads the user was notified about, then the same activity sort as the scan.+/// Active channel membership is always applied: a notification does not keep a+/// thread in a channel the user has left.+#[cfg(feature = "list")]+fn build_channel_thread_notification_query(+ params: &GetThreadReplyRowsParams,+ ids_sql: String,+ residuals: &[&Expr<ChannelThreadLiteral>],+ sort_method_str: String,+ query_limit: Option<i64>,+ cursor_timestamp: Option<DateTime<Utc>>,+ cursor_id_str: Option<String>,+) -> QueryBuilder<'static, Postgres> {+ let mut builder = QueryBuilder::new(format!(+ r#"+ WITH notified AS MATERIALIZED (+ {ids_sql}+ )+ SELECT+ m.id AS id,+ m.parent_entity_id::uuid AS channel_id,+ m.sender_id AS sender_id,+ m.triggered_by_user_id AS triggered_by_user_id,+ m.content AS content,+ m.created_at AS created_at,+ m.updated_at AS updated_at,+ m.edited_at::timestamptz AS edited_at,+ m.deleted_at::timestamptz AS deleted_at+ FROM comms_messages m+ JOIN notified+ ON notified.root_id = m.id+ AND notified.channel_id = m.parent_entity_id+ JOIN comms_channels c ON c.id::text = m.parent_entity_id+ LEFT JOIN LATERAL (+ SELECT MAX(reply.updated_at) AS latest_reply_updated_at+ FROM comms_messages reply+ WHERE reply.thread_id = m.id+ AND reply.deleted_at IS NULL+ ) thread_stats ON TRUE+ WHERE m.thread_id IS NULL+ AND m.deleted_at IS NULL+ AND m.parent_entity_type = 'channel'+ AND EXISTS (+ SELECT 1+ FROM comms_channel_participants cp+ WHERE cp.channel_id::text = m.parent_entity_id+ AND cp.user_id = "#+ ));+ // First bind. The `$1` literals inside `ids_sql` are this same user.+ builder.push_bind(params.user().as_ref().to_string());+ builder.push(+ r#"+ AND cp.left_at IS NULL+ )+ "#,+ );+ for residual in residuals {+ builder.push(" AND ");+ push_channel_thread_filter_expr(&mut builder, residual, params.user());+ }+ push_channel_thread_keyset(+ &mut builder,+ sort_method_str,+ cursor_timestamp,+ cursor_id_str,+ query_limit,+ );+ builder+}+#[cfg(feature = "list")]fn build_channel_thread_rows_query(params: &GetThreadReplyRowsParams,@@ -840,6 +1091,21 @@ fn build_channel_thread_rows_query(let cursor_id_str = cursor_id.map(|id| id.to_string());let cursor_timestamp = cursor_timestamp.cloned();+ if let Some(expr) = cursor.filter().as_deref()+ && let ThreadNotifPlan::Ready { ids_sql, residuals } =+ classify_channel_thread_notification(expr)+ {+ return build_channel_thread_notification_query(+ params,+ ids_sql,+ &residuals,+ sort_method_str,+ query_limit,+ cursor_timestamp,+ cursor_id_str,+ );+ }+let mut builder = QueryBuilder::new(r#"WITH user_channels AS (@@ -880,18 +1146,13 @@ fn build_channel_thread_rows_query(push_channel_thread_filter_expr(&mut builder, expr, params.user());}- builder.push(" AND (");- builder.push_bind(cursor_timestamp);- builder.push("::timestamptz IS NULL OR (");- push_channel_thread_sort_expr(&mut builder, sort_method_str.clone());- builder.push(", m.id::text) < (");- builder.push_bind(cursor_timestamp);- builder.push(", ");- builder.push_bind(cursor_id_str);- builder.push(")) ORDER BY ");- push_channel_thread_sort_expr(&mut builder, sort_method_str);- builder.push(" DESC, m.id::text DESC LIMIT ");- builder.push_bind(query_limit);+ push_channel_thread_keyset(+ &mut builder,+ sort_method_str,+ cursor_timestamp,+ cursor_id_str,+ query_limit,+ );builder}diff --git a/crates/channels/src/outbound/pg_channels_repo/tests.rs b/crates/channels/src/outbound/pg_channels_repo/tests.rsindex 71741fa90d..f406a7d4be 100644--- a/crates/channels/src/outbound/pg_channels_repo/tests.rs+++ b/crates/channels/src/outbound/pg_channels_repo/tests.rs@@ -1548,6 +1548,128 @@ async fn channel_thread_rows_filter_by_notification_seen_secondary_entity(Ok(())}+#[test]+fn channel_thread_rows_notification_plan_splits_and_keeps_or_on_the_scan() {+ let state = |state| Expr::val(ChannelThreadLiteral::NotificationState(state));+ let participant = Expr::val(ChannelThreadLiteral::Participant(macro_user_id(USER_A)));+ let and_filter = Expr::and(+ Expr::or(+ state(item_filters::NotificationState::Unseen),+ state(item_filters::NotificationState::Seen),+ ),+ participant.clone(),+ );+ let plan = super::classify_channel_thread_notification(&and_filter);+ match plan {+ super::ThreadNotifPlan::Ready {+ residuals, ids_sql, ..+ } => {+ assert_eq!(residuals.len(), 1);+ assert!(ids_sql.contains("UNION"));+ assert!(!ids_sql.contains("INTERSECT"));+ }+ other => panic!("expected a notification-first plan, got {other:?}"),+ }++ let mixed_filter = Expr::or(state(item_filters::NotificationState::Unseen), participant);+ let mixed = super::classify_channel_thread_notification(&mixed_filter);+ assert!(matches!(mixed, super::ThreadNotifPlan::Unsafe));++ let negated_filter = Expr::is_not(state(item_filters::NotificationState::Done));+ let negated = super::classify_channel_thread_notification(&negated_filter);+ assert!(matches!(negated, super::ThreadNotifPlan::Unsafe));+}++#[sqlx::test(+ fixtures(path = "../../../fixtures", scripts("channels_repo")),+ migrator = "MACRO_DB_MIGRATIONS"+)]+async fn channel_thread_rows_notification_and_participant(+ pool: Pool<Postgres>,+) -> anyhow::Result<()> {+ insert_channel_thread_notification(&pool, USER_A, CH1, MSG3, false, true).await?;+ insert_channel_thread_notification(&pool, USER_A, CH1, MSG1, true, false).await?;+ insert_channel_thread_notification(&pool, USER_B, CH1, MSG31, false, true).await?;+ let bad_notification_id = Uuid::new_v4();+ sqlx::query(+ r#"+ INSERT INTO notification (+ id, notification_event_type, event_item_id, event_item_type,+ service_sender, metadata, secondary_event_item_id, secondary_event_item_type+ )+ VALUES ($1, 'channel_reply', $2, 'channel', 'channels-test', '{}'::jsonb, 'not-a-uuid', 'channel_message')+ "#,+ )+ .bind(bad_notification_id)+ .bind(CH1.to_string())+ .execute(&pool)+ .await?;+ insert_user_notification(&pool, USER_A, bad_notification_id, true, false).await?;++ let participant = Expr::val(ChannelThreadLiteral::Participant(macro_user_id(USER_A)));+ let parent_ids = thread_ids_matching(+ pool.clone(),+ USER_A,+ Expr::and(+ Expr::or(+ Expr::val(ChannelThreadLiteral::NotificationState(+ item_filters::NotificationState::Seen,+ )),+ Expr::val(ChannelThreadLiteral::NotificationState(+ item_filters::NotificationState::Done,+ )),+ ),+ participant.clone(),+ ),+ )+ .await?;+ assert_eq!(parent_ids, vec![MSG3, MSG1]);++ // A notification on the wrong channel does not surface the thread.+ insert_channel_thread_notification(&pool, USER_A, CH2, MSG1, false, false).await?;+ let unseen = thread_ids_matching(+ pool.clone(),+ USER_A,+ Expr::val(ChannelThreadLiteral::NotificationState(+ item_filters::NotificationState::Unseen,+ )),+ )+ .await?;+ assert!(unseen.is_empty());++ let not_done = thread_ids_matching(+ pool.clone(),+ USER_A,+ Expr::is_not(Expr::val(ChannelThreadLiteral::NotificationState(+ item_filters::NotificationState::Done,+ ))),+ )+ .await?;+ assert!(not_done.contains(&MSG3));+ assert!(!not_done.contains(&MSG1));++ let widened = thread_ids_matching(+ pool.clone(),+ USER_A,+ Expr::or(+ Expr::val(ChannelThreadLiteral::NotificationState(+ item_filters::NotificationState::Done,+ )),+ participant,+ ),+ )+ .await?;+ let participating = thread_ids_matching(+ pool,+ USER_A,+ Expr::val(ChannelThreadLiteral::Participant(macro_user_id(USER_A))),+ )+ .await?;+ assert_eq!(widened, participating);+ assert!(widened.contains(&MSG31));+ Ok(())+}+#[sqlx::test(fixtures(path = "../../../fixtures", scripts("channels_repo")),migrator = "MACRO_DB_MIGRATIONS"