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
seanaye/chore/tweak edge trait (#4856)
Sean Aye3e1e1238525c
diff --git a/Cargo.lock b/Cargo.lockindex 7a95ff6a24..db3536ae61 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -5966,6 +5966,7 @@ dependencies = ["async-graphql","chrono","email",+ "entity_access","macro_user_id","tokio","tracing",diff --git a/crates/complete_graph/Cargo.toml b/crates/complete_graph/Cargo.tomlindex 539a910bfb..493d49eacd 100644--- a/crates/complete_graph/Cargo.toml+++ b/crates/complete_graph/Cargo.toml@@ -1,8 +1,8 @@[package]-name = "complete_graph"-version = "0.1.0"edition = "2024"+name = "complete_graph"publish = false+version = "0.1.0"[dependencies]anyhow = { workspace = true }@@ -25,6 +25,7 @@ graphql_soup = { path = "../graphql_soup" }model-entity = { path = "../model-entity" }model_user = { path = "../model_user", features = ["axum"] }soup = { path = "../soup", default-features = false, features = ["ports"] }+uuid = { workspace = true }workspace-hack = { version = "0.1", path = "../workspace-hack" }[dev-dependencies]diff --git a/crates/complete_graph/src/edges.rs b/crates/complete_graph/src/edges.rsindex 2685ab23cc..5b827c96e1 100644--- a/crates/complete_graph/src/edges.rs+++ b/crates/complete_graph/src/edges.rs@@ -9,8 +9,9 @@ use graphql_notification::{};use graphql_properties::{EntityPropertyReader, GraphqlProperty, load_entity_properties};use graphql_soup::SoupEntityEdges;+use uuid::Uuid;-/// Zero-sized marker tying an edge to its configured reader types.+/// The types of the edge readers for souptype EdgeReaders<NR, PR, ER> = PhantomData<fn() -> (NR, PR, ER)>;/// Notification, property, and email-content fields attached to Soup entities.@@ -48,9 +49,9 @@ where}}- fn email_thread_edges(&self) -> Self::EmailThreadEdges {+ fn email_thread_edges(thread_id: Uuid) -> Self::EmailThreadEdges {SoupEmailThreadEdges {- thread_id: self.entity.entity_id.to_string(),+ thread_id,_reader: PhantomData,}}@@ -80,16 +81,16 @@ where/// Email-content fields attached only to Soup email-thread entities.pub struct SoupEmailThreadEdges<ER> {- /// Identifier of the email thread whose content is being resolved.- thread_id: String,- /// Associates the edge with its configured email-content reader.+ /// The uuid of the email thread+ thread_id: Uuid,+ /// marker for the reader type_reader: PhantomData<fn() -> ER>,}impl<ER> Clone for SoupEmailThreadEdges<ER> {fn clone(&self) -> Self {Self {- thread_id: self.thread_id.clone(),+ thread_id: self.thread_id,_reader: PhantomData,}}@@ -109,7 +110,7 @@ whereload_latest_email_message::<ER>(ctx,EmailContentKey {- thread_id: self.thread_id.clone(),+ thread_id: self.thread_id,},).awaitdiff --git a/crates/complete_graph/src/lib.rs b/crates/complete_graph/src/lib.rsindex 31de6292c5..777ceae15b 100644--- a/crates/complete_graph/src/lib.rs+++ b/crates/complete_graph/src/lib.rs@@ -14,8 +14,8 @@ mod sdl_test;pub use edges::{SoupEdges, SoupEmailThreadEdges};pub use graphql_common::GraphqlSoupRequestParts;pub use graphql_email::{- EmailContentKey, EmailContentLoad, EmailContentLoader, NoOpSoupEmailContentEdgeReader,- SoupEmailContentEdgeReader, email_content_loader,+ EmailContentKey, EmailContentLoad, EmailContentLoader, EmailServiceEmailContentReader,+ NoOpSoupEmailContentEdgeReader, SoupEmailContentEdgeReader, email_content_loader,};pub use graphql_notification::{EntityNotificationsLoader, SoupNotificationEdgeReader, entity_notifications_loader,diff --git a/crates/graphql_email/Cargo.toml b/crates/graphql_email/Cargo.tomlindex 7f1287535e..c8c5f15f10 100644--- a/crates/graphql_email/Cargo.toml+++ b/crates/graphql_email/Cargo.toml@@ -8,6 +8,7 @@ publish = falseasync-graphql = { workspace = true, features = ["dataloader"] }chrono = { workspace = true }email = { path = "../email", default-features = false, features = ["ports"] }+entity_access = { path = "../entity_access", default-features = false }macro_user_id = { path = "../macro_user_id" }tokio = { workspace = true }tracing = { workspace = true }diff --git a/crates/graphql_email/src/lib.rs b/crates/graphql_email/src/lib.rsindex 09c878592b..1d189b2609 100644--- a/crates/graphql_email/src/lib.rs+++ b/crates/graphql_email/src/lib.rs@@ -6,7 +6,7 @@ mod loaders;mod objects;pub use loaders::{- EmailContentKey, EmailContentLoad, EmailContentLoader, NoOpSoupEmailContentEdgeReader,- SoupEmailContentEdgeReader, email_content_loader,+ EmailContentKey, EmailContentLoad, EmailContentLoader, EmailServiceEmailContentReader,+ NoOpSoupEmailContentEdgeReader, SoupEmailContentEdgeReader, email_content_loader,};pub use objects::{GraphqlSoupEmailMessage, load_latest_email_message};diff --git a/crates/graphql_email/src/loaders.rs b/crates/graphql_email/src/loaders.rsindex c1e1a93306..e02ad70909 100644--- a/crates/graphql_email/src/loaders.rs+++ b/crates/graphql_email/src/loaders.rs@@ -1,8 +1,10 @@use std::{collections::HashMap, sync::Arc};use async_graphql::dataloader::{DataLoader, Loader};-use email::domain::models::ParsedMessage;+use email::domain::{models::ParsedMessage, ports::EmailContentService};+use entity_access::domain::{models::AccessError, ports::EntityAccessService};use macro_user_id::user_id::MacroUserIdStr;+use uuid::Uuid;pub(crate) const MAX_EMAIL_CONTENT_KEYS: usize = 20;@@ -18,10 +20,10 @@ pub enum EmailContentLoad {}/// A request for the newest non-draft content message belonging to an email thread.-#[derive(Debug, Clone, Hash, PartialEq, Eq)]+#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]pub struct EmailContentKey {/// Email thread ID.- pub thread_id: String,+ pub thread_id: Uuid,}/// Reader used by the Soup email-content GraphQL edge.@@ -50,6 +52,103 @@ impl SoupEmailContentEdgeReader for NoOpSoupEmailContentEdgeReader {}}+/// GraphQL email-content reader backed by the email domain service and the+/// canonical entity-access service.+pub struct EmailServiceEmailContentReader<S, A> {+ email_service: Arc<S>,+ entity_access_service: Arc<A>,+}++impl<S, A> EmailServiceEmailContentReader<S, A> {+ /// Create an email-content reader from services supplied by the application+ /// composition root.+ pub fn new(email_service: Arc<S>, entity_access_service: Arc<A>) -> Self {+ Self {+ email_service,+ entity_access_service,+ }+ }+}++impl<S, A> SoupEmailContentEdgeReader for EmailServiceEmailContentReader<S, A>+where+ S: EmailContentService,+ A: EntityAccessService,+{+ async fn get_email_content(+ &self,+ user_id: &MacroUserIdStr<'static>,+ keys: Vec<EmailContentKey>,+ ) -> HashMap<EmailContentKey, EmailContentLoad> {+ let thread_ids = keys+ .iter()+ .map(|key| key.thread_id.to_string())+ .collect::<Vec<_>>();+ let mut receipts = self+ .entity_access_service+ .generate_email_thread_view_access_receipts(user_id, None, &thread_ids)+ .await;++ let mut loads = HashMap::with_capacity(keys.len());+ let mut authorized = Vec::with_capacity(keys.len());+ let mut authorized_keys = HashMap::with_capacity(keys.len());++ for key in keys {+ match receipts+ .remove(&key.thread_id.to_string())+ .unwrap_or(Err(AccessError::Internal))+ {+ Ok(receipt) => {+ authorized.push(receipt);+ authorized_keys.insert(key.thread_id, key);+ }+ Err(+ AccessError::Unauthorized+ | AccessError::UnauthorizedWithMessage(_)+ | AccessError::NotFound(_),+ ) => {+ loads.insert(key, EmailContentLoad::Missing);+ }+ Err(error) => {+ tracing::error!(thread_id = %key.thread_id, error = ?error, "email content access check failed");+ loads.insert(key, EmailContentLoad::Failed);+ }+ }+ }++ if authorized.is_empty() {+ return loads;+ }++ match self+ .email_service+ .get_latest_messages_parsed(authorized)+ .await+ {+ Ok(mut messages) => {+ for (thread_id, key) in authorized_keys {+ let load = messages+ .remove(&thread_id)+ .map_or(EmailContentLoad::Missing, |message| {+ EmailContentLoad::Found(Box::new(message))+ });+ loads.insert(key, load);+ }+ }+ Err(error) => {+ tracing::error!(error = ?error, "bulk email content load failed");+ loads.extend(+ authorized_keys+ .into_values()+ .map(|key| (key, EmailContentLoad::Failed)),+ );+ }+ }++ loads+ }+}+/// DataLoader for the newest non-draft content message attached to Soup email threads.pub struct EmailContentLoader<R> {user_id: MacroUserIdStr<'static>,@@ -122,74 +221,4 @@ where}#[cfg(test)]-mod tests {- use std::sync::{- Arc,- atomic::{AtomicUsize, Ordering},- };-- use macro_user_id::user_id::MacroUserIdStr;-- use super::*;-- #[derive(Clone, Default)]- struct RecordingReader {- calls: Arc<AtomicUsize>,- }-- impl SoupEmailContentEdgeReader for RecordingReader {- async fn get_email_content(- &self,- _user_id: &MacroUserIdStr<'static>,- keys: Vec<EmailContentKey>,- ) -> HashMap<EmailContentKey, EmailContentLoad> {- self.calls.fetch_add(1, Ordering::SeqCst);- keys.into_iter()- .map(|key| (key, EmailContentLoad::Missing))- .collect()- }- }-- fn key(index: usize) -> EmailContentKey {- EmailContentKey {- thread_id: format!("00000000-0000-0000-0000-{index:012}"),- }- }-- #[tokio::test]- async fn batches_distinct_threads_in_one_reader_call() {- let reader = RecordingReader::default();- let user_id = MacroUserIdStr::try_from_email("[email protected]").unwrap();- let loader = email_content_loader(user_id, reader.clone());- let first = key(1);- let second = key(2);-- let loaded = loader- .load_many(vec![first.clone(), second.clone()])- .await- .unwrap();-- assert_eq!(reader.calls.load(Ordering::SeqCst), 1);- assert!(matches!(- loaded.get(&first),- Some(EmailContentLoad::Missing)- ));- assert!(matches!(- loaded.get(&second),- Some(EmailContentLoad::Missing)- ));- }-- #[tokio::test]- async fn rejects_oversized_batches_without_calling_the_reader() {- let reader = RecordingReader::default();- let user_id = MacroUserIdStr::try_from_email("[email protected]").unwrap();- let loader = EmailContentLoader::new(user_id, reader.clone());- let keys = (0..=MAX_EMAIL_CONTENT_KEYS).map(key).collect::<Vec<_>>();-- let error = loader.load(&keys).await.unwrap_err();-- assert_eq!(reader.calls.load(Ordering::SeqCst), 0);- assert!(error.to_string().contains("at most 20 threads"));- }-}+mod test;diff --git a/services/document_storage_service/src/api/graphql_soup/email_content/test.rs b/crates/graphql_email/src/loaders/test.rssimilarity index 69%rename from services/document_storage_service/src/api/graphql_soup/email_content/test.rsrename to crates/graphql_email/src/loaders/test.rsindex f925acaf12..7e9af1e2c0 100644--- a/services/document_storage_service/src/api/graphql_soup/email_content/test.rs+++ b/crates/graphql_email/src/loaders/test.rs@@ -1,15 +1,9 @@-use std::{- collections::HashMap,- sync::{- Arc,- atomic::{AtomicUsize, Ordering},- },+use std::sync::{+ Arc,+ atomic::{AtomicUsize, Ordering},};-use email::domain::{- models::{EmailErr, ParsedMessage},- ports::EmailContentService,-};+use email::domain::models::EmailErr;use entity_access::domain::{models::{AccessLevel, BotId, CallChannelInfo, EntityAccessReceipt, EntityPermission, EntityType,@@ -24,6 +18,24 @@ use macro_user_id::{use super::*;+#[derive(Clone, Default)]+struct RecordingReader {+ calls: Arc<AtomicUsize>,+}++impl SoupEmailContentEdgeReader for RecordingReader {+ async fn get_email_content(+ &self,+ _user_id: &MacroUserIdStr<'static>,+ keys: Vec<EmailContentKey>,+ ) -> HashMap<EmailContentKey, EmailContentLoad> {+ self.calls.fetch_add(1, Ordering::SeqCst);+ keys.into_iter()+ .map(|key| (key, EmailContentLoad::Missing))+ .collect()+ }+}+#[derive(Clone)]struct TestAccessService {allow: bool,@@ -150,21 +162,55 @@ impl EmailContentService for RecordingContentService {fn key(index: usize) -> EmailContentKey {EmailContentKey {- thread_id: format!("00000000-0000-0000-0000-{index:012}"),+ thread_id: Uuid::from_u128(index as u128),}}+#[tokio::test]+async fn batches_distinct_threads_in_one_reader_call() {+ let reader = RecordingReader::default();+ let user_id = MacroUserIdStr::try_from_email("[email protected]").unwrap();+ let loader = email_content_loader(user_id, reader.clone());+ let first = key(1);+ let second = key(2);++ let loaded = loader.load_many(vec![first, second]).await.unwrap();++ assert_eq!(reader.calls.load(Ordering::SeqCst), 1);+ assert!(matches!(+ loaded.get(&first),+ Some(EmailContentLoad::Missing)+ ));+ assert!(matches!(+ loaded.get(&second),+ Some(EmailContentLoad::Missing)+ ));+}++#[tokio::test]+async fn rejects_oversized_batches_without_calling_the_reader() {+ let reader = RecordingReader::default();+ let user_id = MacroUserIdStr::try_from_email("[email protected]").unwrap();+ let loader = EmailContentLoader::new(user_id, reader.clone());+ let keys = (0..=MAX_EMAIL_CONTENT_KEYS).map(key).collect::<Vec<_>>();++ let error = loader.load(&keys).await.unwrap_err();++ assert_eq!(reader.calls.load(Ordering::SeqCst), 0);+ assert!(error.to_string().contains("at most 20 threads"));+}+#[tokio::test]async fn authorized_keys_reach_the_email_domain() {let content = Arc::new(RecordingContentService::default());- let service =- DssEmailContentReader::new(content.clone(), Arc::new(TestAccessService { allow: true }));+ let reader = EmailServiceEmailContentReader::new(+ content.clone(),+ Arc::new(TestAccessService { allow: true }),+ );let user_id = MacroUserIdStr::try_from_email("[email protected]").unwrap();let requested = key(1);- let loaded = service- .get_email_content(&user_id, vec![requested.clone()])- .await;+ let loaded = reader.get_email_content(&user_id, vec![requested]).await;assert_eq!(content.calls.load(Ordering::SeqCst), 1);assert!(matches!(@@ -176,16 +222,14 @@ async fn authorized_keys_reach_the_email_domain() {#[tokio::test]async fn unauthorized_keys_do_not_reach_the_email_domain() {let content = Arc::new(RecordingContentService::default());- let service = DssEmailContentReader::new(+ let reader = EmailServiceEmailContentReader::new(content.clone(),Arc::new(TestAccessService { allow: false }),);let user_id = MacroUserIdStr::try_from_email("[email protected]").unwrap();let requested = key(1);- let loaded = service- .get_email_content(&user_id, vec![requested.clone()])- .await;+ let loaded = reader.get_email_content(&user_id, vec![requested]).await;assert_eq!(content.calls.load(Ordering::SeqCst), 0);assert!(matches!(diff --git a/crates/graphql_email/src/objects.rs b/crates/graphql_email/src/objects.rsindex 3cca56a02c..2e61a39b2d 100644--- a/crates/graphql_email/src/objects.rs+++ b/crates/graphql_email/src/objects.rs@@ -225,7 +225,7 @@ mod tests {load_latest_email_message::<ContentReader>(ctx,EmailContentKey {- thread_id: Uuid::from_u128(2).to_string(),+ thread_id: Uuid::from_u128(2),},).await@@ -242,8 +242,10 @@ mod tests {) -> HashMap<EmailContentKey, EmailContentLoad> {keys.into_iter().map(|key| {- let thread_id = Uuid::parse_str(&key.thread_id).unwrap();- (key, EmailContentLoad::Found(Box::new(message(thread_id))))+ (+ key,+ EmailContentLoad::Found(Box::new(message(key.thread_id))),+ )}).collect()}diff --git a/crates/graphql_soup/src/objects.rs b/crates/graphql_soup/src/objects.rsindex 12b50d6d2a..59082b597a 100644--- a/crates/graphql_soup/src/objects.rs+++ b/crates/graphql_soup/src/objects.rs@@ -17,20 +17,22 @@ use models_soup::{};use serde_json::Value;use soup::domain::models::EnrichedSoupItem;+use uuid::Uuid;/// Extension fields attached to every top-level Soup entity.////// The concrete edge object is supplied by the schema composition crate and/// flattened into each Soup entity's GraphQL fields.pub trait SoupEntityEdges: ObjectType + Clone + Send + Sync + 'static {+ /// Construct the common/global edge object for a Soup entity.+ /// This is for edges that apply to all soup entities, e.g. notifications+ fn from_entity(entity: model_entity::Entity<'static>) -> Self;+/// Additional fields attached only to email-thread entities.type EmailThreadEdges: ObjectType + Clone + Send + Sync + 'static;- /// Construct the edge object for a Soup entity.- fn from_entity(entity: model_entity::Entity<'static>) -> Self;-/// Construct the email-thread-specific edge object.- fn email_thread_edges(&self) -> Self::EmailThreadEdges;+ fn email_thread_edges(email_thread_id: Uuid) -> Self::EmailThreadEdges;}/// Page returned by `Query.soup`.@@ -845,7 +847,7 @@ where/// the email thread edge#[graphql(flatten)]async fn email_thread_edges(&self) -> E::EmailThreadEdges {- self.1.email_thread_edges()+ E::email_thread_edges(self.0.thread.id)}}diff --git a/services/document_storage_service/src/api/context.rs b/services/document_storage_service/src/api/context.rsindex 047b06e934..a8828e4ecb 100644--- a/services/document_storage_service/src/api/context.rs+++ b/services/document_storage_service/src/api/context.rs@@ -147,7 +147,7 @@ pub(crate) type DssGraphqlSoupSchema = complete_graph::SharedSoupSchema<complete_graph::PropertiesEntityPropertyWriter<PropertiesService, EntityAccessService>,Arc<ai_tools::ToolNotificationService>,complete_graph::PropertiesEntityPropertyReader<PropertiesService, EntityAccessService>,- crate::api::graphql_soup::DssEmailContentReader<DssEmailService, EntityAccessService>,+ complete_graph::EmailServiceEmailContentReader<DssEmailService, EntityAccessService>,>;type SystemPropertiesService = SystemPropertiesServiceImpl<PgSystemPropertiesRepository>;diff --git a/services/document_storage_service/src/api/graphql_soup.rs b/services/document_storage_service/src/api/graphql_soup.rsindex fa77f7719d..2986ffe1db 100644--- a/services/document_storage_service/src/api/graphql_soup.rs+++ b/services/document_storage_service/src/api/graphql_soup.rs@@ -11,10 +11,6 @@ use axum_extra::extract::Cached;use complete_graph::GraphqlSoupRequestParts;use model_user::axum_extractor::OptionalMacroUserExtractor;-mod email_content;--pub(crate) use email_content::DssEmailContentReader;-pub(crate) fn router() -> Router<ApiContext> {Router::new().route("/soup/graphql", get(graphiql).post(handler))}@@ -67,7 +63,7 @@ async fn handler(State(state): State<ApiContext>, req: Request) -> Response {state.entity_access_service.clone(),macro_user_id.clone(),);- let email_content_reader = DssEmailContentReader::new(+ let email_content_reader = complete_graph::EmailServiceEmailContentReader::new(state.soup_router_state.email_service(),state.entity_access_service.clone(),);diff --git a/services/document_storage_service/src/api/graphql_soup/email_content.rs b/services/document_storage_service/src/api/graphql_soup/email_content.rsdeleted file mode 100644index b4c00c5a30..0000000000--- a/services/document_storage_service/src/api/graphql_soup/email_content.rs+++ /dev/null@@ -1,114 +0,0 @@-#[cfg(test)]-mod test;--use std::{collections::HashMap, sync::Arc};--use complete_graph::{EmailContentKey, EmailContentLoad, SoupEmailContentEdgeReader};-use email::domain::ports::EmailContentService;-use entity_access::domain::{models::AccessError, ports::EntityAccessService};-use macro_user_id::user_id::MacroUserIdStr;-use uuid::Uuid;--/// Document-storage adapter for the Soup email-content edge.-///-/// The adapter batches authorization through the entity-access domain, then-/// passes only typed view receipts to the email domain.-pub(crate) struct DssEmailContentReader<S, EAS> {- email_service: Arc<S>,- entity_access_service: Arc<EAS>,-}--impl<S, EAS> DssEmailContentReader<S, EAS> {- pub(crate) fn new(email_service: Arc<S>, entity_access_service: Arc<EAS>) -> Self {- Self {- email_service,- entity_access_service,- }- }-}--impl<S, EAS> SoupEmailContentEdgeReader for DssEmailContentReader<S, EAS>-where- S: EmailContentService,- EAS: EntityAccessService,-{- async fn get_email_content(- &self,- user_id: &MacroUserIdStr<'static>,- keys: Vec<EmailContentKey>,- ) -> HashMap<EmailContentKey, EmailContentLoad> {- let thread_ids = keys- .iter()- .map(|key| key.thread_id.clone())- .collect::<Vec<_>>();- let mut receipts = self- .entity_access_service- .generate_email_thread_view_access_receipts(user_id, None, &thread_ids)- .await;- let access_results = keys.into_iter().map(|key| {- let result = receipts- .remove(&key.thread_id)- .unwrap_or(Err(AccessError::Internal));- (key, result)- });-- let mut loads = HashMap::with_capacity(access_results.len());- let mut authorized = Vec::new();- let mut keys_by_thread_id = HashMap::new();-- for (key, access_result) in access_results {- match access_result {- Ok(receipt) => match Uuid::parse_str(&key.thread_id) {- Ok(thread_id) => {- keys_by_thread_id.insert(thread_id, key);- authorized.push(receipt);- }- Err(error) => {- tracing::error!(thread_id = %key.thread_id, ?error, "invalid authorized email thread ID");- loads.insert(key, EmailContentLoad::Failed);- }- },- Err(- AccessError::Unauthorized- | AccessError::UnauthorizedWithMessage(_)- | AccessError::NotFound(_),- ) => {- loads.insert(key, EmailContentLoad::Missing);- }- Err(error) => {- tracing::error!(thread_id = %key.thread_id, ?error, "email content access check failed");- loads.insert(key, EmailContentLoad::Failed);- }- }- }-- if authorized.is_empty() {- return loads;- }-- match self- .email_service- .get_latest_messages_parsed(authorized)- .await- {- Ok(mut messages) => {- for (thread_id, key) in keys_by_thread_id {- let load = messages- .remove(&thread_id)- .map_or(EmailContentLoad::Missing, |message| {- EmailContentLoad::Found(Box::new(message))- });- loads.insert(key, load);- }- }- Err(error) => {- tracing::error!(?error, "bulk email content load failed");- for key in keys_by_thread_id.into_values() {- loads.insert(key, EmailContentLoad::Failed);- }- }- }-- loads- }-}