Skip to content

Commit 02ccba9

Browse files
loganjLarryclaude
committed
Compute the member/bot label at read time and retire the roster trigger
`bot` has the same permissions as `member`; it only labels a key with a recorded agent owner. Instead of storing that label and converting rows when an owner is recorded, store `member` for both ordinary roles and compute the label wherever a role is read: rosters, member lists, role reads, DM participants and the bot list. An agent owner is recorded once and never changes, so a key's label changes at most once. - Writers store `member` for an ordinary role; reads show `bot` exactly when users.agent_owner_pubkey is set for the key in that community. - The app-side roster fence treats `member` and `bot` as one role, so an owner recorded between roster capture and insert does not refuse the publication. - After recording an owner, a background task lists the agent's active ordinary channels and republishes their rosters. - BUZZ_RECONCILE_BOT_ROSTERS runs a one-shot startup repair of rosters that show a stale label (for example, ones older relays published). Nothing scans at every startup. - Migration 0074 drops the 0032 roster trigger, which compared roster labels with the stored role exactly. Every relay must run #8611 first. Co-authored-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Logan Johnson <[email protected]>
1 parent d835556 commit 02ccba9

17 files changed

Lines changed: 950 additions & 157 deletions

File tree

‎ARCHITECTURE.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ All database access. Uses `sqlx::query()` (runtime, not compile-time macros) —
475475
| `error.rs` | Database error types |
476476

477477
**Channel types:** `Stream`, `Forum`, `Dm`, `Workflow`, `System`
478-
**Member roles:** `Owner`, `Admin`, `Member`, `Guest`, `Bot`
478+
**Member roles:** `Owner`, `Admin`, `Member`, `Guest`, `Bot` (`Bot` = `Member` permissions; the relay stores `member` and shows `bot` for keys with a recorded agent owner)
479479
**Workflow statuses:** `Active`, `Disabled`, `Archived`
480480
**Run statuses:** `Pending`, `Running`, `WaitingApproval`, `Completed`, `Failed`, `Cancelled`
481481

