feltdb 0.2.0

Embedded durable state store: an append-only log with atomic multi-record transactions, version-conditional writes, and idempotent retries
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
/// Workflow acceptance tests demonstrating the distributed workflow runtime
///
/// Tests comprehensive workflows across multiple peers with various scenarios:
/// - Linear workflows with dependencies
/// - Parallel workflows with joins
/// - Conditional workflows
/// - Workflow mobility (continue on different peer)
/// - Failure and recovery
/// - Provenance tracking
use crate::{FlowRef, WorkflowGraph, WorkflowNode, WorkflowRef, WorkflowRuntime};

#[cfg(test)]
mod tests {
    use super::*;

    /// Test 1: Simple linear workflow
    /// Document → Extract → Embed → Done
    #[test]
    fn test_simple_linear_workflow() {
        let mut runtime = WorkflowRuntime::new();

        // Define workflow
        let workflow_ref = WorkflowRef::new("workflow", "document-processing", 1);
        let graph = WorkflowGraph::new(workflow_ref.clone())
            .add_node(WorkflowNode::new("extract", "extract-text"))
            .add_node(WorkflowNode::new("embed", "embedding").with_input("extract"))
            .add_node(WorkflowNode::new("save", "save-results").with_input("embed"))
            .add_edge("extract", "embed")
            .add_edge("embed", "save");

        runtime
            .register_workflow(graph)
            .expect("Failed to register workflow");

        // Create instance
        let document = FlowRef::new("documents", "doc-1");
        let instance_id = runtime
            .create_instance(workflow_ref.clone(), "workflow-1", vec![document.clone()])
            .expect("Failed to create instance");

        // Start workflow
        runtime
            .start_instance(&instance_id)
            .expect("Failed to start");

        // Verify entry point (extract) is ready
        let ready = runtime.ready_steps(&instance_id);
        assert!(ready.contains(&"extract".to_string()));
        assert!(!ready.contains(&"embed".to_string()));

        // Execute extract step
        runtime
            .claim_step(&instance_id, "extract", "peer-a")
            .expect("Failed to claim");
        runtime
            .step_completed(&instance_id, "extract", FlowRef::new("texts", "text-1"))
            .expect("Failed to complete");

        // Verify embed becomes ready
        let ready = runtime.ready_steps(&instance_id);
        assert!(ready.contains(&"embed".to_string()));

        // Execute embed step
        runtime
            .claim_step(&instance_id, "embed", "peer-b")
            .expect("Failed to claim");
        runtime
            .step_completed(&instance_id, "embed", FlowRef::new("embeddings", "embed-1"))
            .expect("Failed to complete");

        // Verify save becomes ready
        let ready = runtime.ready_steps(&instance_id);
        assert!(ready.contains(&"save".to_string()));

        // Execute save step
        runtime
            .claim_step(&instance_id, "save", "peer-c")
            .expect("Failed to claim");
        runtime
            .step_completed(&instance_id, "save", FlowRef::new("results", "result-1"))
            .expect("Failed to complete");

        // Verify workflow is completed
        let instance = runtime.get_instance(&instance_id).unwrap();
        assert_eq!(instance.state, crate::WorkflowState::Completed);

        // Check operations log
        let ops = runtime.operations_log();
        assert!(!ops.is_empty());
        println!("Generated {} workflow operations", ops.len());
    }

