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(bots): kafka producer (#5031)
Will Hutchinson86268130a47b
diff --git a/.github/kafka-cluster-topics.json b/.github/kafka-cluster-topics.jsonindex 62efe7c442..c5d6c94750 100644--- a/.github/kafka-cluster-topics.json+++ b/.github/kafka-cluster-topics.json@@ -1,4 +1,5 @@[+ "macro.bots","macro.channels","macro.documents","macro.email",diff --git a/Cargo.lock b/Cargo.lockindex 2995496edc..a8fe9d4fdc 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -2138,6 +2138,8 @@ dependencies = ["hex","macro_authorization","macro_db_migrator",+ "macro_event_broker",+ "macro_event_topics","macro_user_id","macro_uuid","model-error-response",diff --git a/crates/bots/Cargo.toml b/crates/bots/Cargo.tomlindex 7aaf7bf73f..6c9ae8c6b7 100644--- a/crates/bots/Cargo.toml+++ b/crates/bots/Cargo.toml@@ -19,7 +19,7 @@ inbound = ["ports",]outbound = ["dep:sqlx", "dep:macro_uuid", "ports"]-ports = ["dep:tokio"]+ports = ["dep:tokio", "macro_event_broker/ports"][dependencies]anyhow = { workspace = true }@@ -30,6 +30,8 @@ chrono = { workspace = true }entity_access = { path = "../entity_access", optional = true, default-features = false }hex = { workspace = true }macro_authorization = { path = "../macro_authorization", optional = true, default-features = false, features = ["axum"] }+macro_event_broker = { path = "../macro_event_broker", default-features = false }+macro_event_topics = { path = "../macro_event_topics" }macro_user_id = { path = "../macro_user_id" }macro_uuid = { path = "../macro_uuid", optional = true }model-error-response = { path = "../model-error-response", optional = true }diff --git a/crates/bots/README.md b/crates/bots/README.mdnew file mode 100644index 0000000000..0efef57933--- /dev/null+++ b/crates/bots/README.md@@ -0,0 +1,81 @@+# Bots crate++This crate owns bot models, persistence ports, service policy, and HTTP adapters.+The bot service also produces sanitized lifecycle events for successful bot+mutations.++## Lifecycle event producer++Successful create, patch, and soft-delete mutations publish schema-version-1+events to the `macro.bots` Kafka topic. Every record uses the subject bot's bare+UUID string as its Kafka key (for example,+`0197f776-6e7b-7c69-a251-780ae754d3e4`, without a `bot|` prefix). Events for one+bot therefore use the same Kafka partition key. The Kafka key is not part of+the JSON envelope.++| Event name | Emitted after | Metadata |+| --- | --- | --- |+| `bot.created` | Regular or channel-scoped bot creation | `bot_id`, `kind`, `owner`, `name`, `handle`, `description`, `avatar_url`, `created_by_user_id`, `channel_id`, `created_at` |+| `bot.updated` | Bot PATCH | `bot_id`, `owner`, `actor_user_id`, requested `name`, `handle`, `description`, and `avatar_url` fields, plus `updated_at` |+| `bot.deleted` | Bot soft deletion | `bot_id`, `owner`, `actor_user_id` |++`owner` is a tagged union. A user owner is encoded as+`{"type":"user","user_id":"..."}`, while a team owner is encoded as+`{"type":"team","team_id":"..."}`. On `bot.created`, `channel_id` is the+channel UUID for channel-scoped creation and `null` for regular creation. On+`bot.updated`, the mutable fields describe the requested PATCH rather than a+full bot snapshot; an omitted field is serialized as `null`.++A created event has this wire-envelope shape:++```json+{+ "event_id": "01998a30-1a2b-7c3d-9e4f-5a6b7c8d9e0f",+ "schema_version": 1,+ "event_type": "bot.created",+ "metadata": {+ "bot_id": "0197f776-6e7b-7c69-a251-780ae754d3e4",+ "kind": "owned",+ "owner": {+ "type": "team",+ "team_id": "3f6f8b0a-6f9f-4a3f-9c3a-2b1e5d4c7a90"+ },+ "name": "Deploy Bot",+ "handle": "deploy-bot",+ "description": null,+ "avatar_url": null,+ "created_by_user_id": "macro|[email protected]",+ "channel_id": null,+ "created_at": "2026-07-20T17:01:02Z"+ }+}+```++## Sanitization and excluded flows++Lifecycle payloads never expose bot token values, hashes, IDs, prefixes, labels,+or expiration metadata. This applies to channel-scoped creation even though its+HTTP response contains newly generated token material.++Only successful bot creation, PATCH, and soft deletion produce lifecycle+events. Read and list operations, token creation, use, rotation, revocation, and+other token lifecycle operations do not publish them. Channel-membership flows+also do not publish bot lifecycle events.++Webhook consumption of `macro.bots` is not included in this change. The webhook+Kafka consumer and webhook delivery pipeline do not consume or deliver these+bot events.++## Delivery semantics and rollout++Publication occurs after the repository mutation and is fire-and-forget. The API+does not wait for Kafka delivery, and a scheduling or publication failure is+logged without rolling back or failing an otherwise successful mutation. There+is no transaction, outbox, or retry coupling database persistence to event+publication. A persisted mutation can therefore have no corresponding event;+production is at-most-once relative to bot lifecycle mutations.++Provision `macro.bots` before deploying the Document Storage Service (DSS)+producer. If DSS is deployed first, mutations still succeed, but events emitted+before the topic is available can be benignly lost under the at-most-once+contract.diff --git a/crates/bots/src/domain/events.rs b/crates/bots/src/domain/events.rsnew file mode 100644index 0000000000..a2c4495230--- /dev/null+++ b/crates/bots/src/domain/events.rs@@ -0,0 +1,146 @@+//! Kafka event models for the `macro.bots` topic.+//!+//! Lifecycle events contain sanitized bot metadata. Bot token values, hashes,+//! identifiers, and token metadata are deliberately excluded from every payload.++#[cfg(test)]+mod test;++use chrono::{DateTime, Utc};+use macro_event_broker::{Event, MacroEvent, TopicEvent};+use macro_event_topics::MacroBotsTopic;+use macro_user_id::user_id::MacroUserIdStr;+use serde::{Deserialize, Serialize};+use uuid::Uuid;++use super::models::{BotId, BotKind, BotOwner};++/// Metadata for [`BotTopicEvent::Created`].+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+pub struct BotCreatedMetadata {+ /// Identifier of the created bot.+ pub bot_id: BotId,+ /// Kind of the created bot.+ pub kind: BotKind,+ /// User or team that owns the bot.+ pub owner: BotOwner,+ /// Display name of the bot.+ pub name: String,+ /// Stable handle of the bot.+ pub handle: String,+ /// Optional bot description.+ pub description: Option<String>,+ /// Optional bot avatar URL.+ pub avatar_url: Option<String>,+ /// Authenticated user who created the bot.+ pub created_by_user_id: MacroUserIdStr<'static>,+ /// Channel for a channel-scoped creation, if any.+ pub channel_id: Option<Uuid>,+ /// Creation timestamp reported by the repository.+ pub created_at: DateTime<Utc>,+}++/// Metadata for [`BotTopicEvent::Updated`].+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+pub struct BotUpdatedMetadata {+ /// Identifier of the updated bot.+ pub bot_id: BotId,+ /// User or team that owns the bot.+ pub owner: BotOwner,+ /// Authenticated user who updated the bot.+ pub actor_user_id: MacroUserIdStr<'static>,+ /// Requested display name, or `None` when the PATCH omitted it.+ pub name: Option<String>,+ /// Requested stable handle, or `None` when the PATCH omitted it.+ pub handle: Option<String>,+ /// Requested description, or `None` when the PATCH omitted it.+ pub description: Option<String>,+ /// Requested avatar URL, or `None` when the PATCH omitted it.+ pub avatar_url: Option<String>,+ /// Update timestamp reported by the repository.+ pub updated_at: DateTime<Utc>,+}++/// Metadata for [`BotTopicEvent::Deleted`].+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+pub struct BotDeletedMetadata {+ /// Identifier of the deleted bot.+ pub bot_id: BotId,+ /// User or team that owned the bot.+ pub owner: BotOwner,+ /// Authenticated user who deleted the bot.+ pub actor_user_id: MacroUserIdStr<'static>,+}++/// Lifecycle events published to [`MacroBotsTopic`].+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+#[serde(tag = "event_type", content = "metadata")]+pub enum BotTopicEvent {+ /// A bot was created.+ #[serde(rename = "bot.created")]+ Created(BotCreatedMetadata),+ /// A bot was updated.+ #[serde(rename = "bot.updated")]+ Updated(BotUpdatedMetadata),+ /// A bot was soft-deleted.+ #[serde(rename = "bot.deleted")]+ Deleted(BotDeletedMetadata),+}++impl TopicEvent for BotTopicEvent {+ type Topic = MacroBotsTopic;++ fn schema_version(&self) -> u8 {+ 1+ }+}++/// Publishable lifecycle event for [`MacroBotsTopic`], keyed by bot id.+pub struct BotMacroEvent {+ key: String,+ event: Event<BotTopicEvent>,+}++impl BotMacroEvent {+ /// Build a created event keyed by the created bot id.+ pub fn created(metadata: BotCreatedMetadata) -> Self {+ let key = metadata.bot_id.to_string();+ Self::new(key, BotTopicEvent::Created(metadata))+ }++ /// Build an updated event keyed by the updated bot id.+ pub fn updated(metadata: BotUpdatedMetadata) -> Self {+ let key = metadata.bot_id.to_string();+ Self::new(key, BotTopicEvent::Updated(metadata))+ }++ /// Build a deleted event keyed by the deleted bot id.+ pub fn deleted(metadata: BotDeletedMetadata) -> Self {+ let key = metadata.bot_id.to_string();+ Self::new(key, BotTopicEvent::Deleted(metadata))+ }++ fn new(key: String, event: BotTopicEvent) -> Self {+ Self::with_event(key, Event::new(event))+ }++ fn with_event(key: String, event: Event<BotTopicEvent>) -> Self {+ Self { key, event }+ }+}++impl MacroEvent for BotMacroEvent {+ type EventPayload = BotTopicEvent;++ 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::with_event(key, event)+ }+}diff --git a/crates/bots/src/domain/events/test.rs b/crates/bots/src/domain/events/test.rsnew file mode 100644index 0000000000..c192d0104c--- /dev/null+++ b/crates/bots/src/domain/events/test.rs@@ -0,0 +1,256 @@+use chrono::{DateTime, Utc};+use macro_event_broker::{Event, MacroEvent};+use macro_event_topics::Topic;+use macro_user_id::user_id::MacroUserIdStr;+use serde_json::{Value, json};+use uuid::Uuid;++use super::*;++const BOT_ID: &str = "0197f776-6e7b-7c69-a251-780ae754d3e4";+const TEAM_ID: &str = "3f6f8b0a-6f9f-4a3f-9c3a-2b1e5d4c7a90";++fn bot_id() -> BotId {+ BotId::parse_uuid_str(BOT_ID).expect("valid bot id")+}++fn user_id(id: &str) -> MacroUserIdStr<'static> {+ MacroUserIdStr::try_from(id.to_string()).expect("valid user id")+}++fn timestamp(value: &str) -> DateTime<Utc> {+ DateTime::parse_from_rfc3339(value)+ .expect("valid timestamp")+ .with_timezone(&Utc)+}++fn team_owner() -> BotOwner {+ BotOwner::Team {+ team_id: Uuid::parse_str(TEAM_ID).expect("valid team id"),+ }+}++fn user_owner() -> BotOwner {+ BotOwner::User {+ user_id: "macro|[email protected]".to_string(),+ }+}++fn created_metadata() -> BotCreatedMetadata {+ BotCreatedMetadata {+ bot_id: bot_id(),+ kind: BotKind::Owned,+ owner: team_owner(),+ name: "Deploy Bot".to_string(),+ handle: "deploy-bot".to_string(),+ description: None,+ avatar_url: None,+ created_by_user_id: user_id("macro|[email protected]"),+ channel_id: None,+ created_at: timestamp("2026-07-20T17:01:02Z"),+ }+}++fn updated_metadata() -> BotUpdatedMetadata {+ BotUpdatedMetadata {+ bot_id: bot_id(),+ owner: user_owner(),+ actor_user_id: user_id("macro|[email protected]"),+ name: None,+ handle: Some("release-bot".to_string()),+ description: Some("Ships releases".to_string()),+ avatar_url: None,+ updated_at: timestamp("2026-07-20T17:03:11Z"),+ }+}++fn deleted_metadata() -> BotDeletedMetadata {+ BotDeletedMetadata {+ bot_id: bot_id(),+ owner: team_owner(),+ actor_user_id: user_id("macro|[email protected]"),+ }+}++#[test]+fn created_event_has_exact_sanitized_wire_shape() {+ let event = Event::with_event_id(+ Uuid::parse_str("01998a30-1a2b-7c3d-9e4f-5a6b7c8d9e0f").expect("valid event id"),+ BotTopicEvent::Created(created_metadata()),+ );++ assert_eq!(+ serde_json::to_value(event).expect("serializable event"),+ json!({+ "event_id": "01998a30-1a2b-7c3d-9e4f-5a6b7c8d9e0f",+ "schema_version": 1,+ "event_type": "bot.created",+ "metadata": {+ "bot_id": BOT_ID,+ "kind": "owned",+ "owner": { "type": "team", "team_id": TEAM_ID },+ "name": "Deploy Bot",+ "handle": "deploy-bot",+ "description": null,+ "avatar_url": null,+ "created_by_user_id": "macro|[email protected]",+ "channel_id": null,+ "created_at": "2026-07-20T17:01:02Z",+ },+ })+ );+}++#[test]+fn updated_event_has_exact_sanitized_wire_shape() {+ let event = Event::with_event_id(+ Uuid::parse_str("01998a30-2b3c-7d4e-8f50-6b7c8d9e0f1a").expect("valid event id"),+ BotTopicEvent::Updated(updated_metadata()),+ );++ assert_eq!(+ serde_json::to_value(event).expect("serializable event"),+ json!({+ "event_id": "01998a30-2b3c-7d4e-8f50-6b7c8d9e0f1a",+ "schema_version": 1,+ "event_type": "bot.updated",+ "metadata": {+ "bot_id": BOT_ID,+ "owner": {+ "type": "user",+ "user_id": "macro|[email protected]",+ },+ "actor_user_id": "macro|[email protected]",+ "name": null,+ "handle": "release-bot",+ "description": "Ships releases",+ "avatar_url": null,+ "updated_at": "2026-07-20T17:03:11Z",+ },+ })+ );+}++#[test]+fn deleted_event_has_exact_wire_shape() {+ let event = Event::with_event_id(+ Uuid::parse_str("01998a30-3c4d-7e5f-8051-7c8d9e0f1a2b").expect("valid event id"),+ BotTopicEvent::Deleted(deleted_metadata()),+ );++ assert_eq!(+ serde_json::to_value(event).expect("serializable event"),+ json!({+ "event_id": "01998a30-3c4d-7e5f-8051-7c8d9e0f1a2b",+ "schema_version": 1,+ "event_type": "bot.deleted",+ "metadata": {+ "bot_id": BOT_ID,+ "owner": { "type": "team", "team_id": TEAM_ID },+ "actor_user_id": "macro|[email protected]",+ },+ })+ );+}++#[test]+fn constructors_use_bot_topic_bare_uuid_key_and_schema_version_one() {+ let cases = [+ BotMacroEvent::created(created_metadata()),+ BotMacroEvent::updated(updated_metadata()),+ BotMacroEvent::deleted(deleted_metadata()),+ ];+ let expected_event_types = ["bot.created", "bot.updated", "bot.deleted"];++ for (event, expected_event_type) in cases.into_iter().zip(expected_event_types) {+ assert_eq!(event.key(), BOT_ID);+ assert!(!event.key().starts_with("bot|"));+ assert_eq!(event.topic().as_str(), "macro.bots");+ assert_eq!(event.event().schema_version, 1);+ assert_eq!(+ serde_json::to_value(event.event()).expect("serializable event")["event_type"],+ expected_event_type+ );+ }+}++#[test]+fn every_event_variant_round_trips() {+ let cases = [+ BotMacroEvent::created(created_metadata()),+ BotMacroEvent::updated(updated_metadata()),+ BotMacroEvent::deleted(deleted_metadata()),+ ];++ for original in cases {+ let payload = serde_json::to_vec(original.event()).expect("serializable event");+ let decoded = BotMacroEvent::decode(original.key(), &payload).expect("decodable event");++ assert_eq!(decoded.key(), BOT_ID);+ assert_eq!(decoded.event(), original.event());+ assert_eq!(decoded.topic().as_str(), "macro.bots");+ }+}++#[test]+fn created_event_serializes_channel_id_when_present() {+ let channel_id =+ Uuid::parse_str("8bfe0c32-5609-47ce-bc54-ef30ab686fb3").expect("valid channel id");+ let mut metadata = created_metadata();+ metadata.channel_id = Some(channel_id);+ metadata.description = Some("Channel automation".to_string());+ metadata.avatar_url = Some("https://example.com/avatar.png".to_string());++ let value =+ serde_json::to_value(BotMacroEvent::created(metadata).event()).expect("serializable event");++ assert_eq!(value["metadata"]["channel_id"], channel_id.to_string());+ assert_eq!(value["metadata"]["description"], "Channel automation");+ assert_eq!(+ value["metadata"]["avatar_url"],+ "https://example.com/avatar.png"+ );+}++#[test]+fn lifecycle_payloads_exclude_token_fields_and_values() {+ let events = [+ BotMacroEvent::created(created_metadata()),+ BotMacroEvent::updated(updated_metadata()),+ BotMacroEvent::deleted(deleted_metadata()),+ ];+ let forbidden_fields = [+ "token",+ "bot_token",+ "bearer_token",+ "token_id",+ "token_hash",+ "token_prefix",+ "token_label",+ "token_expires_at",+ "expires_at",+ ];+ let forbidden_values = ["secret-bot-token", "known-token-hash", "bot_tok_prefix"];++ for event in events {+ let value = serde_json::to_value(event.event()).expect("serializable event");+ let serialized = value.to_string();++ for field in forbidden_fields {+ assert!(!contains_field(&value, field), "payload included {field}");+ }+ for secret in forbidden_values {+ assert!(!serialized.contains(secret), "payload exposed {secret}");+ }+ }+}++fn contains_field(value: &Value, field: &str) -> bool {+ match value {+ Value::Object(object) => {+ object.contains_key(field) || object.values().any(|value| contains_field(value, field))+ }+ Value::Array(values) => values.iter().any(|value| contains_field(value, field)),+ _ => false,+ }+}diff --git a/crates/bots/src/domain/mod.rs b/crates/bots/src/domain/mod.rsindex dc3ae0c350..fe88a27744 100644--- a/crates/bots/src/domain/mod.rs+++ b/crates/bots/src/domain/mod.rs@@ -1,5 +1,7 @@//! Domain layer for bots.+/// Bot lifecycle event contracts.+pub mod events;/// Bot domain models.pub mod models;#[cfg(feature = "ports")]diff --git a/crates/bots/src/domain/service.rs b/crates/bots/src/domain/service.rsindex 730a458ec9..a3e4e19521 100644--- a/crates/bots/src/domain/service.rs+++ b/crates/bots/src/domain/service.rs@@ -1,6 +1,7 @@//! Bot service implementation.use super::{+ events::{BotCreatedMetadata, BotDeletedMetadata, BotMacroEvent, BotUpdatedMetadata},models::{AuthenticatedBot, Bot, BotChannel, BotId, BotKind, BotOwner, BotToken, BotTokenCandidate,CreateBotRequest, CreateBotTokenRequest, CreateChannelScopedBotRequest,@@ -10,19 +11,21 @@ use super::{tokens,};use chrono::{DateTime, Utc};+use macro_event_broker::MacroEventBroker;use macro_user_id::user_id::MacroUserIdStr;use uuid::Uuid;/// Bot service implementation.#[derive(Debug, Clone)]-pub struct BotServiceImpl<R> {+pub struct BotServiceImpl<R, B> {repo: R,+ event_broker: B,}-impl<R> BotServiceImpl<R> {+impl<R, B> BotServiceImpl<R, B> {/// Create a bot service.- pub fn new(repo: R) -> Self {- Self { repo }+ pub fn new(repo: R, event_broker: B) -> Self {+ Self { repo, event_broker }}}@@ -49,10 +52,17 @@ fn token_candidate_is_valid(candidate: &BotTokenCandidate, now: &DateTime<Utc>).is_none_or(|expires_at| expires_at > now)}-impl<R> BotServiceImpl<R>+impl<R, B> BotServiceImpl<R, B>whereR: BotRepo,+ B: MacroEventBroker,{+ fn publish_bot_event(&self, event: &BotMacroEvent) {+ drop(self.event_broker.send_event(event).inspect_err(|error| {+ tracing::error!(error=?error, "failed to schedule bot lifecycle event");+ }));+ }+async fn owner_for_request(&self,caller: MacroUserIdStr<'static>,@@ -133,9 +143,10 @@ where}}-impl<R> BotService for BotServiceImpl<R>+impl<R, B> BotService for BotServiceImpl<R, B>whereR: BotRepo,+ B: MacroEventBroker + Clone,{async fn create_bot(&self,@@ -144,11 +155,28 @@ where) -> Result<Bot, BotError> {validate_handle(&req.handle)?;let owner = self.owner_for_request(caller.clone(), req.team_id).await?;+ let created_by_user_id = caller.clone();- self.repo+ let bot = self+ .repo.create_owned_bot(owner, caller, req).await- .map_err(|err| BotError::Repo(err.into()))+ .map_err(|err| BotError::Repo(err.into()))?;++ self.publish_bot_event(&BotMacroEvent::created(BotCreatedMetadata {+ bot_id: bot.id,+ kind: bot.kind,+ owner: bot.owner.clone().expect("owned bot must have an owner"),+ name: bot.name.clone(),+ handle: bot.handle.clone(),+ description: bot.description.clone(),+ avatar_url: bot.avatar_url.clone(),+ created_by_user_id,+ channel_id: None,+ created_at: bot.created_at,+ }));++ Ok(bot)}async fn create_channel_scoped_bot(@@ -159,6 +187,7 @@ where) -> Result<CreateChannelScopedBotResponse, BotError> {validate_handle(&req.handle)?;let owner = self.owner_for_request(caller.clone(), req.team_id).await?;+ let created_by_user_id = caller.clone();let generated_token = tokens::generate_token();let (bot, token) = self.repo@@ -167,6 +196,19 @@ where.map_err(|err| BotError::Repo(err.into()))?;let bot_token = token.token.clone();+ self.publish_bot_event(&BotMacroEvent::created(BotCreatedMetadata {+ bot_id: bot.id,+ kind: bot.kind,+ owner: bot.owner.clone().expect("owned bot must have an owner"),+ name: bot.name.clone(),+ handle: bot.handle.clone(),+ description: bot.description.clone(),+ avatar_url: bot.avatar_url.clone(),+ created_by_user_id,+ channel_id: Some(channel_id),+ created_at: bot.created_at,+ }));+Ok(CreateChannelScopedBotResponse {bot,token,@@ -195,15 +237,33 @@ wherebot_id: BotId,req: PatchBotRequest,) -> Result<Bot, BotError> {- self.ensure_manageable(caller, bot_id).await?;+ self.ensure_manageable(caller.clone(), bot_id).await?;if let Some(handle) = &req.handle {validate_handle(handle)?;}- self.repo+ let requested_name = req.name.clone();+ let requested_handle = req.handle.clone();+ let requested_description = req.description.clone();+ let requested_avatar_url = req.avatar_url.clone();+ let bot = self+ .repo.patch_bot(bot_id, req).await.map_err(|err| BotError::Repo(err.into()))?- .ok_or_else(|| BotError::NotFound("bot not found".to_string()))+ .ok_or_else(|| BotError::NotFound("bot not found".to_string()))?;++ self.publish_bot_event(&BotMacroEvent::updated(BotUpdatedMetadata {+ bot_id: bot.id,+ owner: bot.owner.clone().expect("owned bot must have an owner"),+ actor_user_id: caller,+ name: requested_name,+ handle: requested_handle,+ description: requested_description,+ avatar_url: requested_avatar_url,+ updated_at: bot.updated_at,+ }));++ Ok(bot)}async fn delete_bot(@@ -211,17 +271,23 @@ wherecaller: MacroUserIdStr<'static>,bot_id: BotId,) -> Result<(), BotError> {- self.ensure_manageable(caller, bot_id).await?;- if self+ let bot = self.ensure_manageable(caller.clone(), bot_id).await?;+ if !self.repo.delete_bot(bot_id).await.map_err(|err| BotError::Repo(err.into()))?{- Ok(())- } else {- Err(BotError::NotFound("bot not found".to_string()))+ return Err(BotError::NotFound("bot not found".to_string()));}++ self.publish_bot_event(&BotMacroEvent::deleted(BotDeletedMetadata {+ bot_id: bot.id,+ owner: bot.owner.expect("owned bot must have an owner"),+ actor_user_id: caller,+ }));++ Ok(())}async fn add_bot_to_channel(diff --git a/crates/bots/src/inbound/axum_router/tests.rs b/crates/bots/src/inbound/axum_router/tests.rsindex 4b97bbd9bc..2b1506d24a 100644--- a/crates/bots/src/inbound/axum_router/tests.rs+++ b/crates/bots/src/inbound/axum_router/tests.rs@@ -21,6 +21,7 @@ use macro_authorization::{MacroAuthorizationState, ValidatedIdentity,};use macro_db_migrator::MACRO_DB_MIGRATIONS;+use macro_event_broker::NoopMacroEventBroker;use macro_user_id::{lowercased::Lowercase, user_id::MacroUserId};use rootcause::Report;use sqlx::{PgPool, Row};@@ -352,7 +353,7 @@ fn router(service: TestBotService, role: EntityParticipantRole) -> Router {}fn real_router(pool: PgPool, user_id: &str) -> Router {- let bot_service = BotServiceImpl::new(PgBotsRepo::new(pool.clone()));+ let bot_service = BotServiceImpl::new(PgBotsRepo::new(pool.clone()), NoopMacroEventBroker);let access_service = EntityAccessServiceImpl::new(PgAccessRepository::new(pool));let bearer_token = user_id.to_string();bots_router(BotsRouterState::new(@@ -661,7 +662,7 @@ async fn bot_owner_can_list_and_remove_bot_channels_via_bot_routes(insert_user(&pool, CHANNEL_ADMIN_ID).await?;insert_private_channel_with_admin(&pool, channel_id, CHANNEL_ADMIN_ID).await?;- let bot_service = BotServiceImpl::new(PgBotsRepo::new(pool.clone()));+ let bot_service = BotServiceImpl::new(PgBotsRepo::new(pool.clone()), NoopMacroEventBroker);let bot = bot_service.create_bot(macro_user_id(BOT_OWNER_ID),@@ -747,7 +748,7 @@ async fn channel_admin_can_add_and_remove_owned_bot_via_http(pool: PgPool) -> aninsert_user(&pool, ADMIN_USER_ID).await?;insert_private_channel_with_admin(&pool, channel_id, ADMIN_USER_ID).await?;- let bot_service = BotServiceImpl::new(PgBotsRepo::new(pool.clone()));+ let bot_service = BotServiceImpl::new(PgBotsRepo::new(pool.clone()), NoopMacroEventBroker);let bot = bot_service.create_bot(macro_user_id(ADMIN_USER_ID),diff --git a/crates/bots/src/outbound/pg_bots_repo/tests.rs b/crates/bots/src/outbound/pg_bots_repo/tests.rsindex b773ef1e63..1ddbd7b913 100644--- a/crates/bots/src/outbound/pg_bots_repo/tests.rs+++ b/crates/bots/src/outbound/pg_bots_repo/tests.rs@@ -2,12 +2,18 @@ use super::*;use crate::domain::{models::{BotChannelType, CreateBotRequest, CreateBotTokenRequest, CreateChannelScopedBotRequest,+ PatchBotRequest,},ports::{BotError, BotService},service::BotServiceImpl,};use macro_db_migrator::MACRO_DB_MIGRATIONS;+use macro_event_broker::{+ EventBrokerError, MacroEvent, MacroEventBroker, NoopMacroEventBroker, Topic as _,+};+use serde_json::{Value, json};use sqlx::PgPool;+use std::sync::{Arc, Mutex};const USER_OWNER: &str = "macro|[email protected]";const USER_OTHER: &str = "macro|[email protected]";@@ -40,8 +46,99 @@ fn create_channel_scoped_req(handle: &str) -> CreateChannelScopedBotRequest {}}-fn service(pool: &PgPool) -> BotServiceImpl<PgBotsRepo> {- BotServiceImpl::new(PgBotsRepo::new(pool.clone()))+fn service(pool: &PgPool) -> BotServiceImpl<PgBotsRepo, NoopMacroEventBroker> {+ BotServiceImpl::new(PgBotsRepo::new(pool.clone()), NoopMacroEventBroker)+}++#[derive(Clone, Debug)]+struct PublishedEvent {+ topic: &'static str,+ key: String,+ payload: Value,+}++#[derive(Clone, Default)]+struct RecordingEventBroker {+ published: Arc<Mutex<Vec<PublishedEvent>>>,+ fail_scheduling: bool,+}++impl RecordingEventBroker {+ fn failing() -> Self {+ Self {+ fail_scheduling: true,+ ..Self::default()+ }+ }++ fn events(&self) -> Vec<PublishedEvent> {+ self.published.lock().expect("event lock poisoned").clone()+ }++ fn clear(&self) {+ self.published.lock().expect("event lock poisoned").clear();+ }+}++impl MacroEventBroker for RecordingEventBroker {+ fn send_event<E: MacroEvent + ?Sized>(+ &self,+ event: &E,+ ) -> Result<tokio::task::JoinHandle<Result<(), EventBrokerError>>, EventBrokerError> {+ if self.fail_scheduling {+ return Err(EventBrokerError::Publish(+ "intentional scheduling failure".to_string(),+ ));+ }++ self.published+ .lock()+ .expect("event lock poisoned")+ .push(PublishedEvent {+ topic: event.topic().as_str(),+ key: event.key().to_string(),+ payload: serde_json::to_value(event.event())?,+ });+ Ok(tokio::spawn(async { Ok(()) }))+ }+}++fn recording_service(+ pool: &PgPool,+ broker: RecordingEventBroker,+) -> BotServiceImpl<PgBotsRepo, RecordingEventBroker> {+ BotServiceImpl::new(PgBotsRepo::new(pool.clone()), broker)+}++fn assert_event(event: &PublishedEvent, bot_id: BotId, event_type: &str, metadata: Value) {+ assert_eq!(event.topic, "macro.bots");+ assert_eq!(event.key, bot_id.to_string());+ assert_eq!(event.payload["schema_version"], 1);+ assert_eq!(event.payload["event_type"], event_type);+ assert_eq!(event.payload["metadata"], metadata);+}++fn assert_no_token_material(payload: &Value, known_token: Option<&str>) {+ let serialized = serde_json::to_string(payload).expect("event serializes");+ if let Some(known_token) = known_token {+ assert!(!serialized.contains(known_token));+ }+ for forbidden_field in [+ "token",+ "bot_token",+ "bearer_token",+ "token_id",+ "token_hash",+ "token_prefix",+ "token_label",+ "label",+ "token_expires_at",+ "expires_at",+ "last_used_at",+ "revoked_at",+ ] {+ assert!(!serialized.contains(&format!("\"{forbidden_field}\"")));+ }}async fn insert_user(pool: &PgPool, user_id: &str) -> anyhow::Result<()> {@@ -559,3 +656,261 @@ async fn authenticate_channel_token_accepts_migrated_uuid_token(Ok(())}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn lifecycle_creation_publishes_exact_sanitized_events(pool: PgPool) -> anyhow::Result<()> {+ let broker = RecordingEventBroker::default();+ let service = recording_service(&pool, broker.clone());+ let user_bot = service+ .create_bot(user_id(USER_OWNER), create_req("event-user"))+ .await?;++ let team_id = Uuid::new_v4();+ insert_team_member(&pool, team_id, TEAM_MEMBER).await?;+ let mut team_request = create_req("event-team");+ team_request.team_id = Some(team_id);+ let team_bot = service+ .create_bot(user_id(TEAM_MEMBER), team_request)+ .await?;++ let channel_id = Uuid::new_v4();+ insert_channel(&pool, channel_id).await?;+ let channel_bot = service+ .create_channel_scoped_bot(+ user_id(USER_OWNER),+ channel_id,+ create_channel_scoped_req("event-channel"),+ )+ .await?;++ let events = broker.events();+ assert_eq!(events.len(), 3);+ assert_event(+ &events[0],+ user_bot.id,+ "bot.created",+ json!({+ "bot_id": user_bot.id,+ "kind": "owned",+ "owner": { "type": "user", "user_id": USER_OWNER },+ "name": user_bot.name,+ "handle": user_bot.handle,+ "description": user_bot.description,+ "avatar_url": user_bot.avatar_url,+ "created_by_user_id": USER_OWNER,+ "channel_id": null,+ "created_at": user_bot.created_at,+ }),+ );+ assert_event(+ &events[1],+ team_bot.id,+ "bot.created",+ json!({+ "bot_id": team_bot.id,+ "kind": "owned",+ "owner": { "type": "team", "team_id": team_id },+ "name": team_bot.name,+ "handle": team_bot.handle,+ "description": team_bot.description,+ "avatar_url": team_bot.avatar_url,+ "created_by_user_id": TEAM_MEMBER,+ "channel_id": null,+ "created_at": team_bot.created_at,+ }),+ );+ assert_event(+ &events[2],+ channel_bot.bot.id,+ "bot.created",+ json!({+ "bot_id": channel_bot.bot.id,+ "kind": "owned",+ "owner": { "type": "user", "user_id": USER_OWNER },+ "name": channel_bot.bot.name,+ "handle": channel_bot.bot.handle,+ "description": channel_bot.bot.description,+ "avatar_url": channel_bot.bot.avatar_url,+ "created_by_user_id": USER_OWNER,+ "channel_id": channel_id,+ "created_at": channel_bot.bot.created_at,+ }),+ );+ for event in &events {+ assert_no_token_material(&event.payload, Some(&channel_bot.bot_token));+ }++ Ok(())+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn patch_and_delete_publish_requested_fields_and_team_owner(+ pool: PgPool,+) -> anyhow::Result<()> {+ let team_id = Uuid::new_v4();+ insert_team_member(&pool, team_id, TEAM_MEMBER).await?;+ let broker = RecordingEventBroker::default();+ let service = recording_service(&pool, broker.clone());+ let mut create_request = create_req("event-mutations");+ create_request.team_id = Some(team_id);+ let bot = service+ .create_bot(user_id(TEAM_MEMBER), create_request)+ .await?;+ broker.clear();++ let patch_request = PatchBotRequest {+ name: Some("Renamed alerts".to_string()),+ handle: None,+ description: Some("Replacement description".to_string()),+ avatar_url: None,+ };+ let patched = service+ .patch_bot(user_id(TEAM_MEMBER), bot.id, patch_request)+ .await?;++ let events = broker.events();+ assert_eq!(events.len(), 1);+ assert_event(+ &events[0],+ bot.id,+ "bot.updated",+ json!({+ "bot_id": bot.id,+ "owner": { "type": "team", "team_id": team_id },+ "actor_user_id": TEAM_MEMBER,+ "name": "Renamed alerts",+ "handle": null,+ "description": "Replacement description",+ "avatar_url": null,+ "updated_at": patched.updated_at,+ }),+ );+ assert_no_token_material(&events[0].payload, None);++ broker.clear();+ service.delete_bot(user_id(TEAM_MEMBER), bot.id).await?;+ let events = broker.events();+ assert_eq!(events.len(), 1);+ assert_event(+ &events[0],+ bot.id,+ "bot.deleted",+ json!({+ "bot_id": bot.id,+ "owner": { "type": "team", "team_id": team_id },+ "actor_user_id": TEAM_MEMBER,+ }),+ );+ assert_no_token_material(&events[0].payload, None);++ Ok(())+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn non_lifecycle_and_failed_operations_do_not_publish(pool: PgPool) -> anyhow::Result<()> {+ let broker = RecordingEventBroker::default();+ let service = recording_service(&pool, broker.clone());+ let channel_id = Uuid::new_v4();+ insert_channel(&pool, channel_id).await?;+ let bot = service+ .create_bot(user_id(USER_OWNER), create_req("event-exclusions"))+ .await?;+ broker.clear();++ service.list_bots(user_id(USER_OWNER)).await?;+ service.get_bot(user_id(USER_OWNER), bot.id).await?;+ service.list_channel_bots(channel_id).await?;+ service+ .add_bot_to_channel(user_id(USER_OWNER), channel_id, bot.id)+ .await?;+ service+ .list_bot_channels(user_id(USER_OWNER), bot.id)+ .await?;+ service+ .remove_bot_from_channel(user_id(USER_OWNER), channel_id, bot.id)+ .await?;++ let token = service+ .create_token(+ user_id(USER_OWNER),+ bot.id,+ CreateBotTokenRequest {+ label: Some("No event".to_string()),+ expires_at: None,+ },+ )+ .await?;+ service.list_tokens(user_id(USER_OWNER), bot.id).await?;+ service.authenticate_token(&token.bearer_token).await?;+ service+ .revoke_token(user_id(USER_OWNER), bot.id, token.token.id)+ .await?;++ let unauthorized = service+ .patch_bot(+ user_id(USER_OTHER),+ bot.id,+ PatchBotRequest {+ name: Some("Forbidden".to_string()),+ handle: None,+ description: None,+ avatar_url: None,+ },+ )+ .await;+ assert!(matches!(unauthorized, Err(BotError::Unauthorized)));++ let missing = service+ .delete_bot(user_id(USER_OWNER), BotId::new_from_uuid(Uuid::new_v4()))+ .await;+ assert!(matches!(missing, Err(BotError::NotFound(_))));++ let repository_failure = service+ .create_channel_scoped_bot(+ user_id(USER_OWNER),+ Uuid::new_v4(),+ create_channel_scoped_req("missing-channel"),+ )+ .await;+ assert!(matches!(repository_failure, Err(BotError::Repo(_))));+ assert!(broker.events().is_empty());++ Ok(())+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn scheduling_failures_do_not_change_successful_mutations(+ pool: PgPool,+) -> anyhow::Result<()> {+ let service = recording_service(&pool, RecordingEventBroker::failing());+ let channel_id = Uuid::new_v4();+ insert_channel(&pool, channel_id).await?;+ let channel_bot = service+ .create_channel_scoped_bot(+ user_id(USER_OWNER),+ channel_id,+ create_channel_scoped_req("scoped-schedule-failure"),+ )+ .await?;+ assert_eq!(channel_bot.bot.handle, "scoped-schedule-failure");++ let bot = service+ .create_bot(user_id(USER_OWNER), create_req("event-schedule-failure"))+ .await?;+ let patched = service+ .patch_bot(+ user_id(USER_OWNER),+ bot.id,+ PatchBotRequest {+ name: Some("Still succeeds".to_string()),+ handle: None,+ description: None,+ avatar_url: None,+ },+ )+ .await?;+ assert_eq!(patched.name, "Still succeeds");+ service.delete_bot(user_id(USER_OWNER), bot.id).await?;++ Ok(())+}diff --git a/crates/macro_event_topics/src/lib.rs b/crates/macro_event_topics/src/lib.rsindex 8014a720f6..5a4917017d 100644--- a/crates/macro_event_topics/src/lib.rs+++ b/crates/macro_event_topics/src/lib.rs@@ -49,6 +49,8 @@ macro_rules! topics {topics! {/// Example kafka topic.MacroExampleTopic => "macro.example",+ /// Bot lifecycle events (created / updated / deleted).+ MacroBotsTopic => "macro.bots",/// Document lifecycle events (created / updated / deleted / copied).MacroDocumentsTopic => "macro.documents",/// Project lifecycle events (created, updated, deleted, restored, permanently deleted, and uploaded).diff --git a/crates/macro_event_topics/src/test.rs b/crates/macro_event_topics/src/test.rsindex 5efa13e540..ad3d191e02 100644--- a/crates/macro_event_topics/src/test.rs+++ b/crates/macro_event_topics/src/test.rs@@ -21,5 +21,6 @@ fn all_topic_names_includes_declared_topics() {assert!(all_topic_names().contains(&MacroDocumentsTopic.as_str()));assert!(all_topic_names().contains(&MacroProjectsTopic.as_str()));assert!(all_topic_names().contains(&MacroChannelsTopic.as_str()));+ assert!(all_topic_names().contains(&MacroBotsTopic.as_str()));assert!(all_topic_names().contains(&MacroWebhooksTopic.as_str()));}diff --git a/services/document_storage_service/src/api/context.rs b/services/document_storage_service/src/api/context.rsindex 0a32a441a6..81678a63aa 100644--- a/services/document_storage_service/src/api/context.rs+++ b/services/document_storage_service/src/api/context.rs@@ -305,7 +305,8 @@ pub(crate) type DssChannelsState =ChannelsRouterState<DssChannelService, EntityAccessService, AuthorizationService>;/// Type alias for the bots service wired into DSS.-pub(crate) type DssBotService = BotServiceImpl<PgBotsRepo>;+pub(crate) type DssBotService =+ BotServiceImpl<PgBotsRepo, MacroEventBrokerService<KafkaEventPublisher>>;/// Type alias for the bots router state.pub(crate) type DssBotsState =diff --git a/services/document_storage_service/src/main.rs b/services/document_storage_service/src/main.rsindex aba283e41d..33278c3fdc 100644--- a/services/document_storage_service/src/main.rs+++ b/services/document_storage_service/src/main.rs@@ -780,7 +780,8 @@ async fn main() -> anyhow::Result<()> {));let channels_repo = PgChannelsRepo::new(db.clone());let bots_repo = bots::outbound::pg_bots_repo::PgBotsRepo::new(db.clone());- let bots_service = bots::domain::service::BotServiceImpl::new(bots_repo.clone());+ let bots_service =+ bots::domain::service::BotServiceImpl::new(bots_repo.clone(), macro_event_broker.clone());let (bot_trigger_sender, bot_trigger_receiver) = tokio::sync::mpsc::unbounded_channel();let channel_side_effects = ChannelSideEffectService::new(