aion-rs 0.27.0

Transport-agnostic Aion workflow engine with durability, replay, timers, and supervision.
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
use super::*;

#[tokio::test]
async fn start_then_cancel_records_started_then_cancelled() -> Result<(), Box<dyn std::error::Error>>
{
    let store: Arc<dyn EventStore> = Arc::new(InMemoryStore::default());
    let engine = engine_with_loaded_workflow(Arc::clone(&store), "checkout", "checkout_deployed")?;
    let handle = engine
        .start_workflow(
            "checkout",
            payload("input")?,
            HashMap::new(),
            String::from("default"),
        )
        .await?;

    engine
        .cancel(
            handle.workflow_id(),
            handle.run_id(),
            "caller requested cancellation",
        )
        .await?;

    let history = store.read_history(handle.workflow_id()).await?;
    match history.as_slice() {
        [
            Event::WorkflowStarted { .. },
            Event::WorkflowCancelled { reason, .. },
        ] => {
            assert_eq!(reason, "caller requested cancellation");
        }
        other => return Err(format!("expected started then cancelled, found {other:?}").into()),
    }
    engine.shutdown()?;
    Ok(())
}

fn test_envelope(workflow_id: &WorkflowId, seq: u64) -> EventEnvelope {
    EventEnvelope {
        seq,
        recorded_at: chrono::DateTime::from_timestamp(1_700_000_000, 0).unwrap_or_default(),
        workflow_id: workflow_id.clone(),
    }
}

fn started_event(workflow_id: &WorkflowId, seq: u64) -> Event {
    Event::WorkflowStarted {
        envelope: test_envelope(workflow_id, seq),
        workflow_type: String::from("checkout"),
        input: Payload::new(aion_core::ContentType::Json, b"{}".to_vec()),
        run_id: RunId::new_v4(),
        parent_run_id: None,
        parent_workflow_id: None,
        package_version: PackageVersion::new("a".repeat(64)),
    }
}

fn timer_started_event(workflow_id: &WorkflowId, seq: u64, timer_id: &TimerId) -> Event {
    Event::TimerStarted {
        envelope: test_envelope(workflow_id, seq),
        timer_id: timer_id.clone(),
        fire_at: chrono::DateTime::from_timestamp(1_700_000_500, 0).unwrap_or_default(),
    }
}

fn timer_fired_event(workflow_id: &WorkflowId, seq: u64, timer_id: &TimerId) -> Event {
    Event::TimerFired {
        envelope: test_envelope(workflow_id, seq),
        timer_id: timer_id.clone(),
    }
}

fn timer_cancelled_event(workflow_id: &WorkflowId, seq: u64, timer_id: &TimerId) -> Event {
    Event::TimerCancelled {
        envelope: test_envelope(workflow_id, seq),
        timer_id: timer_id.clone(),
        cause: TimerCancelCause::WorkflowIntent,
    }
}

#[test]
fn live_timers_lists_started_and_unterminated() {
    let workflow_id = WorkflowId::new_v4();
    let first = TimerId::anonymous(0);
    let second = TimerId::anonymous(1);
    let history = vec![
        started_event(&workflow_id, 0),
        timer_started_event(&workflow_id, 1, &first),
        timer_started_event(&workflow_id, 2, &second),
    ];
    assert_eq!(
        live_timers_in_active_segment(&history),
        vec![first, second],
        "both started, unterminated timers should be live, in start order"
    );
}

#[test]
fn live_timers_excludes_fired_and_cancelled() {
    let workflow_id = WorkflowId::new_v4();
    let fired = TimerId::anonymous(0);
    let cancelled = TimerId::anonymous(1);
    let live = TimerId::anonymous(2);
    let history = vec![
        started_event(&workflow_id, 0),
        timer_started_event(&workflow_id, 1, &fired),
        timer_started_event(&workflow_id, 2, &cancelled),
        timer_started_event(&workflow_id, 3, &live),
        timer_fired_event(&workflow_id, 4, &fired),
        timer_cancelled_event(&workflow_id, 5, &cancelled),
    ];
    assert_eq!(
        live_timers_in_active_segment(&history),
        vec![live],
        "only the timer with no terminal event remains live"
    );
}

