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(teams): create DMs between teammates on join (#5848)
teoeb59a48c7936
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.jsonindex 4329036fd6..379f5d159f 100644--- a/.github/workspace-dep-closures.json+++ b/.github/workspace-dep-closures.json@@ -428,6 +428,7 @@"crates/contacts","crates/cowlike","crates/crm",+ "crates/database_env_vars","crates/document_sub_type","crates/documents","crates/dynamodb_client",@@ -614,6 +615,7 @@"crates/contacts","crates/cowlike","crates/crm",+ "crates/database_env_vars","crates/document_sub_type","crates/documents","crates/dynamodb_client",@@ -1192,6 +1194,7 @@"crates/contacts","crates/cowlike","crates/crm",+ "crates/database_env_vars","crates/document_sub_type","crates/documents","crates/dynamodb_client",@@ -2093,6 +2096,7 @@"crates/contacts","crates/cowlike","crates/crm",+ "crates/database_env_vars","crates/document_storage_service_client","crates/document_sub_type","crates/documents",@@ -2232,6 +2236,7 @@"crates/contacts","crates/cowlike","crates/crm",+ "crates/database_env_vars","crates/document_sub_type","crates/documents","crates/dynamodb_client",@@ -4787,6 +4792,7 @@"crates/contacts","crates/cowlike","crates/crm",+ "crates/database_env_vars","crates/document_sub_type","crates/documents","crates/dynamodb_client",@@ -7155,6 +7161,7 @@"crates/contacts","crates/cowlike","crates/crm",+ "crates/database_env_vars","crates/document_sub_type","crates/email","crates/email_utils",@@ -7171,6 +7178,7 @@"crates/last_online_tracker","crates/macro_auth","crates/macro_authorization",+ "crates/macro_entrypoint","crates/macro_env","crates/macro_env_var","crates/macro_event_broker",diff --git a/.sqlx/query-2bbd08beed02c3b4d5fbf7f77087c2fcee3e2ea81a6191d90f36364684af1ad3.json b/.sqlx/query-2bbd08beed02c3b4d5fbf7f77087c2fcee3e2ea81a6191d90f36364684af1ad3.jsonnew file mode 100644index 0000000000..83892c98b1--- /dev/null+++ b/.sqlx/query-2bbd08beed02c3b4d5fbf7f77087c2fcee3e2ea81a6191d90f36364684af1ad3.json@@ -0,0 +1,23 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT id\n FROM team\n WHERE $1::uuid IS NULL OR id > $1\n ORDER BY id\n LIMIT $2\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id",+ "type_info": "Uuid"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid",+ "Int8"+ ]+ },+ "nullable": [+ false+ ]+ },+ "hash": "2bbd08beed02c3b4d5fbf7f77087c2fcee3e2ea81a6191d90f36364684af1ad3"+}diff --git a/Cargo.lock b/Cargo.lockindex 0a1f18d24d..a69cc98bc2 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -3234,6 +3234,7 @@ dependencies = ["sqlx","thiserror 2.0.18","tokio",+ "tokio-retry","tower 0.5.3","tracing","tracing-subscriber",@@ -15723,10 +15724,12 @@ dependencies = ["channels","chrono","contacts",+ "database_env_vars","entity_access","generic_email_domains","macro_authorization","macro_db_migrator",+ "macro_entrypoint","macro_event_broker","macro_event_topics","macro_user_id",diff --git a/crates/channels/Cargo.toml b/crates/channels/Cargo.tomlindex b7394ecec0..d5be4adb6d 100644--- a/crates/channels/Cargo.toml+++ b/crates/channels/Cargo.toml@@ -33,6 +33,13 @@ inbound = ["ports","schema",]+kafka_consumer = [+ "dep:kafka_util",+ "dep:rdkafka",+ "dep:tokio-retry",+ "macro_event_broker/outbound",+ "ports",+]list = ["dep:filter_ast","dep:frecency",@@ -94,6 +101,7 @@ frecency = { path = "../frecency", optional = true }futures = { workspace = true, optional = true }indexmap = { workspace = true, optional = true }item_filters = { path = "../item_filters", optional = true }+kafka_util = { path = "../kafka_util", optional = true }macro_authorization = { path = "../macro_authorization", optional = true, default-features = false, features = ["axum",] }@@ -108,6 +116,7 @@ models_pagination = { path = "../models_pagination" }models_permissions = { path = "../models_permissions", optional = true }non_empty = { path = "../non_empty", optional = true }notification_hex = { path = "../notification", package = "notification", optional = true }+rdkafka = { workspace = true, optional = true }recursion = { workspace = true, optional = true }rootcause = { workspace = true, optional = true }serde = { workspace = true }@@ -116,6 +125,7 @@ share_permission_db_utils = { path = "../share_permission_db_utils", optional =sqlx = { workspace = true, optional = true }thiserror = { workspace = true }tokio = { workspace = true, features = ["macros", "sync"], optional = true }+tokio-retry = { workspace = true, optional = true }tracing = { workspace = true }utoipa = { workspace = true, optional = true }uuid = { workspace = true }diff --git a/crates/channels/src/domain/dm.rs b/crates/channels/src/domain/dm.rsnew file mode 100644index 0000000000..d4d4749eb9--- /dev/null+++ b/crates/channels/src/domain/dm.rs@@ -0,0 +1,130 @@+//! Direct-message identity and batch commands.++use macro_user_id::{cowlike::CowLike, user_id::MacroUserIdStr};+use std::collections::HashSet;++#[cfg(test)]+mod test;++/// Error returned when both sides of a direct-message pair are the same user.+#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]+#[error("a direct message requires two distinct users")]+pub struct SelfDm;++/// Two distinct users that identify one direct message. `(a, b)` equals `(b, a)`.+#[derive(Debug, Clone, PartialEq, Eq, Hash)]+pub struct DmPair {+ lo: MacroUserIdStr<'static>,+ hi: MacroUserIdStr<'static>,+}++impl DmPair {+ /// Construct a pair of distinct users. Argument order does not matter.+ pub fn new(a: MacroUserIdStr<'_>, b: MacroUserIdStr<'_>) -> Result<Self, SelfDm> {+ match a.as_ref().cmp(b.as_ref()) {+ std::cmp::Ordering::Less => Ok(Self {+ lo: a.into_owned(),+ hi: b.into_owned(),+ }),+ std::cmp::Ordering::Greater => Ok(Self {+ lo: b.into_owned(),+ hi: a.into_owned(),+ }),+ std::cmp::Ordering::Equal => Err(SelfDm),+ }+ }++ /// Return one side of the pair.+ pub fn lo(&self) -> &MacroUserIdStr<'static> {+ &self.lo+ }++ /// Return the other side of the pair.+ pub fn hi(&self) -> &MacroUserIdStr<'static> {+ &self.hi+ }+}++#[derive(Debug, Clone, PartialEq, Eq)]+pub(crate) struct EnsureDm {+ pub(crate) pair: DmPair,+ pub(crate) owner: MacroUserIdStr<'static>,+}++/// An opaque batch of direct messages to ensure.+#[derive(Debug, Clone, Default, PartialEq, Eq)]+pub struct EnsureDms {+ requests: Vec<EnsureDm>,+}++impl EnsureDms {+ pub(crate) fn into_requests(self) -> Vec<EnsureDm> {+ self.requests+ }+}++/// Build the direct-message star for one joining member and a team roster.+pub fn ensure_dms_for_joining_member(+ joiner: MacroUserIdStr<'static>,+ roster: impl IntoIterator<Item = MacroUserIdStr<'static>>,+) -> EnsureDms {+ let teammates = roster+ .into_iter()+ .filter(|user_id| user_id != &joiner)+ .collect::<HashSet<_>>();+ let mut requests = teammates+ .into_iter()+ .map(|teammate| EnsureDm {+ pair: DmPair::new(joiner.clone(), teammate)+ .expect("joining member was removed from the roster"),+ owner: joiner.clone(),+ })+ .collect::<Vec<_>>();+ sort_requests(&mut requests);+ EnsureDms { requests }+}++/// Build the complete direct-message clique for a team roster.+pub fn ensure_dms_for_roster(+ roster: impl IntoIterator<Item = MacroUserIdStr<'static>>,+) -> EnsureDms {+ let mut roster = roster+ .into_iter()+ .collect::<HashSet<_>>()+ .into_iter()+ .collect::<Vec<_>>();+ roster.sort_by(|a, b| a.as_ref().cmp(b.as_ref()));++ let mut requests =+ Vec::with_capacity(roster.len().saturating_mul(roster.len().saturating_sub(1)) / 2);+ for (index, lo) in roster.iter().enumerate() {+ for hi in &roster[index + 1..] {+ requests.push(EnsureDm {+ pair: DmPair::new(lo.clone(), hi.clone()).expect("roster users are distinct"),+ owner: lo.clone(),+ });+ }+ }+ EnsureDms { requests }+}++fn sort_requests(requests: &mut [EnsureDm]) {+ requests.sort_by(|a, b| {+ a.pair+ .lo()+ .as_ref()+ .cmp(b.pair.lo().as_ref())+ .then_with(|| a.pair.hi().as_ref().cmp(b.pair.hi().as_ref()))+ });+}++/// Counts produced by a direct-message ensure batch.+#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]+pub struct EnsureDmsSummary {+ /// Direct-message channels created by the batch.+ pub created: usize,+ /// Direct-message pairs that already had a channel.+ pub existing: usize,+ /// Direct-message pairs that could not be ensured.+ pub failed: usize,+}diff --git a/crates/channels/src/domain/dm/test.rs b/crates/channels/src/domain/dm/test.rsnew file mode 100644index 0000000000..fecdeef03b--- /dev/null+++ b/crates/channels/src/domain/dm/test.rs@@ -0,0 +1,83 @@+use super::*;++fn user(local: &str) -> MacroUserIdStr<'static> {+ MacroUserIdStr::try_from(format!("macro|{local}@test.com")).unwrap()+}++#[test]+fn self_pair_is_rejected() {+ let user = user("same");++ assert_eq!(DmPair::new(user.clone(), user), Err(SelfDm));+}++#[test]+fn pair_identity_is_independent_of_argument_order() {+ let a = user("a");+ let b = user("b");++ assert_eq!(+ DmPair::new(a.clone(), b.clone()).unwrap(),+ DmPair::new(b, a).unwrap()+ );+}++#[test]+fn joining_member_batch_is_a_deduplicated_star() {+ let joiner = user("joiner");+ let a = user("a");+ let b = user("b");++ let command = ensure_dms_for_joining_member(+ joiner.clone(),+ vec![+ joiner.clone(),+ a.clone(),+ b.clone(),+ a.clone(),+ joiner.clone(),+ ],+ );+ let pairs = command+ .requests+ .iter()+ .map(|request| request.pair.clone())+ .collect::<HashSet<_>>();++ assert_eq!(command.requests.len(), 2);+ assert_eq!(+ pairs,+ HashSet::from([+ DmPair::new(joiner.clone(), a).unwrap(),+ DmPair::new(joiner, b).unwrap(),+ ])+ );+}++#[test]+fn roster_batch_is_a_complete_clique() {+ let roster = vec![user("a"), user("b"), user("c"), user("d")];++ let command = ensure_dms_for_roster(roster.clone());+ let pairs = command+ .requests+ .iter()+ .map(|request| request.pair.clone())+ .collect::<HashSet<_>>();++ assert_eq!(+ command.requests.len(),+ roster.len() * (roster.len() - 1) / 2+ );+ assert_eq!(pairs.len(), command.requests.len());+}++#[test]+fn empty_and_singleton_rosters_have_no_pairs() {+ assert!(ensure_dms_for_roster(Vec::new()).requests.is_empty());+ assert!(+ ensure_dms_for_roster(vec![user("only")])+ .requests+ .is_empty()+ );+}diff --git a/crates/channels/src/domain/mod.rs b/crates/channels/src/domain/mod.rsindex b501f13e06..1d79343a8b 100644--- a/crates/channels/src/domain/mod.rs+++ b/crates/channels/src/domain/mod.rs@@ -2,6 +2,8 @@pub mod activity;/// Kafka event models for the `macro.channels` topic.pub mod broker_events;+/// Direct-message identity and batch commands.+pub mod dm;#[cfg(feature = "entity_mutation")]/// Unified entity-mutation capability impls.pub mod entity_mutation;@@ -23,3 +25,6 @@ pub mod service;#[cfg(feature = "ports")]/// Domain orchestration for channel side effects.pub mod side_effects;+/// Ensure teammate direct messages from a join payload.+#[cfg(feature = "ports")]+pub mod teammate_dms;diff --git a/crates/channels/src/domain/ports.rs b/crates/channels/src/domain/ports.rsindex ced2f416e6..26057802ca 100644--- a/crates/channels/src/domain/ports.rs+++ b/crates/channels/src/domain/ports.rs@@ -1,4 +1,3 @@-use crate::domain::events::ChannelEvent;#[cfg(feature = "attachment")]use crate::domain::models::RecentChannelMessage;use crate::domain::models::{@@ -24,6 +23,10 @@ use crate::domain::side_effects::{ChannelDocumentMention, ChannelNotificationEffect, ChannelRealtimeEffect,ThreadNotificationContext,};+use crate::domain::{+ dm::{EnsureDms, EnsureDmsSummary},+ events::ChannelEvent,+};use channel_sender::ChannelSender;use chrono::{DateTime, Utc};use entity_access::domain::models::{EntityAccessReceipt, MemberParticipantRole};@@ -705,6 +708,18 @@ pub trait ChannelService: Send + Sync + 'static {}}+ /// Ensure all direct-message pairs in a batch.+ fn ensure_dms(+ &self,+ _command: EnsureDms,+ ) -> impl Future<Output = Result<EnsureDmsSummary, ChannelMutationErr>> + Send {+ async move {+ Err(ChannelMutationErr::NotFound(+ "channel mutations are not configured".to_string(),+ ))+ }+ }+/// Get or create a direct message channel.fn get_or_create_dm(&self,diff --git a/crates/channels/src/domain/service.rs b/crates/channels/src/domain/service.rsindex 1322fe3867..26cf0328d4 100644--- a/crates/channels/src/domain/service.rs+++ b/crates/channels/src/domain/service.rs@@ -1,4 +1,5 @@use crate::domain::{+ dm::{DmPair, EnsureDms, EnsureDmsSummary},events::ChannelEvent,models::{Activity, ActivityType, AddParticipantsRequest, AttachmentEntityReference, BotId,@@ -386,6 +387,35 @@ where})}+ #[tracing::instrument(err, skip(self, command))]+ async fn ensure_dms(&self, command: EnsureDms) -> Result<EnsureDmsSummary, ChannelMutationErr> {+ let mut summary = EnsureDmsSummary::default();+ for request in command.into_requests() {+ let user_lo = request.pair.lo().as_ref().to_string();+ let user_hi = request.pair.hi().as_ref().to_string();+ match self.ensure_one_dm(request.pair, request.owner).await {+ Ok(GetOrCreateChannelResponse {+ action: GetOrCreateAction::Create,+ ..+ }) => summary.created += 1,+ Ok(GetOrCreateChannelResponse {+ action: GetOrCreateAction::Get,+ ..+ }) => summary.existing += 1,+ Err(error) => {+ summary.failed += 1;+ tracing::error!(+ error=?error,+ user_lo,+ user_hi,+ "unable to ensure teammate direct message"+ );+ }+ }+ }+ Ok(summary)+ }+#[tracing::instrument(err, skip(self, recipient_id))]async fn get_or_create_dm(&self,@@ -393,32 +423,12 @@ whereGetOrCreateDmRequest { recipient_id }: GetOrCreateDmRequest,) -> Result<GetOrCreateChannelResponse, ChannelMutationErr> {let actor = require_user_actor(&actor)?;-- if actor == recipient_id {- return Err(ChannelMutationErr::BadRequest(+ let pair = DmPair::new(actor.clone(), recipient_id).map_err(|_| {+ ChannelMutationErr::BadRequest("recipient_id cannot be the same as the user_id".to_string(),- ));- }-- let existing_channel_id = self- .repo- .maybe_get_dm(actor.clone(), recipient_id.clone())- .await- .map_err(|e| ChannelMutationErr::Repo(e.into()))?;-- self.get_or_create_channel(- existing_channel_id,- actor.clone(),- None,- crate::domain::models::CreateChannelRequest {- name: None,- channel_type: ChannelType::DirectMessage,- team_id: None,- auto_join_team: false,- participants: HashSet::from([actor, recipient_id.clone()]),- },- )- .await+ )+ })?;+ self.ensure_one_dm(pair, actor).await}#[tracing::instrument(err, skip(self, req))]@@ -1248,6 +1258,32 @@ whereP: ChannelReferenceSharePermissions,M: ChannelMentionExtractor,{+ async fn ensure_one_dm(+ &self,+ pair: DmPair,+ owner: MacroUserIdStr<'static>,+ ) -> Result<GetOrCreateChannelResponse, ChannelMutationErr> {+ let existing_channel_id = self+ .repo+ .maybe_get_dm(pair.lo().clone(), pair.hi().clone())+ .await+ .map_err(|e| ChannelMutationErr::Repo(e.into()))?;++ self.get_or_create_channel(+ existing_channel_id,+ owner,+ None,+ crate::domain::models::CreateChannelRequest {+ name: None,+ channel_type: ChannelType::DirectMessage,+ team_id: None,+ auto_join_team: false,+ participants: HashSet::from([pair.lo().clone(), pair.hi().clone()]),+ },+ )+ .await+ }+async fn create_channel_record<'a>(&self,owner_id: MacroUserIdStr<'a>,@@ -1824,6 +1860,10 @@ where.map_err(|e| ChannelMutationErr::Repo(e.into()))}+ async fn ensure_dms(&self, command: EnsureDms) -> Result<EnsureDmsSummary, ChannelMutationErr> {+ ChannelServiceImpl::ensure_dms(self, command).await+ }+async fn get_or_create_dm(&self,actor: Sender,diff --git a/crates/channels/src/domain/service/test.rs b/crates/channels/src/domain/service/test.rsindex 7c7a929e95..5c345ae677 100644--- a/crates/channels/src/domain/service/test.rs+++ b/crates/channels/src/domain/service/test.rs@@ -1,16 +1,17 @@use super::*;use crate::domain::{+ dm::ensure_dms_for_joining_member,events::ChannelEvent,models::{Activity, ActivityType, BotId, BotSenderProfile, ChannelAttachment, ChannelAttachmentType,ChannelContextMessage, ChannelInfo, ChannelMessageFilters, ChannelMetadata,ChannelParticipant, ChannelType, CountedReaction, CreateChannelRequest,CreateEntityMentionOptions, CreatedChannel, DeleteMessageQuery, EntityMention,- MessageAttachment, MessagePageDirection, MutatedAttachment, MutatedMessage,- NewChannelAttachment, ParticipantRole, PatchChannelRequest, PatchMessageRequest,- PostMessageRequest, PostReactionRequest, ReactionAction, ReferencedShareItem,- ReferencedShareItemType, ResolvedChannelMessage, Sender, SimpleMention, ThreadData,- ThreadReplyRow, TopLevelMessageRow,+ GetOrCreateDmRequest, MessageAttachment, MessagePageDirection, MutatedAttachment,+ MutatedMessage, NewChannelAttachment, ParticipantRole, PatchChannelRequest,+ PatchMessageRequest, PostMessageRequest, PostReactionRequest, ReactionAction,+ ReferencedShareItem, ReferencedShareItemType, ResolvedChannelMessage, Sender,+ SimpleMention, ThreadData, ThreadReplyRow, TopLevelMessageRow,},ports::{ChannelEventDispatcher, ChannelMentionExtractor, ChannelReferenceSharePermissions,@@ -2261,6 +2262,131 @@ async fn create_channel_event_carries_channel_name() {));}+#[tokio::test]+async fn ensure_dms_dispatches_created_channel_once() {+ let channel_id = Uuid::new_v4();+ let joiner = macro_id("macro|[email protected]");+ let teammate = macro_id("macro|[email protected]");+ let repo = FakeMutationRepo::new(channel_id, joiner.as_ref());+ let events = FakeEvents::default();+ let service = mutation_service(repo, events.clone(), FakeReferenceSharing::default());++ let summary = service+ .ensure_dms(ensure_dms_for_joining_member(+ joiner.clone(),+ vec![joiner.clone(), teammate.clone()],+ ))+ .await+ .unwrap();++ assert_eq!(+ summary,+ EnsureDmsSummary {+ created: 1,+ existing: 0,+ failed: 0,+ }+ );+ let events = events.events.lock().unwrap();+ assert!(matches!(+ events.as_slice(),+ [ChannelEvent::ChannelCreated {+ channel_id: actual_channel_id,+ actor,+ channel_type: ChannelType::DirectMessage,+ channel_name: None,+ participant_user_ids,+ }] if actual_channel_id == &channel_id+ && actor.as_user() == Some(&joiner)+ && participant_user_ids.contains(&joiner)+ && participant_user_ids.contains(&teammate)+ ));+}++#[tokio::test]+async fn ensure_dms_does_not_dispatch_for_existing_channel() {+ let channel_id = Uuid::new_v4();+ let joiner = macro_id("macro|[email protected]");+ let teammate = macro_id("macro|[email protected]");+ let mut repo = MockChannelRepo::new();+ repo.expect_maybe_get_dm()+ .once()+ .returning(move |_, _| Box::pin(async move { Ok(Some(channel_id)) }));+ let events = FakeEvents::default();+ let service = ChannelServiceImpl::with_dependencies(+ repo,+ events.clone(),+ FakeReferenceSharing::default(),+ );++ let summary = service+ .ensure_dms(ensure_dms_for_joining_member(joiner, vec![teammate]))+ .await+ .unwrap();++ assert_eq!(+ summary,+ EnsureDmsSummary {+ created: 0,+ existing: 1,+ failed: 0,+ }+ );+ assert!(events.events.lock().unwrap().is_empty());+}++#[tokio::test]+async fn get_or_create_dm_rejects_self_pair() {+ let user = macro_id("macro|[email protected]");+ let repo = FakeMutationRepo::new(Uuid::new_v4(), user.as_ref());+ let service = mutation_service(repo, FakeEvents::default(), FakeReferenceSharing::default());++ let error = service+ .get_or_create_dm(+ Sender::new_from_user(user.clone()),+ GetOrCreateDmRequest { recipient_id: user },+ )+ .await+ .unwrap_err();++ assert!(matches!(+ error,+ ChannelMutationErr::BadRequest(message)+ if message == "recipient_id cannot be the same as the user_id"+ ));+}++#[tokio::test]+async fn get_or_create_dm_returns_get_for_existing_pair() {+ let channel_id = Uuid::new_v4();+ let actor = macro_id("macro|[email protected]");+ let recipient = macro_id("macro|[email protected]");+ let mut repo = MockChannelRepo::new();+ repo.expect_maybe_get_dm()+ .once()+ .returning(move |_, _| Box::pin(async move { Ok(Some(channel_id)) }));+ let events = FakeEvents::default();+ let service = ChannelServiceImpl::with_dependencies(+ repo,+ events.clone(),+ FakeReferenceSharing::default(),+ );++ let response = service+ .get_or_create_dm(+ Sender::new_from_user(actor),+ GetOrCreateDmRequest {+ recipient_id: recipient,+ },+ )+ .await+ .unwrap();++ assert_eq!(response.channel_id, channel_id.to_string());+ assert_eq!(response.action, GetOrCreateAction::Get);+ assert!(events.events.lock().unwrap().is_empty());+}+#[tokio::test]async fn create_private_channel_allows_no_invited_participants() {let channel_id = Uuid::new_v4();diff --git a/crates/channels/src/domain/teammate_dms.rs b/crates/channels/src/domain/teammate_dms.rsnew file mode 100644index 0000000000..9df69a0227--- /dev/null+++ b/crates/channels/src/domain/teammate_dms.rs@@ -0,0 +1,75 @@+//! Ensure teammate direct messages from a joining member and a teammate list.++#[cfg(test)]+mod test;++use std::future::Future;++use macro_user_id::user_id::MacroUserIdStr;++use crate::domain::{+ dm::{EnsureDms, EnsureDmsSummary, ensure_dms_for_joining_member},+ ports::{ChannelMutationErr, ChannelService},+};++/// Creates missing teammate direct-message channels.+pub trait TeammateDirectMessages {+ /// Ensure every pair in `command` has a direct-message channel.+ fn ensure_dms(+ &self,+ command: EnsureDms,+ ) -> impl Future<Output = Result<EnsureDmsSummary, ChannelMutationErr>> + Send;+}++impl<T: ChannelService> TeammateDirectMessages for T {+ fn ensure_dms(+ &self,+ command: EnsureDms,+ ) -> impl Future<Output = Result<EnsureDmsSummary, ChannelMutationErr>> + Send {+ ChannelService::ensure_dms(self, command)+ }+}++/// Failure while ensuring teammate direct messages.+#[derive(Debug, thiserror::Error)]+pub enum TeammateDmError {+ /// Channel creation failed for the whole batch.+ #[error(transparent)]+ Channels(#[from] ChannelMutationErr),+ /// Some pairs in the batch could not be ensured.+ #[error("failed to ensure {failed} teammate direct messages")]+ Partial {+ /// Pairs created by this attempt.+ created: usize,+ /// Pairs that already had a channel.+ existing: usize,+ /// Pairs that failed.+ failed: usize,+ },+}++impl TeammateDmError {+ /// Whether the consumer should retry instead of committing the offset.+ pub fn is_transient(&self) -> bool {+ true+ }+}++/// Create a DM between `member_id` and every id in `teammate_ids`.+pub async fn ensure_joined_member_dms<C: TeammateDirectMessages>(+ channels: &C,+ member_id: MacroUserIdStr<'static>,+ teammate_ids: impl IntoIterator<Item = MacroUserIdStr<'static>>,+) -> Result<EnsureDmsSummary, TeammateDmError> {+ let summary = channels+ .ensure_dms(ensure_dms_for_joining_member(member_id, teammate_ids))+ .await?;+ if summary.failed > 0 {+ return Err(TeammateDmError::Partial {+ created: summary.created,+ existing: summary.existing,+ failed: summary.failed,+ });+ }+ Ok(summary)+}diff --git a/crates/channels/src/domain/teammate_dms/test.rs b/crates/channels/src/domain/teammate_dms/test.rsnew file mode 100644index 0000000000..52b471bd05--- /dev/null+++ b/crates/channels/src/domain/teammate_dms/test.rs@@ -0,0 +1,99 @@+use std::sync::{Arc, Mutex};++use super::*;++#[derive(Clone, Default)]+struct RecordingChannels {+ calls: Arc<Mutex<Vec<EnsureDms>>>,+ summary: EnsureDmsSummary,+ fail: bool,+}++impl TeammateDirectMessages for RecordingChannels {+ fn ensure_dms(+ &self,+ command: EnsureDms,+ ) -> impl Future<Output = Result<EnsureDmsSummary, ChannelMutationErr>> + Send {+ self.calls.lock().expect("calls mutex").push(command);+ let summary = self.summary;+ let fail = self.fail;+ async move {+ if fail {+ Err(ChannelMutationErr::Repo(anyhow::anyhow!(+ "ensure direct messages failed"+ )))+ } else {+ Ok(summary)+ }+ }+ }+}++fn user(email: &str) -> MacroUserIdStr<'static> {+ MacroUserIdStr::try_from(format!("macro|{email}")).unwrap()+}++#[tokio::test]+async fn joining_member_gets_a_star_of_distinct_teammates() {+ let joiner = user("[email protected]");+ let owner = user("[email protected]");+ let teammate = user("[email protected]");+ let channels = RecordingChannels::default();++ let summary = ensure_joined_member_dms(+ &channels,+ joiner.clone(),+ vec![+ owner.clone(),+ teammate.clone(),+ teammate.clone(),+ joiner.clone(),+ ],+ )+ .await+ .unwrap();++ assert_eq!(summary, EnsureDmsSummary::default());+ assert_eq!(+ channels.calls.lock().unwrap().clone(),+ vec![ensure_dms_for_joining_member(joiner, vec![owner, teammate])]+ );+}++#[tokio::test]+async fn channel_and_partial_failures_are_transient() {+ let joiner = user("[email protected]");+ let owner = user("[email protected]");++ let channel_error = ensure_joined_member_dms(+ &RecordingChannels {+ fail: true,+ ..Default::default()+ },+ joiner.clone(),+ vec![owner.clone()],+ )+ .await+ .unwrap_err();+ assert!(channel_error.is_transient());++ let partial = ensure_joined_member_dms(+ &RecordingChannels {+ summary: EnsureDmsSummary {+ created: 0,+ existing: 0,+ failed: 1,+ },+ ..Default::default()+ },+ joiner,+ vec![owner],+ )+ .await+ .unwrap_err();+ assert!(matches!(+ partial,+ TeammateDmError::Partial { failed: 1, .. }+ ));+ assert!(partial.is_transient());+}diff --git a/crates/channels/src/inbound/mod.rs b/crates/channels/src/inbound/mod.rsindex 705faeb16a..0ffbc0d785 100644--- a/crates/channels/src/inbound/mod.rs+++ b/crates/channels/src/inbound/mod.rs@@ -7,6 +7,9 @@ pub mod axum_router;/// Axum handler and router for legacy channel list routes.#[cfg(all(feature = "inbound", feature = "list"))]pub mod list_router;+/// Kafka consumer that ensures teammate DMs after `team.member_joined`.+#[cfg(feature = "kafka_consumer")]+pub mod teammate_dms_consumer;/// AI toolset for reading channel messages and threads.#[cfg(feature = "ai_tools")]pub mod toolset;diff --git a/crates/channels/src/inbound/teammate_dms_consumer.rs b/crates/channels/src/inbound/teammate_dms_consumer.rsnew file mode 100644index 0000000000..c2a062be07--- /dev/null+++ b/crates/channels/src/inbound/teammate_dms_consumer.rs@@ -0,0 +1,282 @@+//! Kafka consumer that ensures teammate DMs after `team.member_joined`.+//!+//! Subscribes to `macro.teams` under the `teammate-dms` consumer group. The+//! crate does not depend on `teams`: this adapter decodes a channels-owned+//! view of the join payload (`member_id` + `teammate_ids`) and ignores every+//! other team event. Delivery is at-least-once: an event's offset is committed+//! only after the handler accepted it or permanently rejected it. Transient+//! failures retry in-process; if they persist the consumer exits without+//! committing so a supervisor redelivers. Undecodable messages are logged and+//! skipped rather than wedging the partition.++#[cfg(test)]+mod test;++use crate::domain::{+ ports::ChannelService,+ teammate_dms::{TeammateDirectMessages, TeammateDmError, ensure_joined_member_dms},+};+use anyhow::Context as _;+use kafka_util::{GroupName, KafkaEventConsumer};+use macro_event_broker::{+ Event, KafkaConsumerAdapter, MacroEvent, MacroEventCollection as _, MacroEventConsumerService,+ TopicEvent,+};+use macro_event_topics::MacroTeamsTopic;+use macro_user_id::user_id::MacroUserIdStr;+use rdkafka::consumer::CommitMode;+use rdkafka::message::{BorrowedMessage, Message};+use serde::{Deserialize, Serialize};+use std::future::Future;+use std::time::Duration;+use tokio_retry::{RetryIf, strategy::ExponentialBackoff};++/// Consumer group for teammate direct-message sync.+struct TeammateDmsConsumerGroup;++impl GroupName for TeammateDmsConsumerGroup {+ const GROUP_NAME: &'static str = "teammate-dms";+}++type TeammateDmsKafkaAdapter = KafkaConsumerAdapter<TeammateDmsConsumerGroup, DeclaredMacroEvent>;+type TeammateDmsKafkaConsumer =+ MacroEventConsumerService<DeclaredMacroEvent, TeammateDmsKafkaAdapter>;++macro_event_broker::declare_topics!(DeclaredMacroEvent: TeamsMacroEventForDms);++/// Maximum in-process attempts per event before the consumer bails out.+const MAX_ATTEMPTS: u32 = 5;++/// Delay before the first retry; doubles on each subsequent retry.+const RETRY_BASE_DELAY: Duration = Duration::from_secs(1);++fn retry_strategy() -> impl Iterator<Item = Duration> {+ ExponentialBackoff::from_millis(2)+ .factor(500)+ .take((MAX_ATTEMPTS - 1) as usize)+}++/// Channels-owned view of `team.member_joined` for ensuring teammate DMs.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+pub(crate) struct TeammateDmsJoinedMetadata {+ /// User who joined the team.+ pub member_id: MacroUserIdStr<'static>,+ /// Other current teammates the joining member should have a DM with.+ #[serde(default)]+ pub teammate_ids: Vec<MacroUserIdStr<'static>>,+}++/// Tagged envelope around a `team.member_joined` payload.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+#[serde(tag = "event_type", content = "metadata")]+pub(crate) enum MemberJoinedEnvelope {+ /// Join payload used to ensure teammate DMs.+ #[serde(rename = "team.member_joined")]+ Payload(TeammateDmsJoinedMetadata),+}++/// Placeholder for team events this consumer ignores.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+pub(crate) struct IgnoredTeamEvent {+ event_type: String,+}++/// Wire view of `macro.teams` records this consumer acts on.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+#[serde(untagged)]+pub(crate) enum TeamsTopicForDms {+ /// A member joined; create teammate DMs from the payload roster.+ MemberJoined(MemberJoinedEnvelope),+ /// Any other team event; ignored and committed.+ Other(IgnoredTeamEvent),+}++impl TopicEvent for TeamsTopicForDms {+ type Topic = MacroTeamsTopic;++ const SCHEMA_VERSION: u8 = 1;+}++/// `macro.teams` record decoded for teammate-DM ensure.+pub(crate) struct TeamsMacroEventForDms {+ key: String,+ event: Event<TeamsTopicForDms>,+}++impl MacroEvent for TeamsMacroEventForDms {+ type EventPayload = TeamsTopicForDms;++ fn key(&self) -> &str {+ &self.key+ }++ fn event(&self) -> &Event<Self::EventPayload> {+ &self.event+ }++ fn from_event(key: String, event: Event<Self::EventPayload>) -> Self {+ Self { key, event }+ }+}++fn commit_logged(consumer: &TeammateDmsKafkaConsumer, message: &BorrowedMessage<'_>) {+ match consumer.inner().commit_message(message, CommitMode::Async) {+ Ok(()) => tracing::trace!(+ partition = message.partition(),+ offset = message.offset(),+ "committed offset"+ ),+ Err(error) => tracing::error!(+ error = ?error,+ partition = message.partition(),+ offset = message.offset(),+ "failed to commit offset"+ ),+ }+}++/// Apply one team event. Non-join events succeed immediately.+pub(crate) async fn handle_team_event<S: TeammateDirectMessages>(+ channels: &S,+ event: &TeamsTopicForDms,+) -> Result<(), TeammateDmError> {+ match event {+ TeamsTopicForDms::MemberJoined(MemberJoinedEnvelope::Payload(metadata)) => {+ ensure_joined_member_dms(+ channels,+ metadata.member_id.clone(),+ metadata.teammate_ids.clone(),+ )+ .await+ .map(|_| ())+ }+ TeamsTopicForDms::Other(_) => Ok(()),+ }+}++async fn handle_with_retry<S: TeammateDirectMessages>(+ channels: &S,+ event: &TeamsTopicForDms,+ partition: i32,+ offset: i64,+) -> anyhow::Result<()> {+ let mut attempt = 0u32;+ let result = RetryIf::start(+ retry_strategy(),+ || {+ attempt += 1;+ async move {+ tracing::trace!(partition, offset, attempt, "handling team event");+ let result = handle_team_event(channels, event).await;+ match &result {+ Ok(()) => {+ tracing::trace!(partition, offset, attempt, "team event handled")+ }+ Err(error) if error.is_transient() && attempt < MAX_ATTEMPTS => {+ let delay = RETRY_BASE_DELAY * 2u32.pow(attempt - 1);+ tracing::warn!(+ error = ?error,+ partition,+ offset,+ attempt,+ delay_secs = delay.as_secs_f32(),+ "transient teammate DM failure, retrying"+ );+ }+ Err(_) => {}+ }+ result+ }+ },+ |error: &TeammateDmError| error.is_transient(),+ )+ .await;++ match result {+ Ok(()) => Ok(()),+ Err(error) if !error.is_transient() => {+ tracing::error!(+ error = ?error,+ partition,+ offset,+ "dropping team event after non-retryable teammate DM failure"+ );+ Ok(())+ }+ Err(error) => Err(error).with_context(|| {+ format!(+ "transient teammate DM failure persisted after \+ {MAX_ATTEMPTS} attempts \+ (partition {partition} offset {offset})"+ )+ }),+ }+}++/// Run the teammate-DM consumer until `shutdown` resolves.+///+/// Connects to `brokers` and subscribes to `macro.teams` under the+/// `teammate-dms` consumer group. Callers should restart on error so the+/// uncommitted event is redelivered. Pass `std::future::pending()` as+/// `shutdown` to run until the process exits.+pub async fn run_teammate_dms_consumer<S>(+ brokers: &str,+ channels: S,+ shutdown: impl Future<Output = ()> + Send,+) -> anyhow::Result<()>+where+ S: ChannelService + Clone,+{+ let consumer = KafkaEventConsumer::<TeammateDmsConsumerGroup>::from_env(brokers)?;+ let consumer = KafkaConsumerAdapter::<TeammateDmsConsumerGroup, ()>::new(consumer)+ .subscribe::<DeclaredMacroEvent>()+ .map_err(|error| anyhow::anyhow!("failed to subscribe to team events: {error:?}"))?;+ let consumer = TeammateDmsKafkaConsumer::new(consumer);+ tracing::info!(+ topics = ?DeclaredMacroEvent::topics(),+ group = TeammateDmsConsumerGroup::GROUP_NAME,+ "teammate DM consumer listening"+ );++ let mut shutdown = std::pin::pin!(shutdown);+ loop {+ tokio::select! {+ _ = &mut shutdown => {+ tracing::info!("teammate DM consumer shutting down");+ break;+ }+ result = consumer.recv() => {+ let message = match result {+ Ok(message) => message,+ Err(e) => {+ tracing::error!(error = ?e, "kafka receive error");+ continue;+ }+ };+ let kafka_message = message.inner();+ match message.decode_payload() {+ Ok(DeclaredMacroEvent::TeamsMacroEventForDms(event)) => {+ handle_with_retry(+ &channels,+ &event.event().event,+ kafka_message.partition(),+ kafka_message.offset(),+ )+ .await?;+ }+ Err(e) => tracing::error!(+ error = ?e,+ topic = kafka_message.topic(),+ partition = kafka_message.partition(),+ offset = kafka_message.offset(),+ "failed to decode team event"+ ),+ }++ commit_logged(&consumer, kafka_message);+ }+ }+ }++ Ok(())+}diff --git a/crates/channels/src/inbound/teammate_dms_consumer/test.rs b/crates/channels/src/inbound/teammate_dms_consumer/test.rsnew file mode 100644index 0000000000..3ca49eb05d--- /dev/null+++ b/crates/channels/src/inbound/teammate_dms_consumer/test.rs@@ -0,0 +1,215 @@+use super::*;+use crate::domain::{dm::EnsureDmsSummary, ports::ChannelMutationErr};+use macro_event_broker::{Event, MacroEvent as _, MacroEventCollection as _, MessageParts};+use std::sync::{Arc, Mutex};++#[derive(Clone, Default)]+struct RecordingChannels {+ calls: Arc<Mutex<Vec<String>>>,+}++impl TeammateDirectMessages for RecordingChannels {+ fn ensure_dms(+ &self,+ command: crate::domain::dm::EnsureDms,+ ) -> impl std::future::Future<Output = Result<EnsureDmsSummary, ChannelMutationErr>> + Send+ {+ let owners = command+ .into_requests()+ .into_iter()+ .map(|request| request.owner.as_ref().to_string())+ .collect::<Vec<_>>();+ self.calls.lock().unwrap().extend(owners);+ async move { Ok(EnsureDmsSummary::default()) }+ }+}++struct TestMessage<'a> {+ topic: &'a str,+ payload: &'a [u8],+}++impl MessageParts for TestMessage<'_> {+ fn key(&self) -> Option<&str> {+ Some("3f6f8b0a-6f9f-4a3f-9c3a-2b1e5d4c7a90")+ }++ fn payload(&self) -> Option<&[u8]> {+ Some(self.payload)+ }++ fn topic(&self) -> &str {+ self.topic+ }+}++fn user(value: &str) -> MacroUserIdStr<'static> {+ MacroUserIdStr::try_from(value.to_string()).expect("valid user id")+}++fn member_joined() -> TeamsTopicForDms {+ TeamsTopicForDms::MemberJoined(MemberJoinedEnvelope::Payload(TeammateDmsJoinedMetadata {+ member_id: user("macro|[email protected]"),+ teammate_ids: vec![+ user("macro|[email protected]"),+ user("macro|[email protected]"),+ ],+ }))+}++#[test]+fn subscribes_to_macro_teams() {+ assert_eq!(DeclaredMacroEvent::topics(), ["macro.teams"]);+}++#[test]+fn decodes_member_joined_events() {+ let payload = serde_json::to_vec(&Event::new(member_joined())).expect("serializable");+ let decoded = DeclaredMacroEvent::decode(&TestMessage {+ topic: "macro.teams",+ payload: &payload,+ })+ .expect("decodable");++ match decoded {+ DeclaredMacroEvent::TeamsMacroEventForDms(event) => {+ assert_eq!(event.event().event, member_joined());+ }+ }+}++#[test]+fn decodes_other_team_events_as_ignored() {+ let payload = serde_json::to_vec(&serde_json::json!({+ "event_id": "01998a30-1a2b-7c3d-9e4f-5a6b7c8d9e0f",+ "schema_version": 1,+ "event_type": "team.updated",+ "metadata": {+ "team_id": "3f6f8b0a-6f9f-4a3f-9c3a-2b1e5d4c7a90",+ "actor_user_id": "macro|[email protected]",+ "name": "Acme",+ "slug": null+ }+ }))+ .expect("serializable");+ let decoded = DeclaredMacroEvent::decode(&TestMessage {+ topic: "macro.teams",+ payload: &payload,+ })+ .expect("decodable");++ match decoded {+ DeclaredMacroEvent::TeamsMacroEventForDms(event) => {+ assert!(matches!(event.event().event, TeamsTopicForDms::Other(_)));+ }+ }+}++#[test]+fn decodes_canonical_member_joined_json() {+ let payload = serde_json::to_vec(&serde_json::json!({+ "event_id": "01998a30-1a2b-7c3d-9e4f-5a6b7c8d9e0f",+ "schema_version": 1,+ "event_type": "team.member_joined",+ "metadata": {+ "team_id": "3f6f8b0a-6f9f-4a3f-9c3a-2b1e5d4c7a90",+ "member_id": "macro|[email protected]",+ "teammate_ids": ["macro|[email protected]", "macro|[email protected]"],+ "role": "member",+ "join_method": { "type": "domain_auto_join" }+ }+ }))+ .expect("serializable");+ let decoded = DeclaredMacroEvent::decode(&TestMessage {+ topic: "macro.teams",+ payload: &payload,+ })+ .expect("decodable");++ match decoded {+ DeclaredMacroEvent::TeamsMacroEventForDms(event) => {+ assert_eq!(event.event().event, member_joined());+ }+ }+}++#[test]+fn missing_teammate_ids_defaults_to_empty() {+ let payload = serde_json::to_vec(&serde_json::json!({+ "event_id": "01998a30-1a2b-7c3d-9e4f-5a6b7c8d9e0f",+ "schema_version": 1,+ "event_type": "team.member_joined",+ "metadata": {+ "team_id": "3f6f8b0a-6f9f-4a3f-9c3a-2b1e5d4c7a90",+ "member_id": "macro|[email protected]",+ "role": "member",+ "join_method": { "type": "domain_auto_join" }+ }+ }))+ .expect("serializable");+ let decoded = DeclaredMacroEvent::decode(&TestMessage {+ topic: "macro.teams",+ payload: &payload,+ })+ .expect("decodable");++ match decoded {+ DeclaredMacroEvent::TeamsMacroEventForDms(event) => {+ assert_eq!(+ event.event().event,+ TeamsTopicForDms::MemberJoined(MemberJoinedEnvelope::Payload(+ TeammateDmsJoinedMetadata {+ member_id: user("macro|[email protected]"),+ teammate_ids: Vec::new(),+ }+ ))+ );+ }+ }+}++#[tokio::test]+async fn member_joined_ensures_dms_from_payload() {+ let channels = RecordingChannels::default();++ handle_team_event(&channels, &member_joined())+ .await+ .unwrap();++ let calls = channels.calls.lock().unwrap().clone();+ assert_eq!(calls.len(), 2);+ assert!(calls.iter().all(|owner| owner == "macro|[email protected]"));+}++#[tokio::test]+async fn other_team_events_are_ignored() {+ let channels = RecordingChannels::default();++ handle_team_event(+ &channels,+ &TeamsTopicForDms::Other(IgnoredTeamEvent {+ event_type: "team.updated".to_string(),+ }),+ )+ .await+ .unwrap();++ assert!(channels.calls.lock().unwrap().is_empty());+}++#[tokio::test]+async fn empty_teammate_ids_is_success() {+ let channels = RecordingChannels::default();++ handle_team_event(+ &channels,+ &TeamsTopicForDms::MemberJoined(MemberJoinedEnvelope::Payload(TeammateDmsJoinedMetadata {+ member_id: user("macro|[email protected]"),+ teammate_ids: Vec::new(),+ })),+ )+ .await+ .unwrap();++ assert!(channels.calls.lock().unwrap().is_empty());+}diff --git a/crates/channels/src/lib.rs b/crates/channels/src/lib.rsindex 3d4d173209..cc94f1e2ca 100644--- a/crates/channels/src/lib.rs+++ b/crates/channels/src/lib.rs@@ -3,8 +3,13 @@/// Domain layer: models, ports, and service.pub mod domain;-/// Inbound layer: axum handler, AI tools, and attachment adapters.-#[cfg(any(feature = "inbound", feature = "ai_tools", feature = "attachment"))]+/// Inbound layer: axum handler, AI tools, attachment adapters, and Kafka consumers.+#[cfg(any(+ feature = "inbound",+ feature = "ai_tools",+ feature = "attachment",+ feature = "kafka_consumer"+))]pub mod inbound;/// Outbound layer: postgres repository.#[cfg(feature = "outbound")]diff --git a/crates/channels/src/outbound/pg_channels_repo/tests.rs b/crates/channels/src/outbound/pg_channels_repo/tests.rsindex d6a6cdfa3c..9696de26d1 100644--- a/crates/channels/src/outbound/pg_channels_repo/tests.rs+++ b/crates/channels/src/outbound/pg_channels_repo/tests.rs@@ -448,6 +448,40 @@ async fn create_channel_persists_auto_join_team_and_adds_current_members(pool: P);}+#[sqlx::test(+ fixtures(path = "../../../fixtures", scripts("channels_repo")),+ migrator = "MACRO_DB_MIGRATIONS"+)]+async fn maybe_get_dm_finds_channel_regardless_of_argument_order(pool: Pool<Postgres>) {+ let repo = repo(pool.clone());+ let user_a = macro_user_id(USER_A);+ let user_b = macro_user_id(USER_B);++ let created = repo+ .create_channel(+ user_a.clone(),+ None,+ CreateChannelRequest {+ name: None,+ channel_type: ChannelType::DirectMessage,+ team_id: None,+ auto_join_team: false,+ participants: HashSet::from([user_b.clone()]),+ },+ )+ .await+ .unwrap();++ let forward = repo+ .maybe_get_dm(user_a.clone(), user_b.clone())+ .await+ .unwrap();+ let reverse = repo.maybe_get_dm(user_b, user_a).await.unwrap();++ assert_eq!(forward, Some(created.id));+ assert_eq!(reverse, Some(created.id));+}+#[sqlx::test(fixtures(path = "../../../fixtures", scripts("channels_repo")),migrator = "MACRO_DB_MIGRATIONS"diff --git a/crates/teams/Cargo.toml b/crates/teams/Cargo.tomlindex acd29ad9b7..9e5446ed23 100644--- a/crates/teams/Cargo.toml+++ b/crates/teams/Cargo.toml@@ -4,6 +4,11 @@ name = "teams"publish = falseversion = "0.1.0"+[[bin]]+name = "backfill_teammate_dms"+path = "src/bin/backfill_teammate_dms.rs"+required-features = ["backfill_binary"]+[features]ai_tools = ["dep:ai_toolset",@@ -22,6 +27,13 @@ axum = ["entity_access/inbound","entity_access/ports",]+backfill_binary = [+ "channels/outbound",+ "dep:database_env_vars",+ "dep:macro_entrypoint",+ "outbound",+ "ports",+]default = ["ai_tools", "axum", "inbound", "outbound", "ports"]inbound = ["axum", "ports"]outbound = [@@ -53,9 +65,11 @@ async-trait = { workspace = true, optional = true }chrono = { workspace = true }channels = { path = "../channels", default-features = false, features = ["ports"] }contacts = { path = "../contacts", default-features = false, optional = true }+database_env_vars = { path = "../database_env_vars", optional = true }macro_authorization = { path = "../macro_authorization", optional = true, default-features = false, features = ["axum",] }+macro_entrypoint = { path = "../macro_entrypoint", optional = true }macro_event_broker = { path = "../macro_event_broker", default-features = false }macro_event_topics = { path = "../macro_event_topics" }macro_user_id = { path = "../macro_user_id" }diff --git a/crates/teams/README.md b/crates/teams/README.mdindex bc1e78f729..9b5815d7bb 100644--- a/crates/teams/README.md+++ b/crates/teams/README.md@@ -21,7 +21,7 @@ between different teams is unspecified.| `team.invite_created` | A new invite is persisted; one event is produced per newly invited lowercase email | `team_id`, `invite_id`, `email`, `invited_by`, `team_name` || `team.invite_rejected` | The invitee successfully rejects and deletes an invite | `team_id`, `invite_id`, `email`, `actor_user_id` || `team.invite_revoked` | An administrator successfully deletes an invite | `team_id`, `invite_id`, `email`, `actor_user_id` |-| `team.member_joined` | An invited or domain-auto-joined membership and all rollback-capable side effects succeed | `team_id`, `member_id`, `role`, `join_method` |+| `team.member_joined` | An invited or domain-auto-joined membership and all rollback-capable side effects succeed | `team_id`, `member_id`, `teammate_ids`, `role`, `join_method` || `team.member_removed` | Membership removal and all rollback-capable side effects succeed | `team_id`, `member_id`, `removed_by`, `role` || `team.member_role_changed` | Each individual member role update succeeds | `team_id`, `actor_user_id`, `member_id`, `role`, `previous_role` || `team.auto_join_domain_toggled` | An explicit auto-join-domain toggle succeeds | `team_id`, `actor_user_id`, `auto_join_domain` |@@ -43,6 +43,12 @@ updated event. Role updates are committed in request order: if a later updatefails, earlier successful updates and their events remain committed, while noevent is produced for the failed update.+`teammate_ids` is the sorted, de-duplicated set of other current teammates at+join time (owner plus members, excluding the joining user). It is empty when the+roster could not be loaded or the team has no other members. Consumers must+default a missing field to `[]` so events published before this field existed+still decode.+`join_method` is a tagged union. An accepted invite includes its invite UUID andthe inviter; a domain auto-join is encoded as `{"type":"domain_auto_join"}`.A complete invite-accepted membership envelope has this shape:@@ -55,6 +61,10 @@ A complete invite-accepted membership envelope has this shape:"metadata": {"team_id": "3f6f8b0a-6f9f-4a3f-9c3a-2b1e5d4c7a90","member_id": "macro|[email protected]",+ "teammate_ids": [+ "macro|[email protected]",+ "macro|[email protected]"+ ],"role": "member","join_method": {"type": "invite_accepted",@@ -79,9 +89,11 @@ notification outcomes also do not publish team events. Best-effort CRM,contact, and notification work after a committed mutation does not suppress thecorresponding event.-This change provides production only. It adds no team-event consumer, and the-webhook Kafka consumer and webhook delivery pipeline do not consume or deliver-these events.+The document storage service consumes `team.member_joined` from `macro.teams`+under the `teammate-dms` consumer group. The consumer lives in the channels+crate and creates a DM between `member_id` and each id in `teammate_ids`; it+does not load a team roster. The webhook Kafka consumer and webhook delivery+pipeline do not consume or deliver these events.## Delivery semantics and compatibility@@ -92,6 +104,19 @@ successful request. There is no transaction, outbox, or retry coupling databasepersistence to publication. Delivery is therefore at-most-once relative to teammutations: a committed mutation can have no corresponding event.+Teammate-DM consumption is at-least-once. The consumer commits an offset only+after a successful or permanent result and retries transient failures. A missed+publish can leave a team without DMs until `backfill_teammate_dms` runs.++```bash+just crates/teams/backfill_teammate_dms_local+just crates/teams/backfill_teammate_dms_dev+just crates/teams/backfill_teammate_dms_prod+```++The CLI pages every team and ensures the full teammate DM clique. It is+idempotent. Prod asks you to type `prod backfill` before it writes.+Consumers must tolerate unknown `event_type` values and unknown metadata fieldsso new event variants and additive fields remain forward compatible. Consumersshould use `schema_version` when interpreting a known event; every event in this@@ -99,6 +124,7 @@ contract currently uses version 1.Provision the `macro.teams` topic before deploying the producer. The rolloutorder is: deploy the Kafka-cluster stack, configure `KAFKA_BROKERS` for the-authentication service in Doppler, then deploy the authentication service. If-the producer is deployed before the topic is available, team mutations still-succeed, but events can be lost under the at-most-once contract.+authentication service (producer) and document storage service (teammate-DM+consumer) in Doppler, then deploy those services. If the producer is deployed+before the topic is available, team mutations still succeed, but events can be+lost under the at-most-once contract.diff --git a/crates/teams/justfile b/crates/teams/justfileindex 512e98bbe4..5e27eadf81 100644--- a/crates/teams/justfile+++ b/crates/teams/justfile@@ -26,3 +26,42 @@ check:test:cargo test++# Ensure teammate DMs against the local MacroDB.+backfill_teammate_dms_local:+ RUST_LOG="${RUST_LOG:-info}" ENVIRONMENT="local" DATABASE_URL="{{ DATABASE_URL }}" \+ cargo run -p teams --features backfill_binary --bin backfill_teammate_dms++# Ensure teammate DMs against the dev MacroDB.+backfill_teammate_dms_dev:+ #!/usr/bin/env bash+ set -euo pipefail+ echo -e "\033[33mWARNING: this will create missing teammate DMs in DEV MacroDB.\033[0m"+ echo -n "Continue? [y/N] "+ read -r response+ if [[ ! "$response" =~ ^[Yy]$ ]]; then+ echo "Backfill cancelled."+ exit 0+ fi+ "{{ just_executable() }}" _backfill_teammate_dms_run dev macro-db-dev macrodb-password-dev++# Ensure teammate DMs against the prod MacroDB.+backfill_teammate_dms_prod:+ #!/usr/bin/env bash+ set -euo pipefail+ echo -e "\033[31mWARNING: this will create missing teammate DMs in PRODUCTION MacroDB.\033[0m"+ echo -n "Type 'prod backfill' to continue: "+ read -r response+ if [[ "$response" != "prod backfill" ]]; then+ echo "Production backfill cancelled."+ exit 0+ fi+ "{{ just_executable() }}" _backfill_teammate_dms_run prod macro-db-prod macrodb-password-prod++_backfill_teammate_dms_run env rds_id secret:+ #!/usr/bin/env bash+ set -euo pipefail+ DATABASE_URL="$(../../tooling/scripts/rds_database_url.sh {{ rds_id }} {{ secret }} macrodb)"+ eval "$(aws configure export-credentials --format env)"+ RUST_LOG="${RUST_LOG:-info}" ENVIRONMENT="{{ env }}" DATABASE_URL="$DATABASE_URL" \+ cargo run -p teams --features backfill_binary --bin backfill_teammate_dmsdiff --git a/crates/teams/src/bin/backfill_teammate_dms.rs b/crates/teams/src/bin/backfill_teammate_dms.rsnew file mode 100644index 0000000000..bbdf2abb65--- /dev/null+++ b/crates/teams/src/bin/backfill_teammate_dms.rs@@ -0,0 +1,109 @@+use std::collections::HashSet;++use anyhow::Context;+use channels::{+ domain::{dm::ensure_dms_for_roster, ports::ChannelService, service::ChannelServiceImpl},+ outbound::pg_channels_repo::PgChannelsRepo,+};+use database_env_vars::DatabaseUrl;+use macro_entrypoint::MacroEntrypoint;+use macro_user_id::user_id::MacroUserIdStr;+use sqlx::postgres::PgPoolOptions;+use teams::{domain::team_repo::TeamRepository, outbound::team_repo::TeamRepositoryImpl};++const PAGE_SIZE: u32 = 100;++#[derive(Default)]+struct Progress {+ created: usize,+ existing: usize,+ failed: usize,+ teams_processed: usize,+}++#[tokio::main]+async fn main() -> anyhow::Result<()> {+ MacroEntrypoint::default().init();++ let database_url = DatabaseUrl::new().context("DATABASE_URL must be provided")?;+ let db = PgPoolOptions::new()+ .min_connections(1)+ .max_connections(5)+ .connect(database_url.as_ref())+ .await+ .context("could not connect to db")?;++ let team_repo = TeamRepositoryImpl::new(db.clone());+ let channels = ChannelServiceImpl::new(PgChannelsRepo::new(db));++ let mut cursor = None;+ let mut total = Progress::default();+ loop {+ let team_ids = team_repo.list_team_ids_after(cursor, PAGE_SIZE).await?;+ let next_team_id = if team_ids.len() == PAGE_SIZE as usize {+ team_ids.last().copied()+ } else {+ None+ };++ for team_id in team_ids {+ total.teams_processed += 1;+ let team_with_members = match team_repo.get_team_by_id(&team_id).await {+ Ok(team_with_members) => team_with_members,+ Err(error) => {+ total.failed += 1;+ tracing::error!(+ error=?error,+ %team_id,+ "failed to load team roster for teammate direct-message backfill"+ );+ continue;+ }+ };+ let owner = MacroUserIdStr::try_from(team_with_members.team.owner_id().to_string())+ .context("team owner id is invalid")?;+ let roster = std::iter::once(owner)+ .chain(+ team_with_members+ .members+ .into_iter()+ .map(|member| member.user_id),+ )+ .collect::<HashSet<_>>();+ match channels.ensure_dms(ensure_dms_for_roster(roster)).await {+ Ok(summary) => {+ total.created += summary.created;+ total.existing += summary.existing;+ total.failed += summary.failed;+ println!(+ "team={team_id} created={} existing={} failed={}",+ summary.created, summary.existing, summary.failed+ );+ }+ Err(error) => {+ total.failed += 1;+ tracing::error!(+ error=?error,+ %team_id,+ "failed to ensure teammate direct messages during backfill"+ );+ }+ }+ }++ let Some(next_team_id) = next_team_id else {+ break;+ };+ cursor = Some(next_team_id);+ }++ println!(+ "total teams_processed={} created={} existing={} failed={}",+ total.teams_processed, total.created, total.existing, total.failed+ );+ if total.failed > 0 {+ anyhow::bail!("teammate direct-message backfill completed with failures");+ }++ Ok(())+}diff --git a/crates/teams/src/domain/events.rs b/crates/teams/src/domain/events.rsindex 9343f09aed..cfa7d678d3 100644--- a/crates/teams/src/domain/events.rs+++ b/crates/teams/src/domain/events.rs@@ -120,6 +120,13 @@ pub struct TeamMemberJoinedMetadata {pub team_id: Uuid,/// User who joined the team.pub member_id: MacroUserIdStr<'static>,+ /// Other current teammates the joining member should have a DM with.+ ///+ /// Sorted and de-duplicated. Empty when the roster could not be loaded or+ /// the team has no other members. Missing on events published before this+ /// field existed.+ #[serde(default)]+ pub teammate_ids: Vec<MacroUserIdStr<'static>>,/// Role assigned to the new member.pub role: TeamRole,/// Mechanism by which the member joined.diff --git a/crates/teams/src/domain/events/test.rs b/crates/teams/src/domain/events/test.rsindex ba05f11c5d..bdd9c9a0b4 100644--- a/crates/teams/src/domain/events/test.rs+++ b/crates/teams/src/domain/events/test.rs@@ -138,6 +138,10 @@ fn topic_events() -> Vec<(TeamTopicEvent, Value)> {TeamTopicEvent::MemberJoined(TeamMemberJoinedMetadata {team_id: team_id(),member_id: user_id("macro|[email protected]"),+ teammate_ids: vec![+ user_id("macro|[email protected]"),+ user_id("macro|[email protected]"),+ ],role: TeamRole::Member,join_method: TeamJoinMethod::InviteAccepted {invite_id: invite_id(),@@ -149,6 +153,10 @@ fn topic_events() -> Vec<(TeamTopicEvent, Value)> {"metadata": {"team_id": TEAM_ID,"member_id": "macro|[email protected]",+ "teammate_ids": [+ "macro|[email protected]",+ "macro|[email protected]"+ ],"role": "member","join_method": {"type": "invite_accepted",@@ -274,6 +282,30 @@ fn constructors_use_teams_topic_bare_uuid_key_and_schema_version_one() {}}+#[test]+fn member_joined_defaults_missing_teammate_ids() {+ let payload = json!({+ "event_id": EVENT_ID,+ "schema_version": 1,+ "event_type": "team.member_joined",+ "metadata": {+ "team_id": TEAM_ID,+ "member_id": "macro|[email protected]",+ "role": "member",+ "join_method": { "type": "domain_auto_join" }+ }+ });++ let event: Event<TeamTopicEvent> =+ serde_json::from_value(payload).expect("legacy member_joined is decodable");+ match event.event {+ TeamTopicEvent::MemberJoined(metadata) => {+ assert!(metadata.teammate_ids.is_empty());+ }+ other => panic!("expected member_joined, got {other:?}"),+ }+}+#[test]fn roles_and_join_methods_serialize_lowercase() {let invite_join = serde_json::to_value(TeamJoinMethod::InviteAccepted {diff --git a/crates/teams/src/domain/team_repo.rs b/crates/teams/src/domain/team_repo.rsindex 5dbd5bfd38..ae48b68ebd 100644--- a/crates/teams/src/domain/team_repo.rs+++ b/crates/teams/src/domain/team_repo.rs@@ -305,6 +305,13 @@ pub trait TeamRepository: Clone + Send + Sync + 'static {team_id: &uuid::Uuid,user_id: &MacroUserIdStr<'_>,) -> impl Future<Output = Result<Option<TeamMember<'static>>, TeamError>> + Send;++ /// Return team ids after `after_team_id`, ordered by id, limited to `limit`.+ fn list_team_ids_after(+ &self,+ after_team_id: Option<uuid::Uuid>,+ limit: u32,+ ) -> impl Future<Output = Result<Vec<uuid::Uuid>, TeamError>> + Send;}/// The TeamMembersService defines read-only team membership queries.diff --git a/crates/teams/src/domain/team_service.rs b/crates/teams/src/domain/team_service.rsindex 326b78e1b5..c0d696f7e4 100644--- a/crates/teams/src/domain/team_service.rs+++ b/crates/teams/src/domain/team_service.rs@@ -303,11 +303,11 @@ where.ok();}- async fn enqueue_joining_user_contacts(+ async fn teammate_ids_except(&self,team_id: &uuid::Uuid,user_id: &MacroUserIdStr<'_>,- ) {+ ) -> Vec<MacroUserIdStr<'static>> {let Some(team_with_members) = self.team_repository.get_team_by_id(team_id)@@ -317,16 +317,16 @@ whereerror = ?error,team_id = %team_id,user_id = %user_id,- "failed to load team roster for contact connections"+ "failed to load team roster for joining-member side effects");}).ok()else {- return;+ return Vec::new();};let joining_user = user_id.clone().into_owned();- let teammates: HashSet<_> = std::iter::once(team_with_members.team.owner_id)+ let mut teammate_ids = std::iter::once(team_with_members.team.owner_id).chain(team_with_members.members@@ -334,23 +334,35 @@ where.map(|member| member.user_id),).filter(|teammate| teammate != &joining_user)- .collect();- let connections = teammates+ .collect::<HashSet<_>>().into_iter()- .map(|teammate| (joining_user.clone(), teammate)).collect::<Vec<_>>();+ teammate_ids.sort_by(|a, b| a.as_ref().cmp(b.as_ref()));+ teammate_ids+ }- if connections.is_empty() {+ async fn enqueue_joining_user_contacts(+ &self,+ user_id: &MacroUserIdStr<'_>,+ teammate_ids: &[MacroUserIdStr<'static>],+ ) {+ if teammate_ids.is_empty() {return;}+ let joining_user = user_id.clone().into_owned();+ let connections = teammate_ids+ .iter()+ .cloned()+ .map(|teammate| (joining_user.clone(), teammate))+ .collect::<Vec<_>>();+self.contacts_enqueuer.enqueue_contact_connections(connections).await.inspect_err(|error| {tracing::error!(error = ?error,- team_id = %team_id,user_id = %user_id,"failed to enqueue team contact connections");@@ -1417,7 +1429,10 @@ where);}- self.enqueue_joining_user_contacts(&team_member.team_id, user_id)+ let teammate_ids = self+ .teammate_ids_except(&team_member.team_id, user_id)+ .await;+ self.enqueue_joining_user_contacts(user_id, &teammate_ids).await;self.track_team_analytics_event(TeamAnalyticsEvent::TeamJoined {@@ -1430,6 +1445,7 @@ whereself.publish_team_event(&TeamMacroEvent::member_joined(TeamMemberJoinedMetadata {team_id: team_member.team_id,member_id: team_member.user_id.clone().into_owned(),+ teammate_ids,role: team_member.role,join_method: TeamJoinMethod::InviteAccepted {invite_id: accepted_invite.invite.id,@@ -1992,13 +2008,16 @@ where);}- self.enqueue_joining_user_contacts(&team_id, user_id).await;+ let teammate_ids = self.teammate_ids_except(&team_id, user_id).await;+ self.enqueue_joining_user_contacts(user_id, &teammate_ids)+ .await;// NOTE: no TeamJoined analytics event here - that event is tied to// the team invite that was accepted, and a domain auto-join has none.self.publish_team_event(&TeamMacroEvent::member_joined(TeamMemberJoinedMetadata {team_id,member_id: team_member.user_id.clone().into_owned(),+ teammate_ids,role: team_member.role,join_method: TeamJoinMethod::DomainAutoJoin,}));diff --git a/crates/teams/src/domain/team_service/test.rs b/crates/teams/src/domain/team_service/test.rsindex 567f012d35..e1544ca690 100644--- a/crates/teams/src/domain/team_service/test.rs+++ b/crates/teams/src/domain/team_service/test.rs@@ -8,6 +8,7 @@ use std::{};use channels::domain::{+ dm::{EnsureDms, EnsureDmsSummary},models::{AttachmentEntityReference, ChannelAttachmentType, ChannelMessageFilters,ChannelParticipant, ChannelType, CreateChannelRequest, CreateChannelResponse,@@ -124,6 +125,7 @@ struct MockTeamRepository {delete_team_calls: Arc<Mutex<Vec<uuid::Uuid>>>,fail_delete_team: bool,fail_get_all_team_members: bool,+ team_ids: Vec<uuid::Uuid>,}impl MockTeamRepository {@@ -197,6 +199,7 @@ impl MockTeamRepository {delete_team_calls: Arc::new(Mutex::new(Vec::new())),fail_delete_team: false,fail_get_all_team_members: false,+ team_ids: Vec::new(),}}@@ -740,6 +743,22 @@ impl TeamRepository for MockTeamRepository {let member = self.add_user_to_team_result.clone();async move { Ok(member) }}++ fn list_team_ids_after(+ &self,+ after_team_id: Option<uuid::Uuid>,+ limit: u32,+ ) -> impl Future<Output = Result<Vec<uuid::Uuid>, TeamError>> + Send {+ let mut team_ids = self+ .team_ids+ .iter()+ .copied()+ .filter(|team_id| after_team_id.is_none_or(|after| *team_id > after))+ .collect::<Vec<_>>();+ team_ids.sort();+ team_ids.truncate(limit as usize);+ async move { Ok(team_ids) }+ }}// -- Mock CustomerRepository --@@ -867,11 +886,14 @@ struct RecordingChannelService {auto_join_calls: Arc<Mutex<Vec<(uuid::Uuid, String)>>>,leave_calls: Arc<Mutex<Vec<(uuid::Uuid, String)>>>,restore_calls: Arc<Mutex<Vec<(String, Vec<uuid::Uuid>)>>>,+ ensure_dms_calls: Arc<Mutex<Vec<EnsureDms>>>,+ ensure_dms_summary: EnsureDmsSummary,leave_channel_ids: Vec<uuid::Uuid>,fail_create: bool,fail_auto_join: bool,fail_leave: bool,fail_restore: bool,+ fail_ensure_dms: bool,}impl ChannelService for RecordingChannelService {@@ -1022,6 +1044,24 @@ impl ChannelService for RecordingChannelService {}}}++ fn ensure_dms(+ &self,+ command: EnsureDms,+ ) -> impl Future<Output = Result<EnsureDmsSummary, ChannelMutationErr>> + Send {+ self.ensure_dms_calls.lock().unwrap().push(command);+ let summary = self.ensure_dms_summary;+ let fail = self.fail_ensure_dms;+ async move {+ if fail {+ Err(ChannelMutationErr::Repo(anyhow::anyhow!(+ "ensure direct messages failed"+ )))+ } else {+ Ok(summary)+ }+ }+ }}// -- Mock UserRolesAndPermissionsService --@@ -2004,6 +2044,7 @@ async fn create_team_creates_default_team_channel() {team_repository.created_team = team;let channel_service = RecordingChannelService::default();let create_calls = channel_service.create_calls.clone();+ let ensure_dms_calls = channel_service.ensure_dms_calls.clone();let service = TeamServiceImpl::new(team_repository,MockCustomerRepository::default(),@@ -2035,6 +2076,7 @@ async fn create_team_creates_default_team_channel() {call.request.participants,HashSet::from([owner.into_owned()]));+ assert!(ensure_dms_calls.lock().unwrap().is_empty());}#[tokio::test]@@ -4132,6 +4174,7 @@ async fn team_event_invite_join_uses_accepted_invite_snapshot() {let metadata = &events[0].envelope["metadata"];assert_eq!(metadata["team_id"], team_id.to_string());assert_eq!(metadata["member_id"], user_id.as_ref());+ assert_eq!(metadata["teammate_ids"], serde_json::json!([]));assert_eq!(metadata["role"], "member");assert_eq!(metadata["join_method"]["type"], "invite_accepted");assert_eq!(metadata["join_method"]["invite_id"], invite_id.to_string());@@ -4216,6 +4259,7 @@ async fn test_remove_user_from_team_decrements_customer_seat_count() {let channels_repo = RecordingChannelService::default();let remove_channel_calls = channels_repo.leave_calls.clone();+ let ensure_dms_calls = channels_repo.ensure_dms_calls.clone();let roles_service = MockUserRolesAndPermissionsService::default();let remove_role_calls = roles_service.remove_calls.clone();@@ -4248,6 +4292,7 @@ async fn test_remove_user_from_team_decrements_customer_seat_count() {vec![(team_id, member_id.as_ref().to_string())]);assert_eq!(remove_role_calls.lock().unwrap().len(), 1);+ assert!(ensure_dms_calls.lock().unwrap().is_empty());}#[tokio::test]@@ -4933,6 +4978,7 @@ async fn try_join_team_by_domain_enterprise_bypasses_billing_and_preserves_side_let metadata = &published_events[0].envelope["metadata"];assert_eq!(metadata["team_id"], team_id.to_string());assert_eq!(metadata["member_id"], user_id.as_ref());+ assert_eq!(metadata["teammate_ids"], serde_json::json!([]));assert_eq!(metadata["role"], "member");assert_eq!(metadata["join_method"]["type"], "domain_auto_join");}@@ -6046,3 +6092,99 @@ async fn try_join_team_by_domain_free_team_at_cap_skips() {assert!(increment_calls.lock().unwrap().is_empty());assert!(roles_service.upsert_calls.lock().unwrap().is_empty());}++#[tokio::test]+async fn invite_join_does_not_create_teammate_dms_inline() {+ let team_id = uuid::Uuid::from_u128(7100);+ let invite_id = uuid::Uuid::from_u128(7101);+ let owner = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();+ let joiner = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();+ let teammate = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();+ let team = Team::new(+ team_id,+ "DM Team".to_string(),+ "DM_TEAM".to_string(),+ owner.clone().into_owned(),+ false,+ true,+ );+ let team_repository = make_enterprise_join_team_repository(team_id, invite_id, &joiner)+ .with_team(team)+ .with_team_members(vec![+ make_team_member(team_id, owner.as_ref(), TeamRole::Owner),+ make_team_member(team_id, teammate.as_ref(), TeamRole::Member),+ make_team_member(team_id, teammate.as_ref(), TeamRole::Admin),+ make_team_member(team_id, joiner.as_ref(), TeamRole::Member),+ ]);+ let channels = RecordingChannelService::default();+ let event_broker = RecordingEventBroker::default();+ let service = TeamServiceImpl::new(+ team_repository,+ MockCustomerRepository::default(),+ channels.clone(),+ MockUserRolesAndPermissionsService::default(),+ Arc::new(MockNotificationIngress::new(HashSet::new())),+ NoOpCrmEnqueuer,+ NoOpTeamCrmSettingsRepository,+ )+ .with_event_broker(event_broker.clone());++ service.join_team(&invite_id, &joiner).await.unwrap();++ assert!(channels.ensure_dms_calls.lock().unwrap().is_empty());+ let events = event_broker.events();+ assert_eq!(events.len(), 1);+ assert_eq!(+ events[0].envelope["metadata"]["teammate_ids"],+ serde_json::json!([owner.as_ref(), teammate.as_ref()])+ );+}++#[tokio::test]+async fn domain_join_does_not_create_teammate_dms_inline() {+ let team_id = uuid::Uuid::from_u128(7110);+ let owner = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();+ let joiner = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();+ let teammate = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();+ let team = Team::new(+ team_id,+ "DM Team".to_string(),+ "DM_TEAM".to_string(),+ owner.clone().into_owned(),+ false,+ true,+ );+ let team_repository = make_enterprise_domain_join_team_repository(team_id, &joiner)+ .with_team(team)+ .with_team_members(vec![+ make_team_member(team_id, teammate.as_ref(), TeamRole::Member),+ make_team_member(team_id, teammate.as_ref(), TeamRole::Admin),+ make_team_member(team_id, joiner.as_ref(), TeamRole::Member),+ ]);+ let channels = RecordingChannelService::default();+ let event_broker = RecordingEventBroker::default();+ let service = TeamServiceImpl::new(+ team_repository,+ MockCustomerRepository::default(),+ channels.clone(),+ MockUserRolesAndPermissionsService::default(),+ Arc::new(MockNotificationIngress::new(HashSet::new())),+ NoOpCrmEnqueuer,+ NoOpTeamCrmSettingsRepository,+ )+ .with_event_broker(event_broker.clone());++ service+ .try_join_team_by_domain(&joiner)+ .await+ .unwrap()+ .unwrap();++ assert!(channels.ensure_dms_calls.lock().unwrap().is_empty());+ let events = event_broker.events();+ assert_eq!(events.len(), 1);+ assert_eq!(+ events[0].envelope["metadata"]["teammate_ids"],+ serde_json::json!([owner.as_ref(), teammate.as_ref()])+ );+}diff --git a/crates/teams/src/outbound/team_repo.rs b/crates/teams/src/outbound/team_repo.rsindex 08c3fde4b7..8c0d55f8e8 100644--- a/crates/teams/src/outbound/team_repo.rs+++ b/crates/teams/src/outbound/team_repo.rs@@ -1542,6 +1542,27 @@ impl TeamRepository for TeamRepositoryImpl {}))}+ #[tracing::instrument(skip(self), err)]+ async fn list_team_ids_after(+ &self,+ after_team_id: Option<uuid::Uuid>,+ limit: u32,+ ) -> Result<Vec<uuid::Uuid>, TeamError> {+ Ok(sqlx::query_scalar!(+ r#"+ SELECT id+ FROM team+ WHERE $1::uuid IS NULL OR id > $1+ ORDER BY id+ LIMIT $2+ "#,+ after_team_id,+ i64::from(limit),+ )+ .fetch_all(&self.pool)+ .await?)+ }+#[tracing::instrument(skip(self), err)]async fn get_team_id_by_domain(&self,diff --git a/services/authentication_service/justfile b/services/authentication_service/justfileindex 9ccb7cd52e..b4fa9cedde 100644--- a/services/authentication_service/justfile+++ b/services/authentication_service/justfile@@ -1,5 +1,7 @@set dotenv-load := true+import '../../tooling/just/database.just'+mod ci '../../tooling/just/ci.just'deploy-dev:diff --git a/services/document_storage_service/Cargo.toml b/services/document_storage_service/Cargo.tomlindex a4e05ff238..4ef3ac5b9e 100644--- a/services/document_storage_service/Cargo.toml+++ b/services/document_storage_service/Cargo.toml@@ -60,6 +60,7 @@ channel_bots = { path = "../../crates/channel_bots" }channels = { path = "../../crates/channels", features = ["entity_mutation","inbound",+ "kafka_consumer","list","outbound",] }diff --git a/services/document_storage_service/src/main.rs b/services/document_storage_service/src/main.rsindex f90f0284b3..afc418e199 100644--- a/services/document_storage_service/src/main.rs+++ b/services/document_storage_service/src/main.rs@@ -903,6 +903,47 @@ async fn main() -> anyhow::Result<()> {)),);+ let teammate_dms_brokers = config.kafka_brokers.as_ref().to_string();+ consumer_tracker.spawn({+ let cancellation_token = consumer_cancellation_token.clone();+ let channels = (*channels_service).clone();+ async move {+ loop {+ if cancellation_token.is_cancelled() {+ break;+ }++ tracing::info!("starting teammate DM consumer");+ let result = channels::inbound::teammate_dms_consumer::run_teammate_dms_consumer(+ &teammate_dms_brokers,+ channels.clone(),+ cancellation_token.cancelled(),+ )+ .await;++ if cancellation_token.is_cancelled() {+ break;+ }++ match result {+ Ok(()) => tracing::error!("teammate DM consumer exited unexpectedly"),+ Err(error) => {+ tracing::error!(+ error = ?error,+ "teammate DM consumer exited unexpectedly"+ );+ }+ }++ tokio::select! {+ biased;+ _ = cancellation_token.cancelled() => break,+ _ = tokio::time::sleep(Duration::from_secs(5)) => {}+ }+ }+ }+ });+// 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.