Skip to content

Commit 40d86dd

Browse files
wesbillmanCarl
andcommitted
fix(workflows): commit deletion acceptance and mutation atomically
Serialize concurrent identical requests through one event transaction, preserve legacy repair, and roll back rejected public history. Align confirmation copy with intentional backdated recreation. Co-authored-by: Carl <32a2e2c9d428ee08902cab75d956da2c1d235a22d4766b0dd4138bf6e2e5db1d@buzz.block.builderlab.xyz> Signed-off-by: Wes <[email protected]>
1 parent 659b792 commit 40d86dd

8 files changed

Lines changed: 476 additions & 106 deletions

File tree

‎ARCHITECTURE.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,15 @@ Note: Both `TriggerDef` and `ActionDef` use serde internally-tagged enums. Trigg
561561

562562
**Cron scheduler:** loop ticks every 60 seconds, evaluates cron expressions with window-based matching, and creates workflow runs for matched triggers. Fully implemented.
563563

564+
**Deletion and recreation:** An authorized kind:5 `a`-tag deletion commits its
565+
public event, executable workflow removal, and visible kind:30620 removal in one
566+
transaction. Rejected deletions are not accepted history; identical concurrent
567+
requests have one dispatch/audit owner. Deletions older than a live definition
568+
leave that newer version intact. Deletion is not a permanent coordinate ban:
569+
clients may publish a distinct signed definition afterward, including a backdated
570+
version, to recreate the workflow and enable triggers again. This intentionally
571+
retains arrival-order recreation without a deletion watermark.
572+
564573
**Does NOT:** recursively resolve templates (single-pass only). Does NOT queue workflow runs when at capacity — returns `CapacityExceeded` immediately.
565574

566575
---

‎crates/buzz-db/src/store/workflow/deletion.rs‎

Lines changed: 111 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
//! Keep the executable workflow and its client-visible definition in sync.
22
3-
use buzz_core::{kind::KIND_WORKFLOW_DEF, CommunityId};
3+
use buzz_core::{kind::KIND_WORKFLOW_DEF, CommunityId, StoredEvent};
44
use buzz_datastore_tracing::datastore_span;
55
use chrono::{DateTime, Utc};
6-
use sqlx::Row;
6+
use nostr::Event;
7+
use sqlx::{Postgres, Row, Transaction};
78
use uuid::Uuid;
89

