Skip to content

armgit / macro

Public Imported from GitHub
0

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

fix(calendar): stop deletes flashing skeletons and the syncing banner (#9349)

gbirman25dbc4f45270
diff --git a/.sqlx/query-9e728b268363d424353b3363f427d12df0e8200c3f731276463443f6db02ee2e.json b/.sqlx/query-1892e66d6c081f67180fb6861ab295ff5b00d50735ee97a7c4ec47d63af2a815.json
similarity index 58%
rename from .sqlx/query-9e728b268363d424353b3363f427d12df0e8200c3f731276463443f6db02ee2e.json
rename to .sqlx/query-1892e66d6c081f67180fb6861ab295ff5b00d50735ee97a7c4ec47d63af2a815.json
index 39796f4dd8..e7c9b3f17c 100644
--- a/.sqlx/query-9e728b268363d424353b3363f427d12df0e8200c3f731276463443f6db02ee2e.json
+++ b/.sqlx/query-1892e66d6c081f67180fb6861ab295ff5b00d50735ee97a7c4ec47d63af2a815.json
@@ -1,12 +1,17 @@
{
"db_name": "PostgreSQL",
- "query": "\n WITH deleted_sources AS (\n DELETE FROM calendar_event_sources source\n WHERE source.source_kind IN ('google', 'outlook')\n AND source.account_id = $1\n AND source.calendar_id = $2\n AND (\n source.provider_event_id = ANY($3::text[])\n OR source.provider_recurring_event_id = ANY($3::text[])\n )\n RETURNING source.event_id\n )\n SELECT DISTINCT event_id AS \"event_id!\"\n FROM deleted_sources\n ",
+ "query": "\n WITH deleted_sources AS (\n DELETE FROM calendar_event_sources source\n WHERE source.source_kind IN ('google', 'outlook')\n AND source.account_id = $1\n AND source.calendar_id = $2\n AND (\n source.provider_event_id = ANY($3::text[])\n OR source.provider_recurring_event_id = ANY($3::text[])\n )\n RETURNING source.event_id, source.event_type\n )\n SELECT\n event_id AS \"event_id!\",\n bool_or(event_type = 'out_of_office') AS \"out_of_office!\"\n FROM deleted_sources\n GROUP BY event_id\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "event_id!",
"type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "out_of_office!",
+ "type_info": "Bool"
}
],
"parameters": {
@@ -17,8 +22,9 @@
]
},
"nullable": [
- false
+ false,
+ null
]
},
- "hash": "9e728b268363d424353b3363f427d12df0e8200c3f731276463443f6db02ee2e"
+ "hash": "1892e66d6c081f67180fb6861ab295ff5b00d50735ee97a7c4ec47d63af2a815"
}
diff --git a/.sqlx/query-1d39053f6f37d224aa56c139e14eab3d4a1641ac7ba5560ad03f566a4965632f.json b/.sqlx/query-1d39053f6f37d224aa56c139e14eab3d4a1641ac7ba5560ad03f566a4965632f.json
new file mode 100644
index 0000000000..fcddc2771c
--- /dev/null
+++ b/.sqlx/query-1d39053f6f37d224aa56c139e14eab3d4a1641ac7ba5560ad03f566a4965632f.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE calendar_accounts\n SET sync_status = CASE\n WHEN sync_status = 'ready' AND last_synced_at IS NOT NULL THEN 'ready'\n ELSE 'syncing'\n END,\n updated_at = now()\n WHERE email_link_id = $1\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "1d39053f6f37d224aa56c139e14eab3d4a1641ac7ba5560ad03f566a4965632f"
+}
diff --git a/.sqlx/query-9b61a81388b53e93e441a043db317e32e912688bb5c04a8de2331ae24265bd20.json b/.sqlx/query-2dfe18f6ac1335ed2c5984df2f77e38417a6ecbe59551b9d580b01ee6dcba766.json
similarity index 61%
rename from .sqlx/query-9b61a81388b53e93e441a043db317e32e912688bb5c04a8de2331ae24265bd20.json
rename to .sqlx/query-2dfe18f6ac1335ed2c5984df2f77e38417a6ecbe59551b9d580b01ee6dcba766.json
index 35bc784ef5..1e9c512fa1 100644
--- a/.sqlx/query-9b61a81388b53e93e441a043db317e32e912688bb5c04a8de2331ae24265bd20.json
+++ b/.sqlx/query-2dfe18f6ac1335ed2c5984df2f77e38417a6ecbe59551b9d580b01ee6dcba766.json
@@ -1,12 +1,17 @@
{
"db_name": "PostgreSQL",
- "query": "\n WITH deleted_sources AS (\n DELETE FROM calendar_event_sources source\n WHERE source.source_kind = 'google'\n AND source.account_id = $1\n AND source.calendar_id = $2\n AND (\n source.provider_event_id = ANY($3::text[])\n OR source.provider_recurring_event_id = ANY($3::text[])\n )\n RETURNING source.event_id\n )\n SELECT DISTINCT event_id AS \"event_id!\"\n FROM deleted_sources\n ",
+ "query": "\n WITH deleted_sources AS (\n DELETE FROM calendar_event_sources source\n WHERE source.source_kind = 'google'\n AND source.account_id = $1\n AND source.calendar_id = $2\n AND (\n source.provider_event_id = ANY($3::text[])\n OR source.provider_recurring_event_id = ANY($3::text[])\n )\n RETURNING source.event_id, source.event_type\n )\n SELECT\n event_id AS \"event_id!\",\n bool_or(event_type = 'out_of_office') AS \"out_of_office!\"\n FROM deleted_sources\n GROUP BY event_id\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "event_id!",
"type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "out_of_office!",
+ "type_info": "Bool"
}
],
"parameters": {
@@ -17,8 +22,9 @@
]
},
"nullable": [
- false
+ false,
+ null
]
},
- "hash": "9b61a81388b53e93e441a043db317e32e912688bb5c04a8de2331ae24265bd20"
+ "hash": "2dfe18f6ac1335ed2c5984df2f77e38417a6ecbe59551b9d580b01ee6dcba766"
}
diff --git a/.sqlx/query-c63bed5edd67aa1ac135d71c40965db9013e54b7d32491a64293d15cbc6fe654.json b/.sqlx/query-a61695a62ad708963c16f12b74e0741973b02e380340d222ca9e796fbd9b5cfd.json
similarity index 57%
rename from .sqlx/query-c63bed5edd67aa1ac135d71c40965db9013e54b7d32491a64293d15cbc6fe654.json
rename to .sqlx/query-a61695a62ad708963c16f12b74e0741973b02e380340d222ca9e796fbd9b5cfd.json
index 16aecde5b9..40e8c4fa72 100644
--- a/.sqlx/query-c63bed5edd67aa1ac135d71c40965db9013e54b7d32491a64293d15cbc6fe654.json
+++ b/.sqlx/query-a61695a62ad708963c16f12b74e0741973b02e380340d222ca9e796fbd9b5cfd.json
@@ -1,12 +1,17 @@
{
"db_name": "PostgreSQL",
- "query": "\n WITH deleted_sources AS (\n DELETE FROM calendar_event_sources source\n WHERE source.source_kind = 'google'\n AND source.account_id = $1\n AND source.calendar_id = $2\n AND NOT (source.provider_event_id = ANY($3::text[]))\n RETURNING source.event_id\n )\n SELECT DISTINCT event_id AS \"event_id!\"\n FROM deleted_sources\n ",
+ "query": "\n WITH deleted_sources AS (\n DELETE FROM calendar_event_sources source\n WHERE source.source_kind = 'google'\n AND source.account_id = $1\n AND source.calendar_id = $2\n AND NOT (source.provider_event_id = ANY($3::text[]))\n RETURNING source.event_id, source.event_type\n )\n SELECT\n event_id AS \"event_id!\",\n bool_or(event_type = 'out_of_office') AS \"out_of_office!\"\n FROM deleted_sources\n GROUP BY event_id\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "event_id!",
"type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "out_of_office!",
+ "type_info": "Bool"
}
],
"parameters": {
@@ -17,8 +22,9 @@
]
},
"nullable": [
- false
+ false,
+ null
]
},
- "hash": "c63bed5edd67aa1ac135d71c40965db9013e54b7d32491a64293d15cbc6fe654"
+ "hash": "a61695a62ad708963c16f12b74e0741973b02e380340d222ca9e796fbd9b5cfd"
}
diff --git a/.sqlx/query-014857853dbc87a9f72cecef3a01d4310c17ec7bbaccfc9e0458e7ca090ebb3b.json b/.sqlx/query-a748e258fba7a7f4264d7729fe5850f42d02ceb6ef98b722f2a55f5dd0df2d58.json
similarity index 57%
rename from .sqlx/query-014857853dbc87a9f72cecef3a01d4310c17ec7bbaccfc9e0458e7ca090ebb3b.json
rename to .sqlx/query-a748e258fba7a7f4264d7729fe5850f42d02ceb6ef98b722f2a55f5dd0df2d58.json
index 7d1107a2a8..e0c84e81dd 100644
--- a/.sqlx/query-014857853dbc87a9f72cecef3a01d4310c17ec7bbaccfc9e0458e7ca090ebb3b.json
+++ b/.sqlx/query-a748e258fba7a7f4264d7729fe5850f42d02ceb6ef98b722f2a55f5dd0df2d58.json
@@ -1,12 +1,17 @@
{
"db_name": "PostgreSQL",
- "query": "\n WITH deleted_sources AS (\n DELETE FROM calendar_event_sources source\n WHERE source.source_kind = 'google'\n AND source.account_id = $1\n AND (\n source.calendar_id IS NULL\n OR NOT (source.calendar_id = ANY($2::uuid[]))\n )\n RETURNING source.event_id\n )\n SELECT DISTINCT event_id AS \"event_id!\"\n FROM deleted_sources\n ",
+ "query": "\n WITH deleted_sources AS (\n DELETE FROM calendar_event_sources source\n WHERE source.source_kind = 'google'\n AND source.account_id = $1\n AND (\n source.calendar_id IS NULL\n OR NOT (source.calendar_id = ANY($2::uuid[]))\n )\n RETURNING source.event_id, source.event_type\n )\n SELECT\n event_id AS \"event_id!\",\n bool_or(event_type = 'out_of_office') AS \"out_of_office!\"\n FROM deleted_sources\n GROUP BY event_id\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "event_id!",
"type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "out_of_office!",
+ "type_info": "Bool"
}
],
"parameters": {
@@ -16,8 +21,9 @@
]
},
"nullable": [
- false
+ false,
+ null
]
},
- "hash": "014857853dbc87a9f72cecef3a01d4310c17ec7bbaccfc9e0458e7ca090ebb3b"
+ "hash": "a748e258fba7a7f4264d7729fe5850f42d02ceb6ef98b722f2a55f5dd0df2d58"
}
diff --git a/.sqlx/query-b686dd32c4ce2ee977aff0ccdf0ca18985be170631af2ddcc2caaac151904fde.json b/.sqlx/query-b686dd32c4ce2ee977aff0ccdf0ca18985be170631af2ddcc2caaac151904fde.json
deleted file mode 100644
index 3590d57838..0000000000
--- a/.sqlx/query-b686dd32c4ce2ee977aff0ccdf0ca18985be170631af2ddcc2caaac151904fde.json
+++ /dev/null
@@ -1,14 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n UPDATE calendar_accounts\n SET sync_status = 'syncing',\n updated_at = now()\n WHERE email_link_id = $1\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Uuid"
- ]
- },
- "nullable": []
- },
- "hash": "b686dd32c4ce2ee977aff0ccdf0ca18985be170631af2ddcc2caaac151904fde"
-}
diff --git a/crates/calendar_events/AGENTS.md b/crates/calendar_events/AGENTS.md
index bb48684224..13afeaab3d 100644
--- a/crates/calendar_events/AGENTS.md
+++ b/crates/calendar_events/AGENTS.md
@@ -356,6 +356,15 @@ reminder dispatch, team availability, and provider/Postgres persistence.
as it.
- Preserve the conservative OOO refresh hint on write outcomes and sync reports;
its contract is in [team sharing](../../docs/design/calendar-team-sharing.md).
+ Retirements carry it per event (`RetiredCalendarEvent::team_ooo_changed`): only
+ removing a source whose typed `event_type` is out of office raises it, since
+ every sync of a removed meeting's attendees reaches their teammates' calendars
+ and the hint blanks their overlay.
+- A sync of a `ready`, already synced account stays `ready`, on Google as on
+ Outlook, including after a re-grant that kept calendar access, whose sync tokens
+ survive. Any other state is catching up and marks it `syncing`: a first import,
+ a retry, or a return from `disabled` or `reauth_required`. The web's
+ `Syncing your calendar…` banner and its 5 second poll read that state.
- The team out-of-office overlay (`src/outbound/pg_team/ooo.rs`) starts from
the viewer's teammates and reaches sources through
`calendar_event_sources_out_of_office_idx` on the source's typed
diff --git a/crates/calendar_events/src/domain/mutations.rs b/crates/calendar_events/src/domain/mutations.rs
index 079100122a..2c0890ba8a 100644
--- a/crates/calendar_events/src/domain/mutations.rs
+++ b/crates/calendar_events/src/domain/mutations.rs
@@ -133,9 +133,10 @@ where
if retired.is_empty() {
return;
}
+ let team_ooo_changed = retired.iter().any(|event| event.team_ooo_changed);
self.publish_retirements(retired);
self.refresh
- .calendar_changed(owner_id, email_link_id, true)
+ .calendar_changed(owner_id, email_link_id, team_ooo_changed)
.await;
}
diff --git a/crates/calendar_events/src/domain/mutations/test.rs b/crates/calendar_events/src/domain/mutations/test.rs
index 3dd631a2dd..acafe305aa 100644
--- a/crates/calendar_events/src/domain/mutations/test.rs
+++ b/crates/calendar_events/src/domain/mutations/test.rs
@@ -1386,6 +1386,37 @@ async fn a_write_that_changed_nothing_sends_no_refresh_nudge() {
assert!(refresh.notified.lock().expect("notifier lock").is_empty());
}
+#[tokio::test]
+async fn deleting_clears_team_ooo_rows_only_for_an_out_of_office_event() {
+ for out_of_office in [false, true] {
+ let event_id = Uuid::now_v7();
+ let repo = FakeRepo {
+ mutation_target: Some(mutation_target(false)),
+ retired_events: vec![RetiredCalendarEvent {
+ event_id,
+ owner_id: "macro|owner".to_string(),
+ deleted: true,
+ team_ooo_changed: out_of_office,
+ }],
+ ..FakeRepo::default()
+ };
+ let refresh = RecordingRefreshNotifier::default();
+ service_with_refresh(
+ repo,
+ FakeProvider::new(FakeProviderBehavior::Echo),
+ FakeTokens::ok(),
+ refresh.clone(),
+ )
+ .delete_event("macro|user", event_id, None, CalendarDeletionScope::All)
+ .await
+ .unwrap();
+
+ let notified = refresh.notified.lock().expect("notifier lock").clone();
+ assert_eq!(notified.len(), 1);
+ assert_eq!(notified[0].2, out_of_office);
+ }
+}
+
#[tokio::test]
async fn deleting_a_series_publishes_deleted_per_removed_event() {
// Retiring a recurring master's source also retires its expanded
@@ -1401,17 +1432,20 @@ async fn deleting_a_series_publishes_deleted_per_removed_event() {
event_id: master,
owner_id: "macro|owner".to_string(),
deleted: true,
+ team_ooo_changed: false,
},
RetiredCalendarEvent {
event_id: instance,
owner_id: "macro|owner".to_string(),
deleted: true,
+ team_ooo_changed: false,
},
// Still backed by another source, so the row was rewritten.
RetiredCalendarEvent {
event_id: survivor,
owner_id: "macro|owner".to_string(),
deleted: false,
+ team_ooo_changed: false,
},
],
..FakeRepo::default()
diff --git a/crates/calendar_events/src/domain/ports.rs b/crates/calendar_events/src/domain/ports.rs
index e3b615aad9..5ac116871c 100644
--- a/crates/calendar_events/src/domain/ports.rs
+++ b/crates/calendar_events/src/domain/ports.rs
@@ -569,6 +569,9 @@ pub struct RetiredCalendarEvent {
/// Whether the row is now gone. `false` means it was rewritten from a
/// remaining source.
pub deleted: bool,
+ /// Whether teammates' out-of-office rows may have changed. `false`
+ /// certifies that nothing retired here fed that overlay.
+ pub team_ooo_changed: bool,
}
/// Persistence operations used by calendar business logic.
diff --git a/crates/calendar_events/src/domain/service.rs b/crates/calendar_events/src/domain/service.rs
index b73c00fb66..cad92f7c68 100644
--- a/crates/calendar_events/src/domain/service.rs
+++ b/crates/calendar_events/src/domain/service.rs
@@ -1062,7 +1062,7 @@ where
// gone once the commit lands, so nothing downstream can rediscover
// it by re-reading Postgres.
report.events_retired += retired.len();
- report.team_ooo_changed |= !retired.is_empty() || cancellation_count > 0;
+ report.team_ooo_changed |= retired.iter().any(|event| event.team_ooo_changed);
self.publish_retirements(retired);
// Tombstones only apply inside the snapshot commit, so they
// count once it succeeds.
@@ -1162,7 +1162,7 @@ where
.reconcile_google_calendar_list(key, lease_token, account_id, calendar_ids)
.await?;
report.events_retired += retired.len();
- report.team_ooo_changed |= !retired.is_empty();
+ report.team_ooo_changed |= retired.iter().any(|event| event.team_ooo_changed);
self.publish_retirements(retired);
Ok(())
diff --git a/crates/calendar_events/src/domain/service/test.rs b/crates/calendar_events/src/domain/service/test.rs
index e5ffeb8f71..09d5fbb59b 100644
--- a/crates/calendar_events/src/domain/service/test.rs
+++ b/crates/calendar_events/src/domain/service/test.rs
@@ -1317,6 +1317,50 @@ impl GoogleCalendarProvider for CancellingCalendarProvider {
}
}
+/// Team clients clear their out-of-office rows on this hint, and every sync of
+/// a meeting's attendees carries it, so a removed meeting must not raise it.
+#[tokio::test]
+async fn retiring_a_regular_meeting_leaves_team_ooo_rows_alone() {
+ for sync_retirements in [
+ vec![RetiredCalendarEvent {
+ event_id: Uuid::now_v7(),
+ owner_id: "macro|owner".to_string(),
+ deleted: true,
+ team_ooo_changed: false,
+ }],
+ // A tombstone for something Macro never stored changes nothing.
+ Vec::new(),
+ ] {
+ let coordinator = GoogleCalendarBackfillCoordinator::new(
+ FakeRepo {
+ sync_retirements,
+ ..FakeRepo::default()
+ },
+ CancellingCalendarProvider,
+ FakeLifecycle::claimed(),
+ NoopMacroEventBroker,
+ None,
+ );
+ let mut report = GoogleBackfillRunReport::default();
+ coordinator
+ .run(
+ CalendarBackfillJobKey {
+ job_id: Uuid::now_v7(),
+ email_link_id: Uuid::now_v7(),
+ },
+ "secret",
+ OccurrenceRange::historical_sync(Utc::now()),
+ &mut report,
+ )
+ .await
+ .unwrap();
+
+ assert!(report.changed());
+ assert!(!report.team_ooo_changed);
+ }
+}
+
/// 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.
@@ -1330,12 +1374,14 @@ async fn a_cancelled_provider_event_publishes_a_deletion() {
event_id: removed,
owner_id: "macro|owner".to_string(),
deleted: true,
+ team_ooo_changed: true,
},
// Another source still backs this one, so its row was rewritten.
RetiredCalendarEvent {
event_id: survivor,
owner_id: "macro|owner".to_string(),
deleted: false,
+ team_ooo_changed: false,
},
],
..FakeRepo::default()
diff --git a/crates/calendar_events/src/outbound/pg.rs b/crates/calendar_events/src/outbound/pg.rs
index 20c760746d..88a80875ab 100644
--- a/crates/calendar_events/src/outbound/pg.rs
+++ b/crates/calendar_events/src/outbound/pg.rs
@@ -1827,7 +1827,8 @@ impl CalendarRepository for PgCalendarRepository {
if !sync.cancelled_provider_event_ids.is_empty() {
// A cancelled recurring master retires its expanded instances via
// provider_recurring_event_id, matching Google's tombstone shape.
- let affected_event_ids = sqlx::query_scalar!(
+ let removed = sqlx::query_as!(
+ RemovedSources,
r#"
WITH deleted_sources AS (
DELETE FROM calendar_event_sources source
@@ -1838,10 +1839,13 @@ impl CalendarRepository for PgCalendarRepository {
source.provider_event_id = ANY($3::text[])
OR source.provider_recurring_event_id = ANY($3::text[])
)
- RETURNING source.event_id
+ RETURNING source.event_id, source.event_type
)
- SELECT DISTINCT event_id AS "event_id!"
+ SELECT
+ event_id AS "event_id!",
+ bool_or(event_type = 'out_of_office') AS "out_of_office!"
FROM deleted_sources
+ GROUP BY event_id
"#,
account_id,
sync.calendar_id,
@@ -1850,20 +1854,15 @@ impl CalendarRepository for PgCalendarRepository {
.fetch_all(&mut *tx)
.await
.map_err(report)?;
- if !affected_event_ids.is_empty() {
+ if !removed.is_empty() {
source_access::invalidate_account_freshness(&mut tx, account_id).await?;
}
- for event_id in affected_event_ids {
- if let Some(outcome) =
- restore_best_source_or_delete(&mut tx, &mut log, event_id).await?
- {
- retired.push(outcome);
- }
- }
+ retired.extend(retire_removed_sources(&mut tx, &mut log, removed).await?);
}
if let Some(observed_provider_event_ids) = &sync.observed_provider_event_ids {
- let affected_event_ids = sqlx::query_scalar!(
+ let removed = sqlx::query_as!(
+ RemovedSources,
r#"
WITH deleted_sources AS (
DELETE FROM calendar_event_sources source
@@ -1871,10 +1870,13 @@ impl CalendarRepository for PgCalendarRepository {
AND source.account_id = $1
AND source.calendar_id = $2
AND NOT (source.provider_event_id = ANY($3::text[]))
- RETURNING source.event_id
+ RETURNING source.event_id, source.event_type
)
- SELECT DISTINCT event_id AS "event_id!"
+ SELECT
+ event_id AS "event_id!",
+ bool_or(event_type = 'out_of_office') AS "out_of_office!"
FROM deleted_sources
+ GROUP BY event_id
"#,
account_id,
sync.calendar_id,
@@ -1883,16 +1885,10 @@ impl CalendarRepository for PgCalendarRepository {
.fetch_all(&mut *tx)
.await
.map_err(report)?;
- if !affected_event_ids.is_empty() {
+ if !removed.is_empty() {
source_access::invalidate_account_freshness(&mut tx, account_id).await?;
}
- for event_id in affected_event_ids {
- if let Some(outcome) =
- restore_best_source_or_delete(&mut tx, &mut log, event_id).await?
- {
- retired.push(outcome);
- }
- }
+ retired.extend(retire_removed_sources(&mut tx, &mut log, removed).await?);
}
let has_materialized_range = sync.materialized_range.is_some();
@@ -2162,7 +2158,8 @@ impl CalendarRepository for PgCalendarRepository {
let mut tx = self.pool.begin().await.map_err(report)?;
fence_google_mutation_tx(&mut tx, key, lease_token, Some(account_id)).await?;
- let affected_event_ids = sqlx::query_scalar!(
+ let removed = sqlx::query_as!(
+ RemovedSources,
r#"
WITH deleted_sources AS (
DELETE FROM calendar_event_sources source
@@ -2172,10 +2169,13 @@ impl CalendarRepository for PgCalendarRepository {
source.calendar_id IS NULL
OR NOT (source.calendar_id = ANY($2::uuid[]))
)
- RETURNING source.event_id
+ RETURNING source.event_id, source.event_type
)
- SELECT DISTINCT event_id AS "event_id!"
+ SELECT
+ event_id AS "event_id!",
+ bool_or(event_type = 'out_of_office') AS "out_of_office!"
FROM deleted_sources
+ GROUP BY event_id
"#,
account_id,
&calendar_ids,
@@ -2183,16 +2183,10 @@ impl CalendarRepository for PgCalendarRepository {
.fetch_all(&mut *tx)
.await
.map_err(report)?;
- if !affected_event_ids.is_empty() {
+ if !removed.is_empty() {
source_access::invalidate_account_freshness(&mut tx, account_id).await?;
}
- for event_id in affected_event_ids {
- if let Some(outcome) =
- restore_best_source_or_delete(&mut tx, &mut log, event_id).await?
- {
- retired.push(outcome);
- }
- }
+ retired.extend(retire_removed_sources(&mut tx, &mut log, removed).await?);
let flipped_calendars = sqlx::query!(
r#"
@@ -2335,10 +2329,20 @@ impl CalendarRepository for PgCalendarRepository {
event_id: row.id,
owner_id: row.owner_id,
deleted: false,
+ team_ooo_changed: true,
}
}));
+ // One event can land here twice; either report can carry the hint.
+ let ooo_changed = retired
+ .iter()
+ .filter(|outcome| outcome.team_ooo_changed)
+ .map(|outcome| outcome.event_id)
+ .collect::<HashSet<_>>();
let mut announced = HashSet::new();
- retired.retain(|outcome| announced.insert(outcome.event_id));
+ retired.retain_mut(|outcome| {
+ outcome.team_ooo_changed = ooo_changed.contains(&outcome.event_id);
+ announced.insert(outcome.event_id)
+ });
log.commit(tx).await?;
Ok(retired)
@@ -2816,7 +2820,8 @@ impl CalendarRepository for PgCalendarRepository {
let cancelled = [provider_event_id.to_string()];
// A deleted recurring master retires its expanded instances via
// provider_recurring_event_id, matching Google's tombstone shape.
- let affected_event_ids = sqlx::query_scalar!(
+ let removed = sqlx::query_as!(
+ RemovedSources,
r#"
WITH deleted_sources AS (
DELETE FROM calendar_event_sources source
@@ -2827,10 +2832,13 @@ impl CalendarRepository for PgCalendarRepository {
source.provider_event_id = ANY($3::text[])
OR source.provider_recurring_event_id = ANY($3::text[])
)
- RETURNING source.event_id
+ RETURNING source.event_id, source.event_type
)
- SELECT DISTINCT event_id AS "event_id!"
+ SELECT
+ event_id AS "event_id!",
+ bool_or(event_type = 'out_of_office') AS "out_of_office!"
FROM deleted_sources
+ GROUP BY event_id
"#,
account_id,
calendar_id,
@@ -2839,14 +2847,7 @@ impl CalendarRepository for PgCalendarRepository {
.fetch_all(&mut *tx)
.await
.map_err(report)?;
- let mut retired = Vec::new();
- for event_id in affected_event_ids {
- if let Some(outcome) =
- restore_best_source_or_delete(&mut tx, &mut log, event_id).await?
- {
- retired.push(outcome);
- }
- }
+ let retired = retire_removed_sources(&mut tx, &mut log, removed).await?;
log.commit(tx).await?;
Ok(retired)
}
@@ -3329,10 +3330,18 @@ impl CalendarBackfillRepository for PgCalendarRepository {
) -> Result<(), Report> {
let mut tx = self.pool.begin().await.map_err(report)?;
fence_google_mutation_tx(&mut tx, key, lease_token, None).await?;
+ // A ready, synced account has nothing a viewer should wait for, so the
+ // run leaves it ready, as on Outlook. That includes a re-grant that
+ // kept calendar access: its sync tokens survive. Every other state is
+ // still catching up: a first import, a retry, or a return from
+ // `disabled` or `reauth_required`.
sqlx::query!(
r#"
UPDATE calendar_accounts
- SET sync_status = 'syncing',
+ SET sync_status = CASE
+ WHEN sync_status = 'ready' AND last_synced_at IS NOT NULL THEN 'ready'
+ ELSE 'syncing'
+ END,
updated_at = now()
WHERE email_link_id = $1
"#,
@@ -4198,7 +4207,8 @@ async fn apply_canonical_projection(
/// Rewrite an event from its next-best remaining source, or delete it when no
/// source on a live calendar is left, reporting which happened.
///
-/// `None` means the row was already gone before this call.
+/// `None` means the row was already gone before this call. The report cannot
+/// tell what was removed, so it leaves the team hint uncertain.
async fn restore_best_source_or_delete(
tx: &mut Transaction<'_, Postgres>,
log: &mut ChangeLogBatch,
@@ -4256,6 +4266,7 @@ async fn restore_best_source_or_delete(
event_id,
owner_id: identity.owner_id,
deleted: true,
+ team_ooo_changed: true,
}));
};
@@ -4278,9 +4289,36 @@ async fn restore_best_source_or_delete(
event_id,
owner_id: identity.owner_id,
deleted: false,
+ team_ooo_changed: true,
}))
}
+/// One event that lost sources, and whether any of them was out of office.
+struct RemovedSources {
+ event_id: Uuid,
+ out_of_office: bool,
+}
+
+/// Settles the fate of each event that lost sources. The team overlay reads
+/// sources by this same typed `event_type`, so only removing an out-of-office
+/// source can withdraw a row teammates hold.
+async fn retire_removed_sources(
+ tx: &mut Transaction<'_, Postgres>,
+ log: &mut ChangeLogBatch,
+ removed: Vec<RemovedSources>,
+) -> Result<Vec<RetiredCalendarEvent>, Report> {
+ let mut retired = Vec::new();
+ for source in removed {
+ if let Some(outcome) = restore_best_source_or_delete(tx, log, source.event_id).await? {
+ retired.push(RetiredCalendarEvent {
+ team_ooo_changed: source.out_of_office,
+ ..outcome
+ });
+ }
+ }
+ Ok(retired)
+}
+
async fn replace_attendees(
tx: &mut Transaction<'_, Postgres>,
event_id: Uuid,
diff --git a/crates/calendar_events/src/outbound/pg/test.rs b/crates/calendar_events/src/outbound/pg/test.rs
index 7896187e02..1e050bec52 100644
--- a/crates/calendar_events/src/outbound/pg/test.rs
+++ b/crates/calendar_events/src/outbound/pg/test.rs
@@ -2507,6 +2507,146 @@ async fn sync_status_reflects_visible_account_ingestion_state(pool: PgPool) {
);
}
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn only_a_first_google_import_reports_syncing(pool: PgPool) {
+ let owner_id = "macro|[email protected]";
+ let link_id = insert_link(&pool, owner_id).await;
+ let repo = PgCalendarRepository::new(pool.clone());
+ let enabled = repo
+ .apply_google_grant(
+ link_id,
+ complete_grant(),
+ CalendarGrantIntent::CalendarRequested,
+ )
+ .await
+ .unwrap();
+ let (account_id, _) = provider_ids(&repo, link_id).await;
+ let google_job = enabled
+ .jobs
+ .into_iter()
+ .find(|job| job.kind == CalendarBackfillKind::GoogleCalendar)
+ .unwrap();
+ let key = CalendarBackfillJobKey {
+ job_id: google_job.id,
+ email_link_id: link_id,
+ };
+
+ let CalendarBackfillClaim::Claimed { lease_token, .. } =
+ repo.claim_google_backfill(key).await.unwrap()
+ else {
+ panic!("the first import should be claimable");
+ };
+ repo.mark_google_account_syncing(key, lease_token)
+ .await
+ .unwrap();
+ assert_eq!(
+ repo.sync_status(owner_id).await.unwrap(),
+ CalendarSyncStatus::Syncing
+ );
+ repo.reconcile_google_calendar_list(key, lease_token, account_id, Vec::new())
+ .await
+ .unwrap();
+ repo.complete_google_backfill(key, lease_token)
+ .await
+ .unwrap();
+
+ assert!(repo.schedule_google_sync_for_link(link_id).await.unwrap());
+ let CalendarBackfillClaim::Claimed { lease_token, .. } =
+ repo.claim_google_backfill(key).await.unwrap()
+ else {
+ panic!("the routine run should be claimable");
+ };
+ repo.mark_google_account_syncing(key, lease_token)
+ .await
+ .unwrap();
+ assert_eq!(
+ repo.sync_status(owner_id).await.unwrap(),
+ CalendarSyncStatus::Ready
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn retiring_sources_raises_the_team_hint_only_for_out_of_office_ones(pool: PgPool) {
+ let owner_id = "macro|[email protected]";
+ let link_id = insert_link(&pool, owner_id).await;
+ let repo = PgCalendarRepository::new(pool.clone());
+ let enabled = repo
+ .apply_google_grant(
+ link_id,
+ complete_grant(),
+ CalendarGrantIntent::CalendarRequested,
+ )
+ .await
+ .unwrap();
+ let (account_id, calendar_id) = provider_ids(&repo, link_id).await;
+ let provider = (account_id, calendar_id);
+ let starts_at = (Utc::now() + Duration::hours(3)).trunc_subsecs(0);
+ let out_of_office = repo
+ .upsert_event_fixture(member_primary_upsert(
+ owner_id,
+ link_id,
+ provider,
+ starts_at,
+ ))
+ .await
+ .unwrap();
+ let meeting = repo
+ .upsert_event_fixture(timed_upsert(
+ owner_id,
+ link_id,
+ provider,
+ "Weekly sync",
+ 1,
+ ))
+ .await
+ .unwrap();
+ let google_job = enabled
+ .jobs
+ .into_iter()
+ .find(|job| job.kind == CalendarBackfillKind::GoogleCalendar)
+ .unwrap();
+ let key = CalendarBackfillJobKey {
+ job_id: google_job.id,
+ email_link_id: link_id,
+ };
+ let CalendarBackfillClaim::Claimed { lease_token, .. } =
+ repo.claim_google_backfill(key).await.unwrap()
+ else {
+ panic!("Google job should be claimable");
+ };
+
+ let retired = repo
+ .commit_google_calendar_sync(
+ key,
+ lease_token,
+ account_id,
+ GoogleCalendarSyncSnapshot {
+ calendar_id,
+ next_sync_token: "next".to_string(),
+ observed_provider_event_ids: None,
+ materialized_range: None,
+ cancelled_provider_event_ids: vec![
+ "[email protected]".to_string(),
+ "[email protected]".to_string(),
+ ],
+ },
+ 0,
+ )
+ .await
+ .unwrap();
+
+ let hint_of = |event_id: Uuid| {
+ retired
+ .iter()
+ .find(|outcome| outcome.event_id == event_id)
+ .map(|outcome| outcome.team_ooo_changed)
+ };
+ assert_eq!(hint_of(out_of_office), Some(true));
+ assert_eq!(hint_of(meeting), Some(false));
+}
+
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn unchanged_google_projection_skips_the_write_path(pool: PgPool) {
let owner_id = "macro|[email protected]";
@@ -6158,6 +6298,7 @@ async fn retiring_the_primary_copy_promotes_the_shared_copy(pool: PgPool) {
event_id,
owner_id: member.to_string(),
deleted: false,
+ team_ooo_changed: true,
}]
);
let content = entity_content(&pool, event_id).await;
@@ -6372,6 +6513,7 @@ async fn canonical_selection_skips_copies_on_deleted_calendars(pool: PgPool) {
event_id,
owner_id: member.to_string(),
deleted: true,
+ team_ooo_changed: false,
}]
);
}
diff --git a/docs/agent-guide/calendar.md b/docs/agent-guide/calendar.md
index cdf2765ec0..fa5a1b146f 100644
--- a/docs/agent-guide/calendar.md
+++ b/docs/agent-guide/calendar.md
@@ -164,6 +164,7 @@ If metadata fails, events remain usable with the default calendar presentation.
Provider backfill shows a persistent `Syncing your calendar…` banner above the
grid, explaining that events will appear automatically and Macro remains usable.
The banner stays visible as partial results arrive and disappears when sync finishes.
+A routine sync of an already imported account never shows it.
Errors retain the separate retry state. Verify delayed occurrence
responses: switch Month/Week/Day rapidly and navigate without blanking the grid.
Confirm mixed event shapes, stable positions, clean handoff, and an uncovered Retry.
diff --git a/docs/design/calendar-team-sharing.md b/docs/design/calendar-team-sharing.md
index 1e54f42a32..5f77861cd5 100644
--- a/docs/design/calendar-team-sharing.md
+++ b/docs/design/calendar-team-sharing.md
@@ -94,12 +94,18 @@ not a claim that the person has no time off.
Calendar refresh notifications carry `team_ooo_changed`. Only an explicit `false`
lets the frontend retain OOO rows and open details while refetching. The server
-certifies this for unchanged sources and changes to sources that were and remain
-non-OOO, provided no calendar metadata, coverage failure, cancellation, or source
-retirement makes the result uncertain. OOO edits (including becoming private or
-ceasing to be OOO), sharing changes, and disconnects require immediate clearing.
-Missing or malformed hints from older servers also clear immediately. The
-60-second cache expiry and session-reset behavior remain in force.
+certifies this for unchanged sources, changes to sources that were and remain
+non-OOO, and retiring sources stored as non-OOO, including a delete through Macro
+and a cancellation from a sync, provided no calendar metadata or coverage failure
+makes the result uncertain. The overlay reads sources by their typed `event_type`,
+so a source stored as anything else never fed it. A clear blanks the overlay and
+its sidebar list until the refetch lands, and one meeting's removal reaches every
+attendee's inbox sync, so a hint on ordinary meetings flashes the whole team's
+calendars. OOO edits (including becoming private or ceasing to be OOO), retiring an
+OOO source, sharing changes, and disconnects require immediate clearing. Missing
+or malformed hints from older servers also clear immediately. Outlook sync still
+sends the hint with every change. The 60-second cache expiry and session-reset
+behavior remain in force.
Team projections and availability use server-confirmed source copies. An offline
edit can appear immediately in the editor's own calendar through the mutation