    /// Test 2: Parallel workflow with join
    /// Document → Extract → (Embed || Classify) → Join → Summarize
    #[test]
    fn test_parallel_workflow_with_join() {
        let mut runtime = WorkflowRuntime::new();

        let workflow_ref = WorkflowRef::new("workflow", "parallel-processing", 1);
        let graph = WorkflowGraph::new(workflow_ref.clone())
            .add_node(WorkflowNode::new("extract", "extract-text"))
            .add_node(WorkflowNode::new("embed", "embedding").with_input("extract"))
            .add_node(WorkflowNode::new("classify", "classifier").with_input("extract"))
            .add_node(
                WorkflowNode::new("join", "merge-results")
                    .with_input("embed")
                    .with_input("classify"),
            )
            .add_node(WorkflowNode::new("summarize", "summarization").with_input("join"))
            .add_edge("extract", "embed")
            .add_edge("extract", "classify")
            .add_edge("embed", "join")
            .add_edge("classify", "join")
            .add_edge("join", "summarize");

        runtime
            .register_workflow(graph)
            .expect("Failed to register workflow");

        let instance_id = runtime
            .create_instance(
                workflow_ref.clone(),
                "workflow-2",
                vec![FlowRef::new("documents", "doc-1")],
            )
            .expect("Failed to create instance");

        runtime
            .start_instance(&instance_id)
            .expect("Failed to start");

        // Extract completes
        runtime
            .claim_step(&instance_id, "extract", "peer-a")
            .expect("Failed to claim");
        runtime
            .step_completed(&instance_id, "extract", FlowRef::new("texts", "text-1"))
            .expect("Failed to complete");

        // Both embed and classify should be ready
        let ready = runtime.ready_steps(&instance_id);
        assert!(ready.contains(&"embed".to_string()));
        assert!(ready.contains(&"classify".to_string()));
        assert!(!ready.contains(&"join".to_string()));

        // Complete embed
        runtime
            .claim_step(&instance_id, "embed", "peer-b")
            .expect("Failed to claim");
        runtime
            .step_completed(&instance_id, "embed", FlowRef::new("embeddings", "embed-1"))
            .expect("Failed to complete");

        // Complete classify
        runtime
            .claim_step(&instance_id, "classify", "peer-c")
            .expect("Failed to claim");
        runtime
            .step_completed(&instance_id, "classify", FlowRef::new("classes", "class-1"))
            .expect("Failed to complete");

        // Now join should be ready
        let ready = runtime.ready_steps(&instance_id);
        assert!(ready.contains(&"join".to_string()));

        // Complete join and summarize
        runtime
            .claim_step(&instance_id, "join", "peer-d")
            .expect("Failed to claim");
        runtime
            .step_completed(&instance_id, "join", FlowRef::new("merged", "merged-1"))
            .expect("Failed to complete");

        runtime
            .claim_step(&instance_id, "summarize", "peer-e")
            .expect("Failed to claim");
        runtime
            .step_completed(
                &instance_id,
                "summarize",
                FlowRef::new("summary", "summary-1"),
            )
            .expect("Failed to complete");

        // Verify workflow is completed
        let instance = runtime.get_instance(&instance_id).unwrap();
        assert_eq!(instance.state, crate::WorkflowState::Completed);
    }

    /// Test 3: Workflow with blocking and approval
    /// Document → Review (blocked awaiting approval) → Process
    #[test]
    fn test_workflow_with_human_intervention() {
        let mut runtime = WorkflowRuntime::new();

        let workflow_ref = WorkflowRef::new("workflow", "approval-required", 1);
        let graph = WorkflowGraph::new(workflow_ref.clone())
            .add_node(WorkflowNode::new("extract", "extract-text"))
            .add_node(WorkflowNode::new("review", "manual-review").with_input("extract"))
            .add_node(WorkflowNode::new("process", "process").with_input("review"));

        runtime
            .register_workflow(graph)
            .expect("Failed to register workflow");

        let instance_id = runtime
            .create_instance(
                workflow_ref.clone(),
                "workflow-3",
                vec![FlowRef::new("documents", "doc-1")],
            )
            .expect("Failed to create instance");

        runtime
            .start_instance(&instance_id)
            .expect("Failed to start");

        // Complete extract
        runtime
            .claim_step(&instance_id, "extract", "peer-a")
            .expect("Failed to claim");
        runtime
            .step_completed(&instance_id, "extract", FlowRef::new("texts", "text-1"))
            .expect("Failed to complete");

        // Review should be ready
        let ready = runtime.ready_steps(&instance_id);
        assert!(ready.contains(&"review".to_string()));

        // Claim review but immediately block it
        runtime
            .claim_step(&instance_id, "review", "peer-b")
            .expect("Failed to claim");
        runtime
            .step_blocked(
                &instance_id,
                "review",
                crate::BlockedReason::AwaitingApproval("Waiting for manager approval".to_string()),
            )
            .expect("Failed to block");

        // Process should not be ready
        let ready = runtime.ready_steps(&instance_id);
        assert!(!ready.contains(&"process".to_string()));

        // Unblock review
        runtime
            .unblock_step(&instance_id, "review")
            .expect("Failed to unblock");

        // Review should be ready again
        let ready = runtime.ready_steps(&instance_id);
        assert!(ready.contains(&"review".to_string()));

        // Complete review
        runtime
            .step_completed(&instance_id, "review", FlowRef::new("reviews", "review-1"))
            .expect("Failed to complete");

        // Process should now be ready
        let ready = runtime.ready_steps(&instance_id);
        assert!(ready.contains(&"process".to_string()));
    }

