Skip to content

armgit / macro

Public Imported from GitHub
0

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

fix(agent_harness): stop double-notifying users mentioned in a channel prompt (#7084)

teocd087d50be36
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/index.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/index.ts
index bff6f8e041..ff94c33627 100644
--- a/apps/web/src/lib/service-clients/service-storage/generated/schemas/index.ts
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/index.ts
@@ -1210,6 +1210,7 @@ export * from './sessionIdentity';
export * from './sessionIdentityOrigin';
export * from './sessionMentionedMetadata';
export * from './sessionMentionedMetadataMentionedBy';
+export * from './sessionMentionedMetadataOriginMessageId';
export * from './sessionOpenedMetadata';
export * from './sessionRenamedMetadata';
export * from './sessionSettledMetadata';
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/sessionMentionedMetadata.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sessionMentionedMetadata.ts
index 70a0d12833..c1de3fd4c0 100644
--- a/apps/web/src/lib/service-clients/service-storage/generated/schemas/sessionMentionedMetadata.ts
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sessionMentionedMetadata.ts
@@ -8,6 +8,7 @@ import type { AgentActionId } from './agentActionId';
import type { MacroUserIdStr } from './macroUserIdStr';
import type { SessionIdentity } from './sessionIdentity';
import type { SessionMentionedMetadataMentionedBy } from './sessionMentionedMetadataMentionedBy';
+import type { SessionMentionedMetadataOriginMessageId } from './sessionMentionedMetadataOriginMessageId';
/**
* A prompt named other users who can open the session. Published when the
@@ -23,4 +24,8 @@ export interface SessionMentionedMetadata {
and never including the author. */
mentioned: MacroUserIdStr[];
mentioned_by?: SessionMentionedMetadataMentionedBy;
+ /** The channel or document message the prompt was posted as, when it
+arrived from a thread rather than the session view. That message
+already notified the users it named when it was posted. */
+ origin_message_id?: SessionMentionedMetadataOriginMessageId;
}
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/sessionMentionedMetadataOriginMessageId.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sessionMentionedMetadataOriginMessageId.ts
new file mode 100644
index 0000000000..4a4c94f4bc
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/sessionMentionedMetadataOriginMessageId.ts
@@ -0,0 +1,13 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * The channel or document message the prompt was posted as, when it
+arrived from a thread rather than the session view. That message
+already notified the users it named when it was posted.
+ */
+export type SessionMentionedMetadataOriginMessageId = string | null;
diff --git a/apps/web/src/lib/service-clients/service-storage/openapi.json b/apps/web/src/lib/service-clients/service-storage/openapi.json
index e33b6a8cd9..dde88124d0 100644
--- a/apps/web/src/lib/service-clients/service-storage/openapi.json
+++ b/apps/web/src/lib/service-clients/service-storage/openapi.json
@@ -28937,6 +28937,11 @@
"description": "Who wrote the prompt, absent when a bot acted on nobody's behalf."
}
]
+ },
+ "origin_message_id": {
+ "type": ["string", "null"],
+ "format": "uuid",
+ "description": "The channel or document message the prompt was posted as, when it\narrived from a thread rather than the session view. That message\nalready notified the users it named when it was posted."
}
}
},
diff --git a/crates/agent_harness/src/domain/notifications.rs b/crates/agent_harness/src/domain/notifications.rs
index bffdf24282..ccfc4f01a5 100644
--- a/crates/agent_harness/src/domain/notifications.rs
+++ b/crates/agent_harness/src/domain/notifications.rs
@@ -12,7 +12,7 @@
//! | --- | --- | --- |
//! | `settled` | [`AgentSessionSettledMetadata`] | the session's audience: owner plus everyone who has driven it - unless the turn was a chat agent's announced reply, whose patched message already notifies the thread |
//! | `waiting_for_input` | [`AgentSessionWaitingForInputMetadata`] | the same audience - anyone with edit access may answer, and these are the people already driving it - with the same exception for a chat agent's announced turn, whose reply is patched to say it is waiting |
-//! | `mentioned` | [`AgentSessionMentionedMetadata`] | the users the prompt named, who can now open the session |
+//! | `mentioned` | [`AgentSessionMentionedMetadata`] | the users the prompt named, who can now open the session - unless the prompt was posted as a channel or document message, whose own mention notification already reached them |
//!
//! Everything else is nobody's news.
//!
@@ -28,6 +28,15 @@
//! and a chat turn nobody announced - one driven from the session view -
//! has no message to speak through, so both keep every notification.
//!
+//! A mention has the same shape of exception, and the fact does carry it: a
+//! prompt that arrived as a channel or document message names its users in
+//! that message, and the message service notifies each of them of the
+//! mention when the message is posted. Notifying them again for the session
+//! would tell the same people the same thing twice, so a mention with an
+//! origin message plans nothing; the users are still shared into the
+//! session. A prompt typed into the session view has no message to have
+//! spoken for it, so it keeps its notification.
+//!
//! Retracting a notification once it is stale (the question answered, the
//! next turn started) is deliberately not done yet: the notification ingress
//! has no producer-facing way to mark done, and adding one is its own change.
@@ -205,6 +214,11 @@ fn plan_mentioned(mentioned: &SessionMentionedMetadata) -> Vec<PlannedNotificati
if mentioned.mentioned.is_empty() {
return Vec::new();
}
+ // The message the prompt was posted as already notified the people it
+ // named, on the post itself.
+ if mentioned.origin_message_id.is_some() {
+ return Vec::new();
+ }
let (entity, secondary_entity) = entities(&mentioned.identity);
vec![PlannedNotification::Mentioned(Notify {
notification_id: mentioned_notification_id(
diff --git a/crates/agent_harness/src/domain/notifications/test.rs b/crates/agent_harness/src/domain/notifications/test.rs
index f8e1eef8d4..d94387e312 100644
--- a/crates/agent_harness/src/domain/notifications/test.rs
+++ b/crates/agent_harness/src/domain/notifications/test.rs
@@ -276,6 +276,7 @@ fn mentioned_notifies_exactly_the_people_named() {
],
+ origin_message_id: None,
},
));
@@ -302,12 +303,35 @@ fn a_mention_of_nobody_is_nothing() {
action_id: AgentActionId::mint(),
mentioned_by: Some(owner()),
mentioned: Vec::new(),
+ origin_message_id: None,
},
));
assert!(actions.is_empty());
}
+/// A prompt posted as a channel or document message already notified the
+/// users it named on the post itself; the session does not tell them twice.
+/// Whether the bot is a coding agent makes no difference: the message service
+/// notifies the mention regardless of who else the message names.
+#[test]
+fn a_mention_in_a_posted_message_is_already_announced() {
+ for is_coding in [true, false] {
+ let actions = plan(
+ &AgentSessionLifecycleEvent::Mentioned(SessionMentionedMetadata {
+ identity: identity(),
+ action_id: AgentActionId::mint(),
+ mentioned_by: Some(owner()),
+ mentioned: vec![user("[email protected]"), user("[email protected]")],
+ origin_message_id: Some(Uuid::from_u128(7)),
+ }),
+ is_coding,
+ );
+
+ assert!(actions.is_empty(), "is_coding={is_coding}: {actions:#?}");
+ }
+}
+
#[test]
fn facts_that_are_not_news_to_people_plan_nothing() {
for event in [
diff --git a/crates/agent_harness/src/domain/service/lifecycle_events.rs b/crates/agent_harness/src/domain/service/lifecycle_events.rs
index 8114aed69c..21be327503 100644
--- a/crates/agent_harness/src/domain/service/lifecycle_events.rs
+++ b/crates/agent_harness/src/domain/service/lifecycle_events.rs
@@ -9,6 +9,7 @@ use agent_session::domain::lifecycle::session_identity;
use crate::domain::model::{ReplyPersona, is_coding_agent};
use crate::domain::notifications::plan;
use macro_user_id::user_id::MacroUserIdStr;
+use macro_uuid::Uuid;
use super::*;
@@ -134,12 +135,18 @@ where
/// every lifecycle publish, a failure to resolve the mentions is logged
/// and the prompt goes on regardless - the mention is a courtesy to
/// whoever was named, not part of delivering the prompt.
+ ///
+ /// `origin_message_id` names the channel or document message the prompt
+ /// was posted as, when there is one. The fact carries it so the
+ /// notification plan can tell a prompt whose mentions the message service
+ /// already announced from one typed into the session view.
pub(super) async fn publish_mentions(
&self,
session_id: AgentSessionId,
action_id: AgentActionId,
actor: Option<MacroUserIdStr<'static>>,
prompt_markdown: &str,
+ origin_message_id: Option<Uuid>,
) {
let mentioned = match self
.mentions
@@ -169,6 +176,7 @@ where
action_id,
mentioned_by: actor,
mentioned,
+ origin_message_id,
})
})
.await;
diff --git a/crates/agent_harness/src/domain/service/queue.rs b/crates/agent_harness/src/domain/service/queue.rs
index bfd1699acf..1d79f10288 100644
--- a/crates/agent_harness/src/domain/service/queue.rs
+++ b/crates/agent_harness/src/domain/service/queue.rs
@@ -617,8 +617,14 @@ where
// Mentions are a fact about the prompt, not the turn: published as
// soon as the prompt is accepted, whether it dispatches now or waits.
if let Some(prompt) = prompt {
- self.publish_mentions(session_id, action_id, actor.clone(), &prompt)
- .await;
+ self.publish_mentions(
+ session_id,
+ action_id,
+ actor.clone(),
+ &prompt,
+ announce.as_ref().map(|origin| origin.message_id),
+ )
+ .await;
}
if steers {
diff --git a/crates/agent_harness/src/domain/service/test.rs b/crates/agent_harness/src/domain/service/test.rs
index 12b0f458fc..41adedb191 100644
--- a/crates/agent_harness/src/domain/service/test.rs
+++ b/crates/agent_harness/src/domain/service/test.rs
@@ -3562,6 +3562,8 @@ mod lifecycle_events {
use agent_runtime_protocol::domain::action::{ElicitationAnswer, ElicitationRequestId};
use agent_session::domain::events::AgentSessionLifecycleEvent as Lifecycle;
+ use crate::domain::notifications::PlannedNotification;
+
/// Open a session from a mention and let its first turn settle: `Opened`,
/// `TurnStarted`, `TurnEnded`, `Settled`.
async fn settled_session() -> (TestBench, TurnSignals, AgentSessionId, ContainerMock) {
@@ -3646,7 +3648,7 @@ mod lifecycle_events {
assert!(
matches!(
notified.as_slice(),
- [crate::domain::notifications::PlannedNotification::Settled(notify)]
+ [PlannedNotification::Settled(notify)]
if notify.recipients == vec![sender()]
&& notify.metadata.session.session_id == id.as_uuid()
),
@@ -3679,6 +3681,9 @@ mod lifecycle_events {
turns.lifecycle_published(6).await;
let events = turns.lifecycle();
+ let origin = forward_message("")
+ .announce
+ .expect("a channel prompt announces");
assert!(
matches!(
&events[4..6],
@@ -3686,6 +3691,7 @@ mod lifecycle_events {
if mentioned.identity.session_id == id
&& mentioned.mentioned_by == Some(staff_sender())
&& mentioned.mentioned == vec![reviewer.clone()]
+ && mentioned.origin_message_id == Some(origin.message_id)
),
"mentioned is published on accept, before the turn starts: {events:#?}"
);
@@ -3693,6 +3699,73 @@ mod lifecycle_events {
mentions.prompts().last(),
Some(&"@reviewer look".to_owned())
);
+ // The channel message that carried the prompt already notified the
+ // reviewer of the mention; the session does not tell them twice.
+ let notified = turns.notifier.notified();
+ assert!(
+ !notified
+ .iter()
+ .any(|notification| matches!(notification, PlannedNotification::Mentioned(_))),
+ "no mention notification for a prompt posted as a message: {notified:#?}"
+ );
+ }
+
+ #[tokio::test]
+ async fn a_prompt_from_the_session_view_notifies_the_people_it_names() {
+ let mentions = PromptMentionsMock::new();
+ let reviewer = MacroUserIdStr::try_from_email("[email protected]").unwrap();
+ let ((service, _, containers, _, _), turns) = harness_with_mentions(
+ PromptContextMock::default(),
+ PromptComposerMock::default(),
+ mentions.clone(),
+ );
+ let id = AgentSessionId::new();
+ let _container = live_session(&service, &containers, id).await;
+ turns.lifecycle_published(4).await;
+ mentions.mentions(vec![reviewer.clone()]);
+
+ // Typed into the session view: no message spoke for it anywhere.
+ service
+ .execute(
+ id,
+ HarnessCommand::Deliver(DeliverAction::prompt(
+ AgentAction::prompt("@reviewer look"),
+ Some(staff_sender()),
+ None,
+ )),
+ )
+ .await
+ .expect("the prompt is accepted");
+ turns.lifecycle_published(6).await;
+
+ let events = turns.lifecycle();
+ assert!(
+ matches!(
+ &events[4..6],
+ [Lifecycle::Mentioned(mentioned), Lifecycle::TurnStarted(_)]
+ if mentioned.mentioned == vec![reviewer.clone()]
+ && mentioned.origin_message_id.is_none()
+ ),
+ "mentioned is published without an origin message: {events:#?}"
+ );
+ // Beside whatever the first turn settled with: exactly one mention.
+ let notified = turns.notifier.notified();
+ let mentions_sent: Vec<_> = notified
+ .iter()
+ .filter_map(|notification| match notification {
+ PlannedNotification::Mentioned(notify) => Some(notify),
+ _ => None,
+ })
+ .collect();
+ assert!(
+ matches!(
+ mentions_sent.as_slice(),
+ [notify]
+ if notify.recipients == vec![reviewer.clone()]
+ && notify.metadata.session.session_id == id.as_uuid()
+ ),
+ "one mention notification for the reviewer: {notified:#?}"
+ );
}
#[tokio::test]
diff --git a/crates/agent_session/src/domain/events.rs b/crates/agent_session/src/domain/events.rs
index 15ec98d7ea..3a69637dfa 100644
--- a/crates/agent_session/src/domain/events.rs
+++ b/crates/agent_session/src/domain/events.rs
@@ -250,6 +250,11 @@ pub struct SessionMentionedMetadata {
/// The users named, already narrowed to those who can open the session
/// and never including the author.
pub mentioned: Vec<MacroUserIdStr<'static>>,
+ /// The channel or document message the prompt was posted as, when it
+ /// arrived from a thread rather than the session view. That message
+ /// already notified the users it named when it was posted.
+ #[serde(default)]
+ pub origin_message_id: Option<Uuid>,
}
/// The session's live actor is gone: idle teardown, transport loss, or crash.
diff --git a/crates/agent_session/src/domain/events/test.rs b/crates/agent_session/src/domain/events/test.rs
index ebed9b3170..ac3919f6c7 100644
--- a/crates/agent_session/src/domain/events/test.rs
+++ b/crates/agent_session/src/domain/events/test.rs
@@ -85,6 +85,7 @@ fn one_of_each() -> Vec<AgentSessionLifecycleEvent> {
.expect("valid user id")
.into_owned(),
],
+ origin_message_id: None,
}),
AgentSessionLifecycleEvent::Stopped(SessionStoppedMetadata {
identity: identity(),
diff --git a/packages/sdk/generated/storage/types.gen.ts b/packages/sdk/generated/storage/types.gen.ts
index 4199cd771b..dbc01f0c6c 100644
--- a/packages/sdk/generated/storage/types.gen.ts
+++ b/packages/sdk/generated/storage/types.gen.ts
@@ -8570,6 +8570,12 @@ export type SessionMentionedMetadata = {
*/
mentioned: Array<MacroUserIdStr>;
mentioned_by?: null | MacroUserIdStr;
+ /**
+ * The channel or document message the prompt was posted as, when it
+ * arrived from a thread rather than the session view. That message
+ * already notified the users it named when it was posted.
+ */
+ origin_message_id?: string | null;
};
/**
diff --git a/packages/sdk/specs/storage.json b/packages/sdk/specs/storage.json
index e33b6a8cd9..dde88124d0 100644
--- a/packages/sdk/specs/storage.json
+++ b/packages/sdk/specs/storage.json
@@ -28937,6 +28937,11 @@
"description": "Who wrote the prompt, absent when a bot acted on nobody's behalf."
}
]
+ },
+ "origin_message_id": {
+ "type": ["string", "null"],
+ "format": "uuid",
+ "description": "The channel or document message the prompt was posted as, when it\narrived from a thread rather than the session view. That message\nalready notified the users it named when it was posted."
}
}
},