diff --git a/crates/buzz-db/src/lib.rs b/crates/buzz-db/src/lib.rs index 9c6647983b..5dcf437c63 100644 --- a/crates/buzz-db/src/lib.rs +++ b/crates/buzz-db/src/lib.rs @@ -4090,6 +4090,7 @@ impl Db { tx: &mut sqlx::Transaction<'_, sqlx::Postgres>, community_id: CommunityId, workflow_id: Uuid, + definition_event_id: Option<&[u8]>, trigger_event_id: Option<&[u8]>, trigger_context: Option<&serde_json::Value>, ) -> Result { @@ -4097,6 +4098,7 @@ impl Db { tx, community_id, workflow_id, + definition_event_id, trigger_event_id, trigger_context, ) @@ -4109,6 +4111,7 @@ impl Db { &self, community_id: CommunityId, workflow_id: Uuid, + definition_event_id: Option<&[u8]>, trigger_event_id: Option<&[u8]>, trigger_context: Option<&serde_json::Value>, ) -> Result { @@ -4116,6 +4119,7 @@ impl Db { &self.pool, community_id, workflow_id, + definition_event_id, trigger_event_id, trigger_context, ) diff --git a/crates/buzz-db/src/migration.rs b/crates/buzz-db/src/migration.rs index 80be5328d3..5872910b11 100644 --- a/crates/buzz-db/src/migration.rs +++ b/crates/buzz-db/src/migration.rs @@ -640,7 +640,7 @@ mod tests { let mut migrations: Vec<_> = MIGRATOR.iter().collect(); migrations.sort_by_key(|migration| migration.version); - assert_eq!(migrations.len(), 33); + assert_eq!(migrations.len(), 34); assert_eq!(migrations[0].version, 1); assert_eq!(&*migrations[0].description, "initial schema"); assert!(migrations[0] @@ -1074,6 +1074,14 @@ mod tests { assert!(workflow_revision.contains("ADD COLUMN definition_event_id BYTEA")); assert!(workflow_revision.contains("octet_length(definition_event_id) = 32")); + // Runs durably inherit the exact signed revision they execute. Existing + // runs remain nullable and execution/resume must fail them closed. + assert_eq!(migrations[33].version, 34); + let run_revision = migrations[33].sql.as_str(); + assert!(run_revision.contains("ALTER TABLE workflow_runs")); + assert!(run_revision.contains("ADD COLUMN definition_event_id BYTEA")); + assert!(run_revision.contains("octet_length(definition_event_id) = 32")); + // Fresh desired-state bootstrap must install the identical executable // fence as migration 0032. CI and isolated relay startup use schema.sql // without running migrations, so drift reopens rolling-deploy races. diff --git a/crates/buzz-db/src/workflow.rs b/crates/buzz-db/src/workflow.rs index f72d076c0a..acdece449d 100644 --- a/crates/buzz-db/src/workflow.rs +++ b/crates/buzz-db/src/workflow.rs @@ -203,6 +203,11 @@ pub struct WorkflowRunRecord { pub community_id: CommunityId, /// The workflow definition that was executed. pub workflow_id: Uuid, + /// Exact owner-signed kind:30620 revision this run executes. + /// + /// NULL is retained only for runs created before the revision-binding + /// migration; execution and resume paths must fail those rows closed. + pub definition_event_id: Option>, /// Current execution status of this run. pub status: RunStatus, /// Raw event ID bytes that triggered this run, if any. @@ -834,6 +839,7 @@ pub async fn create_workflow_run_in_transaction( tx: &mut sqlx::Transaction<'_, sqlx::Postgres>, community_id: CommunityId, workflow_id: Uuid, + definition_event_id: Option<&[u8]>, trigger_event_id: Option<&[u8]>, trigger_context: Option<&serde_json::Value>, ) -> Result { @@ -842,13 +848,14 @@ pub async fn create_workflow_run_in_transaction( 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) + (community_id, id, workflow_id, definition_event_id, status, trigger_event_id, current_step, execution_trace, trigger_context) + VALUES ($1, $2, $3, $4, 'pending', $5, 0, '[]', $6) "#, ) .bind(community_id.as_uuid()) .bind(id) .bind(workflow_id) + .bind(definition_event_id) .bind(trigger_event_id) .bind(trigger_context) .execute(&mut **tx) @@ -866,6 +873,7 @@ pub async fn create_workflow_run( pool: &PgPool, community_id: CommunityId, workflow_id: Uuid, + definition_event_id: Option<&[u8]>, trigger_event_id: Option<&[u8]>, trigger_context: Option<&serde_json::Value>, ) -> Result { @@ -874,13 +882,14 @@ pub async fn create_workflow_run( 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) + (community_id, id, workflow_id, definition_event_id, status, trigger_event_id, current_step, execution_trace, trigger_context) + VALUES ($1, $2, $3, $4, 'pending', $5, 0, '[]', $6) "#, ) .bind(community_id.as_uuid()) .bind(id) .bind(workflow_id) + .bind(definition_event_id) .bind(trigger_event_id) .bind(trigger_context) .execute(pool) @@ -897,7 +906,7 @@ pub async fn get_workflow_run( ) -> Result { let row = sqlx::query( r#" - SELECT community_id, id, workflow_id, status::text AS status, trigger_event_id, current_step, + SELECT community_id, id, workflow_id, definition_event_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 @@ -929,7 +938,7 @@ pub async fn list_workflow_runs_page( 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, + SELECT community_id, id, workflow_id, definition_event_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 @@ -1267,6 +1276,7 @@ fn row_to_run_record(row: sqlx::postgres::PgRow) -> Result { id, community_id: CommunityId::from_uuid(community_id), workflow_id, + definition_event_id: row.try_get("definition_event_id")?, status, trigger_event_id: row.try_get("trigger_event_id")?, current_step: row.try_get("current_step")?, @@ -2033,7 +2043,7 @@ mod tests { .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) + let run_id = create_workflow_run(&pool, community, workflow_id, None, None, None) .await .expect("create run ok"); @@ -2062,7 +2072,7 @@ mod tests { // 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) + let other_run = create_workflow_run(&pool, community, workflow_id, None, None, None) .await .expect("create second run ok"); let reattached = @@ -2294,6 +2304,7 @@ mod tests { &mut aborted, community, workflow_id, + None, Some(&trigger_event_id), None, ) @@ -2339,6 +2350,7 @@ mod tests { &mut retry, community, workflow_id, + None, Some(&trigger_event_id), None, ) @@ -2512,10 +2524,10 @@ mod tests { 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) + let run_a = create_workflow_run(&pool, community_a, workflow_id, None, None, None) .await .expect("run A"); - let run_b = create_workflow_run(&pool, community_b, workflow_id, None, None) + let run_b = create_workflow_run(&pool, community_b, workflow_id, None, None, None) .await .expect("run B"); diff --git a/crates/buzz-relay/src/handlers/command_executor.rs b/crates/buzz-relay/src/handlers/command_executor.rs index 342193d98e..60d5bb2e33 100644 --- a/crates/buzz-relay/src/handlers/command_executor.rs +++ b/crates/buzz-relay/src/handlers/command_executor.rs @@ -1110,6 +1110,7 @@ async fn handle_workflow_trigger( &mut tx, community_id, workflow_id, + Some(&requested_revision), Some(&event_id_bytes), trigger_ctx_json.as_ref(), ) diff --git a/migrations/0034_workflow_run_definition_event_id.sql b/migrations/0034_workflow_run_definition_event_id.sql new file mode 100644 index 0000000000..b89222de2d --- /dev/null +++ b/migrations/0034_workflow_run_definition_event_id.sql @@ -0,0 +1,5 @@ +-- Bind each new workflow run to the exact signed definition it executes. +-- Existing rows remain NULL and all resume/execution paths fail them closed. +ALTER TABLE workflow_runs + ADD COLUMN definition_event_id BYTEA + CHECK (definition_event_id IS NULL OR octet_length(definition_event_id) = 32); diff --git a/schema/schema.sql b/schema/schema.sql index 734cdb315e..f2230e7747 100644 --- a/schema/schema.sql +++ b/schema/schema.sql @@ -393,6 +393,9 @@ CREATE TABLE workflow_runs ( community_id UUID NOT NULL REFERENCES communities(id), id UUID NOT NULL DEFAULT gen_random_uuid(), workflow_id UUID NOT NULL, + definition_event_id BYTEA CHECK ( + definition_event_id IS NULL OR octet_length(definition_event_id) = 32 + ), status run_status NOT NULL DEFAULT 'pending', trigger_event_id BYTEA, current_step INT NOT NULL DEFAULT 0,