feat(workflow): implement the send_dm action (WF-07)
Design discussion: #2628 — opened per CONTRIBUTING's significant-change guidance. This PR is in draft pending that discussion; the implementation is here for reference.
Problem
The send_dm workflow action is fully wired through the schema
(ActionDef::SendDm) and template resolution, but its dispatch arm is a
maintainer TODO — crates/buzz-workflow/src/executor.rs:582
// TODO (WF-07): emit DM event. — so every workflow step that uses it fails
with WorkflowError::NotImplemented("SendDm").
Scope is deliberately send_dm only; the neighbouring SetChannelTopic TODO
at executor.rs:588 is untouched (one action per PR).
Fix
Mirrors the two patterns the codebase already establishes:
- Emission follows the sibling
SendMessagearm exactly: the executor loads the run + workflow (community-scoped, same wrong-community rationale as SendMessage) to obtain the owner pubkey, then calls a newActionSink::send_dm. The relay-sideRelayActionSink::send_dmbuilds a kind:9 event, signs with the relay keypair, attributes the owner viaptag, persists viainsert_event_with_thread_metadata(top-level: depth 0), and runsdispatch_persistent_eventunder the run's owning community — the same flow assend_messageincrates/buzz-relay/src/workflow_sink.rs. - DM encoding: Buzz DMs are private channels (
channel_type='dm'), not NIP-17 wraps. The sink opens (or reuses) the owner↔recipient DM channel via the same idempotentopen_dmpath the kind:41010handle_dm_opencommand handler uses, and mirrors that handler's post-open side effects: on create — channels-created metric, membership-cache invalidation,dm_createdsystem message, group discovery events, membership notifications; on re-open — the owner's NIP-DV visibility snapshot refresh.
Details:
- The recipient is
p-tagged on the message event in addition to the owner attribution tag: agent wake (event_mentions_agentin buzz-acp) and push delivery arep-tag gated, so an un-tagged DM would never notify anyone. - New pure helper
resolve_dm_recipientnormalizes thetofield to lowercase hex before the sink call (the executor-side analogue ofresolve_send_message_channel). It accepts hex or bech32npub— the| npubtemplate filter produces the latter — and rejects empty, malformed, and unresolved{{...}}literal values. - Self-DM (
to== workflow owner) is rejected asInvalidInputrather than attempting a 1-participant channel (create_dmrequires >= 2 participants). - Unlike
SendMessage, the DM body is deliberately not written to relay logs (DMs are private; logs are not). buzz:workflowtag is carried, preserving the recursive-trigger guard.
Design note for reviewers: signing follows the established workflow pattern —
relay keypair signs, owner attributed via p tag. If WF-07 intended
owner-key-signed DMs or a dedicated workflow identity instead, the sink method
is the single place to change.
Test evidence
cargo fmt -p buzz-workflow -p buzz-relay— clean.cargo clippy -p buzz-workflow -p buzz-relay --all-targets— no warnings.-p buzz-workflow—fmt/clippy/nextestall green (includes this PR's newresolve_dm_recipientunit tests).-p buzz-relay—fmt+clippygreen;nextestpasses except 8 environment-dependent failures inapi::media::*/api::admin::feedback_*fixtures (they want the sidecar/storage setup — the genuinely DB-gated tests show asignored, requires Postgresin the same run, so these are a different class). They fail identically on unpatchedmain, and none are in files this change touches (the diff only modifiesworkflow_sink.rs,action_sink.rs,executor.rs). Reproduce:cargo nextest run -p buzz-relayonmainshows the same 8.- New unit tests (executor.rs, same pure-helper pattern as the
resolve_send_message_channeltests): hex accepted, npub normalized to hex, uppercase normalized, whitespace trimmed, empty/whitespace rejected, malformed + unresolved-template literals rejected, andresolve_step_templatesresolving bothsend_dmfields from a parsed YAML step. - New Postgres-gated integration test (workflow_sink.rs, mirroring
workflow_send_message_p_tags_mentioned_member, run withcargo test -p buzz-relay --lib workflow_sink -- --ignored):workflow_send_dm_opens_channel_and_p_tags_recipientasserts the emitted event lands in achannel_type='dm'/visibility='private'channel, both participants arep-tagged, a second send reuses the same channel (idempotent open), and self-DM is rejected.
Links
- TODO being implemented:
crates/buzz-workflow/src/executor.rs:582(TODO (WF-07): emit DM event) - Sibling emission path mirrored:
RelayActionSink::send_message(crates/buzz-relay/src/workflow_sink.rs) - DM open path reused:
buzz_db::dm::open_dm/handle_dm_open(crates/buzz-relay/src/handlers/command_executor.rs)
Converted to draft pending the design discussion in #2628, per CONTRIBUTING's guidance to open an issue first for significant changes. I'll mark it ready once the approach — in particular the signing-key question raised there — is settled.