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
feat(soup): filter + sort by touched by me (#5655)
teoeeb3d3595379
diff --git a/Cargo.lock b/Cargo.lockindex fc6b3d8a06..88c33eaf4d 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -14430,6 +14430,7 @@ checksum = "45e14297decde697ddf377c25752aead0927d5cfc89c2684d2af96901a4ceeea"name = "soup"version = "0.1.0"dependencies = [+ "activity","ai_toolset","anyhow","async-trait",diff --git a/apps/web/src/features/next-soup/soup-view/create-grouped-soup-queries.ts b/apps/web/src/features/next-soup/soup-view/create-grouped-soup-queries.tsindex 3ee9d37f1e..df14fec98c 100644--- a/apps/web/src/features/next-soup/soup-view/create-grouped-soup-queries.ts+++ b/apps/web/src/features/next-soup/soup-view/create-grouped-soup-queries.ts@@ -54,7 +54,10 @@ type CreateGroupedSoupQueriesArgs = {groupByField: Accessor<GroupByField | undefined>;soupParams: Accessor<Omit<SoupParams, 'sort_method'> & {- sort_method: Exclude<SoupParams['sort_method'], 'frecency'>;+ sort_method: Exclude<+ SoupParams['sort_method'],+ 'frecency' | 'touched_by_me'+ >;}>;soupBody: Accessor<SoupAstBody>;diff --git a/apps/web/src/features/next-soup/soup-view/soup-view-context.tsx b/apps/web/src/features/next-soup/soup-view/soup-view-context.tsxindex 19ed87b3f8..44f18e8a87 100644--- a/apps/web/src/features/next-soup/soup-view/soup-view-context.tsx+++ b/apps/web/src/features/next-soup/soup-view/soup-view-context.tsx@@ -262,7 +262,7 @@ const persistedPredicatesFor = (type ApiSortMethod = Exclude<NonNullable<SoupParams['sort_method']>,- 'frecency'+ 'frecency' | 'touched_by_me'>;const VALID_API_SORT_METHODS: ApiSortMethod[] = ['viewed_at',diff --git a/apps/web/src/lib/queries/soup/items.ts b/apps/web/src/lib/queries/soup/items.tsindex 67dc676cf4..a6753a9c15 100644--- a/apps/web/src/lib/queries/soup/items.ts+++ b/apps/web/src/lib/queries/soup/items.ts@@ -166,8 +166,9 @@ const useRestSoupAstItemsQuery = (let sort_method = params.sort_method ?? undefined;// TODO(dev-rb/soup): This is temporary fix since we don't support- // 'frecency' for group by. Replace with proper types- if (sort_method === 'frecency') {+ // 'frecency' or 'touched_by_me' for group by. Replace with proper+ // types+ if (sort_method === 'frecency' || sort_method === 'touched_by_me') {sort_method = 'updated_at';}diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/getItemsSoupParams.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/getItemsSoupParams.tsindex dd773dc304..548fd11bf0 100644--- a/apps/web/src/lib/service-clients/service-storage/generated/schemas/getItemsSoupParams.ts+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/getItemsSoupParams.ts@@ -18,8 +18,9 @@ export type GetItemsSoupParams = {*/limit?: number;/**- * Sort method. Options are viewed_at, created_at, updated_at, viewed_updated. Defaults to viewed_at.- */+ * Sort method. Options are viewed_at, created_at, updated_at,+viewed_updated, frecency, touched_by_me. Defaults to viewed_at.+ */sort_method?: SoupApiSort;/*** Sort direction. Options are asc, desc. Defaults to desc.diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/soupApiSort.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/soupApiSort.tsindex 730cfcad88..1a3bb77609 100644--- a/apps/web/src/lib/service-clients/service-storage/generated/schemas/soupApiSort.ts+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/soupApiSort.ts@@ -17,4 +17,5 @@ export const SoupApiSort = {updated_at: 'updated_at',viewed_updated: 'viewed_updated',frecency: 'frecency',+ touched_by_me: 'touched_by_me',} as const;diff --git a/apps/web/src/lib/service-clients/service-storage/generated/zod.ts b/apps/web/src/lib/service-clients/service-storage/generated/zod.tsindex 6784d426b7..9d4b9106fc 100644--- a/apps/web/src/lib/service-clients/service-storage/generated/zod.ts+++ b/apps/web/src/lib/service-clients/service-storage/generated/zod.ts@@ -7757,10 +7757,11 @@ export const getItemsSoupQueryParams = zod.object({'updated_at','viewed_updated','frecency',+ 'touched_by_me',]).optional().describe(- 'Sort method. Options are viewed_at, created_at, updated_at, viewed_updated. Defaults to viewed_at.'+ 'Sort method. Options are viewed_at, created_at, updated_at,\nviewed_updated, frecency, touched_by_me. Defaults to viewed_at.'),sort_direction: zod.enum(['asc', 'desc'])@@ -11385,6 +11386,7 @@ export const postItemsSoupBody = zod'updated_at','viewed_updated','frecency',+ 'touched_by_me',]).describe('Sort options accepted by non-grouped soup API endpoints.'@@ -14482,6 +14484,7 @@ export const postItemsSoupAstBody = zod'updated_at','viewed_updated','frecency',+ 'touched_by_me',]).describe('Sort options accepted by non-grouped soup API endpoints.'diff --git a/apps/web/src/lib/service-clients/service-storage/openapi.json b/apps/web/src/lib/service-clients/service-storage/openapi.jsonindex 8021b2e5cd..5739185e6e 100644--- a/apps/web/src/lib/service-clients/service-storage/openapi.json+++ b/apps/web/src/lib/service-clients/service-storage/openapi.json@@ -8330,7 +8330,7 @@{"name": "sort_method","in": "query",- "description": "Sort method. Options are viewed_at, created_at, updated_at, viewed_updated. Defaults to viewed_at.",+ "description": "Sort method. Options are viewed_at, created_at, updated_at,\nviewed_updated, frecency, touched_by_me. Defaults to viewed_at.","required": false,"schema": {"$ref": "#/components/schemas/SoupApiSort"@@ -19670,7 +19670,7 @@},{"$ref": "#/components/schemas/SoupApiSort",- "description": "Sort method. Options are viewed_at, created_at, updated_at, viewed_updated. Defaults to viewed_at."+ "description": "Sort method. Options are viewed_at, created_at, updated_at,\nviewed_updated, frecency, touched_by_me. Defaults to viewed_at."}]}@@ -21701,7 +21701,8 @@"created_at","updated_at","viewed_updated",- "frecency"+ "frecency",+ "touched_by_me"]},"SoupApiSortDirection": {diff --git a/crates/activity/src/domain/models.rs b/crates/activity/src/domain/models.rsindex e596e5ae6f..855271ec38 100644--- a/crates/activity/src/domain/models.rs+++ b/crates/activity/src/domain/models.rs@@ -141,6 +141,13 @@ impl From<CommonAction> for Action {}}+/// The stored `action` tags that [`Action::is_view`] classifies as views.+///+/// SQL surfaces that must exclude views (e.g. the soup touched-by-me page,+/// which counts mutations only) filter on these tags instead of decoding+/// every row; the pinned codec test keeps this list and `is_view` agreeing.+pub const VIEW_ACTION_TAGS: &[&str] = &["opened"];+impl Action {/// Whether this action is a view rather than a mutation — the only/// classification activity queries need.diff --git a/crates/activity/src/domain/models/test.rs b/crates/activity/src/domain/models/test.rsindex 8f67487123..ffd2ab61a0 100644--- a/crates/activity/src/domain/models/test.rs+++ b/crates/activity/src/domain/models/test.rs@@ -53,6 +53,13 @@ fn every_action_maps_to_stable_columns() {let (tag, payload) = action.to_columns();assert_eq!(tag, expected_tag, "tag for {action:?}");assert_eq!(payload, expected_payload, "payload for {action:?}");+ // VIEW_ACTION_TAGS is the SQL-side mirror of is_view: every variant+ // must agree so tag-filtering queries classify rows identically.+ assert_eq!(+ VIEW_ACTION_TAGS.contains(&tag),+ action.is_view(),+ "VIEW_ACTION_TAGS disagrees with is_view for {action:?}"+ );// The read codec is the exact inverse: every stored pair decodes// back to the action that produced it.diff --git a/crates/activity/src/lib.rs b/crates/activity/src/lib.rsindex a1720996e6..c377756ede 100644--- a/crates/activity/src/lib.rs+++ b/crates/activity/src/lib.rs@@ -28,6 +28,6 @@ pub mod outbound;pub use domain::models::{Action, ActionDecodeError, Activity, ActivityRecord, ActivitySource, Actor, CallStart,CommonAction, DomainActivity, EntityType, Ingest, ParticipantChange, PropertyChange,- RecordedAction, activity_id, event_time,+ RecordedAction, VIEW_ACTION_TAGS, activity_id, event_time,};pub use domain::ports::{ActivityFeedPage, ActivityReads, EntityActivityMap};diff --git a/crates/complete_graph/src/schema/test.rs b/crates/complete_graph/src/schema/test.rsindex a78c9cfb49..72e87996c6 100644--- a/crates/complete_graph/src/schema/test.rs+++ b/crates/complete_graph/src/schema/test.rs@@ -123,7 +123,7 @@ impl SoupService for CountingSoupService {if self.return_empty_raw || raw_response.is_some() {let page: PaginatedCursor<SoupItem<()>, String, SimpleSortMethod, T> =Paginated::from_parts(raw_response.unwrap_or_default(), None);- return Ok(soup::domain::ports::SoupOutput::Left(page));+ return Ok(soup::domain::ports::SoupOutput::Simple(page));}Err(test_soup_err())}diff --git a/crates/graphql_soup/src/loaders.rs b/crates/graphql_soup/src/loaders.rsindex 519c811e7e..f23aac3ed2 100644--- a/crates/graphql_soup/src/loaders.rs+++ b/crates/graphql_soup/src/loaders.rs@@ -148,7 +148,7 @@ where.get_user_soup(request, None).await.map_err(|error| rootcause::report!(error).into_dynamic().into_cloneable())?- .either(|page| page.items, |page| page.items);+ .into_items();let mut loaded = HashMap::with_capacity(items.len());for item in items {diff --git a/crates/graphql_soup/src/loaders/test.rs b/crates/graphql_soup/src/loaders/test.rsindex 4785762fda..5a10e3d222 100644--- a/crates/graphql_soup/src/loaders/test.rs+++ b/crates/graphql_soup/src/loaders/test.rs@@ -4,7 +4,6 @@ use std::{};use chrono::{DateTime, Utc};-use either::Either;use entity_access::domain::models::{EntityAccessReceipt, MemberTeamRole};use item_filters::ast::{call::CallLiteral,@@ -88,7 +87,7 @@ impl SoupService for RecordingSoupService {});let page: PaginatedCursor<SoupItem<()>, String, SimpleSortMethod, T> =Paginated::from_parts(items, None);- Ok(Either::Left(page))+ Ok(SoupOutput::Simple(page))}async fn get_user_soup_with_properties<T>(diff --git a/crates/models_pagination/src/lib.rs b/crates/models_pagination/src/lib.rsindex 40adec232d..07a79ef2a7 100644--- a/crates/models_pagination/src/lib.rs+++ b/crates/models_pagination/src/lib.rs@@ -19,4 +19,4 @@ pub use cursor::{Paginate, PaginateOn, Paginated, PaginatedCursor, PaginatedOpaqueCursor, Paginator, Query,SortOn, Sortable, TypeEraseCursor,};-pub use sort::{CreatedAt, Frecency, FrecencyValue, SimpleSortMethod, SortMethod};+pub use sort::{CreatedAt, Frecency, FrecencyValue, SimpleSortMethod, TouchedByMe};diff --git a/crates/models_pagination/src/sort.rs b/crates/models_pagination/src/sort.rsindex 0076bef087..c997c28c59 100644--- a/crates/models_pagination/src/sort.rs+++ b/crates/models_pagination/src/sort.rs@@ -34,6 +34,21 @@ impl Sortable for SimpleSortMethod {#[serde(rename_all = "snake_case")]pub struct Frecency;+/// Sort by the requesting user's most recent mutation of each entity, as+/// recorded in the activity log. Doubles as a filter: entities the user has+/// never mutated have no value to sort on and are absent from the page.+///+/// Serializes as `null` like [`Frecency`]; cursors for the two are told+/// apart by their value types ([`FrecencyValue`]'s tagged map vs a+/// timestamp).+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]+#[serde(rename_all = "snake_case")]+pub struct TouchedByMe;++impl Sortable for TouchedByMe {+ type Value = DateTime<Utc>;+}+/// the possible values of the cursor when sorting by frecency#[derive(Debug, Serialize, Deserialize, PartialEq)]pub enum FrecencyValue {@@ -74,22 +89,6 @@ impl Sortable for Frecency {type Value = FrecencyValue;}-/// either a [SimpleSortMethod] or an [AdvancedSortMethod]-#[derive(Debug, Clone, Copy, Serialize, Deserialize)]-#[serde(untagged)]-pub enum SortMethod {- /// A [SimpleSortMethod] with some extra params A- Simple(- /// the [SimplpleSortMethod]- SimpleSortMethod,- ),- /// A [AdvancedSortMethod] with some extra params B- Advanced(- /// the [AdvancedSortMethod]- Frecency,- ),-}-impl std::fmt::Display for SimpleSortMethod {fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {let s = serde_json::to_string(self).expect("This cant fail");diff --git a/crates/soup/Cargo.toml b/crates/soup/Cargo.tomlindex b6aff3aac4..2fcc08a5b5 100644--- a/crates/soup/Cargo.toml+++ b/crates/soup/Cargo.toml@@ -44,6 +44,7 @@ ports = ["crm/ports", "dep:tokio", "frecency/ports", "reminders/ports"][dependencies]+activity = { path = "../activity", default-features = false }ai_toolset = { path = "../ai_toolset", optional = true }anyhow = { workspace = true }async-trait = { workspace = true, optional = true }diff --git a/crates/soup/fixtures/touched_by_me.sql b/crates/soup/fixtures/touched_by_me.sqlnew file mode 100644index 0000000000..5941d6f600--- /dev/null+++ b/crates/soup/fixtures/touched_by_me.sql@@ -0,0 +1,126 @@+-- Fixture for the soup touched-by-me candidate query.+--+-- user-1 is the subject. The activity log gives them a mutation history over+-- documents, chats, projects, channels, and email threads, plus every trap+-- the query must dodge: opened-only entities, other users' activity, deleted+-- and inaccessible entities, left channels, and other users' inboxes.+--+-- user-1's expected feed, newest own-mutation first (T = minute past 10:00):+-- T9 doc-A (edited at T9; also created at T1 — group-max wins)+-- T8 chat-A (messaged at T8; also opened at T20 — views don't count)+-- T7 project-A (property_changed; unexpanded feeds only)+-- T6 channel-X (messaged; active participant)+-- T4 thread-Z (sent; user-1's own inbox link)+-- T2 doc-B (created; user-2's newer edit must not move it)++SET session_replication_role = 'replica';++INSERT INTO public."Organization" ("id", "name", "status")+VALUES (1, 'Test Organization', 'PILOT')+ON CONFLICT DO NOTHING;++INSERT INTO public."macro_user" ("id", "username", "email", "stripe_customer_id")+VALUES ('a1111111-1111-1111-1111-111111111111', '[email protected]', '[email protected]', 'stripe_id_1'),+ ('a2222222-2222-2222-2222-222222222222', '[email protected]', '[email protected]', 'stripe_id_2');++INSERT INTO public."User" ("id", "email", "stripeCustomerId", "organizationId", "macro_user_id")+VALUES ('macro|[email protected]', '[email protected]', 'stripe_id_1', 1, 'a1111111-1111-1111-1111-111111111111'),+ ('macro|[email protected]', '[email protected]', 'stripe_id_2', 1, 'a2222222-2222-2222-2222-222222222222')+ON CONFLICT DO NOTHING;++---------------------------------------------------+-- ENTITIES+---------------------------------------------------++INSERT INTO public."Project" ("id", "name", "userId", "parentId", "createdAt", "updatedAt")+VALUES ('aaaaaaaa-ffff-ffff-ffff-ffffffffffff', 'Project A', 'macro|[email protected]', NULL, '2023-01-01 10:00:00', '2023-01-01 10:00:00');++INSERT INTO public."DocumentFamily" ("id", "rootDocumentId")+VALUES (1, '11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa'),+ (2, '11111111-bbbb-bbbb-bbbb-bbbbbbbbbbbb'),+ (3, '11111111-dead-dead-dead-dddddddddddd'),+ (4, '11111111-9999-9999-9999-999999999999'),+ (5, '11111111-5555-5555-5555-555555555555');++INSERT INTO public."Document" ("id", "name", "owner", "projectId", "documentFamilyId", "fileType", "createdAt", "updatedAt", "deletedAt")+VALUES ('11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'Doc A', 'macro|[email protected]', 'aaaaaaaa-ffff-ffff-ffff-ffffffffffff', 1, 'pdf', '2023-01-05 10:00:00', '2023-01-05 10:00:00', NULL),+ ('11111111-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'Doc B', 'macro|[email protected]', NULL, 2, 'docx', '2023-01-05 11:00:00', '2023-01-05 11:00:00', NULL),+ -- Touched at T10 but soft-deleted: must never surface.+ ('11111111-dead-dead-dead-dddddddddddd', 'Deleted Doc', 'macro|[email protected]', NULL, 3, 'pdf', '2023-01-05 12:00:00', '2023-01-05 12:00:00', '2024-06-02 00:00:00'),+ -- Touched at T11 but no entity_access row: must never surface.+ ('11111111-9999-9999-9999-999999999999', 'Isolated Doc', 'macro|[email protected]', NULL, 4, 'pdf', '2023-01-05 13:00:00', '2023-01-05 13:00:00', NULL),+ -- Only ever opened (T12): views are not touches.+ ('11111111-5555-5555-5555-555555555555', 'Opened Doc', 'macro|[email protected]', NULL, 5, 'pdf', '2023-01-05 14:00:00', '2023-01-05 14:00:00', NULL);++INSERT INTO public."DocumentInstance" ("id", "documentId", "sha", "createdAt", "updatedAt")+VALUES (1, '11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'sha_A', '2023-01-05 10:00:00', '2023-01-05 10:00:00'),+ (2, '11111111-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'sha_B', '2023-01-05 11:00:00', '2023-01-05 11:00:00'),+ (3, '11111111-dead-dead-dead-dddddddddddd', 'sha_dead', '2023-01-05 12:00:00', '2023-01-05 12:00:00'),+ (4, '11111111-9999-9999-9999-999999999999', 'sha_iso', '2023-01-05 13:00:00', '2023-01-05 13:00:00'),+ (5, '11111111-5555-5555-5555-555555555555', 'sha_open', '2023-01-05 14:00:00', '2023-01-05 14:00:00');++INSERT INTO public."Chat" ("id", "name", "userId", "projectId", "createdAt", "updatedAt")+VALUES ('22222222-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'Chat A', 'macro|[email protected]', NULL, '2023-01-06 10:00:00', '2023-01-06 10:00:00');++INSERT INTO public.comms_channels ("id", "channel_type", "owner_id", "created_at", "updated_at")+VALUES ('33333333-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'private', 'macro|[email protected]', '2023-01-07 10:00:00', '2023-01-07 10:00:00'),+ ('33333333-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'private', 'macro|[email protected]', '2023-01-07 11:00:00', '2023-01-07 11:00:00');++INSERT INTO public.comms_channel_participants ("channel_id", "role", "user_id", "joined_at", "left_at")+VALUES ('33333333-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'member', 'macro|[email protected]', '2023-01-07 10:00:00', NULL),+ -- Touched at T5 but user-1 has left: must never surface.+ ('33333333-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'member', 'macro|[email protected]', '2023-01-07 11:00:00', '2024-06-01 00:00:00');++INSERT INTO public.email_links ("id", "macro_id", "fusionauth_user_id", "email_address", "provider")+VALUES ('55555555-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'macro|[email protected]', 'fa-user-1', '[email protected]', 'GMAIL'),+ ('55555555-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'macro|[email protected]', 'fa-user-2', '[email protected]', 'GMAIL');++INSERT INTO public.email_threads ("id", "link_id", "inbox_visible", "latest_inbound_message_ts", "created_at", "updated_at")+-- thread-Z is a just-sent thread: not inbox-visible, no inbound reply yet.+-- The touched feed must still carry it (the sender touched it), which is+-- exactly what an inbox-view-filtered hydration would silently drop.+VALUES ('44444444-aaaa-aaaa-aaaa-aaaaaaaaaaaa', '55555555-aaaa-aaaa-aaaa-aaaaaaaaaaaa', FALSE, NULL, '2024-06-01 09:00:00+00', '2024-06-01 09:00:00+00'),+ -- Touched at T3 but the thread lives in user-2's inbox: must never surface.+ ('44444444-bbbb-bbbb-bbbb-bbbbbbbbbbbb', '55555555-bbbb-bbbb-bbbb-bbbbbbbbbbbb', TRUE, '2024-06-01 09:00:00+00', '2024-06-01 09:00:00+00', '2024-06-01 09:00:00+00');++INSERT INTO public.entity_access ("entity_id", "entity_type", "source_id", "source_type", "access_level", "granted_from_project_id")+VALUES ('aaaaaaaa-ffff-ffff-ffff-ffffffffffff', 'project', 'macro|[email protected]', 'user', 'owner', NULL),+ ('11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'document', 'macro|[email protected]', 'user', 'owner', NULL),+ ('11111111-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'document', 'macro|[email protected]', 'user', 'owner', NULL),+ ('11111111-dead-dead-dead-dddddddddddd', 'document', 'macro|[email protected]', 'user', 'owner', NULL),+ ('11111111-5555-5555-5555-555555555555', 'document', 'macro|[email protected]', 'user', 'owner', NULL),+ ('22222222-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'chat', 'macro|[email protected]', 'user', 'owner', NULL);++---------------------------------------------------+-- ACTIVITY LOG+---------------------------------------------------++INSERT INTO public.activity_events ("id", "actor_id", "subject_id", "action", "action_payload", "entity_type", "entity_id", "occurred_at")+VALUES+-- doc-A: created T1 then edited T9 — the max mutation (T9) keys the feed.+('ae000000-0000-0000-0000-000000000001', 'macro|[email protected]', 'macro|[email protected]', 'created', NULL, 'document', '11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa', '2024-06-01 10:01:00+00'),+('ae000000-0000-0000-0000-000000000009', 'macro|[email protected]', 'macro|[email protected]', 'edited', NULL, 'document', '11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa', '2024-06-01 10:09:00+00'),+-- doc-B: created T2; user-2's newer edit (T30) must not move user-1's entry.+('ae000000-0000-0000-0000-000000000002', 'macro|[email protected]', 'macro|[email protected]', 'created', NULL, 'document', '11111111-bbbb-bbbb-bbbb-bbbbbbbbbbbb', '2024-06-01 10:02:00+00'),+('ae000000-0000-0000-0000-000000000030', 'macro|[email protected]', 'macro|[email protected]', 'edited', NULL, 'document', '11111111-bbbb-bbbb-bbbb-bbbbbbbbbbbb', '2024-06-01 10:30:00+00'),+-- chat-A: messaged T8, opened T20 — the view must not bump it above doc-A.+('ae000000-0000-0000-0000-000000000008', 'macro|[email protected]', 'macro|[email protected]', 'messaged', NULL, 'chat', '22222222-aaaa-aaaa-aaaa-aaaaaaaaaaaa', '2024-06-01 10:08:00+00'),+('ae000000-0000-0000-0000-000000000020', 'macro|[email protected]', 'macro|[email protected]', 'opened', NULL, 'chat', '22222222-aaaa-aaaa-aaaa-aaaaaaaaaaaa', '2024-06-01 10:20:00+00'),+-- project-A: property changed T7 (only unexpanded feeds include projects).+('ae000000-0000-0000-0000-000000000007', 'macro|[email protected]', 'macro|[email protected]', 'property_changed', '{"property": "p", "from": null, "to": "x"}', 'project', 'aaaaaaaa-ffff-ffff-ffff-ffffffffffff', '2024-06-01 10:07:00+00'),+-- channel-X: messaged T6 (active participant).+('ae000000-0000-0000-0000-000000000006', 'macro|[email protected]', 'macro|[email protected]', 'messaged', NULL, 'channel', '33333333-aaaa-aaaa-aaaa-aaaaaaaaaaaa', '2024-06-01 10:06:00+00'),+-- channel-Y: messaged T5, but user-1 has left the channel.+('ae000000-0000-0000-0000-000000000005', 'macro|[email protected]', 'macro|[email protected]', 'messaged', NULL, 'channel', '33333333-bbbb-bbbb-bbbb-bbbbbbbbbbbb', '2024-06-01 10:05:00+00'),+-- thread-Z: sent T4 (own inbox).+('ae000000-0000-0000-0000-000000000004', 'macro|[email protected]', 'macro|[email protected]', 'sent', NULL, 'email_thread', '44444444-aaaa-aaaa-aaaa-aaaaaaaaaaaa', '2024-06-01 10:04:00+00'),+-- thread-W: edited T3, but it lives in user-2's inbox.+('ae000000-0000-0000-0000-000000000003', 'macro|[email protected]', 'macro|[email protected]', 'edited', NULL, 'email_thread', '44444444-bbbb-bbbb-bbbb-bbbbbbbbbbbb', '2024-06-01 10:03:00+00'),+-- Deleted doc: edited T10, but the row is soft-deleted.+('ae000000-0000-0000-0000-000000000010', 'macro|[email protected]', 'macro|[email protected]', 'edited', NULL, 'document', '11111111-dead-dead-dead-dddddddddddd', '2024-06-01 10:10:00+00'),+-- Isolated doc: edited T11, but user-1 has no access row.+('ae000000-0000-0000-0000-000000000011', 'macro|[email protected]', 'macro|[email protected]', 'edited', NULL, 'document', '11111111-9999-9999-9999-999999999999', '2024-06-01 10:11:00+00'),+-- Opened-only doc: opened T12, never mutated.+('ae000000-0000-0000-0000-000000000012', 'macro|[email protected]', 'macro|[email protected]', 'opened', NULL, 'document', '11111111-5555-5555-5555-555555555555', '2024-06-01 10:12:00+00');++SET session_replication_role = 'origin';diff --git a/crates/soup/src/domain/models.rs b/crates/soup/src/domain/models.rsindex 4a105be836..0568abfc3f 100644--- a/crates/soup/src/domain/models.rs+++ b/crates/soup/src/domain/models.rs@@ -32,7 +32,9 @@ use item_filters::{use macro_user_id::user_id::MacroUserIdStr;use model_entity::Entity;use models_grouping::GroupingConfig;-use models_pagination::{Cursor, CursorWithValAndFilter, Frecency, Query, SimpleSortMethod};+use models_pagination::{+ Cursor, CursorWithValAndFilter, Frecency, Query, SimpleSortMethod, TouchedByMe,+};use models_soup::SoupProperty;use models_soup::item::SoupItem;use non_empty::IsEmpty;@@ -161,6 +163,9 @@ pub enum SoupQuery<T> {Simple(SimpleQueryInner<T>),/// Query sorted by frecency.Frecency(FrecencyQueryInner<T>),+ /// Query filtered and sorted by the user's own latest mutation per+ /// entity (the activity log's touched-by-me surface).+ Touched(TouchedQueryInner<T>),}impl<T> SoupQuery<T> {@@ -175,6 +180,9 @@ impl<T> SoupQuery<T> {SoupQuery::Frecency(FrecencyQueryInner(i)) => {SoupQuery::Frecency(FrecencyQueryInner(i.map_filter(f)))}+ SoupQuery::Touched(TouchedQueryInner(i)) => {+ SoupQuery::Touched(TouchedQueryInner(i.map_filter(f)))+ }}}}@@ -187,6 +195,15 @@ pub struct SimpleQueryInner<T>(pub(crate) Query<Uuid, SimpleSortMethod, T>);#[derive(Debug)]pub struct FrecencyQueryInner<T>(pub(crate) Query<Uuid, Frecency, T>);+/// the inner private type for [SoupQuery::Touched]+///+/// The id is a [String] rather than a [Uuid]: touched cursors key into the+/// activity log's TEXT `entity_id` column, and the raw string must survive+/// the cursor round-trip byte-for-byte (see+/// [`TouchedPagePosition::entity_id`]).+#[derive(Debug)]+pub struct TouchedQueryInner<T>(pub(crate) Query<String, TouchedByMe, T>);+impl<T> SoupQuery<T> {/// create a new instance of a [SimpleSortMethod] with [T] this is used to/// construct the initial page request. To paginate an existing cursor see [Self::new_cursor_simple]@@ -218,11 +235,29 @@ impl<T> SoupQuery<T> {SoupQuery::Frecency(FrecencyQueryInner(models_pagination::Query::Cursor(cursor)))}+ /// create a new instance of a [TouchedByMe] query with [T]. This is used+ /// to construct the initial page request. To paginate an existing cursor+ /// see [Self::new_cursor_touched]+ pub fn new_sort_touched(filters: T) -> Self {+ SoupQuery::Touched(TouchedQueryInner(models_pagination::Query::Sort(+ TouchedByMe,+ filters,+ )))+ }++ /// create a new instance of a [TouchedByMe] query with an existing cursor+ /// on [T]. This is used to continue paginating on an existing cursor.+ /// To create a new initial page see [Self::new_sort_touched]+ pub fn new_cursor_touched(cursor: CursorWithValAndFilter<String, TouchedByMe, T>) -> Self {+ SoupQuery::Touched(TouchedQueryInner(models_pagination::Query::Cursor(cursor)))+ }+/// Returns the filter payload embedded in this query.pub fn filter(&self) -> &T {match self {SoupQuery::Simple(SimpleQueryInner(query)) => query.filter(),SoupQuery::Frecency(FrecencyQueryInner(query)) => query.filter(),+ SoupQuery::Touched(TouchedQueryInner(query)) => query.filter(),}}}@@ -237,6 +272,9 @@ impl SoupQuery<EntityFilters> {SoupQuery::Frecency(FrecencyQueryInner(query)) => Ok(SoupQuery::Frecency(FrecencyQueryInner(query.try_map_filter(EntityFilterAst::new_from_filters)?),)),+ SoupQuery::Touched(TouchedQueryInner(query)) => Ok(SoupQuery::Touched(+ TouchedQueryInner(query.try_map_filter(EntityFilterAst::new_from_filters)?),+ )),}}}@@ -393,6 +431,8 @@ impl SoupRequest<Option<EntityFilterAst>> {})),// we don't yet have sort by frecency implemented for emails yetSoupQuery::Frecency(_) => None,+ // touched-by-me hydrates emails by thread id, not via this leg+ SoupQuery::Touched(_) => None,}?,include_frecency: false,team_receipt,@@ -409,6 +449,7 @@ impl SoupRequest<Option<EntityFilterAst>> {..}))) => filter.as_ref(),SoupQuery::Frecency(_) => None,+ SoupQuery::Touched(TouchedQueryInner(query)) => query.filter().as_ref(),}}@@ -462,6 +503,9 @@ impl SoupRequest<Option<EntityFilterAst>> {})),// query by frecency not yet implemented for call recordsSoupQuery::Frecency(_) => None,+ // call records carry no activity of their own (calls land on+ // the channel), so the touched feed never includes them+ SoupQuery::Touched(_) => None,}?,})}@@ -511,6 +555,8 @@ impl SoupRequest<Option<EntityFilterAst>> {}),),SoupQuery::Frecency(_) => return None,+ // CRM has no mutation activity (deferred from touched-by-me)+ SoupQuery::Touched(_) => return None,};let sort = match sort_method {@@ -561,8 +607,8 @@ impl SoupRequest<Option<EntityFilterAst>> {}// Reminders sort on `next_run_at` regardless of the requested method,// so there is nothing to translate — but frecency has no reminder- // scoring, so that path skips them.- if matches!(self.cursor, SoupQuery::Frecency(_)) {+ // scoring and reminders record no activity, so both paths skip them.+ if matches!(self.cursor, SoupQuery::Frecency(_) | SoupQuery::Touched(_)) {return None;}@@ -618,6 +664,8 @@ impl SoupRequest<Option<EntityFilterAst>> {})),// query by frecency not yet implemented for channelsSoupQuery::Frecency(_) => None,+ // touched-by-me hydrates channels by id, not via this leg+ SoupQuery::Touched(_) => None,}?,})}@@ -646,6 +694,9 @@ impl SoupRequest<Option<EntityFilterAst>> {})),// query by frecency not yet implemented for channel threadsSoupQuery::Frecency(_) => None,+ // channel-thread rows carry no activity (messages attribute to+ // the channel), so the touched feed never includes them+ SoupQuery::Touched(_) => None,}?;Some(GetThreadReplyRowsRequest {@@ -680,6 +731,9 @@ impl SoupRequest<Option<EntityFilterAst>> {})),// query by frecency is not implemented for foreign entitiesSoupQuery::Frecency(_) => None,+ // foreign entities record no activity, so the touched feed never+ // includes them+ SoupQuery::Touched(_) => None,}}@@ -697,6 +751,48 @@ impl SoupRequest<Option<EntityFilterAst>> {}}+/// Keyset position for a touched-by-me page: the touch timestamp and entity+/// id of the previous page's last row.+///+/// The id stays the raw stored string (never round-tripped through [`Uuid`]):+/// the SQL keyset compares it byte-for-byte against the `entity_id` TEXT+/// column, so any canonicalization would shift the page boundary.+#[derive(Debug, Clone)]+pub struct TouchedPagePosition {+ /// The last entity's latest-mutation timestamp.+ pub occurred_at: chrono::DateTime<chrono::Utc>,+ /// The last entity's id — the global tiebreaker when two entities share+ /// a touch timestamp.+ pub entity_id: String,+}++/// Parameters for one page of touched-by-me candidates.+#[derive(Debug)]+pub struct TouchedSoupRequest<'a> {+ /// User whose touches are listed; also the access-check subject.+ pub user_id: MacroUserIdStr<'a>,+ /// Maximum entities to return.+ pub limit: u16,+ /// Resume after this position; `None` for the first page.+ pub after: Option<TouchedPagePosition>,+ /// Entity filters folded into the candidate query.+ pub filter: Option<&'a EntityFilterAst>,+ /// Whether project rows belong in the feed (unexpanded soup only —+ /// expanded soup shows project contents rather than project rows).+ pub include_projects: bool,+ /// Every inbox the caller can read; gates email-thread candidates.+ pub link_ids: &'a [Uuid],+}++/// One touched entity: what it is and when the user last mutated it.+#[derive(Debug, Clone)]+pub struct TouchedEntity {+ /// The touched entity.+ pub entity: Entity<'static>,+ /// The user's latest mutation of it.+ pub touched_at: chrono::DateTime<chrono::Utc>,+}+/// ANDs a properties filter into the email filter tree as thread-level/// [EmailLiteral::Property] conditions, so the email query returns exactly/// the threads that satisfy it.@@ -982,6 +1078,11 @@ pub enum SoupErr {/// Reminder lookup failed.#[error("A reminder error has occurred, see logs for more details")]ReminderErr,+ /// A touched-by-me query carried a filter kind this mode cannot+ /// evaluate (the fold lives in another domain's query builder).+ /// Rejecting beats silently returning a feed with that type missing.+ #[error("sort_method=touched_by_me does not support {0} filters")]+ TouchedUnsupportedFilter(&'static str),/// The filter requested CRM-scoped data but the caller has no/// qualifying team membership.#[error("CRM-scoped queries require team membership")]diff --git a/crates/soup/src/domain/ports.rs b/crates/soup/src/domain/ports.rsindex 6ab762a613..886556ec52 100644--- a/crates/soup/src/domain/ports.rs+++ b/crates/soup/src/domain/ports.rs@@ -2,12 +2,15 @@ use std::sync::Arc;use crate::domain::models::{AdvancedSortParams, EnrichedSoupItem, GroupedSortRequest, IntoSoupReqAst, SimpleSortRequest,- SoupErr, SoupPropertiesField, SoupRequest, grouping::ItemGroupingInfo,+ SoupErr, SoupPropertiesField, SoupRequest, TouchedEntity, TouchedSoupRequest,+ grouping::ItemGroupingInfo,};-use either::Either;use entity_access::domain::models::{EntityAccessReceipt, MemberTeamRole};use macro_user_id::user_id::MacroUserIdStr;-use models_pagination::{Frecency, PaginatedCursor, SimpleSortMethod};+use models_pagination::{+ Frecency, PaginatedCursor, PaginatedOpaqueCursor, SimpleSortMethod, TouchedByMe,+ TypeEraseCursor,+};use models_properties::service::property_definition_with_options::PropertyDefinitionWithOptions;use models_soup::item::SoupItem;use serde::Serialize;@@ -70,18 +73,88 @@ pub trait SoupRepo: Send + Sync + 'static {&self,req: GroupedSortRequest<'a>,) -> impl Future<Output = Result<Self::GroupedItems, Self::Err>> + Send;++ /// Fetch one page of touched-by-me candidates: entities the user has+ /// mutated (views excluded), newest own-mutation first, already gated on+ /// existence, deletion, access, and the request's entity filters.+ fn touched_soup_page<'a>(+ &self,+ req: TouchedSoupRequest<'a>,+ ) -> impl Future<Output = Result<Vec<TouchedEntity>, Self::Err>> + Send;}-/// type alias which represents the posible outputs of soup-/// The response is a paginated cursor where+/// The possible outputs of soup — one paginated page per query mode.+/// The page is a paginated cursor where/// 1. The item type is the requested raw or enriched Soup representation/// 1. The id type is [String] (this should be changed to uuid)-/// 1. The sort method is [Either] [SimpleSortMethod] or [Frecency]+/// 1. The sort method matches the variant's query mode/// 1. The filter type is an [Option] [EntityFilterAst]-pub type SoupOutput<T, Item = SoupItem<()>> = Either<- PaginatedCursor<Item, String, SimpleSortMethod, T>,- PaginatedCursor<Item, String, Frecency, T>,->;+#[derive(Debug)]+pub enum SoupOutput<T, Item = SoupItem<()>> {+ /// Page of a timestamp-sorted query.+ Simple(PaginatedCursor<Item, String, SimpleSortMethod, T>),+ /// Page of a frecency-sorted query.+ Frecency(PaginatedCursor<Item, String, Frecency, T>),+ /// Page of a touched-by-me query.+ Touched(PaginatedCursor<Item, String, TouchedByMe, T>),+}++impl<T, Item> SoupOutput<T, Item> {+ /// Maps the item type of the page, preserving the cursor.+ pub fn map<F, V>(self, f: F) -> SoupOutput<T, V>+ where+ F: FnMut(Item) -> V,+ {+ match self {+ SoupOutput::Simple(page) => SoupOutput::Simple(page.map(f)),+ SoupOutput::Frecency(page) => SoupOutput::Frecency(page.map(f)),+ SoupOutput::Touched(page) => SoupOutput::Touched(page.map(f)),+ }+ }++ /// Discards the cursor and returns the page's items.+ pub fn into_items(self) -> Vec<Item> {+ match self {+ SoupOutput::Simple(page) => page.items,+ SoupOutput::Frecency(page) => page.items,+ SoupOutput::Touched(page) => page.items,+ }+ }++ /// Returns the page when this is a [`SoupOutput::Simple`] output.+ pub fn into_simple(self) -> Option<PaginatedCursor<Item, String, SimpleSortMethod, T>> {+ match self {+ SoupOutput::Simple(page) => Some(page),+ SoupOutput::Frecency(_) | SoupOutput::Touched(_) => None,+ }+ }++ /// Returns the page when this is a [`SoupOutput::Frecency`] output.+ pub fn into_frecency(self) -> Option<PaginatedCursor<Item, String, Frecency, T>> {+ match self {+ SoupOutput::Frecency(page) => Some(page),+ SoupOutput::Simple(_) | SoupOutput::Touched(_) => None,+ }+ }++ /// Returns the page when this is a [`SoupOutput::Touched`] output.+ pub fn into_touched(self) -> Option<PaginatedCursor<Item, String, TouchedByMe, T>> {+ match self {+ SoupOutput::Touched(page) => Some(page),+ SoupOutput::Simple(_) | SoupOutput::Frecency(_) => None,+ }+ }+}++impl<T, Item> TypeEraseCursor<Item> for SoupOutput<T, Item> {+ fn type_erase(self) -> PaginatedOpaqueCursor<Item> {+ match self {+ SoupOutput::Simple(page) => page.type_erase(),+ SoupOutput::Frecency(page) => page.type_erase(),+ SoupOutput::Touched(page) => page.type_erase(),+ }+ }+}#[cfg(test)]mod test;diff --git a/crates/soup/src/domain/service.rs b/crates/soup/src/domain/service.rsindex 7eed1728cb..bb6acff681 100644--- a/crates/soup/src/domain/service.rs+++ b/crates/soup/src/domain/service.rs@@ -3,7 +3,8 @@ use crate::domain::{AdvancedSortParams, EnrichedSoupItem, FrecencyQueryInner, GetCrmCompaniesRequest,GetRemindersRequest, GroupedSortRequest, IntoSoupReqAst, SimpleQueryInner, SimpleSortQuery,SimpleSortRequest, SoupErr, SoupPropertiesField, SoupQuery, SoupRequest, SoupSortDirection,- SoupType, grouping::ItemGroupingInfo,+ SoupType, TouchedPagePosition, TouchedQueryInner, TouchedSoupRequest,+ grouping::ItemGroupingInfo,},ports::{SoupOutput, SoupRepo, SoupService},};@@ -17,10 +18,11 @@ use crm::domain::service::CrmService;use doppleganger::Mirror;use either::Either;use email::domain::{- models::{EnrichedEmailThreadPreview, GetEmailsRequest},+ models::{EnrichedEmailThreadPreview, GetEmailsRequest, PreviewView, PreviewViewStandardLabel},ports::EmailPreviewServiceReadOnly,};use entity_access::domain::models::{EntityAccessReceipt, MemberTeamRole};+use filter_ast::Expr;use foreign_entity::domain::{models::{ForeignEntity, SourceId},ports::{ForeignEntityListQuery, ForeignEntityService},@@ -31,11 +33,12 @@ use frecency::domain::{},ports::FrecencyQueryService,};-use item_filters::ast::EntityFilterAst;+use item_filters::ast::{EntityFilterAst, channel::ChannelLiteral, email::EmailLiteral};use macro_user_id::user_id::MacroUserIdStr;+use model_entity::EntityType;use models_pagination::{- Cursor, CursorVal, Frecency, FrecencyValue, Identify, PaginateOn, Paginated, Query,- SimpleSortMethod, SortOn,+ Base64Str, Cursor, CursorVal, Frecency, FrecencyValue, Identify, PaginateOn, Paginated, Query,+ SimpleSortMethod, SortOn, TouchedByMe,};use models_properties::service::property_definition_with_options::PropertyDefinitionWithOptions;use models_soup::{@@ -53,6 +56,8 @@ use models_soup::{use reminders::domain::{models::SoupReminderQuery, ports::RemindersService};use serde::Serialize;use std::cmp::Ordering;+use std::collections::HashMap;+use std::sync::Arc;use uuid::Uuid;#[cfg(test)]@@ -64,6 +69,16 @@ struct SoupCandidate {frecency_score: Option<AggregateFrecency>,}+impl SoupCandidate {+ /// A candidate with no frecency loaded yet.+ fn plain(item: SoupItem<()>) -> Self {+ SoupCandidate {+ item,+ frecency_score: None,+ }+ }+}+impl Identify for SoupCandidate {type Id = String;@@ -91,6 +106,24 @@ impl SortOn<SimpleSortMethod> for SoupCandidate {}}+/// Builds a balanced `Or` tree over the given expressions. Balanced rather+/// than a linear fold: a full 500-item page folded linearly produces a+/// recursion-deep tree that downstream walkers and serializers reject.+fn balanced_or_tree<T>(mut nodes: Vec<Expr<T>>) -> Option<Arc<Expr<T>>> {+ while nodes.len() > 1 {+ let mut next = Vec::with_capacity(nodes.len().div_ceil(2));+ let mut iter = nodes.into_iter();+ while let Some(first) = iter.next() {+ next.push(match iter.next() {+ Some(second) => Expr::or(first, second),+ None => first,+ });+ }+ nodes = next;+ }+ nodes.pop().map(Arc::new)+}+fn foreign_entity_to_soup_item(entity: ForeignEntity) -> SoupItem<()> {SoupItem::ForeignEntity(SoupForeignEntity {id: entity.id,@@ -178,10 +211,7 @@ where.await.map_err(anyhow::Error::from)?,};- Ok(res.into_iter().map(|item| SoupCandidate {- item,- frecency_score: None,- }))+ Ok(res.into_iter().map(SoupCandidate::plain))}#[tracing::instrument(err, skip(self, req))]@@ -364,6 +394,176 @@ where})}+ /// Runs one page of the touched-by-me feed: fetches (entity, touched_at)+ /// candidates from the activity log, hydrates each entity type by id, and+ /// reassembles the page in touched order. Returns the ordered candidates+ /// plus the next keyset position when the candidate page was full.+ #[tracing::instrument(err, skip(self, cursor))]+ async fn handle_touched_request(+ &self,+ cursor: Query<String, TouchedByMe, Option<EntityFilterAst>>,+ soup_type: SoupType,+ user: MacroUserIdStr<'static>,+ limit: u16,+ link_ids: Vec<Uuid>,+ ) -> Result<(Vec<SoupCandidate>, Option<TouchedPagePosition>), SoupErr> {+ // Channel and email filter trees fold in their own domains' query+ // builders, which the touched candidate query cannot reach.+ // Rejecting beats accepting the filter and silently returning a+ // feed with that whole type missing.+ if let Some(ast) = cursor.filter() {+ if ast.channel_filter.is_some() {+ return Err(SoupErr::TouchedUnsupportedFilter("channel"));+ }+ if ast.email_filter.tree.is_some() || ast.email_filter.crm_scope.is_some() {+ return Err(SoupErr::TouchedUnsupportedFilter("email"));+ }+ }++ let after = match &cursor {+ Query::Sort(_, _) => None,+ Query::Cursor(c) => Some(TouchedPagePosition {+ occurred_at: c.val.last_val,+ entity_id: c.id.clone(),+ }),+ };+ let touched = self+ .soup_storage+ .touched_soup_page(TouchedSoupRequest {+ user_id: user.copied(),+ limit,+ after,+ filter: cursor.filter().as_ref(),+ include_projects: matches!(soup_type, SoupType::UnExpanded),+ link_ids: &link_ids,+ })+ .await+ .map_err(anyhow::Error::from)?;++ // The next cursor comes from the candidate page's own keyset, not+ // the hydrated item count: the candidate query already gates on+ // existence/access, so hydration only loses items to races, and a+ // race must not end the feed early.+ let next = (touched.len() == usize::from(limit))+ .then(|| {+ touched.last().map(|last| TouchedPagePosition {+ occurred_at: last.touched_at,+ entity_id: last.entity.entity_id.to_string(),+ })+ })+ .flatten();++ let mut main_entities = Vec::new();+ let mut channel_ids = Vec::new();+ let mut email_ids = Vec::new();+ for candidate in &touched {+ match candidate.entity.entity_type {+ EntityType::Document | EntityType::Chat | EntityType::Project => {+ main_entities.push(candidate.entity.copied())+ }+ EntityType::Channel => match Uuid::parse_str(&candidate.entity.entity_id) {+ Ok(id) => channel_ids.push(id),+ Err(error) => {+ tracing::warn!(error = ?error, "touched channel id is not a uuid; skipping")+ }+ },+ EntityType::EmailThread => match Uuid::parse_str(&candidate.entity.entity_id) {+ Ok(id) => email_ids.push(id),+ Err(error) => {+ tracing::warn!(error = ?error, "touched thread id is not a uuid; skipping")+ }+ },+ // The candidate query only returns the types above.+ _ => {}+ }+ }++ let comms_request = balanced_or_tree(+ channel_ids+ .iter()+ .map(|id| Expr::val(ChannelLiteral::ChannelId(*id)))+ .collect(),+ )+ .map(|tree| GetChannelsRequest {+ macro_id: user.clone(),+ limit: Some(channel_ids.len() as u32),+ include_frecency: false,+ query: Query::Sort(SimpleSortMethod::UpdatedAt, Some(tree)),+ });+ let email_request = balanced_or_tree(+ email_ids+ .iter()+ .map(|id| Expr::val(EmailLiteral::ThreadId(*id)))+ .collect(),+ )+ .map(|tree| GetEmailsRequest {+ // The `All` view adds no thread/message conditions. Any other+ // view would re-filter threads the candidate query already+ // admitted (Inbox drops just-sent and archived threads), and a+ // candidate that fails hydration is lost from the page.+ view: PreviewView::StandardLabel(PreviewViewStandardLabel::All),+ link_ids: link_ids.clone(),+ macro_id: user.clone(),+ limit: Some(email_ids.len() as u32),+ query: Query::Sort(SimpleSortMethod::UpdatedAt, Some(tree)),+ include_frecency: false,+ team_receipt: None,+ crm_scope: None,+ });++ // The repo error type is not Send, so it cannot ride through+ // tokio::join!; convert inside the future instead.+ let main_items_fut = async {+ self.handle_soup_by_ids(+ soup_type,+ AdvancedSortParams {+ entities: &main_entities,+ user_id: user.copied(),+ },+ )+ .await+ .map_err(anyhow::Error::from)+ .map_err(SoupErr::from)+ };+ let (main_items, channel_candidates, email_candidates) = tokio::join!(+ main_items_fut,+ self.handle_comms_request(comms_request),+ self.handle_email_request(email_request),+ );++ let mut items_by_entity: HashMap<(EntityType, String), SoupItem<()>> = HashMap::new();+ for item in main_items?+ .into_iter()+ .chain(channel_candidates?.map(|c| c.item))+ .chain(email_candidates?.map(|c| c.item))+ {+ let key = {+ let entity = item.entity();+ (entity.entity_type, entity.entity_id.to_string())+ };+ items_by_entity.insert(key, item);+ }++ let candidates = touched+ .into_iter()+ .filter_map(|candidate| {+ let key = (+ candidate.entity.entity_type,+ candidate.entity.entity_id.to_string(),+ );+ let item = items_by_entity.remove(&key).or_else(|| {+ // The candidate query gates on existence and access, so+ // a miss is a race (revoked/deleted mid-request).+ tracing::warn!(entity_type = ?key.0, "touched entity did not hydrate; skipping");+ None+ })?;+ Some(SoupCandidate::plain(item))+ })+ .collect();++ Ok((candidates, next))+ }+#[tracing::instrument(err, skip(self, req))]async fn handle_email_request(&self,@@ -454,11 +654,10 @@ where.await.map_err(|_| SoupErr::CommsErr)?.into_iter()- .map(|message| SoupCandidate {- item: SoupItem::ChannelThread(SoupChannelThread::new_from_channel_message(- message,- )),- frecency_score: None,+ .map(|message| {+ SoupCandidate::plain(SoupItem::ChannelThread(+ SoupChannelThread::new_from_channel_message(message),+ ))}),))}@@ -502,10 +701,7 @@ where.map(|company| SoupItem::CrmCompany(SoupCrmCompany::from(company))).collect();- Ok(Either::Right(items.into_iter().map(|item| SoupCandidate {- item,- frecency_score: None,- })))+ Ok(Either::Right(items.into_iter().map(SoupCandidate::plain)))}#[tracing::instrument(err, skip(self, req))]@@ -549,10 +745,7 @@ where.map(|reminder| SoupItem::Reminder(SoupReminder::from(reminder))).collect();- Ok(Either::Right(items.into_iter().map(|item| SoupCandidate {- item,- frecency_score: None,- })))+ Ok(Either::Right(items.into_iter().map(SoupCandidate::plain)))}#[tracing::instrument(err, skip(self, req))]@@ -577,10 +770,7 @@ where}).collect();- Ok(Either::Right(items.into_iter().map(|item| SoupCandidate {- item,- frecency_score: None,- })))+ Ok(Either::Right(items.into_iter().map(SoupCandidate::plain)))}#[tracing::instrument(err, skip(self, source_ids, query))]@@ -600,10 +790,7 @@ where.get_foreign_entities_for_user(requesting_user, source_ids, limit, query).await?.into_iter()- .map(|entity| SoupCandidate {- item: foreign_entity_to_soup_item(entity),- frecency_score: None,- }),+ .map(|entity| SoupCandidate::plain(foreign_entity_to_soup_item(entity))),))}@@ -657,10 +844,13 @@ whereoutput: SoupOutput<R, SoupCandidate>,) -> Result<SoupOutput<R, EnrichedSoupItem>, SoupErr> {match output {- Either::Left(page) => Ok(Either::Left(+ SoupOutput::Simple(page) => Ok(SoupOutput::Simple(+ self.populate_properties_page(user_id, page).await?,+ )),+ SoupOutput::Frecency(page) => Ok(SoupOutput::Frecency(self.populate_properties_page(user_id, page).await?,)),- Either::Right(page) => Ok(Either::Right(+ SoupOutput::Touched(page) => Ok(SoupOutput::Touched(self.populate_properties_page(user_id, page).await?,)),}@@ -704,50 +894,43 @@ wherematch output {// Simple sorting does not need frecency to construct the page, so// load it once for only the final page.- Either::Left(page) => Ok(Either::Left(+ SoupOutput::Simple(page) => Ok(SoupOutput::Simple(self.populate_frecency_page(user_id, page).await?,)),// Frecency sorting already loaded the scores needed for ordering// and cursor construction. Reuse them rather than issuing a// duplicate by-id lookup.- Either::Right(page) => Ok(Either::Right(page)),+ SoupOutput::Frecency(page) => Ok(SoupOutput::Frecency(page)),+ // Touched sorting orders on the activity log, so scores are not+ // loaded during page construction; load them for the final page.+ SoupOutput::Touched(page) => Ok(SoupOutput::Touched(+ self.populate_frecency_page(user_id, page).await?,+ )),}}fn into_raw_output<R>(output: SoupOutput<R, SoupCandidate>) -> SoupOutput<R> {- let into_raw = |candidate: SoupCandidate| candidate.item;- match output {- Either::Left(page) => Either::Left(page.map(into_raw)),- Either::Right(page) => Either::Right(page.map(into_raw)),- }+ output.map(|candidate| candidate.item)}fn into_enriched_output<R>(output: SoupOutput<R, SoupCandidate>,) -> SoupOutput<R, EnrichedSoupItem> {- let into_enriched = |candidate: SoupCandidate| EnrichedSoupItem {+ output.map(|candidate| EnrichedSoupItem {item: candidate.item.map_extra(|()| SoupPropertiesField::default()),frecency_score: candidate.frecency_score,- };- match output {- Either::Left(page) => Either::Left(page.map(into_enriched)),- Either::Right(page) => Either::Right(page.map(into_enriched)),- }+ })}fn clear_frecency<R>(output: SoupOutput<R, EnrichedSoupItem>,) -> SoupOutput<R, EnrichedSoupItem> {- let clear = |mut item: EnrichedSoupItem| {+ output.map(|mut item| {item.frecency_score = None;item- };- match output {- Either::Left(page) => Either::Left(page.map(clear)),- Either::Right(page) => Either::Right(page.map(clear)),- }+ })}async fn populate_grouped_items(@@ -893,15 +1076,35 @@ where}.into_page();- Ok(Either::Left(page))+ Ok(SoupOutput::Simple(page))}- SoupQuery::Frecency(FrecencyQueryInner(cursor)) => Ok(Either::Right(+ SoupQuery::Frecency(FrecencyQueryInner(cursor)) => Ok(SoupOutput::Frecency(self.handle_advanced_sort(cursor, req.soup_type, req.user, limit).await?.paginate_on(limit.into(), Frecency).filter_on(entity_filter).into_page(),)),+ SoupQuery::Touched(TouchedQueryInner(cursor)) => {+ let (candidates, next) = self+ .handle_touched_request(cursor, req.soup_type, req.user, limit, req.link_ids)+ .await?;+ let next_cursor = next.map(|position| {+ Base64Str::encode_json(Cursor {+ id: position.entity_id,+ limit: limit.into(),+ val: CursorVal {+ sort_type: TouchedByMe,+ last_val: position.occurred_at,+ },+ filter: entity_filter,+ })+ });+ Ok(SoupOutput::Touched(Paginated::from_parts(+ candidates,+ next_cursor,+ )))+ }}}}diff --git a/crates/soup/src/domain/service/tests.rs b/crates/soup/src/domain/service/tests.rsindex 981154b218..1228deecf2 100644--- a/crates/soup/src/domain/service/tests.rs+++ b/crates/soup/src/domain/service/tests.rs@@ -1,3 +1,4 @@+use crate::domain::models::TouchedEntity;use crate::domain::ports::MockSoupRepo;use channels::domain::{models::{@@ -641,7 +642,8 @@ async fn simple_soup_includes_channel_threads() {).await.unwrap()- .unwrap_left();+ .into_simple()+ .unwrap();assert_eq!(page.items.len(), 1);assert_matches!(@@ -703,7 +705,8 @@ async fn simple_soup_includes_call_records() {).await.unwrap()- .unwrap_left();+ .into_simple()+ .unwrap();assert_eq!(call_query_service.calls(), 1);assert_eq!(page.items.len(), 1);@@ -763,7 +766,8 @@ async fn simple_soup_uses_channel_thread_filters_without_touching_channel_filter).await.unwrap()- .unwrap_left();+ .into_simple()+ .unwrap();assert_eq!(comms_service.channel_calls(), 1);assert_eq!(comms_service.channel_filters(), vec!["null".to_string()]);@@ -827,7 +831,8 @@ async fn simple_soup_includes_foreign_entities() {).await.unwrap()- .unwrap_left();+ .into_simple()+ .unwrap();assert_eq!(page.items.len(), 2);assert_matches!(@@ -1074,7 +1079,8 @@ async fn foreign_entity_filter_suppresses_non_matching_foreign_entities() {).await.unwrap()- .unwrap_left();+ .into_simple()+ .unwrap();assert!(page.items.is_empty());assert!(foreign_entity_service.calls()[0].query.filter().is_some());@@ -1698,7 +1704,8 @@ async fn frecency_should_fallback() {).await.unwrap()- .unwrap_right();+ .into_frecency()+ .unwrap();// output should be the limitassert_eq!(res.items.len(), 100);@@ -1782,7 +1789,8 @@ async fn frecency_should_paginate() {).await.unwrap()- .unwrap_right();+ .into_frecency()+ .unwrap();// output should be the limitassert_eq!(res.items.len(), 100);@@ -1876,7 +1884,8 @@ async fn frecency_should_resume_cursor() {).await.unwrap()- .unwrap_right();+ .into_frecency()+ .unwrap();// first all items should be frecencyassert!(@@ -1978,7 +1987,8 @@ async fn frecency_fallback_cursor_should_resume() {).await.unwrap()- .unwrap_right();+ .into_frecency()+ .unwrap();assert!(res.items.iter().all(|v| v.frecency_score.is_none()));let cursor: CursorWithValAndFilter<String, Frecency, EntityFilters> =@@ -2049,7 +2059,7 @@ async fn cursor_should_return_simple_sort() {.await.unwrap();- let simple_cursor = res.unwrap_left();+ let simple_cursor = res.into_simple().unwrap();let cursor_decoded: CursorWithValAndFilter<String, SimpleSortMethod, EntityFilters> =simple_cursor.next_cursor.unwrap().decode_json().unwrap();assert_matches!(cursor_decoded, CursorWithValAndFilter { id, limit: 1, val: CursorVal { sort_type: SimpleSortMethod::ViewedUpdated, last_val }, filter: _ } => {@@ -2120,7 +2130,7 @@ async fn cursor_should_return_frecency() {.await.unwrap();- let simple_cursor = res.unwrap_right();+ let simple_cursor = res.into_frecency().unwrap();let cursor_decoded: CursorWithValAndFilter<String, Frecency, EntityFilters> =simple_cursor.next_cursor.unwrap().decode_json().unwrap();assert_matches!(cursor_decoded, CursorWithValAndFilter { id, limit: 100, val: CursorVal { sort_type: Frecency, last_val: FrecencyValue::FrecencyScore(1.0) }, filter: _ } => {@@ -2430,7 +2440,8 @@ async fn it_should_preserve_is_completed_in_by_ids_queries() {).await.unwrap()- .unwrap_right();+ .into_frecency()+ .unwrap();// Should have 3 items, verify is_completed values are preservedassert_eq!(res.items.len(), 3);@@ -2449,3 +2460,312 @@ async fn it_should_preserve_is_completed_in_by_ids_queries() {"Should contain is_completed=None");}++#[tokio::test]+async fn touched_soup_orders_by_touch_and_drops_unhydrated() {+ let user = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();+ let doc_1 = Uuid::from_u128(1);+ let doc_2 = Uuid::from_u128(2);+ let ghost = Uuid::from_u128(3);+ let base: DateTime<Utc> = DateTime::default();++ let mut soup_mock = MockSoupRepo::new();+ soup_mock+ .expect_touched_soup_page()+ .times(1)+ .returning(move |req| {+ assert!(req.after.is_none());+ assert!(!req.include_projects, "expanded soup excludes projects");+ Box::pin(async move {+ Ok(vec![+ TouchedEntity {+ entity: EntityType::Document.with_entity_string(doc_1.to_string()),+ touched_at: base + Days::new(3),+ },+ TouchedEntity {+ entity: EntityType::Document.with_entity_string(ghost.to_string()),+ touched_at: base + Days::new(2),+ },+ TouchedEntity {+ entity: EntityType::Document.with_entity_string(doc_2.to_string()),+ touched_at: base + Days::new(1),+ },+ ])+ })+ });+ // Hydration returns rows in arbitrary order and is missing the ghost;+ // the page must come back in touched order without it.+ soup_mock+ .expect_expanded_soup_by_ids()+ .times(1)+ .returning(move |_params| {+ Box::pin(async move {+ Ok(vec![+ SoupItem::Document(soup_document_uuid_with_updated(doc_2, Default::default())),+ SoupItem::Document(soup_document_uuid_with_updated(doc_1, Default::default())),+ ])+ })+ });++ let page = SoupImpl::new(+ soup_mock,+ FrecencyQueryServiceImpl::new(MockFrecencyStorage::new()),+ NoopEmailPreviewService,+ RecordingCommsService::new(vec![]),+ NoopCallRecordQueryService,+ NoOpCrmService,+ NoopForeignEntityService,+ NoOpRemindersService,+ )+ .get_user_soup(+ SoupRequest {+ sort_direction: SoupSortDirection::default(),+ email_preview_view: PreviewView::StandardLabel(+ email::domain::models::PreviewViewStandardLabel::Inbox,+ ),+ link_ids: vec![],+ soup_type: SoupType::Expanded,+ limit: 20,+ cursor: SoupQuery::new_sort_touched(EntityFilters::default()),+ user,+ },+ None,+ )+ .await+ .unwrap()+ .into_touched()+ .unwrap();++ let ids: Vec<Uuid> = page.items.iter().map(|item| item.id()).collect();+ assert_eq!(ids, vec![doc_1, doc_2]);+ // Three candidates against a limit of 20: the feed is exhausted.+ assert!(page.next_cursor.is_none());+}++#[tokio::test]+async fn touched_soup_full_page_builds_keyset_cursor() {+ let user = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();+ let base: DateTime<Utc> = DateTime::default();+ let ids: Vec<Uuid> = (1..=20).map(Uuid::from_u128).collect();+ let last_id = *ids.last().unwrap();+ let last_touch = base + Days::new(1);++ let mut soup_mock = MockSoupRepo::new();+ let touched_ids = ids.clone();+ soup_mock+ .expect_touched_soup_page()+ .times(1)+ .returning(move |_req| {+ let rows = touched_ids+ .iter()+ .enumerate()+ .map(|(i, id)| TouchedEntity {+ entity: EntityType::Document.with_entity_string(id.to_string()),+ touched_at: base + Days::new(20 - i as u64),+ })+ .collect();+ Box::pin(async move { Ok(rows) })+ });+ let hydrated_ids = ids.clone();+ soup_mock+ .expect_expanded_soup_by_ids()+ .times(1)+ .returning(move |_params| {+ let items = hydrated_ids+ .iter()+ .map(|id| {+ SoupItem::Document(soup_document_uuid_with_updated(*id, Default::default()))+ })+ .collect();+ Box::pin(async move { Ok(items) })+ });++ let page = SoupImpl::new(+ soup_mock,+ FrecencyQueryServiceImpl::new(MockFrecencyStorage::new()),+ NoopEmailPreviewService,+ RecordingCommsService::new(vec![]),+ NoopCallRecordQueryService,+ NoOpCrmService,+ NoopForeignEntityService,+ NoOpRemindersService,+ )+ .get_user_soup(+ SoupRequest {+ sort_direction: SoupSortDirection::default(),+ email_preview_view: PreviewView::StandardLabel(+ email::domain::models::PreviewViewStandardLabel::Inbox,+ ),+ link_ids: vec![],+ soup_type: SoupType::Expanded,+ limit: 20,+ cursor: SoupQuery::new_sort_touched(EntityFilters::default()),+ user,+ },+ None,+ )+ .await+ .unwrap()+ .into_touched()+ .unwrap();++ assert_eq!(page.items.len(), 20);+ // A full candidate page continues from the last row's keyset position.+ let decoded: CursorWithValAndFilter<String, models_pagination::TouchedByMe, EntityFilters> =+ page.next_cursor.unwrap().decode_json().unwrap();+ assert_eq!(decoded.id, last_id.to_string());+ assert_eq!(decoded.val.last_val, last_touch);+}++/// Records each email hydration request and returns no threads.+#[derive(Clone, Default)]+struct RecordingEmailPreviewService {+ requests: Arc<Mutex<Vec<(PreviewView, Vec<Uuid>, Option<u32>)>>>,+}++impl EmailPreviewServiceReadOnly for RecordingEmailPreviewService {+ async fn get_email_thread_previews(+ &self,+ req: email::domain::models::GetEmailsRequest,+ ) -> Result<+ PaginatedCursor<EnrichedEmailThreadPreview, Uuid, SimpleSortMethod, ()>,+ email::domain::models::EmailErr,+ > {+ self.requests+ .lock()+ .unwrap()+ .push((req.view.clone(), req.link_ids.clone(), req.limit));+ Ok(Option::<EnrichedEmailThreadPreview>::None+ .into_iter()+ .paginate_on(0, SimpleSortMethod::CreatedAt)+ .into_page())+ }+}++/// Touched email hydration must use the unfiltered `All` view: the candidate+/// query admits threads the caller's display view (e.g. Inbox) would hide,+/// and a view-filtered hydration would silently drop them from the page.+#[tokio::test]+async fn touched_soup_hydrates_emails_with_the_unfiltered_view() {+ let user = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();+ let thread = Uuid::from_u128(7);+ let link = Uuid::from_u128(8);+ let base: DateTime<Utc> = DateTime::default();++ let mut soup_mock = MockSoupRepo::new();+ soup_mock+ .expect_touched_soup_page()+ .times(1)+ .returning(move |_req| {+ Box::pin(async move {+ Ok(vec![TouchedEntity {+ entity: EntityType::EmailThread.with_entity_string(thread.to_string()),+ touched_at: base + Days::new(1),+ }])+ })+ });+ soup_mock+ .expect_expanded_soup_by_ids()+ .returning(|_params| Box::pin(async move { Ok(Vec::new()) }));++ let email_service = RecordingEmailPreviewService::default();+ let requests = email_service.requests.clone();++ let _page = SoupImpl::new(+ soup_mock,+ FrecencyQueryServiceImpl::new(MockFrecencyStorage::new()),+ email_service,+ RecordingCommsService::new(vec![]),+ NoopCallRecordQueryService,+ NoOpCrmService,+ NoopForeignEntityService,+ NoOpRemindersService,+ )+ .get_user_soup(+ SoupRequest {+ sort_direction: SoupSortDirection::default(),+ email_preview_view: PreviewView::StandardLabel(+ email::domain::models::PreviewViewStandardLabel::Inbox,+ ),+ link_ids: vec![link],+ soup_type: SoupType::Expanded,+ limit: 20,+ cursor: SoupQuery::new_sort_touched(EntityFilters::default()),+ user,+ },+ None,+ )+ .await+ .unwrap();++ let recorded = requests.lock().unwrap();+ let (view, link_ids, limit) = recorded.first().expect("email hydration ran");+ assert_eq!(+ view,+ &PreviewView::StandardLabel(email::domain::models::PreviewViewStandardLabel::All)+ );+ assert_eq!(link_ids, &vec![link]);+ assert_eq!(limit, &Some(1));+}++/// Channel and email filter trees fold in their own domains; combining them+/// with touched_by_me is rejected rather than silently dropping the type.+#[tokio::test]+async fn touched_soup_rejects_unfoldable_filters() {+ for (filters, expected_kind) in [+ (+ EntityFilters {+ channel_filters: item_filters::ChannelFilters {+ importance: Some(true),+ ..Default::default()+ },+ ..Default::default()+ },+ "channel",+ ),+ (+ EntityFilters {+ email_filters: item_filters::EmailFilters {+ importance: Some(true),+ ..Default::default()+ },+ ..Default::default()+ },+ "email",+ ),+ ] {+ let mut soup_mock = MockSoupRepo::new();+ soup_mock.expect_touched_soup_page().times(0);++ let err = SoupImpl::new(+ soup_mock,+ FrecencyQueryServiceImpl::new(MockFrecencyStorage::new()),+ NoopEmailPreviewService,+ RecordingCommsService::new(vec![]),+ NoopCallRecordQueryService,+ NoOpCrmService,+ NoopForeignEntityService,+ NoOpRemindersService,+ )+ .get_user_soup(+ SoupRequest {+ sort_direction: SoupSortDirection::default(),+ email_preview_view: PreviewView::StandardLabel(+ email::domain::models::PreviewViewStandardLabel::Inbox,+ ),+ link_ids: vec![],+ soup_type: SoupType::Expanded,+ limit: 20,+ cursor: SoupQuery::new_sort_touched(filters),+ user: MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap(),+ },+ None,+ )+ .await+ .unwrap_err();++ assert_matches!(err, SoupErr::TouchedUnsupportedFilter(kind) => {+ assert_eq!(kind, expected_kind);+ });+ }+}diff --git a/crates/soup/src/inbound/axum_router.rs b/crates/soup/src/inbound/axum_router.rsindex af7f2b1213..12b0968e8e 100644--- a/crates/soup/src/inbound/axum_router.rs+++ b/crates/soup/src/inbound/axum_router.rs@@ -2,6 +2,7 @@ use crate::domain::{models::{EnrichedSoupItem, FrecencyQueryInner, GroupedSortRequest, IntoSoupReqAst, SimpleQueryInner,SoupErr, SoupItemWithProperties, SoupQuery, SoupRequest, SoupSortDirection, SoupType,+ TouchedQueryInner,grouping::{GroupMeta, build_grouped_response},},ports::SoupService,@@ -13,7 +14,7 @@ use axum::{response::IntoResponse,routing::{get, post},};-use axum_extra::either::Either;+use axum_extra::either::Either3;use cowlike::CowLike;use email::{domain::{@@ -55,7 +56,7 @@ use model_entity::Entity;use model_error_response::ErrorResponse;use models_grouping::{GroupByField, GroupingConfig};use models_pagination::{- CursorWithValAndFilter, Frecency, PaginatedOpaqueCursor, SimpleSortMethod, SortMethod,+ CursorWithValAndFilter, Frecency, PaginatedOpaqueCursor, SimpleSortMethod, TouchedByMe,TypeEraseCursor,};use non_empty::IsEmpty;@@ -82,7 +83,8 @@ pub struct Params {/// Limit the number of items returned. Defaults to 20. Max 500.#[serde(default)]limit: Option<u16>,- /// Sort method. Options are viewed_at, created_at, updated_at, viewed_updated. Defaults to viewed_at.+ /// Sort method. Options are viewed_at, created_at, updated_at,+ /// viewed_updated, frecency, touched_by_me. Defaults to viewed_at.#[serde(default)]sort_method: Option<SoupApiSort>,/// Sort direction. Options are asc, desc. Defaults to desc.@@ -130,16 +132,30 @@ pub enum SoupApiSort {ViewedUpdated,/// Sort by frecency score.Frecency,+ /// Only entities the caller has mutated, newest own-mutation first.+ /// Both a filter and an ordering: views don't count, and entities the+ /// caller never touched are absent.+ TouchedByMe,}impl SoupApiSort {- fn into_sort_method(self) -> SortMethod {+ /// Builds the initial query for this sort method over the given filters.+ fn into_query<R>(self, filters: R) -> SoupQuery<R> {match self {- SoupApiSort::ViewedAt => SortMethod::Simple(SimpleSortMethod::ViewedAt),- SoupApiSort::CreatedAt => SortMethod::Simple(SimpleSortMethod::CreatedAt),- SoupApiSort::UpdatedAt => SortMethod::Simple(SimpleSortMethod::UpdatedAt),- SoupApiSort::ViewedUpdated => SortMethod::Simple(SimpleSortMethod::ViewedUpdated),- SoupApiSort::Frecency => SortMethod::Advanced(Frecency),+ SoupApiSort::ViewedAt => {+ SoupQuery::new_sort_simple(SimpleSortMethod::ViewedAt, filters)+ }+ SoupApiSort::CreatedAt => {+ SoupQuery::new_sort_simple(SimpleSortMethod::CreatedAt, filters)+ }+ SoupApiSort::UpdatedAt => {+ SoupQuery::new_sort_simple(SimpleSortMethod::UpdatedAt, filters)+ }+ SoupApiSort::ViewedUpdated => {+ SoupQuery::new_sort_simple(SimpleSortMethod::ViewedUpdated, filters)+ }+ SoupApiSort::Frecency => SoupQuery::new_sort_frecency(Frecency, filters),+ SoupApiSort::TouchedByMe => SoupQuery::new_sort_touched(filters),}}}@@ -529,34 +545,40 @@ wherelet sort_direction: SoupSortDirection =params.sort_direction.map(Into::into).unwrap_or_default();let create_fallback = move || -> SoupQuery<R> {- let params_sort = params+ params.sort_method- .map(|s| s.into_sort_method())- .unwrap_or(SortMethod::Simple(SimpleSortMethod::ViewedAt));- match params_sort {- SortMethod::Simple(simple_sort_method) => {- SoupQuery::new_sort_simple(simple_sort_method, filters)- }- SortMethod::Advanced(frecency) => SoupQuery::new_sort_frecency(frecency, filters),- }+ .unwrap_or(SoupApiSort::ViewedAt)+ .into_query(filters)};let cursor: SoupQuery<R> = match cursor {- Either::E1(l) => l+ Either3::E1(l) => l.map(SoupQuery::new_cursor_simple).unwrap_or_else(create_fallback),- Either::E2(r) => r+ Either3::E2(r) => r.map(SoupQuery::new_cursor_frecency).unwrap_or_else(create_fallback),+ Either3::E3(t) => t+ .map(SoupQuery::new_cursor_touched)+ .unwrap_or_else(create_fallback),};- // Frecency pages are ordered by relevance score, and that branch never- // applies the merged sort the direction would flip. Rejecting beats- // accepting the parameter and silently doing nothing with it. Checked- // against the resolved query so a frecency *cursor* is caught too, not+ // Frecency pages are ordered by relevance score and touched pages by+ // the caller's own latest mutation; neither branch applies the merged+ // sort the direction would flip. Rejecting beats accepting the+ // parameter and silently doing nothing with it. Checked against the+ // resolved query so a frecency/touched *cursor* is caught too, not// just an initial request naming the method.- if sort_direction == SoupSortDirection::Asc && matches!(cursor, SoupQuery::Frecency(_)) {- return Err(SoupHandlerErr::AscendingFrecencyUnsupported);+ if sort_direction == SoupSortDirection::Asc {+ match &cursor {+ SoupQuery::Frecency(_) => {+ return Err(SoupHandlerErr::AscendingFrecencyUnsupported);+ }+ SoupQuery::Touched(_) => {+ return Err(SoupHandlerErr::AscendingTouchedUnsupported);+ }+ SoupQuery::Simple(_) => {}+ }}// CRM authorization (team membership for CRM scope, admin/owner@@ -749,6 +771,12 @@ pub enum SoupHandlerErr {/// Ascending order was requested for a frecency-sorted query.#[error("sort_direction=asc is not supported with sort_method=frecency")]AscendingFrecencyUnsupported,+ /// Ascending order was requested for a touched-by-me query.+ #[error("sort_direction=asc is not supported with sort_method=touched_by_me")]+ AscendingTouchedUnsupported,+ /// A touched-by-me query carried a filter kind the mode cannot evaluate.+ #[error("sort_method=touched_by_me does not support {0} filters")]+ TouchedUnsupportedFilter(&'static str),}impl From<SoupErr> for SoupHandlerErr {@@ -757,6 +785,9 @@ impl From<SoupErr> for SoupHandlerErr {SoupErr::AstErr(expand_err) => SoupHandlerErr::ExpandErr(expand_err),SoupErr::CrmTeamRequired => SoupHandlerErr::CrmScopeForbidden,SoupErr::CrmAdminRequired => SoupHandlerErr::CrmAdminRequired,+ SoupErr::TouchedUnsupportedFilter(kind) => {+ SoupHandlerErr::TouchedUnsupportedFilter(kind)+ }err => SoupHandlerErr::Internal(err),}}@@ -767,7 +798,9 @@ impl IntoResponse for SoupHandlerErr {let status_code = match &self {SoupHandlerErr::ExpandErr(_)| SoupHandlerErr::Expand- | SoupHandlerErr::AscendingFrecencyUnsupported => StatusCode::BAD_REQUEST,+ | SoupHandlerErr::AscendingFrecencyUnsupported+ | SoupHandlerErr::AscendingTouchedUnsupported+ | SoupHandlerErr::TouchedUnsupportedFilter(_) => StatusCode::BAD_REQUEST,SoupHandlerErr::CrmScopeForbidden | SoupHandlerErr::CrmAdminRequired => {StatusCode::FORBIDDEN}@@ -874,9 +907,12 @@ struct ApiSoupRequestInner<T> {email_view: PreviewView,}-type SoupCursor<R> = axum_extra::either::Either<+type SoupCursor<R> = Either3<Option<CursorWithValAndFilter<Uuid, SimpleSortMethod, R>>,Option<CursorWithValAndFilter<Uuid, Frecency, R>>,+ // String id, not Uuid: the touched keyset compares the raw stored+ // entity id byte-for-byte, so the cursor must not canonicalize it.+ Option<CursorWithValAndFilter<String, TouchedByMe, R>>,>;/// Gets the items the user has access to@@ -1214,6 +1250,9 @@ impl IntoSoupReqAst for SoupRequest<ApiEntityFilterAst> {query.try_map_filter(ApiEntityFilterAst::into_optional_entity_ast)?,))}+ SoupQuery::Touched(TouchedQueryInner(query)) => SoupQuery::Touched(TouchedQueryInner(+ query.try_map_filter(ApiEntityFilterAst::into_optional_entity_ast)?,+ )),};Ok(SoupRequest {diff --git a/crates/soup/src/inbound/axum_router/tests.rs b/crates/soup/src/inbound/axum_router/tests.rsindex 1b9bcbad00..726b516a60 100644--- a/crates/soup/src/inbound/axum_router/tests.rs+++ b/crates/soup/src/inbound/axum_router/tests.rs@@ -34,7 +34,7 @@ use crate::{models::{EnrichedSoupItem, FrecencyQueryInner, GroupedSortRequest, IntoSoupReqAst,SimpleQueryInner, SoupErr, SoupPropertiesField, SoupQuery, SoupRequest,- SoupSortDirection, SoupType, grouping::ItemGroupingInfo,+ SoupSortDirection, SoupType, TouchedQueryInner, grouping::ItemGroupingInfo,},ports::{SoupOutput, SoupService},},@@ -53,6 +53,8 @@ enum MockCursorKind {SimpleCursor,FrecencySort,FrecencyCursor,+ TouchedSort,+ TouchedCursor,}#[derive(Debug)]@@ -159,6 +161,10 @@ impl SoupService for MockSoup {SoupQuery::Frecency(FrecencyQueryInner(Query::Cursor(..))) => {MockCursorKind::FrecencyCursor}+ SoupQuery::Touched(TouchedQueryInner(Query::Sort(..))) => MockCursorKind::TouchedSort,+ SoupQuery::Touched(TouchedQueryInner(Query::Cursor(..))) => {+ MockCursorKind::TouchedCursor+ }};let user_id = req.user.to_string();let soup_type = req.soup_type;@@ -179,7 +185,7 @@ impl SoupService for MockSoup {drop(guard);if self.succeeds {- Ok(either::Either::Left(+ Ok(SoupOutput::Simple(models_pagination::Paginated::from_parts(Vec::new(), None),))} else {@@ -1915,3 +1921,105 @@ async fn descending_frecency_is_accepted() {assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);assert_eq!(inner_counter.lock().unwrap().len(), 1);}++/// touched_by_me selects the touched query mode from the plain sort param.+#[tokio::test]+async fn touched_by_me_sort_selects_touched_query() {+ let soup = MockSoup::new();+ let inner_counter = soup.called.clone();+ let router: Router = mock_router_with(+ soup,+ MockEmailLinkResult {+ get_link_result: Arc::new(|| Ok(None)),+ },+ );++ let request = authenticated_request()+ .uri("/soup/ast")+ .method(Method::POST)+ .header("content-type", "application/json")+ .body(axum::body::Body::from(+ serde_json::to_vec(&json!({ "sort_method": "touched_by_me" })).unwrap(),+ ))+ .unwrap();++ let _res = router.oneshot(request).await.unwrap();+ let guard = inner_counter.lock().unwrap();+ let req = guard.first().unwrap();+ assert!(matches!(req.cursor_kind, MockCursorKind::TouchedSort));+}++/// A touched page cursor round-trips through the cursor extractor into the+/// touched query mode, not one of the other two cursor shapes.+#[tokio::test]+async fn touched_cursor_round_trips() {+ let soup = MockSoup::new();+ let inner_counter = soup.called.clone();+ let router: Router = mock_router_with(+ soup,+ MockEmailLinkResult {+ get_link_result: Arc::new(|| Ok(None)),+ },+ );++ // The shape the service emits: entity id + own-mutation timestamp. The+ // sort marker serializes as null (like frecency's); the timestamp value+ // is what keeps this out of the frecency cursor arm.+ let cursor = models_pagination::Base64Str::encode_json(models_pagination::Cursor {+ id: Uuid::new_v4().to_string(),+ limit: 20usize,+ val: CursorVal {+ sort_type: models_pagination::TouchedByMe,+ last_val: chrono::DateTime::<chrono::Utc>::default(),+ },+ filter: EntityFilters::default(),+ })+ .type_erase();++ // Percent-encode the base64 characters that are unsafe in a query string.+ let cursor = cursor+ .replace('+', "%2B")+ .replace('/', "%2F")+ .replace('=', "%3D");+ let request = authenticated_request()+ .uri(format!("/soup?cursor={cursor}"))+ .method(Method::POST)+ .header("content-type", "application/json")+ .body(axum::body::Body::from(+ serde_json::to_vec(&serde_json::json!({})).unwrap(),+ ))+ .unwrap();++ let _res = router.oneshot(request).await.unwrap();+ let guard = inner_counter.lock().unwrap();+ let req = guard.first().unwrap();+ assert!(matches!(req.cursor_kind, MockCursorKind::TouchedCursor));+}++/// Ascending touched order is rejected for the same reason as frecency: the+/// touched branch orders by the caller's own latest mutation and never+/// applies the merged sort a direction would flip.+#[tokio::test]+async fn ascending_touched_is_rejected() {+ let request = authenticated_request()+ .uri("/soup/ast")+ .method(Method::POST)+ .header("content-type", "application/json")+ .body(axum::body::Body::from(+ serde_json::to_vec(&json!({+ "sort_method": "touched_by_me",+ "sort_direction": "asc"+ }))+ .unwrap(),+ ))+ .unwrap();++ let (status, body) = send_json(mock_router(), request).await;+ assert_eq!(status, StatusCode::BAD_REQUEST);+ assert_eq!(+ body,+ json!({+ "message": "sort_direction=asc is not supported with sort_method=touched_by_me"+ })+ );+}diff --git a/crates/soup/src/outbound/pg_soup_repo.rs b/crates/soup/src/outbound/pg_soup_repo.rsindex bc74118442..ab3c4a006a 100644--- a/crates/soup/src/outbound/pg_soup_repo.rs+++ b/crates/soup/src/outbound/pg_soup_repo.rs@@ -2,7 +2,7 @@ use crate::{domain::{models::{AdvancedSortParams, GroupedSortRequest, SimpleSortQuery, SimpleSortRequest,- SoupPropertiesField, grouping::ItemGroupingInfo,+ SoupPropertiesField, TouchedEntity, TouchedSoupRequest, grouping::ItemGroupingInfo,},ports::SoupRepo,},@@ -20,6 +20,7 @@ use system_properties::SystemPropertyKey;mod calendar_event;mod expanded;pub mod grouping;+mod touched;mod unexpanded;/// PostgreSQL implementation of [`SoupRepo`].@@ -188,6 +189,13 @@ impl SoupRepo for PgSoupRepo {).await}++ async fn touched_soup_page<'a>(+ &self,+ req: TouchedSoupRequest<'a>,+ ) -> Result<Vec<TouchedEntity>, Self::Err> {+ touched::touched_soup_page(&self.pool.0, req).await+ }}fn sort_and_truncate(diff --git a/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic.rs b/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic.rsindex 3b6bcd5389..05c87e7ae8 100644--- a/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic.rs+++ b/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic.rs@@ -709,13 +709,18 @@ fn build_notification_join(entity_alias: &str, item_type: &str) -> String {}fn build_task_include_cbm_atm_nc_clause() -> String {- r#"(+ // The completed-status option id is interpolated (not `$6`-bound) so this+ // clause stays usable from queries with a different parameter layout —+ // the touched-by-me page reuses these folds with its own bind positions.+ format!(+ r#"(dt.sub_type = 'task'AND d.owner = $1AND ep_assignees.values->'value' @> jsonb_build_array(jsonb_build_object('entity_id', $1))- AND NOT COALESCE(ep_status.values->'value' ? $6, false)- )"#- .to_string()+ AND NOT COALESCE(ep_status.values->'value' ? '{completed}', false)+ )"#,+ completed = StatusOption::COMPLETED_UUID+ )}/// NULL-safe equality for nullable columns: FALSE (not UNKNOWN) on NULL, so@@ -735,7 +740,9 @@ fn date_predicate(col: &str, lit: &DateLiteral) -> String {}}-fn build_document_filter(ast: Option<&Expr<DocumentLiteral>>) -> String {+pub(in crate::outbound::pg_soup_repo) fn build_document_filter(+ ast: Option<&Expr<DocumentLiteral>>,+) -> String {let Some(expr) = ast else {return String::new();};@@ -888,7 +895,9 @@ fn build_calendar_event_filter(ast: Option<&Expr<CalendarEventLiteral>>) -> Stri}}-fn build_chat_filter(ast: Option<&Expr<ChatLiteral>>) -> String {+pub(in crate::outbound::pg_soup_repo) fn build_chat_filter(+ ast: Option<&Expr<ChatLiteral>>,+) -> String {let Some(expr) = ast else {return String::new();};@@ -932,7 +941,9 @@ fn build_chat_filter(ast: Option<&Expr<ChatLiteral>>) -> String {}}-fn build_project_filter(ast: Option<&Expr<ProjectLiteral>>) -> String {+pub(in crate::outbound::pg_soup_repo) fn build_project_filter(+ ast: Option<&Expr<ProjectLiteral>>,+) -> String {let Some(expr) = ast else {return String::new();};@@ -1023,7 +1034,9 @@ pub(in crate::outbound::pg_soup_repo) fn build_properties_filter(}}-fn document_filter_needs_task_property_joins(ast: Option<&Expr<DocumentLiteral>>) -> bool {+pub(in crate::outbound::pg_soup_repo) fn document_filter_needs_task_property_joins(+ ast: Option<&Expr<DocumentLiteral>>,+) -> bool {ast.is_some_and(|expr| {expr.collapse_frames(|frame| match frame {filter_ast::ExprFrame::And(a, b) | filter_ast::ExprFrame::Or(a, b) => a || b,@@ -1035,7 +1048,7 @@ fn document_filter_needs_task_property_joins(ast: Option<&Expr<DocumentLiteral>>})}-fn properties_filter_can_apply_to(+pub(in crate::outbound::pg_soup_repo) fn properties_filter_can_apply_to(ast: Option<&Expr<PropertiesLiteral>>,entity_types: &[PropertyEntityType],) -> bool {@@ -1052,7 +1065,9 @@ fn properties_filter_can_apply_to(})}-fn chat_filter_is_impossible(ast: Option<&Expr<ChatLiteral>>) -> bool {+pub(in crate::outbound::pg_soup_repo) fn chat_filter_is_impossible(+ ast: Option<&Expr<ChatLiteral>>,+) -> bool {ast.is_some_and(|expr| {expr.collapse_frames(|frame| match frame {filter_ast::ExprFrame::And(a, b) => a || b,@@ -1065,7 +1080,9 @@ fn chat_filter_is_impossible(ast: Option<&Expr<ChatLiteral>>) -> bool {})}-fn document_filter_is_impossible(ast: Option<&Expr<DocumentLiteral>>) -> bool {+pub(in crate::outbound::pg_soup_repo) fn document_filter_is_impossible(+ ast: Option<&Expr<DocumentLiteral>>,+) -> bool {ast.is_some_and(|expr| {expr.collapse_frames(|frame| match frame {filter_ast::ExprFrame::And(a, b) => a || b,@@ -1077,7 +1094,9 @@ fn document_filter_is_impossible(ast: Option<&Expr<DocumentLiteral>>) -> bool {})}-fn project_filter_is_impossible(ast: Option<&Expr<ProjectLiteral>>) -> bool {+pub(in crate::outbound::pg_soup_repo) fn project_filter_is_impossible(+ ast: Option<&Expr<ProjectLiteral>>,+) -> bool {ast.is_some_and(|expr| {expr.collapse_frames(|frame| match frame {filter_ast::ExprFrame::And(a, b) => a || b,@@ -1138,7 +1157,10 @@ fn top_needs_user_history(sort_method: SimpleSortMethod) -> bool {/// `idx_entity_access_entity_text_type_source`) when the arm's own filters/// are selective. The materialized form pinned the worst plan — the whole/// corpus was computed and probed against the item table on every page.-fn access_semi_join(id_sql: &str, entity_type: &str) -> String {+pub(in crate::outbound::pg_soup_repo) fn access_semi_join(+ id_sql: &str,+ entity_type: &str,+) -> String {format!(r#"{id_sql} IN (SELECT ea.entity_id::textdiff --git a/crates/soup/src/outbound/pg_soup_repo/touched.rs b/crates/soup/src/outbound/pg_soup_repo/touched.rsnew file mode 100644index 0000000000..ba886395d7--- /dev/null+++ b/crates/soup/src/outbound/pg_soup_repo/touched.rs@@ -0,0 +1,289 @@+//! The touched-by-me candidate query: one keyset-paginated page of entities+//! the user has mutated, newest own-mutation first, over the activity log.+//!+//! Every returned candidate is already gated on existence, deletion, access,+//! and the request's entity filters, so the page hydrates 1:1 — the service+//! builds the next cursor from this page's length, and a short page here+//! genuinely means the feed is exhausted.++use activity::VIEW_ACTION_TAGS;+use item_filters::ast::properties::{PropertyEntityType, properties_filter_matches_propertyless};+use model_entity::EntityType;+use sqlx::{PgPool, Row};+use std::str::FromStr;+use system_properties::SystemPropertyKey;++use crate::domain::models::{TouchedEntity, TouchedSoupRequest};+use crate::outbound::pg_soup_repo::expanded::dynamic::{+ access_semi_join, build_chat_filter, build_document_filter, build_project_filter,+ build_properties_filter, chat_filter_is_impossible, document_filter_is_impossible,+ document_filter_needs_task_property_joins, project_filter_is_impossible,+ properties_filter_can_apply_to,+};+use crate::outbound::pg_soup_repo::type_err;++/// Renders [`VIEW_ACTION_TAGS`] as a SQL `IN`-list body, e.g. `'opened'`.+/// The tags are compile-time constants of this workspace, never user input.+fn view_tags_sql() -> String {+ VIEW_ACTION_TAGS+ .iter()+ .map(|tag| format!("'{tag}'"))+ .collect::<Vec<_>>()+ .join(", ")+}++/// The per-type `EXISTS` gate for documents: exists, not deleted, accessible,+/// and matching the request's document + properties filters.+fn document_gate(req: &TouchedSoupRequest<'_>) -> String {+ let doc_filter = req.filter.and_then(|f| f.document_filter.as_deref());+ let props_filter = req.filter.and_then(|f| f.properties_filter.as_deref());+ // Importance / IncludeCbmAtmNc literals predicate on the dt/ep_assignees/+ // ep_status aliases, so those joins ride along only when referenced.+ let task_joins = if document_filter_needs_task_property_joins(doc_filter) {+ format!(+ r#"+ LEFT JOIN document_sub_type dt ON dt.document_id = d.id+ LEFT JOIN entity_properties ep_assignees+ ON dt.sub_type = 'task'+ AND ep_assignees.entity_id = d.id+ AND ep_assignees.entity_type = 'TASK'+ AND ep_assignees.property_definition_id = '{assignees}'+ LEFT JOIN entity_properties ep_status+ ON dt.sub_type = 'task'+ AND ep_status.entity_id = d.id+ AND ep_status.entity_type = 'TASK'+ AND ep_status.property_definition_id = '{status}'+"#,+ assignees = SystemPropertyKey::ASSIGNEES_UUID,+ status = SystemPropertyKey::STATUS_UUID,+ )+ } else {+ String::new()+ };+ format!(+ r#"EXISTS (+ SELECT 1 FROM "Document" d+ {task_joins}+ WHERE d.id = ae.entity_id+ AND d."deletedAt" IS NULL+ AND {access}+ {doc_fold}+ {props_fold}+ )"#,+ access = access_semi_join("d.id", "document"),+ doc_fold = build_document_filter(doc_filter),+ props_fold = build_properties_filter(props_filter, "d.id"),+ )+}++fn chat_gate(req: &TouchedSoupRequest<'_>) -> String {+ let chat_filter = req.filter.and_then(|f| f.chat_filter.as_deref());+ let props_filter = req.filter.and_then(|f| f.properties_filter.as_deref());+ format!(+ r#"EXISTS (+ SELECT 1 FROM "Chat" c+ WHERE c.id = ae.entity_id+ AND c."deletedAt" IS NULL+ AND {access}+ {chat_fold}+ {props_fold}+ )"#,+ access = access_semi_join("c.id", "chat"),+ chat_fold = build_chat_filter(chat_filter),+ props_fold = build_properties_filter(props_filter, "c.id"),+ )+}++fn project_gate(req: &TouchedSoupRequest<'_>) -> String {+ let project_filter = req.filter.and_then(|f| f.project_filter.as_deref());+ let props_filter = req.filter.and_then(|f| f.properties_filter.as_deref());+ format!(+ r#"EXISTS (+ SELECT 1 FROM "Project" p+ WHERE p.id = ae.entity_id+ AND p."deletedAt" IS NULL+ AND {access}+ {project_fold}+ {props_fold}+ )"#,+ access = access_semi_join("p.id", "project"),+ project_fold = build_project_filter(project_filter),+ props_fold = build_properties_filter(props_filter, "p.id"),+ )+}++/// Guards a gate whose body casts `ae.entity_id::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.+fn uuid_guarded(gate: String) -> String {+ format!(+ "CASE WHEN ae.entity_id ~ '^[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"+ )+}++/// Channels are participant-gated, not `entity_access` rows, are never+/// soft-deleted (purges delete their activity instead), and carry no+/// properties — a properties filter is settled type-wide in+/// [`included_types`], not per row.+fn channel_gate() -> String {+ uuid_guarded(+ r#"EXISTS (+ SELECT 1 FROM comms_channel_participants cp+ WHERE cp.channel_id = ae.entity_id::uuid+ AND cp.user_id = $1+ AND cp.left_at IS NULL+ )"#+ .to_string(),+ )+}++/// Email threads are inbox-scoped: visible iff the thread belongs to one of+/// the caller's readable links (own + delegated).+fn email_gate(req: &TouchedSoupRequest<'_>) -> String {+ let props_filter = req.filter.and_then(|f| f.properties_filter.as_deref());+ uuid_guarded(format!(+ r#"EXISTS (+ SELECT 1 FROM email_threads et+ WHERE et.id = ae.entity_id::uuid+ AND et.link_id = ANY($5)+ {props_fold}+ )"#,+ props_fold = build_properties_filter(props_filter, "et.id::text"),+ ))+}++/// Which entity types this request can include. A type drops when its own+/// filter can never match or when an active properties filter can never+/// match it. Channel and email filter trees never reach here — the domain+/// service rejects them up front ([`SoupErr::TouchedUnsupportedFilter`])+/// because their folds live in other domain crates' query builders.+///+/// [`SoupErr::TouchedUnsupportedFilter`]: crate::domain::models::SoupErr::TouchedUnsupportedFilter+fn included_types(req: &TouchedSoupRequest<'_>) -> Vec<&'static str> {+ let props = req.filter.and_then(|f| f.properties_filter.as_deref());+ let mut types = Vec::with_capacity(5);+ if !document_filter_is_impossible(req.filter.and_then(|f| f.document_filter.as_deref()))+ && properties_filter_can_apply_to(+ props,+ &[PropertyEntityType::Document, PropertyEntityType::Task],+ )+ {+ types.push(EntityType::Document.into());+ }+ if !chat_filter_is_impossible(req.filter.and_then(|f| f.chat_filter.as_deref()))+ && properties_filter_can_apply_to(props, &[PropertyEntityType::Chat])+ {+ types.push(EntityType::Chat.into());+ }+ if req.include_projects+ && !project_filter_is_impossible(req.filter.and_then(|f| f.project_filter.as_deref()))+ && properties_filter_can_apply_to(props, &[PropertyEntityType::Project])+ {+ types.push(EntityType::Project.into());+ }+ // Channels carry no properties, so the filter is settled here for the+ // whole type: include channels only when a propertyless entity can+ // satisfy the tree (the same gate the simple path's channel leg uses).+ if props.is_none_or(properties_filter_matches_propertyless) {+ types.push(EntityType::Channel.into());+ }+ if properties_filter_can_apply_to(props, &[PropertyEntityType::Thread])+ && !req.link_ids.is_empty()+ {+ types.push(EntityType::EmailThread.into());+ }+ types+}++/// Fetches one page of touched-by-me candidates.+///+/// Shape: keyset scan of the user's mutation activity in `occurred_at DESC`+/// order, keeping only each entity's newest mutation (the `NOT EXISTS`+/// group-max), gated per entity type on existence/deletion/access/filters so+/// `LIMIT` lands after gating and pages come back full until the feed ends.+#[tracing::instrument(err, skip(db, req))]+pub(super) async fn touched_soup_page(+ db: &PgPool,+ req: TouchedSoupRequest<'_>,+) -> Result<Vec<TouchedEntity>, sqlx::Error> {+ let types = included_types(&req);+ if types.is_empty() {+ return Ok(Vec::new());+ }++ let view_tags = view_tags_sql();+ let sql = format!(+ r#"+ WITH user_source_ids AS (+ SELECT cp.channel_id::text as source_id FROM comms_channel_participants cp+ WHERE cp.user_id = $1 AND cp.left_at IS NULL+ UNION ALL+ SELECT t.team_id::text FROM team_user t+ WHERE t.user_id = $1+ UNION ALL+ SELECT $1+ )+ SELECT ae.entity_type, ae.entity_id, ae.occurred_at+ FROM activity_events ae+ WHERE ae.subject_id = $1+ AND ae.action NOT IN ({view_tags})+ AND ae.entity_type = ANY($2)+ AND ($3::timestamptz IS NULL OR (ae.occurred_at, ae.entity_id) < ($3, $4))+ AND NOT EXISTS (+ SELECT 1 FROM activity_events newer+ WHERE newer.subject_id = $1+ AND newer.entity_type = ae.entity_type+ AND newer.entity_id = ae.entity_id+ AND newer.action NOT IN ({view_tags})+ AND (newer.occurred_at, newer.id) > (ae.occurred_at, ae.id)+ )+ AND CASE ae.entity_type+ WHEN 'document' THEN {document_gate}+ WHEN 'chat' THEN {chat_gate}+ WHEN 'project' THEN {project_gate}+ WHEN 'channel' THEN {channel_gate}+ WHEN 'email_thread' THEN {email_gate}+ ELSE FALSE+ END+ ORDER BY ae.occurred_at DESC, ae.entity_id DESC+ LIMIT $6+ "#,+ document_gate = document_gate(&req),+ chat_gate = chat_gate(&req),+ project_gate = project_gate(&req),+ channel_gate = channel_gate(),+ email_gate = email_gate(&req),+ );++ let after_ts = req.after.as_ref().map(|a| a.occurred_at);+ let after_id = req.after.map(|a| a.entity_id);++ sqlx::QueryBuilder::<sqlx::Postgres>::new(sql)+ .build()+ .bind(req.user_id.as_ref())+ .bind(&types)+ .bind(after_ts)+ .bind(after_id)+ .bind(req.link_ids)+ .bind(req.limit as i64)+ // Unnamed statement, same reasoning as the expanded dynamic query:+ // the SQL text varies per filter shape, so a cached prepared+ // statement is rarely reused but would flip to a generic plan.+ .persistent(false)+ .try_map(|row: sqlx::postgres::PgRow| {+ let entity_type: String = row.try_get("entity_type")?;+ let entity_id: String = row.try_get("entity_id")?;+ let occurred_at: chrono::DateTime<chrono::Utc> = row.try_get("occurred_at")?;+ let entity_type = EntityType::from_str(&entity_type).map_err(type_err)?;+ Ok(TouchedEntity {+ entity: entity_type.with_entity_string(entity_id),+ touched_at: occurred_at,+ })+ })+ .fetch_all(db)+ .await+}++#[cfg(test)]+mod test;diff --git a/crates/soup/src/outbound/pg_soup_repo/touched/test.rs b/crates/soup/src/outbound/pg_soup_repo/touched/test.rsnew file mode 100644index 0000000000..f46eac8433--- /dev/null+++ b/crates/soup/src/outbound/pg_soup_repo/touched/test.rs@@ -0,0 +1,258 @@+use super::*;+use crate::domain::models::TouchedPagePosition;+use item_filters::ast::EntityFilterAst;+use macro_db_migrator::MACRO_DB_MIGRATIONS;+use macro_user_id::user_id::MacroUserIdStr;+use sqlx::{Pool, Postgres};+use uuid::Uuid;++const USER_1: &str = "macro|[email protected]";+const DOC_A: &str = "11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa";+const DOC_B: &str = "11111111-bbbb-bbbb-bbbb-bbbbbbbbbbbb";+const CHAT_A: &str = "22222222-aaaa-aaaa-aaaa-aaaaaaaaaaaa";+const PROJECT_A: &str = "aaaaaaaa-ffff-ffff-ffff-ffffffffffff";+const CHANNEL_X: &str = "33333333-aaaa-aaaa-aaaa-aaaaaaaaaaaa";+const THREAD_Z: &str = "44444444-aaaa-aaaa-aaaa-aaaaaaaaaaaa";+const LINK_1: &str = "55555555-aaaa-aaaa-aaaa-aaaaaaaaaaaa";++fn req<'a>(+ user_id: &'a str,+ filter: Option<&'a EntityFilterAst>,+ link_ids: &'a [uuid::Uuid],+) -> TouchedSoupRequest<'a> {+ TouchedSoupRequest {+ user_id: MacroUserIdStr::parse_from_str(user_id).unwrap(),+ limit: 50,+ after: None,+ filter,+ include_projects: false,+ link_ids,+ }+}++fn keys(page: &[TouchedEntity]) -> Vec<(EntityType, String)> {+ page.iter()+ .map(|t| (t.entity.entity_type, t.entity.entity_id.to_string()))+ .collect()+}++#[sqlx::test(+ fixtures(path = "../../../../fixtures", scripts("touched_by_me")),+ migrator = "MACRO_DB_MIGRATIONS"+)]+async fn expanded_feed_orders_by_own_latest_mutation(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let link_ids = [Uuid::parse_str(LINK_1)?];+ let page = touched_soup_page(&pool, req(USER_1, None, &link_ids)).await?;++ // Exactly the touchable, visible entities — the deleted doc, isolated+ // doc, opened-only doc, left channel, foreign-inbox thread, and (in+ // expanded feeds) the project are all absent — ordered by user-1's own+ // latest mutation. user-2's newer edit of doc-B must not move it, and+ // chat-A's later `opened` must not outrank doc-A's edit.+ assert_eq!(+ keys(&page),+ vec![+ (EntityType::Document, DOC_A.to_string()),+ (EntityType::Chat, CHAT_A.to_string()),+ (EntityType::Channel, CHANNEL_X.to_string()),+ (EntityType::EmailThread, THREAD_Z.to_string()),+ (EntityType::Document, DOC_B.to_string()),+ ]+ );++ // The timestamps are each entity's own-mutation max.+ let minutes: Vec<u32> = page+ .iter()+ .map(|t| {+ use chrono::Timelike;+ t.touched_at.minute()+ })+ .collect();+ assert_eq!(minutes, vec![9, 8, 6, 4, 2]);++ Ok(())+}++#[sqlx::test(+ fixtures(path = "../../../../fixtures", scripts("touched_by_me")),+ migrator = "MACRO_DB_MIGRATIONS"+)]+async fn unexpanded_feed_includes_projects(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let link_ids = [Uuid::parse_str(LINK_1)?];+ let mut request = req(USER_1, None, &link_ids);+ request.include_projects = true;+ let page = touched_soup_page(&pool, request).await?;++ assert_eq!(+ keys(&page),+ vec![+ (EntityType::Document, DOC_A.to_string()),+ (EntityType::Chat, CHAT_A.to_string()),+ (EntityType::Project, PROJECT_A.to_string()),+ (EntityType::Channel, CHANNEL_X.to_string()),+ (EntityType::EmailThread, THREAD_Z.to_string()),+ (EntityType::Document, DOC_B.to_string()),+ ]+ );++ Ok(())+}++#[sqlx::test(+ fixtures(path = "../../../../fixtures", scripts("touched_by_me")),+ migrator = "MACRO_DB_MIGRATIONS"+)]+async fn keyset_paginates_without_overlap_or_gaps(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let link_ids = [Uuid::parse_str(LINK_1)?];++ let mut all = Vec::new();+ let mut after = None;+ loop {+ let mut request = req(USER_1, None, &link_ids);+ request.limit = 2;+ request.after = after;+ let page = touched_soup_page(&pool, request).await?;+ let full = page.len() == 2;+ all.extend(page);+ if !full {+ break;+ }+ let last = all.last().unwrap();+ after = Some(TouchedPagePosition {+ occurred_at: last.touched_at,+ entity_id: last.entity.entity_id.to_string(),+ });+ }++ // Walking in pages of 2 yields the same feed as one big page.+ let one_page = touched_soup_page(&pool, req(USER_1, None, &link_ids)).await?;+ assert_eq!(keys(&all), keys(&one_page));+ assert_eq!(all.len(), 5);++ Ok(())+}++#[sqlx::test(+ fixtures(path = "../../../../fixtures", scripts("touched_by_me")),+ migrator = "MACRO_DB_MIGRATIONS"+)]+async fn entity_filters_compose_with_the_touch_gate(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let link_ids = [Uuid::parse_str(LINK_1)?];++ // Restricting documents to pdf drops doc-B (docx) but nothing else.+ let filter = EntityFilterAst {+ document_filter: Some(std::sync::Arc::new(filter_ast::Expr::val(+ item_filters::ast::document::DocumentLiteral::FileType("pdf".parse().unwrap()),+ ))),+ ..EntityFilterAst::default()+ };+ let page = touched_soup_page(&pool, req(USER_1, Some(&filter), &link_ids)).await?;+ assert_eq!(+ keys(&page),+ vec![+ (EntityType::Document, DOC_A.to_string()),+ (EntityType::Chat, CHAT_A.to_string()),+ (EntityType::Channel, CHANNEL_X.to_string()),+ (EntityType::EmailThread, THREAD_Z.to_string()),+ ]+ );++ Ok(())+}++#[test]+fn view_tags_render_as_quoted_list() {+ assert_eq!(view_tags_sql(), "'opened'");+}++#[test]+fn no_filter_includes_every_expanded_type_with_inboxes() {+ let link_ids = [uuid::Uuid::from_u128(1)];+ let types = included_types(&req("macro|[email protected]", None, &link_ids));+ assert_eq!(types, vec!["document", "chat", "channel", "email_thread"]);+}++#[test]+fn projects_join_only_unexpanded_feeds() {+ let link_ids = [uuid::Uuid::from_u128(1)];+ let mut request = req("macro|[email protected]", None, &link_ids);+ request.include_projects = true;+ let types = included_types(&request);+ assert!(types.contains(&"project"));+}++#[test]+fn no_inboxes_drops_email_threads() {+ let types = included_types(&req("macro|[email protected]", None, &[]));+ assert!(!types.contains(&"email_thread"));+}++#[test]+fn positive_properties_filter_drops_propertyless_channels() {+ let link_ids = [uuid::Uuid::from_u128(1)];+ // A bare tag literal (no entity type) can never match a channel, which+ // carries no properties — the type is settled wholesale, not per row.+ let filter = EntityFilterAst {+ properties_filter: Some(std::sync::Arc::new(filter_ast::Expr::val(+ item_filters::ast::properties::PropertiesLiteral {+ property_definition_id: uuid::Uuid::from_u128(9),+ entity_type: None,+ value: item_filters::ast::properties::PropertyMatchValue::SelectOption(+ uuid::Uuid::from_u128(10),+ ),+ },+ ))),+ ..EntityFilterAst::default()+ };+ let types = included_types(&req("macro|[email protected]", Some(&filter), &link_ids));+ assert!(!types.contains(&"channel"));+ assert!(types.contains(&"document"));+}++#[test]+fn negated_properties_filter_keeps_propertyless_channels() {+ let link_ids = [uuid::Uuid::from_u128(1)];+ // NOT(tag) is satisfied by an entity with no properties at all, so+ // channels stay in the feed.+ let filter = EntityFilterAst {+ properties_filter: Some(std::sync::Arc::new(filter_ast::Expr::is_not(+ filter_ast::Expr::val(item_filters::ast::properties::PropertiesLiteral {+ property_definition_id: uuid::Uuid::from_u128(9),+ entity_type: None,+ value: item_filters::ast::properties::PropertyMatchValue::SelectOption(+ uuid::Uuid::from_u128(10),+ ),+ }),+ ))),+ ..EntityFilterAst::default()+ };+ let types = included_types(&req("macro|[email protected]", Some(&filter), &link_ids));+ assert!(types.contains(&"channel"));+}++#[test]+fn impossible_document_filter_drops_documents() {+ let link_ids = [uuid::Uuid::from_u128(1)];+ let filter = EntityFilterAst {+ document_filter: Some(std::sync::Arc::new(filter_ast::Expr::val(+ item_filters::ast::document::DocumentLiteral::Id(uuid::Uuid::nil()),+ ))),+ ..EntityFilterAst::default()+ };+ let types = included_types(&req("macro|[email protected]", Some(&filter), &link_ids));+ assert!(!types.contains(&"document"));+ assert!(types.contains(&"chat"));+}++#[test]+fn keyset_position_is_optional() {+ let link_ids = [uuid::Uuid::from_u128(1)];+ let mut request = req("macro|[email protected]", None, &link_ids);+ request.after = Some(TouchedPagePosition {+ occurred_at: chrono::DateTime::UNIX_EPOCH,+ entity_id: uuid::Uuid::from_u128(2).to_string(),+ });+ // The request stays constructible with a position; the SQL binds it as+ // ($3, $4). Query execution is covered by the pg tests.+ assert!(request.after.is_some());+}diff --git a/packages/sdk/generated/storage/types.gen.ts b/packages/sdk/generated/storage/types.gen.tsindex 6cb3e7ab69..6106230d4e 100644--- a/packages/sdk/generated/storage/types.gen.ts+++ b/packages/sdk/generated/storage/types.gen.ts@@ -6563,7 +6563,7 @@ export type SoupApiItem = SoupItem & {/*** Sort options accepted by non-grouped soup API endpoints.*/-export type SoupApiSort = 'viewed_at' | 'created_at' | 'updated_at' | 'viewed_updated' | 'frecency';+export type SoupApiSort = 'viewed_at' | 'created_at' | 'updated_at' | 'viewed_updated' | 'frecency' | 'touched_by_me';/*** Sort direction accepted by non-grouped soup API endpoints.@@ -11584,7 +11584,8 @@ export type GetItemsSoupData = {*/limit?: number;/**- * Sort method. Options are viewed_at, created_at, updated_at, viewed_updated. Defaults to viewed_at.+ * Sort method. Options are viewed_at, created_at, updated_at,+ * viewed_updated, frecency, touched_by_me. Defaults to viewed_at.*/sort_method?: SoupApiSort;/**diff --git a/packages/sdk/specs/storage.json b/packages/sdk/specs/storage.jsonindex 8021b2e5cd..5739185e6e 100644--- a/packages/sdk/specs/storage.json+++ b/packages/sdk/specs/storage.json@@ -8330,7 +8330,7 @@{"name": "sort_method","in": "query",- "description": "Sort method. Options are viewed_at, created_at, updated_at, viewed_updated. Defaults to viewed_at.",+ "description": "Sort method. Options are viewed_at, created_at, updated_at,\nviewed_updated, frecency, touched_by_me. Defaults to viewed_at.","required": false,"schema": {"$ref": "#/components/schemas/SoupApiSort"@@ -19670,7 +19670,7 @@},{"$ref": "#/components/schemas/SoupApiSort",- "description": "Sort method. Options are viewed_at, created_at, updated_at, viewed_updated. Defaults to viewed_at."+ "description": "Sort method. Options are viewed_at, created_at, updated_at,\nviewed_updated, frecency, touched_by_me. Defaults to viewed_at."}]}@@ -21701,7 +21701,8 @@"created_at","updated_at","viewed_updated",- "frecency"+ "frecency",+ "touched_by_me"]},"SoupApiSortDirection": {