acts 0.24.0

a fast, lightweight, extensiable workflow engine
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
use crate::{
    Act, Config, Engine, Vars, Workflow,
    config::ConfigData,
    data,
    scheduler::NodeContent,
    scheduler::{NodeTree, Process, Runtime, TaskState},
    store::{DbCollectionIden, MemoryStore},
    utils,
};
use std::sync::Arc;
/// Dynamic act chains must survive a store round-trip: node ids are persisted
/// with parent/prev/next links and rebuilt on load, so `Task::move_next`
/// (which reads `task.node.next()`) keeps working after restore.
#[tokio::test]
async fn cache_restore_dynamic_acts() {
    let engine = Engine::new().start().await.unwrap();
    let rt = engine.runtime();
    let store = rt.cache().store();

    let workflow = Workflow::new()
        .with_id("m1")
        .with_step(|step| step.with_id("step1"));
    let pid = utils::longid();
    let proc = rt.create_proc(&pid, &workflow);

    // build the dynamic act chain like ctx.build_acts does
    let act_ids;
    {
        let tree = proc.tree();
        let step1 = tree.node("step1").unwrap();
        let mut prev = step1.clone();
        let mut acts = [
            Act::irq(|r| r.with_params_vars(|v| v.with("key", "act1"))),
            Act::irq(|r| r.with_params_vars(|v| v.with("key", "act2"))),
            Act::irq(|r| r.with_params_vars(|v| v.with("key", "act3"))),
        ];
        for act in acts.iter_mut() {
            if act.id.is_empty() {
                act.id = utils::shortid();
            }
            let node = tree
                .append_node(
                    &step1,
                    &act.id,
                    NodeContent::Act(act.clone()),
                    step1.level + 1,
                )
                .unwrap();
            if node.level == prev.level {
                prev.set_next(&node, true);
            } else {
                node.set_parent(&step1);
            }
            prev = node;
        }
        act_ids = acts.iter().map(|a| a.id.clone()).collect::<Vec<_>>();
    }

    // create tasks for the chain like sched_task does, then save everything
    let step_node = proc.tree().node("step1").unwrap();
    let step_task = proc.create_task(&step_node, None).unwrap();
    let mut prev_task = step_task;
    for id in &act_ids {
        let node = proc.tree().node(id).unwrap();
        let task = proc.create_task(&node, Some(prev_task.clone())).unwrap();
        prev_task = task;
    }
    for task in proc.tasks() {
        store.upsert_task(&task).await.unwrap();
    }
    store.upsert_proc(&proc).await.unwrap();

    // restore the process from the store
    let restored = store.load_proc(&pid, &rt).await.unwrap().unwrap();
    let act_tasks = restored
        .tasks()
        .into_iter()
        .filter(|t| t.node().kind() == crate::scheduler::NodeKind::Act)
        .collect::<Vec<_>>();
    assert_eq!(act_tasks.len(), 3);

    let a1 = restored.tree().node(&act_ids[0]).unwrap();
    let a2 = restored.tree().node(&act_ids[1]).unwrap();
    let a3 = restored.tree().node(&act_ids[2]).unwrap();
    // node chain restored: what Task::move_next reads
    assert_eq!(a1.next().upgrade().unwrap().id(), a2.id());
    assert_eq!(a2.prev().upgrade().unwrap().id(), a1.id());
    assert_eq!(a2.next().upgrade().unwrap().id(), a3.id());
    assert!(a3.next().upgrade().is_none());
    // parent and children restored
    assert_eq!(a1.parent().unwrap().id(), "step1");
    let step = restored.tree().node("step1").unwrap();
    assert_eq!(step.children().len(), 1);
    assert_eq!(step.children()[0].id(), a1.id());
}

#[tokio::test]
async fn cache_count() {
    let engine = Engine::builder()
        .cache_size(10)
        .build()
        .start()
        .await
        .unwrap();
    let rt = engine.runtime();
    let cache = rt.cache();

    let proc = Process::new(&utils::longid(), &rt);
    cache.push_proc(&proc).await.unwrap();
    assert_eq!(cache.count(), 1);
}

#[tokio::test]
async fn cache_push_get() {
    let engine = Engine::builder()
        .cache_size(10)
        .build()
        .start()
        .await
        .unwrap();
    let rt = engine.runtime();
    let cache = rt.cache();
    let pid = utils::longid();
    let proc = Process::new(&pid, &rt);
    cache.push_proc(&proc).await.unwrap();
    assert_eq!(cache.count(), 1);

    let proc = cache.proc(&pid, &engine.runtime()).await.unwrap();
    assert!(proc.is_some());
}

