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
feat(agent): in-memory ACP agent harness (#5834)
Eric Hayes0b76f375b6f4
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.jsonindex bd89dde815..8cc3dba909 100644--- a/.github/workspace-dep-closures.json+++ b/.github/workspace-dep-closures.json@@ -133,20 +133,26 @@"crates/agent","crates/agent_fold","crates/agent_harness",+ "crates/agent_inmem","crates/agent_runtime_protocol","crates/agent_session","crates/agent_trigger",+ "crates/ai_tools","crates/ai_toolset","crates/ai_usage",+ "crates/analytics_client",+ "crates/anthropic","crates/attachment","crates/authentication_service_client","crates/bot_id","crates/bot_token","crates/bots","crates/broadcast",+ "crates/calendar_events","crates/call","crates/channel_sender","crates/channels",+ "crates/chat","crates/connection","crates/connection_gateway_client","crates/connection_gateway_models",@@ -155,25 +161,34 @@"crates/crm","crates/database_env_vars","crates/document_sub_type",+ "crates/documents",+ "crates/dynamodb_client","crates/email",+ "crates/email_service_client","crates/email_utils",+ "crates/embedding","crates/entity_access","crates/entity_access_db_utils","crates/entity_access_management","crates/entity_mutation","crates/filter_ast",+ "crates/foreign_entity","crates/frecency",+ "crates/generic_email_domains",+ "crates/import","crates/invite_email","crates/item_filters","crates/kafka_util","crates/last_online_tracker","crates/lexical_client",+ "crates/lexical_mention_extractor","crates/macro_auth","crates/macro_authorization","crates/macro_aws_config","crates/macro_config","crates/macro_config_derive","crates/macro_cors",+ "crates/macro_db_client","crates/macro_entrypoint","crates/macro_env","crates/macro_env_var",@@ -181,9 +196,14 @@"crates/macro_event_topics","crates/macro_queues","crates/macro_service_urls",+ "crates/macro_sha_count_client",+ "crates/macro_sync_service_jwt","crates/macro_tower_layers","crates/macro_user_id","crates/macro_uuid",+ "crates/mcp_client",+ "crates/mcp_select",+ "crates/memory","crates/mention_utils","crates/model","crates/model-entity",@@ -191,8 +211,12 @@"crates/model_file_type","crates/model_notifications","crates/model_user",+ "crates/models_ai_projection","crates/models_bulk_upload",+ "crates/models_dcs",+ "crates/models_email","crates/models_entity_access_management",+ "crates/models_grouping","crates/models_opensearch","crates/models_pagination","crates/models_permissions",@@ -200,23 +224,162 @@"crates/models_search","crates/models_search_cursor","crates/models_soup",+ "crates/models_team","crates/non_empty","crates/notification","crates/opensearch_client","crates/opensearch_query_builder",+ "crates/pipedream_mcp",+ "crates/projects","crates/prompt","crates/properties","crates/rate_limit",+ "crates/readonly_pool","crates/reminders","crates/remote_env_var",+ "crates/roles_and_permissions",+ "crates/s3_key",+ "crates/search_service_client","crates/secretsmanager_client","crates/share_permission_db_utils",+ "crates/skills",+ "crates/soup",+ "crates/sqs_client","crates/sqs_worker",+ "crates/sync_service",+ "crates/sync_service_client","crates/system_properties",+ "crates/system_skills",+ "crates/task_dedup",+ "crates/teams","crates/unfurl",+ "crates/user_quota","crates/workspace-hack","services/agent_harness_service"],+ "agent_inmem": [+ "crates/activity",+ "crates/agent",+ "crates/agent_fold",+ "crates/agent_inmem",+ "crates/agent_runtime_protocol",+ "crates/agent_session",+ "crates/ai_tools",+ "crates/ai_toolset",+ "crates/ai_usage",+ "crates/analytics_client",+ "crates/anthropic",+ "crates/attachment",+ "crates/authentication_service_client",+ "crates/bot_id",+ "crates/bot_token",+ "crates/bots",+ "crates/broadcast",+ "crates/calendar_events",+ "crates/call",+ "crates/channel_sender",+ "crates/channels",+ "crates/chat",+ "crates/connection",+ "crates/connection_gateway_client",+ "crates/connection_gateway_models",+ "crates/contacts",+ "crates/cowlike",+ "crates/crm",+ "crates/database_env_vars",+ "crates/document_sub_type",+ "crates/documents",+ "crates/dynamodb_client",+ "crates/email",+ "crates/email_service_client",+ "crates/email_utils",+ "crates/embedding",+ "crates/entity_access",+ "crates/entity_access_db_utils",+ "crates/entity_access_management",+ "crates/entity_mutation",+ "crates/filter_ast",+ "crates/foreign_entity",+ "crates/frecency",+ "crates/generic_email_domains",+ "crates/import",+ "crates/invite_email",+ "crates/item_filters",+ "crates/kafka_util",+ "crates/last_online_tracker",+ "crates/lexical_client",+ "crates/lexical_mention_extractor",+ "crates/macro_auth",+ "crates/macro_authorization",+ "crates/macro_aws_config",+ "crates/macro_db_client",+ "crates/macro_entrypoint",+ "crates/macro_env",+ "crates/macro_env_var",+ "crates/macro_event_broker",+ "crates/macro_event_topics",+ "crates/macro_queues",+ "crates/macro_service_urls",+ "crates/macro_sha_count_client",+ "crates/macro_sync_service_jwt",+ "crates/macro_tower_layers",+ "crates/macro_user_id",+ "crates/macro_uuid",+ "crates/mcp_client",+ "crates/mcp_select",+ "crates/memory",+ "crates/mention_utils",+ "crates/model",+ "crates/model-entity",+ "crates/model-error-response",+ "crates/model_file_type",+ "crates/model_notifications",+ "crates/model_user",+ "crates/models_ai_projection",+ "crates/models_bulk_upload",+ "crates/models_dcs",+ "crates/models_email",+ "crates/models_entity_access_management",+ "crates/models_grouping",+ "crates/models_opensearch",+ "crates/models_pagination",+ "crates/models_permissions",+ "crates/models_properties",+ "crates/models_search",+ "crates/models_search_cursor",+ "crates/models_soup",+ "crates/models_team",+ "crates/non_empty",+ "crates/notification",+ "crates/opensearch_client",+ "crates/opensearch_query_builder",+ "crates/pipedream_mcp",+ "crates/projects",+ "crates/prompt",+ "crates/properties",+ "crates/rate_limit",+ "crates/readonly_pool",+ "crates/reminders",+ "crates/remote_env_var",+ "crates/roles_and_permissions",+ "crates/s3_key",+ "crates/search_service_client",+ "crates/secretsmanager_client",+ "crates/share_permission_db_utils",+ "crates/skills",+ "crates/soup",+ "crates/sqs_client",+ "crates/sqs_worker",+ "crates/sync_service",+ "crates/sync_service_client",+ "crates/system_properties",+ "crates/system_skills",+ "crates/task_dedup",+ "crates/teams",+ "crates/unfurl",+ "crates/user_quota",+ "crates/workspace-hack"+ ],"agent_runtime_protocol": ["crates/agent_runtime_protocol","crates/macro_uuid"@@ -2318,7 +2481,6 @@"crates/cal","crates/calendar_events","crates/call",- "crates/channel_bots","crates/channel_sender","crates/channels","crates/chat",diff --git a/Cargo.lock b/Cargo.lockindex e0d7eded65..5204f42e93 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -242,9 +242,11 @@ version = "0.1.0"dependencies = ["agent_fold","agent_harness",+ "agent_inmem","agent_runtime_protocol","agent_session","agent_trigger",+ "ai_tools","ai_usage","anyhow","aws-sdk-secretsmanager",@@ -279,12 +281,41 @@ dependencies = ["serde_json","sqlx","tokio",+ "tokio-util","tracing","utoipa","utoipa-swagger-ui","workspace-hack",]+[[package]]+name = "agent_inmem"+version = "0.1.0"+dependencies = [+ "agent",+ "agent-client-protocol",+ "agent_fold",+ "agent_runtime_protocol",+ "agent_session",+ "ai_tools",+ "ai_toolset",+ "ai_usage",+ "bot_id",+ "dashmap",+ "futures",+ "macro_user_id",+ "macro_uuid",+ "memory",+ "prompt",+ "serde",+ "serde_json",+ "sqlx",+ "tokio",+ "tokio-util",+ "tracing",+ "workspace-hack",+]+[[package]]name = "agent_runtime_protocol"version = "0.1.0"@@ -4934,7 +4965,6 @@ dependencies = ["cal","calendar_events","call",- "channel_bots","channels","chat","chrono",diff --git a/Cargo.toml b/Cargo.tomlindex 64fb351212..86d92be216 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -5,6 +5,7 @@ members = ["crates/activity","crates/agent", "crates/agent_fold","crates/agent_harness",+ "crates/agent_inmem","crates/agent_runtime_protocol", "crates/agent_session","crates/agent_trigger","crates/ai_projections",diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/aiFeature.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/aiFeature.tsindex 2153b3171f..ea91ddae1b 100644--- a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/aiFeature.ts+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/aiFeature.ts@@ -23,4 +23,5 @@ export const AiFeature = {ai_projection: 'ai_projection',ai_editing: 'ai_editing',import: 'import',+ agent_session: 'agent_session',} as const;diff --git a/apps/web/src/lib/service-clients/service-cognition/openapi.json b/apps/web/src/lib/service-clients/service-cognition/openapi.jsonindex 225e6a4fe9..3dcbb6a645 100644--- a/apps/web/src/lib/service-clients/service-cognition/openapi.json+++ b/apps/web/src/lib/service-clients/service-cognition/openapi.json@@ -2130,7 +2130,8 @@"channel_bot","ai_projection","ai_editing",- "import"+ "import",+ "agent_session"]},"AssistantMessagePart": {diff --git a/crates/agent_harness/src/domain/model.rs b/crates/agent_harness/src/domain/model.rsindex 3cb7c96657..efffe4569d 100644--- a/crates/agent_harness/src/domain/model.rs+++ b/crates/agent_harness/src/domain/model.rs@@ -37,14 +37,15 @@ pub struct OpenSession {pub origin: MentionOrigin,}-/// Whether this deployment provisions a sandbox for `bot`'s sessions.+/// Whether this deployment provisions a runtime for `bot`'s sessions.///-/// Only the dedicated Macro coder bot is managed; every other agent bot hosts-/// its own runtime and dials the gateway. This becomes a bot attribute the-/// day managed bots stop being a closed set of one.+/// The Macro coder bot's sessions run in a provisioned sandbox and the Macro+/// bot's run in-process; every other agent bot hosts its own runtime and+/// dials the gateway. This becomes a bot attribute the day managed bots stop+/// being a closed set.#[must_use]pub fn is_managed_bot(bot: BotId) -> bool {- bot == bot_id::MACRO_CODER_BOT_ID+ bot == bot_id::MACRO_CODER_BOT_ID || bot == bot_id::MACRO_AI_BOT_ID}/// Where a prompt came from, when it came from somewhere the session should@@ -191,3 +192,68 @@ pub struct SessionDefaults {/// Repository sessions run against.pub repo_url: String,}++/// Session defaults for every bot a deployment answers for.+///+/// One deployment can serve more than one managed bot (the sandboxed coder+/// bot and the in-process Macro bot), and each stamps different defaults onto+/// the sessions it opens.+#[derive(Debug, Clone)]+pub struct HarnessDefaults {+ default: SessionDefaults,+ per_bot: Vec<(BotId, SessionDefaults)>,+ managed_bot: Option<BotId>,+}++impl HarnessDefaults {+ /// Defaults every bot shares until one is given its own.+ #[must_use]+ pub fn new(default: SessionDefaults) -> Self {+ Self {+ default,+ per_bot: Vec::new(),+ managed_bot: None,+ }+ }++ /// Stamp `bot`'s sessions with `defaults` instead of the shared ones.+ #[must_use]+ pub fn with_bot(mut self, bot: BotId, defaults: SessionDefaults) -> Self {+ self.per_bot.push((bot, defaults));+ self+ }++ /// Open managed sessions - the ones nothing names a bot for, like the+ /// create menu's - as `bot` instead of the deployment's own.+ #[must_use]+ pub fn with_managed_bot(mut self, bot: BotId) -> Self {+ self.managed_bot = Some(bot);+ self+ }++ /// The defaults a session opens with when no particular bot is named -+ /// the `with_managed_bot` override when one is set, the deployment's own+ /// bot otherwise.+ #[must_use]+ pub fn managed(&self) -> &SessionDefaults {+ match self.managed_bot {+ Some(bot) => self.for_bot(bot),+ None => &self.default,+ }+ }++ /// The defaults `bot`'s sessions are stamped with.+ #[must_use]+ pub fn for_bot(&self, bot: BotId) -> &SessionDefaults {+ self.per_bot+ .iter()+ .find(|(candidate, _)| *candidate == bot)+ .map_or(&self.default, |(_, defaults)| defaults)+ }+}++impl From<SessionDefaults> for HarnessDefaults {+ fn from(default: SessionDefaults) -> Self {+ Self::new(default)+ }+}diff --git a/crates/agent_harness/src/domain/service.rs b/crates/agent_harness/src/domain/service.rsindex cf4be0e772..797eb5e398 100644--- a/crates/agent_harness/src/domain/service.rs+++ b/crates/agent_harness/src/domain/service.rs@@ -21,8 +21,8 @@ use tracing::instrument::WithSubscriber as _;use crate::domain::error::{HarnessError, Result};use crate::domain::model::{- AnnounceOrigin, AnnouncePrompt, DeliverAction, HarnessCommand, OpenSession,- SessionAnnouncement, SessionDefaults, SpawnContainer, is_managed_bot,+ AnnounceOrigin, AnnouncePrompt, DeliverAction, HarnessCommand, HarnessDefaults, OpenSession,+ SessionAnnouncement, SpawnContainer, is_managed_bot,};use crate::domain::ports::{ContainerManager, RuntimeConnections, SessionAnnouncer};use crate::domain::sandbox::SandboxResizeEffect;@@ -42,7 +42,7 @@ struct AgentHarnessInner<Sessions, Containers, Announcer, Runtimes> {containers: Containers,announcer: Announcer,runtimes: Runtimes,- defaults: SessionDefaults,+ defaults: HarnessDefaults,}/// Turns trigger commands into running, announced agent sessions.@@ -65,7 +65,7 @@ wherecontainers: Containers,announcer: Announcer,runtimes: Runtimes,- defaults: SessionDefaults,+ defaults: impl Into<HarnessDefaults>,) -> Self {Self {inner: Arc::new(AgentHarnessInner {@@ -73,7 +73,7 @@ wherecontainers,announcer,runtimes,- defaults,+ defaults: defaults.into(),}),workers: Arc::new(DashMap::new()),}@@ -251,6 +251,7 @@ where&self,request: agent_session::domain::ports::OpenExternalAgentSession,) -> agent_session::domain::error::Result<AgentSession> {+ let defaults = self.inner.defaults.for_bot(request.bot_id);let session = self.inner.sessions@@ -260,8 +261,8 @@ wherebot_id: request.bot_id,thread_id: request.thread.as_ref().map(|thread| thread.thread_id),originating_message_id: request.thread.as_ref().map(|thread| thread.message_id),- model: self.inner.defaults.model.clone(),- harness: self.inner.defaults.harness.clone(),+ model: defaults.model.clone(),+ harness: defaults.harness.clone(),repo_url: request.repo_url,workspace: request.workspace,sandbox_size: SandboxSize::Default,@@ -292,18 +293,18 @@ whereOk(session)}- /// Provision a sandbox, open a session on it, and deliver the first- /// prompt if one came with the request.+ /// Provision the managed-default bot's runtime, open a session on it,+ /// and deliver the first prompt if one came with the request.////// Nothing is announced: a managed session opened this way has no- /// originating mention and no thread to answer back into. The sandbox is+ /// originating mention and no thread to answer back into. The runtime is/// spawned before the session is attached because there is nothing to/// attach to until it exists.async fn open_managed_session(&self,request: agent_session::domain::ports::OpenManagedSession,) -> agent_session::domain::error::Result<AgentSession> {- let defaults = &self.inner.defaults;+ let defaults = self.inner.defaults.managed();let sandbox_size = self.inner.sessions@@ -481,7 +482,8 @@ whereasync fn open(&self, session_id: AgentSessionId, command: OpenSession) -> Result<()> {let OpenSession { bot_id, origin } = command;tracing::Span::current().record("agent.session.id", tracing::field::display(session_id));- let repo_url = self.defaults.repo_url.clone();+ let defaults = self.defaults.for_bot(bot_id);+ let repo_url = defaults.repo_url.clone();let sandbox_size = self.sessions.user_sandbox_size(&origin.sender).await?;self.sessions@@ -491,8 +493,8 @@ wherebot_id,thread_id: Some(origin.thread_id),originating_message_id: Some(origin.message_id),- model: self.defaults.model.clone(),- harness: self.defaults.harness.clone(),+ model: defaults.model.clone(),+ harness: defaults.harness.clone(),repo_url: Some(repo_url.clone()),// Managed sandboxes run in the path baked into their image.workspace: agent_session::MANAGED_CONTAINER_WORKSPACE.to_owned(),diff --git a/crates/agent_harness/src/domain/service/test.rs b/crates/agent_harness/src/domain/service/test.rsindex 22d4ecb9ab..04b2f53a9e 100644--- a/crates/agent_harness/src/domain/service/test.rs+++ b/crates/agent_harness/src/domain/service/test.rs@@ -32,8 +32,8 @@ use macro_uuid::Uuid;use super::AgentHarnessService;use crate::domain::error::HarnessError;use crate::domain::model::{- AnnounceOrigin, DeliverAction, HarnessCommand, MentionOrigin, OpenSession, SessionDefaults,- SpawnContainer,+ AnnounceOrigin, DeliverAction, HarnessCommand, HarnessDefaults, MentionOrigin, OpenSession,+ SessionDefaults, SpawnContainer,};use crate::domain::ports::ContainerManager as _;use crate::outbound::runtime_registry::RuntimeRegistry;@@ -979,6 +979,54 @@ async fn a_prompt_after_a_redial_restores_the_session_on_the_new_connection() {assert_eq!(containers.resumed(), 0);}+/// The create menu names no bot, so its sessions open as the deployment's+/// managed default - the in-process bot when one is configured - with that+/// bot's own defaults stamped on.+#[tokio::test]+async fn a_managed_session_opens_as_the_managed_default_bot() {+ let repo = InMemoryAgentSessionRepo::new();+ let containers = MockContainerManager::new();+ let inmem_bot = BotId::TEST_B;+ let service = AgentHarnessService::new(+ AgentSessionServiceImpl::new(+ repo.clone(),+ FoldedMessageService::new(repo.clone()),+ NoOpRealtime,+ ),+ containers.clone(),+ AnnouncerMock::new(),+ RuntimeRegistry::<ContainerSender>::new(),+ HarnessDefaults::new(SessionDefaults {+ bot_id: BotId::TEST_A,+ model: "claude".to_owned(),+ harness: "opencode".to_owned(),+ repo_url: "https://github.com/macro-inc/macro".to_owned(),+ })+ .with_bot(+ inmem_bot,+ SessionDefaults {+ bot_id: inmem_bot,+ model: "fast-model".to_owned(),+ harness: "macro-inmem".to_owned(),+ repo_url: "https://github.com/macro-inc/macro".to_owned(),+ },+ )+ .with_managed_bot(inmem_bot),+ );++ let session = service+ .open_managed_session(agent_session::domain::ports::OpenManagedSession {+ owner: sender(),+ prompt: None,+ })+ .await+ .expect("the managed session should open");++ assert_eq!(session.bot_id, inmem_bot);+ assert_eq!(session.model, "fast-model");+ assert_eq!(session.harness, "macro-inmem");+}+#[tokio::test]async fn a_managed_session_resumes_its_sandbox_rather_than_a_dialed_in_runtime() {let (service, repo, containers, _announcer, runtimes) = harness();diff --git a/crates/agent_harness/src/inbound/kafka.rs b/crates/agent_harness/src/inbound/kafka.rsindex 05e348230d..0902521540 100644--- a/crates/agent_harness/src/inbound/kafka.rs+++ b/crates/agent_harness/src/inbound/kafka.rs@@ -50,14 +50,14 @@ pub enum Skipped {/// Route one trigger event: work for this deployment, or a reason it was/// skipped.///-/// Opens are only ours when the mentioned bot is `our_bot` - external bots'-/// runtimes open their own sessions over the API. Events for sessions that-/// already exist always carry work: a prompt to deliver when the session is-/// managed here, or just its announcement when the bot's own runtime+/// Opens are only ours when the mentioned bot is one of `our_bots` - external+/// bots' runtimes open their own sessions over the API. Events for sessions+/// that already exist always carry work: a prompt to deliver when the session+/// is managed here, or just its announcement when the bot's own runtime/// delivers the prompt.pub fn route_agent_trigger(event: AgentTriggerTopicEvent,- our_bot: BotId,+ our_bots: &[BotId],) -> Result<RoutedTrigger, Skipped> {match event {AgentTriggerTopicEvent::New(NewAgentSessionEvent::TopLevelMentioned(mentioned)) => {@@ -72,7 +72,7 @@ pub fn route_agent_trigger(return Err(Skipped::NotMacroStaff);}- if mentioned.bot_id != our_bot {+ if !our_bots.contains(&mentioned.bot_id) {return Err(Skipped::ForeignBot);}let message = mentioned.message;@@ -112,7 +112,7 @@ pub fn route_agent_trigger(// A managed session's prompt is delivered (and announced)// by the deployment that manages it; anyone else stays out// of the way entirely.- if bot_id != our_bot {+ if !our_bots.contains(&bot_id) {return Err(Skipped::ForeignBot);}return Ok(RoutedTrigger::Command(diff --git a/crates/agent_harness/src/inbound/kafka/test.rs b/crates/agent_harness/src/inbound/kafka/test.rsindex e1cace084b..9fd3c7ad29 100644--- a/crates/agent_harness/src/inbound/kafka/test.rs+++ b/crates/agent_harness/src/inbound/kafka/test.rs@@ -55,7 +55,7 @@ fn channel_message(bot: BotId) -> AgentTriggerTopicEvent {fn a_mention_for_our_bot_opens_a_session() {let routed = route_agent_trigger(mentioned(BotId::TEST_A, ChannelSender::new_from_user(user())),- BotId::TEST_A,+ &[BotId::TEST_A],).expect("a mention for our bot should yield work");@@ -83,7 +83,7 @@ fn a_threaded_mention_answers_into_its_thread() {));let RoutedTrigger::Command(_, HarnessCommand::Open(open)) =- route_agent_trigger(event, BotId::TEST_A).expect("the mention should yield work")+ route_agent_trigger(event, &[BotId::TEST_A]).expect("the mention should yield work")else {panic!("a new-session event should open");};@@ -95,7 +95,7 @@ fn a_foreign_bots_open_is_skipped() {assert_eq!(route_agent_trigger(mentioned(BotId::TEST_A, ChannelSender::new_from_user(user())),- BotId::TEST_B,+ &[BotId::TEST_B],).unwrap_err(),Skipped::ForeignBot@@ -105,7 +105,7 @@ fn a_foreign_bots_open_is_skipped() {#[test]fn another_deployments_managed_traffic_is_skipped() {assert_eq!(- route_agent_trigger(channel_message(MACRO_CODER_BOT_ID), BotId::TEST_B).unwrap_err(),+ route_agent_trigger(channel_message(MACRO_CODER_BOT_ID), &[BotId::TEST_B]).unwrap_err(),Skipped::ForeignBot);}@@ -115,7 +115,7 @@ fn a_bot_authored_mention_is_skipped() {assert_eq!(route_agent_trigger(mentioned(BotId::TEST_A, ChannelSender::new_from_bot(BotId::TEST_B)),- BotId::TEST_A,+ &[BotId::TEST_A],).unwrap_err(),Skipped::NotFromUser@@ -129,7 +129,7 @@ fn a_non_staff_mention_is_skipped() {assert_eq!(route_agent_trigger(mentioned(BotId::TEST_A, ChannelSender::new_from_user(user)),- BotId::TEST_A,+ &[BotId::TEST_A],).unwrap_err(),Skipped::NotMacroStaff@@ -138,7 +138,7 @@ fn a_non_staff_mention_is_skipped() {#[test]fn a_managed_channel_message_forwards_to_its_session() {- let routed = route_agent_trigger(channel_message(MACRO_CODER_BOT_ID), MACRO_CODER_BOT_ID)+ let routed = route_agent_trigger(channel_message(MACRO_CODER_BOT_ID), &[MACRO_CODER_BOT_ID]).expect("a channel event for our bot should yield work");let RoutedTrigger::Command(session_id, HarnessCommand::Deliver(deliver)) = routed else {@@ -162,7 +162,7 @@ fn a_managed_channel_message_forwards_to_its_session() {fn an_external_channel_message_announces_only() {// The external bot's own runtime delivers the prompt; this deployment// only posts the chip, whichever bot it manages itself.- let routed = route_agent_trigger(channel_message(BotId::TEST_A), MACRO_CODER_BOT_ID)+ let routed = route_agent_trigger(channel_message(BotId::TEST_A), &[MACRO_CODER_BOT_ID]).expect("an external existing-session event should yield work");let RoutedTrigger::Announce(session_id, prompt) = routed else {@@ -186,7 +186,7 @@ fn a_bot_authored_external_channel_message_is_skipped() {},));assert_eq!(- route_agent_trigger(event, MACRO_CODER_BOT_ID).unwrap_err(),+ route_agent_trigger(event, &[MACRO_CODER_BOT_ID]).unwrap_err(),Skipped::NotFromUser);}diff --git a/crates/agent_inmem/Cargo.toml b/crates/agent_inmem/Cargo.tomlnew file mode 100644index 0000000000..7ed40ad249--- /dev/null+++ b/crates/agent_inmem/Cargo.toml@@ -0,0 +1,34 @@+[package]+edition = "2024"+name = "agent_inmem"+publish = false+version = "0.1.0"++[dependencies]+agent = { path = "../agent" }+agent-client-protocol = { workspace = true }+agent_runtime_protocol = { path = "../agent_runtime_protocol" }+agent_session = { path = "../agent_session" }+ai_tools = { path = "../ai_tools" }+ai_toolset = { path = "../ai_toolset" }+ai_usage = { path = "../ai_usage" }+dashmap = { workspace = true }+futures = { workspace = true }+macro_user_id = { path = "../macro_user_id" }+macro_uuid = { path = "../macro_uuid" }+memory = { path = "../memory" }+prompt = { path = "../prompt" }+serde = { workspace = true }+serde_json = { workspace = true }+sqlx = { workspace = true }+tokio = { workspace = true, features = ["sync", "rt", "time", "macros"] }+tokio-util = { workspace = true }+tracing = { workspace = true }+workspace-hack = { version = "0.1", path = "../workspace-hack" }++[dev-dependencies]+agent_fold = { path = "../agent_fold" }+agent_session = { path = "../agent_session", features = ["test-utils"] }+bot_id = { path = "../bot_id", features = ["test-utils"] }+futures = { workspace = true }+tokio = { workspace = true, features = ["macros", "rt-multi-thread", "time"] }diff --git a/crates/agent_inmem/src/domain/agent.rs b/crates/agent_inmem/src/domain/agent.rsnew file mode 100644index 0000000000..c76a6e3b1a--- /dev/null+++ b/crates/agent_inmem/src/domain/agent.rs@@ -0,0 +1,454 @@+//! The agent side of ACP, served over an in-process channel.+//!+//! One agent task serves one Macro session ([`RuntimeAttachment::solo`] on+//! the harness side), so the surface is small: `initialize`, `session/new`+//! or `session/resume`, `session/prompt`, `session/set_config_option`, and+//! `session/cancel`. Prompts run through the [`TurnEngine`] and stream back+//! as `session/update` notifications the existing fold and UI already render.+//!+//! [`RuntimeAttachment::solo`]: agent_session::domain::connection::RuntimeAttachment::solo++use std::collections::HashSet;+use std::sync::{Arc, Mutex};+use std::time::Duration;++use agent::types::{AssistantMessagePart, ChatMessage};+use agent::{StreamAccumulator, StreamPart, ToolResponse};+use agent_client_protocol::schema::v1::{+ AgentCapabilities, CancelNotification, ContentBlock, ContentChunk, InitializeRequest,+ InitializeResponse, NewSessionRequest, NewSessionResponse, PromptRequest, PromptResponse,+ ResumeSessionRequest, ResumeSessionResponse, SessionCapabilities, SessionId,+ SessionNotification, SessionResumeCapabilities, SessionUpdate, SetSessionConfigOptionRequest,+ SetSessionConfigOptionResponse, StopReason, ToolCall as AcpToolCall, ToolCallStatus,+ ToolCallUpdate, ToolCallUpdateFields, ToolKind,+};+use agent_client_protocol::{+ Agent, Channel as AcpChannel, Client, ConnectionTo, Error as AcpError,+};+use agent_runtime_protocol::domain::action::{COMPACT_COMMAND, MODEL_CONFIG_ID};+use agent_session::domain::model::AgentSessionId;+use macro_user_id::user_id::MacroUserIdStr;+use tokio_util::sync::CancellationToken;++use crate::domain::engine::{TurnEngine, TurnRequest};+use crate::domain::session::{HistoryEntry, SessionStore, messages_for_turn};++#[cfg(test)]+mod test;++/// A turn that produces nothing for this long is treated as hung and+/// cancelled, so it cannot wedge the session's turn lock forever.+const TURN_IDLE_TIMEOUT: Duration = Duration::from_secs(5 * 60);++/// Everything one agent task serves its session from.+pub struct AgentState {+ /// The Macro session this agent runs.+ pub session_id: AgentSessionId,+ /// The session's owner; turns run on their behalf.+ pub owner: MacroUserIdStr<'static>,+ /// Runs the actual turns.+ pub engine: Arc<dyn TurnEngine>,+ /// Conversation state, shared with the manager so it survives reattach.+ pub store: Arc<SessionStore>,+ /// Every outstanding turn's cancellation token - the running turn and any+ /// queued behind it. `session/cancel` stops them all.+ pub active_cancel: Mutex<Vec<CancellationToken>>,+ /// Serializes turns: the client may queue prompts, the engine runs one at+ /// a time.+ pub turn_lock: tokio::sync::Mutex<()>,+}++impl AgentState {+ fn expect_session(&self, requested: &SessionId) -> Result<(), AcpError> {+ let matches = self+ .store+ .get(&self.session_id)+ .is_some_and(|state| state.acp_session_id.as_ref() == Some(requested));+ if matches {+ Ok(())+ } else {+ Err(AcpError::invalid_params().data(format!("unknown session {requested}")))+ }+ }++ /// Bind `acp_id` as this session's ACP session, keeping the recorded+ /// conversation only when it already belongs to that id.+ fn bind_acp_session(&self, acp_id: SessionId, keep_history: bool) {+ if let Some(mut state) = self.store.get_mut(&self.session_id) {+ if !keep_history || state.acp_session_id.as_ref() != Some(&acp_id) {+ state.history.clear();+ }+ state.acp_session_id = Some(acp_id);+ }+ }++ fn clear_history(&self) {+ if let Some(mut state) = self.store.get_mut(&self.session_id) {+ state.history.clear();+ }+ }++ fn set_model(&self, model: String) {+ if let Some(mut state) = self.store.get_mut(&self.session_id) {+ state.model = model;+ }+ }++ /// The messages and model for a turn answering `prompt`.+ fn turn_input(&self, prompt: &str) -> (Vec<ChatMessage>, String) {+ self.store.get(&self.session_id).map_or_else(+ || (messages_for_turn(&[], prompt), String::new()),+ |state| {+ (+ messages_for_turn(&state.history, prompt),+ state.model.clone(),+ )+ },+ )+ }++ fn push_turn(&self, prompt: String, parts: Vec<AssistantMessagePart>) {+ if let Some(mut state) = self.store.get_mut(&self.session_id) {+ state.history.push(HistoryEntry::User(prompt));+ if !parts.is_empty() {+ state.history.push(HistoryEntry::Assistant(parts));+ }+ }+ }++ fn begin_turn(&self) -> CancellationToken {+ let cancel = CancellationToken::new();+ let mut outstanding = self+ .active_cancel+ .lock()+ .expect("active turn lock should not be poisoned");+ outstanding.retain(|token| !token.is_cancelled());+ outstanding.push(cancel.clone());+ cancel+ }++ fn cancel_active_turns(&self) {+ for cancel in self+ .active_cancel+ .lock()+ .expect("active turn lock should not be poisoned")+ .iter()+ {+ cancel.cancel();+ }+ }+}++/// Serve this session's agent on `acp` until the connection closes.+pub async fn serve(state: Arc<AgentState>, acp: AcpChannel) -> Result<(), AcpError> {+ Agent+ .builder()+ .name("macro-inmem")+ .on_receive_request(+ async move |request: InitializeRequest, responder, _connection| {+ responder.respond(+ InitializeResponse::new(request.protocol_version).agent_capabilities(+ AgentCapabilities::new().session_capabilities(+ SessionCapabilities::new().resume(SessionResumeCapabilities::new()),+ ),+ ),+ )+ },+ agent_client_protocol::on_receive_request!(),+ )+ .on_receive_request(+ {+ let state = Arc::clone(&state);+ async move |_request: NewSessionRequest, responder, _connection| {+ let state = Arc::clone(&state);+ let acp_id = SessionId::new(macro_uuid::generate_uuid_v7().to_string());+ state.bind_acp_session(acp_id.clone(), false);+ responder.respond(NewSessionResponse::new(acp_id))+ }+ },+ agent_client_protocol::on_receive_request!(),+ )+ .on_receive_request(+ {+ let state = Arc::clone(&state);+ async move |request: ResumeSessionRequest, responder, _connection| {+ let state = Arc::clone(&state);+ // Kept when the state already belongs to this ACP id -+ // either this process served the session, or a cold+ // attach replayed the frame log back into it (see+ // `domain::replay`).+ state.bind_acp_session(request.session_id, true);+ responder.respond(ResumeSessionResponse::new())+ }+ },+ agent_client_protocol::on_receive_request!(),+ )+ .on_receive_request(+ {+ let state = Arc::clone(&state);+ async move |request: PromptRequest, responder, connection| {+ let state = Arc::clone(&state);+ if let Err(error) = state.expect_session(&request.session_id) {+ return responder.respond_with_error(error);+ }+ let prompt = prompt_text(&request);+ if prompt.trim() == COMPACT_COMMAND {+ state.clear_history();+ let _ = connection.send_notification(SessionNotification::new(+ request.session_id,+ SessionUpdate::AgentMessageChunk(ContentChunk::new(+ "Compacted: the earlier conversation is no longer in the \+ model's context."+ .into(),+ )),+ ));+ return responder.respond(PromptResponse::new(StopReason::EndTurn));+ }++ let cancel = state.begin_turn();+ connection.spawn({+ let connection = connection.clone();+ async move {+ let stop =+ run_turn(&state, &connection, request.session_id, prompt, cancel)+ .await;+ // A closed connection is the only way this fails,+ // and failing the spawned task would tear the+ // whole (already closing) server down.+ let _ = responder.respond(PromptResponse::new(stop));+ Ok(())+ }+ })?;+ Ok(())+ }+ },+ agent_client_protocol::on_receive_request!(),+ )+ .on_receive_request(+ {+ let state = Arc::clone(&state);+ async move |request: SetSessionConfigOptionRequest, responder, _connection| {+ let state = Arc::clone(&state);+ if let Err(error) = state.expect_session(&request.session_id) {+ return responder.respond_with_error(error);+ }+ if request.config_id.to_string() != MODEL_CONFIG_ID {+ return responder.respond_with_error(+ AcpError::invalid_params()+ .data(format!("unknown config option {}", request.config_id)),+ );+ }+ let Some(model) = request.value.as_value_id() else {+ return responder.respond_with_error(+ AcpError::invalid_params().data("the model option takes a value id"),+ );+ };+ state.set_model(model.to_string());+ responder.respond(SetSessionConfigOptionResponse::new(Vec::new()))+ }+ },+ agent_client_protocol::on_receive_request!(),+ )+ .on_receive_notification(+ {+ let state = Arc::clone(&state);+ async move |notification: CancelNotification, _connection| {+ let state = Arc::clone(&state);+ if state.expect_session(¬ification.session_id).is_ok() {+ state.cancel_active_turns();+ }+ Ok(())+ }+ },+ agent_client_protocol::on_receive_notification!(),+ )+ .connect_to(acp)+ .await+}++/// Run one turn to completion, streaming updates as they arrive.+async fn run_turn(+ state: &AgentState,+ connection: &ConnectionTo<Client>,+ acp_session_id: SessionId,+ prompt: String,+ cancel: CancellationToken,+) -> StopReason {+ let _turn = state.turn_lock.lock().await;+ let (messages, model) = state.turn_input(&prompt);+ let mut parts = state.engine.run_turn(TurnRequest {+ owner: state.owner.clone(),+ model,+ messages,+ cancel: cancel.clone(),+ });++ let mut accumulator = StreamAccumulator::new();+ let mut failure = None;+ loop {+ match tokio::time::timeout(TURN_IDLE_TIMEOUT, parts.recv()).await {+ Ok(Some(Ok(part))) => {+ if let Some(update) = update_for_part(&part) {+ let notification = SessionNotification::new(acp_session_id.clone(), update);+ if connection.send_notification(notification).is_err() {+ // Nobody is listening; stop spending tokens.+ cancel.cancel();+ break;+ }+ }+ accumulator.push(part);+ }+ Ok(Some(Err(error))) => {+ failure = Some(error.to_string());+ break;+ }+ Ok(None) => break,+ Err(_) => {+ failure = Some(format!(+ "the turn produced nothing for {} seconds and was stopped",+ TURN_IDLE_TIMEOUT.as_secs()+ ));+ cancel.cancel();+ break;+ }+ }+ }++ let mut turn_parts = accumulator.into_parts();+ for (id, _name) in close_dangling_tool_calls(&mut turn_parts) {+ let _ = connection.send_notification(SessionNotification::new(+ acp_session_id.clone(),+ SessionUpdate::ToolCallUpdate(ToolCallUpdate::new(+ id,+ ToolCallUpdateFields::new().status(ToolCallStatus::Failed),+ )),+ ));+ }+ if let Some(failure) = failure {+ let _ = connection.send_notification(SessionNotification::new(+ acp_session_id.clone(),+ SessionUpdate::AgentMessageChunk(ContentChunk::new(+ format!("The agent stopped on an error: {failure}").into(),+ )),+ ));+ }+ state.push_turn(prompt, turn_parts);++ if cancel.is_cancelled() {+ StopReason::Cancelled+ } else {+ StopReason::EndTurn+ }+}++/// The `session/update` a stream part renders as, if any.+fn update_for_part(part: &StreamPart) -> Option<SessionUpdate> {+ match part {+ StreamPart::Content(text) => Some(SessionUpdate::AgentMessageChunk(ContentChunk::new(+ ContentBlock::from(text.clone()),+ ))),+ StreamPart::Thinking(text) => Some(SessionUpdate::AgentThoughtChunk(ContentChunk::new(+ ContentBlock::from(text.clone()),+ ))),+ StreamPart::ToolCall(call) => {+ let title = call+ .mcp+ .as_ref()+ .and_then(|mcp| mcp.display_name.clone())+ .unwrap_or_else(|| call.name.clone());+ Some(SessionUpdate::ToolCall(+ AcpToolCall::new(call.id.clone(), title)+ .kind(tool_kind(&call.name))+ .status(ToolCallStatus::InProgress)+ .raw_input(call.json.clone()),+ ))+ }+ StreamPart::ToolResponse(ToolResponse::Json { id, json, .. }) => {+ Some(SessionUpdate::ToolCallUpdate(ToolCallUpdate::new(+ id.clone(),+ ToolCallUpdateFields::new()+ .status(ToolCallStatus::Completed)+ .raw_output(json.clone()),+ )))+ }+ StreamPart::ToolResponse(ToolResponse::Err {+ id, description, ..+ }) => Some(SessionUpdate::ToolCallUpdate(ToolCallUpdate::new(+ id.clone(),+ ToolCallUpdateFields::new()+ .status(ToolCallStatus::Failed)+ .raw_output(serde_json::json!({ "error": description })),+ ))),+ // Recorded by the loop's usage recorder; nothing to render.+ StreamPart::Usage(_) => None,+ }+}++/// A coarse [`ToolKind`] for a Macro tool name, for client iconography only.+fn tool_kind(name: &str) -> ToolKind {+ let name = name.to_ascii_lowercase();+ if name.contains("search") {+ ToolKind::Search+ } else if name.starts_with("read") || name.starts_with("get") || name.starts_with("list") {+ ToolKind::Read+ } else if name.starts_with("delete") {+ ToolKind::Delete+ } else if name.starts_with("create")+ || name.starts_with("edit")+ || name.starts_with("update")+ || name.starts_with("rename")+ || name.starts_with("set")+ {+ ToolKind::Edit+ } else {+ ToolKind::Other+ }+}++/// The prompt's text content, other block types ignored.+fn prompt_text(request: &PromptRequest) -> String {+ request+ .prompt+ .iter()+ .filter_map(|block| match block {+ ContentBlock::Text(text) => Some(text.text.as_str()),+ _ => None,+ })+ .collect()+}++/// Close tool calls that never got a response - a cancelled or failed turn+/// leaves them dangling, and an unmatched call would poison the next turn's+/// provider payload. Returns what was synthesized as `(id, name)`.+pub(crate) fn close_dangling_tool_calls(+ parts: &mut Vec<AssistantMessagePart>,+) -> Vec<(String, String)> {+ let responded: HashSet<String> = parts+ .iter()+ .filter_map(|part| match part {+ AssistantMessagePart::ToolCallResponseJson { id, .. }+ | AssistantMessagePart::ToolCallErr { id, .. } => Some(id.clone()),+ _ => None,+ })+ .collect();+ let dangling: Vec<(String, String)> = parts+ .iter()+ .filter_map(|part| match part {+ AssistantMessagePart::ToolCall { id, name, .. }+ | AssistantMessagePart::McpToolCall { id, name, .. }+ if !responded.contains(id) =>+ {+ Some((id.clone(), name.clone()))+ }+ _ => None,+ })+ .collect();+ for (id, name) in &dangling {+ parts.push(AssistantMessagePart::ToolCallErr {+ name: name.clone(),+ description: "cancelled".to_owned(),+ id: id.clone(),+ });+ }+ dangling+}diff --git a/crates/agent_inmem/src/domain/agent/test.rs b/crates/agent_inmem/src/domain/agent/test.rsnew file mode 100644index 0000000000..68e3e03eb5--- /dev/null+++ b/crates/agent_inmem/src/domain/agent/test.rs@@ -0,0 +1,250 @@+use std::sync::Arc;++use agent::StreamPart;+use agent_client_protocol::schema::ProtocolVersion;+use agent_client_protocol::schema::v1::{+ ContentBlock, InitializeRequest, NewSessionRequest, PromptRequest, SessionNotification,+ TextContent,+};+use agent_client_protocol::{Client, ConnectionTo};++use super::*;+use crate::domain::engine::TurnEngine;+use crate::testing::{HangingEngine, ScriptedEngine};++struct Harness {+ notifications: std::sync::Mutex<Vec<SessionNotification>>,+}++/// Drive the served agent as a scripted ACP client: initialize, open a+/// session, then hand the connection to `scenario`.+async fn with_agent<Engine, Out>(+ engine: Arc<Engine>,+ scenario: impl AsyncFnOnce(ConnectionTo<Agent>, SessionId) -> Out,+) -> (Vec<SessionNotification>, Out)+where+ Engine: TurnEngine,+{+ let store = Arc::new(SessionStore::new());+ let session_id = AgentSessionId::new();+ store.insert(+ session_id,+ crate::domain::session::SessionState::new("test-model".into()),+ );+ let state = Arc::new(AgentState {+ session_id,+ owner: MacroUserIdStr::try_from_email("[email protected]").expect("a valid user id"),+ engine,+ store,+ active_cancel: std::sync::Mutex::new(Vec::new()),+ turn_lock: tokio::sync::Mutex::new(()),+ });++ let (client_channel, agent_channel) = AcpChannel::duplex();+ let agent = tokio::spawn(serve(state, agent_channel));++ let harness = Arc::new(Harness {+ notifications: std::sync::Mutex::new(Vec::new()),+ });+ let observed = Arc::clone(&harness);+ let out = Client+ .builder()+ .on_receive_notification(+ async move |notification: SessionNotification, _connection| {+ observed+ .notifications+ .lock()+ .expect("notifications lock")+ .push(notification);+ Ok(())+ },+ agent_client_protocol::on_receive_notification!(),+ )+ .connect_with(+ client_channel,+ async move |connection: ConnectionTo<Agent>| {+ let initialized = connection+ .send_request(InitializeRequest::new(ProtocolVersion::V1))+ .block_task()+ .await?;+ assert!(+ initialized+ .agent_capabilities+ .session_capabilities+ .resume+ .is_some(),+ "the agent must declare resume support or reattachment dies"+ );+ let session = connection+ .send_request(NewSessionRequest::new("/"))+ .block_task()+ .await?;+ Ok(scenario(connection, session.session_id).await)+ },+ )+ .await+ .expect("the scripted client should run clean");+ agent.abort();++ let notifications = harness+ .notifications+ .lock()+ .expect("notifications lock")+ .clone();+ (notifications, out)+}++fn text_prompt(session: &SessionId, text: &str) -> PromptRequest {+ PromptRequest::new(+ session.clone(),+ vec![ContentBlock::Text(TextContent::new(text))],+ )+}++#[tokio::test]+async fn a_prompt_streams_updates_and_ends_the_turn() {+ let engine = Arc::new(ScriptedEngine::new(vec![+ StreamPart::Thinking("hmm".into()),+ StreamPart::Content("Hello ".into()),+ StreamPart::ToolCall(agent::ToolCall {+ id: "call-1".into(),+ name: "NameSearch".into(),+ json: serde_json::json!({"query": "roadmap"}),+ mcp: None,+ }),+ StreamPart::ToolResponse(agent::ToolResponse::Json {+ id: "call-1".into(),+ json: serde_json::json!({"hits": 1}),+ name: "NameSearch".into(),+ }),+ StreamPart::Content("world".into()),+ ]));++ let (notifications, response) = with_agent(Arc::clone(&engine), async |connection, session| {+ connection+ .send_request(text_prompt(&session, "find the roadmap"))+ .block_task()+ .await+ .expect("the prompt should complete")+ })+ .await;++ assert_eq!(response.stop_reason, StopReason::EndTurn);+ let kinds: Vec<&'static str> = notifications+ .iter()+ .map(|notification| match ¬ification.update {+ SessionUpdate::AgentThoughtChunk(_) => "thought",+ SessionUpdate::AgentMessageChunk(_) => "message",+ SessionUpdate::ToolCall(_) => "tool_call",+ SessionUpdate::ToolCallUpdate(_) => "tool_call_update",+ _ => "other",+ })+ .collect();+ assert_eq!(+ kinds,+ vec![+ "thought",+ "message",+ "tool_call",+ "tool_call_update",+ "message"+ ]+ );+}++#[tokio::test]+async fn turns_accumulate_history_and_send_the_model() {+ let engine = Arc::new(ScriptedEngine::new(vec![StreamPart::Content("ok".into())]));++ with_agent(Arc::clone(&engine), async |connection, session| {+ for prompt in ["first", "second"] {+ connection+ .send_request(text_prompt(&session, prompt))+ .block_task()+ .await+ .expect("the prompt should complete");+ }+ })+ .await;++ let requests = engine.requests();+ assert_eq!(requests.len(), 2);+ assert_eq!(requests[0].0, "test-model");+ assert_eq!(requests[0].1, vec!["first".to_owned()]);+ // The second turn carries the first turn's prompt and reply.+ assert_eq!(+ requests[1].1,+ vec!["first".to_owned(), "ok".to_owned(), "second".to_owned()]+ );+}++#[tokio::test]+async fn compact_clears_history_without_running_a_turn() {+ let engine = Arc::new(ScriptedEngine::new(vec![StreamPart::Content("ok".into())]));++ with_agent(Arc::clone(&engine), async |connection, session| {+ connection+ .send_request(text_prompt(&session, "remember this"))+ .block_task()+ .await+ .expect("the prompt should complete");+ let response = connection+ .send_request(text_prompt(&session, "/compact"))+ .block_task()+ .await+ .expect("compaction should complete");+ assert_eq!(response.stop_reason, StopReason::EndTurn);+ connection+ .send_request(text_prompt(&session, "after"))+ .block_task()+ .await+ .expect("the prompt should complete");+ })+ .await;++ let requests = engine.requests();+ assert_eq!(requests.len(), 2, "/compact must not reach the engine");+ assert_eq!(+ requests[1].1,+ vec!["after".to_owned()],+ "compaction empties the conversation"+ );+}++#[tokio::test]+async fn cancel_stops_the_turn_with_the_cancelled_stop_reason() {+ let engine = Arc::new(HangingEngine);++ let (_notifications, response) = with_agent(engine, async |connection, session| {+ let pending = connection.send_request(text_prompt(&session, "hang"));+ let cancel = agent_client_protocol::schema::v1::CancelNotification::new(session.clone());+ connection+ .send_notification(cancel)+ .expect("the cancel notification should send");+ pending+ .block_task()+ .await+ .expect("a cancelled prompt still completes")+ })+ .await;++ assert_eq!(response.stop_reason, StopReason::Cancelled);+}++#[tokio::test]+async fn a_prompt_for_an_unknown_session_is_refused() {+ let engine = Arc::new(ScriptedEngine::new(vec![]));++ with_agent(engine, async |connection, _session| {+ let error = connection+ .send_request(text_prompt(&SessionId::new("not-a-session"), "hi"))+ .block_task()+ .await+ .expect_err("a foreign session id must be refused");+ assert_eq!(+ error.code,+ agent_client_protocol::schema::v1::ErrorCode::InvalidParams+ );+ })+ .await;+}diff --git a/crates/agent_inmem/src/domain/engine.rs b/crates/agent_inmem/src/domain/engine.rsnew file mode 100644index 0000000000..5311b9aa6e--- /dev/null+++ b/crates/agent_inmem/src/domain/engine.rs@@ -0,0 +1,34 @@+//! The seam between the ACP surface and the agentic loop that serves it.++use agent::types::ChatMessage;+use agent::{AgentError, StreamPart};+use macro_user_id::user_id::MacroUserIdStr;+use tokio::sync::mpsc;+use tokio_util::sync::CancellationToken;++/// Everything one conversational turn needs.+pub struct TurnRequest {+ /// The user the turn acts on behalf of. Tools run with their identity and+ /// token usage is recorded against them.+ pub owner: MacroUserIdStr<'static>,+ /// Model id the turn runs on. Unknown ids fall back to the loop's+ /// default model rather than failing the turn.+ pub model: String,+ /// The full conversation, oldest first, ending with the prompt being+ /// answered.+ pub messages: Vec<ChatMessage>,+ /// Cancelling this token stops the turn; the stream ends after the+ /// engine has drained cooperatively.+ pub cancel: CancellationToken,+}++/// Runs one conversational turn and streams its parts back.+///+/// The trait is the testing seam: the ACP surface is exercised against a+/// scripted engine, and production plugs in+/// [`crate::outbound::rig_engine::RigTurnEngine`].+pub trait TurnEngine: Send + Sync + 'static {+ /// Start the turn. Parts arrive on the returned receiver; the stream+ /// ending is the turn ending, and an `Err` item is a turn-fatal failure.+ fn run_turn(&self, request: TurnRequest) -> mpsc::Receiver<Result<StreamPart, AgentError>>;+}diff --git a/crates/agent_inmem/src/domain/mod.rs b/crates/agent_inmem/src/domain/mod.rsnew file mode 100644index 0000000000..c81f34bf3a--- /dev/null+++ b/crates/agent_inmem/src/domain/mod.rs@@ -0,0 +1,7 @@+//! The in-memory agent's domain: the turn engine seam, per-session state, and+//! the ACP agent surface.++pub mod agent;+pub mod engine;+pub mod replay;+pub mod session;diff --git a/crates/agent_inmem/src/domain/replay.rs b/crates/agent_inmem/src/domain/replay.rsnew file mode 100644index 0000000000..5b58e2bb7c--- /dev/null+++ b/crates/agent_inmem/src/domain/replay.rs@@ -0,0 +1,225 @@+//! Rebuilding a session's model-facing conversation from its durable log.+//!+//! The conversation an in-process agent runs its turns from lives in memory+//! ([`SessionState`](crate::domain::session::SessionState)), so a process+//! restart would otherwise start the model's context over even though the+//! frame log still holds the whole session. This module is the other half of+//! [`update_for_part`](crate::domain::agent): it walks the logged frames -+//! which this same agent wrote - and turns them back into the+//! [`HistoryEntry`] list the next turn is built from.+//!+//! Replay is deliberately lenient: a frame it does not recognize contributes+//! nothing rather than failing the attach, because a partially-rebuilt+//! context is strictly better than none. Two shapes are intentionally lossy:+//! thought chunks are skipped (past reasoning is not replayed to the model),+//! and a tool call is rebuilt under the display title it was streamed with,+//! which for the Macro product toolset is the tool name itself.++use agent::types::AssistantMessagePart;+use agent_client_protocol::schema::v1::{+ ContentBlock, PromptRequest, SessionNotification, SessionUpdate, ToolCallStatus,+};+use agent_client_protocol::{JsonRpcMessage, RawJsonRpcMessage, RawJsonRpcParams};+use agent_runtime_protocol::domain::action::COMPACT_COMMAND;+use agent_runtime_protocol::domain::schema::v0::{ToRuntimeMessage, ToServerMessage};+use agent_session::domain::model::Message;+use futures::future::BoxFuture;++use crate::domain::agent::close_dangling_tool_calls;+use crate::domain::session::HistoryEntry;++#[cfg(test)]+mod test;++/// Where a session's durable frames come from when its conversation has to+/// be rebuilt - a cold attach in a process that has never served the session.+///+/// Failures degrade to an empty history (the model's context starts over,+/// which was every restart's behavior before replay existed) rather than+/// failing the attach.+pub trait FrameSource: Send + Sync + 'static {+ /// The session's logged frames, oldest first.+ fn frames(+ &self,+ session: agent_session::domain::model::AgentSessionId,+ ) -> BoxFuture<'_, Vec<Message>>;+}++/// Rebuild the conversation the log's frames recorded, oldest first.+///+/// User prompts open turns and `session/update` notifications fill them in,+/// mirroring what the live agent pushed into its history as the turn ran. A+/// `/compact` prompt drops everything recorded before it, exactly as the live+/// agent's compact handling cleared its history.+#[must_use]+pub fn replay_history(frames: impl IntoIterator<Item = Message>) -> Vec<HistoryEntry> {+ let mut history = Vec::new();+ let mut open: Option<(String, Vec<AssistantMessagePart>)> = None;++ for frame in frames {+ match frame {+ Message::ToRuntime(ToRuntimeMessage::Acp(acp)) => {+ let RawJsonRpcMessage::Request(request) = &acp.0 else {+ continue;+ };+ if !PromptRequest::matches_method(&request.method) {+ continue;+ }+ let Some(prompt) = deserialize_params::<PromptRequest>(request.params.as_ref())+ else {+ continue;+ };+ let text: String = prompt+ .prompt+ .iter()+ .filter_map(|block| match block {+ ContentBlock::Text(text) => Some(text.text.as_str()),+ _ => None,+ })+ .collect();+ close_turn(&mut history, &mut open);+ if text.trim() == COMPACT_COMMAND {+ // Compaction dropped everything before it from the+ // model's context; replaying it back would undo that.+ history.clear();+ } else {+ open = Some((text, Vec::new()));+ }+ }+ Message::ToServer(ToServerMessage::Acp(acp)) => {+ let RawJsonRpcMessage::Notification(notification) = &acp.0 else {+ continue;+ };+ if !SessionNotification::matches_method(¬ification.method) {+ continue;+ }+ let Some(notification) =+ deserialize_params::<SessionNotification>(notification.params.as_ref())+ else {+ continue;+ };+ // An update outside any turn (the compact acknowledgement,+ // status chatter) is presentation, not conversation.+ if let Some((_, parts)) = open.as_mut() {+ apply_update(parts, notification.update);+ }+ }+ _ => {}+ }+ }+ close_turn(&mut history, &mut open);+ history+}++/// Fold one `session/update` back into the open turn's parts.+fn apply_update(parts: &mut Vec<AssistantMessagePart>, update: SessionUpdate) {+ match update {+ SessionUpdate::AgentMessageChunk(chunk) => {+ let ContentBlock::Text(text) = chunk.content else {+ return;+ };+ // Chunks are streamed deltas; coalesce them back into one part.+ if let Some(AssistantMessagePart::Text { text: last }) = parts.last_mut() {+ last.push_str(&text.text);+ } else {+ parts.push(AssistantMessagePart::Text { text: text.text });+ }+ }+ SessionUpdate::ToolCall(call) => {+ parts.push(AssistantMessagePart::ToolCall {+ name: call.title.clone(),+ json: call.raw_input.clone().unwrap_or(serde_json::Value::Null),+ id: call.tool_call_id.0.to_string(),+ });+ }+ SessionUpdate::ToolCallUpdate(update) => {+ let id = update.tool_call_id.0.to_string();+ // Only a call this turn opened and has not answered yet can be+ // closed; anything else would poison the provider payload.+ let Some(name) = unanswered_call_name(parts, &id) else {+ return;+ };+ match update.fields.status {+ Some(ToolCallStatus::Completed) => {+ parts.push(AssistantMessagePart::ToolCallResponseJson {+ name,+ json: update+ .fields+ .raw_output+ .clone()+ .unwrap_or(serde_json::Value::Null),+ id,+ });+ }+ Some(ToolCallStatus::Failed) => {+ let description = update+ .fields+ .raw_output+ .as_ref()+ .and_then(|output| output.get("error"))+ .and_then(|error| error.as_str())+ .unwrap_or("cancelled")+ .to_owned();+ parts.push(AssistantMessagePart::ToolCallErr {+ name,+ description,+ id,+ });+ }+ _ => {}+ }+ }+ // Thought chunks are not replayed to the model, and everything else+ // the protocol can carry is presentation this agent never emits.+ _ => {}+ }+}++/// The name of the turn's tool call `id`, when it exists and has no response+/// yet.+fn unanswered_call_name(parts: &[AssistantMessagePart], id: &str) -> Option<String> {+ let responded = parts.iter().any(|part| match part {+ AssistantMessagePart::ToolCallResponseJson { id: answered, .. }+ | AssistantMessagePart::ToolCallErr { id: answered, .. } => answered == id,+ _ => false,+ });+ if responded {+ return None;+ }+ parts.iter().find_map(|part| match part {+ AssistantMessagePart::ToolCall { name, id: call, .. }+ | AssistantMessagePart::McpToolCall { name, id: call, .. }+ if call == id =>+ {+ Some(name.clone())+ }+ _ => None,+ })+}++/// Push the open turn into the history, closing whatever it left dangling.+fn close_turn(+ history: &mut Vec<HistoryEntry>,+ open: &mut Option<(String, Vec<AssistantMessagePart>)>,+) {+ let Some((prompt, mut parts)) = open.take() else {+ return;+ };+ let _ = close_dangling_tool_calls(&mut parts);+ history.push(HistoryEntry::User(prompt));+ if !parts.is_empty() {+ history.push(HistoryEntry::Assistant(parts));+ }+}++/// Deserialize a frame's params, `None` when the shape does not match.+fn deserialize_params<T: serde::de::DeserializeOwned>(+ params: Option<&RawJsonRpcParams>,+) -> Option<T> {+ match params? {+ RawJsonRpcParams::Object(map) => {+ serde_json::from_value(serde_json::Value::Object(map.clone())).ok()+ }+ RawJsonRpcParams::Array(_) => None,+ }+}diff --git a/crates/agent_inmem/src/domain/replay/test.rs b/crates/agent_inmem/src/domain/replay/test.rsnew file mode 100644index 0000000000..5a8f36921d--- /dev/null+++ b/crates/agent_inmem/src/domain/replay/test.rs@@ -0,0 +1,165 @@+use agent_client_protocol::schema::v1::{+ ContentChunk, SessionId, TextContent, ToolCall as AcpToolCall, ToolCallStatus, ToolCallUpdate,+ ToolCallUpdateFields,+};+use agent_runtime_protocol::domain::schema::v0::AcpMessage;++use super::*;++fn acp_session() -> SessionId {+ SessionId::new("acp-1")+}++/// A logged `session/prompt` request, the shape the harness's deliver path+/// writes.+fn prompt_frame(text: &str) -> Message {+ let request = PromptRequest::new(+ acp_session(),+ vec![ContentBlock::Text(TextContent::new(text))],+ );+ let raw: RawJsonRpcMessage = serde_json::from_value(serde_json::json!({+ "jsonrpc": "2.0",+ "id": 1,+ "method": "session/prompt",+ "params": serde_json::to_value(request).expect("a prompt should serialize"),+ }))+ .expect("a request frame should deserialize");+ Message::ToRuntime(ToRuntimeMessage::Acp(AcpMessage(raw)))+}++/// A logged `session/update` notification, the shape the agent streams.+fn update_frame(update: SessionUpdate) -> Message {+ let notification = SessionNotification::new(acp_session(), update);+ let raw: RawJsonRpcMessage = serde_json::from_value(serde_json::json!({+ "jsonrpc": "2.0",+ "method": "session/update",+ "params": serde_json::to_value(notification).expect("an update should serialize"),+ }))+ .expect("a notification frame should deserialize");+ Message::ToServer(ToServerMessage::Acp(AcpMessage(raw)))+}++fn message_chunk(text: &str) -> SessionUpdate {+ SessionUpdate::AgentMessageChunk(ContentChunk::new(ContentBlock::Text(TextContent::new(+ text,+ ))))+}++#[test]+fn a_logged_turn_replays_as_the_history_the_live_agent_recorded() {+ let history = replay_history(vec![+ prompt_frame("find the roadmap"),+ update_frame(SessionUpdate::AgentThoughtChunk(ContentChunk::new(+ ContentBlock::Text(TextContent::new("hmm")),+ ))),+ update_frame(message_chunk("Hel")),+ update_frame(message_chunk("lo ")),+ update_frame(SessionUpdate::ToolCall(+ AcpToolCall::new("call-1", "NameSearch")+ .status(ToolCallStatus::InProgress)+ .raw_input(serde_json::json!({"query": "roadmap"})),+ )),+ update_frame(SessionUpdate::ToolCallUpdate(ToolCallUpdate::new(+ "call-1",+ ToolCallUpdateFields::new()+ .status(ToolCallStatus::Completed)+ .raw_output(serde_json::json!({"hits": 1})),+ ))),+ update_frame(message_chunk("done")),+ prompt_frame("thanks"),+ update_frame(message_chunk("anytime")),+ ]);++ let [+ HistoryEntry::User(first),+ HistoryEntry::Assistant(first_parts),+ HistoryEntry::User(second),+ HistoryEntry::Assistant(second_parts),+ ] = history.as_slice()+ else {+ panic!("two full turns should replay, got {history:#?}");+ };+ assert_eq!(first, "find the roadmap");+ assert_eq!(second, "thanks");+ assert_eq!(+ second_parts.as_slice(),+ [AssistantMessagePart::Text {+ text: "anytime".to_owned()+ }]+ );++ // Chunks coalesce, the thought is dropped, and the tool call round-trips+ // with its response.+ match first_parts.as_slice() {+ [+ AssistantMessagePart::Text { text },+ AssistantMessagePart::ToolCall { name, id, .. },+ AssistantMessagePart::ToolCallResponseJson {+ json,+ id: response_id,+ ..+ },+ AssistantMessagePart::Text { text: tail },+ ] => {+ assert_eq!(text, "Hello ");+ assert_eq!(name, "NameSearch");+ assert_eq!(id, "call-1");+ assert_eq!(response_id, "call-1");+ assert_eq!(json, &serde_json::json!({"hits": 1}));+ assert_eq!(tail, "done");+ }+ parts => panic!("unexpected first-turn parts: {parts:#?}"),+ }+}++#[test]+fn a_compact_prompt_drops_everything_recorded_before_it() {+ let history = replay_history(vec![+ prompt_frame("remember this"),+ update_frame(message_chunk("noted")),+ prompt_frame("/compact"),+ // The live agent acknowledges compaction outside any turn; the+ // acknowledgement must not replay as conversation.+ update_frame(message_chunk("Compacted.")),+ prompt_frame("after"),+ update_frame(message_chunk("fresh")),+ ]);++ let [HistoryEntry::User(prompt), HistoryEntry::Assistant(parts)] = history.as_slice() else {+ panic!("only the post-compact turn should replay, got {history:#?}");+ };+ assert_eq!(prompt, "after");+ assert_eq!(+ parts.as_slice(),+ [AssistantMessagePart::Text {+ text: "fresh".to_owned()+ }]+ );+}++#[test]+fn a_call_the_log_never_answered_is_closed_rather_than_left_dangling() {+ let history = replay_history(vec![+ prompt_frame("run something"),+ update_frame(SessionUpdate::ToolCall(+ AcpToolCall::new("call-9", "BashCodeExecution").status(ToolCallStatus::InProgress),+ )),+ ]);++ let [HistoryEntry::User(_), HistoryEntry::Assistant(parts)] = history.as_slice() else {+ panic!("the interrupted turn should still replay, got {history:#?}");+ };+ assert!(+ parts.iter().any(|part| matches!(+ part,+ AssistantMessagePart::ToolCallErr { id, description, .. }+ if id == "call-9" && description == "cancelled"+ )),+ "the dangling call must be closed: {parts:#?}"+ );+}++#[test]+fn an_empty_log_replays_to_an_empty_conversation() {+ assert!(replay_history(Vec::new()).is_empty());+}diff --git a/crates/agent_inmem/src/domain/session.rs b/crates/agent_inmem/src/domain/session.rsnew file mode 100644index 0000000000..b465b839a1--- /dev/null+++ b/crates/agent_inmem/src/domain/session.rs@@ -0,0 +1,76 @@+//! Per-session conversational state, held in memory.+//!+//! The durable record of a session is its frame log; what lives here is only+//! the model-facing conversation the next turn is built from. It survives a+//! reattach within one process lifetime, and a cold attach after a restart+//! rebuilds it from the frame log (see [`crate::domain::replay`]).++use agent::types::{AssistantMessagePart, ChatMessage, ChatMessageContent, Role};+use agent_client_protocol::schema::v1::SessionId;+use agent_session::domain::model::AgentSessionId;+use dashmap::DashMap;++/// One entry of the conversation, in the shape+/// [`agent::to_rig_messages`] round-trips.+#[derive(Debug, Clone)]+pub enum HistoryEntry {+ /// A prompt from a user.+ User(String),+ /// One assistant turn: text, tool calls, and tool results, flattened.+ Assistant(Vec<AssistantMessagePart>),+}++/// The in-memory state of one agent session.+#[derive(Debug)]+pub struct SessionState {+ /// The ACP session id minted by `session/new`, `None` until then.+ pub acp_session_id: Option<SessionId>,+ /// Model id turns run on; `session/set_config_option` moves it.+ pub model: String,+ /// The conversation so far, oldest first.+ pub history: Vec<HistoryEntry>,+}++impl SessionState {+ /// A fresh session on `model` with no conversation yet.+ #[must_use]+ pub fn new(model: String) -> Self {+ Self {+ acp_session_id: None,+ model,+ history: Vec::new(),+ }+ }+}++/// Session state by Macro session id, shared between the manager (which+/// creates and tears down entries) and the agent tasks (which read and extend+/// them). Entries outlive individual agent tasks so a reattach keeps its+/// conversation.+pub type SessionStore = DashMap<AgentSessionId, SessionState>;++/// Materialize the conversation for one turn: the recorded history followed+/// by the prompt being answered.+#[must_use]+pub fn messages_for_turn(history: &[HistoryEntry], prompt: &str) -> Vec<ChatMessage> {+ history+ .iter()+ .map(|entry| match entry {+ HistoryEntry::User(text) => ChatMessage {+ content: ChatMessageContent::Text(text.clone()),+ role: Role::User,+ attachments: None,+ },+ HistoryEntry::Assistant(parts) => ChatMessage {+ content: ChatMessageContent::AssistantMessageParts(parts.clone()),+ role: Role::Assistant,+ attachments: None,+ },+ })+ .chain(std::iter::once(ChatMessage {+ content: ChatMessageContent::Text(prompt.to_owned()),+ role: Role::User,+ attachments: None,+ }))+ .collect()+}diff --git a/crates/agent_inmem/src/lib.rs b/crates/agent_inmem/src/lib.rsnew file mode 100644index 0000000000..9d7f0183e1--- /dev/null+++ b/crates/agent_inmem/src/lib.rs@@ -0,0 +1,25 @@+//! The in-memory agent runtime: an ACP agent served in-process.+//!+//! The sandboxed harness provisions a container per session and speaks ACP to+//! it over a WebSocket; this crate is the other runtime flavor. It serves the+//! agent side of ACP on an in-process channel, so a session starts in+//! milliseconds and its transport never leaves the process. The agentic loop+//! is the same one the rest of the product uses ([`agent::AgentLoop`] over the+//! Macro toolset), adapted to ACP: prompts run turns, stream parts become+//! `session/update` notifications, and the existing session log, fold, and UI+//! consume them unchanged.+//!+//! The seams:+//!+//! - [`domain::engine::TurnEngine`] runs one conversational turn and streams+//! [`agent::StreamPart`]s back; [`outbound::rig_engine::RigTurnEngine`] is+//! the production implementation.+//! - [`outbound::manager::InMemAgentManager`] provisions per-session agent+//! tasks and hands the harness the [`agent_runtime_protocol`] transport it+//! expects from any container manager.+#![deny(missing_docs)]++pub mod domain;+pub mod outbound;+#[cfg(test)]+pub(crate) mod testing;diff --git a/crates/agent_inmem/src/outbound/log_frames.rs b/crates/agent_inmem/src/outbound/log_frames.rsnew file mode 100644index 0000000000..b58ba9fcd6--- /dev/null+++ b/crates/agent_inmem/src/outbound/log_frames.rs@@ -0,0 +1,47 @@+//! The production [`FrameSource`]: the session's durable Postgres log.++use agent_session::domain::model::{AgentSessionId, Message};+use agent_session::domain::ports::AgentSessionLogRepo;+use futures::future::BoxFuture;++use crate::domain::replay::FrameSource;++/// [`FrameSource`] over an [`AgentSessionLogRepo`] - the same log every+/// frame of the session was recorded into.+pub struct LogFrameSource<Repo> {+ repo: Repo,+}++impl<Repo> LogFrameSource<Repo> {+ /// Read frames from `repo`.+ #[must_use]+ pub fn new(repo: Repo) -> Self {+ Self { repo }+ }+}++impl<Repo> FrameSource for LogFrameSource<Repo>+where+ Repo: AgentSessionLogRepo,+{+ fn frames(&self, session: AgentSessionId) -> BoxFuture<'_, Vec<Message>> {+ Box::pin(async move {+ match self.repo.list_by_session(session).await {+ Ok(entries) => entries+ .into_iter()+ .map(|stored| stored.entry.content)+ .collect(),+ // Degrade to the pre-replay behavior - the model's context+ // starts over - rather than failing the attach.+ Err(error) => {+ tracing::warn!(+ error = ?error,+ %session,+ "failed to load the session log; the in-memory agent starts with no model context"+ );+ Vec::new()+ }+ }+ })+ }+}diff --git a/crates/agent_inmem/src/outbound/manager.rs b/crates/agent_inmem/src/outbound/manager.rsnew file mode 100644index 0000000000..0e101a032f--- /dev/null+++ b/crates/agent_inmem/src/outbound/manager.rs@@ -0,0 +1,136 @@+//! Provisioning for in-process agent "containers".+//!+//! The sandbox flavor of this is a Daytona API call and a WebSocket; here it+//! is a duplex channel and a Tokio task, which is what makes a session start+//! in milliseconds. The manager owns the map from session to live agent task+//! and the conversation store those tasks read.++use std::sync::Arc;++use agent_client_protocol::schema::v1::SessionId;+use agent_runtime_protocol::domain::channel::Channel;+use agent_runtime_protocol::domain::connection::{RuntimeConnection, ServerChannel};+use agent_runtime_protocol::domain::schema::v0::SystemEvent;+use agent_session::domain::model::AgentSessionId;+use dashmap::DashMap;+use macro_user_id::user_id::MacroUserIdStr;++use crate::domain::agent::{AgentState, serve};+use crate::domain::engine::TurnEngine;+use crate::domain::replay::{FrameSource, replay_history};+use crate::domain::session::{SessionState, SessionStore};++#[cfg(test)]+mod test;++/// The session-row facts an agent task runs from.+#[derive(Debug, Clone)]+pub struct SessionFacts {+ /// The session the agent serves.+ pub id: AgentSessionId,+ /// The session's owner; turns act on their behalf.+ pub owner: MacroUserIdStr<'static>,+ /// Model id stamped on the session row.+ pub model: String,+ /// The ACP session id the row carries, when one was ever negotiated. A+ /// cold attach hydrates its rebuilt conversation under this id so the+ /// harness's `session/resume` keeps it.+ pub acp_session_id: Option<SessionId>,+}++/// One live agent task and its runtime connection.+///+/// Dropping this ends the agent: the connection's driver aborts with it, and+/// the serve task is aborted explicitly.+struct LiveAgent {+ /// Held for its `Drop`: it aborts the connection driver.+ _runtime: RuntimeConnection,+ task: tokio::task::AbortHandle,+}++impl Drop for LiveAgent {+ fn drop(&mut self) {+ self.task.abort();+ }+}++/// Provisions and tears down in-process agents, one per session.+pub struct InMemAgentManager {+ engine: Arc<dyn TurnEngine>,+ frames: Arc<dyn FrameSource>,+ store: Arc<SessionStore>,+ live: DashMap<AgentSessionId, LiveAgent>,+}++impl InMemAgentManager {+ /// A manager running every session's turns through `engine`, rebuilding+ /// cold sessions' conversations from `frames`.+ #[must_use]+ pub fn new(engine: Arc<dyn TurnEngine>, frames: Arc<dyn FrameSource>) -> Self {+ Self {+ engine,+ frames,+ store: Arc::new(SessionStore::new()),+ live: DashMap::new(),+ }+ }++ /// Start (or restart) the session's agent and return the transport the+ /// harness attaches. Spawn and resume are the same operation here: the+ /// conversation store is what persists across reattachment, not the+ /// task - and a session this process has never held (a fresh spawn, or a+ /// resume after a restart) gets its conversation rebuilt from the durable+ /// frame log first.+ #[must_use]+ pub async fn attach(&self, facts: SessionFacts) -> ServerChannel {+ // A replaced agent must die before its successor serves the session.+ self.live.remove(&facts.id);+ if !self.store.contains_key(&facts.id) {+ // Loaded before taking the entry so the read never blocks the+ // map; `or_insert_with` still wins any race to create it.+ let history = replay_history(self.frames.frames(facts.id).await);+ self.store.entry(facts.id).or_insert_with(|| SessionState {+ acp_session_id: facts.acp_session_id.clone(),+ model: facts.model.clone(),+ history,+ });+ }++ let (server_half, runtime_half) = Channel::duplex();+ let (runtime, acp) = RuntimeConnection::connect(runtime_half);+ let state = Arc::new(AgentState {+ session_id: facts.id,+ owner: facts.owner,+ engine: Arc::clone(&self.engine),+ store: Arc::clone(&self.store),+ active_cancel: std::sync::Mutex::new(Vec::new()),+ turn_lock: tokio::sync::Mutex::new(()),+ });+ let session_id = facts.id;+ let task = tokio::spawn(async move {+ if let Err(error) = serve(state, acp).await {+ tracing::warn!(error = ?error, %session_id, "in-process agent stopped on an error");+ }+ });++ // Queued before the harness can possibly read, so the handshake+ // trigger is always the first thing it sees - same contract as the+ // sidecar transport.+ let _ = runtime.system_event(SystemEvent::AcpReady);+ self.live.insert(+ facts.id,+ LiveAgent {+ _runtime: runtime,+ task: task.abort_handle(),+ },+ );+ server_half+ }++ /// End the session for good: kill its agent task and drop its+ /// conversation.+ pub fn teardown(&self, session: AgentSessionId) {+ self.live.remove(&session);+ self.store.remove(&session);+ }+}diff --git a/crates/agent_inmem/src/outbound/manager/test.rs b/crates/agent_inmem/src/outbound/manager/test.rsnew file mode 100644index 0000000000..5785ad9cd2--- /dev/null+++ b/crates/agent_inmem/src/outbound/manager/test.rs@@ -0,0 +1,324 @@+//! End to end over the real session service: the manager's transport is+//! attached the way the harness attaches any container, and the machine's+//! whole handshake plus one prompt runs against the in-process agent.++use std::sync::Arc;+use std::time::Duration;++use agent::StreamPart;+use agent_client_protocol::RawJsonRpcMessage;+use agent_fold::domain::log::Message;+use agent_fold::domain::service::FoldedMessageService;+use agent_runtime_protocol::domain::action::{AgentAction, AgentActionId};+use agent_session::domain::connection::RuntimeAttachment;+use agent_session::domain::model::{AgentSessionId, CreateAgentSessionParams};+use agent_session::domain::ports::{AgentSessionLogRepo as _, NoOpRealtime};+use agent_session::domain::service::{AgentSessionService as _, AgentSessionServiceImpl};+use agent_session::testing::InMemoryAgentSessionRepo;+use bot_id::BotId;+use macro_user_id::user_id::MacroUserIdStr;++use super::*;+use crate::outbound::log_frames::LogFrameSource;+use crate::testing::ScriptedEngine;++fn owner() -> MacroUserIdStr<'static> {+ MacroUserIdStr::try_from_email("[email protected]").expect("a valid user id")+}++fn manager(repo: &InMemoryAgentSessionRepo, engine: Arc<ScriptedEngine>) -> InMemAgentManager {+ InMemAgentManager::new(engine, Arc::new(LogFrameSource::new(repo.clone())))+}++fn facts(id: AgentSessionId) -> SessionFacts {+ SessionFacts {+ id,+ owner: owner(),+ model: "test-model".to_owned(),+ acp_session_id: None,+ }+}++/// Frames the log holds, flattened to JSON strings for loose assertions.+async fn logged_frames(repo: &InMemoryAgentSessionRepo, id: AgentSessionId) -> Vec<String> {+ repo.list_by_session(id)+ .await+ .expect("the log should read")+ .into_iter()+ .map(|stored| {+ let frame: &RawJsonRpcMessage = match &stored.entry.content {+ Message::ToServer(message) => match message {+ agent_runtime_protocol::domain::schema::v0::ToServerMessage::Acp(acp) => &acp.0,+ _ => return "system-event".to_owned(),+ },+ Message::ToRuntime(message) => match message {+ agent_runtime_protocol::domain::schema::v0::ToRuntimeMessage::Acp(acp) => {+ &acp.0+ }+ _ => return "to-runtime".to_owned(),+ },+ };+ serde_json::to_string(frame).expect("a frame should serialize")+ })+ .collect()+}++#[tokio::test]+async fn a_prompt_runs_end_to_end_through_the_real_session_machine() {+ let repo = InMemoryAgentSessionRepo::new();+ let sessions = AgentSessionServiceImpl::new(+ repo.clone(),+ FoldedMessageService::new(repo.clone()),+ NoOpRealtime,+ );++ let id = AgentSessionId::new();+ sessions+ .create_session(CreateAgentSessionParams {+ id,+ owner_id: owner(),+ bot_id: BotId::TEST_A,+ thread_id: None,+ originating_message_id: None,+ model: "test-model".to_owned(),+ harness: "macro-inmem".to_owned(),+ repo_url: None,+ workspace: "/workspace".to_owned(),+ sandbox_size: agent_session::domain::model::SandboxSize::Default,+ })+ .await+ .expect("the session row should create");++ let manager = manager(+ &repo,+ Arc::new(ScriptedEngine::new(vec![StreamPart::Content(+ "streamed reply".to_owned(),+ )])),+ );+ let transport = manager.attach(facts(id)).await;+ sessions+ .attach_session(id, RuntimeAttachment::solo(transport))+ .await+ .expect("the transport should attach");+ sessions+ .send_action(+ id,+ Some(owner()),+ AgentAction::prompt("hello agent"),+ AgentActionId::mint(),+ )+ .await+ .expect("the prompt should send");++ // The turn is done once its response frame lands in the log.+ let deadline = tokio::time::Instant::now() + Duration::from_secs(5);+ let frames = loop {+ let frames = logged_frames(&repo, id).await;+ if frames.iter().any(|frame| frame.contains("stopReason")) {+ break frames;+ }+ assert!(+ tokio::time::Instant::now() < deadline,+ "the prompt never completed; log so far: {frames:#?}"+ );+ tokio::time::sleep(Duration::from_millis(20)).await;+ };++ let all = frames.join("\n");+ assert!(all.contains("initialize"), "no initialize in: {all}");+ assert!(all.contains("session/new"), "no session/new in: {all}");+ assert!(+ all.contains("hello agent"),+ "the prompt frame never logged: {all}"+ );+ assert!(+ all.contains("streamed reply"),+ "the streamed update never logged: {all}"+ );++ let session = sessions+ .get_session(id)+ .await+ .expect("the session row should read");+ assert!(+ session.acp_session_id.is_some(),+ "session/new's id should persist for resume"+ );++ // A second attach must resume rather than die: the row now carries an+ // ACP session id, and this agent declares resume support. Re-attaching+ // kills the previous agent, and the machine only accepts the new+ // transport once the old actor has observed its stream end - the same+ // disconnect-then-resume order the harness's deliver path drives.+ let deadline = tokio::time::Instant::now() + Duration::from_secs(5);+ loop {+ let transport = manager.attach(facts(id)).await;+ match sessions+ .attach_session(id, RuntimeAttachment::solo(transport))+ .await+ {+ Ok(()) => break,+ Err(agent_session::domain::error::AgentSessionError::AlreadyConnected(_)) => {+ assert!(+ tokio::time::Instant::now() < deadline,+ "the replaced agent never disconnected"+ );+ tokio::time::sleep(Duration::from_millis(20)).await;+ }+ Err(error) => panic!("the transport should reattach: {error:?}"),+ }+ }+ sessions+ .send_action(+ id,+ Some(owner()),+ AgentAction::prompt("still there?"),+ AgentActionId::mint(),+ )+ .await+ .expect("the prompt should send on the resumed session");++ let deadline = tokio::time::Instant::now() + Duration::from_secs(5);+ loop {+ let frames = logged_frames(&repo, id).await;+ if frames.iter().any(|frame| frame.contains("session/resume")) {+ break;+ }+ assert!(+ tokio::time::Instant::now() < deadline,+ "the reattach never resumed; log so far: {frames:#?}"+ );+ tokio::time::sleep(Duration::from_millis(20)).await;+ }+}++/// A "restart": the second manager shares nothing with the first but the+/// durable repo, the way a new process would. The resumed turn must carry+/// the first turn's conversation, rebuilt from the frame log.+#[tokio::test]+async fn a_restarted_manager_rebuilds_the_conversation_from_the_log() {+ let repo = InMemoryAgentSessionRepo::new();+ let sessions = AgentSessionServiceImpl::new(+ repo.clone(),+ FoldedMessageService::new(repo.clone()),+ NoOpRealtime,+ );++ let id = AgentSessionId::new();+ sessions+ .create_session(CreateAgentSessionParams {+ id,+ owner_id: owner(),+ bot_id: BotId::TEST_A,+ thread_id: None,+ originating_message_id: None,+ model: "test-model".to_owned(),+ harness: "macro-inmem".to_owned(),+ repo_url: None,+ workspace: "/workspace".to_owned(),+ sandbox_size: agent_session::domain::model::SandboxSize::Default,+ })+ .await+ .expect("the session row should create");++ let before = manager(+ &repo,+ Arc::new(ScriptedEngine::new(vec![StreamPart::Content(+ "streamed reply".to_owned(),+ )])),+ );+ let transport = before.attach(facts(id)).await;+ sessions+ .attach_session(id, RuntimeAttachment::solo(transport))+ .await+ .expect("the transport should attach");+ sessions+ .send_action(+ id,+ Some(owner()),+ AgentAction::prompt("hello agent"),+ AgentActionId::mint(),+ )+ .await+ .expect("the prompt should send");++ let deadline = tokio::time::Instant::now() + Duration::from_secs(5);+ loop {+ let frames = logged_frames(&repo, id).await;+ if frames.iter().any(|frame| frame.contains("stopReason")) {+ break;+ }+ assert!(+ tokio::time::Instant::now() < deadline,+ "the first prompt never completed; log so far: {frames:#?}"+ );+ tokio::time::sleep(Duration::from_millis(20)).await;+ }++ // Kill the first manager's agents and conversations, as a restart would.+ drop(before);++ let engine = Arc::new(ScriptedEngine::new(vec![StreamPart::Content(+ "back again".to_owned(),+ )]));+ let after = manager(&repo, Arc::clone(&engine));+ let row = sessions+ .get_session(id)+ .await+ .expect("the session row should read");+ let deadline = tokio::time::Instant::now() + Duration::from_secs(5);+ loop {+ let transport = after+ .attach(SessionFacts {+ acp_session_id: row.acp_session_id.clone(),+ ..facts(id)+ })+ .await;+ match sessions+ .attach_session(id, RuntimeAttachment::solo(transport))+ .await+ {+ Ok(()) => break,+ Err(agent_session::domain::error::AgentSessionError::AlreadyConnected(_)) => {+ assert!(+ tokio::time::Instant::now() < deadline,+ "the dropped agent never disconnected"+ );+ tokio::time::sleep(Duration::from_millis(20)).await;+ }+ Err(error) => panic!("the transport should reattach: {error:?}"),+ }+ }+ sessions+ .send_action(+ id,+ Some(owner()),+ AgentAction::prompt("still there?"),+ AgentActionId::mint(),+ )+ .await+ .expect("the prompt should send on the resumed session");++ let deadline = tokio::time::Instant::now() + Duration::from_secs(5);+ loop {+ if !engine.requests().is_empty() {+ break;+ }+ assert!(+ tokio::time::Instant::now() < deadline,+ "the resumed prompt never reached the engine"+ );+ tokio::time::sleep(Duration::from_millis(20)).await;+ }++ // The rebuilt context: the first turn's prompt and reply, then the new+ // prompt - not just the new prompt.+ assert_eq!(+ engine.requests()[0].1,+ vec![+ "hello agent".to_owned(),+ "streamed reply".to_owned(),+ "still there?".to_owned()+ ]+ );+}diff --git a/crates/agent_inmem/src/outbound/mod.rs b/crates/agent_inmem/src/outbound/mod.rsnew file mode 100644index 0000000000..92522caee0--- /dev/null+++ b/crates/agent_inmem/src/outbound/mod.rs@@ -0,0 +1,6 @@+//! Outbound adapters: the production turn engine and the per-session agent+//! manager.++pub mod log_frames;+pub mod manager;+pub mod rig_engine;diff --git a/crates/agent_inmem/src/outbound/rig_engine.rs b/crates/agent_inmem/src/outbound/rig_engine.rsnew file mode 100644index 0000000000..13f0a63ba9--- /dev/null+++ b/crates/agent_inmem/src/outbound/rig_engine.rs@@ -0,0 +1,144 @@+//! The production turn engine: the shared rig agent loop over the Macro+//! product toolset.+//!+//! Consumption mirrors the scheduled-action executor+//! (`services/scheduled_action/src/outbound/inprocess_executor/agent_task.rs`):+//! the full static toolset, the tool-use system prompt plus the agent-session+//! preamble and the owner's memory, and usage recorded per turn against the+//! session owner.++use std::sync::Arc;++use agent::{AgentError, AgentLoop, StreamPart};+use ai_tools::{ToolServiceContext, ToolSetWithPrompt, all_tools};+use ai_toolset::ToolSet as AiToolSet;+use futures::StreamExt as _;+use macro_user_id::user_id::MacroUserIdStr;+use memory::domain::MemoryService as _;+use memory::domain::service::MemoryServiceImpl;+use memory::outbound::pg_memory_repo::PgMemoryRepo;+use sqlx::PgPool;+use tokio::sync::mpsc;++use crate::domain::engine::{TurnEngine, TurnRequest};++/// How many stream parts may sit unread before the engine pauses; keeps a+/// slow consumer from buffering a whole turn.+const PART_BUFFER: usize = 256;++/// [`TurnEngine`] backed by [`agent::AgentLoop`] and+/// [`ai_tools::all_tools`].+pub struct RigTurnEngine {+ db: PgPool,+ tool_context: ToolServiceContext,+}++impl RigTurnEngine {+ /// An engine whose tools run against `tool_context` and whose user+ /// memory comes from `db`.+ #[must_use]+ pub fn new(db: PgPool, tool_context: ToolServiceContext) -> Self {+ Self { db, tool_context }+ }+}++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 tool_context = self.tool_context.clone();+ tokio::spawn(async move {+ if let Err(error) = drive_turn(db, tool_context, request, &parts).await {+ let _ = parts.send(Err(error)).await;+ }+ });+ receiver+ }+}++async fn drive_turn(+ db: PgPool,+ base_context: ToolServiceContext,+ request: TurnRequest,+ parts: &mpsc::Sender<Result<StreamPart, AgentError>>,+) -> Result<(), AgentError> {+ let TurnRequest {+ owner,+ model,+ messages,+ cancel,+ } = request;++ let tools = all_tools();+ let user_memory = fetch_user_memory(&db, &base_context, &owner).await;+ let system_prompt = match user_memory {+ Some(memory) => format!(+ "{}\n{}\n<user_memory>\n{}\n</user_memory>",+ tools.prompt,+ prompt::agent_session::PROMPT,+ memory+ ),+ None => format!("{}\n{}", tools.prompt, prompt::agent_session::PROMPT),+ };++ let toolset: Arc<dyn AiToolSet<_> + Send + Sync> = tools.toolset;+ let agent_loop = AgentLoop::new(base_context.recorder.clone()).with_model(&model);+ let usage_ctx = ai_usage::UsageContext::new(ai_usage::AiFeature::AgentSession, owner);+ // Carry the feature on the context so tool-spawned subagents attribute to it.+ let mut tool_context = base_context;+ tool_context.usage_context = usage_ctx.clone();+ let session = agent_loop+ .session(toolset, Arc::new(tool_context), &system_prompt, usage_ctx)+ .await;+ let (mut session, loop_cancel) = session.cancellable();++ // Bridge the caller's token onto the loop's own; aborted with the turn so+ // an uncancelled token does not strand the forwarder.+ let forward = tokio::spawn({+ let loop_cancel = loop_cancel.clone();+ let cancel = cancel.clone();+ async move {+ cancel.cancelled().await;+ loop_cancel.cancel();+ }+ });++ let rig_messages = agent::to_rig_messages(&messages);+ let result = async {+ let mut stream = session.send_message(rig_messages).await?;+ while let Some(part) = stream.next().await {+ if parts.send(part).await.is_err() {+ // The consumer is gone; stop the loop rather than keep+ // spending tokens into the void.+ loop_cancel.cancel();+ break;+ }+ }+ Ok(())+ }+ .await;+ forward.abort();+ result+}++/// 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,+ owner: &MacroUserIdStr<'static>,+) -> Option<String> {+ let tools = all_tools();+ let tools = ToolSetWithPrompt {+ toolset: tools.toolset,+ prompt: tools.prompt,+ };+ 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) => {+ tracing::warn!(error=?error, %owner, "failed to fetch user memory; running without it");+ None+ }+ }+}diff --git a/crates/agent_inmem/src/testing.rs b/crates/agent_inmem/src/testing.rsnew file mode 100644index 0000000000..e93ffee5f1--- /dev/null+++ b/crates/agent_inmem/src/testing.rs@@ -0,0 +1,63 @@+//! Test doubles shared by this crate's tests.++use agent::{AgentError, StreamPart};+use tokio::sync::mpsc;++use crate::domain::engine::{TurnEngine, TurnRequest};++/// An engine that plays back a script of parts for every turn.+pub(crate) struct ScriptedEngine {+ script: Vec<StreamPart>,+ /// `(model, message texts)` per turn the engine has been asked to run.+ requests: std::sync::Mutex<Vec<(String, Vec<String>)>>,+}++impl ScriptedEngine {+ pub(crate) fn new(script: Vec<StreamPart>) -> Self {+ Self {+ script,+ requests: std::sync::Mutex::new(Vec::new()),+ }+ }++ pub(crate) fn requests(&self) -> Vec<(String, Vec<String>)> {+ self.requests.lock().expect("requests lock").clone()+ }+}++impl TurnEngine for ScriptedEngine {+ fn run_turn(&self, request: TurnRequest) -> mpsc::Receiver<Result<StreamPart, AgentError>> {+ self.requests.lock().expect("requests lock").push((+ request.model.clone(),+ request+ .messages+ .iter()+ .map(|message| message.content.message_text_with_tools())+ .collect(),+ ));+ let (parts, receiver) = mpsc::channel(64);+ let script = self.script.clone();+ tokio::spawn(async move {+ for part in script {+ if parts.send(Ok(part)).await.is_err() {+ break;+ }+ }+ });+ receiver+ }+}++/// An engine that never produces anything until cancelled.+pub(crate) struct HangingEngine;++impl TurnEngine for HangingEngine {+ fn run_turn(&self, request: TurnRequest) -> mpsc::Receiver<Result<StreamPart, AgentError>> {+ let (parts, receiver) = mpsc::channel(1);+ tokio::spawn(async move {+ request.cancel.cancelled().await;+ drop(parts);+ });+ receiver+ }+}diff --git a/crates/ai_usage/src/domain/ports.rs b/crates/ai_usage/src/domain/ports.rsindex 8608b3707c..3c3d11b175 100644--- a/crates/ai_usage/src/domain/ports.rs+++ b/crates/ai_usage/src/domain/ports.rs@@ -60,6 +60,8 @@ pub enum AiFeature {AiEditing,/// Import pipeline agent sessions (connector gathers + Notion imports).Import,+ /// In-process ACP agent sessions (the in-memory harness).+ AgentSession,}/// Resolved price for one completion.diff --git a/crates/bot_id/src/lib.rs b/crates/bot_id/src/lib.rsindex 1c3377337d..9c710d3dc6 100644--- a/crates/bot_id/src/lib.rs+++ b/crates/bot_id/src/lib.rs@@ -51,6 +51,9 @@ fn bot_id_str(input: &str) -> IResult<&str, BotIdStorage<ArcCowStr<'_>>> {}/// Stable [`BotId`] for the first-party "Macro AI" system bot.+///+/// Mentioning it opens an agent session served by the in-process (in-memory)+/// agent harness, which answers with the Macro product toolset.pub const MACRO_AI_BOT_ID: BotId =BotId::new_from_uuid(Uuid::from_u128(0x0000_0000_0000_0000_0000_0000_0000_a1a1));@@ -69,9 +72,9 @@ pub const MACRO_SYSTEM_NAME: &str = "Macro System";/// Stable [`BotId`] for the "Macro Coder" system bot, our coding-agent harness.///-/// Distinct from [`MACRO_AI_BOT_ID`] on purpose: mentioning Macro AI answers in-/// chat, while mentioning this one opens a sandboxed agent session, and one id-/// cannot mean both.+/// Distinct from [`MACRO_AI_BOT_ID`] on purpose: the Macro bot's sessions run+/// in-process on the in-memory harness, while this one's run in a provisioned+/// sandbox, and one id cannot mean both.pub const MACRO_CODER_BOT_ID: BotId =BotId::new_from_uuid(Uuid::from_u128(0x0000_0000_0000_0000_0000_0000_0000_a9e7));diff --git a/crates/macro_db_client/migrations/20260821174707_seed_macro_ai_agent_bot.sql b/crates/macro_db_client/migrations/20260821174707_seed_macro_ai_agent_bot.sqlnew file mode 100644index 0000000000..0232a62bad--- /dev/null+++ b/crates/macro_db_client/migrations/20260821174707_seed_macro_ai_agent_bot.sql@@ -0,0 +1,10 @@+-- Seed the "Macro" system bot (bot_id::MACRO_AI_BOT_ID) as an agent bot.+--+-- The bot has always been code-defined with no row of its own; agent sessions+-- reference bots(id), so mentioning it can only open a session once it exists+-- here. Its sessions run in-process inside the harness service (no sandbox)+-- and answer with the Macro product toolset, unlike the Macro Coder bot's,+-- which run in a provisioned sandbox.+INSERT INTO bots (id, kind, name, handle, has_agent)+VALUES ('00000000-0000-0000-0000-00000000a1a1', 'system', 'Macro', 'macro', true)+ON CONFLICT (id) DO UPDATE SET has_agent = true;diff --git a/crates/prompt/src/agent_session.rs b/crates/prompt/src/agent_session.rsnew file mode 100644index 0000000000..993a606cb3--- /dev/null+++ b/crates/prompt/src/agent_session.rs@@ -0,0 +1,20 @@+//! Behavior for the in-process Macro agent answering an agent session.++use crate::types::StaticPrompt;++static TITLE: &str = "Agent Sessions";++static INSTRUCTIONS: &str = r##"You are Macro's agent, working inside an agent session that was opened from a channel mention. Your replies stream back into that channel thread.++- Each prompt is a message from a user in the thread. Answer it directly; use your tools to look things up or act in the workspace when that is what the request needs.+- Be concise and directly useful. Respond in Markdown.+- You have no shell and no filesystem. Everything you can do, you do through the tools you are given.+- Work autonomously: nobody can approve intermediate questions mid-turn, so make reasonable assumptions, state them briefly, and proceed.+"##;++static INTENT: &str = "The model behaves as a fast product assistant inside an agent session: \+answers the prompt directly, uses Macro tools rather than expecting a shell, and does not \+stall on questions nobody can answer mid-turn.";++/// The agent-session preamble for the in-process Macro agent.+pub static PROMPT: StaticPrompt<'static> = StaticPrompt::borrowed(TITLE, INSTRUCTIONS, INTENT);diff --git a/crates/prompt/src/lib.rs b/crates/prompt/src/lib.rsindex 143f740284..a3f075d67f 100644--- a/crates/prompt/src/lib.rs+++ b/crates/prompt/src/lib.rs@@ -6,6 +6,7 @@#![deny(missing_docs)]pub mod about_macro;+pub mod agent_session;pub mod channel_mention;pub mod citations;pub mod connected_toolsets;diff --git a/packages/sdk/generated/cognition/types.gen.ts b/packages/sdk/generated/cognition/types.gen.tsindex 4a899662cf..6ad13958c3 100644--- a/packages/sdk/generated/cognition/types.gen.ts+++ b/packages/sdk/generated/cognition/types.gen.ts@@ -27,7 +27,7 @@ export type AddServerRequest = {* Everything we use AI for. The wire / DB form of each variant is its* `snake_case` name.*/-export type AiFeature = 'chat' | 'memory' | 'automation' | 'dynamic_completions_api' | 'chat_rename' | 'call_summary' | 'channel_bot' | 'ai_projection' | 'ai_editing' | 'import';+export type AiFeature = 'chat' | 'memory' | 'automation' | 'dynamic_completions_api' | 'chat_rename' | 'call_summary' | 'channel_bot' | 'ai_projection' | 'ai_editing' | 'import' | 'agent_session';/*** A structured part within an assistant message.diff --git a/packages/sdk/specs/cognition.json b/packages/sdk/specs/cognition.jsonindex 225e6a4fe9..3dcbb6a645 100644--- a/packages/sdk/specs/cognition.json+++ b/packages/sdk/specs/cognition.json@@ -2130,7 +2130,8 @@"channel_bot","ai_projection","ai_editing",- "import"+ "import",+ "agent_session"]},"AssistantMessagePart": {diff --git a/services/agent_harness_service/Cargo.toml b/services/agent_harness_service/Cargo.tomlindex 032061f821..25e1bf8ad3 100644--- a/services/agent_harness_service/Cargo.toml+++ b/services/agent_harness_service/Cargo.toml@@ -15,6 +15,8 @@ path = "src/openapi.rs"[dependencies]agent_fold = { path = "../../crates/agent_fold" }agent_harness = { path = "../../crates/agent_harness" }+agent_inmem = { path = "../../crates/agent_inmem" }+ai_tools = { path = "../../crates/ai_tools" }agent_runtime_protocol = { path = "../../crates/agent_runtime_protocol", features = ["utoipa"] }agent_session = { path = "../../crates/agent_session" }agent_trigger = { path = "../../crates/agent_trigger" }@@ -58,5 +60,6 @@ macro_uuid = { path = "../../crates/macro_uuid" }serde = { workspace = true }sqlx = { workspace = true }tokio = { workspace = true, features = ["macros", "rt-multi-thread", "time"] }+tokio-util = { workspace = true, features = ["rt"] }tracing = { workspace = true }workspace-hack = { version = "0.1", path = "../../crates/workspace-hack" }diff --git a/services/agent_harness_service/justfile b/services/agent_harness_service/justfileindex 6991274e6f..1015c744e4 100644--- a/services/agent_harness_service/justfile+++ b/services/agent_harness_service/justfile@@ -12,6 +12,9 @@ check:# Run against host-exposed local Postgres, Kafka, and connection gateway.# Needs DAYTONA_API_KEY, DAYTONA_SNAPSHOT, and GITHUB_TOKEN in the environment.+#+# Two managed bots: @coder (HARNESS_BOT_ID) runs in a Daytona sandbox, @macro+# (INMEM_BOT_ID) runs on the in-process ACP runtime.run-local:ENVIRONMENT=local \DATABASE_URL=postgres://user:password@localhost:5432/macrodb \@@ -19,6 +22,7 @@ run-local:OVERRIDE_CONNECTION_GATEWAY_URL=http://localhost:8082 \INTERNAL_API_KEY=local \HARNESS_BOT_ID=00000000-0000-0000-0000-00000000a9e7 \+ INMEM_BOT_ID=00000000-0000-0000-0000-00000000a1a1 \cargo run -p agent_harness_service# Print the full agent-session protocol log for a dedicated channel, oldestdiff --git a/services/agent_harness_service/src/config.rs b/services/agent_harness_service/src/config.rsindex 90ae38b965..3bc62f98a1 100644--- a/services/agent_harness_service/src/config.rs+++ b/services/agent_harness_service/src/config.rs@@ -85,6 +85,18 @@ pub struct Config {/// Repository sessions run against, until it becomes per-request data.#[macro_config_default(String::from("https://github.com/macro-inc/macro"))]pub harness_repo_url: String,+ /// The bot whose sessions run in-process (the in-memory agent) - the+ /// Macro bot, `bot_id::MACRO_AI_BOT_ID`, in every real deployment. Unset+ /// means this deployment serves only the sandboxed bot. It must be a real+ /// `bots` row - `agent_session.bot_id` references it.+ pub inmem_bot_id: Option<Uuid>,+ /// Model id stamped onto sessions the in-memory bot opens. Unknown ids+ /// fall back to the agent loop's default model.+ #[macro_config_default(String::from("claude-sonnet-5"))]+ pub inmem_model: String,+ /// Harness slug stamped onto sessions the in-memory bot opens.+ #[macro_config_default(String::from("macro-inmem"))]+ pub inmem_harness_slug: String,/// Key for internal service-to-service calls (the connection gateway).pub internal_api_key: String,/// Port the control routes are served on.diff --git a/services/agent_harness_service/src/containers.rs b/services/agent_harness_service/src/containers.rsnew file mode 100644index 0000000000..8186bcd8b4--- /dev/null+++ b/services/agent_harness_service/src/containers.rs@@ -0,0 +1,193 @@+//! Routes container provisioning between the runtimes this deployment hosts.+//!+//! One deployment can manage two bots: a sandboxed bot, whose sessions come+//! from whichever provider [`HarnessContainers`] holds (Daytona, or local+//! Docker when a developer opts in), and the in-memory agent bot, whose+//! sessions run in-process. The session row's `bot_id` - written before any+//! container is asked for - decides which runtime a session's transport comes+//! from.++use agent_harness::domain::error::{HarnessError, Result};+use agent_harness::domain::model::SpawnContainer;+use agent_harness::domain::ports::ContainerManager;+use agent_harness::domain::sandbox::SandboxResizeEffect;+use agent_harness::outbound::containers::{HarnessContainer, HarnessContainers};+use agent_harness::outbound::sidecar::SidecarSender;+use agent_inmem::outbound::manager::{InMemAgentManager, SessionFacts};+use agent_runtime_protocol::domain::connection::ServerChannel;+use agent_runtime_protocol::domain::ports::{Transport, TransportError, TransportSender};+use agent_runtime_protocol::domain::schema::v0::{ToRuntimeMessage, ToServerMessage};+use agent_session::domain::model::{AgentSessionId, SandboxSize};+use agent_session::domain::service::AgentSessionService;+use bot_id::BotId;++/// A session transport from whichever runtime the session's bot uses.+pub enum RoutedTransport {+ /// A sandboxed session's sidecar-backed transport.+ Sandbox(HarnessContainer),+ /// An in-process session's channel.+ InMem(ServerChannel),+}++/// The sending half of a [`RoutedTransport`].+pub enum RoutedSender {+ /// Sends into the sandbox sidecar socket.+ Sandbox(SidecarSender),+ /// Sends into the in-process agent.+ InMem(tokio::sync::mpsc::UnboundedSender<ToRuntimeMessage>),+}++impl Transport<ToRuntimeMessage, ToServerMessage> for RoutedTransport {+ type Sender = RoutedSender;+ type Receiver = tokio::sync::mpsc::UnboundedReceiver<ToServerMessage>;++ fn split(self) -> (Self::Sender, Self::Receiver) {+ match self {+ Self::Sandbox(transport) => {+ let (sender, receiver) = transport.split();+ (RoutedSender::Sandbox(sender), receiver)+ }+ Self::InMem(channel) => {+ let (sender, receiver) = channel.split();+ (RoutedSender::InMem(sender), receiver)+ }+ }+ }+}++impl TransportSender<ToRuntimeMessage> for RoutedSender {+ async fn send(&self, message: ToRuntimeMessage) -> std::result::Result<(), TransportError> {+ match self {+ Self::Sandbox(sender) => sender.send(message).await,+ Self::InMem(sender) => TransportSender::send(sender, message).await,+ }+ }+}++/// The in-memory runtime and the bot whose sessions it serves.+pub struct InMemRuntime {+ /// Sessions of this bot run in-process.+ pub bot: BotId,+ /// Their provisioner.+ pub manager: InMemAgentManager,+}++/// The deployment's [`ContainerManager`]: the sandbox provider for the+/// sandboxed bot, in-process for the in-memory bot when one is configured.+pub struct RoutedContainers<Sessions> {+ sandbox: HarnessContainers,+ inmem: Option<InMemRuntime>,+ sessions: Sessions,+}++enum Route {+ Sandbox,+ InMem(SessionFacts),+}++impl<Sessions> RoutedContainers<Sessions>+where+ Sessions: AgentSessionService,+{+ /// Route by bot: `sessions` is where a session's bot is looked up.+ pub fn new(+ sandbox: HarnessContainers,+ inmem: Option<InMemRuntime>,+ sessions: Sessions,+ ) -> Self {+ Self {+ sandbox,+ inmem,+ sessions,+ }+ }++ /// Which runtime serves `session`. The row exists before any container+ /// operation - `open` creates it first - so the lookup is authoritative.+ async fn route(&self, session: AgentSessionId) -> Result<Route> {+ let Some(inmem) = &self.inmem else {+ return Ok(Route::Sandbox);+ };+ let row = self+ .sessions+ .get_session(session)+ .await+ .map_err(HarnessError::Session)?;+ if row.bot_id == inmem.bot {+ Ok(Route::InMem(SessionFacts {+ id: session,+ owner: row.owner_id,+ model: row.model,+ acp_session_id: row.acp_session_id,+ }))+ } else {+ Ok(Route::Sandbox)+ }+ }++ fn inmem(&self) -> &InMemRuntime {+ self.inmem+ .as_ref()+ .expect("an in-memory route exists only when the runtime is configured")+ }+}++impl<Sessions> ContainerManager for RoutedContainers<Sessions>+where+ Sessions: AgentSessionService,+{+ type Transport = RoutedTransport;++ async fn spawn(&self, command: SpawnContainer) -> Result<Self::Transport> {+ match self.route(command.session_id).await? {+ Route::InMem(facts) => Ok(RoutedTransport::InMem(+ self.inmem().manager.attach(facts).await,+ )),+ Route::Sandbox => self+ .sandbox+ .spawn(command)+ .await+ .map(RoutedTransport::Sandbox),+ }+ }++ /// Delegated to the sandbox provider: the signature carries no session,+ /// so there is no bot to route on. An in-process session has no sandbox,+ /// so the worst this costs is a needless close-and-resume before a+ /// [`Self::resize`] that does nothing.+ fn resize_effect(&self, from: SandboxSize, to: SandboxSize) -> SandboxResizeEffect {+ self.sandbox.resize_effect(from, to)+ }++ /// A no-op for in-process sessions - there is no container whose compute+ /// could change - and the provider's business for everyone else.+ async fn resize(&self, session: AgentSessionId, size: SandboxSize) -> Result<()> {+ match self.route(session).await? {+ Route::InMem(_) => Ok(()),+ Route::Sandbox => self.sandbox.resize(session, size).await,+ }+ }++ async fn resume(&self, session: AgentSessionId) -> Result<Self::Transport> {+ match self.route(session).await? {+ Route::InMem(facts) => Ok(RoutedTransport::InMem(+ self.inmem().manager.attach(facts).await,+ )),+ Route::Sandbox => self+ .sandbox+ .resume(session)+ .await+ .map(RoutedTransport::Sandbox),+ }+ }++ async fn teardown(&self, session: AgentSessionId) -> Result<()> {+ match self.route(session).await? {+ Route::InMem(_) => {+ self.inmem().manager.teardown(session);+ Ok(())+ }+ Route::Sandbox => self.sandbox.teardown(session).await,+ }+ }+}diff --git a/services/agent_harness_service/src/main.rs b/services/agent_harness_service/src/main.rsindex e923492140..f459220e24 100644--- a/services/agent_harness_service/src/main.rs+++ b/services/agent_harness_service/src/main.rs@@ -1,3 +1,4 @@+#![recursion_limit = "256"]//! Composition root for the agent harness service.//!//! The hexagon lives in `crates/agent_harness`; this binary is the shell@@ -9,12 +10,13 @@mod api;mod bots_directory;mod config;+mod containers;mod trigger;use std::{future::Future, pin::Pin, sync::Arc};use agent_fold::domain::service::FoldedMessageService;-use agent_harness::domain::model::{HarnessCommand, SessionDefaults};+use agent_harness::domain::model::{HarnessCommand, HarnessDefaults, SessionDefaults};use agent_harness::domain::service::AgentHarnessService;use agent_harness::inbound::kafka::{RoutedTrigger, route_agent_trigger};use agent_harness::inbound::runtime_gateway::RuntimeGatewayState;@@ -26,6 +28,10 @@ use agent_harness::outbound::daytona::{};use agent_harness::outbound::local::{LocalContainerManager, LocalSettings};use agent_harness::outbound::runtime_registry::RuntimeRegistry;+use agent_inmem::outbound::log_frames::LogFrameSource;+use agent_inmem::outbound::manager::InMemAgentManager;+use agent_inmem::outbound::rig_engine::RigTurnEngine;+use agent_session::domain::ports::NoOpRealtime;use agent_session::domain::service::AgentSessionServiceImpl;use agent_session::inbound::axum_router::{AgentSessionControlState, AgentSessionRouterState, CreateSessionState,@@ -47,6 +53,7 @@ use channels::outbound::pg_channels_repo::PgChannelsRepo;use channels::outbound::pg_side_effect_context::PgChannelSideEffectContext;use config::{Config, Environment};use connection_gateway_client::ConnectionGatewayClient;+use containers::{InMemRuntime, RoutedContainers};use kafka_util::{GroupName, KafkaEventConsumer, consumer_span, record_span_error};use lexical_client::LexicalClient;use macro_auth::middleware::decode_jwt::JwtValidationArgs;@@ -110,6 +117,8 @@ async fn run() -> anyhow::Result<()> {agent_harness::install_tls_provider();let config = Config::from_env()?;let bot_id = BotId::new_from_uuid(config.harness_bot_id);+ let inmem_bot = config.inmem_bot_id.map(BotId::new_from_uuid);+ let our_bots: Vec<BotId> = std::iter::once(bot_id).chain(inmem_bot).collect();let pool = PgPoolOptions::new().min_connections(1)@@ -154,7 +163,7 @@ async fn run() -> anyhow::Result<()> {);}let anthropic_api_key = AnthropicApiKeySecret::new(config.anthropic_api_key.clone());- let containers = if config.dev_dangerous_local_containers {+ let sandbox = if config.dev_dangerous_local_containers {if !matches!(config.environment, Environment::Local) {anyhow::bail!("DEV_DANGEROUS_LOCAL_CONTAINERS is only allowed when ENVIRONMENT=local");}@@ -192,7 +201,41 @@ async fn run() -> anyhow::Result<()> {anthropic_api_key,}))};- let container_shutdown = containers.clone();+ let container_shutdown = sandbox.clone();++ // Tracks event publishes the in-memory agent's tool context starts;+ // closed and drained on shutdown so nothing is dropped mid-publish.+ let event_broker_tracker = tokio_util::task::TaskTracker::new();+ let inmem = match inmem_bot {+ Some(bot) => {+ let tool_context = ai_tools::build_tool_service_context_from_env(+ pool.clone(),+ event_broker_tracker.clone(),+ )+ .await+ .context("failed to build the in-memory agent tool context")?;+ let engine = Arc::new(RigTurnEngine::new(pool.clone(), tool_context));+ // Cold attaches (fresh spawns and post-restart resumes) rebuild+ // their model context from the same log every frame lands in.+ let frames = Arc::new(LogFrameSource::new(session_repo.clone()));+ Some(InMemRuntime {+ bot,+ manager: InMemAgentManager::new(engine, frames),+ })+ }+ None => None,+ };+ // The sandbox provider serves every bot but the in-memory one, which the+ // router pulls out by bot id before the provider ever sees it.+ let containers = RoutedContainers::new(+ sandbox,+ inmem,+ AgentSessionServiceImpl::new(+ session_repo.clone(),+ FoldedMessageService::new(session_repo.clone()),+ NoOpRealtime,+ ),+ );let aws_config = macro_aws_config::get_macro_aws_config().await;let notifications = Arc::new(notification::domain::service::SqsNotificationIngress {@@ -236,17 +279,35 @@ async fn run() -> anyhow::Result<()> {// here because the gateway puts dialed-in sockets into it and the harness// takes sessions out of it.let runtimes = RuntimeRegistry::new();+ let mut defaults = HarnessDefaults::new(SessionDefaults {+ bot_id,+ model: config.harness_model.clone(),+ harness: config.harness_slug.clone(),+ repo_url: config.harness_repo_url.clone(),+ });+ if let Some(bot) = inmem_bot {+ defaults = defaults+ .with_bot(+ bot,+ SessionDefaults {+ bot_id: bot,+ model: config.inmem_model.clone(),+ harness: config.inmem_harness_slug.clone(),+ // Stamped but unused: the in-process agent has no+ // workspace to clone anything into.+ repo_url: config.harness_repo_url.clone(),+ },+ )+ // Sessions nothing names a bot for (the create menu's) run+ // in-process too; only mentioning the coder bot gets a sandbox.+ .with_managed_bot(bot);+ }let harness = Arc::new(AgentHarnessService::new(sessions,containers,announcer,Arc::clone(&runtimes),- SessionDefaults {- bot_id,- model: config.harness_model.clone(),- harness: config.harness_slug.clone(),- repo_url: config.harness_repo_url.clone(),- },+ defaults,));// The complete session API is served from this process because it owns the@@ -383,7 +444,7 @@ async fn run() -> anyhow::Result<()> {tracing::Span::current().record("macro.event.id", tracing::field::display(event.event().event_id));- let routed = match route_agent_trigger(event.event().event.clone(), bot_id) {+ let routed = match route_agent_trigger(event.event().event.clone(), &our_bots) {Ok(routed) => routed,Err(skipped) => {tracing::debug!(?skipped, "skipped an agent session event");@@ -495,6 +556,17 @@ async fn run() -> anyhow::Result<()> {}}+ event_broker_tracker.close();+ if tokio::time::timeout(+ std::time::Duration::from_secs(10),+ event_broker_tracker.wait(),+ )+ .await+ .is_err()+ {+ tracing::warn!("timed out draining in-memory agent event publishes");+ }+match run_error {Some(error) => Err(error),None => Ok(()),diff --git a/services/document_storage_service/Cargo.toml b/services/document_storage_service/Cargo.tomlindex b839cb5ea9..51d839e62a 100644--- a/services/document_storage_service/Cargo.toml+++ b/services/document_storage_service/Cargo.toml@@ -56,7 +56,6 @@ bzip2 = { workspace = true, default-features = false, features = [] }cal = { path = "../../crates/cal", features = ["axum", "outbound"] }call = { path = "../../crates/call" }-channel_bots = { path = "../../crates/channel_bots" }channels = { path = "../../crates/channels", features = ["entity_mutation","inbound",diff --git a/services/document_storage_service/src/main.rs b/services/document_storage_service/src/main.rsindex 3047729f08..0d71762f69 100644--- a/services/document_storage_service/src/main.rs+++ b/services/document_storage_service/src/main.rs@@ -892,7 +892,6 @@ async fn run() -> anyhow::Result<()> {Arc::new(PgTaskMatchRepo::new(db.clone())),));let channels_repo = PgChannelsRepo::new(db.clone());- let (bot_trigger_sender, bot_trigger_receiver) = tokio::sync::mpsc::unbounded_channel();let channel_side_effects = ChannelSideEffectService::new(PgChannelSideEffectContext::new(db.clone()),@@ -900,13 +899,12 @@ async fn run() -> anyhow::Result<()> {NotificationChannelSender::new(notification_ingress_service.clone()),ContactsChannelDispatcher::new(contacts_ingress.clone()),)- .with_bot_trigger_sender(bot_trigger_sender).with_macro_event_broker(macro_event_broker.clone());let channels_service = Arc::new(ChannelServiceImpl::with_dependencies(channels_repo,- SpawnedChannelEventDispatcher::new(channel_side_effects.clone()),+ SpawnedChannelEventDispatcher::new(channel_side_effects),PgChannelReferenceSharePermissions::new(db.clone(), entity_access_service.clone()),).with_mention_extractor(lexical_mention_extractor::LexicalMentionExtractor::new(@@ -955,40 +953,9 @@ async fn run() -> anyhow::Result<()> {}});- // Wire Macro AI to react to mentions. The router posts replies through the- // channel service we just built and runs the agent loop in-process with the- // same pre-configured toolset used by other AI hosts.- let mut macro_agent_tool_context =- ai_tools::build_tool_service_context_from_env(db.clone(), event_broker_tracker.clone())- .await- .context("failed to build Macro agent tool context")?;- // Wire the agent's SendChannelMessage tool to this service's own- // side-effect pipeline so agent-posted messages share the exact instance- // used by the HTTP API, including the in-process bot trigger sender (the- // env builder wires an equivalent pipeline, but without bot triggers).- macro_agent_tool_context.channel_tool_context =- ai_tools::build_channel_tool_context_with_dispatcher(- db.clone(),- std::sync::Arc::new(SpawnedChannelEventDispatcher::new(channel_side_effects)),- lexical_client.clone(),- );- let macro_agent_tools = ai_tools::all_tools();- let bot_trigger_router = channel_bots::inbound::BotTriggerRouter::new(- channels_service.clone(),- Arc::new(channel_bots::outbound::AgentLoopResponder::new(- macro_agent_tool_context,- macro_agent_tools,- )),- Arc::new(- channel_bots::domain::trigger_detector::MentionOrInferredDetector::new(- channels_service.clone(),- Arc::new(channel_bots::outbound::FastModelTriggerClassifier::new(- ai_usage::pg_recorder(db.clone()),- )),- ),- ),- );- bot_trigger_router.spawn(bot_trigger_receiver);+ // Mentioning @macro no longer answers with an in-process chat reply: the+ // mention rides the channel event to the agent trigger service, which+ // opens an agent session on the harness's in-memory runtime instead.let channel_bot_webhook_state =bots::inbound::channel_webhook_router::ChannelBotWebhookRouterState::new(diff --git a/tooling/xtask/crates/xtask_local/src/local/local_env.rs b/tooling/xtask/crates/xtask_local/src/local/local_env.rsindex 405d1baa7c..07dd77e3db 100644--- a/tooling/xtask/crates/xtask_local/src/local/local_env.rs+++ b/tooling/xtask/crates/xtask_local/src/local/local_env.rs@@ -261,6 +261,10 @@ impl MailEnv {/// `DAYTONA_API_KEY=... DEV_DANGEROUS_LOCAL_CONTAINERS=false just run_local`./// `GITHUB_TOKEN` is left to Doppler / process env so a local sandbox can/// still clone.+///+/// Two managed bots: `@coder` (`HARNESS_BOT_ID`) gets a sandbox from the+/// local Docker provider, and `@macro` (`INMEM_BOT_ID`) runs in-process on+/// the in-memory ACP runtime.struct AgentHarnessEnv {bot_id: &'static str,snapshot: &'static str,@@ -286,6 +290,12 @@ impl AgentHarnessEnv {fn write(&self, env: &mut BTreeMap<String, String>) {env.insert("HARNESS_BOT_ID".into(), self.bot_id.into());+ // bot_id::MACRO_AI_BOT_ID: @macro's sessions run on the in-process+ // ACP runtime, while @coder's get sandboxes from the local provider.+ env.insert(+ "INMEM_BOT_ID".into(),+ "00000000-0000-0000-0000-00000000a1a1".into(),+ );env.insert("DAYTONA_SNAPSHOT".into(), self.snapshot.into());env.insert("DAYTONA_API_KEY".into(), String::new());env.insert("DEV_DANGEROUS_LOCAL_CONTAINERS".into(), "true".into());