Skip to content

Commit d256935

Browse files
wpfleger96npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7
andauthored
feat: publish personas, teams, and managed agents as relay events (#939)
Signed-off-by: Will Pfleger <[email protected]> Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Will Pfleger <[email protected]>
1 parent e993b9e commit d256935

44 files changed

Lines changed: 6758 additions & 676 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/workflows/ci.yml‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,35 @@ jobs:
520520
path: /tmp/buzz-relay.log
521521
if-no-files-found: ignore
522522

523+
relay-e2e:
524+
name: Relay E2E
525+
runs-on: ubuntu-latest
526+
timeout-minutes: 20
527+
needs: [changes]
528+
if: github.event_name == 'push' || needs.changes.outputs.rust == 'true'
529+
permissions:
530+
contents: read
531+
steps:
532+
- uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6
533+
- uses: cashapp/activate-hermit@e49f5cb4dd64ff0b0b659d1d8df499595451155a # v1
534+
- uses: Swatinem/rust-cache@e18b497796c12c097a38f9edb9d0641fb99eee32 # v2
535+
with:
536+
save-if: ${{ github.event_name != 'pull_request' }}
537+
- name: Start relay
538+
run: ./scripts/start-relay-for-tests.sh
539+
- name: Relay E2E tests
540+
run: cargo test -p buzz-test-client --test e2e_persona --test e2e_nostr_interop -- --ignored --nocapture
541+
env:
542+
RELAY_URL: ws://localhost:3000
543+
GIT_CREDENTIAL_NOSTR_BIN: ${{ github.workspace }}/target/ci/git-credential-nostr
544+
- name: Upload relay logs
545+
if: failure()
546+
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
547+
with:
548+
name: relay-e2e-artifacts
549+
path: /tmp/buzz-relay.log
550+
if-no-files-found: ignore
551+
523552
web:
524553
name: Web
525554
runs-on: ubuntu-latest

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,33 @@ pub const KIND_EVENT_REMINDER: u32 = 30300;
110110
/// a compile-time bitset or sorted array with binary search for hot-path use.
111111
pub const AUTHOR_ONLY_KINDS: &[u32] = &[KIND_EVENT_REMINDER];
112112

113+
/// NIP-AP: Agent Persona (parameterized replaceable, owner-authored).
114+
///
115+
/// Persona definition event published by the workspace owner. Addressed by
116+
/// `(pubkey, kind, d_tag)` where `d_tag` is the plaintext persona slug.
117+
/// Content is a JSON body containing persona fields (system_prompt,
118+
/// display_name, avatar_url, runtime, model, provider, name_pool).
119+
/// Designed for discoverability and sharing — d-tag is not blinded.
120+
pub const KIND_PERSONA: u32 = 30175;
121+
122+
/// NIP-AP: Agent Team (parameterized replaceable, owner-authored).
123+
///
124+
/// Team definition event published by the workspace owner. Addressed by
125+
/// `(pubkey, kind, d_tag)` where `d_tag` is the team's stable id. Content is a
126+
/// JSON body projecting public team fields (name, description, persona_ids).
127+
/// A team is a user-facing grouping of personas; publishing keeps it
128+
/// authoritative across clients and reboots, mirroring `KIND_PERSONA`.
129+
pub const KIND_TEAM: u32 = 30176;
130+
131+
/// NIP-AP: Managed Agent (parameterized replaceable, owner-authored).
132+
///
133+
/// Managed-agent definition event published by the workspace owner. Addressed
134+
/// by `(pubkey, kind, d_tag)` where `d_tag` is the agent's pubkey. Content is
135+
/// an explicit opt-IN allowlist projection of the agent record — it MUST never
136+
/// carry the agent's secret key, NIP-OA auth tag, env vars, or runtime fields,
137+
/// since these events are world-readable on the relay.
138+
pub const KIND_MANAGED_AGENT: u32 = 30177;
139+
113140
// NIP-29 group admin events
114141
/// NIP-29: Add a user to a group.
115142
pub const KIND_NIP29_PUT_USER: u32 = 9000;
@@ -391,6 +418,9 @@ pub const ALL_KINDS: &[u32] = &[
391418
KIND_AGENT_PROFILE,
392419
KIND_AGENT_ENGRAM,
393420
KIND_EVENT_REMINDER,
421+
KIND_PERSONA,
422+
KIND_TEAM,
423+
KIND_MANAGED_AGENT,
394424
KIND_NIP29_PUT_USER,
395425
KIND_NIP29_REMOVE_USER,
396426
KIND_NIP29_EDIT_METADATA,
@@ -573,6 +603,9 @@ pub fn event_kind_i32(event: &nostr::Event) -> i32 {
573603

574604
// Compile-time: new kinds are in the expected ranges.
575605
const _: () = assert!(is_replaceable(KIND_AGENT_PROFILE)); // 10100 ∈ 10000–19999
606+
const _: () = assert!(is_parameterized_replaceable(KIND_PERSONA)); // 30175 ∈ 30000–39999
607+
const _: () = assert!(is_parameterized_replaceable(KIND_TEAM)); // 30176 ∈ 30000–39999
608+
const _: () = assert!(is_parameterized_replaceable(KIND_MANAGED_AGENT)); // 30177 ∈ 30000–39999
576609
const _: () = assert!(is_parameterized_replaceable(KIND_WORKFLOW_DEF)); // 30620 ∈ 30000–39999
577610
const _: () = assert!(is_parameterized_replaceable(KIND_EVENT_REMINDER)); // 30300 ∈ 30000–39999
578611
const _: () = assert!(is_parameterized_replaceable(KIND_MESH_LLM_RELAY_STATUS)); // 30621 ∈ 30000–39999

‎crates/buzz-relay/src/handlers/ingest.rs‎

Lines changed: 212 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,18 @@ use buzz_core::kind::{
2121
KIND_GIT_STATUS_CLOSED, KIND_GIT_STATUS_DRAFT, KIND_GIT_STATUS_MERGED, KIND_GIT_STATUS_OPEN,
2222
KIND_HUDDLE_ENDED, KIND_HUDDLE_GUIDELINES, KIND_HUDDLE_PARTICIPANT_JOINED,
2323
KIND_HUDDLE_PARTICIPANT_LEFT, KIND_HUDDLE_STARTED, KIND_IA_ARCHIVE_REQUEST,
24-
KIND_IA_UNARCHIVE_REQUEST, KIND_LONG_FORM, KIND_MEMBER_ADDED_NOTIFICATION,
24+
KIND_IA_UNARCHIVE_REQUEST, KIND_LONG_FORM, KIND_MANAGED_AGENT, KIND_MEMBER_ADDED_NOTIFICATION,
2525
KIND_MEMBER_REMOVED_NOTIFICATION, KIND_MESH_LLM_RELAY_STATUS, KIND_MUTE_LIST,
2626
KIND_NIP29_CREATE_GROUP, KIND_NIP29_DELETE_EVENT, KIND_NIP29_DELETE_GROUP,
2727
KIND_NIP29_EDIT_METADATA, KIND_NIP29_JOIN_REQUEST, KIND_NIP29_LEAVE_REQUEST,
2828
KIND_NIP29_PUT_USER, KIND_NIP29_REMOVE_USER, KIND_NIP43_LEAVE_REQUEST,
29-
KIND_NIP65_RELAY_LIST_METADATA, KIND_PIN_LIST, KIND_PRESENCE_UPDATE, KIND_PROFILE,
30-
KIND_REACTION, KIND_READ_STATE, KIND_STREAM_MESSAGE, KIND_STREAM_MESSAGE_BOOKMARKED,
31-
KIND_STREAM_MESSAGE_DIFF, KIND_STREAM_MESSAGE_EDIT, KIND_STREAM_MESSAGE_PINNED,
32-
KIND_STREAM_MESSAGE_SCHEDULED, KIND_STREAM_MESSAGE_V2, KIND_STREAM_REMINDER, KIND_TEXT_NOTE,
33-
KIND_USER_STATUS, KIND_WORKFLOW_DEF, KIND_WORKFLOW_TRIGGER, RELAY_ADMIN_ADD_MEMBER,
34-
RELAY_ADMIN_CHANGE_ROLE, RELAY_ADMIN_REMOVE_MEMBER,
29+
KIND_NIP65_RELAY_LIST_METADATA, KIND_PERSONA, KIND_PIN_LIST, KIND_PRESENCE_UPDATE,
30+
KIND_PROFILE, KIND_REACTION, KIND_READ_STATE, KIND_STREAM_MESSAGE,
31+
KIND_STREAM_MESSAGE_BOOKMARKED, KIND_STREAM_MESSAGE_DIFF, KIND_STREAM_MESSAGE_EDIT,
32+
KIND_STREAM_MESSAGE_PINNED, KIND_STREAM_MESSAGE_SCHEDULED, KIND_STREAM_MESSAGE_V2,
33+
KIND_STREAM_REMINDER, KIND_TEAM, KIND_TEXT_NOTE, KIND_USER_STATUS, KIND_WORKFLOW_DEF,
34+
KIND_WORKFLOW_TRIGGER, RELAY_ADMIN_ADD_MEMBER, RELAY_ADMIN_CHANGE_ROLE,
35+
RELAY_ADMIN_REMOVE_MEMBER,
3536
};
3637
use buzz_core::verification::verify_event;
3738
use nostr::Event;
@@ -154,7 +155,9 @@ fn required_scope_for_kind(kind: u32, event: &Event) -> Result<Scope, &'static s
154155
KIND_PROFILE => Ok(Scope::UsersWrite),
155156
KIND_TEXT_NOTE | KIND_LONG_FORM => Ok(Scope::MessagesWrite),
156157
KIND_CONTACT_LIST | KIND_READ_STATE | KIND_USER_STATUS | KIND_AGENT_ENGRAM
157-
| KIND_EVENT_REMINDER => Ok(Scope::UsersWrite),
158+
| KIND_EVENT_REMINDER | KIND_PERSONA | KIND_TEAM | KIND_MANAGED_AGENT => {
159+
Ok(Scope::UsersWrite)
160+
}
158161
// NIP-51 standard lists and NIP-65 relay list — user-owned global state,
159162
// same ownership shape as kind:3 (contacts) and kind:0 (profile).
160163
KIND_MUTE_LIST
@@ -344,6 +347,12 @@ pub(crate) fn is_global_only_kind(kind: u32) -> bool {
344347
| KIND_EVENT_REMINDER
345348
// Agent profile (10100): user-owned replaceable, keyed by pubkey.
346349
| KIND_AGENT_PROFILE
350+
// NIP-AP: persona definitions (30175): owner-authored, keyed by (pubkey, kind, d_tag).
351+
| KIND_PERSONA
352+
// NIP-AP: team (30176) + managed-agent (30177) definitions: owner-authored,
353+
// keyed by (pubkey, kind, d_tag). A stray `h` tag must not channel-scope them.
354+
| KIND_TEAM
355+
| KIND_MANAGED_AGENT
347356
// NIP-34: git events use `a` tags (repo reference), not `h` tags (channel scope).
348357
// Parameterized replaceable kinds are keyed by (pubkey, kind, d_tag).
349358
| KIND_GIT_REPO_ANNOUNCEMENT
@@ -885,6 +894,56 @@ fn validate_engram_envelope(event: &Event) -> Result<(), String> {
885894
Ok(())
886895
}
887896

897+
/// Validate the envelope of a kind:30175 persona event.
898+
///
899+
/// Enforces:
900+
/// * exactly one `d` tag with a non-empty value matching the slug grammar
901+
/// `^[a-z0-9][a-z0-9_-]{0,63}$`.
902+
///
903+
/// Without this, an empty d-tag collapses every persona into the
904+
/// `(pubkey, 30175, "")` slot — last-write-wins data loss.
905+
fn validate_persona_envelope(event: &Event) -> Result<(), String> {
906+
let mut d_tags: Vec<&str> = Vec::new();
907+
for tag in event.tags.iter() {
908+
let parts = tag.as_slice();
909+
if parts.len() >= 2 && parts[0].as_str() == "d" {
910+
d_tags.push(&parts[1]);
911+
}
912+
}
913+
if d_tags.len() != 1 {
914+
return Err(format!(
915+
"persona event must have exactly one `d` tag (got {})",
916+
d_tags.len()
917+
));
918+
}
919+
let d = d_tags[0];
920+
if d.is_empty() {
921+
return Err("persona event `d` tag must not be empty".to_string());
922+
}
923+
// Slug grammar: ^[a-z0-9][a-z0-9_-]{0,63}$
924+
if d.len() > 64 {
925+
return Err(format!(
926+
"persona event `d` tag too long ({} chars, max 64)",
927+
d.len()
928+
));
929+
}
930+
let bytes = d.as_bytes();
931+
if !bytes[0].is_ascii_lowercase() && !bytes[0].is_ascii_digit() {
932+
return Err(
933+
"persona event `d` tag must start with a lowercase letter or digit".to_string(),
934+
);
935+
}
936+
if !bytes[1..]
937+
.iter()
938+
.all(|&b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'_' || b == b'-')
939+
{
940+
return Err(
941+
"persona event `d` tag must match [a-z0-9_-] after the first character".to_string(),
942+
);
943+
}
944+
Ok(())
945+
}
946+
888947
/// Validate that `content` is a syntactically plausible NIP-44 v2 ciphertext.
889948
///
890949
/// Checks:
@@ -1483,6 +1542,12 @@ pub async fn ingest_event(
14831542
.map_err(|e| IngestError::Rejected(format!("invalid: {e}")))?;
14841543
}
14851544

1545+
// ── 15c. Persona envelope (kind:30175) ──────────────────────────────
1546+
if kind_u32 == KIND_PERSONA {
1547+
validate_persona_envelope(&event)
1548+
.map_err(|e| IngestError::Rejected(format!("invalid: {e}")))?;
1549+
}
1550+
14861551
// Track pre-created channel UUID for compensation on insert failure.
14871552
let mut pre_created_channel: Option<Uuid> = None;
14881553

@@ -1867,7 +1932,8 @@ mod tests {
18671932
use super::*;
18681933
use buzz_core::kind::{
18691934
KIND_CANVAS, KIND_FORUM_COMMENT, KIND_FORUM_POST, KIND_FORUM_VOTE, KIND_LONG_FORM,
1870-
KIND_PRESENCE_UPDATE, KIND_STREAM_MESSAGE, KIND_STREAM_MESSAGE_DIFF, KIND_USER_STATUS,
1935+
KIND_MANAGED_AGENT, KIND_PERSONA, KIND_PRESENCE_UPDATE, KIND_STREAM_MESSAGE,
1936+
KIND_STREAM_MESSAGE_DIFF, KIND_TEAM, KIND_USER_STATUS,
18711937
};
18721938

18731939
#[test]
@@ -2036,6 +2102,9 @@ mod tests {
20362102
KIND_EMOJI_LIST,
20372103
KIND_AGENT_ENGRAM,
20382104
KIND_AGENT_PROFILE,
2105+
KIND_PERSONA,
2106+
KIND_TEAM,
2107+
KIND_MANAGED_AGENT,
20392108
];
20402109
for kind in migrated {
20412110
assert!(
@@ -2094,6 +2163,47 @@ mod tests {
20942163
}
20952164
}
20962165

2166+
#[test]
2167+
fn persona_is_in_scope_allowlist() {
2168+
let dummy = make_dummy_event();
2169+
assert_eq!(
2170+
required_scope_for_kind(KIND_PERSONA, &dummy).unwrap(),
2171+
Scope::UsersWrite,
2172+
);
2173+
}
2174+
2175+
#[test]
2176+
fn persona_is_global_only() {
2177+
assert!(is_global_only_kind(KIND_PERSONA));
2178+
assert!(!requires_h_channel_scope(KIND_PERSONA));
2179+
}
2180+
2181+
#[test]
2182+
fn team_and_managed_agent_are_in_scope_allowlist() {
2183+
let dummy = make_dummy_event();
2184+
for kind in [KIND_TEAM, KIND_MANAGED_AGENT] {
2185+
assert_eq!(
2186+
required_scope_for_kind(kind, &dummy).unwrap(),
2187+
Scope::UsersWrite,
2188+
"kind {kind} should require UsersWrite scope"
2189+
);
2190+
}
2191+
}
2192+
2193+
#[test]
2194+
fn team_and_managed_agent_are_global_only() {
2195+
for kind in [KIND_TEAM, KIND_MANAGED_AGENT] {
2196+
assert!(
2197+
is_global_only_kind(kind),
2198+
"kind {kind} should be global-only (never channel-scoped)"
2199+
);
2200+
assert!(
2201+
!requires_h_channel_scope(kind),
2202+
"kind {kind} must not require an h-tag channel scope"
2203+
);
2204+
}
2205+
}
2206+
20972207
#[test]
20982208
fn unknown_kind_rejected() {
20992209
let dummy = make_dummy_event();
@@ -2582,4 +2692,97 @@ mod tests {
25822692
);
25832693
assert_eq!(validate_event_reminder(&ev), Err("duplicate d tag"));
25842694
}
2695+
2696+
// ── NIP-AP persona envelope validation ───────────────────────────────
2697+
2698+
fn make_persona(tags: &[&[&str]]) -> Event {
2699+
make_event_with_tags(
2700+
KIND_PERSONA,
2701+
r#"{"display_name":"x","system_prompt":"y"}"#,
2702+
tags,
2703+
)
2704+
}
2705+
2706+
#[test]
2707+
fn persona_envelope_accepts_valid_slug() {
2708+
let ev = make_persona(&[&["d", "my-persona-1"]]);
2709+
assert!(validate_persona_envelope(&ev).is_ok());
2710+
}
2711+
2712+
#[test]
2713+
fn persona_envelope_accepts_single_char() {
2714+
let ev = make_persona(&[&["d", "a"]]);
2715+
assert!(validate_persona_envelope(&ev).is_ok());
2716+
}
2717+
2718+
#[test]
2719+
fn persona_envelope_accepts_max_length() {
2720+
let slug = "a".repeat(64);
2721+
let ev = make_persona(&[&["d", &slug]]);
2722+
assert!(validate_persona_envelope(&ev).is_ok());
2723+
}
2724+
2725+
#[test]
2726+
fn persona_envelope_rejects_missing_d_tag() {
2727+
let ev = make_persona(&[]);
2728+
let err = validate_persona_envelope(&ev).unwrap_err();
2729+
assert!(err.contains("`d` tag"), "got: {err}");
2730+
}
2731+
2732+
#[test]
2733+
fn persona_envelope_rejects_empty_d_tag() {
2734+
let ev = make_persona(&[&["d", ""]]);
2735+
let err = validate_persona_envelope(&ev).unwrap_err();
2736+
assert!(err.contains("must not be empty"), "got: {err}");
2737+
}
2738+
2739+
#[test]
2740+
fn persona_envelope_rejects_duplicate_d_tags() {
2741+
let ev = make_persona(&[&["d", "slug-a"], &["d", "slug-b"]]);
2742+
let err = validate_persona_envelope(&ev).unwrap_err();
2743+
assert!(err.contains("`d` tag"), "got: {err}");
2744+
}
2745+
2746+
#[test]
2747+
fn persona_envelope_rejects_too_long() {
2748+
let slug = "a".repeat(65);
2749+
let ev = make_persona(&[&["d", &slug]]);
2750+
let err = validate_persona_envelope(&ev).unwrap_err();
2751+
assert!(err.contains("too long"), "got: {err}");
2752+
}
2753+
2754+
#[test]
2755+
fn persona_envelope_rejects_uppercase() {
2756+
let ev = make_persona(&[&["d", "My-Persona"]]);
2757+
let err = validate_persona_envelope(&ev).unwrap_err();
2758+
assert!(err.contains("`d` tag"), "got: {err}");
2759+
}
2760+
2761+
#[test]
2762+
fn persona_envelope_rejects_leading_underscore() {
2763+
let ev = make_persona(&[&["d", "_invalid"]]);
2764+
let err = validate_persona_envelope(&ev).unwrap_err();
2765+
assert!(err.contains("start with"), "got: {err}");
2766+
}
2767+
2768+
#[test]
2769+
fn persona_envelope_rejects_leading_hyphen() {
2770+
let ev = make_persona(&[&["d", "-invalid"]]);
2771+
let err = validate_persona_envelope(&ev).unwrap_err();
2772+
assert!(err.contains("start with"), "got: {err}");
2773+
}
2774+
2775+
#[test]
2776+
fn persona_envelope_rejects_spaces() {
2777+
let ev = make_persona(&[&["d", "has space"]]);
2778+
let err = validate_persona_envelope(&ev).unwrap_err();
2779+
assert!(err.contains("`d` tag"), "got: {err}");
2780+
}
2781+
2782+
#[test]
2783+
fn persona_envelope_rejects_dots() {
2784+
let ev = make_persona(&[&["d", "has.dot"]]);
2785+
let err = validate_persona_envelope(&ev).unwrap_err();
2786+
assert!(err.contains("`d` tag"), "got: {err}");
2787+
}
25852788
}

0 commit comments

Comments
 (0)