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): notified_at sort for the inbox Signal/Noise tabs (#6138)
gbirmana969f06f1e17
diff --git a/apps/web/src/features/entity/types/entity.ts b/apps/web/src/features/entity/types/entity.tsindex 77f265c96d..12bef674fb 100644--- a/apps/web/src/features/entity/types/entity.ts+++ b/apps/web/src/features/entity/types/entity.ts@@ -23,6 +23,12 @@ export type EntityBase = {* helpers may bump it optimistically.*/touchedAt?: DateValue | null;+ /**+ * When the viewer was last notified about this entity, present only on+ * rows from `notified_at` pages. The inbox sorts and date-buckets on it,+ * and incoming notifications bump it optimistically.+ */+ notifiedAt?: DateValue | null;createdAt?: DateValue | null;updatedAt?: DateValue | null;viewedAt?: DateValue | null;diff --git a/apps/web/src/features/inbox-view/queries/inbox-query.ts b/apps/web/src/features/inbox-view/queries/inbox-query.tsindex 93d5ca8582..ff217ced58 100644--- a/apps/web/src/features/inbox-view/queries/inbox-query.ts+++ b/apps/web/src/features/inbox-view/queries/inbox-query.ts@@ -161,6 +161,15 @@ export type InboxViewContext = {userId: string | undefined;};+/**+ * Signal and Noise are notification feeds: a row belongs where its latest+ * notification puts it, not where its content's last edit does — a comment+ * on a week-old task is today's news. The server sort is also a filter (rows+ * without a notification are absent), which is what those tabs mean anyway.+ */+export const inboxTabOrdersByNotification = (tab: InboxTab): boolean =>+ tab === 'signal' || tab === 'noise';+/** Builds the heterogeneous Soup AST for the composable Inbox view. */export function buildInboxQuery(context: InboxViewContext,@@ -191,7 +200,9 @@ export function buildInboxQuery(params: {expand: true,limit: 100,- sort_method: 'updated_at',+ sort_method: inboxTabOrdersByNotification(context.tab)+ ? 'notified_at'+ : 'updated_at',sort_direction: context.tab === 'reminders' ? 'asc' : 'desc',},body,diff --git a/apps/web/src/features/inbox-view/queries/inbox-results.test.ts b/apps/web/src/features/inbox-view/queries/inbox-results.test.tsnew file mode 100644index 0000000000..7ad8d25292--- /dev/null+++ b/apps/web/src/features/inbox-view/queries/inbox-results.test.ts@@ -0,0 +1,102 @@+import type { EntityData, WithNotification } from '@entity';+import { describe, expect, it, vi } from 'vitest';+import { buildInboxQuery } from './inbox-query';+import { groupInboxEntitiesByDate, inboxGroupTimestamp } from './inbox-results';++// The soup barrel these pull in transitively imports the websocket client+// modules, which open real sockets at module scope and reject under jsdom.+vi.mock('@service-storage/websocket', () => ({+ storageWS: { reconnectIfDisconnected: vi.fn() },+ createWebSocketJob: vi.fn(),+}));+vi.mock('@service-connection/websocket', () => ({+ ws: { addEventListener: vi.fn(), send: vi.fn() },+ state: () => 'closed',+ createConnectionBlockWebsocketEffect: vi.fn(),+ createConnectionWebsocketEffect: vi.fn(),+}));++const now = new Date('2026-09-02T18:00:00Z');++// A task edited two days ago that the viewer was notified about an hour ago:+// the shape of the "fresh comment sorts into Yesterday" bug.+const staleTaskFreshComment = {+ id: 'task',+ type: 'document',+ name: 'Ship the tagging system',+ ownerId: 'macro|[email protected]',+ updatedAt: '2026-08-31T19:00:00Z',+ notifiedAt: '2026-09-02T17:00:00Z',+} as unknown as WithNotification<EntityData>;++const freshEmail = {+ id: 'email',+ type: 'email',+ name: 'Welcome',+ ownerId: 'macro|[email protected]',+ updatedAt: '2026-09-02T16:00:00Z',+ sortTs: '2026-09-02T16:00:00Z',+} as unknown as WithNotification<EntityData>;++const context = {+ facets: {},+ facetContext: { notificationSource: undefined as never },+ capabilities: {+ calendar: false,+ foreignEntities: false,+ reminders: false,+ snippets: false,+ },+ userId: 'macro|[email protected]',+};++describe('inbox sort method per tab', () => {+ it('orders the notification tabs by latest notification', () => {+ for (const tab of ['signal', 'noise'] as const) {+ expect(buildInboxQuery({ ...context, tab }).params.sort_method).toBe(+ 'notified_at'+ );+ }+ });++ it('keeps recency ordering on the other tabs', () => {+ for (const tab of ['all', 'reminders'] as const) {+ expect(buildInboxQuery({ ...context, tab }).params.sort_method).toBe(+ 'updated_at'+ );+ }+ });+});++describe('inbox date buckets', () => {+ it('buckets a notified row by its notification on the notification tabs', () => {+ expect(inboxGroupTimestamp(staleTaskFreshComment, 'signal')).toBe(+ '2026-09-02T17:00:00Z'+ );+ const groups = groupInboxEntitiesByDate(+ [freshEmail, staleTaskFreshComment],+ 'signal',+ now+ );+ expect(groups).toHaveLength(1);+ expect(groups[0].label).toBe('Today');+ });++ it('falls back to content recency without a stamp and on the other tabs', () => {+ expect(inboxGroupTimestamp(freshEmail, 'signal')).toBe(+ '2026-09-02T16:00:00Z'+ );+ expect(inboxGroupTimestamp(staleTaskFreshComment, 'all')).toBe(+ '2026-08-31T19:00:00Z'+ );+ const groups = groupInboxEntitiesByDate(+ [freshEmail, staleTaskFreshComment],+ 'all',+ now+ );+ expect(groups.map((group) => group.label)).toEqual([+ 'Today',+ 'Last 7 days',+ ]);+ });+});diff --git a/apps/web/src/features/inbox-view/queries/inbox-results.ts b/apps/web/src/features/inbox-view/queries/inbox-results.tsindex a4c83470a9..31ca64bb33 100644--- a/apps/web/src/features/inbox-view/queries/inbox-results.ts+++ b/apps/web/src/features/inbox-view/queries/inbox-results.ts@@ -4,17 +4,31 @@ import {type SoupGroup,} from '@app/features/soup/collection';import type { EntityData, WithNotification } from '@entity';+import type { InboxTab } from '../types';+import { inboxTabOrdersByNotification } from './inbox-query';export const inboxSortTimestamp = (entity: EntityData) =>entity.sortTs ?? entity.updatedAt ?? entity.createdAt;+/**+ * The timestamp a row is bucketed on. Tabs served by the `notified_at` sort+ * bucket on the viewer's latest notification, matching the order the rows+ * arrive in; rows without a stamp (websocket inserts) and the other tabs fall+ * back to content recency.+ */+export const inboxGroupTimestamp = (entity: EntityData, tab: InboxTab) =>+ (inboxTabOrdersByNotification(tab) ? entity.notifiedAt : undefined) ??+ inboxSortTimestamp(entity);+export function groupInboxEntitiesByDate(entities: WithNotification<EntityData>[],+ tab: InboxTab,now = new Date()): SoupGroup<WithNotification<EntityData>>[] {return groupSoupEntities(entities, {- getGroupId: (entity) => dateBucket(inboxSortTimestamp(entity), now).key,+ getGroupId: (entity) =>+ dateBucket(inboxGroupTimestamp(entity, tab), now).key,getGroupLabel: (_groupId, firstEntity) =>- dateBucket(inboxSortTimestamp(firstEntity), now).label,+ dateBucket(inboxGroupTimestamp(firstEntity, tab), now).label,});}diff --git a/apps/web/src/features/inbox-view/queries/use-inbox-query.ts b/apps/web/src/features/inbox-view/queries/use-inbox-query.tsindex 53639dbbad..7e1574f4f9 100644--- a/apps/web/src/features/inbox-view/queries/use-inbox-query.ts+++ b/apps/web/src/features/inbox-view/queries/use-inbox-query.ts@@ -247,7 +247,9 @@ export function useInboxDataSource(const items = createMemo<InboxDataSourceItem[]>(() => {let result: InboxDataSourceItem[];if (state.groupBy === 'date' && !search.isSearching()) {- result = buildGroupedSoupRows(groupInboxEntitiesByDate(entities().items));+ result = buildGroupedSoupRows(+ groupInboxEntitiesByDate(entities().items, state.tab)+ );} else {result = buildFlatSoupRows(entities().items);}diff --git a/apps/web/src/features/next-soup/sidebar/soup-filter-presets.test.ts b/apps/web/src/features/next-soup/sidebar/soup-filter-presets.test.tsindex ec0befbc9f..f090e399c8 100644--- a/apps/web/src/features/next-soup/sidebar/soup-filter-presets.test.ts+++ b/apps/web/src/features/next-soup/sidebar/soup-filter-presets.test.ts@@ -298,6 +298,32 @@ describe('recent view preset', () => {it('forces the touched-by-me server sort', () => {expect(getViewPreset('recent')?.sortMethod).toBe('touched_by_me');});+});++describe('inbox view presets', () => {+ // `getViewPreset` falls back to the first compatible tab when the requested+ // one does not resolve, so each check also pins the tab's own predicate.+ it('orders the notification tabs by latest notification', () => {+ // The sort is also a filter (rows without a notification are absent),+ // which matches what Signal and Noise already mean.+ const signal = getViewPreset('inbox', 'signal');+ expect(signal?.clientFilters).toEqual({ and: ['inbox'] });+ expect(signal?.sortMethod).toBe('notified_at');+ const noise = getViewPreset('inbox', 'noise');+ expect(noise?.clientFilters).toEqual({ and: ['noise'] });+ expect(noise?.sortMethod).toBe('notified_at');+ });++ it('keeps recency ordering on the other tabs', () => {+ // The inbox client sort id is not an API sort method, so these must name+ // their server sort or the API would fall back to created_at.+ const all = getViewPreset('inbox', 'all');+ expect(all?.clientFilters).toEqual({ and: ['explicit-noise'] });+ expect(all?.sortMethod).toBe('updated_at');+ const reminders = getViewPreset('inbox', 'reminders');+ expect(reminders?.clientFilters).toEqual({ and: ['reminders-not-done'] });+ expect(reminders?.sortMethod).toBe('updated_at');+ });it('never compiles channel or email filter trees', () => {// The touched-by-me query rejects channel/email trees with a 400, sodiff --git a/apps/web/src/features/next-soup/sidebar/soup-filter-presets.ts b/apps/web/src/features/next-soup/sidebar/soup-filter-presets.tsindex 0a8062346d..ddd0868ed7 100644--- a/apps/web/src/features/next-soup/sidebar/soup-filter-presets.ts+++ b/apps/web/src/features/next-soup/sidebar/soup-filter-presets.ts@@ -34,12 +34,12 @@ type SoupFiltersPreset = {*/sortDirection?: 'asc' | 'desc';/**- * Server sort this tab's meaning requires (e.g. `touched_by_me`), taking- * precedence over the client sort state. Tabs that force one usually also- * clear the client sort (`SoupView`'s `initialClientSort={[]}`) so the- * server's ordering survives to the rendered rows. Frecency is excluded:- * it is a different query flavor with its own client handling, not a- * per-tab ordering.+ * Server sort this tab's meaning requires (e.g. `touched_by_me`,+ * `notified_at`), taking precedence over the client sort state. Tabs that+ * force one usually also pin the client sort (`SoupView`'s+ * `initialClientSort`) so the server's ordering survives to the rendered+ * rows. Frecency is excluded: it is a different query flavor with its own+ * client handling, not a per-tab ordering.*/sortMethod?: Exclude<NonNullable<Params['sort_method']>, 'frecency'>;};@@ -183,16 +183,27 @@ export const VIEW_TAB_PRESETS: Record<ListView, ViewTabConfig> = {inbox: {default: 'signal',tabs: {+ // Signal and Noise order by when the viewer was last notified about+ // each row, not by the row's own recency: a comment on a week-old+ // task is today's news. The sort is also a filter (rows without a+ // notification are absent), which is what these tabs mean anyway.+ // The inbox's client sort (`notified_at`, see `SoupView`) keeps the+ // server order and buckets the date headers on the same timestamp.signal: () => ({filters: getInboxSignalFilters(),clientFilters: { and: ['inbox'] },groupBy: 'date',+ sortMethod: 'notified_at',}),noise: () => ({filters: getInboxNoiseFilters(),clientFilters: { and: ['noise'] },groupBy: 'date',+ sortMethod: 'notified_at',}),+ // All and Reminders keep their recency ordering. Named explicitly+ // because the inbox's client sort id is not an API sort method, so+ // without a preset value the API would fall back to created_at.all: () => ({filters: {// Calendar events are not rendered by Soup, and CRM companies are@@ -219,6 +230,7 @@ export const VIEW_TAB_PRESETS: Record<ListView, ViewTabConfig> = {},clientFilters: { and: ['explicit-noise'] },groupBy: 'date',+ sortMethod: 'updated_at',}),// Every reminder still on the hook: the ones coming up and the ones that// have fired and are waiting to be dealt with (fired ones also surface in@@ -233,6 +245,7 @@ export const VIEW_TAB_PRESETS: Record<ListView, ViewTabConfig> = {}),clientFilters: { and: ['reminders-not-done'] },sortDirection: 'asc',+ sortMethod: 'updated_at',}),},},diff --git a/apps/web/src/features/next-soup/soup-view/sort-options.tsx b/apps/web/src/features/next-soup/soup-view/sort-options.tsxindex 6f1f8b5e35..297e0dcbb2 100644--- a/apps/web/src/features/next-soup/soup-view/sort-options.tsx+++ b/apps/web/src/features/next-soup/soup-view/sort-options.tsx@@ -3,11 +3,7 @@ import type {SoupEntity,} from '@app/features/next-soup/create-soup-state';import { compareDateDesc } from '@core/util/date';-import type {- EntityData,- TaskEntityWithProperties,- WithNotification,-} from '@entity';+import type { EntityData, TaskEntityWithProperties } from '@entity';import {getTaskPriorityOptionId,getTaskStatusOptionId,@@ -33,15 +29,16 @@ export interface SortOption {icon?: () => JSX.Element;}-function _sortByNotifiedAt<T extends WithNotification<EntityData>>(a: T, b: T) {- const aNotification = a.notifications?.()[0];- const bNotification = b.notifications?.()[0];-- if (aNotification && bNotification) {- return compareDateDesc(aNotification.created_at, bNotification.created_at);- } else if (aNotification) {+// The inbox's order: when the viewer was last notified about each row,+// newest first. Only `notified_at` pages carry the stamp, so a row without+// one (a websocket insert, or a tab served by a recency sort) falls back to+// update recency — which keeps the All and Reminders tabs ordered as before.+function sortByNotifiedAt<T extends EntityData>(a: T, b: T): number {+ if (a.notifiedAt && b.notifiedAt) {+ return compareDateDesc(a.notifiedAt, b.notifiedAt);+ } else if (a.notifiedAt) {return -1;- } else if (bNotification) {+ } else if (b.notifiedAt) {return 1;}@@ -159,6 +156,10 @@ export const SORT_CONFIGS = {id: 'touched_at',fn: sortByTouchedAt,},+ notified_at: {+ id: 'notified_at',+ fn: sortByNotifiedAt,+ },priority: {id: 'priority',fn: sortByPriority,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 e77d1ebefc..c71578ef92 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@@ -9,6 +9,7 @@ import { SearchState } from '@app/features/command/mobile/mobileSearchState';import {createSoupState,type GroupMeta,+ type SortConfig,type SoupEntity,type SoupRow,type SoupState,@@ -39,6 +40,7 @@ import {INBOX_FILTER_ENTRY_KEY,registerInboxFilterSplit,} from '@app/features/next-soup/soup-view/inbox-filter-controllers';+import { SORT_CONFIGS } from '@app/features/next-soup/soup-view/sort-options';import { useSoupFilterPersistence } from '@app/features/next-soup/use-soup-filter-persistence';import { deduplicateEntities } from '@app/features/next-soup/utils';import { withEntityNotifications } from '@app/features/soup/entity-notifications';@@ -179,6 +181,9 @@ interface SoupViewContextValues {setInboxFilter: Setter<string[] | undefined>;activeTab: Accessor<string | undefined>;setActiveTab: Setter<string | undefined>;+ /** The sort the rows are rendered in: the active tab's forced sort when+ * it has one the client can reproduce, else the sort state. */+ clientSort: Accessor<SortConfig<SoupEntity>[]>;getPersistedActiveTab: (view: ListView) => string | undefined;viewMode: Accessor<SoupViewMode>;setViewMode: Setter<SoupViewMode>;@@ -275,7 +280,7 @@ const persistedPredicatesFor = (type ApiSortMethod = Exclude<NonNullable<SoupParams['sort_method']>,- 'frecency' | 'touched_by_me'+ 'frecency' | 'touched_by_me' | 'notified_at'>;const VALID_API_SORT_METHODS: ApiSortMethod[] = ['viewed_at',@@ -666,6 +671,16 @@ export const SoupViewContextProvider: FlowComponent<// Sits below `activeTab`/`userId` because the page direction comes from the// active tab's preset, which some views resolve against user context.+ const activePreset = createMemo(() => {+ const view = activeListView();+ return view+ ? getViewPreset(view, activeTab(), {+ userId: userId(),+ isTeamAdmin: isTeamAdmin(),+ })+ : undefined;+ });+const soupParams = createMemo(() => {const sortId = soup.sort.active()[0]?.id ?? 'updated_at';@@ -680,12 +695,7 @@ export const SoupViewContextProvider: FlowComponent<// user's own touches — not to the sort method state, so the preset owns// them. Omitted when absent so the server default (desc) applies and// the query keys of every existing view stay byte-identical.- const preset = view- ? getViewPreset(view, activeTab(), {- userId: userId(),- isTeamAdmin: isTeamAdmin(),- })- : undefined;+ const preset = activePreset();const sortDirection = preset?.sortDirection;return {@@ -696,6 +706,17 @@ export const SoupViewContextProvider: FlowComponent<};});+ // A tab whose preset forces the notified server sort pins the client sort+ // to match, so the rows keep the page's order and the date headers bucket+ // on the same stamp; every other tab sorts by the sort state, so the+ // inbox's All and Reminders tabs stay on update recency even when a row+ // carries a notification stamp from a Signal page or a live delivery.+ const clientSort = createMemo((): SortConfig<SoupEntity>[] =>+ activePreset()?.sortMethod === 'notified_at'+ ? [SORT_CONFIGS.notified_at]+ : soup.sort.active()+ );+// Active deal-stage set (team-customized when present). Drives the// Customers view's stage grouping, stage filter and group labels.const dealStages = useDealStages();@@ -1101,7 +1122,7 @@ export const SoupViewContextProvider: FlowComponent<transformed = deduplicateEntities(next);- const sorts = soup.sort.active();+ const sorts = clientSort();if (sorts.length > 0 && !search.isSearching()) {transformed.sort((a, b) => {for (const sort of sorts) {@@ -1331,9 +1352,19 @@ export const SoupViewContextProvider: FlowComponent<>();const order: string[] = [];const now = new Date();+ // Under the inbox's notified sort a row belongs to the day it was last+ // notified about, not the day its content last changed — otherwise a+ // fresh comment on a stale task sits under "Yesterday" while sorting+ // as today's.+ const bucketOnNotification =+ clientSort()[0]?.id === SORT_CONFIGS.notified_at.id;for (const entity of all) {- const ts = entity.sortTs ?? entity.updatedAt ?? entity.createdAt;+ const ts =+ (bucketOnNotification ? entity.notifiedAt : undefined) ??+ entity.sortTs ??+ entity.updatedAt ??+ entity.createdAt;const bucket = dateBucket(ts, now);let group = buckets.get(bucket.key);@@ -1547,6 +1578,7 @@ export const SoupViewContextProvider: FlowComponent<setInboxFilter,activeTab,setActiveTab,+ clientSort,getPersistedActiveTab,viewMode,setViewMode,diff --git a/apps/web/src/features/next-soup/soup-view/soup-view.tsx b/apps/web/src/features/next-soup/soup-view/soup-view.tsxindex 9d0fd06596..05a31f9818 100644--- a/apps/web/src/features/next-soup/soup-view/soup-view.tsx+++ b/apps/web/src/features/next-soup/soup-view/soup-view.tsx@@ -404,10 +404,11 @@ export const SoupView = (props: SoupViewProps) => {: (persistedGroupBy ?? props.initialGroupBy);// The inbox exposes no sort control on either desktop (the toolbar- // hides SoupViewContextSort) or mobile, so its order is always- // updated_at. Ignore any sort persisted back when the control was- // reachable: honoring it would pin the list to an order the user can- // no longer change.+ // hides SoupViewContextSort) or mobile, so its order is fixed: update+ // recency, which the Signal and Noise presets override with the+ // notified order they serve (see `clientSort`). Ignore any sort+ // persisted back when the control was reachable: honoring it would pin+ // the list to an order the user can no longer change.let initialSortIds =contentId === 'inbox'? ['updated_at']@@ -806,6 +807,7 @@ const SoupViewListContent = (props: SoupViewListProps) => {isSearchServiceLoading,isLocalSearchSettling,activeTab,+ clientSort,fetchNextGroupPage,isFetchingGroupPage,} = useSoupView();@@ -1420,9 +1422,18 @@ const SoupViewListContent = (props: SoupViewListProps) => {>{(row, i) => {const timestamp = () => {+ const sort_ = clientSort();+ // The notified order shows when you were told,+ // ahead of the row's own recency stamp.+ if (+ sort_[0]?.id === 'notified_at' &&+ row.original.notifiedAt+ ) {+ return row.original.notifiedAt;+ }+if (row.original.sortTs) return row.original.sortTs;- const sort_ = soup.sort.active();if (!sort_.length) return;switch (sort_[0].id) {diff --git a/apps/web/src/features/notifications/channel-thread-root.ts b/apps/web/src/features/notifications/channel-thread-root.tsnew file mode 100644index 0000000000..ffab51c371--- /dev/null+++ b/apps/web/src/features/notifications/channel-thread-root.ts@@ -0,0 +1,25 @@+import { match } from 'ts-pattern';+import type { UnifiedNotification } from './types';++/**+ * The channel-thread row a notification belongs to, when it is thread-scoped:+ * a mention keys on its containing thread, or on the message itself for a+ * top-level mention, and a reply on its thread. This is the notification's+ * secondary event item on the server, which the soup's `notified_at` feed+ * keys those notifications on, so the inbox's thread rows and the feed agree.+ * Channel-level notifications (sends, invites) return `undefined`.+ */+export function channelThreadRootId(+ notification: UnifiedNotification+): string | undefined {+ return match(notification.notification_metadata)+ .with(+ { tag: 'channel_mention' },+ (metadata) => metadata.content.threadId ?? metadata.content.messageId+ )+ .with(+ { tag: 'channel_message_reply' },+ (metadata) => metadata.content.threadId+ )+ .otherwise(() => undefined);+}diff --git a/apps/web/src/features/soup/entity-notifications.ts b/apps/web/src/features/soup/entity-notifications.tsindex 8bb996b88d..37c168e502 100644--- a/apps/web/src/features/soup/entity-notifications.ts+++ b/apps/web/src/features/soup/entity-notifications.ts@@ -1,6 +1,7 @@import type { EntityData } from '@entity/types/entity';import type { WithNotification } from '@entity/types/notification';import { toNotificationEntity } from '@entity/utils/notification';+import { channelThreadRootId } from '@notifications/channel-thread-root';import type { NotificationSource } from '@notifications/notification-source';import {getAllNotificationsFromGroup,@@ -26,17 +27,7 @@ function channelThreadNotificationIds({ tag: 'channel_message_send' },(metadata) => metadata.content.messageId === threadId)- .with(- { tag: 'channel_mention' },- (metadata) =>- (metadata.content.threadId ?? metadata.content.messageId) ===- threadId- )- .with(- { tag: 'channel_message_reply' },- (metadata) => metadata.content.threadId === threadId- )- .otherwise(() => false);+ .otherwise(() => channelThreadRootId(notification) === threadId);if (belongsToThread) ids.add(notification.id);}return ids;diff --git a/apps/web/src/lib/queries/notification/tests/user-notifications.test.tsx b/apps/web/src/lib/queries/notification/tests/user-notifications.test.tsxindex d57721c86c..35025cae10 100644--- a/apps/web/src/lib/queries/notification/tests/user-notifications.test.tsx+++ b/apps/web/src/lib/queries/notification/tests/user-notifications.test.tsx@@ -74,6 +74,7 @@ vi.mock('@queries/soup/graphql/active-queries', () => ({}));vi.mock('@queries/soup/normalized-cache', () => ({+ bumpSoupEntityNotifiedAt: vi.fn(),optimisticUpdateSoupItemUpdatedAt: vi.fn(),hasSoupEntity: vi.fn(() => false),refetchSoupEntity: vi.fn(),@@ -84,6 +85,7 @@ vi.mock('@service-storage/graphql-soup', () => ({}));import {+ bumpSoupEntityNotifiedAt,hasSoupEntity,optimisticUpdateSoupItemUpdatedAt,refetchSoupEntity,@@ -707,6 +709,11 @@ describe('optimisticInsertNotification', () => {'document',newNotification.created_at);+ // The inbox's notified_at order moves the row up on arrival.+ expect(vi.mocked(bumpSoupEntityNotifiedAt)).toHaveBeenCalledWith(+ newNotification.entity_id,+ newNotification.created_at+ );expect(mockRefetchSoupEntity).not.toHaveBeenCalled();});@@ -724,6 +731,53 @@ describe('optimisticInsertNotification', () => {expect(notifications[1].id).toBe('n2');});+ it('stamps the thread row for a thread-scoped channel notification', () => {+ // The channel row is cached, the thread row is not.+ mockHasSoupEntity.mockImplementation((id) => id === 'channel-1');+ seedQueryCache([createMockNotificationPage([])]);++ const mention = createMockNotification({+ entity_type: 'channel',+ entity_id: 'channel-1',+ created_at: '2024-01-01T00:00:00.000Z',+ notification_event_type: 'channel_mention',+ notification_metadata: {+ tag: 'channel_mention',+ content: {+ messageContent: 'hey @you',+ messageId: 'msg-1',+ threadId: 'thread-1',+ },+ },+ } as unknown as Partial<UnifiedNotification>);++ optimisticInsertNotification(mention);++ // The channel row still tracks recency, but the notification belongs to+ // the thread row, which is what the inbox's notified_at order keys on.+ expect(mockOptimisticUpdateSoupItemUpdatedAt).toHaveBeenCalledWith(+ 'channel-1',+ 'channel',+ '2024-01-01T00:00:00.000Z'+ );+ expect(vi.mocked(bumpSoupEntityNotifiedAt)).toHaveBeenCalledWith(+ 'thread-1',+ '2024-01-01T00:00:00.000Z'+ );+ expect(vi.mocked(bumpSoupEntityNotifiedAt)).not.toHaveBeenCalledWith(+ 'channel-1',+ expect.anything()+ );+ expect(mockRefetchSoupEntity).toHaveBeenCalledWith(+ 'thread-1',+ 'channelThread'+ );+ expect(mockRefetchSoupEntity).not.toHaveBeenCalledWith(+ 'channel-1',+ expect.anything()+ );+ });+it('should bump the updatedAt of an already-cached email thread', () => {mockHasSoupEntity.mockReturnValue(true);seedQueryCache([createMockNotificationPage([])]);diff --git a/apps/web/src/lib/queries/notification/user-notifications.ts b/apps/web/src/lib/queries/notification/user-notifications.tsindex 0ae6a1bfca..8e1c3eb0e2 100644--- a/apps/web/src/lib/queries/notification/user-notifications.ts+++ b/apps/web/src/lib/queries/notification/user-notifications.ts@@ -1,9 +1,11 @@import { ENABLE_GRAPHQL_SOUP } from '@core/constant/featureFlags';import type { Maybe } from '@core/types';import { throwOnErr } from '@core/util/result';+import { channelThreadRootId } from '@notifications/channel-thread-root';import type { UnifiedNotification } from '@notifications/types';import { refreshActiveGraphqlSoupQueries } from '@queries/soup/graphql/active-queries';import {+ bumpSoupEntityNotifiedAt,hasSoupEntity,optimisticUpdateSoupItemUpdatedAt,refetchSoupEntity,@@ -1070,6 +1072,22 @@ export function optimisticInsertNotification(} else {refetchSoupEntity(notification.entity_id, soupTag);}++ // The inbox's notified_at order moves the notified row up right away+ // rather than on the next refetch of the page. A mention or thread reply+ // belongs to its channel-thread row — the row the soup feed keys it on —+ // so that is the row stamped (and fetched in, when it is not cached yet),+ // not the channel's.+ const threadRootId = channelThreadRootId(notification);+ if (notification.created_at) {+ bumpSoupEntityNotifiedAt(+ threadRootId ?? notification.entity_id,+ notification.created_at+ );+ }+ if (threadRootId && !hasSoupEntity(threadRootId)) {+ refetchSoupEntity(threadRootId, 'channelThread');+ }}// Cache is already updated via setQueriesData above. Mark as stale withoutdiff --git a/apps/web/src/lib/queries/soup/graphql/ast.ts b/apps/web/src/lib/queries/soup/graphql/ast.tsindex 8179767059..506ca3350e 100644--- a/apps/web/src/lib/queries/soup/graphql/ast.ts+++ b/apps/web/src/lib/queries/soup/graphql/ast.ts@@ -528,6 +528,8 @@ function mapSortMethod(return unsupported('sort_method frecency');case 'touched_by_me':return unsupported('sort_method touched_by_me');+ case 'notified_at':+ return unsupported('sort_method notified_at');case undefined:return undefined;}diff --git a/apps/web/src/lib/queries/soup/grouped/api.ts b/apps/web/src/lib/queries/soup/grouped/api.tsindex 205604ce71..9857b54fde 100644--- a/apps/web/src/lib/queries/soup/grouped/api.ts+++ b/apps/web/src/lib/queries/soup/grouped/api.ts@@ -10,21 +10,22 @@ import { type GroupByField, type GroupMeta, NOT_SET_GROUP_KEY } from './types';/** Sort methods the grouped soup endpoints accept. */export type GroupedSortMethod = Exclude<NonNullable<Params['sort_method']>,- 'frecency' | 'touched_by_me'+ 'frecency' | 'touched_by_me' | 'notified_at'>;/*** Maps a flat-feed sort method onto what the grouped endpoints support.- * Frecency and touched-by-me pages are built from their own data sources- * (relevance scores / the activity log) that the grouped queries cannot- * reproduce, so grouping such a view falls back to update recency within- * each group.+ * Frecency, touched-by-me and notified-at pages are built from their own+ * data sources (relevance scores / the activity log / notifications) that+ * the grouped queries cannot reproduce, so grouping such a view falls back+ * to update recency within each group.*/export function groupedSortMethod(sort: Params['sort_method']): GroupedSortMethod | undefined {if (sort == null) return undefined;- if (sort === 'frecency' || sort === 'touched_by_me') return 'updated_at';+ if (sort === 'frecency' || sort === 'touched_by_me' || sort === 'notified_at')+ return 'updated_at';return sort;}diff --git a/apps/web/src/lib/queries/soup/normalized-cache/index.ts b/apps/web/src/lib/queries/soup/normalized-cache/index.tsindex 13199b3f3a..04783bd785 100644--- a/apps/web/src/lib/queries/soup/normalized-cache/index.ts+++ b/apps/web/src/lib/queries/soup/normalized-cache/index.ts@@ -1,6 +1,7 @@export { groupedCacheVersion } from './grouped-operations';export { initSoupNormalizer } from './normalizer';export {+ bumpSoupEntityNotifiedAt,bumpSoupEntityTouchedAt,getSoupEntityById,hasSoupEntity,diff --git a/apps/web/src/lib/queries/soup/normalized-cache/notified-floor.test.ts b/apps/web/src/lib/queries/soup/normalized-cache/notified-floor.test.tsnew file mode 100644index 0000000000..4e9b3f797b--- /dev/null+++ b/apps/web/src/lib/queries/soup/normalized-cache/notified-floor.test.ts@@ -0,0 +1,49 @@+import { afterEach, describe, expect, it } from 'vitest';+import {+ clearNotifiedFloors,+ raiseNotifiedFloor,+ resolveNotifiedAt,+} from './notified-floor';++afterEach(() => {+ clearNotifiedFloors();+});++describe('notified floors', () => {+ it('passes the server value through when no floor exists', () => {+ expect(resolveNotifiedAt('e-1', '2026-09-02T00:00:00Z')).toBe(+ '2026-09-02T00:00:00Z'+ );+ expect(resolveNotifiedAt('e-1', null)).toBeNull();+ });++ it('wins over an older or absent server value', () => {+ raiseNotifiedFloor('e-1', '2026-09-02T17:00:00Z');++ // A notified page that was in flight when the notification landed+ // returns the previous stamp (or none) — the delivered one must hold.+ expect(resolveNotifiedAt('e-1', '2026-09-01T00:00:00Z')).toBe(+ '2026-09-02T17:00:00Z'+ );+ expect(resolveNotifiedAt('e-1', null)).toBe('2026-09-02T17:00:00Z');+ });++ it('keeps the newest delivery when notifications arrive out of order', () => {+ raiseNotifiedFloor('e-1', '2026-09-02T17:00:00Z');+ raiseNotifiedFloor('e-1', '2026-09-02T16:00:00Z');++ expect(resolveNotifiedAt('e-1', null)).toBe('2026-09-02T17:00:00Z');+ });++ it('clears once the server catches up', () => {+ raiseNotifiedFloor('e-1', '2026-09-02T17:00:00Z');++ expect(resolveNotifiedAt('e-1', '2026-09-02T17:00:00.000123Z')).toBe(+ '2026-09-02T17:00:00.000123Z'+ );+ // The floor is gone: subsequent reads are pass-through.+ expect(resolveNotifiedAt('e-1', '2026-09-01T00:00:00Z')).toBe(+ '2026-09-01T00:00:00Z'+ );+ });+});diff --git a/apps/web/src/lib/queries/soup/normalized-cache/notified-floor.ts b/apps/web/src/lib/queries/soup/normalized-cache/notified-floor.tsnew file mode 100644index 0000000000..02c553d47b--- /dev/null+++ b/apps/web/src/lib/queries/soup/normalized-cache/notified-floor.ts@@ -0,0 +1,34 @@+/**+ * Optimistic latest-notification stamps ("floors"), keyed by entity id.+ *+ * `notified_at` is when the viewer was last notified about an entity. A+ * notification arriving over the websocket stamps its entity right away so+ * the inbox re-sorts; a `notified_at` page that was already in flight when+ * it arrived returns the OLD stamp — see `stamp-floors.ts` for how the floor+ * guards against it.+ */++import { createStampFloors } from './stamp-floors';++const notifiedFloors = createStampFloors();++/** Record a delivered notification's time as the entity's floor. */+export function raiseNotifiedFloor(entityId: string, notifiedAt: string): void {+ notifiedFloors.raise(entityId, notifiedAt);+}++/**+ * Resolve the effective notified_at for an entity: the server value unless a+ * newer floor exists. Clears the floor once the server catches up.+ */+export function resolveNotifiedAt(+ entityId: string,+ serverNotifiedAt: string | null | undefined+): string | null | undefined {+ return notifiedFloors.resolve(entityId, serverNotifiedAt);+}++/** Test-only: reset all floors between cases. */+export function clearNotifiedFloors(): void {+ notifiedFloors.clear();+}diff --git a/apps/web/src/lib/queries/soup/normalized-cache/operations.ts b/apps/web/src/lib/queries/soup/normalized-cache/operations.tsindex 37bb8cfc14..41632ffd83 100644--- a/apps/web/src/lib/queries/soup/normalized-cache/operations.ts+++ b/apps/web/src/lib/queries/soup/normalized-cache/operations.ts@@ -33,6 +33,7 @@ import {soupNormKey,stripSoupNormPrefix,} from './normalizer';+import { raiseNotifiedFloor } from './notified-floor';import { ownTouchStamp } from './own-touch';import type {SoupEntityPartial,@@ -174,6 +175,51 @@ export function bumpSoupEntityTouchedAt(} as SoupEntityPartial);}+/**+ * Stamp a freshly delivered notification's time on its cached entity so the+ * inbox's notified_at order moves the row up (and re-buckets its date header)+ * without waiting for a refetch. Newest wins: an out-of-order delivery never+ * moves a row back down. The stamp is also recorded as a floor (see+ * `notified-floor.ts`) so a notified page that was in flight when the+ * notification landed cannot overwrite it with the previous stamp; the floor+ * clears once the server's value catches up. Non-notified responses omit the+ * field, so the field-merge never clears the stamp either.+ */+export function bumpSoupEntityNotifiedAt(+ entityId: string,+ notifiedAt: string+): SoupTransaction | undefined {+ raiseNotifiedFloor(entityId, notifiedAt);+ const current = getSoupEntityById(entityId);+ if (!current) return undefined;+ const existing = current.notified_at ?? undefined;+ if (!shouldUpdateOptimisticTimestamp(existing, notifiedAt)) return undefined;+ const frecency_score = current.frecency_score;++ if (current.tag === 'channel') {+ return optimisticUpdateSoupEntity({+ tag: 'channel',+ data: { channel: { id: current.data.channel.id } },+ frecency_score,+ notified_at: notifiedAt,+ });+ }+ if (current.tag === 'call') {+ return optimisticUpdateSoupEntity({+ tag: 'call',+ data: { callId: current.data.callId },+ frecency_score,+ notified_at: notifiedAt,+ });+ }+ return optimisticUpdateSoupEntity({+ tag: current.tag,+ data: { id: current.data.id },+ frecency_score,+ notified_at: notifiedAt,+ } as SoupEntityPartial);+}+/*** Mark stale only the soup queries containing a specific entity.* Prefer this over `invalidateAllSoup` when you know the affected entity ID.diff --git a/apps/web/src/lib/queries/soup/normalized-cache/own-touch.ts b/apps/web/src/lib/queries/soup/normalized-cache/own-touch.tsindex 0816d90658..1d47b54250 100644--- a/apps/web/src/lib/queries/soup/normalized-cache/own-touch.ts+++ b/apps/web/src/lib/queries/soup/normalized-cache/own-touch.ts@@ -4,10 +4,7 @@* `touched_at` is the viewer's latest own mutation as recorded by the* activity log, which lands through an async consumer. A touched-mode* refetch triggered right after a mutation can therefore return the OLD- * server value, and the normalized cache's field-merge would let it- * overwrite a fresher optimistic stamp. Instead of racing the consumer,- * stamps are recorded here and reads resolve to max(server, floor); an- * entry clears itself the moment the server value catches up.+ * server value — see `stamp-floors.ts` for how the floor guards against it.** Only mutations whose server side records an activity (the domain* `ActivitySource` impls: create, rename, file-type change, project move,@@ -15,13 +12,11 @@* unattributed action would reorder the Recent feed only to snap back once* the server truth arrives. Doc content edits are NOT attributed yet* (`SyncContentUpdated` carries no actor), so typing must not stamp.- *- * This module is import-free on purpose: it sits below the normalized- * cache, the entity mappers, and the list-view gates, which would- * otherwise form an import cycle.*/-const ownTouchFloors = new Map<string, string>();+import { createStampFloors } from './stamp-floors';++const ownTouchFloors = createStampFloors();/*** Record and return a fresh own-touch stamp for the entity. Every optimistic@@ -29,7 +24,7 @@ const ownTouchFloors = new Map<string, string>();*/export function ownTouchStamp(entityId: string): string {const stamp = new Date().toISOString();- ownTouchFloors.set(entityId, stamp);+ ownTouchFloors.raise(entityId, stamp);return stamp;}@@ -42,16 +37,7 @@ export function resolveOwnTouch(entityId: string,serverTouchedAt: string | null | undefined): string | null | undefined {- const floor = ownTouchFloors.get(entityId);- if (floor === undefined) return serverTouchedAt;- if (- serverTouchedAt &&- new Date(serverTouchedAt).getTime() >= new Date(floor).getTime()- ) {- ownTouchFloors.delete(entityId);- return serverTouchedAt;- }- return floor;+ return ownTouchFloors.resolve(entityId, serverTouchedAt);}/** Whether the viewer has an outstanding optimistic touch for the entity. */diff --git a/apps/web/src/lib/queries/soup/normalized-cache/stamp-floors.ts b/apps/web/src/lib/queries/soup/normalized-cache/stamp-floors.tsnew file mode 100644index 0000000000..865c6e087f--- /dev/null+++ b/apps/web/src/lib/queries/soup/normalized-cache/stamp-floors.ts@@ -0,0 +1,60 @@+/**+ * Optimistic timestamp floors keyed by entity id.+ *+ * A stamp the client already knows about (its own mutation, a notification+ * that just arrived) can be overtaken by a refetch that started before the+ * server recorded it and so returns the OLD value; the normalized cache's+ * field-merge would let that stale page overwrite the fresher stamp. Instead+ * of racing the server, stamps are recorded here and reads resolve to+ * max(server, floor); an entry clears itself the moment the server value+ * catches up, so steady-state reads are pass-through.+ *+ * Import-free on purpose: floors sit below the normalized cache, the entity+ * mappers, and the list-view gates, which would otherwise form an import+ * cycle.+ */+export function createStampFloors() {+ const floors = new Map<string, string>();++ return {+ /** Record `stamp` as the entity's floor unless a newer one is held. */+ raise(entityId: string, stamp: string): void {+ const current = floors.get(entityId);+ if (+ current === undefined ||+ new Date(stamp).getTime() > new Date(current).getTime()+ ) {+ floors.set(entityId, stamp);+ }+ },++ /**+ * The server value unless a newer floor exists. Clears the floor once the+ * server catches up.+ */+ resolve(+ entityId: string,+ serverValue: string | null | undefined+ ): string | null | undefined {+ const floor = floors.get(entityId);+ if (floor === undefined) return serverValue;+ if (+ serverValue &&+ new Date(serverValue).getTime() >= new Date(floor).getTime()+ ) {+ floors.delete(entityId);+ return serverValue;+ }+ return floor;+ },++ has(entityId: string): boolean {+ return floors.has(entityId);+ },++ /** Test-only: reset all floors between cases. */+ clear(): void {+ floors.clear();+ },+ };+}diff --git a/apps/web/src/lib/queries/soup/normalized-cache/types.ts b/apps/web/src/lib/queries/soup/normalized-cache/types.tsindex 05a6faff7f..e5cc737c14 100644--- a/apps/web/src/lib/queries/soup/normalized-cache/types.ts+++ b/apps/web/src/lib/queries/soup/normalized-cache/types.ts@@ -31,4 +31,6 @@ export type SoupEntityPartial<T extends SoupEntityTag = SoupEntityTag> = {frecency_score: number;/** Optimistic own-touch stamp; only `bumpSoupEntityTouchedAt` sets it. */touched_at?: string | null;+ /** Latest-notification stamp; only `bumpSoupEntityNotifiedAt` sets it. */+ notified_at?: string | null;};diff --git a/apps/web/src/lib/queries/soup/transform-utils.ts b/apps/web/src/lib/queries/soup/transform-utils.tsindex 96ce47748a..beaf951879 100644--- a/apps/web/src/lib/queries/soup/transform-utils.ts+++ b/apps/web/src/lib/queries/soup/transform-utils.ts@@ -31,6 +31,7 @@ import type {SearchData,WithSearch,} from '@entity';+import { resolveNotifiedAt } from '@queries/soup/normalized-cache/notified-floor';import { resolveOwnTouch } from '@queries/soup/normalized-cache/own-touch';import type {CallRecordSearchResult,@@ -1020,8 +1021,13 @@ export const mapApiSoupItemToEntity = (// activity consumer can't move a freshly-touched row back down.const touchedAt = resolveOwnTouch(entity.id, item.touched_at ?? null);const touched = touchedAt ? { ...entity, touchedAt } : entity;+ // Likewise only notified_at pages carry this one; the inbox sorts and+ // date-buckets on it. Resolved through the notified floor so a page that+ // was in flight when a notification landed can't move the row back down.+ const notifiedAt = resolveNotifiedAt(entity.id, item.notified_at ?? null);+ const notified = notifiedAt ? { ...touched, notifiedAt } : touched;- return withRawNotifications(touched, item);+ return withRawNotifications(notified, item);};const toCalendarEventTime = (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 548fd11bf0..a1aa849691 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@@ -19,7 +19,8 @@ export type GetItemsSoupParams = {limit?: number;/*** Sort method. Options are viewed_at, created_at, updated_at,-viewed_updated, frecency, touched_by_me. Defaults to viewed_at.+viewed_updated, frecency, touched_by_me, notified_at. Defaults to+viewed_at.*/sort_method?: SoupApiSort;/**diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/index.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/index.tsindex 4cdeeff35f..1e6e23a9a2 100644--- a/apps/web/src/lib/service-clients/service-storage/generated/schemas/index.ts+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/index.ts@@ -1027,6 +1027,7 @@ export * from './shortIdResponse';export * from './simpleMention';export * from './soupApiItem';export * from './soupApiItemAllOf';+export * from './soupApiItemAllOfNotifiedAt';export * from './soupApiItemAllOfTouchedAt';export * from './soupApiSort';export * from './soupApiSortDirection';diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/soupApiItemAllOf.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/soupApiItemAllOf.tsindex e3c1ba1d47..14f7074152 100644--- a/apps/web/src/lib/service-clients/service-storage/generated/schemas/soupApiItemAllOf.ts+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/soupApiItemAllOf.ts@@ -4,12 +4,17 @@* document_storage_service* OpenAPI spec version: 0.1.0*/+import type { SoupApiItemAllOfNotifiedAt } from './soupApiItemAllOfNotifiedAt';import type { SoupApiItemAllOfTouchedAt } from './soupApiItemAllOfTouchedAt';export type SoupApiItemAllOf = {frecency_score: number;/** Whether the requesting user has favorited this entity. */is_favorited: boolean;+ /** When the caller was last notified about this entity, present only+when the page was ordered by `notified_at`. Clients keep the notified+feed ordered and date-bucketed on this value. */+ notified_at?: SoupApiItemAllOfNotifiedAt;/** The caller's latest own mutation of this entity, present only when thepage was ordered by `touched_by_me`. Clients keep the touched feedordered on this value, so it can be bumped optimistically. */diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/soupApiItemAllOfNotifiedAt.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/soupApiItemAllOfNotifiedAt.tsnew file mode 100644index 0000000000..a4b829d2f5--- /dev/null+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/soupApiItemAllOfNotifiedAt.ts@@ -0,0 +1,13 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * document_storage_service+ * OpenAPI spec version: 0.1.0+ */++/**+ * When the caller was last notified about this entity, present only+when the page was ordered by `notified_at`. Clients keep the notified+feed ordered and date-bucketed on this value.+ */+export type SoupApiItemAllOfNotifiedAt = string | null;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 1a3bb77609..93a691888f 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@@ -18,4 +18,5 @@ export const SoupApiSort = {viewed_updated: 'viewed_updated',frecency: 'frecency',touched_by_me: 'touched_by_me',+ notified_at: 'notified_at',} 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 d24c4b0521..4d10bc921d 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@@ -8712,10 +8712,11 @@ export const getItemsSoupQueryParams = zod.object({'viewed_updated','frecency','touched_by_me',+ 'notified_at',]).optional().describe(- 'Sort method. Options are viewed_at, created_at, updated_at,\nviewed_updated, frecency, touched_by_me. Defaults to viewed_at.'+ 'Sort method. Options are viewed_at, created_at, updated_at,\nviewed_updated, frecency, touched_by_me, notified_at. Defaults to\nviewed_at.'),sort_direction: zod.enum(['asc', 'desc'])@@ -11686,6 +11687,12 @@ export const getItemsSoupResponse = zod.describe('Whether the requesting user has favorited this entity.'),+ notified_at: zod.iso+ .datetime({})+ .nullish()+ .describe(+ 'When the caller was last notified about this entity, present only\nwhen the page was ordered by `notified_at`. Clients keep the notified\nfeed ordered and date-bucketed on this value.'+ ),touched_at: zod.iso.datetime({}).nullish()@@ -12356,6 +12363,7 @@ export const postItemsSoupBody = zod'viewed_updated','frecency','touched_by_me',+ 'notified_at',]).describe('Sort options accepted by non-grouped soup API endpoints.'@@ -15335,6 +15343,12 @@ export const postItemsSoupResponse = zod.describe('Whether the requesting user has favorited this entity.'),+ notified_at: zod.iso+ .datetime({})+ .nullish()+ .describe(+ 'When the caller was last notified about this entity, present only\nwhen the page was ordered by `notified_at`. Clients keep the notified\nfeed ordered and date-bucketed on this value.'+ ),touched_at: zod.iso.datetime({}).nullish()@@ -15469,6 +15483,7 @@ export const postItemsSoupAstBody = zod'viewed_updated','frecency','touched_by_me',+ 'notified_at',]).describe('Sort options accepted by non-grouped soup API endpoints.'@@ -18450,6 +18465,12 @@ export const postItemsSoupAstResponse = zod.describe('Whether the requesting user has favorited this entity.'),+ notified_at: zod.iso+ .datetime({})+ .nullish()+ .describe(+ 'When the caller was last notified about this entity, present only\nwhen the page was ordered by `notified_at`. Clients keep the notified\nfeed ordered and date-bucketed on this value.'+ ),touched_at: zod.iso.datetime({}).nullish()@@ -21916,6 +21937,12 @@ export const postItemsSoupAstGroupedResponse = zod.describe('Whether the requesting user has favorited this entity.'),+ notified_at: zod.iso+ .datetime({})+ .nullish()+ .describe(+ 'When the caller was last notified about this entity, present only\nwhen the page was ordered by `notified_at`. Clients keep the notified\nfeed ordered and date-bucketed on this value.'+ ),touched_at: zod.iso.datetime({}).nullish()@@ -25032,6 +25059,12 @@ export const postItemsSoupAstGroupedResponse = zod.describe('Whether the requesting user has favorited this entity.'),+ notified_at: zod.iso+ .datetime({})+ .nullish()+ .describe(+ 'When the caller was last notified about this entity, present only\nwhen the page was ordered by `notified_at`. Clients keep the notified\nfeed ordered and date-bucketed on this value.'+ ),touched_at: zod.iso.datetime({}).nullish()diff --git a/apps/web/src/lib/service-clients/service-storage/openapi.json b/apps/web/src/lib/service-clients/service-storage/openapi.jsonindex fc69b1898b..05fcd8a2db 100644--- a/apps/web/src/lib/service-clients/service-storage/openapi.json+++ b/apps/web/src/lib/service-clients/service-storage/openapi.json@@ -9546,7 +9546,7 @@{"name": "sort_method","in": "query",- "description": "Sort method. Options are viewed_at, created_at, updated_at,\nviewed_updated, frecency, touched_by_me. Defaults to viewed_at.",+ "description": "Sort method. Options are viewed_at, created_at, updated_at,\nviewed_updated, frecency, touched_by_me, notified_at. Defaults to\nviewed_at.","required": false,"schema": {"$ref": "#/components/schemas/SoupApiSort"@@ -21972,7 +21972,7 @@},{"$ref": "#/components/schemas/SoupApiSort",- "description": "Sort method. Options are viewed_at, created_at, updated_at,\nviewed_updated, frecency, touched_by_me. Defaults to viewed_at."+ "description": "Sort method. Options are viewed_at, created_at, updated_at,\nviewed_updated, frecency, touched_by_me, notified_at. Defaults to\nviewed_at."}]}@@ -24033,6 +24033,11 @@"type": "boolean","description": "Whether the requesting user has favorited this entity."},+ "notified_at": {+ "type": ["string", "null"],+ "format": "date-time",+ "description": "When the caller was last notified about this entity, present only\nwhen the page was ordered by `notified_at`. Clients keep the notified\nfeed ordered and date-bucketed on this value."+ },"touched_at": {"type": ["string", "null"],"format": "date-time",@@ -24052,7 +24057,8 @@"updated_at","viewed_updated","frecency",- "touched_by_me"+ "touched_by_me",+ "notified_at"]},"SoupApiSortDirection": {diff --git a/crates/macro_db_client/migrations/20260902161056_user_notification_user_created_active_idx.sql b/crates/macro_db_client/migrations/20260902161056_user_notification_user_created_active_idx.sqlnew file mode 100644index 0000000000..dfb06e0175--- /dev/null+++ b/crates/macro_db_client/migrations/20260902161056_user_notification_user_created_active_idx.sql@@ -0,0 +1,9 @@+-- no-transaction+-- Ordered scan for the soup notified-at candidate query: a user's live+-- notifications newest first, keyset-paginated on created_at.+-- Must stay a single statement: sqlx sends no-transaction migrations as+-- one batch, and CONCURRENTLY cannot run inside a transaction.+-- Rollback: DROP INDEX CONCURRENTLY IF EXISTS idx_user_notification_user_created_active;+CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_user_notification_user_created_active+ ON user_notification (user_id, created_at DESC)+ WHERE deleted_at IS NULL;diff --git a/crates/models_pagination/src/lib.rs b/crates/models_pagination/src/lib.rsindex 07a79ef2a7..2cd97d17da 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, TouchedByMe};+pub use sort::{CreatedAt, Frecency, FrecencyValue, NotifiedAt, SimpleSortMethod, TouchedByMe};diff --git a/crates/models_pagination/src/sort.rs b/crates/models_pagination/src/sort.rsindex c997c28c59..c9a8c37c83 100644--- a/crates/models_pagination/src/sort.rs+++ b/crates/models_pagination/src/sort.rs@@ -3,6 +3,9 @@ use chrono::{DateTime, Utc};use ordered_float::OrderedFloat;use serde::{Deserialize, Serialize};+#[cfg(test)]+mod test;+#[derive(Debug, Clone, Copy, Serialize, Deserialize)]#[serde(rename_all = "snake_case")]/// common types of sorts based on timestamps@@ -49,6 +52,42 @@ impl Sortable for TouchedByMe {type Value = DateTime<Utc>;}+/// Sort by when the requesting user was last notified about each entity, as+/// recorded in `user_notification`. Doubles as a filter: entities the user+/// was never notified about have no value to sort on and are absent from the+/// page.+///+/// Serializes as the string `"notified_at"` rather than `null`: its cursor+/// value is a timestamp, exactly like [`TouchedByMe`]'s, so the marker is+/// the only thing that tells the two cursors apart.+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]+#[serde(into = "String", try_from = "String")]+pub struct NotifiedAt;++const NOTIFIED_AT_TAG: &str = "notified_at";++impl From<NotifiedAt> for String {+ fn from(_: NotifiedAt) -> Self {+ NOTIFIED_AT_TAG.to_string()+ }+}++impl TryFrom<String> for NotifiedAt {+ type Error = String;++ fn try_from(value: String) -> Result<Self, Self::Error> {+ if value == NOTIFIED_AT_TAG {+ Ok(NotifiedAt)+ } else {+ Err(format!("expected {NOTIFIED_AT_TAG:?}, got {value:?}"))+ }+ }+}++impl Sortable for NotifiedAt {+ type Value = DateTime<Utc>;+}+/// the possible values of the cursor when sorting by frecency#[derive(Debug, Serialize, Deserialize, PartialEq)]pub enum FrecencyValue {diff --git a/crates/models_pagination/src/sort/test.rs b/crates/models_pagination/src/sort/test.rsnew file mode 100644index 0000000000..ada1d05977--- /dev/null+++ b/crates/models_pagination/src/sort/test.rs@@ -0,0 +1,38 @@+use super::*;+use crate::CursorVal;++#[test]+fn notified_at_marker_round_trips_as_a_string() {+ let json = serde_json::to_string(&NotifiedAt).unwrap();+ assert_eq!(json, r#""notified_at""#);+ let parsed: NotifiedAt = serde_json::from_str(&json).unwrap();+ assert_eq!(parsed, NotifiedAt);+}++#[test]+fn notified_at_marker_rejects_other_tags() {+ assert!(serde_json::from_str::<NotifiedAt>(r#""touched_by_me""#).is_err());+ assert!(serde_json::from_str::<NotifiedAt>("null").is_err());+}++/// Touched and notified cursors carry the same value type, so the marker is+/// what keeps a cursor extractor from decoding one as the other.+#[test]+fn touched_and_notified_cursor_values_do_not_cross_decode() {+ let ts = DateTime::<Utc>::UNIX_EPOCH;+ let touched = serde_json::to_string(&CursorVal {+ sort_type: TouchedByMe,+ last_val: ts,+ })+ .unwrap();+ let notified = serde_json::to_string(&CursorVal {+ sort_type: NotifiedAt,+ last_val: ts,+ })+ .unwrap();++ assert!(serde_json::from_str::<CursorVal<TouchedByMe>>(&touched).is_ok());+ assert!(serde_json::from_str::<CursorVal<NotifiedAt>>(&touched).is_err());+ assert!(serde_json::from_str::<CursorVal<NotifiedAt>>(¬ified).is_ok());+ assert!(serde_json::from_str::<CursorVal<TouchedByMe>>(¬ified).is_err());+}diff --git a/crates/soup/fixtures/notified_at.sql b/crates/soup/fixtures/notified_at.sqlnew file mode 100644index 0000000000..c9cac6b2db--- /dev/null+++ b/crates/soup/fixtures/notified_at.sql@@ -0,0 +1,209 @@+-- Fixture for the soup notified-at candidate query.+--+-- user-1 is the subject. Their notifications span every entity type the feed+-- surfaces, plus every trap the query must dodge: deleted notification rows,+-- other users' notifications, deleted and inaccessible entities, left+-- channels, other users' inboxes/events/foreign entities/reminders,+-- notification types the feed does not roll up, and a malformed entity id.+--+-- user-1's expected feed, latest notification first (T = minute past 10:00):+-- T20 event-E3 (user-2's event on an inbox delegated to user-1)+-- T19 pr-F3 (foreign entity stored for a team user-1 belongs to)+-- T9 doc-A (notified at T1 and T9 — group-max wins; T9 is not done)+-- T8 chat-A (marked done — done rows still set the sort key)+-- T7 project-A+-- T6 thread-M (a mention in channel-X's thread M: primary channel,+-- secondary channel_message — keyed on the thread root)+-- T5 event-E1 (owned calendar event)+-- T4 thread-Z (user-1's own inbox link)+-- T3 pr-F1 (foreign entity stored for user-1)+-- T2 reminder-R1+-- T0 channel-X (a channel-level notification with no secondary item)++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;++INSERT INTO public.team ("id", "name", "owner_id")+VALUES ('eeeeeeee-1111-1111-1111-111111111111', 'Team T', 'macro|[email protected]');++INSERT INTO public.team_user ("user_id", "team_id", "team_role")+VALUES ('macro|[email protected]', 'eeeeeeee-1111-1111-1111-111111111111', 'member');++---------------------------------------------------+-- 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');++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),+ -- Notified at T12, but the notification row is soft-deleted.+ ('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),+ -- Notified at T11 but the document is 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'),+ -- Notified at T10 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);++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');++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_messages ("id", "channel_id", "thread_id", "sender_id", "content", "created_at", "updated_at")+-- thread-M is a root message in channel-X that user-1 was mentioned in.+VALUES ('99999999-aaaa-aaaa-aaaa-aaaaaaaaaaaa', '33333333-aaaa-aaaa-aaaa-aaaaaaaaaaaa', NULL, 'macro|[email protected]', 'hey @user-1', '2024-06-01 10:06:00+00', '2024-06-01 10:06: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),+ -- Notified at T13 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'),+ -- user-2's second inbox, delegated to user-1 below.+ ('55555555-cccc-cccc-cccc-cccccccccccc', 'macro|[email protected]', 'fa-user-2-shared', '[email protected]', 'GMAIL');++INSERT INTO public.macro_user_links ("primary_macro_id", "child_macro_id", "link_id")+VALUES ('macro|[email protected]', 'macro|[email protected]', '55555555-cccc-cccc-cccc-cccccccccccc');++INSERT INTO public.email_threads ("id", "link_id", "inbox_visible", "is_signal", "latest_inbound_message_ts", "created_at", "updated_at")+-- thread-Z is a signal thread, so an importance=false pre-filter drops it.+VALUES ('44444444-aaaa-aaaa-aaaa-aaaaaaaaaaaa', '55555555-aaaa-aaaa-aaaa-aaaaaaaaaaaa', TRUE, TRUE, '2024-06-01 09:00:00+00', '2024-06-01 09:00:00+00', '2024-06-01 09:00:00+00'),+ -- Notified at T14 but the thread lives in user-2's inbox: must never surface.+ ('44444444-bbbb-bbbb-bbbb-bbbbbbbbbbbb', '55555555-bbbb-bbbb-bbbb-bbbbbbbbbbbb', TRUE, 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.calendar_events ("id", "owner_id", "source_link_id", "ical_uid", "title", "starts_at", "ends_at", "canonical_source_kind", "canonical_source_updated_at")+VALUES ('66666666-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'macro|[email protected]', '55555555-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'e1@test', 'Event E1', '2024-06-02 10:00:00+00', '2024-06-02 11:00:00+00', 'google', '2024-06-01 09:00:00+00'),+ -- Notified at T15 but owned by user-2 with no delegation: must never surface.+ ('66666666-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'macro|[email protected]', '55555555-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'e2@test', 'Event E2', '2024-06-02 10:00:00+00', '2024-06-02 11:00:00+00', 'google', '2024-06-01 09:00:00+00'),+ -- user-2's event on the inbox delegated to user-1: visible through the delegation.+ ('66666666-cccc-cccc-cccc-cccccccccccc', 'macro|[email protected]', '55555555-cccc-cccc-cccc-cccccccccccc', 'e3@test', 'Event E3', '2024-06-02 12:00:00+00', '2024-06-02 13:00:00+00', 'google', '2024-06-01 09:00:00+00');++INSERT INTO public.foreign_entity ("id", "foreign_entity_id", "foreign_entity_source", "metadata", "stored_for_id", "stored_for_auth_entity", "created_at", "updated_at")+VALUES ('77777777-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'org/repo#1', 'github_pull_request', '{}', 'macro|[email protected]', 'user', '2024-06-01 09:00:00+00', '2024-06-01 09:00:00+00'),+ -- Notified at T16 but stored for user-2: must never surface.+ ('77777777-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'org/repo#2', 'github_pull_request', '{}', 'macro|[email protected]', 'user', '2024-06-01 09:00:00+00', '2024-06-01 09:00:00+00'),+ -- Stored for Team T, which user-1 belongs to: visible through the team source.+ ('77777777-cccc-cccc-cccc-cccccccccccc', 'org/repo#3', 'github_pull_request', '{}', 'eeeeeeee-1111-1111-1111-111111111111', 'team', '2024-06-01 09:00:00+00', '2024-06-01 09:00:00+00');++INSERT INTO public.reminder ("id", "user_id", "description", "remind_at", "next_run_at")+VALUES ('88888888-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'macro|[email protected]', 'Reminder R1', '2024-06-01 10:02:00+00', '2024-06-01 10:02:00+00'),+ -- Notified at T17 but it is user-2's reminder: must never surface.+ ('88888888-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'macro|[email protected]', 'Reminder R2', '2024-06-01 10:17:00+00', '2024-06-01 10:17: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),+ ('22222222-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'chat', 'macro|[email protected]', 'user', 'owner', NULL);++---------------------------------------------------+-- NOTIFICATIONS+---------------------------------------------------++INSERT INTO public.notification ("id", "notification_event_type", "event_item_id", "event_item_type", "service_sender", "created_at", "metadata", "sender_id", "secondary_event_item_id", "secondary_event_item_type")+VALUES+-- doc-A: mentioned at T1 then commented on at T9 — the newest keys the feed.+('0190a000-0000-7000-8000-000000000001', 'document_mention', '11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'document', 'test', '2024-06-01 10:01:00', '{}', 'macro|[email protected]', NULL, NULL),+('0190a000-0000-7000-8000-000000000009', 'document_comment', '11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'document', 'test', '2024-06-01 10:09:00', '{}', 'macro|[email protected]', NULL, NULL),+-- doc-A for user-2 at T30: another recipient's row must not move user-1's entry.+('0190a000-0000-7000-8000-000000000030', 'document_comment', '11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'document', 'test', '2024-06-01 10:30:00', '{}', 'macro|[email protected]', NULL, NULL),+-- chat-A at T8 (user-1 marked it done).+('0190a000-0000-7000-8000-000000000008', 'chat_complete', '22222222-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'chat', 'test', '2024-06-01 10:08:00', '{}', NULL, NULL, NULL),+-- project-A at T7.+('0190a000-0000-7000-8000-000000000007', 'project_shared', 'aaaaaaaa-ffff-ffff-ffff-ffffffffffff', 'project', 'test', '2024-06-01 10:07:00', '{}', 'macro|[email protected]', NULL, NULL),+-- thread-M at T6: a mention, primary channel-X + secondary thread root.+('0190a000-0000-7000-8000-000000000006', 'channel_mention', '33333333-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'channel', 'test', '2024-06-01 10:06:00', '{"messageId": "99999999-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}', 'macro|[email protected]', '99999999-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'channel_message'),+-- channel-X at T0: a channel-level notification with no secondary item.+('0190a000-0000-7000-8000-000000000000', 'channel_invite', '33333333-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'channel', 'test', '2024-06-01 10:00:00', '{}', 'macro|[email protected]', NULL, NULL),+-- event-E1 at T5 (a fired alarm).+('0190a000-0000-7000-8000-000000000005', 'calendar_event_reminder', '66666666-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'calendar_event', 'test', '2024-06-01 10:05:00', '{}', NULL, NULL, NULL),+-- thread-Z at T4.+('0190a000-0000-7000-8000-000000000004', 'email_received', '44444444-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'email_thread', 'test', '2024-06-01 10:04:00', '{}', NULL, NULL, NULL),+-- pr-F1 at T3.+('0190a000-0000-7000-8000-000000000003', 'github_pull_request_event', '77777777-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'foreign_entity', 'test', '2024-06-01 10:03:00', '{}', NULL, NULL, NULL),+-- reminder-R1 at T2.+('0190a000-0000-7000-8000-000000000002', 'reminder_fired', '88888888-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'reminder', 'test', '2024-06-01 10:02:00', '{}', NULL, NULL, NULL),+-- Isolated doc at T10: user-1 has no access row.+('0190a000-0000-7000-8000-000000000010', 'document_mention', '11111111-9999-9999-9999-999999999999', 'document', 'test', '2024-06-01 10:10:00', '{}', 'macro|[email protected]', NULL, NULL),+-- Deleted doc at T11: the row is soft-deleted.+('0190a000-0000-7000-8000-000000000011', 'document_mention', '11111111-dead-dead-dead-dddddddddddd', 'document', 'test', '2024-06-01 10:11:00', '{}', 'macro|[email protected]', NULL, NULL),+-- doc-B at T12: user-1's notification row is deleted.+('0190a000-0000-7000-8000-000000000012', 'document_mention', '11111111-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'document', 'test', '2024-06-01 10:12:00', '{}', 'macro|[email protected]', NULL, NULL),+-- channel-Y at T13: user-1 has left the channel.+('0190a000-0000-7000-8000-000000000013', 'channel_message', '33333333-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'channel', 'test', '2024-06-01 10:13:00', '{}', 'macro|[email protected]', NULL, NULL),+-- thread-W at T14: lives in user-2's inbox.+('0190a000-0000-7000-8000-000000000014', 'email_received', '44444444-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'email_thread', 'test', '2024-06-01 10:14:00', '{}', NULL, NULL, NULL),+-- event-E2 at T15: owned by user-2.+('0190a000-0000-7000-8000-000000000015', 'calendar_event_reminder', '66666666-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'calendar_event', 'test', '2024-06-01 10:15:00', '{}', NULL, NULL, NULL),+-- pr-F2 at T16: stored for user-2.+('0190a000-0000-7000-8000-000000000016', 'github_pull_request_event', '77777777-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'foreign_entity', 'test', '2024-06-01 10:16:00', '{}', NULL, NULL, NULL),+-- reminder-R2 at T17: user-2's reminder.+('0190a000-0000-7000-8000-000000000017', 'reminder_fired', '88888888-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'reminder', 'test', '2024-06-01 10:17:00', '{}', NULL, NULL, NULL),+-- A call notification at T18: calls are not part of the feed.+('0190a000-0000-7000-8000-000000000018', 'call_started', '33333333-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'call', 'test', '2024-06-01 10:18:00', '{}', NULL, NULL, NULL),+-- pr-F3 at T19: stored for user-1's team.+('0190a000-0000-7000-8000-000000000019', 'github_pull_request_event', '77777777-cccc-cccc-cccc-cccccccccccc', 'foreign_entity', 'test', '2024-06-01 10:19:00', '{}', NULL, NULL, NULL),+-- event-E3 at T20: user-2's event on the inbox delegated to user-1.+('0190a000-0000-7000-8000-000000000020', 'calendar_event_reminder', '66666666-cccc-cccc-cccc-cccccccccccc', 'calendar_event', 'test', '2024-06-01 10:20:00', '{}', NULL, NULL, NULL),+-- A channel notification at T21 whose event_item_id is not a uuid: the+-- unconstrained TEXT column admits it, and the gate must drop the row rather+-- than fail the page on the cast.+('0190a000-0000-7000-8000-000000000021', 'channel_invite', 'not-a-uuid', 'channel', 'test', '2024-06-01 10:21:00', '{}', 'macro|[email protected]', NULL, NULL);++INSERT INTO public.user_notification ("user_id", "notification_id", "created_at", "sent", "seen_at", "deleted_at", "done", "is_important_v0")+VALUES+('macro|[email protected]', '0190a000-0000-7000-8000-000000000001', '2024-06-01 10:01:00', TRUE, '2024-06-01 10:01:30', NULL, TRUE, FALSE),+('macro|[email protected]', '0190a000-0000-7000-8000-000000000009', '2024-06-01 10:09:00', TRUE, NULL, NULL, FALSE, FALSE),+('macro|[email protected]', '0190a000-0000-7000-8000-000000000030', '2024-06-01 10:30:00', TRUE, NULL, NULL, FALSE, FALSE),+('macro|[email protected]', '0190a000-0000-7000-8000-000000000008', '2024-06-01 10:08:00', TRUE, '2024-06-01 10:08:30', NULL, TRUE, FALSE),+('macro|[email protected]', '0190a000-0000-7000-8000-000000000007', '2024-06-01 10:07:00', TRUE, NULL, NULL, FALSE, FALSE),+('macro|[email protected]', '0190a000-0000-7000-8000-000000000006', '2024-06-01 10:06:00', TRUE, NULL, NULL, FALSE, FALSE),+('macro|[email protected]', '0190a000-0000-7000-8000-000000000000', '2024-06-01 10:00:00', TRUE, NULL, NULL, FALSE, FALSE),+('macro|[email protected]', '0190a000-0000-7000-8000-000000000005', '2024-06-01 10:05:00', TRUE, NULL, NULL, FALSE, FALSE),+('macro|[email protected]', '0190a000-0000-7000-8000-000000000004', '2024-06-01 10:04:00', TRUE, NULL, NULL, FALSE, FALSE),+('macro|[email protected]', '0190a000-0000-7000-8000-000000000003', '2024-06-01 10:03:00', TRUE, NULL, NULL, FALSE, FALSE),+('macro|[email protected]', '0190a000-0000-7000-8000-000000000002', '2024-06-01 10:02:00', TRUE, NULL, NULL, FALSE, FALSE),+('macro|[email protected]', '0190a000-0000-7000-8000-000000000010', '2024-06-01 10:10:00', TRUE, NULL, NULL, FALSE, FALSE),+('macro|[email protected]', '0190a000-0000-7000-8000-000000000011', '2024-06-01 10:11:00', TRUE, NULL, NULL, FALSE, FALSE),+('macro|[email protected]', '0190a000-0000-7000-8000-000000000012', '2024-06-01 10:12:00', TRUE, NULL, '2024-06-01 10:12:30', FALSE, FALSE),+('macro|[email protected]', '0190a000-0000-7000-8000-000000000013', '2024-06-01 10:13:00', TRUE, NULL, NULL, FALSE, FALSE),+('macro|[email protected]', '0190a000-0000-7000-8000-000000000014', '2024-06-01 10:14:00', TRUE, NULL, NULL, FALSE, FALSE),+('macro|[email protected]', '0190a000-0000-7000-8000-000000000015', '2024-06-01 10:15:00', TRUE, NULL, NULL, FALSE, FALSE),+('macro|[email protected]', '0190a000-0000-7000-8000-000000000016', '2024-06-01 10:16:00', TRUE, NULL, NULL, FALSE, FALSE),+('macro|[email protected]', '0190a000-0000-7000-8000-000000000017', '2024-06-01 10:17:00', TRUE, NULL, NULL, FALSE, FALSE),+('macro|[email protected]', '0190a000-0000-7000-8000-000000000018', '2024-06-01 10:18:00', TRUE, NULL, NULL, FALSE, FALSE),+('macro|[email protected]', '0190a000-0000-7000-8000-000000000019', '2024-06-01 10:19:00', TRUE, NULL, NULL, FALSE, FALSE),+('macro|[email protected]', '0190a000-0000-7000-8000-000000000020', '2024-06-01 10:20:00', TRUE, NULL, NULL, FALSE, FALSE),+('macro|[email protected]', '0190a000-0000-7000-8000-000000000021', '2024-06-01 10:21:00', TRUE, NULL, NULL, FALSE, FALSE);++SET session_replication_role = 'origin';diff --git a/crates/soup/src/domain/models.rs b/crates/soup/src/domain/models.rsindex db41d7ab41..2b8826cfc1 100644--- a/crates/soup/src/domain/models.rs+++ b/crates/soup/src/domain/models.rs@@ -19,6 +19,7 @@ use item_filters::{EntityFilters,ast::{EntityFilterAst, ExpandErr,+ calendar_event::CalendarEventLiteral,call::CallLiteral,crm_company::CrmCompanyLiteral,email::EmailLiteral,@@ -33,11 +34,12 @@ use macro_user_id::user_id::MacroUserIdStr;use model_entity::Entity;use models_grouping::GroupingConfig;use models_pagination::{- Cursor, CursorWithValAndFilter, Frecency, Query, SimpleSortMethod, TouchedByMe,+ Cursor, CursorWithValAndFilter, Frecency, NotifiedAt, Query, SimpleSortMethod, TouchedByMe,};use models_soup::SoupProperty;use models_soup::item::SoupItem;use non_empty::IsEmpty;+use recursion::CollapsibleExt;use reminders::domain::models::SoupOrder;use serde::{Deserialize, Serialize};use std::sync::Arc;@@ -166,6 +168,9 @@ pub enum SoupQuery<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>),+ /// Query filtered and sorted by the user's latest notification per+ /// entity (the inbox's notified-at surface).+ Notified(NotifiedQueryInner<T>),}impl<T> SoupQuery<T> {@@ -183,6 +188,9 @@ impl<T> SoupQuery<T> {SoupQuery::Touched(TouchedQueryInner(i)) => {SoupQuery::Touched(TouchedQueryInner(i.map_filter(f)))}+ SoupQuery::Notified(NotifiedQueryInner(i)) => {+ SoupQuery::Notified(NotifiedQueryInner(i.map_filter(f)))+ }}}}@@ -204,6 +212,14 @@ pub struct FrecencyQueryInner<T>(pub(crate) Query<Uuid, Frecency, T>);#[derive(Debug)]pub struct TouchedQueryInner<T>(pub(crate) Query<String, TouchedByMe, T>);+/// the inner private type for [SoupQuery::Notified]+///+/// The id is a [String] for the same reason as [`TouchedQueryInner`]: the+/// keyset compares it byte-for-byte against `notification.event_item_id`,+/// an unconstrained TEXT column.+#[derive(Debug)]+pub struct NotifiedQueryInner<T>(pub(crate) Query<String, NotifiedAt, 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]@@ -252,12 +268,29 @@ impl<T> SoupQuery<T> {SoupQuery::Touched(TouchedQueryInner(models_pagination::Query::Cursor(cursor)))}+ /// create a new instance of a [NotifiedAt] query with [T]. This is used+ /// to construct the initial page request. To paginate an existing cursor+ /// see [Self::new_cursor_notified]+ pub fn new_sort_notified(filters: T) -> Self {+ SoupQuery::Notified(NotifiedQueryInner(models_pagination::Query::Sort(+ NotifiedAt, filters,+ )))+ }++ /// create a new instance of a [NotifiedAt] 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_notified]+ pub fn new_cursor_notified(cursor: CursorWithValAndFilter<String, NotifiedAt, T>) -> Self {+ SoupQuery::Notified(NotifiedQueryInner(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(),+ SoupQuery::Notified(NotifiedQueryInner(query)) => query.filter(),}}}@@ -275,6 +308,9 @@ impl SoupQuery<EntityFilters> {SoupQuery::Touched(TouchedQueryInner(query)) => Ok(SoupQuery::Touched(TouchedQueryInner(query.try_map_filter(EntityFilterAst::new_from_filters)?),)),+ SoupQuery::Notified(NotifiedQueryInner(query)) => Ok(SoupQuery::Notified(+ NotifiedQueryInner(query.try_map_filter(EntityFilterAst::new_from_filters)?),+ )),}}}@@ -433,6 +469,19 @@ impl SoupRequest<Option<EntityFilterAst>> {SoupQuery::Frecency(_) => None,// touched-by-me hydrates emails by thread id, not via this legSoupQuery::Touched(_) => None,+ // notified-at hydrates its email candidates through this leg+ // with the request's own tree, so the view and every email+ // literal keep applying: the handler ANDs the thread ids in.+ SoupQuery::Notified(NotifiedQueryInner(query)) => Some(Query::Sort(+ SimpleSortMethod::UpdatedAt,+ with_properties_filter(+ query+ .filter()+ .as_ref()+ .and_then(|f| f.email_filter.tree.clone()),+ properties_filter,+ ),+ )),}?,include_frecency: false,team_receipt,@@ -450,6 +499,7 @@ impl SoupRequest<Option<EntityFilterAst>> {}))) => filter.as_ref(),SoupQuery::Frecency(_) => None,SoupQuery::Touched(TouchedQueryInner(query)) => query.filter().as_ref(),+ SoupQuery::Notified(NotifiedQueryInner(query)) => query.filter().as_ref(),}}@@ -506,6 +556,8 @@ impl SoupRequest<Option<EntityFilterAst>> {// call records carry no activity of their own (calls land on// the channel), so the touched feed never includes themSoupQuery::Touched(_) => None,+ // call notifications are not rolled into the notified feed+ SoupQuery::Notified(_) => None,}?,})}@@ -557,6 +609,8 @@ impl SoupRequest<Option<EntityFilterAst>> {SoupQuery::Frecency(_) => return None,// CRM has no mutation activity (deferred from touched-by-me)SoupQuery::Touched(_) => return None,+ // CRM companies receive no notifications+ SoupQuery::Notified(_) => return None,};let sort = match sort_method {@@ -608,6 +662,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 and reminders record no activity, so both paths skip them.+ // Notified-at keeps the leg: a fired reminder notifies its owner, and+ // the handler narrows `reminder_ids` to the page's candidates.if matches!(self.cursor, SoupQuery::Frecency(_) | SoupQuery::Touched(_)) {return None;}@@ -666,6 +722,15 @@ impl SoupRequest<Option<EntityFilterAst>> {SoupQuery::Frecency(_) => None,// touched-by-me hydrates channels by id, not via this legSoupQuery::Touched(_) => None,+ // notified-at hydrates its channel candidates through this+ // leg with the request's own tree; the handler ANDs the ids in+ SoupQuery::Notified(NotifiedQueryInner(query)) => Some(Query::Sort(+ SimpleSortMethod::UpdatedAt,+ query+ .filter()+ .as_ref()+ .and_then(|f| f.channel_filter.clone()),+ )),}?,})}@@ -697,6 +762,16 @@ impl SoupRequest<Option<EntityFilterAst>> {// channel-thread rows carry no activity (messages attribute to// the channel), so the touched feed never includes themSoupQuery::Touched(_) => None,+ // notified-at hydrates thread-scoped channel notifications through+ // this leg with the request's own tree; the handler ANDs the+ // thread ids in+ SoupQuery::Notified(NotifiedQueryInner(query)) => Some(Query::Sort(+ SimpleSortMethod::UpdatedAt,+ query+ .filter()+ .as_ref()+ .and_then(|f| f.channel_thread_filter.clone()),+ )),}?;Some(GetThreadReplyRowsRequest {@@ -734,6 +809,15 @@ impl SoupRequest<Option<EntityFilterAst>> {// foreign entities record no activity, so the touched feed never// includes themSoupQuery::Touched(_) => None,+ // notified-at hydrates its foreign-entity candidates through this+ // leg with the request's own tree; the handler ANDs the ids in+ SoupQuery::Notified(NotifiedQueryInner(query)) => Some(Query::Sort(+ SimpleSortMethod::UpdatedAt,+ query+ .filter()+ .as_ref()+ .and_then(|filter| filter.foreign_entity_filter.clone()),+ )),}}@@ -790,6 +874,97 @@ pub struct TouchedEntity {pub touched_at: chrono::DateTime<chrono::Utc>,}+/// Keyset position for a notified-at page: the notification timestamp and+/// event item id of the previous page's last candidate.+///+/// Like [`TouchedPagePosition`], the id stays the raw stored string: the SQL+/// keyset compares it byte-for-byte against `notification.event_item_id`.+#[derive(Debug, Clone)]+pub struct NotifiedPagePosition {+ /// When the last candidate's latest notification was created for the user.+ pub notified_at: chrono::DateTime<chrono::Utc>,+ /// The last candidate's event item id — the tiebreaker when two+ /// candidates share a notification timestamp.+ pub entity_id: String,+}++/// Parameters for one page of notified-at candidates.+#[derive(Debug)]+pub struct NotifiedSoupRequest<'a> {+ /// User whose notifications are listed; also the access-check subject.+ pub user_id: MacroUserIdStr<'a>,+ /// Maximum candidates to return.+ pub limit: u16,+ /// Resume after this position; `None` for the first page.+ pub after: Option<NotifiedPagePosition>,+ /// Entity filters folded into the candidate query where soup owns the+ /// fold (documents, chats, projects, calendar events, properties).+ pub filter: Option<&'a EntityFilterAst>,+ /// Every inbox the caller can read; gates email-thread candidates.+ pub link_ids: &'a [Uuid],+ /// Sources (the caller and their team) whose stored foreign entities the+ /// caller may see; gates foreign-entity candidates.+ pub foreign_entity_sources: &'a [SourceId],+ /// Entity types whose candidates the domain can hydrate for this request.+ /// Types outside this set are never returned, whatever the notifications+ /// table holds.+ pub hydratable: NotifiedHydratableTypes,+}++/// Which domain-hydrated entity types a notified-at request can surface.+///+/// The repository gates documents, chats, projects and calendar events on+/// the filter tree itself; these four are hydrated through other domains'+/// legs, so whether that leg is active for the request is decided by the+/// service and passed down here.+#[derive(Debug, Clone, Copy, Default)]+pub struct NotifiedHydratableTypes {+ /// The channel leg is active.+ pub channels: bool,+ /// The channel-thread leg is active. Thread-scoped channel notifications+ /// (mentions and replies, which name their thread root as the secondary+ /// event item) surface as thread rows, the way the client attributes them.+ pub channel_threads: bool,+ /// The email leg is active.+ pub email_threads: bool,+ /// The foreign-entity leg is active.+ pub foreign_entities: bool,+ /// The reminder leg is active (the request opted into reminders).+ pub reminders: bool,+}++/// One notified entity: what it is and when the user was last notified.+#[derive(Debug, Clone)]+pub struct NotifiedEntity {+ /// The entity the notification is about.+ pub entity: Entity<'static>,+ /// When the user's latest notification about it was created.+ pub notified_at: chrono::DateTime<chrono::Utc>,+}++/// Whether the notified-at candidate query can fold this calendar filter.+///+/// Only id and notification-state literals have a soup-owned fold; the rest+/// (status, time bounds, attendee, organizer) live in the calendar leg's+/// query builder, which the candidate query cannot reach. Rejecting up front+/// beats silently returning a feed with those events unfiltered.+pub(crate) fn calendar_filter_supported_by_notified(+ tree: Option<&Expr<CalendarEventLiteral>>,+) -> bool {+ tree.is_none_or(|expr| {+ expr.collapse_frames(|frame| match frame {+ filter_ast::ExprFrame::And(a, b) | filter_ast::ExprFrame::Or(a, b) => a && b,+ filter_ast::ExprFrame::Not(a) => a,+ filter_ast::ExprFrame::Literal(+ CalendarEventLiteral::Id(_)+ | CalendarEventLiteral::NotificationDone(_)+ | CalendarEventLiteral::NotificationSeen(_),+ ) => true,+ filter_ast::ExprFrame::Literal(_) => false,+ })+ })+}+/// 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.@@ -1092,6 +1267,10 @@ pub struct EnrichedSoupItem {/// the page was ordered by `touched_by_me`. Clients sort and optimistic-/// reorder the touched feed on this value.pub touched_at: Option<chrono::DateTime<chrono::Utc>>,+ /// When the caller was last notified about this entity, populated only+ /// when the page was ordered by `notified_at`. Clients sort and bucket+ /// the notified feed on this value.+ pub notified_at: Option<chrono::DateTime<chrono::Utc>>,}/// A soup request with optional grouping configuration.@@ -1132,6 +1311,10 @@ pub enum SoupErr {/// Rejecting beats silently returning a feed with that type missing.#[error("sort_method=touched_by_me does not support {0} filters")]TouchedUnsupportedFilter(&'static str),+ /// A notified-at query carried a filter kind this mode cannot evaluate+ /// (the fold lives in another domain's query builder).+ #[error("sort_method=notified_at does not support {0} filters")]+ NotifiedUnsupportedFilter(&'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/models/grouping.rs b/crates/soup/src/domain/models/grouping.rsindex 19bbe9aea6..2d9b845731 100644--- a/crates/soup/src/domain/models/grouping.rs+++ b/crates/soup/src/domain/models/grouping.rs@@ -114,6 +114,7 @@ pub fn build_grouped_response(item: grouped_item.item,frecency_score: None,touched_at: None,+ notified_at: None,});}diff --git a/crates/soup/src/domain/ports.rs b/crates/soup/src/domain/ports.rsindex 5c030160c2..8a3af65be6 100644--- a/crates/soup/src/domain/ports.rs+++ b/crates/soup/src/domain/ports.rs@@ -1,14 +1,14 @@use std::sync::Arc;use crate::domain::models::{- AdvancedSortParams, EnrichedSoupItem, GroupedSortRequest, IntoSoupReqAst, SimpleSortRequest,- SoupErr, SoupProjectionHydration, SoupPropertiesField, SoupRequest, TouchedEntity,- TouchedSoupRequest, grouping::ItemGroupingInfo,+ AdvancedSortParams, EnrichedSoupItem, GroupedSortRequest, IntoSoupReqAst, NotifiedEntity,+ NotifiedSoupRequest, SimpleSortRequest, SoupErr, SoupProjectionHydration, SoupPropertiesField,+ SoupRequest, TouchedEntity, TouchedSoupRequest, grouping::ItemGroupingInfo,};use entity_access::domain::models::{EntityAccessReceipt, MemberTeamRole};use macro_user_id::user_id::MacroUserIdStr;use models_pagination::{- Frecency, PaginatedCursor, PaginatedOpaqueCursor, SimpleSortMethod, TouchedByMe,+ Frecency, NotifiedAt, PaginatedCursor, PaginatedOpaqueCursor, SimpleSortMethod, TouchedByMe,TypeEraseCursor,};use models_properties::service::property_definition_with_options::PropertyDefinitionWithOptions;@@ -95,6 +95,15 @@ pub trait SoupRepo: Send + Sync + 'static {&self,req: TouchedSoupRequest<'a>,) -> impl Future<Output = Result<Vec<TouchedEntity>, Self::Err>> + Send;++ /// Fetch one page of notified-at candidates: entities the user holds a+ /// live notification for, newest notification first, already gated on+ /// existence, deletion and access, and on the request's entity filters+ /// where soup owns the fold.+ fn notified_soup_page<'a>(+ &self,+ req: NotifiedSoupRequest<'a>,+ ) -> impl Future<Output = Result<Vec<NotifiedEntity>, Self::Err>> + Send;}/// The possible outputs of soup — one paginated page per query mode.@@ -111,6 +120,8 @@ pub enum SoupOutput<T, Item = SoupItem<()>> {Frecency(PaginatedCursor<Item, String, Frecency, T>),/// Page of a touched-by-me query.Touched(PaginatedCursor<Item, String, TouchedByMe, T>),+ /// Page of a notified-at query.+ Notified(PaginatedCursor<Item, String, NotifiedAt, T>),}impl<T, Item> SoupOutput<T, Item> {@@ -123,6 +134,7 @@ impl<T, Item> SoupOutput<T, Item> {SoupOutput::Simple(page) => SoupOutput::Simple(page.map(f)),SoupOutput::Frecency(page) => SoupOutput::Frecency(page.map(f)),SoupOutput::Touched(page) => SoupOutput::Touched(page.map(f)),+ SoupOutput::Notified(page) => SoupOutput::Notified(page.map(f)),}}@@ -132,6 +144,7 @@ impl<T, Item> SoupOutput<T, Item> {SoupOutput::Simple(page) => page.items,SoupOutput::Frecency(page) => page.items,SoupOutput::Touched(page) => page.items,+ SoupOutput::Notified(page) => page.items,}}@@ -139,7 +152,7 @@ impl<T, Item> SoupOutput<T, Item> {pub fn into_simple(self) -> Option<PaginatedCursor<Item, String, SimpleSortMethod, T>> {match self {SoupOutput::Simple(page) => Some(page),- SoupOutput::Frecency(_) | SoupOutput::Touched(_) => None,+ SoupOutput::Frecency(_) | SoupOutput::Touched(_) | SoupOutput::Notified(_) => None,}}@@ -147,7 +160,7 @@ impl<T, Item> SoupOutput<T, Item> {pub fn into_frecency(self) -> Option<PaginatedCursor<Item, String, Frecency, T>> {match self {SoupOutput::Frecency(page) => Some(page),- SoupOutput::Simple(_) | SoupOutput::Touched(_) => None,+ SoupOutput::Simple(_) | SoupOutput::Touched(_) | SoupOutput::Notified(_) => None,}}@@ -155,7 +168,15 @@ impl<T, Item> SoupOutput<T, Item> {pub fn into_touched(self) -> Option<PaginatedCursor<Item, String, TouchedByMe, T>> {match self {SoupOutput::Touched(page) => Some(page),- SoupOutput::Simple(_) | SoupOutput::Frecency(_) => None,+ SoupOutput::Simple(_) | SoupOutput::Frecency(_) | SoupOutput::Notified(_) => None,+ }+ }++ /// Returns the page when this is a [`SoupOutput::Notified`] output.+ pub fn into_notified(self) -> Option<PaginatedCursor<Item, String, NotifiedAt, T>> {+ match self {+ SoupOutput::Notified(page) => Some(page),+ SoupOutput::Simple(_) | SoupOutput::Frecency(_) | SoupOutput::Touched(_) => None,}}}@@ -166,6 +187,7 @@ impl<T, Item> TypeEraseCursor<Item> for SoupOutput<T, Item> {SoupOutput::Simple(page) => page.type_erase(),SoupOutput::Frecency(page) => page.type_erase(),SoupOutput::Touched(page) => page.type_erase(),+ SoupOutput::Notified(page) => page.type_erase(),}}}diff --git a/crates/soup/src/domain/service.rs b/crates/soup/src/domain/service.rsindex 679ee916f5..4f3b2beeff 100644--- a/crates/soup/src/domain/service.rs+++ b/crates/soup/src/domain/service.rs@@ -1,10 +1,12 @@use crate::domain::{models::{AdvancedSortParams, EnrichedSoupItem, FrecencyQueryInner, GetCrmCompaniesRequest,- GetRemindersRequest, GroupedSortRequest, IntoSoupReqAst, SimpleQueryInner, SimpleSortQuery,- SimpleSortRequest, SoupDocumentServerFacts, SoupErr, SoupProjectionHydration,- SoupPropertiesField, SoupQuery, SoupRequest, SoupSortDirection, SoupType,- TouchedPagePosition, TouchedQueryInner, TouchedSoupRequest, grouping::ItemGroupingInfo,+ GetRemindersRequest, GroupedSortRequest, IntoSoupReqAst, NotifiedEntity,+ NotifiedHydratableTypes, NotifiedPagePosition, NotifiedQueryInner, NotifiedSoupRequest,+ SimpleQueryInner, SimpleSortQuery, SimpleSortRequest, SoupDocumentServerFacts, SoupErr,+ SoupProjectionHydration, SoupPropertiesField, SoupQuery, SoupRequest, SoupSortDirection,+ SoupType, TouchedPagePosition, TouchedQueryInner, TouchedSoupRequest,+ calendar_filter_supported_by_notified, grouping::ItemGroupingInfo,},ports::{SoupOutput, SoupRepo, SoupService},};@@ -33,12 +35,17 @@ use frecency::domain::{},ports::FrecencyQueryService,};-use item_filters::ast::{EntityFilterAst, channel::ChannelLiteral, email::EmailLiteral};+use item_filters::ast::{+ EntityFilterAst,+ channel::{ChannelLiteral, ChannelThreadLiteral},+ email::EmailLiteral,+ foreign_entity::ForeignEntityLiteral,+};use macro_user_id::user_id::MacroUserIdStr;-use model_entity::EntityType;+use model_entity::{Entity, EntityType};use models_pagination::{- Base64Str, Cursor, CursorVal, Frecency, FrecencyValue, Identify, PaginateOn, Paginated, Query,- SimpleSortMethod, SortOn, TouchedByMe,+ Base64Str, Cursor, CursorVal, Frecency, FrecencyValue, Identify, NotifiedAt, PaginateOn,+ Paginated, Query, SimpleSortMethod, SortOn, TouchedByMe,};use models_properties::service::property_definition_with_options::PropertyDefinitionWithOptions;use models_soup::{@@ -69,16 +76,18 @@ struct SoupCandidate {document_server_facts: Option<SoupDocumentServerFacts>,frecency_score: Option<AggregateFrecency>,touched_at: Option<chrono::DateTime<chrono::Utc>>,+ notified_at: Option<chrono::DateTime<chrono::Utc>>,}impl SoupCandidate {- /// A candidate with no frecency or touch timestamp loaded yet.+ /// A candidate with no frecency, touch or notification timestamp loaded yet.fn plain(item: SoupItem<()>) -> Self {SoupCandidate {item,document_server_facts: None,frecency_score: None,touched_at: None,+ notified_at: None,}}@@ -89,6 +98,45 @@ impl SoupCandidate {document_server_facts: hydration.document_server_facts,frecency_score: None,touched_at: None,+ notified_at: None,+ }+ }+}++/// Upper bound on candidate pages one notified-at page may consume while+/// refilling after hydration drops (see `handle_notified_request`).+const MAX_NOTIFIED_FILL_ROUNDS: usize = 4;++/// The per-domain hydration legs a notified-at page can draw on, built from+/// the request the same way the simple path builds its sub-requests. A `None`+/// leg is off for this request, and its entity type never enters the+/// candidate query.+struct NotifiedHydrationLegs {+ /// Every inbox the caller can read; gates email-thread candidates.+ link_ids: Vec<Uuid>,+ email: Option<GetEmailsRequest>,+ comms: Option<GetChannelsRequest>,+ comms_threads: Option<GetThreadReplyRowsRequest>,+ foreign_entities: Option<(Vec<SourceId>, ForeignEntityListQuery)>,+ reminders: Option<GetRemindersRequest<'static>>,+}++/// ANDs a leg's request-level filter tree onto the page's id tree, so the+/// leg returns exactly the candidates that also satisfy the request.+fn with_id_tree<T: Clone>(ids: Arc<Expr<T>>, tree: Option<&Arc<Expr<T>>>) -> Arc<Expr<T>> {+ match tree {+ Some(tree) => Arc::new(Expr::and(Arc::unwrap_or_clone(ids), (**tree).clone())),+ None => ids,+ }+}++/// Parses a candidate's id for a leg keyed by uuid, logging and skipping the+/// row when the unconstrained TEXT column holds something else.+fn push_candidate_uuid(ids: &mut Vec<Uuid>, entity: &Entity<'_>, leg: &'static str) {+ match Uuid::parse_str(&entity.entity_id) {+ Ok(id) => ids.push(id),+ Err(error) => {+ tracing::warn!(error = ?error, leg, "notified entity id is not a uuid; skipping")}}}@@ -647,6 +695,350 @@ whereOk((candidates, next))}+ /// Runs one page of the notified-at feed: fetches (entity, notified_at)+ /// candidates from the user's notifications, hydrates each entity type by+ /// id, and reassembles the page in notification order.+ ///+ /// Channel, channel-thread, email, foreign-entity and reminder candidates+ /// hydrate through their own domains' legs with the request's filter tree+ /// for that type ANDed in, so those filters apply at hydration rather+ /// than in the candidate query. A candidate the leg does not return is dropped and+ /// the page refills from the next candidate page, bounded by+ /// [`MAX_NOTIFIED_FILL_ROUNDS`] so a run of filtered-out candidates still+ /// answers promptly — with a cursor to continue from, since the feed is+ /// only exhausted once the candidate query itself runs dry.+ #[tracing::instrument(err, skip(self, cursor, legs))]+ async fn handle_notified_request(+ &self,+ cursor: Query<String, NotifiedAt, Option<EntityFilterAst>>,+ soup_type: SoupType,+ user: MacroUserIdStr<'static>,+ limit: u16,+ legs: NotifiedHydrationLegs,+ include_projection: bool,+ ) -> Result<(Vec<SoupCandidate>, Option<NotifiedPagePosition>), SoupErr> {+ let filter = cursor.filter().as_ref();+ // Calendar events hydrate by id through the main query, which takes+ // no filter, so the candidate query must fold the calendar tree+ // itself — and it only knows the id and notification literals.+ if !calendar_filter_supported_by_notified(+ filter.and_then(|f| f.calendar_event_filter.as_deref()),+ ) {+ return Err(SoupErr::NotifiedUnsupportedFilter("calendar_event"));+ }++ let mut after = match &cursor {+ Query::Sort(_, _) => None,+ Query::Cursor(c) => Some(NotifiedPagePosition {+ notified_at: c.val.last_val,+ entity_id: c.id.clone(),+ }),+ };+ let foreign_entity_sources: Vec<SourceId> = legs+ .foreign_entities+ .as_ref()+ .map(|(sources, _)| sources.clone())+ .unwrap_or_default();+ let hydratable = NotifiedHydratableTypes {+ channels: legs.comms.is_some(),+ channel_threads: legs.comms_threads.is_some(),+ email_threads: legs.email.is_some(),+ foreign_entities: legs.foreign_entities.is_some(),+ reminders: legs.reminders.is_some(),+ };+ let page_len = usize::from(limit);++ let mut page: Vec<SoupCandidate> = Vec::with_capacity(page_len);+ let mut next = None;+ for _ in 0..MAX_NOTIFIED_FILL_ROUNDS {+ let candidates = self+ .soup_storage+ .notified_soup_page(NotifiedSoupRequest {+ user_id: user.copied(),+ limit,+ after: after.clone(),+ filter,+ link_ids: &legs.link_ids,+ foreign_entity_sources: &foreign_entity_sources,+ hydratable,+ })+ .await+ .map_err(anyhow::Error::from)?;+ let candidate_page_full = candidates.len() == page_len;+ let mut hydrated = self+ .hydrate_notified_candidates(+ &candidates,+ soup_type,+ &user,+ &legs,+ include_projection,+ )+ .await?;++ // Walk candidates in feed order until the page is full. The+ // cursor is the last candidate walked, kept or dropped: anything+ // after it is re-fetched by the next page.+ let mut walked = None;+ let mut consumed = 0;+ for candidate in &candidates {+ if page.len() == page_len {+ break;+ }+ consumed += 1;+ walked = Some(NotifiedPagePosition {+ notified_at: candidate.notified_at,+ entity_id: candidate.entity.entity_id.to_string(),+ });+ let key = (+ candidate.entity.entity_type,+ candidate.entity.entity_id.to_string(),+ );+ if let Some(mut item) = hydrated.remove(&key) {+ item.notified_at = Some(candidate.notified_at);+ page.push(item);+ }+ }++ let exhausted = !candidate_page_full && consumed == candidates.len();+ if page.len() == page_len {+ next = if exhausted { None } else { walked };+ break;+ }+ if exhausted {+ next = None;+ break;+ }+ next = walked.clone();+ after = walked;+ }++ Ok((page, next))+ }++ /// Hydrates one candidate page's entities through the by-id queries and+ /// the domain legs, keyed by entity for reassembly in candidate order.+ async fn hydrate_notified_candidates(+ &self,+ candidates: &[NotifiedEntity],+ soup_type: SoupType,+ user: &MacroUserIdStr<'static>,+ legs: &NotifiedHydrationLegs,+ include_projection: bool,+ ) -> Result<HashMap<(EntityType, String), SoupCandidate>, SoupErr> {+ let mut main_entities = Vec::new();+ let mut project_entities = Vec::new();+ let mut channel_ids = Vec::new();+ let mut thread_ids = Vec::new();+ let mut email_ids = Vec::new();+ let mut foreign_entity_ids = Vec::new();+ let mut reminder_ids = Vec::new();+ for candidate in candidates {+ match candidate.entity.entity_type {+ // Calendar events ride the main by-ids query in both soup types.+ EntityType::Document | EntityType::Chat | EntityType::CalendarEvent => {+ main_entities.push(candidate.entity.copied())+ }+ // Same split as the touched feed: the expanded by-ids query+ // omits project rows, so they hydrate unexpanded separately.+ EntityType::Project => match soup_type {+ SoupType::Expanded => project_entities.push(candidate.entity.copied()),+ SoupType::UnExpanded => main_entities.push(candidate.entity.copied()),+ },+ EntityType::Channel => {+ push_candidate_uuid(&mut channel_ids, &candidate.entity, "channel")+ }+ // Thread-scoped channel notifications are keyed on their+ // thread root, which is the thread row's id.+ EntityType::ChannelMessage => {+ push_candidate_uuid(&mut thread_ids, &candidate.entity, "channel thread")+ }+ EntityType::EmailThread => {+ push_candidate_uuid(&mut email_ids, &candidate.entity, "email")+ }+ EntityType::ForeignEntity => {+ push_candidate_uuid(&mut foreign_entity_ids, &candidate.entity, "foreign")+ }+ EntityType::Reminder => {+ push_candidate_uuid(&mut reminder_ids, &candidate.entity, "reminder")+ }+ // The candidate query only returns the types above.+ _ => {}+ }+ }++ let comms_request = legs.comms.as_ref().and_then(|template| {+ let ids = balanced_or_tree(+ channel_ids+ .iter()+ .map(|id| Expr::val(ChannelLiteral::ChannelId(*id)))+ .collect(),+ )?;+ Some(GetChannelsRequest {+ macro_id: user.clone(),+ limit: Some(channel_ids.len() as u32),+ include_frecency: false,+ query: Query::Sort(+ SimpleSortMethod::UpdatedAt,+ Some(with_id_tree(ids, template.query.filter().as_ref())),+ ),+ })+ });+ let comms_thread_request = legs.comms_threads.as_ref().and_then(|template| {+ let ids = balanced_or_tree(+ thread_ids+ .iter()+ .map(|id| Expr::val(ChannelThreadLiteral::ThreadId(*id)))+ .collect(),+ )?;+ Some(GetThreadReplyRowsRequest {+ macro_id: user.clone(),+ limit: Some(thread_ids.len() as u32),+ query: Query::Sort(+ SimpleSortMethod::UpdatedAt,+ Some(with_id_tree(ids, template.query.filter().as_ref())),+ ),+ })+ });+ let email_request = legs.email.as_ref().and_then(|template| {+ let ids = balanced_or_tree(+ email_ids+ .iter()+ .map(|id| Expr::val(EmailLiteral::ThreadId(*id)))+ .collect(),+ )?;+ Some(GetEmailsRequest {+ view: template.view.clone(),+ link_ids: template.link_ids.clone(),+ macro_id: user.clone(),+ limit: Some(email_ids.len() as u32),+ query: Query::Sort(+ SimpleSortMethod::UpdatedAt,+ Some(with_id_tree(ids, template.query.filter().as_ref())),+ ),+ include_frecency: false,+ team_receipt: template.team_receipt.clone(),+ crm_scope: template.crm_scope.clone(),+ })+ });+ let (foreign_entity_sources, foreign_entity_query) = legs+ .foreign_entities+ .as_ref()+ .and_then(|(sources, template)| {+ let ids = balanced_or_tree(+ foreign_entity_ids+ .iter()+ .map(|id| Expr::val(ForeignEntityLiteral::Id(*id)))+ .collect(),+ )?;+ Some((+ sources.clone(),+ Query::Sort(+ SimpleSortMethod::UpdatedAt,+ Some(with_id_tree(ids, template.filter().as_ref())),+ ),+ ))+ })+ .map_or((Vec::new(), None), |(sources, query)| {+ (sources, Some(query))+ });+ let reminder_request = legs.reminders.as_ref().and_then(|template| {+ // A request naming specific reminders keeps that constraint;+ // otherwise the page's candidates are the id set. An empty+ // intersection skips the leg: an empty id list means every+ // reminder to the reminders service.+ let ids: Vec<Uuid> = if template.reminder_ids.is_empty() {+ reminder_ids.clone()+ } else {+ reminder_ids+ .iter()+ .copied()+ .filter(|id| template.reminder_ids.contains(id))+ .collect()+ };+ if ids.is_empty() {+ return None;+ }+ Some(GetRemindersRequest {+ user_id: template.user_id.clone(),+ limit: ids.len() as i64,+ reminder_ids: ids,+ entities: template.entities.clone(),+ completed: template.completed,+ fired: template.fired,+ order: template.order,+ })+ });++ // 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(),+ },+ include_projection,+ )+ .await+ .map_err(anyhow::Error::from)+ .map_err(SoupErr::from)+ };+ let project_items_fut = async {+ if project_entities.is_empty() {+ return Ok(Vec::new());+ }+ self.soup_storage+ .unexpanded_soup_by_ids(AdvancedSortParams {+ entities: &project_entities,+ user_id: user.copied(),+ })+ .await+ .map_err(anyhow::Error::from)+ .map_err(SoupErr::from)+ };+ let (+ main_items,+ project_items,+ channel_candidates,+ thread_candidates,+ email_candidates,+ foreign_entity_candidates,+ reminder_candidates,+ ) = tokio::join!(+ main_items_fut,+ project_items_fut,+ self.handle_comms_request(comms_request),+ self.handle_comms_thread_request(comms_thread_request),+ self.handle_email_request(email_request),+ self.handle_foreign_entity_request(+ Some(user.to_string()),+ foreign_entity_sources,+ foreign_entity_ids.len() as u32,+ foreign_entity_query,+ ),+ self.handle_reminder_request(reminder_request),+ );++ let mut candidates_by_entity = HashMap::new();+ for candidate in main_items?+ .into_iter()+ .chain(project_items?.into_iter().map(SoupCandidate::plain))+ .chain(channel_candidates?)+ .chain(thread_candidates?)+ .chain(email_candidates?)+ .chain(foreign_entity_candidates?)+ .chain(reminder_candidates?)+ {+ let key = {+ let entity = candidate.item.entity();+ (entity.entity_type, entity.entity_id.to_string())+ };+ candidates_by_entity.insert(key, candidate);+ }+ Ok(candidates_by_entity)+ }+#[tracing::instrument(err, skip(self, req))]async fn handle_email_request(&self,@@ -693,6 +1085,7 @@ wheredocument_server_facts: None,frecency_score,touched_at: None,+ notified_at: None,},)))}@@ -720,6 +1113,7 @@ wheredocument_server_facts: None,frecency_score,touched_at: None,+ notified_at: None,}})})?,@@ -892,7 +1286,12 @@ wherelet (items, enrichments): (Vec<_>, Vec<_>) = items.into_iter()- .map(|item| (item.item, (item.frecency_score, item.touched_at)))+ .map(|item| {+ (+ item.item,+ (item.frecency_score, item.touched_at, item.notified_at),+ )+ }).unzip();let items = self.soup_storage@@ -908,11 +1307,14 @@ whereOk(items.into_iter().zip(enrichments)- .map(|(item, (frecency_score, touched_at))| EnrichedSoupItem {- item,- frecency_score,- touched_at,- })+ .map(+ |(item, (frecency_score, touched_at, notified_at))| EnrichedSoupItem {+ item,+ frecency_score,+ touched_at,+ notified_at,+ },+ ).collect())}@@ -941,6 +1343,9 @@ whereSoupOutput::Touched(page) => Ok(SoupOutput::Touched(self.populate_properties_page(user_id, page).await?,)),+ SoupOutput::Notified(page) => Ok(SoupOutput::Notified(+ self.populate_properties_page(user_id, page).await?,+ )),}}@@ -994,6 +1399,10 @@ whereSoupOutput::Touched(page) => Ok(SoupOutput::Touched(self.populate_frecency_page(user_id, page).await?,)),+ // Likewise for notified sorting, which orders on notifications.+ SoupOutput::Notified(page) => Ok(SoupOutput::Notified(+ self.populate_frecency_page(user_id, page).await?,+ )),}}@@ -1010,6 +1419,7 @@ where.map_extra(|()| SoupPropertiesField::default()),frecency_score: candidate.frecency_score,touched_at: candidate.touched_at,+ notified_at: candidate.notified_at,})}@@ -1032,6 +1442,7 @@ where.map_extra(|()| SoupPropertiesField::default()),frecency_score: candidate.frecency_score,touched_at: candidate.touched_at,+ notified_at: candidate.notified_at,},document_server_facts: candidate.document_server_facts,})@@ -1233,6 +1644,42 @@ wherenext_cursor,)))}+ SoupQuery::Notified(NotifiedQueryInner(cursor)) => {+ let legs = NotifiedHydrationLegs {+ link_ids: req.link_ids,+ email: email_request,+ comms: comms_request,+ comms_threads: comms_thread_request,+ foreign_entities: foreign_entity_query+ .map(|query| (foreign_entity_source_ids, query)),+ reminders: reminder_request,+ };+ let (candidates, next) = self+ .handle_notified_request(+ cursor,+ req.soup_type,+ req.user,+ limit,+ legs,+ include_projection,+ )+ .await?;+ let next_cursor = next.map(|position| {+ Base64Str::encode_json(Cursor {+ id: position.entity_id,+ limit: limit.into(),+ val: CursorVal {+ sort_type: NotifiedAt,+ last_val: position.notified_at,+ },+ filter: entity_filter,+ })+ });+ Ok(SoupOutput::Notified(Paginated::from_parts(+ candidates,+ next_cursor,+ )))+ }}}}diff --git a/crates/soup/src/domain/service/tests.rs b/crates/soup/src/domain/service/tests.rsindex 61d2ff995c..1ea61c32b6 100644--- a/crates/soup/src/domain/service/tests.rs+++ b/crates/soup/src/domain/service/tests.rs@@ -1,4 +1,4 @@-use crate::domain::models::TouchedEntity;+use crate::domain::models::{NotifiedEntity, TouchedEntity};use crate::domain::ports::MockSoupRepo;use channels::domain::{models::{@@ -10,7 +10,9 @@ use chrono::Days;use chrono::{DateTime, Utc};use cool_asserts::assert_matches;use email::domain::models::{EnrichedEmailThreadPreview, PreviewView};-use entity_access::domain::models::{EntityAccessReceipt, ViewAccessLevel};+use entity_access::domain::models::{+ AnyEntityPermission, EntityAccessReceipt, OwnerAccessLevel, ViewAccessLevel,+};use filter_ast::Expr;use foreign_entity::domain::{models::{@@ -34,6 +36,10 @@ use models_pagination::{};use models_soup::document::{SoupDocument, SoupDocumentSubType};use ordered_float::OrderedFloat;+use reminders::domain::models::{+ CreateReminder, Reminder, ReminderError, ReminderFilter, ReminderForSoup, ReminderPage,+ ReminderPatch,+};use rootcause::Report;use std::sync::{Arc, Mutex};use uuid::Uuid;@@ -2873,6 +2879,7 @@ async fn touched_soup_full_page_builds_keyset_cursor() {#[derive(Clone, Default)]struct RecordingEmailPreviewService {requests: Arc<Mutex<Vec<(PreviewView, Vec<Uuid>, Option<u32>)>>>,+ filters: Arc<Mutex<Vec<String>>>,}impl EmailPreviewServiceReadOnly for RecordingEmailPreviewService {@@ -2887,6 +2894,10 @@ impl EmailPreviewServiceReadOnly for RecordingEmailPreviewService {.lock().unwrap().push((req.view.clone(), req.link_ids.clone(), req.limit));+ self.filters+ .lock()+ .unwrap()+ .push(serde_json::to_string(req.query.filter()).unwrap());Ok(Option::<EnrichedEmailThreadPreview>::None.into_iter().paginate_on(0, SimpleSortMethod::CreatedAt)@@ -2894,6 +2905,65 @@ impl EmailPreviewServiceReadOnly for RecordingEmailPreviewService {}}+/// Records the id sets and limits the reminders leg is asked for.+#[derive(Default)]+struct RecordingRemindersService {+ queries: Arc<Mutex<Vec<(Vec<Uuid>, i64)>>>,+}++impl RemindersService for RecordingRemindersService {+ async fn create_reminder(+ &self,+ _user_id: &MacroUserIdStr<'_>,+ _request: CreateReminder,+ _entity_receipt: Option<EntityAccessReceipt<AnyEntityPermission>>,+ ) -> Result<Reminder, ReminderError> {+ unimplemented!("RecordingRemindersService.create_reminder")+ }++ async fn get_reminder(+ &self,+ _receipt: EntityAccessReceipt<OwnerAccessLevel>,+ ) -> Result<Reminder, ReminderError> {+ unimplemented!("RecordingRemindersService.get_reminder")+ }++ async fn list_reminders(+ &self,+ _user_id: &MacroUserIdStr<'_>,+ _filter: ReminderFilter,+ ) -> Result<ReminderPage, ReminderError> {+ unimplemented!("RecordingRemindersService.list_reminders")+ }++ async fn list_reminders_for_soup(+ &self,+ _user_id: &MacroUserIdStr<'_>,+ query: SoupReminderQuery<'_>,+ ) -> Result<Vec<ReminderForSoup>, ReminderError> {+ self.queries+ .lock()+ .unwrap()+ .push((query.ids.to_vec(), query.limit));+ Ok(Vec::new())+ }++ async fn update_reminder(+ &self,+ _receipt: EntityAccessReceipt<OwnerAccessLevel>,+ _patch: ReminderPatch,+ ) -> Result<Reminder, ReminderError> {+ unimplemented!("RecordingRemindersService.update_reminder")+ }++ async fn delete_reminder(+ &self,+ _receipt: EntityAccessReceipt<OwnerAccessLevel>,+ ) -> Result<(), ReminderError> {+ unimplemented!("RecordingRemindersService.delete_reminder")+ }+}+/// 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.@@ -3021,3 +3091,489 @@ async fn touched_soup_rejects_unfoldable_filters() {});}}++fn notified_request(+ limit: u16,+ link_ids: Vec<Uuid>,+ filters: EntityFilters,+) -> SoupRequest<EntityFilters> {+ SoupRequest {+ sort_direction: SoupSortDirection::default(),+ email_preview_view: PreviewView::StandardLabel(+ email::domain::models::PreviewViewStandardLabel::Inbox,+ ),+ link_ids,+ soup_type: SoupType::Expanded,+ limit,+ cursor: SoupQuery::new_sort_notified(filters),+ user: MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap(),+ }+}++fn notified(entity_type: EntityType, id: Uuid, notified_at: DateTime<Utc>) -> NotifiedEntity {+ NotifiedEntity {+ entity: entity_type.with_entity_string(id.to_string()),+ notified_at,+ }+}++/// A candidate that fails hydration is dropped and the page refills from the+/// next candidate page, in notification order, until the candidates run out.+#[tokio::test]+async fn notified_soup_refills_after_hydration_drops_and_ends_when_exhausted() {+ let doc_1 = Uuid::from_u128(1);+ let ghost = Uuid::from_u128(2);+ let project = Uuid::from_u128(3);+ let doc_2 = Uuid::from_u128(4);+ let base: DateTime<Utc> = DateTime::default();++ let mut soup_mock = MockSoupRepo::new();+ soup_mock+ .expect_notified_soup_page()+ .times(2)+ .returning(move |req| {+ assert_eq!(req.limit, 3);+ // Round one is a full candidate page; the ghost never hydrates,+ // so round two resumes after the last walked candidate and is+ // short, which ends the feed.+ let rows = match req.after {+ None => vec![+ notified(EntityType::Document, doc_1, base + Days::new(5)),+ notified(EntityType::Document, ghost, base + Days::new(4)),+ notified(EntityType::Project, project, base + Days::new(3)),+ ],+ Some(after) => {+ assert_eq!(after.entity_id, project.to_string());+ assert_eq!(after.notified_at, base + Days::new(3));+ vec![notified(EntityType::Document, doc_2, base + Days::new(2))]+ }+ };+ Box::pin(async move { Ok(rows) })+ });+ soup_mock+ .expect_expanded_soup_by_ids()+ .times(2)+ .returning(move |params| {+ let items: Vec<SoupItem<()>> = params+ .entities+ .iter()+ .filter_map(|entity| {+ let id = Uuid::parse_str(&entity.entity_id).unwrap();+ (id != ghost).then(|| {+ SoupItem::Document(soup_document_uuid_with_updated(id, Default::default()))+ })+ })+ .collect();+ Box::pin(async move { Ok(items) })+ });+ soup_mock+ .expect_unexpanded_soup_by_ids()+ .times(1)+ .returning(move |params| {+ assert_eq!(params.entities.len(), 1);+ Box::pin(async move {+ Ok(vec![SoupItem::Project(models_soup::project::SoupProject {+ id: project,+ name: 'p'.to_string(),+ owner_id: MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap(),+ parent_id: None,+ created_at: Default::default(),+ updated_at: Default::default(),+ viewed_at: Default::default(),+ deleted_at: None,+ extra: (),+ })])+ })+ });+ soup_mock+ .expect_populate_properties()+ .times(1)+ .returning(|_, items| {+ Box::pin(async move {+ Ok(items+ .into_iter()+ .map(|item| item.map_extra(|()| SoupPropertiesField::default()))+ .collect())+ })+ });++ let page = SoupImpl::new(+ soup_mock,+ FrecencyQueryServiceImpl::new(MockFrecencyStorage::new()),+ NoopEmailPreviewService,+ RecordingCommsService::new(vec![]),+ NoopCallRecordQueryService,+ NoOpCrmService,+ NoopForeignEntityService,+ NoOpRemindersService,+ )+ .get_user_soup_with_properties(notified_request(3, vec![], EntityFilters::default()), None)+ .await+ .unwrap()+ .into_notified()+ .unwrap();++ let ids: Vec<Uuid> = page.items.iter().map(|item| item.item.id()).collect();+ assert_eq!(ids, vec![doc_1, project, doc_2]);+ // Every item carries the notification timestamp it was ordered on.+ let notified_at: Vec<_> = page.items.iter().map(|item| item.notified_at).collect();+ assert_eq!(+ notified_at,+ vec![+ Some(base + Days::new(5)),+ Some(base + Days::new(3)),+ Some(base + Days::new(2)),+ ]+ );+ // The second candidate page was short and fully consumed: feed exhausted.+ assert!(page.next_cursor.is_none());+}++#[tokio::test]+async fn notified_soup_full_page_builds_keyset_cursor() {+ let doc_1 = Uuid::from_u128(1);+ let doc_2 = Uuid::from_u128(2);+ let base: DateTime<Utc> = DateTime::default();++ let mut soup_mock = MockSoupRepo::new();+ soup_mock+ .expect_notified_soup_page()+ .times(1)+ .returning(move |_req| {+ Box::pin(async move {+ Ok(vec![+ notified(EntityType::Document, doc_1, base + Days::new(5)),+ notified(EntityType::Document, doc_2, base + Days::new(4)),+ ])+ })+ });+ 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_1, Default::default())),+ SoupItem::Document(soup_document_uuid_with_updated(doc_2, Default::default())),+ ])+ })+ });++ let page = SoupImpl::new(+ soup_mock,+ FrecencyQueryServiceImpl::new(MockFrecencyStorage::new()),+ NoopEmailPreviewService,+ RecordingCommsService::new(vec![]),+ NoopCallRecordQueryService,+ NoOpCrmService,+ NoopForeignEntityService,+ NoOpRemindersService,+ )+ .get_user_soup(notified_request(2, vec![], EntityFilters::default()), None)+ .await+ .unwrap()+ .into_notified()+ .unwrap();++ assert_eq!(page.items.len(), 2);+ // A full candidate page continues from the last walked candidate.+ let decoded: CursorWithValAndFilter<String, models_pagination::NotifiedAt, EntityFilters> =+ page.next_cursor.unwrap().decode_json().unwrap();+ assert_eq!(decoded.id, doc_2.to_string());+ assert_eq!(decoded.val.last_val, base + Days::new(4));+}++/// A run of candidates that all fail hydration must not loop forever: the+/// refill stops after the round cap and hands back a cursor so the client+/// can keep going.+#[tokio::test]+async fn notified_soup_caps_refill_rounds_and_keeps_a_cursor() {+ let ghost = Uuid::from_u128(9);+ let base: DateTime<Utc> = DateTime::default();++ let mut soup_mock = MockSoupRepo::new();+ let mut rounds = 0u64;+ soup_mock+ .expect_notified_soup_page()+ .times(MAX_NOTIFIED_FILL_ROUNDS)+ .returning(move |_req| {+ rounds += 1;+ let rows = vec![notified(+ EntityType::Document,+ ghost,+ base + Days::new(10 - rounds),+ )];+ Box::pin(async move { Ok(rows) })+ });+ soup_mock+ .expect_expanded_soup_by_ids()+ .times(MAX_NOTIFIED_FILL_ROUNDS)+ .returning(|_params| Box::pin(async move { Ok(Vec::new()) }));++ let page = SoupImpl::new(+ soup_mock,+ FrecencyQueryServiceImpl::new(MockFrecencyStorage::new()),+ NoopEmailPreviewService,+ RecordingCommsService::new(vec![]),+ NoopCallRecordQueryService,+ NoOpCrmService,+ NoopForeignEntityService,+ NoOpRemindersService,+ )+ .get_user_soup(notified_request(1, vec![], EntityFilters::default()), None)+ .await+ .unwrap()+ .into_notified()+ .unwrap();++ assert!(page.items.is_empty());+ let decoded: CursorWithValAndFilter<String, models_pagination::NotifiedAt, EntityFilters> =+ page.next_cursor.unwrap().decode_json().unwrap();+ assert_eq!(decoded.id, ghost.to_string());+ assert_eq!(+ decoded.val.last_val,+ base + Days::new(10 - MAX_NOTIFIED_FILL_ROUNDS as u64)+ );+}++/// Channel, channel-thread and email candidates hydrate through their own+/// legs with the request's tree ANDed onto the page's ids, under the+/// request's own view, so every such filter keeps applying to the notified+/// feed.+#[tokio::test]+async fn notified_soup_hydrates_channels_and_emails_with_the_request_tree() {+ let channel = Uuid::from_u128(5);+ let channel_thread = Uuid::from_u128(6);+ 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_notified_soup_page()+ .times(1)+ .returning(move |req| {+ assert!(req.hydratable.channels);+ assert!(req.hydratable.channel_threads);+ assert!(req.hydratable.email_threads);+ assert!(!req.hydratable.reminders);+ Box::pin(async move {+ Ok(vec![+ notified(EntityType::Channel, channel, base + Days::new(3)),+ notified(+ EntityType::ChannelMessage,+ channel_thread,+ base + Days::new(2),+ ),+ notified(EntityType::EmailThread, thread, 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 email_requests = email_service.requests.clone();+ let email_filters = email_service.filters.clone();+ let comms_service = RecordingCommsService::new(vec![]);++ let filters = EntityFilters {+ channel_filters: item_filters::ChannelFilters {+ notification_filters: item_filters::NotificationFilters {+ done: Some(false),+ ..Default::default()+ },+ ..Default::default()+ },+ channel_thread_filters: ChannelThreadFilters {+ participant_ids: vec!["macro|[email protected]".to_string()],+ ..Default::default()+ },+ email_filters: item_filters::EmailFilters {+ importance: Some(true),+ ..Default::default()+ },+ ..Default::default()+ };+ let _page = SoupImpl::new(+ soup_mock,+ FrecencyQueryServiceImpl::new(MockFrecencyStorage::new()),+ email_service,+ comms_service.clone(),+ NoopCallRecordQueryService,+ NoOpCrmService,+ NoopForeignEntityService,+ NoOpRemindersService,+ )+ .get_user_soup(notified_request(20, vec![link], filters), None)+ .await+ .unwrap();++ let channel_filters = comms_service.channel_filters();+ assert_eq!(channel_filters.len(), 1);+ assert!(channel_filters[0].contains("ChannelId"));+ assert!(channel_filters[0].contains(&channel.to_string()));+ assert!(channel_filters[0].contains("NotificationDone"));++ let thread_filters = comms_service.thread_filters();+ assert_eq!(thread_filters.len(), 1);+ assert!(thread_filters[0].contains("ThreadId"));+ assert!(thread_filters[0].contains(&channel_thread.to_string()));+ assert!(thread_filters[0].contains("Participant"));++ let recorded = email_requests.lock().unwrap();+ let (view, link_ids, limit) = recorded.first().expect("email hydration ran");+ // The request's own view, not the touched feed's unfiltered `All`.+ assert_eq!(+ view,+ &PreviewView::StandardLabel(email::domain::models::PreviewViewStandardLabel::Inbox)+ );+ assert_eq!(link_ids, &vec![link]);+ assert_eq!(limit, &Some(1));+ let email_filters = email_filters.lock().unwrap();+ assert!(email_filters[0].contains(&thread.to_string()));+ assert!(email_filters[0].contains("Importance"));+}++fn reminder_id_filters(ids: &[Uuid]) -> EntityFilters {+ EntityFilters {+ reminder_filters: item_filters::ReminderFilters {+ include: true,+ ids: ids.iter().map(Uuid::to_string).collect(),+ ..Default::default()+ },+ ..Default::default()+ }+}++/// A request naming reminder ids hydrates only the page candidates among+/// them: the leg is asked for the intersection.+#[tokio::test]+async fn notified_soup_narrows_the_reminder_leg_to_the_request_ids() {+ let named = Uuid::from_u128(11);+ let unnamed = Uuid::from_u128(12);+ let base: DateTime<Utc> = DateTime::default();++ let mut soup_mock = MockSoupRepo::new();+ soup_mock+ .expect_notified_soup_page()+ .times(1)+ .returning(move |req| {+ assert!(req.hydratable.reminders);+ Box::pin(async move {+ Ok(vec![+ notified(EntityType::Reminder, named, base + Days::new(2)),+ notified(EntityType::Reminder, unnamed, base + Days::new(1)),+ ])+ })+ });+ soup_mock+ .expect_expanded_soup_by_ids()+ .returning(|_params| Box::pin(async move { Ok(Vec::new()) }));++ let reminders_service = RecordingRemindersService::default();+ let queries = reminders_service.queries.clone();++ let _page = SoupImpl::new(+ soup_mock,+ FrecencyQueryServiceImpl::new(MockFrecencyStorage::new()),+ NoopEmailPreviewService,+ RecordingCommsService::new(vec![]),+ NoopCallRecordQueryService,+ NoOpCrmService,+ NoopForeignEntityService,+ reminders_service,+ )+ .get_user_soup(+ notified_request(20, vec![], reminder_id_filters(&[named])),+ None,+ )+ .await+ .unwrap();++ assert_eq!(*queries.lock().unwrap(), vec![(vec![named], 1)]);+}++/// When no page candidate is among the named ids the leg is skipped: an+/// empty id list means every reminder to the reminders service, which would+/// hydrate — and surface — the candidates the request excluded.+#[tokio::test]+async fn notified_soup_skips_the_reminder_leg_when_no_candidate_is_named() {+ let named = Uuid::from_u128(11);+ let unnamed = Uuid::from_u128(12);+ let base: DateTime<Utc> = DateTime::default();++ let mut soup_mock = MockSoupRepo::new();+ soup_mock+ .expect_notified_soup_page()+ .times(1)+ .returning(move |_req| {+ Box::pin(async move {+ Ok(vec![notified(+ EntityType::Reminder,+ unnamed,+ base + Days::new(1),+ )])+ })+ });+ soup_mock+ .expect_expanded_soup_by_ids()+ .returning(|_params| Box::pin(async move { Ok(Vec::new()) }));++ let reminders_service = RecordingRemindersService::default();+ let queries = reminders_service.queries.clone();++ let page = SoupImpl::new(+ soup_mock,+ FrecencyQueryServiceImpl::new(MockFrecencyStorage::new()),+ NoopEmailPreviewService,+ RecordingCommsService::new(vec![]),+ NoopCallRecordQueryService,+ NoOpCrmService,+ NoopForeignEntityService,+ reminders_service,+ )+ .get_user_soup(+ notified_request(20, vec![], reminder_id_filters(&[named])),+ None,+ )+ .await+ .unwrap()+ .into_notified()+ .unwrap();++ assert!(queries.lock().unwrap().is_empty());+ assert!(page.items.is_empty());+}++/// Calendar events hydrate by id through the main query, so only the+/// calendar literals the candidate query can fold are accepted.+#[tokio::test]+async fn notified_soup_rejects_unfoldable_calendar_filters() {+ let mut soup_mock = MockSoupRepo::new();+ soup_mock.expect_notified_soup_page().times(0);++ let filters = EntityFilters {+ calendar_event_filters: item_filters::CalendarEventFilters {+ organizers: vec!["[email protected]".to_string()],+ ..Default::default()+ },+ ..Default::default()+ };+ let err = SoupImpl::new(+ soup_mock,+ FrecencyQueryServiceImpl::new(MockFrecencyStorage::new()),+ NoopEmailPreviewService,+ RecordingCommsService::new(vec![]),+ NoopCallRecordQueryService,+ NoOpCrmService,+ NoopForeignEntityService,+ NoOpRemindersService,+ )+ .get_user_soup(notified_request(20, vec![], filters), None)+ .await+ .unwrap_err();++ assert_matches!(err, SoupErr::NotifiedUnsupportedFilter("calendar_event"));+}diff --git a/crates/soup/src/inbound/axum_router.rs b/crates/soup/src/inbound/axum_router.rsindex 6201f93c49..44f4f2a6bf 100644--- a/crates/soup/src/inbound/axum_router.rs+++ b/crates/soup/src/inbound/axum_router.rs@@ -1,8 +1,8 @@use crate::domain::{models::{- EnrichedSoupItem, FrecencyQueryInner, GroupedSortRequest, IntoSoupReqAst, SimpleQueryInner,- SoupErr, SoupItemWithProperties, SoupQuery, SoupRequest, SoupSortDirection, SoupType,- TouchedQueryInner,+ EnrichedSoupItem, FrecencyQueryInner, GroupedSortRequest, IntoSoupReqAst,+ NotifiedQueryInner, SimpleQueryInner, SoupErr, SoupItemWithProperties, SoupQuery,+ SoupRequest, SoupSortDirection, SoupType, TouchedQueryInner,grouping::{GroupMeta, build_grouped_response},},ports::SoupService,@@ -14,7 +14,7 @@ use axum::{response::IntoResponse,routing::{get, post},};-use axum_extra::either::Either3;+use axum_extra::either::Either4;use cowlike::CowLike;use email::{domain::{@@ -56,8 +56,8 @@ use model_entity::Entity;use model_error_response::ErrorResponse;use models_grouping::{GroupByField, GroupingConfig};use models_pagination::{- CursorWithValAndFilter, Frecency, PaginatedOpaqueCursor, SimpleSortMethod, TouchedByMe,- TypeEraseCursor,+ CursorWithValAndFilter, Frecency, NotifiedAt, PaginatedOpaqueCursor, SimpleSortMethod,+ TouchedByMe, TypeEraseCursor,};use non_empty::IsEmpty;use recursion::CollapsibleExt;@@ -84,7 +84,8 @@ pub struct Params {#[serde(default)]limit: Option<u16>,/// Sort method. Options are viewed_at, created_at, updated_at,- /// viewed_updated, frecency, touched_by_me. Defaults to viewed_at.+ /// viewed_updated, frecency, touched_by_me, notified_at. Defaults to+ /// viewed_at.#[serde(default)]sort_method: Option<SoupApiSort>,/// Sort direction. Options are asc, desc. Defaults to desc.@@ -136,6 +137,12 @@ pub enum SoupApiSort {/// Both a filter and an ordering: views don't count, and entities the/// caller never touched are absent.TouchedByMe,+ /// Only entities the caller holds a notification for, most recently+ /// notified first. Both a filter and an ordering: entities the caller+ /// was never notified about are absent. Thread-scoped channel+ /// notifications surface as channel-thread rows; calls and CRM+ /// companies never appear.+ NotifiedAt,}impl SoupApiSort {@@ -156,6 +163,7 @@ impl SoupApiSort {}SoupApiSort::Frecency => SoupQuery::new_sort_frecency(Frecency, filters),SoupApiSort::TouchedByMe => SoupQuery::new_sort_touched(filters),+ SoupApiSort::NotifiedAt => SoupQuery::new_sort_notified(filters),}}}@@ -552,23 +560,27 @@ where};let cursor: SoupQuery<R> = match cursor {- Either3::E1(l) => l+ Either4::E1(l) => l.map(SoupQuery::new_cursor_simple).unwrap_or_else(create_fallback),- Either3::E2(r) => r+ Either4::E2(r) => r.map(SoupQuery::new_cursor_frecency).unwrap_or_else(create_fallback),- Either3::E3(t) => t+ Either4::E3(t) => t.map(SoupQuery::new_cursor_touched).unwrap_or_else(create_fallback),+ Either4::E4(n) => n+ .map(SoupQuery::new_cursor_notified)+ .unwrap_or_else(create_fallback),};- // 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.+ // Frecency pages are ordered by relevance score, touched pages by the+ // caller's own latest mutation and notified pages by their latest+ // notification; none of those branches 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 *cursor* of those kinds is caught too, not just an initial+ // request naming the method.if sort_direction == SoupSortDirection::Asc {match &cursor {SoupQuery::Frecency(_) => {@@ -577,6 +589,9 @@ whereSoupQuery::Touched(_) => {return Err(SoupHandlerErr::AscendingTouchedUnsupported);}+ SoupQuery::Notified(_) => {+ return Err(SoupHandlerErr::AscendingNotifiedUnsupported);+ }SoupQuery::Simple(_) => {}}}@@ -703,6 +718,11 @@ pub struct SoupApiItem {/// ordered on this value, so it can be bumped optimistically.#[serde(skip_serializing_if = "Option::is_none")]touched_at: Option<chrono::DateTime<chrono::Utc>>,+ /// When the caller was last notified about this entity, present only+ /// when the page was ordered by `notified_at`. Clients keep the notified+ /// feed ordered and date-bucketed on this value.+ #[serde(skip_serializing_if = "Option::is_none")]+ notified_at: Option<chrono::DateTime<chrono::Utc>>,/// Whether the requesting user has favorited this entity.is_favorited: bool,}@@ -713,6 +733,7 @@ impl SoupApiItem {item,frecency_score,touched_at,+ notified_at,..} = item;SoupApiItem {@@ -721,6 +742,7 @@ impl SoupApiItem {.map(|f| f.data.frecency_score).unwrap_or_default(),touched_at,+ notified_at,is_favorited: false,}}@@ -784,6 +806,12 @@ pub enum SoupHandlerErr {/// 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),+ /// Ascending order was requested for a notified-at query.+ #[error("sort_direction=asc is not supported with sort_method=notified_at")]+ AscendingNotifiedUnsupported,+ /// A notified-at query carried a filter kind the mode cannot evaluate.+ #[error("sort_method=notified_at does not support {0} filters")]+ NotifiedUnsupportedFilter(&'static str),}impl From<SoupErr> for SoupHandlerErr {@@ -795,6 +823,9 @@ impl From<SoupErr> for SoupHandlerErr {SoupErr::TouchedUnsupportedFilter(kind) => {SoupHandlerErr::TouchedUnsupportedFilter(kind)}+ SoupErr::NotifiedUnsupportedFilter(kind) => {+ SoupHandlerErr::NotifiedUnsupportedFilter(kind)+ }err => SoupHandlerErr::Internal(err),}}@@ -807,7 +838,9 @@ impl IntoResponse for SoupHandlerErr {| SoupHandlerErr::Expand| SoupHandlerErr::AscendingFrecencyUnsupported| SoupHandlerErr::AscendingTouchedUnsupported- | SoupHandlerErr::TouchedUnsupportedFilter(_) => StatusCode::BAD_REQUEST,+ | SoupHandlerErr::TouchedUnsupportedFilter(_)+ | SoupHandlerErr::AscendingNotifiedUnsupported+ | SoupHandlerErr::NotifiedUnsupportedFilter(_) => StatusCode::BAD_REQUEST,SoupHandlerErr::CrmScopeForbidden | SoupHandlerErr::CrmAdminRequired => {StatusCode::FORBIDDEN}@@ -914,12 +947,15 @@ struct ApiSoupRequestInner<T> {email_view: PreviewView,}-type SoupCursor<R> = Either3<+type SoupCursor<R> = Either4<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>>,+ // Same value shape as the touched cursor; its `"notified_at"` sort+ // marker (touched serializes `null`) is what keeps it out of arm 3.+ Option<CursorWithValAndFilter<String, NotifiedAt, R>>,>;/// Gets the items the user has access to@@ -1260,6 +1296,11 @@ impl IntoSoupReqAst for SoupRequest<ApiEntityFilterAst> {SoupQuery::Touched(TouchedQueryInner(query)) => SoupQuery::Touched(TouchedQueryInner(query.try_map_filter(ApiEntityFilterAst::into_optional_entity_ast)?,)),+ SoupQuery::Notified(NotifiedQueryInner(query)) => {+ SoupQuery::Notified(NotifiedQueryInner(+ 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 7cd04eea07..2771e44417 100644--- a/crates/soup/src/inbound/axum_router/tests.rs+++ b/crates/soup/src/inbound/axum_router/tests.rs@@ -33,8 +33,9 @@ use crate::{domain::{models::{EnrichedSoupItem, FrecencyQueryInner, GroupedSortRequest, IntoSoupReqAst,- SimpleQueryInner, SoupErr, SoupPropertiesField, SoupQuery, SoupRequest,- SoupSortDirection, SoupType, TouchedQueryInner, grouping::ItemGroupingInfo,+ NotifiedQueryInner, SimpleQueryInner, SoupErr, SoupPropertiesField, SoupQuery,+ SoupRequest, SoupSortDirection, SoupType, TouchedQueryInner,+ grouping::ItemGroupingInfo,},ports::{SoupOutput, SoupService},},@@ -55,6 +56,8 @@ enum MockCursorKind {FrecencyCursor,TouchedSort,TouchedCursor,+ NotifiedSort,+ NotifiedCursor,}#[derive(Debug)]@@ -165,6 +168,12 @@ impl SoupService for MockSoup {SoupQuery::Touched(TouchedQueryInner(Query::Cursor(..))) => {MockCursorKind::TouchedCursor}+ SoupQuery::Notified(NotifiedQueryInner(Query::Sort(..))) => {+ MockCursorKind::NotifiedSort+ }+ SoupQuery::Notified(NotifiedQueryInner(Query::Cursor(..))) => {+ MockCursorKind::NotifiedCursor+ }};let user_id = req.user.to_string();let soup_type = req.soup_type;@@ -2041,3 +2050,102 @@ async fn ascending_touched_is_rejected() {}));}++/// notified_at selects the notified query mode from the plain sort param.+#[tokio::test]+async fn notified_at_sort_selects_notified_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": "notified_at" })).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::NotifiedSort));+}++/// A notified page cursor round-trips into the notified query mode. Its+/// value is a timestamp exactly like the touched cursor's, so this also+/// proves the `"notified_at"` sort marker keeps it out of the touched arm.+#[tokio::test]+async fn notified_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)),+ },+ );++ let cursor = models_pagination::Base64Str::encode_json(models_pagination::Cursor {+ id: Uuid::new_v4().to_string(),+ limit: 20usize,+ val: CursorVal {+ sort_type: models_pagination::NotifiedAt,+ last_val: chrono::DateTime::<chrono::Utc>::default(),+ },+ filter: EntityFilters::default(),+ })+ .type_erase();++ 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::NotifiedCursor));+}++/// Ascending notified order is rejected like frecency and touched: the+/// branch orders by the caller's latest notification and never applies the+/// merged sort a direction would flip.+#[tokio::test]+async fn ascending_notified_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": "notified_at",+ "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=notified_at"+ })+ );+}diff --git a/crates/soup/src/outbound/pg_soup_repo.rs b/crates/soup/src/outbound/pg_soup_repo.rsindex b14dfd085b..9006021a44 100644--- a/crates/soup/src/outbound/pg_soup_repo.rs+++ b/crates/soup/src/outbound/pg_soup_repo.rs@@ -1,9 +1,9 @@use crate::{domain::{models::{- AdvancedSortParams, GroupedSortRequest, SimpleSortQuery, SimpleSortRequest,- SoupProjectionHydration, SoupPropertiesField, TouchedEntity, TouchedSoupRequest,- grouping::ItemGroupingInfo,+ AdvancedSortParams, GroupedSortRequest, NotifiedEntity, NotifiedSoupRequest,+ SimpleSortQuery, SimpleSortRequest, SoupProjectionHydration, SoupPropertiesField,+ TouchedEntity, TouchedSoupRequest, grouping::ItemGroupingInfo,},ports::SoupRepo,},@@ -19,8 +19,10 @@ use readonly_pool::ReadOnlyPool;use system_properties::SystemPropertyKey;mod calendar_event;+mod candidate_gates;mod expanded;pub mod grouping;+mod notified;mod touched;mod unexpanded;@@ -241,6 +243,13 @@ impl SoupRepo for PgSoupRepo {) -> Result<Vec<TouchedEntity>, Self::Err> {touched::touched_soup_page(&self.pool.0, req).await}++ async fn notified_soup_page<'a>(+ &self,+ req: NotifiedSoupRequest<'a>,+ ) -> Result<Vec<NotifiedEntity>, Self::Err> {+ notified::notified_soup_page(&self.pool.0, req).await+ }}fn sort_and_truncate_hydrations(diff --git a/crates/soup/src/outbound/pg_soup_repo/candidate_gates.rs b/crates/soup/src/outbound/pg_soup_repo/candidate_gates.rsnew file mode 100644index 0000000000..aa5f230841--- /dev/null+++ b/crates/soup/src/outbound/pg_soup_repo/candidate_gates.rs@@ -0,0 +1,298 @@+//! Per-type `EXISTS` gates shared by the candidate queries (touched-by-me and+//! notified-at): does the candidate's entity exist, is it undeleted, can the+//! user see it, and does it satisfy the request's soup-owned filters.+//!+//! Every gate renders against `id_sql`, the SQL expression yielding the+//! candidate's entity id as TEXT (`ae.entity_id` for the activity log, the+//! `notified` CTE's derived key for notifications). Both candidate queries+//! bind the user id as `$1` and the caller's inbox link ids as `$5`, which+//! the gates reference directly.++use filter_ast::Expr;+use item_filters::ast::EntityFilterAst;+use item_filters::ast::channel::ChannelLiteral;+use item_filters::ast::email::EmailLiteral;+use item_filters::ast::properties::{PropertyEntityType, properties_filter_matches_propertyless};+use system_properties::SystemPropertyKey;+use uuid::Uuid;++use crate::outbound::pg_soup_repo::expanded::dynamic::{+ NotificationPredicate, access_semi_join, build_chat_filter, build_document_filter,+ build_notification_done_clause, build_notification_exists_clause,+ build_notification_seen_clause, 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,+};++/// Literals reachable through `And` alone: conditions the whole tree implies,+/// so applying them as a pre-filter can never drop a row the tree's full fold+/// would admit. `Or` and `Not` subtrees contribute nothing.+fn and_conjuncts<'a, T>(expr: &'a Expr<T>, out: &mut Vec<&'a T>) {+ match expr {+ Expr::And(a, b) => {+ and_conjuncts(a, out);+ and_conjuncts(b, out);+ }+ Expr::Literal(literal) => out.push(literal),+ Expr::Or(..) | Expr::Not(..) => {}+ }+}++/// Renders the implied conjuncts of `tree` that `fold` knows how to express+/// as ` AND ...` clauses, in tree order.+fn implied_conjuncts_sql<T>(tree: Option<&Expr<T>>, fold: impl Fn(&T) -> Option<String>) -> String {+ let Some(tree) = tree else {+ return String::new();+ };+ let mut literals = Vec::new();+ and_conjuncts(tree, &mut literals);+ literals+ .into_iter()+ .filter_map(|literal| fold(literal).map(|sql| format!(" AND {sql}")))+ .collect()+}++/// The per-type `EXISTS` gate for documents: exists, not deleted, accessible,+/// and matching the request's document + properties filters.+pub(super) fn document_gate(id_sql: &str, filter: Option<&EntityFilterAst>) -> String {+ let doc_filter = filter.and_then(|f| f.document_filter.as_deref());+ let props_filter = 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 = {id_sql}+ 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"),+ )+}++pub(super) fn chat_gate(id_sql: &str, filter: Option<&EntityFilterAst>) -> String {+ let chat_filter = filter.and_then(|f| f.chat_filter.as_deref());+ let props_filter = filter.and_then(|f| f.properties_filter.as_deref());+ format!(+ r#"EXISTS (+ SELECT 1 FROM "Chat" c+ WHERE c.id = {id_sql}+ 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"),+ )+}++pub(super) fn project_gate(id_sql: &str, filter: Option<&EntityFilterAst>) -> String {+ let project_filter = filter.and_then(|f| f.project_filter.as_deref());+ let props_filter = filter.and_then(|f| f.properties_filter.as_deref());+ format!(+ r#"EXISTS (+ SELECT 1 FROM "Project" p+ WHERE p.id = {id_sql}+ 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 `{id_sql}::uuid`: a malformed id in the+/// unconstrained TEXT column must drop that one row, not abort the whole+/// page with a cast error. Nested CASE because SQL `AND` does not guarantee+/// evaluation order, but CASE arms do.+pub(super) fn uuid_guarded(id_sql: &str, gate: String) -> String {+ format!(+ "CASE WHEN {id_sql} ~ '^[0-9a-fA-F]{{8}}-[0-9a-fA-F]{{4}}-[0-9a-fA-F]{{4}}-[0-9a-fA-F]{{4}}-[0-9a-fA-F]{{12}}$' THEN {gate} ELSE FALSE END"+ )+}++/// 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+/// [`includes_channels`], not per row.+///+/// The channel tree folds in the channels crate, which hydration applies in+/// full; the notification-state conjuncts it implies are pre-applied here so+/// a feed of done channels does not spend candidate slots on rows hydration+/// is about to drop. Only channel-level notifications count: mentions and+/// thread replies name a message as their secondary item and belong to that+/// thread's row, so a live mention must not keep a channel whose own+/// notifications are all done in the feed.+pub(super) fn channel_gate(id_sql: &str, filter: Option<&EntityFilterAst>) -> String {+ let implied = implied_conjuncts_sql(+ filter.and_then(|f| f.channel_filter.as_deref()),+ |literal| {+ let predicate = match literal {+ ChannelLiteral::NotificationDone(done) => NotificationPredicate::Done(*done),+ ChannelLiteral::NotificationSeen(seen) => NotificationPredicate::Seen(*seen),+ _ => return None,+ };+ Some(build_notification_exists_clause(+ "cp.channel_id",+ "channel",+ &format!(+ "{} AND n.secondary_event_item_type IS DISTINCT FROM 'channel_message'",+ predicate.sql()+ ),+ ))+ },+ );+ uuid_guarded(+ id_sql,+ format!(+ r#"EXISTS (+ SELECT 1 FROM comms_channel_participants cp+ WHERE cp.channel_id = {id_sql}::uuid+ AND cp.user_id = $1+ AND cp.left_at IS NULL+ {implied}+ )"#+ ),+ )+}++/// Channel-thread rows are root messages; one is visible iff it is undeleted+/// and the caller is an active participant of its channel. The thread tree+/// folds in the channels crate, which hydration applies in full.+pub(super) fn channel_thread_gate(id_sql: &str) -> String {+ uuid_guarded(+ id_sql,+ format!(+ r#"EXISTS (+ SELECT 1 FROM comms_messages m+ JOIN comms_channel_participants cp+ ON cp.channel_id = m.channel_id+ AND cp.user_id = $1+ AND cp.left_at IS NULL+ WHERE m.id = {id_sql}::uuid+ AND m.thread_id IS NULL+ AND m.deleted_at IS NULL+ )"#+ ),+ )+}++/// Email threads are inbox-scoped: visible iff the thread belongs to one of+/// the caller's readable links (own + delegated).+///+/// The email tree folds in the email crate, which hydration applies in full;+/// the importance and notification-state conjuncts it implies are+/// pre-applied here (same predicates as the email fold's `t.is_signal` and+/// notification `EXISTS`) so noise or done threads do not spend candidate+/// slots on rows hydration is about to drop.+pub(super) fn email_gate(id_sql: &str, filter: Option<&EntityFilterAst>) -> String {+ let props_filter = filter.and_then(|f| f.properties_filter.as_deref());+ let implied = implied_conjuncts_sql(+ filter.and_then(|f| f.email_filter.tree.as_deref()),+ |literal| match literal {+ EmailLiteral::Importance(true) => Some("et.is_signal".to_string()),+ EmailLiteral::Importance(false) => Some("NOT et.is_signal".to_string()),+ EmailLiteral::NotificationDone(done) => Some(build_notification_done_clause(+ "et.id",+ "email_thread",+ *done,+ )),+ EmailLiteral::NotificationSeen(seen) => Some(build_notification_seen_clause(+ "et.id",+ "email_thread",+ *seen,+ )),+ _ => None,+ },+ );+ uuid_guarded(+ id_sql,+ format!(+ r#"EXISTS (+ SELECT 1 FROM email_threads et+ WHERE et.id = {id_sql}::uuid+ AND et.link_id = ANY($5)+ {implied}+ {props_fold}+ )"#,+ props_fold = build_properties_filter(props_filter, "et.id::text"),+ ),+ )+}++/// Documents drop when their own filter can never match or when an active+/// properties filter can never match a document or task.+pub(super) fn includes_documents(filter: Option<&EntityFilterAst>) -> bool {+ !document_filter_is_impossible(filter.and_then(|f| f.document_filter.as_deref()))+ && properties_filter_can_apply_to(+ filter.and_then(|f| f.properties_filter.as_deref()),+ &[PropertyEntityType::Document, PropertyEntityType::Task],+ )+}++pub(super) fn includes_chats(filter: Option<&EntityFilterAst>) -> bool {+ !chat_filter_is_impossible(filter.and_then(|f| f.chat_filter.as_deref()))+ && properties_filter_can_apply_to(+ filter.and_then(|f| f.properties_filter.as_deref()),+ &[PropertyEntityType::Chat],+ )+}++pub(super) fn includes_projects(filter: Option<&EntityFilterAst>) -> bool {+ !project_filter_is_impossible(filter.and_then(|f| f.project_filter.as_deref()))+ && properties_filter_can_apply_to(+ filter.and_then(|f| f.properties_filter.as_deref()),+ &[PropertyEntityType::Project],+ )+}++/// 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).+pub(super) fn includes_channels(filter: Option<&EntityFilterAst>) -> bool {+ filter+ .and_then(|f| f.properties_filter.as_deref())+ .is_none_or(properties_filter_matches_propertyless)+}++/// Email threads need at least one readable inbox and a properties filter+/// that can match a thread.+pub(super) fn includes_email_threads(filter: Option<&EntityFilterAst>, link_ids: &[Uuid]) -> bool {+ !link_ids.is_empty()+ && properties_filter_can_apply_to(+ filter.and_then(|f| f.properties_filter.as_deref()),+ &[PropertyEntityType::Thread],+ )+}diff --git a/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic.rs b/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic.rsindex 88a9ac04f1..f7e6349e25 100644--- a/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic.rs+++ b/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic.rs@@ -527,7 +527,7 @@ static GROUPED_EMPTY_COMBINED_CLAUSE: &str = r#"WHERE false"#;-fn build_notification_exists_clause(+pub(in crate::outbound::pg_soup_repo) fn build_notification_exists_clause(entity_id_sql: &str,entity_type: &str,predicate_sql: &str,@@ -579,13 +579,13 @@ pub(in crate::outbound::pg_soup_repo) fn build_notification_seen_clause(}#[derive(Debug, Clone, Copy, PartialEq, Eq)]-enum NotificationPredicate {+pub(in crate::outbound::pg_soup_repo) enum NotificationPredicate {Done(bool),Seen(bool),}impl NotificationPredicate {- fn sql(self) -> &'static str {+ pub(in crate::outbound::pg_soup_repo) fn sql(self) -> &'static str {match self {NotificationPredicate::Done(true) => "un.done = true",NotificationPredicate::Done(false) => "un.done = false",@@ -1145,7 +1145,9 @@ pub(in crate::outbound::pg_soup_repo) fn project_filter_is_impossible(})}-fn calendar_event_filter_is_impossible(ast: Option<&Expr<CalendarEventLiteral>>) -> bool {+pub(in crate::outbound::pg_soup_repo) fn calendar_event_filter_is_impossible(+ ast: Option<&Expr<CalendarEventLiteral>>,+) -> bool {ast.is_some_and(|expr| {expr.collapse_frames(|frame| match frame {filter_ast::ExprFrame::And(a, b) => a || b,diff --git a/crates/soup/src/outbound/pg_soup_repo/notified.rs b/crates/soup/src/outbound/pg_soup_repo/notified.rsnew file mode 100644index 0000000000..7c2b74b226--- /dev/null+++ b/crates/soup/src/outbound/pg_soup_repo/notified.rs@@ -0,0 +1,327 @@+//! The notified-at candidate query: one keyset-paginated page of entities the+//! user holds a live notification for, newest notification first.+//!+//! Every candidate is gated on existence, deletion and access, and on the+//! request's filters where soup owns the fold (documents, chats, projects,+//! calendar events, properties). Channel, channel-thread, email,+//! foreign-entity and reminder candidates are gated on access (plus, for+//! channels and emails, the notification-state and importance conjuncts+//! their trees imply): their filter trees fold in their own domains' query+//! builders, so the service applies them in full when it hydrates the page+//! through those legs and drops the candidates that fail, refilling from the+//! next candidate page. A short page from this query therefore still means+//! the feed is exhausted, but a short hydrated page does not.+//!+//! A channel notification that names a message as its secondary event item+//! (mentions and thread replies) is keyed as a `channel_message` candidate on+//! that message — the thread root — because that is the row the client+//! attributes the notification to; the channel row keeps only channel-level+//! notifications.++use filter_ast::Expr;+use item_filters::ast::EntityFilterAst;+use item_filters::ast::calendar_event::CalendarEventLiteral;+use item_filters::ast::foreign_entity::ForeignEntityLiteral;+use item_filters::ast::properties::{PropertyEntityType, properties_filter_matches_propertyless};+use model_entity::EntityType;+use recursion::CollapsibleExt;+use sqlx::{PgPool, Row};+use std::str::FromStr;++use crate::domain::models::{NotifiedEntity, NotifiedSoupRequest};+use crate::outbound::pg_soup_repo::candidate_gates::{+ channel_gate, channel_thread_gate, chat_gate, document_gate, email_gate, includes_channels,+ includes_chats, includes_documents, includes_email_threads, includes_projects, project_gate,+ uuid_guarded,+};+use crate::outbound::pg_soup_repo::expanded::dynamic::{+ build_notification_done_clause, build_notification_seen_clause, build_properties_filter,+ calendar_event_filter_is_impossible, properties_filter_can_apply_to,+};+use crate::outbound::pg_soup_repo::type_err;++/// The candidate row's entity id, as the gates see it: the `notified` CTE's+/// derived key (thread root for thread-scoped channel notifications).+const ID_SQL: &str = "nc.entity_id";++/// Folds a calendar filter into SQL over the `event` alias. Only the literals+/// [`calendar_filter_supported_by_notified`] admits have a fold; anything+/// else fails closed, since the service already rejected such trees.+///+/// [`calendar_filter_supported_by_notified`]: crate::domain::models::calendar_filter_supported_by_notified+fn build_calendar_event_filter(tree: Option<&Expr<CalendarEventLiteral>>) -> String {+ let Some(tree) = tree else {+ return String::new();+ };+ let sql = tree.collapse_frames(|frame| match frame {+ filter_ast::ExprFrame::And(a, b) => format!("({a} AND {b})"),+ filter_ast::ExprFrame::Or(a, b) => format!("({a} OR {b})"),+ filter_ast::ExprFrame::Not(a) => format!("NOT ({a})"),+ filter_ast::ExprFrame::Literal(CalendarEventLiteral::Id(id)) => {+ format!("event.id = '{id}'")+ }+ filter_ast::ExprFrame::Literal(CalendarEventLiteral::NotificationDone(done)) => {+ build_notification_done_clause("event.id", "calendar_event", done)+ }+ filter_ast::ExprFrame::Literal(CalendarEventLiteral::NotificationSeen(seen)) => {+ build_notification_seen_clause("event.id", "calendar_event", seen)+ }+ filter_ast::ExprFrame::Literal(_) => "FALSE".to_string(),+ });+ format!(" AND {sql}")+}++/// Calendar events are owner- or delegation-scoped, never `entity_access`+/// rows: visible iff the caller owns the event or is delegated the inbox it+/// was synced from. Mirrors the calendar by-ids hydration's access check.+fn calendar_event_gate(filter: Option<&EntityFilterAst>) -> String {+ let calendar_filter = filter.and_then(|f| f.calendar_event_filter.as_deref());+ let props_filter = filter.and_then(|f| f.properties_filter.as_deref());+ uuid_guarded(+ ID_SQL,+ format!(+ r#"EXISTS (+ SELECT 1 FROM calendar_events event+ WHERE event.id = {ID_SQL}::uuid+ AND (+ event.owner_id = $1+ OR EXISTS (+ SELECT 1 FROM macro_user_links link+ WHERE link.link_id = event.source_link_id+ AND link.primary_macro_id = $1+ )+ )+ {calendar_fold}+ {props_fold}+ )"#,+ calendar_fold = build_calendar_event_filter(calendar_filter),+ props_fold = build_properties_filter(props_filter, "event.id::text"),+ ),+ )+}++/// Foreign entities are visible through the source they were stored for:+/// the caller themselves or a team they belong to (`$7`/`$8` are the+/// parallel id / auth-entity arrays of those sources).+fn foreign_entity_gate() -> String {+ uuid_guarded(+ ID_SQL,+ format!(+ r#"EXISTS (+ SELECT 1 FROM foreign_entity fe+ WHERE fe.id = {ID_SQL}::uuid+ AND EXISTS (+ SELECT 1 FROM unnest($7::text[], $8::text[]) AS source(id, auth_entity)+ WHERE source.id = fe.stored_for_id+ AND source.auth_entity = fe.stored_for_auth_entity+ )+ )"#+ ),+ )+}++/// Reminders are private to their owner, which is the whole access check.+fn reminder_gate() -> String {+ uuid_guarded(+ ID_SQL,+ format!(+ r#"EXISTS (+ SELECT 1 FROM reminder r+ WHERE r.id = {ID_SQL}::uuid+ AND r.user_id = $1+ )"#+ ),+ )+}++/// A foreign-entity tree that can never match: the nil-id opt-out the client+/// sends for entity types a view does not reference.+fn foreign_entity_filter_is_impossible(tree: Option<&Expr<ForeignEntityLiteral>>) -> bool {+ tree.is_some_and(|expr| {+ expr.collapse_frames(|frame| match frame {+ filter_ast::ExprFrame::And(a, b) => a || b,+ filter_ast::ExprFrame::Or(a, b) => a && b,+ filter_ast::ExprFrame::Not(_) => false,+ filter_ast::ExprFrame::Literal(ForeignEntityLiteral::Id(id)) => id.is_nil(),+ filter_ast::ExprFrame::Literal(_) => false,+ })+ })+}++/// Which entity types this request can include. Soup-folded types drop when+/// their own filter can never match or an active properties filter can never+/// match them; domain-hydrated types additionally need their leg to be+/// active for the request.+fn included_types(req: &NotifiedSoupRequest<'_>) -> Vec<&'static str> {+ let props = req.filter.and_then(|f| f.properties_filter.as_deref());+ let propertyless_ok = props.is_none_or(properties_filter_matches_propertyless);+ let mut types = Vec::with_capacity(8);+ if includes_documents(req.filter) {+ types.push(EntityType::Document.into());+ }+ if includes_chats(req.filter) {+ types.push(EntityType::Chat.into());+ }+ if includes_projects(req.filter) {+ types.push(EntityType::Project.into());+ }+ if req.hydratable.channels && includes_channels(req.filter) {+ types.push(EntityType::Channel.into());+ }+ if req.hydratable.channel_threads && propertyless_ok {+ types.push(EntityType::ChannelMessage.into());+ }+ if req.hydratable.email_threads && includes_email_threads(req.filter, req.link_ids) {+ types.push(EntityType::EmailThread.into());+ }+ if !calendar_event_filter_is_impossible(+ req.filter.and_then(|f| f.calendar_event_filter.as_deref()),+ ) && properties_filter_can_apply_to(props, &[PropertyEntityType::CalendarEvent])+ {+ types.push(EntityType::CalendarEvent.into());+ }+ // Foreign entities and reminders carry no properties, so the filter is+ // settled type-wide like it is for channels.+ if req.hydratable.foreign_entities+ && propertyless_ok+ && !req.foreign_entity_sources.is_empty()+ && !foreign_entity_filter_is_impossible(+ req.filter.and_then(|f| f.foreign_entity_filter.as_deref()),+ )+ {+ types.push(EntityType::ForeignEntity.into());+ }+ if req.hydratable.reminders && propertyless_ok {+ types.push(EntityType::Reminder.into());+ }+ types+}++/// Fetches one page of notified-at candidates.+///+/// Shape: keyset scan of the user's live notifications in `created_at DESC`+/// order over the `notified` CTE's derived entity key, keeping only each+/// entity's newest notification (the `NOT EXISTS` group-max), gated per+/// entity type on existence/deletion/access and the soup-owned filters so+/// `LIMIT` lands after gating.+///+/// `user_notification.created_at` is a naive `TIMESTAMP` written in UTC, so+/// the keyset binds and the returned value stay naive and are re-tagged as+/// UTC here rather than round-tripped through the session time zone.+#[tracing::instrument(err, skip(db, req))]+pub(super) async fn notified_soup_page(+ db: &PgPool,+ req: NotifiedSoupRequest<'_>,+) -> Result<Vec<NotifiedEntity>, sqlx::Error> {+ let types = included_types(&req);+ if types.is_empty() {+ return Ok(Vec::new());+ }++ 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+ ),+ notified AS NOT MATERIALIZED (+ SELECT+ un.created_at,+ n.id AS notification_id,+ CASE WHEN n.event_item_type = 'channel'+ AND n.secondary_event_item_type = 'channel_message'+ THEN 'channel_message' ELSE n.event_item_type+ END AS entity_type,+ CASE WHEN n.event_item_type = 'channel'+ AND n.secondary_event_item_type = 'channel_message'+ THEN n.secondary_event_item_id ELSE n.event_item_id+ END AS entity_id+ FROM user_notification un+ JOIN notification n ON n.id = un.notification_id+ WHERE un.user_id = $1+ AND un.deleted_at IS NULL+ )+ SELECT nc.entity_type, nc.entity_id, nc.created_at AS notified_at+ FROM notified nc+ WHERE nc.entity_type = ANY($2)+ AND ($3::timestamp IS NULL OR (nc.created_at, nc.entity_id) < ($3, $4))+ AND NOT EXISTS (+ SELECT 1 FROM notified newer+ WHERE newer.entity_type = nc.entity_type+ AND newer.entity_id = nc.entity_id+ AND (newer.created_at, newer.notification_id) > (nc.created_at, nc.notification_id)+ )+ AND CASE nc.entity_type+ WHEN 'document' THEN {document_gate}+ WHEN 'chat' THEN {chat_gate}+ WHEN 'project' THEN {project_gate}+ WHEN 'channel' THEN {channel_gate}+ WHEN 'channel_message' THEN {channel_thread_gate}+ WHEN 'email_thread' THEN {email_gate}+ WHEN 'calendar_event' THEN {calendar_event_gate}+ WHEN 'foreign_entity' THEN {foreign_entity_gate}+ WHEN 'reminder' THEN {reminder_gate}+ ELSE FALSE+ END+ ORDER BY nc.created_at DESC, nc.entity_id DESC+ LIMIT $6+ "#,+ document_gate = document_gate(ID_SQL, req.filter),+ chat_gate = chat_gate(ID_SQL, req.filter),+ project_gate = project_gate(ID_SQL, req.filter),+ channel_gate = channel_gate(ID_SQL, req.filter),+ channel_thread_gate = channel_thread_gate(ID_SQL),+ email_gate = email_gate(ID_SQL, req.filter),+ calendar_event_gate = calendar_event_gate(req.filter),+ foreign_entity_gate = foreign_entity_gate(),+ reminder_gate = reminder_gate(),+ );++ let after_ts = req.after.as_ref().map(|a| a.notified_at.naive_utc());+ let after_id = req.after.map(|a| a.entity_id);+ let source_ids: Vec<&str> = req+ .foreign_entity_sources+ .iter()+ .map(|source| source.id.as_str())+ .collect();+ let source_auth_entities: Vec<&str> = req+ .foreign_entity_sources+ .iter()+ .map(|source| source.auth_entity.as_str())+ .collect();++ 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)+ .bind(&source_ids)+ .bind(&source_auth_entities)+ // 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 notified_at: chrono::NaiveDateTime = row.try_get("notified_at")?;+ let entity_type = EntityType::from_str(&entity_type).map_err(type_err)?;+ Ok(NotifiedEntity {+ entity: entity_type.with_entity_string(entity_id),+ notified_at: notified_at.and_utc(),+ })+ })+ .fetch_all(db)+ .await+}++#[cfg(test)]+mod test;diff --git a/crates/soup/src/outbound/pg_soup_repo/notified/test.rs b/crates/soup/src/outbound/pg_soup_repo/notified/test.rsnew file mode 100644index 0000000000..5ffb91c968--- /dev/null+++ b/crates/soup/src/outbound/pg_soup_repo/notified/test.rs@@ -0,0 +1,438 @@+use super::*;+use crate::domain::models::{NotifiedHydratableTypes, NotifiedPagePosition};+use foreign_entity::domain::models::SourceId;+use item_filters::ast::EntityFilterAst;+use item_filters::ast::chat::ChatLiteral;+use item_filters::ast::document::DocumentLiteral;+use macro_db_migrator::MACRO_DB_MIGRATIONS;+use macro_user_id::user_id::MacroUserIdStr;+use sqlx::{Pool, Postgres};+use std::sync::Arc;+use uuid::Uuid;++const USER_1: &str = "macro|[email protected]";+const DOC_A: &str = "11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa";+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_M: &str = "99999999-aaaa-aaaa-aaaa-aaaaaaaaaaaa";+const THREAD_Z: &str = "44444444-aaaa-aaaa-aaaa-aaaaaaaaaaaa";+const EVENT_E1: &str = "66666666-aaaa-aaaa-aaaa-aaaaaaaaaaaa";+const EVENT_E3: &str = "66666666-cccc-cccc-cccc-cccccccccccc";+const PR_F1: &str = "77777777-aaaa-aaaa-aaaa-aaaaaaaaaaaa";+const PR_F3: &str = "77777777-cccc-cccc-cccc-cccccccccccc";+const REMINDER_R1: &str = "88888888-aaaa-aaaa-aaaa-aaaaaaaaaaaa";+const LINK_1: &str = "55555555-aaaa-aaaa-aaaa-aaaaaaaaaaaa";+const TEAM_T: &str = "eeeeeeee-1111-1111-1111-111111111111";+const CHANNEL_X_INVITE: &str = "0190a000-0000-7000-8000-000000000000";++const EVERYTHING: NotifiedHydratableTypes = NotifiedHydratableTypes {+ channels: true,+ channel_threads: true,+ email_threads: true,+ foreign_entities: true,+ reminders: true,+};++fn sources() -> Vec<SourceId> {+ vec![+ SourceId::user(USER_1),+ SourceId::team(Uuid::parse_str(TEAM_T).unwrap()),+ ]+}++fn req<'a>(+ filter: Option<&'a EntityFilterAst>,+ link_ids: &'a [Uuid],+ sources: &'a [SourceId],+ hydratable: NotifiedHydratableTypes,+) -> NotifiedSoupRequest<'a> {+ NotifiedSoupRequest {+ user_id: MacroUserIdStr::parse_from_str(USER_1).unwrap(),+ limit: 50,+ after: None,+ filter,+ link_ids,+ foreign_entity_sources: sources,+ hydratable,+ }+}++fn keys(page: &[NotifiedEntity]) -> Vec<(EntityType, String)> {+ page.iter()+ .map(|n| (n.entity.entity_type, n.entity.entity_id.to_string()))+ .collect()+}++fn minutes(page: &[NotifiedEntity]) -> Vec<u32> {+ use chrono::Timelike;+ page.iter().map(|n| n.notified_at.minute()).collect()+}++#[sqlx::test(+ fixtures(path = "../../../../fixtures", scripts("notified_at")),+ migrator = "MACRO_DB_MIGRATIONS"+)]+async fn feed_orders_by_latest_notification(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let link_ids = [Uuid::parse_str(LINK_1)?];+ let sources = sources();+ let page = notified_soup_page(&pool, req(None, &link_ids, &sources, EVERYTHING)).await?;++ // Exactly the visible entities user-1 holds a live notification for —+ // the deleted-row, deleted-doc, no-access, left-channel, foreign-inbox,+ // other-owner, call and malformed-id rows are all absent — latest+ // notification first. Delegated events and team-stored foreign entities+ // are visible through those access paths. doc-A's older mention must+ // not move it, user-2's newer notification about doc-A must not move+ // it, and chat-A's done row still counts. The thread mention is keyed on+ // its thread root, separately from the channel-level notification on+ // the same channel.+ assert_eq!(+ keys(&page),+ vec![+ (EntityType::CalendarEvent, EVENT_E3.to_string()),+ (EntityType::ForeignEntity, PR_F3.to_string()),+ (EntityType::Document, DOC_A.to_string()),+ (EntityType::Chat, CHAT_A.to_string()),+ (EntityType::Project, PROJECT_A.to_string()),+ (EntityType::ChannelMessage, THREAD_M.to_string()),+ (EntityType::CalendarEvent, EVENT_E1.to_string()),+ (EntityType::EmailThread, THREAD_Z.to_string()),+ (EntityType::ForeignEntity, PR_F1.to_string()),+ (EntityType::Reminder, REMINDER_R1.to_string()),+ (EntityType::Channel, CHANNEL_X.to_string()),+ ]+ );+ assert_eq!(minutes(&page), vec![20, 19, 9, 8, 7, 6, 5, 4, 3, 2, 0]);++ Ok(())+}++#[sqlx::test(+ fixtures(path = "../../../../fixtures", scripts("notified_at")),+ 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 sources = sources();++ let mut all = Vec::new();+ let mut after = None;+ loop {+ let mut request = req(None, &link_ids, &sources, EVERYTHING);+ request.limit = 3;+ request.after = after;+ let page = notified_soup_page(&pool, request).await?;+ let full = page.len() == 3;+ all.extend(page);+ if !full {+ break;+ }+ let last = all.last().unwrap();+ after = Some(NotifiedPagePosition {+ notified_at: last.notified_at,+ entity_id: last.entity.entity_id.to_string(),+ });+ }++ // Walking in pages of 3 yields the same feed as one big page.+ let one_page = notified_soup_page(&pool, req(None, &link_ids, &sources, EVERYTHING)).await?;+ assert_eq!(keys(&all), keys(&one_page));+ assert_eq!(all.len(), 11);++ Ok(())+}++#[sqlx::test(+ fixtures(path = "../../../../fixtures", scripts("notified_at")),+ migrator = "MACRO_DB_MIGRATIONS"+)]+async fn inactive_legs_drop_their_types(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let link_ids = [Uuid::parse_str(LINK_1)?];+ let sources = sources();+ let page = notified_soup_page(+ &pool,+ req(+ None,+ &link_ids,+ &sources,+ NotifiedHydratableTypes::default(),+ ),+ )+ .await?;++ // Only the soup-hydrated types remain when every domain leg is off.+ assert_eq!(+ keys(&page),+ vec![+ (EntityType::CalendarEvent, EVENT_E3.to_string()),+ (EntityType::Document, DOC_A.to_string()),+ (EntityType::Chat, CHAT_A.to_string()),+ (EntityType::Project, PROJECT_A.to_string()),+ (EntityType::CalendarEvent, EVENT_E1.to_string()),+ ]+ );++ Ok(())+}++#[sqlx::test(+ fixtures(path = "../../../../fixtures", scripts("notified_at")),+ migrator = "MACRO_DB_MIGRATIONS"+)]+async fn done_filters_exclude_without_moving_the_sort_key(+ pool: Pool<Postgres>,+) -> anyhow::Result<()> {+ let link_ids = [Uuid::parse_str(LINK_1)?];+ let sources = sources();++ // Not-done gates per type: chat-A's only notification is done, so it+ // drops; doc-A keeps its T9 key even though its T1 mention is done.+ let filter = EntityFilterAst {+ document_filter: Some(Arc::new(filter_ast::Expr::val(+ DocumentLiteral::NotificationDone(false),+ ))),+ chat_filter: Some(Arc::new(filter_ast::Expr::val(+ ChatLiteral::NotificationDone(false),+ ))),+ ..EntityFilterAst::default()+ };+ let page =+ notified_soup_page(&pool, req(Some(&filter), &link_ids, &sources, EVERYTHING)).await?;+ let keys = keys(&page);+ assert_eq!(keys[2], (EntityType::Document, DOC_A.to_string()));+ assert_eq!(minutes(&page)[2], 9);+ assert!(!keys.contains(&(EntityType::Chat, CHAT_A.to_string())));+ assert_eq!(page.len(), 10);++ Ok(())+}++#[sqlx::test(+ fixtures(path = "../../../../fixtures", scripts("notified_at")),+ migrator = "MACRO_DB_MIGRATIONS"+)]+async fn calendar_filter_folds_notification_state(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let link_ids = [Uuid::parse_str(LINK_1)?];+ let sources = sources();++ // Neither alarm is done, so asking for done calendar events drops both.+ let filter = EntityFilterAst {+ calendar_event_filter: Some(Arc::new(filter_ast::Expr::val(+ CalendarEventLiteral::NotificationDone(true),+ ))),+ ..EntityFilterAst::default()+ };+ let page =+ notified_soup_page(&pool, req(Some(&filter), &link_ids, &sources, EVERYTHING)).await?;+ assert!(!keys(&page).contains(&(EntityType::CalendarEvent, EVENT_E1.to_string())));+ assert!(!keys(&page).contains(&(EntityType::CalendarEvent, EVENT_E3.to_string())));+ assert_eq!(page.len(), 9);++ // Naming an event keeps it and drops the other.+ let filter = EntityFilterAst {+ calendar_event_filter: Some(Arc::new(filter_ast::Expr::val(CalendarEventLiteral::Id(+ Uuid::parse_str(EVENT_E1)?,+ )))),+ ..EntityFilterAst::default()+ };+ let page =+ notified_soup_page(&pool, req(Some(&filter), &link_ids, &sources, EVERYTHING)).await?;+ assert!(keys(&page).contains(&(EntityType::CalendarEvent, EVENT_E1.to_string())));+ assert!(!keys(&page).contains(&(EntityType::CalendarEvent, EVENT_E3.to_string())));+ assert_eq!(page.len(), 10);++ Ok(())+}++/// Email and channel trees fold at hydration, but the conjuncts they imply+/// (importance, notification state) pre-filter the candidates so the page+/// does not spend slots on rows hydration would drop. `Or` trees imply+/// nothing and leave the candidates alone.+#[sqlx::test(+ fixtures(path = "../../../../fixtures", scripts("notified_at")),+ migrator = "MACRO_DB_MIGRATIONS"+)]+async fn email_and_channel_conjuncts_prefilter_candidates(+ pool: Pool<Postgres>,+) -> anyhow::Result<()> {+ use item_filters::ast::EmailFilterAst;+ use item_filters::ast::channel::ChannelLiteral;+ use item_filters::ast::email::EmailLiteral;++ let link_ids = [Uuid::parse_str(LINK_1)?];+ let sources = sources();+ let thread = (EntityType::EmailThread, THREAD_Z.to_string());+ let channel = (EntityType::Channel, CHANNEL_X.to_string());+ let email_tree = |tree: filter_ast::Expr<EmailLiteral>| EmailFilterAst {+ tree: Some(Arc::new(tree)),+ crm_scope: None,+ };++ // Signal-shaped: not done AND important. thread-Z is signal and its+ // notification is live, so it stays.+ let filter = EntityFilterAst {+ email_filter: email_tree(filter_ast::Expr::and(+ filter_ast::Expr::val(EmailLiteral::NotificationDone(false)),+ filter_ast::Expr::val(EmailLiteral::Importance(true)),+ )),+ ..EntityFilterAst::default()+ };+ let page =+ notified_soup_page(&pool, req(Some(&filter), &link_ids, &sources, EVERYTHING)).await?;+ assert!(keys(&page).contains(&thread));++ // Noise-shaped: not done AND not important. thread-Z is signal, so the+ // importance conjunct drops it before hydration.+ let filter = EntityFilterAst {+ email_filter: email_tree(filter_ast::Expr::and(+ filter_ast::Expr::val(EmailLiteral::NotificationDone(false)),+ filter_ast::Expr::val(EmailLiteral::Importance(false)),+ )),+ ..EntityFilterAst::default()+ };+ let page =+ notified_soup_page(&pool, req(Some(&filter), &link_ids, &sources, EVERYTHING)).await?;+ assert!(!keys(&page).contains(&thread));+ assert_eq!(page.len(), 10);++ // Done-only channels: channel-X's channel-level notification is live, so+ // the channel row drops; the thread row is gated by the thread tree, so+ // it stays.+ let filter = EntityFilterAst {+ channel_filter: Some(Arc::new(filter_ast::Expr::val(+ ChannelLiteral::NotificationDone(true),+ ))),+ ..EntityFilterAst::default()+ };+ let page =+ notified_soup_page(&pool, req(Some(&filter), &link_ids, &sources, EVERYTHING)).await?;+ assert!(!keys(&page).contains(&channel));+ assert!(keys(&page).contains(&(EntityType::ChannelMessage, THREAD_M.to_string())));+ assert_eq!(page.len(), 10);++ // An `Or` implies neither branch, so nothing is pre-filtered.+ let filter = EntityFilterAst {+ email_filter: email_tree(filter_ast::Expr::or(+ filter_ast::Expr::val(EmailLiteral::Importance(false)),+ filter_ast::Expr::val(EmailLiteral::NotificationDone(true)),+ )),+ ..EntityFilterAst::default()+ };+ let page =+ notified_soup_page(&pool, req(Some(&filter), &link_ids, &sources, EVERYTHING)).await?;+ assert!(keys(&page).contains(&thread));+ assert_eq!(page.len(), 11);++ Ok(())+}++/// The channel gate's notification conjuncts see channel-level notifications+/// only: a mention names its thread as the secondary item and is that+/// thread row's notification, so once the channel's own notifications are+/// done a still-live mention must not keep the channel row in a not-done+/// feed — the mention shows once, on the thread row.+#[sqlx::test(+ fixtures(path = "../../../../fixtures", scripts("notified_at")),+ migrator = "MACRO_DB_MIGRATIONS"+)]+async fn channel_conjuncts_ignore_thread_scoped_notifications(+ pool: Pool<Postgres>,+) -> anyhow::Result<()> {+ use item_filters::ast::channel::ChannelLiteral;++ sqlx::query("UPDATE user_notification SET done = TRUE WHERE notification_id = $1::uuid")+ .bind(CHANNEL_X_INVITE)+ .execute(&pool)+ .await?;++ let link_ids = [Uuid::parse_str(LINK_1)?];+ let sources = sources();+ let filter = EntityFilterAst {+ channel_filter: Some(Arc::new(filter_ast::Expr::val(+ ChannelLiteral::NotificationDone(false),+ ))),+ ..EntityFilterAst::default()+ };+ let page =+ notified_soup_page(&pool, req(Some(&filter), &link_ids, &sources, EVERYTHING)).await?;+ assert!(!keys(&page).contains(&(EntityType::Channel, CHANNEL_X.to_string())));+ assert!(keys(&page).contains(&(EntityType::ChannelMessage, THREAD_M.to_string())));+ assert_eq!(page.len(), 10);++ Ok(())+}++#[test]+fn every_type_with_every_leg_active() {+ let link_ids = [Uuid::from_u128(1)];+ let sources = sources();+ let types = included_types(&req(None, &link_ids, &sources, EVERYTHING));+ assert_eq!(+ types,+ vec![+ "document",+ "chat",+ "project",+ "channel",+ "channel_message",+ "email_thread",+ "calendar_event",+ "foreign_entity",+ "reminder",+ ]+ );+}++#[test]+fn inactive_legs_and_missing_scopes_drop_types() {+ let link_ids = [Uuid::from_u128(1)];+ let sources = sources();+ let types = included_types(&req(+ None,+ &link_ids,+ &sources,+ NotifiedHydratableTypes::default(),+ ));+ assert_eq!(types, vec!["document", "chat", "project", "calendar_event"]);++ // An active email leg without inboxes still has no threads to gate on.+ let types = included_types(&req(None, &[], &sources, EVERYTHING));+ assert!(!types.contains(&"email_thread"));++ // An active foreign-entity leg without sources has nothing to gate on.+ let types = included_types(&req(None, &link_ids, &[], EVERYTHING));+ assert!(!types.contains(&"foreign_entity"));+}++#[test]+fn nil_id_foreign_entity_filter_drops_foreign_entities() {+ let link_ids = [Uuid::from_u128(1)];+ let sources = sources();+ // The client's opt-out for an unreferenced entity type.+ let filter = EntityFilterAst {+ foreign_entity_filter: Some(Arc::new(filter_ast::Expr::val(ForeignEntityLiteral::Id(+ Uuid::nil(),+ )))),+ ..EntityFilterAst::default()+ };+ let types = included_types(&req(Some(&filter), &link_ids, &sources, EVERYTHING));+ assert!(!types.contains(&"foreign_entity"));+ assert!(types.contains(&"document"));+}++#[test]+fn calendar_fold_renders_supported_literals() {+ let tree = filter_ast::Expr::and(+ filter_ast::Expr::val(CalendarEventLiteral::Id(Uuid::from_u128(7))),+ filter_ast::Expr::is_not(filter_ast::Expr::val(+ CalendarEventLiteral::NotificationDone(true),+ )),+ );+ let sql = build_calendar_event_filter(Some(&tree));+ assert!(sql.starts_with(" AND ("));+ assert!(sql.contains("event.id = '00000000-0000-0000-0000-000000000007'"));+ assert!(sql.contains("NOT (EXISTS ("));+ assert!(sql.contains("un.done = true"));+ assert_eq!(build_calendar_event_filter(None), "");+}diff --git a/crates/soup/src/outbound/pg_soup_repo/touched.rs b/crates/soup/src/outbound/pg_soup_repo/touched.rsindex 92aaa7098f..e7a25c9412 100644--- a/crates/soup/src/outbound/pg_soup_repo/touched.rs+++ b/crates/soup/src/outbound/pg_soup_repo/touched.rs@@ -7,21 +7,20 @@//! 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::candidate_gates::{+ channel_gate, chat_gate, document_gate, email_gate, includes_channels, includes_chats,+ includes_documents, includes_email_threads, includes_projects, project_gate,};use crate::outbound::pg_soup_repo::type_err;+/// The candidate row's entity id, as the gates see it.+const ID_SQL: &str = "ae.entity_id";+/// 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 {@@ -32,127 +31,6 @@ fn view_tags_sql() -> String {.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@@ -161,35 +39,20 @@ fn email_gate(req: &TouchedSoupRequest<'_>) -> String {////// [`SoupErr::TouchedUnsupportedFilter`]: crate::domain::models::SoupErr::TouchedUnsupportedFilterfn 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],- )- {+ if includes_documents(req.filter) {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])- {+ if includes_chats(req.filter) {types.push(EntityType::Chat.into());}- if !project_filter_is_impossible(req.filter.and_then(|f| f.project_filter.as_deref()))- && properties_filter_can_apply_to(props, &[PropertyEntityType::Project])- {+ if includes_projects(req.filter) {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) {+ if includes_channels(req.filter) {types.push(EntityType::Channel.into());}- if properties_filter_can_apply_to(props, &[PropertyEntityType::Thread])- && !req.link_ids.is_empty()- {+ if includes_email_threads(req.filter, req.link_ids) {types.push(EntityType::EmailThread.into());}types@@ -248,11 +111,11 @@ pub(super) async fn touched_soup_page(ORDER BY ae.occurred_at DESC, ae.entity_id DESCLIMIT $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),+ document_gate = document_gate(ID_SQL, req.filter),+ chat_gate = chat_gate(ID_SQL, req.filter),+ project_gate = project_gate(ID_SQL, req.filter),+ channel_gate = channel_gate(ID_SQL, req.filter),+ email_gate = email_gate(ID_SQL, req.filter),);let after_ts = req.after.as_ref().map(|a| a.occurred_at);diff --git a/docs/AGENT_GUIDE/surfaces.md b/docs/AGENT_GUIDE/surfaces.mdindex ba55f73e3d..d6583df2ba 100644--- a/docs/AGENT_GUIDE/surfaces.md+++ b/docs/AGENT_GUIDE/surfaces.md@@ -5,8 +5,10 @@Unified triage list (emails, channel messages, task assignments, doc mentions, agentresults). Filter radios: `Signal` (default, AI-filtered "needs attention") / `Noise` / `All`/ `Reminders`; plus `Filter` menu and `Preview` toggle (split list + preview pane; the empty-preview shows "No content selected"). Keyboard: `j`/`k` move, `space` preview, `enter` open,-`e` mark done. Rows are buttons named `<channel> <sender>:<snippet> <time>`.+preview shows "No content selected"). `Signal` and `Noise` order rows (and their date+headers) by when you were last notified about the item, so a fresh comment on an old task+sits under "Today". `All` and `Reminders` order by recency. Keyboard: `j`/`k` move, `space` preview, `enter` open, `e` mark done.+Rows are buttons named `<channel> <sender>:<snippet> <time>`.## Email — `/app/component/mail`diff --git a/packages/sdk/generated/storage/types.gen.ts b/packages/sdk/generated/storage/types.gen.tsindex 12ede791e6..26cece0959 100644--- a/packages/sdk/generated/storage/types.gen.ts+++ b/packages/sdk/generated/storage/types.gen.ts@@ -7234,6 +7234,12 @@ export type SoupApiItem = SoupItem & {* Whether the requesting user has favorited this entity.*/is_favorited: boolean;+ /**+ * When the caller was last notified about this entity, present only+ * when the page was ordered by `notified_at`. Clients keep the notified+ * feed ordered and date-bucketed on this value.+ */+ notified_at?: string | null;/*** The caller's latest own mutation of this entity, present only when the* page was ordered by `touched_by_me`. Clients keep the touched feed@@ -7245,7 +7251,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' | 'touched_by_me';+export type SoupApiSort = 'viewed_at' | 'created_at' | 'updated_at' | 'viewed_updated' | 'frecency' | 'touched_by_me' | 'notified_at';/*** Sort direction accepted by non-grouped soup API endpoints.@@ -12887,7 +12893,8 @@ export type GetItemsSoupData = {limit?: number;/*** Sort method. Options are viewed_at, created_at, updated_at,- * viewed_updated, frecency, touched_by_me. Defaults to viewed_at.+ * viewed_updated, frecency, touched_by_me, notified_at. Defaults to+ * viewed_at.*/sort_method?: SoupApiSort;/**diff --git a/packages/sdk/specs/storage.json b/packages/sdk/specs/storage.jsonindex fc69b1898b..05fcd8a2db 100644--- a/packages/sdk/specs/storage.json+++ b/packages/sdk/specs/storage.json@@ -9546,7 +9546,7 @@{"name": "sort_method","in": "query",- "description": "Sort method. Options are viewed_at, created_at, updated_at,\nviewed_updated, frecency, touched_by_me. Defaults to viewed_at.",+ "description": "Sort method. Options are viewed_at, created_at, updated_at,\nviewed_updated, frecency, touched_by_me, notified_at. Defaults to\nviewed_at.","required": false,"schema": {"$ref": "#/components/schemas/SoupApiSort"@@ -21972,7 +21972,7 @@},{"$ref": "#/components/schemas/SoupApiSort",- "description": "Sort method. Options are viewed_at, created_at, updated_at,\nviewed_updated, frecency, touched_by_me. Defaults to viewed_at."+ "description": "Sort method. Options are viewed_at, created_at, updated_at,\nviewed_updated, frecency, touched_by_me, notified_at. Defaults to\nviewed_at."}]}@@ -24033,6 +24033,11 @@"type": "boolean","description": "Whether the requesting user has favorited this entity."},+ "notified_at": {+ "type": ["string", "null"],+ "format": "date-time",+ "description": "When the caller was last notified about this entity, present only\nwhen the page was ordered by `notified_at`. Clients keep the notified\nfeed ordered and date-bucketed on this value."+ },"touched_at": {"type": ["string", "null"],"format": "date-time",@@ -24052,7 +24057,8 @@"updated_at","viewed_updated","frecency",- "touched_by_me"+ "touched_by_me",+ "notified_at"]},"SoupApiSortDirection": {