Files
buzz/crates/buzz-relay/src/workflow_sink.rs
Tyler 64b8fea6dc Bug-bash round 2: table scroll, Goose instructions, workflow mention wake (#2034)
Signed-off-by: Tyler Longwell <tlongwell@block.xyz>
Co-authored-by: npub1mprnacetjua2xx3p5eddmhxyk6wv929ymm5py8kd2xfxurxahspqqlgyta <d8473ee32b973aa31a21a65adddcc4b69cc2a8a4dee8121ecd51926e0cddbc02@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: Tyler Longwell <tlongwell@block.xyz>
Co-authored-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co>
2026-07-17 14:49:54 -04:00

712 lines
28 KiB
Rust

//! Relay-side implementation of [`ActionSink`] for workflow actions.
//!
//! Builds Nostr events, persists them, and delegates post-persist side effects
//! (WebSocket fan-out, Redis pub/sub, search indexing, audit logging) to the
//! existing [`dispatch_persistent_event`] helper.
use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, Weak};
use buzz_core::kind::KIND_STREAM_MESSAGE;
use buzz_core::tenant::CommunityId;
use buzz_workflow::action_sink::{ActionSink, ActionSinkError};
use chrono::Utc;
use nostr::{EventBuilder, Kind, Tag};
use tracing::info;
use uuid::Uuid;
use crate::handlers::event::dispatch_persistent_event;
use crate::state::AppState;
/// Resolves `@Name` mentions in workflow message text to the pubkeys of the
/// channel members they name, so the emitted kind:9 carries the `p` tags that
/// ACP agent-wake (`event_mentions_agent`) is gated on.
///
/// The client resolves mentions to `p` tags at compose time from an interactive
/// autocomplete pick; the workflow path has only free text, so this reverse-parse
/// *defines* the matching contract. It is deliberately conservative to avoid
/// waking the wrong agent:
///
/// - **Members only.** Candidates are the destination channel's members; global
/// users are never matched.
/// - **Exact display name.** No substring, prefix, or fuzzy matching. Names may
/// contain spaces/punctuation (`"Will Pfleger"`, `"Lep (Subagent)"`), so the
/// match is anchored on `@` and terminated by a non-name boundary rather than
/// whitespace.
/// - **Greedy-longest, non-overlapping.** Longer names are matched first and
/// consume their span, so `@Will Pfleger` binds *Pfleger* and a bare `@Will`
/// does not match the member `"Will Pfleger"`.
/// - **Ambiguous names wake no one.** If two or more members share the matched
/// display name, no `p` tag is emitted for it — arbitrary selection would
/// silently misroute and tagging all of them is a false-wake firehose.
///
/// Returns deduplicated pubkey hexes, in first-appearance order in `text`.
fn resolve_mention_pubkeys(text: &str, members: &[(String, String)]) -> Vec<String> {
// Name → pubkey, folding case (client matches case-insensitively). A name
// that maps to more than one distinct pubkey is ambiguous → wake no one.
let mut by_name: std::collections::HashMap<String, Option<String>> =
std::collections::HashMap::new();
for (name, pubkey) in members {
if name.trim().is_empty() {
continue;
}
by_name
.entry(name.to_lowercase())
.and_modify(|slot| {
if slot.as_deref() != Some(pubkey.as_str()) {
*slot = None; // ambiguous
}
})
.or_insert_with(|| Some(pubkey.clone()));
}
// Match longest names first so a longer name consumes its span before a
// shorter substring name can claim part of it.
let mut names: Vec<&(String, String)> = members.iter().collect();
names.sort_by_key(|(name, _)| std::cmp::Reverse(name.chars().count()));
let chars: Vec<char> = text.chars().collect();
let mut consumed = vec![false; chars.len()];
// Case-insensitivity folds *both* sides through `char::to_lowercase`, which
// can change length: `İ` (U+0130) lowercases to two code points (`i` +
// U+0307 combining dot). Comparing a pre-lowercased copy of the whole text
// against a lowercased name by index silently desyncs once any earlier char
// expands. Instead, fold on the fly: walk the original `chars` at the
// candidate `@`, folding each char, and match against the folded-name char
// stream — tracking how many *original* chars were consumed so
// boundary/`consumed` accounting stays in original coordinates. `None` = no
// match; `Some(n)` = matched, consuming `n` original chars after the `@`.
let match_name_len = |start: usize, folded_name: &[char]| -> Option<usize> {
let mut ci = start;
let mut ni = 0;
while ni < folded_name.len() {
let c = *chars.get(ci)?;
for fc in c.to_lowercase() {
if folded_name.get(ni) != Some(&fc) {
return None;
}
ni += 1;
}
ci += 1;
}
Some(ci - start)
};
// A mention is anchored on `@` at a left boundary (start / whitespace / `(`)
// and the matched name must not be followed by a name-continuation char —
// otherwise `@Will` would match inside `@Willow`. Combined with matching the
// longest member name first, this is the whole rule: no punctuation allowlist
// to get wrong, and it is unicode-safe (em-dash, emoji all terminate a name).
let is_left_boundary = |i: usize| i == 0 || chars[i - 1].is_whitespace() || chars[i - 1] == '(';
let extends_name = |c: char| c.is_alphanumeric() || c == '_';
let mut out: Vec<String> = Vec::new();
let mut seen = std::collections::HashSet::new();
let mut hits: Vec<(usize, String)> = Vec::new();
for (name, _) in &names {
let folded_name: Vec<char> = name.to_lowercase().chars().collect();
if folded_name.is_empty() {
continue;
}
let mut at = 0;
while at < chars.len() {
// Anchor on `@` at a left boundary and an unconsumed span; only then
// attempt the fold-match. `name_len` is measured in *original* chars,
// so `at + 1 + name_len` is the true position just past the name.
let name_len = (chars[at] == '@' && is_left_boundary(at) && !consumed[at])
.then(|| match_name_len(at + 1, &folded_name))
.flatten()
.filter(|&n| {
chars[at + 1 + n..]
.first()
.is_none_or(|&c| !extends_name(c))
});
if let Some(name_len) = name_len {
let span = 1 + name_len;
if let Some(Some(pubkey)) = by_name.get(&name.to_lowercase()) {
hits.push((at, pubkey.clone()));
}
for slot in consumed.iter_mut().skip(at).take(span) {
*slot = true;
}
at += span;
} else {
at += 1;
}
}
}
hits.sort_by_key(|(at, _)| *at);
for (_, pubkey) in hits {
if seen.insert(pubkey.clone()) {
out.push(pubkey);
}
}
out
}
/// Relay-side action sink — executes workflow side-effects directly.
///
/// Holds a **weak** reference to `AppState` to avoid an `Arc` reference cycle:
/// `AppState` → `WorkflowEngine` → `ActionSink` → `AppState`. Using `Weak`
/// breaks the cycle so all structs can be dropped on shutdown.
///
/// Post-persist side effects are delegated to [`dispatch_persistent_event`]
/// for consistency with the REST/WebSocket paths.
pub struct RelayActionSink {
state: Weak<AppState>,
}
impl RelayActionSink {
/// Create a new `RelayActionSink` from the shared application state.
pub fn new(state: &Arc<AppState>) -> Self {
Self {
state: Arc::downgrade(state),
}
}
}
impl ActionSink for RelayActionSink {
fn send_message(
&self,
community_id: CommunityId,
channel_id: &str,
text: &str,
author_pubkey: &str,
) -> Pin<Box<dyn Future<Output = Result<String, ActionSinkError>> + Send + '_>> {
let channel_id = channel_id.to_owned();
let text = text.to_owned();
let author_pubkey = author_pubkey.to_owned();
Box::pin(async move {
// 0. Upgrade weak reference — fails only during shutdown.
let state = self
.state
.upgrade()
.ok_or_else(|| ActionSinkError::Database("relay is shutting down".into()))?;
// The run carries its owning community (`community_id`); the
// relay-signed kind:9 message belongs to *that* community, never the
// deployment default. Re-deriving the tenant from `config.relay_url`
// would post a community-B workflow's output into the deployment/
// default community under N>1. Read the community's host back to
// form a complete TenantContext (host is for labelling only — the
// community is already fixed and is never re-derived from it). Fail
// closed if the community no longer maps to a host.
let host = state
.db
.lookup_community_host(community_id)
.await
.map_err(|e| ActionSinkError::Database(e.to_string()))?
.ok_or_else(|| {
ActionSinkError::Database(format!(
"workflow run community {community_id} is not mapped to a host"
))
})?;
let tenant = buzz_core::tenant::TenantContext::resolved(community_id, host);
// 1. Validate content is not empty/whitespace-only
if text.trim().is_empty() {
return Err(ActionSinkError::EmptyContent);
}
// 2. Parse and validate channel — canonicalize UUID immediately
let channel_uuid = Uuid::parse_str(&channel_id)
.map_err(|e| ActionSinkError::InvalidInput(format!("invalid UUID: {e}")))?;
let channel_id_canonical = channel_uuid.to_string();
let channel = state
.db
.get_channel(tenant.community(), channel_uuid)
.await
.map_err(|e| match &e {
buzz_db::DbError::ChannelNotFound(_) | buzz_db::DbError::NotFound(_) => {
ActionSinkError::ChannelNotFound(channel_id_canonical.clone())
}
_ => ActionSinkError::Database(e.to_string()),
})?;
if channel.archived_at.is_some() {
return Err(ActionSinkError::ChannelArchived(
channel_id_canonical.clone(),
));
}
let author_pubkey = nostr::PublicKey::from_hex(&author_pubkey).map_err(|e| {
ActionSinkError::InvalidInput(format!("invalid author pubkey: {e}"))
})?;
let author_pubkey_bytes = author_pubkey.to_bytes().to_vec();
let author_pubkey_hex = author_pubkey.to_hex();
let is_member = state
.is_member_cached(tenant.community(), channel_uuid, &author_pubkey_bytes)
.await
.map_err(|e| ActionSinkError::Database(e.to_string()))?;
if !is_member && channel.visibility != "open" {
return Err(ActionSinkError::InvalidInput(
"workflow owner does not have access to destination channel".into(),
));
}
// 3. Build kind:9 Nostr event
// - Signed by relay keypair (event.pubkey = relay pubkey)
// - `p` tag attributes the message to the workflow owner
// - `h` tag scopes to the channel (NIP-29, canonical UUID)
// - `buzz:workflow` tag prevents recursive workflow triggering
// - one `p` tag per `@Name` that resolves to a channel member,
// so mentioned agents are woken (wake is `p`-tag gated)
let mut tags = vec![
Tag::parse(["p", &author_pubkey_hex])
.map_err(|e| ActionSinkError::EventBuild(format!("p tag: {e}")))?,
Tag::parse(["h", &channel_id_canonical])
.map_err(|e| ActionSinkError::EventBuild(format!("h tag: {e}")))?,
Tag::parse(["buzz:workflow", "true"])
.map_err(|e| ActionSinkError::EventBuild(format!("workflow tag: {e}")))?,
];
// Resolve `@Name` mentions to channel-member pubkeys and append a
// `p` tag for each (skipping the author, already tagged above). A
// resolution failure must not drop the message, so log and proceed
// with the base tags.
let members = state
.db
.get_members(tenant.community(), channel_uuid)
.await
.map_err(|e| ActionSinkError::Database(e.to_string()))?;
let member_pubkeys: Vec<Vec<u8>> = members.iter().map(|m| m.pubkey.clone()).collect();
let users = state
.db
.get_users_bulk(tenant.community(), &member_pubkeys)
.await
.map_err(|e| ActionSinkError::Database(e.to_string()))?;
let named_members: Vec<(String, String)> = users
.into_iter()
.filter_map(|u| {
let name = u.display_name?;
Some((name, nostr::PublicKey::from_slice(&u.pubkey).ok()?.to_hex()))
})
.collect();
for mentioned in resolve_mention_pubkeys(&text, &named_members) {
if mentioned == author_pubkey_hex {
continue;
}
tags.push(
Tag::parse(["p", &mentioned])
.map_err(|e| ActionSinkError::EventBuild(format!("mention p tag: {e}")))?,
);
}
let kind = Kind::from(KIND_STREAM_MESSAGE as u16);
let event = EventBuilder::new(kind, &text)
.tags(tags)
.sign_with_keys(&state.relay_keypair)
.map_err(|e| ActionSinkError::EventBuild(format!("signing: {e}")))?;
let event_id_hex = event.id.to_hex();
let event_id_bytes = event.id.as_bytes().to_vec();
let kind_u32 = KIND_STREAM_MESSAGE;
let event_created_at = {
let ts = event.created_at.as_secs() as i64;
chrono::DateTime::from_timestamp(ts, 0).unwrap_or_else(Utc::now)
};
info!(
event_id = %event_id_hex,
channel_id = %channel_id_canonical,
author = %author_pubkey,
"Workflow SendMessage: posting kind {kind_u32} event"
);
// 4. Persist event with thread metadata (matches REST handler path).
// Workflow messages are always top-level: depth=0, no parent/root.
let thread_meta = Some(buzz_db::event::ThreadMetadataParams {
event_id: &event_id_bytes,
event_created_at,
channel_id: channel_uuid,
parent_event_id: None,
parent_event_created_at: None,
root_event_id: None,
root_event_created_at: None,
depth: 0,
broadcast: false,
});
let (stored_event, was_inserted) = state
.db
.insert_event_with_thread_metadata(
tenant.community(),
&event,
Some(channel_uuid),
thread_meta,
)
.await
.map_err(|e| ActionSinkError::Database(e.to_string()))?;
// 5. Post-persist side effects (fan-out, search, audit)
// Only if actually inserted (idempotency guard).
if was_inserted {
let _ = dispatch_persistent_event(
&tenant,
&state,
&stored_event,
kind_u32,
&author_pubkey_hex,
None,
)
.await;
}
Ok(event_id_hex)
})
}
}
#[cfg(test)]
mod tests {
use super::*;
fn m(name: &str, pubkey: &str) -> (String, String) {
(name.to_string(), pubkey.to_string())
}
// A 64-char hex pubkey built from a single repeated nibble, for readable tests.
fn pk(nibble: char) -> String {
std::iter::repeat_n(nibble, 64).collect()
}
#[test]
fn resolves_exact_member_name() {
let members = vec![m("Robby", &pk('a'))];
assert_eq!(
resolve_mention_pubkeys("heads up @Robby — please take a look", &members),
vec![pk('a')]
);
}
#[test]
fn matches_case_insensitively() {
let members = vec![m("Robby", &pk('a'))];
assert_eq!(
resolve_mention_pubkeys("ping @robby", &members),
vec![pk('a')]
);
}
#[test]
fn ignores_non_member_and_bare_at() {
let members = vec![m("Robby", &pk('a'))];
assert!(resolve_mention_pubkeys("hey @Stranger and @", &members).is_empty());
}
#[test]
fn greedy_longest_binds_full_name_not_prefix() {
// Both "Will" and "Will Pfleger" are members. `@Will Pfleger` must bind
// Pfleger's key only; a bare `@Will` binds Will.
let members = vec![m("Will", &pk('1')), m("Will Pfleger", &pk('2'))];
assert_eq!(
resolve_mention_pubkeys("cc @Will Pfleger on this", &members),
vec![pk('2')]
);
assert_eq!(
resolve_mention_pubkeys("cc @Will on this", &members),
vec![pk('1')]
);
}
#[test]
fn at_mid_token_does_not_match() {
// `@` must sit at a left boundary (start / whitespace / `(`). An email-ish
// or mid-token `@` (`alice@Robby`) must not wake Robby.
let members = vec![m("Robby", &pk('a'))];
assert!(resolve_mention_pubkeys("alice@Robby", &members).is_empty());
}
#[test]
fn prefix_member_does_not_match_inside_longer_word() {
// "Sam" is a member; `@Sami` (no "Sami" member) must not wake Sam.
let members = vec![m("Sam", &pk('3'))];
assert!(resolve_mention_pubkeys("hi @Sami", &members).is_empty());
}
#[test]
fn name_with_spaces_and_punctuation() {
let members = vec![m("Lep (Subagent)", &pk('4'))];
assert_eq!(
resolve_mention_pubkeys("@Lep (Subagent) take it", &members),
vec![pk('4')]
);
}
#[test]
fn em_dash_terminates_name() {
// Generated prose often writes `@Name—text` with no space.
let members = vec![m("Robby", &pk('a'))];
assert_eq!(
resolve_mention_pubkeys("@Robby—please look", &members),
vec![pk('a')]
);
}
#[test]
fn non_ascii_member_name() {
let members = vec![m("Zoë", &pk('5'))];
assert_eq!(
resolve_mention_pubkeys("welcome @Zoë!", &members),
vec![pk('5')]
);
}
#[test]
fn lowercase_expansion_does_not_shift_later_mentions() {
// Regression (Wren's redteam counterexample): `İ` (U+0130) lowercases to
// TWO code points (`i` + U+0307). A design that pre-lowercases the whole
// text and indexes it in parallel with the original chars desyncs after
// the expansion, dropping every later valid mention. `@İ @Robby` must
// resolve BOTH members, in order.
let members = vec![m("İ", &pk('c')), m("Robby", &pk('a'))];
assert_eq!(
resolve_mention_pubkeys("@İ @Robby", &members),
vec![pk('c'), pk('a')]
);
}
#[test]
fn sharp_s_matches_case_insensitively() {
// `ẞ` (U+1E9E capital sharp s) lowercases to `ß` (U+00DF) — a single
// char, NOT `ss` (that's uppercase/full-case-fold behavior, not
// `char::to_lowercase`). Covers non-ASCII case-insensitive matching, and
// that a later mention still resolves after it.
let members = vec![m("ẞ", &pk('d')), m("Max", &pk('b'))];
assert_eq!(
resolve_mention_pubkeys("@ẞ and @Max", &members),
vec![pk('d'), pk('b')]
);
}
// Adversarial rows from Quinn's re-review (the two `ẞ→ss`-premised ones were
// dropped as vacuous — `ẞ` lowercases to `ß`, one char, so it never inverts
// original-vs-folded length; only `İ` does).
#[test]
fn combining_mark_in_name_matches() {
// A name carrying a combining mark (`é` as `e` + U+0301) matches the same
// sequence in text (1:1 folding) and terminates cleanly.
let members = vec![m("Jos\u{0065}\u{0301}", &pk('4'))]; // "José" decomposed
assert_eq!(
resolve_mention_pubkeys("hi @Jos\u{0065}\u{0301}!", &members),
vec![pk('4')]
);
}
#[test]
fn expanding_name_at_trailing_boundary() {
// Expansion at the very end: `@İ` with nothing after must match, and
// `@İx` (x extends the name, no `İx` member) must NOT match `İ`.
let members = vec![m("İ", &pk('5'))];
assert_eq!(resolve_mention_pubkeys("@İ", &members), vec![pk('5')]);
assert!(resolve_mention_pubkeys("@İx", &members).is_empty());
}
#[test]
fn back_to_back_at_is_one_mention() {
// `@İ@Robby`: the second `@` is preceded by a name char (`İ`), so it is
// NOT at a left boundary — same rule as `alice@Robby`. Back-to-back
// `@a@b` is intentionally one mention; a separator is required to wake
// both. The expanding first name (`İ` → 2 folded chars) also proves the
// span accounting stays in original coordinates.
let members = vec![m("İ", &pk('5')), m("Robby", &pk('a'))];
assert_eq!(resolve_mention_pubkeys("@İ@Robby", &members), vec![pk('5')]);
// ASCII control: same shape, same outcome — it's the boundary rule, not
// a Unicode span-accounting bug.
let ascii = vec![m("Sam", &pk('6')), m("Robby", &pk('a'))];
assert_eq!(resolve_mention_pubkeys("@Sam@Robby", &ascii), vec![pk('6')]);
// With a separator, both wake.
assert_eq!(
resolve_mention_pubkeys("@İ @Robby", &members),
vec![pk('5'), pk('a')]
);
}
#[test]
fn ambiguous_name_wakes_no_one() {
// Six "Fizz" agents (real team case) with distinct pubkeys → tag none.
let members = vec![
m("Fizz", &pk('6')),
m("Fizz", &pk('7')),
m("Fizz", &pk('8')),
];
assert!(resolve_mention_pubkeys("@Fizz status?", &members).is_empty());
}
#[test]
fn duplicate_name_same_pubkey_is_not_ambiguous() {
// Same identity listed twice (e.g. two channels) is not a conflict.
let members = vec![m("Fizz", &pk('6')), m("Fizz", &pk('6'))];
assert_eq!(resolve_mention_pubkeys("@Fizz go", &members), vec![pk('6')]);
}
#[test]
fn dedupes_repeated_mentions_in_first_appearance_order() {
let members = vec![m("Robby", &pk('a')), m("Max", &pk('b'))];
assert_eq!(
resolve_mention_pubkeys("@Max then @Robby then @Max again", &members),
vec![pk('b'), pk('a')]
);
}
}
#[cfg(test)]
mod integration_tests {
//! Regression test for `e3661764` / `7899c1a8`: a workflow `send_message`
//! that mentions a channel member by name (`@Name`) must emit a `p` tag for
//! that member so ACP agent wake (`event_mentions_agent`, p-tag gated) fires.
//!
//! Postgres-gated like the other DB-backed relay tests. Run with:
//! `cargo test -p buzz-relay --lib workflow_sink -- --ignored`
use super::*;
use buzz_core::channel::{ChannelType, ChannelVisibility, MemberRole};
use buzz_db::CreateCommunityWithOwnerResult;
use std::sync::Arc;
/// Real-PG state mirroring `handlers::event::tests::test_state_with_redis_url`.
async fn test_state() -> Arc<AppState> {
let mut config = crate::config::Config::from_env().expect("default config loads");
config.require_relay_membership = false;
config.redis_url = "redis://127.0.0.1:1".to_string();
let pool = sqlx::PgPool::connect_lazy(&config.database_url).expect("lazy pg pool");
let db = buzz_db::Db::from_pool(pool.clone());
let redis_pool = deadpool_redis::Config::from_url(&config.redis_url)
.create_pool(Some(deadpool_redis::Runtime::Tokio1))
.expect("redis pool");
let pubsub = Arc::new(
buzz_pubsub::PubSubManager::new(&config.redis_url, redis_pool.clone())
.await
.expect("pubsub manager"),
);
let audit = buzz_audit::AuditService::new(pool.clone());
let auth = buzz_auth::AuthService::new(config.auth.clone());
let search = buzz_search::SearchService::new(pool.clone());
let workflow_engine = Arc::new(buzz_workflow::WorkflowEngine::new(
db.clone(),
buzz_workflow::WorkflowConfig::default(),
));
let media_storage = buzz_media::MediaStorage::new(&config.media).expect("media storage");
let (state, _audit_shutdown) = AppState::new(
config,
db,
redis_pool,
audit,
pubsub,
auth,
search,
workflow_engine,
nostr::Keys::generate(),
media_storage,
);
Arc::new(state)
}
#[tokio::test]
#[ignore = "requires Postgres"]
async fn workflow_send_message_p_tags_mentioned_member() {
let state = test_state().await;
let author = nostr::Keys::generate();
let author_hex = author.public_key().to_hex();
let agent = nostr::Keys::generate();
let agent_hex = agent.public_key().to_hex();
let agent_bytes = agent.public_key().to_bytes().to_vec();
let host = format!("wf-ptag-{}.example", uuid::Uuid::new_v4().simple());
let community = match state
.db
.create_community_with_owner(&host, &author_hex)
.await
.expect("create community")
{
CreateCommunityWithOwnerResult::Created(rec) => rec.id,
other => panic!("expected fresh community, got {other:?}"),
};
// Open channel; the creator (author) is bootstrapped as an owner-member.
let channel = state
.db
.create_channel(
community,
"wf-ptag",
ChannelType::Stream,
ChannelVisibility::Open,
None,
&author.public_key().to_bytes(),
None,
)
.await
.expect("create channel");
// The mentioned agent is a real member with a resolvable display name.
state
.db
.ensure_user(community, &agent_bytes)
.await
.expect("ensure agent user row");
state
.db
.update_user_profile(community, &agent_bytes, Some("Robby"), None, None, None)
.await
.expect("set agent display name");
state
.db
.add_member(
community,
channel.id,
&agent_bytes,
MemberRole::Bot,
Some(&author.public_key().to_bytes()),
)
.await
.expect("add agent member");
let sink = RelayActionSink::new(&state);
let event_id_hex = sink
.send_message(
community,
&channel.id.to_string(),
"heads up @Robby — please take a look",
&author_hex,
)
.await
.expect("send_message");
let id_bytes = nostr::EventId::from_hex(&event_id_hex)
.expect("event id")
.as_bytes()
.to_vec();
let stored = state
.db
.get_event_by_id(community, &id_bytes)
.await
.expect("query event")
.expect("event persisted");
let p_tag_targets: Vec<&str> = stored
.event
.tags
.iter()
.filter(|t| t.as_slice().first().map(|s| s.as_str()) == Some("p"))
.filter_map(|t| t.as_slice().get(1).map(|s| s.as_str()))
.collect();
assert!(
p_tag_targets.contains(&author_hex.as_str()),
"author should still be attributed via p tag; got {p_tag_targets:?}"
);
assert!(
p_tag_targets.contains(&agent_hex.as_str()),
"mentioned member {agent_hex} must be p-tagged so it wakes; got {p_tag_targets:?}"
);
}
}