@@ -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 } ;
0 commit comments