rustvello 0.9.1

Distributed task queue and workflow runtime for Rust and Python: typed tasks, retries, priorities, triggers and pluggable backends
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
//! Workflow integration tests.
//!
//! Exercises workflow data persistence, isolation, deterministic operations,
//! and workflow type discovery at the integration level using the in-memory
//! state backend.

use std::sync::Arc;

use rustvello::prelude::*;
use rustvello_core::context::{InvocationContext, INVOCATION_CTX};
use rustvello_core::state_backend::{StateBackend, StateBackendCore, StateBackendQuery};
use rustvello_core::workflow::WorkflowRoot;

#[rustvello::workflow]
fn deterministic_root_values() -> RustvelloResult<String> {
    let mut root = WorkflowRoot::current()?;
    Ok(format!(
        "{}|{}|{}",
        root.random()?,
        root.utc_now()?.to_rfc3339(),
        root.uuid()?
    ))
}

// ===========================================================================
// 1. Workflow Data Persistence (via state backend directly)
// ===========================================================================

/// Set and retrieve workflow data through the state backend.
#[tokio::test]
async fn workflow_data_set_get() {
    let sb = rustvello_mem::state_backend::MemStateBackend::new();
    let wf_id = InvocationId::new();

    sb.set_workflow_data(&wf_id, "counter", "42").await.unwrap();

    let val = sb.get_workflow_data(&wf_id, "counter").await.unwrap();
    assert_eq!(val.as_deref(), Some("42"));
}

/// Updating workflow data overwrites previous value.
#[tokio::test]
async fn workflow_data_update() {
    let sb = rustvello_mem::state_backend::MemStateBackend::new();
    let wf_id = InvocationId::new();

    sb.set_workflow_data(&wf_id, "key", "v1").await.unwrap();
    sb.set_workflow_data(&wf_id, "key", "v2").await.unwrap();

    let val = sb.get_workflow_data(&wf_id, "key").await.unwrap();
    assert_eq!(val.as_deref(), Some("v2"));
}

/// Getting non-existent key returns None.
#[tokio::test]
async fn workflow_data_missing_key() {
    let sb = rustvello_mem::state_backend::MemStateBackend::new();
    let wf_id = InvocationId::new();

    let val = sb.get_workflow_data(&wf_id, "nonexistent").await.unwrap();
    assert!(val.is_none());
}

/// Workflow data is isolated between different workflows.
#[tokio::test]
async fn workflow_data_isolation() {
    let sb = rustvello_mem::state_backend::MemStateBackend::new();
    let wf1 = InvocationId::new();
    let wf2 = InvocationId::new();

    sb.set_workflow_data(&wf1, "name", "workflow_one")
        .await
        .unwrap();
    sb.set_workflow_data(&wf2, "name", "workflow_two")
        .await
        .unwrap();

    assert_eq!(
        sb.get_workflow_data(&wf1, "name").await.unwrap().as_deref(),
        Some("workflow_one")
    );
    assert_eq!(
        sb.get_workflow_data(&wf2, "name").await.unwrap().as_deref(),
        Some("workflow_two")
    );
}

// ===========================================================================
// 2. Workflow Type Discovery
// ===========================================================================

/// store_workflow_run + get_all_workflow_types returns stored types.
#[tokio::test]
async fn workflow_type_discovery() {
    let sb = rustvello_mem::state_backend::MemStateBackend::new();

    let task_a = TaskId::new("mod", "task_a");
    let task_b = TaskId::new("mod", "task_b");

    let wf1 = WorkflowIdentity::root(InvocationId::new(), task_a.clone());
    let wf2 = WorkflowIdentity::root(InvocationId::new(), task_b.clone());

    sb.store_workflow_run(&wf1).await.unwrap();
    sb.store_workflow_run(&wf2).await.unwrap();

    let types = sb.get_all_workflow_types().await.unwrap();
    assert!(types.contains(&task_a));
    assert!(types.contains(&task_b));
}

/// get_workflow_runs returns all runs of a given type.
#[tokio::test]
async fn workflow_runs_listing() {
    let sb = rustvello_mem::state_backend::MemStateBackend::new();

    let task_id = TaskId::new("mod", "run_task");
    let wf1 = WorkflowIdentity::root(InvocationId::new(), task_id.clone());
    let wf2 = WorkflowIdentity::root(InvocationId::new(), task_id.clone());

    sb.store_workflow_run(&wf1).await.unwrap();
    sb.store_workflow_run(&wf2).await.unwrap();

    let runs = sb.get_workflow_runs(&task_id).await.unwrap();
    assert_eq!(runs.len(), 2);
}

