Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(calendar): group Home reminders by delivery time (#6681)

teof2af3ba23789
diff --git a/apps/web/src/lib/queries/soup/calendar-entity.test.ts b/apps/web/src/lib/queries/soup/calendar-entity.test.ts
new file mode 100644
index 0000000000..d7430fc52e
--- /dev/null
+++ b/apps/web/src/lib/queries/soup/calendar-entity.test.ts
@@ -0,0 +1,157 @@
+import {
+ groupHomeEntitiesByDate,
+ inboxGroupTimestamp,
+ mergeHomeEntities,
+} from '@app/features/inbox-view/queries/inbox-results';
+import type { Notification } from '@entity/types/notification';
+import type { SoupApiItem } from '@service-storage/generated/schemas';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+import {
+ clearNotifiedFloors,
+ raiseNotifiedFloor,
+} from './normalized-cache/notified-floor';
+import { mapApiSoupItemToEntity } from './transform-utils';
+
+vi.mock('@core/constant/allBlocks', () => ({
+ blockNameToDefaultFile: {},
+ itemToSafeName: vi.fn(),
+}));
+vi.mock('@core/context/channels', () => ({ useChannelsContext: vi.fn() }));
+vi.mock('@core/user', () => ({ emailToId: vi.fn() }));
+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 fridayReminder = '2026-09-18T13:50:00Z';
+const recentSync = '2026-09-20T17:40:00Z';
+const now = new Date('2026-09-20T18:00:00Z');
+const context = {
+ tab: 'signal' as const,
+ capabilities: {
+ calendar: true,
+ foreignEntities: false,
+ notifiedSort: false,
+ reminders: false,
+ snippets: false,
+ },
+};
+
+function reminder(createdAt = fridayReminder): Notification {
+ return {
+ id: `reminder-${createdAt}`,
+ entity_id: 'event',
+ entity_type: 'calendar_event',
+ notification_event_type: 'calendar_event_reminder',
+ notification_metadata: {
+ tag: 'calendar_event_reminder',
+ content: {
+ eventId: 'event',
+ occurrenceKey: '2026-09-18T14:00:00Z',
+ startsAt: '2026-09-18T14:00:00Z',
+ title: 'Friday deployment',
+ minutesBefore: 10,
+ },
+ },
+ sent: true,
+ state: 'unseen',
+ created_at: createdAt,
+ updated_at: recentSync,
+ };
+}
+
+function calendarItem(notifications: Notification[] = [reminder()]) {
+ return {
+ tag: 'calendarEvent',
+ frecency_score: 0,
+ is_favorited: false,
+ data: {
+ id: 'event',
+ title: 'Friday deployment',
+ ownerId: 'macro|[email protected]',
+ status: 'confirmed',
+ time: {
+ kind: 'timed',
+ startsAt: '2026-09-18T14:00:00Z',
+ endsAt: '2026-09-18T15:00:00Z',
+ },
+ isReadOnly: false,
+ createdAt: '2026-09-01T12:00:00Z',
+ updatedAt: recentSync,
+ icalUid: 'event',
+ transparency: 'opaque',
+ visibility: 'default',
+ extra: { properties: [] },
+ notifications,
+ },
+ } satisfies SoupApiItem & { data: { notifications: Notification[] } };
+}
+
+afterEach(clearNotifiedFloors);
+
+describe('calendar reminder dates in Home', () => {
+ it('keeps a cold Friday reminder out of Last hour after a recent calendar sync', () => {
+ // Mapped GraphQL data carries notifications but no notified_at while
+ // the server's notification sort is disabled.
+ const entity = mapApiSoupItemToEntity(calendarItem());
+ const rows = mergeHomeEntities([entity], [], context);
+ expect(rows[0].sortTs).toBe(fridayReminder);
+ expect(groupHomeEntitiesByDate(rows, now)[0].label).toBe('Last 7 days');
+ expect(entity.updatedAt).toBe(recentSync);
+ });
+
+ it('uses the latest reminder delivery regardless of array order or read/done state', () => {
+ const latest = '2026-09-19T13:50:00Z';
+ const item = calendarItem([
+ { ...reminder(latest), state: 'done' },
+ { ...reminder(fridayReminder), state: 'seen' },
+ { ...reminder(recentSync), deleted_at: recentSync },
+ reminder('invalid date'),
+ ]);
+ // The canonical master start can predate a recurring occurrence.
+ item.data.time.startsAt = '2026-09-01T14:00:00Z';
+ expect(mapApiSoupItemToEntity(item).notifiedAt).toBe(latest);
+ });
+
+ it('also uses the REST reminder delivery stamp when notifications are absent', () => {
+ const item = calendarItem([]);
+ expect(
+ mapApiSoupItemToEntity({
+ ...item,
+ data: { ...item.data, lastReminderFiredAt: fridayReminder },
+ }).notifiedAt
+ ).toBe(fridayReminder);
+ });
+
+ it('preserves explicit server notification ordering and newer live notifications', () => {
+ const item = { ...calendarItem(), notified_at: '2026-09-17T12:00:00Z' };
+ expect(mapApiSoupItemToEntity(item).notifiedAt).toBe(item.notified_at);
+ raiseNotifiedFloor('event', recentSync);
+ expect(mapApiSoupItemToEntity(item).notifiedAt).toBe(recentSync);
+ });
+
+ it('keeps content recency when no reminder timestamp is available', () => {
+ const entity = mapApiSoupItemToEntity(calendarItem([]));
+ expect(entity.notifiedAt).toBeUndefined();
+ expect(inboxGroupTimestamp(entity, context)).toBe(recentSync);
+ });
+
+ it('preserves own-activity ordering and non-notification tabs', () => {
+ const entity = mapApiSoupItemToEntity({
+ ...calendarItem(),
+ touched_at: recentSync,
+ });
+ expect(mergeHomeEntities([entity], [entity], context)[0].sortTs).toBe(
+ recentSync
+ );
+ expect(inboxGroupTimestamp(entity, { ...context, tab: 'reminders' })).toBe(
+ recentSync
+ );
+ });
+});
diff --git a/apps/web/src/lib/queries/soup/transform-utils.ts b/apps/web/src/lib/queries/soup/transform-utils.ts
index e712455429..8959a0eaa4 100644
--- a/apps/web/src/lib/queries/soup/transform-utils.ts
+++ b/apps/web/src/lib/queries/soup/transform-utils.ts
@@ -668,6 +668,33 @@ function withRawNotifications<T extends SoupEntity>(
return { ...entity, notifications } as T;
}
+function calendarReminderTimestamp(
+ item: Extract<DisplayableSoupItem, { tag: 'calendarEvent' }>
+): string | undefined {
+ let latest: string | undefined;
+ let latestMs = -Infinity;
+ const include = (timestamp: string | null | undefined) => {
+ const ms = timestamp ? Date.parse(timestamp) : NaN;
+ if (ms > latestMs) {
+ latest = timestamp ?? undefined;
+ latestMs = ms;
+ }
+ };
+
+ include(item.data.lastReminderFiredAt);
+ const notifications = (item as SoupItemWithOptionalNotifications).data
+ .notifications;
+ for (const notification of notifications ?? []) {
+ if (
+ !notification.deleted_at &&
+ notification.notification_metadata.tag === 'calendar_event_reminder'
+ ) {
+ include(notification.created_at);
+ }
+ }
+ return latest;
+}
+
type ReferencedEntityType = NonNullable<
ReminderEntity['referencedEntity']
>['type'];
@@ -1015,10 +1042,15 @@ 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);
+ // Calendar sync can update old events long after their reminders fired.
+ // Without the server's notified_at sort, use the attached reminder's
+ // delivery time (or REST delivery stamp) rather than that metadata update.
+ // The explicit server stamp and newer websocket floor still take priority.
+ const notifiedAt = resolveNotifiedAt(
+ entity.id,
+ item.notified_at ??
+ (item.tag === 'calendarEvent' ? calendarReminderTimestamp(item) : null)
+ );
const notified = notifiedAt ? { ...touched, notifiedAt } : touched;
return withRawNotifications(notified, item);
diff --git a/docs/AGENT_GUIDE/surfaces.md b/docs/AGENT_GUIDE/surfaces.md
index 861e3c3b2f..35370d6c11 100644
--- a/docs/AGENT_GUIDE/surfaces.md
+++ b/docs/AGENT_GUIDE/surfaces.md
@@ -186,6 +186,12 @@ source shows a retry notice while the other source stays usable. Document typing
alone is not yet attributed by Activity; Home reflects the actions the existing
Activity system records.
+Calendar reminder rows use the reminder delivery time for Home's date section,
+including on a cold GraphQL load with notification sorting disabled. Verify that
+an older reminder stays in its older section after the event's metadata syncs;
+opening its details should show the expected occurrence. A newer reminder or your
+own later activity can move the row forward, but a calendar sync alone should not.
+
On mobile, this route always renders the original Notifications soup view,
regardless of the new-app-views flag. The dock and search scope use the bell icon
and **Notifications** label; Home is desktop-only. Notifications uses the existing