diff --git a/crates/buzz-relay/src/handlers/event.rs b/crates/buzz-relay/src/handlers/event.rs index a9e784413c..750018a5c4 100644 --- a/crates/buzz-relay/src/handlers/event.rs +++ b/crates/buzz-relay/src/handlers/event.rs @@ -243,11 +243,13 @@ pub async fn fan_out_pubsub_event(state: &Arc, channel_event: buzz_pub /// Schedule post-commit delivery/side effects for a stored event. /// -/// This intentionally returns after spawning the post-commit task: NIP-01 `OK` -/// means the event was durably accepted, not that Redis publish, local fan-out, -/// audit enqueue, or workflow triggering have completed. The spawned task still -/// runs the same guarded fan-out path, Redis publish, `mark_local_event` echo -/// dedupe, and delivery metrics as the former inline path. +/// This intentionally returns after only the bounded audit enqueue has completed: +/// NIP-01 `OK` means the event was durably accepted, not that Redis publish, +/// local fan-out, or workflow triggering have completed. Keeping audit enqueue on +/// the awaited path preserves the bounded-channel backpressure posture when the +/// audit DB is overloaded; the spawned task still runs the same guarded fan-out +/// path, Redis publish, `mark_local_event` echo dedupe, and delivery metrics as +/// the former inline path. pub(crate) async fn dispatch_persistent_event( tenant: &TenantContext, state: &Arc, @@ -255,11 +257,21 @@ pub(crate) async fn dispatch_persistent_event( kind_u32: u32, actor_pubkey_hex: &str, ) -> usize { + let event_id_hex = stored_event.event.id.to_hex(); + enqueue_event_created_audit( + tenant, + state, + stored_event, + kind_u32, + actor_pubkey_hex, + &event_id_hex, + ) + .await; + let tenant = tenant.clone(); let state = Arc::clone(state); let stored_event = stored_event.clone(); let actor_pubkey_hex = actor_pubkey_hex.to_owned(); - let event_id_hex = stored_event.event.id.to_hex(); metrics::counter!("buzz_post_commit_dispatch_scheduled_total").increment(1); tokio::spawn(async move { @@ -269,6 +281,7 @@ pub(crate) async fn dispatch_persistent_event( &stored_event, kind_u32, &actor_pubkey_hex, + false, ) .await; debug!( @@ -288,6 +301,7 @@ async fn dispatch_persistent_event_inner( stored_event: &StoredEvent, kind_u32: u32, actor_pubkey_hex: &str, + enqueue_audit: bool, ) -> usize { // No `crate::conformance` emit here — the spec doesn't have a // separate fan-out action. Acceptance was already recorded at the @@ -379,32 +393,16 @@ async fn dispatch_persistent_event_inner( // out-of-band index to feed. The old Typesense `index_event` worker and its // `search_index_tx` mpsc are gone with the Typesense backend. - // Audit via bounded channel (capacity 1000). Uses .send().await so entries - // are never silently dropped — backpressure propagates to the event handler - // if the queue is full. This is intentional: the audit advisory lock already - // serializes writes (at most 1 in-flight), so a full queue means the audit - // DB is genuinely overloaded and the relay should slow down rather than - // accumulate unbounded in-memory state. DB write failures in the worker are - // logged but not retried (same as the previous per-event tokio::spawn). - let audit_entry = buzz_audit::NewAuditEntry { - community_id: tenant.community(), - action: buzz_audit::AuditAction::EventCreated, - // Record the *actor* the caller resolved (authenticated principal for - // ingest, triggering user for workflow posts), not `stored_event.event - // .pubkey`. For relay-signed events (workflow sink, side-effect emits) - // the claimed author is the relay key, so deriving from the event would - // erase the human behind the action from the audit trail. This mirrors - // the pre-rewrite semantics, ported to the raw-bytes column. - actor_pubkey: hex::decode(actor_pubkey_hex).ok(), - object_id: Some(event_id_hex.clone()), - detail: serde_json::json!({ - "event_kind": kind_u32, - "channel_id": stored_event.channel_id, - }), - }; - if let Err(e) = state.audit_tx.send(audit_entry).await { - error!(event_id = %event_id_hex, "Audit channel closed — entry lost: {e}"); - metrics::counter!("buzz_audit_send_errors_total").increment(1); + if enqueue_audit { + enqueue_event_created_audit( + tenant, + state, + stored_event, + kind_u32, + actor_pubkey_hex, + &event_id_hex, + ) + .await; } // Skip workflow triggering for workflow-execution kinds and relay-signed workflow messages. @@ -445,6 +443,43 @@ async fn dispatch_persistent_event_inner( matches.len() } +async fn enqueue_event_created_audit( + tenant: &TenantContext, + state: &Arc, + stored_event: &StoredEvent, + kind_u32: u32, + actor_pubkey_hex: &str, + event_id_hex: &str, +) { + // Audit via bounded channel (capacity 1000). Uses .send().await so entries + // are never silently dropped — backpressure propagates to the event handler + // if the queue is full. This is intentional: the audit advisory lock already + // serializes writes (at most 1 in-flight), so a full queue means the audit + // DB is genuinely overloaded and the relay should slow down rather than + // accumulate unbounded in-memory state. DB write failures in the worker are + // logged but not retried (same as the previous per-event tokio::spawn). + let audit_entry = buzz_audit::NewAuditEntry { + community_id: tenant.community(), + action: buzz_audit::AuditAction::EventCreated, + // Record the *actor* the caller resolved (authenticated principal for + // ingest, triggering user for workflow posts), not `stored_event.event + // .pubkey`. For relay-signed events (workflow sink, side-effect emits) + // the claimed author is the relay key, so deriving from the event would + // erase the human behind the action from the audit trail. This mirrors + // the pre-rewrite semantics, ported to the raw-bytes column. + actor_pubkey: hex::decode(actor_pubkey_hex).ok(), + object_id: Some(event_id_hex.to_owned()), + detail: serde_json::json!({ + "event_kind": kind_u32, + "channel_id": stored_event.channel_id, + }), + }; + if let Err(e) = state.audit_tx.send(audit_entry).await { + error!(event_id = %event_id_hex, "Audit channel closed — entry lost: {e}"); + metrics::counter!("buzz_audit_send_errors_total").increment(1); + } +} + /// Handle an EVENT message from a WebSocket connection. /// /// Extracts auth from the WS connection, dispatches ephemeral events locally, @@ -1512,6 +1547,7 @@ mod tests { &stored, KIND_PRESENCE_UPDATE, &actor_hex, + true, ) .await; @@ -1612,6 +1648,7 @@ mod tests { &a_stored, KIND_PRESENCE_UPDATE, &actor_hex, + true, ) .await; super::super::dispatch_persistent_event_inner( @@ -1620,6 +1657,7 @@ mod tests { &b_stored, KIND_PRESENCE_UPDATE, &actor_hex, + true, ) .await;