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
better key for ai projection (#4240)
Will Hutchinson14d42961ab9a
diff --git a/rust/cloud-storage/.sqlx/query-68ad2106694a83d947a19b96aeccbc867cd7b85ed4a40283019b2bd116c4a1e8.json b/rust/cloud-storage/.sqlx/query-68ad2106694a83d947a19b96aeccbc867cd7b85ed4a40283019b2bd116c4a1e8.jsonnew file mode 100644index 0000000000..c7a867c82d--- /dev/null+++ b/rust/cloud-storage/.sqlx/query-68ad2106694a83d947a19b96aeccbc867cd7b85ed4a40283019b2bd116c4a1e8.json@@ -0,0 +1,65 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT ai_projection_id, target_id, prompt_hash, status,\n result, error, generated_at, stale_at\n FROM user_ai_projection\n WHERE ai_projection_id = $1 AND target_id = $2\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "ai_projection_id",+ "type_info": "Text"+ },+ {+ "ordinal": 1,+ "name": "target_id",+ "type_info": "Text"+ },+ {+ "ordinal": 2,+ "name": "prompt_hash",+ "type_info": "Text"+ },+ {+ "ordinal": 3,+ "name": "status",+ "type_info": "Text"+ },+ {+ "ordinal": 4,+ "name": "result",+ "type_info": "Text"+ },+ {+ "ordinal": 5,+ "name": "error",+ "type_info": "Text"+ },+ {+ "ordinal": 6,+ "name": "generated_at",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 7,+ "name": "stale_at",+ "type_info": "Timestamptz"+ }+ ],+ "parameters": {+ "Left": [+ "Text",+ "Text"+ ]+ },+ "nullable": [+ false,+ false,+ false,+ false,+ true,+ true,+ true,+ true+ ]+ },+ "hash": "68ad2106694a83d947a19b96aeccbc867cd7b85ed4a40283019b2bd116c4a1e8"+}diff --git a/rust/cloud-storage/.sqlx/query-f3d1ff560e0f56fbaa9d7dfc7c0b6c5db474bfc4d596e29c5618abc5307e1e52.json b/rust/cloud-storage/.sqlx/query-f3d1ff560e0f56fbaa9d7dfc7c0b6c5db474bfc4d596e29c5618abc5307e1e52.jsonnew file mode 100644index 0000000000..e63dd7cc6f--- /dev/null+++ b/rust/cloud-storage/.sqlx/query-f3d1ff560e0f56fbaa9d7dfc7c0b6c5db474bfc4d596e29c5618abc5307e1e52.json@@ -0,0 +1,17 @@+{+ "db_name": "PostgreSQL",+ "query": "\n INSERT INTO user_ai_projection (ai_projection_id, target_id, prompt_hash, status)\n VALUES ($1, $2, $3, $4)\n ON CONFLICT (target_id, ai_projection_id) DO NOTHING\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Text",+ "Text",+ "Text"+ ]+ },+ "nullable": []+ },+ "hash": "f3d1ff560e0f56fbaa9d7dfc7c0b6c5db474bfc4d596e29c5618abc5307e1e52"+}diff --git a/rust/cloud-storage/Cargo.lock b/rust/cloud-storage/Cargo.lockindex 5403ab3fcf..102cb69c5d 100644--- a/rust/cloud-storage/Cargo.lock+++ b/rust/cloud-storage/Cargo.lock@@ -104,7 +104,6 @@ dependencies = ["http-body-util","macro_db_migrator","macro_user_id",- "macro_uuid","model-error-response","model_user","serde",diff --git a/rust/cloud-storage/ai_projections/Cargo.toml b/rust/cloud-storage/ai_projections/Cargo.tomlindex 054f1b4429..d556dc40cd 100644--- a/rust/cloud-storage/ai_projections/Cargo.toml+++ b/rust/cloud-storage/ai_projections/Cargo.toml@@ -14,7 +14,6 @@ axum = [default = ["axum", "inbound", "outbound", "ports"]inbound = ["axum", "ports"]outbound = [- "dep:macro_uuid","dep:sqlx","ports",]@@ -42,7 +41,6 @@ model_user = { path = "../model_user", optional = true, features = ["axum"] }utoipa = { workspace = true, optional = true }# Outbound dependencies-macro_uuid = { path = "../macro_uuid", optional = true }sqlx = { workspace = true, optional = true }[dev-dependencies]diff --git a/rust/cloud-storage/ai_projections/src/domain/ai_projection_service/test.rs b/rust/cloud-storage/ai_projections/src/domain/ai_projection_service/test.rsindex 63d13a75ce..d79e1ed46e 100644--- a/rust/cloud-storage/ai_projections/src/domain/ai_projection_service/test.rs+++ b/rust/cloud-storage/ai_projections/src/domain/ai_projection_service/test.rs@@ -49,7 +49,6 @@ impl AiProjectionRepository for MockRepo {prompt_hash: &str,) -> Result<UserAiProjection, AiProjectionError> {let target_projection = UserAiProjection {- id: macro_uuid::generate_uuid_v7(),ai_projection_id: ai_projection_id.to_string(),target_id: target_id.to_string(),prompt_hash: prompt_hash.to_string(),@@ -150,7 +149,7 @@ async fn upsert_projection_creates_cold_target_instance_for_user() {#[tokio::test]async fn upsert_projection_resolves_team_target_from_user() {- let team_id = macro_uuid::generate_uuid_v7();+ let team_id = uuid::Uuid::new_v4();let repo = MockRepo {team_ids: vec![team_id],..Default::default()@@ -195,10 +194,7 @@ async fn upsert_projection_team_target_errors_without_exactly_one_team() {// Multiple teams -> ambiguous bad request.let service = AiProjectionServiceImpl::new(MockRepo {- team_ids: vec![- macro_uuid::generate_uuid_v7(),- macro_uuid::generate_uuid_v7(),- ],+ team_ids: vec![uuid::Uuid::new_v4(), uuid::Uuid::new_v4()],..Default::default()});let err = servicediff --git a/rust/cloud-storage/ai_projections/src/domain/model.rs b/rust/cloud-storage/ai_projections/src/domain/model.rsindex bbc8a29c46..1a139005b9 100644--- a/rust/cloud-storage/ai_projections/src/domain/model.rs+++ b/rust/cloud-storage/ai_projections/src/domain/model.rs@@ -199,11 +199,10 @@ pub struct AiProjection {pub updated_at: DateTime<Utc>,}-/// A per-user cached instance of an [`AiProjection`].+/// A per-target cached instance of an [`AiProjection`], identified by its+/// `(target_id, ai_projection_id)` pair.#[derive(Debug, Clone, PartialEq, Eq)]pub struct UserAiProjection {- /// The surrogate id of the instance.- pub id: uuid::Uuid,/// The id of the projection definition this instance materializes.pub ai_projection_id: String,/// The target this instance belongs to (a user id or a team id, per thediff --git a/rust/cloud-storage/ai_projections/src/inbound/axum_router/test.rs b/rust/cloud-storage/ai_projections/src/inbound/axum_router/test.rsindex d63c551e10..816e78cb48 100644--- a/rust/cloud-storage/ai_projections/src/inbound/axum_router/test.rs+++ b/rust/cloud-storage/ai_projections/src/inbound/axum_router/test.rs@@ -29,7 +29,6 @@ impl AiProjectionService for MockService {params: UpsertProjectionParams,) -> Result<UserAiProjection, UpsertProjectionError> {Ok(UserAiProjection {- id: macro_uuid::generate_uuid_v7(),ai_projection_id: params.id,target_id: user_id.as_ref().to_string(),prompt_hash: "hash".to_string(),diff --git a/rust/cloud-storage/ai_projections/src/outbound/ai_projection_repo.rs b/rust/cloud-storage/ai_projections/src/outbound/ai_projection_repo.rsindex 3445e20306..844f227f54 100644--- a/rust/cloud-storage/ai_projections/src/outbound/ai_projection_repo.rs+++ b/rust/cloud-storage/ai_projections/src/outbound/ai_projection_repo.rs@@ -90,15 +90,12 @@ impl AiProjectionRepository for AiProjectionRepositoryImpl {target_id: &str,prompt_hash: &str,) -> Result<UserAiProjection, AiProjectionError> {- let id = macro_uuid::generate_uuid_v7();-sqlx::query!(r#"- INSERT INTO user_ai_projection (id, ai_projection_id, target_id, prompt_hash, status)- VALUES ($1, $2, $3, $4, $5)- ON CONFLICT (ai_projection_id, target_id, prompt_hash) DO NOTHING+ INSERT INTO user_ai_projection (ai_projection_id, target_id, prompt_hash, status)+ VALUES ($1, $2, $3, $4)+ ON CONFLICT (target_id, ai_projection_id) DO NOTHING"#,- id,ai_projection_id,target_id,prompt_hash,@@ -110,21 +107,19 @@ impl AiProjectionRepository for AiProjectionRepositoryImpl {let row = sqlx::query!(r#"- SELECT id, ai_projection_id, target_id, prompt_hash, status,+ SELECT ai_projection_id, target_id, prompt_hash, status,result, error, generated_at, stale_atFROM user_ai_projection- WHERE ai_projection_id = $1 AND target_id = $2 AND prompt_hash = $3+ WHERE ai_projection_id = $1 AND target_id = $2"#,ai_projection_id,target_id,- prompt_hash,).fetch_one(&self.pool).await.map_err(sqlx_err)?;Ok(UserAiProjection {- id: row.id,ai_projection_id: row.ai_projection_id,target_id: row.target_id,prompt_hash: row.prompt_hash,diff --git a/rust/cloud-storage/ai_projections/src/outbound/ai_projection_repo/test.rs b/rust/cloud-storage/ai_projections/src/outbound/ai_projection_repo/test.rsindex 8b425e1a3f..eb68d661c5 100644--- a/rust/cloud-storage/ai_projections/src/outbound/ai_projection_repo/test.rs+++ b/rust/cloud-storage/ai_projections/src/outbound/ai_projection_repo/test.rs@@ -72,18 +72,14 @@ async fn get_or_create_target_projection_is_idempotent(pool: Pool<Postgres>) ->.get_or_create_target_projection("inbox/important", user.as_ref(), "hash_v1").await?;- assert_eq!(first.id, second.id);+ // Same (target_id, ai_projection_id) -> the same row is returned, not a new+ // one. The composite primary key guarantees no duplicate can be inserted.+ assert_eq!(first, second);assert_eq!(first.ai_projection_id, "inbox/important");assert_eq!(first.target_id, "macro|[email protected]");assert_eq!(first.status, ProjectionStatus::Cold);assert!(first.result.is_none());- // A new prompt version creates a separate instance.- let other_version = repo- .get_or_create_target_projection("inbox/important", user.as_ref(), "hash_v2")- .await?;- assert_ne!(first.id, other_version.id);-Ok(())}diff --git a/rust/cloud-storage/macro_db_client/migrations/20260622133041_create_ai_projection_tables.sql b/rust/cloud-storage/macro_db_client/migrations/20260622133041_create_ai_projection_tables.sqlindex 5c2b06fc3c..aef1af706a 100644--- a/rust/cloud-storage/macro_db_client/migrations/20260622133041_create_ai_projection_tables.sql+++ b/rust/cloud-storage/macro_db_client/migrations/20260622133041_create_ai_projection_tables.sql@@ -19,10 +19,12 @@ CREATE TABLE ai_projection (-- Per-target cached instances of an AI projection. `target_id` holds either a-- user id or a team id, interpreted via the parent projection's `target_type`.--- Keyed by (ai_projection_id, target_id, prompt_hash) so a projection is--- cached, refreshed, and expired independently per target and prompt version.+-- Keyed by (target_id, ai_projection_id) so a projection is cached, refreshed,+-- and expired independently per target and is directly addressable without a+-- surrogate id. `prompt_hash` records the projection's prompt version at+-- materialization time (the definition's prompt is immutable, so it is not part+-- of the key).CREATE TABLE user_ai_projection (- id UUID NOT NULL,ai_projection_id TEXT NOT NULL REFERENCES ai_projection (id) ON DELETE CASCADE,target_id TEXT NOT NULL,prompt_hash TEXT NOT NULL,@@ -35,11 +37,9 @@ CREATE TABLE user_ai_projection (created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),- CONSTRAINT user_ai_projection_pkey PRIMARY KEY (id),- CONSTRAINT user_ai_projection_cache_key UNIQUE (ai_projection_id, target_id, prompt_hash),+ CONSTRAINT user_ai_projection_pkey PRIMARY KEY (target_id, ai_projection_id),CONSTRAINT user_ai_projection_status_checkCHECK (status IN ('loading', 'cold', 'ready', 'refreshing', 'error')));-CREATE INDEX user_ai_projection_target_idx ON user_ai_projection (target_id);CREATE INDEX user_ai_projection_projection_idx ON user_ai_projection (ai_projection_id);