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(graphql): read agent session rows with the viewer's team (#9211)
Wolf Mermelsteind439c38a9844
diff --git a/crates/complete_graph/src/schema/test.rs b/crates/complete_graph/src/schema/test.rsindex 1736ccf433..03f1078ce0 100644--- a/crates/complete_graph/src/schema/test.rs+++ b/crates/complete_graph/src/schema/test.rs@@ -1168,6 +1168,18 @@ impl EntityAccessService for CountingEntityAccessService {| (EntityType::Form, form_activity::RESPONDED_FORM_ID) => Some(AccessLevel::View),(EntityType::Form, form_activity::EDITABLE_FORM_ID) => Some(AccessLevel::Edit),(EntityType::Database | EntityType::Form, _) => None,+ (EntityType::Team, _) => {+ return EntityAccessReceipt::try_new_authenticated_user(+ MacroUserIdStr::parse_from_str(VALID_USER_ID).unwrap(),+ entity_access::domain::models::Entity {+ entity_id: entity_id.to_owned(),+ entity_type,+ },+ EntityPermission::TeamRole {+ role: TeamRole::Member,+ },+ );+ }(EntityType::EmailThread, _) => {let thread_id = Uuid::parse_str(entity_id).unwrap();self.email_thread_checks.lock().unwrap().push(thread_id);@@ -1557,19 +1569,15 @@ impl TestHarness {self.thread_reader.clone(),),))- .data(graphql_soup::AgentSessionEntityLoader(- graphql_soup::soup_item_loader(- self.primary_soup_service.clone(),- Arc::new(self.email_service.clone()),- self.state.entity_access.as_ref().clone(),- ),+ .data(graphql_soup::AgentSessionEntityLoader::new(+ self.primary_soup_service.clone(),+ Arc::new(self.email_service.clone()),+ self.state.entity_access.as_ref().clone(),))- .data(graphql_soup::PrimarySoupItemLoader(- graphql_soup::soup_item_loader(- self.primary_soup_service.clone(),- Arc::new(self.email_service.clone()),- self.state.entity_access.as_ref().clone(),- ),+ .data(graphql_soup::PrimarySoupItemLoader::new(+ self.primary_soup_service.clone(),+ Arc::new(self.email_service.clone()),+ self.state.entity_access.as_ref().clone(),)).data(graphql_properties::entity_properties_loader(user_id.clone(),@@ -1613,11 +1621,11 @@ async fn soup_updates_subscribes_as_the_authenticated_user() {let document_id = Uuid::from_u128(42);let deleted_document_id = Uuid::from_u128(43);soup_service.set_raw_response(vec![soup_document(document_id)]);- let loader = graphql_soup::PrimarySoupItemLoader(graphql_soup::soup_item_loader(+ let loader = graphql_soup::PrimarySoupItemLoader::new(soup_service.clone(),Arc::new(NoOpEmailService),NoOpEntityAccessService,- ));+ );let schema: SoupSchema<CountingSoupService,TestRealtimeSubscriptionService,@@ -1716,6 +1724,32 @@ async fn agent_session_is_read_from_the_primary_not_the_list_replica() {assert_eq!(harness.raw_soup_calls.load(Ordering::SeqCst), 0);}+#[tokio::test]+async fn agent_session_read_carries_the_viewer_team_for_its_linked_pr() {+ let harness = harness();+ let session_id = Uuid::from_u128(7);+ harness+ .primary_soup_service+ .set_raw_response(vec![soup_agent_session(session_id)]);++ let response = harness+ .execute(&format!(+ r#"{{ user {{ agentSession(sessionId: "{session_id}") {{ id }} }} }}"#+ ))+ .await;++ assert!(response.errors.is_empty(), "{:?}", response.errors);+ // Linked PRs are stored for the team; without its receipt the row's PR+ // state reads as unknown and overwrites the list's known state.+ assert_eq!(+ harness+ .primary_soup_service+ .raw_team_receipts+ .load(Ordering::SeqCst),+ 1+ );+}+#[tokio::test]async fn soup_entity_is_read_from_the_primary_not_the_list_replica() {let harness = harness();@@ -1798,11 +1832,11 @@ async fn agent_session_log_appended_streams_runs_for_an_accessible_session() {});let soup_service = CountingSoupService::default();soup_service.set_raw_response(vec![soup_agent_session(session_id)]);- let loader = graphql_soup::AgentSessionEntityLoader(graphql_soup::soup_item_loader(+ let loader = graphql_soup::AgentSessionEntityLoader::new(soup_service.clone(),Arc::new(NoOpEmailService),NoOpEntityAccessService,- ));+ );let schema = build_schema_with_service::<CountingSoupService,NoOpEmailService,diff --git a/crates/complete_graph/src/schema/test/soup_patches.rs b/crates/complete_graph/src/schema/test/soup_patches.rsindex 9c983844cd..75bec8a366 100644--- a/crates/complete_graph/src/schema/test/soup_patches.rs+++ b/crates/complete_graph/src/schema/test/soup_patches.rs@@ -71,11 +71,11 @@ async fn subscription_responses_from(Arc::new(NoOpEmailService),NoOpEntityAccessService,);- let hydration_loader = graphql_soup::PrimarySoupItemLoader(graphql_soup::soup_item_loader(+ let hydration_loader = graphql_soup::PrimarySoupItemLoader::new(primary,Arc::new(NoOpEmailService),NoOpEntityAccessService,- ));+ );let schema: RealtimeSchema = build_schema_with_services(replica,TestRealtimeSubscriptionService {diff --git a/crates/graphql_soup/AGENTS.md b/crates/graphql_soup/AGENTS.mdindex e40901151e..28c1ef630e 100644--- a/crates/graphql_soup/AGENTS.md+++ b/crates/graphql_soup/AGENTS.md@@ -18,6 +18,10 @@ viewer pattern), which apply to every type defined here.serves lists. A thread shared through a team's CRM, a project, or a sharetherefore opens through `emailThread` even though it lives in a teammate'sinbox.+- Build `AgentSessionEntityLoader` and `PrimarySoupItemLoader` with `::new`,+ which rechecks the viewer's team. PRs are stored for the team; without it,+ a session row reads `pullRequestState: null` and overwrites the list's+ state in the client cache.- Property grouping accepts optional `groupBy.bucketing` with `type: DATE_RANGES`,caller-defined half-open UTC `ranges`, and `missingValueBucketKey`; client querykeys restart pagination when ranges change.diff --git a/crates/graphql_soup/src/loaders.rs b/crates/graphql_soup/src/loaders.rsindex e9a8f2bf28..0301790236 100644--- a/crates/graphql_soup/src/loaders.rs+++ b/crates/graphql_soup/src/loaders.rs@@ -461,7 +461,25 @@ impl SoupItemDataLoader {/// and querying second; both need every committed row, which a replica/// lagging by seconds does not have. Lists keep the replica-backed reader.#[derive(Clone)]-pub struct AgentSessionEntityLoader(pub SoupItemDataLoader);+pub struct AgentSessionEntityLoader(pub(crate) SoupItemDataLoader);++impl AgentSessionEntityLoader {+ /// Build the reader over a primary-backed Soup service. It always rechecks+ /// the viewer's team: linked pull requests are stored for the team, so a+ /// read without it reports every session's PR state as unknown.+ pub fn new<S, E, A>(soup_service: S, email_service: Arc<E>, access: A) -> Self+ where+ S: SoupService,+ E: EmailThreadPreviewService,+ A: EntityAccessService,+ {+ Self(soup_item_loader_with_team_access(+ soup_service,+ email_service,+ access,+ ))+ }+}/// Primary-backed Soup reader for reads that must see a write committed/// moments earlier, before a replica has replayed it: `soupUpdates`@@ -469,7 +487,24 @@ pub struct AgentSessionEntityLoader(pub SoupItemDataLoader);/// caches it as current, and `soupEntity`, which opens an entity right after/// it is created or edited. Lists keep the replica-backed reader.#[derive(Clone)]-pub struct PrimarySoupItemLoader(pub SoupItemDataLoader);+pub struct PrimarySoupItemLoader(pub(crate) SoupItemDataLoader);++impl PrimarySoupItemLoader {+ /// Build the reader over a primary-backed Soup service, rechecking the+ /// viewer's team so team-stored PRs and CRM records hydrate.+ pub fn new<S, E, A>(soup_service: S, email_service: Arc<E>, access: A) -> Self+ where+ S: SoupService,+ E: EmailThreadPreviewService,+ A: EntityAccessService,+ {+ Self(soup_item_loader_with_team_access(+ soup_service,+ email_service,+ access,+ ))+ }+}/// Build the Soup DataLoader from the Soup service and the access-checked/// email thread reader.diff --git a/docs/guides/add-graphql-field.md b/docs/guides/add-graphql-field.mdindex 907e43bed1..f54df822ce 100644--- a/docs/guides/add-graphql-field.md+++ b/docs/guides/add-graphql-field.md@@ -117,15 +117,20 @@ Nothing checks this at compile time. A missing insert surfaces only when a clienselects the field, as a "data does not exist" error. The `complete_graph` testsinsert their own data, so they don't catch it.-**Replica or primary.** Ordinary queries, lists, and subscription hydration read-the replica (`readonly_db`). In production it lags 1–9 s. Mutation replies, and-anything read right after a write (a just-created entity, a subscribe-then-query-log), need a primary-backed loader. Copy `AgentSessionEntityLoader` in-`crates/graphql_soup/src/loaders.rs` or `email_mutation_thread_loader` in-`crates/graphql_soup/src/mutation_thread.rs`. A new agent session read from the-replica came back `null` and the client reported "not accessible". Local stacks-can't reproduce this because their replica URL is the primary, so decide-deliberately.+**Replica or primary.** Ordinary queries and lists read the replica+(`readonly_db`). In production it lags 1–9 s. Mutation replies, `soupUpdates`+hydration, and anything read right after a write (a just-created entity, a+subscribe-then-query log), need a primary-backed loader. Copy+`AgentSessionEntityLoader` in `crates/graphql_soup/src/loaders.rs` or+`email_mutation_thread_loader` in `crates/graphql_soup/src/mutation_thread.rs`.+A new agent session read from the replica came back `null` and the client+reported "not accessible". Local stacks can't reproduce this because their+replica URL is the primary, so decide deliberately.++**Team access.** Build Soup entity loaders with their `::new` constructors or+`soup_item_loader_with_team_access`. GitHub pull requests and CRM records are+stored for the team, so a loader without the viewer's team returns sessions+whose PR state is unknown. The client then caches that state over the list's.## 5. Regenerate the SDL (required)diff --git a/services/document_storage_service/src/server.rs b/services/document_storage_service/src/server.rsindex 19cec21fd8..2bbf0b9553 100644--- a/services/document_storage_service/src/server.rs+++ b/services/document_storage_service/src/server.rs@@ -2199,12 +2199,10 @@ pub async fn run() -> anyhow::Result<()> {entity_access_service.as_ref().clone(),),),- graphql_agent_session_entity_loader: graphql_soup::AgentSessionEntityLoader(- graphql_soup::soup_item_loader(- primary_entity_soup.clone(),- Arc::new(email_service.clone()),- entity_access_service.as_ref().clone(),- ),+ graphql_agent_session_entity_loader: graphql_soup::AgentSessionEntityLoader::new(+ primary_entity_soup.clone(),+ Arc::new(email_service.clone()),+ entity_access_service.as_ref().clone(),),graphql_database_rows_context: graphql_databases::DatabaseRowsGraphqlContext::new(databases_sql::view_rows::DatabaseViewRows::new(@@ -2218,12 +2216,10 @@ pub async fn run() -> anyhow::Result<()> {entity_access_service.as_ref().clone(),),),- graphql_primary_soup_item_loader: graphql_soup::PrimarySoupItemLoader(- graphql_soup::soup_item_loader_with_team_access(- primary_entity_soup,- Arc::new(email_service.clone()),- entity_access_service.as_ref().clone(),- ),+ graphql_primary_soup_item_loader: graphql_soup::PrimarySoupItemLoader::new(+ primary_entity_soup,+ Arc::new(email_service.clone()),+ entity_access_service.as_ref().clone(),),// Ordinary single-entity GraphQL reads use the replica-backed Soup// and email readers. Subscription hydration, `soupEntity`, and