a3s-flow 1.1.0

Durable workflow engine and Rust SDK for A3S
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
use super::*;

#[tokio::test]
async fn start_with_id_is_idempotent_for_same_spec_and_input() {
    let engine = FlowEngine::in_memory(Arc::new(SequentialRuntime));
    let run_id = engine
        .start_with_id("stable-run", spec(), json!({ "userId": "u1" }))
        .await
        .unwrap();
    let first_events = engine.store().list(&run_id).await.unwrap();

    let second_run_id = engine
        .start_with_id("stable-run", spec(), json!({ "userId": "u1" }))
        .await
        .unwrap();
    let second_events = engine.store().list(&run_id).await.unwrap();

    assert_eq!(run_id, "stable-run");
    assert_eq!(second_run_id, run_id);
    assert_eq!(second_events.len(), first_events.len());
    assert_eq!(
        second_events
            .iter()
            .filter(|event| matches!(event.event, a3s_flow::FlowEvent::RunCreated { .. }))
            .count(),
        1
    );
    assert_eq!(
        second_events
            .iter()
            .filter(|event| matches!(event.event, a3s_flow::FlowEvent::RunStarted))
            .count(),
        1
    );

    let snapshot = engine.snapshot(&run_id).await.unwrap();
    assert_eq!(snapshot.status, WorkflowRunStatus::Completed);
}

#[tokio::test]
async fn lists_run_ids_history_and_snapshots() {
    let engine = FlowEngine::in_memory(Arc::new(SequentialRuntime));
    engine
        .start_with_id("run-b", spec(), json!({ "userId": "u2" }))
        .await
        .unwrap();
    engine
        .start_with_id("run-a", spec(), json!({ "userId": "u1" }))
        .await
        .unwrap();

    assert_eq!(
        engine.list_run_ids().await.unwrap(),
        vec!["run-a".to_string(), "run-b".to_string()]
    );

    let history = engine.history("run-a").await.unwrap();
    assert_eq!(
        history.first().map(|event| event.event.event_key()),
        Some("flow.run.created")
    );
    assert_eq!(
        history.last().map(|event| event.event.event_key()),
        Some("flow.run.completed")
    );

    let snapshots = engine.list_snapshots().await.unwrap();
    assert_eq!(
        snapshots
            .iter()
            .map(|snapshot| snapshot.run_id.as_str())
            .collect::<Vec<_>>(),
        vec!["run-a", "run-b"]
    );
    assert!(snapshots
        .iter()
        .all(|snapshot| snapshot.status == WorkflowRunStatus::Completed));
}

#[tokio::test]
async fn start_with_id_rejects_conflicting_input_or_spec() {
    let engine = FlowEngine::in_memory(Arc::new(SequentialRuntime));
    engine
        .start_with_id("stable-run", spec(), json!({ "userId": "u1" }))
        .await
        .unwrap();

    let err = engine
        .start_with_id("stable-run", spec(), json!({ "userId": "u2" }))
        .await
        .unwrap_err();
    assert!(
        matches!(err, FlowError::RunConflict { run_id, reason } if run_id == "stable-run" && reason == "workflow input differs")
    );

    let err = engine
        .start_with_id(
            "stable-run",
            WorkflowSpec::rust_embedded("test.workflow", "0.2.0", "tests::runtime", "main"),
            json!({ "userId": "u1" }),
        )
        .await
        .unwrap_err();
    assert!(
        matches!(err, FlowError::RunConflict { run_id, reason } if run_id == "stable-run" && reason == "workflow spec differs")
    );
}

#[tokio::test]
async fn start_with_id_rejects_unsafe_run_ids() {
    let engine = FlowEngine::in_memory(Arc::new(SequentialRuntime));
    let err = engine
        .start_with_id("../stable-run", spec(), json!({ "userId": "u1" }))
        .await
        .unwrap_err();

    assert!(matches!(err, FlowError::InvalidRunId(run_id) if run_id == "../stable-run"));
}

#[tokio::test]
async fn in_memory_store_rejects_stale_expected_sequence() {
    let store = InMemoryEventStore::new();

    let first = store
        .append_if_sequence("sequence-run", 0, run_created_event())
        .await
        .unwrap();
    let second = store
        .append_if_sequence("sequence-run", first.sequence, FlowEvent::RunStarted)
        .await
        .unwrap();

    let err = store
        .append_if_sequence("sequence-run", first.sequence, FlowEvent::RunStarted)
        .await
        .unwrap_err();

    assert_eq!(first.sequence, 1);
    assert_eq!(second.sequence, 2);
    assert!(matches!(
        err,
        FlowError::EventConflict {
            run_id,
            expected_sequence: 1,
            actual_sequence: 2,
        } if run_id == "sequence-run"
    ));
    assert_eq!(store.list("sequence-run").await.unwrap().len(), 2);
}