#[tokio::test]
async fn cache_push_to_store() {
    let engine = Engine::builder()
        .cache_size(1)
        .build()
        .start()
        .await
        .unwrap();
    let rt = engine.runtime();
    let cache = rt.cache();

    let mut pids = Vec::new();
    for _ in 0..5 {
        let pid = utils::longid();
        let proc = Process::new(&pid, &rt);
        cache.push_proc(&proc).await.unwrap();
        pids.push(pid);
    }

    // the resident set has no eviction policy — `cache_cap` gates *starts*
    // (`Cache::admit` parks over-cap ones), it never evicts resident
    // processes, so all five pushed processes stay in memory
    assert_eq!(cache.count(), 5);
    for pid in pids.iter() {
        let exists = cache.store().procs().exists(pid).await.unwrap();
        assert!(exists);
    }
}

#[tokio::test]
async fn cache_remove() {
    let engine = Engine::builder()
        .cache_size(10)
        .build()
        .start()
        .await
        .unwrap();
    let rt = engine.runtime();
    let cache = rt.cache();

    let mut pids = Vec::new();
    for _ in 0..5 {
        let pid = utils::longid();
        let proc = Process::new(&pid, &rt);
        cache.push_proc(&proc).await.unwrap();
        pids.push(pid);
    }

    assert_eq!(cache.count(), 5);
    for pid in pids.iter() {
        let exists = cache.store().procs().exists(pid).await.unwrap();
        assert!(exists);

        cache.remove(pid).await.unwrap();
        assert!(cache.proc(pid, &engine.runtime()).await.unwrap().is_none());

        let exists = cache.store().procs().exists(pid).await.unwrap();
        assert!(!exists);
    }
    assert_eq!(cache.count(), 0);
}

#[tokio::test]
async fn cache_upsert() {
    let engine = Engine::builder()
        .cache_size(10)
        .build()
        .start()
        .await
        .unwrap();
    let rt = engine.runtime();
    let mut workflow = Workflow::new().with_step(|step| step.with_name("step1"));

    let pid = utils::longid();
    let tree = NodeTree::build(&mut workflow).unwrap();

    let cache = rt.cache();
    let proc = Process::new(&pid, &rt);
    cache.push_proc(&proc).await.unwrap();
    assert_eq!(cache.count(), 1);

    let node = tree.root.as_ref().unwrap();
    let task = proc.create_task(node, None).unwrap();

    proc.set_state(TaskState::Running);
    cache.upsert(&task).await.unwrap();

    let proc = cache.proc(&pid, &engine.runtime()).await.unwrap().unwrap();
    assert_eq!(proc.state(), TaskState::Running);
}

/// `Cache::remove` is serialized through the store writer (FIFO): a task
/// write queued before the removal is applied first, and then every row of
/// the process (proc, tasks, outbox ops) is dropped — one flush reports no
/// failure, so removal can never race the writes still queued behind it.
#[tokio::test]
async fn cache_remove_after_writer_writes_drops_all_rows() {
    let engine = Engine::builder()
        .cache_size(10)
        .build()
        .start()
        .await
        .unwrap();
    let rt = engine.runtime();
    let cache = rt.cache();
    let store = cache.store();

    let pid = utils::longid();
    let proc = Process::new(&pid, &rt);
    cache.push_proc(&proc).await.unwrap();
    assert!(store.procs().exists(&pid).await.unwrap());

    let mut workflow = Workflow::new().with_step(|step| step.with_name("step1"));
    let tree = NodeTree::build(&mut workflow).unwrap();
    let node = tree.root.as_ref().unwrap();
    let task = proc.create_task(node, None).unwrap();
    let tid = task.id.clone();
    // the persisted row id is the composite pid-tid
    let task_row_id = utils::Id::new(&pid, &tid).id();

    // queue a task write on the writer and remove without flushing first:
    // remove() must drain the queue (the task write applies) before it drops
    // the rows
    proc.set_state(TaskState::Completed);
    cache.upsert_async(&task).unwrap();
    cache.remove(&pid).await.unwrap();

    assert!(!store.procs().exists(&pid).await.unwrap());
    assert!(store.tasks().find(&task_row_id).await.is_err());
    assert!(cache.proc(&pid, &rt).await.unwrap().is_none());
    cache.flush().await.unwrap();
}

