Skip to content

armgit / macro

Public Imported from GitHub
0

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

fix(documents): record a created document's mentions so References finds it (#6339)

gbirman830c9d6ab75c
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.json
index 9c3c202132..a311233d60 100644
--- a/.github/workspace-dep-closures.json
+++ b/.github/workspace-dep-closures.json
@@ -59,6 +59,7 @@
"crates/broadcast",
"crates/channel_sender",
"crates/channels",
+ "crates/comms_db_client",
"crates/connection",
"crates/connection_gateway_client",
"crates/connection_gateway_models",
@@ -160,6 +161,7 @@
"crates/broadcast",
"crates/channel_sender",
"crates/channels",
+ "crates/comms_db_client",
"crates/connection",
"crates/connection_gateway_client",
"crates/connection_gateway_models",
@@ -263,6 +265,7 @@
"crates/channel_sender",
"crates/channels",
"crates/chat",
+ "crates/comms_db_client",
"crates/connection",
"crates/connection_gateway_client",
"crates/connection_gateway_models",
@@ -401,6 +404,7 @@
"crates/channel_sender",
"crates/channels",
"crates/chat",
+ "crates/comms_db_client",
"crates/connection",
"crates/connection_gateway_client",
"crates/connection_gateway_models",
@@ -736,6 +740,7 @@
"crates/channel_sender",
"crates/channels",
"crates/chat",
+ "crates/comms_db_client",
"crates/connection",
"crates/connection_gateway_client",
"crates/connection_gateway_models",
@@ -936,6 +941,7 @@
"crates/channel_sender",
"crates/channels",
"crates/chat",
+ "crates/comms_db_client",
"crates/connection",
"crates/connection_gateway_client",
"crates/connection_gateway_models",
@@ -1561,6 +1567,7 @@
"crates/channel_sender",
"crates/channels",
"crates/chat",
+ "crates/comms_db_client",
"crates/connection",
"crates/connection_gateway_client",
"crates/connection_gateway_models",
@@ -1797,6 +1804,7 @@
"crates/channel_sender",
"crates/channels",
"crates/coding_agent_worker",
+ "crates/comms_db_client",
"crates/connection",
"crates/connection_gateway_client",
"crates/connection_gateway_models",
@@ -1995,6 +2003,7 @@
"crates/channel_sender",
"crates/channels",
"crates/chat",
+ "crates/comms_db_client",
"crates/complete_graph",
"crates/connection",
"crates/connection_gateway_client",
@@ -2521,10 +2530,13 @@
"crates/bot_token",
"crates/broadcast",
"crates/channel_sender",
+ "crates/channels",
"crates/chat",
+ "crates/comms_db_client",
"crates/connection",
"crates/connection_gateway_client",
"crates/connection_gateway_models",
+ "crates/contacts",
"crates/cowlike",
"crates/crm",
"crates/database_env_vars",
@@ -2595,6 +2607,7 @@
"crates/secretsmanager_client",
"crates/share_permission_db_utils",
"crates/sqs_client",
+ "crates/sqs_worker",
"crates/sync_service",
"crates/sync_service_client",
"crates/system_properties",
@@ -2627,6 +2640,7 @@
"crates/channel_sender",
"crates/channels",
"crates/chat",
+ "crates/comms_db_client",
"crates/connection",
"crates/connection_gateway_client",
"crates/connection_gateway_models",
@@ -2991,10 +3005,14 @@
"crates/attachment",
"crates/bot_id",
"crates/bot_token",
+ "crates/broadcast",
"crates/channel_sender",
+ "crates/channels",
+ "crates/comms_db_client",
"crates/connection",
"crates/connection_gateway_client",
"crates/connection_gateway_models",
+ "crates/contacts",
"crates/cowlike",
"crates/database_env_vars",
"crates/document_sub_type",
@@ -3006,11 +3024,14 @@
"crates/entity_mutation",
"crates/filter_ast",
"crates/foreign_entity",
+ "crates/frecency",
"crates/genai_telemetry",
"crates/harness_id",
"crates/harness_token",
+ "crates/invite_email",
"crates/item_filters",
"crates/kafka_util",
+ "crates/last_online_tracker",
"crates/lexical_client",
"crates/macro_auth",
"crates/macro_authorization",
@@ -3025,10 +3046,12 @@
"crates/macro_tower_layers",
"crates/macro_user_id",
"crates/macro_uuid",
+ "crates/mention_utils",
"crates/model",
"crates/model-entity",
"crates/model-error-response",
"crates/model_file_type",
+ "crates/model_notifications",
"crates/model_user",
"crates/models_bulk_upload",
"crates/models_entity_access_management",
@@ -3036,12 +3059,15 @@
"crates/models_permissions",
"crates/models_properties",
"crates/non_empty",
+ "crates/notification",
"crates/notification_state",
"crates/prompt",
+ "crates/rate_limit",
"crates/remote_env_var",
"crates/s3_key",
"crates/secretsmanager_client",
"crates/share_permission_db_utils",
+ "crates/sqs_worker",
"crates/sync_service",
"crates/sync_service_client",
"crates/system_properties",
@@ -3060,10 +3086,14 @@
"crates/attachment",
"crates/bot_id",
"crates/bot_token",
+ "crates/broadcast",
"crates/channel_sender",
+ "crates/channels",
+ "crates/comms_db_client",
"crates/connection",
"crates/connection_gateway_client",
"crates/connection_gateway_models",
+ "crates/contacts",
"crates/cowlike",
"crates/database_env_vars",
"crates/document_sub_type",
@@ -3075,11 +3105,14 @@
"crates/entity_mutation",
"crates/filter_ast",
"crates/foreign_entity",
+ "crates/frecency",
"crates/genai_telemetry",
"crates/harness_id",
"crates/harness_token",
+ "crates/invite_email",
"crates/item_filters",
"crates/kafka_util",
+ "crates/last_online_tracker",
"crates/lexical_client",
"crates/macro_auth",
"crates/macro_authorization",
@@ -3094,10 +3127,12 @@
"crates/macro_tower_layers",
"crates/macro_user_id",
"crates/macro_uuid",
+ "crates/mention_utils",
"crates/model",
"crates/model-entity",
"crates/model-error-response",
"crates/model_file_type",
+ "crates/model_notifications",
"crates/model_user",
"crates/models_bulk_upload",
"crates/models_entity_access_management",
@@ -3105,12 +3140,15 @@
"crates/models_permissions",
"crates/models_properties",
"crates/non_empty",
+ "crates/notification",
"crates/notification_state",
"crates/prompt",
+ "crates/rate_limit",
"crates/remote_env_var",
"crates/s3_key",
"crates/secretsmanager_client",
"crates/share_permission_db_utils",
+ "crates/sqs_worker",
"crates/sync_service",
"crates/sync_service_client",
"crates/system_properties",
@@ -3982,9 +4020,12 @@
"crates/bot_token",
"crates/broadcast",
"crates/channel_sender",
+ "crates/channels",
+ "crates/comms_db_client",
"crates/connection",
"crates/connection_gateway_client",
"crates/connection_gateway_models",
+ "crates/contacts",
"crates/cowlike",
"crates/database_env_vars",
"crates/document_sub_type",
@@ -3996,6 +4037,7 @@
"crates/entity_mutation",
"crates/filter_ast",
"crates/foreign_entity",
+ "crates/frecency",
"crates/fusionauth",
"crates/genai_telemetry",
"crates/github",
@@ -4040,6 +4082,7 @@
"crates/s3_key",
"crates/secretsmanager_client",
"crates/share_permission_db_utils",
+ "crates/sqs_worker",
"crates/sync_service",
"crates/sync_service_client",
"crates/system_properties",
@@ -4264,6 +4307,7 @@
"crates/channel_sender",
"crates/channels",
"crates/chat",
+ "crates/comms_db_client",
"crates/connection",
"crates/connection_gateway_client",
"crates/connection_gateway_models",
@@ -4373,6 +4417,7 @@
"crates/channel_sender",
"crates/channels",
"crates/chat",
+ "crates/comms_db_client",
"crates/connection",
"crates/connection_gateway_client",
"crates/connection_gateway_models",
@@ -4619,6 +4664,7 @@
"crates/channel_sender",
"crates/channels",
"crates/chat",
+ "crates/comms_db_client",
"crates/connection",
"crates/connection_gateway_client",
"crates/connection_gateway_models",
@@ -5374,6 +5420,7 @@
"crates/channel_sender",
"crates/channels",
"crates/chat",
+ "crates/comms_db_client",
"crates/connection",
"crates/connection_gateway_client",
"crates/connection_gateway_models",
@@ -5513,6 +5560,7 @@
"crates/channel_sender",
"crates/channels",
"crates/chat",
+ "crates/comms_db_client",
"crates/connection",
"crates/connection_gateway_client",
"crates/connection_gateway_models",
@@ -6455,9 +6503,12 @@
"crates/bot_token",
"crates/broadcast",
"crates/channel_sender",
+ "crates/channels",
+ "crates/comms_db_client",
"crates/connection",
"crates/connection_gateway_client",
"crates/connection_gateway_models",
+ "crates/contacts",
"crates/cowlike",
"crates/crm",
"crates/database_env_vars",
@@ -6522,6 +6573,7 @@
"crates/secretsmanager_client",
"crates/share_permission_db_utils",
"crates/sqs_client",
+ "crates/sqs_worker",
"crates/sync_service",
"crates/sync_service_client",
"crates/system_properties",
@@ -6778,6 +6830,7 @@
"crates/channel_sender",
"crates/channels",
"crates/chat",
+ "crates/comms_db_client",
"crates/connection",
"crates/connection_gateway_client",
"crates/connection_gateway_models",
@@ -7707,6 +7760,7 @@
"crates/channel_sender",
"crates/channels",
"crates/chat",
+ "crates/comms_db_client",
"crates/connection",
"crates/connection_gateway_client",
"crates/connection_gateway_models",
@@ -8368,6 +8422,7 @@
"crates/broadcast",
"crates/channel_sender",
"crates/channels",
+ "crates/comms_db_client",
"crates/connection",
"crates/connection_gateway_client",
"crates/connection_gateway_models",
diff --git a/.sqlx/query-17b62498d6b51217c7d86088a9be84dcca913396bfed619579b89879690c0cc4.json b/.sqlx/query-235e8cd97b37c00ee58d9eb468ec4fba328d2e134496915eeb6e2fc1f420eec3.json
similarity index 55%
rename from .sqlx/query-17b62498d6b51217c7d86088a9be84dcca913396bfed619579b89879690c0cc4.json
rename to .sqlx/query-235e8cd97b37c00ee58d9eb468ec4fba328d2e134496915eeb6e2fc1f420eec3.json
index 35df7bd9f0..bc70fcf763 100644
--- a/.sqlx/query-17b62498d6b51217c7d86088a9be84dcca913396bfed619579b89879690c0cc4.json
+++ b/.sqlx/query-235e8cd97b37c00ee58d9eb468ec4fba328d2e134496915eeb6e2fc1f420eec3.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT\n em.source_entity_type,\n em.source_entity_id,\n em.entity_type,\n em.entity_id,\n em.user_id,\n em.created_at\n FROM comms_entity_mentions em\n WHERE em.entity_type = ANY($1)\n AND em.entity_id = $2\n AND em.source_entity_type != 'message'\n ORDER BY em.created_at DESC\n ",
+ "query": "\n SELECT DISTINCT ON (em.source_entity_type, em.source_entity_id)\n em.source_entity_type,\n em.source_entity_id,\n em.entity_type,\n em.entity_id,\n em.user_id,\n em.created_at\n FROM comms_entity_mentions em\n WHERE em.entity_type = ANY($1)\n AND em.entity_id = $2\n AND em.source_entity_type != 'message'\n ORDER BY em.source_entity_type, em.source_entity_id, em.created_at DESC\n ",
"describe": {
"columns": [
{
@@ -49,5 +49,5 @@
false
]
},
- "hash": "17b62498d6b51217c7d86088a9be84dcca913396bfed619579b89879690c0cc4"
+ "hash": "235e8cd97b37c00ee58d9eb468ec4fba328d2e134496915eeb6e2fc1f420eec3"
}
diff --git a/.sqlx/query-8e79c2b1ccfeacc66703adc9e342120cb8bf19ff928b35f90735924e2e2d801f.json b/.sqlx/query-8e79c2b1ccfeacc66703adc9e342120cb8bf19ff928b35f90735924e2e2d801f.json
new file mode 100644
index 0000000000..5321e91506
--- /dev/null
+++ b/.sqlx/query-8e79c2b1ccfeacc66703adc9e342120cb8bf19ff928b35f90735924e2e2d801f.json
@@ -0,0 +1,19 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO comms_entity_mentions (id, source_entity_type, source_entity_id, entity_type, entity_id, user_id)\n SELECT * FROM UNNEST($1::uuid[], $2::varchar[], $3::varchar[], $4::varchar[], $5::varchar[], $6::varchar[])\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "UuidArray",
+ "VarcharArray",
+ "VarcharArray",
+ "VarcharArray",
+ "VarcharArray",
+ "VarcharArray"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "8e79c2b1ccfeacc66703adc9e342120cb8bf19ff928b35f90735924e2e2d801f"
+}
diff --git a/Cargo.lock b/Cargo.lock
index f2913e6cf6..137d256e29 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -5498,6 +5498,7 @@ dependencies = [
"chrono",
"clap",
"cloudfront_sign",
+ "comms_db_client",
"connection",
"document_sub_type",
"dotenvy",
diff --git a/crates/channels/src/outbound/pg_channels_repo.rs b/crates/channels/src/outbound/pg_channels_repo.rs
index d64ac5c2ca..4ecf0656ac 100644
--- a/crates/channels/src/outbound/pg_channels_repo.rs
+++ b/crates/channels/src/outbound/pg_channels_repo.rs
@@ -2387,7 +2387,7 @@ impl ChannelRepo for PgChannelsRepo {
let generic_references_fut = async {
sqlx::query!(
r#"
- SELECT
+ SELECT DISTINCT ON (em.source_entity_type, em.source_entity_id)
em.source_entity_type,
em.source_entity_id,
em.entity_type,
@@ -2398,7 +2398,7 @@ impl ChannelRepo for PgChannelsRepo {
WHERE em.entity_type = ANY($1)
AND em.entity_id = $2
AND em.source_entity_type != 'message'
- ORDER BY em.created_at DESC
+ ORDER BY em.source_entity_type, em.source_entity_id, em.created_at DESC
"#,
&entity_types,
entity_id,
diff --git a/crates/channels/src/outbound/pg_channels_repo/tests.rs b/crates/channels/src/outbound/pg_channels_repo/tests.rs
index cceb36f777..8fae2acf16 100644
--- a/crates/channels/src/outbound/pg_channels_repo/tests.rs
+++ b/crates/channels/src/outbound/pg_channels_repo/tests.rs
@@ -3048,6 +3048,76 @@ async fn attachment_references_returns_generic_reference(
Ok(())
}
+#[sqlx::test(
+ fixtures(path = "../../../fixtures", scripts("channels_repo")),
+ migrator = "MACRO_DB_MIGRATIONS"
+)]
+async fn attachment_references_collapse_repeat_mentions_from_one_source(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ // src-doc already mentions doc-generic at 2024-01-03; a second mention of the
+ // same pair must not surface as a second reference.
+ sqlx::query!(
+ r#"
+ INSERT INTO comms_entity_mentions
+ (id, source_entity_type, source_entity_id, entity_type, entity_id, user_id, created_at)
+ VALUES
+ ('00000000-0000-0000-0000-00000000e0a1'::uuid, 'doc', 'src-doc',
+ 'document', 'doc-generic', 'macro|[email protected]', '2024-01-05 00:00:00+00')
+ "#,
+ )
+ .execute(&pool)
+ .await?;
+
+ let refs = repo(pool)
+ .get_attachment_references("document", "doc-generic", NON_MEMBER)
+ .await?;
+
+ assert_eq!(refs.len(), 1);
+ let AttachmentEntityReference::Generic(generic) = &refs[0] else {
+ anyhow::bail!("expected a generic reference");
+ };
+ assert_eq!(generic.source_entity_id, "src-doc");
+ assert_eq!(generic.created_at.to_rfc3339(), "2024-01-05T00:00:00+00:00");
+ Ok(())
+}
+
+#[sqlx::test(
+ fixtures(path = "../../../fixtures", scripts("channels_repo")),
+ migrator = "MACRO_DB_MIGRATIONS"
+)]
+async fn attachment_references_collapse_alias_typed_mentions_from_one_source(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ // The same email thread recorded once as `thread` and once as `email` — both
+ // match the thread lookup aliases, and both come from one source.
+ sqlx::query!(
+ r#"
+ INSERT INTO comms_entity_mentions
+ (id, source_entity_type, source_entity_id, entity_type, entity_id, user_id, created_at)
+ VALUES
+ ('00000000-0000-0000-0000-00000000e0b1'::uuid, 'document', 'task-1',
+ 'thread', 'thread-9', 'macro|[email protected]', '2024-01-05 00:00:00+00'),
+ ('00000000-0000-0000-0000-00000000e0b2'::uuid, 'document', 'task-1',
+ 'email', 'thread-9', 'macro|[email protected]', '2024-01-06 00:00:00+00')
+ "#,
+ )
+ .execute(&pool)
+ .await?;
+
+ let refs = repo(pool)
+ .get_attachment_references("email", "thread-9", NON_MEMBER)
+ .await?;
+
+ assert_eq!(refs.len(), 1);
+ let AttachmentEntityReference::Generic(generic) = &refs[0] else {
+ anyhow::bail!("expected a generic reference");
+ };
+ assert_eq!(generic.source_entity_id, "task-1");
+ assert_eq!(generic.created_at.to_rfc3339(), "2024-01-06T00:00:00+00:00");
+ Ok(())
+}
+
#[sqlx::test(
fixtures(path = "../../../fixtures", scripts("channels_repo")),
migrator = "MACRO_DB_MIGRATIONS"
diff --git a/crates/comms_db_client/src/entity_mentions/create_entity_mentions.rs b/crates/comms_db_client/src/entity_mentions/create_entity_mentions.rs
new file mode 100644
index 0000000000..296a7b745d
--- /dev/null
+++ b/crates/comms_db_client/src/entity_mentions/create_entity_mentions.rs
@@ -0,0 +1,60 @@
+#[cfg(test)]
+mod test;
+
+use sqlx::{Executor, Postgres};
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct NewEntityMention {
+ pub entity_type: String,
+ pub entity_id: String,
+}
+
+#[tracing::instrument(skip(executor, mentions), fields(mention_count = mentions.len()))]
+pub async fn create_entity_mentions<'e, E>(
+ executor: E,
+ source_entity_type: &str,
+ source_entity_id: &str,
+ user_id: Option<&str>,
+ mentions: &[NewEntityMention],
+) -> anyhow::Result<u64>
+where
+ E: Executor<'e, Database = Postgres>,
+{
+ if mentions.is_empty() {
+ return Ok(0);
+ }
+
+ let ids: Vec<uuid::Uuid> = mentions
+ .iter()
+ .map(|_| macro_uuid::generate_uuid_v7())
+ .collect();
+ let entity_types: Vec<String> = mentions
+ .iter()
+ .map(|mention| mention.entity_type.clone())
+ .collect();
+ let entity_ids: Vec<String> = mentions
+ .iter()
+ .map(|mention| mention.entity_id.clone())
+ .collect();
+
+ let source_entity_types: Vec<String> = vec![source_entity_type.to_string(); mentions.len()];
+ let source_entity_ids: Vec<String> = vec![source_entity_id.to_string(); mentions.len()];
+ let user_ids: Vec<Option<String>> = vec![user_id.map(str::to_string); mentions.len()];
+
+ let result = sqlx::query!(
+ r#"
+ INSERT INTO comms_entity_mentions (id, source_entity_type, source_entity_id, entity_type, entity_id, user_id)
+ SELECT * FROM UNNEST($1::uuid[], $2::varchar[], $3::varchar[], $4::varchar[], $5::varchar[], $6::varchar[])
+ "#,
+ &ids,
+ &source_entity_types,
+ &source_entity_ids,
+ &entity_types,
+ &entity_ids,
+ &user_ids as &[Option<String>],
+ )
+ .execute(executor)
+ .await?;
+
+ Ok(result.rows_affected())
+}
diff --git a/crates/comms_db_client/src/entity_mentions/create_entity_mentions/test.rs b/crates/comms_db_client/src/entity_mentions/create_entity_mentions/test.rs
new file mode 100644
index 0000000000..0638237d88
--- /dev/null
+++ b/crates/comms_db_client/src/entity_mentions/create_entity_mentions/test.rs
@@ -0,0 +1,67 @@
+use super::*;
+use macro_db_migrator::MACRO_DB_MIGRATIONS;
+use sqlx::Pool;
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn records_mentions_against_their_source_document(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ const _: &sqlx::migrate::Migrator = &MACRO_DB_MIGRATIONS; // Dummy reference for IDE
+ let mentions = vec![
+ NewEntityMention {
+ entity_type: "thread".to_string(),
+ entity_id: "thread-7".to_string(),
+ },
+ NewEntityMention {
+ entity_type: "document".to_string(),
+ entity_id: "doc-9".to_string(),
+ },
+ ];
+
+ let inserted = create_entity_mentions(
+ &pool,
+ "document",
+ "task-1",
+ Some("macro|[email protected]"),
+ &mentions,
+ )
+ .await?;
+ assert_eq!(inserted, 2);
+
+ let rows = sqlx::query!(
+ r#"
+ SELECT source_entity_type, source_entity_id, entity_type, entity_id, user_id
+ FROM comms_entity_mentions
+ ORDER BY entity_id
+ "#,
+ )
+ .fetch_all(&pool)
+ .await?;
+
+ assert_eq!(rows.len(), 2);
+ assert_eq!(rows[0].entity_type, "document");
+ assert_eq!(rows[0].entity_id, "doc-9");
+ assert_eq!(rows[1].entity_type, "thread");
+ assert_eq!(rows[1].entity_id, "thread-7");
+ for row in &rows {
+ assert_eq!(row.source_entity_type, "document");
+ assert_eq!(row.source_entity_id, "task-1");
+ assert_eq!(row.user_id.as_deref(), Some("macro|[email protected]"));
+ }
+
+ Ok(())
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn recording_nothing_writes_nothing(pool: Pool<Postgres>) -> anyhow::Result<()> {
+ const _: &sqlx::migrate::Migrator = &MACRO_DB_MIGRATIONS; // Dummy reference for IDE
+ let inserted = create_entity_mentions(&pool, "document", "task-1", None, &[]).await?;
+ assert_eq!(inserted, 0);
+
+ let count = sqlx::query_scalar!(r#"SELECT COUNT(*) FROM comms_entity_mentions"#)
+ .fetch_one(&pool)
+ .await?;
+ assert_eq!(count, Some(0));
+
+ Ok(())
+}
diff --git a/crates/comms_db_client/src/entity_mentions/mod.rs b/crates/comms_db_client/src/entity_mentions/mod.rs
index a721cd068c..99a58fde66 100644
--- a/crates/comms_db_client/src/entity_mentions/mod.rs
+++ b/crates/comms_db_client/src/entity_mentions/mod.rs
@@ -1,3 +1,5 @@
+pub mod create_entity_mentions;
pub mod delete_entity_mentions_by_source;
+pub use create_entity_mentions::{NewEntityMention, create_entity_mentions};
pub use delete_entity_mentions_by_source::delete_entity_mentions_by_source;
diff --git a/crates/documents/Cargo.toml b/crates/documents/Cargo.toml
index 9e0243ca14..ce3c9b6d57 100644
--- a/crates/documents/Cargo.toml
+++ b/crates/documents/Cargo.toml
@@ -54,7 +54,13 @@ backfill_binary = [
]
default = ["ai_tools", "axum", "inbound", "outbound", "ports", "service"]
document_create = ["dep:base64", "dep:sha2", "ports"]
-document_create_adapters = ["dep:reqwest", "document_create", "markdown_init"]
+document_create_adapters = [
+ "dep:comms_db_client",
+ "dep:reqwest",
+ "dep:sqlx",
+ "document_create",
+ "markdown_init",
+]
inbound = ["axum", "document_create_adapters", "ports"]
markdown_init = ["dep:lexical_client"]
outbound = [
@@ -134,6 +140,7 @@ utoipa = { workspace = true, optional = true }
aws-sdk-s3 = { workspace = true, optional = true }
base64 = { workspace = true, optional = true }
cloudfront_sign = { workspace = true }
+comms_db_client = { path = "../comms_db_client", optional = true }
entity_access_db_utils = { path = "../entity_access_db_utils", optional = true }
futures = { workspace = true }
share_permission_db_utils = { path = "../share_permission_db_utils", optional = true }
diff --git a/crates/documents/src/domain/create.rs b/crates/documents/src/domain/create.rs
index f3b78470d0..3c51cfab7d 100644
--- a/crates/documents/src/domain/create.rs
+++ b/crates/documents/src/domain/create.rs
@@ -5,6 +5,9 @@
//! call sites should choose the lifecycle they need (`create_markdown_text` or
//! `create_text_file`).
+#[cfg(test)]
+mod test;
+
use activity::Attribution;
use anyhow::Context;
use base64::Engine;
@@ -20,6 +23,7 @@ use crate::domain::ports::create::{
DocumentBytesUpload, DocumentBytesUploadPort, DocumentCreationService,
};
use crate::domain::ports::markdown::MarkdownInitializationPort;
+use crate::domain::ports::mentions::DocumentMentionTrackingPort;
use crate::domain::response::CreateDocumentResponseData;
/// Common metadata for a document that has not been created yet.
@@ -465,32 +469,39 @@ impl CreatedDocument {
/// Service for creating backend-owned document content.
#[derive(Clone)]
-pub struct DocumentCreator<Svc, MarkdownInit, BytesUpload> {
+pub struct DocumentCreator<Svc, MarkdownInit, BytesUpload, MentionTracker> {
document_service: Svc,
markdown_initializer: MarkdownInit,
bytes_uploader: BytesUpload,
+ mention_tracker: MentionTracker,
}
-impl<Svc, MarkdownInit, BytesUpload> DocumentCreator<Svc, MarkdownInit, BytesUpload> {
+impl<Svc, MarkdownInit, BytesUpload, MentionTracker>
+ DocumentCreator<Svc, MarkdownInit, BytesUpload, MentionTracker>
+{
/// Construct a document creator.
pub fn new(
document_service: Svc,
markdown_initializer: MarkdownInit,
bytes_uploader: BytesUpload,
+ mention_tracker: MentionTracker,
) -> Self {
Self {
document_service,
markdown_initializer,
bytes_uploader,
+ mention_tracker,
}
}
}
-impl<Svc, MarkdownInit, BytesUpload> DocumentCreator<Svc, MarkdownInit, BytesUpload>
+impl<Svc, MarkdownInit, BytesUpload, MentionTracker>
+ DocumentCreator<Svc, MarkdownInit, BytesUpload, MentionTracker>
where
Svc: DocumentCreationService,
MarkdownInit: MarkdownInitializationPort,
BytesUpload: DocumentBytesUploadPort,
+ MentionTracker: DocumentMentionTrackingPort,
{
/// Create a plaintext document using the lifecycle implied by its file type.
#[tracing::instrument(skip(self, document), err)]
@@ -576,6 +587,7 @@ where
},
);
let attribution = args.resolved_attribution();
+ let mention_user_id = user_id.clone();
let mut response = self
.document_service
@@ -632,6 +644,14 @@ where
}
};
+ if let Err(error) = self
+ .mention_tracker
+ .track_document_mentions(&document_id, &mention_user_id, &markdown)
+ .await
+ {
+ tracing::error!(error=?error, document_id=%document_id, "unable to track document mentions");
+ }
+
response.document_response.document_metadata.content =
DocumentContent::ready(DocumentContentLocation::SyncService);
diff --git a/crates/documents/src/domain/create/test.rs b/crates/documents/src/domain/create/test.rs
new file mode 100644
index 0000000000..1512e0bcba
--- /dev/null
+++ b/crates/documents/src/domain/create/test.rs
@@ -0,0 +1,205 @@
+use std::sync::Mutex;
+
+use activity::Attribution;
+use macro_user_id::user_id::MacroUserIdStr;
+
+use super::{DocumentCreator, MarkdownSubtype, NewDocumentMetadata, NewMarkdownTextDocument};
+use crate::domain::content::DocumentContent;
+use crate::domain::models::{CreateDocumentRepoArgs, CreateTaskRequest, DocumentError};
+use crate::domain::ports::create::{
+ DocumentBytesUpload, DocumentBytesUploadPort, DocumentCreationService,
+};
+use crate::domain::ports::markdown::MarkdownInitializationPort;
+use crate::domain::ports::mentions::DocumentMentionTrackingPort;
+use crate::domain::response::{
+ CreateDocumentResponseData, DocumentResponse, DocumentResponseMetadataWithContent,
+};
+use model::document::response::DocumentResponseMetadata;
+
+const DOCUMENT_ID: &str = "created-task";
+const EMAIL_SEED: &str = r#"<m-document-mention>{"documentId":"thread-7","blockName":"email","documentName":"Re: invoice"}</m-document-mention>"#;
+
+fn owner() -> MacroUserIdStr<'static> {
+ MacroUserIdStr::try_from("macro|[email protected]".to_string()).unwrap()
+}
+
+struct FakeCreationService;
+
+impl DocumentCreationService for FakeCreationService {
+ async fn create_document(
+ &self,
+ user_id: MacroUserIdStr<'static>,
+ _args: CreateDocumentRepoArgs,
+ _job_id: Option<String>,
+ ) -> Result<CreateDocumentResponseData, DocumentError> {
+ Ok(CreateDocumentResponseData {
+ document_response: DocumentResponse {
+ document_metadata: DocumentResponseMetadataWithContent::new(
+ DocumentResponseMetadata {
+ document_id: DOCUMENT_ID.to_string(),
+ document_version_id: 1,
+ owner: user_id,
+ document_name: "task".to_string(),
+ file_type: Some("md".to_string()),
+ sha: None,
+ branched_from_id: None,
+ branched_from_version_id: None,
+ document_family_id: None,
+ document_bom: None,
+ modification_data: None,
+ created_at: None,
+ updated_at: None,
+ sub_type: None,
+ },
+ DocumentContent::pending(),
+ ),
+ presigned_url: None,
+ },
+ content_type: "text/markdown".to_string(),
+ file_type: Some("md".to_string()),
+ })
+ }
+
+ async fn handle_task_properties(
+ &self,
+ _user_id: MacroUserIdStr<'static>,
+ _document_id: &str,
+ _request: &CreateTaskRequest,
+ _attribution: &Attribution,
+ ) -> Result<(), DocumentError> {
+ Ok(())
+ }
+
+ async fn mark_document_uploaded(&self, _document_id: &str) -> Result<(), DocumentError> {
+ panic!("unexpected mark_document_uploaded call")
+ }
+
+ async fn set_document_content(
+ &self,
+ _document_id: &str,
+ _content: DocumentContent,
+ ) -> Result<(), DocumentError> {
+ Ok(())
+ }
+
+ async fn cleanup_created_document(&self, _document_id: &str) {
+ panic!("unexpected cleanup_created_document call")
+ }
+}
+
+struct FakeMarkdownInitializer;
+
+impl MarkdownInitializationPort for FakeMarkdownInitializer {
+ async fn initialize_existing_markdown(
+ &self,
+ _document_id: &str,
+ _markdown: &str,
+ ) -> Result<Vec<u8>, DocumentError> {
+ Ok(vec![1, 2, 3])
+ }
+}
+
+struct FakeBytesUploader;
+
+impl DocumentBytesUploadPort for FakeBytesUploader {
+ async fn upload_document_bytes(
+ &self,
+ _upload: DocumentBytesUpload,
+ ) -> Result<(), DocumentError> {
+ panic!("unexpected upload_document_bytes call")
+ }
+}
+
+#[derive(Default)]
+struct RecordingMentionTracker {
+ calls: Mutex<Vec<(String, String, String)>>,
+ fail: bool,
+}
+
+impl RecordingMentionTracker {
+ fn failing() -> Self {
+ Self {
+ calls: Mutex::new(Vec::new()),
+ fail: true,
+ }
+ }
+
+ fn calls(&self) -> Vec<(String, String, String)> {
+ self.calls.lock().unwrap().clone()
+ }
+}
+
+impl DocumentMentionTrackingPort for &RecordingMentionTracker {
+ async fn track_document_mentions(
+ &self,
+ document_id: &str,
+ user_id: &MacroUserIdStr<'static>,
+ markdown: &str,
+ ) -> anyhow::Result<()> {
+ self.calls.lock().unwrap().push((
+ document_id.to_string(),
+ user_id.as_ref().to_string(),
+ markdown.to_string(),
+ ));
+ if self.fail {
+ anyhow::bail!("lexical service unavailable");
+ }
+ Ok(())
+ }
+}
+
+fn task_document(markdown: &str) -> NewMarkdownTextDocument {
+ NewMarkdownTextDocument {
+ metadata: NewDocumentMetadata::new("task"),
+ markdown: markdown.to_string(),
+ subtype: MarkdownSubtype::Task {
+ property_values: None,
+ share_with_team: false,
+ team_id: None,
+ },
+ }
+}
+
+#[tokio::test]
+async fn markdown_creation_tracks_the_seeded_mentions_once() {
+ let tracker = RecordingMentionTracker::default();
+ let creator = DocumentCreator::new(
+ FakeCreationService,
+ FakeMarkdownInitializer,
+ FakeBytesUploader,
+ &tracker,
+ );
+
+ creator
+ .create_markdown_text(owner(), task_document(EMAIL_SEED))
+ .await
+ .expect("creation should succeed");
+
+ assert_eq!(
+ tracker.calls(),
+ vec![(
+ DOCUMENT_ID.to_string(),
+ "macro|[email protected]".to_string(),
+ EMAIL_SEED.to_string(),
+ )]
+ );
+}
+
+#[tokio::test]
+async fn mention_tracking_failure_does_not_fail_creation() {
+ let tracker = RecordingMentionTracker::failing();
+ let creator = DocumentCreator::new(
+ FakeCreationService,
+ FakeMarkdownInitializer,
+ FakeBytesUploader,
+ &tracker,
+ );
+
+ let created = creator
+ .create_markdown_text(owner(), task_document(EMAIL_SEED))
+ .await
+ .expect("creation should survive a mention tracking failure");
+
+ assert_eq!(created.initial_snapshot(), Some([1, 2, 3].as_slice()));
+ assert_eq!(tracker.calls().len(), 1);
+}
diff --git a/crates/documents/src/domain/ports.rs b/crates/documents/src/domain/ports.rs
index bb15a8fd13..3029f19bf0 100644
--- a/crates/documents/src/domain/ports.rs
+++ b/crates/documents/src/domain/ports.rs
@@ -6,6 +6,7 @@
pub mod create;
pub mod editing;
pub mod markdown;
+pub mod mentions;
use std::future::Future;
diff --git a/crates/documents/src/domain/ports/mentions.rs b/crates/documents/src/domain/ports/mentions.rs
new file mode 100644
index 0000000000..35eff7288f
--- /dev/null
+++ b/crates/documents/src/domain/ports/mentions.rs
@@ -0,0 +1,33 @@
+//! Port definitions for tracking the mentions embedded in document content.
+
+use std::future::Future;
+
+use macro_user_id::user_id::MacroUserIdStr;
+
+/// Records the entity mentions embedded in a document's markdown, so a
+/// mentioned entity can surface the mentioning document in its references.
+pub trait DocumentMentionTrackingPort: Send + Sync {
+ /// Record every mention embedded in `markdown` as a reference owned by
+ /// `document_id`, attributed to `user_id`.
+ fn track_document_mentions(
+ &self,
+ document_id: &str,
+ user_id: &MacroUserIdStr<'static>,
+ markdown: &str,
+ ) -> impl Future<Output = anyhow::Result<()>> + Send;
+}
+
+/// Mention tracker that records nothing, for callers without a MacroDB pool.
+#[derive(Debug, Clone, Copy, Default)]
+pub struct NoOpDocumentMentionTracker;
+
+impl DocumentMentionTrackingPort for NoOpDocumentMentionTracker {
+ async fn track_document_mentions(
+ &self,
+ _document_id: &str,
+ _user_id: &MacroUserIdStr<'static>,
+ _markdown: &str,
+ ) -> anyhow::Result<()> {
+ Ok(())
+ }
+}
diff --git a/crates/documents/src/inbound/axum_router.rs b/crates/documents/src/inbound/axum_router.rs
index 3a0c8ead22..9c16249841 100644
--- a/crates/documents/src/inbound/axum_router.rs
+++ b/crates/documents/src/inbound/axum_router.rs
@@ -154,6 +154,7 @@ pub type DefaultDocumentCreator<T> = crate::domain::create::DocumentCreator<
Arc<T>,
crate::outbound::markdown_init::LexicalSyncMarkdownInitializer,
crate::outbound::document_bytes_upload::ReqwestDocumentBytesUploader,
+ crate::outbound::mention_tracker::LexicalCommsMentionTracker,
>;
/// Router state containing document router dependencies.
diff --git a/crates/documents/src/inbound/axum_router/tests.rs b/crates/documents/src/inbound/axum_router/tests.rs
index 585099359d..750fb9fbe9 100644
--- a/crates/documents/src/inbound/axum_router/tests.rs
+++ b/crates/documents/src/inbound/axum_router/tests.rs
@@ -61,7 +61,7 @@ use crate::{
},
outbound::{
document_bytes_upload::ReqwestDocumentBytesUploader,
- markdown_init::LexicalSyncMarkdownInitializer,
+ markdown_init::LexicalSyncMarkdownInitializer, mention_tracker::LexicalCommsMentionTracker,
},
};
@@ -840,6 +840,7 @@ fn test_router() -> (
),
),
ReqwestDocumentBytesUploader::default(),
+ LexicalCommsMentionTracker::new(pool.clone(), lexical_client.clone()),
);
let state = DocumentRouterState {
service: document_service.clone(),
diff --git a/crates/documents/src/inbound/toolset.rs b/crates/documents/src/inbound/toolset.rs
index 96a11de71d..e97e9768d3 100644
--- a/crates/documents/src/inbound/toolset.rs
+++ b/crates/documents/src/inbound/toolset.rs
@@ -14,6 +14,7 @@ use crate::{
domain::ports::DocumentService,
domain::ports::create::DocumentCreationService,
domain::ports::editing::EditingWorkerService,
+ domain::ports::mentions::NoOpDocumentMentionTracker,
inbound::toolset::{
create_document::CreateDocument, edit_document::EditDocument, read_content::ReadContent,
read_metadata::ReadMetadata, rename_document::RenameDocument,
@@ -33,8 +34,12 @@ use std::sync::Arc;
use sync_service_client::SyncServiceClient;
/// Default backend-owned document creation use case for document tools.
-pub type DefaultDocumentToolCreator<DSvc> =
- DocumentCreator<Arc<DSvc>, LexicalSyncMarkdownInitializer, ReqwestDocumentBytesUploader>;
+pub type DefaultDocumentToolCreator<DSvc> = DocumentCreator<
+ Arc<DSvc>,
+ LexicalSyncMarkdownInitializer,
+ ReqwestDocumentBytesUploader,
+ NoOpDocumentMentionTracker,
+>;
/// Service context for document AI tools
pub struct DocumentToolContext<
@@ -117,6 +122,7 @@ impl<
sync_service_client.as_ref().clone(),
),
ReqwestDocumentBytesUploader::default(),
+ NoOpDocumentMentionTracker,
);
Self {
diff --git a/crates/documents/src/outbound.rs b/crates/documents/src/outbound.rs
index f765325446..2b77635b99 100644
--- a/crates/documents/src/outbound.rs
+++ b/crates/documents/src/outbound.rs
@@ -6,6 +6,8 @@ pub mod document_bytes_upload;
pub mod editing_worker_client;
#[cfg(feature = "markdown_init")]
pub mod markdown_init;
+#[cfg(feature = "document_create_adapters")]
+pub mod mention_tracker;
#[cfg(feature = "outbound")]
pub mod pg_document_repo;
#[cfg(feature = "outbound")]
diff --git a/crates/documents/src/outbound/mention_tracker.rs b/crates/documents/src/outbound/mention_tracker.rs
new file mode 100644
index 0000000000..1481064b1b
--- /dev/null
+++ b/crates/documents/src/outbound/mention_tracker.rs
@@ -0,0 +1,63 @@
+//! Outbound adapter for recording the mentions embedded in document content.
+
+use lexical_client::LexicalClient;
+use macro_user_id::user_id::MacroUserIdStr;
+use sqlx::PgPool;
+use std::sync::Arc;
+
+use crate::domain::ports::mentions::DocumentMentionTrackingPort;
+
+/// Mention tracker backed by lexical-service and MacroDB.
+#[derive(Clone)]
+pub struct LexicalCommsMentionTracker {
+ db: PgPool,
+ lexical: Arc<LexicalClient>,
+}
+
+impl LexicalCommsMentionTracker {
+ /// Construct a tracker writing to `db` and parsing through `lexical`.
+ pub fn new(db: PgPool, lexical: Arc<LexicalClient>) -> Self {
+ Self { db, lexical }
+ }
+}
+
+impl DocumentMentionTrackingPort for LexicalCommsMentionTracker {
+ #[tracing::instrument(skip(self, markdown), fields(document.id = %document_id))]
+ async fn track_document_mentions(
+ &self,
+ document_id: &str,
+ user_id: &MacroUserIdStr<'static>,
+ markdown: &str,
+ ) -> anyhow::Result<()> {
+ if markdown.trim().is_empty() {
+ return Ok(());
+ }
+
+ let mentions = self.lexical.extract_mentions(markdown).await?;
+ if mentions.is_empty() {
+ return Ok(());
+ }
+
+ let mentions: Vec<_> = mentions
+ .into_iter()
+ .map(
+ |mention| comms_db_client::entity_mentions::NewEntityMention {
+ entity_type: mention.entity_type,
+ entity_id: mention.entity_id,
+ },
+ )
+ .collect();
+
+ let inserted = comms_db_client::entity_mentions::create_entity_mentions(
+ &self.db,
+ "document",
+ document_id,
+ Some(user_id.as_ref()),
+ &mentions,
+ )
+ .await?;
+ tracing::debug!(mention_count = inserted, "recorded document mentions");
+
+ Ok(())
+ }
+}
diff --git a/services/document_storage_service/src/main.rs b/services/document_storage_service/src/main.rs
index 35184084d7..43b93a1599 100644
--- a/services/document_storage_service/src/main.rs
+++ b/services/document_storage_service/src/main.rs
@@ -1405,6 +1405,10 @@ async fn run() -> anyhow::Result<()> {
document_service,
markdown_initializer,
documents_hex::outbound::document_bytes_upload::ReqwestDocumentBytesUploader::default(),
+ documents_hex::outbound::mention_tracker::LexicalCommsMentionTracker::new(
+ db.clone(),
+ lexical_client.clone(),
+ ),
),
document_permission_jwt_secret: config.document_permission_jwt.as_ref().to_string(),
},