#[test]
fn live_timers_dedups_repeated_start() {
    let workflow_id = WorkflowId::new_v4();
    let timer = TimerId::anonymous(0);
    let history = vec![
        started_event(&workflow_id, 0),
        timer_started_event(&workflow_id, 1, &timer),
        timer_started_event(&workflow_id, 2, &timer),
    ];
    assert_eq!(live_timers_in_active_segment(&history), vec![timer]);
}

#[test]
fn live_timers_scopes_to_active_run_segment() {
    // A timer started in a prior run (before a continue-as-new
    // `WorkflowStarted`) must not be surfaced for the replacement run.
    let workflow_id = WorkflowId::new_v4();
    let prior_run = TimerId::anonymous(0);
    let current_run = TimerId::anonymous(0);
    let history = vec![
        started_event(&workflow_id, 0),
        timer_started_event(&workflow_id, 1, &prior_run),
        started_event(&workflow_id, 2),
        timer_started_event(&workflow_id, 3, &current_run),
    ];
    assert_eq!(
        live_timers_in_active_segment(&history),
        vec![current_run],
        "only timers from the latest WorkflowStarted segment are live"
    );
}

#[test]
fn live_timers_empty_history_is_empty() {
    assert!(live_timers_in_active_segment(&[]).is_empty());
}

/// Build an engine whose runtime has the production timer NIF bridge
/// installed against the given store + registry, so `Engine::cancel`'s timer
/// cleanup exercises the real `TimerService` path (not a fake). Must be
/// called from within a tokio runtime (`Handle::current()`).
fn engine_with_timer_bridge(
    store: Arc<dyn EventStore>,
    registry: Arc<Registry>,
) -> Result<Engine, EngineError> {
    let runtime = RuntimeHandle::new(RuntimeConfig::new(Some(1)))?;
    runtime.register_waiting_test_module("checkout_deployed", "run");
    crate::runtime::nif_timer_bridge::install_timer_nif_bridge(
        runtime.nif_state(),
        Arc::clone(&registry),
        Arc::clone(&store),
        tokio::runtime::Handle::current(),
        crate::runtime::SignalDeliveryConfig::default(),
    );
    let visibility_store: Arc<dyn VisibilityStore> = Arc::new(InMemoryStore::default());
    Ok(Engine::new(EngineComponents {
        store,
        visibility_store,
        runtime: Arc::new(runtime),
        catalog: workflow_catalog("checkout", "checkout_deployed"),
        registry,
        supervision: Arc::new(SupervisionTree::new()),
        delegated: DelegatedSeams::default(),
        signal_handoff: Arc::new(crate::signal::SignalResumeHandoff::new()),
        search_attribute_schema: Arc::new(SearchAttributeSchema::new()),
        visibility_reconciliation_task: None,
        deferred_startup_recovery: None,
        workloop: None,
    }))
}

