Skip to content

Commit d3c2217

Browse files
tlongwell-blockDawn (sprout agent)npub1jh9wn95s0472h86ahapupaf7m6kx4v9sx2n0atj2hltcfer8k06s5n3pyf
authored
relay: add author_type label to buzz_events_stored_total (#2243)
Signed-off-by: tlongwell-block <[email protected]> Signed-off-by: npub1jh9wn95s0472h86ahapupaf7m6kx4v9sx2n0atj2hltcfer8k06s5n3pyf <95cae996907d7cab9f5dbf43c0f53edeac6ab0b032a6feae4abfd784e467b3f5@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Dawn (sprout agent) <c6237ef84fa537c78dcee78efd2d4e59f728859c7f194da42ac51ededfa0be05@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1jh9wn95s0472h86ahapupaf7m6kx4v9sx2n0atj2hltcfer8k06s5n3pyf <95cae996907d7cab9f5dbf43c0f53edeac6ab0b032a6feae4abfd784e467b3f5@sprout-oss.stage.blox.sqprod.co>
1 parent 530844a commit d3c2217

6 files changed

Lines changed: 188 additions & 106 deletions

File tree

‎crates/buzz-relay/src/api/bridge.rs‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,13 +626,23 @@ pub async fn submit_event(
626626
.map_err(|e| api_error(StatusCode::BAD_REQUEST, &format!("invalid event JSON: {e}")))?;
627627
// Enforce relay membership (with NIP-OA fallback via x-auth-tag header).
628628
let auth_tag = headers.get("x-auth-tag").and_then(|v| v.to_str().ok());
629-
super::relay_members::enforce_relay_membership(
629+
let nip_oa_owner = super::relay_members::enforce_relay_membership(
630630
&state,
631631
tenant.community(),
632632
&pubkey_bytes,
633633
auth_tag,
634634
)
635-
.await?;
635+
.await?
636+
.or_else(|| {
637+
if !state.config.require_relay_membership {
638+
super::relay_members::extract_nip_oa_owner(&pubkey_bytes, auth_tag)
639+
} else {
640+
None
641+
}
642+
});
643+
if let Some(owner) = nip_oa_owner {
644+
super::relay_members::materialize_nip_oa_owner(&state, &tenant, &pubkey, &owner).await;
645+
}
636646

637647
let auth = IngestAuth::Http {
638648
pubkey,

‎crates/buzz-relay/src/api/mod.rs‎

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub(crate) fn not_found(msg: &str) -> (StatusCode, Json<serde_json::Value>) {
3636
/// `git/transport.rs`, and `audio/handler.rs`.
3737
pub mod relay_members {
3838
use axum::{http::StatusCode, response::Json};
39-
use buzz_core::tenant::CommunityId;
39+
use buzz_core::{tenant::CommunityId, TenantContext};
4040
use tracing::{debug, info};
4141

4242
use crate::state::AppState;
@@ -166,6 +166,71 @@ pub mod relay_members {
166166
}
167167
}
168168

169+
/// Persist a cryptographically verified NIP-OA agent→owner relationship.
170+
///
171+
/// Both principals are ensured first because `agent_owner_pubkey` has a
172+
/// community-scoped foreign key. The mapping is first-write-wins; an
173+
/// existing mapping is accepted only when it names the same owner.
174+
pub async fn materialize_nip_oa_owner(
175+
state: &AppState,
176+
tenant: &TenantContext,
177+
agent: &nostr::PublicKey,
178+
owner: &nostr::PublicKey,
179+
) -> bool {
180+
for (role, pubkey) in [("agent", agent), ("owner", owner)] {
181+
match state
182+
.db
183+
.ensure_user(tenant.community(), pubkey.as_bytes())
184+
.await
185+
{
186+
Ok(true) => {
187+
metrics::counter!(
188+
"buzz_users_created_total",
189+
"community" => tenant.host().to_owned()
190+
)
191+
.increment(1);
192+
}
193+
Ok(false) => {}
194+
Err(e) => {
195+
tracing::warn!(%role, error = %e, "ensure_user failed during NIP-OA backfill");
196+
return false;
197+
}
198+
}
199+
}
200+
201+
let materialized = match state
202+
.db
203+
.set_agent_owner(tenant.community(), agent.as_bytes(), owner.as_bytes())
204+
.await
205+
{
206+
Ok(true) => true,
207+
Ok(false) => state
208+
.db
209+
.is_agent_owner(tenant.community(), agent.as_bytes(), owner.as_bytes())
210+
.await
211+
.unwrap_or(false),
212+
Err(e) => {
213+
tracing::warn!(error = %e, "failed to backfill agent_owner_pubkey");
214+
false
215+
}
216+
};
217+
218+
if materialized {
219+
state
220+
.author_type_cache
221+
.insert((tenant.community(), agent.to_bytes().to_vec()), true);
222+
state.observer_owner_cache.insert(
223+
(
224+
tenant.community(),
225+
agent.to_bytes().to_vec(),
226+
owner.to_bytes().to_vec(),
227+
),
228+
true,
229+
);
230+
}
231+
materialized
232+
}
233+
169234
#[cfg(test)]
170235
mod tests {
171236
use super::*;

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

Lines changed: 17 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -252,106 +252,25 @@ pub async fn handle_auth(event: nostr::Event, conn: Arc<ConnectionState>, state:
252252
}
253253
});
254254

255-
// Stash NIP-OA owner on the auth context (session-scoped) only if
256-
// the DB confirms this owner relationship (first-write-wins).
255+
// Stash NIP-OA owner on the auth context only after the shared
256+
// backfill confirms the first-write-wins relationship.
257257
if let Some(owner) = nip_oa_owner {
258-
// Ensure both agent and owner have users rows (BYO agents may not,
259-
// and agent_owner_pubkey has a FK constraint to users.pubkey).
260-
match state
261-
.db
262-
.ensure_user(conn.tenant.community(), pubkey.as_bytes())
263-
.await
264-
{
265-
Ok(true) => {
266-
metrics::counter!(
267-
"buzz_users_created_total",
268-
"community" => conn.tenant.host().to_owned()
269-
)
270-
.increment(1);
271-
}
272-
Ok(false) => {}
273-
Err(e) => {
274-
warn!(conn_id = %conn_id, error = %e, "ensure_user(agent) failed during NIP-OA backfill");
275-
}
276-
}
277-
match state
278-
.db
279-
.ensure_user(conn.tenant.community(), owner.as_bytes())
280-
.await
258+
if crate::api::relay_members::materialize_nip_oa_owner(
259+
&state,
260+
&conn.tenant,
261+
&pubkey,
262+
&owner,
263+
)
264+
.await
281265
{
282-
Ok(true) => {
283-
metrics::counter!(
284-
"buzz_users_created_total",
285-
"community" => conn.tenant.host().to_owned()
286-
)
287-
.increment(1);
288-
}
289-
Ok(false) => {}
290-
Err(e) => {
291-
warn!(conn_id = %conn_id, error = %e, "ensure_user(owner) failed during NIP-OA backfill");
292-
}
293-
}
294-
295-
// Idempotent backfill: record agent→owner in DB so cross-connection
296-
// features (observer frames, channel policy) work for BYO agents.
297-
// Returns Ok(true) if written, Ok(false) if already owned by someone else.
298-
match state
299-
.db
300-
.set_agent_owner(conn.tenant.community(), pubkey.as_bytes(), owner.as_bytes())
301-
.await
302-
{
303-
Ok(true) => {
304-
// Successfully materialized — this owner is authoritative.
305-
auth_ctx.agent_owner_pubkey = Some(owner);
306-
// Pre-warm the observer cache to avoid stale negatives.
307-
let cache_key = (
308-
conn.tenant.community(),
309-
pubkey.to_bytes().to_vec(),
310-
owner.to_bytes().to_vec(),
311-
);
312-
state.observer_owner_cache.insert(cache_key, true);
313-
}
314-
Ok(false) => {
315-
// Agent already owned by someone else. Verify if this
316-
// owner matches the existing DB record before trusting it.
317-
match state
318-
.db
319-
.is_agent_owner(
320-
conn.tenant.community(),
321-
pubkey.as_bytes(),
322-
owner.as_bytes(),
323-
)
324-
.await
325-
{
326-
Ok(true) => {
327-
auth_ctx.agent_owner_pubkey = Some(owner);
328-
}
329-
Ok(false) => {
330-
warn!(
331-
conn_id = %conn_id,
332-
agent = %pubkey.to_hex(),
333-
nip_oa_owner = %owner.to_hex(),
334-
"NIP-OA owner differs from DB owner — session will not get owner fast-path"
335-
);
336-
}
337-
Err(e) => {
338-
warn!(
339-
conn_id = %conn_id,
340-
error = %e,
341-
"is_agent_owner check failed after set_agent_owner conflict"
342-
);
343-
}
344-
}
345-
}
346-
Err(e) => {
347-
warn!(
348-
conn_id = %conn_id,
349-
agent = %pubkey.to_hex(),
350-
owner = %owner.to_hex(),
351-
error = %e,
352-
"failed to backfill agent_owner_pubkey"
353-
);
354-
}
266+
auth_ctx.agent_owner_pubkey = Some(owner);
267+
} else {
268+
warn!(
269+
conn_id = %conn_id,
270+
agent = %pubkey.to_hex(),
271+
nip_oa_owner = %owner.to_hex(),
272+
"NIP-OA owner could not be materialized"
273+
);
355274
}
356275
}
357276

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ fn reject(reason: &'static str) {
3232
}
3333

3434
/// Bound the `kind` label to prevent cardinality explosion from arbitrary Nostr kinds.
35-
fn bounded_kind_label(kind: u32) -> String {
35+
pub(crate) fn bounded_kind_label(kind: u32) -> String {
3636
match kind {
3737
0..=9 | 1059 | 1063 => kind.to_string(),
3838
8000..=8003 | 9000..=9022 | 9030..=9036 => kind.to_string(),
@@ -598,7 +598,7 @@ pub async fn handle_event(event: Event, conn: Arc<ConnectionState>, state: Arc<A
598598
// Rationale: bounded_kind_label passes through all 10k values in
599599
// 20000..=29999 (client-controlled ephemeral range). Crossing kind ×
600600
// community would produce up to millions of series. Keep kind fleet-wide.
601-
metrics::counter!("buzz_events_received_total", "kind" => kind_str.clone()).increment(1);
601+
metrics::counter!("buzz_events_received_total", "kind" => kind_str).increment(1);
602602
// Per-community volume counter: community-only, no kind tag.
603603
// Use this for per-community throughput graphs; the fleet counter above
604604
// for per-kind breakdowns.
@@ -705,9 +705,8 @@ pub async fn handle_event(event: Event, conn: Arc<ConnectionState>, state: Arc<A
705705
match super::ingest::ingest_event(&state, &conn.tenant, event, ingest_auth).await {
706706
Ok(result) => {
707707
if result.accepted {
708-
// Fleet-wide stored counter: kind-only, no community tag.
709-
// Same cardinality rationale as buzz_events_received_total above.
710-
metrics::counter!("buzz_events_stored_total", "kind" => kind_str).increment(1);
708+
// buzz_events_stored_total is emitted inside ingest_event()
709+
// (shared WS/HTTP seam), not here.
711710
info!(
712711
event_id = %result.event_id,
713712
kind = kind_u32,

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ impl IngestAuth {
9191
}
9292
}
9393

94+
/// Pubkey used for principal-scoped accounting and policy lookups.
95+
pub fn principal_pubkey_bytes(&self) -> Vec<u8> {
96+
self.pubkey().to_bytes().to_vec()
97+
}
98+
9499
/// Permission scopes for this auth context.
95100
pub fn scopes(&self) -> &[Scope] {
96101
match self {
@@ -1299,6 +1304,36 @@ fn validate_event_reminder(event: &Event) -> Result<(), &'static str> {
12991304
Ok(())
13001305
}
13011306

1307+
/// Resolve the `author_type` metric label (`"agent"` / `"human"`) for an
1308+
/// event author, from `users.agent_owner_pubkey IS NOT NULL` via a
1309+
/// per-community cache. Metric-labeling only — never used for authorization.
1310+
/// Unknown pubkeys and lookup errors count as "human" (the label must not
1311+
/// add a failure path to ingest).
1312+
async fn author_type_label(
1313+
state: &Arc<AppState>,
1314+
tenant: &TenantContext,
1315+
author_pubkey_bytes: Vec<u8>,
1316+
) -> &'static str {
1317+
let key = (tenant.community(), author_pubkey_bytes);
1318+
let cached = state.author_type_cache.get(&key);
1319+
let is_agent = match cached {
1320+
Some(v) => v,
1321+
None => {
1322+
let v = match state.db.get_agent_channel_policy(key.0, &key.1).await {
1323+
Ok(Some((_, owner))) => owner.is_some(),
1324+
Ok(None) | Err(_) => false,
1325+
};
1326+
state.author_type_cache.insert(key, v);
1327+
v
1328+
}
1329+
};
1330+
if is_agent {
1331+
"agent"
1332+
} else {
1333+
"human"
1334+
}
1335+
}
1336+
13021337
/// Ingest a signed Nostr event through the full validation pipeline.
13031338
///
13041339
/// Shared by WebSocket and HTTP transports. The caller constructs [`IngestAuth`]
@@ -1319,6 +1354,14 @@ pub async fn ingest_event(
13191354
event: Event,
13201355
auth: IngestAuth,
13211356
) -> Result<IngestResult, IngestError> {
1357+
// Captured before `event` moves into the inner fn: the stored-events
1358+
// counter below is emitted at this shared seam so WebSocket and HTTP
1359+
// transports are counted identically.
1360+
let kind_label = super::event::bounded_kind_label(event_kind_u32(&event));
1361+
// Classify the authenticated principal, not the event envelope signer:
1362+
// NIP-59 gift wraps deliberately use an unrelated ephemeral pubkey.
1363+
let author_pubkey_bytes = auth.principal_pubkey_bytes();
1364+
13221365
let abstract_state = state_for_request(tenant, auth.pubkey());
13231366
let (_guard, tracer) = EmitGuard::arm(
13241367
state.tracer.clone(),
@@ -1328,6 +1371,22 @@ pub async fn ingest_event(
13281371

13291372
let result = ingest_event_inner(state, &tracer, tenant, event, auth).await;
13301373

1374+
// Fleet-wide stored counter: kind + author_type only, no community tag
1375+
// (see the cardinality rationale on buzz_events_received_total —
1376+
// author_type is a 2-value label so it merely doubles the kind series).
1377+
// Emitted here rather than per-transport so HTTP bridge ingests count too.
1378+
if let Ok(r) = &result {
1379+
if r.accepted {
1380+
let author_type = author_type_label(state, tenant, author_pubkey_bytes).await;
1381+
metrics::counter!(
1382+
"buzz_events_stored_total",
1383+
"kind" => kind_label,
1384+
"author_type" => author_type
1385+
)
1386+
.increment(1);
1387+
}
1388+
}
1389+
13311390
// Map terminal error variants onto the closed SanitizedReason
13321391
// alphabet (spec line 778). The inner fn's success path emits
13331392
// WriteInsert/WriteInsertGlobal/WriteDuplicate explicitly at its
@@ -2856,6 +2915,24 @@ mod tests {
28562915
);
28572916
}
28582917

2918+
#[test]
2919+
fn accounting_uses_authenticated_principal_pubkey() {
2920+
let principal = nostr::Keys::generate();
2921+
let envelope_signer = nostr::Keys::generate();
2922+
let auth = IngestAuth::Nip42 {
2923+
pubkey: principal.public_key(),
2924+
scopes: vec![],
2925+
channel_ids: None,
2926+
conn_id: Uuid::new_v4(),
2927+
};
2928+
2929+
assert_ne!(principal.public_key(), envelope_signer.public_key());
2930+
assert_eq!(
2931+
auth.principal_pubkey_bytes(),
2932+
principal.public_key().to_bytes().to_vec()
2933+
);
2934+
}
2935+
28592936
#[test]
28602937
fn ingest_auth_is_http_returns_true_for_http_variant() {
28612938
use crate::handlers::ingest::{HttpAuthMethod, IngestAuth};

‎crates/buzz-relay/src/state.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,12 @@ pub struct AppState {
547547
/// Prevents repeated DB lookups from bursty observer traffic.
548548
#[allow(clippy::type_complexity)]
549549
pub observer_owner_cache: Arc<moka::sync::Cache<(CommunityId, Vec<u8>, Vec<u8>), bool>>,
550+
/// Cache for the `author_type` metric label on the ingest path.
551+
/// Key: (community_id, author pubkey bytes). Value: is_agent
552+
/// (`users.agent_owner_pubkey IS NOT NULL`). The mapping is
553+
/// first-write-wins and set during auth before an agent's first event,
554+
/// so a short TTL only bounds staleness for the rare backfill race.
555+
pub author_type_cache: Arc<moka::sync::Cache<(CommunityId, Vec<u8>), bool>>,
550556

551557
/// Runtime conformance tracer. Production binds [`crate::conformance::NoopTracer`]
552558
/// (zero cost). Conformance tests bind [`crate::conformance::JsonlTracer`] to
@@ -718,6 +724,12 @@ impl AppState {
718724
.time_to_live(std::time::Duration::from_secs(300))
719725
.build(),
720726
),
727+
author_type_cache: Arc::new(
728+
moka::sync::Cache::builder()
729+
.max_capacity(10_000)
730+
.time_to_live(std::time::Duration::from_secs(300))
731+
.build(),
732+
),
721733
// Default to NoopTracer: production builds pay zero cost.
722734
// Conformance tests overwrite this with a JsonlTracer after
723735
// construction (see test helpers in

0 commit comments

Comments
 (0)