acts 0.25.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
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
use crate::{
    Act, Action, Config, Engine, Vars, Workflow,
    config::ConfigData,
    data,
    event::EventAction,
    scheduler::{NodeContent, NodeTree, Process, Runtime, TaskState},
    store::{DbCollectionIden, KvStore, MemoryStore, ScanOptions, Store, StoreBatchOp},
    utils,
};
use std::sync::{
    Arc,
    atomic::{AtomicBool, AtomicUsize, Ordering},
};

/// Evicting a finished process must free its whole in-memory graph: the task
/// tree is owned by the process and every task references its process, so a
/// strong `Task.proc` would form a `Process → TaskTree → Task → Process`
/// cycle and the evicted process would leak as unreachable cyclic garbage
/// (`Arc` cannot collect a cycle with no external strong refs). `Task` holds
/// its process `Weak`ly instead — while a caller still holds the process its
/// tasks stay queryable, and once the last holder drops, everything frees.
#[tokio::test]
async fn cache_evict_breaks_proc_task_cycle() {
    let engine = Engine::builder().start().await.unwrap();
    let rt = engine.runtime();
    let cache = rt.cache();

    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();
    cache.start_proc(&proc, Some(&root)).await.unwrap();
    assert_eq!(cache.count(), 1);
    assert_eq!(proc.tasks().len(), 1);

    let weak = Arc::downgrade(&proc);
    cache.evict(&pid);
    assert_eq!(cache.count(), 0);
    // still held by the caller: the task tree stays queryable
    assert_eq!(proc.tasks().len(), 1);
    // last holder dropped: the process must be deallocated, not kept alive
    // by its own tasks
    drop(proc);
    drop(root);
    assert!(
        weak.upgrade().is_none(),
        "the evicted process must be deallocated — its tasks kept the cycle alive"
    );

    rt.close().await;
}
/// 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::builder().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).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).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).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).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).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).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).await.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).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).await.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).await.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).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).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");
}

/// Concurrent cache misses for the same pid must coalesce into one store load
/// and one in-memory instance. Returning independently loaded `Arc<Process>`
/// values would let callers drive the same durable process with two state trees.
#[tokio::test(flavor = "multi_thread")]
async fn cache_concurrent_proc_miss_returns_one_instance() {
    let config = Config::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 pid = utils::longid();
    let row = data::Proc {
        id: pid.clone(),
        name: "test".to_string(),
        mid: "m1".to_string(),
        state: TaskState::Running.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(),
    };
    cache.store().procs().create(&row).await.unwrap();

    let mut handles = Vec::new();
    for _ in 0..50 {
        let cache = cache.clone();
        let rt = rt.clone();
        let pid = pid.clone();
        handles.push(tokio::spawn(async move {
            cache.proc(&pid, &rt).await.unwrap().unwrap()
        }));
    }
    let mut handles = handles.into_iter();
    let first = handles.next().unwrap().await.unwrap();
    let expected = Arc::as_ptr(&first);
    let mut pointers = std::collections::HashSet::from([expected]);
    for handle in handles {
        let proc = handle.await.unwrap();
        assert!(Arc::ptr_eq(&first, &proc));
        pointers.insert(Arc::as_ptr(&proc));
    }
    assert_eq!(pointers.len(), 1);
    assert_eq!(cache.count(), 1);

    rt.close().await;
}

/// Admission claims a pid atomically. Concurrent fresh starts that all missed
/// the durable row can therefore produce only one executable instance.
#[tokio::test(flavor = "multi_thread")]
async fn cache_admit_same_pid_has_single_winner() {
    let config = Config {
        data: ConfigData {
            cache_cap: Some(1),
            ..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"));

    let pid = "concurrent-admit";
    let make = || {
        let proc = Process::new(pid, &rt);
        proc.load(&model).unwrap();
        proc
    };

    let mut handles = Vec::new();
    for _ in 0..20 {
        let cache = cache.clone();
        let proc = make();
        handles.push(tokio::spawn(async move { cache.admit(&proc).await }));
    }
    let mut admitted = 0;
    for handle in handles {
        match handle.await.unwrap() {
            Ok(true) => admitted += 1,
            Ok(false) => panic!("cap is 1, so no second process should be parked"),
            Err(err) => assert!(err.to_string().contains("duplicated")),
        }
    }
    assert_eq!(admitted, 1);
    assert_eq!(cache.count(), 1);

    rt.close().await;
}

/// 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::builder().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::builder().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())
        .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())
        .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 second overflow scan (e.g. a driver re-running the boot pass) is
    // idempotent: already-queued pids are not enqueued again, so the queue
    // stays bounded by overflow rows, not by the number of scans.
    rt.resume().await.unwrap();
    assert_eq!(cache.count(), 2);
    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;
}