910
use crate::{Db, DbError, Result};
@@ -33,75 +34,127 @@ impl Db {
3334
d_tag: &str,
3435
deletion_created_at_secs: i64,
3536
) -> Result<WorkflowDeletionOutcome> {
36-
let cutoff = DateTime::from_timestamp(deletion_created_at_secs, 0)
37-
.ok_or(DbError::InvalidTimestamp(deletion_created_at_secs))?;
3837
let mut tx = self.begin_event_write_transaction().await?;
39-
let lock_key = crate::store::replaceable::event_replacement_lock_key(
38+
let outcome = delete_workflow_in_transaction(
39+
&mut tx,
4040
community_id,
41-
KIND_WORKFLOW_DEF as i32,
4241
owner_pubkey,
43-
Some(d_tag.as_bytes()),
44-
);
45-
crate::observability::observe_advisory_lock(
46-
crate::observability::LockType::Replacement,
47-
sqlx::query("SELECT pg_advisory_xact_lock($1)")
48-
.bind(lock_key)
49-
.execute(&mut *tx),
42+
d_tag,
43+
deletion_created_at_secs,
5044
)
5145
.await?;
46+
tx.commit().await?;
47+
Ok(outcome)
48+
}
5249

53-
let head: Option<DateTime<Utc>> = sqlx::query_scalar(
54-
"SELECT created_at FROM events WHERE community_id = $1 AND kind = $2 \
55-
AND pubkey = $3 AND d_tag = $4 AND deleted_at IS NULL \
56-
ORDER BY created_at DESC LIMIT 1",
50+
/// Commit the public deletion request and both workflow projections together.
51+
///
52+
/// The returned boolean grants dispatch ownership: either this transaction
53+
/// inserted the request, or it repaired an older relay's incomplete deletion.
54+
/// Concurrent identical requests cannot split those two outcomes across commits.
55+
/// Authorization of the coordinate is the caller's responsibility.
56+
pub async fn insert_workflow_deletion(
57+
&self,
58+
community_id: CommunityId,
59+
event: &Event,
60+
owner_pubkey: &[u8],
61+
d_tag: &str,
62+
) -> Result<(StoredEvent, bool, Option<Uuid>)> {
63+
let mut tx = self.begin_event_write_transaction().await?;
64+
let (stored, inserted) =
65+
crate::event::insert_event_in_transaction(&mut tx, community_id, event, None).await?;
66+
if inserted {
67+
// Unlike best-effort indexing for ordinary events, deletion fails
68+
// closed: its public request and discoverability commit together.
69+
crate::runtime::insert_mentions_in_transaction(&mut tx, community_id, event, None)
70+
.await?;
71+
}
72+
let outcome = delete_workflow_in_transaction(
73+
&mut tx,
74+
community_id,
75+
owner_pubkey,
76+
d_tag,
77+
event.created_at.as_secs() as i64,
5778
)
58-
.bind(community_id.as_uuid())
59-
.bind(KIND_WORKFLOW_DEF as i32)
60-
.bind(owner_pubkey)
61-
.bind(d_tag)
62-
.fetch_optional(&mut *tx)
6379
.await?;
64-
if head.is_some_and(|created_at| created_at > cutoff) {
65-
return Ok(WorkflowDeletionOutcome::default());
66-
}
80+
tx.commit().await?;
81+
Ok((stored, inserted || outcome.changed, outcome.channel_id))
82+
}
83+
}
84+
85+
async fn delete_workflow_in_transaction(
86+
tx: &mut Transaction<'_, Postgres>,
87+
community_id: CommunityId,
88+
owner_pubkey: &[u8],
89+
d_tag: &str,
90+
deletion_created_at_secs: i64,
91+
) -> Result<WorkflowDeletionOutcome> {
92+
let cutoff = DateTime::from_timestamp(deletion_created_at_secs, 0)
93+
.ok_or(DbError::InvalidTimestamp(deletion_created_at_secs))?;
94+
let lock_key = crate::store::replaceable::event_replacement_lock_key(
95+
community_id,
96+
KIND_WORKFLOW_DEF as i32,
97+
owner_pubkey,
98+
Some(d_tag.as_bytes()),
99+
);
100+
crate::observability::observe_advisory_lock(
101+
crate::observability::LockType::Replacement,
102+
sqlx::query("SELECT pg_advisory_xact_lock($1)")
103+
.bind(lock_key)
104+
.execute(&mut **tx),
105+
)
106+
.await?;
107+
108+
let head: Option<DateTime<Utc>> = sqlx::query_scalar(
109+
"SELECT created_at FROM events WHERE community_id = $1 AND kind = $2 \
110+
AND pubkey = $3 AND d_tag = $4 AND deleted_at IS NULL \
111+
ORDER BY created_at DESC LIMIT 1",
112+
)
113+
.bind(community_id.as_uuid())
114+
.bind(KIND_WORKFLOW_DEF as i32)
115+
.bind(owner_pubkey)
116+
.bind(d_tag)
117+
.fetch_optional(&mut **tx)
118+
.await?;
119+
if head.is_some_and(|created_at| created_at > cutoff) {
120+
return Ok(WorkflowDeletionOutcome::default());
121+
}
67122

68-
// UUID coordinates are canonical; retain the legacy name-based path.
69-
// The owner predicate remains in the mutation, not just a prior check.
70-
let workflow_id = Uuid::parse_str(d_tag).ok();
71-
let row = sqlx::query(
72-
"DELETE FROM workflows WHERE community_id = $1 AND owner_pubkey = $2 \
123+
// UUID coordinates are canonical; retain the legacy name-based path.
124+
// The owner predicate remains in the mutation, not just a prior check.
125+
let workflow_id = Uuid::parse_str(d_tag).ok();
126+
let row = sqlx::query(
127+
"DELETE FROM workflows WHERE community_id = $1 AND owner_pubkey = $2 \
73128
AND id = COALESCE($3::uuid, (SELECT id FROM workflows \
74129
WHERE community_id = $1 AND owner_pubkey = $2 AND name = $4 LIMIT 1)) \
75130
RETURNING channel_id",
76-
)
77-
.bind(community_id.as_uuid())
78-
.bind(owner_pubkey)
79-
.bind(workflow_id)
80-
.bind(d_tag)
81-
.fetch_optional(&mut *tx)
82-
.await?;
83-
let definitions = sqlx::query(
84-
"UPDATE events SET deleted_at = NOW() WHERE community_id = $1 AND kind = $2 \
131+
)
132+
.bind(community_id.as_uuid())
133+
.bind(owner_pubkey)
134+
.bind(workflow_id)
135+
.bind(d_tag)
136+
.fetch_optional(&mut **tx)
137+
.await?;
138+
let definitions = sqlx::query(
139+
"UPDATE events SET deleted_at = NOW() WHERE community_id = $1 AND kind = $2 \
85140
AND pubkey = $3 AND d_tag = $4 AND deleted_at IS NULL AND created_at <= $5",
86-
)
87-
.bind(community_id.as_uuid())
88-
.bind(KIND_WORKFLOW_DEF as i32)
89-
.bind(owner_pubkey)
90-
.bind(d_tag)
91-
.bind(cutoff)
92-
.execute(&mut *tx)
93-
.await?;
94-
let changed = row.is_some() || definitions.rows_affected() > 0;
95-
let channel_id = row
96-
.map(|row| row.try_get("channel_id"))
97-
.transpose()?
98-
.flatten();
99-
tx.commit().await?;
100-
Ok(WorkflowDeletionOutcome {
101-
changed,
102-
channel_id,
103-
})
104-
}
141+
)
142+
.bind(community_id.as_uuid())
143+
.bind(KIND_WORKFLOW_DEF as i32)
144+
.bind(owner_pubkey)
145+
.bind(d_tag)
146+
.bind(cutoff)
147+
.execute(&mut **tx)
148+
.await?;
149+
let changed = row.is_some() || definitions.rows_affected() > 0;
150+
let channel_id = row
151+
.map(|row| row.try_get("channel_id"))
152+
.transpose()?
153+
.flatten();
154+
Ok(WorkflowDeletionOutcome {
155+
changed,
156+
channel_id,
157+
})
105158
}
106159

