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(email): retry Gmail reads once with a fresh token after a 401 (#8422)
teoa965c656264b
diff --git a/crates/email_api_client/AGENTS.md b/crates/email_api_client/AGENTS.mdindex 586751a3b8..d826233a69 100644--- a/crates/email_api_client/AGENTS.md+++ b/crates/email_api_client/AGENTS.md@@ -50,6 +50,10 @@ Outlook (Microsoft Graph) outbound adapters.- The service is generic over `<R, T, L>` (repository, token source, ratelimiter) — add a new dependency as a port + generic bound, not a concretefield.+- Link-based reads go through `read_with_token`, which retries once with a+ fresh token when the provider answers `AuthRequired`; a cached token can+ outlive a revoked grant, and only the fresh fetch lets the token source mark+ the link for reauthorization. Writes use `prepare` and do not retry.- Graph answers a bodyless POST or PATCH without `Content-Length: 0` with 411Length Required. `transport.rs` adds the header, so new Graph calls must gothrough `request`/`conditional_request` rather than the raw client.diff --git a/crates/email_api_client/src/domain/service.rs b/crates/email_api_client/src/domain/service.rsindex a51e8b5271..925fad096e 100644--- a/crates/email_api_client/src/domain/service.rs+++ b/crates/email_api_client/src/domain/service.rs@@ -82,6 +82,41 @@ where.inspect_err(log_operation_error)}+ /// Runs a provider read with the cached token, retrying once with a fresh+ /// token if the provider rejects it, and logs a failure in the calling+ /// operation's span.+ ///+ /// A cached token can outlive its grant. Only a fresh fetch reaches the+ /// authentication service, which either renews the token or reports the+ /// revoked grant so the token source marks the link for reauthorization.+ ///+ /// Callers declare `retry_count = 0` on their span; a retry sets it to 1.+ async fn read_with_token<O, F, Fut>(+ &self,+ link_id: Uuid,+ operation: ApiOperationKind,+ read: F,+ ) -> Result<O, EmailApiError>+ where+ F: Fn(AccessToken) -> Fut,+ Fut: Future<Output = Result<O, EmailApiError>>,+ {+ let access_token = self.prepare(link_id, operation).await?;+ let result = match read(access_token).await {+ Err(EmailApiError::AuthRequired) => {+ tracing::Span::current().record("retry_count", 1);+ self.check_rate_limit(link_id, operation).await?;+ let fresh = self+ .get_access_token(link_id, TokenFreshness::Fresh)+ .await+ .inspect_err(log_operation_error)?;+ read(fresh).await+ }+ result => result,+ };+ result.inspect_err(log_operation_error)+ }+/// Applies local quota policy, logging a refusal in the calling/// operation's span.async fn check_rate_limit(diff --git a/crates/email_api_client/src/domain/service/attachments.rs b/crates/email_api_client/src/domain/service/attachments.rsindex 106e640267..f138c3b256 100644--- a/crates/email_api_client/src/domain/service/attachments.rs+++ b/crates/email_api_client/src/domain/service/attachments.rs@@ -2,7 +2,7 @@ use uuid::Uuid;use super::super::models::EmailApiError;use super::super::ports::{MailboxAttachmentClient, ProviderRateLimiter, ProviderTokenSource};-use super::{ApiOperationKind, EmailApiClientServiceImpl, log_operation_error};+use super::{ApiOperationKind, EmailApiClientServiceImpl};impl<R, T, L> EmailApiClientServiceImpl<R, T, L>where@@ -11,21 +11,23 @@ whereL: ProviderRateLimiter,{/// Downloads an attachment from a provider message.- #[tracing::instrument(skip(self))]+ #[tracing::instrument(skip(self), fields(retry_count = 0))]pub async fn get_attachment(&self,link_id: Uuid,provider_message_id: &str,provider_attachment_id: &str,) -> Result<Vec<u8>, EmailApiError> {- let access_token = self- .prepare(link_id, ApiOperationKind::GetAttachment)- .await?;-- self.repository- .get_attachment(&access_token, provider_message_id, provider_attachment_id)- .await- .inspect_err(log_operation_error)+ self.read_with_token(+ link_id,+ ApiOperationKind::GetAttachment,+ |token| async move {+ self.repository+ .get_attachment(&token, provider_message_id, provider_attachment_id)+ .await+ },+ )+ .await}}diff --git a/crates/email_api_client/src/domain/service/labels.rs b/crates/email_api_client/src/domain/service/labels.rsindex 21d5d83f41..d1e9dc2aa5 100644--- a/crates/email_api_client/src/domain/service/labels.rs+++ b/crates/email_api_client/src/domain/service/labels.rs@@ -12,14 +12,12 @@ whereL: ProviderRateLimiter,{/// Lists all labels for a linked mailbox.- #[tracing::instrument(skip(self))]+ #[tracing::instrument(skip(self), fields(retry_count = 0))]pub async fn list_labels(&self, link_id: Uuid) -> Result<Vec<Label>, EmailApiError> {- let access_token = self.prepare(link_id, ApiOperationKind::ListLabels).await?;-- self.repository- .list_labels(&access_token, link_id)- .await- .inspect_err(log_operation_error)+ self.read_with_token(link_id, ApiOperationKind::ListLabels, |token| async move {+ self.repository.list_labels(&token, link_id).await+ })+ .await}/// Creates a user label.diff --git a/crates/email_api_client/src/domain/service/messages.rs b/crates/email_api_client/src/domain/service/messages.rsindex bbe932efc3..0623e60382 100644--- a/crates/email_api_client/src/domain/service/messages.rs+++ b/crates/email_api_client/src/domain/service/messages.rs@@ -15,85 +15,87 @@ where{/// Fetches and normalizes one provider message, including any calendar/// invitation parts, in a single provider read.- #[tracing::instrument(skip(self))]+ #[tracing::instrument(skip(self), fields(retry_count = 0))]pub async fn get_message(&self,link_id: Uuid,provider_message_id: &str,) -> Result<Option<MessageWithCalendarParts>, EmailApiError> {- let access_token = self.prepare(link_id, ApiOperationKind::GetMessage).await?;-- self.repository- .get_message(&access_token, link_id, provider_message_id)- .await- .inspect_err(log_operation_error)+ self.read_with_token(link_id, ApiOperationKind::GetMessage, |token| async move {+ self.repository+ .get_message(&token, link_id, provider_message_id)+ .await+ })+ .await}/// Fetches the provider label identifiers attached to one message.- #[tracing::instrument(skip(self))]+ #[tracing::instrument(skip(self), fields(retry_count = 0))]pub async fn get_message_label_ids(&self,link_id: Uuid,provider_message_id: &str,) -> Result<Option<Vec<String>>, EmailApiError> {- let access_token = self.prepare(link_id, ApiOperationKind::GetMessage).await?;-- self.repository- .get_message_label_ids(&access_token, provider_message_id)- .await- .inspect_err(log_operation_error)+ self.read_with_token(link_id, ApiOperationKind::GetMessage, |token| async move {+ self.repository+ .get_message_label_ids(&token, provider_message_id)+ .await+ })+ .await}/// Lists message identifiers carrying all requested provider labels.- #[tracing::instrument(skip(self))]+ #[tracing::instrument(skip(self), fields(retry_count = 0))]pub async fn list_messages(&self,link_id: Uuid,limit: u32,label_ids: &[&str],) -> Result<Vec<String>, EmailApiError> {- let access_token = self- .prepare(link_id, ApiOperationKind::ListMessages)- .await?;-- self.repository- .list_messages(&access_token, limit, label_ids)- .await- .inspect_err(log_operation_error)+ self.read_with_token(+ link_id,+ ApiOperationKind::ListMessages,+ |token| async move {+ self.repository+ .list_messages(&token, limit, label_ids)+ .await+ },+ )+ .await}/// Lists message identifiers belonging to one provider thread.- #[tracing::instrument(skip(self))]+ #[tracing::instrument(skip(self), fields(retry_count = 0))]pub async fn get_message_ids_for_thread(&self,link_id: Uuid,provider_thread_id: &str,) -> Result<Vec<String>, EmailApiError> {- let access_token = self.prepare(link_id, ApiOperationKind::GetThread).await?;-- self.repository- .get_message_ids_for_thread(&access_token, provider_thread_id)- .await- .inspect_err(log_operation_error)+ self.read_with_token(link_id, ApiOperationKind::GetThread, |token| async move {+ self.repository+ .get_message_ids_for_thread(&token, provider_thread_id)+ .await+ })+ .await}/// Fetches and normalizes every message in one provider thread.- #[tracing::instrument(skip(self))]+ #[tracing::instrument(skip(self), fields(retry_count = 0))]pub async fn get_thread(&self,link_id: Uuid,provider_thread_id: &str,) -> Result<Vec<Message>, EmailApiError> {- let access_token = self.prepare(link_id, ApiOperationKind::GetThread).await?;-- self.repository- .get_thread(&access_token, link_id, provider_thread_id)- .await- .inspect_err(log_operation_error)+ self.read_with_token(link_id, ApiOperationKind::GetThread, |token| async move {+ self.repository+ .get_thread(&token, link_id, provider_thread_id)+ .await+ })+ .await}/// Lists one page of provider threads.- #[tracing::instrument(skip(self))]+ #[tracing::instrument(skip(self), fields(retry_count = 0))]pub async fn list_threads(&self,link_id: Uuid,@@ -101,12 +103,12 @@ wherenext_page_token: Option<&str>,label_ids: &[&str],) -> Result<ThreadListPage, EmailApiError> {- let access_token = self.prepare(link_id, ApiOperationKind::ListThreads).await?;-- self.repository- .list_threads(&access_token, limit, next_page_token, label_ids)- .await- .inspect_err(log_operation_error)+ self.read_with_token(link_id, ApiOperationKind::ListThreads, |token| async move {+ self.repository+ .list_threads(&token, limit, next_page_token, label_ids)+ .await+ })+ .await}/// Applies provider label additions and removals to one message.diff --git a/crates/email_api_client/src/domain/service/sync.rs b/crates/email_api_client/src/domain/service/sync.rsindex 48c3f86417..51ebf593bb 100644--- a/crates/email_api_client/src/domain/service/sync.rs+++ b/crates/email_api_client/src/domain/service/sync.rs@@ -22,18 +22,16 @@ where}/// Lists mailbox changes following `cursor`.- #[tracing::instrument(skip(self))]+ #[tracing::instrument(skip(self), fields(retry_count = 0))]pub async fn list_changes(&self,link_id: Uuid,cursor: &SyncCursor,) -> Result<ChangeBatch, EmailApiError> {- let access_token = self.prepare(link_id, ApiOperationKind::ListChanges).await?;-- self.repository- .list_changes(&access_token, cursor)- .await- .inspect_err(log_operation_error)+ self.read_with_token(link_id, ApiOperationKind::ListChanges, |token| async move {+ self.repository.list_changes(&token, cursor).await+ })+ .await}}diff --git a/crates/email_api_client/src/domain/service/test.rs b/crates/email_api_client/src/domain/service/test.rsindex cf44d8a664..df81300114 100644--- a/crates/email_api_client/src/domain/service/test.rs+++ b/crates/email_api_client/src/domain/service/test.rs@@ -1,13 +1,112 @@+use std::sync::{Arc, Mutex};use std::time::Duration;+use models_email::email::service::label::Label;use uuid::Uuid;use super::super::models::{AccessToken, ApiOperationKind, EmailApiError, RateLimitRefusal, TokenError, TokenFreshness,};+use super::super::ports::MailboxLabelClient;use super::EmailApiClientServiceImpl;use super::test_support::{Call, FakeRateLimiter, FakeRepository, FakeTokenSource, call_log};+/// Rejects the listed tokens the way Gmail answers a 401.+#[derive(Clone, Default)]+struct TokenCheckingLabels {+ rejected: Vec<&'static str>,+ seen: Arc<Mutex<Vec<String>>>,+}++impl MailboxLabelClient for TokenCheckingLabels {+ async fn list_labels(&self, token: &AccessToken, _: Uuid) -> Result<Vec<Label>, EmailApiError> {+ let token = token.expose_secret();+ self.seen.lock().unwrap().push(token.to_string());+ if self.rejected.contains(&token) {+ Err(EmailApiError::AuthRequired)+ } else {+ Ok(Vec::new())+ }+ }++ async fn create_label(+ &self,+ _: &AccessToken,+ _: Uuid,+ _: &str,+ ) -> Result<Label, EmailApiError> {+ unreachable!("not used by these tests")+ }++ async fn delete_label(&self, _: &AccessToken, _: &str) -> Result<(), EmailApiError> {+ unreachable!("not used by these tests")+ }+}++async fn list_labels_with(+ rejected: Vec<&'static str>,+ fresh: Result<AccessToken, TokenError>,+) -> (Result<usize, EmailApiError>, Vec<Call>, Vec<String>) {+ let calls = call_log();+ let repository = TokenCheckingLabels {+ rejected,+ ..Default::default()+ };+ let seen = repository.seen.clone();+ let service = EmailApiClientServiceImpl::new(+ repository,+ FakeTokenSource::new(calls.clone(), Ok(AccessToken::new("cached"))).with_fresh(fresh),+ FakeRateLimiter::new(calls.clone(), Ok(())),+ );++ let result = service+ .list_labels(Uuid::nil())+ .await+ .map(|labels| labels.len());+ let calls = calls.lock().unwrap().clone();+ let seen = seen.lock().unwrap().clone();+ (result, calls, seen)+}++fn retried_calls() -> Vec<Call> {+ vec![+ Call::RateLimit(Uuid::nil(), ApiOperationKind::ListLabels),+ Call::Token(Uuid::nil(), TokenFreshness::Cached),+ Call::RateLimit(Uuid::nil(), ApiOperationKind::ListLabels),+ Call::Token(Uuid::nil(), TokenFreshness::Fresh),+ ]+}++#[tokio::test]+async fn a_rejected_cached_token_is_retried_with_a_fresh_one() {+ let (result, calls, seen) =+ list_labels_with(vec!["cached"], Ok(AccessToken::new("fresh"))).await;++ assert_eq!(result, Ok(0));+ assert_eq!(calls, retried_calls());+ assert_eq!(seen, ["cached", "fresh"]);+}++#[tokio::test]+async fn a_revoked_grant_found_by_the_fresh_fetch_stops_the_read() {+ let (result, calls, seen) =+ list_labels_with(vec!["cached"], Err(TokenError::ReauthRequired)).await;++ assert_eq!(result, Err(EmailApiError::AuthRequired));+ assert_eq!(calls, retried_calls());+ assert_eq!(seen, ["cached"]);+}++#[tokio::test]+async fn a_rejected_fresh_token_is_not_retried_again() {+ let (result, calls, seen) =+ list_labels_with(vec!["cached", "fresh"], Ok(AccessToken::new("fresh"))).await;++ assert_eq!(result, Err(EmailApiError::AuthRequired));+ assert_eq!(calls, retried_calls());+ assert_eq!(seen, ["cached", "fresh"]);+}+#[tokio::test]async fn token_failure_stops_before_repository_after_the_quota_check() {let calls = call_log();diff --git a/crates/email_api_client/src/domain/service/test_support.rs b/crates/email_api_client/src/domain/service/test_support.rsindex 15d9be3420..ad12f44305 100644--- a/crates/email_api_client/src/domain/service/test_support.rs+++ b/crates/email_api_client/src/domain/service/test_support.rs@@ -33,11 +33,22 @@ pub(super) fn call_log() -> CallLog {pub(super) struct FakeTokenSource {calls: CallLog,result: Result<AccessToken, TokenError>,+ fresh_result: Option<Result<AccessToken, TokenError>>,}impl FakeTokenSource {pub(super) fn new(calls: CallLog, result: Result<AccessToken, TokenError>) -> Self {- Self { calls, result }+ Self {+ calls,+ result,+ fresh_result: None,+ }+ }++ /// Answers fresh link-id fetches with `result` instead.+ pub(super) fn with_fresh(mut self, result: Result<AccessToken, TokenError>) -> Self {+ self.fresh_result = Some(result);+ self}}@@ -51,7 +62,10 @@ impl ProviderTokenSource for FakeTokenSource {.lock().unwrap().push(Call::Token(link_id, freshness));- self.result.clone()+ match (&self.fresh_result, freshness) {+ (Some(fresh), TokenFreshness::Fresh) => fresh.clone(),+ _ => self.result.clone(),+ }}async fn get_access_token_for_link(