    /// Test 4: Workflow continuation after peer failure
    /// Demonstrates workflow mobility - workflow continues on different peer
    #[test]
    fn test_workflow_continues_on_peer_failure() {
        let mut runtime = WorkflowRuntime::new();

        let workflow_ref = WorkflowRef::new("workflow", "failover-test", 1);
        let graph = WorkflowGraph::new(workflow_ref.clone())
            .add_node(WorkflowNode::new("step1", "capability-a"))
            .add_node(WorkflowNode::new("step2", "capability-b").with_input("step1"));

        runtime
            .register_workflow(graph)
            .expect("Failed to register workflow");

        let instance_id = runtime
            .create_instance(workflow_ref.clone(), "workflow-4", Vec::new())
            .expect("Failed to create instance");

        runtime
            .start_instance(&instance_id)
            .expect("Failed to start");

        // Step1 starts on peer-a
        runtime
            .claim_step(&instance_id, "step1", "peer-a")
            .expect("Failed to claim");
        runtime
            .step_executing(&instance_id, "step1")
            .expect("Failed to mark executing");

        // Simulate peer-a failure by not completing
        // The workflow is durable - the step state is preserved in the runtime

        // Get the instance to verify state is persisted
        let instance = runtime.get_instance(&instance_id).unwrap();
        assert_eq!(
            instance.get_step_state("step1"),
            Some(crate::WorkflowStepState::Running)
        );

        // Another peer (peer-b) can read the workflow state and continue
        // The step remains claimed but peer-b can check and proceed
        // (In production, peer-b would acquire the workflow state from the fabric)

        // For this test, we complete the step (simulating recovery and completion)
        runtime
            .step_completed(&instance_id, "step1", FlowRef::new("results", "result-1"))
            .expect("Failed to complete");

        // Step2 becomes ready and can be claimed by peer-c
        let ready = runtime.ready_steps(&instance_id);
        assert!(ready.contains(&"step2".to_string()));

        runtime
            .claim_step(&instance_id, "step2", "peer-c")
            .expect("Failed to claim by peer-c");
        runtime
            .step_completed(&instance_id, "step2", FlowRef::new("results", "result-2"))
            .expect("Failed to complete step2");

        // Workflow completes
        let instance = runtime.get_instance(&instance_id).unwrap();
        assert_eq!(instance.state, crate::WorkflowState::Completed);
    }

    /// Test 5: Multiple concurrent workflows
    /// Demonstrates independent workflow instances in the same runtime
    #[test]
    fn test_multiple_concurrent_workflows() {
        let mut runtime = WorkflowRuntime::new();

        // Define workflow
        let workflow_ref = WorkflowRef::new("workflow", "multi-workflow", 1);
        let graph = WorkflowGraph::new(workflow_ref.clone())
            .add_node(WorkflowNode::new("step1", "capability-a"))
            .add_node(WorkflowNode::new("step2", "capability-b").with_input("step1"))
            .add_edge("step1", "step2");

        runtime
            .register_workflow(graph)
            .expect("Failed to register workflow");

        // Create multiple instances
        let instance1 = runtime
            .create_instance(workflow_ref.clone(), "wf-1", Vec::new())
            .expect("Failed to create");
        let instance2 = runtime
            .create_instance(workflow_ref.clone(), "wf-2", Vec::new())
            .expect("Failed to create");
        let instance3 = runtime
            .create_instance(workflow_ref.clone(), "wf-3", Vec::new())
            .expect("Failed to create");

        runtime.start_instance(&instance1).expect("Failed to start");
        runtime.start_instance(&instance2).expect("Failed to start");
        runtime.start_instance(&instance3).expect("Failed to start");

        // All should have step1 ready
        assert!(runtime
            .ready_steps(&instance1)
            .contains(&"step1".to_string()));
        assert!(runtime
            .ready_steps(&instance2)
            .contains(&"step1".to_string()));
        assert!(runtime
            .ready_steps(&instance3)
            .contains(&"step1".to_string()));

        // Execute each workflow independently
        for (instance_id, peer) in &[
            (&instance1, "peer-a"),
            (&instance2, "peer-b"),
            (&instance3, "peer-c"),
        ] {
            runtime
                .claim_step(*instance_id, "step1", *peer)
                .expect("Failed to claim");
            runtime
                .step_completed(
                    *instance_id,
                    "step1",
                    FlowRef::new("results", &format!("result-{}", peer)),
                )
                .expect("Failed to complete");

            runtime
                .claim_step(*instance_id, "step2", *peer)
                .expect("Failed to claim");
            runtime
                .step_completed(
                    *instance_id,
                    "step2",
                    FlowRef::new("results", &format!("final-{}", peer)),
                )
                .expect("Failed to complete");
        }

        // All workflows should be completed
        assert_eq!(
            runtime.get_instance(&instance1).unwrap().state,
            crate::WorkflowState::Completed
        );
        assert_eq!(
            runtime.get_instance(&instance2).unwrap().state,
            crate::WorkflowState::Completed
        );
        assert_eq!(
            runtime.get_instance(&instance3).unwrap().state,
            crate::WorkflowState::Completed
        );
    }