‎NOSTR.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,31 @@ relay to specific external Nostr identities without granting full access.
101101
SELECT encode(pubkey, 'hex'), added_at, note FROM pubkey_allowlist;
102102
```
103103

104+
### The bot role
105+
106+
`bot` is a label, not a permission tier. It has the same permissions as
107+
`member`, and the relay chooses between the two:
108+
109+
- When an add or role change ends at `member` or `bot`, the relay stores
110+
`member`. It ignores which of the two the client asked for. Asking for the
111+
other one is not a role change.
112+
- The relay shows an ordinary member as `bot` if the key has a recorded
113+
agent owner, and as `member` otherwise, in kind:39002 rosters and every
114+
other place it reports a role.
115+
- An agent owner is recorded once, by the first NIP-OA authentication, and
116+
never changes. When it is recorded, the relay republishes the kind:39002
117+
rosters of that key's channels so they show `bot`.
118+
- `owner`, `admin` and `guest` are stored and shown as asked.
119+
120+
Clients follow three rules:
121+
122+
- **Write:** send `member` for an ordinary member. The relay accepts `bot`
123+
from older clients and stores the same role it would store for `member`.
124+
- **Parse:** read a `bot` role in a kind:39002 roster or kind:9000 as `member`
125+
as soon as it is parsed. Do not keep `bot` as a separate client role.
126+
- **Identity:** never infer that a key is an agent from its channel role. A
127+
key is an agent only when a verified NIP-OA attestation says so.
128+
104129
### Announce channels
105130

106131
Roles have the same permissions in every channel. A channel's settings

‎crates/buzz-core/src/channel.rs‎

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,12 @@ pub fn decide_member_put(put: MemberPut<'_>) -> Result<MemberRole, MemberPutDeni
347347
// No role tag: an active member keeps their role.
348348
return current.parse().map_err(|_| MemberPutDenial::RoleChange);
349349
};
350-
if current == requested.as_str() {
350+
// `member` and `bot` are one role: the relay stores `member` and shows
351+
// `bot` when the target has a recorded agent owner, so asking for the
352+
// other one is not a role change.
353+
if current.parse::<MemberRole>().is_ok_and(|current| {
354+
current == requested || (current.is_ordinary_member() && requested.is_ordinary_member())
355+
}) {
351356
return Ok(requested);
352357
}
353358
// A stored role this build does not know is changed only by an elevated
@@ -439,7 +444,9 @@ impl FromStr for ChannelType {
439444
/// A member's role within a channel.
440445
///
441446
/// The hierarchy for permission checks is: Owner > Admin > Member > Guest.
442-
/// Bot is a **separate designation** — it is not part of the linear hierarchy.
447+
/// Bot has the same permissions as Member. The relay stores `member` for
448+
/// both and shows `bot` for a key with a recorded agent owner, so the role
449+
/// only labels an owned agent; it grants and refuses nothing extra.
443450
/// Use [`MemberRole::permission_level`] for numeric comparisons in authorization.
444451
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
445452
pub enum MemberRole {
@@ -451,7 +458,8 @@ pub enum MemberRole {
451458
Member,
452459
/// Read-only external participant.
453460
Guest,
454-
/// Automated agent or integration (not in the role hierarchy).
461+
/// A member whose key has a recorded agent owner. Same permissions as
462+
/// [`MemberRole::Member`]; the relay computes it when it reads a role.
455463
Bot,
456464
}
457465

@@ -472,6 +480,13 @@ impl MemberRole {
472480
matches!(self, Self::Owner | Self::Admin)
473481
}
474482

483+
/// Whether this is the ordinary member role, `member` or `bot`. A client
484+
/// may send either; the relay shows `bot` exactly when the key has a
485+
/// recorded agent owner.
486+
pub fn is_ordinary_member(&self) -> bool {
487+
matches!(self, Self::Member | Self::Bot)
488+
}
489+
475490
/// Whether this role may write in a channel: post, react, edit, change
476491
/// topic, add people, push to git. The same in every channel: every role
477492
/// except [`MemberRole::Guest`] may, and a bot counts as a member.
@@ -504,21 +519,18 @@ impl MemberRole {
504519

505520
/// Numeric permission level for authorization comparisons.
506521
///
507-
/// Higher = more privileged. Bot returns 0 (must use explicit grants).
522+
/// Higher = more privileged. Bot has the same level as Member.
508523
/// Use `role.permission_level() >= required.permission_level()` for checks.
509524
pub fn permission_level(self) -> u8 {
510525
match self {
511526
Self::Owner => 4,
512527
Self::Admin => 3,
513-
Self::Member => 2,
528+
Self::Member | Self::Bot => 2,
514529
Self::Guest => 1,
515-
Self::Bot => 0,
516530
}
517531
}
518532

519533
/// Returns true if this role meets or exceeds the required role's permission level.
520-
///
521-
/// Bot never meets any requirement (returns false for all non-Bot requirements).
522534
pub fn has_at_least(self, required: MemberRole) -> bool {
523535
self.permission_level() >= required.permission_level()
524536
}
@@ -759,6 +771,32 @@ mod tests {
759771
}
760772
}
761773

774+
#[test]
775+
fn bot_has_the_member_permission_level() {
776+
assert_eq!(
777+
MemberRole::Bot.permission_level(),
778+
MemberRole::Member.permission_level()
779+
);
780+
for required in [
781+
MemberRole::Owner,
782+
MemberRole::Admin,
783+
MemberRole::Member,
784+
MemberRole::Guest,
785+
MemberRole::Bot,
786+
] {
787+
assert_eq!(
788+
MemberRole::Bot.has_at_least(required),
789+
MemberRole::Member.has_at_least(required),
790+
"{required}"
791+
);
792+
}
793+
assert!(MemberRole::Bot.is_ordinary_member());
794+
assert!(MemberRole::Member.is_ordinary_member());
795+
for role in [MemberRole::Owner, MemberRole::Admin, MemberRole::Guest] {
796+
assert!(!role.is_ordinary_member(), "{role}");
797+
}
798+
}
799+
762800
#[test]
763801
fn only_guests_cannot_post() {
764802
for role in [

‎crates/buzz-core/src/git_perms.rs‎

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,9 @@ pub fn parse_protection_tag_with_warnings(
345345
.parse()
346346
.map_err(|_| RuleParseError::InvalidRole(role_str.to_string()))?;
347347
// Reject push:bot and push:guest — nonsensical rules.
348-
// Bot is promoted to Member at the policy layer; push:bot is meaningless.
349-
// Guest cannot push regardless; push:guest would be confusing.
348+
// Bot has the member permission level, so push:bot would mean
349+
// push:member. Guest cannot push regardless; push:guest would be
350+
// confusing.
350351
if matches!(role, MemberRole::Bot | MemberRole::Guest) {
351352
return Err(RuleParseError::InvalidRole(role_str.to_string()));
352353
}
@@ -931,19 +932,25 @@ mod tests {
931932
}
932933

933934
#[test]
934-
fn evaluate_bot_cannot_push_without_explicit_grant() {
935+
fn evaluate_bot_pushes_like_a_member() {
935936
let rules = vec![];
936-
let update = RefUpdate {
937-
ref_name: "refs/heads/feature".to_string(),
938-
kind: UpdateKind::FastForward,
939-
old_oid: "a".repeat(40),
940-
new_oid: "b".repeat(40),
941-
};
942-
// Bot has permission_level 0 at the core evaluator level.
943-
// NOTE: The policy layer (policy.rs) promotes Bot → Member before calling
944-
// evaluate_push, so bots in a channel CAN push in practice. This test
945-
// verifies the raw evaluator behavior; the promotion is tested in policy.
946-
assert!(evaluate_ref_update(&update, MemberRole::Bot, &rules).is_err());
937+
for (ref_name, kind) in [
938+
("refs/heads/feature", UpdateKind::FastForward),
939+
("refs/heads/feature", UpdateKind::NonFastForward),
940+
("refs/heads/main", UpdateKind::FastForward),
941+
] {
942+
let update = RefUpdate {
943+
ref_name: ref_name.to_string(),
944+
kind,
945+
old_oid: "a".repeat(40),
946+
new_oid: "b".repeat(40),
947+
};
948+
assert_eq!(
949+
evaluate_ref_update(&update, MemberRole::Bot, &rules).is_ok(),
950+
evaluate_ref_update(&update, MemberRole::Member, &rules).is_ok(),
951+
"{ref_name} {kind:?}"
952+
);
953+
}
947954
}
948955

949956
#[test]

‎crates/buzz-db/src/runtime/migration.rs‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -782,8 +782,7 @@ mod postgres_tests {
782782
}
783783
let retire_roster_fence = strip_sql_comments(migrations[75].sql.as_str());
784784
for statement in [
785-
"SET LOCAL lock_timeout = '1s';",
786-
"SET LOCAL statement_timeout = '5s';",
785+
"SET LOCAL lock_timeout = '5s';",
787786
"DROP TRIGGER trg_events_guard_channel_roster_snapshot ON events;",
788787
"DROP FUNCTION guard_channel_roster_snapshot();",
789788
] {
@@ -1343,7 +1342,7 @@ mod postgres_tests {
13431342
assert!(roster_fence.contains("snapshot_members IS DISTINCT FROM canonical_members"));
13441343
assert!(roster_fence.contains("ERRCODE = '23514'"));
13451344

1346-
// Migration 0076 retires this fence; the desired state never installs it.
1345+
// Migration 0074 retires this fence; the desired state never installs it.
13471346
assert!(!desired_schema.contains("guard_channel_roster_snapshot"));
13481347

13491348
// The single-row heartbeat table is updated continuously. Prevent

0 commit comments

Comments
 (0)