szal 1.2.0

Workflow engine — step/flow execution with branching, retry, rollback, and parallel stages
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//! Durable [`ExecutionStore`] backends backed by [`sqlx`].
//!
//! Enables crash-recoverable, queryable execution tracking against SQLite or
//! PostgreSQL. Each backend is feature-gated so the lean default build pulls no
//! database dependencies:
//!
//! - `sqlite` → [`SqliteExecutionStore`]
//! - `postgres` → [`PostgresExecutionStore`]
//!
//! Both expose an async, DB-authoritative API ([`connect`](SqliteExecutionStore::connect),
//! [`migrate`](SqliteExecutionStore::migrate), `save`/`get`/`list`/`remove`) for
//! dashboards and recovery, plus [`engine_sink`](SqliteExecutionStore::engine_sink)
//! which adapts the store to the synchronous
//! [`ExecutionStore`](crate::storage::ExecutionStore) the engine consumes.
//!
//! Records are stored as JSON in a single `szal_executions` table, keyed by
//! execution id and indexed by flow name:
//!
//! ```sql
//! CREATE TABLE szal_executions (
//!     execution_id TEXT PRIMARY KEY,
//!     flow_name    TEXT NOT NULL,
//!     data         TEXT NOT NULL  -- serialized ExecutionRecord
//! );
//! ```
//!
//! ## Engine integration semantics
//!
//! [`engine_sink`](SqliteExecutionStore::engine_sink) returns a sink whose `save`
//! is **fire-and-forget**: the record is mirrored in memory immediately (so the
//! sink's synchronous reads are consistent within this process) and the durable
//! DB write is spawned onto the Tokio runtime. For authoritative cross-process
//! reads, query the async store directly.

use std::collections::HashMap;
use std::sync::{Arc, Mutex, Once, RwLock};

use tokio::sync::mpsc;

use crate::storage::{ExecutionRecord, ExecutionStore};

#[inline]
fn sqlx_err(e: sqlx::Error) -> crate::SzalError {
    crate::SzalError::Other(anyhow::Error::new(e))
}

#[inline]
fn json_err(e: serde_json::Error) -> crate::SzalError {
    crate::SzalError::Other(anyhow::Error::new(e))
}

/// Internal: a store that can durably persist/delete records asynchronously.
///
/// Lets the synchronous [`SpawnSink`] bridge stay generic over the concrete
/// backend without dynamic dispatch on a futures-returning trait.
trait DurableSave: Clone + Send + Sync + 'static {
    fn save_record(&self, record: ExecutionRecord) -> impl std::future::Future<Output = ()> + Send;
    fn delete_record(&self, id: String) -> impl std::future::Future<Output = ()> + Send;
}

/// An ordered durable write applied by the background writer.
enum WriteOp {
    Save(ExecutionRecord),
    Delete(String),
}

/// Synchronous [`ExecutionStore`] bridge over an async sqlx store.
///
/// Writes are mirrored in memory (so the sink's reads are immediately consistent
/// within this process) and forwarded over an ordered channel to a single
/// background task that applies them to the database **in submission order**.
/// Ordering matters: a flow's `Running` save must not overwrite its later
/// `Completed` save. Reads are served from the in-process mirror.
struct SpawnSink<S: DurableSave> {
    store: S,
    mirror: RwLock<HashMap<String, ExecutionRecord>>,
    tx: mpsc::UnboundedSender<WriteOp>,
    rx: Mutex<Option<mpsc::UnboundedReceiver<WriteOp>>>,
    writer_started: Once,
}

impl<S: DurableSave> SpawnSink<S> {
    fn new(store: S) -> Self {
        let (tx, rx) = mpsc::unbounded_channel();
        Self {
            store,
            mirror: RwLock::new(HashMap::new()),
            tx,
            rx: Mutex::new(Some(rx)),
            writer_started: Once::new(),
        }
    }

    /// Lazily start the single background writer on first use. Spawning here
    /// (rather than in `new`) means it always lands inside the caller's Tokio
    /// runtime — the engine calls `save` from within `run`.
    fn ensure_writer(&self) {
        self.writer_started.call_once(|| {
            let Some(mut rx) = self
                .rx
                .lock()
                .expect("execution sink writer lock poisoned")
                .take()
            else {
                return;
            };
            let store = self.store.clone();
            tokio::spawn(async move {
                while let Some(op) = rx.recv().await {
                    match op {
                        WriteOp::Save(record) => store.save_record(record).await,
                        WriteOp::Delete(id) => store.delete_record(id).await,
                    }
                }
            });
        });
    }
}