/// get_workflow_invocations tracks members of a workflow.
#[tokio::test]
async fn workflow_invocations_tracked() {
    let sb = rustvello_mem::state_backend::MemStateBackend::new();

    let wf_id = InvocationId::new();
    let task_id = TaskId::new("mod", "wf_parent");

    // Store the root workflow run
    let wf = WorkflowIdentity::root(wf_id.clone(), task_id.clone());
    sb.store_workflow_run(&wf).await.unwrap();

    // Upsert some invocations that belong to this workflow
    let child_inv_id = InvocationId::new();
    let call = CallDTO::new(task_id.clone(), SerializedArguments::new());
    let inv_dto = InvocationDTO::with_workflow(
        child_inv_id.clone(),
        task_id.clone(),
        call.call_id.clone(),
        None,
        wf.clone(),
    );
    sb.upsert_invocation(&inv_dto, &call).await.unwrap();

    let members = sb.get_workflow_invocations(&wf_id).await.unwrap();
    assert!(
        members.contains(&child_inv_id),
        "Workflow invocations should include upserted member"
    );
}

// ===========================================================================
// 3. Deterministic Operations
// ===========================================================================

#[tokio::test]
async fn workflow_root_operations_run_through_task_runner() {
    let mut app = RustvelloApp::new(AppConfig::new("workflow-root-runner"));
    app.register(DeterministicRootValuesTask::new()).unwrap();
    let handle = app
        .submit_call(&DeterministicRootValuesTask::new(), ())
        .await
        .unwrap();
    let runner = TaskRunner::new(
        "workflow-test".to_string(),
        AppConfig::default(),
        app.broker(),
        app.orchestrator(),
        app.state_backend(),
        Arc::new({
            let mut registry = TaskRegistry::new();
            registry
                .register_typed(DeterministicRootValuesTask::new())
                .unwrap();
            registry
        }),
        None,
    );

    runner.run_one().await.unwrap();
    let result: String = handle.result().await.unwrap();
    let values: Vec<&str> = result.split('|').collect();
    assert_eq!(values.len(), 3);
    assert!(values[0].parse::<f64>().is_ok());
    assert!(chrono::DateTime::parse_from_rfc3339(values[1]).is_ok());
    assert!(uuid::Uuid::parse_str(values[2]).is_ok());
}

fn workflow_context(
    sb: Arc<dyn StateBackend>,
    workflow_id: InvocationId,
    invocation_id: InvocationId,
    is_workflow_defining: bool,
) -> InvocationContext {
    InvocationContext {
        invocation_id,
        task_id: TaskId::new("tests", "workflow"),
        workflow: Some(WorkflowIdentity::root(
            workflow_id,
            TaskId::new("tests", "workflow"),
        )),
        is_workflow_defining,
        state_backend: Some(sb),
        parent_invocation_id: None,
        num_retries: 0,
        trace_context: Default::default(),
    }
}

/// WorkflowRoot produces deterministic random values.
#[tokio::test]
async fn deterministic_random_is_seeded() {
    let sb: Arc<dyn StateBackend> = Arc::new(rustvello_mem::state_backend::MemStateBackend::new());
    let wf_id = InvocationId::new();
    let ctx = workflow_context(Arc::clone(&sb), wf_id.clone(), wf_id, true);
    let (r1, r2) = INVOCATION_CTX
        .scope(ctx, async {
            let mut root = WorkflowRoot::current().unwrap();
            (
                root.random_async().await.unwrap(),
                root.random_async().await.unwrap(),
            )
        })
        .await;

    // Both should be in [0, 1) range
    assert!((0.0..1.0).contains(&r1));
    assert!((0.0..1.0).contains(&r2));

    // They should be different (distinct sequences)
    assert_ne!(r1, r2);
}

/// WorkflowRoot utc_now produces ascending timestamps.
#[tokio::test]
async fn deterministic_time_ascending() {
    let sb: Arc<dyn StateBackend> = Arc::new(rustvello_mem::state_backend::MemStateBackend::new());
    let wf_id = InvocationId::new();

    let ctx = workflow_context(Arc::clone(&sb), wf_id.clone(), wf_id, true);
    let (t1, t2, t3) = INVOCATION_CTX
        .scope(ctx, async {
            let mut root = WorkflowRoot::current().unwrap();
            (
                root.utc_now_async().await.unwrap(),
                root.utc_now_async().await.unwrap(),
                root.utc_now_async().await.unwrap(),
            )
        })
        .await;

    assert!(t1 < t2, "Timestamps should be ascending");
    assert!(t2 < t3, "Timestamps should be ascending");
}

/// WorkflowRoot uuid produces valid UUIDs.
#[tokio::test]
async fn deterministic_uuid_valid() {
    let sb: Arc<dyn StateBackend> = Arc::new(rustvello_mem::state_backend::MemStateBackend::new());
    let wf_id = InvocationId::new();

    let ctx = workflow_context(Arc::clone(&sb), wf_id.clone(), wf_id, true);
    let (u1, u2) = INVOCATION_CTX
        .scope(ctx, async {
            let mut root = WorkflowRoot::current().unwrap();
            (
                root.uuid_async().await.unwrap(),
                root.uuid_async().await.unwrap(),
            )
        })
        .await;

    // Should be valid UUID format (36 chars with hyphens)
    assert_eq!(u1.len(), 36);
    assert_eq!(u2.len(), 36);
    assert_ne!(u1, u2);
}

