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): skip the opening line for replies posted to a thread (#8508)
Wolf Mermelsteinee2ab37d8d16
diff --git a/crates/agent_inmem/src/domain/agent.rs b/crates/agent_inmem/src/domain/agent.rsindex bc81939908..5900e55f81 100644--- a/crates/agent_inmem/src/domain/agent.rs+++ b/crates/agent_inmem/src/domain/agent.rs@@ -47,7 +47,9 @@ use tokio_util::sync::CancellationToken;use tracing::Instrument as _;use crate::domain::admission::admit_turn;-use crate::domain::engine::{AgentIdentity, AwaitingUser, TurnEngine, TurnPurpose, TurnRequest};+use crate::domain::engine::{+ AgentIdentity, AwaitingUser, ReplyDelivery, TurnEngine, TurnPurpose, TurnRequest,+};use crate::domain::mcp::{DynMcpToolConnector, dialable_servers};use crate::domain::model_options::REASONING_EFFORT_CONFIG_ID;use crate::domain::session::{HistoryEntry, SessionStore, UserPrompt, messages_for_turn};@@ -123,6 +125,8 @@ pub struct AgentState {pub session_id: AgentSessionId,/// The session's owner; turns run on their behalf.pub owner: Owner,+ /// How the session's replies reach their reader.+ pub delivery: ReplyDelivery,/// Runs the actual turns.pub engine: Arc<dyn TurnEngine>,/// Admission for new provider-backed turns, including direct ACP requests.@@ -720,6 +724,7 @@ pub async fn serve(state: Arc<AgentState>, acp: AcpChannel) -> Result<(), AcpErr"agent.acp.prompt",agent.session.id = %state.session_id,gen_ai.conversation.id = %state.session_id,+ agent.turn.reply_delivery = ?state.delivery,agent.turn.ttft_ms = tracing::field::Empty,agent.turn.first_text_ms = tracing::field::Empty,agent.turn.admission_wait_ms = tracing::field::Empty,@@ -1014,6 +1019,7 @@ async fn run_turn(let requester = user_input_requester(state, connection, acp_session_id.clone(), &awaiting);let mut parts = state.engine.run_turn(TurnRequest {purpose: TurnPurpose::Conversation,+ delivery: state.delivery,session_id: state.session_id,awaiting: Arc::clone(&awaiting),owner: state.owner.clone(),diff --git a/crates/agent_inmem/src/domain/agent/compaction.rs b/crates/agent_inmem/src/domain/agent/compaction.rsindex 468edc5284..28a7791d70 100644--- a/crates/agent_inmem/src/domain/agent/compaction.rs+++ b/crates/agent_inmem/src/domain/agent/compaction.rs@@ -581,6 +581,7 @@ async fn complete(let _stop = call.clone().drop_guard();let mut parts = state.engine.run_turn(TurnRequest {purpose: TurnPurpose::Summary,+ delivery: state.delivery,session_id: state.session_id,awaiting: Arc::new(AwaitingUser::default()),owner: state.owner.clone(),diff --git a/crates/agent_inmem/src/domain/agent/test.rs b/crates/agent_inmem/src/domain/agent/test.rsindex d049ecfd8e..7a1401eaa7 100644--- a/crates/agent_inmem/src/domain/agent/test.rs+++ b/crates/agent_inmem/src/domain/agent/test.rs@@ -142,6 +142,7 @@ async fn record<Engine: TurnEngine, Out>(crate::domain::session::SessionState::new("anthropic/claude-sonnet-5-5".into()),);let state = Arc::new(AgentState {+ delivery: crate::domain::engine::ReplyDelivery::Streamed,session_id,owner: model_owner::Owner::User(MacroUserIdStr::try_from_email("[email protected]").expect("a valid user id"),@@ -1814,6 +1815,7 @@ async fn serve_with_mcp(crate::domain::session::SessionState::new("anthropic/claude-sonnet-5-5".into()),);let state = Arc::new(AgentState {+ delivery: crate::domain::engine::ReplyDelivery::Streamed,session_id,owner: model_owner::Owner::User(MacroUserIdStr::try_from_email("[email protected]").expect("a valid user id"),@@ -1871,6 +1873,7 @@ async fn session_new_dials_the_advertised_servers_except_macros_own() {crate::domain::session::SessionState::new("anthropic/claude-sonnet-5-5".into()),);let state = Arc::new(AgentState {+ delivery: crate::domain::engine::ReplyDelivery::Streamed,session_id,owner: model_owner::Owner::User(MacroUserIdStr::try_from_email("[email protected]").expect("a valid user id"),@@ -1961,6 +1964,7 @@ async fn first_prompt_waits_for_background_mcp_connect() {crate::domain::session::SessionState::new("anthropic/claude-sonnet-5-5".into()),);let state = Arc::new(AgentState {+ delivery: crate::domain::engine::ReplyDelivery::Streamed,session_id,owner: model_owner::Owner::User(MacroUserIdStr::try_from_email("[email protected]").expect("a valid user id"),@@ -2066,6 +2070,7 @@ wherecrate::domain::session::SessionState::new("anthropic/claude-sonnet-5-5".into()),);let state = Arc::new(AgentState {+ delivery: crate::domain::engine::ReplyDelivery::Streamed,session_id,owner: model_owner::Owner::User(MacroUserIdStr::try_from_email("[email protected]").expect("a valid user id"),diff --git a/crates/agent_inmem/src/domain/engine.rs b/crates/agent_inmem/src/domain/engine.rsindex abce24acc9..a7f65767a5 100644--- a/crates/agent_inmem/src/domain/engine.rs+++ b/crates/agent_inmem/src/domain/engine.rs@@ -37,6 +37,8 @@ pub struct AgentIdentity {pub struct TurnRequest {/// Whether the turn answers the user or condenses their conversation.pub purpose: TurnPurpose,+ /// How the session's reader receives the reply.+ pub delivery: ReplyDelivery,/// The session the turn belongs to.pub session_id: agent_session::domain::model::AgentSessionId,/// Counts what the turn is waiting on a person for - a question, or a@@ -95,6 +97,18 @@ pub enum TurnPurpose {Summary,}+/// How the session's reader receives a reply.+#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub enum ReplyDelivery {+ /// Live, as the model writes it, so a fast model's opening line shows+ /// long before the turn's own first token.+ Streamed,+ /// Only once the turn has finished, as one message (a reply posted to the+ /// thread of the message that mentioned the agent). An opening line would+ /// arrive with the rest, so none is written.+ Whole,+}+/// How many things a turn currently waits on a person for. Shared between/// whatever is waiting (a question to the user, a tool call held for the/// owner), which counts itself in, and the turn loop, which reads it to telldiff --git a/crates/agent_inmem/src/outbound/local_attachments/test.rs b/crates/agent_inmem/src/outbound/local_attachments/test.rsindex eed6747ec5..9fb9f45644 100644--- a/crates/agent_inmem/src/outbound/local_attachments/test.rs+++ b/crates/agent_inmem/src/outbound/local_attachments/test.rs@@ -145,6 +145,7 @@ fn message(uri: &str) -> ChatMessage {fn request(messages: Vec<ChatMessage>) -> TurnRequest {TurnRequest {+ delivery: crate::domain::engine::ReplyDelivery::Streamed,purpose: crate::domain::engine::TurnPurpose::Conversation,session_id: agent_session::domain::model::AgentSessionId::new(),awaiting: std::sync::Arc::default(),diff --git a/crates/agent_inmem/src/outbound/manager.rs b/crates/agent_inmem/src/outbound/manager.rsindex 1601879c52..ee39f6d5ec 100644--- a/crates/agent_inmem/src/outbound/manager.rs+++ b/crates/agent_inmem/src/outbound/manager.rs@@ -19,7 +19,7 @@ use dashmap::DashMap;use model_owner::Owner;use crate::domain::agent::{AgentState, serve};-use crate::domain::engine::{AgentIdentity, TurnEngine};+use crate::domain::engine::{AgentIdentity, ReplyDelivery, TurnEngine};use crate::domain::mcp::DynMcpToolConnector;use crate::domain::replay::{FrameSource, replay_history, replay_reasoning_effort, replay_speed};use crate::domain::session::{SessionState, SessionStore, Summarizing};@@ -34,6 +34,8 @@ pub struct SessionFacts {pub id: AgentSessionId,/// The session's owner; turns act on their behalf.pub owner: Owner,+ /// How the session's replies reach their reader.+ pub delivery: ReplyDelivery,/// Model id stamped on the session row.pub model: String,/// The bot's display name and `@` handle, folded into every turn's@@ -199,6 +201,7 @@ impl InMemAgentManager {let state = Arc::new(AgentState {session_id: facts.id,owner: facts.owner,+ delivery: facts.delivery,engine: Arc::clone(&self.engine),admission: Arc::clone(&self.admission),agent_keys: Arc::clone(&self.agent_keys),diff --git a/crates/agent_inmem/src/outbound/manager/test.rs b/crates/agent_inmem/src/outbound/manager/test.rsindex ccabd0c0ac..ff1ae287e1 100644--- a/crates/agent_inmem/src/outbound/manager/test.rs+++ b/crates/agent_inmem/src/outbound/manager/test.rs@@ -183,6 +183,7 @@ fn manager(repo: &InMemoryAgentSessionRepo, engine: Arc<ScriptedEngine>) -> InMefn facts(id: AgentSessionId) -> SessionFacts {SessionFacts {+ delivery: crate::domain::engine::ReplyDelivery::Streamed,id,owner: Owner::User(owner()),model: "anthropic/claude-sonnet-5-5".to_owned(),@@ -195,6 +196,7 @@ fn facts(id: AgentSessionId) -> SessionFacts {/// The same facts, with the session's row carrying `instructions`.fn facts_with_instructions(id: AgentSessionId, instructions: &str) -> SessionFacts {SessionFacts {+ delivery: crate::domain::engine::ReplyDelivery::Streamed,instructions: Some(instructions.to_owned()),..facts(id)}@@ -202,6 +204,7 @@ fn facts_with_instructions(id: AgentSessionId, instructions: &str) -> SessionFacfn facts_with_identity(id: AgentSessionId, name: &str, handle: &str) -> SessionFacts {SessionFacts {+ delivery: crate::domain::engine::ReplyDelivery::Streamed,identity: Some(AgentIdentity {bot: BotId::TEST_A,name: name.to_owned(),@@ -569,6 +572,7 @@ async fn a_restarted_manager_rebuilds_the_conversation_from_the_log() {let transport = after.attach(SessionFacts {+ delivery: crate::domain::engine::ReplyDelivery::Streamed,acp_session_id: row.acp_session_id.clone(),..facts(id)},diff --git a/crates/agent_inmem/src/outbound/manager/test/summary.rs b/crates/agent_inmem/src/outbound/manager/test/summary.rsindex 06bf1bc537..c27b820106 100644--- a/crates/agent_inmem/src/outbound/manager/test/summary.rs+++ b/crates/agent_inmem/src/outbound/manager/test/summary.rs@@ -130,6 +130,7 @@ async fn a_restarted_manager_reads_a_summary_in_place_of_the_turns_it_covered().await.expect("the session row should create");let on_model = |acp_session_id| SessionFacts {+ delivery: crate::domain::engine::ReplyDelivery::Streamed,model: MODEL.to_owned(),acp_session_id,..facts(id)diff --git a/crates/agent_inmem/src/outbound/rig_engine/measure_prompt_cache.rs b/crates/agent_inmem/src/outbound/rig_engine/measure_prompt_cache.rsindex d8930bf080..b08299c9f1 100644--- a/crates/agent_inmem/src/outbound/rig_engine/measure_prompt_cache.rs+++ b/crates/agent_inmem/src/outbound/rig_engine/measure_prompt_cache.rs@@ -95,6 +95,7 @@ async fn turn(&native.prompt,"",agent_session::domain::model::AgentSessionId::new(),+ ReplyDelivery::Streamed,Some(&identity),Some(instructions),None,@@ -252,6 +253,7 @@ async fn measure_prompt_cache_turns() {&native.prompt,&sdk_docs,session_id,+ ReplyDelivery::Streamed,Some(&identity),None,None,diff --git a/crates/agent_inmem/src/outbound/rig_engine/opener.rs b/crates/agent_inmem/src/outbound/rig_engine/opener.rsindex 7dba261177..843776acbc 100644--- a/crates/agent_inmem/src/outbound/rig_engine/opener.rs+++ b/crates/agent_inmem/src/outbound/rig_engine/opener.rs@@ -29,6 +29,12 @@ pub(super) const VERDICT_TIMEOUT: Duration = Duration::from_millis(1500);/// How long the rest of the line may take once the verdict is in.pub(super) const LINE_TIMEOUT: Duration = Duration::from_secs(2);+/// Told to the turn's model when no opening line is shown, since the shared+/// session prompt says one may be.+pub(super) const NOT_OPENED: &str = "In this session no faster model opens your replies: \+nothing is shown before your reply, so its first line is yours to write. Still begin with \+substance.";+const MAX_TOKENS: u64 = 64;/// The tail of the conversation the fast model reads: enough to tell "thanks"diff --git a/crates/agent_inmem/src/outbound/rig_engine/test.rs b/crates/agent_inmem/src/outbound/rig_engine/test.rsindex 794974fdf1..8036261876 100644--- a/crates/agent_inmem/src/outbound/rig_engine/test.rs+++ b/crates/agent_inmem/src/outbound/rig_engine/test.rs@@ -190,7 +190,16 @@ async fn shared_definitions_keep_the_callers_context_and_check_each_sessions_gat#[test]fn instructions_are_a_delimited_section_after_the_standing_prompt() {let session = AgentSessionId::new();- let prompt = system_prompt(&TOOLS, "", session, None, Some("be terse"), None).to_string();+ let prompt = system_prompt(+ &TOOLS,+ "",+ session,+ ReplyDelivery::Streamed,+ None,+ Some("be terse"),+ None,+ )+ .to_string();assert!(prompt.starts_with(&prompt::agent_session::PROMPT.to_string()),@@ -221,7 +230,15 @@ fn identity_opens_the_session_part_and_does_not_need_instructions() {name: "Grunk".to_owned(),handle: "grunk".to_owned(),};- let prompt = system_prompt(&TOOLS, "", session, Some(&identity), None, None);+ let prompt = system_prompt(+ &TOOLS,+ "",+ session,+ ReplyDelivery::Streamed,+ Some(&identity),+ None,+ None,+ );let identity_section = prompt::agent_identity::render("Grunk", "grunk");assert!(@@ -241,6 +258,7 @@ fn memory_follows_instructions_rather_than_preceding_them() {&TOOLS,"",session,+ ReplyDelivery::Streamed,None,Some("be terse"),Some("prefers Rust"),@@ -271,11 +289,20 @@ fn sessions_share_everything_before_their_identity() {&TOOLS,"<macro_sdk>documents</macro_sdk>",session,+ ReplyDelivery::Streamed,Some(&identity),Some("task A"),Some("prefers Rust"),);- let second = system_prompt(&TOOLS, "", session, None, Some("task B"), None);+ let second = system_prompt(+ &TOOLS,+ "",+ session,+ ReplyDelivery::Streamed,+ None,+ Some("task B"),+ None,+ );let shared = format!("{}{TOOLS}", prompt::agent_session::PROMPT);assert_eq!(first.shared(), Some(shared.as_str()));@@ -304,7 +331,16 @@ fn sessions_share_everything_before_their_identity() {#[test]fn no_instructions_means_no_section() {let session = AgentSessionId::new();- let prompt = system_prompt(&TOOLS, "", session, None, None, Some("prefers Rust")).to_string();+ let prompt = system_prompt(+ &TOOLS,+ "",+ session,+ ReplyDelivery::Streamed,+ None,+ None,+ Some("prefers Rust"),+ )+ .to_string();assert!(!prompt.contains("session_instructions"));assert!(prompt.contains("<user_memory>\nprefers Rust\n</user_memory>"));@@ -315,11 +351,41 @@ fn no_instructions_means_no_section() {#[test]fn empty_instructions_add_no_section() {let session = AgentSessionId::new();- let prompt = system_prompt(&TOOLS, "", session, None, Some(""), None).to_string();+ let prompt = system_prompt(+ &TOOLS,+ "",+ session,+ ReplyDelivery::Streamed,+ None,+ Some(""),+ None,+ )+ .to_string();assert!(!prompt.contains("session_instructions"));}+/// A reply read only once finished gets no opening line, so its session part+/// says so; the cached block every session shares stays the same.+#[test]+fn a_whole_reply_is_told_no_opening_line_was_shown_without_splitting_the_cache() {+ let session = AgentSessionId::new();+ let streamed = system_prompt(+ &TOOLS,+ "",+ session,+ ReplyDelivery::Streamed,+ None,+ None,+ None,+ );+ let whole = system_prompt(&TOOLS, "", session, ReplyDelivery::Whole, None, None, None);++ assert_eq!(streamed.shared(), whole.shared());+ assert!(!streamed.to_string().contains(opener::NOT_OPENED));+ assert!(whole.to_string().contains(opener::NOT_OPENED));+}+#[tokio::test]async fn opening_verdict_split_across_deltas_keeps_the_head_of_the_line() {let (lines, mut receiver) = mpsc::channel(8);diff --git a/crates/agent_inmem/src/outbound/rig_engine/test/tool_mode.rs b/crates/agent_inmem/src/outbound/rig_engine/test/tool_mode.rsindex 7e481d4bd9..2e224c96a4 100644--- a/crates/agent_inmem/src/outbound/rig_engine/test/tool_mode.rs+++ b/crates/agent_inmem/src/outbound/rig_engine/test/tool_mode.rs@@ -47,6 +47,7 @@ async fn remote(fn request(mcp_tools: Option<RemoteMcpToolSet>) -> TurnRequest {TurnRequest {+ delivery: crate::domain::engine::ReplyDelivery::Streamed,purpose: TurnPurpose::Conversation,session_id: AgentSessionId::new(),awaiting: Arc::new(AwaitingUser::default()),diff --git a/crates/agent_inmem/src/rig_engine.rs b/crates/agent_inmem/src/rig_engine.rsindex 4797a98c1e..f2442bda45 100644--- a/crates/agent_inmem/src/rig_engine.rs+++ b/crates/agent_inmem/src/rig_engine.rs@@ -38,7 +38,7 @@ use tokio::sync::mpsc;use tracing::Instrument as _;use crate::domain::auto_model::{AutoModelSelector, DefaultAutoModel, JevAutoModel};-use crate::domain::engine::{AgentIdentity, TurnEngine, TurnRequest};+use crate::domain::engine::{AgentIdentity, ReplyDelivery, TurnEngine, TurnRequest};use crate::domain::tool_gate::{NativeToolGate, NativeToolVerdict, UngatedNativeTools};use crate::domain::tool_mode::{StandardTools, ToolMode, ToolModeSelector};use crate::inbound::ask_user::{AskUser, AskUserContext};@@ -449,6 +449,7 @@ async fn drive_turn() -> Result<(), AgentError> {let TurnRequest {purpose,+ delivery,session_id,awaiting,owner,@@ -492,6 +493,10 @@ async fn drive_turn("the agent's API key does not serve {model}")));}+ // The opening line runs on Macro's own fast model, so a turn the agent's+ // key pays for goes without one; so does a reply read only once it is+ // finished, which gains nothing from it.+ let opening = !summarizing && agent_key.is_none() && delivery == ReplyDelivery::Streamed;// Started before anything else so its line is out before the turn's// model has a first token.@@ -505,7 +510,7 @@ async fn drive_turn(// reply), `silent` (no line fit this follow-up), `turn_first` (the// turn's model spoke first), `no_verdict` (the// fast model failed or answered without one) or `timeout`.- let opener_span = if summarizing {+ let opener_span = if !opening {tracing::Span::none()} else {tracing::info_span!(@@ -518,9 +523,7 @@ async fn drive_turn()};// A closed receiver has no verdict, and the turn's own stream follows.- // The opening line runs on Macro's own fast model, so a turn the agent's- // key pays for goes without one.- let mut opening_line = if summarizing || agent_key.is_some() {+ let mut opening_line = if !opening {mpsc::channel(1).1} else {opener_span.in_scope(|| {@@ -570,6 +573,7 @@ async fn drive_turn(&native_tools.prompt,&sdk_docs,session_id,+ delivery,identity.as_ref(),instructions.as_deref(),user_memory.as_deref(),@@ -842,7 +846,8 @@ fn elapsed_ms(since: tokio::time::Instant) -> u64 {/// The turn's system prompt, ordered so the longest possible prefix is/// cached: the agent-session preamble (which notes that an opening line may-/// already be out, see [`opener`]) and the static Macro prompt (how to use the+/// already be out, see [`opener`]; a session whose replies are read whole is+/// told otherwise in its own part) and the static Macro prompt (how to use the/// product: mentions, tools, terminology), which are the same for every/// session of every agent; then the agent's identity, the session's own/// instructions when there are any, which session this is, and the owner's@@ -862,6 +867,7 @@ fn system_prompt(tools_prompt: &impl std::fmt::Display,sdk_docs: &str,session_id: agent_session::domain::model::AgentSessionId,+ delivery: ReplyDelivery,identity: Option<&AgentIdentity>,instructions: Option<&str>,user_memory: Option<&str>,@@ -891,6 +897,10 @@ fn system_prompt(words of anything said earlier, summarized or not, run ContentSearch with \agentSessionIds [\"{session_id}\"]. ContentSearch remains a direct tool for scoped session-history search.\n</agent_session>"));+ if delivery == ReplyDelivery::Whole {+ rest.push('\n');+ rest.push_str(opener::NOT_OPENED);+ }if let Some(memory) = user_memory {rest.push_str("\n<user_memory>\n");rest.push_str(memory);diff --git a/docs/agent-guide/ai-chat.md b/docs/agent-guide/ai-chat.mdindex 8e5bbf8191..ec6af25e2d 100644--- a/docs/agent-guide/ai-chat.md+++ b/docs/agent-guide/ai-chat.md@@ -83,6 +83,11 @@ words, never the prompt's hidden context. The sent prompt is fully opaque fromthe first paint, and its `Context` chip is already in place, so nothing shiftswhen the server confirms it.+A session started by mentioning the agent in a thread (for example `@macro` in a+channel) gets no opener. Its thread reply is one message that updates only as+each passage finishes, so the line would arrive with the text after it. The+reply starts with the chosen model's answer.+Repeat from a fresh tab using Home, Agents, and a document's Chat action.Focusing an agent composer prepares its transcript renderer locally; focus alonemust not send a prompt. Record navigation and focus time separately from typingdiff --git a/services/agent_harness_service/src/containers.rs b/services/agent_harness_service/src/containers.rsindex 5e3223ee95..db81904e43 100644--- a/services/agent_harness_service/src/containers.rs+++ b/services/agent_harness_service/src/containers.rs@@ -14,7 +14,7 @@ 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::domain::engine::AgentIdentity;+use agent_inmem::domain::engine::{AgentIdentity, ReplyDelivery};use agent_inmem::outbound::manager::{InMemAgentManager, SessionFacts};use agent_runtime_protocol::domain::connection::ServerChannel;use agent_runtime_protocol::domain::ports::{Transport, TransportError, TransportSender};@@ -133,6 +133,13 @@ wherereturn Ok(Route::InMem(Box::new(SessionFacts {id: session,owner: row.owner_id,+ // A thread's reply is one message, rewritten only as each+ // passage finishes; an opening line lands with what follows it.+ delivery: if row.thread_id.is_some() {+ ReplyDelivery::Whole+ } else {+ ReplyDelivery::Streamed+ },model: row.model,identity: Some(AgentIdentity {bot: row.bot_id,