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(soup): drive the calendar feed from the caller's notifications (#9121)
gbirman9bb44de548d6
diff --git a/crates/soup/AGENTS.md b/crates/soup/AGENTS.mdindex 6d37e87f52..a9f4a0af8d 100644--- a/crates/soup/AGENTS.md+++ b/crates/soup/AGENTS.md@@ -82,6 +82,16 @@ rows, initiatives, ...) and get back a single unified, paginated result.- Keep the notified feed's notification lookup behind a lateral `OFFSET 0`fence so the caller's `user_notification` rows drive the join; aggregate thelatest notification before applying the cursor or notification-state gates.+- A notification-state conjunct drives a listing from the caller's+ notifications (`strip_notification_conjunction`, which folds `(unseen OR+ seen) AND unseen` to `unseen`): the dynamic query joins `NotificationItems`,+ the calendar leg semi-joins `notified_event_ids_sql` over+ `idx_user_notification_viewer_entity`. The per-row `EXISTS` fold stays only+ for trees the stripper rejects; on the calendar leg it seq-scanned every+ event in the table.+- Calendar access is `owner_id = $1 OR source_link_id = ANY(ARRAY(...))`, not+ an `EXISTS` over `macro_user_links`, so the planner can index both sides of+ the OR.- Queries over a known id list (`*/by_ids.rs`, hydration) and the initiativelisting (`initiative_access_clause`) check access per row with `EXISTS` on`entity_access`, matching sources with `= ANY(ARRAY(SELECT ...))` so they arediff --git a/crates/soup/src/outbound/pg_soup_repo/agent_session.rs b/crates/soup/src/outbound/pg_soup_repo/agent_session.rsindex 4eaaca57da..c08e4b4103 100644--- a/crates/soup/src/outbound/pg_soup_repo/agent_session.rs+++ b/crates/soup/src/outbound/pg_soup_repo/agent_session.rs@@ -96,6 +96,7 @@ pub(super) fn opted_in(expr: &Expr<AgentSessionLiteral>) -> bool {out.include || out.named}+#[tracing::instrument(err, skip_all, fields(limit = req.limit))]pub(super) async fn cursor_soup(db: &PgPool,req: SimpleSortRequest<'_>,diff --git a/crates/soup/src/outbound/pg_soup_repo/calendar_event.rs b/crates/soup/src/outbound/pg_soup_repo/calendar_event.rsindex e6e57a7ddd..64f502085a 100644--- a/crates/soup/src/outbound/pg_soup_repo/calendar_event.rs+++ b/crates/soup/src/outbound/pg_soup_repo/calendar_event.rs@@ -4,6 +4,7 @@ use item_filters::ast::{calendar_event::CalendarEventLiteral,properties::{PropertiesLiteral, PropertyEntityType, properties_filter_can_apply_to},};+use macro_user_id::user_id::MacroUserIdStr;use model_entity::EntityType;use models_pagination::{Query, SimpleSortMethod};use models_soup::{@@ -13,6 +14,8 @@ use models_soup::{use sqlx::{FromRow, PgPool, Postgres, QueryBuilder};use uuid::Uuid;+use super::candidate_gates::UUID_PATTERN;+use super::expanded::dynamic::{NotificationPredicate, strip_notification_conjunction};use crate::domain::models::{AdvancedSortParams, SimpleSortQuery, SimpleSortRequest};#[derive(FromRow)]@@ -49,6 +52,7 @@ struct CursorParts {property_filter: Option<Expr<PropertiesLiteral>>,}+#[tracing::instrument(err, skip_all, fields(limit = req.limit))]pub(super) async fn cursor_soup(db: &PgPool,req: SimpleSortRequest<'_>,@@ -60,14 +64,32 @@ pub(super) async fn cursor_soup(return Ok(Vec::new());}+ // A notification-state conjunct bounds the page to events the caller was+ // notified about, a set the viewer index answers directly; folding it per+ // event instead scanned every calendar event in the table.+ let notification = parts.filter.as_ref().and_then(|filter| {+ strip_notification_conjunction(filter, |literal| match literal {+ CalendarEventLiteral::NotificationState(state) => {+ Some(NotificationPredicate::state(*state))+ }+ _ => None,+ })+ });+ let (notified, filter) = match ¬ification {+ Some((predicate, rest)) => (Some(*predicate), rest.as_ref()),+ None => (None, parts.filter.as_ref()),+ };+let sort = sort_sql(parts.sort);- let mut query =- QueryBuilder::<Postgres>::new(format!("{} WHERE (event.owner_id = ", select_sql()));- query.push_bind(req.user_id.as_ref().to_string());- query.push(" OR EXISTS (SELECT 1 FROM macro_user_links link WHERE link.link_id = event.source_link_id AND link.primary_macro_id = ");- query.push_bind(req.user_id.as_ref().to_string());- query.push("))");- if let Some(filter) = &parts.filter {+ let mut query = QueryBuilder::<Postgres>::new(format!("{} WHERE ", select_sql()));+ push_access(&mut query, req.user_id);+ if let Some(predicate) = notified {+ query.push(format!(+ " AND event.id IN ({})",+ notified_event_ids_sql(predicate)+ ));+ }+ if let Some(filter) = filter {query.push(" AND (");push_filter(&mut query, filter);query.push(")");@@ -109,12 +131,9 @@ pub(super) async fn by_ids(return Ok(Vec::new());}- let mut query =- QueryBuilder::<Postgres>::new(format!("{} WHERE (event.owner_id = ", select_sql()));- query.push_bind(req.user_id.as_ref().to_string());- query.push(" OR EXISTS (SELECT 1 FROM macro_user_links link WHERE link.link_id = event.source_link_id AND link.primary_macro_id = ");- query.push_bind(req.user_id.as_ref().to_string());- query.push(")) AND event.id = ANY(");+ let mut query = QueryBuilder::<Postgres>::new(format!("{} WHERE ", select_sql()));+ push_access(&mut query, req.user_id);+ query.push(" AND event.id = ANY(");query.push_bind(ids);query.push(") ORDER BY GREATEST(event.updated_at, event.last_reminder_fired_at) DESC, event.id DESC",@@ -128,6 +147,35 @@ pub(super) async fn by_ids(.collect()}+/// Calendar events are owner- or delegation-scoped, never `entity_access`+/// rows: visible iff the caller owns the event or is delegated the inbox it+/// was synced from. Binds the caller first, so `$1` is the requesting user+/// for every clause rendered after it. The delegated links are collected once+/// rather than probed per event, which lets the planner index both sides of+/// the OR.+fn push_access(query: &mut QueryBuilder<'_, Postgres>, user_id: MacroUserIdStr<'_>) {+ query.push("(event.owner_id = ");+ query.push_bind(user_id.as_ref().to_string());+ query.push(+ " OR event.source_link_id = ANY(ARRAY(SELECT link.link_id FROM macro_user_links link WHERE link.primary_macro_id = $1)))",+ );+}++/// Ids of the events the caller holds a non-deleted notification for in the+/// predicate's states. Each recipient row carries its notification's entity,+/// so `idx_user_notification_viewer_entity` answers this without touching+/// `notification`. The id column is unconstrained text: a malformed id is+/// filtered out before the projection casts it.+fn notified_event_ids_sql(predicate: NotificationPredicate) -> String {+ format!(+ "SELECT un.event_item_id::uuid FROM user_notification un \+ WHERE un.user_id = $1 AND un.deleted_at IS NULL \+ AND un.event_item_type = 'calendar_event' AND {} \+ AND un.event_item_id ~ '{UUID_PATTERN}'",+ predicate.sql()+ )+}+fn cursor_parts(cursor: SimpleSortQuery) -> CursorParts {match cursor {SimpleSortQuery::NoFilter(query) => parts_from_query(&query, None, None),diff --git a/crates/soup/src/outbound/pg_soup_repo/calendar_event/test.rs b/crates/soup/src/outbound/pg_soup_repo/calendar_event/test.rsindex 83f4c295ef..7893bbbc52 100644--- a/crates/soup/src/outbound/pg_soup_repo/calendar_event/test.rs+++ b/crates/soup/src/outbound/pg_soup_repo/calendar_event/test.rs@@ -1,7 +1,9 @@use super::*;+use item_filters::{NotificationState, ast::EntityFilterAst};use macro_db_migrator::MACRO_DB_MIGRATIONS;use macro_user_id::user_id::MacroUserIdStr;-use models_pagination::Frecency;+use models_pagination::{CursorVal, CursorWithValAndFilter, Frecency};+use std::sync::Arc;#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]async fn frecency_fallback_keeps_calendar_events_with_aggregates(@@ -160,3 +162,275 @@ async fn recency_sort_uses_reminder_delivery_time(pool: PgPool) -> anyhow::Resul));Ok(())}++const UNREAD_OWNER: &str = "macro|[email protected]";+const UNREAD_CHILD: &str = "macro|[email protected]";+const UNREAD_STRANGER: &str = "macro|[email protected]";++/// The owner's events, newest first: `unseen` and `seen` each hold one live+/// notification in that state, `done` a done one, `deleted` only a+/// soft-deleted unseen one, `twice` a done and an unseen one, and `quiet`+/// only a stranger's unseen one. `delegated` belongs to the child whose+/// inbox the owner is delegated; `strangers` to a user the owner cannot+/// read, though the owner was notified about it.+struct UnreadFixture {+ unseen: Uuid,+ seen: Uuid,+ delegated: Uuid,+ twice: Uuid,+}++async fn seed_unread(pool: &PgPool) -> anyhow::Result<UnreadFixture> {+ let [owner_link, child_link, stranger_link] = std::array::from_fn(|_| Uuid::now_v7());+ for (user, link) in [+ (UNREAD_OWNER, owner_link),+ (UNREAD_CHILD, child_link),+ (UNREAD_STRANGER, stranger_link),+ ] {+ let macro_user = Uuid::now_v7();+ sqlx::query!(+ "INSERT INTO macro_user (id, username, email, stripe_customer_id) VALUES ($1, $2, $2, $2)",+ macro_user,+ user,+ )+ .execute(pool)+ .await?;+ sqlx::query!(+ r#"INSERT INTO "User" (id, email, macro_user_id) VALUES ($1, $1, $2)"#,+ user,+ macro_user,+ )+ .execute(pool)+ .await?;+ sqlx::query!(+ "INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider) VALUES ($1, $2, $2, $3, 'GMAIL')",+ link,+ user,+ user,+ )+ .execute(pool)+ .await?;+ }+ sqlx::query!(+ "INSERT INTO macro_user_links (primary_macro_id, child_macro_id, link_id) VALUES ($1, $2, $3)",+ UNREAD_OWNER,+ UNREAD_CHILD,+ child_link,+ )+ .execute(pool)+ .await?;++ let [+ unseen,+ seen,+ done,+ deleted,+ quiet,+ delegated,+ strangers,+ twice,+ ] = std::array::from_fn(|_| Uuid::now_v7());+ for (id, owner, link, days_ago, status) in [+ (unseen, UNREAD_OWNER, owner_link, 1, "confirmed"),+ (seen, UNREAD_OWNER, owner_link, 2, "tentative"),+ (done, UNREAD_OWNER, owner_link, 3, "confirmed"),+ (deleted, UNREAD_OWNER, owner_link, 4, "confirmed"),+ (quiet, UNREAD_OWNER, owner_link, 5, "confirmed"),+ (delegated, UNREAD_CHILD, child_link, 6, "confirmed"),+ (strangers, UNREAD_STRANGER, stranger_link, 7, "confirmed"),+ (twice, UNREAD_OWNER, owner_link, 8, "confirmed"),+ ] {+ sqlx::query!(+ r#"+ INSERT INTO calendar_events (+ id, owner_id, source_link_id, ical_uid, title, status,+ starts_at, ends_at, canonical_source_kind, updated_at+ )+ VALUES (+ $1, $2, $3, $6, 'Unread fixture', $4,+ now(), now() + interval '1 hour', 'google', now() - make_interval(days => $5)+ )+ "#,+ id,+ owner,+ link,+ status,+ days_ago,+ id.to_string(),+ )+ .execute(pool)+ .await?;+ }+ for (event, user, state, soft_deleted) in [+ (unseen, UNREAD_OWNER, "unseen", false),+ (seen, UNREAD_OWNER, "seen", false),+ (done, UNREAD_OWNER, "done", false),+ (deleted, UNREAD_OWNER, "unseen", true),+ (quiet, UNREAD_STRANGER, "unseen", false),+ (delegated, UNREAD_OWNER, "unseen", false),+ (strangers, UNREAD_OWNER, "unseen", false),+ (twice, UNREAD_OWNER, "done", false),+ (twice, UNREAD_OWNER, "unseen", false),+ ] {+ let notification = Uuid::now_v7();+ sqlx::query!(+ "INSERT INTO notification (id, notification_event_type, event_item_id, event_item_type, service_sender) VALUES ($1, 'calendar_reminder', $2, 'calendar_event', 'test')",+ notification,+ event.to_string(),+ )+ .execute(pool)+ .await?;+ sqlx::query!(+ r#"+ INSERT INTO user_notification (+ user_id, notification_id, state, deleted_at,+ event_item_type, event_item_id, notification_event_type+ )+ VALUES (+ $1, $2, ($3::text)::notification_state, CASE WHEN $4::bool THEN now() END,+ 'calendar_event', $5, 'calendar_reminder'+ )+ "#,+ user,+ notification,+ state,+ soft_deleted,+ event.to_string(),+ )+ .execute(pool)+ .await?;+ }+ Ok(UnreadFixture {+ unseen,+ seen,+ delegated,+ twice,+ })+}++fn state(state: NotificationState) -> Expr<CalendarEventLiteral> {+ Expr::val(CalendarEventLiteral::NotificationState(state))+}++/// The Home inbox's calendar clause, `calendarEventDone = false`.+fn inbox() -> Expr<CalendarEventLiteral> {+ Expr::or(+ state(NotificationState::Unseen),+ state(NotificationState::Seen),+ )+}++/// The inbox's unread facet adds `AND unseen` to [`inbox`].+fn unread() -> Expr<CalendarEventLiteral> {+ Expr::and(inbox(), state(NotificationState::Unseen))+}++fn owner_request(+ filter: Expr<CalendarEventLiteral>,+ limit: u16,+ after: Option<&SoupCalendarEvent>,+) -> SimpleSortRequest<'static> {+ let filter = EntityFilterAst {+ calendar_event_filter: Some(Arc::new(filter)),+ ..EntityFilterAst::default()+ };+ let cursor = match after {+ Some(event) => SimpleSortQuery::ItemsFilter(Query::Cursor(CursorWithValAndFilter {+ id: event.id,+ limit: limit.into(),+ val: CursorVal {+ sort_type: SimpleSortMethod::UpdatedAt,+ last_val: event.updated_at,+ },+ filter,+ })),+ None => SimpleSortQuery::ItemsFilter(Query::Sort(SimpleSortMethod::UpdatedAt, filter)),+ };+ SimpleSortRequest {+ limit,+ cursor,+ user_id: MacroUserIdStr::parse_from_str(UNREAD_OWNER).expect("valid user id"),+ }+}++fn event_ids(items: &[SoupItem<()>]) -> Vec<Uuid> {+ items+ .iter()+ .map(|item| match item {+ SoupItem::CalendarEvent(event) => event.id,+ other => panic!("unexpected soup item: {other:?}"),+ })+ .collect()+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn unread_feed_lists_events_the_caller_holds_an_unseen_notification_for(+ pool: PgPool,+) -> anyhow::Result<()> {+ let fixture = seed_unread(&pool).await?;++ let unread_items = cursor_soup(&pool, owner_request(unread(), 10, None)).await?;+ assert_eq!(+ event_ids(&unread_items),+ vec![fixture.unseen, fixture.delegated, fixture.twice]+ );++ let inbox_items = cursor_soup(&pool, owner_request(inbox(), 10, None)).await?;+ assert_eq!(+ event_ids(&inbox_items),+ vec![+ fixture.unseen,+ fixture.seen,+ fixture.delegated,+ fixture.twice+ ]+ );++ // What remains of the tree still applies to the notified events.+ let tentative = Expr::and(+ inbox(),+ Expr::val(CalendarEventLiteral::Status("tentative".to_string())),+ );+ let tentative_items = cursor_soup(&pool, owner_request(tentative, 10, None)).await?;+ assert_eq!(event_ids(&tentative_items), vec![fixture.seen]);+ Ok(())+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn unread_feed_pages_without_repeating_or_skipping(pool: PgPool) -> anyhow::Result<()> {+ let fixture = seed_unread(&pool).await?;++ let first = cursor_soup(&pool, owner_request(unread(), 2, None)).await?;+ assert_eq!(event_ids(&first), vec![fixture.unseen, fixture.delegated]);+ let SoupItem::CalendarEvent(last) = &first[1] else {+ unreachable!()+ };++ let second = cursor_soup(&pool, owner_request(unread(), 2, Some(last))).await?;+ assert_eq!(event_ids(&second), vec![fixture.twice]);+ Ok(())+}++/// A notification literal under OR keeps the per-event fold; the semi-join+/// over the caller's notifications must agree with it.+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn notified_semi_join_matches_the_per_event_fold(pool: PgPool) -> anyhow::Result<()> {+ let fixture = seed_unread(&pool).await?;+ let folded = Expr::or(+ state(NotificationState::Unseen),+ Expr::and(+ state(NotificationState::Unseen),+ Expr::val(CalendarEventLiteral::Id(Uuid::nil())),+ ),+ );++ let semi_joined = cursor_soup(&pool, owner_request(unread(), 10, None)).await?;+ let per_event = cursor_soup(&pool, owner_request(folded, 10, None)).await?;++ assert_eq!(event_ids(&semi_joined), event_ids(&per_event));+ assert_eq!(+ event_ids(&semi_joined),+ vec![fixture.unseen, fixture.delegated, fixture.twice]+ );+ Ok(())+}diff --git a/crates/soup/src/outbound/pg_soup_repo/candidate_gates.rs b/crates/soup/src/outbound/pg_soup_repo/candidate_gates.rsindex c4625e3baf..82d2dec72c 100644--- a/crates/soup/src/outbound/pg_soup_repo/candidate_gates.rs+++ b/crates/soup/src/outbound/pg_soup_repo/candidate_gates.rs@@ -153,14 +153,16 @@ pub(super) fn project_gate(id_sql: &str, filter: Option<&EntityFilterAst>) -> St)}+/// Regex a `notification.event_item_id` must match before it is cast to uuid.+pub(super) const UUID_PATTERN: &str =+ "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$";+/// Guards a gate whose body casts `{id_sql}::uuid`: a malformed id in the/// unconstrained TEXT column must drop that one row, not abort the whole/// page with a cast error. Nested CASE because SQL `AND` does not guarantee/// evaluation order, but CASE arms do.pub(super) fn uuid_guarded(id_sql: &str, gate: String) -> String {- format!(- "CASE WHEN {id_sql} ~ '^[0-9a-fA-F]{{8}}-[0-9a-fA-F]{{4}}-[0-9a-fA-F]{{4}}-[0-9a-fA-F]{{4}}-[0-9a-fA-F]{{12}}$' THEN {gate} ELSE FALSE END"- )+ format!("CASE WHEN {id_sql} ~ '{UUID_PATTERN}' THEN {gate} ELSE FALSE END")}/// Channels are participant-gated, not `entity_access` rows, are neverdiff --git a/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic.rs b/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic.rsindex a7709ac781..cd2a983f22 100644--- a/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic.rs+++ b/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic.rs@@ -610,7 +610,9 @@ pub(in crate::outbound::pg_soup_repo) fn build_notification_state_clause(}// A union of exact states for one EXISTS predicate. Do not intersect these on-// AND: separate literals may be witnessed by different notification rows.+// AND: separate literals may be witnessed by different notification rows. A+// conjunction folds only when one side implies the other, since the narrower+// side's witness row satisfies both.#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub(in crate::outbound::pg_soup_repo) struct NotificationPredicate(u8);@@ -624,6 +626,11 @@ impl NotificationPredicate {})}+ /// Whether every row matching `self` also matches `other`.+ fn implies(self, other: Self) -> bool {+ self.0 & other.0 == self.0+ }+pub(in crate::outbound::pg_soup_repo) fn sql(self) -> &'static str {match self.0 {1 => "un.state = 'unseen'",@@ -694,7 +701,13 @@ fn push_not_inward<T: Clone>(expr: &Expr<T>, negated: bool) -> Expr<T> {}}-fn strip_notification_conjunction<T: Clone>(+/// Splits one notification-state predicate off the top-level conjunction of+/// `expr`, returning it with whatever remains of the tree, so the query can+/// drive from the viewer's notifications instead of probing them per row.+/// `None` when the tree has no such conjunct that is equivalent to the whole+/// tree's notification constraint (NOT, a notification literal under OR with+/// other predicates, or two conjuncts neither of which implies the other).+pub(in crate::outbound::pg_soup_repo) fn strip_notification_conjunction<T: Clone>(expr: &Expr<T>,notification_predicate: impl Fn(&T) -> Option<NotificationPredicate> + Copy,) -> Option<(NotificationPredicate, Option<Expr<T>>)> {@@ -705,14 +718,22 @@ fn strip_notification_conjunction<T: Clone>(let right = strip_notification_conjunction(b, notification_predicate);match (left, right) {(Some((left_pred, left_expr)), Some((right_pred, right_expr)))- if left_pred == right_pred =>+ if left_pred.implies(right_pred) || right_pred.implies(left_pred) =>{+ // The client's unread feed asks for `(unseen OR seen) AND+ // unseen`: the narrower side's witness row is also one for+ // the wider side, so only the narrower predicate remains.+ let pred = if left_pred.implies(right_pred) {+ left_pred+ } else {+ right_pred+ };let stripped = match (left_expr, right_expr) {(Some(left), Some(right)) => Some(Expr::and(left, right)),(Some(expr), None) | (None, Some(expr)) => Some(expr),(None, None) => None,};- Some((left_pred, stripped))+ Some((pred, stripped))}(Some((pred, left_expr)), None)if !expr_contains_notification(b, notification_predicate) =>diff --git a/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic/test.rs b/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic/test.rsindex 2403da94fb..205e713741 100644--- a/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic/test.rs+++ b/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic/test.rs@@ -262,3 +262,36 @@ fn initiative_name_literals_are_escaped_and_not_like_patterns() {assert!(sql.contains("'O''Reilly_%'"));assert!(sql.contains("strpos("));}++#[test]+fn notification_conjunction_folds_to_the_side_that_implies_the_other() {+ use item_filters::NotificationState::{Done, Seen, Unseen};+ let state = |state| Expr::val(DocumentLiteral::NotificationState(state));+ let inbox = || Expr::or(state(Unseen), state(Seen));+ let strip = |expr: &Expr<DocumentLiteral>| {+ strip_notification_conjunction(expr, |literal| match literal {+ DocumentLiteral::NotificationState(state) => Some(NotificationPredicate::state(*state)),+ _ => None,+ })+ };++ // The unread feed: `(unseen OR seen) AND unseen` needs only the unseen witness.+ let (predicate, rest) = strip(&Expr::and(inbox(), state(Unseen))).unwrap();+ assert_eq!(predicate, NotificationPredicate::state(Unseen));+ assert!(rest.is_none());++ let (predicate, rest) = strip(&Expr::and(+ Expr::and(state(Unseen), Expr::val(DocumentLiteral::Importance(true))),+ inbox(),+ ))+ .unwrap();+ assert_eq!(predicate, NotificationPredicate::state(Unseen));+ assert!(matches!(+ rest,+ Some(Expr::Literal(DocumentLiteral::Importance(true)))+ ));++ // Neither side implies the other: different rows may witness each.+ assert!(strip(&Expr::and(state(Unseen), state(Seen))).is_none());+ assert!(strip(&Expr::and(inbox(), state(Done))).is_none());+}diff --git a/crates/soup/src/outbound/pg_soup_repo/expanded/tests.rs b/crates/soup/src/outbound/pg_soup_repo/expanded/tests.rsindex c3e364679f..ae0a22a35f 100644--- a/crates/soup/src/outbound/pg_soup_repo/expanded/tests.rs+++ b/crates/soup/src/outbound/pg_soup_repo/expanded/tests.rs@@ -7067,3 +7067,110 @@ async fn crm_document_literals_match_attachments_and_associations(assert!(not_acme.contains(DOC_B) && not_acme.contains(DOC_D));Ok(())}++/// The Home unread feed sends `(unseen OR seen) AND unseen` for every entity+/// type. The notification join it now takes must agree with the per-row fold+/// of an equivalent tree the stripper leaves alone.+#[sqlx::test(+ fixtures(+ path = "../../../../../macro_db_client/fixtures",+ scripts("entity_filter_tests")+ ),+ migrator = "MACRO_DB_MIGRATIONS"+)]+async fn test_unread_conjunction_matches_unoptimized_equivalent(db: PgPool) -> anyhow::Result<()> {+ use item_filters::NotificationState::{Seen, Unseen};+ let unseen_doc = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";+ let seen_doc = "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb";+ let unseen_chat = "a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1";+ let seen_project = "11111111-1111-1111-1111-111111111111";+ // The viewer `run_notification_filter` queries as owns this document.+ let user_id = sqlx::query_scalar!(+ r#"SELECT owner FROM "Document" WHERE id = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'"#+ )+ .fetch_one(&db)+ .await?;+ for (notification_id, item_type, item_id, seen) in [+ (+ "30000000-0000-0000-0000-000000000001",+ "document",+ unseen_doc,+ false,+ ),+ (+ "30000000-0000-0000-0000-000000000002",+ "document",+ seen_doc,+ true,+ ),+ (+ "30000000-0000-0000-0000-000000000003",+ "chat",+ unseen_chat,+ false,+ ),+ (+ "30000000-0000-0000-0000-000000000004",+ "project",+ seen_project,+ true,+ ),+ ] {+ insert_notification(+ &db,+ notification_id,+ &user_id,+ item_type,+ item_id,+ false,+ seen,+ )+ .await?;+ }++ let doc = |state| Expr::val(DocumentLiteral::NotificationState(state));+ let chat = |state| Expr::val(ChatLiteral::NotificationState(state));+ let project = |state| Expr::val(ProjectLiteral::NotificationState(state));+ let optimized = EntityFilterAst {+ document_filter: Some(Arc::new(Expr::and(+ Expr::or(doc(Unseen), doc(Seen)),+ doc(Unseen),+ ))),+ chat_filter: Some(Arc::new(Expr::and(+ Expr::or(chat(Unseen), chat(Seen)),+ chat(Unseen),+ ))),+ project_filter: Some(Arc::new(Expr::and(+ Expr::or(project(Unseen), project(Seen)),+ project(Unseen),+ ))),+ ..mock_empty_ast()+ };+ // Equivalent, but a notification literal under OR with another predicate+ // stays on the correlated-EXISTS path.+ let unoptimized = EntityFilterAst {+ document_filter: Some(Arc::new(Expr::or(+ doc(Unseen),+ Expr::and(doc(Unseen), Expr::val(DocumentLiteral::Importance(true))),+ ))),+ chat_filter: Some(Arc::new(Expr::or(+ chat(Unseen),+ Expr::and(chat(Unseen), Expr::val(ChatLiteral::Importance(true))),+ ))),+ project_filter: Some(Arc::new(Expr::or(+ project(Unseen),+ Expr::and(project(Unseen), Expr::val(ProjectLiteral::Importance(true))),+ ))),+ ..mock_empty_ast()+ };++ let optimized_ids = run_notification_filter(&db, optimized).await?;+ let unoptimized_ids = run_notification_filter(&db, unoptimized).await?;++ assert_eq!(optimized_ids, unoptimized_ids);+ assert!(optimized_ids.contains(&uuid(unseen_doc)));+ assert!(optimized_ids.contains(&uuid(unseen_chat)));+ assert!(!optimized_ids.contains(&uuid(seen_doc)));+ assert!(!optimized_ids.contains(&uuid(seen_project)));+ Ok(())+}