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.

feat(call): only assign voice if user is within team scope (#3247)

Will Hutchinsonaa70062aa135
diff --git a/rust/cloud-storage/.sqlx/query-006ebc0f8730714a1755ea1dbfee6e8c5bdc57566579a482dedb774aeda6a1c2.json b/rust/cloud-storage/.sqlx/query-006ebc0f8730714a1755ea1dbfee6e8c5bdc57566579a482dedb774aeda6a1c2.json
new file mode 100644
index 0000000000..78f1631b26
--- /dev/null
+++ b/rust/cloud-storage/.sqlx/query-006ebc0f8730714a1755ea1dbfee6e8c5bdc57566579a482dedb774aeda6a1c2.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE call_record_transcripts AS t\n SET custom_speaker = u.id\n FROM UNNEST($2::text[], $3::uuid[]) AS a(diarized_speaker_id, macro_user_id)\n JOIN \"User\" u ON u.macro_user_id = a.macro_user_id\n WHERE t.call_record_id = $1\n AND t.diarized_speaker_id = a.diarized_speaker_id\n AND t.custom_speaker IS NULL\n AND EXISTS (\n SELECT 1\n FROM team_user matched_user_team\n JOIN team_user participant_team\n ON participant_team.team_id = matched_user_team.team_id\n JOIN call_record_participants p\n ON p.user_id = participant_team.user_id\n AND p.call_record_id = $1\n WHERE matched_user_team.user_id = u.id\n )\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "TextArray",
+ "UuidArray"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "006ebc0f8730714a1755ea1dbfee6e8c5bdc57566579a482dedb774aeda6a1c2"
+}
diff --git a/rust/cloud-storage/.sqlx/query-b7d24d8fd171b58f03a8c4bab503279140fef9ddccbf7caebc285913adae2e3f.json b/rust/cloud-storage/.sqlx/query-b7d24d8fd171b58f03a8c4bab503279140fef9ddccbf7caebc285913adae2e3f.json
deleted file mode 100644
index 5fd0f3ee4a..0000000000
--- a/rust/cloud-storage/.sqlx/query-b7d24d8fd171b58f03a8c4bab503279140fef9ddccbf7caebc285913adae2e3f.json
+++ /dev/null
@@ -1,16 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n UPDATE call_record_transcripts AS t\n SET custom_speaker = u.id\n FROM UNNEST($2::text[], $3::uuid[]) AS a(diarized_speaker_id, macro_user_id)\n JOIN \"User\" u ON u.macro_user_id = a.macro_user_id\n WHERE t.call_record_id = $1\n AND t.diarized_speaker_id = a.diarized_speaker_id\n AND t.custom_speaker IS NULL\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Uuid",
- "TextArray",
- "UuidArray"
- ]
- },
- "nullable": []
- },
- "hash": "b7d24d8fd171b58f03a8c4bab503279140fef9ddccbf7caebc285913adae2e3f"
-}
diff --git a/rust/cloud-storage/call/src/domain/ports.rs b/rust/cloud-storage/call/src/domain/ports.rs
index f456aa66d6..127a94170e 100644
--- a/rust/cloud-storage/call/src/domain/ports.rs
+++ b/rust/cloud-storage/call/src/domain/ports.rs
@@ -288,10 +288,11 @@ pub trait CallRepository: Send + Sync + 'static {
/// Apply auto-matched speaker assignments to `call_record_transcripts`
/// rows, only where `custom_speaker IS NULL` (so manual overrides are
- /// never clobbered). Each tuple is `(diarized_speaker_id, macro_user_id)`;
- /// the canonical `User.id` is resolved inline via the `User` row whose
- /// `macro_user_id` matches and stored in `custom_speaker`. Empty
- /// `assignments` is a no-op.
+ /// never clobbered) and the matched macro user shares a team with at least
+ /// one call participant. Each tuple is `(diarized_speaker_id,
+ /// macro_user_id)`; the canonical `User.id` is resolved inline via the
+ /// `User` row whose `macro_user_id` matches and stored in
+ /// `custom_speaker`. Empty `assignments` is a no-op.
fn patch_call_transcript_speakers_from_voice_match(
&self,
call_record_id: &Uuid,
diff --git a/rust/cloud-storage/call/src/domain/service.rs b/rust/cloud-storage/call/src/domain/service.rs
index c6b00a38f2..d4db64fc2a 100644
--- a/rust/cloud-storage/call/src/domain/service.rs
+++ b/rust/cloud-storage/call/src/domain/service.rs
@@ -1429,10 +1429,11 @@ async fn enroll_stable_speaker_voices_for_call_record<R: CallRepository, Vr: Voi
///
/// Pulls the distinct `(diarized_speaker_id, voice_id)` pairs from the
/// call's transcripts, resolves each `voice_id` to a macro user via
-/// the voice repository's nearest-neighbour lookup, then writes the
+/// the voice repository's nearest-neighbour lookup, then writes eligible
/// assignments back via [`CallRepository::patch_call_transcript_speakers_from_voice_match`]
-/// (which only fills `custom_speaker` rows that are still `NULL` so
-/// manual corrections are preserved).
+/// (which only fills `custom_speaker` rows that are still `NULL` and whose
+/// matched user shares a team with at least one participant, so manual
+/// corrections are preserved and cross-team matches are ignored).
async fn match_voices_for_call_record<R: CallRepository, Vr: VoiceRepository>(
repo: &R,
voice_repo: &Vr,
diff --git a/rust/cloud-storage/call/src/outbound/pg_call_repo.rs b/rust/cloud-storage/call/src/outbound/pg_call_repo.rs
index 96d8e37e17..278c5ea820 100644
--- a/rust/cloud-storage/call/src/outbound/pg_call_repo.rs
+++ b/rust/cloud-storage/call/src/outbound/pg_call_repo.rs
@@ -1531,6 +1531,16 @@ impl CallRepository for PgCallRepo {
WHERE t.call_record_id = $1
AND t.diarized_speaker_id = a.diarized_speaker_id
AND t.custom_speaker IS NULL
+ AND EXISTS (
+ SELECT 1
+ FROM team_user matched_user_team
+ JOIN team_user participant_team
+ ON participant_team.team_id = matched_user_team.team_id
+ JOIN call_record_participants p
+ ON p.user_id = participant_team.user_id
+ AND p.call_record_id = $1
+ WHERE matched_user_team.user_id = u.id
+ )
"#,
call_record_id,
&diarized_ids as &[&str],
diff --git a/rust/cloud-storage/call/src/outbound/pg_call_repo/test.rs b/rust/cloud-storage/call/src/outbound/pg_call_repo/test.rs
index 9e5105fafe..8233ce2b90 100644
--- a/rust/cloud-storage/call/src/outbound/pg_call_repo/test.rs
+++ b/rust/cloud-storage/call/src/outbound/pg_call_repo/test.rs
@@ -102,6 +102,32 @@ async fn insert_user_mapping(
Ok(())
}
+async fn insert_team_members(
+ pool: &Pool<Postgres>,
+ team_id: Uuid,
+ owner_id: &str,
+ member_ids: &[&str],
+) -> anyhow::Result<()> {
+ sqlx::query(r#"INSERT INTO team (id, name, owner_id) VALUES ($1, $2, $3)"#)
+ .bind(team_id)
+ .bind(format!("test team {team_id}"))
+ .bind(owner_id)
+ .execute(pool)
+ .await?;
+
+ for member_id in member_ids {
+ sqlx::query(
+ r#"INSERT INTO team_user (user_id, team_id, team_role) VALUES ($1, $2, 'member')"#,
+ )
+ .bind(member_id)
+ .bind(team_id)
+ .execute(pool)
+ .await?;
+ }
+
+ Ok(())
+}
+
// -- create_call --------------------------------------------------------------
#[sqlx::test(
@@ -1173,6 +1199,102 @@ async fn get_stable_speaker_voices_for_call_record_returns_all_voices_for_consis
Ok(())
}
+// -- patch_call_transcript_speakers_from_voice_match -------------------------
+
+#[sqlx::test(
+ fixtures(path = "../../../fixtures", scripts("call_repo")),
+ migrator = "MACRO_DB_MIGRATIONS"
+)]
+async fn patch_call_transcript_speakers_from_voice_match_requires_same_team_as_participant(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ let repo = repo(pool.clone());
+ let user_d = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
+ let macro_user_d = Uuid::now_v7();
+
+ insert_user_mapping(&pool, USER_A.deref(), MACRO_USER_A).await?;
+ insert_user_mapping(&pool, USER_C.deref(), MACRO_USER_C).await?;
+ insert_user_mapping(&pool, &user_d, macro_user_d).await?;
+
+ // USER_A is a participant in CALL_ARCHIVED. USER_C is not a participant,
+ // but is in USER_A's team, so C is eligible for auto-assignment.
+ insert_team_members(
+ &pool,
+ Uuid::from_u128(0xaaaaaaaa_aaaa_aaaa_aaaa_aaaaaaaaaa01),
+ USER_A.as_ref(),
+ &[USER_A.as_ref(), USER_C.as_ref()],
+ )
+ .await?;
+
+ // USER_D has an enrolled voice match but belongs to a team with no call
+ // participants, so D must not be auto-assigned.
+ insert_team_members(
+ &pool,
+ Uuid::from_u128(0xdddddddd_dddd_dddd_dddd_dddddddddd01),
+ user_d.as_ref(),
+ &[user_d.as_ref()],
+ )
+ .await?;
+
+ let now = Utc::now();
+ sqlx::query(
+ r#"
+ INSERT INTO call_record_transcripts
+ (call_record_id, segment_id, speaker_id, diarized_speaker_id, custom_speaker, content, started_at, ended_at, sequence_num)
+ VALUES
+ ($1, 'seg-team-match', $2, 'spk-team-match', NULL, 'team match', $3, $3, 10),
+ ($1, 'seg-cross-team-match', $2, 'spk-cross-team-match', NULL, 'cross team match', $3, $3, 11)
+ "#,
+ )
+ .bind(CALL_ARCHIVED)
+ .bind(USER_A.as_ref())
+ .bind(now)
+ .execute(&pool)
+ .await?;
+
+ repo.patch_call_transcript_speakers_from_voice_match(
+ &CALL_ARCHIVED,
+ &[
+ ("spk-team-match".to_string(), MACRO_USER_C),
+ ("spk-cross-team-match".to_string(), macro_user_d),
+ ],
+ )
+ .await?;
+
+ let rows = sqlx::query(
+ r#"
+ SELECT diarized_speaker_id, custom_speaker
+ FROM call_record_transcripts
+ WHERE call_record_id = $1
+ AND diarized_speaker_id IN ('spk-team-match', 'spk-cross-team-match')
+ ORDER BY sequence_num
+ "#,
+ )
+ .bind(CALL_ARCHIVED)
+ .fetch_all(&pool)
+ .await?;
+
+ use sqlx::Row as _;
+ assert_eq!(rows.len(), 2);
+ assert_eq!(
+ rows[0].get::<String, _>("diarized_speaker_id"),
+ "spk-team-match"
+ );
+ assert_eq!(
+ rows[0]
+ .get::<Option<String>, _>("custom_speaker")
+ .as_deref(),
+ Some(USER_C.as_ref())
+ );
+ assert_eq!(
+ rows[1].get::<String, _>("diarized_speaker_id"),
+ "spk-cross-team-match"
+ );
+ assert_eq!(rows[1].get::<Option<String>, _>("custom_speaker"), None);
+
+ Ok(())
+}
+
// -- get_call_record_by_call_id ----------------------------------------------
#[sqlx::test(