/// A task write that reaches the writer after its process was removed is
/// dead data: it is skipped — neither applied (which would resurrect the
/// rows) nor failed (which would poison a later flush).
#[tokio::test]
async fn cache_writes_after_remove_are_skipped() {
    let engine = Engine::builder()
        .cache_size(10)
        .build()
        .start()
        .await
        .unwrap();
    let rt = engine.runtime();
    let cache = rt.cache();
    let store = cache.store();

    let pid = utils::longid();
    let proc = Process::new(&pid, &rt);
    cache.push_proc(&proc).await.unwrap();

    let mut workflow = Workflow::new().with_step(|step| step.with_name("step1"));
    let tree = NodeTree::build(&mut workflow).unwrap();
    let node = tree.root.as_ref().unwrap();
    let task = proc.create_task(node, None).unwrap();
    let tid = task.id.clone();
    // the persisted row id is the composite pid-tid
    let task_row_id = utils::Id::new(&pid, &tid).id();

    proc.set_state(TaskState::Completed);
    cache.upsert_async(&task).unwrap();
    cache.flush().await.unwrap();
    assert!(store.tasks().find(&task_row_id).await.is_ok());

    cache.remove(&pid).await.unwrap();
    assert!(store.tasks().find(&task_row_id).await.is_err());

    // late write for the removed process: skipped silently
    cache.upsert_async(&task).unwrap();
    cache.flush().await.unwrap();
    assert!(
        store.tasks().find(&task_row_id).await.is_err(),
        "late write resurrected the task row of a removed process"
    );
    assert!(!store.procs().exists(&pid).await.unwrap());
}

/// `start_parked` touches ONLY parked rows (durable `None` state): it starts them
/// into free slots and leaves every other row alone. Non-`None` non-terminal
/// rows (`Ready`/`Running`/`Pending`) belong to processes with no in-memory
/// executor to drive them — a live process's row is its resident instance
/// (reloading it would create a second one), and a crash-left one is reached
/// on demand (`proc()`) — so `start_parked` must NOT pull them into the cache, and
/// terminal rows are never refilled either. Runtime built WITHOUT an event
/// loop so the started seeds stay `Running` and every assertion is
/// deterministic.
#[tokio::test]
async fn cache_start_parked_refills_only_parked_none_rows() {
    let config = Config {
        data: ConfigData {
            cache_cap: Some(5),
            ..Default::default()
        },
        table: Default::default(),
    };
    let rt = Runtime::new(&config, None).unwrap();
    let cache = rt.cache();
    let model = Workflow::new()
        .with_id("m1")
        .with_step(|step| step.with_name("step1"));
    cache.store().deploy(&model, None).await.unwrap();

    let seed = |state: TaskState| data::Proc {
        id: utils::longid(),
        name: "test".to_string(),
        mid: "m1".to_string(),
        state: state.to_string(),
        start_time: 0,
        end_time: 0,
        timestamp: 0,
        model: model.to_json().unwrap(),
        env: "{}".to_string(),
        err: None,
        removable: false,
        v: data::Proc::version(),
    };

    assert_eq!(cache.count(), 0);

    // parked (None) rows that restore MUST start, oldest first
    let parked = [
        seed(TaskState::None),
        seed(TaskState::None),
        seed(TaskState::None),
    ];
    let parked_ids: Vec<String> = parked.iter().map(|p| p.id.clone()).collect();
    // rows restore MUST leave alone: crash-left working states + finished
    let ignored = [
        seed(TaskState::Ready),
        seed(TaskState::Running),
        seed(TaskState::Pending),
        seed(TaskState::Completed),
        seed(TaskState::Error),
    ];
    for proc in parked.into_iter().chain(ignored.into_iter()) {
        cache.store().procs().create(&proc).await.unwrap();
    }

    cache.start_parked(&rt).await.unwrap();

    // exactly the three parked rows were started — the non-None/terminal
    // seeds stay out of the resident set
    assert_eq!(cache.count(), 3);
    let resident: Vec<String> = cache.procs().iter().map(|p| p.id().to_string()).collect();
    for pid in &parked_ids {
        assert!(resident.contains(pid), "parked row {pid} must be started");
        let row = cache.store().procs().find(pid).await.unwrap();
        assert!(
            TaskState::from(row.state.as_str()).is_running(),
            "parked row {pid} must be Running"
        );
    }
    let unexpected: Vec<&String> = resident
        .iter()
        .filter(|p| !parked_ids.contains(p))
        .collect();
    assert!(
        unexpected.is_empty(),
        "start_parked must not load non-parked rows: {unexpected:?}"
    );

    rt.close().await;
}