#[tokio::test]
async fn local_file_store_rejects_stale_expected_sequence() {
    let dir = tempfile::tempdir().unwrap();
    let store = LocalFileEventStore::new(dir.path());

    let first = store
        .append_if_sequence("sequence-run", 0, run_created_event())
        .await
        .unwrap();
    let second = store
        .append_if_sequence("sequence-run", first.sequence, FlowEvent::RunStarted)
        .await
        .unwrap();

    let err = store
        .append_if_sequence("sequence-run", first.sequence, FlowEvent::RunStarted)
        .await
        .unwrap_err();

    assert_eq!(first.sequence, 1);
    assert_eq!(second.sequence, 2);
    assert!(matches!(
        err,
        FlowError::EventConflict {
            run_id,
            expected_sequence: 1,
            actual_sequence: 2,
        } if run_id == "sequence-run"
    ));
    assert_eq!(store.list("sequence-run").await.unwrap().len(), 2);
}

#[cfg(feature = "sqlite")]
#[tokio::test]
async fn sqlite_store_rejects_stale_expected_sequence() {
    let dir = tempfile::tempdir().unwrap();
    let store = SqliteEventStore::connect(sqlite_url(&dir)).await.unwrap();

    let first = store
        .append_if_sequence("sequence-run", 0, run_created_event())
        .await
        .unwrap();
    let second = store
        .append_if_sequence("sequence-run", first.sequence, FlowEvent::RunStarted)
        .await
        .unwrap();

    let err = store
        .append_if_sequence("sequence-run", first.sequence, FlowEvent::RunStarted)
        .await
        .unwrap_err();

    assert_eq!(first.sequence, 1);
    assert_eq!(second.sequence, 2);
    assert!(matches!(
        err,
        FlowError::EventConflict {
            run_id,
            expected_sequence: 1,
            actual_sequence: 2,
        } if run_id == "sequence-run"
    ));
    assert_eq!(store.list("sequence-run").await.unwrap().len(), 2);
}

#[cfg(feature = "sqlite")]
#[tokio::test]
async fn sqlite_store_creates_parent_directory_on_connect() {
    let dir = tempfile::tempdir().unwrap();
    let db_path = dir.path().join("nested").join("flow.db");
    let store = SqliteEventStore::connect(format!("sqlite://{}", db_path.display()))
        .await
        .unwrap();

    assert!(db_path.parent().unwrap().is_dir());
    store
        .append_if_sequence("parent-dir-run", 0, run_created_event())
        .await
        .unwrap();
    assert_eq!(store.list("parent-dir-run").await.unwrap().len(), 1);
}

#[cfg(feature = "sqlite")]
#[tokio::test]
async fn sqlite_store_accepts_memory_and_short_form_urls() {
    let memory = SqliteEventStore::connect("sqlite::memory:").await.unwrap();
    memory
        .append_if_sequence("memory-run", 0, run_created_event())
        .await
        .unwrap();
    assert_eq!(memory.list("memory-run").await.unwrap().len(), 1);

    let directory = tempfile::tempdir().unwrap();
    let database_path = directory.path().join("short-form.db");
    let store = SqliteEventStore::connect(format!("sqlite:{}", database_path.display()))
        .await
        .unwrap();
    store
        .append_if_sequence("short-form-run", 0, run_created_event())
        .await
        .unwrap();
    assert!(database_path.is_file());
}

#[tokio::test]
async fn local_file_store_preserves_log_order_for_projection_validation() {
    let dir = tempfile::tempdir().unwrap();
    let run_id = "out-of-order-run";
    let path = dir.path().join(format!("{run_id}.jsonl"));
    let second = envelope(run_id, 2, FlowEvent::RunStarted);
    let first = envelope(run_id, 1, run_created_event());
    let content = format!(
        "{}\n{}\n",
        serde_json::to_string(&second).unwrap(),
        serde_json::to_string(&first).unwrap()
    );
    tokio::fs::write(path, content).await.unwrap();

    let store = Arc::new(LocalFileEventStore::new(dir.path()));
    let engine = FlowEngine::new(store, Arc::new(SequentialRuntime));
    let err = engine.snapshot(run_id).await.unwrap_err();

    assert_invalid_transition(err, "first run event must be run_created");
}

