Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(agent-harness): say when a tool call waits for the owner; never leave a chat reply spinning (#7562)
Wolf Mermelsteincf9814a88076
diff --git a/Cargo.lock b/Cargo.lockindex d53ff495cd..0da7844804 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -390,6 +390,7 @@ dependencies = ["url","utoipa","uuid",+ "wiremock","workspace-hack",]diff --git a/crates/agent_harness/Cargo.toml b/crates/agent_harness/Cargo.tomlindex 74ef38d6cb..3fccda0a8b 100644--- a/crates/agent_harness/Cargo.toml+++ b/crates/agent_harness/Cargo.toml@@ -91,3 +91,4 @@ harness_id = { path = "../harness_id", features = ["test-utils"] }mockall = { workspace = true }model_user = { path = "../model_user" }tower = { workspace = true }+wiremock = { workspace = true }diff --git a/crates/agent_harness/src/domain/model.rs b/crates/agent_harness/src/domain/model.rsindex f01e10709c..6993f2be14 100644--- a/crates/agent_harness/src/domain/model.rs+++ b/crates/agent_harness/src/domain/model.rs@@ -551,6 +551,11 @@ pub enum HarnessCommand {/// Why the actor stopped.reason: String,},+ /// A tool call in the session's running turn started or stopped waiting+ /// for the owner's approval. Sent by the approval service from whichever+ /// replica held the call, and forwarded to the one managing the session,+ /// since that is where the turn's reply is known.+ ToolApproval(ToolApprovalChange),/// Change the session's sandbox size and the owner's default.SetSandboxSize(SandboxSize),/// Release a session's live resources and delete it.@@ -703,11 +708,13 @@ pub struct AnnouncedMessage {/// thread would rather read that than a notice. The rest distinguish the/// silences a reader can act on differently - try again, or not.///-/// Two are not ends at all. A turn that asks the user something through an+/// Three are not ends at all. A turn that asks the user something through an/// ACP elicitation is held open until someone answers it in the session-/// view, and a thread showing a spinner has no way of knowing that; so the-/// reply says so ([`Self::NeedsInput`]) and returns to pending once the-/// question is cleared ([`Self::Resumed`]).+/// view, and one whose tool call waits on the owner's approval is held until+/// the owner answers; a thread showing a spinner has no way of knowing+/// either. So the reply says so ([`Self::NeedsInput`],+/// [`Self::AwaitingApproval`]) and returns to pending once nothing is+/// waiting ([`Self::Resumed`]).#[derive(Debug, Clone, PartialEq, Eq)]pub enum ReplyOutcome {/// The agent answered; its last message, whole.@@ -724,10 +731,39 @@ pub enum ReplyOutcome {/// What the agent is asking, in prose.question: String,},- /// The question was answered or withdrawn and the turn is running again.+ /// A tool call is held until the session's owner approves it.+ AwaitingApproval(HeldToolCall),+ /// The question was answered or withdrawn, or every held tool call was+ /// settled, and the turn is running again.Resumed,}+/// A tool call held for the session owner's approval, as far as the thread+/// is told about it.+#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]+pub struct HeldToolCall {+ /// The approval row; what its settling names.+ pub approval_id: String,+ /// `macro`, or the connected app's slug.+ pub server_slug: String,+ /// What a person calls the server the tool is on.+ pub server_name: String,+ /// The tool called.+ pub tool_name: String,+}++/// How a session's held tool calls changed.+#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]+pub enum ToolApprovalChange {+ /// A call started waiting for the owner.+ Held(HeldToolCall),+ /// The call was approved, denied, cancelled, or expired.+ Settled {+ /// The approval that settled.+ approval_id: String,+ },+}+impl ReplyOutcome {/// The outcome of a turn the fold closed with `stop`, whose last text/// part was `last_text`.diff --git a/crates/agent_harness/src/domain/pending.rs b/crates/agent_harness/src/domain/pending.rsindex 80f7d8bcb9..4635ab88cc 100644--- a/crates/agent_harness/src/domain/pending.rs+++ b/crates/agent_harness/src/domain/pending.rs@@ -67,6 +67,19 @@ impl PendingCommands {self.0.get(&session).and_then(|entry| entry.clone())}+ /// Change the turn `session` has in flight, handing back the turn as+ /// changed. `None`, and nothing changed, when it has none.+ pub fn update_turn(+ &self,+ session: AgentSessionId,+ change: impl FnOnce(&mut InFlightTurn),+ ) -> Option<InFlightTurn> {+ let mut entry = self.0.get_mut(&session)?;+ let turn = entry.as_mut()?;+ change(turn);+ Some(turn.clone())+ }+/// Whether `session` currently has a command admitted and unresolved.////// A reaper's cue to leave the session's transport alone this tick even@@ -97,6 +110,7 @@ mod test {announce: None,announcement_message_id: None,dispatched_at: chrono::Utc::now(),+ held_tool_calls: Vec::new(),}}diff --git a/crates/agent_harness/src/domain/ports.rs b/crates/agent_harness/src/domain/ports.rsindex 9ad3271df3..82325d25df 100644--- a/crates/agent_harness/src/domain/ports.rs+++ b/crates/agent_harness/src/domain/ports.rs@@ -17,7 +17,7 @@ use super::error::{HarnessError, Result};use super::model::{AgentKind, AgentRuntimeConfig, AnnouncedMessage, CommandOutcome, ConversationContext,DeclinedMention, HarnessCommand, ProvisionedEgress, ReachableRepository, ResolvedReply,- SandboxEgress, SessionAnnouncement, SessionBlocker, SpawnContainer,+ SandboxEgress, SessionAnnouncement, SessionBlocker, SpawnContainer, ToolApprovalChange,};use super::notifications::PlannedNotification;use super::sandbox::SandboxResizeEffect;@@ -301,6 +301,52 @@ pub trait SessionAnnouncer: Send + Sync + 'static {fn decline(&self, declined: DeclinedMention) -> impl Future<Output = Result<()>> + Send;}+/// Told when a tool call in a session's turn starts or stops waiting for the+/// owner's approval, so the thread that prompted the turn can be told.+///+/// Synchronous, like the turn observer: the one implementation admits a+/// command to the session's queue and returns.+pub trait HeldToolCallObserver: Send + Sync + 'static {+ /// `session`'s held tool calls changed.+ fn changed(&self, session: AgentSessionId, change: ToolApprovalChange);+}++/// A [`HeldToolCallObserver`] bound after construction: the approval service+/// is built before the harness that observes it. Changes before the bind+/// are dropped.+#[derive(Default)]+pub struct LateBoundHeldToolCallObserver {+ observer: std::sync::OnceLock<Box<dyn HeldToolCallObserver>>,+}++impl LateBoundHeldToolCallObserver {+ /// An observer awaiting its target.+ #[must_use]+ pub fn new() -> Self {+ Self::default()+ }++ /// Attach the real observer. A second bind is a wiring bug; the first+ /// stays authoritative.+ pub fn bind(&self, observer: impl HeldToolCallObserver) {+ let _ = self.observer.set(Box::new(observer));+ }+}++impl HeldToolCallObserver for LateBoundHeldToolCallObserver {+ fn changed(&self, session: AgentSessionId, change: ToolApprovalChange) {+ if let Some(observer) = self.observer.get() {+ observer.changed(session, change);+ }+ }+}++impl<T: HeldToolCallObserver + ?Sized> HeldToolCallObserver for Arc<T> {+ fn changed(&self, session: AgentSessionId, change: ToolApprovalChange) {+ (**self).changed(session, change);+ }+}+/// Where a session finds its bot's live runtime connection.////// A self-hosted runtime dials once and carries every session its bot isdiff --git a/crates/agent_harness/src/domain/queue.rs b/crates/agent_harness/src/domain/queue.rsindex 9663d53cfb..b6d86fa731 100644--- a/crates/agent_harness/src/domain/queue.rs+++ b/crates/agent_harness/src/domain/queue.rs@@ -33,7 +33,7 @@ use dashmap::DashMap;use macro_user_id::user_id::MacroUserIdStr;use macro_uuid::Uuid;-use super::model::AnnounceOrigin;+use super::model::{AnnounceOrigin, HeldToolCall};#[cfg(test)]mod test;@@ -94,6 +94,9 @@ pub struct InFlightTurn {/// turn hours old with nothing streaming is the shape of a wedged/// session, and without this it looks exactly like a long one.pub dispatched_at: DateTime<Utc>,+ /// Tool calls this turn made that wait on the owner's approval, oldest+ /// first. The reply names the oldest while any wait.+ pub held_tool_calls: Vec<HeldToolCall>,}impl InFlightTurn {diff --git a/crates/agent_harness/src/domain/service.rs b/crates/agent_harness/src/domain/service.rsindex 48155dcfea..781a8cc161 100644--- a/crates/agent_harness/src/domain/service.rs+++ b/crates/agent_harness/src/domain/service.rs@@ -51,7 +51,7 @@ use crate::domain::error::{HarnessError, Result};use crate::domain::model::{AgentKind, AnnounceOrigin, AnnouncePrompt, CommandOutcome, DeclinedMention, DeliverAction,HarnessCommand, HarnessDefaults, OpenSession, PromptPeople, ReplyOutcome, ResolvedReply,- SessionAnnouncement, SpawnContainer, is_macro_staff,+ SessionAnnouncement, SpawnContainer, ToolApprovalChange, is_macro_staff,};use crate::domain::pending::PendingCommands;use crate::domain::ports::{diff --git a/crates/agent_harness/src/domain/service/lifecycle.rs b/crates/agent_harness/src/domain/service/lifecycle.rsindex f61f3fd636..dcc717c09c 100644--- a/crates/agent_harness/src/domain/service/lifecycle.rs+++ b/crates/agent_harness/src/domain/service/lifecycle.rs@@ -244,6 +244,50 @@ where}}+impl<+ Sessions,+ Containers,+ Announcer,+ Runtimes,+ PromptContext,+ PromptComposer,+ Egress,+ Lifecycle,+ Mentions,+ Notifier,+> crate::domain::ports::HeldToolCallObserver+ for AgentHarnessService<+ Sessions,+ Containers,+ Announcer,+ Runtimes,+ PromptContext,+ PromptComposer,+ Egress,+ Lifecycle,+ Mentions,+ Notifier,+ >+where+ Sessions: AgentSessionService,+ Containers: ContainerManager,+ Announcer: SessionAnnouncer,+ Runtimes: RuntimeConnections,+ PromptContext: MessagePromptContext,+ PromptComposer: AgentPromptComposer,+ Egress: SandboxEgressProvisioner,+ Lifecycle: AgentSessionLifecyclePublisher,+ Mentions: PromptMentions,+ Notifier: AgentSessionNotifier,+{+ /// Through [`execute`](AgentHarnessService::execute), not `execute_here`:+ /// the call may have been held on any replica, and only the one managing+ /// the session knows the turn whose reply to change.+ fn changed(&self, id: AgentSessionId, change: crate::domain::model::ToolApprovalChange) {+ drop(self.execute(id, HarnessCommand::ToolApproval(change)));+ }+}+impl<Sessions,Containers,diff --git a/crates/agent_harness/src/domain/service/queue.rs b/crates/agent_harness/src/domain/service/queue.rsindex b25bd073c8..a1fadef109 100644--- a/crates/agent_harness/src/domain/service/queue.rs+++ b/crates/agent_harness/src/domain/service/queue.rs@@ -339,6 +339,7 @@ where}HarnessCommand::Open(_)| HarnessCommand::Turn(_)+ | HarnessCommand::ToolApproval(_)| HarnessCommand::SessionStopped { .. }| HarnessCommand::Delete => {}HarnessCommand::SetSandboxSize(_) => {@@ -531,6 +532,10 @@ where.await;Ok(CommandOutcome::Completed)}+ HarnessCommand::ToolApproval(change) => {+ self.tool_approval_changed(session_id, change).await;+ Ok(CommandOutcome::Completed)+ }HarnessCommand::SetSandboxSize(size) => {self.apply_sandbox_size(session_id, size).await?;Ok(CommandOutcome::Completed)@@ -718,6 +723,35 @@ where////// A failed cancel is best-effort. The entry is already at the front, so/// it still drains when the current turn ends on its own.+ /// Keep the running turn's reply honest about tool calls held for the+ /// owner: it names the oldest one waiting, and goes back to pending once+ /// none are. Only the managing replica knows the turn, so a change for a+ /// session with nothing in flight here has no reply to touch.+ async fn tool_approval_changed(&self, session_id: AgentSessionId, change: ToolApprovalChange) {+ let mut waiting_before = None;+ let Some(turn) = self.busy.update_turn(session_id, |turn| {+ waiting_before = turn.held_tool_calls.first().cloned();+ match &change {+ ToolApprovalChange::Held(call) => turn.held_tool_calls.push(call.clone()),+ ToolApprovalChange::Settled { approval_id } => turn+ .held_tool_calls+ .retain(|held| held.approval_id != *approval_id),+ }+ }) else {+ tracing::info!(%session_id, "held tool call changed with no in-flight record");+ return;+ };+ let waiting = turn.held_tool_calls.first().cloned();+ if waiting == waiting_before {+ return;+ }+ let outcome = match waiting {+ Some(call) => ReplyOutcome::AwaitingApproval(call),+ None => ReplyOutcome::Resumed,+ };+ self.resolve_reply(session_id, Some(&turn), outcome).await;+ }+async fn steer_queued(&self,session_id: AgentSessionId,@@ -1040,6 +1074,7 @@ whereannounce: entry.announce,announcement_message_id: entry.announced,dispatched_at: chrono::Utc::now(),+ held_tool_calls: Vec::new(),};self.busy.mark_turn(session_id, turn.clone());self.publish_lifecycle(session_id, |identity| {diff --git a/crates/agent_harness/src/domain/service/test/chat_reply.rs b/crates/agent_harness/src/domain/service/test/chat_reply.rsindex c48cd8f4b3..a754afb35d 100644--- a/crates/agent_harness/src/domain/service/test/chat_reply.rs+++ b/crates/agent_harness/src/domain/service/test/chat_reply.rs@@ -4,7 +4,9 @@//! is what the domain asked of it and when.use super::*;-use crate::domain::model::{ReplyOutcome, ResolvedReply, TaskAssignmentOrigin};+use crate::domain::model::{+ HeldToolCall, ReplyOutcome, ResolvedReply, TaskAssignmentOrigin, ToolApprovalChange,+};use crate::domain::notifications::PlannedNotification;/// A mention of a bot whose sessions the in-memory runtime serves.@@ -484,6 +486,104 @@ mod elicitation {}}+/// Tool calls held for the owner's approval: the reply names the oldest one+/// waiting, says nothing new while a later one joins it, moves on to the+/// next when the oldest settles, and is pending again once none wait.+#[tokio::test]+async fn held_tool_calls_are_told_in_the_thread_until_none_wait() {+ let ((service, _, containers, announcer, _), turns) =+ harness_with_signals(PromptContextMock::default(), PromptComposerMock::default());+ let id = AgentSessionId::new();+ let container = chat_session_with_a_running_turn(&service, &containers, id).await;+ let agent = container.agent();+ let pending = announcer.announced_messages()[0].message_id;+ let list_entities = HeldToolCall {+ approval_id: "approval-1".to_owned(),+ server_slug: "macro".to_owned(),+ server_name: "Macro".to_owned(),+ tool_name: "ListEntities".to_owned(),+ };+ let create_issue = HeldToolCall {+ approval_id: "approval-2".to_owned(),+ server_slug: "linear".to_owned(),+ server_name: "Linear".to_owned(),+ tool_name: "create_issue".to_owned(),+ };++ for change in [+ ToolApprovalChange::Held(list_entities.clone()),+ ToolApprovalChange::Held(create_issue.clone()),+ ToolApprovalChange::Settled {+ approval_id: "approval-1".to_owned(),+ },+ ToolApprovalChange::Settled {+ approval_id: "approval-2".to_owned(),+ },+ ] {+ service+ .execute(id, HarnessCommand::ToolApproval(change))+ .await+ .expect("a held call change is handled");+ }++ let resolved = announcer.resolved();+ assert_eq!(+ resolved+ .iter()+ .map(|reply| (reply.message_id, reply.outcome.clone()))+ .collect::<Vec<_>>(),+ [+ (pending, ReplyOutcome::AwaitingApproval(list_entities)),+ (pending, ReplyOutcome::AwaitingApproval(create_issue)),+ (pending, ReplyOutcome::Resumed),+ ]+ );+ assert!(+ turns.notifier.notified().is_empty(),+ "{:#?}",+ turns.notifier.notified()+ );++ says(&agent, "Here are your files.");+ agent.completes_prompt().await;+ turns.lifecycle_published(4).await;+ let resolved = announcer.resolved();+ assert_eq!(resolved.len(), 4, "{resolved:#?}");+ assert_eq!(+ resolved[3].outcome,+ ReplyOutcome::Answered("Here are your files.".to_owned())+ );+}++/// A call settled after its turn ended - the release on turn end - has no+/// reply left to change.+#[tokio::test]+async fn a_held_call_settled_after_its_turn_changes_nothing() {+ let ((service, _, containers, announcer, _), turns) =+ harness_with_signals(PromptContextMock::default(), PromptComposerMock::default());+ let id = AgentSessionId::new();+ let container = chat_session_with_a_running_turn(&service, &containers, id).await;+ let agent = container.agent();+ says(&agent, "Done.");+ agent.completes_prompt().await;+ turns.lifecycle_published(4).await;++ service+ .execute(+ id,+ HarnessCommand::ToolApproval(ToolApprovalChange::Settled {+ approval_id: "approval-1".to_owned(),+ }),+ )+ .await+ .expect("a late settle is handled");++ assert_eq!(+ one_resolved(&announcer).outcome,+ ReplyOutcome::Answered("Done.".to_owned())+ );+}+#[tokio::test]async fn task_assignment_links_the_session_without_copying_its_answer_to_discussion() {let ((service, repo, containers, announcer, _), turns) =diff --git a/crates/agent_harness/src/domain/service/test/quota.rs b/crates/agent_harness/src/domain/service/test/quota.rsindex e2b26ef40e..5bb9bdd6ff 100644--- a/crates/agent_harness/src/domain/service/test/quota.rs+++ b/crates/agent_harness/src/domain/service/test/quota.rs@@ -353,6 +353,7 @@ async fn retries_of_queued_and_in_flight_actions_do_not_recheck_quota() {announce: None,announcement_message_id: None,dispatched_at: chrono::Utc::now(),+ held_tool_calls: Vec::new(),},);assert_eq!(@@ -614,6 +615,7 @@ async fn denying_a_steering_follow_up_does_not_cancel_the_running_turn() {announce: None,announcement_message_id: None,dispatched_at: chrono::Utc::now(),+ held_tool_calls: Vec::new(),};service.inner.busy.mark_turn(id, running.clone());// Enqueue succeeds, but steering's revalidation denies before Stop or chip.diff --git a/crates/agent_harness/src/outbound/channel_announcer.rs b/crates/agent_harness/src/outbound/channel_announcer.rsindex 28661ef4c2..f3dd5d402e 100644--- a/crates/agent_harness/src/outbound/channel_announcer.rs+++ b/crates/agent_harness/src/outbound/channel_announcer.rs@@ -17,6 +17,7 @@ mod test;use std::sync::Arc;+use agent_egress::domain::approval::MACRO_SERVER_SLUG;use agent_session::domain::model::AgentSessionId;use bot_id::BotId;use entity_access::domain::{@@ -41,8 +42,8 @@ use messages::domain::{use crate::domain::error::{HarnessError, Result};use crate::domain::model::{- AnnouncedMessage, DeclinedMention, ReplyOutcome, ResolvedReply, SessionAnnouncement,- SessionBlocker,+ AnnouncedMessage, DeclinedMention, HeldToolCall, ReplyOutcome, ResolvedReply,+ SessionAnnouncement, SessionBlocker,};use crate::domain::ports::SessionAnnouncer;@@ -54,6 +55,12 @@ const ERROR_FALLBACK: &str = "Sorry — I ran into an error while responding.";/// reader gets there.const NEEDS_INPUT_LEAD: &str ="I have a question before I can continue — open the agent session to answer it:";+/// Said instead of an answer the thread may not carry: one that mentions+/// something the person who asked cannot open, typically what a tool the+/// owner approved found with the owner's access. The answer is still in the+/// session, which the owner can read.+const UNSHAREABLE_ANSWER: &str = "My answer mentions things you can't open here, so it stays in \+ the agent session.";/// A chat agent's message in one state, for Lexical to compose: the link to/// its session, then `body`.@@ -80,11 +87,25 @@ fn reply_body(outcome: ReplyOutcome) -> AgentChatReplyBody {ReplyOutcome::Cancelled => CANCELLED_FALLBACK.to_owned(),ReplyOutcome::Failed => ERROR_FALLBACK.to_owned(),ReplyOutcome::NeedsInput { question } => format!("{NEEDS_INPUT_LEAD}\n\n{question}"),+ ReplyOutcome::AwaitingApproval(call) => awaiting_approval(&call),ReplyOutcome::Resumed => return AgentChatReplyBody::Pending,};AgentChatReplyBody::Markdown { markdown }}+/// What the thread reads while a tool call waits on the owner. Only the+/// owner can approve it, and they are notified on their own, so this names+/// the hold for everyone else rather than asking anything of them.+fn awaiting_approval(call: &HeldToolCall) -> String {+ let tool = &call.tool_name;+ if call.server_slug == MACRO_SERVER_SLUG {+ format!("Waiting for the session owner to allow `{tool}`.")+ } else {+ let server = &call.server_name;+ format!("Waiting for the session owner to allow `{tool}` from {server}.")+ }+}+/// Whether the thread should hear about a patch. The answer, and a question/// only a person can unblock, are news the pending reply withheld; the/// spinner coming back is not.@@ -95,7 +116,10 @@ const fn patch_policy(outcome: &ReplyOutcome) -> PatchMessageNotificationPolicy| ReplyOutcome::Cancelled| ReplyOutcome::Failed| ReplyOutcome::NeedsInput { .. } => PatchMessageNotificationPolicy::NotifyAsPostedMessage,- ReplyOutcome::Resumed => PatchMessageNotificationPolicy::Default,+ // The owner, the one person who can act, is notified by the hold.+ ReplyOutcome::AwaitingApproval(_) | ReplyOutcome::Resumed => {+ PatchMessageNotificationPolicy::Default+ }}}@@ -209,6 +233,20 @@ impl<Access: EntityAccessService> MessageAnnouncer<Access> {}}+impl<Access> MessageAnnouncer<Access> {+ /// A chat agent's reply saying `body`, composed by Lexical.+ async fn compose_reply(+ &self,+ session_id: AgentSessionId,+ body: AgentChatReplyBody,+ ) -> Result<String> {+ self.lexical+ .compose_agent_chat_reply(&chat_reply(session_id, body))+ .await+ .map_err(|error| HarnessError::Announce(rootcause::report!(error).into()))+ }+}+impl<Access: EntityAccessService> SessionAnnouncer for MessageAnnouncer<Access> {async fn announce(&self, announcement: SessionAnnouncement) -> Result<AnnouncedMessage> {let access = self@@ -294,27 +332,39 @@ impl<Access: EntityAccessService> SessionAnnouncer for MessageAnnouncer<Access>.await?;let message_id = resolution.message_id;let notification_policy = patch_policy(&resolution.outcome);- let content = self- .lexical- .compose_agent_chat_reply(&chat_reply(- resolution.session_id,- reply_body(resolution.outcome),- ))- .await- .map_err(|error| HarnessError::Announce(rootcause::report!(error).into()))?;- match self+ let body = reply_body(resolution.outcome);+ let carries_answer = matches!(body, AgentChatReplyBody::Markdown { .. });+ let patch = |content| MessagePatch {+ notification_policy,+ content: Some(content),+ ..Default::default()+ };+ let content = self.compose_reply(resolution.session_id, body).await?;+ let patched = match self.messages- .patch(- access,- message_id,- MessagePatch {- notification_policy,- content: Some(content),- ..Default::default()- },- )+ .patch(access.clone(), message_id, patch(content)).await{+ // The message service refuses an edit whose mentions the person+ // who asked could not open. Left there, the reply would spin+ // forever; the answer is kept in the session instead.+ Err(MessageError::Forbidden) if carries_answer => {+ tracing::warn!(%message_id, "the thread may not carry this answer; pointing at the session");+ let content = self+ .compose_reply(+ resolution.session_id,+ AgentChatReplyBody::Markdown {+ markdown: UNSHAREABLE_ANSWER.to_owned(),+ },+ )+ .await?;+ self.messages+ .patch(access, message_id, patch(content))+ .await+ }+ patched => patched,+ };+ match patched {Ok(_) => Ok(()),// A participant deleted the pending reply while the agent ran:// they did not want the answer, and there is nowhere to put it.diff --git a/crates/agent_harness/src/outbound/channel_announcer/test.rs b/crates/agent_harness/src/outbound/channel_announcer/test.rsindex 48e9d5b741..4b838b28aa 100644--- a/crates/agent_harness/src/outbound/channel_announcer/test.rs+++ b/crates/agent_harness/src/outbound/channel_announcer/test.rs@@ -3,6 +3,13 @@ use macro_uuid::Uuid;use super::*;use bot_id::BotId;+use entity_access::domain::models::{+ AccessError, AccessLevel, CallChannelInfo, Entity, EntityPermission, EntityType,+ RequiredPermission, TeamRole, UserTeamInfo,+};+use macro_user_id::lowercased::Lowercase;+use macro_user_id::user_id::MacroUserId;+use messages::domain::models::Message;fn announcement() -> SessionAnnouncement {SessionAnnouncement {@@ -91,6 +98,12 @@ fn every_outcome() -> Vec<ReplyOutcome> {ReplyOutcome::NeedsInput {question: "Which inbox?".to_owned(),},+ ReplyOutcome::AwaitingApproval(HeldToolCall {+ approval_id: "approval-1".to_owned(),+ server_slug: "macro".to_owned(),+ server_name: "Macro".to_owned(),+ tool_name: "ListEntities".to_owned(),+ }),ReplyOutcome::Resumed,]}@@ -192,11 +205,37 @@ fn a_resumed_reply_is_the_pending_reply_again() {);}-/// The answer and a question are news; the spinner coming back is not.+/// A held call names the tool, and the app it belongs to when that is not+/// Macro, and asks nothing of the thread: only the owner can approve it.#[test]-fn only_the_spinner_returning_is_a_silent_patch() {+fn a_held_call_names_its_tool_while_it_waits() {+ let held = |server_slug: &str, server_name: &str, tool_name: &str| {+ markdown(reply_body(ReplyOutcome::AwaitingApproval(HeldToolCall {+ approval_id: "approval-1".to_owned(),+ server_slug: server_slug.to_owned(),+ server_name: server_name.to_owned(),+ tool_name: tool_name.to_owned(),+ })))+ };+ assert_eq!(+ held("macro", "Macro", "ListEntities"),+ "Waiting for the session owner to allow `ListEntities`."+ );+ assert_eq!(+ held("linear", "Linear", "create_issue"),+ "Waiting for the session owner to allow `create_issue` from Linear."+ );+}++/// The answer and a question are news; the spinner coming back is not, and+/// a held call is news only to the owner, who the hold notifies itself.+#[test]+fn only_the_answer_and_a_question_notify_the_thread() {for outcome in every_outcome() {- let expected = if outcome == ReplyOutcome::Resumed {+ let expected = if matches!(+ outcome,+ ReplyOutcome::Resumed | ReplyOutcome::AwaitingApproval(_)+ ) {PatchMessageNotificationPolicy::Default} else {PatchMessageNotificationPolicy::NotifyAsPostedMessage@@ -204,3 +243,188 @@ fn only_the_spinner_returning_is_a_silent_patch() {assert_eq!(patch_policy(&outcome), expected, "{outcome:?}");}}++/// Grants the bot whatever the user it acts for may do: here, edit.+#[derive(Clone)]+struct EditingAccess;++impl EntityAccessService for EditingAccess {+ async fn generate_entity_access_receipt<T: RequiredPermission>(+ &self,+ _: &MacroUserId<Lowercase<'_>>,+ _: Option<i64>,+ _: &str,+ _: EntityType,+ ) -> Result<EntityAccessReceipt<T>, AccessError> {+ unimplemented!("the announcer only mints bot receipts")+ }+ async fn generate_bot_entity_access_receipt<T: RequiredPermission>(+ &self,+ bot_id: BotId,+ scope: BotAccessScope,+ entity_id: &str,+ entity_type: EntityType,+ ) -> Result<EntityAccessReceipt<T>, AccessError> {+ EntityAccessReceipt::try_new_bot(+ bot_id.into_storage_id(),+ (&scope).into(),+ Entity {+ entity_id: entity_id.to_owned(),+ entity_type,+ },+ EntityPermission::AccessLevel {+ access_level: AccessLevel::Edit,+ },+ )+ }+ async fn get_access_level(+ &self,+ _: Option<&MacroUserId<Lowercase<'_>>>,+ _: &str,+ _: EntityType,+ ) -> Result<Option<AccessLevel>, AccessError> {+ unimplemented!("the announcer only mints bot receipts")+ }+ async fn check_access(+ &self,+ _: Option<&MacroUserId<Lowercase<'_>>>,+ _: &str,+ _: EntityType,+ _: AccessLevel,+ ) -> Result<AccessLevel, AccessError> {+ unimplemented!("the announcer only mints bot receipts")+ }+ async fn check_public_access(+ &self,+ _: &str,+ _: EntityType,+ _: AccessLevel,+ ) -> Result<AccessLevel, AccessError> {+ unimplemented!("the announcer only mints bot receipts")+ }+ async fn get_entity_permission(+ &self,+ _: Option<&MacroUserId<Lowercase<'_>>>,+ _: &str,+ _: EntityType,+ _: Option<i64>,+ ) -> Result<EntityPermission, AccessError> {+ unimplemented!("the announcer only mints bot receipts")+ }+ async fn get_crm_entity_permission_with_team(+ &self,+ _: Option<&MacroUserId<Lowercase<'_>>>,+ _: &str,+ _: EntityType,+ ) -> Result<(EntityPermission, Uuid, TeamRole), AccessError> {+ unimplemented!("the announcer only mints bot receipts")+ }+ async fn get_users_by_entity(+ &self,+ _: &str,+ _: EntityType,+ ) -> Result<Vec<MacroUserIdStr<'static>>, AccessError> {+ unimplemented!("the announcer only mints bot receipts")+ }+ async fn get_call_channel(&self, _: &Uuid) -> Result<Option<CallChannelInfo>, AccessError> {+ unimplemented!("the announcer only mints bot receipts")+ }+ async fn get_call_channel_by_channel_id(+ &self,+ _: &Uuid,+ ) -> Result<Option<CallChannelInfo>, AccessError> {+ unimplemented!("the announcer only mints bot receipts")+ }+ async fn get_user_team(+ &self,+ _: &MacroUserId<Lowercase<'_>>,+ ) -> Result<Option<UserTeamInfo>, AccessError> {+ unimplemented!("the announcer only mints bot receipts")+ }+}++/// An answer that mentions something the person who asked cannot open -+/// what a tool the owner approved found with the owner's access - is+/// refused by the message service. The reply says where the answer is+/// instead of spinning forever.+#[tokio::test]+async fn an_answer_the_thread_may_not_carry_points_at_the_session() {+ let lexical = wiremock::MockServer::start().await;+ for (markdown, composed) in [+ ("Your emails: <m-document-mention/>", "composed answer"),+ (UNSHAREABLE_ANSWER, "composed pointer"),+ ] {+ wiremock::Mock::given(wiremock::matchers::method("POST"))+ .and(wiremock::matchers::path("/agent-announcement"))+ .and(wiremock::matchers::body_partial_json(serde_json::json!({+ "chatReply": { "body": { "kind": "markdown", "markdown": markdown } }+ })))+ .respond_with(+ wiremock::ResponseTemplate::new(200)+ .set_body_json(serde_json::json!({ "markdown": composed })),+ )+ .expect(1)+ .mount(&lexical)+ .await;+ }+ let parent = MessageParent::Channel(Uuid::from_u128(1));+ let reply_id = Uuid::from_u128(3);+ let patched: Arc<std::sync::Mutex<Vec<String>>> = Arc::default();+ let mut messages = messages::domain::api::MockMessageCommands::new();+ let seen = Arc::clone(&patched);+ let replied = Message {+ id: reply_id,+ parent: parent.clone(),+ thread_id: Some(Uuid::from_u128(2)),+ sender_id: channel_sender::ChannelSender::new_from_user(+ MacroUserIdStr::try_from_email("[email protected]").unwrap(),+ ),+ triggered_by: None,+ bot_profile: None,+ mentions: vec![],+ imported_author: None,+ content: "composed pointer".to_owned(),+ created_at: chrono::Utc::now(),+ updated_at: chrono::Utc::now(),+ edited_at: None,+ deleted_at: None,+ attachments: vec![],+ reactions: vec![],+ };+ messages+ .expect_patch()+ .times(2)+ .returning(move |_, id, patch| {+ assert_eq!(id, reply_id);+ let mut seen = seen.lock().unwrap();+ seen.push(patch.content.unwrap());+ if seen.len() == 1 {+ Err(MessageError::Forbidden)+ } else {+ Ok(replied.clone())+ }+ });+ let announcer = MessageAnnouncer::new(+ Arc::new(messages),+ Arc::new(EditingAccess),+ LexicalClient::new("test-key".to_owned(), lexical.uri()),+ );++ announcer+ .resolve(ResolvedReply {+ session_id: SESSION,+ bot_id: BotId::TEST_A,+ is_coding: false,+ message_id: reply_id,+ origin_parent: parent,+ triggered_by: MacroUserIdStr::try_from_email("[email protected]").unwrap(),+ outcome: ReplyOutcome::Answered("Your emails: <m-document-mention/>".to_owned()),+ })+ .await+ .expect("the pointer replaces the spinner");++ assert_eq!(+ *patched.lock().unwrap(),+ ["composed answer", "composed pointer"]+ );+}diff --git a/crates/agent_harness/src/outbound/tool_approvals.rs b/crates/agent_harness/src/outbound/tool_approvals.rsindex 69dbd61134..0766fd11fd 100644--- a/crates/agent_harness/src/outbound/tool_approvals.rs+++ b/crates/agent_harness/src/outbound/tool_approvals.rs@@ -2,8 +2,9 @@//!//! Each state of a held call becomes a frame in the session's log, which is//! what the session view renders and answers from; a call that starts-//! waiting also notifies the owner, the only person who can approve it.-//! Both are best-effort: the hold stands on its approval row either way.+//! waiting also notifies the owner, the only person who can approve it, and+//! every change reaches the harness, which tells the thread that prompted+//! the turn. All best-effort: the hold stands on its approval row either way.//!//! Also here: releasing a session's held calls when its turn ends, stops, or//! the session is deleted, by listening to the same lifecycle facts the@@ -20,26 +21,33 @@ use agent_session::domain::ports::AgentSessionLifecyclePublisher;use agent_session::domain::service::AgentSessionService;use macro_user_id::user_id::MacroUserIdStr;+use crate::domain::model::{HeldToolCall, ToolApprovalChange};use crate::domain::notifications::plan_tool_approval;-use crate::domain::ports::AgentSessionNotifier;+use crate::domain::ports::{AgentSessionNotifier, HeldToolCallObserver};#[cfg(test)]mod test;-/// [`ToolApprovalAnnouncer`] over the session's log and the notifier.-pub struct SessionToolApprovalAnnouncer<Sessions, Notifier> {+/// [`ToolApprovalAnnouncer`] over the session's log, the notifier, and the+/// harness.+pub struct SessionToolApprovalAnnouncer<Sessions, Notifier, HeldCalls> {sessions: Sessions,notifier: Notifier,+ held_calls: HeldCalls,}-impl<Sessions, Notifier> SessionToolApprovalAnnouncer<Sessions, Notifier> {- /// Announce through `sessions`' log and `notifier`.- pub fn new(sessions: Sessions, notifier: Notifier) -> Self {- Self { sessions, notifier }+impl<Sessions, Notifier, HeldCalls> SessionToolApprovalAnnouncer<Sessions, Notifier, HeldCalls> {+ /// Announce through `sessions`' log, `notifier`, and `held_calls`.+ pub fn new(sessions: Sessions, notifier: Notifier, held_calls: HeldCalls) -> Self {+ Self {+ sessions,+ notifier,+ held_calls,+ }}}-impl<Sessions, Notifier> SessionToolApprovalAnnouncer<Sessions, Notifier>+impl<Sessions, Notifier, HeldCalls> SessionToolApprovalAnnouncer<Sessions, Notifier, HeldCalls>whereSessions: AgentSessionService,Notifier: AgentSessionNotifier,@@ -79,13 +87,24 @@ where}}-impl<Sessions, Notifier> ToolApprovalAnnouncer for SessionToolApprovalAnnouncer<Sessions, Notifier>+impl<Sessions, Notifier, HeldCalls> ToolApprovalAnnouncer+ for SessionToolApprovalAnnouncer<Sessions, Notifier, HeldCalls>whereSessions: AgentSessionService,Notifier: AgentSessionNotifier,+ HeldCalls: HeldToolCallObserver,{async fn requested(&self, approval: &ToolApproval, _owner: &MacroUserIdStr<'static>) {self.record(approval).await;+ self.held_calls.changed(+ approval.session,+ ToolApprovalChange::Held(HeldToolCall {+ approval_id: approval.id.to_string(),+ server_slug: approval.server_slug.clone(),+ server_name: approval.server_name.clone(),+ tool_name: approval.tool_name.clone(),+ }),+ );if let Err(error) = self.notify_owner(approval).await {tracing::warn!(error = ?error,@@ -98,6 +117,12 @@ whereasync fn resolved(&self, approval: &ToolApproval) {self.record(approval).await;+ self.held_calls.changed(+ approval.session,+ ToolApprovalChange::Settled {+ approval_id: approval.id.to_string(),+ },+ );}}diff --git a/crates/agent_harness/src/outbound/tool_approvals/test.rs b/crates/agent_harness/src/outbound/tool_approvals/test.rsindex 12d647b43b..20a9f26961 100644--- a/crates/agent_harness/src/outbound/tool_approvals/test.rs+++ b/crates/agent_harness/src/outbound/tool_approvals/test.rs@@ -101,6 +101,15 @@ impl AgentSessionNotifier for RecordingNotifier {}}+#[derive(Clone, Default)]+struct RecordingHeldCalls(Arc<Mutex<Vec<(AgentSessionId, ToolApprovalChange)>>>);++impl HeldToolCallObserver for RecordingHeldCalls {+ fn changed(&self, session: AgentSessionId, change: ToolApprovalChange) {+ self.0.lock().unwrap().push((session, change));+ }+}+fn approval(session: AgentSessionId, status: ToolApprovalStatus) -> ToolApproval {ToolApproval {id: ToolApprovalId::mint(),@@ -127,7 +136,7 @@ fn notice_of(message: &ToServerMessage) -> ToolApprovalNotice {}#[tokio::test]-async fn every_state_of_a_held_call_is_recorded_in_the_session_log() {+async fn every_state_of_a_held_call_is_recorded_and_told_to_the_harness() {let session = AgentSessionId::new();let recorded: Arc<Mutex<Vec<ToolApprovalNotice>>> = Arc::default();let mut sessions = MockAgentSessionService::new();@@ -146,7 +155,8 @@ async fn every_state_of_a_held_call_is_recorded_in_the_session_log() {.expect_get_session().returning(move |_| Box::pin(async move { Err(AgentSessionError::Disconnected(session)) }));let notifier = RecordingNotifier::default();- let announcer = SessionToolApprovalAnnouncer::new(sessions, notifier.clone());+ let held = RecordingHeldCalls::default();+ let announcer = SessionToolApprovalAnnouncer::new(sessions, notifier.clone(), held.clone());let pending = approval(session, ToolApprovalStatus::Pending);announcer.requested(&pending, &pending.owner).await;@@ -167,4 +177,24 @@ async fn every_state_of_a_held_call_is_recorded_in_the_session_log() {);assert_eq!(recorded[0].approval_id, pending.id.to_string());assert!(notifier.0.lock().unwrap().is_empty());+ assert_eq!(+ *held.0.lock().unwrap(),+ [+ (+ session,+ ToolApprovalChange::Held(HeldToolCall {+ approval_id: pending.id.to_string(),+ server_slug: "macro".to_owned(),+ server_name: "Macro".to_owned(),+ tool_name: "ListEmails".to_owned(),+ })+ ),+ (+ session,+ ToolApprovalChange::Settled {+ approval_id: pending.id.to_string(),+ }+ ),+ ]+ );}diff --git a/services/agent_harness_service/src/main.rs b/services/agent_harness_service/src/main.rsindex 7da454a3a9..3ea4014f52 100644--- a/services/agent_harness_service/src/main.rs+++ b/services/agent_harness_service/src/main.rs@@ -49,6 +49,7 @@ use agent_harness::domain::model::{};use agent_harness::domain::model_load::AgentModelsServiceImpl;use agent_harness::domain::ports::AgentRuntimeDirectory as _;+use agent_harness::domain::ports::LateBoundHeldToolCallObserver;use agent_harness::domain::service::AgentHarnessService;use agent_harness::domain::trigger_router::{RoutedTrigger, agent_trigger_bot_id, route_agent_trigger,@@ -452,12 +453,16 @@ async fn run() -> anyhow::Result<()> {// for the owner. The rows and their NOTIFY wake a hold on whichever// replica serves it; each state lands in the session's log for its// viewers, and a call that starts waiting notifies the owner.+ // The harness hears about each held call too, to tell the thread that+ // prompted the turn; it is built later, so it binds late.+ let held_tool_calls = Arc::new(LateBoundHeldToolCallObserver::new());let tool_approvals = Arc::new(ToolApprovalService::new(PgToolApprovalStore::new(pool.clone()),PgToolApprovalSignals::spawn(pool.clone()),SessionToolApprovalAnnouncer::new(sessions.clone(),IngressAgentSessionNotifier::new(Arc::clone(¬ifications)),+ held_tool_calls.clone(),),));let egress = Arc::new(@@ -1048,6 +1053,7 @@ async fn run() -> anyhow::Result<()> {// Close the loop: turn ends observed by the session actors drain the// harness's prompt queue, and capture what the turn changed.turn_observer.bind((harness.clone(), CaptureOnTurnEnd::new(changes.clone())));+ held_tool_calls.bind(harness.clone());let runtime_command_models = macrod_models.clone();let runtime_command_redis = redis.clone();let runtime_command_harness = harness.clone();@@ -1465,11 +1471,13 @@ async fn run() -> anyhow::Result<()> {"agent_trigger.set_sandbox_size"}// Never trigger-borne: queue mutations arrive over- // HTTP, and the turn signals are the harness's own.+ // HTTP, and the turn signals and held tool calls are+ // the harness's own.HarnessCommand::EditQueued { .. }| HarnessCommand::RemoveQueued { .. }| HarnessCommand::SteerQueued { .. }| HarnessCommand::Turn(_)+ | HarnessCommand::ToolApproval(_)| HarnessCommand::SessionStopped { .. } => "agent_trigger.unexpected",};tracing::Span::current().record("macro.event.type", event_type);