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(search): make calendar events searchable by title (#5868)
gbirmanca0f898d7007
diff --git a/.github/kafka-cluster-topics.json b/.github/kafka-cluster-topics.jsonindex 9ed32644b8..9a9c86f5d1 100644--- a/.github/kafka-cluster-topics.json+++ b/.github/kafka-cluster-topics.json@@ -1,6 +1,7 @@["macro.agent_sessions","macro.bots",+ "macro.calendar","macro.calls","macro.channels","macro.chats",diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.jsonindex 379f5d159f..89d34bd2b7 100644--- a/.github/workspace-dep-closures.json+++ b/.github/workspace-dep-closures.json@@ -6054,6 +6054,7 @@"crates/bot_id","crates/bot_token","crates/broadcast",+ "crates/calendar_events","crates/call","crates/channel_sender","crates/channels",diff --git a/.sqlx/query-7560f5929cf5e8b1cd8b7afd4cad52a6c1fd612db386416870c4b6bca7f97421.json b/.sqlx/query-09567c7fdbca8c652db4d0037a498bf203a27e6d08737a809ae77efc4dd76680.jsonsimilarity index 88%rename from .sqlx/query-7560f5929cf5e8b1cd8b7afd4cad52a6c1fd612db386416870c4b6bca7f97421.jsonrename to .sqlx/query-09567c7fdbca8c652db4d0037a498bf203a27e6d08737a809ae77efc4dd76680.jsonindex 242f4f5c93..d731c73ce0 100644--- a/.sqlx/query-7560f5929cf5e8b1cd8b7afd4cad52a6c1fd612db386416870c4b6bca7f97421.json+++ b/.sqlx/query-09567c7fdbca8c652db4d0037a498bf203a27e6d08737a809ae77efc4dd76680.json@@ -1,12 +1,17 @@{"db_name": "PostgreSQL",- "query": "\n INSERT INTO calendar_events (\n id, owner_id, source_link_id, ical_uid, title, description, location,\n status, visibility, transparency,\n starts_at, ends_at, start_date, end_date, time_zone,\n recurrence_lines, organizer_email, organizer_name,\n conference_url, conference_provider, sequence, is_read_only,\n canonical_source_kind,\n canonical_source_updated_at,\n reminders_use_default, reminder_overrides,\n created_at, updated_at\n )\n VALUES (\n $1, $2, $3, $4, $5, $6, $7,\n $8, $9, $10,\n $11, $12, $13, $14, $15,\n $16, $17, $18,\n $19, $27, $20, $21, $22, $24,\n $25, $26,\n $23, $24\n )\n ON CONFLICT (owner_id, source_link_id, ical_uid) DO UPDATE SET\n title = EXCLUDED.title,\n description = EXCLUDED.description,\n location = EXCLUDED.location,\n status = EXCLUDED.status,\n visibility = EXCLUDED.visibility,\n transparency = EXCLUDED.transparency,\n starts_at = EXCLUDED.starts_at,\n ends_at = EXCLUDED.ends_at,\n start_date = EXCLUDED.start_date,\n end_date = EXCLUDED.end_date,\n time_zone = EXCLUDED.time_zone,\n recurrence_lines = EXCLUDED.recurrence_lines,\n organizer_email = EXCLUDED.organizer_email,\n organizer_name = EXCLUDED.organizer_name,\n conference_url = EXCLUDED.conference_url,\n conference_provider = EXCLUDED.conference_provider,\n sequence = EXCLUDED.sequence,\n is_read_only = EXCLUDED.is_read_only,\n canonical_source_kind = EXCLUDED.canonical_source_kind,\n canonical_source_updated_at = EXCLUDED.canonical_source_updated_at,\n reminders_use_default = EXCLUDED.reminders_use_default,\n reminder_overrides = EXCLUDED.reminder_overrides,\n updated_at = GREATEST(calendar_events.updated_at, EXCLUDED.updated_at)\n WHERE\n EXCLUDED.sequence > calendar_events.sequence\n OR (\n EXCLUDED.sequence = calendar_events.sequence\n AND EXCLUDED.canonical_source_updated_at\n >= calendar_events.canonical_source_updated_at\n )\n RETURNING id\n ",+ "query": "\n INSERT INTO calendar_events (\n id, owner_id, source_link_id, ical_uid, title, description, location,\n status, visibility, transparency,\n starts_at, ends_at, start_date, end_date, time_zone,\n recurrence_lines, organizer_email, organizer_name,\n conference_url, conference_provider, sequence, is_read_only,\n canonical_source_kind,\n canonical_source_updated_at,\n reminders_use_default, reminder_overrides,\n created_at, updated_at\n )\n VALUES (\n $1, $2, $3, $4, $5, $6, $7,\n $8, $9, $10,\n $11, $12, $13, $14, $15,\n $16, $17, $18,\n $19, $27, $20, $21, $22, $24,\n $25, $26,\n $23, $24\n )\n ON CONFLICT (owner_id, source_link_id, ical_uid) DO UPDATE SET\n title = EXCLUDED.title,\n description = EXCLUDED.description,\n location = EXCLUDED.location,\n status = EXCLUDED.status,\n visibility = EXCLUDED.visibility,\n transparency = EXCLUDED.transparency,\n starts_at = EXCLUDED.starts_at,\n ends_at = EXCLUDED.ends_at,\n start_date = EXCLUDED.start_date,\n end_date = EXCLUDED.end_date,\n time_zone = EXCLUDED.time_zone,\n recurrence_lines = EXCLUDED.recurrence_lines,\n organizer_email = EXCLUDED.organizer_email,\n organizer_name = EXCLUDED.organizer_name,\n conference_url = EXCLUDED.conference_url,\n conference_provider = EXCLUDED.conference_provider,\n sequence = EXCLUDED.sequence,\n is_read_only = EXCLUDED.is_read_only,\n canonical_source_kind = EXCLUDED.canonical_source_kind,\n canonical_source_updated_at = EXCLUDED.canonical_source_updated_at,\n reminders_use_default = EXCLUDED.reminders_use_default,\n reminder_overrides = EXCLUDED.reminder_overrides,\n updated_at = GREATEST(calendar_events.updated_at, EXCLUDED.updated_at)\n WHERE\n EXCLUDED.sequence > calendar_events.sequence\n OR (\n EXCLUDED.sequence = calendar_events.sequence\n AND EXCLUDED.canonical_source_updated_at\n >= calendar_events.canonical_source_updated_at\n )\n -- `xmax = 0` distinguishes the INSERT from the DO UPDATE: a\n -- freshly inserted row carries no updating transaction id.\n RETURNING id, (xmax = 0) AS \"inserted!\"\n ","describe": {"columns": [{"ordinal": 0,"name": "id","type_info": "Uuid"+ },+ {+ "ordinal": 1,+ "name": "inserted!",+ "type_info": "Bool"}],"parameters": {@@ -41,8 +46,9 @@]},"nullable": [- false+ false,+ null]},- "hash": "7560f5929cf5e8b1cd8b7afd4cad52a6c1fd612db386416870c4b6bca7f97421"+ "hash": "09567c7fdbca8c652db4d0037a498bf203a27e6d08737a809ae77efc4dd76680"}diff --git a/.sqlx/query-47e360c89a42e3491a9611f30494e9a96b56a961e6f7b7c84a3ac5e6d2dcc496.json b/.sqlx/query-47e360c89a42e3491a9611f30494e9a96b56a961e6f7b7c84a3ac5e6d2dcc496.jsonnew file mode 100644index 0000000000..71f6ab0865--- /dev/null+++ b/.sqlx/query-47e360c89a42e3491a9611f30494e9a96b56a961e6f7b7c84a3ac5e6d2dcc496.json@@ -0,0 +1,132 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT\n event.id AS \"id!\",\n event.owner_id AS \"owner_id!\",\n event.title AS \"title!\",\n event.status AS \"status!\",\n event.starts_at,\n event.ends_at,\n event.start_date,\n event.end_date,\n event.time_zone,\n (cardinality(event.recurrence_lines) > 0) AS \"is_recurring!\",\n event.conference_url,\n event.is_read_only AS \"is_read_only!\",\n event.created_at AS \"created_at!\",\n event.updated_at AS \"updated_at!\",\n occurrence.occurrence_key AS \"occurrence_key?\",\n occurrence.starts_at AS \"occurrence_starts_at?\",\n occurrence.ends_at AS \"occurrence_ends_at?\",\n occurrence.start_date AS \"occurrence_start_date?\",\n occurrence.end_date AS \"occurrence_end_date?\"\n FROM calendar_events event\n LEFT JOIN LATERAL (\n SELECT\n instance.occurrence_key,\n instance.starts_at,\n instance.ends_at,\n instance.start_date,\n instance.end_date\n FROM calendar_event_occurrences instance\n CROSS JOIN LATERAL (\n SELECT COALESCE(\n instance.starts_at,\n instance.start_date::timestamp AT TIME ZONE 'UTC'\n ) AS at\n ) instance_start\n WHERE instance.event_id = event.id\n AND NOT instance.is_cancelled\n ORDER BY\n (instance_start.at >= $3) DESC,\n CASE WHEN instance_start.at >= $3 THEN instance_start.at END ASC,\n instance_start.at DESC,\n instance.occurrence_key\n LIMIT 1\n ) occurrence ON true\n WHERE event.id = ANY($2)\n AND (\n event.owner_id = $1\n OR EXISTS (\n SELECT 1\n FROM macro_user_links link\n WHERE link.link_id = event.source_link_id\n AND link.primary_macro_id = $1\n )\n )\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id!",+ "type_info": "Uuid"+ },+ {+ "ordinal": 1,+ "name": "owner_id!",+ "type_info": "Text"+ },+ {+ "ordinal": 2,+ "name": "title!",+ "type_info": "Text"+ },+ {+ "ordinal": 3,+ "name": "status!",+ "type_info": "Text"+ },+ {+ "ordinal": 4,+ "name": "starts_at",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 5,+ "name": "ends_at",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 6,+ "name": "start_date",+ "type_info": "Date"+ },+ {+ "ordinal": 7,+ "name": "end_date",+ "type_info": "Date"+ },+ {+ "ordinal": 8,+ "name": "time_zone",+ "type_info": "Text"+ },+ {+ "ordinal": 9,+ "name": "is_recurring!",+ "type_info": "Bool"+ },+ {+ "ordinal": 10,+ "name": "conference_url",+ "type_info": "Text"+ },+ {+ "ordinal": 11,+ "name": "is_read_only!",+ "type_info": "Bool"+ },+ {+ "ordinal": 12,+ "name": "created_at!",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 13,+ "name": "updated_at!",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 14,+ "name": "occurrence_key?",+ "type_info": "Text"+ },+ {+ "ordinal": 15,+ "name": "occurrence_starts_at?",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 16,+ "name": "occurrence_ends_at?",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 17,+ "name": "occurrence_start_date?",+ "type_info": "Date"+ },+ {+ "ordinal": 18,+ "name": "occurrence_end_date?",+ "type_info": "Date"+ }+ ],+ "parameters": {+ "Left": [+ "Text",+ "UuidArray",+ "Timestamptz"+ ]+ },+ "nullable": [+ false,+ false,+ false,+ false,+ true,+ true,+ true,+ true,+ true,+ null,+ true,+ false,+ false,+ false,+ false,+ true,+ true,+ true,+ true+ ]+ },+ "hash": "47e360c89a42e3491a9611f30494e9a96b56a961e6f7b7c84a3ac5e6d2dcc496"+}diff --git a/.sqlx/query-cbd7fc8fc7f1e6115f065b2a52c656b81d0afbcba06b24ce33da49e66b45657d.json b/.sqlx/query-6329f4a2b1d8826a50d3fe0fbeac9728d77cd67f43d69357a2a04c76ad9b5923.jsonsimilarity index 55%rename from .sqlx/query-cbd7fc8fc7f1e6115f065b2a52c656b81d0afbcba06b24ce33da49e66b45657d.jsonrename to .sqlx/query-6329f4a2b1d8826a50d3fe0fbeac9728d77cd67f43d69357a2a04c76ad9b5923.jsonindex 3ea560cf3a..42786dfe40 100644--- a/.sqlx/query-cbd7fc8fc7f1e6115f065b2a52c656b81d0afbcba06b24ce33da49e66b45657d.json+++ b/.sqlx/query-6329f4a2b1d8826a50d3fe0fbeac9728d77cd67f43d69357a2a04c76ad9b5923.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n SELECT source_link_id, ical_uid\n FROM calendar_events\n WHERE id = $1\n ",+ "query": "\n SELECT source_link_id, ical_uid, owner_id\n FROM calendar_events\n WHERE id = $1\n ","describe": {"columns": [{@@ -12,6 +12,11 @@"ordinal": 1,"name": "ical_uid","type_info": "Text"+ },+ {+ "ordinal": 2,+ "name": "owner_id",+ "type_info": "Text"}],"parameters": {@@ -20,9 +25,10 @@]},"nullable": [+ false,false,false]},- "hash": "cbd7fc8fc7f1e6115f065b2a52c656b81d0afbcba06b24ce33da49e66b45657d"+ "hash": "6329f4a2b1d8826a50d3fe0fbeac9728d77cd67f43d69357a2a04c76ad9b5923"}diff --git a/.sqlx/query-9f159ad721926ca7018cae3cdda9b9699bacb7dca55c37cbc701dd4b7be2f4e8.json b/.sqlx/query-9f159ad721926ca7018cae3cdda9b9699bacb7dca55c37cbc701dd4b7be2f4e8.jsonnew file mode 100644index 0000000000..202179e5f9--- /dev/null+++ b/.sqlx/query-9f159ad721926ca7018cae3cdda9b9699bacb7dca55c37cbc701dd4b7be2f4e8.json@@ -0,0 +1,106 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT\n event.id AS \"id!\",\n event.owner_id AS \"owner_id!\",\n event.source_link_id AS \"source_link_id!\",\n event.ical_uid AS \"ical_uid!\",\n event.title AS \"title!\",\n event.status AS \"status!\",\n event.starts_at,\n event.ends_at,\n event.start_date,\n event.end_date,\n (cardinality(event.recurrence_lines) > 0) AS \"is_recurring!\",\n event.organizer_email,\n event.created_at AS \"created_at!\",\n event.updated_at AS \"updated_at!\",\n COALESCE(\n (\n SELECT array_agg(lower(attendee.email) ORDER BY lower(attendee.email))\n FROM calendar_event_attendees attendee\n WHERE attendee.event_id = event.id\n ),\n '{}'\n ) AS \"attendee_emails!\"\n FROM calendar_events event\n WHERE event.id = $1\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id!",+ "type_info": "Uuid"+ },+ {+ "ordinal": 1,+ "name": "owner_id!",+ "type_info": "Text"+ },+ {+ "ordinal": 2,+ "name": "source_link_id!",+ "type_info": "Uuid"+ },+ {+ "ordinal": 3,+ "name": "ical_uid!",+ "type_info": "Text"+ },+ {+ "ordinal": 4,+ "name": "title!",+ "type_info": "Text"+ },+ {+ "ordinal": 5,+ "name": "status!",+ "type_info": "Text"+ },+ {+ "ordinal": 6,+ "name": "starts_at",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 7,+ "name": "ends_at",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 8,+ "name": "start_date",+ "type_info": "Date"+ },+ {+ "ordinal": 9,+ "name": "end_date",+ "type_info": "Date"+ },+ {+ "ordinal": 10,+ "name": "is_recurring!",+ "type_info": "Bool"+ },+ {+ "ordinal": 11,+ "name": "organizer_email",+ "type_info": "Text"+ },+ {+ "ordinal": 12,+ "name": "created_at!",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 13,+ "name": "updated_at!",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 14,+ "name": "attendee_emails!",+ "type_info": "TextArray"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": [+ false,+ false,+ false,+ false,+ false,+ false,+ true,+ true,+ true,+ true,+ null,+ true,+ false,+ false,+ null+ ]+ },+ "hash": "9f159ad721926ca7018cae3cdda9b9699bacb7dca55c37cbc701dd4b7be2f4e8"+}diff --git a/.sqlx/query-a94c8d5d81051369847d6711c16dbecc93afa14827bc75c234182e2d6b416e19.json b/.sqlx/query-a94c8d5d81051369847d6711c16dbecc93afa14827bc75c234182e2d6b416e19.jsonnew file mode 100644index 0000000000..a61118c357--- /dev/null+++ b/.sqlx/query-a94c8d5d81051369847d6711c16dbecc93afa14827bc75c234182e2d6b416e19.json@@ -0,0 +1,32 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT\n event.id AS \"event_id!\",\n event.updated_at AS \"updated_at!\"\n FROM calendar_events event\n WHERE ($2::timestamptz IS NULL OR $3::uuid IS NULL\n OR (event.updated_at, event.id) > ($2, $3))\n AND ($4::timestamptz IS NULL OR event.updated_at >= $4)\n AND ($5::timestamptz IS NULL OR event.updated_at < $5)\n ORDER BY event.updated_at ASC, event.id ASC\n LIMIT $1\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "event_id!",+ "type_info": "Uuid"+ },+ {+ "ordinal": 1,+ "name": "updated_at!",+ "type_info": "Timestamptz"+ }+ ],+ "parameters": {+ "Left": [+ "Int8",+ "Timestamptz",+ "Uuid",+ "Timestamptz",+ "Timestamptz"+ ]+ },+ "nullable": [+ false,+ false+ ]+ },+ "hash": "a94c8d5d81051369847d6711c16dbecc93afa14827bc75c234182e2d6b416e19"+}diff --git a/Cargo.lock b/Cargo.lockindex a69cc98bc2..25dcfed822 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -2936,6 +2936,8 @@ dependencies = ["futures","macro_authorization","macro_db_migrator",+ "macro_event_broker",+ "macro_event_topics","macro_user_id","model-entity","model_notifications",@@ -13805,6 +13807,7 @@ dependencies = ["aws-sdk-s3","aws-sdk-sqs","axum",+ "calendar_events","call","channels","chat",diff --git a/apps/docs/AI/mcp/tools/content-search.mdx b/apps/docs/AI/mcp/tools/content-search.mdxindex 5d738bcb74..b4d92c80b3 100644--- a/apps/docs/AI/mcp/tools/content-search.mdx+++ b/apps/docs/AI/mcp/tools/content-search.mdx@@ -13,7 +13,7 @@ Search items by their content: document body text; email subject/body/sender/rec| --- | --- | --- | --- || `query` | string | Yes | The text to search. Pass 1-3 keywords drawn from words that would literally appear in the content, not the user's natural-language description. Whitespace-separated terms are ANDed. For documents, every term must appear somewhere in the document (different chunks/pages are fine). For emails each term is matched across subject/body/sender/recipient. For chats/channels/calls the whole query is matched as a single adjacent phrase prefix, so long phrases will not match. Wrap a multi-word phrase in double quotes to match it as one phrase. Use matchType 'exact' for whole-token (or full email address) matching instead of prefix. || `matchType` | `"partial"` \| `"exact"` | No | How search tools match query terms. Restricted to partial/exact — the backend also supports regexp and an internal query mode, but those are not offered to the model. |-| `entityTypes` | (`"documents"` \| `"chats"` \| `"emails"` \| `"channels"` \| `"projects"` \| `"call_records"`)[] | No | Which types of items to search. Leave empty (the default) to search all types — this is almost always what you want. Only set this when the user's request clearly targets one or more specific types. Examples: ['documents'], ['emails', 'documents'], ['channels'], ['call_records']. |+| `entityTypes` | (`"documents"` \| `"chats"` \| `"emails"` \| `"channels"` \| `"projects"` \| `"call_records"` \| `"calendar_events"`)[] | No | Which types of items to search. Leave empty (the default) to search all types — this is almost always what you want. Only set this when the user's request clearly targets one or more specific types. Examples: ['documents'], ['emails', 'documents'], ['channels'], ['call_records']. Projects and calendar events carry no indexed content, so they never match here — search those by name with NameSearch instead. || `inbox` | string | No | Restrict email results to a single connected inbox, given as that inbox's email address (from ListInboxes). Omit to search every inbox the user can access. Only set this when the user scopes the request to a specific mailbox. Only affects email results. || `tags` | TagFilter[] | No | Restrict results to items carrying the given tags — any of them by default, every one of them with tagsMatch="all". Each entry names a tag by its label, matched case-insensitively against the user's own tags; only set scope ("personal" or "team") when the user distinguishes between their personal and team tags. An unknown label fails with the list of available tags — call ListTags first when unsure what tags exist. Only taggable items (documents, emails, AI chats, projects, call records) can match, so channels are dropped while a tag filter is active. || `tagsMatch` | `"any"` \| `"all"` | No | How multiple tag filters combine when filtering items. |diff --git a/apps/docs/AI/mcp/tools/name-search.mdx b/apps/docs/AI/mcp/tools/name-search.mdxindex b600290dd1..39e432b8c1 100644--- a/apps/docs/AI/mcp/tools/name-search.mdx+++ b/apps/docs/AI/mcp/tools/name-search.mdx@@ -13,7 +13,7 @@ Search items by their name or title: document name, email subject, chat title, c| --- | --- | --- | --- || `name` | string | Yes | The name or title to search. Pass 1-3 keywords drawn from words that would literally appear in the title, not the user's natural-language description. Whitespace-separated terms are ANDed. For non-email types the whole query is matched as a single adjacent phrase prefix, so long phrases will not match. For emails each term is matched against the subject. Wrap a multi-word phrase in double quotes to match it as one phrase. Use matchType 'exact' for whole-token matching instead of prefix. || `matchType` | `"partial"` \| `"exact"` | No | How search tools match query terms. Restricted to partial/exact — the backend also supports regexp and an internal query mode, but those are not offered to the model. |-| `entityTypes` | (`"documents"` \| `"chats"` \| `"emails"` \| `"channels"` \| `"projects"` \| `"call_records"`)[] | No | Which types of items to search. Leave empty (the default) to search all types — this is almost always what you want. Only set this when the user's request clearly targets one or more specific types. Examples: ['documents'], ['emails', 'documents'], ['channels'], ['call_records']. |+| `entityTypes` | (`"documents"` \| `"chats"` \| `"emails"` \| `"channels"` \| `"projects"` \| `"call_records"` \| `"calendar_events"`)[] | No | Which types of items to search. Leave empty (the default) to search all types — this is almost always what you want. Only set this when the user's request clearly targets one or more specific types. Examples: ['documents'], ['emails', 'documents'], ['channels'], ['call_records']. || `inbox` | string | No | Restrict email results to a single connected inbox, given as that inbox's email address (from ListInboxes). Omit to search every inbox the user can access. Only set this when the user scopes the request to a specific mailbox. Only affects email results. |-| `tags` | TagFilter[] | No | Restrict results to items carrying the given tags — any of them by default, every one of them with tagsMatch="all". Each entry names a tag by its label, matched case-insensitively against the user's own tags; only set scope ("personal" or "team") when the user distinguishes between their personal and team tags. An unknown label fails with the list of available tags — call ListTags first when unsure what tags exist. Only taggable items (documents, emails, AI chats, projects, call records) can match, so channels are dropped while a tag filter is active. |+| `tags` | TagFilter[] | No | Restrict results to items carrying the given tags — any of them by default, every one of them with tagsMatch="all". Each entry names a tag by its label, matched case-insensitively against the user's own tags; only set scope ("personal" or "team") when the user distinguishes between their personal and team tags. An unknown label fails with the list of available tags — call ListTags first when unsure what tags exist. Only taggable items (documents, emails, AI chats, projects, call records, calendar events) can match, so channels are dropped while a tag filter is active. || `tagsMatch` | `"any"` \| `"all"` | No | How multiple tag filters combine when filtering items. |diff --git a/apps/web/src/features/entity/types/entity.ts b/apps/web/src/features/entity/types/entity.tsindex 9ab655a307..2bb2c902eb 100644--- a/apps/web/src/features/entity/types/entity.ts+++ b/apps/web/src/features/entity/types/entity.ts@@ -361,6 +361,12 @@ export type CalendarEventEntity = EntityBase & {status: string;/** Master event time. Absent when the wire shape could not be read. */time?: CalendarEventEntityTime;+ /**+ * The instance this row means, when one was resolved. Search rows carry it+ * so a click lands on the relevant occurrence of a recurring series rather+ * than the master's original start; soup rows leave it unset.+ */+ occurrenceKey?: string;/** Direct join URL when known. */conferenceUrl?: string;/** Whether the canonical source prohibits mutation. */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 e67b1491e2..37b498da45 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@@ -60,15 +60,28 @@ describe('task view presets', () => {});describe('calendar event scoping', () => {- it('excludes calendar events from views that do not render them', () => {- const nilId = '00000000-0000-0000-0000-000000000000';+ const nilId = '00000000-0000-0000-0000-000000000000';+ it('excludes calendar events from feeds that do not render them', () => {expect(getViewPreset('mail', 'important')?.filters.include?.calendarEventId).toEqual([nilId]);expect(getViewPreset('inbox', 'all')?.filters.include?.calendarEventId).toEqual([nilId]);+ });++ it('searches calendar events, which carry a title index of their own', () => {+ expect(+ getViewPreset('search', 'all')?.filters.include?.calendarEventId+ ).toBeUndefined();+ });++ it('excludes them from search when the calendar UI is off', () => {+ // Opening a hit needs the calendar block, which the flag gates, so+ // without it a result would render an inert row.+ mocks.calendarUiEnabled = false;+expect(getViewPreset('search', 'all')?.filters.include?.calendarEventId).toEqual([nilId]);diff --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 97b4a4d3de..b7076d7c71 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@@ -591,17 +591,20 @@ export const VIEW_TAB_PRESETS: Record<ListView, ViewTabConfig> = {all: () => ({// Temporary: search has no full-text index over foreign entities yet,// so always exclude them (matching no record id) until search supports- // them. Calendar, CRM, and non-displayable channel-thread rows are- // NIL-excluded the same way. `search-supported` mirrors these- // exclusions client-side so entities that enter the soup cache outside- // this query (e.g. websocket-driven inserts) don't surface in the- // search feed.+ // them. CRM and non-displayable channel-thread rows are NIL-excluded+ // the same way. Calendar events are not excluded — they carry a title+ // index of their own. `search-supported` mirrors these exclusions+ // client-side so entities that enter the soup cache outside this query+ // (e.g. websocket-driven inserts) don't surface in the search feed.filters: {include: {- calendarEventId: [NIL_UUID],foreignEntityRecordId: [NIL_UUID],crmCompanyId: [NIL_UUID],channelThreadId: [NIL_UUID],+ // Events are title-indexed, so search returns them — but opening+ // one needs the calendar block, which the flag gates. Without it+ // a hit would render an inert row, so exclude the type instead.+ ...(ENABLE_CALENDAR_UI() ? {} : { calendarEventId: [NIL_UUID] }),},exclude: getDisabledSnippetSubtypeExclude(),},diff --git a/apps/web/src/features/next-soup/soup-view/create-search-state.ts b/apps/web/src/features/next-soup/soup-view/create-search-state.tsindex 386cc327b1..322b75a39a 100644--- a/apps/web/src/features/next-soup/soup-view/create-search-state.ts+++ b/apps/web/src/features/next-soup/soup-view/create-search-state.ts@@ -67,15 +67,14 @@ function filterDataToQueryFilters(data: QueryState): EntityFilters {const filters: EntityFilters = {};const { include } = data;- // Calendar events are not displayed in Soup search. Older restored query- // states predate calendarEventId, so treat a missing field as match-nothing;- // an explicit include/exclude remains authoritative.- const calendarEventIds =- include.calendarEventId ??- (data.exclude.calendarEventId === undefined ? [NIL_UUID] : undefined);- if (calendarEventIds?.length) {+ // Calendar events are searchable by title, so they are scoped like every+ // other entity type: a view that names ids gets them, one that names none+ // leaves the type unfiltered. Views that should not surface events keep+ // NIL-excluding them (the inbox feed does), and every view's client+ // predicates gate the merged pool regardless.+ if (include.calendarEventId?.length) {filters.calendar_event_filters = {- calendar_event_ids: calendarEventIds,+ calendar_event_ids: include.calendarEventId,};}diff --git a/apps/web/src/features/next-soup/utils.ts b/apps/web/src/features/next-soup/utils.tsindex 0dba05d125..583f956d48 100644--- a/apps/web/src/features/next-soup/utils.ts+++ b/apps/web/src/features/next-soup/utils.ts@@ -822,7 +822,9 @@ function calendarBlockParamsForEntity(return {eventId: content?.eventId ?? entity.id,- occurrenceKey: content?.occurrenceKey,+ // A reminder names a precise instance, so it wins; otherwise fall back to+ // whatever resolved the row (search supplies one, soup does not).+ occurrenceKey: content?.occurrenceKey ?? entity.occurrenceKey,range: time ? createCalendarBlockRange(time) : undefined,};}diff --git a/apps/web/src/lib/queries/soup/normalized-cache/operations.ts b/apps/web/src/lib/queries/soup/normalized-cache/operations.tsindex d9935773a5..dcb0154e26 100644--- a/apps/web/src/lib/queries/soup/normalized-cache/operations.ts+++ b/apps/web/src/lib/queries/soup/normalized-cache/operations.ts@@ -783,6 +783,7 @@ function getSearchResultId(result: UnifiedSearchResponseItem): string {.with({ type: 'project' }, (r) => r.id).with({ type: 'call' }, (r) => r.call_id).with({ type: 'company' }, (r) => r.id)+ .with({ type: 'calendarEvent' }, (r) => r.id).exhaustive();}diff --git a/apps/web/src/lib/queries/soup/transform-utils.ts b/apps/web/src/lib/queries/soup/transform-utils.tsindex 3a3dca76e9..f79368262b 100644--- a/apps/web/src/lib/queries/soup/transform-utils.ts+++ b/apps/web/src/lib/queries/soup/transform-utils.ts@@ -543,6 +543,40 @@ export const useSearchResponseItemMapper = () => {];}+ case 'calendarEvent': {+ if (!result.metadata) return [];+ const search = getSearchData({+ results: result.calendar_event_search_results,+ });++ // A recurring series is indexed once, as its master. The service+ // resolves which instance this row means — next upcoming, else most+ // recent past — so prefer that span over the master's, which for a+ // long-running series is its original (stale) start.+ const metadata = result.metadata;+ const time = toCalendarEventTime(+ metadata.occurrence?.time ?? metadata.time+ );++ return [+ {+ type: 'calendar_event',+ id: result.id,+ name: result.name,+ ownerId: result.owner_id,+ status: metadata.status,+ time,+ occurrenceKey: metadata.occurrence?.occurrenceKey,+ conferenceUrl: metadata.conferenceUrl ?? undefined,+ isReadOnly: metadata.isReadOnly,+ createdAt: metadata.createdAt,+ updatedAt: metadata.updatedAt,+ properties: result.properties ?? undefined,+ search,+ },+ ];+ }+case 'call': {if (!result.metadata) return [];const search = getSearchData({diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/tools/schemas.ts b/apps/web/src/lib/service-clients/service-cognition/generated/tools/schemas.tsindex d8e387e0d1..e76f1829d8 100644--- a/apps/web/src/lib/service-clients/service-cognition/generated/tools/schemas.ts+++ b/apps/web/src/lib/service-clients/service-cognition/generated/tools/schemas.ts@@ -180,6 +180,7 @@ export const ContentSearch = z.object({'channels','projects','call_records',+ 'calendar_events',])).optional(),@@ -667,6 +668,120 @@ export const SearchToolResponse = z.object({}),z.object({ type: z.literal('company') })),+ z.intersection(+ z.object({+ metadata: z+ .union([+ z.object({+ createdAt: z.string().datetime({ offset: true }),+ updatedAt: z.string().datetime({ offset: true }),+ status: z.string(),+ time: z.any().superRefine((x, ctx) => {+ const schemas = [+ z.object({+ startsAt: z.string().datetime({ offset: true }),+ endsAt: z.string().datetime({ offset: true }),+ timeZone: z+ .union([z.string(), z.null()])+ .optional(),+ kind: z.literal('timed'),+ }),+ z.object({+ startDate: z.string().date(),+ endDate: z.string().date(),+ kind: z.literal('allDay'),+ }),+ ];+ const errors = schemas.reduce<z.ZodError[]>(+ (errors, schema) =>+ ((result) =>+ result.error+ ? [...errors, result.error]+ : errors)(schema.safeParse(x)),+ []+ );+ if (schemas.length - errors.length !== 1) {+ ctx.addIssue({+ path: ctx.path,+ code: 'invalid_union',+ unionErrors: errors,+ message: 'Invalid input: Should pass single schema',+ });+ }+ }),+ isRecurring: z.boolean(),+ occurrence: z+ .union([+ z.object({+ occurrenceKey: z.string(),+ time: z.any().superRefine((x, ctx) => {+ const schemas = [+ z.object({+ startsAt: z+ .string()+ .datetime({ offset: true }),+ endsAt: z.string().datetime({ offset: true }),+ timeZone: z+ .union([z.string(), z.null()])+ .optional(),+ kind: z.literal('timed'),+ }),+ z.object({+ startDate: z.string().date(),+ endDate: z.string().date(),+ kind: z.literal('allDay'),+ }),+ ];+ const errors = schemas.reduce<z.ZodError[]>(+ (errors, schema) =>+ ((result) =>+ result.error+ ? [...errors, result.error]+ : errors)(schema.safeParse(x)),+ []+ );+ if (schemas.length - errors.length !== 1) {+ ctx.addIssue({+ path: ctx.path,+ code: 'invalid_union',+ unionErrors: errors,+ message:+ 'Invalid input: Should pass single schema',+ });+ }+ }),+ }),+ z.null(),+ ])+ .optional(),+ conferenceUrl: z.union([z.string(), z.null()]).optional(),+ isReadOnly: z.boolean(),+ }),+ z.null(),+ ])+ .optional(),+ id: z.string().uuid(),+ name: z.string(),+ owner_id: z.string(),+ updated_at: z.string().datetime({ offset: true }),+ created_at: z.string().datetime({ offset: true }),+ calendar_event_search_results: z.array(+ z.object({+ highlight: z.object({+ name: z.union([z.string(), z.null()]).optional(),+ content: z.array(z.string()).optional(),+ user_id: z.union([z.string(), z.null()]).optional(),+ sender: z.union([z.string(), z.null()]).optional(),+ recipients: z.array(z.string()).optional(),+ cc: z.array(z.string()).optional(),+ bcc: z.array(z.string()).optional(),+ }),+ score: z.union([z.number(), z.null()]).optional(),+ })+ ),+ }),+ z.object({ type: z.literal('calendarEvent') })+ ),];const errors = schemas.reduce<z.ZodError[]>((errors, schema) =>@@ -2509,6 +2624,7 @@ export const NameSearch = z.object({'channels','projects','call_records',+ 'calendar_events',])).optional(),diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/tools/types.ts b/apps/web/src/lib/service-clients/service-cognition/generated/tools/types.tsindex 2f99adef14..2db6bfd8ff 100644--- a/apps/web/src/lib/service-clients/service-cognition/generated/tools/types.ts+++ b/apps/web/src/lib/service-clients/service-cognition/generated/tools/types.ts@@ -71,7 +71,8 @@ export type UnifiedSearchIndex =| 'emails'| 'channels'| 'projects'- | 'call_records';+ | 'call_records'+ | 'calendar_events';/*** Which tag set a tag belongs to, relative to the caller.*/@@ -113,6 +114,9 @@ export type TaggedSearchResult1 =})| (CrmCompanySearchResponseItem & {type: 'company';+ })+ | (CalendarEventSearchResponseItemWithMetadata & {+ type: 'calendarEvent';});/*** The document sub type enum represents all values of document sub types.@@ -124,6 +128,45 @@ export type DocumentSubType = 'task' | 'snippet' | 'skill';* Serializes as `ATTENDED`, `MISSED`, or `UNATTENDED`.*/export type CallStatus = 'ATTENDED' | 'MISSED' | 'UNATTENDED';+/**+ * Timed or all-day span of a calendar event.+ *+ * Deliberately a local mirror of `models_soup::calendar_event::SoupCalendarEventTime`+ * rather than a reuse of it: this module needs both `ToSchema` (OpenAPI) and+ * `JsonSchema` (AI tool schemas), and `models_soup` carries no schemars+ * dependency. The wire shape is identical and must stay that way, so a+ * client can decode a search row and a soup row with one mapping.+ *+ * Fields are camelCased per variant rather than via `rename_all_fields`,+ * which utoipa ignores.+ */+export type CalendarEventSearchTime =+ | {+ /**+ * Inclusive start.+ */+ startsAt: string;+ /**+ * Exclusive end.+ */+ endsAt: string;+ /**+ * Original IANA time zone.+ */+ timeZone?: string | null;+ kind: 'timed';+ }+ | {+ /**+ * Inclusive start date.+ */+ startDate: string;+ /**+ * Exclusive end date.+ */+ endDate: string;+ kind: 'allDay';+ };/*** The mutually exclusive time shape supplied to calendar tools.*/@@ -1003,7 +1046,7 @@ export interface ContentSearch {query: string;matchType?: SearchMatchType & string;/**- * Which types of items to search. Leave empty (the default) to search all types — this is almost always what you want. Only set this when the user's request clearly targets one or more specific types. Examples: ['documents'], ['emails', 'documents'], ['channels'], ['call_records'].+ * Which types of items to search. Leave empty (the default) to search all types — this is almost always what you want. Only set this when the user's request clearly targets one or more specific types. Examples: ['documents'], ['emails', 'documents'], ['channels'], ['call_records']. Projects and calendar events carry no indexed content, so they never match here — search those by name with NameSearch instead.*/entityTypes?: UnifiedSearchIndex[];/**@@ -1539,6 +1582,82 @@ export interface CrmCompanySearchDomain {*/createdAt: string;}+/**+ * CalendarEventSearchResponseItem with metadata fetched from macrodb.+ */+export interface CalendarEventSearchResponseItemWithMetadata {+ /**+ * Metadata from the database. None if the event no longer exists.+ */+ metadata?: CalendarEventMetadata | null;+ /**+ * Standardized fields that all item types will share.+ */+ id: string;+ name: string;+ owner_id: string;+ updated_at: string;+ created_at: string;+ calendar_event_search_results: CalendarEventSearchResult[];+}+/**+ * Metadata for a calendar event fetched from the database.+ *+ * The index carries the master span for ranking, but the occurrence a row+ * should display depends on when the query ran, so it is resolved here+ * rather than indexed.+ */+export interface CalendarEventMetadata {+ createdAt: string;+ updatedAt: string;+ /**+ * Canonical status (`confirmed`, `tentative`, `cancelled`).+ */+ status: string;+ time: CalendarEventSearchTime;+ /**+ * Whether the series carries a recurrence rule. Lets a row render a+ * recurring badge without parsing the rules.+ */+ isRecurring: boolean;+ /**+ * The instance this row points at. `None` when the series has no+ * materialized occurrence — occurrences exist only inside a rolling+ * window, so a row can legitimately fall back to `time`.+ */+ occurrence?: CalendarEventSearchOccurrence | null;+ /**+ * Direct conference join URL when known.+ */+ conferenceUrl?: string | null;+ /**+ * Whether the canonical source prohibits mutation.+ */+ isReadOnly: boolean;+}+/**+ * The instance a search row points at.+ *+ * A recurring series is indexed once, as its master. Which instance the row+ * should show is decided per request rather than baked into the index: the+ * next occurrence at or after now, else the most recent past one. This is+ * the same resolution a calendar mention performs, so a search row and a+ * mention of the same event agree on where they land.+ */+export interface CalendarEventSearchOccurrence {+ /**+ * Stable key of the resolved instance within its series.+ */+ occurrenceKey: string;+ time: CalendarEventSearchTime;+}+export interface CalendarEventSearchResult {+ highlight: SearchHighlight;+ /**+ * The score of the result+ */+ score?: number | null;+}/*** Create a bot with a name, stable handle, and optional profile. Omit teamId for a bot owned by the current user; provide teamId to create a team-owned bot, which requires team administrator or owner permission. Pass channelId when the bot should post to a channel immediately: the current user must be a member of that channel. The response then includes that channel's webhook URL and a credential proposal. The user mints the bearer token from the chat card or bot settings; the secret is never returned in this tool result. Omit channelId to create the bot only, then use ManageBotChannelAccess and IssueBotCredential for later setup.*/@@ -3618,7 +3737,7 @@ export interface NameSearch {*/inbox?: string | null;/**- * Restrict results to items carrying the given tags — any of them by default, every one of them with tagsMatch="all". Each entry names a tag by its label, matched case-insensitively against the user's own tags; only set scope ("personal" or "team") when the user distinguishes between their personal and team tags. An unknown label fails with the list of available tags — call ListTags first when unsure what tags exist. Only taggable items (documents, emails, AI chats, projects, call records) can match, so channels are dropped while a tag filter is active.+ * Restrict results to items carrying the given tags — any of them by default, every one of them with tagsMatch="all". Each entry names a tag by its label, matched case-insensitively against the user's own tags; only set scope ("personal" or "team") when the user distinguishes between their personal and team tags. An unknown label fails with the list of available tags — call ListTags first when unsure what tags exist. Only taggable items (documents, emails, AI chats, projects, call records, calendar events) can match, so channels are dropped while a tag filter is active.*/tags?: TagFilter[] | null;tagsMatch?: TagMatch;diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventMetadata.ts b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventMetadata.tsnew file mode 100644index 0000000000..100b4a2dd9--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventMetadata.ts@@ -0,0 +1,33 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */+import type { CalendarEventMetadataConferenceUrl } from './calendarEventMetadataConferenceUrl';+import type { CalendarEventMetadataOccurrence } from './calendarEventMetadataOccurrence';+import type { CalendarEventSearchTime } from './calendarEventSearchTime';++/**+ * Metadata for a calendar event fetched from the database.++The index carries the master span for ranking, but the occurrence a row+should display depends on when the query ran, so it is resolved here+rather than indexed.+ */+export interface CalendarEventMetadata {+ /** Direct conference join URL when known. */+ conferenceUrl?: CalendarEventMetadataConferenceUrl;+ createdAt: string;+ /** Whether the canonical source prohibits mutation. */+ isReadOnly: boolean;+ /** Whether the series carries a recurrence rule. Lets a row render a+recurring badge without parsing the rules. */+ isRecurring: boolean;+ occurrence?: CalendarEventMetadataOccurrence;+ /** Canonical status (`confirmed`, `tentative`, `cancelled`). */+ status: string;+ /** The series' own span. */+ time: CalendarEventSearchTime;+ updatedAt: string;+}diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventMetadataConferenceUrl.ts b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventMetadataConferenceUrl.tsnew file mode 100644index 0000000000..484f1c39dc--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventMetadataConferenceUrl.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */++/**+ * Direct conference join URL when known.+ */+export type CalendarEventMetadataConferenceUrl = string | null;diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventMetadataOccurrence.ts b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventMetadataOccurrence.tsnew file mode 100644index 0000000000..84890d908f--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventMetadataOccurrence.ts@@ -0,0 +1,10 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */+import type { CalendarEventSearchOccurrence } from './calendarEventSearchOccurrence';++export type CalendarEventMetadataOccurrence =+ null | CalendarEventSearchOccurrence;diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchOccurrence.ts b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchOccurrence.tsnew file mode 100644index 0000000000..95b6816358--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchOccurrence.ts@@ -0,0 +1,23 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */+import type { CalendarEventSearchTime } from './calendarEventSearchTime';++/**+ * The instance a search row points at.++A recurring series is indexed once, as its master. Which instance the row+should show is decided per request rather than baked into the index: the+next occurrence at or after now, else the most recent past one. This is+the same resolution a calendar mention performs, so a search row and a+mention of the same event agree on where they land.+ */+export interface CalendarEventSearchOccurrence {+ /** Stable key of the resolved instance within its series. */+ occurrenceKey: string;+ /** Span of the resolved instance. */+ time: CalendarEventSearchTime;+}diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchResponseItem.ts b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchResponseItem.tsnew file mode 100644index 0000000000..63ac570cbf--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchResponseItem.ts@@ -0,0 +1,20 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */+import type { CalendarEventSearchResult } from './calendarEventSearchResult';++/**+ * A single response item, part of the CalendarEventSearchResponse object+ */+export interface CalendarEventSearchResponseItem {+ calendar_event_search_results: CalendarEventSearchResult[];+ created_at: string;+ /** Standardized fields that all item types will share. */+ id: string;+ name: string;+ owner_id: string;+ updated_at: string;+}diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchResponseItemWithMetadata.ts b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchResponseItemWithMetadata.tsnew file mode 100644index 0000000000..c4283d03ba--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchResponseItemWithMetadata.ts@@ -0,0 +1,15 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */+import type { CalendarEventSearchResponseItem } from './calendarEventSearchResponseItem';+import type { CalendarEventSearchResponseItemWithMetadataAllOf } from './calendarEventSearchResponseItemWithMetadataAllOf';++/**+ * CalendarEventSearchResponseItem with metadata fetched from macrodb.+ */+export type CalendarEventSearchResponseItemWithMetadata =+ CalendarEventSearchResponseItem &+ CalendarEventSearchResponseItemWithMetadataAllOf;diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchResponseItemWithMetadataAllOf.ts b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchResponseItemWithMetadataAllOf.tsnew file mode 100644index 0000000000..a337e5d36c--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchResponseItemWithMetadataAllOf.ts@@ -0,0 +1,14 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */+import type { CalendarEventSearchResponseItemWithMetadataAllOfMetadata } from './calendarEventSearchResponseItemWithMetadataAllOfMetadata';+import type { CalendarEventSearchResponseItemWithMetadataAllOfProperties } from './calendarEventSearchResponseItemWithMetadataAllOfProperties';++export type CalendarEventSearchResponseItemWithMetadataAllOf = {+ metadata?: CalendarEventSearchResponseItemWithMetadataAllOfMetadata;+ /** Entity properties (e.g. tags) on the event. */+ properties?: CalendarEventSearchResponseItemWithMetadataAllOfProperties;+};diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchResponseItemWithMetadataAllOfMetadata.ts b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchResponseItemWithMetadataAllOfMetadata.tsnew file mode 100644index 0000000000..83a380773c--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchResponseItemWithMetadataAllOfMetadata.ts@@ -0,0 +1,10 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */+import type { CalendarEventMetadata } from './calendarEventMetadata';++export type CalendarEventSearchResponseItemWithMetadataAllOfMetadata =+ null | CalendarEventMetadata;diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchResponseItemWithMetadataAllOfProperties.ts b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchResponseItemWithMetadataAllOfProperties.tsnew file mode 100644index 0000000000..108eb87c39--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchResponseItemWithMetadataAllOfProperties.ts@@ -0,0 +1,14 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */+import type { SoupProperty } from './soupProperty';++/**+ * Entity properties (e.g. tags) on the event.+ */+export type CalendarEventSearchResponseItemWithMetadataAllOfProperties =+ | SoupProperty[]+ | null;diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchResult.ts b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchResult.tsnew file mode 100644index 0000000000..f08dad0030--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchResult.ts@@ -0,0 +1,15 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */++import type { CalendarEventSearchResultScore } from './calendarEventSearchResultScore';+import type { SearchHighlight } from './searchHighlight';++export interface CalendarEventSearchResult {+ highlight: SearchHighlight;+ /** The score of the result */+ score?: CalendarEventSearchResultScore;+}diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchResultScore.ts b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchResultScore.tsnew file mode 100644index 0000000000..062651890c--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchResultScore.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */++/**+ * The score of the result+ */+export type CalendarEventSearchResultScore = number | null;diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchTime.ts b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchTime.tsnew file mode 100644index 0000000000..180dbd5f12--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchTime.ts@@ -0,0 +1,24 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */+import type { CalendarEventSearchTimeOneOf } from './calendarEventSearchTimeOneOf';+import type { CalendarEventSearchTimeOneOfFour } from './calendarEventSearchTimeOneOfFour';++/**+ * Timed or all-day span of a calendar event.++Deliberately a local mirror of `models_soup::calendar_event::SoupCalendarEventTime`+rather than a reuse of it: this module needs both `ToSchema` (OpenAPI) and+`JsonSchema` (AI tool schemas), and `models_soup` carries no schemars+dependency. The wire shape is identical and must stay that way, so a+client can decode a search row and a soup row with one mapping.++Fields are camelCased per variant rather than via `rename_all_fields`,+which utoipa ignores.+ */+export type CalendarEventSearchTime =+ | CalendarEventSearchTimeOneOf+ | CalendarEventSearchTimeOneOfFour;diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchTimeOneOf.ts b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchTimeOneOf.tsnew file mode 100644index 0000000000..c7ae62d96e--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchTimeOneOf.ts@@ -0,0 +1,21 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */+import type { CalendarEventSearchTimeOneOfKind } from './calendarEventSearchTimeOneOfKind';+import type { CalendarEventSearchTimeOneOfTimeZone } from './calendarEventSearchTimeOneOfTimeZone';++/**+ * Absolute timed event.+ */+export type CalendarEventSearchTimeOneOf = {+ /** Exclusive end. */+ endsAt: string;+ kind: CalendarEventSearchTimeOneOfKind;+ /** Inclusive start. */+ startsAt: string;+ /** Original IANA time zone. */+ timeZone?: CalendarEventSearchTimeOneOfTimeZone;+};diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchTimeOneOfFour.ts b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchTimeOneOfFour.tsnew file mode 100644index 0000000000..ae0b5b4e6b--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchTimeOneOfFour.ts@@ -0,0 +1,18 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */+import type { CalendarEventSearchTimeOneOfFourKind } from './calendarEventSearchTimeOneOfFourKind';++/**+ * All-day event with an exclusive end date.+ */+export type CalendarEventSearchTimeOneOfFour = {+ /** Exclusive end date. */+ endDate: string;+ kind: CalendarEventSearchTimeOneOfFourKind;+ /** Inclusive start date. */+ startDate: string;+};diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchTimeOneOfFourKind.ts b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchTimeOneOfFourKind.tsnew file mode 100644index 0000000000..faf79712a1--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchTimeOneOfFourKind.ts@@ -0,0 +1,14 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */++export type CalendarEventSearchTimeOneOfFourKind =+ (typeof CalendarEventSearchTimeOneOfFourKind)[keyof typeof CalendarEventSearchTimeOneOfFourKind];++// eslint-disable-next-line @typescript-eslint/no-redeclare+export const CalendarEventSearchTimeOneOfFourKind = {+ allDay: 'allDay',+} as const;diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchTimeOneOfKind.ts b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchTimeOneOfKind.tsnew file mode 100644index 0000000000..9183315bd5--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchTimeOneOfKind.ts@@ -0,0 +1,14 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */++export type CalendarEventSearchTimeOneOfKind =+ (typeof CalendarEventSearchTimeOneOfKind)[keyof typeof CalendarEventSearchTimeOneOfKind];++// eslint-disable-next-line @typescript-eslint/no-redeclare+export const CalendarEventSearchTimeOneOfKind = {+ timed: 'timed',+} as const;diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchTimeOneOfTimeZone.ts b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchTimeOneOfTimeZone.tsnew file mode 100644index 0000000000..7ddeaae39e--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/calendarEventSearchTimeOneOfTimeZone.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */++/**+ * Original IANA time zone.+ */+export type CalendarEventSearchTimeOneOfTimeZone = string | null;diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/index.ts b/apps/web/src/lib/service-clients/service-search/generated/models/index.tsindex 9c97ffb467..fe025b476f 100644--- a/apps/web/src/lib/service-clients/service-search/generated/models/index.ts+++ b/apps/web/src/lib/service-clients/service-search/generated/models/index.ts@@ -8,6 +8,23 @@export * from './calendarEventFilters';export * from './calendarEventFiltersEndsAfter';export * from './calendarEventFiltersStartsBefore';+export * from './calendarEventMetadata';+export * from './calendarEventMetadataConferenceUrl';+export * from './calendarEventMetadataOccurrence';+export * from './calendarEventSearchOccurrence';+export * from './calendarEventSearchResponseItem';+export * from './calendarEventSearchResponseItemWithMetadata';+export * from './calendarEventSearchResponseItemWithMetadataAllOf';+export * from './calendarEventSearchResponseItemWithMetadataAllOfMetadata';+export * from './calendarEventSearchResponseItemWithMetadataAllOfProperties';+export * from './calendarEventSearchResult';+export * from './calendarEventSearchResultScore';+export * from './calendarEventSearchTime';+export * from './calendarEventSearchTimeOneOf';+export * from './calendarEventSearchTimeOneOfFour';+export * from './calendarEventSearchTimeOneOfFourKind';+export * from './calendarEventSearchTimeOneOfKind';+export * from './calendarEventSearchTimeOneOfTimeZone';export * from './callFilters';export * from './callFiltersAttended';export * from './callFiltersStatus';@@ -215,6 +232,8 @@ export * from './searchHighlightUserId';export * from './searchOn';export * from './searchResponse';export * from './sharedEmailFilter';+export * from './simpleCalendarEventSearchResponseBaseItemHumanReadableTimestamp';+export * from './simpleCalendarEventSearchResponseBaseItemHumanReadableTimestampOccurrenceKey';export * from './simpleCallRecordSearchResponseBaseItemHumanReadableTimestamp';export * from './simpleCallRecordSearchResponseBaseItemHumanReadableTimestampChannelName';export * from './simpleChannelSearchReponseBaseItem';@@ -252,6 +271,9 @@ export * from './simpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfAexport * from './simpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfFour';export * from './simpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfFourAllOf';export * from './simpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfFourAllOfType';+export * from './simpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnenine';+export * from './simpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnenineAllOf';+export * from './simpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnenineAllOfType';export * from './simpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnesix';export * from './simpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnesixAllOf';export * from './simpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnesixAllOfType';@@ -270,6 +292,9 @@ export * from './simpleUnifiedSearchResponseBaseItemOneOfAllOfType';export * from './simpleUnifiedSearchResponseBaseItemOneOfFour';export * from './simpleUnifiedSearchResponseBaseItemOneOfFourAllOf';export * from './simpleUnifiedSearchResponseBaseItemOneOfFourAllOfType';+export * from './simpleUnifiedSearchResponseBaseItemOneOfOnenine';+export * from './simpleUnifiedSearchResponseBaseItemOneOfOnenineAllOf';+export * from './simpleUnifiedSearchResponseBaseItemOneOfOnenineAllOfType';export * from './simpleUnifiedSearchResponseBaseItemOneOfOnesix';export * from './simpleUnifiedSearchResponseBaseItemOneOfOnesixAllOf';export * from './simpleUnifiedSearchResponseBaseItemOneOfOnesixAllOfType';@@ -313,6 +338,9 @@ export * from './unifiedSearchResponseItemOneOfOnezeroAllOfType';export * from './unifiedSearchResponseItemOneOfSeven';export * from './unifiedSearchResponseItemOneOfSevenAllOf';export * from './unifiedSearchResponseItemOneOfSevenAllOfType';+export * from './unifiedSearchResponseItemOneOfTwofive';+export * from './unifiedSearchResponseItemOneOfTwofiveAllOf';+export * from './unifiedSearchResponseItemOneOfTwofiveAllOfType';export * from './unifiedSearchResponseItemOneOfTwotwo';export * from './unifiedSearchResponseItemOneOfTwotwoAllOf';export * from './unifiedSearchResponseItemOneOfTwotwoAllOfType';diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/simpleCalendarEventSearchResponseBaseItemHumanReadableTimestamp.ts b/apps/web/src/lib/service-clients/service-search/generated/models/simpleCalendarEventSearchResponseBaseItemHumanReadableTimestamp.tsnew file mode 100644index 0000000000..ac02139113--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/simpleCalendarEventSearchResponseBaseItemHumanReadableTimestamp.ts@@ -0,0 +1,27 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */+import type { SearchHighlight } from './searchHighlight';+import type { SimpleCalendarEventSearchResponseBaseItemHumanReadableTimestampOccurrenceKey } from './simpleCalendarEventSearchResponseBaseItemHumanReadableTimestampOccurrenceKey';++export interface SimpleCalendarEventSearchResponseBaseItemHumanReadableTimestamp {+ /** The calendar event id */+ calendar_event_id: string;+ /** The time the event was created */+ created_at: string;+ /** The highlights on the event */+ highlight: SearchHighlight;+ /** Whether the series carries a recurrence rule */+ is_recurring: boolean;+ /** Stable key of the instance this row points at, when one resolved */+ occurrence_key?: SimpleCalendarEventSearchResponseBaseItemHumanReadableTimestampOccurrenceKey;+ /** The event title */+ title: string;+ /** The time the event was last updated */+ updated_at: string;+ /** The owner of this event projection */+ user_id: string;+}diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/simpleCalendarEventSearchResponseBaseItemHumanReadableTimestampOccurrenceKey.ts b/apps/web/src/lib/service-clients/service-search/generated/models/simpleCalendarEventSearchResponseBaseItemHumanReadableTimestampOccurrenceKey.tsnew file mode 100644index 0000000000..00091bf7e9--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/simpleCalendarEventSearchResponseBaseItemHumanReadableTimestampOccurrenceKey.ts@@ -0,0 +1,13 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */++/**+ * Stable key of the instance this row points at, when one resolved+ */+export type SimpleCalendarEventSearchResponseBaseItemHumanReadableTimestampOccurrenceKey =+ | string+ | null;diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/simpleUnifiedSearchResponseBaseItem.ts b/apps/web/src/lib/service-clients/service-search/generated/models/simpleUnifiedSearchResponseBaseItem.tsindex f43ce6ce19..55c352ca74 100644--- a/apps/web/src/lib/service-clients/service-search/generated/models/simpleUnifiedSearchResponseBaseItem.ts+++ b/apps/web/src/lib/service-clients/service-search/generated/models/simpleUnifiedSearchResponseBaseItem.ts@@ -6,6 +6,7 @@*/import type { SimpleUnifiedSearchResponseBaseItemOneOf } from './simpleUnifiedSearchResponseBaseItemOneOf';import type { SimpleUnifiedSearchResponseBaseItemOneOfFour } from './simpleUnifiedSearchResponseBaseItemOneOfFour';+import type { SimpleUnifiedSearchResponseBaseItemOneOfOnenine } from './simpleUnifiedSearchResponseBaseItemOneOfOnenine';import type { SimpleUnifiedSearchResponseBaseItemOneOfOnesix } from './simpleUnifiedSearchResponseBaseItemOneOfOnesix';import type { SimpleUnifiedSearchResponseBaseItemOneOfOnethree } from './simpleUnifiedSearchResponseBaseItemOneOfOnethree';import type { SimpleUnifiedSearchResponseBaseItemOneOfOnezero } from './simpleUnifiedSearchResponseBaseItemOneOfOnezero';@@ -17,4 +18,5 @@ export type SimpleUnifiedSearchResponseBaseItem =| SimpleUnifiedSearchResponseBaseItemOneOfSeven| SimpleUnifiedSearchResponseBaseItemOneOfOnezero| SimpleUnifiedSearchResponseBaseItemOneOfOnethree- | SimpleUnifiedSearchResponseBaseItemOneOfOnesix;+ | SimpleUnifiedSearchResponseBaseItemOneOfOnesix+ | SimpleUnifiedSearchResponseBaseItemOneOfOnenine;diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/simpleUnifiedSearchResponseBaseItemHumanReadableTimestamp.ts b/apps/web/src/lib/service-clients/service-search/generated/models/simpleUnifiedSearchResponseBaseItemHumanReadableTimestamp.tsindex 19dd22194e..06cb1f94c9 100644--- a/apps/web/src/lib/service-clients/service-search/generated/models/simpleUnifiedSearchResponseBaseItemHumanReadableTimestamp.ts+++ b/apps/web/src/lib/service-clients/service-search/generated/models/simpleUnifiedSearchResponseBaseItemHumanReadableTimestamp.ts@@ -6,6 +6,7 @@*/import type { SimpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOf } from './simpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOf';import type { SimpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfFour } from './simpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfFour';+import type { SimpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnenine } from './simpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnenine';import type { SimpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnesix } from './simpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnesix';import type { SimpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnethree } from './simpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnethree';import type { SimpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnezero } from './simpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnezero';@@ -17,4 +18,5 @@ export type SimpleUnifiedSearchResponseBaseItemHumanReadableTimestamp =| SimpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfSeven| SimpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnezero| SimpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnethree- | SimpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnesix;+ | SimpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnesix+ | SimpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnenine;diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/simpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnenine.ts b/apps/web/src/lib/service-clients/service-search/generated/models/simpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnenine.tsnew file mode 100644index 0000000000..d6437d58ac--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/simpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnenine.ts@@ -0,0 +1,12 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */+import type { SimpleCalendarEventSearchResponseBaseItemHumanReadableTimestamp } from './simpleCalendarEventSearchResponseBaseItemHumanReadableTimestamp';+import type { SimpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnenineAllOf } from './simpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnenineAllOf';++export type SimpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnenine =+ SimpleCalendarEventSearchResponseBaseItemHumanReadableTimestamp &+ SimpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnenineAllOf;diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/simpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnenineAllOf.ts b/apps/web/src/lib/service-clients/service-search/generated/models/simpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnenineAllOf.tsnew file mode 100644index 0000000000..55c656d403--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/simpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnenineAllOf.ts@@ -0,0 +1,12 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */+import type { SimpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnenineAllOfType } from './simpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnenineAllOfType';++export type SimpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnenineAllOf =+ {+ type: SimpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnenineAllOfType;+ };diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/simpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnenineAllOfType.ts b/apps/web/src/lib/service-clients/service-search/generated/models/simpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnenineAllOfType.tsnew file mode 100644index 0000000000..a62ecf2c86--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/simpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnenineAllOfType.ts@@ -0,0 +1,15 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */++export type SimpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnenineAllOfType =+ (typeof SimpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnenineAllOfType)[keyof typeof SimpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnenineAllOfType];++// eslint-disable-next-line @typescript-eslint/no-redeclare+export const SimpleUnifiedSearchResponseBaseItemHumanReadableTimestampOneOfOnenineAllOfType =+ {+ calendarEvent: 'calendarEvent',+ } as const;diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/simpleUnifiedSearchResponseBaseItemOneOfOnenine.ts b/apps/web/src/lib/service-clients/service-search/generated/models/simpleUnifiedSearchResponseBaseItemOneOfOnenine.tsnew file mode 100644index 0000000000..58cf8d22a7--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/simpleUnifiedSearchResponseBaseItemOneOfOnenine.ts@@ -0,0 +1,12 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */+import type { SimpleCalendarEventSearchResponseBaseItemHumanReadableTimestamp } from './simpleCalendarEventSearchResponseBaseItemHumanReadableTimestamp';+import type { SimpleUnifiedSearchResponseBaseItemOneOfOnenineAllOf } from './simpleUnifiedSearchResponseBaseItemOneOfOnenineAllOf';++export type SimpleUnifiedSearchResponseBaseItemOneOfOnenine =+ SimpleCalendarEventSearchResponseBaseItemHumanReadableTimestamp &+ SimpleUnifiedSearchResponseBaseItemOneOfOnenineAllOf;diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/simpleUnifiedSearchResponseBaseItemOneOfOnenineAllOf.ts b/apps/web/src/lib/service-clients/service-search/generated/models/simpleUnifiedSearchResponseBaseItemOneOfOnenineAllOf.tsnew file mode 100644index 0000000000..48ae12ba28--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/simpleUnifiedSearchResponseBaseItemOneOfOnenineAllOf.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */+import type { SimpleUnifiedSearchResponseBaseItemOneOfOnenineAllOfType } from './simpleUnifiedSearchResponseBaseItemOneOfOnenineAllOfType';++export type SimpleUnifiedSearchResponseBaseItemOneOfOnenineAllOf = {+ type: SimpleUnifiedSearchResponseBaseItemOneOfOnenineAllOfType;+};diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/simpleUnifiedSearchResponseBaseItemOneOfOnenineAllOfType.ts b/apps/web/src/lib/service-clients/service-search/generated/models/simpleUnifiedSearchResponseBaseItemOneOfOnenineAllOfType.tsnew file mode 100644index 0000000000..52be7e42cb--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/simpleUnifiedSearchResponseBaseItemOneOfOnenineAllOfType.ts@@ -0,0 +1,14 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */++export type SimpleUnifiedSearchResponseBaseItemOneOfOnenineAllOfType =+ (typeof SimpleUnifiedSearchResponseBaseItemOneOfOnenineAllOfType)[keyof typeof SimpleUnifiedSearchResponseBaseItemOneOfOnenineAllOfType];++// eslint-disable-next-line @typescript-eslint/no-redeclare+export const SimpleUnifiedSearchResponseBaseItemOneOfOnenineAllOfType = {+ calendarEvent: 'calendarEvent',+} as const;diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/unifiedSearchResponseItem.ts b/apps/web/src/lib/service-clients/service-search/generated/models/unifiedSearchResponseItem.tsindex 5bc3a01b8b..29125d7712 100644--- a/apps/web/src/lib/service-clients/service-search/generated/models/unifiedSearchResponseItem.ts+++ b/apps/web/src/lib/service-clients/service-search/generated/models/unifiedSearchResponseItem.ts@@ -11,6 +11,7 @@ import type { UnifiedSearchResponseItemOneOfOnesix } from './unifiedSearchResponimport type { UnifiedSearchResponseItemOneOfOnethree } from './unifiedSearchResponseItemOneOfOnethree';import type { UnifiedSearchResponseItemOneOfOnezero } from './unifiedSearchResponseItemOneOfOnezero';import type { UnifiedSearchResponseItemOneOfSeven } from './unifiedSearchResponseItemOneOfSeven';+import type { UnifiedSearchResponseItemOneOfTwofive } from './unifiedSearchResponseItemOneOfTwofive';import type { UnifiedSearchResponseItemOneOfTwotwo } from './unifiedSearchResponseItemOneOfTwotwo';export type UnifiedSearchResponseItem =@@ -21,4 +22,5 @@ export type UnifiedSearchResponseItem =| UnifiedSearchResponseItemOneOfOnethree| UnifiedSearchResponseItemOneOfOnesix| UnifiedSearchResponseItemOneOfOnenine- | UnifiedSearchResponseItemOneOfTwotwo;+ | UnifiedSearchResponseItemOneOfTwotwo+ | UnifiedSearchResponseItemOneOfTwofive;diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/unifiedSearchResponseItemOneOfTwofive.ts b/apps/web/src/lib/service-clients/service-search/generated/models/unifiedSearchResponseItemOneOfTwofive.tsnew file mode 100644index 0000000000..4b1b042686--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/unifiedSearchResponseItemOneOfTwofive.ts@@ -0,0 +1,12 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */+import type { CalendarEventSearchResponseItemWithMetadata } from './calendarEventSearchResponseItemWithMetadata';+import type { UnifiedSearchResponseItemOneOfTwofiveAllOf } from './unifiedSearchResponseItemOneOfTwofiveAllOf';++export type UnifiedSearchResponseItemOneOfTwofive =+ CalendarEventSearchResponseItemWithMetadata &+ UnifiedSearchResponseItemOneOfTwofiveAllOf;diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/unifiedSearchResponseItemOneOfTwofiveAllOf.ts b/apps/web/src/lib/service-clients/service-search/generated/models/unifiedSearchResponseItemOneOfTwofiveAllOf.tsnew file mode 100644index 0000000000..36c7a9d589--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/unifiedSearchResponseItemOneOfTwofiveAllOf.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */+import type { UnifiedSearchResponseItemOneOfTwofiveAllOfType } from './unifiedSearchResponseItemOneOfTwofiveAllOfType';++export type UnifiedSearchResponseItemOneOfTwofiveAllOf = {+ type: UnifiedSearchResponseItemOneOfTwofiveAllOfType;+};diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/unifiedSearchResponseItemOneOfTwofiveAllOfType.ts b/apps/web/src/lib/service-clients/service-search/generated/models/unifiedSearchResponseItemOneOfTwofiveAllOfType.tsnew file mode 100644index 0000000000..a004812308--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/unifiedSearchResponseItemOneOfTwofiveAllOfType.ts@@ -0,0 +1,14 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */++export type UnifiedSearchResponseItemOneOfTwofiveAllOfType =+ (typeof UnifiedSearchResponseItemOneOfTwofiveAllOfType)[keyof typeof UnifiedSearchResponseItemOneOfTwofiveAllOfType];++// eslint-disable-next-line @typescript-eslint/no-redeclare+export const UnifiedSearchResponseItemOneOfTwofiveAllOfType = {+ calendarEvent: 'calendarEvent',+} as const;diff --git a/apps/web/src/lib/service-clients/service-search/openapi.json b/apps/web/src/lib/service-clients/service-search/openapi.jsonindex 79f55767dc..225f0bfcc0 100644--- a/apps/web/src/lib/service-clients/service-search/openapi.json+++ b/apps/web/src/lib/service-clients/service-search/openapi.json@@ -217,6 +217,209 @@}}},+ "CalendarEventMetadata": {+ "type": "object",+ "description": "Metadata for a calendar event fetched from the database.\n\nThe index carries the master span for ranking, but the occurrence a row\nshould display depends on when the query ran, so it is resolved here\nrather than indexed.",+ "required": [+ "createdAt",+ "updatedAt",+ "status",+ "time",+ "isRecurring",+ "isReadOnly"+ ],+ "properties": {+ "conferenceUrl": {+ "type": ["string", "null"],+ "description": "Direct conference join URL when known."+ },+ "createdAt": {+ "type": "string",+ "format": "date-time"+ },+ "isReadOnly": {+ "type": "boolean",+ "description": "Whether the canonical source prohibits mutation."+ },+ "isRecurring": {+ "type": "boolean",+ "description": "Whether the series carries a recurrence rule. Lets a row render a\nrecurring badge without parsing the rules."+ },+ "occurrence": {+ "oneOf": [+ {+ "type": "null"+ },+ {+ "$ref": "#/components/schemas/CalendarEventSearchOccurrence",+ "description": "The instance this row points at. `None` when the series has no\nmaterialized occurrence — occurrences exist only inside a rolling\nwindow, so a row can legitimately fall back to `time`."+ }+ ]+ },+ "status": {+ "type": "string",+ "description": "Canonical status (`confirmed`, `tentative`, `cancelled`)."+ },+ "time": {+ "$ref": "#/components/schemas/CalendarEventSearchTime",+ "description": "The series' own span."+ },+ "updatedAt": {+ "type": "string",+ "format": "date-time"+ }+ }+ },+ "CalendarEventSearchOccurrence": {+ "type": "object",+ "description": "The instance a search row points at.\n\nA recurring series is indexed once, as its master. Which instance the row\nshould show is decided per request rather than baked into the index: the\nnext occurrence at or after now, else the most recent past one. This is\nthe same resolution a calendar mention performs, so a search row and a\nmention of the same event agree on where they land.",+ "required": ["occurrenceKey", "time"],+ "properties": {+ "occurrenceKey": {+ "type": "string",+ "description": "Stable key of the resolved instance within its series."+ },+ "time": {+ "$ref": "#/components/schemas/CalendarEventSearchTime",+ "description": "Span of the resolved instance."+ }+ }+ },+ "CalendarEventSearchResponseItem": {+ "type": "object",+ "description": "A single response item, part of the CalendarEventSearchResponse object",+ "required": [+ "id",+ "name",+ "owner_id",+ "updated_at",+ "created_at",+ "calendar_event_search_results"+ ],+ "properties": {+ "calendar_event_search_results": {+ "type": "array",+ "items": {+ "$ref": "#/components/schemas/CalendarEventSearchResult"+ }+ },+ "created_at": {+ "type": "string",+ "format": "date-time"+ },+ "id": {+ "type": "string",+ "format": "uuid",+ "description": "Standardized fields that all item types will share."+ },+ "name": {+ "type": "string"+ },+ "owner_id": {+ "type": "string"+ },+ "updated_at": {+ "type": "string",+ "format": "date-time"+ }+ }+ },+ "CalendarEventSearchResponseItemWithMetadata": {+ "allOf": [+ {+ "$ref": "#/components/schemas/CalendarEventSearchResponseItem"+ },+ {+ "type": "object",+ "properties": {+ "metadata": {+ "oneOf": [+ {+ "type": "null"+ },+ {+ "$ref": "#/components/schemas/CalendarEventMetadata",+ "description": "Metadata from the database. None if the event no longer exists."+ }+ ]+ },+ "properties": {+ "type": ["array", "null"],+ "items": {+ "$ref": "#/components/schemas/SoupProperty"+ },+ "description": "Entity properties (e.g. tags) on the event."+ }+ }+ }+ ],+ "description": "CalendarEventSearchResponseItem with metadata fetched from macrodb."+ },+ "CalendarEventSearchResult": {+ "type": "object",+ "required": ["highlight"],+ "properties": {+ "highlight": {+ "$ref": "#/components/schemas/SearchHighlight"+ },+ "score": {+ "type": ["number", "null"],+ "format": "double",+ "description": "The score of the result"+ }+ }+ },+ "CalendarEventSearchTime": {+ "oneOf": [+ {+ "type": "object",+ "description": "Absolute timed event.",+ "required": ["startsAt", "endsAt", "kind"],+ "properties": {+ "endsAt": {+ "type": "string",+ "format": "date-time",+ "description": "Exclusive end."+ },+ "kind": {+ "type": "string",+ "enum": ["timed"]+ },+ "startsAt": {+ "type": "string",+ "format": "date-time",+ "description": "Inclusive start."+ },+ "timeZone": {+ "type": ["string", "null"],+ "description": "Original IANA time zone."+ }+ }+ },+ {+ "type": "object",+ "description": "All-day event with an exclusive end date.",+ "required": ["startDate", "endDate", "kind"],+ "properties": {+ "endDate": {+ "type": "string",+ "format": "date",+ "description": "Exclusive end date."+ },+ "kind": {+ "type": "string",+ "enum": ["allDay"]+ },+ "startDate": {+ "type": "string",+ "format": "date",+ "description": "Inclusive start date."+ }+ }+ }+ ],+ "description": "Timed or all-day span of a calendar event.\n\nDeliberately a local mirror of `models_soup::calendar_event::SoupCalendarEventTime`\nrather than a reuse of it: this module needs both `ToSchema` (OpenAPI) and\n`JsonSchema` (AI tool schemas), and `models_soup` carries no schemars\ndependency. The wire shape is identical and must stay that way, so a\nclient can decode a search row and a soup row with one mapping.\n\nFields are camelCased per variant rather than via `rename_all_fields`,\nwhich utoipa ignores."+ },"CallFilters": {"type": "object","description": "Filters for call records.",@@ -2947,6 +3150,64 @@"description": "Controls whether shared email threads are included in results.","enum": ["exclude", "include", "only"]},+ "SimpleCalendarEventSearchResponseBaseItem_HumanReadableTimestamp": {+ "type": "object",+ "required": [+ "calendar_event_id",+ "updated_at",+ "created_at",+ "title",+ "user_id",+ "is_recurring",+ "highlight"+ ],+ "properties": {+ "calendar_event_id": {+ "type": "string",+ "description": "The calendar event id"+ },+ "created_at": {+ "oneOf": [+ {+ "type": "string",+ "format": "date-time",+ "description": "DateTime<Utc> ts_seconds deserialization + RFC3339 serialization"+ }+ ],+ "description": "The time the event was created"+ },+ "highlight": {+ "$ref": "#/components/schemas/SearchHighlight",+ "description": "The highlights on the event"+ },+ "is_recurring": {+ "type": "boolean",+ "description": "Whether the series carries a recurrence rule"+ },+ "occurrence_key": {+ "type": ["string", "null"],+ "description": "Stable key of the instance this row points at, when one resolved"+ },+ "title": {+ "type": "string",+ "description": "The event title"+ },+ "updated_at": {+ "oneOf": [+ {+ "type": "string",+ "format": "date-time",+ "description": "DateTime<Utc> ts_seconds deserialization + RFC3339 serialization"+ }+ ],+ "description": "The time the event was last updated"+ },+ "user_id": {+ "type": "string",+ "description": "The owner of this event projection"+ }+ }+ },"SimpleCallRecordSearchResponseBaseItem_HumanReadableTimestamp": {"type": "object","required": [@@ -3811,6 +4072,23 @@}}]+ },+ {+ "allOf": [+ {+ "$ref": "#/components/schemas/SimpleCalendarEventSearchResponseBaseItem_HumanReadableTimestamp"+ },+ {+ "type": "object",+ "required": ["type"],+ "properties": {+ "type": {+ "type": "string",+ "enum": ["calendarEvent"]+ }+ }+ }+ ]}]},@@ -3917,6 +4195,23 @@}}]+ },+ {+ "allOf": [+ {+ "$ref": "#/components/schemas/SimpleCalendarEventSearchResponseBaseItem_HumanReadableTimestamp"+ },+ {+ "type": "object",+ "required": ["type"],+ "properties": {+ "type": {+ "type": "string",+ "enum": ["calendarEvent"]+ }+ }+ }+ ]}]},@@ -4145,6 +4440,23 @@}}]+ },+ {+ "allOf": [+ {+ "$ref": "#/components/schemas/CalendarEventSearchResponseItemWithMetadata"+ },+ {+ "type": "object",+ "required": ["type"],+ "properties": {+ "type": {+ "type": "string",+ "enum": ["calendarEvent"]+ }+ }+ }+ ]}]}diff --git a/crates/ai_tools/src/search/search_service/content.rs b/crates/ai_tools/src/search/search_service/content.rsindex be6e9d33c3..d8bd508851 100644--- a/crates/ai_tools/src/search/search_service/content.rs+++ b/crates/ai_tools/src/search/search_service/content.rs@@ -35,7 +35,7 @@ pub struct ContentSearch {pub match_type: SearchMatchType,#[schemars(- description = "Which types of items to search. Leave empty (the default) to search all types — this is almost always what you want. Only set this when the user's request clearly targets one or more specific types. Examples: ['documents'], ['emails', 'documents'], ['channels'], ['call_records']."+ description = "Which types of items to search. Leave empty (the default) to search all types — this is almost always what you want. Only set this when the user's request clearly targets one or more specific types. Examples: ['documents'], ['emails', 'documents'], ['channels'], ['call_records']. Projects and calendar events carry no indexed content, so they never match here — search those by name with NameSearch instead.")]#[serde(default)]pub entity_types: Vec<UnifiedSearchIndex>,diff --git a/crates/ai_tools/src/search/search_service/name.rs b/crates/ai_tools/src/search/search_service/name.rsindex bec2753973..4eb9476bdc 100644--- a/crates/ai_tools/src/search/search_service/name.rs+++ b/crates/ai_tools/src/search/search_service/name.rs@@ -54,7 +54,8 @@ them with tagsMatch=\"all\". Each entry names a tag by its label, matched case-iagainst the user's own tags; only set scope (\"personal\" or \"team\") when the user \distinguishes between their personal and team tags. An unknown label fails with the list of \available tags — call ListTags first when unsure what tags exist. Only taggable items \-(documents, emails, AI chats, projects, call records) can match, so channels are dropped \+(documents, emails, AI chats, projects, call records, calendar events) can match, so \+channels are dropped \while a tag filter is active.")]#[serde(default)]pub tags: Option<Vec<TagFilter>>,diff --git a/crates/ai_tools/src/search/search_service/types.rs b/crates/ai_tools/src/search/search_service/types.rsindex fd8519b7d2..ecb68b6c68 100644--- a/crates/ai_tools/src/search/search_service/types.rs+++ b/crates/ai_tools/src/search/search_service/types.rs@@ -169,6 +169,7 @@ fn item_properties(item: &UnifiedSearchResponseItem) -> Option<&Vec<SoupPropertyUnifiedSearchResponseItem::Email(i) => i.properties.as_ref(),UnifiedSearchResponseItem::Project(i) => i.properties.as_ref(),UnifiedSearchResponseItem::Call(i) => i.properties.as_ref(),+ UnifiedSearchResponseItem::CalendarEvent(i) => i.properties.as_ref(),UnifiedSearchResponseItem::Channel(_)| UnifiedSearchResponseItem::ChannelMessage(_)| UnifiedSearchResponseItem::Company(_) => None,@@ -182,6 +183,7 @@ fn item_properties_mut(item: &mut UnifiedSearchResponseItem) -> Option<&mut Vec<UnifiedSearchResponseItem::Email(i) => i.properties.as_mut(),UnifiedSearchResponseItem::Project(i) => i.properties.as_mut(),UnifiedSearchResponseItem::Call(i) => i.properties.as_mut(),+ UnifiedSearchResponseItem::CalendarEvent(i) => i.properties.as_mut(),UnifiedSearchResponseItem::Channel(_)| UnifiedSearchResponseItem::ChannelMessage(_)| UnifiedSearchResponseItem::Company(_) => None,diff --git a/crates/calendar_events/Cargo.toml b/crates/calendar_events/Cargo.tomlindex d6102637f6..376cba30e9 100644--- a/crates/calendar_events/Cargo.toml+++ b/crates/calendar_events/Cargo.toml@@ -45,6 +45,8 @@ model_notifications = { path = "../model_notifications", optional = true }models_pagination = { path = "../models_pagination", optional = true }notification = { path = "../notification", optional = true }reqwest = { workspace = true, optional = true }+macro_event_broker = { path = "../macro_event_broker", default-features = false }+macro_event_topics = { path = "../macro_event_topics" }rootcause = { workspace = true }schemars = { workspace = true, optional = true }serde = { workspace = true }diff --git a/crates/calendar_events/src/domain/events.rs b/crates/calendar_events/src/domain/events.rsnew file mode 100644index 0000000000..2e1ed5463f--- /dev/null+++ b/crates/calendar_events/src/domain/events.rs@@ -0,0 +1,111 @@+//! Kafka event models for the `macro.calendar` topic.++use macro_event_broker::{Event, MacroEvent, TopicEvent};+use macro_event_topics::MacroCalendarTopic;+use serde::{Deserialize, Serialize};+use uuid::Uuid;++/// Metadata carried by every calendar topic event.+///+/// The same shape for all three variants: the variant says what happened, and+/// the owner lets a consumer route or filter without a database read.+/// Everything else about the event is re-read from the row — except on+/// [`CalendarTopicEvent::Deleted`], where there is no row left to read.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+pub struct CalendarEventMetadata {+ /// The event entity this change concerns.+ pub event_id: Uuid,+ /// Owner of this per-user event projection.+ pub owner_id: String,+}++/// Calendar events published to [`MacroCalendarTopic`].+///+/// The variants report what happened to the canonical `calendar_events` row,+/// not what a caller asked for: an idempotent re-create that lands on the+/// upsert's conflict path is an [`Updated`](CalendarTopicEvent::Updated), and+/// deleting one source of a multi-source event is too, because the row+/// survives on its next-best source.+///+/// A write that changes nothing publishes no event at all, so a full provider+/// snapshot re-observing thousands of unchanged events stays quiet.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+pub enum CalendarTopicEvent {+ /// The event row was inserted.+ #[serde(rename = "calendar_event.created")]+ Created(CalendarEventMetadata),+ /// The event row was rewritten in place.+ #[serde(rename = "calendar_event.updated")]+ Updated(CalendarEventMetadata),+ /// The event row is gone: its last remaining source was retired.+ /// Consumers cannot re-read it, so this is the only signal that the+ /// entity existed and no longer does.+ #[serde(rename = "calendar_event.deleted")]+ Deleted(CalendarEventMetadata),+}++impl CalendarTopicEvent {+ /// The event entity this change concerns.+ pub fn event_id(&self) -> Uuid {+ self.metadata().event_id+ }++ /// The metadata common to every variant.+ pub fn metadata(&self) -> &CalendarEventMetadata {+ match self {+ Self::Created(metadata) | Self::Updated(metadata) | Self::Deleted(metadata) => metadata,+ }+ }+}++impl TopicEvent for CalendarTopicEvent {+ type Topic = MacroCalendarTopic;++ const SCHEMA_VERSION: u8 = 1;+}++/// Publishable event for [`MacroCalendarTopic`], keyed by the event entity id.+///+/// Keying by entity id keeps every change to one event on one partition, so a+/// consumer sharding by key applies them in order.+pub struct CalendarMacroEvent {+ key: String,+ event: Event<CalendarTopicEvent>,+}++impl CalendarMacroEvent {+ /// Builds an event for one change, keyed by the event entity id.+ pub fn for_change(event: CalendarTopicEvent) -> Self {+ let key = event.event_id().to_string();+ Self::new(key, event)+ }++ /// Builds an event from a topic-specific calendar event.+ pub fn new(key: impl Into<String>, event: CalendarTopicEvent) -> Self {+ Self::with_event(key, Event::new(event))+ }++ /// Builds an event from a pre-built envelope.+ pub fn with_event(key: impl Into<String>, event: Event<CalendarTopicEvent>) -> Self {+ Self {+ key: key.into(),+ event,+ }+ }+}++impl MacroEvent for CalendarMacroEvent {+ type EventPayload = CalendarTopicEvent;++ fn key(&self) -> &str {+ &self.key+ }++ fn event(&self) -> &Event<Self::EventPayload> {+ &self.event+ }++ fn from_event(key: String, event: Event<Self::EventPayload>) -> Self {+ Self::with_event(key, event)+ }+}diff --git a/crates/calendar_events/src/domain/mod.rs b/crates/calendar_events/src/domain/mod.rsindex d3cd7be33f..9be62db172 100644--- a/crates/calendar_events/src/domain/mod.rs+++ b/crates/calendar_events/src/domain/mod.rs@@ -1,5 +1,7 @@//! Calendar domain layer.+/// Kafka event models for the calendar topic.+pub mod events;/// Domain models.pub mod models;/// User-initiated calendar mutation policy.diff --git a/crates/calendar_events/src/domain/mutations.rs b/crates/calendar_events/src/domain/mutations.rsindex 503a861c6c..8ed6b9aad7 100644--- a/crates/calendar_events/src/domain/mutations.rs+++ b/crates/calendar_events/src/domain/mutations.rs@@ -17,34 +17,89 @@ use super::{REMINDER_MINUTES_MAX, REMINDER_OVERRIDES_MAX,},ports::{- CalendarAccessTokenProvider, CalendarDeletionScope, CalendarEventWrite,- CalendarMutationError, CalendarMutationService, CalendarRepository, CalendarRsvpScope,- CalendarTokenError, CalendarUpdateScope, GoogleCalendarMutationProvider,- GoogleInstanceUpdateOutcome, GoogleProviderError, GoogleProviderErrorKind,- GoogleRsvpOutcome, GoogleSeriesMutationOutcome,+ CalendarAccessTokenProvider, CalendarDeletionScope, CalendarEventChange,+ CalendarEventWrite, CalendarEventWriteOutcome, CalendarMutationError,+ CalendarMutationService, CalendarRepository, CalendarRsvpScope, CalendarTokenError,+ CalendarUpdateScope, GoogleCalendarMutationProvider, GoogleInstanceUpdateOutcome,+ GoogleProviderError, GoogleProviderErrorKind, GoogleRsvpOutcome,+ GoogleSeriesMutationOutcome, RetiredCalendarEvent,},};+use crate::domain::events::{CalendarEventMetadata, CalendarMacroEvent, CalendarTopicEvent};+use macro_event_broker::MacroEventBroker;/// Calendar mutation use cases with provider, token, and persistence/// details behind ports.-pub struct CalendarMutationServiceImpl<R, G, T> {+pub struct CalendarMutationServiceImpl<R, G, T, B> {repository: R,provider: G,tokens: T,+ macro_event_broker: B,}-impl<R, G, T> CalendarMutationServiceImpl<R, G, T>+impl<R, G, T, B> CalendarMutationServiceImpl<R, G, T, B>whereR: CalendarRepository,G: GoogleCalendarMutationProvider,T: CalendarAccessTokenProvider,+ B: MacroEventBroker,{/// Construct the service from its ports.- pub fn new(repository: R, provider: G, tokens: T) -> Self {+ pub fn new(repository: R, provider: G, tokens: T, macro_event_broker: B) -> Self {Self {repository,provider,tokens,+ macro_event_broker,+ }+ }++ /// Publish one calendar topic event; failures are logged and dropped.+ ///+ /// The provider and the local projection are already updated by this+ /// point, so a publish failure must not fail the mutation.+ fn publish_calendar_event(&self, event: CalendarTopicEvent) {+ let _ = self+ .macro_event_broker+ .send_event(&CalendarMacroEvent::for_change(event))+ .inspect_err(|error| {+ tracing::error!(error=?error, "failed to publish calendar event");+ });+ }++ /// Announce every event a source retirement touched. Retiring a source+ /// does not necessarily remove the event — the row survives, rewritten+ /// from its next-best remaining source — so each event reports its own+ /// fate.+ fn publish_retirements(&self, retired: Vec<RetiredCalendarEvent>) {+ for event in retired {+ let metadata = CalendarEventMetadata {+ event_id: event.event_id,+ owner_id: event.owner_id,+ };+ self.publish_calendar_event(if event.deleted {+ CalendarTopicEvent::Deleted(metadata)+ } else {+ CalendarTopicEvent::Updated(metadata)+ });+ }+ }++ /// Announce what a write did to the canonical row. A write that changed+ /// nothing publishes nothing, so an idempotent replay stays quiet.+ fn publish_write_outcome(&self, outcome: &CalendarEventWriteOutcome) {+ let metadata = CalendarEventMetadata {+ event_id: outcome.event_id,+ owner_id: outcome.owner_id.clone(),+ };+ match outcome.change {+ CalendarEventChange::Created => {+ self.publish_calendar_event(CalendarTopicEvent::Created(metadata));+ }+ CalendarEventChange::Updated => {+ self.publish_calendar_event(CalendarTopicEvent::Updated(metadata));+ }+ CalendarEventChange::Unchanged => {}}}@@ -82,21 +137,23 @@ whereupsert: CalendarEventUpsert,) -> Result<CalendarEvent, CalendarMutationError> {let mut event = upsert.event.clone();- let event_id = self+ let outcome = self.repository.upsert_event(CalendarEventWrite::UserMutation(upsert)).await.map_err(|error| CalendarMutationError::PersistFailed(format!("{error:?}")))?;- event.id = event_id;+ event.id = outcome.event_id;+ self.publish_write_outcome(&outcome);Ok(event)}}-impl<R, G, T> CalendarMutationService for CalendarMutationServiceImpl<R, G, T>+impl<R, G, T, B> CalendarMutationService for CalendarMutationServiceImpl<R, G, T, B>whereR: CalendarRepository,G: GoogleCalendarMutationProvider,T: CalendarAccessTokenProvider,+ B: MacroEventBroker,{#[tracing::instrument(skip(self, requester_id, draft), err)]async fn create_event(@@ -283,15 +340,22 @@ where}// Either the deletion removed the series or it was already// gone; retiring the local source converges both.- GoogleSeriesMutationOutcome::SeriesDeleted | GoogleSeriesMutationOutcome::Gone => self- .repository- .remove_google_source(- target.account_id,- target.calendar_id,- target.master_provider_event_id(),- )- .await- .map_err(|error| CalendarMutationError::PersistFailed(format!("{error:?}"))),+ GoogleSeriesMutationOutcome::SeriesDeleted | GoogleSeriesMutationOutcome::Gone => {+ // Retiring a recurring master's source also retires its+ // expanded instances, so this reports several events; each+ // announces its own fate.+ let retired = self+ .repository+ .remove_google_source(+ target.account_id,+ target.calendar_id,+ target.master_provider_event_id(),+ )+ .await+ .map_err(|error| CalendarMutationError::PersistFailed(format!("{error:?}")))?;+ self.publish_retirements(retired);+ Ok(())+ }}}@@ -359,11 +423,12 @@ where}}-impl<R, G, T> CalendarMutationServiceImpl<R, G, T>+impl<R, G, T, B> CalendarMutationServiceImpl<R, G, T, B>whereR: CalendarRepository,G: GoogleCalendarMutationProvider,T: CalendarAccessTokenProvider,+ B: MacroEventBroker,{/// Close the push channels a disconnected calendar left open. Best-effort:/// the local calendars are already gone, so a notification that still@@ -416,7 +481,8 @@ where/// Best-effort cleanup when the provider reports the event gone: the/// regular sync converges the projection either way.async fn retire_gone_source(&self, target: &CalendarEventMutationTarget) {- self.repository+ let retired = self+ .repository.remove_google_source(target.account_id,target.calendar_id,@@ -430,7 +496,10 @@ where"failed to retire a provider-deleted calendar event source");})- .ok();+ .unwrap_or_default();+ // The row may be gone now, so search cannot rediscover this by+ // re-reading Postgres — the retirement has to be announced.+ self.publish_retirements(retired);}}diff --git a/crates/calendar_events/src/domain/mutations/test.rs b/crates/calendar_events/src/domain/mutations/test.rsindex 7db95b84ed..22243c6d41 100644--- a/crates/calendar_events/src/domain/mutations/test.rs+++ b/crates/calendar_events/src/domain/mutations/test.rs@@ -7,6 +7,7 @@ use crate::domain::models::{GoogleCalendarTarget, GoogleEventSource, GoogleWatchChannel, ProviderCalendar,StoredGoogleCalendar,};+use crate::domain::ports::RetiredCalendarEvent;use chrono::{Duration, TimeZone};use std::sync::{Arc, Mutex};@@ -113,17 +114,38 @@ fn draft() -> CalendarEventDraft {}}-#[derive(Clone, Default)]+#[derive(Clone)]struct FakeRepo {mutation_target: Option<CalendarEventMutationTarget>,creation_target: Option<CalendarCreationTarget>,persisted_event_id: Option<Uuid>,+ /// What the upsert reports doing to the row. `Created` by default, since+ /// most fixtures persist a fresh event.+ write_change: CalendarEventChange,upserts: Arc<Mutex<Vec<CalendarEventUpsert>>>,removed_sources: Arc<Mutex<Vec<(Uuid, Uuid, String)>>>,+ /// Per-event fates `remove_google_source` reports back.+ retired_events: Vec<RetiredCalendarEvent>,disconnected: Option<DisconnectedGoogleCalendar>,disconnect_requests: Arc<Mutex<Vec<(String, Uuid)>>>,}+impl Default for FakeRepo {+ fn default() -> Self {+ Self {+ mutation_target: None,+ creation_target: None,+ persisted_event_id: None,+ write_change: CalendarEventChange::Created,+ upserts: Default::default(),+ removed_sources: Default::default(),+ retired_events: Vec::new(),+ disconnected: None,+ disconnect_requests: Default::default(),+ }+ }+}+impl CalendarRepository for FakeRepo {async fn apply_google_grant(&self,@@ -146,12 +168,20 @@ impl CalendarRepository for FakeRepo {Ok(self.disconnected.clone())}- async fn upsert_event(&self, write: CalendarEventWrite) -> Result<Uuid, rootcause::Report> {+ async fn upsert_event(+ &self,+ write: CalendarEventWrite,+ ) -> Result<CalendarEventWriteOutcome, rootcause::Report> {let CalendarEventWrite::UserMutation(upsert) = write else {panic!("mutations must persist through the UserMutation authority");};+ let owner_id = upsert.event.owner_id.clone();self.upserts.lock().unwrap().push(upsert);- Ok(self.persisted_event_id.unwrap_or_else(Uuid::now_v7))+ Ok(CalendarEventWriteOutcome {+ event_id: self.persisted_event_id.unwrap_or_else(Uuid::now_v7),+ owner_id,+ change: self.write_change,+ })}async fn list_occurrences(@@ -197,7 +227,7 @@ impl CalendarRepository for FakeRepo {_account_id: Uuid,_sync: GoogleCalendarSyncSnapshot,_events_upserted: usize,- ) -> Result<(), rootcause::Report> {+ ) -> Result<Vec<RetiredCalendarEvent>, rootcause::Report> {unreachable!()}@@ -233,7 +263,7 @@ impl CalendarRepository for FakeRepo {_lease_token: Uuid,_account_id: Uuid,_calendar_ids: Vec<Uuid>,- ) -> Result<(), rootcause::Report> {+ ) -> Result<Vec<RetiredCalendarEvent>, rootcause::Report> {unreachable!()}@@ -266,13 +296,13 @@ impl CalendarRepository for FakeRepo {account_id: Uuid,calendar_id: Uuid,provider_event_id: &str,- ) -> Result<(), rootcause::Report> {+ ) -> Result<Vec<RetiredCalendarEvent>, rootcause::Report> {self.removed_sources.lock().unwrap().push((account_id,calendar_id,provider_event_id.to_string(),));- Ok(())+ Ok(self.retired_events.clone())}}@@ -494,12 +524,74 @@ impl CalendarAccessTokenProvider for FakeTokens {}}+/// One event captured off the broker.+#[derive(Clone, Debug)]+struct PublishedEvent {+ topic: &'static str,+ key: String,+ payload: serde_json::Value,+}++/// Records the calendar events a mutation published to the broker.+#[derive(Clone, Default)]+struct RecordingEventBroker {+ published: Arc<Mutex<Vec<PublishedEvent>>>,+ fail: bool,+}++impl RecordingEventBroker {+ fn failing() -> Self {+ Self {+ published: Default::default(),+ fail: true,+ }+ }++ fn published(&self) -> Vec<PublishedEvent> {+ self.published.lock().expect("broker lock").clone()+ }+}++impl macro_event_broker::MacroEventBroker for RecordingEventBroker {+ fn send_event<E: macro_event_broker::MacroEvent + ?Sized>(+ &self,+ event: &E,+ ) -> Result<+ tokio::task::JoinHandle<Result<(), macro_event_broker::EventBrokerError>>,+ macro_event_broker::EventBrokerError,+ > {+ if self.fail {+ return Err(macro_event_broker::EventBrokerError::Publish(+ "test failure".to_string(),+ ));+ }+ self.published+ .lock()+ .expect("broker lock")+ .push(PublishedEvent {+ topic: event.topic(),+ key: event.key().to_string(),+ payload: serde_json::to_value(event.event())?,+ });+ Ok(tokio::spawn(async { Ok(()) }))+ }+}+fn service(repo: FakeRepo,provider: FakeProvider,tokens: FakeTokens,-) -> CalendarMutationServiceImpl<FakeRepo, FakeProvider, FakeTokens> {- CalendarMutationServiceImpl::new(repo, provider, tokens)+) -> CalendarMutationServiceImpl<FakeRepo, FakeProvider, FakeTokens, RecordingEventBroker> {+ CalendarMutationServiceImpl::new(repo, provider, tokens, RecordingEventBroker::default())+}++fn service_with_broker(+ repo: FakeRepo,+ provider: FakeProvider,+ tokens: FakeTokens,+ broker: RecordingEventBroker,+) -> CalendarMutationServiceImpl<FakeRepo, FakeProvider, FakeTokens, RecordingEventBroker> {+ CalendarMutationServiceImpl::new(repo, provider, tokens, broker)}#[tokio::test]@@ -554,6 +646,212 @@ async fn create_persists_the_provider_echo_and_returns_the_applied_id() {assert_eq!(upserts.lock().unwrap().len(), 1);}+#[tokio::test]+async fn create_publishes_created_when_the_row_was_inserted() {+ let applied_id = Uuid::now_v7();+ let repo = FakeRepo {+ creation_target: Some(creation_target(false)),+ persisted_event_id: Some(applied_id),+ write_change: CalendarEventChange::Created,+ ..FakeRepo::default()+ };+ let broker = RecordingEventBroker::default();+ service_with_broker(+ repo,+ FakeProvider::new(FakeProviderBehavior::Echo),+ FakeTokens::ok(),+ broker.clone(),+ )+ .create_event("macro|user", None, None, draft())+ .await+ .unwrap();++ let published = broker.published();+ assert_eq!(published.len(), 1);+ let event = &published[0];+ assert_eq!(event.topic, "macro.calendar");+ // Keyed by entity id: the consumer shards on the key, so every change to+ // one event must land on one partition to stay ordered.+ assert_eq!(event.key, applied_id.to_string());+ let metadata = &event.payload["calendar_event.created"];+ // The applied entity id, not the draft's — consumers read what persisted.+ assert_eq!(metadata["event_id"], serde_json::json!(applied_id));+ assert_eq!(+ metadata["owner_id"],+ serde_json::json!("macro|[email protected]")+ );+ assert_eq!(event.payload["schema_version"], serde_json::json!(1));+}++#[tokio::test]+async fn an_idempotent_create_that_hits_the_conflict_path_publishes_updated() {+ // The variant reports what happened to the row, not what the caller asked+ // for: replaying a create lands on the upsert's DO UPDATE.+ let applied_id = Uuid::now_v7();+ let repo = FakeRepo {+ creation_target: Some(creation_target(false)),+ persisted_event_id: Some(applied_id),+ write_change: CalendarEventChange::Updated,+ ..FakeRepo::default()+ };+ let broker = RecordingEventBroker::default();+ service_with_broker(+ repo,+ FakeProvider::new(FakeProviderBehavior::Echo),+ FakeTokens::ok(),+ broker.clone(),+ )+ .create_event("macro|user", None, None, draft())+ .await+ .unwrap();++ let published = broker.published();+ assert_eq!(published.len(), 1);+ assert!(+ published[0].payload.get("calendar_event.updated").is_some(),+ "expected an updated event, got {:?}",+ published[0].payload+ );+}++#[tokio::test]+async fn a_write_that_changed_nothing_publishes_nothing() {+ // The upsert skips identical projections and rejects stale sequences.+ // Publishing those would flood the topic on a full provider snapshot.+ let repo = FakeRepo {+ creation_target: Some(creation_target(false)),+ persisted_event_id: Some(Uuid::now_v7()),+ write_change: CalendarEventChange::Unchanged,+ ..FakeRepo::default()+ };+ let broker = RecordingEventBroker::default();+ service_with_broker(+ repo,+ FakeProvider::new(FakeProviderBehavior::Echo),+ FakeTokens::ok(),+ broker.clone(),+ )+ .create_event("macro|user", None, None, draft())+ .await+ .unwrap();++ assert!(+ broker.published().is_empty(),+ "an unchanged row must stay off the topic"+ );+}++#[tokio::test]+async fn deleting_a_series_publishes_deleted_per_removed_event() {+ // Retiring a recurring master's source also retires its expanded+ // instances, so every affected event announces its own fate — not just+ // the one the caller named.+ let master = Uuid::now_v7();+ let instance = Uuid::now_v7();+ let survivor = Uuid::now_v7();+ let repo = FakeRepo {+ mutation_target: Some(mutation_target(false)),+ retired_events: vec![+ RetiredCalendarEvent {+ event_id: master,+ owner_id: "macro|owner".to_string(),+ deleted: true,+ },+ RetiredCalendarEvent {+ event_id: instance,+ owner_id: "macro|owner".to_string(),+ deleted: true,+ },+ // Still backed by another source, so the row was rewritten.+ RetiredCalendarEvent {+ event_id: survivor,+ owner_id: "macro|owner".to_string(),+ deleted: false,+ },+ ],+ ..FakeRepo::default()+ };+ let broker = RecordingEventBroker::default();+ service_with_broker(+ repo,+ FakeProvider::new(FakeProviderBehavior::Echo),+ FakeTokens::ok(),+ broker.clone(),+ )+ .delete_event("macro|user", master, CalendarDeletionScope::All)+ .await+ .unwrap();++ let published = broker.published();+ assert_eq!(published.len(), 3, "one event per affected entity");+ let variants: Vec<(String, &'static str)> = published+ .iter()+ .map(|event| {+ let variant = if event.payload.get("calendar_event.deleted").is_some() {+ "deleted"+ } else if event.payload.get("calendar_event.updated").is_some() {+ "updated"+ } else {+ "other"+ };+ (event.key.clone(), variant)+ })+ .collect();+ assert_eq!(+ variants,+ vec![+ (master.to_string(), "deleted"),+ (instance.to_string(), "deleted"),+ // A surviving row is an update, not a deletion.+ (survivor.to_string(), "updated"),+ ]+ );+}++#[tokio::test]+async fn a_failed_publish_does_not_fail_the_mutation() {+ // Google and the local projection are already updated by this point, so a+ // broker failure must cost index freshness rather than the write. The+ // search backfill re-enumerates from Postgres to recover.+ let applied_id = Uuid::now_v7();+ let repo = FakeRepo {+ creation_target: Some(creation_target(false)),+ persisted_event_id: Some(applied_id),+ ..FakeRepo::default()+ };+ let created = service_with_broker(+ repo,+ FakeProvider::new(FakeProviderBehavior::Echo),+ FakeTokens::ok(),+ RecordingEventBroker::failing(),+ )+ .create_event("macro|user", None, None, draft())+ .await+ .expect("a publish failure must not fail the mutation");++ assert_eq!(created.id, applied_id);+}++#[tokio::test]+async fn a_rejected_mutation_publishes_nothing() {+ let broker = RecordingEventBroker::default();+ let svc = service_with_broker(+ FakeRepo::default(),+ FakeProvider::new(FakeProviderBehavior::Echo),+ FakeTokens::ok(),+ broker.clone(),+ );+ assert!(+ svc.create_event("macro|user", None, None, draft())+ .await+ .is_err()+ );+ assert!(+ broker.published().is_empty(),+ "nothing persisted, so nothing to announce"+ );+}+#[tokio::test]async fn create_rejects_invalid_input_before_reaching_the_provider() {let provider = FakeProvider::new(FakeProviderBehavior::Echo);diff --git a/crates/calendar_events/src/domain/ports.rs b/crates/calendar_events/src/domain/ports.rsindex 3db4bd1f4c..42d47351e4 100644--- a/crates/calendar_events/src/domain/ports.rs+++ b/crates/calendar_events/src/domain/ports.rs@@ -320,6 +320,50 @@ pub trait CalendarOccurrenceService: Send + Sync + 'static {) -> impl Future<Output = Result<Vec<CalendarMentionPreview>, Report>> + Send;}+/// What a write did to one event's canonical `calendar_events` row.+///+/// Reports the row's fate, not the caller's intent: an idempotent re-create+/// that lands on the upsert's conflict path is [`Updated`](Self::Updated), and+/// a write the sequence guard rejected is [`Unchanged`](Self::Unchanged).+#[derive(Clone, Copy, Debug, PartialEq, Eq)]+pub enum CalendarEventChange {+ /// The row was inserted.+ Created,+ /// The row was rewritten in place.+ Updated,+ /// Nothing was written: the incoming projection matched the stored one, or+ /// the sequence guard rejected it as stale.+ Unchanged,+}++/// One event's identity and what a write did to it.+#[derive(Clone, Debug, PartialEq, Eq)]+pub struct CalendarEventWriteOutcome {+ /// The canonical entity id the write applied to.+ pub event_id: Uuid,+ /// Owner of this per-user event projection.+ pub owner_id: String,+ /// What happened to the row.+ pub change: CalendarEventChange,+}++/// One event's fate after its sources were retired.+///+/// Retiring a source does not necessarily remove the event: the row survives,+/// rewritten from its next-best remaining source. Retiring a recurring+/// master's source also retires its expanded instances, so one call reports+/// several events.+#[derive(Clone, Debug, PartialEq, Eq)]+pub struct RetiredCalendarEvent {+ /// The event whose sources were retired.+ pub event_id: Uuid,+ /// Owner of this per-user event projection.+ pub owner_id: String,+ /// Whether the row is now gone. `false` means it was rewritten from a+ /// remaining source.+ pub deleted: bool,+}+/// Persistence operations used by calendar business logic.pub trait CalendarRepository: Send + Sync + 'static {/// Apply the actual scopes returned by Google and atomically schedule any@@ -346,11 +390,12 @@ pub trait CalendarRepository: Send + Sync + 'static {email_link_id: Uuid,) -> impl Future<Output = Result<Option<DisconnectedGoogleCalendar>, Report>> + Send;- /// Upsert an event through an explicit, source-matched ingestion authority.+ /// Upsert an event through an explicit, source-matched ingestion+ /// authority, reporting what the write did to the canonical row.fn upsert_event(&self,write: CalendarEventWrite,- ) -> impl Future<Output = Result<Uuid, Report>> + Send;+ ) -> impl Future<Output = Result<CalendarEventWriteOutcome, Report>> + Send;/// Return occurrences visible to a requester across owned and delegated inboxes.fn list_occurrences(@@ -397,7 +442,7 @@ pub trait CalendarRepository: Send + Sync + 'static {account_id: Uuid,sync: GoogleCalendarSyncSnapshot,events_upserted: usize,- ) -> impl Future<Output = Result<(), Report>> + Send;+ ) -> impl Future<Output = Result<Vec<RetiredCalendarEvent>, Report>> + Send;/// Record a freshly opened push channel for one calendar under the/// backfill's fencing token.@@ -432,7 +477,7 @@ pub trait CalendarRepository: Send + Sync + 'static {lease_token: Uuid,account_id: Uuid,calendar_ids: Vec<Uuid>,- ) -> impl Future<Output = Result<(), Report>> + Send;+ ) -> impl Future<Output = Result<Vec<RetiredCalendarEvent>, Report>> + Send;/// Resolve an event visible to the requester to its best Google source/// and the connected inbox that can mutate it. `None` covers both an@@ -463,12 +508,14 @@ pub trait CalendarRepository: Send + Sync + 'static {/// Retire a Google source the provider confirmed deleted (a recurring/// master also retires its expanded instances), restoring the best/// surviving source or removing the entity, mirroring feed tombstones.+ /// Retire a provider source and reconcile every event it backed,+ /// reporting which of them survived on another source and which are gone.fn remove_google_source(&self,account_id: Uuid,calendar_id: Uuid,provider_event_id: &str,- ) -> impl Future<Output = Result<(), Report>> + Send;+ ) -> impl Future<Output = Result<Vec<RetiredCalendarEvent>, Report>> + Send;}/// Inbound service port for user-initiated calendar event mutations.diff --git a/crates/calendar_events/src/domain/service.rs b/crates/calendar_events/src/domain/service.rsindex ea0787d06d..4f5416d988 100644--- a/crates/calendar_events/src/domain/service.rs+++ b/crates/calendar_events/src/domain/service.rs@@ -5,6 +5,9 @@ use futures::future::{Either, select};use rootcause::Report;use uuid::Uuid;+use crate::domain::events::{CalendarEventMetadata, CalendarMacroEvent, CalendarTopicEvent};+use macro_event_broker::MacroEventBroker;+use super::{models::{AppliedGoogleGrant, CalendarBackfillClaim, CalendarBackfillFailureDisposition,@@ -13,9 +16,10 @@ use super::{GoogleCalendarSyncSnapshot, GoogleScopeSet, OccurrenceRange,},ports::{- CalendarBackfillRepository, CalendarEventWrite, CalendarOccurrenceService,- CalendarRepository, GoogleCalendarProvider, GoogleCalendarSyncRepository,- GoogleEventSyncContext, GoogleProviderError, GoogleProviderErrorKind,+ CalendarBackfillRepository, CalendarEventChange, CalendarEventWrite,+ CalendarEventWriteOutcome, CalendarOccurrenceService, CalendarRepository,+ GoogleCalendarProvider, GoogleCalendarSyncRepository, GoogleEventSyncContext,+ GoogleProviderError, GoogleProviderErrorKind, RetiredCalendarEvent,},};@@ -187,9 +191,10 @@ where}/// Orchestrates a Google account backfill while keeping HTTP and SQL behind ports.-pub struct GoogleCalendarBackfillService<R, G> {+pub struct GoogleCalendarBackfillService<R, G, B> {repository: R,provider: G,+ macro_event_broker: B,watch: Option<super::models::GoogleWatchConfig>,}@@ -285,18 +290,20 @@ pub enum GoogleCalendarBackfillRunError {}/// Application service that owns Google backfill claim, lease, and terminal policy.-pub struct GoogleCalendarBackfillCoordinator<R, G, L> {+pub struct GoogleCalendarBackfillCoordinator<R, G, L, B> {repository: R,provider: G,lifecycle: L,+ macro_event_broker: B,watch: Option<super::models::GoogleWatchConfig>,}-impl<R, G, L> GoogleCalendarBackfillCoordinator<R, G, L>+impl<R, G, L, B> GoogleCalendarBackfillCoordinator<R, G, L, B>whereR: CalendarRepository + Clone,G: GoogleCalendarProvider + Clone,L: CalendarBackfillRepository,+ B: MacroEventBroker + Clone,{/// Construct a coordinator from domain ports; supplying a watch config/// makes every backfill maintain push notification channels.@@ -304,12 +311,14 @@ whererepository: R,provider: G,lifecycle: L,+ macro_event_broker: B,watch: Option<super::models::GoogleWatchConfig>,) -> Self {Self {repository,provider,lifecycle,+ macro_event_broker,watch,}}@@ -367,6 +376,7 @@ wherelet backfill = GoogleCalendarBackfillService::new(self.repository.clone(),self.provider.clone(),+ self.macro_event_broker.clone(),self.watch.clone(),);let work = backfill.backfill(@@ -452,24 +462,78 @@ where}}-impl<R, G> GoogleCalendarBackfillService<R, G>+impl<R, G, B> GoogleCalendarBackfillService<R, G, B>whereR: CalendarRepository,G: GoogleCalendarProvider,+ B: MacroEventBroker,{/// Construct the provider backfill service.pub fn new(repository: R,provider: G,+ macro_event_broker: B,watch: Option<super::models::GoogleWatchConfig>,) -> Self {Self {repository,provider,+ macro_event_broker,watch,}}+ /// Publish one calendar topic event. Sync progress is already committed by+ /// the time this runs, so a publish failure is logged and dropped rather+ /// than failing the run — the search backfill re-enumerates present rows.+ fn publish_calendar_event(&self, event: CalendarTopicEvent) {+ let _ = self+ .macro_event_broker+ .send_event(&CalendarMacroEvent::for_change(event))+ .inspect_err(|error| {+ tracing::error!(error=?error, "failed to publish calendar event");+ });+ }++ /// Announce what a write did to the canonical row. A write that changed+ /// nothing publishes nothing, so a full snapshot re-observing thousands of+ /// unchanged events stays off the topic.+ fn publish_write_outcome(&self, outcome: &CalendarEventWriteOutcome) {+ let metadata = CalendarEventMetadata {+ event_id: outcome.event_id,+ owner_id: outcome.owner_id.clone(),+ };+ match outcome.change {+ CalendarEventChange::Created => {+ self.publish_calendar_event(CalendarTopicEvent::Created(metadata));+ }+ CalendarEventChange::Updated => {+ self.publish_calendar_event(CalendarTopicEvent::Updated(metadata));+ }+ CalendarEventChange::Unchanged => {}+ }+ }++ /// Announce every event a source retirement touched. Retiring a source+ /// does not necessarily remove the event — the row survives, rewritten+ /// from its next-best remaining source — so each event reports its own+ /// fate. Without this a provider-side deletion would leave a permanently+ /// stale search document: the row is gone, so the backfill can never+ /// re-enumerate it.+ fn publish_retirements(&self, retired: Vec<RetiredCalendarEvent>) {+ for event in retired {+ let metadata = CalendarEventMetadata {+ event_id: event.event_id,+ owner_id: event.owner_id,+ };+ self.publish_calendar_event(if event.deleted {+ CalendarTopicEvent::Deleted(metadata)+ } else {+ CalendarTopicEvent::Updated(metadata)+ });+ }+ }+/// Fetch and reconcile calendars and events for a connected inbox.////// Progress accumulates into `report` as each calendar commits, so a@@ -552,13 +616,15 @@ where}let super::models::CalendarEventSource::Google(source) = &upsert.source;debug_assert_eq!(source.calendar_id, calendar_id);- self.repository+ let outcome = self+ .repository.upsert_event(CalendarEventWrite::GoogleBackfill {key,lease_token,upsert,}).await?;+ self.publish_write_outcome(&outcome);calendar_count += 1;}// The upserts above committed individually, so they count even@@ -568,7 +634,8 @@ where// Committing per calendar keeps earlier calendars' sync tokens// durable when a later calendar's poll fails, so the retry only// re-pulls what never committed.- self.repository+ let retired = self+ .repository.commit_google_calendar_sync(key,lease_token,@@ -583,6 +650,10 @@ wherecalendar_count,).await?;+ // A provider-side deletion reaches search only here: the row is+ // gone once the commit lands, so nothing downstream can rediscover+ // it by re-reading Postgres.+ self.publish_retirements(retired);// Tombstones only apply inside the snapshot commit, so they// count once it succeeds.report.cancellations_observed += cancellation_count;@@ -637,9 +708,13 @@ where}}- self.repository+ // A calendar dropped from the provider's list retires its sources, so+ // the events it backed announce their own fate here too.+ let retired = self+ .repository.reconcile_google_calendar_list(key, lease_token, account_id, calendar_ids).await?;+ self.publish_retirements(retired);Ok(())}diff --git a/crates/calendar_events/src/domain/service/test.rs b/crates/calendar_events/src/domain/service/test.rsindex 7ed71580b1..eeedc2407d 100644--- a/crates/calendar_events/src/domain/service/test.rs+++ b/crates/calendar_events/src/domain/service/test.rs@@ -1,4 +1,5 @@use super::*;+use crate::domain::ports::{CalendarEventChange, CalendarEventWriteOutcome, RetiredCalendarEvent};use crate::domain::{models::{AttendeeResponseStatus, CalendarAttendee, CalendarBackfillClaim,@@ -16,12 +17,16 @@ use crate::domain::{},};use chrono::{TimeZone, Utc};+use macro_event_broker::NoopMacroEventBroker;use std::sync::{Arc, Mutex};#[derive(Clone, Default)]struct FakeRepo {upserts: Arc<Mutex<Vec<CalendarEventUpsert>>>,stored_synced_at: Option<chrono::DateTime<Utc>>,+ /// Retirements the snapshot commit reports back, standing in for sources+ /// the change feed cancelled or a full snapshot no longer observed.+ sync_retirements: Vec<RetiredCalendarEvent>,}impl CalendarRepository for FakeRepo {@@ -42,15 +47,23 @@ impl CalendarRepository for FakeRepo {unreachable!()}- async fn upsert_event(&self, write: CalendarEventWrite) -> Result<Uuid, Report> {+ async fn upsert_event(+ &self,+ write: CalendarEventWrite,+ ) -> Result<CalendarEventWriteOutcome, Report> {let upsert = match write {CalendarEventWrite::GoogleBackfill { upsert, .. }| CalendarEventWrite::UserMutation(upsert)| CalendarEventWrite::Fixture(upsert) => upsert,};let id = upsert.event.id;+ let owner_id = upsert.event.owner_id.clone();self.upserts.lock().unwrap().push(upsert);- Ok(id)+ Ok(CalendarEventWriteOutcome {+ event_id: id,+ owner_id,+ change: CalendarEventChange::Created,+ })}async fn list_occurrences(@@ -108,7 +121,7 @@ impl CalendarRepository for FakeRepo {_account_id: Uuid,_calendar_id: Uuid,_provider_event_id: &str,- ) -> Result<(), Report> {+ ) -> Result<Vec<RetiredCalendarEvent>, Report> {unreachable!("mutation cleanup is not exercised by sync tests")}@@ -135,8 +148,8 @@ impl CalendarRepository for FakeRepo {_account_id: Uuid,_sync: GoogleCalendarSyncSnapshot,_events_upserted: usize,- ) -> Result<(), Report> {- Ok(())+ ) -> Result<Vec<RetiredCalendarEvent>, Report> {+ Ok(self.sync_retirements.clone())}async fn reconcile_google_calendar_list(@@ -145,8 +158,8 @@ impl CalendarRepository for FakeRepo {_lease_token: Uuid,_account_id: Uuid,_calendar_ids: Vec<Uuid>,- ) -> Result<(), Report> {- Ok(())+ ) -> Result<Vec<RetiredCalendarEvent>, Report> {+ Ok(Vec::new())}async fn record_watch_channel(@@ -527,6 +540,7 @@ async fn partial_progress_reports_changes_when_a_later_calendar_fails() {FakeRepo::default(),PartialFailureGoogleProvider,lifecycle.clone(),+ NoopMacroEventBroker,None,);@@ -563,6 +577,7 @@ async fn google_coordinator_owns_claim_and_completion_lifecycle() {FakeRepo::default(),FakeGoogleProvider,lifecycle.clone(),+ NoopMacroEventBroker,None,);@@ -585,6 +600,150 @@ async fn google_coordinator_owns_claim_and_completion_lifecycle() {assert_eq!(lifecycle.completions.lock().unwrap().len(), 1);}+/// Records what the backfill published, so a retirement can be distinguished+/// from an upsert.+#[derive(Clone, Default)]+struct RecordingEventBroker {+ published: Arc<Mutex<Vec<(String, serde_json::Value)>>>,+}++impl macro_event_broker::MacroEventBroker for RecordingEventBroker {+ fn send_event<E: macro_event_broker::MacroEvent + ?Sized>(+ &self,+ event: &E,+ ) -> Result<+ tokio::task::JoinHandle<Result<(), macro_event_broker::EventBrokerError>>,+ macro_event_broker::EventBrokerError,+ > {+ self.published.lock().unwrap().push((+ event.key().to_string(),+ serde_json::to_value(event.event())?,+ ));+ Ok(tokio::spawn(async { Ok(()) }))+ }+}++/// One ordinary calendar whose poll reports a cancelled provider event, so the+/// snapshot commit runs and its retirements reach the publisher.+#[derive(Clone)]+struct CancellingCalendarProvider;++impl GoogleCalendarProvider for CancellingCalendarProvider {+ async fn list_calendars(+ &self,+ _access_token: &str,+ _email_link_id: Uuid,+ ) -> Result<Vec<ProviderCalendar>, GoogleProviderError> {+ Ok(vec![ProviderCalendar {+ provider_calendar_id: "primary".to_string(),+ name: "Calendar".to_string(),+ description: None,+ time_zone: Some("UTC".to_string()),+ color: None,+ access_role: Some("owner".to_string()),+ is_primary: true,+ is_selected: true,+ default_reminders: Vec::new(),+ }])+ }++ async fn sync_events(+ &self,+ _access_token: &str,+ context: GoogleEventSyncContext,+ ) -> Result<GoogleEventSyncBatch, GoogleProviderError> {+ Ok(GoogleEventSyncBatch {+ upserts: Vec::new(),+ observed_provider_event_ids: Some(Vec::new()),+ next_sync_token: "next".to_string(),+ materialized_range: Some(context.target.range),+ cancelled_provider_event_ids: vec!["gone-at-google".to_string()],+ })+ }++ async fn watch_calendar(+ &self,+ _access_token: &str,+ _email_link_id: Uuid,+ _provider_calendar_id: &str,+ _channel_id: Uuid,+ _config: &GoogleWatchConfig,+ ) -> Result<GoogleWatchChannel, GoogleProviderError> {+ unreachable!("watch is disabled in these tests")+ }+}++/// A provider-side deletion reaches search only through the topic: the row is+/// gone once the snapshot commit lands, so the search backfill — which+/// enumerates existing rows — can never rediscover it.+#[tokio::test]+async fn a_cancelled_provider_event_publishes_a_deletion() {+ let removed = Uuid::now_v7();+ let survivor = Uuid::now_v7();+ let repo = FakeRepo {+ sync_retirements: vec![+ RetiredCalendarEvent {+ event_id: removed,+ owner_id: "macro|owner".to_string(),+ deleted: true,+ },+ // Another source still backs this one, so its row was rewritten.+ RetiredCalendarEvent {+ event_id: survivor,+ owner_id: "macro|owner".to_string(),+ deleted: false,+ },+ ],+ ..FakeRepo::default()+ };+ let broker = RecordingEventBroker::default();+ let coordinator = GoogleCalendarBackfillCoordinator::new(+ repo,+ CancellingCalendarProvider,+ FakeLifecycle::claimed(),+ broker.clone(),+ None,+ );++ let mut report = GoogleBackfillRunReport::default();+ coordinator+ .run(+ CalendarBackfillJobKey {+ job_id: Uuid::now_v7(),+ email_link_id: Uuid::now_v7(),+ },+ "macro|[email protected]",+ "secret",+ OccurrenceRange::historical_sync(Utc::now()),+ &mut report,+ )+ .await+ .unwrap();++ let published = broker.published.lock().unwrap().clone();+ let deleted: Vec<&String> = published+ .iter()+ .filter(|(_, payload)| payload.get("calendar_event.deleted").is_some())+ .map(|(key, _)| key)+ .collect();+ let updated: Vec<&String> = published+ .iter()+ .filter(|(_, payload)| payload.get("calendar_event.updated").is_some())+ .map(|(key, _)| key)+ .collect();++ assert_eq!(+ deleted,+ vec![&removed.to_string()],+ "the retired event must be announced as deleted, got {published:?}"+ );+ assert_eq!(+ updated,+ vec![&survivor.to_string()],+ "an event that survived on another source is an update, not a deletion"+ );+}+#[derive(Clone)]struct SystemCalendarProvider;@@ -638,6 +797,7 @@ async fn freshly_synced_system_calendars_are_skipped() {repo,SystemCalendarProvider,lifecycle.clone(),+ NoopMacroEventBroker,None,);@@ -699,6 +859,7 @@ async fn google_coordinator_surfaces_lease_loss_while_work_is_running() {FakeRepo::default(),HangingGoogleProvider,lifecycle.clone(),+ NoopMacroEventBroker,None,);@@ -728,6 +889,7 @@ async fn google_coordinator_keeps_calendar_permission_health_separate_from_gmailFakeRepo::default(),ReauthGoogleProvider,lifecycle.clone(),+ NoopMacroEventBroker,None,);diff --git a/crates/calendar_events/src/outbound/pg.rs b/crates/calendar_events/src/outbound/pg.rsindex 794cb2c7fd..e79d87ab92 100644--- a/crates/calendar_events/src/outbound/pg.rs+++ b/crates/calendar_events/src/outbound/pg.rs@@ -24,8 +24,9 @@ use crate::domain::{OccurrenceRange, ProviderCalendar, StoredGoogleCalendar, VisibleCalendar,},ports::{- CalendarBackfillRepository, CalendarEventWrite, CalendarReminderDispatchRepo,- CalendarRepository, GoogleCalendarSyncRepository,+ CalendarBackfillRepository, CalendarEventChange, CalendarEventWrite,+ CalendarEventWriteOutcome, CalendarReminderDispatchRepo, CalendarRepository,+ GoogleCalendarSyncRepository, RetiredCalendarEvent,},};@@ -207,7 +208,9 @@ impl PgCalendarRepository {#[cfg(test)]async fn upsert_event_fixture(&self, upsert: CalendarEventUpsert) -> Result<Uuid, Report> {- self.upsert_event(CalendarEventWrite::Fixture(upsert)).await+ self.upsert_event(CalendarEventWrite::Fixture(upsert))+ .await+ .map(|outcome| outcome.event_id)}#[cfg(test)]@@ -627,7 +630,10 @@ impl CalendarRepository for PgCalendarRepository {}#[tracing::instrument(skip(self, write), err)]- async fn upsert_event(&self, write: CalendarEventWrite) -> Result<Uuid, Report> {+ async fn upsert_event(+ &self,+ write: CalendarEventWrite,+ ) -> Result<CalendarEventWriteOutcome, Report> {let mut tx = self.pool.begin().await.map_err(report)?;let upsert = match write {CalendarEventWrite::GoogleBackfill {@@ -685,7 +691,11 @@ impl CalendarRepository for PgCalendarRepository {serde_json::to_value(StoredSourceProjection::from(&upsert)).map_err(report)?;if canonical_projection(&row.normalized_payload) == canonical_projection(&incoming) {tx.commit().await.map_err(report)?;- return Ok(row.event_id);+ return Ok(CalendarEventWriteOutcome {+ event_id: row.event_id,+ owner_id: upsert.event.owner_id.clone(),+ change: CalendarEventChange::Unchanged,+ });}}@@ -695,7 +705,7 @@ impl CalendarRepository for PgCalendarRepository {// Google is the authoritative source when the same RFC UID was first// discovered in email. Email can still create/update entities that do// not yet have a Google source.- let applied_id = sqlx::query_scalar!(+ let applied = sqlx::query!(r#"INSERT INTO calendar_events (id, owner_id, source_link_id, ical_uid, title, description, location,@@ -748,7 +758,9 @@ impl CalendarRepository for PgCalendarRepository {AND EXCLUDED.canonical_source_updated_at>= calendar_events.canonical_source_updated_at)- RETURNING id+ -- `xmax = 0` distinguishes the INSERT from the DO UPDATE: a+ -- freshly inserted row carries no updating transaction id.+ RETURNING id, (xmax = 0) AS "inserted!""#,proposed_id,&upsert.event.owner_id,@@ -785,24 +797,37 @@ impl CalendarRepository for PgCalendarRepository {.await.map_err(report)?;- let event_id = match applied_id {- Some(id) => id,- None => sqlx::query_scalar!(- "SELECT id FROM calendar_events WHERE owner_id = $1 AND source_link_id = $2 AND ical_uid = $3",- &upsert.event.owner_id,- source_link_id,- &upsert.event.ical_uid,- )- .fetch_one(&mut *tx)- .await- .map_err(report)?,+ // No returned row means the sequence guard rejected the write as+ // stale: the source is still recorded below, but the canonical row and+ // its projections are untouched.+ let (event_id, change) = match &applied {+ Some(row) => (+ row.id,+ if row.inserted {+ CalendarEventChange::Created+ } else {+ CalendarEventChange::Updated+ },+ ),+ None => (+ sqlx::query_scalar!(+ "SELECT id FROM calendar_events WHERE owner_id = $1 AND source_link_id = $2 AND ical_uid = $3",+ &upsert.event.owner_id,+ source_link_id,+ &upsert.event.ical_uid,+ )+ .fetch_one(&mut *tx)+ .await+ .map_err(report)?,+ CalendarEventChange::Unchanged,+ ),};persist_source(&mut tx, event_id, &upsert).await?;// Only the source selected as canonical replaces projections and// attendees. Lower-sequence/stale sources are still recorded above.- if applied_id.is_some() {+ if applied.is_some() {replace_attendees(&mut tx, event_id, &upsert.event.attendees).await?;replace_overrides(&mut tx, event_id, &upsert.overrides).await?;replace_occurrences(@@ -825,7 +850,11 @@ impl CalendarRepository for PgCalendarRepository {}tx.commit().await.map_err(report)?;- Ok(event_id)+ Ok(CalendarEventWriteOutcome {+ event_id,+ owner_id: upsert.event.owner_id.clone(),+ change,+ })}#[tracing::instrument(skip(self, requester_id, range), err)]@@ -1148,7 +1177,8 @@ impl CalendarRepository for PgCalendarRepository {account_id: Uuid,sync: GoogleCalendarSyncSnapshot,events_upserted: usize,- ) -> Result<(), Report> {+ ) -> Result<Vec<RetiredCalendarEvent>, Report> {+ let mut retired = Vec::new();let mut tx = self.pool.begin().await.map_err(report)?;fence_google_mutation_tx(&mut tx, key, lease_token, Some(account_id)).await?;@@ -1201,7 +1231,9 @@ impl CalendarRepository for PgCalendarRepository {.await.map_err(report)?;for event_id in affected_event_ids {- restore_best_source_or_delete(&mut tx, event_id).await?;+ if let Some(outcome) = restore_best_source_or_delete(&mut tx, event_id).await? {+ retired.push(outcome);+ }}}@@ -1227,7 +1259,9 @@ impl CalendarRepository for PgCalendarRepository {.await.map_err(report)?;for event_id in affected_event_ids {- restore_best_source_or_delete(&mut tx, event_id).await?;+ if let Some(outcome) = restore_best_source_or_delete(&mut tx, event_id).await? {+ retired.push(outcome);+ }}}@@ -1286,7 +1320,8 @@ impl CalendarRepository for PgCalendarRepository {));}- tx.commit().await.map_err(report)+ tx.commit().await.map_err(report)?;+ Ok(retired)}#[tracing::instrument(skip(self, channel), fields(job_id = %key.job_id), err)]@@ -1398,7 +1433,8 @@ impl CalendarRepository for PgCalendarRepository {lease_token: Uuid,account_id: Uuid,calendar_ids: Vec<Uuid>,- ) -> Result<(), Report> {+ ) -> Result<Vec<RetiredCalendarEvent>, Report> {+ let mut retired = Vec::new();let mut tx = self.pool.begin().await.map_err(report)?;fence_google_mutation_tx(&mut tx, key, lease_token, Some(account_id)).await?;@@ -1424,7 +1460,9 @@ impl CalendarRepository for PgCalendarRepository {.await.map_err(report)?;for event_id in affected_event_ids {- restore_best_source_or_delete(&mut tx, event_id).await?;+ if let Some(outcome) = restore_best_source_or_delete(&mut tx, event_id).await? {+ retired.push(outcome);+ }}sqlx::query!(@@ -1462,7 +1500,8 @@ impl CalendarRepository for PgCalendarRepository {.await.map_err(report)?;- tx.commit().await.map_err(report)+ tx.commit().await.map_err(report)?;+ Ok(retired)}#[tracing::instrument(skip(self, requester_id), err)]@@ -1672,7 +1711,7 @@ impl CalendarRepository for PgCalendarRepository {account_id: Uuid,calendar_id: Uuid,provider_event_id: &str,- ) -> Result<(), Report> {+ ) -> Result<Vec<RetiredCalendarEvent>, Report> {let mut tx = self.pool.begin().await.map_err(report)?;let cancelled = [provider_event_id.to_string()];// A deleted recurring master retires its expanded instances via@@ -1700,10 +1739,14 @@ impl CalendarRepository for PgCalendarRepository {.fetch_all(&mut *tx).await.map_err(report)?;+ let mut retired = Vec::new();for event_id in affected_event_ids {- restore_best_source_or_delete(&mut tx, event_id).await?;+ if let Some(outcome) = restore_best_source_or_delete(&mut tx, event_id).await? {+ retired.push(outcome);+ }}- tx.commit().await.map_err(report)+ tx.commit().await.map_err(report)?;+ Ok(retired)}}@@ -2219,6 +2262,15 @@ async fn clear_calendar_opt_out_tx(Ok(())}+/// Retirements here are deliberately **not** returned for publication.+///+/// Both callers — a grant that lost its calendar scopes, and an explicit+/// disconnect — purge every event on the inbox at once, so per-event `Deleted`+/// topic messages would fan out to one message per event in the account.+/// Search documents for those events are consequently left stale; they are+/// invisible rather than leaked, because enrichment re-reads visibility from+/// Postgres and drops a hit whose row is gone. Removing them wants a+/// purge-by-owner operation, not this path.async fn disable_google_calendar_capability_tx(tx: &mut Transaction<'_, Postgres>,email_link_id: Uuid,@@ -2457,13 +2509,17 @@ async fn persist_source(Ok(())}+/// Rewrite an event from its next-best remaining source, or delete it when no+/// source is left, reporting which happened.+///+/// `None` means the row was already gone before this call.async fn restore_best_source_or_delete(tx: &mut Transaction<'_, Postgres>,event_id: Uuid,-) -> Result<(), Report> {+) -> Result<Option<RetiredCalendarEvent>, Report> {let identity = sqlx::query!(r#"- SELECT source_link_id, ical_uid+ SELECT source_link_id, ical_uid, owner_idFROM calendar_eventsWHERE id = $1"#,@@ -2473,7 +2529,7 @@ async fn restore_best_source_or_delete(.await.map_err(report)?;let Some(identity) = identity else {- return Ok(());+ return Ok(None);};let reconciliation_lock =event_reconciliation_lock(identity.source_link_id, &identity.ical_uid);@@ -2507,7 +2563,11 @@ async fn restore_best_source_or_delete(.execute(&mut **tx).await.map_err(report)?;- return Ok(());+ return Ok(Some(RetiredCalendarEvent {+ event_id,+ owner_id: identity.owner_id,+ deleted: true,+ }));};let projection: StoredSourceProjection =@@ -2591,7 +2651,12 @@ async fn restore_best_source_or_delete(&projection.event.reminders,calendar.as_ref(),)- .await+ .await?;+ Ok(Some(RetiredCalendarEvent {+ event_id,+ owner_id: identity.owner_id,+ deleted: false,+ }))}async fn replace_attendees(diff --git a/crates/calendar_events/src/outbound/pg/test.rs b/crates/calendar_events/src/outbound/pg/test.rsindex f220b2bd22..8df8d34de5 100644--- a/crates/calendar_events/src/outbound/pg/test.rs+++ b/crates/calendar_events/src/outbound/pg/test.rs@@ -404,7 +404,8 @@ async fn removing_calendar_scope_disables_sources_and_fences_the_running_job(pooupsert: google,}).await- .unwrap();+ .unwrap()+ .event_id;let downgraded = repo.apply_google_grant(@@ -883,7 +884,8 @@ async fn expired_google_worker_cannot_resurrect_reconciled_provider_data(pool: Pupsert: google.clone(),}).await- .unwrap();+ .unwrap()+ .event_id;sqlx::query!(r#"@@ -2240,7 +2242,8 @@ async fn user_mutation_write_persists_a_google_echo_without_a_lease(pool: PgPoollet event_id = repo.upsert_event(CalendarEventWrite::UserMutation(upsert)).await- .unwrap();+ .unwrap()+ .event_id;let row = sqlx::query!(r#"@@ -2280,7 +2283,8 @@ async fn mutation_target_resolves_only_for_visible_requesters(pool: PgPool) {1,))).await- .unwrap();+ .unwrap()+ .event_id;sqlx::query!(r#"INSERT INTO macro_user_links (primary_macro_id, child_macro_id, link_id)@@ -2455,7 +2459,8 @@ async fn removing_a_google_source_restores_the_surviving_calendar_copy(pool: PgPlet event_id = repo.upsert_event(CalendarEventWrite::UserMutation(primary_copy)).await- .unwrap();+ .unwrap()+ .event_id;let mut team_copy = timed_upsert(owner_id,link_id,@@ -2475,7 +2480,8 @@ async fn removing_a_google_source_restores_the_surviving_calendar_copy(pool: PgP});repo.upsert_event(CalendarEventWrite::UserMutation(team_copy)).await- .unwrap();+ .unwrap()+ .event_id;repo.remove_google_source(account_id, primary_id, "[email protected]").await@@ -2971,7 +2977,8 @@ async fn conference_provider_round_trips_through_persistence(pool: PgPool) {upsert.event.conference_provider = Some(ConferenceProvider::GoogleMeet);repo.upsert_event(CalendarEventWrite::UserMutation(upsert)).await- .unwrap();+ .unwrap()+ .event_id;let starts_at = Utc.with_ymd_and_hms(2026, 7, 24, 0, 0, 0).unwrap();let ends_at = starts_at + Duration::days(2);@@ -3089,7 +3096,8 @@ async fn connected_calendar(),}).await- .unwrap();+ .unwrap()+ .event_id;sqlx::query!(r#"INSERT INTO calendar_event_reminder_deliveries (diff --git a/crates/macro_db_client/src/calendar_event.rs b/crates/macro_db_client/src/calendar_event.rsnew file mode 100644index 0000000000..5471d3d475--- /dev/null+++ b/crates/macro_db_client/src/calendar_event.rs@@ -0,0 +1,5 @@+//! Read queries for calendar events consumed by search enrichment.++pub mod get_event_for_index;+pub mod get_events_for_backfill;+pub mod get_events_for_search;diff --git a/crates/macro_db_client/src/calendar_event/get_event_for_index.rs b/crates/macro_db_client/src/calendar_event/get_event_for_index.rsnew file mode 100644index 0000000000..8b1c9305fb--- /dev/null+++ b/crates/macro_db_client/src/calendar_event/get_event_for_index.rs@@ -0,0 +1,87 @@+//! The projection of a calendar event that the search index stores.++use chrono::{DateTime, NaiveDate, Utc};+use sqlx::{Pool, Postgres};+use uuid::Uuid;++/// A calendar event series master, flattened for indexing.+#[derive(Debug, Clone)]+pub struct CalendarEventForIndex {+ pub id: Uuid,+ pub owner_id: String,+ /// The inbox link the canonical source belongs to; grants delegates.+ pub source_link_id: Uuid,+ pub ical_uid: String,+ pub title: String,+ pub status: String,+ pub starts_at: Option<DateTime<Utc>>,+ pub ends_at: Option<DateTime<Utc>>,+ pub start_date: Option<NaiveDate>,+ pub end_date: Option<NaiveDate>,+ /// Whether the series carries any recurrence rule.+ pub is_recurring: bool,+ pub organizer_email: Option<String>,+ /// Attendee addresses, lowercased for the keyword filter.+ pub attendee_emails: Vec<String>,+ pub created_at: DateTime<Utc>,+ pub updated_at: DateTime<Utc>,+}++/// Read one event's indexable projection. `None` means the row is gone and the+/// caller should remove it from the index.+#[tracing::instrument(skip(db), err)]+pub async fn get_calendar_event_for_index(+ db: &Pool<Postgres>,+ event_id: Uuid,+) -> anyhow::Result<Option<CalendarEventForIndex>> {+ let row = sqlx::query!(+ r#"+ SELECT+ event.id AS "id!",+ event.owner_id AS "owner_id!",+ event.source_link_id AS "source_link_id!",+ event.ical_uid AS "ical_uid!",+ event.title AS "title!",+ event.status AS "status!",+ event.starts_at,+ event.ends_at,+ event.start_date,+ event.end_date,+ (cardinality(event.recurrence_lines) > 0) AS "is_recurring!",+ event.organizer_email,+ event.created_at AS "created_at!",+ event.updated_at AS "updated_at!",+ COALESCE(+ (+ SELECT array_agg(lower(attendee.email) ORDER BY lower(attendee.email))+ FROM calendar_event_attendees attendee+ WHERE attendee.event_id = event.id+ ),+ '{}'+ ) AS "attendee_emails!"+ FROM calendar_events event+ WHERE event.id = $1+ "#,+ event_id,+ )+ .fetch_optional(db)+ .await?;++ Ok(row.map(|row| CalendarEventForIndex {+ id: row.id,+ owner_id: row.owner_id,+ source_link_id: row.source_link_id,+ ical_uid: row.ical_uid,+ title: row.title,+ status: row.status,+ starts_at: row.starts_at,+ ends_at: row.ends_at,+ start_date: row.start_date,+ end_date: row.end_date,+ is_recurring: row.is_recurring,+ organizer_email: row.organizer_email,+ attendee_emails: row.attendee_emails,+ created_at: row.created_at,+ updated_at: row.updated_at,+ }))+}diff --git a/crates/macro_db_client/src/calendar_event/get_events_for_backfill.rs b/crates/macro_db_client/src/calendar_event/get_events_for_backfill.rsnew file mode 100644index 0000000000..500a6901b2--- /dev/null+++ b/crates/macro_db_client/src/calendar_event/get_events_for_backfill.rs@@ -0,0 +1,69 @@+//! Keyset enumeration of calendar events for search backfills.++use chrono::{DateTime, Utc};+use sqlx::{Pool, Postgres};+use uuid::Uuid;++/// One page entry: enough to enqueue a reindex and build the next cursor.+#[derive(Debug, Clone)]+pub struct CalendarEventBackfillRow {+ pub event_id: Uuid,+ pub updated_at: DateTime<Utc>,+}++/// Walk `calendar_events` in `(updated_at ASC, id ASC)` order.+///+/// Only series masters exist in this table — recurring instances live in+/// `calendar_event_occurrences` and are not indexed — so this enumerates+/// exactly the set the index holds.+///+/// This recovers a dropped `macro.calendar` change for an event that still+/// exists, and populates a new index. It cannot recover a **deletion**: the+/// row is gone, so nothing here enumerates it and a stale search document for+/// it survives. Deletions reach the index only through the topic's `Deleted`+/// events, which every retirement path publishes. Removing an orphaned+/// document would need the reverse sweep — enumerate the index and drop ids+/// absent from this table — which does not exist yet.+#[tracing::instrument(skip(db), err)]+pub async fn get_calendar_events_for_search_backfill(+ db: &Pool<Postgres>,+ limit: i64,+ cursor: Option<(DateTime<Utc>, Uuid)>,+ updated_after: Option<DateTime<Utc>>,+ updated_before: Option<DateTime<Utc>>,+) -> anyhow::Result<Vec<CalendarEventBackfillRow>> {+ let (cursor_updated_at, cursor_id) = match cursor {+ Some((updated_at, id)) => (Some(updated_at), Some(id)),+ None => (None, None),+ };++ let rows = sqlx::query!(+ r#"+ SELECT+ event.id AS "event_id!",+ event.updated_at AS "updated_at!"+ FROM calendar_events event+ WHERE ($2::timestamptz IS NULL OR $3::uuid IS NULL+ OR (event.updated_at, event.id) > ($2, $3))+ AND ($4::timestamptz IS NULL OR event.updated_at >= $4)+ AND ($5::timestamptz IS NULL OR event.updated_at < $5)+ ORDER BY event.updated_at ASC, event.id ASC+ LIMIT $1+ "#,+ limit,+ cursor_updated_at,+ cursor_id,+ updated_after,+ updated_before,+ )+ .fetch_all(db)+ .await?;++ Ok(rows+ .into_iter()+ .map(|row| CalendarEventBackfillRow {+ event_id: row.event_id,+ updated_at: row.updated_at,+ })+ .collect())+}diff --git a/crates/macro_db_client/src/calendar_event/get_events_for_search.rs b/crates/macro_db_client/src/calendar_event/get_events_for_search.rsnew file mode 100644index 0000000000..4431c06a43--- /dev/null+++ b/crates/macro_db_client/src/calendar_event/get_events_for_search.rs@@ -0,0 +1,169 @@+//! Metadata and contextual-occurrence resolution for calendar event search+//! hits.++use std::collections::HashMap;++use chrono::{DateTime, NaiveDate, Utc};+use sqlx::{Pool, Postgres};+use uuid::Uuid;++/// One calendar event's search metadata, with the instance a result row+/// should point at already resolved.+#[derive(Debug, Clone)]+pub struct CalendarEventSearchInfo {+ /// Event entity id.+ pub id: Uuid,+ /// Owner of this per-user projection.+ pub owner_id: String,+ /// Series title.+ pub title: String,+ /// Canonical status.+ pub status: String,+ /// Master span start, for a timed series.+ pub starts_at: Option<DateTime<Utc>>,+ /// Master span end, for a timed series.+ pub ends_at: Option<DateTime<Utc>>,+ /// Master span start date, for an all-day series.+ pub start_date: Option<NaiveDate>,+ /// Master span end date, for an all-day series.+ pub end_date: Option<NaiveDate>,+ /// Original IANA zone, when the source supplied one.+ pub time_zone: Option<String>,+ /// Whether the series carries any recurrence rule.+ pub is_recurring: bool,+ /// Direct conference join URL.+ pub conference_url: Option<String>,+ /// Whether the canonical source prohibits mutation.+ pub is_read_only: bool,+ pub created_at: DateTime<Utc>,+ pub updated_at: DateTime<Utc>,+ /// Stable key of the resolved instance, when one is materialized.+ pub occurrence_key: Option<String>,+ /// Resolved instance span.+ pub occurrence_starts_at: Option<DateTime<Utc>>,+ pub occurrence_ends_at: Option<DateTime<Utc>>,+ pub occurrence_start_date: Option<NaiveDate>,+ pub occurrence_end_date: Option<NaiveDate>,+}++/// Fetch search metadata for `event_ids` the requester may see, resolving one+/// contextual occurrence per event.+///+/// Visibility mirrors the soup predicate: the requester owns the projection,+/// or the event's source link is delegated to them through `macro_user_links`.+/// An id the requester cannot see is simply absent from the result, which+/// drops the hit during enrichment.+///+/// A recurring series is indexed once, as its master, so the instance a row+/// should display is decided here rather than baked into the index. The+/// lateral prefers the next occurrence at or after `now`, falling back to the+/// most recent past one — the same ordering `mention_previews` uses, so a+/// search row and a mention of the same event agree on where they land.+/// Occurrences are materialized only inside a rolling window, so a series can+/// legitimately resolve to no occurrence at all; callers fall back to the+/// master span.+#[tracing::instrument(skip(db, event_ids), fields(event_count = event_ids.len()), err)]+pub async fn get_calendar_events_for_search(+ db: &Pool<Postgres>,+ requester_id: &str,+ event_ids: &[Uuid],+ now: DateTime<Utc>,+) -> anyhow::Result<HashMap<Uuid, CalendarEventSearchInfo>> {+ if event_ids.is_empty() {+ return Ok(HashMap::new());+ }++ let rows = sqlx::query!(+ r#"+ SELECT+ event.id AS "id!",+ event.owner_id AS "owner_id!",+ event.title AS "title!",+ event.status AS "status!",+ event.starts_at,+ event.ends_at,+ event.start_date,+ event.end_date,+ event.time_zone,+ (cardinality(event.recurrence_lines) > 0) AS "is_recurring!",+ event.conference_url,+ event.is_read_only AS "is_read_only!",+ event.created_at AS "created_at!",+ event.updated_at AS "updated_at!",+ occurrence.occurrence_key AS "occurrence_key?",+ occurrence.starts_at AS "occurrence_starts_at?",+ occurrence.ends_at AS "occurrence_ends_at?",+ occurrence.start_date AS "occurrence_start_date?",+ occurrence.end_date AS "occurrence_end_date?"+ FROM calendar_events event+ LEFT JOIN LATERAL (+ SELECT+ instance.occurrence_key,+ instance.starts_at,+ instance.ends_at,+ instance.start_date,+ instance.end_date+ FROM calendar_event_occurrences instance+ CROSS JOIN LATERAL (+ SELECT COALESCE(+ instance.starts_at,+ instance.start_date::timestamp AT TIME ZONE 'UTC'+ ) AS at+ ) instance_start+ WHERE instance.event_id = event.id+ AND NOT instance.is_cancelled+ ORDER BY+ (instance_start.at >= $3) DESC,+ CASE WHEN instance_start.at >= $3 THEN instance_start.at END ASC,+ instance_start.at DESC,+ instance.occurrence_key+ LIMIT 1+ ) occurrence ON true+ WHERE event.id = ANY($2)+ 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+ )+ )+ "#,+ requester_id,+ event_ids,+ now,+ )+ .fetch_all(db)+ .await?;++ Ok(rows+ .into_iter()+ .map(|row| {+ (+ row.id,+ CalendarEventSearchInfo {+ id: row.id,+ owner_id: row.owner_id,+ title: row.title,+ status: row.status,+ starts_at: row.starts_at,+ ends_at: row.ends_at,+ start_date: row.start_date,+ end_date: row.end_date,+ time_zone: row.time_zone,+ is_recurring: row.is_recurring,+ conference_url: row.conference_url,+ is_read_only: row.is_read_only,+ created_at: row.created_at,+ updated_at: row.updated_at,+ occurrence_key: row.occurrence_key,+ occurrence_starts_at: row.occurrence_starts_at,+ occurrence_ends_at: row.occurrence_ends_at,+ occurrence_start_date: row.occurrence_start_date,+ occurrence_end_date: row.occurrence_end_date,+ },+ )+ })+ .collect())+}diff --git a/crates/macro_db_client/src/entity_name.rs b/crates/macro_db_client/src/entity_name.rsindex 8595966c42..45e67f8b6c 100644--- a/crates/macro_db_client/src/entity_name.rs+++ b/crates/macro_db_client/src/entity_name.rs@@ -88,6 +88,9 @@ pub async fn get_entity_name_and_owner(SearchEntityType::CrmCompanies => {anyhow::bail!("entity type not supported");}+ SearchEntityType::CalendarEvents => {+ anyhow::bail!("entity type not supported");+ }};Ok(result)diff --git a/crates/macro_db_client/src/lib.rs b/crates/macro_db_client/src/lib.rsindex 80350f6530..64aea5bab6 100644--- a/crates/macro_db_client/src/lib.rs+++ b/crates/macro_db_client/src/lib.rs@@ -3,6 +3,7 @@ pub mod account_merge_request;pub mod advisory_lock;pub mod annotations;pub mod blocked_email;+pub mod calendar_event;pub mod call_record;pub mod chat;pub mod chat_history;diff --git a/crates/macro_event_topics/src/lib.rs b/crates/macro_event_topics/src/lib.rsindex 9aada1f8b2..3f1ceca7c5 100644--- a/crates/macro_event_topics/src/lib.rs+++ b/crates/macro_event_topics/src/lib.rs@@ -75,4 +75,6 @@ topics! {MacroNotificationsTopic => "macro.notifications",/// AI chat lifecycle and message events.MacroChatsTopic => "macro.chats",+ /// Calendar event changes, from provider sync and user mutations alike.+ MacroCalendarTopic => "macro.calendar",}diff --git a/crates/models_opensearch/src/lib.rs b/crates/models_opensearch/src/lib.rsindex 932ecb9b58..8bd2c522ce 100644--- a/crates/models_opensearch/src/lib.rs+++ b/crates/models_opensearch/src/lib.rs@@ -23,6 +23,8 @@ pub enum SearchIndex {CallRecords,/// The projects aliasProjects,+ /// The calendar events alias+ CalendarEvents,}/// All searchable entity types — the tag on a unified `SearchHit`,@@ -59,6 +61,8 @@ pub enum SearchEntityType {CallRecords,/// The CRM company entity type (Postgres-only)CrmCompanies,+ /// The calendar event entity type (has OpenSearch index)+ CalendarEvents,}/// `SearchEntityType` variants that have an OpenSearch index.@@ -71,6 +75,8 @@ pub enum SearchEntityType {strum::Display,strum::EnumString,strum::AsRefStr,+ strum::EnumIter,+ strum::IntoStaticStr,serde::Serialize,serde::Deserialize,)]@@ -89,6 +95,8 @@ pub enum OpenSearchEntityType {CallRecords,/// The projects indexProjects,+ /// The calendar events index+ CalendarEvents,}impl OpenSearchEntityType {@@ -103,10 +111,40 @@ impl OpenSearchEntityType {Self::Emails => "emails",Self::CallRecords => "call_records",Self::Projects => "projects",+ Self::CalendarEvents => "calendar_events",}}}+impl OpenSearchEntityType {+ /// Name to tag this entity's clause with in a unified query, and to read+ /// back out of a hit's `matched_queries`.+ ///+ /// This is the strum representation, so the round trip is generated rather+ /// than maintained: a new variant gets a name and parses back without any+ /// list here to keep in step. Deliberately *not* the index name — the+ /// physical index a hit reports (`documents_v2`) is a deployment detail on+ /// a naming convention nothing enforces, whereas a clause name is set by+ /// the query we wrote.+ pub fn query_name(&self) -> &'static str {+ // `strum::IntoStaticStr` is what carries the 'static lifetime; the+ // `AsRefStr` impl borrows from `self`.+ self.clone().into()+ }++ /// Resolve the entity from the clause names a hit reported matching.+ ///+ /// Each entity's clause filters `_index` to its own alias, so exactly one+ /// can match a given hit. `None` means none of the names belong to a known+ /// entity, or more than one does — either way the caller must reject the+ /// hit rather than guess at it.+ pub fn from_matched_queries<'a>(names: impl IntoIterator<Item = &'a str>) -> Option<Self> {+ let mut matched = names.into_iter().filter_map(|name| name.parse().ok());+ let first = matched.next()?;+ matched.next().is_none().then_some(first)+ }+}+impl From<OpenSearchEntityType> for SearchEntityType {fn from(value: OpenSearchEntityType) -> Self {match value {@@ -116,6 +154,7 @@ impl From<OpenSearchEntityType> for SearchEntityType {OpenSearchEntityType::Emails => SearchEntityType::Emails,OpenSearchEntityType::CallRecords => SearchEntityType::CallRecords,OpenSearchEntityType::Projects => SearchEntityType::Projects,+ OpenSearchEntityType::CalendarEvents => SearchEntityType::CalendarEvents,}}}@@ -129,6 +168,7 @@ impl From<OpenSearchEntityType> for SearchIndex {OpenSearchEntityType::Emails => SearchIndex::Emails,OpenSearchEntityType::CallRecords => SearchIndex::CallRecords,OpenSearchEntityType::Projects => SearchIndex::Projects,+ OpenSearchEntityType::CalendarEvents => SearchIndex::CalendarEvents,}}}@@ -136,17 +176,51 @@ impl From<OpenSearchEntityType> for SearchIndex {#[cfg(test)]mod test {use super::*;+ use strum::IntoEnumIterator as _;++ #[test]+ fn query_names_round_trip_for_every_variant() {+ for variant in OpenSearchEntityType::iter() {+ assert_eq!(+ OpenSearchEntityType::from_matched_queries([variant.query_name()]),+ Some(variant.clone()),+ "{} must parse back from its own query name",+ variant.query_name()+ );+ }+ }++ #[test]+ fn a_hit_matching_no_known_clause_is_rejected() {+ assert_eq!(OpenSearchEntityType::from_matched_queries([]), None);+ assert_eq!(+ OpenSearchEntityType::from_matched_queries(["reminders"]),+ None+ );+ }++ #[test]+ fn unrelated_clause_names_do_not_hide_the_entity() {+ // Naming an inner clause for debugging must not break dispatch.+ assert_eq!(+ OpenSearchEntityType::from_matched_queries(["title_term_0", "calendar_events"]),+ Some(OpenSearchEntityType::CalendarEvents)+ );+ }++ #[test]+ fn an_ambiguous_hit_is_rejected_rather_than_picked() {+ // Two entity clauses matching one hit means the query is wrong; a+ // silent first-wins choice is what this whole mechanism replaced.+ assert_eq!(+ OpenSearchEntityType::from_matched_queries(["projects", "calendar_events"]),+ None+ );+ }#[test]fn index_name_matches_search_index() {- for variant in [- OpenSearchEntityType::Channels,- OpenSearchEntityType::Chats,- OpenSearchEntityType::Documents,- OpenSearchEntityType::Emails,- OpenSearchEntityType::CallRecords,- OpenSearchEntityType::Projects,- ] {+ for variant in OpenSearchEntityType::iter() {let from_index: SearchIndex = variant.clone().into();assert_eq!(variant.index_name(), from_index.as_ref());}diff --git a/crates/models_search/src/calendar_event.rs b/crates/models_search/src/calendar_event.rsnew file mode 100644index 0000000000..d42bfca3c6--- /dev/null+++ b/crates/models_search/src/calendar_event.rs@@ -0,0 +1,175 @@+use crate::{SearchHighlight, SearchResponse, SearchResponseItem};+use chrono::{DateTime, Utc};+use models_soup::SoupProperty;+use schemars::JsonSchema;+use serde::{Deserialize, Serialize};+use utoipa::ToSchema;++/// Timed or all-day span of a calendar event.+///+/// Deliberately a local mirror of `models_soup::calendar_event::SoupCalendarEventTime`+/// rather than a reuse of it: this module needs both `ToSchema` (OpenAPI) and+/// `JsonSchema` (AI tool schemas), and `models_soup` carries no schemars+/// dependency. The wire shape is identical and must stay that way, so a+/// client can decode a search row and a soup row with one mapping.+///+/// Fields are camelCased per variant rather than via `rename_all_fields`,+/// which utoipa ignores.+#[derive(Debug, Clone, Serialize, Deserialize, ToSchema, JsonSchema)]+#[serde(rename_all = "camelCase", tag = "kind")]+pub enum CalendarEventSearchTime {+ /// Absolute timed event.+ #[serde(rename_all = "camelCase")]+ Timed {+ /// Inclusive start.+ starts_at: DateTime<Utc>,+ /// Exclusive end.+ ends_at: DateTime<Utc>,+ /// Original IANA time zone.+ time_zone: Option<String>,+ },+ /// All-day event with an exclusive end date.+ #[serde(rename_all = "camelCase")]+ AllDay {+ /// Inclusive start date.+ start_date: chrono::NaiveDate,+ /// Exclusive end date.+ end_date: chrono::NaiveDate,+ },+}++#[derive(Debug, Serialize, Deserialize, ToSchema, JsonSchema)]+pub struct CalendarEventSearchResult {+ pub highlight: SearchHighlight,+ /// The score of the result+ #[serde(skip_serializing_if = "Option::is_none")]+ pub score: Option<f64>,+}++/// Metadata associated with calendar event search, to be used with+/// SearchResponseItem+#[derive(Debug, Serialize, Deserialize, ToSchema)]+pub struct CalendarEventSearchMetadata {+ pub event_id: uuid::Uuid,+ pub owner_id: String,+ pub title: String,+ pub updated_at: DateTime<Utc>,+ pub created_at: DateTime<Utc>,+}++/// The instance a search row points at.+///+/// A recurring series is indexed once, as its master. Which instance the row+/// should show is decided per request rather than baked into the index: the+/// next occurrence at or after now, else the most recent past one. This is+/// the same resolution a calendar mention performs, so a search row and a+/// mention of the same event agree on where they land.+#[derive(Debug, Clone, Serialize, Deserialize, ToSchema, JsonSchema)]+#[serde(rename_all = "camelCase")]+pub struct CalendarEventSearchOccurrence {+ /// Stable key of the resolved instance within its series.+ pub occurrence_key: String,+ /// Span of the resolved instance.+ pub time: CalendarEventSearchTime,+}++/// A single response item, part of the CalendarEventSearchResponse object+#[derive(Debug, Serialize, Deserialize, ToSchema, JsonSchema)]+pub struct CalendarEventSearchResponseItem {+ /// Standardized fields that all item types will share.+ pub id: uuid::Uuid,+ pub name: String,+ pub owner_id: String,++ pub updated_at: DateTime<Utc>,+ pub created_at: DateTime<Utc>,+ pub calendar_event_search_results: Vec<CalendarEventSearchResult>,+}++/// Metadata for a calendar event fetched from the database.+///+/// The index carries the master span for ranking, but the occurrence a row+/// should display depends on when the query ran, so it is resolved here+/// rather than indexed.+#[derive(Debug, Serialize, Deserialize, ToSchema, JsonSchema)]+#[serde(rename_all = "camelCase")]+pub struct CalendarEventMetadata {+ pub created_at: DateTime<Utc>,+ pub updated_at: DateTime<Utc>,+ /// Canonical status (`confirmed`, `tentative`, `cancelled`).+ pub status: String,+ /// The series' own span.+ pub time: CalendarEventSearchTime,+ /// Whether the series carries a recurrence rule. Lets a row render a+ /// recurring badge without parsing the rules.+ pub is_recurring: bool,+ /// The instance this row points at. `None` when the series has no+ /// materialized occurrence — occurrences exist only inside a rolling+ /// window, so a row can legitimately fall back to `time`.+ #[serde(skip_serializing_if = "Option::is_none")]+ pub occurrence: Option<CalendarEventSearchOccurrence>,+ /// Direct conference join URL when known.+ #[serde(skip_serializing_if = "Option::is_none")]+ pub conference_url: Option<String>,+ /// Whether the canonical source prohibits mutation.+ pub is_read_only: bool,+}++/// CalendarEventSearchResponseItem with metadata fetched from macrodb.+#[derive(Debug, Serialize, Deserialize, ToSchema, JsonSchema)]+pub struct CalendarEventSearchResponseItemWithMetadata {+ /// Metadata from the database. None if the event no longer exists.+ pub metadata: Option<CalendarEventMetadata>,+ /// Entity properties (e.g. tags) on the event.+ #[serde(skip_serializing_if = "Option::is_none")]+ #[schemars(skip)]+ pub properties: Option<Vec<SoupProperty>>,+ #[serde(flatten)]+ pub extra: CalendarEventSearchResponseItem,+}++impl From<SearchResponseItem<CalendarEventSearchResult, CalendarEventSearchMetadata>>+ for CalendarEventSearchResponseItem+{+ fn from(+ response: SearchResponseItem<CalendarEventSearchResult, CalendarEventSearchMetadata>,+ ) -> Self {+ CalendarEventSearchResponseItem {+ id: response.metadata.event_id,+ name: response.metadata.title,+ owner_id: response.metadata.owner_id,+ updated_at: response.metadata.updated_at,+ created_at: response.metadata.created_at,+ calendar_event_search_results: response.results,+ }+ }+}++/// Calendar Event Search Response+pub type CalendarEventSearchResponse = SearchResponse<CalendarEventSearchResponseItemWithMetadata>;++#[derive(Debug, Serialize, Deserialize, ToSchema, JsonSchema)]+pub struct SimpleCalendarEventSearchResponseBaseItem<T> {+ /// The calendar event id+ pub calendar_event_id: String,+ #[schema(inline)]+ /// The time the event was last updated+ pub updated_at: T,+ #[schema(inline)]+ /// The time the event was created+ pub created_at: T,+ /// The event title+ pub title: String,+ /// The owner of this event projection+ pub user_id: String,+ /// Whether the series carries a recurrence rule+ pub is_recurring: bool,+ /// Stable key of the instance this row points at, when one resolved+ #[serde(skip_serializing_if = "Option::is_none")]+ pub occurrence_key: Option<String>,+ /// The highlights on the event+ pub highlight: SearchHighlight,+}++pub type SimpleCalendarEventSearchResponseItem =+ SimpleCalendarEventSearchResponseBaseItem<crate::HumanReadableTimestamp>;diff --git a/crates/models_search/src/lib.rs b/crates/models_search/src/lib.rsindex 94bb1ca831..be2a30284d 100644--- a/crates/models_search/src/lib.rs+++ b/crates/models_search/src/lib.rs@@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize};use strum::{Display, EnumString};use utoipa::ToSchema;+pub mod calendar_event;pub mod call_record;pub mod channel;pub mod chat;diff --git a/crates/models_search/src/unified.rs b/crates/models_search/src/unified.rsindex 3135adc189..fc6f3842ca 100644--- a/crates/models_search/src/unified.rs+++ b/crates/models_search/src/unified.rs@@ -14,8 +14,8 @@ use crate::{project::SimpleProjectSearchResponseBaseItem,};use item_filters::{- CallFilters, ChannelFilters, ChatFilters, DocumentFilters, EmailFilters, EntityFilters,- ProjectFilters, ast::document::resolve_file_types,+ CalendarEventFilters, CallFilters, ChannelFilters, ChatFilters, DocumentFilters, EmailFilters,+ EntityFilters, ProjectFilters, ast::document::resolve_file_types,};use model_file_type::FileAssociation;use schemars::JsonSchema;@@ -32,6 +32,8 @@ pub enum UnifiedSearchIndex {Projects,#[serde(rename = "call_records")]CallRecords,+ #[serde(rename = "calendar_events")]+ CalendarEvents,}const NIL_UUID: &str = "00000000-0000-0000-0000-000000000000";@@ -64,7 +66,10 @@ pub fn entity_filters_from_include(filters.project_filters.project_ids = exclude.clone();}if !include.contains(&UnifiedSearchIndex::CallRecords) {- filters.call_filters.channel_ids = exclude;+ filters.call_filters.channel_ids = exclude.clone();+ }+ if !include.contains(&UnifiedSearchIndex::CalendarEvents) {+ filters.calendar_event_filters.calendar_event_ids = exclude;}filters}@@ -126,6 +131,8 @@ pub struct SearchEntityFilters {pub should_include_projects: bool,/// Whether to include call records in search resultspub should_include_call_records: bool,+ /// Whether to include calendar events in search results+ pub should_include_calendar_events: bool,/// Document filters with file associations expandedpub document_filters: DocumentFilters,/// Chat filters@@ -138,6 +145,8 @@ pub struct SearchEntityFilters {pub project_filters: ProjectFilters,/// Call filterspub call_filters: CallFilters,+ /// Calendar event filters+ pub calendar_event_filters: CalendarEventFilters,}fn contains_nil_uuid(ids: &[String]) -> bool {@@ -168,6 +177,7 @@ impl From<EntityFilters> for SearchEntityFilters {let mut channel_filters = filters.channel_filters;let mut project_filters = filters.project_filters;let mut call_filters = filters.call_filters;+ let mut calendar_event_filters = filters.calendar_event_filters;let should_include_documents = !contains_nil_uuid(&document_filters.document_ids);let should_include_chats = !contains_nil_uuid(&chat_filters.chat_ids);@@ -175,6 +185,8 @@ impl From<EntityFilters> for SearchEntityFilters {let should_include_channels = !contains_nil_uuid(&channel_filters.channel_ids);let should_include_projects = !contains_nil_uuid(&project_filters.project_ids);let should_include_call_records = !contains_nil_uuid(&call_filters.channel_ids);+ let should_include_calendar_events =+ !contains_nil_uuid(&calendar_event_filters.calendar_event_ids);strip_nil_uuids(&mut document_filters.document_ids);strip_nil_uuids(&mut chat_filters.chat_ids);@@ -183,6 +195,7 @@ impl From<EntityFilters> for SearchEntityFilters {strip_nil_uuids(&mut channel_filters.channel_ids);strip_nil_uuids(&mut project_filters.project_ids);strip_nil_uuids(&mut call_filters.channel_ids);+ strip_nil_uuids(&mut calendar_event_filters.calendar_event_ids);document_filters.file_types = expand_file_types_for_search(document_filters.file_types);@@ -193,12 +206,14 @@ impl From<EntityFilters> for SearchEntityFilters {should_include_channels,should_include_projects,should_include_call_records,+ should_include_calendar_events,document_filters,chat_filters,email_filters,channel_filters,project_filters,call_filters,+ calendar_event_filters,}}}@@ -214,6 +229,7 @@ pub enum UnifiedSearchResponseItem {Project(ProjectSearchResponseItemWithMetadata),Call(CallRecordSearchResponseItemWithMetadata),Company(crate::crm_company::CrmCompanySearchResponseItem),+ CalendarEvent(crate::calendar_event::CalendarEventSearchResponseItemWithMetadata),}impl UnifiedSearchResponseItem {@@ -227,6 +243,7 @@ impl UnifiedSearchResponseItem {Self::Project(item) => item.extra.id,Self::Call(item) => item.extra.id,Self::Company(item) => item.id,+ Self::CalendarEvent(item) => item.extra.id,}}/// Get the updated_at timestamp for each item@@ -240,6 +257,7 @@ impl UnifiedSearchResponseItem {Self::Project(item) => item.metadata.as_ref().map(|m| m.updated_at),Self::Call(item) => item.metadata.as_ref().map(|m| m.updated_at),Self::Company(item) => Some(item.updated_at),+ Self::CalendarEvent(item) => item.metadata.as_ref().map(|m| m.updated_at),}}}@@ -261,6 +279,7 @@ pub enum SimpleUnifiedSearchResponseBaseItem<T> {Channel(SimpleChannelSearchReponseBaseItem<T>),Project(SimpleProjectSearchResponseBaseItem<T>),Call(SimpleCallRecordSearchResponseBaseItem<T>),+ CalendarEvent(crate::calendar_event::SimpleCalendarEventSearchResponseBaseItem<T>),}pub type SimpleUnifiedSearchResponseItem =diff --git a/crates/opensearch_client/src/calendar_event.rs b/crates/opensearch_client/src/calendar_event.rsnew file mode 100644index 0000000000..68e6246d13--- /dev/null+++ b/crates/opensearch_client/src/calendar_event.rs@@ -0,0 +1,44 @@+use crate::{+ OpensearchClient, Result, delete,+ upsert::{self, calendar_event::UpsertCalendarEventArgs, properties::IndexedProperty},+};++impl OpensearchClient {+ /// Upserts a calendar event series master into the opensearch+ /// calendar_events index+ #[tracing::instrument(skip(self, args), fields(event_id=%args.event_id), err)]+ pub async fn upsert_calendar_event(+ &self,+ args: &UpsertCalendarEventArgs,+ index_override: Option<&str>,+ ) -> Result<()> {+ upsert::calendar_event::upsert_calendar_event(&self.inner, args, index_override).await+ }++ /// Deletes a calendar event from the opensearch calendar_events index+ #[tracing::instrument(skip(self), err)]+ pub async fn delete_calendar_event(+ &self,+ event_id: &str,+ index_override: Option<&str>,+ ) -> Result<()> {+ delete::calendar_event::delete_calendar_event_by_id(&self.inner, event_id, index_override)+ .await+ }++ /// Updates only the denormalized `properties` on an indexed calendar event+ #[tracing::instrument(skip(self, properties), err)]+ pub async fn update_calendar_event_properties(+ &self,+ event_id: &str,+ properties: &[IndexedProperty],+ ) -> Result<()> {+ upsert::calendar_event::update_calendar_event_properties(+ &self.inner,+ event_id,+ properties,+ None,+ )+ .await+ }+}diff --git a/crates/opensearch_client/src/delete/calendar_event.rs b/crates/opensearch_client/src/delete/calendar_event.rsnew file mode 100644index 0000000000..a0bf20cfd8--- /dev/null+++ b/crates/opensearch_client/src/delete/calendar_event.rs@@ -0,0 +1,59 @@+use models_opensearch::SearchIndex;++use crate::{Result, error::OpensearchClientError};++/// Deletes a calendar event doc by id. The calendar events index is flat with+/// `_id` = event id, so this is a direct delete. A missing-doc 404 is a no-op+/// — the event was never indexed or is already gone. A missing-index 404+/// (`index_not_found_exception`) is a real outage and must propagate.+#[tracing::instrument(skip(client), err)]+pub async fn delete_calendar_event_by_id(+ client: &opensearch::OpenSearch,+ event_id: &str,+ index_override: Option<&str>,+) -> Result<()> {+ let index = index_override.unwrap_or(SearchIndex::CalendarEvents.as_ref());+ let response = client+ .delete(opensearch::DeleteParts::IndexId(index, event_id))+ .send()+ .await+ .map_err(|err| OpensearchClientError::Unknown {+ details: err.to_string(),+ method: Some("delete_calendar_event_by_id".to_string()),+ })?;++ let status_code = response.status_code();+ if status_code.is_success() {+ return Ok(());+ }++ let body =+ response+ .text()+ .await+ .map_err(|err| OpensearchClientError::DeserializationFailed {+ details: err.to_string(),+ method: Some("delete_calendar_event_by_id".to_string()),+ })?;++ if status_code.as_u16() == 404 {+ let result = serde_json::from_str::<serde_json::Value>(&body)+ .ok()+ .and_then(|value| value["result"].as_str().map(str::to_owned));+ if result.as_deref() == Some("not_found") {+ tracing::debug!(event_id=%event_id, "calendar event not indexed; nothing to delete");+ return Ok(());+ }+ }++ tracing::error!(+ status_code = ?status_code,+ body = ?body,+ "error deleting calendar event by id"+ );++ Err(OpensearchClientError::Unknown {+ details: body,+ method: Some("delete_calendar_event_by_id".to_string()),+ })+}diff --git a/crates/opensearch_client/src/delete/mod.rs b/crates/opensearch_client/src/delete/mod.rsindex 01bd3928de..3635d4ae88 100644--- a/crates/opensearch_client/src/delete/mod.rs+++ b/crates/opensearch_client/src/delete/mod.rs@@ -1,3 +1,4 @@+pub mod calendar_event;pub mod call_record;pub mod channel;pub mod chat;diff --git a/crates/opensearch_client/src/lib.rs b/crates/opensearch_client/src/lib.rsindex 401ff01aba..e39fc543dd 100644--- a/crates/opensearch_client/src/lib.rs+++ b/crates/opensearch_client/src/lib.rs@@ -5,6 +5,7 @@ pub mod search;pub mod unified;pub mod upsert;+pub mod calendar_event;pub mod call_record;pub mod channel_message;pub mod chat;diff --git a/crates/opensearch_client/src/search/calendar_events.rs b/crates/opensearch_client/src/search/calendar_events.rsnew file mode 100644index 0000000000..2f03fef32a--- /dev/null+++ b/crates/opensearch_client/src/search/calendar_events.rs@@ -0,0 +1,244 @@+use crate::{+ Result, delegate_methods,+ search::{+ builder::{SearchQueryBuilder, SearchQueryConfig},+ properties::build_tag_filter,+ },+};++use models_opensearch::OpenSearchEntityType;+use opensearch_query_builder::{BoolQueryBuilder, QueryType};++#[derive(Clone)]+pub(crate) struct CalendarEventSearchConfig;++impl SearchQueryConfig for CalendarEventSearchConfig {+ const USER_ID_KEY: Option<&'static str> = Some("owner_id");+ const TITLE_KEY: &'static str = "name";+ const ENTITY_INDEX: OpenSearchEntityType = OpenSearchEntityType::CalendarEvents;+}++/// Query builder for the flat calendar events index.+///+/// One doc per **series master**: recurring instances are query projections+/// materialized only inside a rolling window, so indexing them individually+/// would bound a recurring event's searchability to that slice and flood+/// results with near-identical rows. The relevant occurrence is resolved at+/// enrichment time instead.+///+/// Calendar events carry no indexed content, so every mode matches terms+/// against `name` alone. Access mirrors the soup predicate: `owner_id ==+/// caller` or the event's `source_link_id` is one of the caller's delegated+/// inbox links.+pub(crate) struct CalendarEventQueryBuilder {+ inner: SearchQueryBuilder<CalendarEventSearchConfig>,+ link_ids: Vec<String>,+ statuses: Vec<String>,+ organizer_emails: Vec<String>,+ attendee_emails: Vec<String>,+ tag_option_ids: Vec<String>,+ match_all_tags: bool,+}++impl CalendarEventQueryBuilder {+ pub fn new(terms: Vec<String>) -> Self {+ Self {+ inner: SearchQueryBuilder::new(terms),+ link_ids: Vec::new(),+ statuses: Vec::new(),+ organizer_emails: Vec::new(),+ attendee_emails: Vec::new(),+ tag_option_ids: Vec::new(),+ match_all_tags: false,+ }+ }++ /// Inbox links the caller may read through delegation. Combined with+ /// `owner_id` as an OR, so a delegate finds events on inboxes they were+ /// granted without owning the projection row.+ pub fn link_ids(mut self, link_ids: Vec<String>) -> Self {+ self.link_ids = link_ids;+ self+ }++ pub fn statuses(mut self, statuses: Vec<String>) -> Self {+ self.statuses = statuses;+ self+ }++ pub fn organizer_emails(mut self, organizer_emails: Vec<String>) -> Self {+ self.organizer_emails = organizer_emails;+ self+ }++ pub fn attendee_emails(mut self, attendee_emails: Vec<String>) -> Self {+ self.attendee_emails = attendee_emails;+ self+ }++ pub fn tag_option_ids(mut self, tag_option_ids: Vec<String>) -> Self {+ self.tag_option_ids = tag_option_ids;+ self+ }++ pub fn match_all_tags(mut self, match_all_tags: bool) -> Self {+ self.match_all_tags = match_all_tags;+ self+ }++ // Copy function signature from SearchQueryBuilder+ delegate_methods! {+ fn match_type(match_type: &str) -> Self;+ fn page(page: u32) -> Self;+ fn page_size(page_size: u32) -> Self;+ fn user_id(user_id: &str) -> Self;+ fn collapse(collapse: bool) -> Self;+ fn ids(ids: Vec<String>) -> Self;+ fn ids_only(ids_only: bool) -> Self;+ }++ pub fn build_bool_query<'a>(&'a self) -> Result<BoolQueryBuilder<'a>> {+ let mut bool_query = BoolQueryBuilder::new();++ // Only search on the calendar events alias.+ bool_query.filter(QueryType::term(+ "_index",+ CalendarEventSearchConfig::ENTITY_INDEX+ .index_name()+ .to_string(),+ ));++ // Access control. `ids_only` means the caller already resolved an+ // explicit id set, so the shared builder's id filter is authoritative+ // and the link/owner clause would only narrow it further.+ if self.inner.ids_only {+ bool_query.filter(+ self.inner+ .build_filter_query(CalendarEventSearchConfig::USER_ID_KEY)?,+ );+ } else {+ // An event is visible when the caller owns the projection or the+ // event's source link is delegated to them. Two disjoint fields,+ // so this cannot go through `build_filter_query` — that ORs a+ // single user-id field against an entity-id list.+ let mut access = BoolQueryBuilder::new();+ access.minimum_should_match(1);+ access.should(QueryType::term(+ CalendarEventSearchConfig::USER_ID_KEY+ .expect("calendar events declare a user id key")+ .to_string(),+ self.inner.user_id.clone(),+ ));+ if !self.link_ids.is_empty() {+ access.should(QueryType::terms("source_link_id", self.link_ids.clone()));+ }+ bool_query.filter(access.build().into());++ // An explicit id list narrows within what the caller can see.+ if !self.inner.ids.is_empty() {+ bool_query.filter(QueryType::terms("entity_id", self.inner.ids.clone()));+ }+ }++ if !self.statuses.is_empty() {+ bool_query.filter(QueryType::terms("status", self.statuses.clone()));+ }++ if !self.organizer_emails.is_empty() {+ bool_query.filter(QueryType::terms(+ "organizer_email",+ self.organizer_emails.clone(),+ ));+ }++ if !self.attendee_emails.is_empty() {+ bool_query.filter(QueryType::terms(+ "attendee_emails",+ self.attendee_emails.clone(),+ ));+ }++ // Tag filter: nested clause(s) matching the option ids in+ // `properties.values`, with no definition_id constraint.+ if let Some(nested) = build_tag_filter(&self.tag_option_ids, self.match_all_tags) {+ bool_query.filter(nested);+ }++ // Title match: every term must match the event title.+ bool_query.must(self.inner.build_title_term_query()?);++ Ok(bool_query)+ }+}++/// The `_source` fields of a calendar event doc as returned by search.+///+/// `source_link_id` and `ical_uid` are deliberately required, not optional.+/// [`super::unified::UnifiedSearchIndex`] is an untagged enum, so a variant+/// matches on doc shape alone; both fields are NOT NULL in Postgres and+/// unique to calendar docs, which keeps this variant from swallowing another+/// index's hits regardless of where it sits in the variant order.+#[derive(Debug, serde::Serialize, serde::Deserialize)]+pub(crate) struct CalendarEventIndex {+ pub entity_id: uuid::Uuid,+ /// The series title, indexed as `name` so it sits in the shared unified+ /// highlight field list alongside every other entity's title field.+ pub name: String,+ pub owner_id: String,+ pub source_link_id: String,+ pub ical_uid: String,+ #[serde(default)]+ pub status: Option<String>,+ #[serde(default)]+ pub is_recurring: bool,+ #[serde(default)]+ pub starts_at_millis: Option<i64>,+ #[serde(default)]+ pub ends_at_millis: Option<i64>,+ #[serde(default)]+ pub start_date: Option<String>,+ #[serde(default)]+ pub end_date: Option<String>,+ #[serde(default)]+ pub updated_at_millis: Option<i64>,+}++#[derive(Debug, Default)]+pub struct CalendarEventSearchArgs {+ pub terms: Vec<String>,+ pub user_id: String,+ pub calendar_event_ids: Vec<String>,+ pub link_ids: Vec<String>,+ pub statuses: Vec<String>,+ pub organizer_emails: Vec<String>,+ pub attendee_emails: Vec<String>,+ pub page: u32,+ pub page_size: u32,+ pub match_type: String,+ pub collapse: bool,+ pub ids_only: bool,+ pub tag_option_ids: Vec<String>,+ pub match_all_tags: bool,+}++impl From<CalendarEventSearchArgs> for CalendarEventQueryBuilder {+ fn from(args: CalendarEventSearchArgs) -> Self {+ CalendarEventQueryBuilder::new(args.terms)+ .match_type(&args.match_type)+ .page_size(args.page_size)+ .page(args.page)+ .user_id(&args.user_id)+ .ids(args.calendar_event_ids)+ .link_ids(args.link_ids)+ .statuses(args.statuses)+ .organizer_emails(args.organizer_emails)+ .attendee_emails(args.attendee_emails)+ .collapse(args.collapse)+ .ids_only(args.ids_only)+ .tag_option_ids(args.tag_option_ids)+ .match_all_tags(args.match_all_tags)+ }+}++#[cfg(test)]+mod test;diff --git a/crates/opensearch_client/src/search/calendar_events/test.rs b/crates/opensearch_client/src/search/calendar_events/test.rsnew file mode 100644index 0000000000..429b2d2f1d--- /dev/null+++ b/crates/opensearch_client/src/search/calendar_events/test.rs@@ -0,0 +1,99 @@+use super::*;+use opensearch_query_builder::ToOpenSearchJson;++#[test]+fn test_build_bool_query_owner_or_delegated_link() -> anyhow::Result<()> {+ let builder = CalendarEventQueryBuilder::new(vec!["standup".to_string()])+ .match_type("exact")+ .page_size(20)+ .page(1)+ .user_id("user123")+ .link_ids(vec!["link1".to_string(), "link2".to_string()]);++ let json = builder.build_bool_query()?.build().to_json();++ let filter = json["bool"]["filter"].as_array().expect("filter array");+ assert!(+ filter.contains(&serde_json::json!({"term": {"_index": "calendar_events"}})),+ "filter must constrain to the calendar_events index: {filter:?}"+ );+ // Access is owner OR delegated link — two disjoint fields OR'd, mirroring+ // the soup predicate.+ assert!(+ filter.contains(&serde_json::json!({+ "bool": {+ "minimum_should_match": 1,+ "should": [+ {"term": {"owner_id": "user123"}},+ {"terms": {"source_link_id": ["link1", "link2"]}}+ ]+ }+ })),+ "filter must admit owned or delegated events: {filter:?}"+ );++ let must = json["bool"]["must"].as_array().expect("must array");+ assert_eq!(must.len(), 1, "one title clause per query: {must:?}");++ Ok(())+}++#[test]+fn test_owner_only_when_no_delegated_links() -> anyhow::Result<()> {+ let builder = CalendarEventQueryBuilder::new(vec!["standup".to_string()]).user_id("user123");+ let json = builder.build_bool_query()?.build().to_json();+ let filter = json["bool"]["filter"].as_array().expect("filter array");+ assert!(+ filter.contains(&serde_json::json!({+ "bool": {+ "minimum_should_match": 1,+ "should": [{"term": {"owner_id": "user123"}}]+ }+ })),+ "a caller with no delegated inboxes sees only their own events: {filter:?}"+ );+ Ok(())+}++#[test]+fn test_filters_are_applied() -> anyhow::Result<()> {+ let builder = CalendarEventQueryBuilder::new(vec!["review".to_string()])+ .user_id("user123")+ .statuses(vec!["confirmed".to_string()])+ .organizer_emails(vec!["[email protected]".to_string()])+ .attendee_emails(vec!["[email protected]".to_string()])+ .ids(vec!["event1".to_string()]);++ let json = builder.build_bool_query()?.build().to_json();+ let filter = json["bool"]["filter"].as_array().expect("filter array");++ for expected in [+ serde_json::json!({"terms": {"status": ["confirmed"]}}),+ serde_json::json!({"terms": {"organizer_email": ["[email protected]"]}}),+ serde_json::json!({"terms": {"attendee_emails": ["[email protected]"]}}),+ serde_json::json!({"terms": {"entity_id": ["event1"]}}),+ ] {+ assert!(+ filter.contains(&expected),+ "missing filter {expected:?} in {filter:?}"+ );+ }+ Ok(())+}++#[test]+fn test_ids_only_without_ids_errors() {+ let builder = CalendarEventQueryBuilder::new(vec!["standup".to_string()])+ .user_id("user123")+ .ids_only(true);+ assert!(+ builder.build_bool_query().is_err(),+ "ids_only with an empty id set must not fall back to an unscoped query"+ );+}++#[test]+fn test_no_terms_errors() {+ let builder = CalendarEventQueryBuilder::new(vec![]).user_id("user123");+ assert!(builder.build_bool_query().is_err(), "terms are required");+}diff --git a/crates/opensearch_client/src/search/channels/test.rs b/crates/opensearch_client/src/search/channels/test.rsindex e40ddfe74f..9709c66d8e 100644--- a/crates/opensearch_client/src/search/channels/test.rs+++ b/crates/opensearch_client/src/search/channels/test.rs@@ -19,6 +19,8 @@ fn channel_index(updated_at_millis: Option<i64>) -> ChannelMessageIndex {fn hit_for(source: ChannelMessageIndex) -> Hit<ChannelMessageIndex> {Hit {+ index: "channels_v2".to_string(),+ matched_queries: vec!["channels".to_string()],score: Some(1.0),source,highlight: None,diff --git a/crates/opensearch_client/src/search/mod.rs b/crates/opensearch_client/src/search/mod.rsindex a35d563692..46a0d631c5 100644--- a/crates/opensearch_client/src/search/mod.rs+++ b/crates/opensearch_client/src/search/mod.rs@@ -8,6 +8,7 @@pub(crate) const HIGHLIGHT_MAX_ANALYZER_OFFSET: u32 = 999_999;mod builder;+pub mod calendar_events;pub mod call_records;pub mod channels;pub mod chats;diff --git a/crates/opensearch_client/src/search/model.rs b/crates/opensearch_client/src/search/model.rsindex 3fff826b0d..1da8976ce5 100644--- a/crates/opensearch_client/src/search/model.rs+++ b/crates/opensearch_client/src/search/model.rs@@ -220,6 +220,16 @@ impl Display for MacroEm {#[derive(Debug, serde::Serialize, serde::Deserialize)]pub(crate) struct Hit<T> {+ /// Physical index this hit came from, e.g. `documents_v2`. Kept for+ /// diagnostics only — it is a deployment detail on a naming convention+ /// nothing enforces, so it must not be used to identify the entity.+ #[serde(rename = "_index")]+ pub index: String,+ /// Names of the query clauses this hit matched. The unified query names+ /// each entity's clause after that entity, which is how a hit is resolved+ /// to a type. Absent when the query named no clauses.+ #[serde(default)]+ pub matched_queries: Vec<String>,#[serde(rename = "_score")]pub score: Option<f64>,#[serde(rename = "_source")]diff --git a/crates/opensearch_client/src/search/unified.rs b/crates/opensearch_client/src/search/unified.rsindex bf190ba3ba..8cf4749a79 100644--- a/crates/opensearch_client/src/search/unified.rs+++ b/crates/opensearch_client/src/search/unified.rs@@ -5,6 +5,10 @@ use crate::{error::{OpensearchClientError, ResponseExt},search::{builder::{SearchQueryConfig, updated_at_sort},+ calendar_events::{+ CalendarEventIndex, CalendarEventQueryBuilder, CalendarEventSearchArgs,+ CalendarEventSearchConfig,+ },call_records::{CallRecordIndex, CallRecordQueryBuilder, CallRecordSearchArgs, CallRecordSearchConfig,CallRecordSearchMode,@@ -68,6 +72,8 @@ pub struct UnifiedSearchArgs {pub call_record_search_args: UnifiedCallRecordSearchArgs,/// The project search args. If None, we do not search projectspub project_search_args: UnifiedProjectSearchArgs,+ /// The calendar event search args. If None, we do not search calendar events+ pub calendar_event_search_args: UnifiedCalendarEventSearchArgs,}impl From<UnifiedSearchArgs> for DocumentSearchArgs {@@ -190,6 +196,27 @@ impl From<UnifiedSearchArgs> for ProjectSearchArgs {}}+impl From<UnifiedSearchArgs> for CalendarEventSearchArgs {+ fn from(args: UnifiedSearchArgs) -> Self {+ CalendarEventSearchArgs {+ terms: args.calendar_event_search_args.terms,+ user_id: args.user_id,+ page: args.page,+ page_size: args.page_size,+ match_type: args.match_type,+ collapse: args.collapse,+ ids_only: args.calendar_event_search_args.ids_only,+ calendar_event_ids: args.calendar_event_search_args.calendar_event_ids,+ link_ids: args.calendar_event_search_args.link_ids,+ statuses: args.calendar_event_search_args.statuses,+ organizer_emails: args.calendar_event_search_args.organizer_emails,+ attendee_emails: args.calendar_event_search_args.attendee_emails,+ tag_option_ids: args.calendar_event_search_args.tag_option_ids,+ match_all_tags: args.calendar_event_search_args.match_all_tags,+ }+ }+}+#[derive(Debug, Default, Clone)]pub struct UnifiedChatSearchArgs {pub terms: Vec<String>,@@ -251,6 +278,20 @@ pub struct UnifiedCallRecordSearchArgs {pub mode: CallRecordSearchMode,}+#[derive(Debug, Default, Clone)]+pub struct UnifiedCalendarEventSearchArgs {+ pub terms: Vec<String>,+ pub calendar_event_ids: Vec<String>,+ /// Inbox links the caller reads through delegation.+ pub link_ids: Vec<String>,+ pub statuses: Vec<String>,+ pub organizer_emails: Vec<String>,+ pub attendee_emails: Vec<String>,+ pub ids_only: bool,+ pub tag_option_ids: Vec<String>,+ pub match_all_tags: bool,+}+#[derive(Debug, Default, Clone)]pub struct UnifiedProjectSearchArgs {pub terms: Vec<String>,@@ -260,21 +301,75 @@ pub struct UnifiedProjectSearchArgs {pub match_all_tags: bool,}-/// Possible search result indices for unified search-#[derive(Debug, serde::Serialize, serde::Deserialize)]-#[serde(untagged)]+/// Possible search result indices for unified search.+///+/// Deliberately **not** `#[serde(untagged)]`. `_source` carries no type+/// discriminator, so an untagged enum had to guess the entity from whichever+/// fields happened to be present — first matching variant wins, silently — and+/// several shapes are subsets of others: a calendar doc carries exactly the+/// `entity_id`, `name`, and `owner_id` that a project doc requires.+///+/// Instead every entity's clause in the unified query is named after its+/// entity, and OpenSearch reports the names a hit matched in its+/// `matched_queries`. The discriminator therefore comes from the query this+/// module wrote, not from the shape of the document or the name of the+/// physical index it happened to live in.+#[derive(Debug)]pub(crate) enum UnifiedSearchIndex {ChannelMessage(ChannelMessageIndex),Document(DocumentIndex),Chat(ChatIndex),Email(Box<EmailIndex>),CallRecord(CallRecordIndex),- // Keep last: with `untagged`, earlier variants win and every other doc- // shape carries required fields (document_name, title, message_id, …)- // a project doc lacks.+ CalendarEvent(CalendarEventIndex),Project(ProjectIndex),}+impl UnifiedSearchIndex {+ /// Deserialize a hit's `_source` into the variant its matched clause names.+ ///+ /// Each entity's clause filters `_index` to its own alias, so exactly one+ /// can match a hit. Zero matches, several, or a `_source` that fails to+ /// parse as the shape its own clause promises are all errors — never a+ /// guess at which entity was meant.+ fn from_matched(matched_queries: &[String], source: serde_json::Value) -> Result<Self> {+ let entity =+ OpenSearchEntityType::from_matched_queries(matched_queries.iter().map(String::as_str))+ .ok_or_else(|| crate::error::OpensearchClientError::Unknown {+ details: format!(+ "hit matched no single known entity clause: {matched_queries:?}"+ ),+ method: Some("UnifiedSearchIndex::from_matched".to_string()),+ })?;++ fn parse<T: serde::de::DeserializeOwned>(+ entity: &OpenSearchEntityType,+ source: serde_json::Value,+ ) -> Result<T> {+ serde_json::from_value(source).map_err(|error| {+ crate::error::OpensearchClientError::DeserializationFailed {+ details: format!(+ "`{}` hit did not match its own shape: {error}",+ entity.query_name()+ ),+ method: Some("UnifiedSearchIndex::from_matched".to_string()),+ }+ })+ }++ Ok(match entity {+ OpenSearchEntityType::Channels => Self::ChannelMessage(parse(&entity, source)?),+ OpenSearchEntityType::Documents => Self::Document(parse(&entity, source)?),+ OpenSearchEntityType::Chats => Self::Chat(parse(&entity, source)?),+ OpenSearchEntityType::Emails => Self::Email(Box::new(parse(&entity, source)?)),+ OpenSearchEntityType::CallRecords => Self::CallRecord(parse(&entity, source)?),+ OpenSearchEntityType::CalendarEvents => Self::CalendarEvent(parse(&entity, source)?),+ OpenSearchEntityType::Projects => Self::Project(parse(&entity, source)?),+ })+ }+}++#[derive(Default)]pub struct SplitUnifiedSearchResponseValues {pub channel_message: Vec<SearchHit>,pub chat: Vec<SearchHit>,@@ -283,6 +378,7 @@ pub struct SplitUnifiedSearchResponseValues {pub project: Vec<SearchHit>,pub call_record: Vec<SearchHit>,pub crm_company: Vec<SearchHit>,+ pub calendar_event: Vec<SearchHit>,}pub trait SplitUnifiedSearchResponse: Iterator<Item = SearchHit> {@@ -294,63 +390,20 @@ whereT: Iterator<Item = SearchHit>,{fn split_search_response(self) -> SplitUnifiedSearchResponseValues {- let (channel_message, chat, document, email, project, call_record, crm_company) =- self.into_iter().fold(- (vec![], vec![], vec![], vec![], vec![], vec![], vec![]),- |(- mut channel_message,- mut chat,- mut document,- mut email,- mut project,- mut call_record,- mut crm_company,- ),- item| {- match item.entity_type {- SearchEntityType::Channels => {- channel_message.push(item);- }- SearchEntityType::Chats => {- chat.push(item);- }- SearchEntityType::Documents => {- document.push(item);- }- SearchEntityType::Emails => {- email.push(item);- }- SearchEntityType::Projects => {- project.push(item);- }- SearchEntityType::CallRecords => {- call_record.push(item);- }- SearchEntityType::CrmCompanies => {- crm_company.push(item);- }- }- (- channel_message,- chat,- document,- email,- project,- call_record,- crm_company,- )- },- );-- SplitUnifiedSearchResponseValues {- channel_message,- chat,- document,- email,- project,- call_record,- crm_company,+ let mut split = SplitUnifiedSearchResponseValues::default();+ for item in self {+ match item.entity_type {+ SearchEntityType::Channels => split.channel_message.push(item),+ SearchEntityType::Chats => split.chat.push(item),+ SearchEntityType::Documents => split.document.push(item),+ SearchEntityType::Emails => split.email.push(item),+ SearchEntityType::Projects => split.project.push(item),+ SearchEntityType::CallRecords => split.call_record.push(item),+ SearchEntityType::CrmCompanies => split.crm_company.push(item),+ SearchEntityType::CalendarEvents => split.calendar_event.push(item),+ }}+ split}}@@ -567,6 +620,25 @@ impl From<Hit<UnifiedSearchIndex>> for SearchHit {goto: None,updated_at: millis_to_datetime(a.updated_at_millis),},+ UnifiedSearchIndex::CalendarEvent(a) => SearchHit {+ entity_id: a.entity_id,+ entity_type: SearchEntityType::CalendarEvents,+ score: index.score,+ highlight: index+ .highlight+ .map(|h| {+ parse_highlight_hit(+ h,+ Keys {+ title_key: CalendarEventSearchConfig::TITLE_KEY,+ content_key: CalendarEventSearchConfig::CONTENT_KEY,+ },+ )+ })+ .unwrap_or_default(),+ goto: None,+ updated_at: millis_to_datetime(a.updated_at_millis),+ },UnifiedSearchIndex::Project(a) => SearchHit {entity_id: a.entity_id,entity_type: SearchEntityType::Projects,@@ -644,7 +716,8 @@ fn build_unified_search_request(args: &UnifiedSearchArgs) -> Result<SearchReques{let document_search_args: DocumentSearchArgs = args.clone().into();let document_query_builder: DocumentQueryBuilder = document_search_args.into();- let document_bool_query = document_query_builder.build_bool_query()?;+ let mut document_bool_query = document_query_builder.build_bool_query()?;+ document_bool_query.name(OpenSearchEntityType::Documents.query_name());let query_type: QueryType = document_bool_query.build().into();bool_query.should(query_type.to_owned());}@@ -652,7 +725,8 @@ fn build_unified_search_request(args: &UnifiedSearchArgs) -> Result<SearchRequesif args.search_indices.contains(&OpenSearchEntityType::Emails) {let email_search_args: EmailSearchArgs = args.clone().into();let email_query_builder: EmailQueryBuilder = email_search_args.into();- let email_bool_query = email_query_builder.build_bool_query()?;+ let mut email_bool_query = email_query_builder.build_bool_query()?;+ email_bool_query.name(OpenSearchEntityType::Emails.query_name());let query_type: QueryType = email_bool_query.build().into();bool_query.should(query_type.to_owned());}@@ -664,7 +738,8 @@ fn build_unified_search_request(args: &UnifiedSearchArgs) -> Result<SearchRequeslet channel_message_search_args: ChannelMessageSearchArgs = args.clone().into();let channel_message_query_builder: ChannelMessageQueryBuilder =channel_message_search_args.into();- let channel_message_bool_query = channel_message_query_builder.build_bool_query()?;+ let mut channel_message_bool_query = channel_message_query_builder.build_bool_query()?;+ channel_message_bool_query.name(OpenSearchEntityType::Channels.query_name());let query_type: QueryType = channel_message_bool_query.build().into();bool_query.should(query_type.to_owned());}@@ -672,7 +747,8 @@ fn build_unified_search_request(args: &UnifiedSearchArgs) -> Result<SearchRequesif args.search_indices.contains(&OpenSearchEntityType::Chats) {let chat_search_args: ChatSearchArgs = args.clone().into();let chat_query_builder: ChatQueryBuilder = chat_search_args.into();- let chat_bool_query = chat_query_builder.build_bool_query()?;+ let mut chat_bool_query = chat_query_builder.build_bool_query()?;+ chat_bool_query.name(OpenSearchEntityType::Chats.query_name());let query_type: QueryType = chat_bool_query.build().into();bool_query.should(query_type.to_owned());}@@ -683,7 +759,8 @@ fn build_unified_search_request(args: &UnifiedSearchArgs) -> Result<SearchReques{let call_record_search_args: CallRecordSearchArgs = args.clone().into();let call_record_query_builder: CallRecordQueryBuilder = call_record_search_args.into();- let call_record_bool_query = call_record_query_builder.build_bool_query()?;+ let mut call_record_bool_query = call_record_query_builder.build_bool_query()?;+ call_record_bool_query.name(OpenSearchEntityType::CallRecords.query_name());let query_type: QueryType = call_record_bool_query.build().into();bool_query.should(query_type.to_owned());}@@ -694,11 +771,25 @@ fn build_unified_search_request(args: &UnifiedSearchArgs) -> Result<SearchReques{let project_search_args: ProjectSearchArgs = args.clone().into();let project_query_builder: ProjectQueryBuilder = project_search_args.into();- let project_bool_query = project_query_builder.build_bool_query()?;+ let mut project_bool_query = project_query_builder.build_bool_query()?;+ project_bool_query.name(OpenSearchEntityType::Projects.query_name());let query_type: QueryType = project_bool_query.build().into();bool_query.should(query_type.to_owned());}+ if args+ .search_indices+ .contains(&OpenSearchEntityType::CalendarEvents)+ {+ let calendar_event_search_args: CalendarEventSearchArgs = args.clone().into();+ let calendar_event_query_builder: CalendarEventQueryBuilder =+ calendar_event_search_args.into();+ let mut calendar_event_bool_query = calendar_event_query_builder.build_bool_query()?;+ calendar_event_bool_query.name(OpenSearchEntityType::CalendarEvents.query_name());+ let query_type: QueryType = calendar_event_bool_query.build().into();+ bool_query.should(query_type.to_owned());+ }+// create the search requestlet mut search_request_builder = SearchRequestBuilder::new();@@ -764,7 +855,7 @@ fn build_unified_search_request(args: &UnifiedSearchArgs) -> Result<SearchReques/// child a parent expands into carries that parent's `entity_id`/`updated_at`,/// so the last expanded hit yields the correct `search_after` for the next page.fn paginate_unified_hits(- hits: Vec<Hit<UnifiedSearchIndex>>,+ hits: Vec<Hit<serde_json::Value>>,page_size: usize,) -> (Vec<SearchHit>, SearchCursorOption) {let has_more = hits.len() > page_size;@@ -772,6 +863,39 @@ fn paginate_unified_hits(let results: Vec<SearchHit> = hits.into_iter().take(page_size)+ .filter_map(|hit| {+ // A hit we cannot place is dropped rather than failing the whole+ // page — but loudly, because it means an index is in the response+ // that the caller never asked for, or a doc no longer matches the+ // shape its index promises.+ let Hit {+ index,+ matched_queries,+ score,+ source,+ highlight,+ inner_hits,+ } = hit;+ match UnifiedSearchIndex::from_matched(&matched_queries, source) {+ Ok(source) => Some(Hit {+ index,+ matched_queries,+ score,+ source,+ highlight,+ inner_hits,+ }),+ Err(error) => {+ tracing::error!(+ error=?error,+ index=%index,+ matched_queries=?matched_queries,+ "dropping unplaceable search hit"+ );+ None+ }+ }+ }).flat_map(expand_hit_into_search_hits).collect();@@ -806,6 +930,13 @@ pub(crate) async fn search_unified(let response = client.search(opensearch::SearchParts::Index(&search_indices))+ // An index that does not exist yet — a newly added entity type before+ // its create-index run, or an alias mid-reindex — otherwise fails the+ // whole request with `index_not_found_exception`, taking down search+ // for every entity rather than just that one. Degrade to the indices+ // that are present; `warn_on_shard_failures` still reports partial+ // results from indices that do exist.+ .ignore_unavailable(true).body(search_request).send().await@@ -819,10 +950,12 @@ pub(crate) async fn search_unified(details: e.to_string(),})?;- let result: DefaultSearchResponse<UnifiedSearchIndex> = serde_json::from_slice(&bytes)- .map_err(|e| OpensearchClientError::SearchDeserializationFailed {- details: e.to_string(),- raw_body: String::from_utf8_lossy(&bytes).to_string(),+ let result: DefaultSearchResponse<serde_json::Value> =+ serde_json::from_slice(&bytes).map_err(|e| {+ OpensearchClientError::SearchDeserializationFailed {+ details: e.to_string(),+ raw_body: String::from_utf8_lossy(&bytes).to_string(),+ }})?;result.warn_on_shard_failures("search_unified");diff --git a/crates/opensearch_client/src/search/unified/test.rs b/crates/opensearch_client/src/search/unified/test.rsindex 671e650689..083f03114c 100644--- a/crates/opensearch_client/src/search/unified/test.rs+++ b/crates/opensearch_client/src/search/unified/test.rs@@ -16,6 +16,8 @@ fn expand_document_name_highlight_yields_name_hit_without_goto() {// A parent document whose name matched: a top-level `document_name`// highlight, no inner_hits (the content branch did not match).let hit = Hit {+ index: "documents_v2".to_string(),+ matched_queries: vec!["documents".to_string()],score: Some(2.0),source: UnifiedSearchIndex::Document(DocumentIndex {entity_id,@@ -435,7 +437,7 @@ fn test_deserialization() -> anyhow::Result<()> {}});- let _: DefaultSearchResponse<UnifiedSearchIndex> = serde_json::from_value(json)?;+ let _: DefaultSearchResponse<serde_json::Value> = serde_json::from_value(json)?;Ok(())}@@ -492,6 +494,7 @@ fn test_build_unified_search_request_content() -> anyhow::Result<()> {},call_record_search_args: UnifiedCallRecordSearchArgs::default(),project_search_args: UnifiedProjectSearchArgs::default(),+ calendar_event_search_args: UnifiedCalendarEventSearchArgs::default(),cursor: SearchCursorOption::NotDone(Some(SearchMethodCursor::UpdatedAt {entity_id,updated_at: time,@@ -503,18 +506,20 @@ fn test_build_unified_search_request_content() -> anyhow::Result<()> {// Documents and chats are join-shape (has_child); compute their// sub-queries from the builders rather than inlining the has_child// JSON. Emails and channels are flat and stay inline below.- let doc_should = QueryType::from(- DocumentQueryBuilder::from(DocumentSearchArgs::from(unified_search_args.clone()))- .build_bool_query()?- .build(),- )- .to_json();- let chat_should = QueryType::from(- ChatQueryBuilder::from(ChatSearchArgs::from(unified_search_args.clone()))- .build_bool_query()?- .build(),- )- .to_json();+ let doc_query_builder =+ DocumentQueryBuilder::from(DocumentSearchArgs::from(unified_search_args.clone()));+ let doc_should = {+ let mut builder = doc_query_builder.build_bool_query()?;+ builder.name(OpenSearchEntityType::Documents.query_name());+ QueryType::from(builder.build()).to_json()+ };+ let chat_query_builder =+ ChatQueryBuilder::from(ChatSearchArgs::from(unified_search_args.clone()));+ let chat_should = {+ let mut builder = chat_query_builder.build_bool_query()?;+ builder.name(OpenSearchEntityType::Chats.query_name());+ QueryType::from(builder.build()).to_json()+ };let expected = serde_json::json!({"collapse": {@@ -666,6 +671,7 @@ fn test_build_unified_search_request_content() -> anyhow::Result<()> {}}],+ "_name": "emails","must": [{"bool": {@@ -720,6 +726,7 @@ fn test_build_unified_search_request_content() -> anyhow::Result<()> {}}],+ "_name": "channels","must": [{"bool": {@@ -794,6 +801,7 @@ fn test_build_unified_search_request_content() -> anyhow::Result<()> {},call_record_search_args: UnifiedCallRecordSearchArgs::default(),project_search_args: UnifiedProjectSearchArgs::default(),+ calendar_event_search_args: UnifiedCalendarEventSearchArgs::default(),cursor: SearchCursorOption::NotDone(Some(SearchMethodCursor::UpdatedAt {entity_id,updated_at: time,@@ -830,12 +838,13 @@ fn test_build_unified_search_request_single_index() -> anyhow::Result<()> {// Documents are join-shape (has_child); compute the sub-query from the// builder rather than inlining the has_child JSON.- let doc_should = QueryType::from(- DocumentQueryBuilder::from(DocumentSearchArgs::from(unified_search_args.clone()))- .build_bool_query()?- .build(),- )- .to_json();+ let doc_query_builder =+ DocumentQueryBuilder::from(DocumentSearchArgs::from(unified_search_args.clone()));+ let doc_should = {+ let mut builder = doc_query_builder.build_bool_query()?;+ builder.name(OpenSearchEntityType::Documents.query_name());+ QueryType::from(builder.build()).to_json()+ };let expected = serde_json::json!({@@ -910,7 +919,7 @@ fn doc_hit_with_chunks(entity_id: uuid::Uuid,updated_at_millis: i64,chunk_count: usize,-) -> Hit<UnifiedSearchIndex> {+) -> Hit<serde_json::Value> {let chunks: Vec<serde_json::Value> = (0..chunk_count).map(|i| {serde_json::json!({@@ -923,13 +932,15 @@ fn doc_hit_with_chunks(.collect();Hit {+ index: "documents_v2".to_string(),+ matched_queries: vec!["documents".to_string()],score: Some(1.0),- source: UnifiedSearchIndex::Document(DocumentIndex {- entity_id,- document_name: "Doc".to_string(),- owner_id: "alice".to_string(),- file_type: "md".to_string(),- updated_at_millis: Some(updated_at_millis),+ source: serde_json::json!({+ "entity_id": entity_id,+ "document_name": "Doc",+ "owner_id": "alice",+ "file_type": "md",+ "updated_at_millis": updated_at_millis,}),highlight: None,inner_hits: Some(serde_json::json!({ "term_0": { "hits": { "hits": chunks } } })),@@ -1025,12 +1036,313 @@ fn paginate_page_size_zero_terminates() {);}+#[test]+fn calendar_source_routes_by_matched_clause_not_shape() -> anyhow::Result<()> {+ // A calendar doc's entity_id + name + owner_id is exactly ProjectIndex's+ // required set, so nothing in the _source distinguishes the two. The clause+ // the hit matched does. Feeding the identical body under two clause names+ // must produce two different variants.+ let source = serde_json::json!({+ "entity_id": "0197a863-0000-7000-8000-0000000000c1",+ "name": "Standup",+ "owner_id": "macro|[email protected]",+ "source_link_id": "0197a863-0000-7000-8000-0000000000c2",+ "ical_uid": "[email protected]",+ "status": "confirmed",+ "is_recurring": true,+ "starts_at_millis": 1783000000000_i64,+ "updated_at_millis": 1783000000000_i64+ });++ assert!(matches!(+ UnifiedSearchIndex::from_matched(&["calendar_events".to_string()], source.clone())?,+ UnifiedSearchIndex::CalendarEvent(_)+ ));+ assert!(+ matches!(+ UnifiedSearchIndex::from_matched(&["projects".to_string()], source)?,+ UnifiedSearchIndex::Project(_)+ ),+ "the same body under a different clause must resolve to that clause's shape"+ );+ Ok(())+}++#[test]+fn an_unknown_clause_is_rejected_not_guessed() {+ let source = serde_json::json!({+ "entity_id": "0197a863-0000-7000-8000-0000000000c1",+ "name": "Something",+ "owner_id": "macro|[email protected]"+ });+ assert!(+ UnifiedSearchIndex::from_matched(&["reminders".to_string()], source.clone()).is_err(),+ "a hit matching no known entity clause must not be filed as some other entity"+ );+ assert!(+ UnifiedSearchIndex::from_matched(&[], source).is_err(),+ "a hit that reported no clause names at all has no discriminator"+ );+}++#[test]+fn calendar_hit_carries_its_title_highlight() -> anyhow::Result<()> {+ // The highlight only arrives because the indexed title field is `name`,+ // which is in the unified request's fixed highlight field list.+ let hits = vec![Hit {+ index: "calendar_events_v1".to_string(),+ matched_queries: vec!["calendar_events".to_string()],+ score: Some(3.1),+ source: serde_json::json!({+ "entity_id": "0197a863-0000-7000-8000-0000000000c1",+ "name": "Standup",+ "owner_id": "macro|[email protected]",+ "source_link_id": "0197a863-0000-7000-8000-0000000000c2",+ "ical_uid": "[email protected]",+ "updated_at_millis": 1783000000000_i64+ }),+ highlight: Some(std::collections::HashMap::from([(+ "name".to_string(),+ vec!["<macro_em>Standup</macro_em>".to_string()],+ )])),+ inner_hits: None,+ }];++ let (hits, _) = paginate_unified_hits(hits, 10);+ assert_eq!(hits.len(), 1);+ assert_eq!(hits[0].entity_type, SearchEntityType::CalendarEvents);+ assert_eq!(+ hits[0].highlight.name.as_deref(),+ Some("<macro_em>Standup</macro_em>")+ );+ Ok(())+}++#[test]+fn an_unplaceable_hit_is_dropped_without_failing_the_page() {+ // One good hit, one from an index we do not know. The page survives.+ let good = Hit {+ index: "projects_v1".to_string(),+ matched_queries: vec!["projects".to_string()],+ score: Some(1.0),+ source: serde_json::json!({+ "entity_id": "0197a863-0000-7000-8000-000000000001",+ "name": "Mobile Redesign",+ "owner_id": "macro|[email protected]",+ "updated_at_millis": 1783000000000_i64+ }),+ highlight: None,+ inner_hits: None,+ };+ let unplaceable = Hit {+ index: "reminders_v1".to_string(),+ matched_queries: vec!["reminders".to_string()],+ score: Some(1.0),+ source: serde_json::json!({ "entity_id": "0197a863-0000-7000-8000-000000000009" }),+ highlight: None,+ inner_hits: None,+ };++ let (hits, _) = paginate_unified_hits(vec![good, unplaceable], 10);+ assert_eq!(hits.len(), 1, "the unplaceable hit is dropped, not fatal");+ assert_eq!(hits[0].entity_type, SearchEntityType::Projects);+}++/// A response mixing every entity type, the way a search across all indices+/// actually comes back. Each hit must land on its own type — this is the case+/// the untagged enum could get wrong and the reason dispatch is explicit.+#[test]+fn a_mixed_response_routes_every_entity_to_its_own_type() -> anyhow::Result<()> {+ fn hit(index: &str, matched: &str, source: serde_json::Value) -> Hit<serde_json::Value> {+ Hit {+ index: index.to_string(),+ matched_queries: vec![matched.to_string()],+ score: Some(1.0),+ source,+ highlight: None,+ inner_hits: None,+ }+ }++ let id = |n: u8| format!("0197a863-0000-7000-8000-0000000000{n:02}");++ let hits = vec![+ hit(+ "channels_v2",+ "channels",+ serde_json::json!({+ "entity_id": id(1), "channel_type": "standard", "org_id": null,+ "message_id": id(2), "thread_id": id(2), "sender_id": "macro|a",+ "mentions": [], "updated_at_millis": 1_783_000_000_000_i64+ }),+ ),+ hit(+ "documents_v2",+ "documents",+ serde_json::json!({+ "entity_id": id(3), "document_name": "Spec", "owner_id": "macro|a",+ "file_type": "md", "updated_at_millis": 1_783_000_000_001_i64+ }),+ ),+ hit(+ "chats_v2",+ "chats",+ serde_json::json!({+ "entity_id": id(4), "user_id": "macro|a", "title": "Chat",+ "updated_at_millis": 1_783_000_000_002_i64+ }),+ ),+ hit(+ "emails_v2",+ "emails",+ serde_json::json!({+ "entity_id": id(5), "message_id": id(6), "sender": "[email protected]",+ "recipients": ["[email protected]"], "cc": [], "bcc": [], "labels": [],+ "link_id": id(7), "user_id": "macro|a", "subject": "Hi",+ "sent_at_millis": 1_783_000_000_003_i64+ }),+ ),+ hit(+ "call_records_v2",+ "call_records",+ serde_json::json!({+ "entity_id": id(8), "channel_id": id(9), "participant_ids": [],+ "name": "Standup call", "started_at_millis": 1_783_000_000_004_i64+ }),+ ),+ hit(+ "projects_v1",+ "projects",+ serde_json::json!({+ "entity_id": id(10), "name": "Redesign", "owner_id": "macro|a",+ "updated_at_millis": 1_783_000_000_005_i64+ }),+ ),+ hit(+ "calendar_events_v1",+ "calendar_events",+ serde_json::json!({+ "entity_id": id(11), "name": "Standup", "owner_id": "macro|a",+ "source_link_id": id(12), "ical_uid": "[email protected]",+ "updated_at_millis": 1_783_000_000_006_i64+ }),+ ),+ ];++ let (results, _) = paginate_unified_hits(hits, 50);++ // Every hit survived: none was dropped as unplaceable.+ assert_eq!(results.len(), 7, "got {results:?}");++ let by_id: std::collections::HashMap<uuid::Uuid, SearchEntityType> = results+ .iter()+ .map(|hit| (hit.entity_id, hit.entity_type.clone()))+ .collect();+ for (n, expected) in [+ (1u8, SearchEntityType::Channels),+ (3, SearchEntityType::Documents),+ (4, SearchEntityType::Chats),+ (5, SearchEntityType::Emails),+ (8, SearchEntityType::CallRecords),+ (10, SearchEntityType::Projects),+ (11, SearchEntityType::CalendarEvents),+ ] {+ let entity_id = id(n).parse::<uuid::Uuid>()?;+ assert_eq!(+ by_id.get(&entity_id),+ Some(&expected),+ "entity {n} routed to the wrong type"+ );+ }++ // The downstream split must bucket them the same way.+ let split = results.into_iter().split_search_response();+ assert_eq!(split.channel_message.len(), 1);+ assert_eq!(split.document.len(), 1);+ assert_eq!(split.chat.len(), 1);+ assert_eq!(split.email.len(), 1);+ assert_eq!(split.call_record.len(), 1);+ assert_eq!(split.project.len(), 1);+ assert_eq!(split.calendar_event.len(), 1);+ Ok(())+}++/// Searching every index must name every clause, or a hit from an unnamed one+/// would come back with no discriminator and be dropped.+#[test]+fn every_searched_index_contributes_a_named_clause() -> anyhow::Result<()> {+ use strum::IntoEnumIterator as _;++ let args = UnifiedSearchArgs {+ search_indices: OpenSearchEntityType::iter().collect(),+ user_id: "user".to_string(),+ page_size: 10,+ match_type: "exact".to_string(),+ document_search_args: UnifiedDocumentSearchArgs {+ terms: vec!["test".to_string()],+ ..Default::default()+ },+ email_search_args: UnifiedEmailSearchArgs {+ terms: vec!["test".to_string()],+ ..Default::default()+ },+ channel_message_search_args: UnifiedChannelMessageSearchArgs {+ terms: vec!["test".to_string()],+ channel_ids: vec!["c1".to_string()],+ ..Default::default()+ },+ chat_search_args: UnifiedChatSearchArgs {+ terms: vec!["test".to_string()],+ ..Default::default()+ },+ call_record_search_args: UnifiedCallRecordSearchArgs {+ terms: vec!["test".to_string()],+ call_ids: vec!["r1".to_string()],+ ids_only: true,+ ..Default::default()+ },+ project_search_args: UnifiedProjectSearchArgs {+ terms: vec!["test".to_string()],+ ..Default::default()+ },+ calendar_event_search_args: UnifiedCalendarEventSearchArgs {+ terms: vec!["test".to_string()],+ ..Default::default()+ },+ ..Default::default()+ };++ let json = build_unified_search_request(&args)?.to_json();+ let should = json["query"]["bool"]["should"]+ .as_array()+ .expect("a should clause per searched index");++ let names: std::collections::HashSet<&str> = should+ .iter()+ .filter_map(|clause| clause["bool"]["_name"].as_str())+ .collect();++ for entity in OpenSearchEntityType::iter() {+ assert!(+ names.contains(entity.query_name()),+ "{} contributed an unnamed clause; its hits would be undispatchable",+ entity.query_name()+ );+ }+ assert_eq!(+ names.len(),+ should.len(),+ "every should clause must be named, got {names:?} across {} clauses",+ should.len()+ );+ Ok(())+}+#[test]fn project_hit_deserializes_and_converts() -> anyhow::Result<()> {- // A project doc as the flat projects index returns it. With the untagged- // UnifiedSearchIndex enum the Project variant must win: no other variant's- // required fields (document_name, title, message_id, channel_id, …) are- // present in a project _source.+ // A project doc as the flat projects index returns it, parsed the way the+ // client parses a real response: raw `_source`, then dispatched on+ // `_index`.let json = serde_json::json!({"took": 3,"timed_out": false,@@ -1054,6 +1366,7 @@ fn project_hit_deserializes_and_converts() -> anyhow::Result<()> {{ "definition_id": "0197a863-0000-7000-8000-00000000000a", "values": ["opt-1"] }]},+ "matched_queries": ["projects"],"highlight": {"name": ["<macro_em>Mobile</macro_em> Redesign"]}@@ -1062,10 +1375,13 @@ fn project_hit_deserializes_and_converts() -> anyhow::Result<()> {}});- let result: DefaultSearchResponse<UnifiedSearchIndex> = serde_json::from_value(json)?;+ let result: DefaultSearchResponse<serde_json::Value> = serde_json::from_value(json)?;assert_eq!(result.hits.hits.len(), 1);assert!(matches!(- result.hits.hits[0].source,+ UnifiedSearchIndex::from_matched(+ &result.hits.hits[0].matched_queries,+ result.hits.hits[0].source.clone()+ )?,UnifiedSearchIndex::Project(_)));diff --git a/crates/opensearch_client/src/upsert.rs b/crates/opensearch_client/src/upsert.rsindex c22417da01..ef561bc25c 100644--- a/crates/opensearch_client/src/upsert.rs+++ b/crates/opensearch_client/src/upsert.rs@@ -1,3 +1,4 @@+pub mod calendar_event;pub mod call_record;pub mod channel_message;pub mod chat_message;diff --git a/crates/opensearch_client/src/upsert/calendar_event.rs b/crates/opensearch_client/src/upsert/calendar_event.rsnew file mode 100644index 0000000000..b17b3dbccc--- /dev/null+++ b/crates/opensearch_client/src/upsert/calendar_event.rs@@ -0,0 +1,189 @@+use models_opensearch::SearchIndex;++use super::properties::IndexedProperty;+use crate::{Result, date_format::EpochMillis, error::OpensearchClientError};++/// The arguments for upserting a calendar event into the opensearch index.+///+/// The calendar events index is flat: one doc per **series master** (a+/// `calendar_events` row), `_id` = event id. Recurring instances are not+/// indexed — they are materialized only inside a rolling window+/// (`calendars.materialized_range`), so a per-occurrence doc would make a+/// recurring event searchable for that slice alone and would need rewriting+/// as the window rolls. Search resolves a contextual occurrence at+/// enrichment time instead.+#[derive(Debug, serde::Serialize)]+pub struct UpsertCalendarEventArgs {+ /// The id of the calendar event entity+ #[serde(rename = "entity_id")]+ pub event_id: String,+ /// The series title. Indexed as `name`: the unified search request+ /// highlights a fixed set of field names, and `name` is the one every+ /// other flat index uses for its title.+ #[serde(rename = "name")]+ pub title: String,+ /// Owner of this per-user event projection+ pub owner_id: String,+ /// The inbox link the canonical source belongs to. Delegated access is+ /// granted through this, mirroring the soup access predicate.+ pub source_link_id: String,+ /// RFC 5545 UID shared across every attendee's projection of the meeting+ pub ical_uid: String,+ /// Canonical status (`confirmed`, `tentative`, `cancelled`)+ pub status: String,+ /// Whether the series carries any recurrence rule. Lets a caller tell a+ /// recurring row from a one-off without reading the rules themselves.+ pub is_recurring: bool,+ /// Master start, in milliseconds. Absent for an all-day series.+ #[serde(skip_serializing_if = "Option::is_none")]+ pub starts_at_millis: Option<EpochMillis>,+ /// Master end, in milliseconds. Absent for an all-day series.+ #[serde(skip_serializing_if = "Option::is_none")]+ pub ends_at_millis: Option<EpochMillis>,+ /// Inclusive local start date of an all-day series, as `YYYY-MM-DD`.+ #[serde(skip_serializing_if = "Option::is_none")]+ pub start_date: Option<String>,+ /// Exclusive local end date of an all-day series, as `YYYY-MM-DD`.+ #[serde(skip_serializing_if = "Option::is_none")]+ pub end_date: Option<String>,+ /// Organizer email, for the organizer filter+ #[serde(skip_serializing_if = "Option::is_none")]+ pub organizer_email: Option<String>,+ /// Attendee emails, for the attendee filter+ pub attendee_emails: Vec<String>,+ /// The created at time of the event, in milliseconds+ pub created_at_millis: EpochMillis,+ /// The updated at time of the event, in milliseconds+ pub updated_at_millis: EpochMillis,+ /// Entity properties (tags, custom) used for search filtering.+ pub properties: Vec<IndexedProperty>,+}++/// Resolve `index_override` to the physical/alias name we'll write to.+fn resolve_destination(index_override: Option<&str>) -> &str {+ index_override.unwrap_or(SearchIndex::CalendarEvents.as_ref())+}++/// Upsert a single calendar event doc. Full-overwrite `index` semantics so+/// omitted optional fields (e.g. `organizer_email`) get cleared on+/// Some→None transitions.+#[tracing::instrument(skip(client, args), fields(event_id=%args.event_id), err)]+pub(crate) async fn upsert_calendar_event(+ client: &opensearch::OpenSearch,+ args: &UpsertCalendarEventArgs,+ index_override: Option<&str>,+) -> Result<()> {+ let index = resolve_destination(index_override);+ let body = serde_json::to_value(args).map_err(|err| OpensearchClientError::Unknown {+ details: err.to_string(),+ method: Some("upsert_calendar_event".to_string()),+ })?;++ let response = client+ .index(opensearch::IndexParts::IndexId(index, &args.event_id))+ .body(body)+ .send()+ .await+ .map_err(|err| OpensearchClientError::Unknown {+ details: err.to_string(),+ method: Some("upsert_calendar_event".to_string()),+ })?;++ let status_code = response.status_code();+ if status_code.is_success() {+ tracing::trace!(event_id=%args.event_id, "calendar event upserted successfully");+ return Ok(());+ }++ let body =+ response+ .text()+ .await+ .map_err(|err| OpensearchClientError::DeserializationFailed {+ details: err.to_string(),+ method: Some("upsert_calendar_event".to_string()),+ })?;++ tracing::error!(+ status_code=?status_code,+ body=?body,+ event_id=%args.event_id,+ "error upserting calendar event",+ );++ Err(OpensearchClientError::Unknown {+ details: body,+ method: Some("upsert_calendar_event".to_string()),+ })+}++/// Update only the denormalized `properties` on an existing calendar event+/// doc. A missing doc (404) is a no-op — the next full upsert carries them.+pub(crate) async fn update_calendar_event_properties(+ client: &opensearch::OpenSearch,+ event_id: &str,+ properties: &[IndexedProperty],+ index_override: Option<&str>,+) -> Result<()> {+ use serde_json::json;++ let index = resolve_destination(index_override);+ let properties_value =+ serde_json::to_value(properties).map_err(|err| OpensearchClientError::Unknown {+ details: err.to_string(),+ method: Some("update_calendar_event_properties".to_string()),+ })?;+ let body = json!({ "doc": { "properties": properties_value } });++ let response = client+ .update(opensearch::UpdateParts::IndexId(index, event_id))+ .body(body)+ .send()+ .await+ .map_err(|err| OpensearchClientError::DeserializationFailed {+ details: err.to_string(),+ method: Some("update_calendar_event_properties".to_string()),+ })?;++ let status_code = response.status_code();+ if status_code.is_success() {+ tracing::trace!(event_id=%event_id, "calendar event properties updated");+ return Ok(());+ }+ let body =+ response+ .text()+ .await+ .map_err(|err| OpensearchClientError::DeserializationFailed {+ details: err.to_string(),+ method: Some("update_calendar_event_properties".to_string()),+ })?;++ // A *missing document* 404 is a no-op: the doc isn't indexed yet, so the+ // next full upsert will include its properties. A *missing index* 404+ // (`index_not_found_exception`) is a real outage and must propagate.+ if status_code.as_u16() == 404 {+ let error_type = serde_json::from_str::<serde_json::Value>(&body)+ .ok()+ .and_then(|value| value["error"]["type"].as_str().map(str::to_owned));+ if error_type.as_deref() == Some("document_missing_exception") {+ tracing::debug!(+ event_id=%event_id,+ "calendar event not indexed yet; skipping property update"+ );+ return Ok(());+ }+ }++ tracing::error!(+ status_code=?status_code,+ body=?body,+ event_id=%event_id,+ "error updating calendar event properties",+ );++ Err(OpensearchClientError::Unknown {+ details: body,+ method: Some("update_calendar_event_properties".to_string()),+ })+}diff --git a/crates/opensearch_query_builder/src/query/bool.rs b/crates/opensearch_query_builder/src/query/bool.rsindex 2a9a09d137..9138c92a79 100644--- a/crates/opensearch_query_builder/src/query/bool.rs+++ b/crates/opensearch_query_builder/src/query/bool.rs@@ -26,6 +26,11 @@ pub struct BoolQuery<'a> {/// Boost#[serde(skip_serializing_if = "Option::is_none")]pub boost: Option<f64>,+ /// Clause name. OpenSearch echoes the names of every named clause a hit+ /// matched back in that hit's `matched_queries`, which lets a caller tell+ /// which branch of a disjunction produced a result.+ #[serde(rename = "_name", skip_serializing_if = "Option::is_none")]+ pub name: Option<Cow<'a, str>>,}impl<'a> BoolQuery<'a> {@@ -70,6 +75,12 @@ impl<'a> BoolQuery<'a> {self}+ /// Name this clause so hits it matches report it in `matched_queries`.+ pub fn name(mut self, name: impl Into<Cow<'a, str>>) -> Self {+ self.name = Some(name.into());+ self+ }+/// Convert to an owned version with 'static lifetimepub fn to_owned(&self) -> BoolQuery<'static> {BoolQuery {@@ -79,6 +90,7 @@ impl<'a> BoolQuery<'a> {filter: Cow::Owned(self.filter.iter().map(|q| q.to_owned()).collect()),minimum_should_match: self.minimum_should_match,boost: self.boost,+ name: self.name.as_ref().map(|n| Cow::Owned(n.to_string())),}}}@@ -124,6 +136,10 @@ impl<'a> ToOpenSearchJson for BoolQuery<'a> {bool_obj.insert("boost".to_string(), boost.into());}+ if let Some(name) = &self.name {+ bool_obj.insert("_name".to_string(), Value::String(name.to_string()));+ }+let mut result = Map::new();result.insert("bool".to_string(), Value::Object(bool_obj));Value::Object(result)@@ -139,6 +155,7 @@ pub struct BoolQueryBuilder<'a> {filter: Cow<'a, [QueryType<'a>]>,minimum_should_match: Option<i32>,boost: Option<f64>,+ name: Option<Cow<'a, str>>,}impl<'a> BoolQueryBuilder<'a> {@@ -189,6 +206,12 @@ impl<'a> BoolQueryBuilder<'a> {self}+ /// Name this clause so hits it matches report it in `matched_queries`.+ pub fn name(&mut self, name: impl Into<Cow<'a, str>>) -> &mut Self {+ self.name = Some(name.into());+ self+ }+/// Build the final BoolQuerypub fn build(self) -> BoolQuery<'a> {BoolQuery {@@ -198,6 +221,7 @@ impl<'a> BoolQueryBuilder<'a> {filter: self.filter,minimum_should_match: self.minimum_should_match,boost: self.boost,+ name: self.name,}}}diff --git a/crates/search_service/src/api/context.rs b/crates/search_service/src/api/context.rsindex 01bb965183..6ea7f9402d 100644--- a/crates/search_service/src/api/context.rs+++ b/crates/search_service/src/api/context.rs@@ -27,4 +27,9 @@ pub struct SearchHandlerState {pub entity_access_service: Arc<SearchEntityAccessService>,/// Authorization state used to authenticate search requests.pub authorization_state: MacroAuthorizationState<SearchAuthorizationService>,+ /// Whether calendar events participate in search. Off in deployed+ /// environments until the calendar index has been created and backfilled;+ /// gating here covers every caller of the search API at once, the AI+ /// search tools included.+ pub calendar_search_enabled: bool,}diff --git a/crates/search_service/src/api/search.rs b/crates/search_service/src/api/search.rsindex 6d4434eb5d..2ac83f04fc 100644--- a/crates/search_service/src/api/search.rs+++ b/crates/search_service/src/api/search.rs@@ -2,6 +2,7 @@ use axum::{Router, routing::post};use crate::api::context::SearchHandlerState;+pub(in crate::api) mod calendar_event;pub(in crate::api) mod call_record;pub(in crate::api) mod channel;pub(in crate::api) mod chat;diff --git a/crates/search_service/src/api/search/calendar_event.rs b/crates/search_service/src/api/search/calendar_event.rsnew file mode 100644index 0000000000..fc340d4fa3--- /dev/null+++ b/crates/search_service/src/api/search/calendar_event.rs@@ -0,0 +1,184 @@+use crate::api::search::simple::SearchError;+use indexmap::IndexMap;+use macro_db_client::calendar_event::get_events_for_search::{+ CalendarEventSearchInfo, get_calendar_events_for_search,+};+use macro_user_id::user_id::MacroUserIdStr;+use models_properties::{EntityReference, EntityType};+use models_search::calendar_event::{+ CalendarEventMetadata, CalendarEventSearchOccurrence, CalendarEventSearchResponseItem,+ CalendarEventSearchResponseItemWithMetadata, CalendarEventSearchResult,+ CalendarEventSearchTime,+};+use models_soup::SoupProperty;+use sqlx::types::Uuid;+use std::collections::HashMap;+use system_properties::SystemPropertyKey;++use crate::api::context::SearchHandlerState;++/// Enriches calendar event search results with metadata and the instance each+/// row points at.+#[tracing::instrument(skip(ctx, results), err)]+pub(in crate::api::search) async fn enrich_calendar_events(+ ctx: &SearchHandlerState,+ user_id: &str,+ results: Vec<opensearch_client::search::model::SearchHit>,+) -> Result<Vec<CalendarEventSearchResponseItemWithMetadata>, SearchError> {+ let results: Vec<opensearch_client::search::model::SearchHit> = results+ .into_iter()+ .filter(|r| r.entity_type == models_opensearch::SearchEntityType::CalendarEvents)+ .collect();++ if results.is_empty() {+ return Ok(vec![]);+ }++ let event_ids: Vec<Uuid> = results.iter().map(|r| r.entity_id).collect();++ // Re-reads visibility from Postgres rather than trusting the index: an+ // event the caller lost access to since it was indexed is absent here and+ // its hit is dropped below.+ let events = get_calendar_events_for_search(&ctx.db, user_id, &event_ids, chrono::Utc::now())+ .await+ .map_err(SearchError::InternalError)?;++ // Fetch event properties (e.g. tags) so rows can render and re-check+ // them, mirroring the projects enrichment.+ let entity_refs: Vec<EntityReference> = event_ids+ .iter()+ .map(|id| EntityReference::new(id.to_string(), EntityType::CalendarEvent))+ .collect();+ // Scope tags to the viewer so another user's personal tags never leak into+ // the response. System properties still ride along via the key list.+ let viewer_user_id = MacroUserIdStr::parse_from_str(user_id)+ .map_err(|_| SearchError::InvalidUserId(user_id.to_string()))?;+ let properties_map: HashMap<String, Vec<SoupProperty>> =+ properties::outbound::entity_properties_get_query::get_bulk_entity_properties_values_filtered(+ &ctx.db,+ &entity_refs,+ SystemPropertyKey::all_system_property_keys(),+ Some(&viewer_user_id),+ )+ .await+ .inspect_err(|e| tracing::error!(error=?e, "failed to fetch calendar event properties"))+ .unwrap_or_default()+ .into_iter()+ .map(|(id, props)| {+ (+ id,+ props+ .into_iter()+ .map(SoupProperty::from)+ .collect::<Vec<_>>(),+ )+ })+ .collect();++ Ok(construct_search_result(results, events, properties_map))+}++/// Read a timed or all-day span out of the four nullable columns the+/// `calendar_events` time-shape constraint keeps mutually exclusive.+fn to_search_time(+ starts_at: Option<chrono::DateTime<chrono::Utc>>,+ ends_at: Option<chrono::DateTime<chrono::Utc>>,+ start_date: Option<chrono::NaiveDate>,+ end_date: Option<chrono::NaiveDate>,+ time_zone: Option<String>,+) -> Option<CalendarEventSearchTime> {+ match (starts_at, ends_at, start_date, end_date) {+ (Some(starts_at), Some(ends_at), None, None) => Some(CalendarEventSearchTime::Timed {+ starts_at,+ ends_at,+ time_zone,+ }),+ (None, None, Some(start_date), Some(end_date)) => Some(CalendarEventSearchTime::AllDay {+ start_date,+ end_date,+ }),+ _ => None,+ }+}++/// Build the resolved-occurrence payload, if the lateral found one and its+/// span reads cleanly.+fn to_occurrence(info: &CalendarEventSearchInfo) -> Option<CalendarEventSearchOccurrence> {+ let occurrence_key = info.occurrence_key.clone()?;+ let time = to_search_time(+ info.occurrence_starts_at,+ info.occurrence_ends_at,+ info.occurrence_start_date,+ info.occurrence_end_date,+ info.time_zone.clone(),+ )?;+ Some(CalendarEventSearchOccurrence {+ occurrence_key,+ time,+ })+}++pub fn construct_search_result(+ search_results: Vec<opensearch_client::search::model::SearchHit>,+ events: HashMap<Uuid, CalendarEventSearchInfo>,+ mut properties_map: HashMap<String, Vec<SoupProperty>>,+) -> Vec<CalendarEventSearchResponseItemWithMetadata> {+ // construct entity hit map of id -> vec<hits> using IndexMap to preserve+ // insertion order+ let entity_id_hit_map: IndexMap<Uuid, Vec<CalendarEventSearchResult>> = search_results+ .into_iter()+ .map(|hit| {+ let result = CalendarEventSearchResult {+ highlight: hit.highlight.into(),+ score: hit.score,+ };+ (hit.entity_id, result)+ })+ .fold(IndexMap::new(), |mut map, (entity_id, result)| {+ map.entry(entity_id).or_insert_with(Vec::new).push(result);+ map+ });++ entity_id_hit_map+ .into_iter()+ .filter_map(|(entity_id, hits)| {+ let info = events.get(&entity_id)?;+ // An event whose stored span satisfies neither leg of the+ // time-shape constraint cannot be rendered on a calendar row, so+ // it is dropped rather than surfaced without a date.+ let time = to_search_time(+ info.starts_at,+ info.ends_at,+ info.start_date,+ info.end_date,+ info.time_zone.clone(),+ )?;+ Some(CalendarEventSearchResponseItemWithMetadata {+ metadata: Some(CalendarEventMetadata {+ created_at: info.created_at,+ updated_at: info.updated_at,+ status: info.status.clone(),+ time,+ is_recurring: info.is_recurring,+ occurrence: to_occurrence(info),+ conference_url: info.conference_url.clone(),+ is_read_only: info.is_read_only,+ }),+ properties: properties_map+ .remove(&entity_id.to_string())+ .filter(|p| !p.is_empty()),+ extra: CalendarEventSearchResponseItem {+ id: entity_id,+ name: info.title.clone(),+ owner_id: info.owner_id.clone(),+ updated_at: info.updated_at,+ created_at: info.created_at,+ calendar_event_search_results: hits,+ },+ })+ })+ .collect()+}++#[cfg(test)]+mod test;diff --git a/crates/search_service/src/api/search/calendar_event/test.rs b/crates/search_service/src/api/search/calendar_event/test.rsnew file mode 100644index 0000000000..dd94cbd062--- /dev/null+++ b/crates/search_service/src/api/search/calendar_event/test.rs@@ -0,0 +1,164 @@+use super::*;+use chrono::{NaiveDate, TimeZone, Utc};+use opensearch_client::search::model::{Highlight, SearchHit};++fn hit(entity_id: Uuid) -> SearchHit {+ SearchHit {+ entity_id,+ entity_type: models_opensearch::SearchEntityType::CalendarEvents,+ score: Some(1.0),+ highlight: Highlight::default(),+ goto: None,+ updated_at: None,+ }+}++fn timed_series(id: Uuid) -> CalendarEventSearchInfo {+ CalendarEventSearchInfo {+ id,+ owner_id: "user123".to_string(),+ title: "Standup".to_string(),+ status: "confirmed".to_string(),+ starts_at: Some(Utc.with_ymd_and_hms(2024, 1, 3, 10, 0, 0).unwrap()),+ ends_at: Some(Utc.with_ymd_and_hms(2024, 1, 3, 10, 15, 0).unwrap()),+ start_date: None,+ end_date: None,+ time_zone: Some("America/New_York".to_string()),+ is_recurring: true,+ conference_url: None,+ is_read_only: false,+ created_at: Utc.with_ymd_and_hms(2024, 1, 1, 0, 0, 0).unwrap(),+ updated_at: Utc.with_ymd_and_hms(2024, 1, 2, 0, 0, 0).unwrap(),+ occurrence_key: None,+ occurrence_starts_at: None,+ occurrence_ends_at: None,+ occurrence_start_date: None,+ occurrence_end_date: None,+ }+}++#[test]+fn resolved_occurrence_rides_on_the_row() {+ let id = Uuid::new_v4();+ let mut info = timed_series(id);+ // The lateral resolved next Tuesday rather than the series' 2024 master.+ info.occurrence_key = Some("2026-08-25T10:00:00+00:00".to_string());+ info.occurrence_starts_at = Some(Utc.with_ymd_and_hms(2026, 8, 25, 10, 0, 0).unwrap());+ info.occurrence_ends_at = Some(Utc.with_ymd_and_hms(2026, 8, 25, 10, 15, 0).unwrap());++ let results =+ construct_search_result(vec![hit(id)], HashMap::from([(id, info)]), HashMap::new());++ assert_eq!(results.len(), 1);+ let metadata = results[0].metadata.as_ref().expect("metadata");+ assert!(metadata.is_recurring);+ let occurrence = metadata.occurrence.as_ref().expect("resolved occurrence");+ assert_eq!(occurrence.occurrence_key, "2026-08-25T10:00:00+00:00");+ match &occurrence.time {+ CalendarEventSearchTime::Timed { starts_at, .. } => {+ assert_eq!(+ *starts_at,+ Utc.with_ymd_and_hms(2026, 8, 25, 10, 0, 0).unwrap()+ );+ }+ other => panic!("expected a timed occurrence, got {other:?}"),+ }+ // The master span still rides along so a client can fall back to it.+ match &metadata.time {+ CalendarEventSearchTime::Timed { starts_at, .. } => {+ assert_eq!(+ *starts_at,+ Utc.with_ymd_and_hms(2024, 1, 3, 10, 0, 0).unwrap()+ );+ }+ other => panic!("expected a timed master, got {other:?}"),+ }+}++#[test]+fn series_with_no_materialized_occurrence_keeps_its_row() {+ // Occurrences live only inside a rolling window, so a series can resolve+ // to none. The row must still render, falling back to the master span.+ let id = Uuid::new_v4();+ let results = construct_search_result(+ vec![hit(id)],+ HashMap::from([(id, timed_series(id))]),+ HashMap::new(),+ );++ assert_eq!(results.len(), 1);+ let metadata = results[0].metadata.as_ref().expect("metadata");+ assert!(+ metadata.occurrence.is_none(),+ "no occurrence should be reported when none is materialized"+ );+ assert!(matches!(+ metadata.time,+ CalendarEventSearchTime::Timed { .. }+ ));+}++#[test]+fn all_day_series_reads_the_date_pair() {+ let id = Uuid::new_v4();+ let mut info = timed_series(id);+ info.starts_at = None;+ info.ends_at = None;+ info.start_date = Some(NaiveDate::from_ymd_opt(2026, 8, 25).unwrap());+ info.end_date = Some(NaiveDate::from_ymd_opt(2026, 8, 26).unwrap());++ let results =+ construct_search_result(vec![hit(id)], HashMap::from([(id, info)]), HashMap::new());++ let metadata = results[0].metadata.as_ref().expect("metadata");+ match &metadata.time {+ CalendarEventSearchTime::AllDay {+ start_date,+ end_date,+ } => {+ assert_eq!(*start_date, NaiveDate::from_ymd_opt(2026, 8, 25).unwrap());+ assert_eq!(*end_date, NaiveDate::from_ymd_opt(2026, 8, 26).unwrap());+ }+ other => panic!("expected an all-day master, got {other:?}"),+ }+}++#[test]+fn hit_the_caller_cannot_see_is_dropped() {+ // The visibility query is the authority: an id absent from its result set+ // is one the caller lost access to since it was indexed.+ let id = Uuid::new_v4();+ let results = construct_search_result(vec![hit(id)], HashMap::new(), HashMap::new());+ assert!(+ results.is_empty(),+ "a hit with no visible row must not reach the response"+ );+}++#[test]+fn unreadable_span_is_dropped_rather_than_dated_wrong() {+ let id = Uuid::new_v4();+ let mut info = timed_series(id);+ info.starts_at = None;+ info.ends_at = None;+ // Neither leg of the time-shape constraint is satisfied.+ let results =+ construct_search_result(vec![hit(id)], HashMap::from([(id, info)]), HashMap::new());+ assert!(results.is_empty());+}++#[test]+fn multiple_hits_for_one_event_collapse_into_one_row() {+ let id = Uuid::new_v4();+ let results = construct_search_result(+ vec![hit(id), hit(id)],+ HashMap::from([(id, timed_series(id))]),+ HashMap::new(),+ );+ assert_eq!(results.len(), 1, "one row per event entity");+ assert_eq!(+ results[0].extra.calendar_event_search_results.len(),+ 2,+ "both hits are kept on that row"+ );+}diff --git a/crates/search_service/src/api/search/enrich.rs b/crates/search_service/src/api/search/enrich.rsindex 8679ca54c4..b408b78925 100644--- a/crates/search_service/src/api/search/enrich.rs+++ b/crates/search_service/src/api/search/enrich.rs@@ -7,9 +7,9 @@ use opensearch_client::search::model::SearchHit;use crate::api::{context::SearchHandlerState,search::{- call_record::enrich_call_records, channel::enrich_channel_messages, chat::enrich_chats,- document::enrich_documents, email::enrich_emails, project::enrich_projects,- simple::SearchError,+ calendar_event::enrich_calendar_events, call_record::enrich_call_records,+ channel::enrich_channel_messages, chat::enrich_chats, document::enrich_documents,+ email::enrich_emails, project::enrich_projects, simple::SearchError,},};@@ -65,6 +65,13 @@ pub async fn enrich_search_response(.map(UnifiedSearchResponseItem::Call).collect())}+ SearchEntityType::CalendarEvents => {+ let response = enrich_calendar_events(ctx, user_id, results).await?;+ Ok(response+ .into_iter()+ .map(UnifiedSearchResponseItem::CalendarEvent)+ .collect())+ }// CRM companies are enriched separately (they need the team// receipt) via `crm_company::enrich_crm_companies`.SearchEntityType::CrmCompanies => Ok(vec![]),diff --git a/crates/search_service/src/api/search/simple.rs b/crates/search_service/src/api/search/simple.rsindex 2d0b4ca2c8..b196a478ef 100644--- a/crates/search_service/src/api/search/simple.rs+++ b/crates/search_service/src/api/search/simple.rs@@ -10,6 +10,7 @@ use name_search::NameSearchError;use opensearch_client::error::OpensearchClientError;pub(in crate::api) mod filter;+pub(in crate::api) mod simple_calendar_event;pub(in crate::api) mod simple_call_record;pub(in crate::api) mod simple_channel;pub(in crate::api) mod simple_chat;diff --git a/crates/search_service/src/api/search/simple/filter.rs b/crates/search_service/src/api/search/simple/filter.rsindex 16e52de1ed..6a3e98836c 100644--- a/crates/search_service/src/api/search/simple/filter.rs+++ b/crates/search_service/src/api/search/simple/filter.rs@@ -3,14 +3,16 @@use comms_db_client::model::SimpleMention;use opensearch_client::search::unified::{- UnifiedCallRecordSearchArgs, UnifiedChannelMessageSearchArgs, UnifiedChatSearchArgs,- UnifiedDocumentSearchArgs, UnifiedEmailSearchArgs, UnifiedProjectSearchArgs,+ UnifiedCalendarEventSearchArgs, UnifiedCallRecordSearchArgs, UnifiedChannelMessageSearchArgs,+ UnifiedChatSearchArgs, UnifiedDocumentSearchArgs, UnifiedEmailSearchArgs,+ UnifiedProjectSearchArgs,};use crate::api::{context::SearchHandlerState,search::simple::{- SearchError, simple_call_record::filter_calls, simple_channel::filter_channels,+ SearchError, simple_calendar_event::filter_calendar_events,+ simple_call_record::filter_calls, simple_channel::filter_channels,simple_chat::filter_chats, simple_document::filter_documents,simple_project::filter_projects,},@@ -197,3 +199,31 @@ impl FilterVariantToSearchArgs for item_filters::CallFilters {})}}++impl FilterVariantToSearchArgs for item_filters::CalendarEventFilters {+ type Output = UnifiedCalendarEventSearchArgs;++ async fn filter_to_search_args(+ &self,+ ctx: &SearchHandlerState,+ user_id: &str,+ _user_organization_id: Option<i32>,+ should_include: bool,+ ) -> Result<Self::Output, SearchError> {+ if !should_include {+ return Ok(UnifiedCalendarEventSearchArgs::default());+ }++ let response = filter_calendar_events(ctx, user_id, self).await?;++ Ok(UnifiedCalendarEventSearchArgs {+ calendar_event_ids: response.calendar_event_ids,+ link_ids: response.link_ids,+ ids_only: response.ids_only,+ statuses: self.statuses.clone(),+ organizer_emails: self.organizers.clone(),+ attendee_emails: self.attendees.clone(),+ ..Default::default()+ })+ }+}diff --git a/crates/search_service/src/api/search/simple/simple_calendar_event.rs b/crates/search_service/src/api/search/simple/simple_calendar_event.rsnew file mode 100644index 0000000000..07f1cb7668--- /dev/null+++ b/crates/search_service/src/api/search/simple/simple_calendar_event.rs@@ -0,0 +1,53 @@+use crate::api::search::simple::SearchError;+use item_filters::CalendarEventFilters;+use uuid::Uuid;++use crate::api::context::SearchHandlerState;++#[derive(Debug)]+pub(in crate::api::search) struct FilterCalendarEventResponse {+ pub calendar_event_ids: Vec<String>,+ /// Every inbox link the caller can read, resolved server-side.+ pub link_ids: Vec<String>,+ pub ids_only: bool,+}++/// Resolve what the caller may search over.+///+/// Access is resolved here rather than taken from the request: an event is+/// visible when the caller owns its projection or the event's source link is+/// one of their inboxes (own or delegated). `fetch_inboxes_for_macro_id` is+/// the authority for that set, so a caller cannot widen their own scope by+/// passing link ids.+///+/// `ids_only` is set only when the caller named specific event ids. Otherwise+/// the query stays scoped by owner/link, because an empty id list under+/// `ids_only` would be rejected rather than matching everything.+#[tracing::instrument(skip(ctx, filters), err)]+pub(in crate::api::search) async fn filter_calendar_events(+ ctx: &SearchHandlerState,+ user_id: &str,+ filters: &CalendarEventFilters,+) -> Result<FilterCalendarEventResponse, SearchError> {+ let inboxes = email_db_client::links::get::fetch_inboxes_for_macro_id(&ctx.db, user_id)+ .await+ .map_err(SearchError::InternalError)?;+ let link_ids: Vec<String> = inboxes.iter().map(|l| l.id.to_string()).collect();++ // Keep only well-formed ids: the index stores `entity_id` as a keyword, so+ // a malformed value would silently match nothing rather than error.+ let calendar_event_ids: Vec<String> = filters+ .calendar_event_ids+ .iter()+ .filter(|id| Uuid::parse_str(id).is_ok())+ .cloned()+ .collect();++ let ids_only = !filters.calendar_event_ids.is_empty();++ Ok(FilterCalendarEventResponse {+ calendar_event_ids,+ link_ids,+ ids_only,+ })+}diff --git a/crates/search_service/src/api/search/simple/simple_unified.rs b/crates/search_service/src/api/search/simple/simple_unified.rsindex 7cb7b59a18..9d18f88275 100644--- a/crates/search_service/src/api/search/simple/simple_unified.rs+++ b/crates/search_service/src/api/search/simple/simple_unified.rs@@ -26,6 +26,16 @@ use opensearch_client::search::documents::{DocumentSearchMode, PropertyFilterArguse opensearch_client::search::model::SearchHit;use opensearch_client::search::unified::UnifiedSearchArgs;+/// Whether calendar events may appear in this search.+///+/// The flag overrides the request, never the reverse. The AI search tools+/// default `entityTypes` to empty — which means every type — so an environment+/// whose calendar index has not been created and backfilled cannot rely on+/// callers opting out.+fn calendar_events_searchable(requested: bool, flag_enabled: bool) -> bool {+ requested && flag_enabled+}+/// Identifies the source of a search result for cursor regeneration#[derive(Debug, Clone, Copy, PartialEq, Eq)]enum SearchSource {@@ -256,16 +266,22 @@ pub(in crate::api::search) async fn perform_unified_search(let doc_filters = search_filters.document_filters;let project_filters = search_filters.project_filters;let call_filters = search_filters.call_filters;+ let calendar_event_filters = search_filters.calendar_event_filters;let should_include_documents = search_filters.should_include_documents;// A tag filter drops channels (and CRM, below) because they aren't// tag-indexed. Every tag-indexed source — documents, chats, projects,- // emails, call records — stays included and applies the filter itself.+ // emails, call records, calendar events — stays included and applies the+ // filter itself.let should_include_channels = search_filters.should_include_channels && !tags_active;let should_include_chats = search_filters.should_include_chats;let should_include_projects = search_filters.should_include_projects;let should_include_emails = search_filters.should_include_emails;let should_include_call_records = search_filters.should_include_call_records;+ let should_include_calendar_events = calendar_events_searchable(+ search_filters.should_include_calendar_events,+ ctx.calendar_search_enabled,+ );let email_terms = search_terms.clone();// Await all tasks in parallel@@ -276,6 +292,7 @@ pub(in crate::api::search) async fn perform_unified_search(mut filter_email_response,mut filter_project_response,mut filter_call_record_response,+ mut filter_calendar_event_response,) = tokio::try_join!(doc_filters.filter_to_search_args(ctx,@@ -312,6 +329,12 @@ pub(in crate::api::search) async fn perform_unified_search(user_id.as_ref(),user_organization_id,should_include_call_records,+ ),+ calendar_event_filters.filter_to_search_args(+ ctx,+ user_id.as_ref(),+ user_organization_id,+ should_include_calendar_events,)).map_err(|e| SearchError::InternalError(anyhow::anyhow!("tokio error: {:?}", e)))?;@@ -323,8 +346,8 @@ pub(in crate::api::search) async fn perform_unified_search(// channel messages are single-doc-per-message so each token must// appear in the same message via bool.must. Documents, chats, and// call records are join-shape, where each term becomes a separate- // has_child clause ANDed via bool.must. Projects are flat and match- // terms against their name only.+ // has_child clause ANDed via bool.must. Projects and calendar events are+ // flat and match terms against their name/title only.filter_document_response.terms = search_terms.clone();filter_document_response.property_filters = property_filter_args;filter_document_response.tag_option_ids = tag_option_ids.clone();@@ -340,8 +363,11 @@ pub(in crate::api::search) async fn perform_unified_search(filter_call_record_response.tag_option_ids = tag_option_ids.clone();filter_call_record_response.match_all_tags = match_all_tags;filter_project_response.terms = search_terms.clone();- filter_project_response.tag_option_ids = tag_option_ids;+ filter_project_response.tag_option_ids = tag_option_ids.clone();filter_project_response.match_all_tags = match_all_tags;+ filter_calendar_event_response.terms = search_terms.clone();+ filter_calendar_event_response.tag_option_ids = tag_option_ids;+ filter_calendar_event_response.match_all_tags = match_all_tags;// Widen the email access filter to every inbox the caller can reach (their// own plus delegated). Connected secondary inboxes share the owner's@@ -419,6 +445,14 @@ pub(in crate::api::search) async fn perform_unified_search({indices.insert(models_opensearch::OpenSearchEntityType::Projects);}+ // An event set narrowed to nothing would be rejected by the query+ // builder rather than matching everything, so drop the index.+ if should_include_calendar_events+ && !(filter_calendar_event_response.ids_only+ && filter_calendar_event_response.calendar_event_ids.is_empty())+ {+ indices.insert(models_opensearch::OpenSearchEntityType::CalendarEvents);+ }indices},document_search_args: filter_document_response.clone(),@@ -427,6 +461,7 @@ pub(in crate::api::search) async fn perform_unified_search(chat_search_args: filter_chat_response.clone(),call_record_search_args: filter_call_record_response.clone(),project_search_args: filter_project_response,+ calendar_event_search_args: filter_calendar_event_response,};// Call search functions in parallel for included entity types@@ -458,12 +493,14 @@ pub(in crate::api::search) async fn perform_unified_search(}},async {- // Documents, email subjects, project names, and call names are- // name-searched in OpenSearch. In Name mode keep only those- // indices: chats still name-search in Postgres above, and channels- // have no name concept. Documents match `document_name`, emails- // their subject, projects their `name`, calls their `name`.- // Projects have no content, so a content-only search drops them.+ // Documents, email subjects, project names, call names, and+ // calendar event titles are name-searched in OpenSearch. In Name+ // mode keep only those indices: chats still name-search in+ // Postgres above, and channels have no name concept. Documents+ // match `document_name`, emails their subject, projects their+ // `name`, calls their `name`, calendar events their `title`.+ // Projects and calendar events have no content, so a content-only+ // search drops them.let mut args = unified_search_args;match search_on {SearchOn::Name => {@@ -475,6 +512,7 @@ pub(in crate::api::search) async fn perform_unified_search(|| *i == models_opensearch::OpenSearchEntityType::Emails|| *i == models_opensearch::OpenSearchEntityType::Projects|| *i == models_opensearch::OpenSearchEntityType::CallRecords+ || *i == models_opensearch::OpenSearchEntityType::CalendarEvents});}SearchOn::NameContent => {@@ -485,6 +523,8 @@ pub(in crate::api::search) async fn perform_unified_search(args.document_search_args.mode = DocumentSearchMode::Content;args.search_indices.remove(&models_opensearch::OpenSearchEntityType::Projects);+ args.search_indices+ .remove(&models_opensearch::OpenSearchEntityType::CalendarEvents);}}if args.search_indices.is_empty() {diff --git a/crates/search_service/src/api/search/simple/simple_unified/test.rs b/crates/search_service/src/api/search/simple/simple_unified/test.rsindex 5189144cca..6e974b1cca 100644--- a/crates/search_service/src/api/search/simple/simple_unified/test.rs+++ b/crates/search_service/src/api/search/simple/simple_unified/test.rs@@ -538,3 +538,23 @@ fn enforce_term_limits_truncates_on_char_boundary() {let out = enforce_term_limits(vec![long]).expect("single term is under the count cap");assert_eq!(out[0].chars().count(), MAX_TERM_CHARS);}++/// The flag is the only thing keeping calendar events out of a deployed+/// environment's search results, so the override direction matters: a request+/// that asks for them must still get nothing while the flag is off.+#[test]+fn the_calendar_flag_overrides_what_the_request_asked_for() {+ assert!(+ calendar_events_searchable(true, true),+ "requested and enabled is the only combination that searches calendar"+ );+ assert!(+ !calendar_events_searchable(true, false),+ "an explicit request must not defeat a disabled environment"+ );+ assert!(+ !calendar_events_searchable(false, true),+ "an enabled environment must still honour a NIL-excluded request"+ );+ assert!(!calendar_events_searchable(false, false));+}diff --git a/crates/search_service/src/api/search/unified.rs b/crates/search_service/src/api/search/unified.rsindex 7beaaccd39..6634477dd1 100644--- a/crates/search_service/src/api/search/unified.rs+++ b/crates/search_service/src/api/search/unified.rs@@ -78,6 +78,7 @@ pub async fn handler(project,call_record,crm_company,+ calendar_event,} = results.into_iter().split_search_response();let (@@ -88,6 +89,7 @@ pub async fn handler(enriched_email_results,enriched_call_record_results,enriched_crm_results,+ enriched_calendar_event_results,) = tokio::try_join!(enrich_search_response(&ctx,@@ -132,6 +134,13 @@ pub async fn handler(None,),enrich_crm_companies(&ctx, crm_access.as_ref(), crm_company),+ enrich_search_response(+ &ctx,+ &user_context.user_id,+ calendar_event,+ models_opensearch::SearchEntityType::CalendarEvents,+ None,+ ),).map_err(|e| SearchError::InternalError(anyhow::anyhow!("tokio error: {:?}", e)))?;@@ -145,6 +154,7 @@ pub async fn handler(results.extend(enriched_email_results);results.extend(enriched_call_record_results);results.extend(enriched_crm_results);+ results.extend(enriched_calendar_event_results);sort_unified_search_results(results)};diff --git a/crates/sqs_client/src/search.rs b/crates/sqs_client/src/search.rsindex 30850b9a72..7b34d03ed4 100644--- a/crates/sqs_client/src/search.rs+++ b/crates/sqs_client/src/search.rs@@ -1,6 +1,7 @@use crate::{SQS,search::{+ calendar_event::UpsertCalendarEvent,call::{CallRecordMessage, RemoveCallRecord},channel::ChannelMessageUpdate,chat::ChatMessage,@@ -14,6 +15,7 @@ use aws_sdk_sqs::{self as sqs, types::SendMessageBatchRequestEntry};use futures::StreamExt;use strum::Display;+pub mod calendar_event;pub mod call;pub mod channel;pub mod chat;@@ -83,6 +85,8 @@ pub enum SearchQueueMessage {RemoveCallRecord(RemoveCallRecord),// ProjectUpsertProject(UpsertProject),+ // Calendar+ UpsertCalendarEvent(UpsertCalendarEvent),// UserRemoveUserProfile(String),@@ -106,6 +110,7 @@ impl PrimaryId for SearchQueueMessage {message.call_id.clone().unwrap_or_default()),SearchQueueMessage::UpsertProject(message) => message.project_id.clone(),+ SearchQueueMessage::UpsertCalendarEvent(message) => message.event_id.clone(),SearchQueueMessage::RemoveUserProfile(message) => message.clone(),}@@ -129,6 +134,8 @@ impl SearchQueueMessage {SearchQueueMessage::RemoveCallRecord(_) => Operation::Remove,// ProjectsSearchQueueMessage::UpsertProject(_) => Operation::UpdateMetadata,+ // Calendar+ SearchQueueMessage::UpsertCalendarEvent(_) => Operation::UpdateMetadata,// UsersSearchQueueMessage::RemoveUserProfile(_) => Operation::Remove,}diff --git a/crates/sqs_client/src/search/calendar_event.rs b/crates/sqs_client/src/search/calendar_event.rsnew file mode 100644index 0000000000..ede690900b--- /dev/null+++ b/crates/sqs_client/src/search/calendar_event.rs@@ -0,0 +1,14 @@+/// Requests calendar event reconciliation.+///+/// Live changes arrive on the `macro.calendar` Kafka topic; this payload backs+/// the SQS backfill path, which can also retarget an alternate OpenSearch+/// index through `index_override`. Both paths converge on the same+/// reconciliation, which re-reads the row.+#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Eq)]+pub struct UpsertCalendarEvent {+ /// The calendar event entity id+ pub event_id: String,+ /// Optional override for the target OpenSearch index+ #[serde(skip_serializing_if = "Option::is_none", default)]+ pub index_override: Option<String>,+}diff --git a/infra/stacks/opensearch/helpers/constants.ts b/infra/stacks/opensearch/helpers/constants.tsindex deb8d9490b..b2c15814f6 100644--- a/infra/stacks/opensearch/helpers/constants.ts+++ b/infra/stacks/opensearch/helpers/constants.ts@@ -7,6 +7,7 @@ export const DOCUMENTS_ALIAS = 'documents';export const EMAILS_ALIAS = 'emails';export const CALL_RECORDS_ALIAS = 'call_records';export const PROJECTS_ALIAS = 'projects';+export const CALENDAR_EVENTS_ALIAS = 'calendar_events';// Underlying physical indices (versioned). Bump the suffix to roll a new// version, then swap the alias atomically.@@ -16,6 +17,7 @@ export const DOCUMENTS_INDEX = 'documents_v2';export const EMAILS_INDEX = 'emails_v2';export const CALL_RECORDS_INDEX = 'call_records_v2';export const PROJECTS_INDEX = 'projects_v1';+export const CALENDAR_EVENTS_INDEX = 'calendar_events_v1';export const ALIAS_TO_INDEX: Record<string, string> = {[CHANNELS_ALIAS]: CHANNELS_INDEX,@@ -24,6 +26,7 @@ export const ALIAS_TO_INDEX: Record<string, string> = {[EMAILS_ALIAS]: EMAILS_INDEX,[CALL_RECORDS_ALIAS]: CALL_RECORDS_INDEX,[PROJECTS_ALIAS]: PROJECTS_INDEX,+ [CALENDAR_EVENTS_ALIAS]: CALENDAR_EVENTS_INDEX,};// Backward-compat shorthands used by older migration scripts. They now pointdiff --git a/infra/stacks/opensearch/helpers/scripts/create_indices.ts b/infra/stacks/opensearch/helpers/scripts/create_indices.tsindex a588fd7932..e590831578 100644--- a/infra/stacks/opensearch/helpers/scripts/create_indices.ts+++ b/infra/stacks/opensearch/helpers/scripts/create_indices.ts@@ -1,6 +1,8 @@import type { Client } from '@opensearch-project/opensearch';import { client } from '../client';import {+ CALENDAR_EVENTS_ALIAS,+ CALENDAR_EVENTS_INDEX,CALL_RECORDS_ALIAS,CALL_RECORDS_INDEX,CHANNELS_ALIAS,@@ -617,6 +619,140 @@ const PROJECTS_BODY = {},};+// `calendar_events_v1` is flat: one doc per **series master** (a+// `calendar_events` row), `_id` = event id. Recurring instances are not+// indexed — `calendar_event_occurrences` is materialized only inside a+// rolling window (`calendars.materialized_range`), so a per-occurrence doc+// would bound a recurring event's searchability to that slice and need+// rewriting as the window rolls. Search resolves the relevant occurrence at+// enrichment time instead.+//+// Access mirrors the soup predicate: `owner_id` is the event's own per-user+// projection, and `source_link_id` grants delegates on that inbox.+const CALENDAR_EVENTS_BODY = {+ settings: {+ ...SHARD_SETTINGS,+ ...SLOWLOG_SETTINGS,+ refresh_interval: '1s',+ },+ mappings: {+ dynamic: 'false',+ properties: {+ entity_id: {+ type: 'keyword',+ },+ // The series title. Named `name` rather than `title` because the+ // unified search request highlights a fixed list of field names, and+ // `name` is the one every other flat index uses — see the `Highlight`+ // builder in `opensearch_client::search::unified`.+ name: {+ type: 'text',+ fields: {+ keyword: {+ type: 'keyword',+ ignore_above: 128,+ },+ },+ },+ // Titles Google carries on renamed single instances (exception events+ // with their own `summary`). Mapped but not yet populated: this index is+ // `dynamic: false`, so an unmapped field is silently dropped — reserving+ // it here makes turning renamed instances on a populate + backfill+ // rather than a mapping migration and reindex (macro-2731).+ override_names: {+ type: 'text',+ },+ owner_id: {+ type: 'keyword',+ index: true,+ doc_values: true,+ },+ // The inbox link the canonical source belongs to. Delegated access is+ // granted through this, OR'd with owner_id at query time.+ source_link_id: {+ type: 'keyword',+ index: true,+ doc_values: true,+ },+ // RFC 5545 UID, shared across every attendee's projection of a meeting.+ ical_uid: {+ type: 'keyword',+ index: true,+ doc_values: true,+ },+ status: {+ type: 'keyword',+ index: true,+ doc_values: true,+ },+ is_recurring: {+ type: 'boolean',+ index: true,+ doc_values: true,+ },+ organizer_email: {+ type: 'keyword',+ index: true,+ doc_values: true,+ },+ attendee_emails: {+ type: 'keyword',+ index: true,+ doc_values: true,+ },+ // Master span. A timed series carries the millis pair; an all-day+ // series carries the date pair instead.+ starts_at_millis: {+ type: 'date',+ format: 'epoch_millis',+ index: true,+ doc_values: true,+ },+ ends_at_millis: {+ type: 'date',+ format: 'epoch_millis',+ index: true,+ doc_values: true,+ },+ start_date: {+ type: 'date',+ format: 'strict_date',+ index: true,+ doc_values: true,+ },+ end_date: {+ type: 'date',+ format: 'strict_date',+ index: true,+ doc_values: true,+ },+ created_at_millis: {+ type: 'date',+ format: 'epoch_millis',+ index: false,+ doc_values: true,+ },+ updated_at_millis: {+ type: 'date',+ format: 'epoch_millis',+ index: false,+ doc_values: true,+ },+ // Entity properties (tags, custom). Same nested shape as the documents+ // index so the shared property/tag query builders apply unchanged.+ properties: {+ type: 'nested',+ properties: {+ definition_id: { type: 'keyword' },+ values: { type: 'keyword' },+ number_value: { type: 'double' },+ date_value: { type: 'date' },+ },+ },+ },+ },+};+// Splits addresses into positioned segment tokens for the `.parts`// sub-fields: `[email protected]` -> [jane, doe, mail, foo, com].// Same-width space replacements keep highlight offsets aligned, and the@@ -934,6 +1070,11 @@ export const INDEX_SPECS: CreateIndexArgs[] = [body: CALL_RECORDS_V2_BODY,},{ indexName: PROJECTS_INDEX, aliasName: PROJECTS_ALIAS, body: PROJECTS_BODY },+ {+ indexName: CALENDAR_EVENTS_INDEX,+ aliasName: CALENDAR_EVENTS_ALIAS,+ body: CALENDAR_EVENTS_BODY,+ },];/**diff --git a/packages/sdk/generated/search/index.ts b/packages/sdk/generated/search/index.tsindex 208aa0e94e..f27a2fc1da 100644--- a/packages/sdk/generated/search/index.ts+++ b/packages/sdk/generated/search/index.ts@@ -1,4 +1,4 @@// This file is auto-generated by @hey-api/openapi-tsexport { type Options, Sdk } from './sdk.gen';-export type { CalendarEventFilters, CallFilters, CallRecordMetadata, CallRecordSearchResponseItem, CallRecordSearchResponseItemWithMetadata, CallRecordSearchResult, CallStatus, ChannelFilters, ChannelMessageSearchResponseItem, ChannelMetadata, ChannelNameSearchResponseItem, ChannelSearchRequest, ChannelSearchResponse, ChannelSearchResponseItem, ChannelSearchResponseItemWithMetadata, ChannelSearchResult, ChannelSortTimestamp, ChannelThreadFilters, ChatFilters, ChatMessageSearchResult, ChatMetadata, ChatSearchRequest, ChatSearchResponse, ChatSearchResponseItem, ChatSearchResponseItemWithMetadata, ClientOptions, CrmCompanyFilters, CrmCompanySearchDomain, CrmCompanySearchResponseItem, DataType, DocumentFilters, DocumentMetadata, DocumentSearchRequest, DocumentSearchResponse, DocumentSearchResponseItem, DocumentSearchResponseItemWithMetadata, DocumentSearchResult, DocumentSubType, EmailFilters, EmailSearchParticipant, EmailSearchRequest, EmailSearchResponse, EmailSearchResponseItem, EmailSearchResponseItemWithMetadata, EmailSearchResult, EmptyResponse, EntityFilters, EntityReference, EntityType, ErrorResponse, FileType, ForeignEntityFilters, MatchType, NotificationFilters, ProjectFilters, ProjectMetadata, ProjectSearchMetadata, ProjectSearchRequest, ProjectSearchResponseItem, ProjectSearchResponseItemWithMetadata, ProjectSearchResult, PropertyDefinition, PropertyFilter, PropertyOwner, PropertyValue, ReminderFilters, SearchHighlight, SearchOn, SearchResponse, SharedEmailFilter, SimpleCallRecordSearchResponseBaseItemHumanReadableTimestamp, SimpleChannelSearchReponseBaseItem, SimpleChannelSearchReponseBaseItemHumanReadableTimestamp, SimpleChannelSearchResponse, SimpleChatSearchResponse, SimpleChatSearchResponseBaseItem, SimpleChatSearchResponseBaseItemHumanReadableTimestamp, SimpleDocumentSearchResponse, SimpleDocumentSearchResponseBaseItem, SimpleDocumentSearchResponseBaseItemHumanReadableTimestamp, SimpleEmailSearchResponse, SimpleEmailSearchResponseBaseItem, SimpleEmailSearchResponseBaseItemHumanReadableTimestamp, SimpleProjectSearchResponse, SimpleProjectSearchResponseBaseItem, SimpleProjectSearchResponseBaseItemHumanReadableTimestamp, SimpleUnifiedSearchBaseResponse, SimpleUnifiedSearchData, SimpleUnifiedSearchError, SimpleUnifiedSearchErrors, SimpleUnifiedSearchResponse, SimpleUnifiedSearchResponseBaseItem, SimpleUnifiedSearchResponseBaseItemHumanReadableTimestamp, SimpleUnifiedSearchResponses, SoupProperty, TagFilterMode, TaskFilters, UnifiedSearchData, UnifiedSearchError, UnifiedSearchErrors, UnifiedSearchRequest, UnifiedSearchResponse, UnifiedSearchResponse2, UnifiedSearchResponseItem, UnifiedSearchResponses } from './types.gen';+export type { CalendarEventFilters, CalendarEventMetadata, CalendarEventSearchOccurrence, CalendarEventSearchResponseItem, CalendarEventSearchResponseItemWithMetadata, CalendarEventSearchResult, CalendarEventSearchTime, CallFilters, CallRecordMetadata, CallRecordSearchResponseItem, CallRecordSearchResponseItemWithMetadata, CallRecordSearchResult, CallStatus, ChannelFilters, ChannelMessageSearchResponseItem, ChannelMetadata, ChannelNameSearchResponseItem, ChannelSearchRequest, ChannelSearchResponse, ChannelSearchResponseItem, ChannelSearchResponseItemWithMetadata, ChannelSearchResult, ChannelSortTimestamp, ChannelThreadFilters, ChatFilters, ChatMessageSearchResult, ChatMetadata, ChatSearchRequest, ChatSearchResponse, ChatSearchResponseItem, ChatSearchResponseItemWithMetadata, ClientOptions, CrmCompanyFilters, CrmCompanySearchDomain, CrmCompanySearchResponseItem, DataType, DocumentFilters, DocumentMetadata, DocumentSearchRequest, DocumentSearchResponse, DocumentSearchResponseItem, DocumentSearchResponseItemWithMetadata, DocumentSearchResult, DocumentSubType, EmailFilters, EmailSearchParticipant, EmailSearchRequest, EmailSearchResponse, EmailSearchResponseItem, EmailSearchResponseItemWithMetadata, EmailSearchResult, EmptyResponse, EntityFilters, EntityReference, EntityType, ErrorResponse, FileType, ForeignEntityFilters, MatchType, NotificationFilters, ProjectFilters, ProjectMetadata, ProjectSearchMetadata, ProjectSearchRequest, ProjectSearchResponseItem, ProjectSearchResponseItemWithMetadata, ProjectSearchResult, PropertyDefinition, PropertyFilter, PropertyOwner, PropertyValue, ReminderFilters, SearchHighlight, SearchOn, SearchResponse, SharedEmailFilter, SimpleCalendarEventSearchResponseBaseItemHumanReadableTimestamp, SimpleCallRecordSearchResponseBaseItemHumanReadableTimestamp, SimpleChannelSearchReponseBaseItem, SimpleChannelSearchReponseBaseItemHumanReadableTimestamp, SimpleChannelSearchResponse, SimpleChatSearchResponse, SimpleChatSearchResponseBaseItem, SimpleChatSearchResponseBaseItemHumanReadableTimestamp, SimpleDocumentSearchResponse, SimpleDocumentSearchResponseBaseItem, SimpleDocumentSearchResponseBaseItemHumanReadableTimestamp, SimpleEmailSearchResponse, SimpleEmailSearchResponseBaseItem, SimpleEmailSearchResponseBaseItemHumanReadableTimestamp, SimpleProjectSearchResponse, SimpleProjectSearchResponseBaseItem, SimpleProjectSearchResponseBaseItemHumanReadableTimestamp, SimpleUnifiedSearchBaseResponse, SimpleUnifiedSearchData, SimpleUnifiedSearchError, SimpleUnifiedSearchErrors, SimpleUnifiedSearchResponse, SimpleUnifiedSearchResponseBaseItem, SimpleUnifiedSearchResponseBaseItemHumanReadableTimestamp, SimpleUnifiedSearchResponses, SoupProperty, TagFilterMode, TaskFilters, UnifiedSearchData, UnifiedSearchError, UnifiedSearchErrors, UnifiedSearchRequest, UnifiedSearchResponse, UnifiedSearchResponse2, UnifiedSearchResponseItem, UnifiedSearchResponses } from './types.gen';diff --git a/packages/sdk/generated/search/types.gen.ts b/packages/sdk/generated/search/types.gen.tsindex 7ba9478205..6e4cb8aae4 100644--- a/packages/sdk/generated/search/types.gen.ts+++ b/packages/sdk/generated/search/types.gen.ts@@ -34,6 +34,132 @@ export type CalendarEventFilters = {statuses?: Array<string>;};+/**+ * Metadata for a calendar event fetched from the database.+ *+ * The index carries the master span for ranking, but the occurrence a row+ * should display depends on when the query ran, so it is resolved here+ * rather than indexed.+ */+export type CalendarEventMetadata = {+ /**+ * Direct conference join URL when known.+ */+ conferenceUrl?: string | null;+ createdAt: string;+ /**+ * Whether the canonical source prohibits mutation.+ */+ isReadOnly: boolean;+ /**+ * Whether the series carries a recurrence rule. Lets a row render a+ * recurring badge without parsing the rules.+ */+ isRecurring: boolean;+ occurrence?: null | CalendarEventSearchOccurrence;+ /**+ * Canonical status (`confirmed`, `tentative`, `cancelled`).+ */+ status: string;+ /**+ * The series' own span.+ */+ time: CalendarEventSearchTime;+ updatedAt: string;+};++/**+ * The instance a search row points at.+ *+ * A recurring series is indexed once, as its master. Which instance the row+ * should show is decided per request rather than baked into the index: the+ * next occurrence at or after now, else the most recent past one. This is+ * the same resolution a calendar mention performs, so a search row and a+ * mention of the same event agree on where they land.+ */+export type CalendarEventSearchOccurrence = {+ /**+ * Stable key of the resolved instance within its series.+ */+ occurrenceKey: string;+ /**+ * Span of the resolved instance.+ */+ time: CalendarEventSearchTime;+};++/**+ * A single response item, part of the CalendarEventSearchResponse object+ */+export type CalendarEventSearchResponseItem = {+ calendar_event_search_results: Array<CalendarEventSearchResult>;+ created_at: string;+ /**+ * Standardized fields that all item types will share.+ */+ id: string;+ name: string;+ owner_id: string;+ updated_at: string;+};++/**+ * CalendarEventSearchResponseItem with metadata fetched from macrodb.+ */+export type CalendarEventSearchResponseItemWithMetadata = CalendarEventSearchResponseItem & {+ metadata?: null | CalendarEventMetadata;+ /**+ * Entity properties (e.g. tags) on the event.+ */+ properties?: Array<SoupProperty> | null;+};++export type CalendarEventSearchResult = {+ highlight: SearchHighlight;+ /**+ * The score of the result+ */+ score?: number | null;+};++/**+ * Timed or all-day span of a calendar event.+ *+ * Deliberately a local mirror of `models_soup::calendar_event::SoupCalendarEventTime`+ * rather than a reuse of it: this module needs both `ToSchema` (OpenAPI) and+ * `JsonSchema` (AI tool schemas), and `models_soup` carries no schemars+ * dependency. The wire shape is identical and must stay that way, so a+ * client can decode a search row and a soup row with one mapping.+ *+ * Fields are camelCased per variant rather than via `rename_all_fields`,+ * which utoipa ignores.+ */+export type CalendarEventSearchTime = {+ /**+ * Exclusive end.+ */+ endsAt: string;+ kind: 'timed';+ /**+ * Inclusive start.+ */+ startsAt: string;+ /**+ * Original IANA time zone.+ */+ timeZone?: string | null;+} | {+ /**+ * Exclusive end date.+ */+ endDate: string;+ kind: 'allDay';+ /**+ * Inclusive start date.+ */+ startDate: string;+};+/*** Filters for call records.*/@@ -1517,6 +1643,41 @@ export type SearchResponse = {*/export type SharedEmailFilter = 'exclude' | 'include' | 'only';+export type SimpleCalendarEventSearchResponseBaseItemHumanReadableTimestamp = {+ /**+ * The calendar event id+ */+ calendar_event_id: string;+ /**+ * DateTime<Utc> ts_seconds deserialization + RFC3339 serialization+ */+ created_at: string;+ /**+ * The highlights on the event+ */+ highlight: SearchHighlight;+ /**+ * Whether the series carries a recurrence rule+ */+ is_recurring: boolean;+ /**+ * Stable key of the instance this row points at, when one resolved+ */+ occurrence_key?: string | null;+ /**+ * The event title+ */+ title: string;+ /**+ * DateTime<Utc> ts_seconds deserialization + RFC3339 serialization+ */+ updated_at: string;+ /**+ * The owner of this event projection+ */+ user_id: string;+};+export type SimpleCallRecordSearchResponseBaseItemHumanReadableTimestamp = {call_id: string;channel_id: string;@@ -1973,6 +2134,8 @@ export type SimpleUnifiedSearchResponseBaseItem = (SimpleDocumentSearchResponseBtype: 'project';}) | (SimpleCallRecordSearchResponseBaseItemHumanReadableTimestamp & {type: 'call';+}) | (SimpleCalendarEventSearchResponseBaseItemHumanReadableTimestamp & {+ type: 'calendarEvent';});export type SimpleUnifiedSearchResponseBaseItemHumanReadableTimestamp = (SimpleDocumentSearchResponseBaseItemHumanReadableTimestamp & {@@ -1987,6 +2150,8 @@ export type SimpleUnifiedSearchResponseBaseItemHumanReadableTimestamp = (SimpleDtype: 'project';}) | (SimpleCallRecordSearchResponseBaseItemHumanReadableTimestamp & {type: 'call';+}) | (SimpleCalendarEventSearchResponseBaseItemHumanReadableTimestamp & {+ type: 'calendarEvent';});/**@@ -2077,6 +2242,8 @@ export type UnifiedSearchResponseItem = (DocumentSearchResponseItemWithMetadatatype: 'call';}) | (CrmCompanySearchResponseItem & {type: 'company';+}) | (CalendarEventSearchResponseItemWithMetadata & {+ type: 'calendarEvent';});export type UnifiedSearchData = {diff --git a/packages/sdk/specs/search.json b/packages/sdk/specs/search.jsonindex 79f55767dc..225f0bfcc0 100644--- a/packages/sdk/specs/search.json+++ b/packages/sdk/specs/search.json@@ -217,6 +217,209 @@}}},+ "CalendarEventMetadata": {+ "type": "object",+ "description": "Metadata for a calendar event fetched from the database.\n\nThe index carries the master span for ranking, but the occurrence a row\nshould display depends on when the query ran, so it is resolved here\nrather than indexed.",+ "required": [+ "createdAt",+ "updatedAt",+ "status",+ "time",+ "isRecurring",+ "isReadOnly"+ ],+ "properties": {+ "conferenceUrl": {+ "type": ["string", "null"],+ "description": "Direct conference join URL when known."+ },+ "createdAt": {+ "type": "string",+ "format": "date-time"+ },+ "isReadOnly": {+ "type": "boolean",+ "description": "Whether the canonical source prohibits mutation."+ },+ "isRecurring": {+ "type": "boolean",+ "description": "Whether the series carries a recurrence rule. Lets a row render a\nrecurring badge without parsing the rules."+ },+ "occurrence": {+ "oneOf": [+ {+ "type": "null"+ },+ {+ "$ref": "#/components/schemas/CalendarEventSearchOccurrence",+ "description": "The instance this row points at. `None` when the series has no\nmaterialized occurrence — occurrences exist only inside a rolling\nwindow, so a row can legitimately fall back to `time`."+ }+ ]+ },+ "status": {+ "type": "string",+ "description": "Canonical status (`confirmed`, `tentative`, `cancelled`)."+ },+ "time": {+ "$ref": "#/components/schemas/CalendarEventSearchTime",+ "description": "The series' own span."+ },+ "updatedAt": {+ "type": "string",+ "format": "date-time"+ }+ }+ },+ "CalendarEventSearchOccurrence": {+ "type": "object",+ "description": "The instance a search row points at.\n\nA recurring series is indexed once, as its master. Which instance the row\nshould show is decided per request rather than baked into the index: the\nnext occurrence at or after now, else the most recent past one. This is\nthe same resolution a calendar mention performs, so a search row and a\nmention of the same event agree on where they land.",+ "required": ["occurrenceKey", "time"],+ "properties": {+ "occurrenceKey": {+ "type": "string",+ "description": "Stable key of the resolved instance within its series."+ },+ "time": {+ "$ref": "#/components/schemas/CalendarEventSearchTime",+ "description": "Span of the resolved instance."+ }+ }+ },+ "CalendarEventSearchResponseItem": {+ "type": "object",+ "description": "A single response item, part of the CalendarEventSearchResponse object",+ "required": [+ "id",+ "name",+ "owner_id",+ "updated_at",+ "created_at",+ "calendar_event_search_results"+ ],+ "properties": {+ "calendar_event_search_results": {+ "type": "array",+ "items": {+ "$ref": "#/components/schemas/CalendarEventSearchResult"+ }+ },+ "created_at": {+ "type": "string",+ "format": "date-time"+ },+ "id": {+ "type": "string",+ "format": "uuid",+ "description": "Standardized fields that all item types will share."+ },+ "name": {+ "type": "string"+ },+ "owner_id": {+ "type": "string"+ },+ "updated_at": {+ "type": "string",+ "format": "date-time"+ }+ }+ },+ "CalendarEventSearchResponseItemWithMetadata": {+ "allOf": [+ {+ "$ref": "#/components/schemas/CalendarEventSearchResponseItem"+ },+ {+ "type": "object",+ "properties": {+ "metadata": {+ "oneOf": [+ {+ "type": "null"+ },+ {+ "$ref": "#/components/schemas/CalendarEventMetadata",+ "description": "Metadata from the database. None if the event no longer exists."+ }+ ]+ },+ "properties": {+ "type": ["array", "null"],+ "items": {+ "$ref": "#/components/schemas/SoupProperty"+ },+ "description": "Entity properties (e.g. tags) on the event."+ }+ }+ }+ ],+ "description": "CalendarEventSearchResponseItem with metadata fetched from macrodb."+ },+ "CalendarEventSearchResult": {+ "type": "object",+ "required": ["highlight"],+ "properties": {+ "highlight": {+ "$ref": "#/components/schemas/SearchHighlight"+ },+ "score": {+ "type": ["number", "null"],+ "format": "double",+ "description": "The score of the result"+ }+ }+ },+ "CalendarEventSearchTime": {+ "oneOf": [+ {+ "type": "object",+ "description": "Absolute timed event.",+ "required": ["startsAt", "endsAt", "kind"],+ "properties": {+ "endsAt": {+ "type": "string",+ "format": "date-time",+ "description": "Exclusive end."+ },+ "kind": {+ "type": "string",+ "enum": ["timed"]+ },+ "startsAt": {+ "type": "string",+ "format": "date-time",+ "description": "Inclusive start."+ },+ "timeZone": {+ "type": ["string", "null"],+ "description": "Original IANA time zone."+ }+ }+ },+ {+ "type": "object",+ "description": "All-day event with an exclusive end date.",+ "required": ["startDate", "endDate", "kind"],+ "properties": {+ "endDate": {+ "type": "string",+ "format": "date",+ "description": "Exclusive end date."+ },+ "kind": {+ "type": "string",+ "enum": ["allDay"]+ },+ "startDate": {+ "type": "string",+ "format": "date",+ "description": "Inclusive start date."+ }+ }+ }+ ],+ "description": "Timed or all-day span of a calendar event.\n\nDeliberately a local mirror of `models_soup::calendar_event::SoupCalendarEventTime`\nrather than a reuse of it: this module needs both `ToSchema` (OpenAPI) and\n`JsonSchema` (AI tool schemas), and `models_soup` carries no schemars\ndependency. The wire shape is identical and must stay that way, so a\nclient can decode a search row and a soup row with one mapping.\n\nFields are camelCased per variant rather than via `rename_all_fields`,\nwhich utoipa ignores."+ },"CallFilters": {"type": "object","description": "Filters for call records.",@@ -2947,6 +3150,64 @@"description": "Controls whether shared email threads are included in results.","enum": ["exclude", "include", "only"]},+ "SimpleCalendarEventSearchResponseBaseItem_HumanReadableTimestamp": {+ "type": "object",+ "required": [+ "calendar_event_id",+ "updated_at",+ "created_at",+ "title",+ "user_id",+ "is_recurring",+ "highlight"+ ],+ "properties": {+ "calendar_event_id": {+ "type": "string",+ "description": "The calendar event id"+ },+ "created_at": {+ "oneOf": [+ {+ "type": "string",+ "format": "date-time",+ "description": "DateTime<Utc> ts_seconds deserialization + RFC3339 serialization"+ }+ ],+ "description": "The time the event was created"+ },+ "highlight": {+ "$ref": "#/components/schemas/SearchHighlight",+ "description": "The highlights on the event"+ },+ "is_recurring": {+ "type": "boolean",+ "description": "Whether the series carries a recurrence rule"+ },+ "occurrence_key": {+ "type": ["string", "null"],+ "description": "Stable key of the instance this row points at, when one resolved"+ },+ "title": {+ "type": "string",+ "description": "The event title"+ },+ "updated_at": {+ "oneOf": [+ {+ "type": "string",+ "format": "date-time",+ "description": "DateTime<Utc> ts_seconds deserialization + RFC3339 serialization"+ }+ ],+ "description": "The time the event was last updated"+ },+ "user_id": {+ "type": "string",+ "description": "The owner of this event projection"+ }+ }+ },"SimpleCallRecordSearchResponseBaseItem_HumanReadableTimestamp": {"type": "object","required": [@@ -3811,6 +4072,23 @@}}]+ },+ {+ "allOf": [+ {+ "$ref": "#/components/schemas/SimpleCalendarEventSearchResponseBaseItem_HumanReadableTimestamp"+ },+ {+ "type": "object",+ "required": ["type"],+ "properties": {+ "type": {+ "type": "string",+ "enum": ["calendarEvent"]+ }+ }+ }+ ]}]},@@ -3917,6 +4195,23 @@}}]+ },+ {+ "allOf": [+ {+ "$ref": "#/components/schemas/SimpleCalendarEventSearchResponseBaseItem_HumanReadableTimestamp"+ },+ {+ "type": "object",+ "required": ["type"],+ "properties": {+ "type": {+ "type": "string",+ "enum": ["calendarEvent"]+ }+ }+ }+ ]}]},@@ -4145,6 +4440,23 @@}}]+ },+ {+ "allOf": [+ {+ "$ref": "#/components/schemas/CalendarEventSearchResponseItemWithMetadata"+ },+ {+ "type": "object",+ "required": ["type"],+ "properties": {+ "type": {+ "type": "string",+ "enum": ["calendarEvent"]+ }+ }+ }+ ]}]}diff --git a/services/document_storage_service/src/api/context.rs b/services/document_storage_service/src/api/context.rsindex 9bde05c02c..dc55030ada 100644--- a/services/document_storage_service/src/api/context.rs+++ b/services/document_storage_service/src/api/context.rs@@ -555,6 +555,7 @@ impl From<&ApiContext> for SearchHandlerState {opensearch_client: ctx.opensearch_client.clone(),entity_access_service: ctx.entity_access_service.clone(),authorization_state: ctx.authorization_state.clone(),+ calendar_search_enabled: ctx.config.calendar_search_enabled,}}}diff --git a/services/document_storage_service/src/config.rs b/services/document_storage_service/src/config.rsindex 41c7367196..d03293d964 100644--- a/services/document_storage_service/src/config.rs+++ b/services/document_storage_service/src/config.rs@@ -126,6 +126,13 @@ pub struct Config {#[macro_config_default(Environment::new_or_prod())]pub environment: Environment,+ /// Whether calendar events participate in search. Off by default so a+ /// deployed environment can carry the code before its calendar index has+ /// been created and backfilled — with this false, calendar events are+ /// dropped from the searched set no matter what a request asks for.+ #[macro_config_default(false)]+ pub calendar_search_enabled: bool,+/// Maximum number of SQS messages to receive per poll for the delete document worker#[macro_config_default(10)]pub queue_max_messages: i32,diff --git a/services/email_service/src/api/context.rs b/services/email_service/src/api/context.rsindex 12e67848b7..60567c89b6 100644--- a/services/email_service/src/api/context.rs+++ b/services/email_service/src/api/context.rs@@ -38,6 +38,7 @@ pub(crate) type CalendarMutationSvc = CalendarMutationServiceImpl<PgCalendarRepository,GoogleCalendarClient<RedisCalendarRequestGate>,CalendarTokenProviderAdapter,+ EmailEventBroker,>;pub(crate) type EmailEntityAccessService = EntityAccessServiceImpl<PgAccessRepository>;pub(crate) type EmailEntityAccessManagementService =diff --git a/services/email_service/src/main.rs b/services/email_service/src/main.rsindex d493a59af2..55c1b08fd8 100644--- a/services/email_service/src/main.rs+++ b/services/email_service/src/main.rs@@ -212,6 +212,7 @@ async fn main() -> anyhow::Result<()> {RedisCalendarRequestGate::new((*redis_client).clone()),),CalendarTokenProviderAdapter::new(redis_conn.clone(), auth_service_client.clone()),+ macro_event_broker.clone(),));let api_result = api::setup_and_serve(ApiContext {db,diff --git a/services/email_service/src/pubsub/backfill/worker.rs b/services/email_service/src/pubsub/backfill/worker.rsindex 844f92bbec..bf304a5e4a 100644--- a/services/email_service/src/pubsub/backfill/worker.rs+++ b/services/email_service/src/pubsub/backfill/worker.rs@@ -78,7 +78,8 @@ pub async fn run_worker_with_cancellation(calendar_sync_enabled: bool,cancellation_token: CancellationToken,) {- let calendar_backfills = CalendarBackfillServices::new(db.clone(), redis_client.clone());+ let calendar_backfills =+ CalendarBackfillServices::new(db.clone(), redis_client.clone(), macro_event_broker.clone());let ctx = PubSubContext {db,sqs_worker: worker.clone(),diff --git a/services/email_service/src/pubsub/context.rs b/services/email_service/src/pubsub/context.rsindex c7d879b327..65ab5fbe5c 100644--- a/services/email_service/src/pubsub/context.rs+++ b/services/email_service/src/pubsub/context.rs@@ -36,6 +36,7 @@ pub type GoogleCalendarBackfillService = GoogleCalendarBackfillCoordinator<PgCalendarRepository,GoogleCalendarClient<RedisCalendarRequestGate>,PgCalendarRepository,+ PubSubEventBroker,>;/// Concrete pre-lease Google Calendar failure application service.@@ -53,7 +54,11 @@ pub struct CalendarBackfillServices {impl CalendarBackfillServices {/// Compose calendar application services from process-level adapters.- pub fn new(db: PgPool, redis_client: RedisClient) -> Self {+ pub fn new(+ db: PgPool,+ redis_client: RedisClient,+ macro_event_broker: PubSubEventBroker,+ ) -> Self {let repository = PgCalendarRepository::new(db);Self {google: Arc::new(GoogleCalendarBackfillCoordinator::new(@@ -66,6 +71,7 @@ impl CalendarBackfillServices {RedisCalendarRequestGate::new(redis_client),),repository.clone(),+ macro_event_broker,calendar_watch_config(),)),google_failure: Arc::new(GoogleCalendarBackfillFailureService::new(repository)),diff --git a/services/email_service/src/pubsub/inbox_sync/worker.rs b/services/email_service/src/pubsub/inbox_sync/worker.rsindex a14eb1e7f5..d3bf7bb197 100644--- a/services/email_service/src/pubsub/inbox_sync/worker.rs+++ b/services/email_service/src/pubsub/inbox_sync/worker.rs@@ -81,7 +81,8 @@ pub async fn run_worker_with_cancellation(retry_worker: bool,cancellation_token: CancellationToken,) {- let calendar_backfills = CalendarBackfillServices::new(db.clone(), redis_client.clone());+ let calendar_backfills =+ CalendarBackfillServices::new(db.clone(), redis_client.clone(), macro_event_broker.clone());let ctx = PubSubContext {db,sqs_worker: worker.clone(),diff --git a/services/search_processing_service/Cargo.toml b/services/search_processing_service/Cargo.tomlindex ca3cf93794..dcc5695213 100644--- a/services/search_processing_service/Cargo.toml+++ b/services/search_processing_service/Cargo.toml@@ -67,6 +67,7 @@ macro_db_client = { path = "../../crates/macro_db_client", optional = true } # Mmacro_entrypoint = { path = "../../crates/macro_entrypoint" }macro_env = { path = "../../crates/macro_env" }macro_env_var = { path = "../../crates/macro_env_var" }+calendar_events = { path = "../../crates/calendar_events" }macro_event_broker = { path = "../../crates/macro_event_broker", default-features = false, features = ["outbound",], optional = true }diff --git a/services/search_processing_service/src/api/internal/backfill.rs b/services/search_processing_service/src/api/internal/backfill.rsindex a6cf72206b..32d552309b 100644--- a/services/search_processing_service/src/api/internal/backfill.rs+++ b/services/search_processing_service/src/api/internal/backfill.rs@@ -28,8 +28,9 @@ use crate::BackfillServiceImpl;use crate::api::context::{ApiContext, AuthorizationService};use crate::domain::jobs::{BackfillJobs, JobId};use crate::domain::models::{- CallBackfillRequest, ChannelBackfillRequest, ChatBackfillRequest, DocumentBackfillRequest,- EmailBackfillRequest, ProjectBackfillRequest, PropertiesBackfillRequest,+ CalendarEventBackfillRequest, CallBackfillRequest, ChannelBackfillRequest, ChatBackfillRequest,+ DocumentBackfillRequest, EmailBackfillRequest, ProjectBackfillRequest,+ PropertiesBackfillRequest,};use crate::domain::service::BackfillService;@@ -42,6 +43,7 @@ pub fn router() -> Router<ApiContext> {.route("/emails", post(emails)).route("/properties", post(properties)).route("/projects", post(projects))+ .route("/calendar-events", post(calendar_events)).route("/{job_id}", get(status))}@@ -172,6 +174,24 @@ async fn projects(.await}+#[tracing::instrument(skip(service, jobs, _internal_authorization, req))]+async fn calendar_events(+ State(service): State<Arc<BackfillServiceImpl>>,+ State(jobs): State<BackfillJobs>,+ _internal_authorization: MacroAuthorizationExtractor<AuthorizationService, InternalOnly>,+ extract::Json(req): extract::Json<CalendarEventBackfillRequest>,+) -> Response {+ spawn_backfill(+ service,+ jobs,+ "calendar-events",+ move |svc, progress, cancel| async move {+ svc.backfill_calendar_events(req, progress, cancel).await+ },+ )+ .await+}+#[tracing::instrument(skip(jobs, _internal_authorization))]async fn status(State(jobs): State<BackfillJobs>,diff --git a/services/search_processing_service/src/config.rs b/services/search_processing_service/src/config.rsindex 14cb871fa3..12ea284813 100644--- a/services/search_processing_service/src/config.rs+++ b/services/search_processing_service/src/config.rs@@ -24,6 +24,7 @@ maybe_env_vars! {pub struct BackfillDocumentsPageSize;pub struct BackfillEmailsPageSize;pub struct BackfillProjectsPageSize;+ pub struct BackfillCalendarEventsPageSize;pub struct BackfillJobTtlSeconds;}@@ -37,6 +38,7 @@ pub struct BackfillPageSizes {pub documents: usize,pub emails: usize,pub projects: usize,+ pub calendar_events: usize,}const DEFAULT_CALLS_PAGE: usize = 2000;@@ -45,6 +47,7 @@ const DEFAULT_CHANNELS_PAGE: usize = 5000;const DEFAULT_DOCUMENTS_PAGE: usize = 1000;const DEFAULT_EMAILS_PAGE: usize = 1000;const DEFAULT_PROJECTS_PAGE: usize = 2000;+const DEFAULT_CALENDAR_EVENTS_PAGE: usize = 2000;const DEFAULT_BACKFILL_JOB_TTL_SECONDS: u64 = 24 * 60 * 60;fn parse_page_size(name: &str, raw_value: Option<&str>, default: usize) -> anyhow::Result<usize> {@@ -92,6 +95,14 @@ pub struct Config {#[macro_config_default(8080)]pub port: usize,+ /// Whether calendar events are written to the search index. Off by default+ /// so a deployed environment can carry the code before its calendar index+ /// exists: an upsert against a missing index would otherwise be+ /// auto-created with a dynamic mapping, which is not the mapping this+ /// service expects. Enable only after the create-indices run.+ #[macro_config_default(false)]+ pub calendar_search_enabled: bool,+/// The queue max messages per poll#[macro_config_default(10)]pub queue_max_messages: i32,@@ -136,6 +147,8 @@ pub struct Config {pub backfill_emails_page_size: BackfillEmailsPageSize,/// DB page size used when backfilling projects.pub backfill_projects_page_size: BackfillProjectsPageSize,+ /// DB page size used when backfilling calendar events.+ pub backfill_calendar_events_page_size: BackfillCalendarEventsPageSize,/// DynamoDB table name backing the backfill job registry. Items carry an/// `expires_at` epoch attribute that DynamoDB's TTL sweeps in the@@ -187,6 +200,11 @@ impl Config {self.backfill_projects_page_size.value(),DEFAULT_PROJECTS_PAGE,)?,+ calendar_events: parse_page_size(+ "BACKFILL_CALENDAR_EVENTS_PAGE_SIZE",+ self.backfill_calendar_events_page_size.value(),+ DEFAULT_CALENDAR_EVENTS_PAGE,+ )?,})}diff --git a/services/search_processing_service/src/domain/models.rs b/services/search_processing_service/src/domain/models.rsindex 256f92ec8a..95991c605d 100644--- a/services/search_processing_service/src/domain/models.rs+++ b/services/search_processing_service/src/domain/models.rs@@ -210,6 +210,30 @@ pub struct ProjectBackfillCursor {pub project_id: String,}+/// Calendar event backfill filter. All `None` means "every event".+///+/// Only series masters are enumerated — recurring instances are not indexed.+#[derive(Debug, Clone, Default, Deserialize)]+#[serde(default)]+pub struct CalendarEventBackfillRequest {+ pub updated_after: Option<DateTime<Utc>>,+ pub updated_before: Option<DateTime<Utc>>,+ /// Override the OpenSearch target index for upserts (e.g. blue/green swap).+ pub index_override: Option<String>,+}++/// Keyset (seek-method) pagination cursor for calendar event backfills.+///+/// `get_calendar_events_for_search_backfill` walks `calendar_events` in+/// `(updated_at ASC, id ASC)` order; the cursor carries the last row's pair+/// so the next page resumes with `WHERE (updated_at, id) > cursor`.+/// `None` starts at the beginning.+#[derive(Debug, Clone)]+pub struct CalendarEventBackfillCursor {+ pub updated_at: DateTime<Utc>,+ pub event_id: uuid::Uuid,+}+/// Email-thread backfill filter.#[derive(Debug, Clone, Default, Deserialize)]#[serde(default)]diff --git a/services/search_processing_service/src/domain/ports.rs b/services/search_processing_service/src/domain/ports.rsindex ef8dc868e0..20b54d0287 100644--- a/services/search_processing_service/src/domain/ports.rs+++ b/services/search_processing_service/src/domain/ports.rs@@ -20,10 +20,10 @@ use models_properties::EntityType;use sqs_client::search::SearchQueueMessage;use super::models::{- BackfillError, CallBackfillCursor, CallBackfillRequest, ChannelBackfillRequest,- ChatBackfillCursor, ChatBackfillRequest, DocumentBackfillCursor, DocumentBackfillRequest,- EmailBackfillRequest, ProjectBackfillCursor, ProjectBackfillRequest, PropertiesBackfillRequest,- PropertySourcePage, SourcePage,+ BackfillError, CalendarEventBackfillCursor, CalendarEventBackfillRequest, CallBackfillCursor,+ CallBackfillRequest, ChannelBackfillRequest, ChatBackfillCursor, ChatBackfillRequest,+ DocumentBackfillCursor, DocumentBackfillRequest, EmailBackfillRequest, ProjectBackfillCursor,+ ProjectBackfillRequest, PropertiesBackfillRequest, PropertySourcePage, SourcePage,};/// Publishes batches of search-event messages.@@ -117,4 +117,14 @@ pub trait BackfillSource: Send + Sync + 'static {req: &ProjectBackfillRequest,cursor: Option<ProjectBackfillCursor>,) -> impl Future<Output = Result<(SourcePage, Option<ProjectBackfillCursor>), BackfillError>> + Send;++ /// Calendar events paginate by keyset cursor (mirroring projects): each+ /// call returns one page plus the cursor to resume from.+ fn fetch_calendar_events(+ &self,+ req: &CalendarEventBackfillRequest,+ cursor: Option<CalendarEventBackfillCursor>,+ ) -> impl Future<+ Output = Result<(SourcePage, Option<CalendarEventBackfillCursor>), BackfillError>,+ > + Send;}diff --git a/services/search_processing_service/src/domain/service.rs b/services/search_processing_service/src/domain/service.rsindex fe54092708..c7678896b6 100644--- a/services/search_processing_service/src/domain/service.rs+++ b/services/search_processing_service/src/domain/service.rs@@ -15,9 +15,9 @@ use tokio_util::sync::CancellationToken;use super::jobs::JobProgress;use super::models::{- BackfillError, BackfillReceipt, CallBackfillRequest, ChannelBackfillRequest,- ChatBackfillRequest, DocumentBackfillRequest, EmailBackfillRequest, ProjectBackfillRequest,- PropertiesBackfillRequest, PropertySourcePage, SourcePage,+ BackfillError, BackfillReceipt, CalendarEventBackfillRequest, CallBackfillRequest,+ ChannelBackfillRequest, ChatBackfillRequest, DocumentBackfillRequest, EmailBackfillRequest,+ ProjectBackfillRequest, PropertiesBackfillRequest, PropertySourcePage, SourcePage,};use super::ports::{BackfillSource, PropertyBackfillIndexer, SearchEventPublisher};@@ -111,6 +111,12 @@ pub trait BackfillService: Send + Sync + 'static {progress: Arc<JobProgress>,cancel: CancellationToken,) -> impl Future<Output = Result<BackfillReceipt, BackfillError>> + Send;+ fn backfill_calendar_events(+ &self,+ req: CalendarEventBackfillRequest,+ progress: Arc<JobProgress>,+ cancel: CancellationToken,+ ) -> impl Future<Output = Result<BackfillReceipt, BackfillError>> + Send;}pub struct BackfillOrchestrator<S, P, I> {@@ -310,6 +316,18 @@ where}).await}++ async fn backfill_calendar_events(+ &self,+ req: CalendarEventBackfillRequest,+ progress: Arc<JobProgress>,+ cancel: CancellationToken,+ ) -> Result<BackfillReceipt, BackfillError> {+ drain_source_with_cursor(&self.publisher, &progress, &cancel, |cursor| {+ self.source.fetch_calendar_events(&req, cursor)+ })+ .await+ }}#[cfg(test)]diff --git a/services/search_processing_service/src/inbound/kafka_consumer.rs b/services/search_processing_service/src/inbound/kafka_consumer.rsindex 6ac6eec0c7..ba5f13885c 100644--- a/services/search_processing_service/src/inbound/kafka_consumer.rs+++ b/services/search_processing_service/src/inbound/kafka_consumer.rs@@ -9,12 +9,14 @@//! Processing is retried in-process; exhausted events are logged and dropped//! because their offsets are already committed.//!-//! Per-entity event mapping and processing live in the [`call`], [`channel`],-//! [`chat`], [`document`], [`email`], [`project`], and [`property`] submodules;-//! this module owns the poll loop, worker, retry policy, and commit semantics.+//! Per-entity event mapping and processing live in the [`calendar_event`],+//! [`call`], [`channel`], [`chat`], [`document`], [`email`], [`project`], and+//! [`property`] submodules; this module owns the poll loop, worker, retry+//! policy, and commit semantics.#![allow(clippy::enum_variant_names)]+mod calendar_event;mod call;mod channel;mod chat;@@ -37,6 +39,7 @@ use std::{use ::call::domain::events::CallMacroEvent;use ::chat::domain::events::ChatMacroEvent;use ::email::domain::events::EmailMacroEvent;+use calendar_events::domain::events::CalendarMacroEvent;use channels::domain::broker_events::ChannelMacroEvent;use documents::domain::events::DocumentMacroEvent;use kafka_util::{GroupName, KafkaEventConsumer};@@ -54,6 +57,7 @@ use tokio::{sync::mpsc, task::JoinHandle};use tokio_retry::{Retry, strategy::ExponentialBackoff};use self::{+ calendar_event::process_calendar_event,call::process_call_event,channel::process_channel_event,chat::process_chat_event,@@ -79,6 +83,7 @@ type SearchProcessingKafkaConsumer =macro_event_broker::declare_topics!(DeclaredMacroEvent:+ CalendarMacroEvent,CallMacroEvent,ChannelMacroEvent,ChatMacroEvent,@@ -169,11 +174,13 @@ where/// Sharding key that serializes all events mutating the same search documents.////// Every topic's producer key is already the entity its index actions target-/// (call, channel, chat, document, project, or property-entity id), except-/// email events, which are produced with a link-scoped key and instead shard-/// by thread so one inbox's backlog can spread across the worker pool.+/// (calendar-event, call, channel, chat, document, project, or property-entity+/// id), except email events, which are produced with a link-scoped key and+/// instead shard by thread so one inbox's backlog can spread across the worker+/// pool.fn ordering_key(event: &DeclaredMacroEvent) -> Cow<'_, str> {match event {+ DeclaredMacroEvent::CalendarMacroEvent(event) => Cow::Borrowed(event.key()),DeclaredMacroEvent::CallMacroEvent(event) => Cow::Borrowed(event.key()),DeclaredMacroEvent::ChannelMacroEvent(event) => Cow::Borrowed(event.key()),DeclaredMacroEvent::ChatMacroEvent(event) => Cow::Borrowed(event.key()),@@ -264,6 +271,20 @@ async fn process_event(DeclaredMacroEvent::CallMacroEvent(event) => {process_call_event(db, opensearch_client, event, partition, offset).await}+ DeclaredMacroEvent::CalendarMacroEvent(event) => {+ if !context.calendar_search_enabled {+ // The offset is committed either way, so a change that arrives+ // while the flag is off is not replayed when it flips — the+ // backfill endpoint is how an enabled environment catches up.+ tracing::debug!(+ partition,+ offset,+ "calendar search is disabled; skipping calendar event"+ );+ return EventOutcome::Ignored;+ }+ process_calendar_event(context, event, partition, offset).await+ }DeclaredMacroEvent::ChannelMacroEvent(event) => {process_channel_event(db, opensearch_client, event, partition, offset).await}diff --git a/services/search_processing_service/src/inbound/kafka_consumer/calendar_event.rs b/services/search_processing_service/src/inbound/kafka_consumer/calendar_event.rsnew file mode 100644index 0000000000..e36e089aaa--- /dev/null+++ b/services/search_processing_service/src/inbound/kafka_consumer/calendar_event.rs@@ -0,0 +1,104 @@+//! Maps calendar event changes to search-index reconciliations and processes them.++use calendar_events::domain::events::{CalendarMacroEvent, CalendarTopicEvent};+use macro_event_broker::MacroEvent as _;+use sqs_client::search::calendar_event::UpsertCalendarEvent;++use super::{+ EventOutcome, KafkaProcessingContext, MAX_PROCESSING_ATTEMPTS, PROCESSING_RETRY_BASE_DELAY,+ retry_processing,+};+use crate::process::calendar_event::{remove_calendar_event, upsert_calendar_event};++/// What one topic event asks the index to do.+#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub(super) enum CalendarIndexAction {+ /// Re-read the row and write the resulting document.+ Reindex,+ /// Drop the document. The row is already gone, so there is nothing to+ /// read — reindexing would spend a query to learn that.+ Remove,+}++pub(super) fn index_action(event: &CalendarTopicEvent) -> (CalendarIndexAction, &'static str) {+ match event {+ CalendarTopicEvent::Created(_) => (CalendarIndexAction::Reindex, "calendar_event.created"),+ CalendarTopicEvent::Updated(_) => (CalendarIndexAction::Reindex, "calendar_event.updated"),+ CalendarTopicEvent::Deleted(_) => (CalendarIndexAction::Remove, "calendar_event.deleted"),+ }+}++/// Reconcile one event's search document.+pub(super) async fn process_calendar_event(+ context: &KafkaProcessingContext,+ event: &CalendarMacroEvent,+ partition: i32,+ offset: i64,+) -> EventOutcome {+ let opensearch_client = context.opensearch_client.as_ref();+ // The producer keys every calendar event by its entity id.+ let event_id = event.key().to_string();+ let (action, event_type) = index_action(&event.event().event);+ let result = retry_processing(|attempt| {+ let event_id = event_id.clone();+ async move {+ tracing::trace!(+ event_id = event_id,+ event_type,+ partition,+ offset,+ attempt,+ "processing calendar search-index event"+ );+ match action {+ CalendarIndexAction::Reindex => {+ upsert_calendar_event(+ opensearch_client,+ &context.db,+ &UpsertCalendarEvent {+ event_id: event_id.clone(),+ index_override: None,+ },+ )+ .await+ }+ CalendarIndexAction::Remove => {+ remove_calendar_event(opensearch_client, &event_id, None).await+ }+ }+ .inspect_err(|error| {+ if attempt < MAX_PROCESSING_ATTEMPTS {+ let retry_delay =+ PROCESSING_RETRY_BASE_DELAY * 2u32.pow(attempt.saturating_sub(1));+ tracing::warn!(+ error = ?error,+ event_id = event_id,+ event_type,+ partition,+ offset,+ attempt,+ delay_secs = retry_delay.as_secs(),+ "calendar search-index processing failed, retrying"+ );+ }+ })+ }+ })+ .await;++ match result {+ Ok(()) => EventOutcome::Indexed,+ Err(error) => {+ tracing::error!(+ error = ?error,+ event_id = event_id,+ event_type,+ partition,+ offset,+ attempts = MAX_PROCESSING_ATTEMPTS,+ "dropping calendar event after processing retries were exhausted"+ );+ EventOutcome::Dropped+ }+ }+}diff --git a/services/search_processing_service/src/inbound/kafka_consumer/context.rs b/services/search_processing_service/src/inbound/kafka_consumer/context.rsindex dfdc66112f..dc1bc8f914 100644--- a/services/search_processing_service/src/inbound/kafka_consumer/context.rs+++ b/services/search_processing_service/src/inbound/kafka_consumer/context.rs@@ -13,4 +13,6 @@ pub(crate) struct KafkaProcessingContext {pub(crate) s3_client: Arc<S3>,pub(crate) document_storage_bucket: String,pub(crate) lexical_client: Arc<LexicalClient>,+ /// Whether calendar events are written to the search index.+ pub(crate) calendar_search_enabled: bool,}diff --git a/services/search_processing_service/src/inbound/kafka_consumer/test.rs b/services/search_processing_service/src/inbound/kafka_consumer/test.rsindex 6c522b4ad5..571cc57549 100644--- a/services/search_processing_service/src/inbound/kafka_consumer/test.rs+++ b/services/search_processing_service/src/inbound/kafka_consumer/test.rs@@ -21,6 +21,9 @@ use ::email::domain::events::{ThreadStarredMetadata, ThreadTrashedMetadata, ThreadsReindexReason,ThreadsReindexRequestedMetadata,};+use calendar_events::domain::events::{+ CalendarEventMetadata, CalendarMacroEvent, CalendarTopicEvent,+};use channels::domain::{broker_events::{ChannelCreatedMetadata, ChannelDeletedMetadata, ChannelMessageAttachmentCreatedMetadata,@@ -40,8 +43,8 @@ use documents::domain::events::{};use macro_event_broker::{Event, EventBrokerError, MacroEvent as _, MessageParts};use macro_event_topics::{- MacroCallsTopic, MacroChannelsTopic, MacroChatsTopic, MacroDocumentsTopic, MacroEmailTopic,- MacroProjectsTopic, MacroPropertiesTopic, Topic as _,+ MacroCalendarTopic, MacroCallsTopic, MacroChannelsTopic, MacroChatsTopic, MacroDocumentsTopic,+ MacroEmailTopic, MacroProjectsTopic, MacroPropertiesTopic, Topic as _,};use macro_user_id::user_id::MacroUserIdStr;use model::document::FileType;@@ -1266,6 +1269,7 @@ fn subscribes_to_declared_search_processing_topics_with_durable_group() {assert!(topics.contains(&MacroEmailTopic::TOPIC_STR));assert!(topics.contains(&MacroProjectsTopic::TOPIC_STR));assert!(topics.contains(&MacroPropertiesTopic::TOPIC_STR));+ assert!(topics.contains(&MacroCalendarTopic::TOPIC_STR));}#[test]@@ -1541,6 +1545,71 @@ fn channel_envelope_decodes_round_trip() {assert_eq!(decoded_event.event().event, event);}+#[test]+fn calendar_envelope_decodes_round_trip_keyed_by_event_id() {+ let event_id = uuid::Uuid::now_v7();+ let event = CalendarTopicEvent::Updated(CalendarEventMetadata {+ event_id,+ owner_id: "macro|user".to_string(),+ });+ let message = encoded_message(+ MacroCalendarTopic::TOPIC_STR,+ &event_id.to_string(),+ Event::new(event.clone()),+ );++ let decoded = DeclaredMacroEvent::decode(&message).expect("decodable calendar event");+ let DeclaredMacroEvent::CalendarMacroEvent(decoded_event) = decoded else {+ panic!("expected calendar event");+ };+ assert_eq!(decoded_event.key(), event_id.to_string());+ assert_eq!(decoded_event.event().event, event);+}++#[test]+fn calendar_variants_choose_reindex_or_remove() {+ // A deletion has no row left to read, so it drops the document directly+ // instead of spending a query to learn the row is gone.+ let metadata = CalendarEventMetadata {+ event_id: uuid::Uuid::now_v7(),+ owner_id: "macro|user".to_string(),+ };+ assert_eq!(+ super::calendar_event::index_action(&CalendarTopicEvent::Created(metadata.clone())).0,+ super::calendar_event::CalendarIndexAction::Reindex+ );+ assert_eq!(+ super::calendar_event::index_action(&CalendarTopicEvent::Updated(metadata.clone())).0,+ super::calendar_event::CalendarIndexAction::Reindex+ );+ assert_eq!(+ super::calendar_event::index_action(&CalendarTopicEvent::Deleted(metadata)).0,+ super::calendar_event::CalendarIndexAction::Remove+ );+}++#[test]+fn calendar_events_shard_by_event_id_so_one_event_stays_ordered() {+ // Two changes to one event must land on the same worker, or a later+ // update could be indexed before an earlier one.+ let event_id = uuid::Uuid::now_v7();+ let first = DeclaredMacroEvent::CalendarMacroEvent(CalendarMacroEvent::for_change(+ CalendarTopicEvent::Created(CalendarEventMetadata {+ event_id,+ owner_id: "macro|user".to_string(),+ }),+ ));+ // A different variant for the same entity must still shard together.+ let second = DeclaredMacroEvent::CalendarMacroEvent(CalendarMacroEvent::for_change(+ CalendarTopicEvent::Deleted(CalendarEventMetadata {+ event_id,+ owner_id: "macro|user".to_string(),+ }),+ ));+ assert_eq!(ordering_key(&first), event_id.to_string());+ assert_eq!(ordering_key(&first), ordering_key(&second));+}+#[test]fn project_envelope_decodes_round_trip_with_string_key() {let event = ProjectTopicEvent::Restored(ProjectRestoredMetadata {diff --git a/services/search_processing_service/src/main.rs b/services/search_processing_service/src/main.rsindex c823171a9a..827a988da5 100644--- a/services/search_processing_service/src/main.rs+++ b/services/search_processing_service/src/main.rs@@ -254,6 +254,7 @@ async fn main() -> anyhow::Result<()> {s3_client: s3_client.clone(),opensearch_client: opensearch_client.clone(),lexical_client: lexical_client.clone(),+ calendar_search_enabled: config.calendar_search_enabled,};run_search_processing_workers(search_processing_context, config.worker_count);@@ -263,6 +264,7 @@ async fn main() -> anyhow::Result<()> {s3_client: s3_client.clone(),document_storage_bucket: config.document_storage_bucket.to_string(),lexical_client,+ calendar_search_enabled: config.calendar_search_enabled,};tokio::spawn(supervise_event_consumer(config.kafka_brokers.as_ref().to_owned(),diff --git a/services/search_processing_service/src/outbound/source.rs b/services/search_processing_service/src/outbound/source.rsindex 24476efd96..3292403b99 100644--- a/services/search_processing_service/src/outbound/source.rs+++ b/services/search_processing_service/src/outbound/source.rs@@ -5,16 +5,17 @@ use model::document::FileType;use models_properties::EntityType;use sqlx::PgPool;use sqs_client::search::{- SearchQueueMessage, call::CallRecordMessage, channel::ChannelMessageUpdate, chat::ChatMessage,- email::EmailThreadBatchMessage, project::UpsertProject,+ SearchQueueMessage, calendar_event::UpsertCalendarEvent, call::CallRecordMessage,+ channel::ChannelMessageUpdate, chat::ChatMessage, email::EmailThreadBatchMessage,+ project::UpsertProject,};use crate::config::BackfillPageSizes;use crate::domain::models::{- BackfillError, CallBackfillCursor, CallBackfillRequest, ChannelBackfillRequest,- ChatBackfillCursor, ChatBackfillRequest, DocumentBackfillCursor, DocumentBackfillRequest,- EmailBackfillRequest, ProjectBackfillCursor, ProjectBackfillRequest, PropertiesBackfillRequest,- PropertySourcePage, SourcePage,+ BackfillError, CalendarEventBackfillCursor, CalendarEventBackfillRequest, CallBackfillCursor,+ CallBackfillRequest, ChannelBackfillRequest, ChatBackfillCursor, ChatBackfillRequest,+ DocumentBackfillCursor, DocumentBackfillRequest, EmailBackfillRequest, ProjectBackfillCursor,+ ProjectBackfillRequest, PropertiesBackfillRequest, PropertySourcePage, SourcePage,};use crate::domain::ports::BackfillSource;@@ -362,6 +363,46 @@ impl BackfillSource for PgBackfillSource {})}+ async fn fetch_calendar_events(+ &self,+ req: &CalendarEventBackfillRequest,+ cursor: Option<CalendarEventBackfillCursor>,+ ) -> Result<(SourcePage, Option<CalendarEventBackfillCursor>), BackfillError> {+ let db_cursor = cursor.map(|c| (c.updated_at, c.event_id));+ let batch = macro_db_client::calendar_event::get_events_for_backfill::get_calendar_events_for_search_backfill(+ &self.db,+ self.page_sizes.calendar_events as i64,+ db_cursor,+ req.updated_after,+ req.updated_before,+ )+ .await+ .map_err(BackfillError::Source)?;++ let next_cursor = batch.last().map(|event| CalendarEventBackfillCursor {+ updated_at: event.updated_at,+ event_id: event.event_id,+ });+ let rows_consumed = batch.len();+ let messages: Vec<SearchQueueMessage> = batch+ .into_iter()+ .map(|event| {+ SearchQueueMessage::UpsertCalendarEvent(UpsertCalendarEvent {+ event_id: event.event_id.to_string(),+ index_override: req.index_override.clone(),+ })+ })+ .collect();++ Ok((+ SourcePage {+ messages,+ rows_consumed,+ },+ next_cursor,+ ))+ }+async fn fetch_projects(&self,req: &ProjectBackfillRequest,diff --git a/services/search_processing_service/src/process.rs b/services/search_processing_service/src/process.rsindex f996a3124b..64fe032af4 100644--- a/services/search_processing_service/src/process.rs+++ b/services/search_processing_service/src/process.rs@@ -1,3 +1,4 @@+pub(crate) mod calendar_event;pub(crate) mod call;pub(crate) mod channel;pub(crate) mod chat;@@ -135,6 +136,20 @@ pub async fn process_message(SearchQueueMessage::UpsertProject(message) => {project::upsert_project(&ctx.opensearch_client, &ctx.db, &message).await?;}+ SearchQueueMessage::UpsertCalendarEvent(message) => {+ // Logged rather than silent: running the calendar backfill against+ // an environment where the flag is off should say why nothing+ // happened instead of reporting a successful no-op.+ if ctx.calendar_search_enabled {+ calendar_event::upsert_calendar_event(&ctx.opensearch_client, &ctx.db, &message)+ .await?;+ } else {+ tracing::info!(+ event_id = message.event_id,+ "calendar search is disabled; not indexing calendar event"+ );+ }+ }}ctx.worker.cleanup_message(message).await?;diff --git a/services/search_processing_service/src/process/calendar_event.rs b/services/search_processing_service/src/process/calendar_event.rsnew file mode 100644index 0000000000..fc7d11db3f--- /dev/null+++ b/services/search_processing_service/src/process/calendar_event.rs@@ -0,0 +1,116 @@+use anyhow::Context;+use models_properties::EntityType;+use opensearch_client::{+ OpensearchClient,+ date_format::EpochMillis,+ upsert::{calendar_event::UpsertCalendarEventArgs, properties::IndexedProperty},+};+use properties::outbound::entity_properties_get_query::get_entity_properties_for_index;+use sqs_client::search::calendar_event::UpsertCalendarEvent;++/// Fetch the event's indexed properties, flattened for the search index.+async fn fetch_indexed_properties(+ db: &sqlx::Pool<sqlx::Postgres>,+ event_id: &str,+) -> anyhow::Result<Vec<IndexedProperty>> {+ let properties = get_entity_properties_for_index(db, event_id, EntityType::CalendarEvent)+ .await+ .context("failed to fetch calendar event properties for search index")?;+ Ok(properties+ .into_iter()+ .map(|p| IndexedProperty {+ definition_id: p.definition_id,+ values: p.values,+ number_value: p.number_value,+ date_value: p.date_value,+ })+ .collect())+}++/// Handles upserting a calendar event series master into the opensearch index.+///+/// Re-reads the event row so the indexed doc always reflects current state; a+/// missing row turns the upsert into a removal. Only the master is indexed —+/// recurring instances are materialized inside a rolling window, so search+/// resolves the relevant occurrence at query time instead.+#[tracing::instrument(skip(opensearch_client, db), err)]+pub async fn upsert_calendar_event(+ opensearch_client: &OpensearchClient,+ db: &sqlx::Pool<sqlx::Postgres>,+ message: &UpsertCalendarEvent,+) -> anyhow::Result<()> {+ let index_override = message.index_override.as_deref();++ let event_id = uuid::Uuid::parse_str(&message.event_id)+ .context("calendar event id is not a valid uuid")?;++ let event = macro_db_client::calendar_event::get_event_for_index::get_calendar_event_for_index(+ db, event_id,+ )+ .await+ .context("failed to get calendar event for search")?;++ let Some(event) = event else {+ tracing::trace!("calendar event row is gone, removing from search index");+ opensearch_client+ .delete_calendar_event(&message.event_id, index_override)+ .await+ .context("failed to delete missing calendar event from search")?;+ return Ok(());+ };++ let properties = fetch_indexed_properties(db, &message.event_id).await?;++ let starts_at_millis = event+ .starts_at+ .map(|t| EpochMillis::new(t.timestamp_millis()))+ .transpose()?;+ let ends_at_millis = event+ .ends_at+ .map(|t| EpochMillis::new(t.timestamp_millis()))+ .transpose()?;++ opensearch_client+ .upsert_calendar_event(+ &UpsertCalendarEventArgs {+ event_id: event.id.to_string(),+ title: event.title,+ owner_id: event.owner_id,+ source_link_id: event.source_link_id.to_string(),+ ical_uid: event.ical_uid,+ status: event.status,+ is_recurring: event.is_recurring,+ starts_at_millis,+ ends_at_millis,+ start_date: event.start_date.map(|d| d.to_string()),+ end_date: event.end_date.map(|d| d.to_string()),+ organizer_email: event.organizer_email,+ attendee_emails: event.attendee_emails,+ created_at_millis: EpochMillis::new(event.created_at.timestamp_millis())?,+ updated_at_millis: EpochMillis::new(event.updated_at.timestamp_millis())?,+ properties,+ },+ index_override,+ )+ .await+ .context("failed to upsert calendar event")?;++ Ok(())+}++/// Removes a calendar event from the opensearch index.+///+/// Used when the topic reports a deletion: the row is already gone, so there+/// is nothing to re-read and reindexing would only spend a query learning that.+#[tracing::instrument(skip(opensearch_client), err)]+pub async fn remove_calendar_event(+ opensearch_client: &OpensearchClient,+ event_id: &str,+ index_override: Option<&str>,+) -> anyhow::Result<()> {+ opensearch_client+ .delete_calendar_event(event_id, index_override)+ .await+ .context("failed to delete calendar event from search")?;+ Ok(())+}diff --git a/services/search_processing_service/src/process/context.rs b/services/search_processing_service/src/process/context.rsindex f69769442e..eac373d468 100644--- a/services/search_processing_service/src/process/context.rs+++ b/services/search_processing_service/src/process/context.rs@@ -8,4 +8,6 @@ pub struct SearchProcessingContext {pub s3_client: Arc<s3_client::S3>,pub opensearch_client: Arc<opensearch_client::OpensearchClient>,pub lexical_client: Arc<lexical_client::LexicalClient>,+ /// Whether calendar events are written to the search index.+ pub calendar_search_enabled: bool,}diff --git a/tooling/xtask/crates/xtask_local/src/local/local_env.rs b/tooling/xtask/crates/xtask_local/src/local/local_env.rsindex 671eb52623..405d1baa7c 100644--- a/tooling/xtask/crates/xtask_local/src/local/local_env.rs+++ b/tooling/xtask/crates/xtask_local/src/local/local_env.rs@@ -75,6 +75,9 @@ impl LocalEnv {// deployed envs); local stacks keep it on for development.env.insert("CALENDAR_SYNC_ENABLED".into(), "true".into());env.insert("CALENDAR_SCOPE_ENABLED".into(), "true".into());+ // Calendar search ships dark too: off in deployed envs until each has+ // its calendar index created and backfilled.+ env.insert("CALENDAR_SEARCH_ENABLED".into(), "true".into());self.infra.write(&mut env);self.storage.write(&mut env);self.queues.write(&mut env);