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
fix(soup): group by query performance (#6352)
Will Hutchinson9a1b6582c4ac
diff --git a/.sqlx/query-0912a0912bc058ca42843caa1595a4bb4198de7c4cb6d2f1e42fac2267a4866b.json b/.sqlx/query-0912a0912bc058ca42843caa1595a4bb4198de7c4cb6d2f1e42fac2267a4866b.jsonnew file mode 100644index 0000000000..65b21e4307--- /dev/null+++ b/.sqlx/query-0912a0912bc058ca42843caa1595a4bb4198de7c4cb6d2f1e42fac2267a4866b.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO document_sub_type (document_id, sub_type) SELECT id, 'task' FROM unnest($1::text[]) id ON CONFLICT DO NOTHING",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "TextArray"+ ]+ },+ "nullable": []+ },+ "hash": "0912a0912bc058ca42843caa1595a4bb4198de7c4cb6d2f1e42fac2267a4866b"+}diff --git a/.sqlx/query-1176d102637cc13aed0121feb6f0d3799e150c099bcccbf1b809b00d075338f1.json b/.sqlx/query-1176d102637cc13aed0121feb6f0d3799e150c099bcccbf1b809b00d075338f1.jsonnew file mode 100644index 0000000000..456df98e2d--- /dev/null+++ b/.sqlx/query-1176d102637cc13aed0121feb6f0d3799e150c099bcccbf1b809b00d075338f1.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO entity_access (entity_id, entity_type, source_id, source_type, access_level, granted_from_project_id) SELECT id::uuid, 'document', 'macro|[email protected]', 'user', 'view', 'aaaaaaaa-ffff-ffff-ffff-ffffffffffff' FROM unnest($1::text[]) id ON CONFLICT DO NOTHING",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "TextArray"+ ]+ },+ "nullable": []+ },+ "hash": "1176d102637cc13aed0121feb6f0d3799e150c099bcccbf1b809b00d075338f1"+}diff --git a/.sqlx/query-217acdbed6242ff0c4b45c2d2d20fabf7819587af3f045b0bbcd500b6f9c5107.json b/.sqlx/query-217acdbed6242ff0c4b45c2d2d20fabf7819587af3f045b0bbcd500b6f9c5107.jsonnew file mode 100644index 0000000000..32825ab260--- /dev/null+++ b/.sqlx/query-217acdbed6242ff0c4b45c2d2d20fabf7819587af3f045b0bbcd500b6f9c5107.json@@ -0,0 +1,12 @@+{+ "db_name": "PostgreSQL",+ "query": "ANALYZE",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": []+ },+ "nullable": []+ },+ "hash": "217acdbed6242ff0c4b45c2d2d20fabf7819587af3f045b0bbcd500b6f9c5107"+}diff --git a/.sqlx/query-3a1113165192ebbcfc408f47ac619aa213e32acb94b1d488e35a090e67c25cfa.json b/.sqlx/query-3a1113165192ebbcfc408f47ac619aa213e32acb94b1d488e35a090e67c25cfa.jsonnew file mode 100644index 0000000000..058e507bdb--- /dev/null+++ b/.sqlx/query-3a1113165192ebbcfc408f47ac619aa213e32acb94b1d488e35a090e67c25cfa.json@@ -0,0 +1,17 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO calendar_events (id, owner_id, source_link_id, ical_uid, title, starts_at, ends_at, canonical_source_kind) VALUES ($1, $2, $3, $4, 'calendar access', '2026-01-01', '2026-01-02', 'google') ON CONFLICT DO NOTHING",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "Text",+ "Uuid",+ "Text"+ ]+ },+ "nullable": []+ },+ "hash": "3a1113165192ebbcfc408f47ac619aa213e32acb94b1d488e35a090e67c25cfa"+}diff --git a/.sqlx/query-3b2593c3b32464aeaf6a745998a2d899f543873636d1d45c69d5773f6722ab3c.json b/.sqlx/query-3b2593c3b32464aeaf6a745998a2d899f543873636d1d45c69d5773f6722ab3c.jsonnew file mode 100644index 0000000000..27f4d56242--- /dev/null+++ b/.sqlx/query-3b2593c3b32464aeaf6a745998a2d899f543873636d1d45c69d5773f6722ab3c.json@@ -0,0 +1,17 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO entity_properties (id, entity_id, entity_type, property_definition_id, values) VALUES ($1, $2, 'TASK', $3, $4) ON CONFLICT DO NOTHING",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "Text",+ "Uuid",+ "Jsonb"+ ]+ },+ "nullable": []+ },+ "hash": "3b2593c3b32464aeaf6a745998a2d899f543873636d1d45c69d5773f6722ab3c"+}diff --git a/.sqlx/query-3c24ed41ba475636af7da1ea9033cba24cfff815ba342cd53a2f3db3a4df775a.json b/.sqlx/query-3c24ed41ba475636af7da1ea9033cba24cfff815ba342cd53a2f3db3a4df775a.jsonnew file mode 100644index 0000000000..ff4471fef2--- /dev/null+++ b/.sqlx/query-3c24ed41ba475636af7da1ea9033cba24cfff815ba342cd53a2f3db3a4df775a.json@@ -0,0 +1,18 @@+{+ "db_name": "PostgreSQL",+ "query": "UPDATE calendar_events SET created_at = $1, updated_at = $2, last_reminder_fired_at = CASE WHEN id = $3 THEN $4::timestamptz ELSE NULL END WHERE id = ANY($5)",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Timestamptz",+ "Timestamptz",+ "Uuid",+ "Timestamptz",+ "UuidArray"+ ]+ },+ "nullable": []+ },+ "hash": "3c24ed41ba475636af7da1ea9033cba24cfff815ba342cd53a2f3db3a4df775a"+}diff --git a/.sqlx/query-4800f97cdff83b99d03f828d9498342b18aaa97974ac87cd3c68fd5bc90678c2.json b/.sqlx/query-4800f97cdff83b99d03f828d9498342b18aaa97974ac87cd3c68fd5bc90678c2.jsonnew file mode 100644index 0000000000..8f4416bf88--- /dev/null+++ b/.sqlx/query-4800f97cdff83b99d03f828d9498342b18aaa97974ac87cd3c68fd5bc90678c2.json@@ -0,0 +1,20 @@+{+ "db_name": "PostgreSQL",+ "query": "SELECT current_date::timestamptz + interval '12 hours' AS \"today!\"",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "today!",+ "type_info": "Timestamptz"+ }+ ],+ "parameters": {+ "Left": []+ },+ "nullable": [+ null+ ]+ },+ "hash": "4800f97cdff83b99d03f828d9498342b18aaa97974ac87cd3c68fd5bc90678c2"+}diff --git a/.sqlx/query-67548c84a432a8a3fc4b1da770f4fba29282314cae0933fc191f54b519220648.json b/.sqlx/query-67548c84a432a8a3fc4b1da770f4fba29282314cae0933fc191f54b519220648.jsonnew file mode 100644index 0000000000..145d7f3167--- /dev/null+++ b/.sqlx/query-67548c84a432a8a3fc4b1da770f4fba29282314cae0933fc191f54b519220648.json@@ -0,0 +1,12 @@+{+ "db_name": "PostgreSQL",+ "query": "UPDATE \"Document\" SET \"deletedAt\" = now() WHERE id = '11111111-0000-0000-0000-000000000000'",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": []+ },+ "nullable": []+ },+ "hash": "67548c84a432a8a3fc4b1da770f4fba29282314cae0933fc191f54b519220648"+}diff --git a/.sqlx/query-8dd1c5a77b6178a5b5378aa462c00edcbb4e00d28e4ad53db3eb66fafc20e337.json b/.sqlx/query-8dd1c5a77b6178a5b5378aa462c00edcbb4e00d28e4ad53db3eb66fafc20e337.jsonnew file mode 100644index 0000000000..4a7fa822e4--- /dev/null+++ b/.sqlx/query-8dd1c5a77b6178a5b5378aa462c00edcbb4e00d28e4ad53db3eb66fafc20e337.json@@ -0,0 +1,16 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider) VALUES ($1, $2, $2, $3, 'GMAIL') ON CONFLICT DO NOTHING",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "Text",+ "Varchar"+ ]+ },+ "nullable": []+ },+ "hash": "8dd1c5a77b6178a5b5378aa462c00edcbb4e00d28e4ad53db3eb66fafc20e337"+}diff --git a/.sqlx/query-b135b9523b42eab1c92a7d5cc4a425a6ce84d6f64b77cd1143d6a5ec4a3bd920.json b/.sqlx/query-b135b9523b42eab1c92a7d5cc4a425a6ce84d6f64b77cd1143d6a5ec4a3bd920.jsonnew file mode 100644index 0000000000..2062217e5a--- /dev/null+++ b/.sqlx/query-b135b9523b42eab1c92a7d5cc4a425a6ce84d6f64b77cd1143d6a5ec4a3bd920.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO \"DocumentInstance\" (id, \"documentId\", sha)\n SELECT 100 + ordinality, id, 'test' FROM unnest($1::text[]) WITH ORDINALITY AS ids(id, ordinality)\n ON CONFLICT DO NOTHING",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "TextArray"+ ]+ },+ "nullable": []+ },+ "hash": "b135b9523b42eab1c92a7d5cc4a425a6ce84d6f64b77cd1143d6a5ec4a3bd920"+}diff --git a/.sqlx/query-b792e97d62ad9366fb8d741745928f058306782e77dfc87af9ac77ea676a5692.json b/.sqlx/query-b792e97d62ad9366fb8d741745928f058306782e77dfc87af9ac77ea676a5692.jsonnew file mode 100644index 0000000000..093f305d25--- /dev/null+++ b/.sqlx/query-b792e97d62ad9366fb8d741745928f058306782e77dfc87af9ac77ea676a5692.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO \"Document\" (id, name, owner, \"createdAt\", \"updatedAt\")\n SELECT id, 'explain task', 'macro|[email protected]', '2026-01-01', '2026-01-02'\n FROM unnest($1::text[]) id ON CONFLICT DO NOTHING",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "TextArray"+ ]+ },+ "nullable": []+ },+ "hash": "b792e97d62ad9366fb8d741745928f058306782e77dfc87af9ac77ea676a5692"+}diff --git a/.sqlx/query-cae308981be4bbd27dda7caa4ea5d737cc7c5a3215e01254f933a7acff92d3e4.json b/.sqlx/query-cae308981be4bbd27dda7caa4ea5d737cc7c5a3215e01254f933a7acff92d3e4.jsonnew file mode 100644index 0000000000..c29c9ab7df--- /dev/null+++ b/.sqlx/query-cae308981be4bbd27dda7caa4ea5d737cc7c5a3215e01254f933a7acff92d3e4.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO macro_user_links (primary_macro_id, child_macro_id, link_id) VALUES ('macro|[email protected]', 'macro|[email protected]', $1) ON CONFLICT DO NOTHING",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "cae308981be4bbd27dda7caa4ea5d737cc7c5a3215e01254f933a7acff92d3e4"+}diff --git a/.sqlx/query-df1f1d15d442789a5b9c81cdddf44d88d5748499cc48865023ddc1ff1587d0f6.json b/.sqlx/query-df1f1d15d442789a5b9c81cdddf44d88d5748499cc48865023ddc1ff1587d0f6.jsonnew file mode 100644index 0000000000..7fe6da3923--- /dev/null+++ b/.sqlx/query-df1f1d15d442789a5b9c81cdddf44d88d5748499cc48865023ddc1ff1587d0f6.json@@ -0,0 +1,20 @@+{+ "db_name": "PostgreSQL",+ "query": "SELECT version()",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "version",+ "type_info": "Text"+ }+ ],+ "parameters": {+ "Left": []+ },+ "nullable": [+ null+ ]+ },+ "hash": "df1f1d15d442789a5b9c81cdddf44d88d5748499cc48865023ddc1ff1587d0f6"+}diff --git a/.sqlx/query-e313cb4b4f14331990417320890f6d1dcc9f4743dc4842edf3e3edd024678fce.json b/.sqlx/query-e313cb4b4f14331990417320890f6d1dcc9f4743dc4842edf3e3edd024678fce.jsonnew file mode 100644index 0000000000..f19298e382--- /dev/null+++ b/.sqlx/query-e313cb4b4f14331990417320890f6d1dcc9f4743dc4842edf3e3edd024678fce.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO \"Document\" (id, name, owner, \"createdAt\", \"updatedAt\")\n SELECT id, 'pagination task', 'macro|[email protected]', '2026-01-01', '2026-01-02'\n FROM unnest($1::text[]) id ON CONFLICT DO NOTHING",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "TextArray"+ ]+ },+ "nullable": []+ },+ "hash": "e313cb4b4f14331990417320890f6d1dcc9f4743dc4842edf3e3edd024678fce"+}diff --git a/.sqlx/query-e9e07eb22736f0e8855bc323cd978ad9132a45423ffd00ea0af27e6f191dfe95.json b/.sqlx/query-e9e07eb22736f0e8855bc323cd978ad9132a45423ffd00ea0af27e6f191dfe95.jsonnew file mode 100644index 0000000000..5bff4a9431--- /dev/null+++ b/.sqlx/query-e9e07eb22736f0e8855bc323cd978ad9132a45423ffd00ea0af27e6f191dfe95.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO frecency_aggregates (entity_id, entity_type, user_id, first_event) VALUES ($1, 'document', 'macro|[email protected]', now()) ON CONFLICT DO NOTHING",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text"+ ]+ },+ "nullable": []+ },+ "hash": "e9e07eb22736f0e8855bc323cd978ad9132a45423ffd00ea0af27e6f191dfe95"+}diff --git a/.sqlx/query-ee6275a42ff3150baf5cb1005548873c59f37658461064f986c0cd6369aa6119.json b/.sqlx/query-ee6275a42ff3150baf5cb1005548873c59f37658461064f986c0cd6369aa6119.jsonnew file mode 100644index 0000000000..5e3f43453d--- /dev/null+++ b/.sqlx/query-ee6275a42ff3150baf5cb1005548873c59f37658461064f986c0cd6369aa6119.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO entity_access (entity_id, entity_type, source_id, source_type, access_level) SELECT id::uuid, 'document', 'macro|[email protected]', 'user', 'view' FROM unnest($1::text[]) id ON CONFLICT DO NOTHING",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "TextArray"+ ]+ },+ "nullable": []+ },+ "hash": "ee6275a42ff3150baf5cb1005548873c59f37658461064f986c0cd6369aa6119"+}diff --git a/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic.rs b/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic.rsindex 9952762c08..3516b8ac6d 100644--- a/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic.rs+++ b/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic.rs@@ -1,6 +1,9 @@//! This module exposes a expanded dynamic query builder which is able to build specific soup queries//! which filter out content basd on some input ast+#[cfg(test)]+mod test;+use std::str::FromStr;use chrono::{DateTime, Utc};@@ -65,81 +68,83 @@ static DOCUMENT_TASK_PROPERTY_JOINS: &str = r#"AND ep_status.property_definition_id = $7"#;-static DOCUMENT_TOP_WHERE_CLAUSE: &str = r#"- LEFT JOIN "UserHistory" uh ON uh."itemId" = d.id AND uh."itemType" = 'document' AND uh."userId" = $1- WHERE d."deletedAt" IS NULL-"#;-// -- Grouped top clauses: include project_id for grouping support ---static GROUPED_DOCUMENT_TOP_CLAUSE: &str = r#"+fn grouped_document_top_clause(sort_method: SimpleSortMethod) -> String {+ let sort_ts = top_sort_expr("d", sort_method);+ format!(+ r#"SELECT'document'::text as item_type,d.id,- CASE $2- WHEN 'viewed_updated' THEN COALESCE(uh."updatedAt", d."updatedAt")- WHEN 'viewed_at' THEN COALESCE(uh."updatedAt", '1970-01-01 00:00:00+00')- WHEN 'created_at' THEN d."createdAt"- ELSE d."updatedAt"- END::timestamptz as sort_ts,+ {sort_ts}::timestamptz as sort_ts,d."projectId"::text as project_id,CASEWHEN dt.sub_type = 'task' THEN 'TASK'::property_entity_typeELSE 'DOCUMENT'::property_entity_typeEND as property_entity_type- FROM AccessibleItems ai- INNER JOIN "Document" d ON d.id = ai.item_id AND ai.item_type = 'document'+ FROM "Document" dLEFT JOIN document_sub_type dt ON dt.document_id = d.id-"#;+"#+ )+}-static GROUPED_CHAT_TOP_CLAUSE: &str = r#"+fn grouped_chat_top_clause(sort_method: SimpleSortMethod) -> String {+ let sort_ts = top_sort_expr("c", sort_method);+ let user_history_join = if top_needs_user_history(sort_method) {+ r#"LEFT JOIN "UserHistory" uh ON uh."itemId" = c.id AND uh."itemType" = 'chat' AND uh."userId" = $1"#+ } else {+ ""+ };+ format!(+ r#"SELECT'chat'::text as item_type,c.id,- CASE $2- WHEN 'viewed_updated' THEN COALESCE(uh."updatedAt", c."updatedAt")- WHEN 'viewed_at' THEN COALESCE(uh."updatedAt", '1970-01-01 00:00:00+00')- WHEN 'created_at' THEN c."createdAt"- ELSE c."updatedAt"- END::timestamptz as sort_ts,+ {sort_ts}::timestamptz as sort_ts,c."projectId"::text as project_id,'CHAT'::property_entity_type as property_entity_type- FROM AccessibleItems ai- INNER JOIN "Chat" c ON c.id = ai.item_id AND ai.item_type = 'chat'- LEFT JOIN "UserHistory" uh ON uh."itemId" = c.id AND uh."itemType" = 'chat' AND uh."userId" = $1- WHERE c."deletedAt" IS NULL-"#;+ FROM "Chat" c+ {user_history_join}+"#+ )+}-static GROUPED_PROJECT_TOP_CLAUSE: &str = r#"+fn grouped_project_top_clause(sort_method: SimpleSortMethod) -> String {+ let sort_ts = top_sort_expr("p", sort_method);+ let user_history_join = if top_needs_user_history(sort_method) {+ r#"LEFT JOIN "UserHistory" uh ON uh."itemId" = p.id AND uh."itemType" = 'project' AND uh."userId" = $1"#+ } else {+ ""+ };+ format!(+ r#"SELECT'project'::text as item_type,p.id,- CASE $2- WHEN 'viewed_updated' THEN COALESCE(uh."updatedAt", p."updatedAt")- WHEN 'viewed_at' THEN COALESCE(uh."updatedAt", '1970-01-01 00:00:00+00')- WHEN 'created_at' THEN p."createdAt"- ELSE p."updatedAt"- END::timestamptz as sort_ts,+ {sort_ts}::timestamptz as sort_ts,p."parentId"::text as project_id,'PROJECT'::property_entity_type as property_entity_type- FROM AccessibleItems ai- INNER JOIN "Project" p ON p.id = ai.item_id AND ai.item_type = 'project'- LEFT JOIN "UserHistory" uh- ON uh."itemId" = p.id- AND uh."itemType" = 'project'- AND uh."userId" = $1- WHERE p."deletedAt" IS NULL-"#;+ FROM "Project" p+ {user_history_join}+"#+ )+}-static GROUPED_CALENDAR_EVENT_TOP_CLAUSE: &str = r#"+fn grouped_calendar_event_top_clause(sort_method: SimpleSortMethod) -> String {+ let sort_ts = match sort_method {+ SimpleSortMethod::CreatedAt => "event.created_at",+ SimpleSortMethod::ViewedAt => "'1970-01-01 00:00:00+00'",+ SimpleSortMethod::UpdatedAt | SimpleSortMethod::ViewedUpdated => {+ "GREATEST(event.updated_at, event.last_reminder_fired_at)"+ }+ };+ format!(+ r#"SELECT'calendar_event'::text as item_type,event.id::text as id,- CASE $2- WHEN 'created_at' THEN event.created_at- WHEN 'viewed_at' THEN '1970-01-01 00:00:00+00'::timestamptz- ELSE GREATEST(event.updated_at, event.last_reminder_fired_at)- END::timestamptz as sort_ts,+ {sort_ts}::timestamptz as sort_ts,NULL::text as project_id,'CALENDAR_EVENT'::property_entity_type as property_entity_typeFROM calendar_events event@@ -152,7 +157,9 @@ static GROUPED_CALENDAR_EVENT_TOP_CLAUSE: &str = r#"AND link.primary_macro_id = $1))-"#;+"#+ )+}// -- Detail clauses: full columns, joined back from TopItems --@@ -1297,75 +1304,6 @@ fn project_top_where_clause() -> String {)}-fn push_accessible_items_cte(- builder: &mut QueryBuilder<'_, Postgres>,- include_documents: bool,- include_chats: bool,- include_projects: bool,-) {- let mut entity_types = Vec::with_capacity(3);- if include_documents {- entity_types.push("'document'");- }- if include_chats {- entity_types.push("'chat'");- }- if include_projects {- entity_types.push("'project'");- }-- if entity_types.is_empty() {- return;- }-- let entity_types = entity_types.join(", ");- builder.push(format!(- r#"AccessibleItems AS MATERIALIZED (- SELECT DISTINCT item_id, item_type- FROM (- SELECT- ea.entity_id::text as item_id,- ea.entity_type as item_type- FROM entity_access ea- WHERE ea.source_id = $1- AND ea.entity_type IN ({entity_types})-- UNION ALL-- SELECT- ea.entity_id::text as item_id,- ea.entity_type as item_type- FROM comms_channel_participants cp- CROSS JOIN LATERAL (- SELECT ea.entity_id, ea.entity_type- FROM entity_access ea- WHERE ea.source_id = cp.channel_id::text- AND ea.entity_type IN ({entity_types})- OFFSET 0- ) ea- WHERE cp.user_id = $1- AND cp.left_at IS NULL-- UNION ALL-- SELECT- ea.entity_id::text as item_id,- ea.entity_type as item_type- FROM team_user t- CROSS JOIN LATERAL (- SELECT ea.entity_id, ea.entity_type- FROM entity_access ea- WHERE ea.source_id = t.team_id::text- AND ea.entity_type IN ({entity_types})- OFFSET 0- ) ea- WHERE t.user_id = $1- ) accessible- ),-"#- ));-}-fn build_query(filter_ast: &EntityFilterAst,exclude_frecency: bool,@@ -1991,6 +1929,7 @@ fn build_grouped_query<'a>(filter_ast: &'a EntityFilterAst,exclude_frecency: bool,grouping: &'a GroupingConfig,+ sort_method: SimpleSortMethod,) -> (QueryBuilder<'a, Postgres>, Option<String>) {let mut builder = sqlx::QueryBuilder::new(PREFIX);@@ -2020,14 +1959,7 @@ fn build_grouped_query<'a>(&[PropertyEntityType::CalendarEvent],);- push_accessible_items_cte(- &mut builder,- include_documents,- include_chats,- include_projects,- );-- // TopItems CTE: lightweight id + sort_ts + project_id with filters, cursor, and limit+ // All matching candidates are needed for exact counts; limits apply after grouping.builder.push("TopItems AS (");builder.push("SELECT all_items.item_type, all_items.id, all_items.sort_ts, all_items.project_id, all_items.property_entity_type FROM (",@@ -2037,11 +1969,11 @@ fn build_grouped_query<'a>(if include_documents {push_union_separator(&mut builder, &mut needs_separator);- builder.push(GROUPED_DOCUMENT_TOP_CLAUSE);+ builder.push(grouped_document_top_clause(sort_method));if document_filter_needs_task_property_joins(filter_ast.document_filter.as_deref()) {builder.push(DOCUMENT_TASK_PROPERTY_JOINS);}- builder.push(DOCUMENT_TOP_WHERE_CLAUSE);+ builder.push(document_top_where_clause(sort_method));builder.push(build_document_filter(filter_ast.document_filter.as_deref()));builder.push(build_properties_filter(filter_ast.properties_filter.as_deref(),@@ -2051,7 +1983,8 @@ fn build_grouped_query<'a>(if include_chats {push_union_separator(&mut builder, &mut needs_separator);- builder.push(GROUPED_CHAT_TOP_CLAUSE);+ builder.push(grouped_chat_top_clause(sort_method));+ builder.push(chat_top_where_clause());builder.push(build_chat_filter(filter_ast.chat_filter.as_deref()));builder.push(build_properties_filter(filter_ast.properties_filter.as_deref(),@@ -2061,7 +1994,8 @@ fn build_grouped_query<'a>(if include_projects {push_union_separator(&mut builder, &mut needs_separator);- builder.push(GROUPED_PROJECT_TOP_CLAUSE);+ builder.push(grouped_project_top_clause(sort_method));+ builder.push(project_top_where_clause());builder.push(build_project_filter(filter_ast.project_filter.as_deref()));builder.push(build_properties_filter(filter_ast.properties_filter.as_deref(),@@ -2071,7 +2005,7 @@ fn build_grouped_query<'a>(if include_calendar_events {push_union_separator(&mut builder, &mut needs_separator);- builder.push(GROUPED_CALENDAR_EVENT_TOP_CLAUSE);+ builder.push(grouped_calendar_event_top_clause(sort_method));builder.push(build_calendar_event_filter(filter_ast.calendar_event_filter.as_deref(),));@@ -2114,8 +2048,7 @@ fn build_grouped_query<'a>(builder.push(")");}- // Note: we don't limit TopItems here for grouped queries - limit is applied at the end- builder.push(" ORDER BY all_items.sort_ts DESC, all_items.id DESC");+ // Ranking windows and the final SELECT provide their own ordering.builder.push("), ");// GroupedItems CTE: adds group metadata (and FilteredGroupedItems if not single-group mode)@@ -2210,9 +2143,14 @@ pub async fn expanded_dynamic_cursor_soup_grouped(let assignees_property_id = SystemPropertyKey::ASSIGNEES_UUID;let completed_option_id = StatusOption::COMPLETED_UUID.to_string();- let (mut query_builder, entity_type_bind) =- build_grouped_query(cursor.filter(), exclude_frecency, &grouping);+ let (mut query_builder, entity_type_bind) = build_grouped_query(+ cursor.filter(),+ exclude_frecency,+ &grouping,+ *cursor.sort_method(),+ );+ // Keep the reserved $2 sort slot even though candidates now specialize it away.// $9 is bound unconditionally (NULL when not in single-group mode) so $10 stays aligned.let mut query = query_builder.build_query_as::<'_, GroupedSoupRow>()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.rsnew file mode 100644index 0000000000..57ef9fa1b9--- /dev/null+++ b/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic/test.rs@@ -0,0 +1,196 @@+use super::*;+use macro_db_migrator::MACRO_DB_MIGRATIONS;+use std::sync::Arc;++#[test]+fn grouped_access_shape() {+ let filter = EntityFilterAst::mock_empty();+ let grouping = GroupingConfig {+ field: GroupByField::EntityType,+ group_key: None,+ per_group_limit: None,+ };+ let (builder, _) =+ build_grouped_query(&filter, false, &grouping, SimpleSortMethod::ViewedUpdated);+ let candidates = builder.sql().split("GroupedItems AS").next().unwrap();+ assert!(!candidates.contains("AccessibleItems"));+ for (id, entity_type) in [("d.id", "document"), ("c.id", "chat"), ("p.id", "project")] {+ assert!(candidates.contains(&access_semi_join(id, entity_type)));+ }+ assert!(candidates.contains("cp.left_at IS NULL"));+ assert!(candidates.contains("event.owner_id = $1"));+ assert!(candidates.contains("link.link_id = event.source_link_id"));+ assert!(candidates.contains("link.primary_macro_id = $1"));+ assert!(candidates.contains("THEN 'TASK'::property_entity_type"));+}++#[test]+fn grouped_candidate_sort_shape() {+ for sort in [+ SimpleSortMethod::CreatedAt,+ SimpleSortMethod::UpdatedAt,+ SimpleSortMethod::ViewedAt,+ SimpleSortMethod::ViewedUpdated,+ ] {+ for group_key in [None, Some("document".to_string())] {+ let filter = EntityFilterAst::mock_empty();+ let grouping = GroupingConfig {+ field: GroupByField::Property {+ property_definition_id: SystemPropertyKey::ASSIGNEES_UUID,+ entity_type: Some("TASK".to_string()),+ },+ group_key,+ per_group_limit: None,+ };+ let (builder, entity_type) = build_grouped_query(&filter, true, &grouping, sort);+ let sql = builder.sql();+ let (candidates, rest) = sql.split_once("GroupedItems AS").unwrap();+ assert_eq!(entity_type.as_deref(), Some("TASK"));+ assert!(!candidates.contains("CASE $2"));+ assert!(!candidates.contains("ORDER BY all_items.sort_ts"));+ assert_eq!(+ candidates.matches("LEFT JOIN \"UserHistory\"").count(),+ if top_needs_user_history(sort) { 3 } else { 0 }+ );+ for alias in ["d", "c", "p"] {+ assert!(candidates.contains(&format!(+ "{}::timestamptz as sort_ts",+ top_sort_expr(alias, sort)+ )));+ }+ assert!(candidates.contains("LEFT JOIN document_sub_type dt"));+ assert!(candidates.contains("fa.id IS NULL AND ("));+ assert!(candidates.contains("(all_items.sort_ts, all_items.id::text) < ($4, $5)"));+ assert_eq!(rest.matches("LEFT JOIN \"UserHistory\"").count(), 3);+ assert!(rest.contains("COUNT(*) OVER (PARTITION BY"));+ assert!(rest.contains("ORDER BY t.sort_ts DESC, t.id DESC"));+ assert!(rest.contains("AND ep.entity_type = $10"));+ assert!(rest.contains("\"group_key\", \"sort_ts\" DESC, \"id\" DESC"));+ assert_eq!(sql.ends_with("LIMIT $3"), grouping.group_key.is_some());+ assert_eq!(+ rest.contains("row_in_group <= 10"),+ grouping.group_key.is_none()+ );+ }+ }+}++/// Bounded, synthetic diagnostic. SQLx creates an isolated database; never point+/// the test harness at a hosted database. No planner settings or timing assertions.+#[sqlx::test(+ fixtures(+ path = "../../../../../../macro_db_client/fixtures",+ scripts("mixed_items_expanded")+ ),+ migrator = "MACRO_DB_MIGRATIONS"+)]+#[ignore = "local EXPLAIN diagnostic; run explicitly with --ignored --nocapture"]+async fn grouped_query_explain_local(pool: PgPool) -> anyhow::Result<()> {+ let ids = (0..12_000)+ .map(|_| Uuid::now_v7().to_string())+ .collect::<Vec<_>>();+ sqlx::query!(+ r#"INSERT INTO "Document" (id, name, owner, "createdAt", "updatedAt")+ SELECT id, 'explain task', 'macro|[email protected]', '2026-01-01', '2026-01-02'+ FROM unnest($1::text[]) id ON CONFLICT DO NOTHING"#,+ &ids,+ )+ .execute(&pool)+ .await?;+ sqlx::query!(+ "INSERT INTO document_sub_type (document_id, sub_type) SELECT id, 'task' FROM unnest($1::text[]) id ON CONFLICT DO NOTHING",+ &ids[..120],+ ).execute(&pool).await?;+ sqlx::query!(+ "INSERT INTO entity_access (entity_id, entity_type, source_id, source_type, access_level) SELECT id::uuid, 'document', 'macro|[email protected]', 'user', 'view' FROM unnest($1::text[]) id ON CONFLICT DO NOTHING",+ &ids,+ ).execute(&pool).await?;+ // Also exercise duplicate direct + inherited grants over the larger corpus.+ sqlx::query!(+ "INSERT INTO entity_access (entity_id, entity_type, source_id, source_type, access_level, granted_from_project_id) SELECT id::uuid, 'document', 'macro|[email protected]', 'user', 'view', 'aaaaaaaa-ffff-ffff-ffff-ffffffffffff' FROM unnest($1::text[]) id ON CONFLICT DO NOTHING",+ &ids,+ ).execute(&pool).await?;+ sqlx::query!("ANALYZE").execute(&pool).await?;+ let version = sqlx::query_scalar!("SELECT version()")+ .fetch_one(&pool)+ .await?;+ println!(+ "database={version:?}; synthetic_documents=12000; tasks=120; grants=24000; repetitions=3"+ );+ let tasks = EntityFilterAst {+ document_filter: Some(Arc::new(Expr::val(DocumentLiteral::SubType(+ DocumentSubType::Task,+ )))),+ chat_filter: Some(Arc::new(Expr::val(ChatLiteral::Importance(false)))),+ project_filter: Some(Arc::new(Expr::val(ProjectLiteral::Importance(false)))),+ calendar_event_filter: Some(Arc::new(Expr::val(CalendarEventLiteral::Id(Uuid::nil())))),+ ..EntityFilterAst::mock_empty()+ };+ for sort in [SimpleSortMethod::UpdatedAt, SimpleSortMethod::ViewedUpdated] {+ for (label, filter, field, group_key) in [+ (+ "selective_tasks",+ tasks.clone(),+ GroupByField::EntityType,+ None,+ ),+ (+ "broad_mixed",+ EntityFilterAst::mock_empty(),+ GroupByField::EntityType,+ None,+ ),+ (+ "property_initial",+ tasks.clone(),+ GroupByField::Property {+ property_definition_id: SystemPropertyKey::ASSIGNEES_UUID,+ entity_type: None,+ },+ None,+ ),+ (+ "continuation",+ tasks.clone(),+ GroupByField::EntityType,+ Some("document".to_string()),+ ),+ ] {+ let grouping = GroupingConfig {+ field,+ group_key,+ per_group_limit: None,+ };+ let timestamp = grouping+ .group_key+ .as_ref()+ .map(|_| "2026-01-02T00:00:00Z".parse::<DateTime<Utc>>().unwrap());+ let cursor_id = grouping.group_key.as_ref().map(|_| ids[60].clone());+ let (builder, entity_type) = build_grouped_query(&filter, false, &grouping, sort);+ // Dynamic AST/grouping SQL cannot use a compile-time macro. Mirror+ // production's positional binds, including unused reserved slots.+ let sql = format!(+ "EXPLAIN (ANALYZE, BUFFERS, VERBOSE, FORMAT JSON) {}",+ builder.sql()+ );+ for repetition in 1..=3 {+ let mut query = sqlx::query_scalar::<_, serde_json::Value>(&sql)+ .bind("macro|[email protected]")+ .bind(sort.to_string())+ .bind(50_i64)+ .bind(timestamp)+ .bind(cursor_id.clone())+ .bind(StatusOption::COMPLETED_UUID.to_string())+ .bind(SystemPropertyKey::STATUS_UUID)+ .bind(SystemPropertyKey::ASSIGNEES_UUID)+ .bind(grouping.group_key.clone());+ if let Some(ref entity_type) = entity_type {+ query = query.bind(entity_type);+ }+ let plan = query.persistent(false).fetch_one(&pool).await?;+ println!("EXPLAIN {label} {sort} repetition={repetition}: {plan}");+ }+ }+ }+ Ok(())+}diff --git a/crates/soup/src/outbound/pg_soup_repo/grouping/test.rs b/crates/soup/src/outbound/pg_soup_repo/grouping/test.rsindex 4308fdc337..f6a03281c3 100644--- a/crates/soup/src/outbound/pg_soup_repo/grouping/test.rs+++ b/crates/soup/src/outbound/pg_soup_repo/grouping/test.rs@@ -1,3 +1,7 @@+mod access;+mod pagination;+mod sorting;+use super::*;use crate::outbound::pg_soup_repo::expanded::dynamic::{GroupedDynamicCursorArgs, expanded_dynamic_cursor_soup_grouped,diff --git a/crates/soup/src/outbound/pg_soup_repo/grouping/test/access.rs b/crates/soup/src/outbound/pg_soup_repo/grouping/test/access.rsnew file mode 100644index 0000000000..a6735cc31f--- /dev/null+++ b/crates/soup/src/outbound/pg_soup_repo/grouping/test/access.rs@@ -0,0 +1,200 @@+use super::*;++async fn assert_membership(pool: &Pool<Postgres>) -> anyhow::Result<()> {+ let items = expanded_dynamic_cursor_soup_grouped(+ pool,+ GroupedDynamicCursorArgs {+ user_id: MacroUserIdStr::parse_from_str("macro|[email protected]")?,+ limit: 2,+ cursor: Query::Sort(SimpleSortMethod::UpdatedAt, EntityFilterAst::mock_empty()),+ exclude_frecency: false,+ grouping: GroupingConfig {+ field: GroupByField::EntityType,+ group_key: None,+ per_group_limit: None,+ },+ },+ )+ .await?+ .collect::<Vec<_>>();+ for (key, expected) in [+ (+ "document",+ vec![+ "11111111-0000-0000-0000-000000000000",+ "11111111-dddd-dddd-dddd-dddddddddddd",+ "11111111-cccc-cccc-cccc-cccccccccccc",+ "11111111-bbbb-bbbb-bbbb-bbbbbbbbbbbb",+ "11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa",+ ],+ ),+ (+ "chat",+ vec![+ "22222222-0000-0000-0000-000000000000",+ "22222222-cccc-cccc-cccc-cccccccccccc",+ "22222222-bbbb-bbbb-bbbb-bbbbbbbbbbbb",+ "22222222-aaaa-aaaa-aaaa-aaaaaaaaaaaa",+ ],+ ),+ (+ "project",+ vec![+ "dddddddd-ffff-ffff-ffff-ffffffffffff",+ "cccccccc-ffff-ffff-ffff-ffffffffffff",+ "bbbbbbbb-ffff-ffff-ffff-ffffffffffff",+ "aaaaaaaa-ffff-ffff-ffff-ffffffffffff",+ ],+ ),+ ] {+ let group = items+ .iter()+ .filter(|item| item.key == key)+ .collect::<Vec<_>>();+ assert_eq!(+ group+ .iter()+ .map(|i| i.item.id().to_string())+ .collect::<Vec<_>>(),+ expected+ );+ for (index, item) in group.iter().enumerate() {+ assert_eq!(item.total_group_count, expected.len());+ assert_eq!(item.index_in_group, index + 1);+ }+ }+ assert_eq!(items.len(), 13);+ Ok(())+}++#[sqlx::test(+ fixtures(+ path = "../../../../../../macro_db_client/fixtures",+ scripts("mixed_items_expanded")+ ),+ migrator = "MACRO_DB_MIGRATIONS"+)]+async fn inherited_and_direct_grants_without_owner_fallback(+ pool: Pool<Postgres>,+) -> anyhow::Result<()> {+ assert_membership(&pool).await+}++#[sqlx::test(+ fixtures(+ path = "../../../../../../macro_db_client/fixtures",+ scripts("mixed_items_expanded")+ ),+ fixtures(path = ".", scripts("access")),+ migrator = "MACRO_DB_MIGRATIONS"+)]+async fn source_membership_deduplicates_grants(pool: Pool<Postgres>) -> anyhow::Result<()> {+ assert_membership(&pool).await?;+ sqlx::query!(r#"UPDATE "Document" SET "deletedAt" = now() WHERE id = '11111111-0000-0000-0000-000000000000'"#).execute(&pool).await?;+ let items = expanded_dynamic_cursor_soup_grouped(+ &pool,+ GroupedDynamicCursorArgs {+ user_id: MacroUserIdStr::parse_from_str("macro|[email protected]")?,+ limit: 50,+ cursor: Query::Sort(+ SimpleSortMethod::UpdatedAt,+ super::pagination::task_filter(),+ ),+ exclude_frecency: false,+ grouping: GroupingConfig {+ field: GroupByField::EntityType,+ group_key: None,+ per_group_limit: None,+ },+ },+ )+ .await?+ .collect::<Vec<_>>();+ assert!(+ items.is_empty(),+ "deleted task must be excluded despite multiple grants"+ );+ Ok(())+}++pub(super) fn calendar_filter() -> EntityFilterAst {+ EntityFilterAst {+ document_filter: Some(Arc::new(Expr::val(DocumentLiteral::Id(uuid::Uuid::nil())))),+ chat_filter: Some(Arc::new(Expr::val(ChatLiteral::Importance(false)))),+ project_filter: Some(Arc::new(Expr::val(+ item_filters::ast::project::ProjectLiteral::Importance(false),+ ))),+ ..EntityFilterAst::mock_empty()+ }+}++pub(super) async fn seed_calendar_access(pool: &Pool<Postgres>) -> anyhow::Result<Vec<uuid::Uuid>> {+ let mut ids = Vec::new();+ for (index, owner) in [+ "macro|[email protected]",+ "macro|[email protected]",+ "macro|[email protected]",+ ]+ .into_iter()+ .enumerate()+ {+ let link = uuid::Uuid::now_v7();+ let id = uuid::Uuid::now_v7();+ sqlx::query!(+ "INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider) VALUES ($1, $2, $2, $3, 'GMAIL') ON CONFLICT DO NOTHING",+ link, owner, format!("calendar-{index}@test.com"),+ ).execute(pool).await?;+ sqlx::query!(+ "INSERT INTO calendar_events (id, owner_id, source_link_id, ical_uid, title, starts_at, ends_at, canonical_source_kind) VALUES ($1, $2, $3, $4, 'calendar access', '2026-01-01', '2026-01-02', 'google') ON CONFLICT DO NOTHING",+ id, owner, link, id.to_string(),+ ).execute(pool).await?;+ if index == 1 {+ sqlx::query!(+ "INSERT INTO macro_user_links (primary_macro_id, child_macro_id, link_id) VALUES ('macro|[email protected]', 'macro|[email protected]', $1) ON CONFLICT DO NOTHING",+ link,+ ).execute(pool).await?;+ }+ ids.push(id);+ }+ Ok(ids)+}++#[sqlx::test(+ fixtures(+ path = "../../../../../../macro_db_client/fixtures",+ scripts("mixed_items_expanded")+ ),+ fixtures(path = ".", scripts("access")),+ migrator = "MACRO_DB_MIGRATIONS"+)]+async fn calendar_owner_or_linked_account_only(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let ids = seed_calendar_access(&pool).await?;+ for filter in [EntityFilterAst::mock_empty(), calendar_filter()] {+ let items = expanded_dynamic_cursor_soup_grouped(+ &pool,+ GroupedDynamicCursorArgs {+ user_id: MacroUserIdStr::parse_from_str("macro|[email protected]")?,+ limit: 50,+ cursor: Query::Sort(SimpleSortMethod::ViewedAt, filter),+ exclude_frecency: false,+ grouping: GroupingConfig {+ field: GroupByField::EntityType,+ group_key: None,+ per_group_limit: None,+ },+ },+ )+ .await?+ .filter(|i| i.key == "calendar_event")+ .collect::<Vec<_>>();+ assert_eq!(+ items.iter().map(|i| i.item.id()).collect::<Vec<_>>(),+ vec![ids[1], ids[0]]+ );+ for (index, item) in items.iter().enumerate() {+ assert_eq!(item.total_group_count, 2);+ assert_eq!(item.index_in_group, index + 1);+ }+ }+ Ok(())+}diff --git a/crates/soup/src/outbound/pg_soup_repo/grouping/test/access.sql b/crates/soup/src/outbound/pg_soup_repo/grouping/test/access.sqlnew file mode 100644index 0000000000..db4b482a92--- /dev/null+++ b/crates/soup/src/outbound/pg_soup_repo/grouping/test/access.sql@@ -0,0 +1,52 @@+-- Test-local grants only; mixed_items_expanded supplies entities and the caller.+INSERT INTO comms_channels (id, channel_type, name, owner_id) VALUES+('ac000000-0000-0000-0000-000000000001', 'private', 'active', 'macro|[email protected]'),+('ac000000-0000-0000-0000-000000000002', 'private', 'left', 'macro|[email protected]');+INSERT INTO comms_channel_participants (channel_id, role, user_id, left_at) VALUES+('ac000000-0000-0000-0000-000000000001', 'member', 'macro|[email protected]', NULL),+('ac000000-0000-0000-0000-000000000002', 'member', 'macro|[email protected]', now());+INSERT INTO "User" (id, email, macro_user_id) VALUES ('macro|[email protected]', '[email protected]', 'a1111111-1111-1111-1111-111111111111') ON CONFLICT DO NOTHING;+INSERT INTO team (id, name, owner_id) VALUES+('ac000000-0000-0000-0000-000000000003', 'member', 'macro|[email protected]'),+('ac000000-0000-0000-0000-000000000004', 'nonmember', 'macro|[email protected]');+INSERT INTO team_user (team_id, user_id, team_role) VALUES+('ac000000-0000-0000-0000-000000000003', 'macro|[email protected]', 'member');++-- B is channel-only, C is team-only, for each entity kind. A retains inherited+-- user grants; B retains direct plus inherited grants through its channel.+UPDATE entity_access SET source_type = 'channel', source_id = 'ac000000-0000-0000-0000-000000000001'+WHERE entity_id::text LIKE '%bbbb%';+UPDATE entity_access SET source_type = 'team', source_id = 'ac000000-0000-0000-0000-000000000003'+WHERE entity_id::text LIKE '%cccc%';++-- Simultaneous direct, channel and team grants must not consume page slots.+INSERT INTO entity_access (entity_id, entity_type, source_id, source_type, access_level)+SELECT entity_id, entity_type, source_id, source_type::entity_access_source_type, 'view'+FROM (VALUES+('ac000000-0000-0000-0000-000000000001', 'channel'),+('ac000000-0000-0000-0000-000000000003', 'team')+) sources(source_id, source_type)+CROSS JOIN (VALUES+('11111111-0000-0000-0000-000000000000'::uuid, 'document'),+('22222222-0000-0000-0000-000000000000'::uuid, 'chat'),+('aaaaaaaa-ffff-ffff-ffff-ffffffffffff'::uuid, 'project')+) entities(entity_id, entity_type)+ON CONFLICT DO NOTHING;++-- The isolated entities are owned by the caller but have only unusable grants.+INSERT INTO entity_access (entity_id, entity_type, source_id, source_type, access_level)+SELECT entity_id, entity_type, source_id, source_type::entity_access_source_type, 'view'+FROM (VALUES+('ac000000-0000-0000-0000-000000000002', 'channel'),+('ac000000-0000-0000-0000-000000000004', 'team'),+('macro|[email protected]', 'user')+) sources(source_id, source_type)+CROSS JOIN (VALUES+('11111111-9999-9999-9999-999999999999'::uuid, 'document'),+('22222222-9999-9999-9999-999999999999'::uuid, 'chat'),+('99999999-ffff-ffff-ffff-ffffffffffff'::uuid, 'project')+) entities(entity_id, entity_type)+ON CONFLICT DO NOTHING;++INSERT INTO document_sub_type (document_id, sub_type)+VALUES ('11111111-0000-0000-0000-000000000000', 'task');diff --git a/crates/soup/src/outbound/pg_soup_repo/grouping/test/pagination.rs b/crates/soup/src/outbound/pg_soup_repo/grouping/test/pagination.rsnew file mode 100644index 0000000000..02f65b4443--- /dev/null+++ b/crates/soup/src/outbound/pg_soup_repo/grouping/test/pagination.rs@@ -0,0 +1,211 @@+use super::*;+use crate::domain::models::grouping::ItemGroupingInfo;+use document_sub_type::DocumentSubType;+use item_filters::ast::project::ProjectLiteral;+use models_pagination::{CursorWithValAndFilter, SortOn};+use models_soup::item::SoupItem;+use uuid::Uuid;++pub(super) fn task_filter() -> EntityFilterAst {+ EntityFilterAst {+ document_filter: Some(Arc::new(Expr::val(DocumentLiteral::SubType(+ DocumentSubType::Task,+ )))),+ chat_filter: Some(Arc::new(Expr::val(ChatLiteral::Importance(false)))),+ project_filter: Some(Arc::new(Expr::val(ProjectLiteral::Importance(false)))),+ calendar_event_filter: Some(Arc::new(Expr::val(CalendarEventLiteral::Id(Uuid::nil())))),+ ..EntityFilterAst::mock_empty()+ }+}++pub(super) fn continuation(+ item: &ItemGroupingInfo,+ sort: SimpleSortMethod,+) -> Query<Uuid, SimpleSortMethod, EntityFilterAst> {+ Query::Cursor(CursorWithValAndFilter {+ id: item.item.id(),+ limit: 10,+ val: SoupItem::sort_on(sort)(&item.item),+ filter: task_filter(),+ })+}++#[sqlx::test(+ fixtures(+ path = "../../../../../../macro_db_client/fixtures",+ scripts("mixed_items_expanded")+ ),+ migrator = "MACRO_DB_MIGRATIONS"+)]+async fn fanout_caps_counts_and_tied_cursor_pages(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let mut ids = (0..18)+ .map(|_| Uuid::now_v7().to_string())+ .collect::<Vec<_>>();+ ids.sort();+ sqlx::query!(+ r#"INSERT INTO "Document" (id, name, owner, "createdAt", "updatedAt")+ SELECT id, 'pagination task', 'macro|[email protected]', '2026-01-01', '2026-01-02'+ FROM unnest($1::text[]) id ON CONFLICT DO NOTHING"#,+ &ids,+ )+ .execute(&pool)+ .await?;+ sqlx::query!(+ r#"INSERT INTO "DocumentInstance" (id, "documentId", sha)+ SELECT 100 + ordinality, id, 'test' FROM unnest($1::text[]) WITH ORDINALITY AS ids(id, ordinality)+ ON CONFLICT DO NOTHING"#,+ &ids,+ ).execute(&pool).await?;+ sqlx::query!(+ "INSERT INTO document_sub_type (document_id, sub_type) SELECT id, 'task' FROM unnest($1::text[]) id ON CONFLICT DO NOTHING",+ &ids,+ ).execute(&pool).await?;+ // Test-local denormalized permissions, not production grant orchestration.+ sqlx::query!(+ "INSERT INTO entity_access (entity_id, entity_type, source_id, source_type, access_level) SELECT id::uuid, 'document', 'macro|[email protected]', 'user', 'view' FROM unnest($1::text[]) id ON CONFLICT DO NOTHING",+ &ids,+ ).execute(&pool).await?;+ for (index, id) in ids.iter().enumerate() {+ if index == 17 {+ continue;+ } // Missing property, empty array and scalar all map to unset.+ let values = match index {+ 15 => serde_json::json!({"type": "EntityReference", "value": []}),+ 16 => serde_json::json!({"type": "String", "value": "scalar"}),+ _ => serde_json::json!({"type": "EntityReference", "value": [+ {"entity_id": "a"}, {"entity_id": "b"}, {"entity_id": "c"}+ ]}),+ };+ sqlx::query!(+ "INSERT INTO entity_properties (id, entity_id, entity_type, property_definition_id, values) VALUES ($1, $2, 'TASK', $3, $4) ON CONFLICT DO NOTHING",+ Uuid::now_v7(), id, system_properties::SystemPropertyKey::ASSIGNEES_UUID, values,+ ).execute(&pool).await?;+ }+ for entity_type in [None, Some("TASK".to_string())] {+ let grouping = GroupingConfig {+ field: GroupByField::Property {+ property_definition_id: system_properties::SystemPropertyKey::ASSIGNEES_UUID,+ entity_type,+ },+ group_key: None,+ per_group_limit: None,+ };+ let fetch = |cursor, grouping| {+ expanded_dynamic_cursor_soup_grouped(+ &pool,+ GroupedDynamicCursorArgs {+ user_id: MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap(),+ limit: 2,+ cursor,+ exclude_frecency: false,+ grouping,+ },+ )+ };+ let result = fetch(+ Query::Sort(SimpleSortMethod::UpdatedAt, task_filter()),+ grouping.clone(),+ )+ .await;+ if matches!(+ grouping.field,+ GroupByField::Property {+ entity_type: Some(_),+ ..+ }+ ) {+ // Existing API binds $10 as text, but compares it to a PostgreSQL+ // enum without a cast. Preserve this baseline error in this perf-only change.+ let error = result.unwrap_err();+ assert_eq!(+ error.as_database_error().and_then(|e| e.code()).as_deref(),+ Some("42883")+ );+ continue;+ }+ let initial = result?.collect::<Vec<_>>();+ assert_eq!(initial.len(), 33); // Three full bins plus three unset items, not a global limit of 2.+ for key in ["", "a", "b", "c"] {+ let group = initial.iter().filter(|i| i.key == key).collect::<Vec<_>>();+ let expected = if key.is_empty() {+ &ids[15..]+ } else {+ &ids[..15]+ };+ let expected = expected.iter().rev().cloned().collect::<Vec<_>>();+ assert_eq!(+ group+ .iter()+ .map(|i| i.item.id().to_string())+ .collect::<Vec<_>>(),+ expected[..expected.len().min(10)]+ );+ for (index, item) in group.iter().enumerate() {+ assert_eq!(item.total_group_count, expected.len());+ assert_eq!(item.index_in_group, index + 1);+ }+ let mut seen = group+ .iter()+ .map(|i| i.item.id().to_string())+ .collect::<Vec<_>>();+ let mut cursor = continuation(group.last().unwrap(), SimpleSortMethod::UpdatedAt);+ loop {+ let page = fetch(+ cursor,+ GroupingConfig {+ group_key: Some(key.to_string()),+ ..grouping.clone()+ },+ )+ .await?+ .collect::<Vec<_>>();+ if page.is_empty() {+ break;+ }+ for (index, item) in page.iter().enumerate() {+ assert_eq!(item.total_group_count, expected.len() - seen.len());+ assert_eq!(item.index_in_group, index + 1);+ }+ cursor = continuation(page.last().unwrap(), SimpleSortMethod::UpdatedAt);+ seen.extend(page.iter().map(|i| i.item.id().to_string()));+ assert!(seen.len() <= expected.len());+ }+ assert_eq!(seen, expected);+ }+ }+ sqlx::query!(+ "INSERT INTO frecency_aggregates (entity_id, entity_type, user_id, first_event) VALUES ($1, 'document', 'macro|[email protected]', now()) ON CONFLICT DO NOTHING",+ &ids[0],+ ).execute(&pool).await?;+ let excluded = expanded_dynamic_cursor_soup_grouped(+ &pool,+ GroupedDynamicCursorArgs {+ user_id: MacroUserIdStr::parse_from_str("macro|[email protected]")?,+ limit: 50,+ cursor: Query::Sort(SimpleSortMethod::UpdatedAt, task_filter()),+ exclude_frecency: true,+ grouping: GroupingConfig {+ field: GroupByField::Property {+ property_definition_id: system_properties::SystemPropertyKey::ASSIGNEES_UUID,+ entity_type: None,+ },+ group_key: Some("a".to_string()),+ per_group_limit: None,+ },+ },+ )+ .await?+ .collect::<Vec<_>>();+ assert_eq!(+ excluded+ .iter()+ .map(|i| i.item.id().to_string())+ .collect::<Vec<_>>(),+ ids[1..15].iter().rev().cloned().collect::<Vec<_>>()+ );+ for (index, item) in excluded.iter().enumerate() {+ assert_eq!(item.total_group_count, 14);+ assert_eq!(item.index_in_group, index + 1);+ }+ Ok(())+}diff --git a/crates/soup/src/outbound/pg_soup_repo/grouping/test/sorting.rs b/crates/soup/src/outbound/pg_soup_repo/grouping/test/sorting.rsnew file mode 100644index 0000000000..3d45ef5571--- /dev/null+++ b/crates/soup/src/outbound/pg_soup_repo/grouping/test/sorting.rs@@ -0,0 +1,301 @@+use super::*;+use crate::domain::models::grouping::ItemGroupingInfo;+use chrono::{DateTime, Duration, Utc};+use models_pagination::{CursorWithValAndFilter, SortOn};+use models_soup::item::SoupItem;+use uuid::Uuid;++const SORTS: [SimpleSortMethod; 4] = [+ SimpleSortMethod::CreatedAt,+ SimpleSortMethod::UpdatedAt,+ SimpleSortMethod::ViewedAt,+ SimpleSortMethod::ViewedUpdated,+];++async fn fetch(+ pool: &Pool<Postgres>,+ cursor: Query<Uuid, SimpleSortMethod, EntityFilterAst>,+ field: GroupByField,+ group_key: Option<String>,+ limit: u16,+) -> anyhow::Result<Vec<ItemGroupingInfo>> {+ Ok(expanded_dynamic_cursor_soup_grouped(+ pool,+ GroupedDynamicCursorArgs {+ user_id: MacroUserIdStr::parse_from_str("macro|[email protected]")?,+ limit,+ cursor,+ exclude_frecency: false,+ grouping: GroupingConfig {+ field,+ group_key,+ per_group_limit: None,+ },+ },+ )+ .await?+ .collect())+}++fn cursor_from(+ item: &ItemGroupingInfo,+ sort: SimpleSortMethod,+ filter: EntityFilterAst,+) -> Query<Uuid, SimpleSortMethod, EntityFilterAst> {+ Query::Cursor(CursorWithValAndFilter {+ id: item.item.id(),+ limit: 2,+ val: SoupItem::sort_on(sort)(&item.item),+ filter,+ })+}++#[sqlx::test(+ fixtures(+ path = "../../../../../../macro_db_client/fixtures",+ scripts("mixed_items_expanded")+ ),+ fixtures(path = ".", scripts("access", "sorting")),+ migrator = "MACRO_DB_MIGRATIONS"+)]+async fn all_sorts_preserve_history_dates_and_real_cursors(+ pool: Pool<Postgres>,+) -> anyhow::Result<()> {+ let today = sqlx::query_scalar!(+ r#"SELECT current_date::timestamptz + interval '12 hours' AS "today!""#+ )+ .fetch_one(&pool)+ .await?;+ let created = today - Duration::days(400);+ let updated = today - Duration::days(1);+ let old_history = today - Duration::days(500);+ let epoch = DateTime::<Utc>::default();+ let expected_ids = [+ ("document", "11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa"),+ ("document", "11111111-bbbb-bbbb-bbbb-bbbbbbbbbbbb"),+ ("document", "11111111-cccc-cccc-cccc-cccccccccccc"),+ ("document", "11111111-dddd-dddd-dddd-dddddddddddd"),+ ("document", "11111111-0000-0000-0000-000000000000"),+ ("chat", "22222222-aaaa-aaaa-aaaa-aaaaaaaaaaaa"),+ ("chat", "22222222-bbbb-bbbb-bbbb-bbbbbbbbbbbb"),+ ("chat", "22222222-cccc-cccc-cccc-cccccccccccc"),+ ("chat", "22222222-0000-0000-0000-000000000000"),+ ("project", "aaaaaaaa-ffff-ffff-ffff-ffffffffffff"),+ ("project", "bbbbbbbb-ffff-ffff-ffff-ffffffffffff"),+ ("project", "cccccccc-ffff-ffff-ffff-ffffffffffff"),+ ("project", "dddddddd-ffff-ffff-ffff-ffffffffffff"),+ ];+ let history = |id: &str| {+ if id.contains("aaaa") {+ Some(today)+ } else if id.contains("bbbb") {+ Some(old_history)+ } else {+ None+ }+ };+ for sort in SORTS {+ let timestamp = |id: &str| match sort {+ SimpleSortMethod::CreatedAt => created,+ SimpleSortMethod::UpdatedAt => updated,+ SimpleSortMethod::ViewedAt => history(id).unwrap_or(epoch),+ SimpleSortMethod::ViewedUpdated => history(id).unwrap_or(updated),+ };+ let initial = fetch(+ &pool,+ Query::Sort(sort, EntityFilterAst::mock_empty()),+ GroupByField::EntityType,+ None,+ 2,+ )+ .await?;+ assert_eq!(initial.len(), 13);+ for item in &initial {+ let (actual_created, actual_updated, actual_viewed) = match &item.item {+ SoupItem::Document(d) => (d.created_at, d.updated_at, d.viewed_at),+ SoupItem::Chat(c) => (c.created_at, c.updated_at, c.viewed_at),+ SoupItem::Project(p) => (p.created_at, p.updated_at, p.viewed_at),+ other => panic!("unexpected item: {other:?}"),+ };+ let id = item.item.id().to_string();+ assert_eq!(+ (actual_created, actual_updated, actual_viewed),+ (created, updated, history(&id))+ );+ assert_eq!(SoupItem::sort_on(sort)(&item.item).last_val, timestamp(&id));+ }+ for key in ["document", "chat", "project"] {+ let mut expected = expected_ids+ .iter()+ .filter(|(kind, _)| *kind == key)+ .map(|(_, id)| *id)+ .collect::<Vec<_>>();+ expected.sort_by_key(|id| std::cmp::Reverse((timestamp(id), *id)));+ let group = initial.iter().filter(|i| i.key == key).collect::<Vec<_>>();+ assert_eq!(+ group+ .iter()+ .map(|i| i.item.id().to_string())+ .collect::<Vec<_>>(),+ expected+ );+ let mut cursor = Query::Sort(sort, EntityFilterAst::mock_empty());+ let mut seen = Vec::new();+ loop {+ let page = fetch(+ &pool,+ cursor,+ GroupByField::EntityType,+ Some(key.to_string()),+ 2,+ )+ .await?;+ if page.is_empty() {+ break;+ }+ for (index, item) in page.iter().enumerate() {+ assert_eq!(item.total_group_count, expected.len() - seen.len());+ assert_eq!(item.index_in_group, index + 1);+ }+ cursor = cursor_from(page.last().unwrap(), sort, EntityFilterAst::mock_empty());+ seen.extend(page.iter().map(|i| i.item.id().to_string()));+ assert!(seen.len() <= expected.len());+ }+ assert_eq!(seen, expected);+ }+ let dated = fetch(+ &pool,+ Query::Sort(sort, EntityFilterAst::mock_empty()),+ GroupByField::Date,+ None,+ 50,+ )+ .await?;+ // A single date bucket can exceed the initial ten-item cap. Use its+ // single-group form to check all IDs and its exact count.+ for key in ["today", "yesterday", "older"] {+ let mut expected = expected_ids+ .iter()+ .filter(|(_, id)| {+ let ts = timestamp(id);+ let bucket = if ts == today {+ "today"+ } else if ts == updated {+ "yesterday"+ } else {+ "older"+ };+ bucket == key+ })+ .map(|(_, id)| *id)+ .collect::<Vec<_>>();+ expected.sort_by_key(|id| std::cmp::Reverse((timestamp(id), *id)));+ let group = dated.iter().filter(|i| i.key == key).collect::<Vec<_>>();+ assert_eq!(group.len(), expected.len().min(10));+ assert!(group.iter().all(|i| i.total_group_count == expected.len()));+ let full = fetch(+ &pool,+ Query::Sort(sort, EntityFilterAst::mock_empty()),+ GroupByField::Date,+ Some(key.to_string()),+ 50,+ )+ .await?;+ assert_eq!(+ full.iter()+ .map(|i| i.item.id().to_string())+ .collect::<Vec<_>>(),+ expected+ );+ }+ }+ Ok(())+}++#[sqlx::test(+ fixtures(+ path = "../../../../../../macro_db_client/fixtures",+ scripts("mixed_items_expanded")+ ),+ fixtures(path = ".", scripts("access")),+ migrator = "MACRO_DB_MIGRATIONS"+)]+async fn calendar_sorts_preserve_reminders_and_linked_account_cursors(+ pool: Pool<Postgres>,+) -> anyhow::Result<()> {+ let ids = super::access::seed_calendar_access(&pool).await?;+ let created = "2026-01-01T00:00:00Z".parse::<DateTime<Utc>>()?;+ let updated = created + Duration::days(1);+ let reminder = created + Duration::days(2);+ sqlx::query!(+ "UPDATE calendar_events SET created_at = $1, updated_at = $2, last_reminder_fired_at = CASE WHEN id = $3 THEN $4::timestamptz ELSE NULL END WHERE id = ANY($5)",+ created, updated, ids[0], reminder, &ids,+ ).execute(&pool).await?;+ for sort in SORTS {+ let expected = match sort {+ SimpleSortMethod::CreatedAt => vec![(ids[1], created), (ids[0], created)],+ SimpleSortMethod::ViewedAt => vec![+ (ids[1], DateTime::<Utc>::default()),+ (ids[0], DateTime::<Utc>::default()),+ ],+ _ => vec![(ids[0], reminder), (ids[1], updated)],+ };+ let mut cursor = Query::Sort(sort, super::access::calendar_filter());+ for (index, (id, ts)) in expected.into_iter().enumerate() {+ let page = fetch(+ &pool,+ cursor,+ GroupByField::EntityType,+ Some("calendar_event".to_string()),+ 1,+ )+ .await?;+ assert_eq!(page.len(), 1);+ let item = &page[0];+ assert_eq!(item.item.id(), id);+ assert_eq!(item.total_group_count, 2 - index);+ assert_eq!(item.index_in_group, 1);+ assert_eq!(SoupItem::sort_on(sort)(&item.item).last_val, ts);+ let SoupItem::CalendarEvent(event) = &item.item else {+ panic!("expected event")+ };+ assert_eq!(event.created_at, created);+ assert_eq!(event.updated_at, updated);+ assert_eq!(+ event.last_reminder_fired_at,+ (id == ids[0]).then_some(reminder)+ );+ cursor = cursor_from(item, sort, super::access::calendar_filter());+ }+ assert!(+ fetch(+ &pool,+ cursor,+ GroupByField::EntityType,+ Some("calendar_event".to_string()),+ 1+ )+ .await?+ .is_empty()+ );+ }+ // Impossible union arms stay typed and executable for every specialized sort.+ for sort in SORTS {+ let filter = EntityFilterAst {+ calendar_event_filter: Some(Arc::new(Expr::val(CalendarEventLiteral::Id(Uuid::nil())))),+ ..super::access::calendar_filter()+ };+ assert!(+ fetch(+ &pool,+ Query::Sort(sort, filter),+ GroupByField::EntityType,+ None,+ 50+ )+ .await?+ .is_empty()+ );+ }+ Ok(())+}diff --git a/crates/soup/src/outbound/pg_soup_repo/grouping/test/sorting.sql b/crates/soup/src/outbound/pg_soup_repo/grouping/test/sorting.sqlnew file mode 100644index 0000000000..06ef6e51c3--- /dev/null+++ b/crates/soup/src/outbound/pg_soup_repo/grouping/test/sorting.sql@@ -0,0 +1,20 @@+-- Deliberately separated date buckets and tied entity timestamps.+UPDATE "Document" SET "createdAt" = current_date - interval '400 days' + interval '12 hours',+ "updatedAt" = current_date - interval '1 day' + interval '12 hours';+UPDATE "Chat" SET "createdAt" = current_date - interval '400 days' + interval '12 hours',+ "updatedAt" = current_date - interval '1 day' + interval '12 hours';+UPDATE "Project" SET "createdAt" = current_date - interval '400 days' + interval '12 hours',+ "updatedAt" = current_date - interval '1 day' + interval '12 hours';+DELETE FROM "UserHistory" WHERE "userId" = 'macro|[email protected]';+INSERT INTO "UserHistory" ("userId", "itemId", "itemType", "updatedAt")+SELECT 'macro|[email protected]', id, kind,+ current_date + interval '12 hours' - age+FROM (VALUES+('11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'document', interval '0 days'),+('11111111-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'document', interval '500 days'),+('22222222-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'chat', interval '0 days'),+('22222222-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'chat', interval '500 days'),+('aaaaaaaa-ffff-ffff-ffff-ffffffffffff', 'project', interval '0 days'),+('bbbbbbbb-ffff-ffff-ffff-ffffffffffff', 'project', interval '500 days')+) history(id, kind, age)+ON CONFLICT DO NOTHING;