/// A finished process is evicted from the in-memory cache on its terminal
/// proc event (its store rows stay — the sweeper deletes them only after the
/// process's deliveries settled), so the freed slot lets the restore pass
/// start parked processes (`None` state) into it. Without the eviction,
/// finished processes would squat in the cache and block restoring others.
#[tokio::test(flavor = "multi_thread")]
async fn cache_finished_proc_frees_slot_for_restore() {
    let engine = Engine::builder()
        .cache_size(4)
        .build()
        .start()
        .await
        .unwrap();
    let rt = engine.runtime();
    let cache = rt.cache();
    let store = cache.store();

    let model = Workflow::new()
        .with_id("m1")
        .with_step(|step| step.with_name("step1"));
    store.deploy(&model, None).await.unwrap();

    // three processes persisted but never started (a crash left them behind)
    let mut seeds = Vec::new();
    for _ in 0..3 {
        let pid = utils::longid();
        let proc = data::Proc {
            id: pid.clone(),
            name: "seed".to_string(),
            mid: "m1".to_string(),
            state: TaskState::None.into(),
            start_time: 0,
            end_time: 0,
            timestamp: 0,
            model: model.to_json().unwrap(),
            env: "{}".to_string(),
            err: None,
            removable: false,
            v: data::Proc::version(),
        };
        store.procs().create(&proc).await.unwrap();
        seeds.push(pid);
    }

    // two real processes run concurrently; while both are resident the cache
    // count (2) sits at the restore checkpoint of cap 4 (cap/2 = 2), so no
    // restore pass starts — only their terminal eviction drops the count
    // below the checkpoint and lets the seeds be restored
    let (a, b) = tokio::join!(rt.start(&model, Vars::new()), rt.start(&model, Vars::new()));
    let running = [a.unwrap(), b.unwrap()];
    let mut pids = seeds.clone();
    pids.extend(running.iter().map(|p| p.id().to_string()));

    // every process — the two that ran and the three restored seeds — must
    // end up terminal in the store (or gone, swept after its rows settled);
    // under the old behavior the finished procs stayed cached and the seeds
    // were never restored, so they would remain `None` forever
    tokio::time::timeout(std::time::Duration::from_secs(10), async {
        loop {
            let mut done = true;
            for pid in &pids {
                if let Ok(row) = store.procs().find(pid).await
                    && !TaskState::from(row.state.as_str()).is_completed()
                {
                    done = false;
                }
            }
            if done {
                break;
            }
            tokio::time::sleep(std::time::Duration::from_millis(20)).await;
        }
    })
    .await
    .expect("finished/restored processes never reached a terminal state in time");
}

/// The resident set has no eviction policy: once it is full a new process is
/// *parked* — its durable row stays `None` and it is NOT cached (a resident
/// parked row would occupy a slot forever, since `restore` skips resident
/// pids) — instead of evicting a live process to make room. A terminal event
/// then frees a slot and `restore` starts the parked process, which runs to
/// a terminal row of its own.
#[tokio::test]
async fn cache_park_over_cap_then_refill_on_terminal() {
    let engine = Engine::builder()
        .cache_size(2)
        .build()
        .start()
        .await
        .unwrap();
    let rt = engine.runtime();
    let cache = rt.cache();
    let model = Workflow::new()
        .with_id("m1")
        .with_step(|step| step.with_name("step1"));
    cache.store().deploy(&model, None).await.unwrap();

    let make = |tag: &str| {
        let pid = format!("park-{tag}");
        let proc = Process::new(&pid, &rt);
        proc.load(&model).unwrap();
        (pid, proc)
    };

    let (pid1, p1) = make("1");
    let (pid2, p2) = make("2");
    let (pid3, p3) = make("3");

    // two admitted starts fill the resident set exactly to cap
    assert!(cache.admit(&p1).await.unwrap());
    assert!(cache.admit(&p2).await.unwrap());
    assert_eq!(cache.count(), 2);

    // the third is parked: durable `None` row, no resident slot taken, and
    // the admitted processes are never evicted to make room for it
    assert!(!cache.admit(&p3).await.unwrap());
    assert_eq!(cache.count(), 2);
    let row = cache.store().procs().find(&pid3).await.unwrap();
    assert_eq!(row.state, TaskState::None.to_string());
    let resident: Vec<String> = cache.procs().iter().map(|p| p.id().to_string()).collect();
    assert!(resident.contains(&pid1));
    assert!(resident.contains(&pid2));
    assert!(!resident.contains(&pid3));

    // a demand load of a parked process must not cache it
    let got = cache.proc(&pid3, &rt).await.unwrap().unwrap();
    assert_eq!(got.id(), pid3);
    assert!(got.state().is_none());
    assert_eq!(cache.count(), 2);

    // terminal event: pid1 frees its slot; restore refills it with the
    // parked process and starts it
    cache.evict(&pid1);
    cache.start_parked(&rt).await.unwrap();
    tokio::time::timeout(std::time::Duration::from_secs(10), async {
        loop {
            match cache.store().procs().find(&pid3).await {
                Ok(row) if !TaskState::from(row.state.as_str()).is_completed() => {}
                // completed, or already swept away after its rows settled
                _ => break,
            }
            tokio::time::sleep(std::time::Duration::from_millis(20)).await;
        }
    })
    .await
    .expect("parked process never ran to completion after refill");
}

