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: gate calendar ingestion and consent scope behind env flags (#5401)
gbirmanea49259e7b8d
diff --git a/.sqlx/query-48f7e5541e5bca5aa2d8ff2c955505822fcca9ff7fd46ebf8f969f193827cfdb.json b/.sqlx/query-48f7e5541e5bca5aa2d8ff2c955505822fcca9ff7fd46ebf8f969f193827cfdb.jsonnew file mode 100644index 0000000000..f7ca3cd766--- /dev/null+++ b/.sqlx/query-48f7e5541e5bca5aa2d8ff2c955505822fcca9ff7fd46ebf8f969f193827cfdb.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE calendar_sync_outbox\n SET published_at = NULL\n WHERE backfill_job_id = $1\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "48f7e5541e5bca5aa2d8ff2c955505822fcca9ff7fd46ebf8f969f193827cfdb"+}diff --git a/services/authentication_service/src/api/context.rs b/services/authentication_service/src/api/context.rsindex 432ec34a67..4b99f6dc0c 100644--- a/services/authentication_service/src/api/context.rs+++ b/services/authentication_service/src/api/context.rs@@ -138,6 +138,8 @@ pub(crate) struct ApiContext {pub rate_limit_service: RateLimiter,/// The stripe price idpub stripe_price_id: String,+ /// Whether Gmail link consent requests the Google Calendar scope.+ pub calendar_scope_enabled: bool,}env_var! {diff --git a/services/authentication_service/src/api/link/gmail.rs b/services/authentication_service/src/api/link/gmail.rsindex 8a87146497..4ff9b54cd5 100644--- a/services/authentication_service/src/api/link/gmail.rs+++ b/services/authentication_service/src/api/link/gmail.rs@@ -128,7 +128,11 @@ pub async fn init_gmail_link_handler(return Err(InitGmailLinkError::TooManyInProgressLinks);}- let authorization_scopes = format!("{GMAIL_SCOPES} {}", google_calendar_scope_parameter());+ let authorization_scopes = if ctx.calendar_scope_enabled {+ format!("{GMAIL_SCOPES} {}", google_calendar_scope_parameter())+ } else {+ GMAIL_SCOPES.to_string()+ };let requested_google_scopes: Vec<String> = authorization_scopes.split_ascii_whitespace().map(ToOwned::to_owned)diff --git a/services/authentication_service/src/config.rs b/services/authentication_service/src/config.rsindex c88c992d64..ebc6b149b8 100644--- a/services/authentication_service/src/config.rs+++ b/services/authentication_service/src/config.rs@@ -118,6 +118,11 @@ pub struct Config {pub internal_api_key: InternalApiKey,/// Comma-separated Kafka bootstrap servers for the macro event broker.pub kafka_brokers: KafkaBrokers,+ /// Whether Gmail link consent requests the Google Calendar scope. Off by+ /// default so deployed environments don't ask users for a scope the+ /// calendar feature isn't using yet.+ #[macro_config_default(false)]+ pub calendar_scope_enabled: bool,}impl Config {diff --git a/services/authentication_service/src/main.rs b/services/authentication_service/src/main.rsindex 7475defb78..38f688ea71 100644--- a/services/authentication_service/src/main.rs+++ b/services/authentication_service/src/main.rs@@ -410,6 +410,7 @@ async fn main() -> anyhow::Result<()> {sqs_client,environment: config.environment,rate_limit_service: rate_limit,+ calendar_scope_enabled: config.calendar_scope_enabled,jwt_args,authorization_state,token_context: MacroApiTokenContext {diff --git a/services/email_service/src/bin/pubsub_workers/pubsub_workers.rs b/services/email_service/src/bin/pubsub_workers/pubsub_workers.rsindex 9a1c2d1e49..1652033d18 100644--- a/services/email_service/src/bin/pubsub_workers/pubsub_workers.rs+++ b/services/email_service/src/bin/pubsub_workers/pubsub_workers.rs@@ -106,6 +106,7 @@ async fn main() -> anyhow::Result<()> {calendar_events::domain::service::GoogleCalendarSyncScheduler::new(calendar_events::outbound::pg::PgCalendarRepository::new(db.clone()),),+ config.calendar_sync_enabled,worker_cancellation_token.clone(),));let macro_event_broker = MacroEventBrokerService::new(@@ -358,6 +359,7 @@ async fn main() -> anyhow::Result<()> {crm_service_inbox_sync,macro_event_broker_inbox_sync,config.notifications_enabled,+ config.calendar_sync_enabled,false,cancellation_token,)@@ -402,6 +404,7 @@ async fn main() -> anyhow::Result<()> {crm_service_inbox_sync,macro_event_broker_inbox_sync,config.notifications_enabled,+ config.calendar_sync_enabled,true,cancellation_token,)@@ -500,6 +503,7 @@ async fn main() -> anyhow::Result<()> {crm_service_backfill,macro_event_broker_backfill,config.notifications_enabled,+ config.calendar_sync_enabled,cancellation_token,).await;diff --git a/services/email_service/src/calendar_outbox.rs b/services/email_service/src/calendar_outbox.rsindex 1612133f2c..af574917ed 100644--- a/services/email_service/src/calendar_outbox.rs+++ b/services/email_service/src/calendar_outbox.rs@@ -42,6 +42,7 @@ pub async fn run<R>(db: PgPool,sqs: SQS,scheduler: GoogleCalendarSyncScheduler<R>,+ calendar_sync_enabled: bool,cancellation_token: tokio_util::sync::CancellationToken,) whereR: GoogleCalendarSyncRepository,@@ -50,19 +51,24 @@ pub async fn run<R>(if cancellation_token.is_cancelled() {return;}- scheduler- .run_once(Utc::now())- .await- .inspect_err(|error| {- tracing::error!(error=?error, "failed to schedule due Google Calendar syncs");- })- .ok();- drain_calendar(&db, &sqs)- .await- .inspect_err(|error| {- tracing::error!(error = ?error, "failed to publish calendar backfill outbox");- })- .ok();+ // With calendar sync disabled, outbox rows keep accumulating+ // unpublished and due syncs never re-arm — enabling the flag later+ // resumes everything without a migration.+ if calendar_sync_enabled {+ scheduler+ .run_once(Utc::now())+ .await+ .inspect_err(|error| {+ tracing::error!(error=?error, "failed to schedule due Google Calendar syncs");+ })+ .ok();+ drain_calendar(&db, &sqs)+ .await+ .inspect_err(|error| {+ tracing::error!(error = ?error, "failed to publish calendar backfill outbox");+ })+ .ok();+ }drain_email_init(&db, &sqs).await.inspect_err(|error| {@@ -153,6 +159,24 @@ fn to_email_completion_message(row: &EmailCompletionOutboxRow) -> Option<Backfil})}+/// Return a calendar backfill delivery to the outbox so the drain republishes+/// it once calendar sync is enabled again. Deliveries received while the+/// switch is off would otherwise be acked and lost.+#[tracing::instrument(skip(db), err)]+pub async fn republish_calendar_job(db: &PgPool, backfill_job_id: Uuid) -> anyhow::Result<()> {+ sqlx::query!(+ r#"+ UPDATE calendar_sync_outbox+ SET published_at = NULL+ WHERE backfill_job_id = $1+ "#,+ backfill_job_id,+ )+ .execute(db)+ .await?;+ Ok(())+}+#[tracing::instrument(skip(db, sqs), err)]async fn drain_calendar(db: &PgPool, sqs: &SQS) -> anyhow::Result<usize> {let mut published = 0;diff --git a/services/email_service/src/config.rs b/services/email_service/src/config.rsindex 49fa8f5106..b8a12dbeeb 100644--- a/services/email_service/src/config.rs+++ b/services/email_service/src/config.rs@@ -55,6 +55,13 @@ pub struct Config {#[macro_config_default(false)]pub use_apollo_crm_enrichment: bool,+ /// Master switch for calendar ingestion and sync. When `false` (the+ /// default) inline ICS extraction is skipped, the calendar outbox stays+ /// parked, and calendar backfill deliveries requeue their outbox rows.+ /// Grant bookkeeping still runs so enabling later resumes cleanly.+ #[macro_config_default(false)]+ pub calendar_sync_enabled: bool,+/// Apollo.io API key for CRM enrichment. Locally this is the key/// itself; in deployed envs it's the name of the Secrets Manager/// secret holding it (resolved at startup). Empty disables enrichment.diff --git a/services/email_service/src/openapi.rs b/services/email_service/src/openapi.rsindex 591ac02713..8ee6895179 100644--- a/services/email_service/src/openapi.rs+++ b/services/email_service/src/openapi.rs@@ -4,6 +4,7 @@ mod api;mod backfill_completion_service;mod backfill_init_service;mod calendar_ingest;+mod calendar_outbox;mod config;mod convert;mod pubsub;diff --git a/services/email_service/src/pubsub/backfill/backfill_message.rs b/services/email_service/src/pubsub/backfill/backfill_message.rsindex 186ffc173d..e8a63b3d90 100644--- a/services/email_service/src/pubsub/backfill/backfill_message.rs+++ b/services/email_service/src/pubsub/backfill/backfill_message.rs@@ -97,29 +97,31 @@ pub async fn backfill_message(})})?;- crate::calendar_ingest::ingest_calendar_parts(- &ctx.db,- &ctx.gmail_client,- &ctx.redis_client,- crate::calendar_ingest::CalendarIngestInput {- is_backfill: true,- access_token,- owner_id: link.macro_id.as_ref(),- email_link_id: link.id,- email_thread_id: persisted_thread_id,- email_message_id: message_db_id,- provider_message_id: &p.message_provider_id,- payload: &calendar_payload,- },- )- .await- .inspect_err(|error| {- // Calendar extraction is best-effort: a Gmail or database failure- // here must not block the message's core side effects, and the- // durable email-ICS backfill re-extracts anything missed.- tracing::warn!(error = ?error, "failed to extract calendar invitation");- })- .ok();+ if ctx.calendar_sync_enabled {+ crate::calendar_ingest::ingest_calendar_parts(+ &ctx.db,+ &ctx.gmail_client,+ &ctx.redis_client,+ crate::calendar_ingest::CalendarIngestInput {+ is_backfill: true,+ access_token,+ owner_id: link.macro_id.as_ref(),+ email_link_id: link.id,+ email_thread_id: persisted_thread_id,+ email_message_id: message_db_id,+ provider_message_id: &p.message_provider_id,+ payload: &calendar_payload,+ },+ )+ .await+ .inspect_err(|error| {+ // Calendar extraction is best-effort: a Gmail or database failure+ // here must not block the message's core side effects, and the+ // durable email-ICS backfill re-extracts anything missed.+ tracing::warn!(error = ?error, "failed to extract calendar invitation");+ })+ .ok();+ }// Fan out a PopulateCrmContact job per address involved in the// message — every non-draft message contributes, in bothdiff --git a/services/email_service/src/pubsub/backfill/process.rs b/services/email_service/src/pubsub/backfill/process.rsindex 1d975536e4..e3de175b6c 100644--- a/services/email_service/src/pubsub/backfill/process.rs+++ b/services/email_service/src/pubsub/backfill/process.rs@@ -17,6 +17,30 @@ use models_email::email::service::pubsub::{DetailedError, FailureReason, Processuse sqs_worker::cleanup_message;use uuid::Uuid;+/// When calendar sync is disabled, return the delivery's outbox row to the+/// unpublished state and ack the message; the outbox drain republishes it+/// once the switch flips back on. `None` means calendar sync is enabled and+/// the delivery should proceed.+async fn park_calendar_delivery(+ ctx: &PubSubContext,+ calendar_job_id: Uuid,+) -> Option<Result<(), ProcessingError>> {+ if ctx.calendar_sync_enabled {+ return None;+ }+ tracing::info!(%calendar_job_id, "calendar sync disabled; returning delivery to the outbox");+ Some(+ crate::calendar_outbox::republish_calendar_job(&ctx.db, calendar_job_id)+ .await+ .map_err(|e| {+ ProcessingError::Retryable(DetailedError {+ reason: FailureReason::DatabaseQueryFailed,+ source: e.context("failed to return calendar delivery to the outbox"),+ })+ }),+ )+}+// Process a single message from the backfill queuepub async fn process_message(ctx: PubSubContext,@@ -134,6 +158,9 @@ async fn inner_process_message(.await}BackfillOperation::CalendarGoogleBackfill(scope) => {+ if let Some(parked) = park_calendar_delivery(ctx, scope.payload.calendar_job_id).await {+ return parked;+ }let link = fetch_link(ctx, scope.link_id).await?;// Calendar jobs can be created immediately after an incremental// Google scope grant. Bypass the Gmail token cache so this job does@@ -166,6 +193,9 @@ async fn inner_process_message(.await}BackfillOperation::CalendarEmailIcsBackfill(scope) => {+ if let Some(parked) = park_calendar_delivery(ctx, scope.payload.calendar_job_id).await {+ return parked;+ }let link = fetch_link(ctx, scope.link_id).await?;calendar_email_ics_backfill::calendar_email_ics_backfill(ctx, &link, &scope.payload).awaitdiff --git a/services/email_service/src/pubsub/backfill/worker.rs b/services/email_service/src/pubsub/backfill/worker.rsindex 88055f8677..01414e4503 100644--- a/services/email_service/src/pubsub/backfill/worker.rs+++ b/services/email_service/src/pubsub/backfill/worker.rs@@ -34,6 +34,7 @@ pub async fn run_worker(crm_service: CrmServiceType,macro_event_broker: PubSubEventBroker,notifications_enabled: bool,+ calendar_sync_enabled: bool,) {run_worker_with_cancellation(db,@@ -51,6 +52,7 @@ pub async fn run_worker(crm_service,macro_event_broker,notifications_enabled,+ calendar_sync_enabled,CancellationToken::new(),).await;@@ -76,6 +78,7 @@ pub async fn run_worker_with_cancellation(crm_service: CrmServiceType,macro_event_broker: PubSubEventBroker,notifications_enabled: bool,+ calendar_sync_enabled: bool,cancellation_token: CancellationToken,) {let calendar_backfills =@@ -96,6 +99,7 @@ pub async fn run_worker_with_cancellation(crm_service,macro_event_broker,notifications_enabled,+ calendar_sync_enabled,retry_worker: false,calendar_backfills,};diff --git a/services/email_service/src/pubsub/context.rs b/services/email_service/src/pubsub/context.rsindex 249c0fdba9..57ab116683 100644--- a/services/email_service/src/pubsub/context.rs+++ b/services/email_service/src/pubsub/context.rs@@ -157,6 +157,7 @@ pub struct PubSubContext {pub crm_service: CrmServiceType,pub macro_event_broker: PubSubEventBroker,pub notifications_enabled: bool,+ pub calendar_sync_enabled: bool,pub retry_worker: bool,pub calendar_backfills: CalendarBackfillServices,}diff --git a/services/email_service/src/pubsub/inbox_sync/operations/upsert_message.rs b/services/email_service/src/pubsub/inbox_sync/operations/upsert_message.rsindex ccf9bc298e..15d9f8a01f 100644--- a/services/email_service/src/pubsub/inbox_sync/operations/upsert_message.rs+++ b/services/email_service/src/pubsub/inbox_sync/operations/upsert_message.rs@@ -323,29 +323,31 @@ pub async fn upsert_message(publish_email_event(&ctx.macro_event_broker, &event);}- crate::calendar_ingest::ingest_calendar_parts(- &ctx.db,- &ctx.gmail_client,- &ctx.redis_client,- crate::calendar_ingest::CalendarIngestInput {- is_backfill: false,- access_token: &gmail_access_token,- owner_id: link.macro_id.as_ref(),- email_link_id: link.id,- email_thread_id: thread_db_id,- email_message_id: message_db_id,- provider_message_id: &payload.provider_message_id,- payload: &calendar_payload,- },- )- .await- .inspect_err(|error| {- // Calendar extraction is best-effort: a Gmail or database failure- // here must not block the message's core side effects, and the- // durable email-ICS backfill re-extracts anything missed.- tracing::warn!(error = ?error, "failed to extract calendar invitation");- })- .ok();+ if ctx.calendar_sync_enabled {+ crate::calendar_ingest::ingest_calendar_parts(+ &ctx.db,+ &ctx.gmail_client,+ &ctx.redis_client,+ crate::calendar_ingest::CalendarIngestInput {+ is_backfill: false,+ access_token: &gmail_access_token,+ owner_id: link.macro_id.as_ref(),+ email_link_id: link.id,+ email_thread_id: thread_db_id,+ email_message_id: message_db_id,+ provider_message_id: &payload.provider_message_id,+ payload: &calendar_payload,+ },+ )+ .await+ .inspect_err(|error| {+ // Calendar extraction is best-effort: a Gmail or database failure+ // here must not block the message's core side effects, and the+ // durable email-ICS backfill re-extracts anything missed.+ tracing::warn!(error = ?error, "failed to extract calendar invitation");+ })+ .ok();+ }handle_attachment_upload(ctx,diff --git a/services/email_service/src/pubsub/inbox_sync/worker.rs b/services/email_service/src/pubsub/inbox_sync/worker.rsindex 8c0ae90eae..93478560b4 100644--- a/services/email_service/src/pubsub/inbox_sync/worker.rs+++ b/services/email_service/src/pubsub/inbox_sync/worker.rs@@ -34,6 +34,7 @@ pub async fn run_worker(crm_service: CrmServiceType,macro_event_broker: PubSubEventBroker,notifications_enabled: bool,+ calendar_sync_enabled: bool,retry_worker: bool,) {run_worker_with_cancellation(@@ -52,6 +53,7 @@ pub async fn run_worker(crm_service,macro_event_broker,notifications_enabled,+ calendar_sync_enabled,retry_worker,CancellationToken::new(),)@@ -78,6 +80,7 @@ pub async fn run_worker_with_cancellation(crm_service: CrmServiceType,macro_event_broker: PubSubEventBroker,notifications_enabled: bool,+ calendar_sync_enabled: bool,retry_worker: bool,cancellation_token: CancellationToken,) {@@ -99,6 +102,7 @@ pub async fn run_worker_with_cancellation(crm_service,macro_event_broker,notifications_enabled,+ calendar_sync_enabled,retry_worker,calendar_backfills,};diff --git a/tooling/xtask/crates/xtask_local/src/local/local_env.rs b/tooling/xtask/crates/xtask_local/src/local/local_env.rsindex 17c614048e..e8df51b218 100644--- a/tooling/xtask/crates/xtask_local/src/local/local_env.rs+++ b/tooling/xtask/crates/xtask_local/src/local/local_env.rs@@ -65,6 +65,10 @@ impl LocalEnv {env.insert("COMPOSE_PROJECT_NAME".into(), self.project_name.clone());env.insert("PORT".into(), "8080".into());env.insert("FRONTEND_PORT".into(), self.frontend_port.to_string());+ // Calendar ingestion/sync ships dark (both flags default off in+ // 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());self.infra.write(&mut env);self.storage.write(&mut env);self.queues.write(&mut env);