/// KV store that can be switched, from the test thread, to block inside its
/// store I/O: `scan_prefix` (the call every store query runs through) and the
/// writes (`put`/`batch`, the call every collection mutation runs through).
/// The blocked counts let a test park a known number of callers before
/// releasing them, so interleavings are exercised deterministically.
struct GatedKv {
    inner: MemoryStore,
    find_gate: AtomicBool,
    find_entered: AtomicUsize,
    write_gate: AtomicBool,
    write_entered: AtomicUsize,
}

impl GatedKv {
    fn new() -> Self {
        Self {
            inner: MemoryStore::new(),
            find_gate: AtomicBool::new(false),
            find_entered: AtomicUsize::new(0),
            write_gate: AtomicBool::new(false),
            write_entered: AtomicUsize::new(0),
        }
    }

    fn arm_find(&self) {
        self.find_gate.store(true, Ordering::SeqCst);
    }

    fn disarm_find(&self) {
        self.find_gate.store(false, Ordering::SeqCst);
    }

    fn arm_write(&self) {
        self.write_gate.store(true, Ordering::SeqCst);
    }

    fn disarm_write(&self) {
        self.write_gate.store(false, Ordering::SeqCst);
    }

    /// Yield until `entered` callers have parked on the read gate.
    async fn wait_in_find(&self, entered: usize) {
        while self.find_entered.load(Ordering::SeqCst) < entered {
            tokio::task::yield_now().await;
        }
    }

    async fn wait_in_write(&self, entered: usize) {
        while self.write_entered.load(Ordering::SeqCst) < entered {
            tokio::task::yield_now().await;
        }
    }

    /// Park while `armed`, counting this caller as parked.
    async fn park(armed: &AtomicBool, entered: &AtomicUsize) {
        entered.fetch_add(1, Ordering::SeqCst);
        while armed.load(Ordering::SeqCst) {
            tokio::time::sleep(std::time::Duration::from_millis(1)).await;
        }
    }
}

#[async_trait::async_trait]
impl KvStore for GatedKv {
    async fn one(&self, key: &str) -> crate::Result<Option<Vec<u8>>> {
        self.inner.one(key).await
    }

    async fn put(&self, key: &str, value: Vec<u8>) -> crate::Result<()> {
        if self.write_gate.load(Ordering::SeqCst) {
            Self::park(&self.write_gate, &self.write_entered).await;
        }
        self.inner.put(key, value).await
    }

    async fn delete(&self, key: &str) -> crate::Result<()> {
        if self.write_gate.load(Ordering::SeqCst) {
            Self::park(&self.write_gate, &self.write_entered).await;
        }
        self.inner.delete(key).await
    }

    async fn batch(&self, ops: &[StoreBatchOp]) -> crate::Result<()> {
        if self.write_gate.load(Ordering::SeqCst) {
            Self::park(&self.write_gate, &self.write_entered).await;
        }
        self.inner.batch(ops).await
    }

    async fn scan_prefix(
        &self,
        key: &str,
        options: ScanOptions,
    ) -> crate::Result<Vec<(String, Vec<u8>)>> {
        if self.find_gate.load(Ordering::SeqCst) {
            Self::park(&self.find_gate, &self.find_entered).await;
        }
        self.inner.scan_prefix(key, options).await
    }
}