/// Parked rows refill FIFO (oldest first) and a refill never overshoots
/// `cap`: freeing one slot starts exactly the oldest parked process; newer
/// parked rows keep waiting for the next terminal event. The runtime is
/// built WITHOUT an event loop, so started processes stay `Running` (their
/// tasks sit in the queue) and every assertion is deterministic.
#[tokio::test]
async fn cache_parked_refill_is_oldest_first_within_cap() {
    let config = Config {
        data: ConfigData {
            cache_cap: Some(2),
            ..Default::default()
        },
        table: Default::default(),
    };
    let rt = Runtime::new(&config, None).unwrap();
    let cache = rt.cache();
    let model = Workflow::new()
        .with_id("m1")
        .with_step(|step| step.with_name("step1"));
    cache.store().deploy(&model, None).await.unwrap();

    let make = |tag: &str, ts: i64| {
        let pid = format!("park-fifo-{tag}");
        let proc = Process::new_with_timestamp(&pid, ts, &rt);
        proc.load(&model).unwrap();
        (pid, proc)
    };

    let (pid1, p1) = make("1", 100);
    let (_pid2, p2) = make("2", 200);
    let (old, p_old) = make("old", 10);
    let (new, p_new) = make("new", 999);

    assert!(cache.admit(&p1).await.unwrap());
    assert!(cache.admit(&p2).await.unwrap());
    assert_eq!(cache.count(), 2);

    // two parked rows, older first
    assert!(!cache.admit(&p_old).await.unwrap());
    assert!(!cache.admit(&p_new).await.unwrap());

    // freeing one slot starts only the oldest parked process — the newer one
    // keeps waiting, so the resident set never exceeds cap
    cache.evict(&pid1);
    cache.start_parked(&rt).await.unwrap();
    assert_eq!(cache.count(), 2);
    let resident: Vec<String> = cache.procs().iter().map(|p| p.id().to_string()).collect();
    assert!(
        resident.contains(&old),
        "oldest parked row must be refilled first: {resident:?}"
    );
    assert!(!resident.contains(&new));
    let still = cache.store().procs().find(&new).await.unwrap();
    assert_eq!(still.state, TaskState::None.to_string());
    let started = cache.store().procs().find(&old).await.unwrap();
    assert!(TaskState::from(started.state.as_str()).is_running());

    rt.close().await;
}

