Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(calendar): stop one unreadable Outlook event blocking calendar sync (#9573)
gbirmanc7df8c3584ad
diff --git a/crates/calendar_events/AGENTS.md b/crates/calendar_events/AGENTS.mdindex c4e6a8b3cb..a95fc7ef6b 100644--- a/crates/calendar_events/AGENTS.md+++ b/crates/calendar_events/AGENTS.md@@ -359,6 +359,18 @@ reminder dispatch, team availability, and provider/Postgres persistence.removing a source whose typed `event_type` is out of office raises it, sinceevery sync of a removed meeting's attendees reaches their teammates' calendarsand the hint blanks their overlay.+- The Outlook sync (`src/domain/outlook.rs`) refreshes a page's events one+ at a time, first pending id first, and retries that id after any error, so+ one error blocks the calendar. An event the adapter cannot normalize is not+ an error: it comes back as `OutlookEventRefresh::Unreadable` with the Graph+ property it rejected, and the sync skips it, as Google never shows an event+ it cannot normalize. The event is absent: a copy stored before is retired+ (`OutlookCalendarProgress::Unreadable`), since without the calendar error+ Google shows beside its old copy a stale copy would look current, but its+ page membership stays so later tombstones resolve. The round still finishes+ as a clean sync. The warning `skipping an Outlook event Macro cannot read`+ and the `calendar.outlook_sync` span carry `calendar_id`,+ `provider_event_id` and `rejected_field`.- A sync of a `ready`, already synced account stays `ready`, on Google as onOutlook, including after a re-grant that kept calendar access, whose sync tokenssurvive. Any other state is catching up and marks it `syncing`: a first import,diff --git a/crates/calendar_events/src/domain/outlook.rs b/crates/calendar_events/src/domain/outlook.rsindex cc7aa47d29..a3071ff2ed 100644--- a/crates/calendar_events/src/domain/outlook.rs+++ b/crates/calendar_events/src/domain/outlook.rs@@ -66,12 +66,35 @@ pub enum OutlookCalendarProgress {/// Whether the current provider event still exists.exists: bool,},+ /// The event exists but Macro cannot read it. Retire any copy stored+ /// before, which would show a version Microsoft no longer has, but keep+ /// the event's page membership so later tombstones still resolve.+ Unreadable {+ /// Master/single ID from this lease's pending work.+ id: String,+ },/// Finish a page, or atomically prune and commit the terminal cursor.Finish,/// Restart an expired or unresolvable delta with a new full snapshot.Reset,}+/// One provider event read back for synchronization.+pub enum OutlookEventRefresh {+ /// The current event or complete series, normalized.+ Refreshed(Box<CalendarEventUpsert>),+ /// Microsoft no longer has the event on this calendar.+ Gone,+ /// Microsoft has the event, but Macro cannot represent it. The sync skips+ /// it and retires any copy stored before, so the calendar never shows an+ /// event Macro could not read, as Google sync never shows one.+ Unreadable {+ /// Path of the Graph property Macro rejected, such as+ /// `recurrence.range.endDate` or `instances.start.dateTime`.+ field: String,+ },+}+/// Provider reads with normalized results and no persistence policy.pub trait OutlookCalendarReader: Send + Sync + 'static {/// Discover all visible calendars, following every page.@@ -94,7 +117,7 @@ pub trait OutlookCalendarReader: Send + Sync + 'static {token: &str,target: &ProviderCalendarTarget,id: &str,- ) -> impl Future<Output = Result<Option<CalendarEventUpsert>, CalendarProviderError>> + Send;+ ) -> impl Future<Output = Result<OutlookEventRefresh, CalendarProviderError>> + Send;}/// Calendar-owned persistence and exact-generation leases.@@ -195,7 +218,23 @@ impl<}Ok(true)}+ #[tracing::instrument(+ name = "calendar.outlook_sync",+ skip_all,+ fields(+ work_id = %lease.id,+ account_id = %lease.account_id,+ calendar_id = lease.target.as_ref().map(|target| tracing::field::display(target.calendar_id)),+ step = tracing::field::Empty,+ provider_event_id = tracing::field::Empty,+ outcome = tracing::field::Empty,+ rejected_field = tracing::field::Empty,+ error_kind = tracing::field::Empty,+ ),+ err(level = tracing::Level::DEBUG)+ )]async fn run_claim(&self, lease: &OutlookCalendarLease) -> Result<(), CalendarProviderError> {+ let span = tracing::Span::current();let token = self.tokens.fetch_access_token(&lease.token_identity)@@ -210,29 +249,48 @@ impl<},"Microsoft calendar credential acquisition failed",)- })?;+ })+ .inspect_err(|error| record_failure(&span, error))?;let work = async {if let Some(target) = &lease.target {let progress = if let Some(id) = lease.pending_ids.first() {- let upsert = self+ span.record("step", "event");+ span.record("provider_event_id", id.as_str());+ let id = id.clone();+ match self.provider- .refresh_calendar_event(&token, target, id)- .await?;- let exists = upsert.is_some();- if let Some(upsert) = upsert {- self.repository- .upsert_event(CalendarEventWrite::OutlookSync {- lease: Box::new(lease.clone()),- upsert,- })- .await- .map_err(storage_error)?;- }- OutlookCalendarProgress::Event {- id: id.clone(),- exists,+ .refresh_calendar_event(&token, target, &id)+ .await?+ {+ OutlookEventRefresh::Refreshed(upsert) => {+ span.record("outcome", "refreshed");+ self.repository+ .upsert_event(CalendarEventWrite::OutlookSync {+ lease: Box::new(lease.clone()),+ upsert: *upsert,+ })+ .await+ .map_err(storage_error)?;+ OutlookCalendarProgress::Event { id, exists: true }+ }+ OutlookEventRefresh::Gone => {+ span.record("outcome", "gone");+ OutlookCalendarProgress::Event { id, exists: false }+ }+ OutlookEventRefresh::Unreadable { field } => {+ span.record("outcome", "unreadable");+ span.record("rejected_field", field.as_str());+ tracing::warn!(+ calendar_id = %target.calendar_id,+ provider_event_id = %id,+ rejected_field = %field,+ "skipping an Outlook event Macro cannot read"+ );+ OutlookCalendarProgress::Unreadable { id }+ }}} else if lease.page_loaded {+ span.record("step", "finish");automatic_decline::run(&self.repository,&self.provider,@@ -243,6 +301,7 @@ impl<.await?;OutlookCalendarProgress::Finish} else {+ span.record("step", "page");match self.provider.calendar_page(@@ -267,6 +326,7 @@ impl<.await.map_err(storage_error)?;} else {+ span.record("step", "discover");let calendars = self.provider.calendars(&token, lease.binding).await?;self.repository.commit_outlook_calendars(lease, calendars)@@ -291,9 +351,15 @@ impl<futures::future::Either::Left((result, _))| futures::future::Either::Right((result, _)) => result,}+ .inspect_err(|error| record_failure(&span, error))}}+fn record_failure(span: &tracing::Span, error: &CalendarProviderError) {+ span.record("outcome", "failed");+ span.record("error_kind", tracing::field::debug(error.kind()));+}+fn storage_error(_error: Report) -> CalendarProviderError {CalendarProviderError::new(CalendarProviderErrorKind::Transient,diff --git a/crates/calendar_events/src/outbound/pg/outlook.rs b/crates/calendar_events/src/outbound/pg/outlook.rsindex b6fd09e3fd..ba9bab9f3a 100644--- a/crates/calendar_events/src/outbound/pg/outlook.rs+++ b/crates/calendar_events/src/outbound/pg/outlook.rs@@ -156,16 +156,9 @@ impl OutlookCalendarRepository for PgCalendarRepository {}}OutlookCalendarProgress::Event { id, exists } => {- if state.pending_ids.first() != Some(&id) || !state.page_loaded {- return Err(rootcause::report!(- "calendar event acknowledgment does not match pending work"- ));- }+ acknowledge_event(&state.pending_ids, state.page_loaded, &id)?;if !exists {- let ids=sqlx::query_scalar!("DELETE FROM calendar_event_sources WHERE source_kind='outlook' AND calendar_id=$1 AND provider_event_id=$2 RETURNING event_id",target.calendar_id,&id).fetch_all(&mut *tx).await.map_err(report)?;- for id in ids {- restore_best_source_or_delete(&mut tx, &mut log, id).await?;- }+ retire_provider_event(&mut tx, &mut log, target.calendar_id, &id).await?;sqlx::query!("DELETE FROM calendar_outlook_members WHERE work_id=$1 AND master_id=$2",lease.id,@@ -175,7 +168,12 @@ impl OutlookCalendarRepository for PgCalendarRepository {.await.map_err(report)?;}- sqlx::query!("UPDATE calendar_outlook_work SET pending_ids=array_remove(pending_ids,$2) WHERE id=$1",lease.id,&id).execute(&mut *tx).await.map_err(report)?;+ remove_pending(&mut tx, lease.id, &id).await?;+ }+ OutlookCalendarProgress::Unreadable { id } => {+ acknowledge_event(&state.pending_ids, state.page_loaded, &id)?;+ retire_provider_event(&mut tx, &mut log, target.calendar_id, &id).await?;+ remove_pending(&mut tx, lease.id, &id).await?;}OutlookCalendarProgress::Finish => {if !state.pending_ids.is_empty() || !state.page_loaded {@@ -452,6 +450,44 @@ impl OutlookCalendarRepository for PgCalendarRepository {}}+fn acknowledge_event(pending_ids: &[String], page_loaded: bool, id: &str) -> Result<(), Report> {+ if pending_ids.first().map(String::as_str) != Some(id) || !page_loaded {+ return Err(rootcause::report!(+ "calendar event acknowledgment does not match pending work"+ ));+ }+ Ok(())+}++async fn retire_provider_event(+ tx: &mut Transaction<'_, Postgres>,+ log: &mut ChangeLogBatch,+ calendar_id: Uuid,+ id: &str,+) -> Result<(), Report> {+ let ids=sqlx::query_scalar!("DELETE FROM calendar_event_sources WHERE source_kind='outlook' AND calendar_id=$1 AND provider_event_id=$2 RETURNING event_id",calendar_id,id).fetch_all(&mut **tx).await.map_err(report)?;+ for id in ids {+ restore_best_source_or_delete(tx, log, id).await?;+ }+ Ok(())+}++async fn remove_pending(+ tx: &mut Transaction<'_, Postgres>,+ work_id: Uuid,+ id: &str,+) -> Result<(), Report> {+ sqlx::query!(+ "UPDATE calendar_outlook_work SET pending_ids=array_remove(pending_ids,$2) WHERE id=$1",+ work_id,+ id+ )+ .execute(&mut **tx)+ .await+ .map_err(report)?;+ Ok(())+}+async fn reset_cursor(tx: &mut Transaction<'_, Postgres>, id: Uuid) -> Result<(), Report> {sqlx::query!("UPDATE calendar_outlook_work SET cursor=NULL,page_cursor=NULL,next_page=NULL,terminal_cursor=NULL,pending_ids='{}',visited_pages='{}',page_loaded=false,full_scan=true,scan_id=gen_random_uuid() WHERE id=$1",id).execute(&mut **tx).await.map_err(report)?;Ok(())diff --git a/crates/email_api_client/AGENTS.md b/crates/email_api_client/AGENTS.mdindex c50a4aae75..4d02b4492b 100644--- a/crates/email_api_client/AGENTS.md+++ b/crates/email_api_client/AGENTS.md@@ -105,6 +105,14 @@ Outlook (Microsoft Graph) outbound adapters.- `domain::request_count::counted` counts the Graph requests one unit of worksends (the transport calls `record_sent`); Outlook token failures surfaceas `EmailApiError::CredentialsUnavailable`, apart from Graph failures.+- The Outlook calendar normalizer (`outlook/calendar/normalize.rs`,+ `recurrence.rs`) fails with `Rejected`, which names the Graph property+ path (`recurrence.range.endDate`, `instances.start.dateTime`), built only+ from literals so its log carries no event content, so the sync can skip and+ log that one event. Give a new check a `Rejected`, not `invalid()`. Graph names UTC `tzone://Microsoft/Utc` on meetings Exchange+ imported from another system's invitation, which `zones::zone` maps; a+ series end date in a legacy custom zone (`tzone://Microsoft/Custom`) is+ read in the zone the event's time is kept in.- Same-mailbox Outlook replies use `createReply`. When Graph refuses it with400 `ErrorInvalidOperation`, `create_draft` creates a standalone draft thatcarries the same correlation and RFC reply headers.diff --git a/crates/email_api_client/src/outbound/outlook/calendar.rs b/crates/email_api_client/src/outbound/outlook/calendar.rsindex 544b9510e0..5595d7f21b 100644--- a/crates/email_api_client/src/outbound/outlook/calendar.rs+++ b/crates/email_api_client/src/outbound/outlook/calendar.rs@@ -35,6 +35,24 @@ fn invalid() -> CalendarProviderError {"Microsoft returned an invalid calendar response",)}+/// A Graph event property Macro cannot represent, named by its path in the+/// event, such as `recurrence.range.endDate`. Paths are built only from+/// literals, so a log of one never carries event content.+#[derive(Debug, PartialEq, Eq)]+struct Rejected(String);+impl Rejected {+ fn at(field: &'static str) -> Self {+ Self(field.to_owned())+ }+ fn under(self, parent: &'static str) -> Self {+ Self(format!("{parent}.{}", self.0))+ }+}+impl From<Rejected> for CalendarProviderError {+ fn from(_: Rejected) -> Self {+ invalid()+ }+}fn unsupported(message: &str) -> CalendarProviderError {CalendarProviderError::new(CalendarProviderErrorKind::Permanent, message)}@@ -56,11 +74,14 @@ fn error(error: EmailApiError) -> CalendarProviderError {},)}-fn string<'a>(value: &'a Value, key: &str) -> Result<&'a str, CalendarProviderError> {+fn string<'a>(value: &'a Value, key: &'static str) -> Result<&'a str, CalendarProviderError> {+ Ok(field(value, key)?)+}+fn field<'a>(value: &'a Value, key: &'static str) -> Result<&'a str, Rejected> {value[key].as_str().filter(|s| !s.is_empty())- .ok_or_else(invalid)+ .ok_or_else(|| Rejected::at(key))}fn access(binding: CalendarGrantBinding) -> MailboxAccess {MailboxAccess {@@ -217,19 +238,27 @@ impl OutlookApiClientRepository {);Ok(self.calendar_pages(token, url).await?.0)}+ async fn calendar_occurrences(+ &self,+ token: &AccessToken,+ target: &ProviderCalendarTarget,+ master: &Value,+ ) -> Result<Vec<Value>, CalendarProviderError> {+ if master["type"].as_str() == Some("seriesMaster") {+ self.calendar_instances(token, target, string(master, "id")?)+ .await+ } else {+ Ok(vec![master.clone()])+ }+ }async fn calendar_echo(&self,token: &AccessToken,target: &ProviderCalendarTarget,master: Value,) -> Result<CalendarEventUpsert, CalendarProviderError> {- let instances = if master["type"].as_str() == Some("seriesMaster") {- self.calendar_instances(token, target, string(&master, "id")?)- .await?- } else {- vec![master.clone()]- };- normalize::projection(target, master, instances)+ let instances = self.calendar_occurrences(token, target, &master).await?;+ Ok(normalize::projection(target, master, instances)?)}async fn calendar_refresh(&self,@@ -426,10 +455,17 @@ impl OutlookCalendarReader for OutlookApiClientRepository {token: &str,target: &ProviderCalendarTarget,id: &str,- ) -> Result<Option<CalendarEventUpsert>, CalendarProviderError> {- self.calendar_scope(target)?- .calendar_refresh(&AccessToken::new(token.to_owned()), target, id)- .await+ ) -> Result<OutlookEventRefresh, CalendarProviderError> {+ let api = self.calendar_scope(target)?;+ let token = AccessToken::new(token.to_owned());+ let Some(master) = api.calendar_event(&token, target, id).await? else {+ return Ok(OutlookEventRefresh::Gone);+ };+ let instances = api.calendar_occurrences(&token, target, &master).await?;+ Ok(match normalize::projection(target, master, instances) {+ Ok(upsert) => OutlookEventRefresh::Refreshed(Box::new(upsert)),+ Err(Rejected(field)) => OutlookEventRefresh::Unreadable { field },+ })}}diff --git a/crates/email_api_client/src/outbound/outlook/calendar/normalize.rs b/crates/email_api_client/src/outbound/outlook/calendar/normalize.rsindex d755cb119f..0d7b637d57 100644--- a/crates/email_api_client/src/outbound/outlook/calendar/normalize.rs+++ b/crates/email_api_client/src/outbound/outlook/calendar/normalize.rs@@ -1,28 +1,32 @@use super::*;use chrono::{DateTime, NaiveDate, NaiveDateTime, TimeZone, Utc};+const DECLINE_FIELD: &str = "singleValueExtendedProperties.automaticDecline";+const INSTANCE_DECLINE_FIELD: &str = "instances.singleValueExtendedProperties.automaticDecline";+#[cfg(test)]mod test;-pub(super) fn instant(value: &Value) -> Result<DateTime<Utc>, CalendarProviderError> {- let text = string(value, "dateTime")?;+pub(super) fn instant(value: &Value) -> Result<DateTime<Utc>, Rejected> {+ let text = field(value, "dateTime")?;if let Ok(instant) = DateTime::parse_from_rfc3339(text) {return Ok(instant.with_timezone(&Utc));}- let local =- NaiveDateTime::parse_from_str(text, "%Y-%m-%dT%H:%M:%S%.f").map_err(|_| invalid())?;+ let local = NaiveDateTime::parse_from_str(text, "%Y-%m-%dT%H:%M:%S%.f")+ .map_err(|_| Rejected::at("dateTime"))?;// Requests receive UTC by default. Never guess an unknown Windows zone or// choose one side of an ambiguous local-time response silently.- let zone = zones::zone(value["timeZone"].as_str().unwrap_or("UTC")).ok_or_else(invalid)?;+ let zone = zones::zone(value["timeZone"].as_str().unwrap_or("UTC"))+ .ok_or_else(|| Rejected::at("timeZone"))?;zone.from_local_datetime(&local).single().map(|v| v.with_timezone(&Utc))- .ok_or_else(invalid)+ .ok_or_else(|| Rejected::at("dateTime"))}-pub(super) fn event_time(value: &Value) -> Result<EventTime, CalendarProviderError> {- let start = instant(&value["start"])?;- let end = instant(&value["end"])?;+pub(super) fn event_time(value: &Value) -> Result<EventTime, Rejected> {+ let start = instant(&value["start"]).map_err(|e| e.under("start"))?;+ let end = instant(&value["end"]).map_err(|e| e.under("end"))?;let zone_name = value["originalStartTimeZone"].as_str().or(value["start"]["timeZone"].as_str())@@ -45,17 +49,17 @@ pub(super) fn event_time(value: &Value) -> Result<EventTime, CalendarProviderErr}};if !time.is_valid() {- return Err(invalid());+ return Err(Rejected::at("end"));}Ok(time)}-pub(super) fn original(value: &Value) -> Result<Option<EventStart>, CalendarProviderError> {+pub(super) fn original(value: &Value) -> Result<Option<EventStart>, Rejected> {let Some(text) = value["originalStart"].as_str() else {return Ok(None);};let instant = DateTime::parse_from_rfc3339(text)- .map_err(|_| invalid())?+ .map_err(|_| Rejected::at("originalStart"))?.with_timezone(&Utc);if value["isAllDay"].as_bool() == Some(true) {let zone = zones::zone(value["originalStartTimeZone"].as_str().unwrap_or("UTC"))@@ -174,14 +178,14 @@ pub(super) fn projection(target: &ProviderCalendarTarget,master: Value,instances: Vec<Value>,-) -> Result<CalendarEventUpsert, CalendarProviderError> {+) -> Result<CalendarEventUpsert, Rejected> {let event_id = Uuid::now_v7();let time = event_time(&master)?;let updated = master["lastModifiedDateTime"].as_str().and_then(|v| DateTime::parse_from_rfc3339(v).ok()).map(|v| v.with_timezone(&Utc))- .ok_or_else(invalid)?;+ .ok_or_else(|| Rejected::at("lastModifiedDateTime"))?;let created = master["createdDateTime"].as_str().and_then(|v| DateTime::parse_from_rfc3339(v).ok())@@ -196,7 +200,7 @@ pub(super) fn projection(let event = CalendarEvent {id: event_id,owner_id: target.owner_id.clone(),- ical_uid: string(&master, "iCalUId")?.into(),+ ical_uid: field(&master, "iCalUId")?.into(),calendar_id: Some(target.calendar_id),sources: vec![],title: master["subject"].as_str().unwrap_or_default().into(),@@ -258,11 +262,11 @@ pub(super) fn projection(if instance["isCancelled"].as_bool() == Some(true) {continue;}- let time = event_time(&instance)?;- let original = original(&instance)?;+ let time = event_time(&instance).map_err(|e| e.under("instances"))?;+ let original = original(&instance).map_err(|e| e.under("instances"))?;let recurrence_id = original.as_ref().map(EventStart::occurrence_key);if instance["type"].as_str() == Some("exception") {- let original_time = original.ok_or_else(invalid)?;+ let original_time = original.ok_or_else(|| Rejected::at("instances.originalStart"))?;let instance_preferences = preferences(&instance);// Graph may omit inherited extended properties. Preserve the series'// extra reminders unless this instance carries its own settings or@@ -279,7 +283,7 @@ pub(super) fn projection(.cloned().map(serde_json::from_value).transpose()- .map_err(|_| invalid())?,+ .map_err(|_| Rejected::at(INSTANCE_DECLINE_FIELD))?,recurrence_id: original_time.occurrence_key(),original_time,time: time.clone(),@@ -324,7 +328,7 @@ pub(super) fn projection(.cloned().map(serde_json::from_value).transpose()- .map_err(|_| invalid())?+ .map_err(|_| Rejected::at(DECLINE_FIELD))?} else {None},@@ -333,7 +337,7 @@ pub(super) fn projection(account_id: target.account_id,calendar_id: target.calendar_id,observed_access_role: target.observed_access_role.clone(),- provider_event_id: string(&master, "id")?.into(),+ provider_event_id: field(&master, "id")?.into(),provider_recurring_event_id: None,provider_etag: master["@odata.etag"].as_str().map(str::to_owned),raw_payload: master,diff --git a/crates/email_api_client/src/outbound/outlook/calendar/recurrence.rs b/crates/email_api_client/src/outbound/outlook/calendar/recurrence.rsindex a88ed5cabd..ceec301132 100644--- a/crates/email_api_client/src/outbound/outlook/calendar/recurrence.rs+++ b/crates/email_api_client/src/outbound/outlook/calendar/recurrence.rs@@ -1,6 +1,7 @@//! Exact mappings for the recurrence patterns supported by Microsoft Graph.use super::*;use chrono::{Datelike, NaiveDate, TimeZone};+use chrono_tz::Tz;use std::collections::BTreeMap;const DAYS: [(&str, &str); 7] = [@@ -20,21 +21,24 @@ const INDICES: [(&str, &str); 5] = [("-1", "last"),];-pub(super) fn from_graph(event: &Value) -> Result<Vec<String>, CalendarProviderError> {+pub(super) fn from_graph(event: &Value) -> Result<Vec<String>, Rejected> {let recurrence = &event["recurrence"];if recurrence.is_null() {return Ok(vec![]);}let p = &recurrence["pattern"];let r = &recurrence["range"];- let kind = string(p, "type")?;- let (_, _, start, _) = normalize::time_body(&normalize::event_time(event)?)?;+ let pattern = |key: &'static str| Rejected::at(key).under("recurrence.pattern");+ let range = |key: &'static str| Rejected::at(key).under("recurrence.range");+ let kind = field(p, "type").map_err(|e| e.under("recurrence.pattern"))?;+ let (_, _, start, _) = normalize::time_body(&normalize::event_time(event)?)+ .map_err(|_| Rejected::at("start.timeZone"))?;let frequency = match kind {"daily" => "DAILY","weekly" => "WEEKLY","absoluteMonthly" | "relativeMonthly" => "MONTHLY","absoluteYearly" | "relativeYearly" => "YEARLY",- _ => return Err(invalid()),+ _ => return Err(pattern("type")),};let mut fields = vec![format!("FREQ={frequency}"),@@ -43,23 +47,23 @@ pub(super) fn from_graph(event: &Value) -> Result<Vec<String>, CalendarProviderEp["interval"].as_u64().filter(|n| *n > 0)- .ok_or_else(invalid)?+ .ok_or_else(|| pattern("interval"))?),];if matches!(kind, "weekly" | "relativeMonthly" | "relativeYearly") {let days: Vec<_> = p["daysOfWeek"].as_array()- .ok_or_else(invalid)?+ .ok_or_else(|| pattern("daysOfWeek"))?.iter().map(|v| {DAYS.iter().find(|d| Some(d.1) == v.as_str()).map(|d| d.0)- .ok_or_else(invalid)+ .ok_or_else(|| pattern("daysOfWeek"))}).collect::<Result<_, _>>()?;if days.is_empty() {- return Err(invalid());+ return Err(pattern("daysOfWeek"));}if !kind.starts_with("relative") {fields.push(format!("BYDAY={}", days.join(",")));@@ -68,7 +72,7 @@ pub(super) fn from_graph(event: &Value) -> Result<Vec<String>, CalendarProviderElet index = INDICES.iter().find(|i| Some(i.1) == p["index"].as_str())- .ok_or_else(invalid)?+ .ok_or_else(|| pattern("index"))?.0;if days.len() == 1 {fields.push(format!("BYDAY={index}{}", days[0]));@@ -82,7 +86,7 @@ pub(super) fn from_graph(event: &Value) -> Result<Vec<String>, CalendarProviderElet day = p["dayOfMonth"].as_u64().filter(|n| (1..=31).contains(n))- .ok_or_else(invalid)?;+ .ok_or_else(|| pattern("dayOfMonth"))?;// A DTSTART-aligned pattern is the same rule the shared editor emits.// Keep explicit selectors only when omitting them would change dates.if day != u64::from(start.day()) {@@ -93,7 +97,7 @@ pub(super) fn from_graph(event: &Value) -> Result<Vec<String>, CalendarProviderElet month = p["month"].as_u64().filter(|n| (1..=12).contains(n))- .ok_or_else(invalid)?;+ .ok_or_else(|| pattern("month"))?;if month != u64::from(start.month()) {fields.push(format!("BYMONTH={month}"));}@@ -106,31 +110,30 @@ pub(super) fn from_graph(event: &Value) -> Result<Vec<String>, CalendarProviderE.unwrap_or("SU");fields.push(format!("WKST={day}"));}- match string(r, "type")? {+ match field(r, "type").map_err(|e| e.under("recurrence.range"))? {"numbered" => fields.push(format!("COUNT={}",r["numberOfOccurrences"].as_u64().filter(|n| *n > 0)- .ok_or_else(invalid)?+ .ok_or_else(|| range("numberOfOccurrences"))?)),"endDate" => {- let date = NaiveDate::parse_from_str(string(r, "endDate")?, "%Y-%m-%d")- .map_err(|_| invalid())?;+ let date = r["endDate"]+ .as_str()+ .and_then(|date| NaiveDate::parse_from_str(date, "%Y-%m-%d").ok())+ .ok_or_else(|| range("endDate"))?;if event["isAllDay"].as_bool() == Some(true) {fields.push(format!("UNTIL={}", date.format("%Y%m%d")));} else {- let zone = zones::zone(- r["recurrenceTimeZone"]- .as_str()- .or(event["originalStartTimeZone"].as_str())- .unwrap_or("UTC"),- )- .ok_or_else(invalid)?;- let end = zone- .from_local_datetime(&date.and_hms_opt(23, 59, 59).ok_or_else(invalid)?)- .single()- .ok_or_else(invalid)?;+ let end = range_zone(event)+ .from_local_datetime(+ &date+ .and_hms_opt(23, 59, 59)+ .ok_or_else(|| range("endDate"))?,+ )+ .latest()+ .ok_or_else(|| range("endDate"))?;fields.push(format!("UNTIL={}",end.with_timezone(&chrono::Utc).format("%Y%m%dT%H%M%SZ")@@ -138,11 +141,26 @@ pub(super) fn from_graph(event: &Value) -> Result<Vec<String>, CalendarProviderE}}"noEnd" => {}- _ => return Err(invalid()),+ _ => return Err(range("type")),}Ok(vec![format!("RRULE:{}", fields.join(";"))])}+/// The zone the series' end date is in. Graph names a legacy custom zone+/// (`tzone://Microsoft/Custom`) Macro cannot resolve, so fall back as+/// `normalize::event_time` does, to the zone the event's time is kept in:+/// `to_graph` writes the end date back in that zone.+fn range_zone(event: &Value) -> Tz {+ [+ &event["recurrence"]["range"]["recurrenceTimeZone"],+ &event["originalStartTimeZone"],+ &event["start"]["timeZone"],+ ]+ .into_iter()+ .find_map(|name| name.as_str().and_then(zones::zone))+ .unwrap_or(chrono_tz::UTC)+}+pub(super) fn to_graph(lines: &[String], time: &EventTime) -> Result<Value, CalendarProviderError> {if lines.is_empty() {return Ok(Value::Null);diff --git a/crates/email_api_client/src/outbound/outlook/calendar/test.rs b/crates/email_api_client/src/outbound/outlook/calendar/test.rsindex 48463c28a0..8d46bd4a14 100644--- a/crates/email_api_client/src/outbound/outlook/calendar/test.rs+++ b/crates/email_api_client/src/outbound/outlook/calendar/test.rs@@ -6,6 +6,7 @@ use wiremock::{};mod mutations;+mod sync;pub(super) fn target() -> ProviderCalendarTarget {let link_id = Uuid::now_v7();@@ -509,12 +510,12 @@ async fn moved_event_is_absent_from_its_old_calendar_even_if_mailbox_id_survives.expect(1).mount(&server).await;- assert!(+ assert!(matches!(api.refresh_calendar_event("token", &target(), "moved").await- .unwrap()- .is_none()- );+ .unwrap(),+ OutlookEventRefresh::Gone+ ));}#[test]@@ -662,6 +663,95 @@ fn ordinary_outlook_recurrence_round_trips_in_the_shared_editor_shape() {}}+/// A weekly meeting another calendar system organized, as Exchange imported it+/// from the invitation, after the organizer ended the series ("this and+/// following"): Graph names the range's zone with its own alias for UTC.+pub(super) fn ended_imported_series(id: &str) -> Value {+ json!({+ "id":id,"@odata.etag":"W/\"4\"","iCalUId":format!("040000008200E00074C5B7101A82E008{id}"),+ "type":"seriesMaster","subject":"Team sync","isCancelled":false,"isAllDay":false,+ "createdDateTime":"2026-11-02T18:08:30Z","lastModifiedDateTime":"2026-11-02T19:10:12Z",+ "originalStartTimeZone":"tzone://Microsoft/Custom","originalEndTimeZone":"tzone://Microsoft/Custom",+ "start":{"dateTime":"2026-10-21T11:00:00.0000000","timeZone":"UTC"},+ "end":{"dateTime":"2026-10-21T11:30:00.0000000","timeZone":"UTC"},+ "organizer":{"emailAddress":{"address":"[email protected]","name":"Organizer"}},+ "attendees":[],+ "recurrence":{+ "pattern":{"type":"weekly","interval":1,"month":0,"dayOfMonth":0,"daysOfWeek":["wednesday"],"firstDayOfWeek":"monday","index":"first"},+ "range":{"type":"endDate","startDate":"2026-10-21","endDate":"2026-11-03","recurrenceTimeZone":"tzone://Microsoft/Utc","numberOfOccurrences":0}+ }+ })+}++#[test]+fn an_imported_series_ended_by_its_organizer_reads_graphs_utc_alias() {+ let series = ended_imported_series("series");+ assert_eq!(+ recurrence::from_graph(&series).unwrap(),+ vec!["RRULE:FREQ=WEEKLY;INTERVAL=1;BYDAY=WE;WKST=MO;UNTIL=20261103T235959Z".to_owned()]+ );+ let instances = ["2026-10-21", "2026-10-28"].map(|day| {+ let mut instance = series.clone();+ instance["id"] = json!(format!("series-{day}"));+ instance["type"] = json!("occurrence");+ instance["seriesMasterId"] = json!("series");+ instance["originalStart"] = json!(format!("{day}T11:00:00Z"));+ instance["start"]["dateTime"] = json!(format!("{day}T11:00:00.0000000"));+ instance["end"]["dateTime"] = json!(format!("{day}T11:30:00.0000000"));+ instance+ });+ let upsert = normalize::projection(&target(), series, instances.to_vec()).unwrap();+ assert_eq!(upsert.occurrences.len(), 2);+}++#[test]+fn an_end_date_in_a_zone_macro_cannot_resolve_is_read_in_the_events_own_zone() {+ let mut series = ended_imported_series("series");+ series["originalStartTimeZone"] = json!("Pacific Standard Time");+ series["recurrence"]["range"]["recurrenceTimeZone"] = json!("tzone://Microsoft/Custom");+ let lines = recurrence::from_graph(&series).unwrap();+ assert_eq!(+ lines,+ vec!["RRULE:FREQ=WEEKLY;INTERVAL=1;BYDAY=WE;WKST=MO;UNTIL=20261104T075959Z".to_owned()],+ "the end of November 3 in Los Angeles"+ );+ let body = recurrence::to_graph(&lines, &normalize::event_time(&series).unwrap()).unwrap();+ assert_eq!(body["range"]["endDate"], "2026-11-03");+}++#[tokio::test]+async fn an_event_macro_cannot_read_names_the_rejected_field() {+ let (server, api) = setup().await;+ let mut hourly = event("hourly");+ hourly["type"] = json!("seriesMaster");+ hourly["recurrence"] = json!({"pattern":{"type":"hourly","interval":1},"range":{"type":"noEnd","startDate":"2026-11-02"}});+ let mut series = event("series");+ series["type"] = json!("seriesMaster");+ series["recurrence"] = json!({"pattern":{"type":"daily","interval":1},"range":{"type":"noEnd","startDate":"2026-11-02"}});+ let mut occurrence = event("occurrence");+ occurrence["start"]["dateTime"] = json!("next Monday");+ for (master, instances, field) in [+ (hourly, vec![], "recurrence.pattern.type"),+ (series, vec![occurrence], "instances.start.dateTime"),+ ] {+ let id = master["id"].as_str().unwrap().to_owned();+ Mock::given(method("GET"))+ .and(path(format!("/v1.0/me/calendars/calendar/events/{id}")))+ .respond_with(ResponseTemplate::new(200).set_body_json(master))+ .mount(&server)+ .await;+ Mock::given(method("GET"))+ .and(path(format!("/v1.0/me/events/{id}/instances")))+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({"value":instances})))+ .mount(&server)+ .await;+ match api.refresh_calendar_event("token", &target(), &id).await {+ Ok(OutlookEventRefresh::Unreadable { field: rejected }) => assert_eq!(rejected, field),+ _ => panic!("{id} should be unreadable at {field}"),+ }+ }+}+#[test]fn occurrence_preferences_override_or_inherit_series_reminders_and_decline_policy() {let policy = AutomaticDeclinePolicy {diff --git a/crates/email_api_client/src/outbound/outlook/calendar/test/mutations.rs b/crates/email_api_client/src/outbound/outlook/calendar/test/mutations.rsindex d6d2bce335..47470985ba 100644--- a/crates/email_api_client/src/outbound/outlook/calendar/test/mutations.rs+++ b/crates/email_api_client/src/outbound/outlook/calendar/test/mutations.rs@@ -11,12 +11,12 @@ use sqlx::PgPool;use std::sync::{Arc, Mutex};use wiremock::{Request, Respond, matchers::path_regex};-const OWNER: &str = "macro|[email protected]";+pub(super) const OWNER: &str = "macro|[email protected]";const INBOX: &str = "[email protected]";-const TOKEN: &str = "graph-access-token";+pub(super) const TOKEN: &str = "graph-access-token";const EVENTS: &str = r"^/v1\.0/me/(calendars/[^/]+/)?events(/|$)";-struct IssuedToken;+pub(super) struct IssuedToken;impl CalendarAccessTokenProvider for IssuedToken {async fn fetch_access_token(@@ -27,7 +27,7 @@ impl CalendarAccessTokenProvider for IssuedToken {}}-struct NoOpenClients;+pub(super) struct NoOpenClients;impl CalendarRefreshNotifier for NoOpenClients {async fn calendar_changed(&self, _: &str, _: Uuid, _: bool) {}@@ -76,7 +76,7 @@ impl Respond for GraphEvent {}}-async fn connected_mailbox(+pub(super) async fn connected_mailbox(pool: &PgPool,server: &MockServer,api: &OutlookApiClientRepository,@@ -155,14 +155,16 @@ async fn sync_event(repo: &PgCalendarRepository, api: &OutlookApiClientRepositor.await.unwrap().unwrap();- let upsert = api+ let OutlookEventRefresh::Refreshed(upsert) = api.refresh_calendar_event(TOKEN, lease.target.as_ref().unwrap(), graph_id).await.unwrap()- .unwrap();+ else {+ panic!("{graph_id} should be readable");+ };repo.upsert_event(CalendarEventWrite::OutlookSync {lease: Box::new(lease),- upsert,+ upsert: *upsert,}).await.unwrap();diff --git a/crates/email_api_client/src/outbound/outlook/calendar/test/sync.rs b/crates/email_api_client/src/outbound/outlook/calendar/test/sync.rsnew file mode 100644index 0000000000..2e2ad6241e--- /dev/null+++ b/crates/email_api_client/src/outbound/outlook/calendar/test/sync.rs@@ -0,0 +1,223 @@+use super::mutations::{IssuedToken, NoOpenClients, OWNER, connected_mailbox};+use super::*;+use calendar_events::outbound::pg::PgCalendarRepository;+use macro_db_migrator::MACRO_DB_MIGRATIONS;+use sqlx::PgPool;+use wiremock::matchers::query_param_is_missing;++type Sync = OutlookCalendarSync<+ PgCalendarRepository,+ OutlookApiClientRepository,+ IssuedToken,+ NoOpenClients,+>;++/// A meeting tomorrow that Macro can read.+fn upcoming(id: &str) -> Value {+ let now = Utc::now();+ let mut value = event(id);+ value["start"]["dateTime"] = json!((now + chrono::Duration::days(1)).to_rfc3339());+ value["end"]["dateTime"] =+ json!((now + chrono::Duration::days(1) + chrono::Duration::hours(1)).to_rfc3339());+ value+}++/// A daily out-of-office series as Graph returns it, carrying Macro's own+/// preferences property with an automatic-decline policy this build cannot+/// read, as another Macro version could have written it. The reader rejects+/// it at `singleValueExtendedProperties.automaticDecline`.+fn unreadable_series(id: &str) -> Value {+ let mut value = upcoming(id);+ let start_date = value["start"]["dateTime"].as_str().unwrap()[..10].to_owned();+ value["type"] = json!("seriesMaster");+ value["showAs"] = json!("oof");+ value["recurrence"] = json!({+ "pattern":{"type":"daily","interval":1,"month":0,"dayOfMonth":0,"firstDayOfWeek":"sunday","index":"first"},+ "range":{"type":"noEnd","startDate":start_date,"endDate":"0001-01-01","recurrenceTimeZone":"UTC","numberOfOccurrences":0},+ });+ value["singleValueExtendedProperties"] = json!([{+ "id":PREFERENCES_ID,+ "value":json!({"automaticDecline":{"mode":"declineWhenAway"}}).to_string(),+ }]);+ value+}++/// A delta entry for a single event.+fn single(id: &str) -> Value {+ json!({"id":id})+}++/// A delta entry for one occurrence, which the page maps to its series.+fn occurrence(master: &str) -> Value {+ json!({"id":format!("{master}-occurrence"),"seriesMasterId":master})+}++async fn serve_delta(server: &MockServer, cursor: Option<&str>, entries: &[Value], next: &str) {+ let mock = Mock::given(method("GET")).and(path("/v1.0/me/calendarView/delta"));+ let mock = match cursor {+ Some(cursor) => mock.and(query_param("$deltatoken", cursor)),+ None => mock.and(query_param_is_missing("$deltatoken")),+ };+ mock.respond_with(ResponseTemplate::new(200).set_body_json(json!({+ "value":entries,+ "@odata.deltaLink":format!("{}/v1.0/me/calendarView/delta?$deltatoken={next}", server.uri()),+ })))+ .mount(server)+ .await;+}++async fn serve_event(server: &MockServer, body: Value) {+ let id = body["id"].as_str().unwrap().to_owned();+ Mock::given(method("GET"))+ .and(path(format!("/v1.0/me/calendars/AAMkCalendar/events/{id}")))+ .respond_with(ResponseTemplate::new(200).set_body_json(body))+ .mount(server)+ .await;+ Mock::given(method("GET"))+ .and(path(format!("/v1.0/me/events/{id}/instances")))+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({"value":[]})))+ .mount(server)+ .await;+}++async fn sync_until_idle(sync: &Sync) {+ // A page, each event, then the finish; a blocked round stops early.+ for _ in 0..6 {+ if !matches!(sync.run_once().await, Ok(true)) {+ break;+ }+ }+}++async fn assert_synced(pool: &PgPool) {+ let calendar = sqlx::query!(+ r#"SELECT c.synced_at, c.consecutive_sync_failures, w.pending_ids+ FROM calendars c JOIN calendar_outlook_work w ON w.calendar_id = c.id+ WHERE c.provider_calendar_id = 'AAMkCalendar'"#+ )+ .fetch_one(pool)+ .await+ .unwrap();+ assert!(calendar.synced_at.is_some(), "the round finished");+ assert_eq!(calendar.consecutive_sync_failures, 0);+ assert!(calendar.pending_ids.is_empty());+}++async fn imported(pool: &PgPool) -> Vec<String> {+ sqlx::query_scalar!(+ r#"SELECT s.provider_event_id AS "provider_event_id!" FROM calendar_event_sources s+ JOIN calendar_events e ON e.id = s.event_id WHERE e.owner_id = $1 ORDER BY 1"#,+ OWNER+ )+ .fetch_all(pool)+ .await+ .unwrap()+}++/// Whether the page membership of a series' occurrence survives. An event+/// confirmed gone drops it; one skipped as unreadable keeps it.+async fn occurrence_is_member(pool: &PgPool, master: &str) -> bool {+ sqlx::query_scalar!(+ r#"SELECT EXISTS(SELECT 1 FROM calendar_outlook_members+ WHERE provider_id = $1 AND master_id = $2) AS "member!""#,+ format!("{master}-occurrence"),+ master+ )+ .fetch_one(pool)+ .await+ .unwrap()+}++async fn connected(pool: &PgPool) -> (MockServer, Sync) {+ let (server, api) = setup().await;+ let repo = connected_mailbox(+ pool,+ &server,+ &api,+ json!([{"id":"AAMkCalendar","name":"Calendar","canEdit":true,"isDefaultCalendar":true}]),+ )+ .await;+ (+ server,+ OutlookCalendarSync::new(repo, api, IssuedToken, NoOpenClients),+ )+}++#[test]+fn the_unreadable_series_is_rejected_at_its_decline_policy() {+ assert_eq!(+ normalize::projection(&target(), unreadable_series("series"), vec![]).err(),+ Some(Rejected::at(+ "singleValueExtendedProperties.automaticDecline"+ ))+ );+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn an_event_macro_cannot_read_is_skipped_and_the_rest_of_the_calendar_syncs(pool: PgPool) {+ let (server, sync) = connected(&pool).await;+ // The unreadable series sorts first, so it is refreshed first.+ serve_delta(+ &server,+ None,+ &[single("AAMkReadable"), occurrence("AAMkAway")],+ "complete",+ )+ .await;+ serve_event(&server, upcoming("AAMkReadable")).await;+ serve_event(&server, unreadable_series("AAMkAway")).await;+ sync_until_idle(&sync).await;++ assert_synced(&pool).await;+ assert_eq!(+ imported(&pool).await,+ vec!["AAMkReadable"],+ "the readable meeting is imported and the unreadable one is absent"+ );+ assert!(+ occurrence_is_member(&pool, "AAMkAway").await,+ "the series was read and skipped, not found gone"+ );+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn a_stored_event_that_turns_unreadable_leaves_the_calendar(pool: PgPool) {+ let (server, sync) = connected(&pool).await;+ serve_delta(+ &server,+ None,+ &[single("AAMkReadable"), single("AAMkChanged")],+ "first",+ )+ .await;+ serve_event(&server, upcoming("AAMkReadable")).await;+ serve_event(&server, upcoming("AAMkChanged")).await;+ sync_until_idle(&sync).await;+ assert_eq!(imported(&pool).await, vec!["AAMkChanged", "AAMkReadable"]);++ // The organizer turns the meeting into a series Macro cannot read. Its+ // stored copy would show a version Microsoft no longer has.+ server.reset().await;+ serve_delta(+ &server,+ Some("first"),+ &[occurrence("AAMkChanged")],+ "second",+ )+ .await;+ serve_event(&server, unreadable_series("AAMkChanged")).await;+ sqlx::query!(+ "UPDATE calendar_outlook_work SET next_run_at = now() WHERE calendar_id IS NOT NULL"+ )+ .execute(&pool)+ .await+ .unwrap();+ sync_until_idle(&sync).await;++ assert_synced(&pool).await;+ assert_eq!(imported(&pool).await, vec!["AAMkReadable"]);+ assert!(+ occurrence_is_member(&pool, "AAMkChanged").await,+ "a later tombstone for it still resolves"+ );+}diff --git a/crates/email_api_client/src/outbound/outlook/calendar/zones.rs b/crates/email_api_client/src/outbound/outlook/calendar/zones.rsindex 6bc8f3f170..efabf1e9fa 100644--- a/crates/email_api_client/src/outbound/outlook/calendar/zones.rs+++ b/crates/email_api_client/src/outbound/outlook/calendar/zones.rs@@ -3,7 +3,12 @@//! License: CLDR-LICENSE.txt in this directory. Generated territory="001" entries.use chrono_tz::Tz;+/// Graph's own name for UTC, which it gives meetings Exchange imported from+/// another calendar system's invitation. CLDR does not list it.+const GRAPH_UTC: &str = "tzone://Microsoft/Utc";+pub(super) fn zone(name: &str) -> Option<Tz> {+ let name = if name == GRAPH_UTC { "UTC" } else { name };let iana = match name {"Dateline Standard Time" => "Etc/GMT+12","UTC-11" => "Etc/GMT+11",