#[tokio::test]
async fn local_file_store_rejects_append_to_invalid_log() {
    let dir = tempfile::tempdir().unwrap();
    let run_id = "out-of-order-run";
    let path = dir.path().join(format!("{run_id}.jsonl"));
    let second = envelope(run_id, 2, FlowEvent::RunStarted);
    let first = envelope(run_id, 1, run_created_event());
    let content = format!(
        "{}\n{}\n",
        serde_json::to_string(&second).unwrap(),
        serde_json::to_string(&first).unwrap()
    );
    tokio::fs::write(&path, &content).await.unwrap();

    let store = LocalFileEventStore::new(dir.path());
    let err = store
        .append_if_sequence(run_id, 1, FlowEvent::RunCancelled { reason: None })
        .await
        .unwrap_err();

    assert_invalid_transition(err, "first run event must be run_created");
    assert_eq!(tokio::fs::read_to_string(path).await.unwrap(), content);
}

#[tokio::test]
async fn local_file_store_repairs_a_missing_final_delimiter_before_append() {
    let dir = tempfile::tempdir().unwrap();
    let run_id = "missing-final-delimiter";
    let path = dir.path().join(format!("{run_id}.jsonl"));
    let store = LocalFileEventStore::new(dir.path());
    let first = store
        .append_if_sequence(run_id, 0, run_created_event())
        .await
        .unwrap();

    let mut bytes = tokio::fs::read(&path).await.unwrap();
    assert_eq!(bytes.pop(), Some(b'\n'));
    tokio::fs::write(&path, bytes).await.unwrap();

    let resumed = LocalFileEventStore::new(dir.path());
    resumed
        .append_if_sequence(run_id, first.sequence, FlowEvent::RunStarted)
        .await
        .unwrap();

    let events = resumed.list(run_id).await.unwrap();
    assert_eq!(events.len(), 2);
    assert_eq!(events[0].sequence, 1);
    assert_eq!(events[1].sequence, 2);
    let repaired = tokio::fs::read(&path).await.unwrap();
    assert!(repaired.ends_with(b"\n"));
    assert_eq!(repaired.split(|byte| *byte == b'\n').count(), 3);
}

