feat(relay): name the agent whose add policy refuses a DM (#9601)

## Problem

When an agent's add policy refuses a DM open or add (from #9125), the
refusal says only that an agent refused. The person can't tell which
agent blocked it, so they don't know whose owner to ask.

## Change

The DM refusal now ends with `(agent <hex pubkey>)` for the first agent
refused, in the order the request named them. For example:

`forbidden: policy:owner_only — only the agent owner can add this agent
(agent 3f9a…)`

The prefix is unchanged, so existing clients that match on
`policy:owner_only` or `policy:nobody` keep working. Desktop and mobile
follow-ups will show the agent's name.

## Tests

- `adding_an_agent_follows_its_add_policy` asserts the full refusal
including the agent.
- `opening_with_several_agents_follows_their_add_policy` adds a second
refusing agent and checks the first one named is reported.
- DM Postgres tests: 297/297 passed. Clippy is clean.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: morgmart <98432065+morgmart@users.noreply.github.com>
Co-authored-by: Carl <c217fe6b9d958f41c3a5e030dccc7f626775a923089cb6491305eade75ea1f1b@buzz.block.builderlab.xyz>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
morgmart
2026-11-17 11:26:25 -08:00
committed by GitHub
parent 4e8f836576
commit f158fdd9a1
2 changed files with 33 additions and 14 deletions

View File

@@ -129,7 +129,9 @@ async fn agents_among<'e>(
pub type AgentAddPolicy<'a> = &'a (dyn Fn(&str, &[u8]) -> std::result::Result<(), String> + Sync);
/// Refuse unless every agent in `joining`, other than `actor`, may join by
/// `policy`.
/// `policy`. The refusal names the first agent refused, in the order of
/// `joining`, as `(agent <hex pubkey>)` at its end, so clients can say whose
/// setting blocked it.
///
/// Callers run this in the same transaction, and under the same locks, as the
/// write it guards, so it sees exactly who joins.
@@ -149,17 +151,22 @@ async fn check_joining_agents(
return Ok(());
}
let rows = sqlx::query(
"SELECT channel_add_policy::text AS policy, agent_owner_pubkey AS owner FROM users \
WHERE community_id = $1 AND pubkey = ANY($2) AND agent_owner_pubkey IS NOT NULL",
"SELECT pubkey, channel_add_policy::text AS policy, agent_owner_pubkey AS owner \
FROM users \
WHERE community_id = $1 AND pubkey = ANY($2) AND agent_owner_pubkey IS NOT NULL \
ORDER BY array_position($2, pubkey)",
)
.bind(community_id.as_uuid())
.bind(&joining)
.fetch_all(&mut *conn)
.await?;
for row in rows {
let agent: Vec<u8> = row.try_get("pubkey")?;
let agent_policy: String = row.try_get("policy")?;
let owner: Vec<u8> = row.try_get("owner")?;
policy(&agent_policy, &owner).map_err(DbError::AccessDenied)?;
policy(&agent_policy, &owner).map_err(|refusal| {
DbError::AccessDenied(format!("{refusal} (agent {})", hex::encode(&agent)))
})?;
}
Ok(())
}

View File

@@ -383,14 +383,12 @@ async fn adding_an_agent_follows_its_add_policy() {
make_agent(&state, &tenant, &agent, &b, "owner_only").await;
let dm = open(&state, &tenant, &a, &[&b]).await.expect("open");
rejected(
add(&state, &tenant, &a, dm, &[&agent], true).await,
"policy:owner_only",
);
rejected(
open(&state, &tenant, &a, &[&b, &agent]).await,
"policy:owner_only",
let named = format!(
"policy:owner_only — only the agent owner can add this agent (agent {})",
hex(&agent)
);
rejected(add(&state, &tenant, &a, dm, &[&agent], true).await, &named);
rejected(open(&state, &tenant, &a, &[&b, &agent]).await, &named);
assert_eq!(members(&state, &tenant, dm).await, set_of(&[&a, &b]));
let (target, _) = add(&state, &tenant, &b, dm, &[&agent], true)
@@ -785,8 +783,11 @@ async fn kicking_an_agent_keeps_a_group_dm_its_identity() {
}
/// Opening a DM that puts someone else's agent with anyone besides the caller
/// follows its add policy, even when no other person is in it.
/// Mutation: check policies only when people are named → the DM opens → RED.
/// follows its add policy, even when no other person is in it. The refusal
/// names the first agent refused, in the order named, so the caller knows
/// whose setting blocked it.
/// Mutation: check policies only when people are named → the DM opens → RED;
/// leave the agent out of the refusal → RED.
#[tokio::test]
#[ignore = "requires Postgres"]
async fn opening_with_several_agents_follows_their_add_policy() {
@@ -794,13 +795,24 @@ async fn opening_with_several_agents_follows_their_add_policy() {
let tenant = community(&state, "dm-agents-policy").await;
let (a, b) = (Keys::generate(), Keys::generate());
let (theirs, mine) = (Keys::generate(), Keys::generate());
let strict = Keys::generate();
make_agent(&state, &tenant, &theirs, &b, "nobody").await;
make_agent(&state, &tenant, &mine, &a, "owner_only").await;
make_agent(&state, &tenant, &strict, &b, "owner_only").await;
rejected(
open(&state, &tenant, &a, &[&theirs, &mine]).await,
"policy:nobody",
&format!(
"policy:nobody — this agent has disabled external channel additions (agent {})",
hex(&theirs)
),
);
for (first, second) in [(&strict, &theirs), (&theirs, &strict)] {
rejected(
open(&state, &tenant, &a, &[&mine, first, second]).await,
&format!("(agent {})", hex(first)),
);
}
}
/// Adding an agent to a DM of one person and their agent moves its identity