/// Admission must not wait on a restore pass's store I/O. The old design held
/// one global async mutex across `load_parked`, so a slow store blocked every
/// new start (`launch` → `admit`) behind a restore; with the capacity decision
/// made synchronously and the I/O outside the lock, `admit` completes while
/// the restore is still parked inside `scan_prefix`.
#[tokio::test(flavor = "multi_thread")]
async fn cache_admit_not_blocked_by_restore_io() {
    let config = Config {
        data: ConfigData {
            cache_cap: Some(4),
            ..Default::default()
        },
        table: Default::default(),
    };
    let kv = Arc::new(GatedKv::new());
    let kv_store: Arc<dyn KvStore> = kv.clone();
    let rt = Runtime::new(&config, Some(kv_store)).unwrap();
    let cache = rt.cache();
    let model = Workflow::new()
        .with_id("m1")
        .with_step(|step| step.with_id("step1"));

    // one resident proc (the restore's slot accounting) and one parked row
    let resident = Process::new("restore-io-resident", &rt);
    resident.load(&model).unwrap();
    assert!(cache.admit(&resident).await.unwrap());
    assert_eq!(cache.count(), 1);

    let parked = Process::new_with_timestamp("restore-io-parked", 1, &rt);
    parked.load(&model).unwrap();
    parked.set_pure_state(TaskState::None);
    cache.store().upsert_proc(&parked).await.unwrap();

    // park a restore pass inside its store read
    kv.arm_find();
    let restore = {
        let cache = cache.clone();
        let rt = rt.clone();
        tokio::spawn(async move { cache.start_parked(&rt).await })
    };
    kv.wait_in_find(1).await;

    // ...and admit a fresh process while it is still there
    let fresh = Process::new("restore-io-fresh", &rt);
    fresh.load(&model).unwrap();
    let admitted = tokio::time::timeout(std::time::Duration::from_secs(5), cache.admit(&fresh))
        .await
        .expect("admit must not block behind a restore's store I/O")
        .unwrap();
    assert!(admitted);

    kv.disarm_find();
    restore.await.unwrap().unwrap();
    assert_eq!(cache.count(), 3, "resident + fresh + refilled parked");
    let resident: Vec<String> = cache.procs().iter().map(|p| p.id().to_string()).collect();
    assert!(resident.contains(&"restore-io-parked".to_string()));

    rt.close().await;
}