#[tokio::test]
async fn local_file_store_discards_only_an_unterminated_torn_tail() {
    let dir = tempfile::tempdir().unwrap();
    let run_id = "unterminated-torn-tail";
    let path = dir.path().join(format!("{run_id}.jsonl"));
    let store = LocalFileEventStore::new(dir.path());
    let first = store
        .append_if_sequence(run_id, 0, run_created_event())
        .await
        .unwrap();

    let mut bytes = tokio::fs::read(&path).await.unwrap();
    bytes.extend_from_slice(br#"{"run_id":"unterminated"#);
    tokio::fs::write(&path, bytes).await.unwrap();

    let resumed = LocalFileEventStore::new(dir.path());
    let recovered = resumed.list(run_id).await.unwrap();
    assert_eq!(recovered.len(), 1);
    resumed
        .append_if_sequence(run_id, first.sequence, FlowEvent::RunStarted)
        .await
        .unwrap();

    let events = resumed.list(run_id).await.unwrap();
    assert_eq!(events.len(), 2);
    assert_eq!(events[0].sequence, 1);
    assert_eq!(events[1].sequence, 2);
    let repaired = tokio::fs::read_to_string(path).await.unwrap();
    assert!(!repaired.contains(r#""run_id":"unterminated""#));
    assert_eq!(repaired.lines().count(), 2);
}

#[tokio::test]
async fn local_file_store_still_rejects_a_terminated_corrupt_tail() {
    let dir = tempfile::tempdir().unwrap();
    let run_id = "terminated-corrupt-tail";
    let path = dir.path().join(format!("{run_id}.jsonl"));
    let store = LocalFileEventStore::new(dir.path());
    let first = store
        .append_if_sequence(run_id, 0, run_created_event())
        .await
        .unwrap();

    let mut bytes = tokio::fs::read(&path).await.unwrap();
    bytes.extend_from_slice(b"not-json\n");
    tokio::fs::write(&path, &bytes).await.unwrap();

    let resumed = LocalFileEventStore::new(dir.path());
    let error = resumed.list(run_id).await.unwrap_err();
    assert!(error.to_string().contains("failed to decode event line 2"));
    let append_error = resumed
        .append_if_sequence(run_id, first.sequence, FlowEvent::RunStarted)
        .await
        .unwrap_err();
    assert!(append_error
        .to_string()
        .contains("failed to decode event line 2"));
    assert_eq!(tokio::fs::read(path).await.unwrap(), bytes);
}

#[tokio::test]
async fn local_file_store_prunes_only_old_terminal_runs() {
    let dir = tempfile::tempdir().unwrap();
    let store = Arc::new(LocalFileEventStore::new(dir.path()));
    let completed_engine = FlowEngine::new(store.clone(), Arc::new(SequentialRuntime));
    completed_engine
        .start_with_id("completed-run", spec(), json!({ "userId": "u1" }))
        .await
        .unwrap();

    let suspended_engine = FlowEngine::new(store.clone(), Arc::new(InputSleepRuntime));
    suspended_engine
        .start_with_id(
            "cancelled-run",
            spec(),
            json!({ "resume_at": (Utc::now() + ChronoDuration::hours(1)).to_rfc3339() }),
        )
        .await
        .unwrap();
    completed_engine
        .cancel("cancelled-run", Some("retention test".to_string()))
        .await
        .unwrap();

    suspended_engine
        .start_with_id(
            "suspended-run",
            spec(),
            json!({ "resume_at": (Utc::now() + ChronoDuration::hours(1)).to_rfc3339() }),
        )
        .await
        .unwrap();

    let none_removed = store
        .prune_terminal_runs_older_than(Utc::now() - ChronoDuration::days(1))
        .await
        .unwrap();
    assert!(none_removed.is_empty());

    let removed = store
        .prune_terminal_runs_older_than(Utc::now() + ChronoDuration::days(1))
        .await
        .unwrap();
    assert_eq!(
        removed,
        vec!["cancelled-run".to_string(), "completed-run".to_string()]
    );

    assert!(matches!(
        store.list("completed-run").await.unwrap_err(),
        FlowError::RunNotFound(_)
    ));
    assert!(matches!(
        store.list("cancelled-run").await.unwrap_err(),
        FlowError::RunNotFound(_)
    ));
    let retained = suspended_engine.snapshot("suspended-run").await.unwrap();
    assert_eq!(retained.status, WorkflowRunStatus::Suspended);
    assert_eq!(store.list_run_ids().await.unwrap(), vec!["suspended-run"]);
}

struct RunCreatedConflictStore {
    inner: InMemoryEventStore,
    injected: AtomicBool,
}

impl RunCreatedConflictStore {
    fn new() -> Self {
        Self {
            inner: InMemoryEventStore::new(),
            injected: AtomicBool::new(false),
        }
    }
}

#[async_trait]
impl FlowEventStore for RunCreatedConflictStore {
    async fn append(
        &self,
        run_id: &str,
        event: FlowEvent,
    ) -> a3s_flow::Result<a3s_flow::FlowEventEnvelope> {
        self.inner.append(run_id, event).await
    }

    async fn append_if_sequence(
        &self,
        run_id: &str,
        expected_sequence: u64,
        event: FlowEvent,
    ) -> a3s_flow::Result<a3s_flow::FlowEventEnvelope> {
        if expected_sequence == 0
            && matches!(event, FlowEvent::RunCreated { .. })
            && !self.injected.swap(true, Ordering::SeqCst)
        {
            let inserted = self
                .inner
                .append_if_sequence(run_id, expected_sequence, event)
                .await?;
            return Err(FlowError::EventConflict {
                run_id: run_id.to_string(),
                expected_sequence,
                actual_sequence: inserted.sequence,
            });
        }

        self.inner
            .append_if_sequence(run_id, expected_sequence, event)
            .await
    }

    async fn list(&self, run_id: &str) -> a3s_flow::Result<Vec<a3s_flow::FlowEventEnvelope>> {
        self.inner.list(run_id).await
    }

    async fn list_run_ids(&self) -> a3s_flow::Result<Vec<String>> {
        self.inner.list_run_ids().await
    }
}

#[tokio::test]
async fn start_with_id_replays_after_run_created_conflict_without_duplicate_event() {
    let store = Arc::new(RunCreatedConflictStore::new());
    let engine = FlowEngine::new(store.clone(), Arc::new(SequentialRuntime));

    let run_id = engine
        .start_with_id("race-run", spec(), json!({ "userId": "u1" }))
        .await
        .unwrap();
    let events = store.list("race-run").await.unwrap();
    let snapshot = engine.snapshot("race-run").await.unwrap();

    assert_eq!(run_id, "race-run");
    assert_eq!(snapshot.status, WorkflowRunStatus::Completed);
    assert_eq!(
        events
            .iter()
            .filter(|event| matches!(event.event, FlowEvent::RunCreated { .. }))
            .count(),
        1
    );
    assert_eq!(
        events
            .iter()
            .filter(|event| matches!(event.event, FlowEvent::RunStarted))
            .count(),
        1
    );
}