Files
buzz/crates/buzz-db/src/store/workflow.rs
Luke Tornquist 60de1db0b8 fix(db): admit fleet sweeps per tenant instead of in-SQL fence predicate (#8318)
## What

Removes every production call to the SQL fence predicate
`community_write_allowed()` from application SQL, so no application
mutation depends on the boolean predicate anymore. This is one
prerequisite for BUZZ-315 part B; #8322 (tolerating absent fence
objects) is a separate one. The triggers and SQL functions stay in this
PR because mixed-version pods still depend on them.

The predicate isn't inlined. It's a VOLATILE plpgsql function that takes
the deletion lock and *then* reads lifecycle state on a fresh snapshot,
and an inline expression would read state from the statement's start
snapshot. A separate earlier check would race deletion. So each sweep
now:

1. reads its candidate community IDs with a plain read, bounded the same
way the original statement was,
2. opens one `AdmittedTx` per community
(`begin_community_sweep_transaction`, a thin wrapper over
`begin_community_write_transaction` that turns an admission refusal into
"skip this tenant"; the four periodic sweeps go through
`sweep_admitted_communities`, which also owns their error handling),
3. runs the original mutation scoped to `community_id = $1` and commits.

| Site | Change |
| --- | --- |
| `runtime/push_enqueue.rs` | Deleted the redundant predicate. This
transaction already calls `guard_transaction`. |
| `store/relay_invite.rs` `reap_expired_relay_invites` | Keeps the
global expiry order and the 1,000-row ceiling. Candidates are
pre-filtered to active tenants so fenced invites can't fill the batch.
Admission is still what authorizes the write. |
| `store/workflow.rs` `prune_scheduled_workflow_fires_before` | Prunes
per admitted community and sums the row counts. |
| `store/channel.rs` `reap_expired_ephemeral_channels` | Archives per
admitted community and returns the committed rows, so the relay still
runs their system and discovery follow-ups. The candidate read has no
lifecycle pre-filter (it has no `LIMIT`, so admission alone decides). |
| `store/event.rs` `backfill_d_tags` | Backfills per admitted community
at startup. |
| `store/push.rs` `claim_due_match_batch_with_loader` | Picks the
earliest due active community, admits it, and claims under the same `FOR
UPDATE OF q SKIP LOCKED` and batch limit. It skips at most 16 tenants
that fence between the read and admission. |
| `store/push.rs` `reap_exhausted_matches` | Reaps per admitted
community. |

**Tenant failures.** The four periodic sweeps (invite reap, workflow
fire prune, channel reap, exhausted match reap) log a warning and
continue when one tenant fails. That tenant's rows are left unmodified
and stay eligible, so the next tick retries them. A tenant error doesn't
abort processing of the other tenants already in the selected candidate
page. Invite cleanup keeps its oldest-first 1,000-row selection, so if
one tenant owns that entire page and its delete keeps failing, tenants
outside the page wait. No persistent tenant-local failure for that
delete is known in the current schema, and base aborted the whole page
on any error. The returned count or rows cover the tenants that
committed. Claim, enqueue and the startup backfill still return the
error to the caller.

**Database outages.** A connection-level error (pool acquire timeout,
closed pool, socket I/O; `DbError::is_connection_unavailable`) isn't
tenant-local, so it stops the periodic sweep at that tenant instead of
moving on. Without this, an outage that began after the candidate read
cost one writer acquire timeout and one warning per remaining tenant,
and invite cleanup runs inside the usage-metrics leader tick. Rows
already committed are still returned, so reaped channels keep their
follow-ups. Remaining tenants retry next tick.

**Known limit (pre-existing, tracked separately):**
- *Claim at a locked head.* `claim_due_match_batch_with_loader` returns
`None` once an admitted tenant claims zero rows. If every due row in the
oldest active tenant is row-locked, younger tenants aren't tried on that
call. Base behaved the same way. This PR doesn't change it.

## Tests

All tests use a one-session fixture (`test_support::admission_fixture`)
with `session_replication_role = replica`, so the trigger backstop is
off and only application admission protects the fenced tenants. The
fixture shadows `community_write_allowed` with a function that raises,
in a per-call `admission_test_<uuid>` schema.

- One test per site: a quiescing tenant's rows are untouched while an
active tenant in the same sweep still progresses.
- Race tests for invite reap, channel reap and claim. These are the
sites where the candidate read already filters out fenced tenants. Each
test holds the tenant's deletion lock, waits until the sweep is blocked
in admission, fences the tenant in that transaction, and asserts its
rows are untouched. The claim test also asserts that a second active
tenant is claimed in the same call.
- `channel_reap_returns_committed_rows_after_earlier_tenant_error` and
`periodic_reaps_continue_after_tenant_error_and_retry_retained_rows`
inject a per-tenant failure. They check that later tenants commit, that
the failed tenant's rows are retained, and that the next call processes
them.
- `fleet_sweep_stops_at_first_unavailable_connection` runs the sweep
helper against a closed pool with three candidates and asserts only the
first is attempted.
`fleet_sweep_returns_committed_results_when_database_drops_mid_page`
(PostgreSQL) commits the first tenant, closes the pool, and asserts the
first tenant's result is returned and the sweep stops at the second of
three candidates.
`connection_unavailable_covers_only_pool_and_io_failures` pins the
classification; the per-tenant failure tests above confirm a
database-raised error still continues.

Falsification, each with the tests failing as expected:

| Mutation | Result |
| --- | --- |
| Original SQL restored at invite sweep, push claim, d-tag backfill |
0/3 pass (`legacy SQL fence predicate called`) |
| `guard_community_write` returns `Ok(())` after taking the lock | race
tests + channel reap test 0/4 pass |
| Per-tenant errors abort the sweep again | 0/2 pass |
| Sweep helper `continue`s instead of stopping on a connection-level
error | `fleet_sweep_stops_at_first_unavailable_connection` and
`fleet_sweep_returns_committed_results_when_database_drops_mid_page`
fail |
| Sweep helper drops already-committed results when it stops (`return
Vec::new()`) |
`fleet_sweep_returns_committed_results_when_database_drops_mid_page`
fails |
| Sweep helper removed from the source-policy chokepoint check |
`serving_table_writes_expose_syntactic_chokepoint_or_guarded_tx_routes`
fails |

The PostgreSQL tests are under `postgres_tests` modules with `#[ignore =
"requires Postgres"]`, which puts them in the `.config/nextest.toml`
PostgreSQL filter. CI evidence is under Verification.

## Verification