/// Root-cause regression: cancelling a workflow with a live durable timer
/// must record `TimerCancelled` (before the terminal `WorkflowCancelled`),
/// so the timer is dead in history and recovery never fires it as an
/// orphan. Drives the real `Engine::cancel` against a runtime with the
/// production timer bridge installed.
#[tokio::test(flavor = "multi_thread")]
async fn cancel_records_timer_cancelled_before_workflow_cancelled()
-> Result<(), Box<dyn std::error::Error>> {
    let store: Arc<dyn EventStore> = Arc::new(InMemoryStore::default());
    let registry = Arc::new(Registry::default());
    let engine = engine_with_timer_bridge(Arc::clone(&store), Arc::clone(&registry))?;

    let handle = engine
        .start_workflow(
            "checkout",
            payload("input")?,
            HashMap::new(),
            String::from("default"),
        )
        .await?;

    // Arm a live durable timer for the resident run and record its
    // `TimerStarted`, exactly as the resume-live handoff would in production.
    let timer_id = TimerId::anonymous(0);
    let fire_at = chrono::Utc::now() + chrono::Duration::hours(1);
    let armed_seq = handle
        .recorder()
        .lock()
        .await
        .record_timer_started(chrono::Utc::now(), timer_id.clone(), fire_at)
        .await?;
    let timer_service =
        crate::runtime::nif_timer_bridge::installed_timer_service(engine.runtime().nif_state())
            .map_err(|error| format!("timer service unavailable: {error}"))?;
    timer_service
        .schedule(
            handle.workflow_id().clone(),
            timer_id.clone(),
            fire_at,
            armed_seq,
        )
        .await?;

    engine
        .cancel(
            handle.workflow_id(),
            handle.run_id(),
            "caller requested cancellation",
        )
        .await?;

    let history = store.read_history(handle.workflow_id()).await?;
    match history.as_slice() {
        [
            Event::WorkflowStarted { .. },
            Event::TimerStarted {
                timer_id: started, ..
            },
            Event::TimerCancelled {
                timer_id: cancelled,
                ..
            },
            Event::WorkflowCancelled { reason, .. },
        ] => {
            assert_eq!(started, &timer_id);
            assert_eq!(cancelled, &timer_id, "the live timer must be cancelled");
            assert_eq!(reason, "caller requested cancellation");
        }
        other => {
            return Err(format!(
                "expected [started, timer-started, timer-cancelled, cancelled], found {other:?}"
            )
            .into());
        }
    }
    engine.shutdown()?;
    Ok(())
}

/// All live timers (not just one) are cancelled, in start order, before the
/// terminal `WorkflowCancelled`.
#[tokio::test(flavor = "multi_thread")]
async fn cancel_cancels_multiple_live_timers() -> Result<(), Box<dyn std::error::Error>> {
    let store: Arc<dyn EventStore> = Arc::new(InMemoryStore::default());
    let registry = Arc::new(Registry::default());
    let engine = engine_with_timer_bridge(Arc::clone(&store), Arc::clone(&registry))?;
    let handle = engine
        .start_workflow(
            "checkout",
            payload("input")?,
            HashMap::new(),
            String::from("default"),
        )
        .await?;

    let first = TimerId::anonymous(0);
    let second = TimerId::anonymous(1);
    let fire_at = chrono::Utc::now() + chrono::Duration::hours(1);
    {
        let recorder = handle.recorder();
        let mut recorder = recorder.lock().await;
        recorder
            .record_timer_started(chrono::Utc::now(), first.clone(), fire_at)
            .await?;
        recorder
            .record_timer_started(chrono::Utc::now(), second.clone(), fire_at)
            .await?;
    }

    engine
        .cancel(handle.workflow_id(), handle.run_id(), "stop")
        .await?;

    let history = store.read_history(handle.workflow_id()).await?;
    match history.as_slice() {
        [
            Event::WorkflowStarted { .. },
            Event::TimerStarted {
                timer_id: started_first,
                ..
            },
            Event::TimerStarted {
                timer_id: started_second,
                ..
            },
            Event::TimerCancelled {
                timer_id: cancelled_first,
                ..
            },
            Event::TimerCancelled {
                timer_id: cancelled_second,
                ..
            },
            Event::WorkflowCancelled { .. },
        ] => {
            assert_eq!(started_first, &first);
            assert_eq!(started_second, &second);
            assert_eq!(cancelled_first, &first, "first live timer cancelled first");
            assert_eq!(
                cancelled_second, &second,
                "second live timer cancelled second"
            );
        }
        other => {
            return Err(format!(
                "expected two timer-cancels before workflow-cancel, found {other:?}"
            )
            .into());
        }
    }
    engine.shutdown()?;
    Ok(())
}

