Skip to content

Commit 2693e0d

Browse files
wesbillmanCarlMongoPrincess Donut
authored
Make workflow run history authoritative in Desktop (block#5780)
## Summary - persist stable workflow run `error_code` values separately from human diagnostics - expose NIP-98 authenticated, channel-authorized run history and approval reads with stable keyset pagination - connect Desktop to those authoritative reads and return the relay-created run ID on trigger - show truthful loading, failure, and pending-trace states, and do not render approval actions from non-actionable stored hashes ## Validation - pre-push `branch-skew`, `desktop-typecheck`, `desktop-test`, `rust-tests`, `desktop-tauri-checks`, and `desktop-check` all passed on `a097dbe5f` - Desktop tests: 4,761 passed, 0 failed - `cargo check -p buzz-relay` - `git diff --check` ## Remaining gate This does not claim a relay-backed Playwright workflow journey. The browser relay bridge still routes workflow invokes through in-memory handlers; that production-shaped acceptance gate remains follow-up work before Workflows can leave preview. --------- Signed-off-by: Wes <[email protected]> Co-authored-by: Carl <c7ebe626f000404285d3686e1dc74cc07cc60a9754a150041ba132e14bd3e2ec@buzz.block.builderlab.xyz> Co-authored-by: Mongo <5c25403eab7271f9f94ddd4f2b270e8cac2c92e2c830c51877cca6ec974ffb3f@buzz.block.builderlab.xyz> Co-authored-by: Princess Donut <68157ebd23b3897c1991015c3038658ea916200c67d3a54620b0754d1b92f6e0@buzz.block.builderlab.xyz>
1 parent 45f4b91 commit 2693e0d

21 files changed

Lines changed: 726 additions & 139 deletions

File tree

‎crates/buzz-db/src/lib.rs‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4051,6 +4051,27 @@ impl Db {
40514051
workflow::list_workflow_runs(&self.pool, community_id, workflow_id, limit).await
40524052
}
40534053

4054+
/// List one keyset-paginated page of workflow runs.
4055+
#[datastore_span(name = "list_workflow_runs_page", system = "postgresql")]
4056+
pub async fn list_workflow_runs_page(
4057+
&self,
4058+
community_id: CommunityId,
4059+
workflow_id: Uuid,
4060+
before: Option<chrono::DateTime<chrono::Utc>>,
4061+
before_id: Option<Uuid>,
4062+
limit: i64,
4063+
) -> Result<Vec<workflow::WorkflowRunRecord>> {
4064+
workflow::list_workflow_runs_page(
4065+
&self.pool,
4066+
community_id,
4067+
workflow_id,
4068+
before,
4069+
before_id,
4070+
limit,
4071+
)
4072+
.await
4073+
}
4074+
40544075
/// Update a workflow run's status.
40554076
#[datastore_span(name = "update_workflow_run", system = "postgresql")]
40564077
pub async fn update_workflow_run(
@@ -4060,7 +4081,7 @@ impl Db {
40604081
status: workflow::RunStatus,
40614082
current_step: i32,
40624083
trace: &serde_json::Value,
4063-
error: Option<&str>,
4084+
failure: Option<workflow::WorkflowRunFailure<'_>>,
40644085
) -> Result<()> {
40654086
workflow::update_workflow_run(
40664087
&self.pool,
@@ -4069,7 +4090,7 @@ impl Db {
40694090
status,
40704091
current_step,
40714092
trace,
4072-
error,
4093+
failure,
40734094
)
40744095
.await
40754096
}

‎crates/buzz-db/src/migration.rs‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ mod tests {
625625
let mut migrations: Vec<_> = MIGRATOR.iter().collect();
626626
migrations.sort_by_key(|migration| migration.version);
627627

628-
assert_eq!(migrations.len(), 30);
628+
assert_eq!(migrations.len(), 31);
629629
assert_eq!(migrations[0].version, 1);
630630
assert_eq!(&*migrations[0].description, "initial schema");
631631
assert!(migrations[0]
@@ -1038,6 +1038,27 @@ mod tests {
10381038
assert!(deletion_recovery.contains("SET LOCAL lock_timeout = '5s'"));
10391039
}
10401040

1041+
#[test]
1042+
fn workflow_run_error_codes_are_additive_and_backfilled_without_parsing_diagnostics() {
1043+
let mut migrations: Vec<_> = MIGRATOR.iter().collect();
1044+
migrations.sort_by_key(|migration| migration.version);
1045+
1046+
assert_eq!(migrations[30].version, 31);
1047+
let sql = migrations[30].sql.as_str();
1048+
assert!(sql.contains("ALTER TABLE workflow_runs ADD COLUMN error_code TEXT"));
1049+
assert!(sql.contains("SET error_code = 'legacy_unclassified'"));
1050+
assert!(sql.contains("status IN ('failed', 'cancelled')"));
1051+
assert!(!sql.contains("error_message LIKE"));
1052+
assert!(!MIGRATOR
1053+
.iter()
1054+
.find(|migration| migration.version == 1)
1055+
.expect("initial migration")
1056+
.sql
1057+
.as_str()
1058+
.contains("error_code"));
1059+
assert!(include_str!("../../../schema/schema.sql").contains("error_code TEXT"));
1060+
}
1061+
10411062
#[test]
10421063
fn migration_lint_detects_tables_missing_community_id_by_default() {
10431064
let sql = r#"

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

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,11 @@ pub struct WorkflowRunRecord {
216216
pub started_at: Option<DateTime<Utc>>,
217217
/// When execution finished (success or failure).
218218
pub completed_at: Option<DateTime<Utc>>,
219-
/// Error message if the run failed.
219+
/// Redacted human-readable diagnostic for failed or cancelled runs.
220220
pub error_message: Option<String>,
221+
/// Stable machine-readable failure or cancellation classification.
222+
/// Kept separate from `error_message` so callers never parse diagnostics.
223+
pub error_code: Option<String>,
221224
/// When the run record was created.
222225
pub created_at: DateTime<Utc>,
223226
}
@@ -831,7 +834,7 @@ pub async fn get_workflow_run(
831834
let row = sqlx::query(
832835
r#"
833836
SELECT community_id, id, workflow_id, status::text AS status, trigger_event_id, current_step,
834-
execution_trace, trigger_context, started_at, completed_at, error_message, created_at
837+
execution_trace, trigger_context, started_at, completed_at, error_message, error_code, created_at
835838
FROM workflow_runs
836839
WHERE community_id = $1 AND id = $2
837840
"#,
@@ -845,34 +848,67 @@ pub async fn get_workflow_run(
845848
row_to_run_record(row)
846849
}
847850

848-
/// List runs for a workflow, newest first, up to `limit` rows.
849-
pub async fn list_workflow_runs(
851+
/// List runs for a workflow using a stable newest-first keyset.
852+
///
853+
/// Rows are ordered by `(created_at DESC, id DESC)`. A cursor is valid only
854+
/// when both `before` and `before_id` are supplied; callers should pass the
855+
/// final row from the previous page. `limit` is clamped to the shared list
856+
/// bounds.
857+
pub async fn list_workflow_runs_page(
850858
pool: &PgPool,
851859
community_id: CommunityId,
852860
workflow_id: Uuid,
861+
before: Option<DateTime<Utc>>,
862+
before_id: Option<Uuid>,
853863
limit: i64,
854864
) -> Result<Vec<WorkflowRunRecord>> {
855-
let limit = limit.min(1000);
865+
let limit = limit.clamp(1, LIST_MAX_LIMIT);
856866
let rows = sqlx::query(
857867
r#"
858868
SELECT community_id, id, workflow_id, status::text AS status, trigger_event_id, current_step,
859-
execution_trace, trigger_context, started_at, completed_at, error_message, created_at
869+
execution_trace, trigger_context, started_at, completed_at, error_message, error_code, created_at
860870
FROM workflow_runs
861871
WHERE community_id = $1 AND workflow_id = $2
862-
ORDER BY created_at DESC
863-
LIMIT $3
872+
AND (
873+
$3::timestamptz IS NULL
874+
OR $4::uuid IS NULL
875+
OR (created_at, id) < ($3, $4)
876+
)
877+
ORDER BY created_at DESC, id DESC
878+
LIMIT $5
864879
"#,
865880
)
866881
.bind(community_id.as_uuid())
867882
.bind(workflow_id)
883+
.bind(before)
884+
.bind(before_id)
868885
.bind(limit)
869886
.fetch_all(pool)
870887
.await?;
871888

872889
rows.into_iter().map(row_to_run_record).collect()
873890
}
874891

875-
/// Update run status, current step, execution trace, and optional error message.
892+
/// List runs for a workflow, newest first, up to `limit` rows.
893+
pub async fn list_workflow_runs(
894+
pool: &PgPool,
895+
community_id: CommunityId,
896+
workflow_id: Uuid,
897+
limit: i64,
898+
) -> Result<Vec<WorkflowRunRecord>> {
899+
list_workflow_runs_page(pool, community_id, workflow_id, None, None, limit).await
900+
}
901+
902+
/// Structured failure persisted for a workflow run.
903+
#[derive(Debug, Clone, Copy)]
904+
pub struct WorkflowRunFailure<'a> {
905+
/// Stable machine-readable failure code.
906+
pub code: &'a str,
907+
/// Human-readable failure detail.
908+
pub message: &'a str,
909+
}
910+
911+
/// Update run status, current step, execution trace, and optional failure.
876912
///
877913
/// Fix C3: `started_at` is set when the NEW status is 'running' and `started_at`
878914
/// has not yet been stamped (IS NULL). The original code read `status` from the
@@ -885,26 +921,31 @@ pub async fn update_workflow_run(
885921
status: RunStatus,
886922
current_step: i32,
887923
trace: &serde_json::Value,
888-
error: Option<&str>,
924+
failure: Option<WorkflowRunFailure<'_>>,
889925
) -> Result<()> {
890926
let status_str = status.to_string();
927+
let (error_code, error) = failure
928+
.map(|failure| (Some(failure.code), Some(failure.message)))
929+
.unwrap_or((None, None));
891930
let affected = sqlx::query(
892931
r#"
893932
UPDATE workflow_runs
894933
SET status = $1::run_status,
895934
current_step = $2,
896935
execution_trace = $3,
897-
error_message = $4,
898-
started_at = CASE WHEN $5 = 'running' AND started_at IS NULL
936+
error_code = $4,
937+
error_message = $5,
938+
started_at = CASE WHEN $6 = 'running' AND started_at IS NULL
899939
THEN NOW() ELSE started_at END,
900-
completed_at = CASE WHEN $6 IN ('completed','failed','cancelled')
940+
completed_at = CASE WHEN $7 IN ('completed','failed','cancelled')
901941
THEN NOW() ELSE completed_at END
902-
WHERE community_id = $7 AND id = $8
942+
WHERE community_id = $8 AND id = $9
903943
"#,
904944
)
905945
.bind(&status_str)
906946
.bind(current_step)
907947
.bind(trace)
948+
.bind(error_code)
908949
.bind(error)
909950
.bind(&status_str) // for started_at CASE
910951
.bind(&status_str) // for completed_at CASE
@@ -1169,6 +1210,7 @@ fn row_to_run_record(row: sqlx::postgres::PgRow) -> Result<WorkflowRunRecord> {
11691210
started_at: row.try_get("started_at")?,
11701211
completed_at: row.try_get("completed_at")?,
11711212
error_message: row.try_get("error_message")?,
1213+
error_code: row.try_get("error_code")?,
11721214
created_at: row.try_get("created_at")?,
11731215
})
11741216
}
@@ -1473,6 +1515,7 @@ mod tests {
14731515
started_at: Some(now),
14741516
completed_at: None,
14751517
error_message: None,
1518+
error_code: None,
14761519
created_at: now,
14771520
};
14781521

@@ -1501,6 +1544,7 @@ mod tests {
15011544
started_at: None,
15021545
completed_at: None,
15031546
error_message: None,
1547+
error_code: None,
15041548
created_at: now,
15051549
};
15061550

@@ -1524,6 +1568,7 @@ mod tests {
15241568
started_at: Some(now),
15251569
completed_at: Some(now),
15261570
error_message: Some("step timeout exceeded".to_owned()),
1571+
error_code: Some("step_timeout".to_owned()),
15271572
created_at: now,
15281573
};
15291574

@@ -1555,6 +1600,7 @@ mod tests {
15551600
started_at: Some(now),
15561601
completed_at: Some(now),
15571602
error_message: None,
1603+
error_code: None,
15581604
created_at: now,
15591605
};
15601606

@@ -1577,6 +1623,7 @@ mod tests {
15771623
started_at: None,
15781624
completed_at: None,
15791625
error_message: None,
1626+
error_code: None,
15801627
created_at: now,
15811628
};
15821629

‎crates/buzz-relay/src/api/bridge.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::state::AppState;
2121

2222
use super::{api_error, internal_error, not_found};
2323

24-
async fn enforce_http_admission(
24+
pub(crate) async fn enforce_http_admission(
2525
state: &AppState,
2626
tenant: &TenantContext,
2727
pubkey: &nostr::PublicKey,
@@ -1938,7 +1938,10 @@ pub async fn workflow_webhook(
19381938
buzz_db::workflow::RunStatus::Failed,
19391939
0,
19401940
&serde_json::json!([]),
1941-
Some(&format!("definition parse error: {e}")),
1941+
Some(buzz_db::workflow::WorkflowRunFailure {
1942+
code: "invalid_definition",
1943+
message: &format!("definition parse error: {e}"),
1944+
}),
19421945
)
19431946
.await
19441947
{

‎crates/buzz-relay/src/api/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub mod media;
99
pub mod mesh_demo;
1010
pub mod nip05;
1111
pub mod operator;
12+
pub mod workflows;
1213

1314
// Re-export imeta helpers used by ingest pipeline.
1415
pub use crate::handlers::imeta::{validate_imeta_tags, verify_imeta_blobs};

0 commit comments

Comments
 (0)