Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(agent-session): read a just-created session from the primary (#7692)

Wolf Mermelstein6d3688ca7692
diff --git a/crates/complete_graph/src/schema/test.rs b/crates/complete_graph/src/schema/test.rs
index 6de9fc0895..24aaaf7764 100644
--- a/crates/complete_graph/src/schema/test.rs
+++ b/crates/complete_graph/src/schema/test.rs
@@ -1129,6 +1129,8 @@ struct TestHarness {
>,
state: TestState,
soup_service: CountingSoupService,
+ /// Answers single agent-session lookups, as the primary does in DSS.
+ primary_soup_service: CountingSoupService,
email_service: CountingEmailService,
email_content_reader: RecordingEmailContentReader,
activity_reader: RecordingActivityReader,
@@ -1171,6 +1173,7 @@ fn harness() -> TestHarness {
entity_access: Arc::new(entity_access),
},
soup_service: soup,
+ primary_soup_service: CountingSoupService::default(),
email_service: email,
email_content_reader,
activity_reader,
@@ -1242,6 +1245,12 @@ impl TestHarness {
self.soup_service.clone(),
Arc::new(self.email_service.clone()),
))
+ .data(graphql_soup::AgentSessionEntityLoader(
+ graphql_soup::soup_item_loader(
+ self.primary_soup_service.clone(),
+ Arc::new(self.email_service.clone()),
+ ),
+ ))
.data(graphql_properties::entity_properties_loader(
user_id.clone(),
NoOpEntityPropertyReader,
@@ -1358,6 +1367,28 @@ async fn soup_updates_subscribes_as_the_authenticated_user() {
);
}
+#[tokio::test]
+async fn agent_session_is_read_from_the_primary_not_the_list_replica() {
+ let harness = harness();
+ let session_id = Uuid::from_u128(7);
+ // Just created: the replica behind Soup lists has not seen the session yet.
+ harness.soup_service.set_raw_response(Vec::new());
+ 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);
+ let data = response.data.into_json().unwrap();
+ assert_eq!(data["user"]["agentSession"]["id"], session_id.to_string());
+ assert_eq!(harness.raw_soup_calls.load(Ordering::SeqCst), 0);
+}
+
#[tokio::test]
async fn agent_session_log_appended_streams_runs_for_an_accessible_session() {
use agent_runtime_protocol::domain::schema::v0::{SystemEvent, ToServerMessage};
@@ -1397,7 +1428,10 @@ 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::soup_item_loader(soup_service.clone(), Arc::new(NoOpEmailService));
+ let loader = graphql_soup::AgentSessionEntityLoader(graphql_soup::soup_item_loader(
+ soup_service.clone(),
+ Arc::new(NoOpEmailService),
+ ));
let schema = build_schema_with_service::<
CountingSoupService,
NoOpEmailService,
diff --git a/crates/graphql_soup/src/lib.rs b/crates/graphql_soup/src/lib.rs
index 79c2077502..872c874675 100644
--- a/crates/graphql_soup/src/lib.rs
+++ b/crates/graphql_soup/src/lib.rs
@@ -20,8 +20,8 @@ mod resolvers;
pub use graphql_common::{GraphqlRequestParts, GraphqlSoupEntityType};
pub use inputs::{GraphqlSimpleSortMethod, GroupedSoupInput, SoupInput};
pub use loaders::{
- EmailServiceInboxReader, SoupInboxReader, SoupItemDataLoader, SoupItemLoader,
- SoupItemLoaderError, SoupItemLoaderKey, soup_item_loader,
+ AgentSessionEntityLoader, EmailServiceInboxReader, SoupInboxReader, SoupItemDataLoader,
+ SoupItemLoader, SoupItemLoaderError, SoupItemLoaderKey, soup_item_loader,
};
pub use mutation_thread::{
EmailMutationThreadLoader, EmailMutationThreadReader, email_mutation_thread_loader,
diff --git a/crates/graphql_soup/src/loaders.rs b/crates/graphql_soup/src/loaders.rs
index 9c19e78710..c61a054be9 100644
--- a/crates/graphql_soup/src/loaders.rs
+++ b/crates/graphql_soup/src/loaders.rs
@@ -310,6 +310,13 @@ impl SoupItemDataLoader {
}
}
+/// Primary-backed Soup reader for single agent-session lookups. A session is
+/// opened the moment it is created, and its log is read by subscribing first
+/// 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);
+
/// Build the realtime Soup DataLoader from the existing Soup and email services.
pub fn soup_item_loader<S, E>(soup_service: S, email_service: Arc<E>) -> SoupItemDataLoader
where
diff --git a/crates/graphql_soup/src/resolvers.rs b/crates/graphql_soup/src/resolvers.rs
index 8bff7c8d9f..797e2978b2 100644
--- a/crates/graphql_soup/src/resolvers.rs
+++ b/crates/graphql_soup/src/resolvers.rs
@@ -23,7 +23,7 @@ use soup_realtime::domain::{models::Patch, ports::SoupRealtimeSubscriptionServic
use crate::{
inputs::{GroupedSoupInput, SoupInput},
- loaders::SoupItemDataLoader,
+ loaders::{AgentSessionEntityLoader, SoupItemDataLoader},
objects::{
GraphqlSoupAgentSession, GraphqlSoupEmailThread, GraphqlSoupEntity, GroupedSoup,
SoupEntityEdges, SoupPage, SoupPatch,
@@ -125,9 +125,10 @@ where
}
}
-/// Fetch one agent session the viewer can see, through the same access-
-/// filtered loader the Soup list uses, so a session outside their grants
-/// reads as absent rather than as an error.
+/// Fetch one agent session the viewer can see, through the same access
+/// filter the Soup list uses, so a session outside their grants reads as
+/// absent rather than as an error. Reads the primary: see
+/// [`AgentSessionEntityLoader`].
pub async fn resolve_soup_agent_session<Edges>(
ctx: &Context<'_>,
user_id: MacroUserIdStr<'static>,
@@ -136,7 +137,7 @@ pub async fn resolve_soup_agent_session<Edges>(
where
Edges: SoupEntityEdges,
{
- let loader = ctx.data::<SoupItemDataLoader>()?;
+ let loader = &ctx.data::<AgentSessionEntityLoader>()?.0;
let entity = EntityType::AgentSession.with_entity_string(session_id.to_string());
let Some(item) = loader.load_one((user_id, entity)).await? else {
return Ok(None);
diff --git a/services/document_storage_service/src/api/context.rs b/services/document_storage_service/src/api/context.rs
index 84c8e3a8e1..823be1eaa8 100644
--- a/services/document_storage_service/src/api/context.rs
+++ b/services/document_storage_service/src/api/context.rs
@@ -790,6 +790,7 @@ pub(crate) struct ApiContext {
pub graphql_initiative_context: graphql_initiative::InitiativeGraphqlContext,
pub graphql_scheduled_action_context: graphql_scheduled_action::ScheduledActionGraphqlContext,
pub graphql_initiative_entity_loader: graphql_initiative::InitiativeEntityLoader,
+ pub graphql_agent_session_entity_loader: graphql_soup::AgentSessionEntityLoader,
pub databases_state: DssDatabasesState,
pub forms_state: DssFormsState,
pub database_starter_state: DssDatabaseStarterState,
diff --git a/services/document_storage_service/src/api/graphql_soup.rs b/services/document_storage_service/src/api/graphql_soup.rs
index a62cb7934a..dc84f45364 100644
--- a/services/document_storage_service/src/api/graphql_soup.rs
+++ b/services/document_storage_service/src/api/graphql_soup.rs
@@ -172,6 +172,7 @@ fn insert_graphql_context_data(
data.insert(state.graphql_initiative_context.clone());
data.insert(state.graphql_scheduled_action_context.clone());
data.insert(state.graphql_initiative_entity_loader.clone());
+ data.insert(state.graphql_agent_session_entity_loader.clone());
data.insert(graphql_initiative::initiative_detail_loader(
state.graphql_initiative_context.clone(),
macro_user_id.clone(),
@@ -193,12 +194,12 @@ fn insert_graphql_context_data(
data.insert(complete_graph::agent_session_bot_loader(PgBotsRepo::new(
state.readonly_db.0.clone(),
)));
+ // Read right after the session is created and after its subscription
+ // starts, so the log must hold every committed row: the primary.
data.insert(complete_graph::agent_session_log_loader(
PgAgentSessionRepo::new(
- state.readonly_db.0.clone(),
- OwnedEntityRegistrar::new(OwnerGrantPolicy::new(PgBotsRepo::new(
- state.readonly_db.0.clone(),
- ))),
+ state.db.clone(),
+ OwnedEntityRegistrar::new(OwnerGrantPolicy::new(PgBotsRepo::new(state.db.clone()))),
),
));
data.insert(complete_graph::entity_properties_loader(
diff --git a/services/document_storage_service/src/server.rs b/services/document_storage_service/src/server.rs
index c24717d2ee..b96e7d6efd 100644
--- a/services/document_storage_service/src/server.rs
+++ b/services/document_storage_service/src/server.rs
@@ -1456,10 +1456,11 @@ pub async fn run() -> anyhow::Result<()> {
collab_surface_service.clone(),
));
- // Individual initiative reads preserve read-after-write consistency when a
- // newly created project opens immediately. Lists retain the replica reader.
- // Reuse Soup hydration so detail and mutation metadata include viewer history.
- let initiative_entity_soup = Arc::new(
+ // Individual initiative and agent-session reads preserve read-after-write
+ // consistency when a newly created entity opens immediately. Lists retain
+ // the replica reader. Reuse Soup hydration so detail and mutation metadata
+ // include viewer history.
+ let primary_entity_soup = Arc::new(
SoupImpl::new(
PgSoupRepo::new(readonly_pool::ReadOnlyPool(db.clone())),
frecency_service.clone(),
@@ -1871,7 +1872,13 @@ pub async fn run() -> anyhow::Result<()> {
authorization_state.clone(),
),
graphql_initiative_entity_loader: graphql_initiative::InitiativeEntityLoader(
- graphql_soup::soup_item_loader(initiative_entity_soup, Arc::new(email_service.clone())),
+ graphql_soup::soup_item_loader(
+ primary_entity_soup.clone(),
+ Arc::new(email_service.clone()),
+ ),
+ ),
+ graphql_agent_session_entity_loader: graphql_soup::AgentSessionEntityLoader(
+ graphql_soup::soup_item_loader(primary_entity_soup, Arc::new(email_service.clone())),
),
graphql_initiative_context: graphql_initiative::InitiativeGraphqlContext::new(
initiative_service.clone(),