/// Scope vars live in their own rows: a lifecycle-only persist (state/timing
/// change, no data touched) must NOT write a vars row, and a data mutation
/// must create one with exactly that scope's content.
#[tokio::test]
async fn cache_vars_row_written_only_on_mutation() {
    let engine = Engine::new().start().await.unwrap();
    let rt = engine.runtime();
    let store = rt.cache().store();

    let workflow = Workflow::new()
        .with_id("m1")
        .with_step(|s| s.with_id("step1"));
    let pid = utils::longid();
    let proc = rt.create_proc(&pid, &workflow);
    let root = proc
        .create_task(&proc.tree().node("step1").unwrap(), None)
        .unwrap();
    let task_id = utils::Id::new(&pid, &root.id).id();
    proc.set_state(TaskState::Running);

    // 1. lifecycle-only write: state/time changed, vars untouched — no vars
    // row is created and the lifecycle row carries no scope vars
    root.set_pure_state(TaskState::Running);
    root.set_start_time(1);
    store.persist_task_rows(&root).await.unwrap();
    assert!(
        store.vars().find(&task_id).await.is_err(),
        "a lifecycle-only write must not write a scope vars row"
    );
    let row = store.tasks().find(&task_id).await.unwrap();
    let json = serde_json::to_string(&row).unwrap();
    assert!(
        !json.contains("\"data\"") && !json.contains("\"sealed\""),
        "the lifecycle row must not carry scope vars: {json}"
    );

    // 2. a data mutation flushes exactly the mutated scope's vars row
    root.set_data(&Vars::new().with("var1", 10));
    store.persist_task_rows(&root).await.unwrap();
    let vars = store.vars().find(&task_id).await.unwrap();
    let data: Vars = serde_json::from_str(&vars.data).unwrap();
    assert_eq!(data.get::<i32>("var1").unwrap(), 10);
    assert!(
        !root.is_vars_dirty(),
        "vars dirty flag must clear after the flush"
    );

    // 3. a later lifecycle-only write does not rewrite the vars row
    root.set_pure_state(TaskState::Completed);
    root.set_end_time(2);
    store.persist_task_rows(&root).await.unwrap();
    let vars = store.vars().find(&task_id).await.unwrap();
    let data: Vars = serde_json::from_str(&vars.data).unwrap();
    assert_eq!(
        data.get::<i32>("var1").unwrap(),
        10,
        "vars row must not be rewritten"
    );
}

/// A data write that lands in an ancestor scope — the classic "child output
/// folds up to the declaring owner" case — persists exactly that owner's
/// vars row, and restore re-attaches it: the store round-trip keeps the
/// ancestor's updated vars without ever touching the root row.
#[tokio::test]
async fn cache_vars_ancestor_scope_round_trip() {
    let engine = Engine::new().start().await.unwrap();
    let rt = engine.runtime();
    let store = rt.cache().store();

    let workflow = Workflow::new()
        .with_id("m1")
        .with_step(|s| s.with_id("step1"));
    let pid = utils::longid();
    let proc = rt.create_proc(&pid, &workflow);
    proc.set_state(TaskState::Running);

    // root (workflow) > step1 > act; the act's output folds up to step1,
    // the scope that declares it
    let root_node = proc.tree().root.clone().unwrap();
    let root = proc.create_task(&root_node, None).unwrap();
    let step1_node = proc.tree().node("step1").unwrap();
    let step1 = proc.create_task(&step1_node, Some(root.clone())).unwrap();
    let act_id = utils::shortid();
    {
        let tree = proc.tree();
        let act = Act::irq(|r| r.with_params_vars(|v| v.with("key", "a1"))).with_id(&act_id);
        let node = tree
            .append_node(
                &step1_node,
                &act_id,
                NodeContent::Act(act),
                step1_node.level + 1,
            )
            .unwrap();
        node.set_parent(&step1_node);
    }
    let act_node = proc.tree().node(&act_id).unwrap();
    let act = proc.create_task(&act_node, Some(step1.clone())).unwrap();
    let step1_tid = step1.id.clone();
    let root_tid = root.id.clone();

    // step1 declares `x`; the act writes it → step1's scope owns the value
    step1.set_data_with(|data| data.set("x", 1));
    store.persist_task_rows(&step1).await.unwrap();
    store.persist_task_rows(&root).await.unwrap();
    assert!(
        store
            .vars()
            .find(&utils::Id::new(&pid, &root_tid).id())
            .await
            .is_err(),
        "root scope has no vars row — it never mutated"
    );

    act.set_data_with(|data| data.set("x", 2));
    act.update_data(&act.data());
    assert!(
        step1.is_vars_dirty(),
        "the owner scope must be marked dirty"
    );
    assert!(!root.is_vars_dirty(), "the root scope must stay untouched");

    store.persist_task_rows(&act).await.unwrap();
    let step1_vars = store
        .vars()
        .find(&utils::Id::new(&pid, &step1_tid).id())
        .await
        .unwrap();
    let step1_data: Vars = serde_json::from_str(&step1_vars.data).unwrap();
    assert_eq!(
        step1_data.get::<i32>("x").unwrap(),
        2,
        "owner scope row updated"
    );
    assert!(
        store
            .vars()
            .find(&utils::Id::new(&pid, &root_tid).id())
            .await
            .is_err(),
        "root scope still has no vars row"
    );

    // restore: the step scope's vars re-attach to the reloaded task
    store.upsert_proc(&proc).await.unwrap();
    let restored = store.load_proc(&pid, &rt).await.unwrap().unwrap();
    let step1 = restored.task(&step1_tid).unwrap();
    assert_eq!(
        step1.with_data(|d| d.get::<i32>("x")),
        Some(2),
        "restored owner scope keeps its updated var"
    );
    assert_eq!(
        restored
            .task(&root_tid)
            .unwrap()
            .with_data(|d| d.get::<i32>("x")),
        None,
        "the untouched root scope stays empty"
    );
}