    /// Test 6: Complex DAG with multiple dependencies
    /// Tests the reactive advancement with more complex dependency patterns
    #[test]
    fn test_complex_dag_workflow() {
        let mut runtime = WorkflowRuntime::new();

        let workflow_ref = WorkflowRef::new("workflow", "complex-dag", 1);
        let graph = WorkflowGraph::new(workflow_ref.clone())
            .add_node(WorkflowNode::new("a", "cap-a"))
            .add_node(WorkflowNode::new("b", "cap-b").with_input("a"))
            .add_node(WorkflowNode::new("c", "cap-c").with_input("a"))
            .add_node(
                WorkflowNode::new("d", "cap-d")
                    .with_input("b")
                    .with_input("c"),
            )
            .add_node(WorkflowNode::new("e", "cap-e").with_input("d"))
            .add_edge("a", "b")
            .add_edge("a", "c")
            .add_edge("b", "d")
            .add_edge("c", "d")
            .add_edge("d", "e");

        runtime
            .register_workflow(graph)
            .expect("Failed to register workflow");

        let instance_id = runtime
            .create_instance(workflow_ref.clone(), "complex-1", Vec::new())
            .expect("Failed to create");

        runtime
            .start_instance(&instance_id)
            .expect("Failed to start");

        // a should be ready
        assert!(runtime.ready_steps(&instance_id).contains(&"a".to_string()));

        // Complete a
        runtime
            .claim_step(&instance_id, "a", "peer-1")
            .expect("Failed");
        runtime
            .step_completed(&instance_id, "a", FlowRef::new("res", "a-result"))
            .expect("Failed");

        // b and c should be ready
        let ready = runtime.ready_steps(&instance_id);
        assert!(ready.contains(&"b".to_string()));
        assert!(ready.contains(&"c".to_string()));

        // Complete b
        runtime
            .claim_step(&instance_id, "b", "peer-2")
            .expect("Failed");
        runtime
            .step_completed(&instance_id, "b", FlowRef::new("res", "b-result"))
            .expect("Failed");

        // d should not be ready yet (c not done)
        assert!(!runtime.ready_steps(&instance_id).contains(&"d".to_string()));

        // Complete c
        runtime
            .claim_step(&instance_id, "c", "peer-3")
            .expect("Failed");
        runtime
            .step_completed(&instance_id, "c", FlowRef::new("res", "c-result"))
            .expect("Failed");

        // Now d should be ready
        assert!(runtime.ready_steps(&instance_id).contains(&"d".to_string()));

        // Complete d and e
        runtime
            .claim_step(&instance_id, "d", "peer-4")
            .expect("Failed");
        runtime
            .step_completed(&instance_id, "d", FlowRef::new("res", "d-result"))
            .expect("Failed");

        runtime
            .claim_step(&instance_id, "e", "peer-5")
            .expect("Failed");
        runtime
            .step_completed(&instance_id, "e", FlowRef::new("res", "e-result"))
            .expect("Failed");

        // Workflow should be completed
        let instance = runtime.get_instance(&instance_id).unwrap();
        assert_eq!(instance.state, crate::WorkflowState::Completed);
    }
}