/// Replaying deterministic operations returns the same values.
#[tokio::test]
async fn deterministic_replay_consistency() {
    let sb: Arc<dyn StateBackend> = Arc::new(rustvello_mem::state_backend::MemStateBackend::new());
    let wf_id = InvocationId::new();

    let ctx = workflow_context(Arc::clone(&sb), wf_id.clone(), wf_id, true);
    let first = INVOCATION_CTX
        .scope(ctx.clone(), async {
            let mut root = WorkflowRoot::current().unwrap();
            (
                root.random_async().await.unwrap(),
                root.utc_now_async().await.unwrap(),
                root.uuid_async().await.unwrap(),
            )
        })
        .await;
    let second = INVOCATION_CTX
        .scope(ctx, async {
            let mut root = WorkflowRoot::current().unwrap();
            (
                root.random_async().await.unwrap(),
                root.utc_now_async().await.unwrap(),
                root.uuid_async().await.unwrap(),
            )
        })
        .await;

    assert_eq!(first, second, "replay should reuse all recorded values");
}

/// Mixed deterministic operations produce isolated sequences.
#[tokio::test]
async fn deterministic_mixed_operations() {
    let sb: Arc<dyn StateBackend> = Arc::new(rustvello_mem::state_backend::MemStateBackend::new());
    let wf_id = InvocationId::new();

    let ctx = workflow_context(Arc::clone(&sb), wf_id.clone(), wf_id, true);
    let (r, t, u, r2) = INVOCATION_CTX
        .scope(ctx, async {
            let mut root = WorkflowRoot::current().unwrap();
            (
                root.random_async().await.unwrap(),
                root.utc_now_async().await.unwrap(),
                root.uuid_async().await.unwrap(),
                root.random_async().await.unwrap(),
            )
        })
        .await;

    // All should produce distinct values
    assert!((0.0..1.0).contains(&r));
    assert!((0.0..1.0).contains(&r2));
    assert_ne!(r, r2);
    assert_eq!(u.len(), 36);
    assert!(t.timestamp() > 0);
}

#[test]
fn workflow_root_requires_running_context() {
    assert!(matches!(
        WorkflowRoot::current(),
        Err(RustvelloError::WorkflowContextUnavailable)
    ));
}

#[tokio::test]
async fn workflow_root_rejects_ordinary_task() {
    let sb: Arc<dyn StateBackend> = Arc::new(rustvello_mem::state_backend::MemStateBackend::new());
    let inv_id = InvocationId::new();
    let ctx = InvocationContext {
        invocation_id: inv_id.clone(),
        task_id: TaskId::new("tests", "ordinary"),
        workflow: None,
        is_workflow_defining: false,
        state_backend: Some(sb),
        parent_invocation_id: None,
        num_retries: 0,
        trace_context: Default::default(),
    };

    let error = INVOCATION_CTX
        .scope(ctx, async { WorkflowRoot::current().err().unwrap() })
        .await;
    assert!(matches!(
        error,
        RustvelloError::WorkflowMembershipRequired { invocation_id } if invocation_id == inv_id
    ));
}

#[tokio::test]
async fn workflow_root_rejects_child_member() {
    let sb: Arc<dyn StateBackend> = Arc::new(rustvello_mem::state_backend::MemStateBackend::new());
    let workflow_id = InvocationId::new();
    let child_id = InvocationId::new();
    let ctx = workflow_context(sb, workflow_id.clone(), child_id.clone(), false);

    let error = INVOCATION_CTX
        .scope(ctx, async { WorkflowRoot::current().err().unwrap() })
        .await;
    assert!(matches!(
        error,
        RustvelloError::WorkflowRootRequired { invocation_id, workflow_id: actual }
            if invocation_id == child_id && actual == workflow_id
    ));
}

// ===========================================================================
// 4. Sub-Workflow Boundary Tests
// ===========================================================================

/// WorkflowIdentity::sub_workflow creates a separate workflow with a parent link.
#[test]
fn sub_workflow_identity_structure() {
    let root_wf_id = InvocationId::new();
    let root_task = TaskId::new("mod", "parent_task");
    let root = WorkflowIdentity::root(root_wf_id.clone(), root_task.clone());

    let child_inv_id = InvocationId::new();
    let child_task = TaskId::new("mod", "child_task");
    let sub = WorkflowIdentity::sub_workflow(
        child_inv_id.clone(),
        child_task.clone(),
        root.workflow_id.clone(),
    );

    assert_eq!(sub.workflow_id, child_inv_id); // own workflow
    assert_eq!(sub.workflow_type, child_task);
    assert_eq!(sub.parent_id, Some(root_wf_id)); // linked to parent
    assert!(sub.is_sub_workflow());
}

/// Child workflow inherits parent's workflow_id.
#[test]
fn child_workflow_inherits_id() {
    let root_wf_id = InvocationId::new();
    let root_task = TaskId::new("mod", "root_task");

    let child_inv = InvocationId::new();
    let child =
        WorkflowIdentity::child(root_wf_id.clone(), root_task.clone(), child_inv.clone(), 1);

    assert_eq!(child.workflow_id, root_wf_id); // shares workflow
    assert_eq!(child.depth, 1);
    assert_eq!(child.parent_id, Some(child_inv));
    // child also has parent_id set (so is_sub_workflow is true for both child and sub_workflow)
    assert!(child.is_sub_workflow());
}