CI at head `5d37fbda` ([run
38925996275](https://github.com/block/buzz/actions/runs/38925996275)) is
green. The PostgreSQL job ([job
117062895683](https://github.com/block/buzz/actions/runs/38925996275/job/117062895683))
passed 1017/1017. That includes named PASS lines for all 30
`admission_postgres_tests`,
`fleet_sweep_returns_committed_results_when_database_drops_mid_page`,
and both writer-pool tests listed below. Unit Tests ([job
117058136482](https://github.com/block/buzz/actions/runs/38925996275/job/117058136482))
has PASS lines for
`connection_unavailable_covers_only_pool_and_io_failures` and
`fleet_sweep_stops_at_first_unavailable_connection`.

Local gates ran on a Linux cloud workstation against private PostgreSQL
16 and Redis. The tree was `735170df`, which is this head rebased on
`main` `c0ab789`, so the gates built the merge result.

| Gate | Result |
| --- | --- |
| `cargo fmt --all -- --check` | pass |
| `cargo clippy --workspace --all-targets -- -D warnings` | pass |
| `cargo nextest run -p buzz-db --lib --tests` | 185 passed |
| `scripts/test-postgres-test-discovery.sh` | pass |
| PostgreSQL lane for buzz-db (excluding the two writer-pool tests
below) | 530 passed |
| PostgreSQL lanes for buzz-admin, buzz-audit, buzz-deletion,
buzz-push-gateway, buzz-search, buzz-workflow | all pass |
| PostgreSQL lane for buzz-relay | 413/426. The 13
`group_state_postgres_tests` fail with `PoolTimedOut` on unmodified
`main` `c0ab789` too (see below) |
| The three mutations in the falsification table above (`continue` ×2,
`return Vec::new()`) | each fails its target test |

These failures happen only on the workstation, and each one passes in
CI's PostgreSQL job at this head:
- `writer_pool_metrics_stop_after_session_timeout_setup_failure` stalls.
It's an unchanged writer-pool configuration test, and it stalled the
same way at earlier heads.
- The 13 relay `group_state_postgres_tests` fail at the same point,
`ensure community: Sqlx(PoolTimedOut)`, on unmodified `main` `c0ab789`.
This PR doesn't touch `buzz-relay`.

## What got simpler

- No application mutation calls the boolean predicate
`community_write_allowed()` anymore. Dropping the fence objects still
has other dependencies: serving-write lease acquisition calls
`assert_community_write_allowed()` (`store/deletion.rs:3068`), catalog
validation requires both functions (`store/deletion.rs:1195-1198`), and
the triggers and compatibility SQL still use them. Those belong to #8322
and part B, not this PR.
- The claim's `target` CTE is gone, and every sweep uses the same
admission-and-skip helper.
- The four periodic sweeps share one loop (`sweep_admitted_communities`)
for admission, skip, continue-on-tenant-error and stop-on-outage,
instead of four copies of it.
- The source policy treats that helper as a tenant-local chokepoint, so
`backfill_d_tags` no longer needs a reviewed exception.
- The per-tenant loops add code, because each community now gets its own
admitted transaction.

---------

Signed-off-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz>
Co-authored-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz>
2026-10-16 15:46:37 -04:00

2939 lines
100 KiB
Rust

//! Workflow CRUD -- workflows, workflow_runs, and workflow_approvals tables.
//!
//! All IDs are native Postgres UUID columns. Never uses string interpolation
//! for query values -- all user data goes through bind parameters.
//!
//! Security notes:
//! - Approval tokens are stored as SHA-256 hashes (never plaintext).
//! - All list queries have a bounded LIMIT to prevent unbounded scans.
mod deletion;
pub use deletion::WorkflowDeletionOutcome;
use std::fmt;
use std::str::FromStr;
use chrono::{DateTime, Utc};
use sha2::{Digest, Sha256};
use sqlx::{PgPool, Row};
use uuid::Uuid;
use buzz_core::CommunityId;
use crate::error::{DbError, Result};
use crate::Db;
use buzz_datastore_tracing::datastore_span;
// -- Token hashing ------------------------------------------------------------
/// Default maximum rows returned by list queries. Callers may request fewer.
pub const LIST_DEFAULT_LIMIT: i64 = 100;
/// Hard cap on rows returned by list queries.
pub const LIST_MAX_LIMIT: i64 = 1000;
/// SHA-256 hash of a raw approval token. Returns the 32-byte digest.
///
/// Approval tokens are stored hashed so that a DB read does not expose
/// the raw token (same pattern as API tokens in buzz-auth).
fn hash_approval_token(token: &str) -> Vec<u8> {
Sha256::digest(token.as_bytes()).to_vec()
}
// -- Status enums -------------------------------------------------------------
/// Status of a workflow definition. Stored as ENUM('active','disabled','archived').
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum WorkflowStatus {
/// Workflow is live and will fire on matching events.
Active,
/// Workflow is paused and will not fire.
Disabled,
/// Workflow has been retired.
Archived,
}
impl fmt::Display for WorkflowStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
WorkflowStatus::Active => write!(f, "active"),
WorkflowStatus::Disabled => write!(f, "disabled"),
WorkflowStatus::Archived => write!(f, "archived"),
}
}
}
impl FromStr for WorkflowStatus {
type Err = DbError;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s {
"active" => Ok(WorkflowStatus::Active),
"disabled" => Ok(WorkflowStatus::Disabled),
"archived" => Ok(WorkflowStatus::Archived),
other => Err(DbError::InvalidData(format!(
"unknown workflow status: {other}"
))),
}
}
}
/// Status of a workflow run. Stored as ENUM in workflow_runs.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RunStatus {
/// Run is queued but not yet started.
Pending,
/// Run is actively executing steps.
Running,
/// Run is suspended waiting for an approval gate.
WaitingApproval,
/// Run finished successfully.
Completed,
/// Run terminated with an error.
Failed,
/// Run was cancelled before completion.
Cancelled,
}
impl fmt::Display for RunStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
RunStatus::Pending => write!(f, "pending"),
RunStatus::Running => write!(f, "running"),
RunStatus::WaitingApproval => write!(f, "waiting_approval"),
RunStatus::Completed => write!(f, "completed"),
RunStatus::Failed => write!(f, "failed"),
RunStatus::Cancelled => write!(f, "cancelled"),
}
}
}
impl FromStr for RunStatus {
type Err = DbError;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s {
"pending" => Ok(RunStatus::Pending),
"running" => Ok(RunStatus::Running),
"waiting_approval" => Ok(RunStatus::WaitingApproval),
"completed" => Ok(RunStatus::Completed),
"failed" => Ok(RunStatus::Failed),
"cancelled" => Ok(RunStatus::Cancelled),
other => Err(DbError::InvalidData(format!("unknown run status: {other}"))),
}
}
}
/// Status of an approval request. Stored as ENUM in workflow_approvals.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ApprovalStatus {
/// Approval has been requested but not yet acted on.
Pending,
/// Approval was granted; the run may proceed.
Granted,
/// Approval was denied; the run should fail.
Denied,
/// The approval window elapsed without a decision.
Expired,
}
impl fmt::Display for ApprovalStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ApprovalStatus::Pending => write!(f, "pending"),
ApprovalStatus::Granted => write!(f, "granted"),
ApprovalStatus::Denied => write!(f, "denied"),
ApprovalStatus::Expired => write!(f, "expired"),
}
}
}
impl FromStr for ApprovalStatus {
type Err = DbError;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s {
"pending" => Ok(ApprovalStatus::Pending),
"granted" => Ok(ApprovalStatus::Granted),
"denied" => Ok(ApprovalStatus::Denied),
"expired" => Ok(ApprovalStatus::Expired),
other => Err(DbError::InvalidData(format!(
"unknown approval status: {other}"
))),
}
}
}
// -- Record types -------------------------------------------------------------
/// A workflow definition record. Run-state columns live in `workflow_runs`.
#[derive(Debug, Clone)]
pub struct WorkflowRecord {
/// Unique workflow identifier.
pub id: Uuid,
/// Server-resolved community that owns this workflow.
pub community_id: CommunityId,
/// Human-readable workflow name.
pub name: String,
/// Compressed public key bytes of the workflow owner.
pub owner_pubkey: Vec<u8>,
/// Channel this workflow is scoped to, if any.
pub channel_id: Option<Uuid>,
/// Canonical JSON of the workflow definition.
pub definition: serde_json::Value,
/// SHA-256 hash of the canonical definition JSON.
pub definition_hash: Vec<u8>,
/// Current lifecycle status of the workflow definition.
pub status: WorkflowStatus,
/// Whether the workflow will fire on matching events.
pub enabled: bool,
/// When the workflow was created.
pub created_at: DateTime<Utc>,
/// When the workflow was last updated.
pub updated_at: DateTime<Utc>,
}
/// A single execution of a workflow.
#[derive(Debug, Clone)]
pub struct WorkflowRunRecord {
/// Unique run identifier.
pub id: Uuid,
/// Server-resolved community this run (and its workflow) belongs to.
///
/// `workflow_runs` is keyed `(community_id, id)`; the same run/workflow
/// UUID is allowed across communities, so every run carries its owning
/// community and downstream execution (side-effect sink, scoped lookups)
/// runs under it rather than re-deriving a tenant from the deployment host.
pub community_id: CommunityId,
/// The workflow definition that was executed.
pub workflow_id: Uuid,
/// Current execution status of this run.
pub status: RunStatus,
/// Raw event ID bytes that triggered this run, if any.
pub trigger_event_id: Option<Vec<u8>>,
/// Index of the step currently executing (0-based).
pub current_step: i32,
/// JSON execution trace -- one entry per completed step.
pub execution_trace: serde_json::Value,
/// Serialized `TriggerContext` captured at workflow start.
/// NULL for runs created before this column was added (backwards-compatible).
pub trigger_context: Option<serde_json::Value>,
/// When execution began.
pub started_at: Option<DateTime<Utc>>,
/// When execution finished (success or failure).
pub completed_at: Option<DateTime<Utc>>,
/// Redacted human-readable diagnostic for failed or cancelled runs.
pub error_message: Option<String>,
/// Stable machine-readable failure or cancellation classification.
/// Kept separate from `error_message` so callers never parse diagnostics.
pub error_code: Option<String>,
/// When the run record was created.
pub created_at: DateTime<Utc>,
}
/// A winning scheduled workflow fire claim.
///
/// The primary identity is `(workflow_id, scheduled_for)`. `community_id` is
/// resolved from the workflow row inside the claim SQL and returned for scoped
/// audit/logging; callers never supply it as a claim.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ScheduledWorkflowFireClaim {
/// Community that owns this scheduled fire.
pub community_id: CommunityId,
/// Workflow definition that should run.
pub workflow_id: Uuid,
/// Authoritative schedule instant this claim represents.
pub scheduled_for: DateTime<Utc>,
/// Database timestamp for when this pod won the claim.
pub claimed_at: DateTime<Utc>,
}
/// A pending or resolved approval gate for a workflow step.
#[derive(Debug, Clone)]
pub struct ApprovalRecord {
/// Token hash as stored in the DB (BYTEA).
pub token: Vec<u8>,
/// The workflow this approval belongs to.
pub workflow_id: Uuid,
/// The run waiting on this approval.
pub run_id: Uuid,
/// The step ID that requested approval.
pub step_id: String,
/// Zero-based index of the step in the workflow.
pub step_index: i32,
/// Who may approve (user mention or role spec).
pub approver_spec: String,
/// Current status of this approval request.
pub status: ApprovalStatus,
/// Compressed public key bytes of the user who acted on this approval.
pub approver_pubkey: Option<Vec<u8>>,
/// Optional note left by the approver.
pub note: Option<String>,
/// When this approval request expires.
pub expires_at: DateTime<Utc>,
/// When the approval record was created.
pub created_at: DateTime<Utc>,
}
// -- Workflow CRUD ------------------------------------------------------------
/// Insert a new workflow record. Returns the new workflow's UUID.
/// New workflows start as `active` and `enabled = TRUE`.
///
/// NOTE: see the cache-invalidation note on [`update_workflow`]. The relay's
/// creation path is [`upsert_workflow`] via event ingest. (No current callers.)
pub async fn create_workflow(
pool: &PgPool,
community_id: CommunityId,
channel_id: Option<Uuid>,
owner_pubkey: &[u8],
name: &str,
definition_json: &str,
definition_hash: &[u8],
) -> Result<Uuid> {
let mut tx = crate::begin_community_write_transaction(
pool,
community_id,
crate::observability::WriterOperation::EventWrite,
)
.await?;
let id = Uuid::new_v4();
sqlx::query(
r#"
INSERT INTO workflows
(id, community_id, name, owner_pubkey, channel_id, definition, definition_hash, status, enabled)
VALUES ($1, $2, $3, $4, $5, $6::jsonb, $7, 'active', TRUE)
"#,
)
.bind(id)
.bind(community_id.as_uuid())
.bind(name)
.bind(owner_pubkey)
.bind(channel_id)
.bind(definition_json)
.bind(definition_hash)
.execute(tx.conn())
.await?;
tx.commit().await?;
Ok(id)
}
/// Insert or update a workflow at the caller-supplied NIP-33 `d`-tag UUID.
///
/// Updates are allowed only when the existing row has the same owner and
/// channel. That keeps a learned workflow UUID from becoming a cross-user or
/// cross-channel overwrite primitive while still making retries idempotent.
#[allow(clippy::too_many_arguments)]
pub async fn upsert_workflow(
pool: &PgPool,
community_id: CommunityId,
id: Uuid,
channel_id: Option<Uuid>,
owner_pubkey: &[u8],
name: &str,
definition_json: &str,
definition_hash: &[u8],
) -> Result<()> {
let mut tx = crate::begin_community_write_transaction(
pool,
community_id,
crate::observability::WriterOperation::EventWrite,
)
.await?;
let row = sqlx::query(
r#"
INSERT INTO workflows
(community_id, id, name, owner_pubkey, channel_id, definition, definition_hash, status, enabled)
VALUES ($1, $2, $3, $4, $5, $6::jsonb, $7, 'active', TRUE)
ON CONFLICT (community_id, id) DO UPDATE
SET name = EXCLUDED.name,
definition = EXCLUDED.definition,
definition_hash = EXCLUDED.definition_hash,
updated_at = NOW()
WHERE workflows.owner_pubkey = EXCLUDED.owner_pubkey
AND workflows.channel_id IS NOT DISTINCT FROM EXCLUDED.channel_id
RETURNING id
"#,
)
.bind(community_id.as_uuid())
.bind(id)
.bind(name)
.bind(owner_pubkey)
.bind(channel_id)
.bind(definition_json)
.bind(definition_hash)
.fetch_optional(tx.conn())
.await?;
tx.commit().await?;
if row.is_none() {
return Err(DbError::AccessDenied(format!(
"workflow {id} belongs to a different owner or channel"
)));
}
Ok(())
}
/// Fetch a single workflow by ID, scoped to its community.
///
/// `workflows` is keyed `(community_id, id)`; the same workflow UUID can exist
/// in two communities, so a request-scoped lookup must bind both. The caller
/// supplies the server-resolved community (host-bound tenant for request paths,
/// the run's own community for execution paths) — never a client-supplied id.
pub async fn get_workflow(
pool: &PgPool,
community_id: CommunityId,
id: Uuid,
) -> Result<WorkflowRecord> {
let row = sqlx::query(
r#"
SELECT id, community_id, name, owner_pubkey, channel_id, definition, definition_hash,
status::text AS status, enabled, created_at, updated_at
FROM workflows
WHERE community_id = $1 AND id = $2
"#,
)
.bind(community_id.as_uuid())
.bind(id)
.fetch_optional(pool)
.await?
.ok_or_else(|| DbError::NotFound(format!("workflow {id}")))?;
row_to_workflow_record(row)
}
/// List workflows for a channel, ordered newest first.
///
/// `limit` is capped at [`LIST_MAX_LIMIT`]. Pass `None` to use [`LIST_DEFAULT_LIMIT`].
/// `offset` enables pagination (0-based row offset).
pub async fn list_channel_workflows(
pool: &PgPool,
community_id: CommunityId,
channel_id: Uuid,
limit: Option<i64>,
offset: Option<i64>,
) -> Result<Vec<WorkflowRecord>> {
let limit = limit.unwrap_or(LIST_DEFAULT_LIMIT).clamp(1, LIST_MAX_LIMIT);
let offset = offset.unwrap_or(0).max(0);
let rows = sqlx::query(
r#"
SELECT id, community_id, name, owner_pubkey, channel_id, definition, definition_hash,
status::text AS status, enabled, created_at, updated_at
FROM workflows
WHERE community_id = $1 AND channel_id = $2
ORDER BY created_at DESC
LIMIT $3 OFFSET $4
"#,
)
.bind(community_id.as_uuid())
.bind(channel_id)
.bind(limit)
.bind(offset)
.fetch_all(pool)
.await?;
rows.into_iter().map(row_to_workflow_record).collect()
}
/// List active, enabled workflows for a channel.
/// Used by the trigger-matching path to find workflows that should fire.
/// Only returns workflows with status = 'active' AND enabled = TRUE.
///
/// Bounded to [`LIST_MAX_LIMIT`] rows -- the trigger path should not process
/// an unbounded number of workflows per event.
pub async fn list_enabled_channel_workflows(
pool: &PgPool,
community_id: CommunityId,
channel_id: Uuid,
) -> Result<Vec<WorkflowRecord>> {
let rows = sqlx::query(
r#"
SELECT id, community_id, name, owner_pubkey, channel_id, definition, definition_hash,
status::text AS status, enabled, created_at, updated_at
FROM workflows
WHERE community_id = $1
AND channel_id = $2
AND status = 'active'
AND enabled = TRUE
ORDER BY created_at DESC
LIMIT $3
"#,
)
.bind(community_id.as_uuid())
.bind(channel_id)
.bind(LIST_MAX_LIMIT)
.fetch_all(pool)
.await?;
rows.into_iter().map(row_to_workflow_record).collect()
}
/// List all active, enabled workflows with a `schedule` trigger across all channels.
///
/// Used by the cron scheduler. Filters by trigger type in SQL to avoid loading
/// event-triggered workflows that the cron loop would immediately discard.
/// Results are bounded to [`LIST_MAX_LIMIT`] rows.
pub async fn list_all_enabled_workflows(pool: &PgPool) -> Result<Vec<WorkflowRecord>> {
let rows = sqlx::query(
r#"
SELECT w.id, w.community_id, w.name, w.owner_pubkey, w.channel_id, w.definition, w.definition_hash,
w.status::text AS status, w.enabled, w.created_at, w.updated_at
FROM workflows w
JOIN communities c ON c.id = w.community_id
WHERE w.status = 'active'
AND w.enabled = TRUE
AND w.definition->'trigger'->>'on' = 'schedule'
AND c.archived_at IS NULL
ORDER BY w.created_at ASC
LIMIT $1
"#,
)
.bind(LIST_MAX_LIMIT)
.fetch_all(pool)
.await?;
rows.into_iter().map(row_to_workflow_record).collect()
}
/// Claim a scheduled workflow fire for an authoritative schedule instant.
///
/// Returns `Some` only for the first pod that claims `(community_id,
/// workflow_id, scheduled_for)`. All other pods receive `None` and must skip
/// creating a workflow run. The `scheduled_for` value must come from an
/// external schedule anchor (cron expression) or DB-authoritative interval
/// anchor; a per-pod in-memory timestamp is not safe because different pods
/// can compute different claim keys.
///
/// `community_id` is server provenance — for the global scheduler scan it is
/// the `workflow.community_id` returned by [`list_all_enabled_workflows`], not
/// any client-supplied value. It is required because `workflows` is keyed
/// `(community_id, id)`: duplicate workflow UUIDs across communities are
/// allowed, so resolving the owning community from `id` alone is ambiguous and
/// would fan a single claim across every community holding that UUID. Binding
/// `(community_id, id)` confines the claim — and its `SELECT`/`INSERT` row — to
/// exactly the intended tenant.
pub async fn claim_scheduled_workflow_fire(
pool: &PgPool,
community_id: CommunityId,
workflow_id: Uuid,
scheduled_for: DateTime<Utc>,
) -> Result<Option<ScheduledWorkflowFireClaim>> {
let mut tx = crate::begin_community_write_transaction(
pool,
community_id,
crate::observability::WriterOperation::EventWrite,
)
.await?;
let row = sqlx::query(
r#"
INSERT INTO scheduled_workflow_fires (community_id, workflow_id, scheduled_for)
SELECT w.community_id, w.id, $3
FROM workflows w
WHERE w.community_id = $1 AND w.id = $2
ON CONFLICT (community_id, workflow_id, scheduled_for) DO NOTHING
RETURNING community_id, workflow_id, scheduled_for, claimed_at
"#,
)
.bind(community_id.as_uuid())
.bind(workflow_id)
.bind(scheduled_for)
.fetch_optional(tx.conn())
.await?;
tx.commit().await?;
row.map(|row| {
let community_id: Uuid = row.try_get("community_id")?;
Ok(ScheduledWorkflowFireClaim {
community_id: CommunityId::from_uuid(community_id),
workflow_id: row.try_get("workflow_id")?,
scheduled_for: row.try_get("scheduled_for")?,
claimed_at: row.try_get("claimed_at")?,
})
})
.transpose()
}
/// Fetch the greatest claimed schedule instant for a workflow.
///
/// Interval schedulers use this as their DB-authoritative `last_fired` anchor.
/// It makes all pods compute the same next interval instant after a successful
/// claim, and preserves the interval clock across pod restarts. This intentionally
/// reads from `scheduled_workflow_fires`, not `workflow_runs`, because the claim
/// row is the source of truth for schedule deduplication.
pub async fn latest_scheduled_workflow_fire(
pool: &PgPool,
community_id: CommunityId,
workflow_id: Uuid,
) -> Result<Option<DateTime<Utc>>> {
let row = sqlx::query(
r#"
SELECT MAX(scheduled_for) AS scheduled_for
FROM scheduled_workflow_fires
WHERE community_id = $1 AND workflow_id = $2
"#,
)
.bind(community_id.as_uuid())
.bind(workflow_id)
.fetch_one(pool)
.await?;
row.try_get("scheduled_for").map_err(Into::into)
}
/// Link a won scheduled-fire claim to the workflow run it created.
///
/// This is for ops/audit forensics only; the claim row remains the dedupe
/// boundary. If run creation succeeds, callers should attach the run id before
/// spawning execution. If run creation fails, leaving `workflow_run_id` NULL is
/// intentional: the schedule instant was claimed and must not duplicate later.
pub async fn attach_scheduled_workflow_run(
pool: &PgPool,
community_id: CommunityId,
workflow_id: Uuid,
scheduled_for: DateTime<Utc>,
workflow_run_id: Uuid,
) -> Result<bool> {
let mut tx = crate::begin_community_write_transaction(
pool,
community_id,
crate::observability::WriterOperation::EventWrite,
)
.await?;
let result = sqlx::query(
r#"
UPDATE scheduled_workflow_fires
SET workflow_run_id = $4
WHERE community_id = $1
AND workflow_id = $2
AND scheduled_for = $3
AND workflow_run_id IS NULL
"#,
)
.bind(community_id.as_uuid())
.bind(workflow_id)
.bind(scheduled_for)
.bind(workflow_run_id)
.execute(tx.conn())
.await?;
tx.commit().await?;
Ok(result.rows_affected() == 1)
}
/// Delete old scheduled workflow fire claims for retention.
///
/// Schedule claim rows are correctness metadata, but they grow with every fire.
/// The relay/ops janitor should retain enough history for audits and interval
/// anchoring: the cutoff must be older than the largest interval schedule the
/// deployment supports, or interval workflows can lose their DB-authoritative
/// anchor after pruning.
pub async fn prune_scheduled_workflow_fires_before(
pool: &PgPool,
older_than: DateTime<Utc>,
) -> Result<u64> {
let communities: Vec<Uuid> = sqlx::query_scalar(
"SELECT DISTINCT community_id FROM scheduled_workflow_fires WHERE claimed_at < $1",
)
.bind(older_than)
.fetch_all(pool)
.await?;
let deleted = crate::sweep_admitted_communities(
pool,
communities,
crate::observability::WriterOperation::Maintenance,
"scheduled_workflow_fire_prune",
|id, mut tx| async move {
let result = sqlx::query(
"DELETE FROM scheduled_workflow_fires WHERE community_id = $1 AND claimed_at < $2",
)
.bind(id)
.bind(older_than)
.execute(tx.conn())
.await?;
tx.commit().await?;
Ok(result.rows_affected())
},
)
.await;
Ok(deleted.into_iter().sum())
}
/// Update a workflow's name, definition, and definition_hash.
///
/// NOTE: the relay's `WorkflowEngine` caches enabled workflows per
/// `(community_id, channel_id)`; a caller mutating trigger behavior must
/// invalidate via `WorkflowEngine::invalidate_channel_workflows` or trigger
/// matching lags the change by up to the cache TTL. (No current callers.)
pub async fn update_workflow(
pool: &PgPool,
community_id: CommunityId,
id: Uuid,
name: &str,
definition_json: &str,
definition_hash: &[u8],
) -> Result<()> {
let mut tx = crate::begin_community_write_transaction(
pool,
community_id,
crate::observability::WriterOperation::EventWrite,
)
.await?;
let affected = sqlx::query(
r#"
UPDATE workflows
SET name = $1, definition = $2::jsonb, definition_hash = $3
WHERE community_id = $4 AND id = $5
"#,
)
.bind(name)
.bind(definition_json)
.bind(definition_hash)
.bind(community_id.as_uuid())
.bind(id)
.execute(tx.conn())
.await?
.rows_affected();
tx.commit().await?;
if affected == 0 {
return Err(DbError::NotFound(format!("workflow {id}")));
}
Ok(())
}
/// Update a workflow's status (active -> disabled -> archived).
///
/// NOTE: status gates trigger eligibility; see the cache-invalidation note on
/// [`update_workflow`]. (No current callers.)
pub async fn update_workflow_status(
pool: &PgPool,
community_id: CommunityId,
id: Uuid,
status: WorkflowStatus,
) -> Result<()> {
let mut tx = crate::begin_community_write_transaction(
pool,
community_id,
crate::observability::WriterOperation::EventWrite,
)
.await?;
let affected = sqlx::query(
r#"
UPDATE workflows
SET status = $1::workflow_status
WHERE community_id = $2 AND id = $3
"#,
)
.bind(status.to_string())
.bind(community_id.as_uuid())
.bind(id)
.execute(tx.conn())
.await?
.rows_affected();
tx.commit().await?;
if affected == 0 {
return Err(DbError::NotFound(format!("workflow {id}")));
}
Ok(())
}
/// Enable or disable a workflow without changing its status.
///
/// NOTE: `enabled` gates trigger eligibility; see the cache-invalidation note
/// on [`update_workflow`]. (No current callers.)
pub async fn set_workflow_enabled(
pool: &PgPool,
community_id: CommunityId,
id: Uuid,
enabled: bool,
) -> Result<()> {
let mut tx = crate::begin_community_write_transaction(
pool,
community_id,
crate::observability::WriterOperation::EventWrite,
)
.await?;
let affected = sqlx::query(
r#"
UPDATE workflows
SET enabled = $1
WHERE community_id = $2 AND id = $3
"#,
)
.bind(enabled)
.bind(community_id.as_uuid())
.bind(id)
.execute(tx.conn())
.await?
.rows_affected();
tx.commit().await?;
if affected == 0 {
return Err(DbError::NotFound(format!("workflow {id}")));
}
Ok(())
}
/// Disable all of `owner_pubkey`'s workflows in a channel (SEC-006).
///
/// Called when the owner loses channel membership (kind 9001 removal or kind
/// 9022 leave) so their workflows stop firing durably — across pods and
/// restarts — rather than only until the per-fire authority gate happens to
/// run. Idempotent; returns the number of workflows disabled so the caller
/// can decide whether a trigger-cache invalidation is needed.
pub async fn disable_workflows_for_owner_in_channel(
pool: &PgPool,
community_id: CommunityId,
channel_id: Uuid,
owner_pubkey: &[u8],
) -> Result<u64> {
let mut tx = crate::begin_community_write_transaction(
pool,
community_id,
crate::observability::WriterOperation::EventWrite,
)
.await?;
let affected = sqlx::query(
r#"
UPDATE workflows
SET enabled = FALSE
WHERE community_id = $1 AND channel_id = $2 AND owner_pubkey = $3 AND enabled = TRUE
"#,
)
.bind(community_id.as_uuid())
.bind(channel_id)
.bind(owner_pubkey)
.execute(tx.conn())
.await?
.rows_affected();
tx.commit().await?;
Ok(affected)
}
/// Connection-local variant: run the same workflow-disable UPDATE on the
/// provided connection rather than acquiring a new one from the pool.
///
/// Used by [`buzz_db::channel_members::MembershipRemovalFence::commit_disabling_workflows`]
/// so the disable executes on the fence's own connection, avoiding a second
/// pool acquisition while the fence holds one connection.
pub async fn disable_workflows_for_owner_in_channel_on_conn(
tx: &mut crate::AdmittedTx,
community_id: CommunityId,
channel_id: Uuid,
owner_pubkey: &[u8],
) -> Result<u64> {
if tx.community() != community_id {
return Err(DbError::AccessDenied(
"workflow write community mismatch".into(),
));
}
let affected = sqlx::query(
r#"
UPDATE workflows
SET enabled = FALSE
WHERE community_id = $1 AND channel_id = $2 AND owner_pubkey = $3 AND enabled = TRUE
"#,
)
.bind(community_id.as_uuid())
.bind(channel_id)
.bind(owner_pubkey)
.execute(tx.conn())
.await?
.rows_affected();
Ok(affected)
}
// -- Workflow Run CRUD --------------------------------------------------------
/// Insert a new workflow run. Returns the new run's UUID.
///
/// `trigger_context` is the serialized `TriggerContext` for this run. It is stored
/// so that post-approval resume steps can restore the original trigger data and
/// correctly resolve `{{trigger.*}}` template variables.
pub async fn create_workflow_run(
pool: &PgPool,
community_id: CommunityId,
workflow_id: Uuid,
trigger_event_id: Option<&[u8]>,
trigger_context: Option<&serde_json::Value>,
) -> Result<Uuid> {
let mut tx = crate::begin_community_write_transaction(
pool,
community_id,
crate::observability::WriterOperation::EventWrite,
)
.await?;
let id = Uuid::new_v4();
sqlx::query(
r#"
INSERT INTO workflow_runs
(community_id, id, workflow_id, status, trigger_event_id, current_step, execution_trace, trigger_context)
VALUES ($1, $2, $3, 'pending', $4, 0, '[]', $5)
"#,
)
.bind(community_id.as_uuid())
.bind(id)
.bind(workflow_id)
.bind(trigger_event_id)
.bind(trigger_context)
.execute(tx.conn())
.await?;
tx.commit().await?;
Ok(id)
}
/// Fetch a single workflow run by ID, scoped to its community.
pub async fn get_workflow_run(
pool: &PgPool,
community_id: CommunityId,
id: Uuid,
) -> Result<WorkflowRunRecord> {
let row = sqlx::query(
r#"
SELECT community_id, id, workflow_id, status::text AS status, trigger_event_id, current_step,
execution_trace, trigger_context, started_at, completed_at, error_message, error_code, created_at
FROM workflow_runs
WHERE community_id = $1 AND id = $2
"#,
)
.bind(community_id.as_uuid())
.bind(id)
.fetch_optional(pool)
.await?
.ok_or_else(|| DbError::NotFound(format!("workflow_run {id}")))?;
row_to_run_record(row)
}
/// List runs for a workflow using a stable newest-first keyset.
///
/// Rows are ordered by `(created_at DESC, id DESC)`. A cursor is valid only
/// when both `before` and `before_id` are supplied; callers should pass the
/// final row from the previous page. `limit` is clamped to the shared list
/// bounds.
pub async fn list_workflow_runs_page(
pool: &PgPool,
community_id: CommunityId,
workflow_id: Uuid,
before: Option<DateTime<Utc>>,
before_id: Option<Uuid>,
limit: i64,
) -> Result<Vec<WorkflowRunRecord>> {
let limit = limit.clamp(1, LIST_MAX_LIMIT);
let rows = sqlx::query(
r#"
SELECT community_id, id, workflow_id, status::text AS status, trigger_event_id, current_step,
execution_trace, trigger_context, started_at, completed_at, error_message, error_code, created_at
FROM workflow_runs
WHERE community_id = $1 AND workflow_id = $2
AND (
$3::timestamptz IS NULL
OR $4::uuid IS NULL
OR (created_at, id) < ($3, $4)
)
ORDER BY created_at DESC, id DESC
LIMIT $5
"#,
)
.bind(community_id.as_uuid())
.bind(workflow_id)
.bind(before)
.bind(before_id)
.bind(limit)
.fetch_all(pool)
.await?;
rows.into_iter().map(row_to_run_record).collect()
}
/// List runs for a workflow, newest first, up to `limit` rows.
pub async fn list_workflow_runs(
pool: &PgPool,
community_id: CommunityId,
workflow_id: Uuid,
limit: i64,
) -> Result<Vec<WorkflowRunRecord>> {
list_workflow_runs_page(pool, community_id, workflow_id, None, None, limit).await
}
/// Structured failure persisted for a workflow run.
#[derive(Debug, Clone, Copy)]
pub struct WorkflowRunFailure<'a> {
/// Stable machine-readable failure code.
pub code: &'a str,
/// Human-readable failure detail.
pub message: &'a str,
}
/// Update run status, current step, execution trace, and optional failure.
///
/// Fix C3: `started_at` is set when the NEW status is 'running' and `started_at`
/// has not yet been stamped (IS NULL). The original code read `status` from the
/// column AFTER `SET status = ?` had already changed it, so the condition was
/// always false. We now check the bind parameter directly.
pub async fn update_workflow_run(
pool: &PgPool,
community_id: CommunityId,
id: Uuid,
status: RunStatus,
current_step: i32,
trace: &serde_json::Value,
failure: Option<WorkflowRunFailure<'_>>,
) -> Result<()> {
let mut tx = crate::begin_community_write_transaction(
pool,
community_id,
crate::observability::WriterOperation::EventWrite,
)
.await?;
let status_str = status.to_string();
let (error_code, error) = failure
.map(|failure| (Some(failure.code), Some(failure.message)))
.unwrap_or((None, None));
let affected = sqlx::query(
r#"
UPDATE workflow_runs
SET status = $1::run_status,
current_step = $2,
execution_trace = $3,
error_code = $4,
error_message = $5,
started_at = CASE WHEN $6 = 'running' AND started_at IS NULL
THEN NOW() ELSE started_at END,
completed_at = CASE WHEN $7 IN ('completed','failed','cancelled')
THEN NOW() ELSE completed_at END
WHERE community_id = $8 AND id = $9
"#,
)
.bind(&status_str)
.bind(current_step)
.bind(trace)
.bind(error_code)
.bind(error)
.bind(&status_str) // for started_at CASE
.bind(&status_str) // for completed_at CASE
.bind(community_id.as_uuid())
.bind(id)
.execute(tx.conn())
.await?
.rows_affected();
tx.commit().await?;
if affected == 0 {
return Err(DbError::NotFound(format!("workflow_run {id}")));
}
Ok(())
}
// -- Approval CRUD ------------------------------------------------------------
/// Parameters for creating a new approval request.
pub struct CreateApprovalParams<'a> {
/// Server-resolved community that owns the workflow/run this approval gates.
pub community_id: CommunityId,
/// Raw approval token (will be hashed before storage).
pub token: &'a str,
/// The workflow this approval belongs to.
pub workflow_id: Uuid,
/// The run waiting on this approval.
pub run_id: Uuid,
/// The step ID that requested approval.
pub step_id: &'a str,
/// Zero-based index of the step in the workflow.
pub step_index: i32,
/// Who may approve (user mention or role spec).
pub approver_spec: &'a str,
/// When this approval request expires.
pub expires_at: DateTime<Utc>,
}
/// Insert a new approval request.
///
/// The `token` parameter is the raw (plaintext) token. It is hashed with
/// SHA-256 before storage so the DB never holds the raw value.
pub async fn create_approval(pool: &PgPool, params: CreateApprovalParams<'_>) -> Result<()> {
let CreateApprovalParams {
community_id,
token,
workflow_id,
run_id,
step_id,
step_index,
approver_spec,
expires_at,
} = params;
let token_hash = hash_approval_token(token);
let mut tx = crate::begin_community_write_transaction(
pool,
community_id,
crate::observability::WriterOperation::EventWrite,
)
.await?;
sqlx::query(
r#"
INSERT INTO workflow_approvals
(community_id, token, workflow_id, run_id, step_id, step_index, approver_spec, status, expires_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, 'pending', $8)
"#,
)
.bind(community_id.as_uuid())
.bind(token_hash)
.bind(workflow_id)
.bind(run_id)
.bind(step_id)
.bind(step_index)
.bind(approver_spec)
.bind(expires_at)
.execute(tx.conn())
.await?;
tx.commit().await?;
Ok(())
}
/// Fetch an approval record by raw token.
///
/// The token is hashed before the DB lookup so plaintext tokens are never
/// sent to the database layer.
pub async fn get_approval(
pool: &PgPool,
community_id: CommunityId,
token: &str,
) -> Result<ApprovalRecord> {
let token_hash = hash_approval_token(token);
get_approval_by_stored_hash(pool, community_id, &token_hash).await
}
/// Fetch an approval record by its already-hashed token value.
///
/// Use this when you already have the hash stored in the DB (e.g., from
/// `get_run_approvals`). The `token_hash` is used directly without re-hashing.
///
/// `workflow_approvals` is keyed `(community_id, token)`; the same token bytes
/// could in principle collide across communities, so the lookup binds the
/// server-resolved community alongside the token.
pub async fn get_approval_by_stored_hash(
pool: &PgPool,
community_id: CommunityId,
token_hash: &[u8],
) -> Result<ApprovalRecord> {
let row = sqlx::query(
r#"
SELECT token, workflow_id, run_id, step_id, step_index, approver_spec,
status::text AS status, approver_pubkey, note, expires_at, created_at
FROM workflow_approvals
WHERE community_id = $1 AND token = $2
"#,
)
.bind(community_id.as_uuid())
.bind(token_hash)
.fetch_optional(pool)
.await?
.ok_or_else(|| DbError::NotFound("approval token (hashed)".to_string()))?;
row_to_approval_record(row)
}
/// Fetch all approval records for a given workflow run.
pub async fn get_run_approvals(
pool: &PgPool,
community_id: CommunityId,
workflow_id: Uuid,
run_id: Uuid,
) -> Result<Vec<ApprovalRecord>> {
let rows = sqlx::query(
r#"
SELECT token, workflow_id, run_id, step_id, step_index, approver_spec,
status::text AS status, approver_pubkey, note, expires_at, created_at
FROM workflow_approvals
WHERE community_id = $1 AND run_id = $2 AND workflow_id = $3
ORDER BY step_index, created_at
"#,
)
.bind(community_id.as_uuid())
.bind(run_id)
.bind(workflow_id)
.fetch_all(pool)
.await?;
rows.into_iter().map(row_to_approval_record).collect()
}
/// Update an approval's status, approver pubkey, and optional note.
/// Also stamps `granted_at` or `denied_at` based on the new status.
///
/// The `token` parameter is the raw (plaintext) token; it is hashed before
/// the WHERE lookup.
///
/// # TOCTOU safety (N5)
/// The WHERE clause includes `AND status = 'pending'` so that two concurrent
/// grant/deny requests cannot both succeed. If the approval was already acted
/// on (status != 'pending'), the UPDATE touches 0 rows and this function
/// returns `Ok(false)`. Callers should treat `false` as a conflict (HTTP 409).
pub async fn update_approval(
pool: &PgPool,
community_id: CommunityId,
token: &str,
status: ApprovalStatus,
approver_pubkey: Option<&[u8]>,
note: Option<&str>,
) -> Result<bool> {
let token_hash = hash_approval_token(token);
update_approval_by_stored_hash(
pool,
community_id,
&token_hash,
status,
approver_pubkey,
note,
)
.await
}
/// Update an approval by its already-hashed token value.
///
/// Use this when you already have the hash stored in the DB (e.g., from
/// `get_run_approvals`). The `token_hash` is used directly without re-hashing.
///
/// See [`update_approval`] for TOCTOU safety notes. The predicate binds the
/// server-resolved community alongside the token so an approval action for A/X
/// can never act on B/X.
pub async fn update_approval_by_stored_hash(
pool: &PgPool,
community_id: CommunityId,
token_hash: &[u8],
status: ApprovalStatus,
approver_pubkey: Option<&[u8]>,
note: Option<&str>,
) -> Result<bool> {
let mut tx = crate::begin_community_write_transaction(
pool,
community_id,
crate::observability::WriterOperation::EventWrite,
)
.await?;
let status_str = status.to_string();
let affected = sqlx::query(
r#"
UPDATE workflow_approvals
SET status = $1::approval_status,
approver_pubkey = $2,
note = $3,
granted_at = CASE WHEN $4 = 'granted' THEN NOW() ELSE granted_at END,
denied_at = CASE WHEN $5 = 'denied' THEN NOW() ELSE denied_at END
WHERE community_id = $6 AND token = $7 AND status = 'pending'
"#,
)
.bind(&status_str)
.bind(approver_pubkey)
.bind(note)
.bind(&status_str) // for granted_at CASE
.bind(&status_str) // for denied_at CASE
.bind(community_id.as_uuid())
.bind(token_hash)
.execute(tx.conn())
.await?
.rows_affected();
tx.commit().await?;
Ok(affected > 0)
}
// -- Row mappers --------------------------------------------------------------
fn row_to_workflow_record(row: sqlx::postgres::PgRow) -> Result<WorkflowRecord> {
let id: Uuid = row.try_get("id")?;
let channel_id: Option<Uuid> = row.try_get("channel_id")?;
let status_str: String = row.try_get("status")?;
let status = status_str.parse::<WorkflowStatus>()?;
let enabled: bool = row.try_get("enabled")?;
let community_id: Uuid = row.try_get("community_id")?;
Ok(WorkflowRecord {
id,
community_id: CommunityId::from_uuid(community_id),
name: row.try_get("name")?,
owner_pubkey: row.try_get("owner_pubkey")?,
channel_id,
definition: row.try_get("definition")?,
definition_hash: row.try_get("definition_hash")?,
status,
enabled,
created_at: row.try_get("created_at")?,
updated_at: row.try_get("updated_at")?,
})
}
fn row_to_run_record(row: sqlx::postgres::PgRow) -> Result<WorkflowRunRecord> {
let id: Uuid = row.try_get("id")?;
let community_id: Uuid = row.try_get("community_id")?;
let workflow_id: Uuid = row.try_get("workflow_id")?;
let status_str: String = row.try_get("status")?;
let status = status_str.parse::<RunStatus>()?;
Ok(WorkflowRunRecord {
id,
community_id: CommunityId::from_uuid(community_id),
workflow_id,
status,
trigger_event_id: row.try_get("trigger_event_id")?,
current_step: row.try_get("current_step")?,
execution_trace: row.try_get("execution_trace")?,
trigger_context: row.try_get("trigger_context")?,
started_at: row.try_get("started_at")?,
completed_at: row.try_get("completed_at")?,
error_message: row.try_get("error_message")?,
error_code: row.try_get("error_code")?,
created_at: row.try_get("created_at")?,
})
}
fn row_to_approval_record(row: sqlx::postgres::PgRow) -> Result<ApprovalRecord> {
let workflow_id: Uuid = row.try_get("workflow_id")?;
let run_id: Uuid = row.try_get("run_id")?;
let status_str: String = row.try_get("status")?;
let status = status_str.parse::<ApprovalStatus>()?;
Ok(ApprovalRecord {
token: row.try_get("token")?,
workflow_id,
run_id,
step_id: row.try_get("step_id")?,
step_index: row.try_get("step_index")?,
approver_spec: row.try_get("approver_spec")?,
status,
approver_pubkey: row.try_get("approver_pubkey")?,
note: row.try_get("note")?,
expires_at: row.try_get("expires_at")?,
created_at: row.try_get("created_at")?,
})
}
/// Find a workflow by owner pubkey and name within a community. Returns the
/// first match (active or not).
pub async fn find_by_owner_and_name(
pool: &PgPool,
community_id: CommunityId,
owner_pubkey: &[u8],
name: &str,
) -> Result<Option<WorkflowRecord>> {
let row = sqlx::query(
r#"
SELECT id, community_id, name, owner_pubkey, channel_id, definition, definition_hash,
status::text AS status, enabled, created_at, updated_at
FROM workflows
WHERE community_id = $1 AND owner_pubkey = $2 AND name = $3
LIMIT 1
"#,
)
.bind(community_id.as_uuid())
.bind(owner_pubkey)
.bind(name)
.fetch_optional(pool)
.await?;
match row {
Some(r) => Ok(Some(row_to_workflow_record(r)?)),
None => Ok(None),
}
}
// -- Run and approval Db API --------------------------------------------------
impl Db {
/// Create a new workflow run.
#[datastore_span(name = "create_workflow_run", system = "postgresql")]
pub async fn create_workflow_run(
&self,
community_id: CommunityId,
workflow_id: Uuid,
trigger_event_id: Option<&[u8]>,
trigger_context: Option<&serde_json::Value>,
) -> Result<Uuid> {
crate::workflow::create_workflow_run(
&self.pool,
community_id,
workflow_id,
trigger_event_id,
trigger_context,
)
.await
}
/// Fetch a single workflow run, scoped to its community.
#[datastore_span(name = "get_workflow_run", system = "postgresql")]
pub async fn get_workflow_run(
&self,
community_id: CommunityId,
id: Uuid,
) -> Result<crate::workflow::WorkflowRunRecord> {
crate::workflow::get_workflow_run(&self.pool, community_id, id).await
}
/// List runs for a workflow.
#[datastore_span(name = "list_workflow_runs", system = "postgresql")]
pub async fn list_workflow_runs(
&self,
community_id: CommunityId,
workflow_id: Uuid,
limit: i64,
) -> Result<Vec<crate::workflow::WorkflowRunRecord>> {
crate::workflow::list_workflow_runs(&self.pool, community_id, workflow_id, limit).await
}
/// List one keyset-paginated page of workflow runs.
#[datastore_span(name = "list_workflow_runs_page", system = "postgresql")]
pub async fn list_workflow_runs_page(
&self,
community_id: CommunityId,
workflow_id: Uuid,
before: Option<chrono::DateTime<chrono::Utc>>,
before_id: Option<Uuid>,
limit: i64,
) -> Result<Vec<crate::workflow::WorkflowRunRecord>> {
crate::workflow::list_workflow_runs_page(
&self.pool,
community_id,
workflow_id,
before,
before_id,
limit,
)
.await
}
/// Update a workflow run's status.
#[datastore_span(name = "update_workflow_run", system = "postgresql")]
pub async fn update_workflow_run(
&self,
community_id: CommunityId,
id: Uuid,
status: crate::workflow::RunStatus,
current_step: i32,
trace: &serde_json::Value,
failure: Option<crate::workflow::WorkflowRunFailure<'_>>,
) -> Result<()> {
crate::workflow::update_workflow_run(
&self.pool,
community_id,
id,
status,
current_step,
trace,
failure,
)
.await
}
/// Create an approval request.
#[datastore_span(name = "create_approval", system = "postgresql")]
pub async fn create_approval(
&self,
params: crate::workflow::CreateApprovalParams<'_>,
) -> Result<()> {
crate::workflow::create_approval(&self.pool, params).await
}
/// Fetch an approval by raw token.
#[datastore_span(name = "get_approval", system = "postgresql")]
pub async fn get_approval(
&self,
community_id: CommunityId,
token: &str,
) -> Result<crate::workflow::ApprovalRecord> {
crate::workflow::get_approval(&self.pool, community_id, token).await
}
/// Fetch an approval by its already-hashed token (no re-hashing).
#[datastore_span(name = "get_approval_by_stored_hash", system = "postgresql")]
pub async fn get_approval_by_stored_hash(
&self,
community_id: CommunityId,
token_hash: &[u8],
) -> Result<crate::workflow::ApprovalRecord> {
crate::workflow::get_approval_by_stored_hash(&self.pool, community_id, token_hash).await
}
/// Fetch all approvals for a workflow run.
#[datastore_span(name = "get_run_approvals", system = "postgresql")]
pub async fn get_run_approvals(
&self,
community_id: CommunityId,
workflow_id: uuid::Uuid,
run_id: uuid::Uuid,
) -> Result<Vec<crate::workflow::ApprovalRecord>> {
crate::workflow::get_run_approvals(&self.pool, community_id, workflow_id, run_id).await
}
/// Update an approval's status.
#[datastore_span(name = "update_approval", system = "postgresql")]
pub async fn update_approval(
&self,
community_id: CommunityId,
token: &str,
status: crate::workflow::ApprovalStatus,
approver_pubkey: Option<&[u8]>,
note: Option<&str>,
) -> Result<bool> {
crate::workflow::update_approval(
&self.pool,
community_id,
token,
status,
approver_pubkey,
note,
)
.await
}
/// Update an approval by its already-hashed token (no re-hashing).
#[datastore_span(name = "update_approval_by_stored_hash", system = "postgresql")]
pub async fn update_approval_by_stored_hash(
&self,
community_id: CommunityId,
token_hash: &[u8],
status: crate::workflow::ApprovalStatus,
approver_pubkey: Option<&[u8]>,
note: Option<&str>,
) -> Result<bool> {
crate::workflow::update_approval_by_stored_hash(
&self.pool,
community_id,
token_hash,
status,
approver_pubkey,
note,
)
.await
}
}
// -- Workflow lifecycle Db API ------------------------------------------------
impl Db {
/// Create a new workflow.
#[datastore_span(name = "create_workflow", system = "postgresql")]
pub async fn create_workflow(
&self,
community_id: CommunityId,
channel_id: Option<Uuid>,
owner_pubkey: &[u8],
name: &str,
definition_json: &str,
definition_hash: &[u8],
) -> Result<Uuid> {
crate::workflow::create_workflow(
&self.pool,
community_id,
channel_id,
owner_pubkey,
name,
definition_json,
definition_hash,
)
.await
}
/// Insert or update a workflow using its NIP-33 `d`-tag UUID.
#[allow(clippy::too_many_arguments)]
#[datastore_span(name = "upsert_workflow", system = "postgresql")]
pub async fn upsert_workflow(
&self,
community_id: CommunityId,
id: Uuid,
channel_id: Option<Uuid>,
owner_pubkey: &[u8],
name: &str,
definition_json: &str,
definition_hash: &[u8],
) -> Result<()> {
crate::workflow::upsert_workflow(
&self.pool,
community_id,
id,
channel_id,
owner_pubkey,
name,
definition_json,
definition_hash,
)
.await
}
/// Fetch a single workflow by ID, scoped to its community.
#[datastore_span(name = "get_workflow", system = "postgresql")]
pub async fn get_workflow(
&self,
community_id: CommunityId,
id: Uuid,
) -> Result<crate::workflow::WorkflowRecord> {
crate::workflow::get_workflow(&self.pool, community_id, id).await
}
/// List workflows for a channel.
#[datastore_span(name = "list_channel_workflows", system = "postgresql")]
pub async fn list_channel_workflows(
&self,
community_id: CommunityId,
channel_id: Uuid,
limit: Option<i64>,
offset: Option<i64>,
) -> Result<Vec<crate::workflow::WorkflowRecord>> {
crate::workflow::list_channel_workflows(&self.pool, community_id, channel_id, limit, offset)
.await
}
/// List active, enabled workflows for a channel.
#[datastore_span(name = "list_enabled_channel_workflows", system = "postgresql")]
pub async fn list_enabled_channel_workflows(
&self,
community_id: CommunityId,
channel_id: Uuid,
) -> Result<Vec<crate::workflow::WorkflowRecord>> {
crate::workflow::list_enabled_channel_workflows(&self.pool, community_id, channel_id).await
}
/// List all active, enabled schedule-triggered workflows.
#[datastore_span(name = "list_all_enabled_workflows", system = "postgresql")]
pub async fn list_all_enabled_workflows(&self) -> Result<Vec<crate::workflow::WorkflowRecord>> {
crate::workflow::list_all_enabled_workflows(&self.pool).await
}
/// Claim a scheduled workflow fire for an authoritative schedule instant.
///
/// Returns `Some` only for the first pod to claim `(community_id,
/// workflow_id, scheduled_for)`; all other pods must skip creating a run.
/// `community_id` is server provenance (the workflow row's own community
/// from the scheduler scan), never client-supplied — `workflows` is keyed
/// `(community_id, id)`, so the claim must bind both to avoid fanning
/// across communities that share the workflow UUID.
#[datastore_span(name = "claim_scheduled_workflow_fire", system = "postgresql")]
pub async fn claim_scheduled_workflow_fire(
&self,
community_id: CommunityId,
workflow_id: Uuid,
scheduled_for: chrono::DateTime<chrono::Utc>,
) -> Result<Option<crate::workflow::ScheduledWorkflowFireClaim>> {
crate::workflow::claim_scheduled_workflow_fire(
&self.pool,
community_id,
workflow_id,
scheduled_for,
)
.await
}
/// Fetch the latest claimed schedule instant for interval trigger anchoring.
#[datastore_span(name = "latest_scheduled_workflow_fire", system = "postgresql")]
pub async fn latest_scheduled_workflow_fire(
&self,
community_id: CommunityId,
workflow_id: Uuid,
) -> Result<Option<chrono::DateTime<chrono::Utc>>> {
crate::workflow::latest_scheduled_workflow_fire(&self.pool, community_id, workflow_id).await
}
/// Attach the workflow run id created from a won scheduled-fire claim.
#[datastore_span(name = "attach_scheduled_workflow_run", system = "postgresql")]
pub async fn attach_scheduled_workflow_run(
&self,
community_id: CommunityId,
workflow_id: Uuid,
scheduled_for: chrono::DateTime<chrono::Utc>,
workflow_run_id: Uuid,
) -> Result<bool> {
crate::workflow::attach_scheduled_workflow_run(
&self.pool,
community_id,
workflow_id,
scheduled_for,
workflow_run_id,
)
.await
}
/// Delete old scheduled workflow fire claims before a retention cutoff.
#[datastore_span(name = "prune_scheduled_workflow_fires_before", system = "postgresql")]
pub async fn prune_scheduled_workflow_fires_before(
&self,
older_than: chrono::DateTime<chrono::Utc>,
) -> Result<u64> {
crate::workflow::prune_scheduled_workflow_fires_before(&self.pool, older_than).await
}
/// Update a workflow's name, definition, and hash.
#[datastore_span(name = "update_workflow", system = "postgresql")]
pub async fn update_workflow(
&self,
community_id: CommunityId,
id: Uuid,
name: &str,
definition_json: &str,
definition_hash: &[u8],
) -> Result<()> {
crate::workflow::update_workflow(
&self.pool,
community_id,
id,
name,
definition_json,
definition_hash,
)
.await
}
/// Update a workflow's status.
#[datastore_span(name = "update_workflow_status", system = "postgresql")]
pub async fn update_workflow_status(
&self,
community_id: CommunityId,
id: Uuid,
status: crate::workflow::WorkflowStatus,
) -> Result<()> {
crate::workflow::update_workflow_status(&self.pool, community_id, id, status).await
}
/// Enable or disable a workflow.
#[datastore_span(name = "set_workflow_enabled", system = "postgresql")]
pub async fn set_workflow_enabled(
&self,
community_id: CommunityId,
id: Uuid,
enabled: bool,
) -> Result<()> {
crate::workflow::set_workflow_enabled(&self.pool, community_id, id, enabled).await
}
/// Disable all of an owner's workflows in a channel (SEC-006, on
/// membership loss). Returns the number of workflows disabled.
#[datastore_span(name = "disable_workflows_for_owner_in_channel", system = "postgresql")]
pub async fn disable_workflows_for_owner_in_channel(
&self,
community_id: CommunityId,
channel_id: Uuid,
owner_pubkey: &[u8],
) -> Result<u64> {
crate::workflow::disable_workflows_for_owner_in_channel(
&self.pool,
community_id,
channel_id,
owner_pubkey,
)
.await
}
/// Find a workflow by owner pubkey and name within a community. Used for
/// NIP-09 a-tag deletion where the d-tag is the workflow name (not UUID).
#[datastore_span(name = "find_workflow_by_owner_and_name", system = "postgresql")]
pub async fn find_workflow_by_owner_and_name(
&self,
community_id: CommunityId,
owner_pubkey: &[u8],
name: &str,
) -> Result<Option<crate::workflow::WorkflowRecord>> {
crate::workflow::find_by_owner_and_name(&self.pool, community_id, owner_pubkey, name).await
}
}
// -- Tests --------------------------------------------------------------------
#[cfg(test)]
mod postgres_tests {
use super::*;
use chrono::TimeZone;
// -- WorkflowStatus enum --------------------------------------------------
#[test]
fn workflow_status_display_is_lowercase() {
assert_eq!(WorkflowStatus::Active.to_string(), "active");
assert_eq!(WorkflowStatus::Disabled.to_string(), "disabled");
assert_eq!(WorkflowStatus::Archived.to_string(), "archived");
}
#[test]
fn workflow_status_from_str_round_trips() {
for s in &["active", "disabled", "archived"] {
let status: WorkflowStatus = s.parse().expect("parse");
assert_eq!(status.to_string(), *s);
}
}
#[test]
fn workflow_status_from_str_rejects_unknown() {
let err = "pending".parse::<WorkflowStatus>().unwrap_err();
assert!(matches!(err, DbError::InvalidData(_)));
}
#[test]
fn workflow_status_equality() {
assert_eq!(WorkflowStatus::Active, WorkflowStatus::Active);
assert_ne!(WorkflowStatus::Active, WorkflowStatus::Disabled);
}
// -- RunStatus enum -------------------------------------------------------
#[test]
fn run_status_display_is_lowercase() {
assert_eq!(RunStatus::Pending.to_string(), "pending");
assert_eq!(RunStatus::Running.to_string(), "running");
assert_eq!(RunStatus::WaitingApproval.to_string(), "waiting_approval");
assert_eq!(RunStatus::Completed.to_string(), "completed");
assert_eq!(RunStatus::Failed.to_string(), "failed");
assert_eq!(RunStatus::Cancelled.to_string(), "cancelled");
}
#[test]
fn run_status_from_str_round_trips() {
for s in &[
"pending",
"running",
"waiting_approval",
"completed",
"failed",
"cancelled",
] {
let status: RunStatus = s.parse().expect("parse");
assert_eq!(status.to_string(), *s);
}
}
#[test]
fn run_status_from_str_rejects_unknown() {
let err = "active".parse::<RunStatus>().unwrap_err();
assert!(matches!(err, DbError::InvalidData(_)));
}
// -- ApprovalStatus enum --------------------------------------------------
#[test]
fn approval_status_display_is_lowercase() {
assert_eq!(ApprovalStatus::Pending.to_string(), "pending");
assert_eq!(ApprovalStatus::Granted.to_string(), "granted");
assert_eq!(ApprovalStatus::Denied.to_string(), "denied");
assert_eq!(ApprovalStatus::Expired.to_string(), "expired");
}
#[test]
fn approval_status_from_str_round_trips() {
for s in &["pending", "granted", "denied", "expired"] {
let status: ApprovalStatus = s.parse().expect("parse");
assert_eq!(status.to_string(), *s);
}
}
#[test]
fn approval_status_from_str_rejects_unknown() {
let err = "approved".parse::<ApprovalStatus>().unwrap_err();
assert!(matches!(err, DbError::InvalidData(_)));
}
// -- WorkflowRecord -------------------------------------------------------
#[test]
fn workflow_record_fields_are_accessible() {
let id = Uuid::new_v4();
let channel_id = Uuid::new_v4();
let now = Utc::now();
let def = serde_json::json!({
"name": "My Workflow",
"trigger": { "on": "message_posted" },
"steps": [{ "id": "s1", "action": "send_message", "text": "hi" }]
});
let community_id = CommunityId::from_uuid(Uuid::new_v4());
let record = WorkflowRecord {
id,
community_id,
name: "My Workflow".to_owned(),
owner_pubkey: vec![0xab; 32],
channel_id: Some(channel_id),
definition: def.clone(),
definition_hash: vec![0x01, 0x02, 0x03, 0x04],
status: WorkflowStatus::Active,
enabled: true,
created_at: now,
updated_at: now,
};
assert_eq!(record.id, id);
assert_eq!(record.community_id, community_id);
assert_eq!(record.name, "My Workflow");
assert_eq!(record.owner_pubkey, vec![0xab; 32]);
assert_eq!(record.channel_id, Some(channel_id));
assert_eq!(record.definition, def);
assert_eq!(record.definition_hash, vec![0x01, 0x02, 0x03, 0x04]);
assert_eq!(record.status, WorkflowStatus::Active);
assert!(record.enabled);
}
#[test]
fn workflow_record_channel_id_can_be_none() {
let id = Uuid::new_v4();
let now = Utc::now();
let record = WorkflowRecord {
id,
community_id: CommunityId::from_uuid(Uuid::new_v4()),
name: "Global Workflow".to_owned(),
owner_pubkey: vec![0x00; 32],
channel_id: None,
definition: serde_json::json!({}),
definition_hash: vec![],
status: WorkflowStatus::Active,
enabled: true,
created_at: now,
updated_at: now,
};
assert!(record.channel_id.is_none());
}
#[test]
fn workflow_record_clone_is_independent() {
let id = Uuid::new_v4();
let now = Utc::now();
let record = WorkflowRecord {
id,
community_id: CommunityId::from_uuid(Uuid::new_v4()),
name: "Original".to_owned(),
owner_pubkey: vec![0x01; 32],
channel_id: None,
definition: serde_json::json!({}),
definition_hash: vec![0xAA],
status: WorkflowStatus::Active,
enabled: true,
created_at: now,
updated_at: now,
};
let mut cloned = record.clone();
cloned.name = "Cloned".to_owned();
assert_eq!(record.name, "Original");
assert_eq!(cloned.name, "Cloned");
}
#[test]
fn workflow_record_status_variants() {
let now = Utc::now();
for status in &[
WorkflowStatus::Active,
WorkflowStatus::Disabled,
WorkflowStatus::Archived,
] {
let record = WorkflowRecord {
id: Uuid::new_v4(),
community_id: CommunityId::from_uuid(Uuid::new_v4()),
name: "Test".to_owned(),
owner_pubkey: vec![],
channel_id: None,
definition: serde_json::json!({}),
definition_hash: vec![],
status: status.clone(),
enabled: true,
created_at: now,
updated_at: now,
};
assert_eq!(&record.status, status);
}
}
#[test]
fn workflow_record_disabled_has_enabled_false() {
let now = Utc::now();
let record = WorkflowRecord {
id: Uuid::new_v4(),
community_id: CommunityId::from_uuid(Uuid::new_v4()),
name: "Paused".to_owned(),
owner_pubkey: vec![],
channel_id: None,
definition: serde_json::json!({}),
definition_hash: vec![],
status: WorkflowStatus::Active,
enabled: false,
created_at: now,
updated_at: now,
};
assert!(!record.enabled);
assert_eq!(record.status, WorkflowStatus::Active);
}
// -- WorkflowRunRecord ----------------------------------------------------
#[test]
fn workflow_run_record_fields_are_accessible() {
let id = Uuid::new_v4();
let workflow_id = Uuid::new_v4();
let now = Utc::now();
let trigger_event_id = vec![0xde, 0xad, 0xbe, 0xef];
let record = WorkflowRunRecord {
id,
community_id: CommunityId::from_uuid(Uuid::new_v4()),
workflow_id,
status: RunStatus::Running,
trigger_event_id: Some(trigger_event_id.clone()),
current_step: 2,
execution_trace: serde_json::json!([
{ "step": "s1", "status": "completed" }
]),
trigger_context: None,
started_at: Some(now),
completed_at: None,
error_message: None,
error_code: None,
created_at: now,
};
assert_eq!(record.id, id);
assert_eq!(record.workflow_id, workflow_id);
assert_eq!(record.status, RunStatus::Running);
assert_eq!(record.trigger_event_id, Some(trigger_event_id));
assert_eq!(record.current_step, 2);
assert!(record.started_at.is_some());
assert!(record.completed_at.is_none());
assert!(record.error_message.is_none());
}
#[test]
fn workflow_run_record_no_trigger_event() {
let now = Utc::now();
let record = WorkflowRunRecord {
id: Uuid::new_v4(),
community_id: CommunityId::from_uuid(Uuid::new_v4()),
workflow_id: Uuid::new_v4(),
status: RunStatus::Pending,
trigger_event_id: None,
current_step: 0,
execution_trace: serde_json::json!([]),
trigger_context: None,
started_at: None,
completed_at: None,
error_message: None,
error_code: None,
created_at: now,
};
assert!(record.trigger_event_id.is_none());
assert_eq!(record.current_step, 0);
assert!(record.started_at.is_none());
}
#[test]
fn workflow_run_record_failed_with_error_message() {
let now = Utc::now();
let record = WorkflowRunRecord {
id: Uuid::new_v4(),
community_id: CommunityId::from_uuid(Uuid::new_v4()),
workflow_id: Uuid::new_v4(),
status: RunStatus::Failed,
trigger_event_id: None,
current_step: 1,
execution_trace: serde_json::json!([]),
trigger_context: None,
started_at: Some(now),
completed_at: Some(now),
error_message: Some("step timeout exceeded".to_owned()),
error_code: Some("step_timeout".to_owned()),
created_at: now,
};
assert_eq!(record.status, RunStatus::Failed);
assert!(record.completed_at.is_some());
assert_eq!(
record.error_message.as_deref(),
Some("step timeout exceeded")
);
}
#[test]
fn workflow_run_record_execution_trace_is_json_array() {
let now = Utc::now();
let trace = serde_json::json!([
{ "step_id": "notify", "status": "completed", "output": { "sent": true } },
{ "step_id": "log", "status": "skipped" }
]);
let record = WorkflowRunRecord {
id: Uuid::new_v4(),
community_id: CommunityId::from_uuid(Uuid::new_v4()),
workflow_id: Uuid::new_v4(),
status: RunStatus::Completed,
trigger_event_id: None,
current_step: 2,
execution_trace: trace.clone(),
trigger_context: None,
started_at: Some(now),
completed_at: Some(now),
error_message: None,
error_code: None,
created_at: now,
};
assert!(record.execution_trace.is_array());
assert_eq!(record.execution_trace.as_array().unwrap().len(), 2);
}
#[test]
fn workflow_run_record_clone_is_independent() {
let now = Utc::now();
let record = WorkflowRunRecord {
id: Uuid::new_v4(),
community_id: CommunityId::from_uuid(Uuid::new_v4()),
workflow_id: Uuid::new_v4(),
status: RunStatus::Pending,
trigger_event_id: None,
current_step: 0,
execution_trace: serde_json::json!([]),
trigger_context: None,
started_at: None,
completed_at: None,
error_message: None,
error_code: None,
created_at: now,
};
let mut cloned = record.clone();
cloned.status = RunStatus::Running;
assert_eq!(record.status, RunStatus::Pending);
assert_eq!(cloned.status, RunStatus::Running);
}
// -- ApprovalRecord -------------------------------------------------------
#[test]
fn approval_record_fields_are_accessible() {
let workflow_id = Uuid::new_v4();
let run_id = Uuid::new_v4();
let expires_at = Utc.with_ymd_and_hms(2026, 12, 31, 23, 59, 59).unwrap();
let now = Utc::now();
let record = ApprovalRecord {
token: b"abc123def456abc123def456abc123de".to_vec(),
workflow_id,
run_id,
step_id: "request_approval".to_owned(),
step_index: 1,
approver_spec: "@engineering-lead".to_owned(),
status: ApprovalStatus::Pending,
approver_pubkey: None,
note: None,
expires_at,
created_at: now,
};
assert_eq!(record.token, b"abc123def456abc123def456abc123de");
assert_eq!(record.workflow_id, workflow_id);
assert_eq!(record.run_id, run_id);
assert_eq!(record.step_id, "request_approval");
assert_eq!(record.step_index, 1);
assert_eq!(record.approver_spec, "@engineering-lead");
assert_eq!(record.status, ApprovalStatus::Pending);
assert!(record.approver_pubkey.is_none());
assert!(record.note.is_none());
}
#[test]
fn approval_record_granted_with_pubkey_and_note() {
let now = Utc::now();
let approver_pubkey = vec![0xca; 32];
let record = ApprovalRecord {
token: b"token-granted".to_vec(),
workflow_id: Uuid::new_v4(),
run_id: Uuid::new_v4(),
step_id: "gate".to_owned(),
step_index: 0,
approver_spec: "@manager".to_owned(),
status: ApprovalStatus::Granted,
approver_pubkey: Some(approver_pubkey.clone()),
note: Some("Looks good, approved.".to_owned()),
expires_at: now,
created_at: now,
};
assert_eq!(record.status, ApprovalStatus::Granted);
assert_eq!(record.approver_pubkey, Some(approver_pubkey));
assert_eq!(record.note.as_deref(), Some("Looks good, approved."));
}
#[test]
fn approval_record_denied_with_note() {
let now = Utc::now();
let record = ApprovalRecord {
token: b"token-denied".to_vec(),
workflow_id: Uuid::new_v4(),
run_id: Uuid::new_v4(),
step_id: "gate".to_owned(),
step_index: 0,
approver_spec: "@manager".to_owned(),
status: ApprovalStatus::Denied,
approver_pubkey: Some(vec![0xbb; 32]),
note: Some("Not ready for production.".to_owned()),
expires_at: now,
created_at: now,
};
assert_eq!(record.status, ApprovalStatus::Denied);
assert!(record.note.is_some());
}
#[test]
fn approval_record_clone_is_independent() {
let now = Utc::now();
let record = ApprovalRecord {
token: b"original-token".to_vec(),
workflow_id: Uuid::new_v4(),
run_id: Uuid::new_v4(),
step_id: "gate".to_owned(),
step_index: 0,
approver_spec: "@lead".to_owned(),
status: ApprovalStatus::Pending,
approver_pubkey: None,
note: None,
expires_at: now,
created_at: now,
};
let mut cloned = record.clone();
cloned.status = ApprovalStatus::Granted;
assert_eq!(record.status, ApprovalStatus::Pending);
assert_eq!(cloned.status, ApprovalStatus::Granted);
}
// -- Scheduled workflow claim confinement ---------------------------------
//
// RECONCILED spec (supersedes the earlier S1 lock; Eva/Max 2026-06-27).
//
// The earlier S1 lock asserted "`workflow_id` is globally unique, so the
// claim resolves community server-side from `workflow_id` alone and the
// caller never names it." The final schema does NOT have that property:
// `workflows` PK is `(community_id, id)` and `scheduled_workflow_fires` is
// keyed/FK'd by `(community_id, workflow_id, scheduled_for)`. Duplicate
// workflow UUIDs across communities are explicitly allowed (and pinned by
// the Issue-4 confinement tests below). So resolve-from-id-alone is both
// unimplementable and unsafe: `WHERE w.id = $1` matches every community
// holding that UUID and fans one claim across all of them.
//
// The invariant that survives is NOT "the claim never receives community";
// it is "the community used for the claim is server provenance, never
// client-controlled." For the global scheduler scan that provenance is the
// `workflow.community_id` returned by `list_all_enabled_workflows()`. The
// claim therefore takes `community_id` and binds
// `WHERE w.community_id = $1 AND w.id = $2`, confining the claim row to the
// intended tenant.
//
// 1. `workflows.community_id` is row-owned, NOT NULL, immutable.
// 2. The claim binds `(community_id, workflow_id)` of the workflow row.
// 3. Claim uniqueness is `(community_id, workflow_id, scheduled_for)`.
// 4. `latest_scheduled_workflow_fire` / `attach_scheduled_workflow_run`
// are already community-scoped; `claim` now matches.
//
// `claim_confined_to_its_community` is the confinement lock: a dup workflow
// UUID in A and B must claim independently (claiming A/id leaves B/id
// claimable). The other two tests are characterization guards: same-window
// race must yield exactly one winner, and pruning below the largest
// interval breaks `latest_*` (the §5c retention rule Sami flagged).
use crate::user::ensure_user;
const TEST_DB_URL: &str = "postgres://buzz:buzz_dev@localhost:5432/buzz"; // sadscan:disable np.postgres.1 -- local test-only credentials
async fn setup_pool() -> PgPool {
let database_url = std::env::var("BUZZ_TEST_DATABASE_URL")
.or_else(|_| std::env::var("DATABASE_URL"))
.unwrap_or_else(|_| TEST_DB_URL.to_owned());
PgPool::connect(&database_url)
.await
.expect("connect to test DB")
}
/// Insert a community with a unique host. Returns its `CommunityId`.
async fn make_community(pool: &PgPool) -> CommunityId {
let id = Uuid::new_v4();
let host = format!("test-{}.example", id.simple());
sqlx::query("INSERT INTO communities (id, host) VALUES ($1, $2)")
.bind(id)
.bind(&host)
.execute(pool)
.await
.expect("insert community");
CommunityId::from_uuid(id)
}
/// Insert a channel under a community. Returns the channel id.
async fn make_channel(pool: &PgPool, community: CommunityId, owner: &[u8]) -> Uuid {
let id = Uuid::new_v4();
sqlx::query(
r#"
INSERT INTO channels (id, community_id, name, created_by)
VALUES ($1, $2, $3, $4)
"#,
)
.bind(id)
.bind(community.as_uuid())
.bind(format!("ch-{}", id.simple()))
.bind(owner)
.execute(pool)
.await
.expect("insert channel");
id
}
/// Insert a workflow whose tenant is `community`'s channel. Returns the
/// workflow id and the owning community for callers that want to assert
/// the resolved tenant.
async fn make_workflow_in(pool: &PgPool, community: CommunityId) -> (Uuid, CommunityId) {
let owner = vec![0xa1; 32];
ensure_user(pool, community, &owner)
.await
.expect("ensure owner");
let channel_id = make_channel(pool, community, &owner).await;
let workflow_id = create_workflow(
pool,
community,
Some(channel_id),
&owner,
"f1-attack-workflow",
r#"{"trigger":{"on":"schedule"},"steps":[]}"#,
&[0u8; 32],
)
.await
.expect("create workflow");
(workflow_id, community)
}
/// Confinement: a duplicate workflow UUID existing in both community A and
/// community B must claim independently. Claiming `(A, id, t)` must NOT
/// consume `(B, id, t)` — B's identical instant stays claimable, and the
/// A-claim's resolved community is A (server provenance), never B.
///
/// This is the reconciliation of the old S1 lock with the real
/// `(community_id, id)` schema: because `id` is not globally unique, the
/// claim binds `WHERE w.community_id = $1 AND w.id = $2`. With the old
/// bare-`id` SQL (`WHERE w.id = $1`), a single `INSERT ... SELECT` matched
/// BOTH workflow rows and fanned the claim across A and B — this test goes
/// RED on that regression (B/id is no longer independently claimable).
#[tokio::test]
#[ignore = "requires Postgres"]
async fn claim_confined_to_its_community() {
let pool = setup_pool().await;
let community_a = make_community(&pool).await;
let community_b = make_community(&pool).await;
// Same workflow UUID + same channel UUID in both communities — the PK
// is `(community_id, id)`, so the collision is structurally allowed.
let workflow_id = Uuid::new_v4();
let channel_id = Uuid::new_v4();
insert_workflow_with_ids(&pool, community_a, workflow_id, channel_id, "sched-a").await;
insert_workflow_with_ids(&pool, community_b, workflow_id, channel_id, "sched-b").await;
let scheduled_for = Utc.with_ymd_and_hms(2026, 6, 27, 0, 0, 0).unwrap();
// Claim A/id/t.
let claim_a = claim_scheduled_workflow_fire(&pool, community_a, workflow_id, scheduled_for)
.await
.expect("claim A should not error")
.expect("claim A should win");
assert_eq!(
claim_a.community_id, community_a,
"A-claim must resolve to community A (server provenance)"
);
assert_eq!(claim_a.workflow_id, workflow_id);
assert_eq!(claim_a.scheduled_for, scheduled_for);
// B/id/t must still be claimable — A's claim did not touch B's row.
let claim_b = claim_scheduled_workflow_fire(&pool, community_b, workflow_id, scheduled_for)
.await
.expect("claim B should not error")
.expect("claim B must still win — A's claim must not have consumed B's instant");
assert_eq!(
claim_b.community_id, community_b,
"B-claim must resolve to community B"
);
// And a second A-claim for the same instant must now lose (dedup holds
// within the community).
let claim_a_again =
claim_scheduled_workflow_fire(&pool, community_a, workflow_id, scheduled_for)
.await
.expect("second A-claim should not error");
assert!(
claim_a_again.is_none(),
"the same (A, id, t) instant must not be claimable twice"
);
}
/// Same `(community_id, workflow_id, scheduled_for)` claimed concurrently by
/// N tasks must yield exactly one `Some` winner. Post-reconciliation the
/// claim key is `(community_id, workflow_id, scheduled_for)`; `community_id`
/// is server provenance, not a client-named label. Characterization guard:
/// protects the dedup boundary against regressions in the claim SQL.
#[tokio::test]
#[ignore = "requires Postgres"]
async fn concurrent_same_window_claims_exactly_one_wins() {
let pool = setup_pool().await;
let community = make_community(&pool).await;
let (workflow_id, _) = make_workflow_in(&pool, community).await;
let scheduled_for = Utc.with_ymd_and_hms(2026, 6, 27, 0, 1, 0).unwrap();
const N: usize = 8;
let mut handles = Vec::with_capacity(N);
for _ in 0..N {
let pool = pool.clone();
handles.push(tokio::spawn(async move {
claim_scheduled_workflow_fire(&pool, community, workflow_id, scheduled_for).await
}));
}
let mut winners = 0usize;
for h in handles {
let result = h.await.expect("task did not panic").expect("claim ok");
if result.is_some() {
winners += 1;
}
}
assert_eq!(
winners, 1,
"exactly one task must win the claim race for (workflow_id, scheduled_for)"
);
}
/// `attach_scheduled_workflow_run` links a won claim to the run it created.
/// This is the regression for the missing `scheduled_workflow_fires.
/// workflow_run_id` column: before the schema added it, the UPDATE failed at
/// runtime with `column "workflow_run_id" does not exist`, so the audit link
/// silently never populated and the scheduler warned on every fire. This test
/// proves the column is present, the attach writes it, and the
/// `workflow_run_id IS NULL` guard makes a second attach a no-op. It is RED
/// without the migration column.
#[tokio::test]
#[ignore = "requires Postgres"]
async fn attach_links_run_to_claim_and_is_idempotent() {
let pool = setup_pool().await;
let community = make_community(&pool).await;
let (workflow_id, _) = make_workflow_in(&pool, community).await;
let scheduled_for = Utc.with_ymd_and_hms(2026, 6, 27, 0, 2, 0).unwrap();
// Win the claim for this instant.
claim_scheduled_workflow_fire(&pool, community, workflow_id, scheduled_for)
.await
.expect("claim ok")
.expect("claim wins");
// Create the run the won claim is responsible for, then attach it.
let run_id = create_workflow_run(&pool, community, workflow_id, None, None)
.await
.expect("create run ok");
let attached =
attach_scheduled_workflow_run(&pool, community, workflow_id, scheduled_for, run_id)
.await
.expect("attach ok");
assert!(attached, "first attach must update the claim row");
// The column is populated with the run id.
let linked: Option<Uuid> = sqlx::query_scalar(
"SELECT workflow_run_id FROM scheduled_workflow_fires \
WHERE community_id = $1 AND workflow_id = $2 AND scheduled_for = $3",
)
.bind(community.as_uuid())
.bind(workflow_id)
.bind(scheduled_for)
.fetch_one(&pool)
.await
.expect("row exists");
assert_eq!(
linked,
Some(run_id),
"the claim row must now point at the run it created"
);
// A second attach is a no-op: the `workflow_run_id IS NULL` guard means
// an already-linked claim is never re-pointed to a different run.
let other_run = create_workflow_run(&pool, community, workflow_id, None, None)
.await
.expect("create second run ok");
let reattached =
attach_scheduled_workflow_run(&pool, community, workflow_id, scheduled_for, other_run)
.await
.expect("second attach ok");
assert!(
!reattached,
"attach must not overwrite an already-linked claim row"
);
}
/// Documents the retention-vs-interval coupling Sami flagged for §5c:
/// pruning every claim below the workflow's interval makes
/// `latest_scheduled_workflow_fire` return `None`, which re-introduces the
/// per-pod-clock anchor bug F5 was meant to fix. Test is GREEN today and
/// MUST stay green — it pins the deployment-config rule that the janitor
/// cutoff must exceed `MAX(interval_secs) + safety margin`. If a future
/// change makes `latest_*` resilient to pruning (e.g. by reading the most
/// recent workflow_run instead, or by retaining a sentinel row), this
/// test's assertion encodes the contract that must be updated alongside.
///
/// Test isolation: the prune primitive is global (filters only on
/// `claimed_at`), so to avoid colliding with parallel claim tests we
/// back-date this workflow's `claimed_at` into the deep past and use a
/// past cutoff that cannot match any other test's `claimed_at = NOW()`.
#[tokio::test]
#[ignore = "requires Postgres"]
async fn latest_after_prune_below_interval_breaks_anchor() {
let pool = setup_pool().await;
let community = make_community(&pool).await;
let (workflow_id, _) = make_workflow_in(&pool, community).await;
let scheduled_for = Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap();
claim_scheduled_workflow_fire(&pool, community, workflow_id, scheduled_for)
.await
.expect("claim ok")
.expect("first claim wins");
// Backdate this row's `claimed_at` so the global prune below targets
// only this workflow's row and cannot race-delete other tests' rows.
let backdated_claimed_at = Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap();
sqlx::query(
"UPDATE scheduled_workflow_fires SET claimed_at = $1 \
WHERE community_id = $2 AND workflow_id = $3 AND scheduled_for = $4",
)
.bind(backdated_claimed_at)
.bind(community.as_uuid())
.bind(workflow_id)
.bind(scheduled_for)
.execute(&pool)
.await
.expect("backdate ok");
let latest_before = latest_scheduled_workflow_fire(&pool, community, workflow_id)
.await
.expect("latest ok");
assert_eq!(
latest_before,
Some(scheduled_for),
"latest must reflect the claim before pruning",
);
// Janitor cutoff above only the back-dated row: prunes the anchor row
// without touching anything claimed at wall-clock NOW.
let cutoff = backdated_claimed_at + chrono::Duration::seconds(1);
let pruned = prune_scheduled_workflow_fires_before(&pool, cutoff)
.await
.expect("prune ok");
assert!(
pruned >= 1,
"expected at least one row pruned, got {pruned}"
);
let latest_after = latest_scheduled_workflow_fire(&pool, community, workflow_id)
.await
.expect("latest ok");
assert_eq!(
latest_after, None,
"pruning below the largest interval breaks the DB anchor; \
retention cutoff MUST exceed MAX(interval_secs) + safety margin (§5c)",
);
}
// -- Issue 4: workflow / approval community confinement -------------------
/// Insert a workflow under `community` with a caller-chosen `id` and
/// `channel_id`, so two communities can be given the *same* workflow UUID
/// and channel UUID (the PK is `(community_id, id)`, which structurally
/// allows the collision). Returns nothing; callers already hold the ids.
async fn insert_workflow_with_ids(
pool: &PgPool,
community: CommunityId,
id: Uuid,
channel_id: Uuid,
name: &str,
) {
let owner = vec![0xb2; 32];
ensure_user(pool, community, &owner)
.await
.expect("ensure owner");
// The channel must exist first: `workflows.channel_id` is a composite FK
// to `(community_id, channel_id)`.
sqlx::query(
r#"
INSERT INTO channels (id, community_id, name, created_by)
VALUES ($1, $2, $3, $4)
"#,
)
.bind(channel_id)
.bind(community.as_uuid())
.bind(format!("ch-{}", channel_id.simple()))
.bind(&owner)
.execute(pool)
.await
.expect("insert channel");
sqlx::query(
r#"
INSERT INTO workflows
(id, community_id, name, owner_pubkey, channel_id, definition, definition_hash, status, enabled)
VALUES ($1, $2, $3, $4, $5, $6::jsonb, $7, 'active', TRUE)
"#,
)
.bind(id)
.bind(community.as_uuid())
.bind(name)
.bind(&owner)
.bind(channel_id)
.bind(r#"{"trigger":{"on":"webhook"},"steps":[]}"#)
.bind(&[0u8; 32][..])
.execute(pool)
.await
.expect("insert workflow");
}
/// Issue 4 (workflow identity): the same workflow UUID and channel UUID can
/// exist in communities A and B (PK `(community_id, id)`). A request-scoped
/// `get_workflow` / `list_enabled_channel_workflows` MUST return only the
/// row owned by the bound community — never B's colliding row for an
/// A-scoped lookup. Pre-fix these bound only `id` / `channel_id`, so a
/// B-host request (or a webhook/manual trigger satisfying membership against
/// B's colliding channel) could load and drive A's workflow.
#[tokio::test]
#[ignore = "requires Postgres"]
async fn workflow_lookup_is_confined_to_its_community() {
let pool = setup_pool().await;
let community_a = make_community(&pool).await;
let community_b = make_community(&pool).await;
// Same workflow UUID and channel UUID in both communities.
let shared_workflow_id = Uuid::new_v4();
let shared_channel_id = Uuid::new_v4();
insert_workflow_with_ids(
&pool,
community_a,
shared_workflow_id,
shared_channel_id,
"wf-A",
)
.await;
insert_workflow_with_ids(
&pool,
community_b,
shared_workflow_id,
shared_channel_id,
"wf-B",
)
.await;
// Scoped get returns each community's own row, never the other's.
let from_a = get_workflow(&pool, community_a, shared_workflow_id)
.await
.expect("A's workflow exists");
let from_b = get_workflow(&pool, community_b, shared_workflow_id)
.await
.expect("B's workflow exists");
assert_eq!(
from_a.community_id, community_a,
"A lookup must resolve A's row"
);
assert_eq!(from_a.name, "wf-A");
assert_eq!(
from_b.community_id, community_b,
"B lookup must resolve B's row"
);
assert_eq!(from_b.name, "wf-B");
// A workflow that exists ONLY in B must be NotFound under A.
let b_only_id = Uuid::new_v4();
let b_only_channel = Uuid::new_v4();
insert_workflow_with_ids(&pool, community_b, b_only_id, b_only_channel, "wf-B-only").await;
let cross = get_workflow(&pool, community_a, b_only_id).await;
assert!(
matches!(cross, Err(DbError::NotFound(_))),
"A must not see B's workflow by id: {cross:?}"
);
// The channel listing is confined too: A's channel listing yields only
// A's workflow even though B has the same channel UUID.
let listed_a = list_enabled_channel_workflows(&pool, community_a, shared_channel_id)
.await
.expect("list A");
assert_eq!(
listed_a.len(),
1,
"A's channel listing must contain exactly A's workflow"
);
assert_eq!(listed_a[0].community_id, community_a);
assert_eq!(listed_a[0].name, "wf-A");
}
/// Issue 4 (workflow lifecycle): deleting `A/id` must not delete `B/id`
/// when both communities hold the same workflow UUID. Pre-fix the workflow
/// delete predicated only on `id`, so a NIP-09 a-tag deletion in one
/// community would erase the colliding workflow in every community.
#[tokio::test]
#[ignore = "requires Postgres"]
async fn workflow_delete_is_confined_to_its_community() {
let pool = setup_pool().await;
let community_a = make_community(&pool).await;
let community_b = make_community(&pool).await;
let shared_workflow_id = Uuid::new_v4();
insert_workflow_with_ids(
&pool,
community_a,
shared_workflow_id,
Uuid::new_v4(),
"wf-A",
)
.await;
insert_workflow_with_ids(
&pool,
community_b,
shared_workflow_id,
Uuid::new_v4(),
"wf-B",
)
.await;
crate::Db::from_pool(pool.clone())
.delete_workflow_by_coordinate(
community_a,
&[0xb2; 32],
&shared_workflow_id.to_string(),
chrono::Utc::now().timestamp(),
)
.await
.expect("delete A's workflow");
// A's row is gone; B's identical-UUID row survives untouched.
assert!(
matches!(
get_workflow(&pool, community_a, shared_workflow_id).await,
Err(DbError::NotFound(_))
),
"A's workflow must be deleted"
);
let surviving_b = get_workflow(&pool, community_b, shared_workflow_id)
.await
.expect("B's workflow must survive A's delete");
assert_eq!(surviving_b.community_id, community_b);
assert_eq!(surviving_b.name, "wf-B");
}
/// Issue 4 (approval path): the same approval token can hash to the same
/// bytes in A and B (PK `(community_id, token)`). A scoped grant/deny acting
/// on `A/token` MUST NOT touch `B/token`. Pre-fix the approval helpers
/// predicated only on `token`, so granting one community's approval would
/// silently resolve another's colliding gate.
#[tokio::test]
#[ignore = "requires Postgres"]
async fn approval_is_confined_to_its_community() {
let pool = setup_pool().await;
let community_a = make_community(&pool).await;
let community_b = make_community(&pool).await;
// Same workflow + run + token in both communities.
let workflow_id = Uuid::new_v4();
let channel_id = Uuid::new_v4();
insert_workflow_with_ids(&pool, community_a, workflow_id, channel_id, "wf-A").await;
insert_workflow_with_ids(&pool, community_b, workflow_id, Uuid::new_v4(), "wf-B").await;
let run_a = create_workflow_run(&pool, community_a, workflow_id, None, None)
.await
.expect("run A");
let run_b = create_workflow_run(&pool, community_b, workflow_id, None, None)
.await
.expect("run B");
let token = "shared-approval-token";
let expires = Utc::now() + chrono::Duration::hours(1);
create_approval(
&pool,
CreateApprovalParams {
community_id: community_a,
token,
workflow_id,
run_id: run_a,
step_id: "gate",
step_index: 0,
approver_spec: "@anyone",
expires_at: expires,
},
)
.await
.expect("create approval A");
create_approval(
&pool,
CreateApprovalParams {
community_id: community_b,
token,
workflow_id,
run_id: run_b,
step_id: "gate",
step_index: 0,
approver_spec: "@anyone",
expires_at: expires,
},
)
.await
.expect("create approval B");
// Scoped read returns each community's own approval (its own run id).
let read_a = get_approval(&pool, community_a, token)
.await
.expect("read A");
let read_b = get_approval(&pool, community_b, token)
.await
.expect("read B");
assert_eq!(read_a.run_id, run_a, "A read must resolve A's approval");
assert_eq!(read_b.run_id, run_b, "B read must resolve B's approval");
// Granting A/token must NOT act on B/token.
let approver = vec![0xc3; 32];
let granted = update_approval(
&pool,
community_a,
token,
ApprovalStatus::Granted,
Some(&approver),
None,
)
.await
.expect("grant A");
assert!(granted, "A's approval must be granted");
let after_a = get_approval(&pool, community_a, token)
.await
.expect("re-read A");
let after_b = get_approval(&pool, community_b, token)
.await
.expect("re-read B");
assert_eq!(after_a.status, ApprovalStatus::Granted, "A is now granted");
assert_eq!(
after_b.status,
ApprovalStatus::Pending,
"B's approval must remain pending after A is granted"
);
}
// -- SEC-006: disable-on-membership-loss primitive -------------------------
/// `disable_workflows_for_owner_in_channel` must disable exactly the
/// departing owner's enabled workflows in that channel — not other owners'
/// workflows, not the same owner's workflows in other channels — and be
/// idempotent. Disabled workflows must drop out of the trigger-path list.
#[tokio::test]
#[ignore = "requires Postgres"]
async fn disable_for_owner_scopes_to_owner_and_channel() {
let pool = setup_pool().await;
let community = make_community(&pool).await;
let departing = vec![0xd1; 32];
let staying = vec![0xd2; 32];
ensure_user(&pool, community, &departing)
.await
.expect("ensure departing");
ensure_user(&pool, community, &staying)
.await
.expect("ensure staying");
let channel_a = make_channel(&pool, community, &departing).await;
let channel_b = make_channel(&pool, community, &departing).await;
let def = r#"{"trigger":{"on":"message_posted"},"steps":[]}"#;
let wf_departing_a = create_workflow(
&pool,
community,
Some(channel_a),
&departing,
"departing-a",
def,
&[0u8; 32],
)
.await
.expect("wf departing a");
let wf_departing_b = create_workflow(
&pool,
community,
Some(channel_b),
&departing,
"departing-b",
def,
&[0u8; 32],
)
.await
.expect("wf departing b");
let wf_staying_a = create_workflow(
&pool,
community,
Some(channel_a),
&staying,
"staying-a",
def,
&[0u8; 32],
)
.await
.expect("wf staying a");
let disabled =
disable_workflows_for_owner_in_channel(&pool, community, channel_a, &departing)
.await
.expect("disable");
assert_eq!(
disabled, 1,
"exactly the departing owner's channel-A workflow"
);
// Idempotent: second call finds nothing enabled.
let again = disable_workflows_for_owner_in_channel(&pool, community, channel_a, &departing)
.await
.expect("disable again");
assert_eq!(again, 0, "second disable must be a no-op");
let enabled_a = list_enabled_channel_workflows(&pool, community, channel_a)
.await
.expect("list channel a");
let enabled_a_ids: Vec<Uuid> = enabled_a.iter().map(|w| w.id).collect();
assert!(
!enabled_a_ids.contains(&wf_departing_a),
"departing owner's workflow must no longer be trigger-eligible"
);
assert!(
enabled_a_ids.contains(&wf_staying_a),
"other owners' workflows in the channel must be untouched"
);
let enabled_b = list_enabled_channel_workflows(&pool, community, channel_b)
.await
.expect("list channel b");
assert!(
enabled_b.iter().any(|w| w.id == wf_departing_b),
"same owner's workflow in a different channel must be untouched"
);
}
}