107160
#[cfg(test)]

‎crates/buzz-db/src/store/workflow/deletion_tests.rs‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,45 @@ async fn workflow_deletion_rolls_back_both_representations_on_failure() {
250250
.expect("retry after failure");
251251
assert_absent(&db, &query, id).await;
252252
}
253+
254+
#[tokio::test]
255+
#[ignore = "requires Postgres"]
256+
async fn legacy_stored_deletion_repair_has_one_committed_dispatch_owner() {
257+
let (db, community) = setup().await;
258+
let keys = Keys::generate();
259+
let owner = keys.public_key().to_bytes();
260+
let id = Uuid::new_v4();
261+
let d_tag = id.to_string();
262+
let now = Timestamp::now().as_secs();
263+
let query = seed(&db, community, &keys, id, &d_tag, now).await;
264+
let deletion = EventBuilder::new(Kind::EventDeletion, "")
265+
.tags([Tag::parse([
266+
"a",
267+
&format!("30620:{}:{d_tag}", keys.public_key().to_hex()),
268+
])
269+
.expect("coordinate")])
270+
.custom_created_at(Timestamp::from(now))
271+
.sign_with_keys(&keys)
272+
.expect("sign legacy deletion");
273+
// Older relay versions could persist the request without applying deletion.
274+
db.insert_event_with_thread_metadata(community, &deletion, None, None)
275+
.await
276+
.expect("legacy request");
277+
let (first, second) = tokio::join!(
278+
db.insert_workflow_deletion(community, &deletion, &owner, &d_tag),
279+
db.insert_workflow_deletion(community, &deletion, &owner, &d_tag),
280+
);
281+
let (_, first_dispatch, _) = first.expect("first repair");
282+
let (_, second_dispatch, _) = second.expect("concurrent repair");
283+
assert_ne!(
284+
first_dispatch, second_dispatch,
285+
"only one committed repair dispatches"
286+
);
287+
assert_absent(&db, &query, id).await;
288+
assert!(
289+
!db.insert_workflow_deletion(community, &deletion, &owner, &d_tag)
290+
.await
291+
.expect("completed duplicate")
292+
.1
293+
);
294+
}

