From b42fe004ee9ef52691d39bfed0b64154d33a71b2 Mon Sep 17 00:00:00 2001 From: Logan Johnson Date: Tue, 25 Aug 2026 16:39:57 -0400 Subject: [PATCH] feat(workflows): define signed owner lifecycle protocol Freeze one signed command and result identity for start, pause, resume, cancel, and restore. Keep the owner-command surface pure and dormant for downstream state-machine and executor work. Co-authored-by: Other Brother Darryl Signed-off-by: Logan Johnson --- crates/buzz-core/src/kind.rs | 7 + crates/buzz-core/src/lib.rs | 2 + .../buzz-core/src/workflow_owner_command.rs | 552 ++++++++++++++++++ crates/buzz-sdk/src/builders.rs | 177 +++++- crates/buzz-sdk/src/lib.rs | 4 + 5 files changed, 741 insertions(+), 1 deletion(-) create mode 100644 crates/buzz-core/src/workflow_owner_command.rs diff --git a/crates/buzz-core/src/kind.rs b/crates/buzz-core/src/kind.rs index 3c6f1d5913..e383eada1f 100644 --- a/crates/buzz-core/src/kind.rs +++ b/crates/buzz-core/src/kind.rs @@ -440,6 +440,10 @@ pub const KIND_WINDOW_BOUNDS: u32 = 39006; /// Workflow definition (parameterized replaceable, d=workflow_uuid). pub const KIND_WORKFLOW_DEF: u32 = 30620; +/// Owner-signed lifecycle command targeting a managed workflow. +pub const KIND_WORKFLOW_OWNER_COMMAND: u32 = 46021; +/// Agent-signed terminal result for a workflow owner command. +pub const KIND_WORKFLOW_OWNER_RESULT: u32 = 46022; /// NIP-DV: per-viewer DM visibility snapshot (relay-signed, parameterized /// replaceable, d=viewer_pubkey). Carries one `h` tag per DM the viewer has @@ -726,6 +730,8 @@ pub const ALL_KINDS: &[u32] = &[ KIND_MEMBER_REMOVED_NOTIFICATION, KIND_AGENT_TURN_METRIC, KIND_WORKFLOW_DEF, + KIND_WORKFLOW_OWNER_COMMAND, + KIND_WORKFLOW_OWNER_RESULT, KIND_LONG_FORM, KIND_USER_STATUS, KIND_READ_STATE, @@ -820,6 +826,7 @@ pub const fn is_command_kind(kind: u32) -> bool { | KIND_DM_ADD_MEMBER | KIND_DM_HIDE | KIND_WORKFLOW_TRIGGER + | KIND_WORKFLOW_OWNER_COMMAND | KIND_APPROVAL_GRANT | KIND_APPROVAL_DENY ) diff --git a/crates/buzz-core/src/lib.rs b/crates/buzz-core/src/lib.rs index 36dc772da3..5aac988db9 100644 --- a/crates/buzz-core/src/lib.rs +++ b/crates/buzz-core/src/lib.rs @@ -42,6 +42,8 @@ pub mod relay; pub mod tenant; /// Schnorr signature and event ID verification. pub mod verification; +/// Signed owner lifecycle commands for managed workflows. +pub mod workflow_owner_command; pub use error::VerificationError; pub use event::StoredEvent; diff --git a/crates/buzz-core/src/workflow_owner_command.rs b/crates/buzz-core/src/workflow_owner_command.rs new file mode 100644 index 0000000000..e9c5cf860c --- /dev/null +++ b/crates/buzz-core/src/workflow_owner_command.rs @@ -0,0 +1,552 @@ +//! Signed owner-command protocol for managed workflows. +//! +//! This module contains only the pure wire contract. Relay admission, +//! persistence, and agent execution are intentionally downstream consumers. + +use nostr::{Event, EventId, PublicKey}; +use serde::{Deserialize, Serialize}; +use thiserror::Error; +use uuid::Uuid; + +use crate::kind::{KIND_WORKFLOW_DEF, KIND_WORKFLOW_OWNER_COMMAND, KIND_WORKFLOW_OWNER_RESULT}; + +/// Maximum UTF-8 byte length of a terminal result reason. +pub const WORKFLOW_OWNER_MAX_REASON_BYTES: usize = 4 * 1024; + +/// Operation requested by a workflow owner. +/// +/// These five operations deliberately share one command shape. In particular, +/// restore is not a relay-side special case: it is an ordinary command that +/// the downstream capability-gated lifecycle executor must execute. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum WorkflowOwnerOperation { + /// Start a workflow run. + Start, + /// Pause workflow execution. + Pause, + /// Resume paused workflow execution. + Resume, + /// Cancel workflow execution. + Cancel, + /// Restore a retired workflow. + Restore, +} + +impl WorkflowOwnerOperation { + /// Return the canonical wire spelling. + pub const fn as_str(self) -> &'static str { + match self { + Self::Start => "start", + Self::Pause => "pause", + Self::Resume => "resume", + Self::Cancel => "cancel", + Self::Restore => "restore", + } + } + + fn parse(value: &str) -> Option { + match value { + "start" => Some(Self::Start), + "pause" => Some(Self::Pause), + "resume" => Some(Self::Resume), + "cancel" => Some(Self::Cancel), + "restore" => Some(Self::Restore), + _ => None, + } + } +} + +/// Terminal status reported by an agent after executing an owner command. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum WorkflowOwnerResultStatus { + /// The agent applied the requested operation. + Applied, + /// The agent rejected the operation without applying it. + Rejected, +} + +impl WorkflowOwnerResultStatus { + /// Return the canonical wire spelling. + pub const fn as_str(self) -> &'static str { + match self { + Self::Applied => "applied", + Self::Rejected => "rejected", + } + } + + fn parse(value: &str) -> Option { + match value { + "applied" => Some(Self::Applied), + "rejected" => Some(Self::Rejected), + _ => None, + } + } +} + +/// Signed owner-command content. +/// +/// All five lifecycle operations use this same body. Identity and replay +/// binding live in the signed tags, not in operation-specific cargo. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct WorkflowOwnerCommandBody { + /// Requested operation. + pub operation: WorkflowOwnerOperation, +} + +/// Signed terminal-result content. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct WorkflowOwnerResultBody { + /// Terminal result status. + pub status: WorkflowOwnerResultStatus, + /// Optional machine-readable or human-readable terminal context. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub reason: Option, +} + +/// Parsed owner-signed command identity and target. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct WorkflowOwnerCommand { + /// Event ID of the signed request. + pub event_id: EventId, + /// Human owner that signed the request. + pub owner_pubkey: PublicKey, + /// Replay identity carried by the command `d` tag. + pub command_id: Uuid, + /// Immutable agent author of the target workflow coordinate. + pub agent_pubkey: PublicKey, + /// Workflow d-tag in the target coordinate. + pub workflow_id: Uuid, + /// Explicit command recipient; equal to the coordinate agent. + pub recipient: PublicKey, + /// Exact owner-signed kind-30620 revision being managed. + pub expected_revision: EventId, + /// Requested operation. + pub operation: WorkflowOwnerOperation, +} + +impl WorkflowOwnerCommand { + /// Return the canonical parameterized workflow coordinate. + pub fn workflow_coordinate(&self) -> String { + format!( + "{KIND_WORKFLOW_DEF}:{}:{}", + self.agent_pubkey.to_hex(), + self.workflow_id + ) + } +} + +/// Parsed agent-signed terminal-result identity and payload. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct WorkflowOwnerResult { + /// Event ID of the signed result. + pub event_id: EventId, + /// Agent that signed and executed the result. + pub agent_pubkey: PublicKey, + /// Human owner receiving the result. + pub owner_pubkey: PublicKey, + /// Replay identity copied from the command `d` tag. + pub command_id: Uuid, + /// Workflow agent author from the target coordinate. + pub target_agent_pubkey: PublicKey, + /// Workflow d-tag in the target coordinate. + pub workflow_id: Uuid, + /// Exact revision the command targeted. + pub expected_revision: EventId, + /// Operation executed by the agent. + pub operation: WorkflowOwnerOperation, + /// Terminal result status. + pub status: WorkflowOwnerResultStatus, + /// Optional terminal reason. + pub reason: Option, +} + +/// Canonical failures while parsing owner-command protocol events. +#[derive(Debug, Error, PartialEq, Eq)] +pub enum WorkflowOwnerError { + /// Event kind did not match the requested protocol type. + #[error("unexpected workflow owner event kind: {0}")] + UnexpectedKind(u16), + /// Nostr signature verification failed. + #[error("invalid workflow owner event signature: {0}")] + InvalidSignature(String), + /// A required tag was missing. + #[error("missing workflow owner {0} tag")] + MissingTag(&'static str), + /// A tag occurred more than once. + #[error("duplicate workflow owner {0} tag")] + DuplicateTag(&'static str), + /// A tag did not have its canonical two-field shape. + #[error("workflow owner {0} tag has a non-canonical shape")] + InvalidTagShape(&'static str), + /// An unrecognized tag was present. + #[error("unexpected workflow owner tag: {0}")] + UnexpectedTag(String), + /// The replay identity was malformed or nil. + #[error("invalid workflow owner command id")] + InvalidCommandId, + /// The workflow coordinate was malformed. + #[error("invalid workflow owner workflow coordinate")] + InvalidWorkflowCoordinate, + /// The coordinate used a kind other than workflow definition kind 30620. + #[error("workflow owner coordinate must target kind 30620")] + WrongWorkflowKind, + /// The coordinate agent pubkey was malformed. + #[error("invalid workflow owner agent pubkey")] + InvalidAgentPubkey, + /// The coordinate workflow UUID was malformed or nil. + #[error("invalid workflow owner workflow id")] + InvalidWorkflowId, + /// The exact definition event ID was malformed. + #[error("invalid workflow owner expected revision")] + InvalidExpectedRevision, + /// A command recipient was malformed. + #[error("invalid workflow owner command recipient")] + InvalidRecipient, + /// The explicit recipient did not equal the workflow coordinate agent. + #[error("workflow owner command recipient must equal target agent")] + RecipientMismatch, + /// A result was not signed by the coordinate agent. + #[error("workflow owner result signer must equal target agent")] + ResultSignerMismatch, + /// Signed JSON content could not be decoded. + #[error("invalid workflow owner event content: {0}")] + InvalidContent(String), + /// A result reason exceeded the protocol limit. + #[error("workflow owner result reason exceeds {WORKFLOW_OWNER_MAX_REASON_BYTES} bytes")] + ReasonTooLarge, + /// A result status in content and tag disagreed. + #[error("workflow owner result status tag disagrees with content")] + ResultStatusMismatch, + /// An operation was malformed or unknown. + #[error("invalid workflow owner operation")] + InvalidOperation, + /// A result status was malformed or unknown. + #[error("invalid workflow owner result status")] + InvalidStatus, +} + +/// Parse and structurally validate an owner-signed command event. +pub fn parse_owner_command(event: &Event) -> Result { + if event.kind.as_u16() as u32 != KIND_WORKFLOW_OWNER_COMMAND { + return Err(WorkflowOwnerError::UnexpectedKind(event.kind.as_u16())); + } + verify_signature(event)?; + let tags = canonical_tags(event, &["d", "a", "revision", "p"])?; + let command_id = parse_command_id(tag_value(&tags, "d")?)?; + let (agent_pubkey, workflow_id) = parse_workflow_coordinate(tag_value(&tags, "a")?)?; + let expected_revision = parse_event_id(tag_value(&tags, "revision")?)?; + let recipient = PublicKey::from_hex(tag_value(&tags, "p")?) + .map_err(|_| WorkflowOwnerError::InvalidRecipient)?; + if recipient != agent_pubkey { + return Err(WorkflowOwnerError::RecipientMismatch); + } + let body: WorkflowOwnerCommandBody = serde_json::from_str(&event.content) + .map_err(|error| WorkflowOwnerError::InvalidContent(error.to_string()))?; + Ok(WorkflowOwnerCommand { + event_id: event.id, + owner_pubkey: event.pubkey, + command_id, + agent_pubkey, + workflow_id, + recipient, + expected_revision, + operation: body.operation, + }) +} + +/// Parse and structurally validate an agent-signed terminal result event. +pub fn parse_owner_result(event: &Event) -> Result { + if event.kind.as_u16() as u32 != KIND_WORKFLOW_OWNER_RESULT { + return Err(WorkflowOwnerError::UnexpectedKind(event.kind.as_u16())); + } + verify_signature(event)?; + let tags = canonical_tags(event, &["d", "a", "revision", "p", "operation", "status"])?; + let command_id = parse_command_id(tag_value(&tags, "d")?)?; + let (target_agent_pubkey, workflow_id) = parse_workflow_coordinate(tag_value(&tags, "a")?)?; + if event.pubkey != target_agent_pubkey { + return Err(WorkflowOwnerError::ResultSignerMismatch); + } + let owner_pubkey = PublicKey::from_hex(tag_value(&tags, "p")?) + .map_err(|_| WorkflowOwnerError::InvalidRecipient)?; + let expected_revision = parse_event_id(tag_value(&tags, "revision")?)?; + let operation = WorkflowOwnerOperation::parse(tag_value(&tags, "operation")?) + .ok_or(WorkflowOwnerError::InvalidOperation)?; + let status_tag = WorkflowOwnerResultStatus::parse(tag_value(&tags, "status")?) + .ok_or(WorkflowOwnerError::InvalidStatus)?; + let body: WorkflowOwnerResultBody = serde_json::from_str(&event.content) + .map_err(|error| WorkflowOwnerError::InvalidContent(error.to_string()))?; + if body.status != status_tag { + return Err(WorkflowOwnerError::ResultStatusMismatch); + } + if body + .reason + .as_deref() + .is_some_and(|reason| reason.len() > WORKFLOW_OWNER_MAX_REASON_BYTES) + { + return Err(WorkflowOwnerError::ReasonTooLarge); + } + Ok(WorkflowOwnerResult { + event_id: event.id, + agent_pubkey: event.pubkey, + owner_pubkey, + command_id, + target_agent_pubkey, + workflow_id, + expected_revision, + operation, + status: status_tag, + reason: body.reason, + }) +} + +fn verify_signature(event: &Event) -> Result<(), WorkflowOwnerError> { + event + .verify() + .map_err(|error| WorkflowOwnerError::InvalidSignature(error.to_string())) +} + +fn canonical_tags<'a>( + event: &'a Event, + allowed: &[&'static str], +) -> Result, WorkflowOwnerError> { + let mut found = Vec::with_capacity(allowed.len()); + for tag in event.tags.iter() { + let values = tag.as_slice(); + let name = values + .first() + .map(String::as_str) + .ok_or_else(|| WorkflowOwnerError::UnexpectedTag(String::new()))?; + let Some(&canonical_name) = allowed.iter().find(|candidate| **candidate == name) else { + return Err(WorkflowOwnerError::UnexpectedTag(name.to_owned())); + }; + if found + .iter() + .any(|(candidate, _)| *candidate == canonical_name) + { + return Err(WorkflowOwnerError::DuplicateTag(canonical_name)); + } + if values.len() != 2 { + return Err(WorkflowOwnerError::InvalidTagShape(canonical_name)); + } + found.push((canonical_name, values)); + } + for &name in allowed { + if !found.iter().any(|(candidate, _)| *candidate == name) { + return Err(WorkflowOwnerError::MissingTag(name)); + } + } + Ok(found) +} + +fn tag_value<'a>( + tags: &[(&'static str, &'a [String])], + name: &'static str, +) -> Result<&'a str, WorkflowOwnerError> { + tags.iter() + .find(|(candidate, _)| *candidate == name) + .map(|(_, values)| values[1].as_str()) + .ok_or(WorkflowOwnerError::MissingTag(name)) +} + +fn parse_command_id(value: &str) -> Result { + let id = Uuid::parse_str(value).map_err(|_| WorkflowOwnerError::InvalidCommandId)?; + if id.is_nil() { + return Err(WorkflowOwnerError::InvalidCommandId); + } + Ok(id) +} + +fn parse_workflow_coordinate(value: &str) -> Result<(PublicKey, Uuid), WorkflowOwnerError> { + let mut parts = value.split(':'); + let kind = parts + .next() + .and_then(|part| part.parse::().ok()) + .ok_or(WorkflowOwnerError::InvalidWorkflowCoordinate)?; + if kind != KIND_WORKFLOW_DEF { + return Err(WorkflowOwnerError::WrongWorkflowKind); + } + let agent = PublicKey::from_hex(parts.next().unwrap_or_default()) + .map_err(|_| WorkflowOwnerError::InvalidAgentPubkey)?; + let workflow_id = Uuid::parse_str(parts.next().unwrap_or_default()) + .map_err(|_| WorkflowOwnerError::InvalidWorkflowId)?; + if workflow_id.is_nil() || parts.next().is_some() { + return Err(WorkflowOwnerError::InvalidWorkflowId); + } + Ok((agent, workflow_id)) +} + +fn parse_event_id(value: &str) -> Result { + EventId::from_hex(value).map_err(|_| WorkflowOwnerError::InvalidExpectedRevision) +} + +#[cfg(test)] +mod tests { + use nostr::{EventBuilder, Keys, Kind, Tag}; + + use super::*; + + fn command(owner: &Keys, agent: &Keys, operation: WorkflowOwnerOperation) -> Event { + let command_id = Uuid::new_v4(); + let workflow_id = Uuid::new_v4(); + let revision = EventId::from_hex(&"11".repeat(32)).unwrap(); + let body = WorkflowOwnerCommandBody { operation }; + EventBuilder::new( + Kind::Custom(KIND_WORKFLOW_OWNER_COMMAND as u16), + serde_json::to_string(&body).unwrap(), + ) + .tags([ + Tag::parse(["d", &command_id.to_string()]).unwrap(), + Tag::parse([ + "a", + &format!( + "{KIND_WORKFLOW_DEF}:{}:{workflow_id}", + agent.public_key().to_hex() + ), + ]) + .unwrap(), + Tag::parse(["revision", &revision.to_hex()]).unwrap(), + Tag::parse(["p", &agent.public_key().to_hex()]).unwrap(), + ]) + .sign_with_keys(owner) + .unwrap() + } + + #[test] + fn every_operation_has_the_same_command_identity_and_body_shape() { + let owner = Keys::generate(); + let agent = Keys::generate(); + for operation in [ + WorkflowOwnerOperation::Start, + WorkflowOwnerOperation::Pause, + WorkflowOwnerOperation::Resume, + WorkflowOwnerOperation::Cancel, + WorkflowOwnerOperation::Restore, + ] { + let event = command(&owner, &agent, operation); + let parsed = parse_owner_command(&event).unwrap(); + assert_eq!(parsed.operation, operation); + assert_eq!(parsed.owner_pubkey, owner.public_key()); + assert_eq!(parsed.agent_pubkey, agent.public_key()); + assert_eq!(parsed.recipient, agent.public_key()); + assert_eq!(event.tags.len(), 4); + } + } + + #[test] + fn rejects_unknown_or_duplicate_tags_and_recipient_mismatch() { + let owner = Keys::generate(); + let agent = Keys::generate(); + let other = Keys::generate(); + let event = command(&owner, &agent, WorkflowOwnerOperation::Start); + let mut tags = event.tags.to_vec(); + tags.push(Tag::parse(["d", &Uuid::new_v4().to_string()]).unwrap()); + let event = EventBuilder::new(event.kind, event.content) + .tags(tags) + .sign_with_keys(&owner) + .unwrap(); + assert_eq!( + parse_owner_command(&event), + Err(WorkflowOwnerError::DuplicateTag("d")) + ); + + let event = command(&owner, &agent, WorkflowOwnerOperation::Pause); + let mut tags = event.tags.to_vec(); + tags.pop(); + tags.push(Tag::parse(["p", &other.public_key().to_hex()]).unwrap()); + let event = EventBuilder::new(event.kind, event.content) + .tags(tags) + .sign_with_keys(&owner) + .unwrap(); + assert_eq!( + parse_owner_command(&event), + Err(WorkflowOwnerError::RecipientMismatch) + ); + + let event = command(&owner, &agent, WorkflowOwnerOperation::Resume); + let mut tags = event.tags.to_vec(); + tags.push(Tag::parse(["private", "leak"]).unwrap()); + let event = EventBuilder::new(event.kind, event.content) + .tags(tags) + .sign_with_keys(&owner) + .unwrap(); + assert_eq!( + parse_owner_command(&event), + Err(WorkflowOwnerError::UnexpectedTag("private".into())) + ); + } + + #[test] + fn result_requires_agent_signature_and_matching_status() { + let owner = Keys::generate(); + let agent = Keys::generate(); + let command_event = command(&owner, &agent, WorkflowOwnerOperation::Restore); + let command = parse_owner_command(&command_event).unwrap(); + let body = WorkflowOwnerResultBody { + status: WorkflowOwnerResultStatus::Applied, + reason: None, + }; + let event = EventBuilder::new( + Kind::Custom(KIND_WORKFLOW_OWNER_RESULT as u16), + serde_json::to_string(&body).unwrap(), + ) + .tags([ + Tag::parse(["d", &command.command_id.to_string()]).unwrap(), + Tag::parse(["a", &command.workflow_coordinate()]).unwrap(), + Tag::parse(["revision", &command.expected_revision.to_hex()]).unwrap(), + Tag::parse(["p", &command.owner_pubkey.to_hex()]).unwrap(), + Tag::parse(["operation", command.operation.as_str()]).unwrap(), + Tag::parse(["status", "applied"]).unwrap(), + ]) + .sign_with_keys(&agent) + .unwrap(); + let result = parse_owner_result(&event).unwrap(); + assert_eq!(result.agent_pubkey, agent.public_key()); + assert_eq!(result.owner_pubkey, owner.public_key()); + assert_eq!(result.operation, WorkflowOwnerOperation::Restore); + + let mut tags = event.tags.to_vec(); + tags[5] = Tag::parse(["status", "rejected"]).unwrap(); + let event = EventBuilder::new(event.kind, event.content) + .tags(tags) + .sign_with_keys(&agent) + .unwrap(); + assert_eq!( + parse_owner_result(&event), + Err(WorkflowOwnerError::ResultStatusMismatch) + ); + } + + #[test] + fn rejects_unknown_operation_and_unknown_body_fields() { + let owner = Keys::generate(); + let agent = Keys::generate(); + let event = command(&owner, &agent, WorkflowOwnerOperation::Cancel); + let event = EventBuilder::new(event.kind, r#"{"operation":"update"}"#) + .tags(event.tags) + .sign_with_keys(&owner) + .unwrap(); + assert_eq!( + parse_owner_command(&event), + Err(WorkflowOwnerError::InvalidContent( + "unknown variant `update`, expected one of `start`, `pause`, `resume`, `cancel`, `restore`".into() + )) + ); + + let event = command(&owner, &agent, WorkflowOwnerOperation::Start); + let event = EventBuilder::new( + event.kind, + r#"{"operation":"start","yaml_definition":"no"}"#, + ) + .tags(event.tags) + .sign_with_keys(&owner) + .unwrap(); + assert!(matches!( + parse_owner_command(&event), + Err(WorkflowOwnerError::InvalidContent(_)) + )); + } +} diff --git a/crates/buzz-sdk/src/builders.rs b/crates/buzz-sdk/src/builders.rs index c8fdc83223..02bc96764e 100644 --- a/crates/buzz-sdk/src/builders.rs +++ b/crates/buzz-sdk/src/builders.rs @@ -12,7 +12,8 @@ use buzz_core::{ KIND_GIT_STATUS_OPEN, KIND_IA_ARCHIVE_REQUEST, KIND_IA_UNARCHIVE_REQUEST, KIND_MODERATION_BAN, KIND_MODERATION_RESOLVE_REPORT, KIND_MODERATION_TIMEOUT, KIND_MODERATION_UNBAN, KIND_MODERATION_UNTIMEOUT, KIND_PRESENCE_UPDATE, KIND_PROJECT, - KIND_USER_STATUS, KIND_WORKFLOW_DEF, KIND_WORKFLOW_TRIGGER, + KIND_USER_STATUS, KIND_WORKFLOW_DEF, KIND_WORKFLOW_OWNER_COMMAND, + KIND_WORKFLOW_OWNER_RESULT, KIND_WORKFLOW_TRIGGER, }, observer::{ content_looks_like_nip44, OBSERVER_AGENT_TAG, OBSERVER_FRAME_CONTROL, OBSERVER_FRAME_TAG, @@ -1640,6 +1641,88 @@ pub fn build_workflow_delete( build_delete_addressable(KIND_WORKFLOW_DEF, author_pubkey, &workflow_id.to_string()) } +/// Build an owner-signed managed-workflow lifecycle command (kind 46021). +/// +/// All five lifecycle operations use this same wire shape. The caller signs +/// the returned builder with the human owner's keys. The target agent is the +/// immutable author in the parameterized kind:30620 coordinate and the `p` +/// recipient. +pub fn build_workflow_owner_command( + command_id: Uuid, + agent_pubkey: &str, + workflow_id: Uuid, + expected_revision: &str, + operation: crate::WorkflowOwnerOperation, +) -> Result { + if command_id.is_nil() { + return Err(SdkError::InvalidInput("command_id must not be nil".into())); + } + if workflow_id.is_nil() { + return Err(SdkError::InvalidInput("workflow_id must not be nil".into())); + } + let agent = check_pubkey_hex(agent_pubkey, "agent pubkey")?; + let revision = check_hex_exact(expected_revision, 64, "expected revision")?; + let body = buzz_core::workflow_owner_command::WorkflowOwnerCommandBody { operation }; + let content = serde_json::to_string(&body) + .map_err(|error| SdkError::InvalidInput(format!("invalid owner command: {error}")))?; + let coordinate = format!("{KIND_WORKFLOW_DEF}:{agent}:{workflow_id}"); + let tags = vec![ + tag(&["d", &command_id.to_string()])?, + tag(&["a", &coordinate])?, + tag(&["revision", &revision])?, + tag(&["p", &agent])?, + ]; + Ok(EventBuilder::new(Kind::Custom(KIND_WORKFLOW_OWNER_COMMAND as u16), content).tags(tags)) +} + +/// Build an agent-signed terminal result (kind 46022) for an owner command. +/// +/// The caller signs the returned builder with the target agent's keys. The +/// `p` recipient is the human owner, while the coordinate's agent identity is +/// explicit because builders do not receive signing keys. +pub fn build_workflow_owner_result( + command_id: Uuid, + owner_pubkey: &str, + agent_pubkey: &str, + workflow_id: Uuid, + expected_revision: &str, + operation: crate::WorkflowOwnerOperation, + status: crate::WorkflowOwnerResultStatus, + reason: Option<&str>, +) -> Result { + if command_id.is_nil() { + return Err(SdkError::InvalidInput("command_id must not be nil".into())); + } + if workflow_id.is_nil() { + return Err(SdkError::InvalidInput("workflow_id must not be nil".into())); + } + let owner = check_pubkey_hex(owner_pubkey, "owner pubkey")?; + let agent = check_pubkey_hex(agent_pubkey, "agent pubkey")?; + let revision = check_hex_exact(expected_revision, 64, "expected revision")?; + if let Some(reason) = reason { + check_content( + reason, + buzz_core::workflow_owner_command::WORKFLOW_OWNER_MAX_REASON_BYTES, + )?; + } + let body = buzz_core::workflow_owner_command::WorkflowOwnerResultBody { + status, + reason: reason.map(str::to_owned), + }; + let content = serde_json::to_string(&body) + .map_err(|error| SdkError::InvalidInput(format!("invalid owner result: {error}")))?; + let coordinate = format!("{KIND_WORKFLOW_DEF}:{agent}:{workflow_id}"); + let tags = vec![ + tag(&["d", &command_id.to_string()])?, + tag(&["a", &coordinate])?, + tag(&["revision", &revision])?, + tag(&["p", &owner])?, + tag(&["operation", operation.as_str()])?, + tag(&["status", status.as_str()])?, + ]; + Ok(EventBuilder::new(Kind::Custom(KIND_WORKFLOW_OWNER_RESULT as u16), content).tags(tags)) +} + /// Build a workflow trigger event (kind 46020) bound to an exact signed revision. pub fn build_workflow_trigger( workflow_id: Uuid, @@ -4014,6 +4097,98 @@ mod tests { assert!(matches!(err, SdkError::InvalidInput(_))); } + #[test] + fn workflow_owner_command_covers_all_five_lifecycle_operations() { + let owner = nostr::Keys::generate(); + let agent = nostr::Keys::generate(); + let workflow = uuid(); + let command_id = uuid(); + let revision = "a".repeat(64); + for operation in [ + crate::WorkflowOwnerOperation::Start, + crate::WorkflowOwnerOperation::Pause, + crate::WorkflowOwnerOperation::Resume, + crate::WorkflowOwnerOperation::Cancel, + crate::WorkflowOwnerOperation::Restore, + ] { + let event = build_workflow_owner_command( + command_id, + &agent.public_key().to_hex(), + workflow, + &revision, + operation, + ) + .unwrap() + .sign_with_keys(&owner) + .unwrap(); + let parsed = buzz_core::workflow_owner_command::parse_owner_command(&event).unwrap(); + assert_eq!(parsed.operation, operation); + assert_eq!(parsed.owner_pubkey, owner.public_key()); + assert_eq!(parsed.agent_pubkey, agent.public_key()); + } + } + + #[test] + fn workflow_owner_result_preserves_agent_and_owner_identities() { + let owner = nostr::Keys::generate(); + let agent = nostr::Keys::generate(); + let command_id = uuid(); + let workflow = uuid(); + let revision = "b".repeat(64); + let event = build_workflow_owner_result( + command_id, + &owner.public_key().to_hex(), + &agent.public_key().to_hex(), + workflow, + &revision, + crate::WorkflowOwnerOperation::Restore, + crate::WorkflowOwnerResultStatus::Applied, + Some("restored"), + ) + .unwrap() + .sign_with_keys(&agent) + .unwrap(); + let parsed = buzz_core::workflow_owner_command::parse_owner_result(&event).unwrap(); + assert_eq!(parsed.agent_pubkey, agent.public_key()); + assert_eq!(parsed.owner_pubkey, owner.public_key()); + assert_eq!(parsed.operation, crate::WorkflowOwnerOperation::Restore); + assert_eq!(parsed.status, crate::WorkflowOwnerResultStatus::Applied); + assert_eq!(parsed.reason.as_deref(), Some("restored")); + } + + #[test] + fn workflow_owner_builders_reject_nil_and_malformed_identity() { + let revision = "c".repeat(64); + let agent = "a".repeat(64); + assert!(build_workflow_owner_command( + Uuid::nil(), + &agent, + uuid(), + &revision, + crate::WorkflowOwnerOperation::Start, + ) + .is_err()); + assert!(build_workflow_owner_command( + uuid(), + "bad", + uuid(), + &revision, + crate::WorkflowOwnerOperation::Pause, + ) + .is_err()); + assert!(build_workflow_owner_result( + uuid(), + &agent, + &agent, + uuid(), + &revision, + crate::WorkflowOwnerOperation::Cancel, + crate::WorkflowOwnerResultStatus::Rejected, + Some(&"x".repeat(4097)), + ) + .is_err()); + } + #[test] fn workflow_trigger_happy_path() { let wid = uuid(); diff --git a/crates/buzz-sdk/src/lib.rs b/crates/buzz-sdk/src/lib.rs index 4ee0cd4c88..2d6cd7d61c 100644 --- a/crates/buzz-sdk/src/lib.rs +++ b/crates/buzz-sdk/src/lib.rs @@ -20,6 +20,10 @@ pub use builders::*; /// Re-export kind constants so consumers don't need buzz-core directly. pub use buzz_core::kind; +/// Operation requested by a managed-workflow owner. +pub use buzz_core::workflow_owner_command::WorkflowOwnerOperation; +/// Terminal status reported by an agent for a workflow owner command. +pub use buzz_core::workflow_owner_command::WorkflowOwnerResultStatus; /// Thread reference for reply builders (NIP-10 markers). ///