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): send content-length on bodyless outlook graph writes (#8338)
teo22cdf934a019
diff --git a/crates/email_api_client/AGENTS.md b/crates/email_api_client/AGENTS.mdindex 7d3fafb8ef..586751a3b8 100644--- a/crates/email_api_client/AGENTS.md+++ b/crates/email_api_client/AGENTS.md@@ -3,8 +3,8 @@## What it isPart of the Email feature: provider-neutral contracts and models for email-provider integrations (sync, send, tokens, rate limits), plus the Gmail-outbound adapter.+provider integrations (sync, send, tokens, rate limits), plus the Gmail and+Outlook (Microsoft Graph) outbound adapters.## Start here@@ -15,6 +15,9 @@ outbound adapter.- `src/domain/ports.rs`: `ProviderRateLimiter`, `ProviderTokenSource`, and therepository trait adapters implement.- `src/outbound/gmail.rs`: the Gmail adapter (feature `outbound-gmail`).+- `src/outbound/outlook.rs`: the Microsoft Graph adapter (feature+ `outbound-outlook`); every Graph call goes through `outlook/transport.rs`,+ which owns request headers, provider error mapping, and write-failure logs.## Scope@@ -26,8 +29,9 @@ outbound adapter.## Boundaries- Depends on: `email_utils`, `models_email`, `gmail_client` (feature- `outbound-gmail`).-- Used by: `services/email_service`.+ `outbound-gmail`), `calendar_events` (feature `outbound-outlook`).+- Used by: `crates/email` (ports only), `services/email_service`,+ `services/calendar_service`.## Recipes@@ -38,7 +42,7 @@ outbound adapter.## Checks-- `cargo test -p email_api_client --features outbound-gmail`.+- `cargo test -p email_api_client --all-features`.## Gotchas@@ -46,6 +50,9 @@ outbound adapter.- The service is generic over `<R, T, L>` (repository, token source, ratelimiter) — add a new dependency as a port + generic bound, not a concretefield.+- Graph answers a bodyless POST or PATCH without `Content-Length: 0` with 411+ Length Required. `transport.rs` adds the header, so new Graph calls must go+ through `request`/`conditional_request` rather than the raw client.## Relateddiff --git a/crates/email_api_client/src/outbound/outlook/test.rs b/crates/email_api_client/src/outbound/outlook/test.rsindex 8ee002da65..a492bfc112 100644--- a/crates/email_api_client/src/outbound/outlook/test.rs+++ b/crates/email_api_client/src/outbound/outlook/test.rs@@ -124,6 +124,29 @@ async fn accepted_send_does_not_require_a_response_body() {);}+#[tokio::test]+async fn bodyless_send_declares_zero_content_length() {+ let (server, api, token) = setup().await;+ Mock::given(method("POST"))+ .and(path("/v1.0/me/messages/draft/send"))+ .and(header("content-length", "0"))+ .respond_with(ResponseTemplate::new(202))+ .expect(1)+ .mount(&server)+ .await;+ Mock::given(method("POST"))+ .and(path("/v1.0/me/messages/draft/send"))+ .respond_with(ResponseTemplate::new(411))+ .mount(&server)+ .await;+ assert_eq!(+ api.submit_draft(&token, &ProviderId::new("draft").unwrap())+ .await+ .unwrap(),+ SubmissionOutcome::Accepted+ );+}+#[tokio::test]async fn throttling_respects_provider_retry_after() {let (server, api, token) = setup().await;diff --git a/crates/email_api_client/src/outbound/outlook/transport.rs b/crates/email_api_client/src/outbound/outlook/transport.rsindex e8f12f45ae..74fe6a153f 100644--- a/crates/email_api_client/src/outbound/outlook/transport.rs+++ b/crates/email_api_client/src/outbound/outlook/transport.rs@@ -62,14 +62,20 @@ impl OutlookApiClientRepository {let token = refreshed.as_ref().unwrap_or(token);let mut request = self.client- .request(method, url)+ .request(method.clone(), url).bearer_auth(token.expose_secret()).header("Prefer", IMMUTABLE_ID_PREFERENCE);if let Some(version) = expected_version {request = request.header("If-Match", version);}- if let Some(body) = body {- request = request.json(body);+ match body {+ Some(body) => request = request.json(body),+ // reqwest omits Content-Length for an empty body, and Graph answers a+ // bodyless POST or PATCH without it with 411 Length Required.+ None if matches!(method, Method::POST | Method::PATCH) => {+ request = request.header("Content-Length", 0)+ }+ None => {}}let retry = self.rejected_token_refresh@@ -129,13 +135,23 @@ impl OutlookApiClientRepository {}async fn send_request(request: RequestBuilder) -> Result<Response, EmailApiError> {- let response = request.send().await.map_err(|_| EmailApiError::Transient {+ let transport_failed = |_| EmailApiError::Transient {message: "Microsoft Graph transport failed".into(),- })?;+ };+ let (client, request) = request.build_split();+ let request = request.map_err(transport_failed)?;+ let method = request.method().clone();+ let response = client.execute(request).await.map_err(transport_failed)?;if response.status().is_success() {return Ok(response);}let status = response.status();+ let path = response.url().path().to_owned();+ let request_id = response+ .headers()+ .get("request-id")+ .and_then(|v| v.to_str().ok())+ .map(str::to_owned);let retry_after = response.headers().get("Retry-After")@@ -147,8 +163,23 @@ impl OutlookApiClientRepository {.map(|date| date.duration_since(SystemTime::now()).unwrap_or_default())})});- let body: Value = response.json().await.unwrap_or(Value::Null);- let code = body.pointer("/error/code").and_then(Value::as_str);+ let body = response.text().await.unwrap_or_default();+ let error: Value = serde_json::from_str(&body).unwrap_or(Value::Null);+ let code = error.pointer("/error/code").and_then(Value::as_str);+ if method != Method::GET {+ // Graph error messages can quote recipient addresses. The code and+ // request-id identify the failure; only bodies without a Graph error+ // code, such as the front end's 411 page, are logged verbatim.+ tracing::warn!(+ %method,+ path,+ status = status.as_u16(),+ graph_error_code = code,+ graph_request_id = request_id.as_deref(),+ body = code.is_none().then_some(body.as_str()),+ "Microsoft Graph rejected a write"+ );+ }// Do not propagate provider messages, bodies, request URLs, or cursor tokens.Err(match status {StatusCode::UNAUTHORIZED => EmailApiError::AuthRequired,