/// Two concurrent restore passes must not both load the same parked rows. The
/// first pass commits every free slot before its store I/O, so the second sees
/// the set as full and returns immediately instead of blocking on — or
/// duplicating — the first pass's read.
#[tokio::test(flavor = "multi_thread")]
async fn cache_concurrent_restore_passes_do_not_block_or_double_load() {
    let config = Config {
        data: ConfigData {
            cache_cap: Some(2),
            ..Default::default()
        },
        table: Default::default(),
    };
    let kv = Arc::new(GatedKv::new());
    let kv_store: Arc<dyn KvStore> = kv.clone();
    let rt = Runtime::new(&config, Some(kv_store)).unwrap();
    let cache = rt.cache();
    let model = Workflow::new()
        .with_id("m1")
        .with_step(|step| step.with_id("step1"));

    for (tag, ts) in [("a", 1), ("b", 2)] {
        let pid = format!("restore-concurrent-{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();
    }

    kv.arm_find();
    let first = {
        let cache = cache.clone();
        let rt = rt.clone();
        tokio::spawn(async move { cache.start_parked(&rt).await })
    };
    kv.wait_in_find(1).await;

    // the second pass must return without touching the store
    tokio::time::timeout(std::time::Duration::from_secs(5), cache.start_parked(&rt))
        .await
        .expect("a restore pass must not park on another pass's store I/O")
        .unwrap();

    kv.disarm_find();
    first.await.unwrap().unwrap();
    assert_eq!(cache.count(), 2, "each parked row started exactly once");
    let resident: Vec<String> = cache.procs().iter().map(|p| p.id().to_string()).collect();
    assert!(resident.contains(&"restore-concurrent-a".to_string()));
    assert!(resident.contains(&"restore-concurrent-b".to_string()));

    rt.close().await;
}

/// A miss whose `flush` overlaps another caller's load must reuse that load's
/// instance. The single-flight leadership claim happens *after* `flush`, so
/// without a post-flush re-check a caller that missed before the leader cached
/// the process claimed leadership as soon as the leader's in-flight entry was
/// removed — running a SECOND load that returned a second `Arc<Process>` for
/// one pid, i.e. two instances driving the same durable process.
#[tokio::test(flavor = "multi_thread")]
async fn cache_proc_miss_overlapping_load_reuses_instance() {
    let kv = Arc::new(GatedKv::new());
    let kv_store: Arc<dyn KvStore> = kv.clone();
    let rt = Runtime::new(&Config::default(), Some(kv_store)).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();

    let pid = utils::longid();
    let row = data::Proc {
        id: pid.clone(),
        name: "test".to_string(),
        mid: "m1".to_string(),
        state: TaskState::Running.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(),
    };
    cache.store().procs().create(&row).await.unwrap();

    // hold the leader inside its store read, and the writer behind the read
    // gate, so nothing below can make progress on its own
    kv.arm_find();
    kv.arm_write();

    let leader = {
        let cache = cache.clone();
        let rt = rt.clone();
        let pid = pid.clone();
        tokio::spawn(async move { cache.proc(&pid, &rt).await.unwrap().unwrap() })
    };
    kv.wait_in_find(1).await;

    // queue a writer op: it parks in the same read gate, which keeps the
    // writer — and therefore every later `flush` — stuck
    let action = Action::new(&pid, "op-tid", EventAction::Next, Vars::new());
    cache.enqueue_action(&action).await.unwrap();
    kv.wait_in_find(2).await;

    // a second caller misses the (still empty) cache and parks in `flush`
    let waiter = {
        let cache = cache.clone();
        let rt = rt.clone();
        let pid = pid.clone();
        tokio::spawn(async move { cache.proc(&pid, &rt).await.unwrap().unwrap() })
    };
    tokio::time::sleep(std::time::Duration::from_millis(200)).await;
    assert_eq!(cache.count(), 0, "the leader is still parked in its load");

    // release the leader's read: it finishes the load and caches the process,
    // while the writer moves on to its write and parks on the write gate — so
    // the waiter is still inside `flush` when the leader's entry is removed
    kv.disarm_find();
    tokio::time::timeout(std::time::Duration::from_secs(5), kv.wait_in_write(1))
        .await
        .expect("the queued write must park on the write gate");
    let first = leader.await.unwrap();
    assert_eq!(cache.count(), 1, "the leader caches the loaded process");

    kv.disarm_write();
    let second = tokio::time::timeout(std::time::Duration::from_secs(5), waiter)
        .await
        .expect("the second caller must not hang in flush")
        .unwrap();
    assert!(
        Arc::ptr_eq(&first, &second),
        "a caller that missed before the load landed must reuse its instance"
    );
    assert_eq!(cache.count(), 1);

    rt.close().await;
}

/// KV backend that fails every call, as an unreachable/down backend does.
struct FailKv;

#[async_trait::async_trait]
impl KvStore for FailKv {
    async fn one(&self, _key: &str) -> crate::Result<Option<Vec<u8>>> {
        Err(crate::ActError::Store("backend unavailable".to_string()))
    }

    async fn put(&self, _key: &str, _value: Vec<u8>) -> crate::Result<()> {
        Err(crate::ActError::Store("backend unavailable".to_string()))
    }

    async fn delete(&self, _key: &str) -> crate::Result<()> {
        Err(crate::ActError::Store("backend unavailable".to_string()))
    }

    async fn scan_prefix(
        &self,
        _key: &str,
        _options: ScanOptions,
    ) -> crate::Result<Vec<(String, Vec<u8>)>> {
        Err(crate::ActError::Store("backend unavailable".to_string()))
    }
}

/// Only the explicit not-found case is normalized to `Ok(None)`; the
/// must-exist read keeps its not-found error contract.
#[tokio::test]
async fn find_opt_normalizes_only_missing_rows() {
    let store = Store::new(Arc::new(MemoryStore::new()));
    assert!(store.procs().find_opt("absent").await.unwrap().is_none());
    assert!(store.procs().find("absent").await.is_err());
}

/// A backend failure must never be normalized into "no record" or an empty
/// success: process load, delivery updates and the delivery retry scan all
/// surface it, so a down store stays visible (and the retry timer keeps its
/// next tick) instead of masquerading as settled business state.
#[tokio::test]
async fn cache_store_error_is_not_an_empty_success() {
    let kv_store: Arc<dyn KvStore> = Arc::new(FailKv);
    let rt = Runtime::new(&Config::default(), Some(kv_store)).unwrap();
    let store = rt.cache().store();

    // process load: a store error is not `Ok(None)` (a missing pid)
    let err = store.load_proc("any-pid", &rt).await.unwrap_err();
    assert!(matches!(err, crate::ActError::Store(_)), "{err:?}");

    // delivery update: a store error is not a silent success
    let err = store
        .set_delivery("any-delivery", data::DeliveryStatus::Acked)
        .await
        .unwrap_err();
    assert!(matches!(err, crate::ActError::Store(_)), "{err:?}");
    let err = store.mark_delivered("any-delivery").await.unwrap_err();
    assert!(matches!(err, crate::ActError::Store(_)), "{err:?}");

    // retry scan: a store error is not an empty re-arm set
    let err = store
        .with_no_response_deliveries(1_000, 3)
        .await
        .unwrap_err();
    assert!(matches!(err, crate::ActError::Store(_)), "{err:?}");

    rt.close().await;
}