impl<S: DurableSave> ExecutionStore for SpawnSink<S> {
    fn save(&self, record: ExecutionRecord) {
        self.ensure_writer();
        self.mirror
            .write()
            .expect("execution sink mirror poisoned")
            .insert(record.execution_id.clone(), record.clone());
        let _ = self.tx.send(WriteOp::Save(record));
    }

    fn get(&self, execution_id: &str) -> Option<ExecutionRecord> {
        self.mirror
            .read()
            .expect("execution sink mirror poisoned")
            .get(execution_id)
            .cloned()
    }

    fn list(&self, flow_name: Option<&str>) -> Vec<String> {
        self.mirror
            .read()
            .expect("execution sink mirror poisoned")
            .values()
            .filter(|r| flow_name.is_none_or(|n| r.flow_name == n))
            .map(|r| r.execution_id.clone())
            .collect()
    }

    fn remove(&self, execution_id: &str) -> Option<ExecutionRecord> {
        self.ensure_writer();
        let removed = self
            .mirror
            .write()
            .expect("execution sink mirror poisoned")
            .remove(execution_id);
        if removed.is_some() {
            let _ = self.tx.send(WriteOp::Delete(execution_id.to_owned()));
        }
        removed
    }
}

/// Generate a sqlx-backed execution store for a specific backend.
///
/// The SQL is identical across backends except for bind-parameter placeholders
/// (`?` for SQLite, `$N` for PostgreSQL), so those are supplied per invocation.
macro_rules! sqlx_execution_store {
    (
        $(#[$doc:meta])*
        store: $name:ident,
        pool: $pool:ty,
        connect: $connect:path,
        upsert: $upsert:literal,
        select: $select:literal,
        list_flow: $list_flow:literal,
        delete: $delete:literal $(,)?
    ) => {
        $(#[$doc])*
        #[derive(Clone)]
        pub struct $name {
            pool: $pool,
        }

        impl $name {
            /// Connect to `url` and build a connection pool.
            ///
            /// Does not create the schema — call [`migrate`](Self::migrate) once.
            pub async fn connect(url: &str) -> crate::Result<Self> {
                let pool = $connect(url).await.map_err(sqlx_err)?;
                Ok(Self { pool })
            }

            /// Build a store from an existing sqlx pool.
            #[must_use]
            pub fn from_pool(pool: $pool) -> Self {
                Self { pool }
            }

            /// The underlying sqlx connection pool.
            #[must_use]
            pub fn pool(&self) -> &$pool {
                &self.pool
            }

            /// Create the `szal_executions` table if it does not yet exist.
            pub async fn migrate(&self) -> crate::Result<()> {
                sqlx::query(
                    "CREATE TABLE IF NOT EXISTS szal_executions (\
                     execution_id TEXT PRIMARY KEY, \
                     flow_name TEXT NOT NULL, \
                     data TEXT NOT NULL)",
                )
                .execute(&self.pool)
                .await
                .map_err(sqlx_err)?;
                Ok(())
            }

            /// Persist (insert or update) an execution record.
            pub async fn save(&self, record: &ExecutionRecord) -> crate::Result<()> {
                let data = serde_json::to_string(record).map_err(json_err)?;
                sqlx::query($upsert)
                    .bind(&record.execution_id)
                    .bind(&record.flow_name)
                    .bind(&data)
                    .execute(&self.pool)
                    .await
                    .map_err(sqlx_err)?;
                Ok(())
            }

            /// Load an execution record by id.
            pub async fn get(&self, execution_id: &str) -> crate::Result<Option<ExecutionRecord>> {
                use sqlx::Row;
                let row = sqlx::query($select)
                    .bind(execution_id)
                    .fetch_optional(&self.pool)
                    .await
                    .map_err(sqlx_err)?;
                match row {
                    Some(row) => {
                        let data: String = row.try_get("data").map_err(sqlx_err)?;
                        Ok(Some(serde_json::from_str(&data).map_err(json_err)?))
                    }
                    None => Ok(None),
                }
            }

            /// List execution ids, optionally filtered by flow name.
            pub async fn list(&self, flow_name: Option<&str>) -> crate::Result<Vec<String>> {
                use sqlx::Row;
                let rows = match flow_name {
                    Some(name) => sqlx::query($list_flow)
                        .bind(name)
                        .fetch_all(&self.pool)
                        .await,
                    None => sqlx::query("SELECT execution_id FROM szal_executions")
                        .fetch_all(&self.pool)
                        .await,
                }
                .map_err(sqlx_err)?;
                rows.iter()
                    .map(|r| r.try_get::<String, _>("execution_id").map_err(sqlx_err))
                    .collect()
            }

            /// Remove an execution record, returning it if it existed.
            pub async fn remove(
                &self,
                execution_id: &str,
            ) -> crate::Result<Option<ExecutionRecord>> {
                let existing = self.get(execution_id).await?;
                if existing.is_some() {
                    sqlx::query($delete)
                        .bind(execution_id)
                        .execute(&self.pool)
                        .await
                        .map_err(sqlx_err)?;
                }
                Ok(existing)
            }

            /// Adapt this store to the synchronous
            /// [`ExecutionStore`](crate::storage::ExecutionStore) the engine consumes.
            ///
            /// See the [module docs](crate::sql_store) for the fire-and-forget save
            /// semantics.
            #[must_use]
            pub fn engine_sink(&self) -> Arc<dyn ExecutionStore> {
                Arc::new(SpawnSink::new(self.clone()))
            }
        }

        impl DurableSave for $name {
            async fn save_record(&self, record: ExecutionRecord) {
                if let Err(e) = self.save(&record).await {
                    tracing::error!(
                        execution_id = %record.execution_id,
                        error = %e,
                        "failed to persist execution record"
                    );
                }
            }
            async fn delete_record(&self, id: String) {
                if let Err(e) = self.remove(&id).await {
                    tracing::error!(execution_id = %id, error = %e, "failed to delete execution record");
                }
            }
        }

        impl std::fmt::Debug for $name {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                f.debug_struct(stringify!($name)).finish_non_exhaustive()
            }
        }
    };
}

#[cfg(feature = "sqlite")]
sqlx_execution_store! {
    /// Durable [`ExecutionStore`](crate::storage::ExecutionStore) backed by SQLite.
    ///
    /// ```no_run
    /// use szal::sql_store::SqliteExecutionStore;
    ///
    /// # async fn demo() -> szal::Result<()> {
    /// let store = SqliteExecutionStore::connect("sqlite://executions.db?mode=rwc").await?;
    /// store.migrate().await?;
    /// let ids = store.list(None).await?;
    /// # let _ = ids;
    /// # Ok(())
    /// # }
    /// ```
    store: SqliteExecutionStore,
    pool: sqlx::SqlitePool,
    connect: sqlx::SqlitePool::connect,
    upsert: "INSERT INTO szal_executions (execution_id, flow_name, data) VALUES (?, ?, ?) \
             ON CONFLICT(execution_id) DO UPDATE SET flow_name = excluded.flow_name, data = excluded.data",
    select: "SELECT data FROM szal_executions WHERE execution_id = ?",
    list_flow: "SELECT execution_id FROM szal_executions WHERE flow_name = ?",
    delete: "DELETE FROM szal_executions WHERE execution_id = ?",
}

#[cfg(feature = "postgres")]
sqlx_execution_store! {
    /// Durable [`ExecutionStore`](crate::storage::ExecutionStore) backed by PostgreSQL.
    ///
    /// ```no_run
    /// use szal::sql_store::PostgresExecutionStore;
    ///
    /// # async fn demo() -> szal::Result<()> {
    /// let store = PostgresExecutionStore::connect("postgres://user:pass@localhost/db").await?;
    /// store.migrate().await?;
    /// # Ok(())
    /// # }
    /// ```
    store: PostgresExecutionStore,
    pool: sqlx::PgPool,
    connect: sqlx::PgPool::connect,
    upsert: "INSERT INTO szal_executions (execution_id, flow_name, data) VALUES ($1, $2, $3) \
             ON CONFLICT(execution_id) DO UPDATE SET flow_name = excluded.flow_name, data = excluded.data",
    select: "SELECT data FROM szal_executions WHERE execution_id = $1",
    list_flow: "SELECT execution_id FROM szal_executions WHERE flow_name = $1",
    delete: "DELETE FROM szal_executions WHERE execution_id = $1",
}

#[cfg(all(test, feature = "sqlite"))]
mod tests {
    use super::*;
    use crate::engine::{Engine, EngineConfig, handler_fn};
    use crate::flow::{FlowDef, FlowMode};
    use crate::state::WorkflowState;
    use crate::step::StepDef;

    async fn temp_store() -> (SqliteExecutionStore, tempfile::TempDir) {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("exec.db");
        let url = format!("sqlite://{}?mode=rwc", path.display());
        let store = SqliteExecutionStore::connect(&url).await.unwrap();
        store.migrate().await.unwrap();
        (store, dir)
    }

    fn record(id: &str, flow: &str, state: WorkflowState) -> ExecutionRecord {
        ExecutionRecord {
            execution_id: id.into(),
            flow_name: flow.into(),
            state,
            result: None,
            started_at: "2026-06-10T00:00:00Z".into(),
            finished_at: None,
        }
    }

    #[tokio::test]
    async fn save_get_roundtrip() {
        let (store, _dir) = temp_store().await;
        store
            .save(&record("e1", "deploy", WorkflowState::Running))
            .await
            .unwrap();
        let got = store.get("e1").await.unwrap().unwrap();
        assert_eq!(got.flow_name, "deploy");
        assert_eq!(got.state, WorkflowState::Running);
        assert!(store.get("missing").await.unwrap().is_none());
    }

    #[tokio::test]
    async fn upsert_updates_existing() {
        let (store, _dir) = temp_store().await;
        store
            .save(&record("e1", "deploy", WorkflowState::Running))
            .await
            .unwrap();
        store
            .save(&record("e1", "deploy", WorkflowState::Completed))
            .await
            .unwrap();
        let got = store.get("e1").await.unwrap().unwrap();
        assert_eq!(got.state, WorkflowState::Completed);
        // Still a single row.
        assert_eq!(store.list(None).await.unwrap().len(), 1);
    }

    #[tokio::test]
    async fn list_filters_by_flow() {
        let (store, _dir) = temp_store().await;
        store
            .save(&record("e1", "deploy", WorkflowState::Completed))
            .await
            .unwrap();
        store
            .save(&record("e2", "test", WorkflowState::Failed))
            .await
            .unwrap();
        store
            .save(&record("e3", "deploy", WorkflowState::RolledBack))
            .await
            .unwrap();
        assert_eq!(store.list(None).await.unwrap().len(), 3);
        assert_eq!(store.list(Some("deploy")).await.unwrap().len(), 2);
        assert_eq!(store.list(Some("test")).await.unwrap().len(), 1);
        assert_eq!(store.list(Some("missing")).await.unwrap().len(), 0);
    }

    #[tokio::test]
    async fn remove_returns_and_deletes() {
        let (store, _dir) = temp_store().await;
        store
            .save(&record("e1", "x", WorkflowState::Created))
            .await
            .unwrap();
        let removed = store.remove("e1").await.unwrap().unwrap();
        assert_eq!(removed.execution_id, "e1");
        assert!(store.get("e1").await.unwrap().is_none());
        assert!(store.remove("e1").await.unwrap().is_none());
    }

    #[tokio::test]
    async fn engine_sink_persists_execution() {
        let (store, _dir) = temp_store().await;
        let sink = store.engine_sink();

        let mut flow = FlowDef::new("persist", FlowMode::Sequential);
        flow.add_step(StepDef::new("a"));
        let exec_id = flow.id.to_string();

        let engine = Engine::new(
            EngineConfig {
                execution_store: Some(sink),
                ..Default::default()
            },
            handler_fn(|s| async move { Ok(serde_json::json!({"step": s.name})) }),
        );
        assert!(engine.run(&flow).await.unwrap().success);

        // The final fire-and-forget DB write may still be in flight; poll briefly.
        let mut found = None;
        for _ in 0..50 {
            if let Some(r) = store.get(&exec_id).await.unwrap()
                && r.state == WorkflowState::Completed
            {
                found = Some(r);
                break;
            }
            tokio::time::sleep(std::time::Duration::from_millis(10)).await;
        }
        let rec = found.expect("execution not persisted to sqlite");
        assert_eq!(rec.flow_name, "persist");
        assert!(rec.finished_at.is_some());
    }
}