/// End-to-end source-of-bug proof: a cancelled workflow leaves no orphan for
/// startup recovery. With a past-due durable timer row (the exact shape that
/// bricked startup before the fix), recovery surfaces no `UnknownWorkflow`
/// and fires nothing — because cancel recorded `TimerCancelled`, so the
/// timer is dead in history. Complements the committed boot-sweep defense
/// test by proving the orphan is gone *at the source*.
#[tokio::test(flavor = "multi_thread")]
async fn cancelled_workflow_leaves_no_orphan_for_recovery() -> Result<(), Box<dyn std::error::Error>>
{
    let concrete: Arc<InMemoryStore> = Arc::new(InMemoryStore::default());
    let store: Arc<dyn EventStore> = concrete.clone();
    let registry = Arc::new(Registry::default());
    let engine = engine_with_timer_bridge(Arc::clone(&store), Arc::clone(&registry))?;
    let handle = engine
        .start_workflow(
            "checkout",
            payload("input")?,
            HashMap::new(),
            String::from("default"),
        )
        .await?;
    let workflow_id = handle.workflow_id().clone();

    // A live timer whose durable row is already past-due, inserted directly
    // (no wheel arm, so nothing races the cancel).
    let timer_id = TimerId::anonymous(0);
    let fire_at = chrono::Utc::now() - chrono::Duration::hours(1);
    let armed_seq = handle
        .recorder()
        .lock()
        .await
        .record_timer_started(chrono::Utc::now(), timer_id.clone(), fire_at)
        .await?;
    concrete
        .schedule_timer(&workflow_id, &timer_id, fire_at, armed_seq)
        .await?;

    let timer_service =
        crate::runtime::nif_timer_bridge::installed_timer_service(engine.runtime().nif_state())
            .map_err(|error| format!("timer service unavailable: {error}"))?;

    engine.cancel(&workflow_id, handle.run_id(), "stop").await?;

    // Cancel removed the workflow from the registry and the durable row is
    // now past-due — exactly the orphan scenario. Recovery must handle it
    // cleanly: the recorded `TimerCancelled` makes `fire_timer` a no-op, so
    // no `TimerFired` and (critically) no `UnknownWorkflow`.
    let readable: Arc<dyn ReadableEventStore> = concrete.clone();
    TimerRecovery::new(readable, timer_service)
        .recover_on_startup(chrono::Utc::now())
        .await?;

    let history = concrete.read_history(&workflow_id).await?;
    assert!(
        !history
            .iter()
            .any(|event| matches!(event, Event::TimerFired { .. })),
        "no timer should fire for a cancelled workflow during recovery"
    );
    assert!(
        history
            .iter()
            .any(|event| matches!(event, Event::TimerCancelled { .. })),
        "cancel must have recorded TimerCancelled at the source"
    );
    engine.shutdown()?;
    Ok(())
}

#[tokio::test]
async fn result_returns_completed_payload() -> Result<(), Box<dyn std::error::Error>> {
    let store: Arc<dyn EventStore> = Arc::new(InMemoryStore::default());
    let engine = engine_with_loaded_workflow(Arc::clone(&store), "checkout", "checkout_deployed")?;
    let handle = engine
        .start_workflow(
            "checkout",
            payload("input")?,
            HashMap::new(),
            String::from("default"),
        )
        .await?;
    let result_payload = payload("result")?;

    terminate::complete(
        termination_context(&engine),
        handle.workflow_id(),
        handle.run_id(),
        result_payload.clone(),
    )
    .await?;

    assert_eq!(
        engine.result(handle.workflow_id(), handle.run_id()).await?,
        Ok(result_payload)
    );
    engine.shutdown()?;
    Ok(())
}

#[tokio::test]
async fn result_returns_failed_workflow_error() -> Result<(), Box<dyn std::error::Error>> {
    let store: Arc<dyn EventStore> = Arc::new(InMemoryStore::default());
    let engine = engine_with_loaded_workflow(Arc::clone(&store), "checkout", "checkout_deployed")?;
    let handle = engine
        .start_workflow(
            "checkout",
            payload("input")?,
            HashMap::new(),
            String::from("default"),
        )
        .await?;
    let error = workflow_error("workflow failed");

    terminate::fail(
        termination_context(&engine),
        handle.workflow_id(),
        handle.run_id(),
        error.clone(),
    )
    .await?;

    assert_eq!(
        engine.result(handle.workflow_id(), handle.run_id()).await?,
        Err(error)
    );
    engine.shutdown()?;
    Ok(())
}