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
perf(agents): reuse native tool definitions across turns (#7581)
Wolf Mermelstein37ad593959be
diff --git a/crates/agent_inmem/src/outbound/rig_engine/test.rs b/crates/agent_inmem/src/outbound/rig_engine/test.rsindex 038a19c748..7d65fe1634 100644--- a/crates/agent_inmem/src/outbound/rig_engine/test.rs+++ b/crates/agent_inmem/src/outbound/rig_engine/test.rs@@ -1,24 +1,129 @@use super::*;use crate::domain::engine::AgentIdentity;+use agent_session::domain::model::AgentSessionId;+use ai_toolset::{+ AsyncTool, RequestContext, ServiceContext, ToolAnnotated, ToolAnnotations, ToolResult,+};+use async_trait::async_trait;+use schemars::JsonSchema;+use serde::Deserialize;+use std::sync::Mutex;/// A stand-in for the toolset prompt, short enough to assert on positionally.const TOOLS: &str = "TOOLS";-fn has_ask_user(supports_user_input: bool) -> bool {- let tools = tools_for(AiHost::AgentSession);- let base_tools = Arc::into_inner(tools.toolset)- .expect("tools_for should return a fresh, uniquely owned collection");- tools_for_turn(base_tools, supports_user_input)- .request_schemas()- .expect("tool schemas should be valid")- .iter()- .any(|schema| schema.name == "AskUser")+#[test]+fn cached_definitions_keep_client_capabilities_and_reviewable_tools() {+ let tools = NativeTools::new();+ let supported = tools.for_turn(true);+ let unsupported = tools.for_turn(false);++ assert!(supported.tools.contains_key("AskUser"));+ assert!(!unsupported.tools.contains_key("AskUser"));+ assert!(Arc::ptr_eq(&supported, &tools.for_turn(true)));+ assert!(Arc::ptr_eq(&unsupported, &tools.for_turn(false)));+ assert_eq!(+ supported+ .tools+ .keys()+ .filter(|name| name.as_str() != "AskUser")+ .collect::<Vec<_>>(),+ unsupported.tools.keys().collect::<Vec<_>>()+ );+ for tools in [supported, unsupported] {+ for name in ["SendEmail", "CreateCalendarEvent"] {+ assert!(tools.user_tools.contains_key(name));+ }+ }}-#[test]-fn ask_user_is_only_advertised_when_the_client_supports_it() {- assert!(has_ask_user(true));- assert!(!has_ask_user(false));+#[derive(Deserialize, JsonSchema)]+#[schemars(title = "ReadTurnContext", description = "Read this call's context.")]+struct ReadTurnContext {}++impl ToolAnnotated for ReadTurnContext {+ const ANNOTATIONS: ToolAnnotations = ToolAnnotations::read_only("Read context");+}++#[async_trait]+impl AsyncTool<String> for ReadTurnContext {+ type Output = serde_json::Value;++ async fn call(+ &self,+ context: ServiceContext<String>,+ request: RequestContext,+ ) -> ToolResult<Self::Output> {+ Ok(serde_json::json!({+ "context": context.0,+ "user": request.user_id.to_string(),+ }))+ }+}++struct RecordingGate {+ refused: AgentSessionId,+ calls: Mutex<Vec<AgentSessionId>>,+}++impl NativeToolGate for RecordingGate {+ fn check<'a>(+ &'a self,+ session: AgentSessionId,+ _tool: &'a str,+ _arguments: &'a serde_json::Value,+ ) -> std::pin::Pin<Box<dyn Future<Output = NativeToolVerdict> + Send + 'a>> {+ Box::pin(async move {+ self.calls.lock().unwrap().push(session);+ if session == self.refused {+ NativeToolVerdict::Refuse("not approved".to_owned())+ } else {+ NativeToolVerdict::Run+ }+ })+ }+}++#[tokio::test]+async fn shared_definitions_keep_the_callers_context_and_check_each_sessions_gate() {+ let definitions: Arc<dyn AiToolSet<String> + Send + Sync> =+ Arc::new(AsyncToolCollection::<String>::new().add_tool::<ReadTurnContext, String>());+ let allowed = AgentSessionId::new();+ let refused = AgentSessionId::new();+ let gate = Arc::new(RecordingGate {+ refused,+ calls: Mutex::new(Vec::new()),+ });+ for (session, user, label) in [+ (allowed, "macro|[email protected]", "first turn"),+ (refused, "macro|[email protected]", "refused turn"),+ (allowed, "macro|[email protected]", "third turn"),+ ] {+ let tools = GatedToolSet {+ tools: Arc::clone(&definitions),+ gate: gate.clone(),+ session,+ awaiting: Arc::default(),+ };+ let result = tools+ .dispatch_tool_call(+ label.to_owned(),+ RequestContext::new(MacroUserIdStr::try_from(user.to_owned()).unwrap()),+ "ReadTurnContext",+ &serde_json::json!({}),+ )+ .await+ .unwrap();+ if session == refused {+ assert_eq!(result.unwrap_err().description, "not approved");+ } else {+ assert_eq!(+ result.unwrap(),+ serde_json::json!({"context": label, "user": user})+ );+ }+ }+ assert_eq!(*gate.calls.lock().unwrap(), vec![allowed, refused, allowed]);}#[test]diff --git a/crates/agent_inmem/src/rig_engine.rs b/crates/agent_inmem/src/rig_engine.rsindex b8a115ee67..1811b2d97c 100644--- a/crates/agent_inmem/src/rig_engine.rs+++ b/crates/agent_inmem/src/rig_engine.rs@@ -25,7 +25,7 @@ use std::sync::Arc;use agent::{AgentError, AgentLoop, StreamPart};use ai_tools::user_tool_review::user_tool_finisher;-use ai_tools::{AiHost, DeferredToolSet, ToolServiceContext, ToolSetWithPrompt, tools_for};+use ai_tools::{AiHost, DeferredToolSet, ToolServiceContext, tools_for};use ai_toolset::{AsyncToolCollection, ToolSet as AiToolSet};use axum::extract::FromRef;use futures::StreamExt as _;@@ -53,7 +53,8 @@ const PART_BUFFER: usize = 256;/// [`TurnEngine`] backed by [`agent::AgentLoop`] and/// [`ai_tools::tools_for`].pub struct RigTurnEngine {- db: PgPool,+ memory: Arc<MemoryServiceImpl<PgMemoryRepo>>,+ native_tools: Arc<NativeTools>,tool_context: ToolServiceContext,gate: Arc<dyn NativeToolGate>,}@@ -65,7 +66,12 @@ impl RigTurnEngine {#[must_use]pub fn new(db: PgPool, tool_context: ToolServiceContext) -> Self {Self {- db,+ memory: Arc::new(MemoryServiceImpl::new(+ PgMemoryRepo::new(db),+ tool_context.clone(),+ tools_for(AiHost::Chat),+ )),+ native_tools: Arc::new(NativeTools::new()),tool_context,gate: Arc::new(UngatedNativeTools),}@@ -171,6 +177,40 @@ fn tools_for_turn(}}+/// Native definitions contain schemas and deserializers, not a user's context.+/// Build both client-capability variants once, before the first prompt. Each+/// turn still supplies its own identity, reviewer, gate, MCP tools and memory.+struct NativeTools {+ with_user_input: Arc<AsyncToolCollection<InMemToolContext>>,+ without_user_input: Arc<AsyncToolCollection<InMemToolContext>>,+ prompt: String,+ deferred: Arc<[ai_toolset::SearchableTool]>,+}++impl NativeTools {+ fn new() -> Self {+ let tools = tools_for(AiHost::AgentSession);+ let base_tools = Arc::into_inner(tools.toolset)+ .expect("tools_for should return a fresh, uniquely owned collection");+ let other_tools = Arc::into_inner(tools_for(AiHost::AgentSession).toolset)+ .expect("tools_for should return a fresh, uniquely owned collection");+ Self {+ with_user_input: Arc::new(tools_for_turn(base_tools, true)),+ without_user_input: Arc::new(tools_for_turn(other_tools, false)),+ prompt: tools.prompt.to_string(),+ deferred: tools.deferred,+ }+ }++ fn for_turn(&self, supports_user_input: bool) -> Arc<AsyncToolCollection<InMemToolContext>> {+ Arc::clone(if supports_user_input {+ &self.with_user_input+ } else {+ &self.without_user_input+ })+ }+}+impl TurnEngine for RigTurnEngine {fn supported_models(&self) -> &[&str] {crate::domain::models::advertised_models()@@ -178,13 +218,16 @@ impl TurnEngine for RigTurnEngine {fn run_turn(&self, request: TurnRequest) -> mpsc::Receiver<Result<StreamPart, AgentError>> {let (parts, receiver) = mpsc::channel(PART_BUFFER);- let db = self.db.clone();+ let memory = Arc::clone(&self.memory);+ let native_tools = Arc::clone(&self.native_tools);let tool_context = self.tool_context.clone();let gate = Arc::clone(&self.gate);let metering = agent::MeteringContext::current();tokio::spawn(agent::MeteringContext::carry(metering, async move {- if let Err(error) = drive_turn(db, tool_context, gate, request, &parts).await {+ if let Err(error) =+ drive_turn(&memory, &native_tools, tool_context, gate, request, &parts).await+ {let _ = parts.send(Err(error)).await;}})@@ -195,7 +238,8 @@ impl TurnEngine for RigTurnEngine {}async fn drive_turn(- db: PgPool,+ memory: &MemoryServiceImpl<PgMemoryRepo>,+ native_tools: &NativeTools,base_context: ToolServiceContext,gate: Arc<dyn NativeToolGate>,request: TurnRequest,@@ -234,21 +278,15 @@ async fn drive_turn(// Chat's tools with the session's prompt: the user tools (`SendEmail`,// `CreateCalendarEvent`) defer to the user, and this runtime finishes// them in the turn through `reviewer`.- let tools = tools_for(AiHost::AgentSession);- let user_memory = fetch_user_memory(&db, &base_context, &owner).await;+ let user_memory = fetch_user_memory(memory, &owner).await;let system_prompt = system_prompt(- &tools.prompt,+ &native_tools.prompt,identity.as_ref(),instructions.as_deref(),user_memory.as_deref(),);- // `tools_for` returns a fresh Arc. Take its collection back so the- // in-memory runtime can widen it onto the session-specific context and- // add the one tool that needs the active ACP connection.- let base_tools = Arc::into_inner(tools.toolset)- .expect("tools_for should return a fresh, uniquely owned collection");- let toolset = Arc::new(tools_for_turn(base_tools, user_input.is_some()));+ let toolset = native_tools.for_turn(user_input.is_some());let usage_ctx = ai_usage::UsageContext::new(ai_usage::AiFeature::AgentSession, owner.clone());// Carry the feature on the context so tool-spawned subagents attribute to it.let mut tool_context = base_context.clone();@@ -295,8 +333,10 @@ async fn drive_turn(None => toolset,};// Most of Macro's tools go out by name only; the model loads the rest.- let toolset: Arc<dyn AiToolSet<_> + Send + Sync> =- Arc::new(DeferredToolSet::new(toolset, tools.deferred));+ let toolset: Arc<dyn AiToolSet<_> + Send + Sync> = Arc::new(DeferredToolSet::new(+ toolset,+ Arc::clone(&native_tools.deferred),+ ));let toolset: Arc<dyn AiToolSet<_> + Send + Sync> = Arc::new(GatedToolSet {tools: toolset,gate,@@ -383,18 +423,9 @@ fn system_prompt(/// The owner's memory block, or `None` when it is missing or failed to load.async fn fetch_user_memory(- db: &PgPool,- tool_context: &ToolServiceContext,+ memory_service: &MemoryServiceImpl<PgMemoryRepo>,owner: &MacroUserIdStr<'static>,) -> Option<String> {- let tools = tools_for(AiHost::Chat);- let tools = ToolSetWithPrompt {- toolset: tools.toolset,- prompt: tools.prompt,- deferred: tools.deferred,- };- let memory_service =- MemoryServiceImpl::new(PgMemoryRepo::new(db.clone()), tool_context.clone(), tools);match memory_service.get_or_generate_memory(owner.clone()).await {Ok(memory) => memory.map(|memory| memory.to_string()),Err(error) => {