/// Boot-time resume priority: in-flight (`Ready`/`Running`/`Pending`) rows
/// are loaded into the resident set before parked (`None`) rows, both capped;
/// everything beyond the cap waits. Deterministic — the runtime has no event
/// loop, so nothing executes.
#[tokio::test]
async fn cache_resume_loads_in_flight_before_parked() {
    let config = Config {
        data: ConfigData {
            cache_cap: Some(3),
            ..Default::default()
        },
        table: Default::default(),
    };
    let rt = Runtime::new(&config, None).unwrap();
    let cache = rt.cache();
    let model = Workflow::new()
        .with_id("m1")
        .with_step(|step| step.with_id("step1"))
        .with_step(|step| step.with_id("step2"));
    cache.store().deploy(&model, None).await.unwrap();

    // an in-flight process with a task graph (a crash mid-run)
    let inflight = {
        let proc = Process::new_with_timestamp("resume-inflight", 5, &rt);
        proc.load(&model).unwrap();
        proc.set_pure_state(TaskState::Running);
        proc
    };
    let root = inflight
        .create_task(&inflight.tree().root.clone().unwrap(), None)
        .unwrap();
    let step1 = inflight
        .create_task(&inflight.tree().node("step1").unwrap(), Some(root.clone()))
        .unwrap();
    root.set_pure_state(TaskState::Running);
    step1.set_pure_state(TaskState::Ready);
    cache.store().upsert_proc(&inflight).await.unwrap();
    cache.store().upsert_task(&root).await.unwrap();
    cache.store().upsert_task(&step1).await.unwrap();

    // three parked rows (never started): two fit the free slots left by the
    // in-flight process under cap 3, the third must keep waiting
    let mut parked = Vec::new();
    for (tag, ts) in [("a", 10), ("b", 20), ("new", 999)] {
        let pid = format!("resume-parked-{tag}");
        let proc = Process::new_with_timestamp(&pid, ts, &rt);
        proc.load(&model).unwrap();
        proc.set_pure_state(TaskState::None);
        cache.store().upsert_proc(&proc).await.unwrap();
        parked.push(pid);
    }

    assert_eq!(cache.count(), 0);
    rt.resume().await.unwrap();

    // cap 3: the in-flight process is resumed first, then the two oldest
    // parked rows are started into the free slots; the newest keeps waiting
    assert_eq!(cache.count(), 3);
    let resident: Vec<String> = cache.procs().iter().map(|p| p.id().to_string()).collect();
    assert!(
        resident.contains(&"resume-inflight".to_string()),
        "in-flight process must be loaded first: {resident:?}"
    );
    assert!(resident.contains(&"resume-parked-a".to_string()));
    assert!(resident.contains(&"resume-parked-b".to_string()));
    assert!(!resident.contains(&"resume-parked-new".to_string()));
    let waiting = cache
        .store()
        .procs()
        .find("resume-parked-new")
        .await
        .unwrap();
    assert_eq!(waiting.state, TaskState::None.to_string());

    // the in-flight process's task graph was decoded and re-dispatched
    let loaded = cache.proc("resume-inflight", &rt).await.unwrap().unwrap();
    assert!(loaded.state().is_running());
    assert_eq!(
        loaded.task_by_nid("step1").first().unwrap().state(),
        TaskState::Ready
    );

    rt.close().await;
}