‎crates/buzz-relay/src/handlers/ingest.rs‎

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3144,7 +3144,15 @@ async fn ingest_event_inner(
31443144
});
31453145
}
31463146

3147-
let (stored_event, was_inserted) = if buzz_core::kind::is_replaceable(kind_u32) {
3147+
let workflow_deletion = crate::handlers::side_effects::is_workflow_deletion(&event);
3148+
let (stored_event, was_inserted) = if workflow_deletion {
3149+
// A single commit owns public acceptance, domain mutation, and dispatch.
3150+
// Failure rolls everything back; identical concurrent requests cannot
3151+
// divide insertion and repair ownership between two relay workers.
3152+
crate::handlers::side_effects::persist_workflow_deletion(tenant, &event, state)
3153+
.await
3154+
.map_err(|e| IngestError::Internal(format!("error: workflow deletion failed: {e}")))?
3155+
} else if buzz_core::kind::is_replaceable(kind_u32) {
31483156
// NIP-16 replaceable event — atomic replace with stale-write protection.
31493157
// channel_id is None for global kinds (0, 1, 3) due to step 5b above.
31503158
state
@@ -3203,21 +3211,7 @@ async fn ingest_event_inner(
32033211
}
32043212
};
32053213

3206-
// Workflow deletion must not acknowledge a failed domain mutation. Its
3207-
// idempotent transaction also runs for duplicate requests: the event may
3208-
// have been stored before a failure, including by an older relay version.
3209-
let workflow_deletion = crate::handlers::side_effects::is_workflow_deletion(&event);
3210-
let workflow_deletion_changed = if workflow_deletion {
3211-
crate::handlers::side_effects::handle_a_tag_deletion(tenant, &event, state)
3212-
.await
3213-
.map_err(|e| IngestError::Internal(format!("error: workflow deletion failed: {e}")))?
3214-
} else {
3215-
false
3216-
};
3217-
3218-
// A repaired deletion still needs the normal audit and live dispatch below.
3219-
// Completed duplicates remain no-ops, including channel-less/orphan repairs.
3220-
if !was_inserted && !workflow_deletion_changed {
3214+
if !was_inserted {
32213215
return Ok(IngestResult {
32223216
event_id: event_id_hex,
32233217
accepted: true,

‎crates/buzz-relay/src/handlers/side_effects.rs‎

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,14 +2109,43 @@ pub(crate) fn is_workflow_deletion(event: &Event) -> bool {
21092109
})
21102110
}
21112111

2112+
/// Persist an already-authorized workflow deletion and its domain changes atomically.
2113+
pub(crate) async fn persist_workflow_deletion(
2114+
tenant: &TenantContext,
2115+
event: &Event,
2116+
state: &Arc<AppState>,
2117+
) -> anyhow::Result<(buzz_core::StoredEvent, bool)> {
2118+
let coordinate = event
2119+
.tags
2120+
.iter()
2121+
.find(|tag| tag.kind().to_string() == "a")
2122+
.and_then(|tag| tag.content())
2123+
.ok_or_else(|| anyhow::anyhow!("missing workflow coordinate"))?;
2124+
let mut parts = coordinate.splitn(3, ':');
2125+
let _kind = parts.next();
2126+
let owner = hex::decode(parts.next().unwrap_or_default())?;
2127+
let d_tag = parts
2128+
.next()
2129+
.ok_or_else(|| anyhow::anyhow!("invalid workflow coordinate"))?;
2130+
let (stored, dispatch, channel_id) = state
2131+
.db
2132+
.insert_workflow_deletion(tenant.community(), event, &owner, d_tag)
2133+
.await?;
2134+
if let Some(channel_id) = channel_id {
2135+
state
2136+
.workflow_engine
2137+
.invalidate_channel_workflows(tenant.community(), channel_id);
2138+
}
2139+
Ok((stored, dispatch))
2140+
}
2141+
21122142
/// Handle NIP-09 deletion via `a` tag (addressable/parameterized-replaceable events).
21132143
/// Parses "kind:pubkey:d-tag" and deletes the corresponding DB record.
2114-
/// Returns whether a workflow deletion changed state, for duplicate repair dispatch.
2115-
pub(crate) async fn handle_a_tag_deletion(
2144+
async fn handle_a_tag_deletion(
21162145
tenant: &TenantContext,
21172146
event: &Event,
21182147
state: &Arc<AppState>,
2119-
) -> anyhow::Result<bool> {
2148+
) -> anyhow::Result<()> {
21202149
let a_value = event
21212150
.tags
21222151
.iter()
@@ -2140,25 +2169,10 @@ pub(crate) async fn handle_a_tag_deletion(
21402169
tracing::debug!(d_tag, "NIP-09 deletion ignored for push lease");
21412170
}
21422171
buzz_core::kind::KIND_WORKFLOW_DEF => {
2143-
// Authorization checked this coordinate's owner (including NIP-OA),
2144-
// not necessarily the signer of the deletion request.
2145-
let owner_bytes = hex::decode(pubkey_hex)
2146-
.map_err(|_| anyhow::anyhow!("invalid pubkey hex in workflow coordinate"))?;
2147-
let outcome = state
2148-
.db
2149-
.delete_workflow_by_coordinate(
2150-
tenant.community(),
2151-
&owner_bytes,
2152-
d_tag,
2153-
event.created_at.as_secs() as i64,
2154-
)
2155-
.await?;
2156-
if let Some(channel_id) = outcome.channel_id {
2157-
state
2158-
.workflow_engine
2159-
.invalidate_channel_workflows(tenant.community(), channel_id);
2160-
}
2161-
return Ok(outcome.changed);
2172+
// Workflow deletion belongs to the atomic persistence path in ingest.
2173+
return Err(anyhow::anyhow!(
2174+
"workflow deletion requires atomic persistence"
2175+
));
21622176
}
21632177
// Other NIP-33 events have no executable workflow projection.
21642178
k if is_parameterized_replaceable(k) => {
@@ -2213,7 +2227,7 @@ pub(crate) async fn handle_a_tag_deletion(
22132227
}
22142228
}
22152229

2216-
Ok(false)
2230+
Ok(())
22172231
}
22182232

22192233
async fn handle_standard_deletion_event(
@@ -2226,9 +2240,7 @@ async fn handle_standard_deletion_event(
22262240
// NIP-09 a-tag deletion path for addressable events. Keyed on the
22272241
// absence of *any* e tag (not just valid e-ids): a malformed e + a must
22282242
// not route here and silently soft-delete the coordinate.
2229-
return handle_a_tag_deletion(tenant, event, state)
2230-
.await
2231-
.map(|_| ());
2243+
return handle_a_tag_deletion(tenant, event, state).await;
22322244
}
22332245

22342246
for target_id in target_ids {

0 commit comments

Comments
 (0)