diff --git a/crates/buzz-cli/src/lib.rs b/crates/buzz-cli/src/lib.rs index 0481fb7927..e523890d25 100644 --- a/crates/buzz-cli/src/lib.rs +++ b/crates/buzz-cli/src/lib.rs @@ -1357,7 +1357,7 @@ pub enum DmsCmd { #[arg(long)] pubkey: String, /// Add them to this DM with its history, instead of starting a new DM - /// of everyone. A person added to a DM of one person and their agents + /// of its people and them (agents stay behind). A person added to a DM of one person and their agents /// always starts a new DM. Agents cannot use this. #[arg(long)] include_history: bool, diff --git a/crates/buzz-db/src/store/dm.rs b/crates/buzz-db/src/store/dm.rs index fa8c62aa9d..6502d3cc6a 100644 --- a/crates/buzz-db/src/store/dm.rs +++ b/crates/buzz-db/src/store/dm.rs @@ -728,15 +728,16 @@ pub async fn grow_dm( }) } -/// Start a DM of everyone in the DM `source` plus `additions`, without its +/// Start a DM of the people in the DM `source` plus `additions`, without its /// history: what adding people to a DM does when they do not join it in place. +/// Agents already in `source` stay behind and can be added later; agents among +/// `additions` join, with `policy`'s permission. /// /// `source` must be a live, unarchived DM with `actor` as an active member. /// Its members are read under its membership lock, held until the new DM is /// made on the same transaction, so the new DM holds exactly who is in -/// `source` now. Every agent in the new DM needs `policy`'s permission, -/// whoever brought it into `source`. If a DM of exactly these members exists, -/// it is returned to `actor` if they are in it, and shown in their sidebar. +/// `source` now. If a DM of exactly these members exists, it is returned to +/// `actor` if they are in it, and shown in their sidebar. /// /// Returns `(channel, was_created, participants)`. pub async fn fork_dm( @@ -754,7 +755,13 @@ pub async fn fork_dm( ) .await?; crate::channel_members::lock_channel_for_member_put(&mut tx, source).await?; - let mut members = locked_dm_members(tx.conn(), community_id, source, actor).await?; + let source_members = locked_dm_members(tx.conn(), community_id, source, actor).await?; + let source_refs: Vec<&[u8]> = source_members.iter().map(Vec::as_slice).collect(); + let agents = agents_among(tx.conn(), community_id, &source_refs).await?; + let mut members: Vec> = source_members + .into_iter() + .filter(|pk| pk.as_slice() == actor || !agents.contains(pk)) + .collect(); for pk in additions { if !members.iter().any(|m| m == pk) { members.push(pk.to_vec()); diff --git a/crates/buzz-relay/src/handlers/command_executor.rs b/crates/buzz-relay/src/handlers/command_executor.rs index 62eeea4064..6bab5ad5ba 100644 --- a/crates/buzz-relay/src/handlers/command_executor.rs +++ b/crates/buzz-relay/src/handlers/command_executor.rs @@ -734,8 +734,9 @@ async fn handle_dm_add_member( let added = grow_with_agents(tenant, state, other, &self_bytes, &agents).await?; (other, false, added, true, Vec::new()) } - // Start a new DM of everyone (no history); this DM is untouched. A DM - // of at most one person never gains people, even with the tag. + // Start a new DM of its people and the additions (no history); this + // DM and its agents stay put. A DM of at most one person never gains + // people, even with the tag. Some(DmGrowth::PeopleStartNewDm) | None => { let (channel, created, participants) = state .db diff --git a/crates/buzz-relay/src/handlers/dm_growth_postgres_tests.rs b/crates/buzz-relay/src/handlers/dm_growth_postgres_tests.rs index 993489cedc..4465207b35 100644 --- a/crates/buzz-relay/src/handlers/dm_growth_postgres_tests.rs +++ b/crates/buzz-relay/src/handlers/dm_growth_postgres_tests.rs @@ -838,7 +838,7 @@ async fn an_agent_joining_a_one_person_dm_moves_its_identity() { } /// A DM of one person and their agents never gains a person in place, even -/// with the history tag: adding one starts a DM of everyone, and adding them +/// with the history tag: adding one starts a DM of its people, and adding them /// again opens that DM. The original keeps its members and its identity. /// Mutation: let the tag grow it in place → the original gains B → RED. #[tokio::test] @@ -857,10 +857,7 @@ async fn a_person_added_to_an_agent_only_dm_starts_a_new_one() { assert!(created); assert_ne!(target, dm); assert_eq!(members(&state, &tenant, dm).await, set_of(&[&a, &agent])); - assert_eq!( - members(&state, &tenant, target).await, - set_of(&[&a, &b, &agent]) - ); + assert_eq!(members(&state, &tenant, target).await, set_of(&[&a, &b])); assert_eq!( add(&state, &tenant, &a, dm, &[&b], true) .await @@ -1080,31 +1077,61 @@ async fn an_agent_joins_an_old_dm_in_place_when_its_people_have_another() { ); } -/// Adding a person to a DM with someone else's agent starts a DM that puts -/// that agent with others, so it needs the agent's add policy, with or without -/// the tag. -/// Mutation: check only the agents being added → the new DM is made → RED. +/// A new DM started from a DM carries its people, not the agents in it, so +/// no agent's add policy can block it. Agents named in the add still join. +/// Mutation: carry the source's agents → X, M and Y are in the new DMs, or the +/// policy refuses them → RED. #[tokio::test] #[ignore = "requires Postgres"] -async fn adding_to_a_dm_with_someone_elses_agent_follows_its_policy() { +async fn a_new_dm_from_a_dm_carries_only_its_people() { let state = state().await; - let tenant = community(&state, "dm-fork-policy").await; - let (a, b, c, agent) = ( + let tenant = community(&state, "dm-fork-people").await; + let (a, b, c, x, m, y) = ( + Keys::generate(), + Keys::generate(), Keys::generate(), Keys::generate(), Keys::generate(), Keys::generate(), ); - make_agent(&state, &tenant, &agent, &b, "nobody").await; - let dm = open(&state, &tenant, &a, &[&agent]).await.expect("open"); + make_agent(&state, &tenant, &x, &b, "owner_only").await; + make_agent(&state, &tenant, &m, &a, "owner_only").await; + make_agent(&state, &tenant, &y, &b, "nobody").await; - for history in [true, false] { - rejected( - add(&state, &tenant, &a, dm, &[&c], history).await, - "policy:nobody", - ); + // A DM of people with someone else's agent: the new DM is its people. + let trio = open(&state, &tenant, &a, &[&b]).await.expect("open"); + add(&state, &tenant, &b, trio, &[&x], true) + .await + .expect("owner adds"); + let (forked, created) = add(&state, &tenant, &a, trio, &[&c], false) + .await + .expect("start new DM"); + assert!(created); + assert_eq!( + members(&state, &tenant, forked).await, + set_of(&[&a, &b, &c]) + ); + assert_eq!(members(&state, &tenant, trio).await, set_of(&[&a, &b, &x])); + + // An agent named in the add joins. + let (named, created) = add(&state, &tenant, &a, trio, &[&c, &m], false) + .await + .expect("start new DM with an agent"); + assert!(created); + assert_eq!( + members(&state, &tenant, named).await, + set_of(&[&a, &b, &c, &m]) + ); + + // A DM of one person and agents, theirs or not: the new DM is people only. + for (agent, history) in [(&m, true), (&y, false)] { + let solo = open(&state, &tenant, &a, &[agent]).await.expect("solo"); + let (target, _) = add(&state, &tenant, &a, solo, &[&c], history) + .await + .expect("add a person"); + assert_ne!(target, solo); + assert_eq!(members(&state, &tenant, target).await, set_of(&[&a, &c])); } - assert_eq!(members(&state, &tenant, dm).await, set_of(&[&a, &agent])); } /// A new DM started from a DM holds who is in that DM when it is made, not diff --git a/crates/buzz-sdk/src/builders.rs b/crates/buzz-sdk/src/builders.rs index 3ede94be9d..0e43fb6105 100644 --- a/crates/buzz-sdk/src/builders.rs +++ b/crates/buzz-sdk/src/builders.rs @@ -2025,8 +2025,9 @@ fn dm_open(pubkeys: &[&str], include_history: bool) -> Result