/// A process that was mid-run when the engine died is resumed by a fresh
/// engine on the same store: its in-flight tasks are re-dispatched and the
/// workflow runs to its terminal state instead of hanging forever.
#[tokio::test(flavor = "multi_thread")]
async fn cache_resume_in_flight_proc_after_restart() {
    let kv: Arc<dyn crate::store::KvStore> = Arc::new(MemoryStore::new());

    // engine 1 seeds the store with a mid-run process, then "crashes"
    let engine1 = Engine::builder()
        .set_store(kv.clone())
        .build()
        .start()
        .await
        .unwrap();
    let store = engine1.runtime().cache().store();
    let model = Workflow::new()
        .with_id("m1")
        .with_step(|step| step.with_id("step1"))
        .with_step(|step| step.with_id("step2"));
    store.deploy(&model, None).await.unwrap();

    let pid = "resume-restart".to_string();
    let proc = engine1.runtime().create_proc(&pid, &model);
    proc.set_pure_state(TaskState::Running);
    let root = proc
        .create_task(&proc.tree().root.clone().unwrap(), None)
        .unwrap();
    let step1 = proc
        .create_task(&proc.tree().node("step1").unwrap(), Some(root.clone()))
        .unwrap();
    root.set_pure_state(TaskState::Running);
    step1.set_pure_state(TaskState::Ready);
    store.upsert_proc(&proc).await.unwrap();
    store.upsert_task(&root).await.unwrap();
    store.upsert_task(&step1).await.unwrap();
    engine1.close().await;

    // engine 2 on the same store resumes the process to completion
    let engine2 = Engine::builder()
        .set_store(kv.clone())
        .build()
        .start()
        .await
        .unwrap();
    let store2 = engine2.runtime().cache().store();
    tokio::time::timeout(std::time::Duration::from_secs(10), async {
        loop {
            match store2.procs().find(&pid).await {
                Ok(row) if !TaskState::from(row.state.as_str()).is_completed() => {
                    tokio::time::sleep(std::time::Duration::from_millis(20)).await;
                }
                // completed, or swept away after its rows settled
                _ => break,
            }
        }
    })
    .await
    .expect("resumed process never reached a terminal state after restart");
    engine2.close().await;
}

/// Boot-resume overflow is not stranded: in-flight rows beyond the resident
/// cap are queued, and every slot freed by a terminal event (`refill`) loads
/// one of them back — `restore` alone could never, since it only refills
/// parked (`None`) rows. Deterministic — no event loop, so nothing executes.
#[tokio::test]
async fn cache_resume_overflow_drains_on_free_slot() {
    let config = Config {
        data: ConfigData {
            cache_cap: Some(2),
            ..Default::default()
        },
        table: Default::default(),
    };
    let rt = Runtime::new(&config, None).unwrap();
    let cache = rt.cache();
    let model = Workflow::new()
        .with_id("m1")
        .with_step(|step| step.with_id("step1"));
    cache.store().deploy(&model, None).await.unwrap();

    // four in-flight rows, only two fit
    let mut pids = Vec::new();
    for i in 0..4 {
        let pid = format!("overflow-{i}");
        let proc = Process::new_with_timestamp(&pid, i as i64 + 1, &rt);
        proc.load(&model).unwrap();
        proc.set_pure_state(TaskState::Running);
        cache.store().upsert_proc(&proc).await.unwrap();
        pids.push(pid);
    }

    assert_eq!(cache.count(), 0);
    rt.resume().await.unwrap();
    assert_eq!(cache.count(), 2);
    // the two overflow rows are queued for the next free slots
    assert_eq!(
        cache.pending_resume_ids(),
        vec![pids[2].clone(), pids[3].clone()]
    );

    // a terminal event frees pid0's slot: the oldest queued process is loaded
    cache.evict(&pids[0]);
    rt.restore().await.unwrap();
    assert_eq!(cache.count(), 2);
    let resident: Vec<String> = cache.procs().iter().map(|p| p.id().to_string()).collect();
    assert!(resident.contains(&pids[1]));
    assert!(resident.contains(&pids[2]));
    assert_eq!(cache.pending_resume_ids(), vec![pids[3].clone()]);

    // another terminal event frees pid1's slot: the last queued row is loaded
    cache.evict(&pids[1]);
    rt.restore().await.unwrap();
    assert_eq!(cache.count(), 2);
    let resident: Vec<String> = cache.procs().iter().map(|p| p.id().to_string()).collect();
    assert!(resident.contains(&pids[2]));
    assert!(resident.contains(&pids[3]));
    assert!(cache.pending_resume_ids().is_empty());

    rt.close().await;
}