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
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
use std::collections::HashMap;
use std::future::Future;
use std::sync::Arc;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use serde::{Deserialize, Serialize};
use taquba::{
EnqueueOptions, EnqueueResult, JobRecord, PermanentFailure, Queue, Worker, WorkerError,
};
use tokio::sync::Mutex;
use tokio_util::sync::CancellationToken;
use tracing::{debug, instrument, warn};
use crate::error::{Error, Result};
use crate::runner::{Step, StepError, StepErrorKind, StepOutcome, StepRunner};
use crate::terminal::{RunOutcome, TerminalHook, TerminalStatus};
/// Header key carrying the run identifier on every step job.
pub const HEADER_RUN_ID: &str = "workflow.run_id";
/// Header key carrying the zero-based step number on every step job.
pub const HEADER_STEP: &str = "workflow.step";
/// Reserved prefix the runtime owns on step-job headers. Submitter-supplied
/// headers must not start with this prefix; if they do, the runtime treats
/// them as its own and strips them before invoking the runner.
pub const RESERVED_HEADER_PREFIX: &str = "workflow.";
const DEDUP_PREFIX: &str = "run:";
/// Prefix for the durable per-run record in Taquba's user KV namespace.
const RUN_KV_PREFIX: &[u8] = b"workflow/runs/";
fn run_kv_key(run_id: &str) -> Vec<u8> {
let mut k = Vec::with_capacity(RUN_KV_PREFIX.len() + run_id.len());
k.extend_from_slice(RUN_KV_PREFIX);
k.extend_from_slice(run_id.as_bytes());
k
}
/// Durable per-run record written atomically with the step-0 enqueue in
/// [`WorkflowRuntime::submit`] via [`Queue::enqueue_with_kv`]. Carries
/// just enough state to detect duplicate submissions across runtime
/// restarts; the in-memory registry remains the source of truth for
/// active-run status and cancellation while a runtime is up. Cleaned up
/// in [`RuntimeInner::terminate`] when the run reaches a terminal state.
///
/// The cross-restart dedup property only requires the *existence* of
/// this key, so the body is intentionally minimal: `run_id` keeps the
/// record self-describing for ad hoc operator inspection, and
/// `submitted_at_ms` is useful for ordering and stale-record auditing.
#[derive(Debug, Clone, Serialize, Deserialize)]
struct DurableRunRecord {
run_id: String,
submitted_at_ms: u64,
}
/// Per-step enqueue options the runtime forwards through to Taquba. The
/// runtime always owns `headers` (it injects [`HEADER_RUN_ID`] and
/// [`HEADER_STEP`]) and `dedup_key` (it derives one from
/// `(run_id, step_number)`), so callers only pick the three fields below.
#[derive(Debug, Default)]
struct StepEnqueueOpts {
/// Earliest claimable time for the step. `None` means immediate.
run_at: Option<SystemTime>,
/// Per-step priority override.
priority: Option<u32>,
/// Per-step `max_attempts` override.
max_attempts: Option<u32>,
}
/// Spec passed to [`WorkflowRuntime::submit`].
#[derive(Debug, Clone, Default)]
pub struct RunSpec {
/// Caller-supplied run identifier. If `None`, the runtime generates a
/// ULID. The dedup key for the first step job is `run:{run_id}:0`, so
/// re-submitting the same `run_id` while the run is active returns the
/// existing job rather than creating a duplicate.
pub run_id: Option<String>,
/// Bytes handed to the runner as the first step's payload.
pub input: Vec<u8>,
/// Submitter-supplied metadata, threaded through every step of the run
/// and surfaced to the terminal hook. Reserved `workflow.*` keys are
/// rejected at submission with [`Error::ReservedHeaderInSubmit`].
pub headers: HashMap<String, String>,
/// Override the queue's default priority for every step of this run.
pub priority: Option<u32>,
/// Override the queue's `max_attempts` for every step of this run.
pub max_attempts_per_step: Option<u32>,
}
/// Returned by [`WorkflowRuntime::submit`].
#[derive(Debug, Clone)]
pub struct RunHandle {
/// The run's identifier (generated if the spec didn't carry one).
pub run_id: String,
/// Taquba job ID of the first enqueued step.
pub first_job_id: String,
}
/// In-memory status snapshot for an active run. Returned by
/// [`WorkflowRuntime::status`]. Terminal runs are not retained; once the
/// terminal hook fires, the registry entry is removed.
#[derive(Debug, Clone)]
pub struct RunStatus {
/// The run's identifier.
pub run_id: String,
/// Lifecycle state of the run within this runtime process.
pub state: RunState,
/// Step number of the most recently observed step.
pub current_step: u32,
}
/// Lifecycle state tracked in [`RunStatus::state`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum RunState {
/// A step job exists in the queue but has not yet been claimed.
Pending,
/// A step is currently being processed by a worker.
Running,
/// [`WorkflowRuntime::cancel`] was called for this run and the
/// terminal hook has not yet fired. Reported until the in-flight
/// step returns and the runtime settles the run as
/// [`crate::TerminalStatus::Cancelled`] (entry removed and hook
/// fired); after that, [`WorkflowRuntime::status`] returns `None`.
///
/// Only set by external cancellation. A pure runner-issued
/// [`crate::StepOutcome::Cancel`] (with no external `cancel()`
/// call) terminates as `Cancelled` without ever transitioning
/// through `Cancelling`: the registry only learns the runner's
/// verdict when `run_step` returns, at which point the entry is
/// removed.
Cancelling,
}
/// Builder for [`WorkflowRuntime`].
///
/// Construct via [`WorkflowRuntime::builder`], which takes the three required
/// fields (queue, runner, terminal hook) directly so missing-required-field
/// errors are caught at compile time rather than at `build()`.
pub struct WorkflowRuntimeBuilder<R, H> {
queue: Arc<Queue>,
queue_name: String,
runner: R,
terminal_hook: H,
max_concurrent_steps: usize,
poll_interval: Duration,
}
impl<R: StepRunner, H: TerminalHook> WorkflowRuntimeBuilder<R, H> {
/// The Taquba queue name that step jobs are enqueued onto. Defaults to
/// `"workflow-steps"`. Multiple runtimes can share a `Queue` handle by
/// using distinct queue names.
pub fn queue_name(mut self, name: impl Into<String>) -> Self {
self.queue_name = name.into();
self
}
/// Maximum number of steps processed concurrently in [`WorkflowRuntime::run`].
/// Defaults to 16.
pub fn max_concurrent_steps(mut self, n: usize) -> Self {
assert!(n > 0, "max_concurrent_steps must be at least 1");
self.max_concurrent_steps = n;
self
}
/// Maximum time the worker loop waits on an empty queue before re-checking.
/// Defaults to 250ms.
pub fn poll_interval(mut self, interval: Duration) -> Self {
self.poll_interval = interval;
self
}
/// Finalize the builder.
pub fn build(self) -> WorkflowRuntime<R, H> {
let inner = RuntimeInner {
queue: self.queue,
queue_name: self.queue_name,
runner: self.runner,
terminal_hook: self.terminal_hook,
max_concurrent_steps: self.max_concurrent_steps,
poll_interval: self.poll_interval,
registry: Mutex::new(HashMap::new()),
};
WorkflowRuntime {
inner: Arc::new(inner),
}
}
}
/// Durable runtime for workflow runs. Cheap to clone (internally `Arc`).
pub struct WorkflowRuntime<R, H> {
inner: Arc<RuntimeInner<R, H>>,
}
impl<R, H> Clone for WorkflowRuntime<R, H> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}
struct RuntimeInner<R, H> {
queue: Arc<Queue>,
queue_name: String,
runner: R,
terminal_hook: H,
max_concurrent_steps: usize,
poll_interval: Duration,
registry: Mutex<HashMap<String, RegistryEntry>>,
}
/// Per-active-run state retained by the runtime. Combines the publicly
/// observable [`RunStatus`] with the in-process state needed to resolve
/// [`WorkflowRuntime::cancel`] races: the Taquba job currently
/// representing the run (so `cancel` can target it), the submitter's
/// headers (so the terminal hook fires with the right metadata even when
/// `cancel` fires it directly from a pending step), a flag for any
/// pending cancellation request, and a [`CancellationToken`] cloned into
/// the in-flight [`Step`] so runners can short-circuit cooperatively.
struct RegistryEntry {
status: RunStatus,
current_job_id: String,
user_headers: HashMap<String, String>,
cancel_requested: bool,
cancel_token: CancellationToken,
}
impl<R: StepRunner, H: TerminalHook> WorkflowRuntime<R, H> {
/// Start configuring a runtime. Takes the three required dependencies
/// (Taquba queue, [`StepRunner`], [`TerminalHook`]); optional fields are
/// set via [`WorkflowRuntimeBuilder`] methods before [`build`].
///
/// Use [`crate::NoopTerminalHook`] if you don't need terminal callbacks.
///
/// [`build`]: WorkflowRuntimeBuilder::build
pub fn builder(queue: Arc<Queue>, runner: R, terminal_hook: H) -> WorkflowRuntimeBuilder<R, H> {
WorkflowRuntimeBuilder {
queue,
queue_name: "workflow-steps".to_string(),
runner,
terminal_hook,
max_concurrent_steps: 16,
poll_interval: Duration::from_millis(250),
}
}
/// Submit a new run. Enqueues step 0 with payload `spec.input`. Idempotent
/// against in-process duplicates: if a run with the same `run_id` is
/// already active in this runtime, returns [`Error::DuplicateRun`].
/// Cross-restart duplicate-prevention is enforced by a durable
/// per-run record written to Taquba's user KV namespace atomically
/// with the step-0 enqueue.
#[instrument(skip(self, spec), fields(run_id))]
pub async fn submit(&self, spec: RunSpec) -> Result<RunHandle> {
let run_id = spec.run_id.unwrap_or_else(|| ulid::Ulid::new().to_string());
tracing::Span::current().record("run_id", run_id.as_str());
for k in spec.headers.keys() {
if k.starts_with(RESERVED_HEADER_PREFIX) {
return Err(Error::ReservedHeaderInSubmit(k.clone()));
}
}
// Hold the registry lock across enqueue so two concurrent submits
// with the same `run_id` can't both pass the duplicate check before
// either commits. Submission is not on a hot path; queue I/O
// latency here is acceptable.
let mut registry = self.inner.registry.lock().await;
if registry.contains_key(&run_id) {
return Err(Error::DuplicateRun(run_id));
}
// Cross-restart duplicate check. The registry lock above closes
// the in-process race window; this read closes the across-restart
// one (same queue, fresh runtime).
if self
.inner
.queue
.kv_get(&run_kv_key(&run_id))
.await?
.is_some()
{
return Err(Error::DuplicateRun(run_id));
}
let mut headers = spec.headers.clone();
headers.insert(HEADER_RUN_ID.to_string(), run_id.clone());
headers.insert(HEADER_STEP.to_string(), "0".to_string());
let enqueue_opts = EnqueueOptions {
headers,
run_at: None,
priority: spec.priority,
max_attempts: spec.max_attempts_per_step,
dedup_key: Some(format!("{DEDUP_PREFIX}{run_id}:0")),
};
let record_bytes = rmp_serde::to_vec_named(&DurableRunRecord {
run_id: run_id.clone(),
submitted_at_ms: SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_millis() as u64,
})
.map_err(taquba::Error::from)?;
let kv = HashMap::from([(run_kv_key(&run_id), record_bytes)]);
let job_id = match self
.inner
.queue
.enqueue_with_kv(&self.inner.queue_name, spec.input, enqueue_opts, kv)
.await?
{
EnqueueResult::New(id) => id,
// A dedup_key hit without our durable record means either
// another writer beat us, or a prior run on `(run_id, step 0)`
// released its dedup key (job claimed) but the durable record
// is missing, which only happens if the run terminated
// without going through `terminate`. Either way the safe
// verdict is duplicate.
EnqueueResult::AlreadyEnqueued(_) => return Err(Error::DuplicateRun(run_id)),
};
registry.insert(
run_id.clone(),
RegistryEntry {
status: RunStatus {
run_id: run_id.clone(),
state: RunState::Pending,
current_step: 0,
},
current_job_id: job_id.clone(),
user_headers: spec.headers.clone(),
cancel_requested: false,
cancel_token: CancellationToken::new(),
},
);
drop(registry);
debug!(run_id = %run_id, job_id = %job_id, "run submitted");
Ok(RunHandle {
run_id,
first_job_id: job_id,
})
}
/// Look up the in-process status of a run. Returns `None` for unknown or
/// already-terminated runs (the registry only retains active runs).
///
/// Returns [`RunState::Cancelling`] for any run with a pending
/// cancellation request, regardless of its underlying step lifecycle
/// position; the cancellation overlay wins over `Pending`/`Running`
/// until the terminal hook fires.
pub async fn status(&self, run_id: &str) -> Option<RunStatus> {
self.inner.registry.lock().await.get(run_id).map(|e| {
let mut status = e.status.clone();
if e.cancel_requested {
status.state = RunState::Cancelling;
}
status
})
}
/// Request cancellation of an active run.
///
/// Returns `Ok(true)` if a cancellation was initiated for `run_id`, or
/// `Ok(false)` if the run is not active in this runtime (already
/// terminal, never submitted here, or owned by a different runtime
/// instance).
///
/// The terminal hook fires once with [`TerminalStatus::Cancelled`]:
///
/// - **Pending / scheduled step**: the queued step job is cancelled in
/// Taquba and the hook fires from this call before it returns.
/// - **Running step**: cancellation is delivered to the runner via
/// [`Step::cancel_token`]; runners that watch the token short-circuit
/// immediately. Runners that ignore the token are allowed to run to
/// completion (futures cannot be safely aborted mid-step). In both
/// cases the runner's [`StepOutcome`] / [`StepError`] is discarded
/// and the hook fires from the worker once the step returns, with
/// any pending transient retry suppressed and the step acked rather
/// than nacked.
///
/// Cancellation is best-effort: if the run is already terminal by the
/// time `cancel` is called (either because the runner returned a
/// terminating [`StepOutcome`] or a prior `cancel` already settled
/// it), `cancel` returns `Ok(false)`, the run keeps whatever terminal
/// outcome it already delivered, and no additional hook fires.
pub async fn cancel(&self, run_id: &str) -> Result<bool> {
let (job_id, headers, current_step) = {
let mut registry = self.inner.registry.lock().await;
let Some(entry) = registry.get_mut(run_id) else {
return Ok(false);
};
entry.cancel_requested = true;
// Signal cooperative cancellation. Idempotent on
// `CancellationToken`: a second `cancel()` is a no-op. Runners
// that watch `step.cancel_token` can short-circuit; runners
// that ignore it still get terminated by the worker via the
// `cancel_requested` flag after `run_step` returns.
entry.cancel_token.cancel();
(
entry.current_job_id.clone(),
entry.user_headers.clone(),
entry.status.current_step,
)
};
match self.inner.queue.cancel(&job_id).await? {
taquba::CancelOutcome::Removed => {
// Job was Pending/Scheduled and is now removed; no worker
// will ever see it. Fire the hook here. `error` is `None`:
// external cancellation carries no reason at the API level.
self.inner
.terminate(RunOutcome {
run_id: run_id.to_string(),
status: TerminalStatus::Cancelled,
result: None,
error: None,
headers,
final_step: current_step,
})
.await;
}
taquba::CancelOutcome::Requested => {
// Worker is processing the step. The worker reads our own
// registry `cancel_requested` flag after `run_step` returns
// and fires the hook.
}
taquba::CancelOutcome::NotFound => {
// Job already gone from Taquba (e.g. just acked between our
// registry read and the queue call). The worker path still
// honours our `cancel_requested` flag if it hasn't fired the
// hook yet; if it has, this cancel is a no-op past the
// registry update.
}
}
Ok(true)
}
/// Drive the step worker loop until `shutdown` resolves. Spawns up to
/// `max_concurrent_steps` step processors and drains them on shutdown.
pub async fn run<F>(&self, shutdown: F) -> Result<()>
where
F: Future<Output = ()>,
R: 'static,
H: 'static,
{
let worker = Arc::new(StepWorker {
inner: self.inner.clone(),
});
taquba::run_worker_concurrent(
&self.inner.queue,
&self.inner.queue_name,
worker,
self.inner.max_concurrent_steps,
self.inner.poll_interval,
shutdown,
)
.await?;
Ok(())
}
}
struct StepWorker<R, H> {
inner: Arc<RuntimeInner<R, H>>,
}
impl<R: StepRunner + 'static, H: TerminalHook + 'static> Worker for StepWorker<R, H> {
async fn process(&self, job: &JobRecord) -> std::result::Result<(), WorkerError> {
self.inner.process_step(job).await
}
}
impl<R: StepRunner, H: TerminalHook> RuntimeInner<R, H> {
async fn enqueue_step(
&self,
run_id: &str,
step_number: u32,
payload: Vec<u8>,
user_headers: &HashMap<String, String>,
opts: StepEnqueueOpts,
) -> Result<String> {
let mut headers = user_headers.clone();
headers.insert(HEADER_RUN_ID.to_string(), run_id.to_string());
headers.insert(HEADER_STEP.to_string(), step_number.to_string());
let enqueue_opts = EnqueueOptions {
headers,
run_at: opts.run_at,
priority: opts.priority,
max_attempts: opts.max_attempts,
dedup_key: Some(format!("{DEDUP_PREFIX}{run_id}:{step_number}")),
};
Ok(self
.queue
.enqueue_with(&self.queue_name, payload, enqueue_opts)
.await?)
}
fn split_headers(headers: &HashMap<String, String>) -> HashMap<String, String> {
headers
.iter()
.filter(|(k, _)| !k.starts_with(RESERVED_HEADER_PREFIX))
.map(|(k, v)| (k.clone(), v.clone()))
.collect()
}
fn parse_step_headers(job: &JobRecord) -> std::result::Result<(String, u32), Error> {
let run_id = job
.headers
.get(HEADER_RUN_ID)
.ok_or(Error::MissingHeader(HEADER_RUN_ID))?
.to_string();
let step_str = job
.headers
.get(HEADER_STEP)
.ok_or(Error::MissingHeader(HEADER_STEP))?;
let step_number: u32 = step_str.parse().map_err(|_| Error::InvalidStepHeader {
header: HEADER_STEP,
value: step_str.clone(),
})?;
Ok((run_id, step_number))
}
/// Settle a run into its terminal state: drop its registry entry,
/// delete the durable run record from Taquba's KV namespace, and
/// fire the terminal hook. Registry removal happens first so that
/// [`WorkflowRuntime::status`] doesn't briefly report an
/// already-terminated run as active while a slow hook (e.g. a webhook
/// delivery) is in flight. KV cleanup is best-effort: a transient
/// failure here leaves a stale durable record that will block a
/// future submit with the same `run_id`, but does not affect the
/// already-running cleanup of *this* run.
async fn terminate(&self, outcome: RunOutcome) {
self.registry.lock().await.remove(&outcome.run_id);
if let Err(err) = self.queue.kv_delete(&run_kv_key(&outcome.run_id)).await {
warn!(
run_id = %outcome.run_id,
"failed to clear durable run record: {err}"
);
}
self.terminal_hook.on_termination(&outcome).await;
}
/// Transition the entry for `run_id` into [`RunState::Running`] for
/// `step_number`, recording the Taquba job ID powering the step so a
/// concurrent [`WorkflowRuntime::cancel`] can target it. Creates a
/// fresh entry if the run is unknown to this runtime (e.g. after a
/// restart on another runtime, where the worker first learns of the
/// run by claiming its step). Returns the entry's
/// [`CancellationToken`] for cloning into the in-flight [`Step`].
async fn registry_mark_running(
&self,
run_id: &str,
step_number: u32,
job_id: &str,
user_headers: &HashMap<String, String>,
) -> CancellationToken {
let mut registry = self.registry.lock().await;
match registry.get_mut(run_id) {
Some(entry) => {
entry.status.state = RunState::Running;
entry.status.current_step = step_number;
entry.current_job_id = job_id.to_string();
entry.cancel_token.clone()
}
None => {
let cancel_token = CancellationToken::new();
registry.insert(
run_id.to_string(),
RegistryEntry {
status: RunStatus {
run_id: run_id.to_string(),
state: RunState::Running,
current_step: step_number,
},
current_job_id: job_id.to_string(),
user_headers: user_headers.clone(),
cancel_requested: false,
cancel_token: cancel_token.clone(),
},
);
cancel_token
}
}
}
async fn process_step(&self, job: &JobRecord) -> std::result::Result<(), WorkerError> {
let (run_id, step_number) = match Self::parse_step_headers(job) {
Ok(v) => v,
Err(e) => {
warn!(job_id = %job.id, error = %e, "workflow step has malformed headers");
if e.is_permanent() {
return Err(PermanentFailure::new(e.to_string()).into());
}
return Err(e.to_string().into());
}
};
let user_headers = Self::split_headers(&job.headers);
let cancel_token = self
.registry_mark_running(&run_id, step_number, &job.id, &user_headers)
.await;
let step = Step {
run_id: run_id.clone(),
step_number,
payload: job.payload.clone(),
headers: user_headers.clone(),
job_id: job.id.clone(),
attempts: job.attempts,
cancel_token,
};
// Preserve the run's per-step priority and max_attempts across the
// boundary by re-using the values from the just-processed job.
let inherit_opts = || StepEnqueueOpts {
run_at: None,
priority: Some(job.priority),
max_attempts: Some(job.max_attempts),
};
let outcome = self.runner.run_step(&step).await;
let external_cancel = self
.registry
.lock()
.await
.get(&run_id)
.is_some_and(|e| e.cancel_requested);
// Cancellation precedence:
// 1. A runner-issued `StepOutcome::Cancel` wins (it carries an
// in-step reason that we surface on `RunOutcome::error`).
// 2. Otherwise an external `WorkflowRuntime::cancel` overrides
// whatever outcome the runner returned (including transient
// retries and permanent dead-letters), with `error: None` so
// consumers can distinguish external vs. runner-issued cancel.
match outcome {
Ok(StepOutcome::Cancel { reason }) => {
self.terminate(RunOutcome {
run_id: run_id.clone(),
status: TerminalStatus::Cancelled,
result: None,
error: Some(reason),
headers: user_headers,
final_step: step_number,
})
.await;
Ok(())
}
_ if external_cancel => {
self.terminate(RunOutcome {
run_id: run_id.clone(),
status: TerminalStatus::Cancelled,
result: None,
error: None,
headers: user_headers,
final_step: step_number,
})
.await;
Ok(())
}
Ok(StepOutcome::Continue { payload }) => {
self.advance(
&run_id,
step_number + 1,
payload,
&user_headers,
inherit_opts(),
)
.await
}
Ok(StepOutcome::ContinueAfter { payload, delay }) => {
let opts = StepEnqueueOpts {
run_at: Some(SystemTime::now() + delay),
..inherit_opts()
};
self.advance(&run_id, step_number + 1, payload, &user_headers, opts)
.await
}
Ok(StepOutcome::Succeed { result }) => {
self.terminate(RunOutcome {
run_id: run_id.clone(),
status: TerminalStatus::Succeeded,
result: Some(result),
error: None,
headers: user_headers,
final_step: step_number,
})
.await;
Ok(())
}
Ok(StepOutcome::Fail { reason }) => {
// Runner verdict: workflow failed but the step itself ran
// cleanly. Ack the step (no dead-letter) and fire the hook
// with `Failed`.
self.terminate(RunOutcome {
run_id: run_id.clone(),
status: TerminalStatus::Failed,
result: None,
error: Some(reason),
headers: user_headers,
final_step: step_number,
})
.await;
Ok(())
}
Err(StepError {
message,
kind: StepErrorKind::Permanent,
}) => {
self.terminate(RunOutcome {
run_id: run_id.clone(),
status: TerminalStatus::Failed,
result: None,
error: Some(message.clone()),
headers: user_headers,
final_step: step_number,
})
.await;
Err(PermanentFailure::new(message).into())
}
Err(StepError {
message,
kind: StepErrorKind::Transient,
}) => {
// Last attempt: this nack will dead-letter. Fire the failure
// hook now so the user is notified once, before the job
// record disappears from the registry.
if job.attempts >= job.max_attempts {
self.terminate(RunOutcome {
run_id: run_id.clone(),
status: TerminalStatus::Failed,
result: None,
error: Some(message.clone()),
headers: user_headers,
final_step: step_number,
})
.await;
}
Err(message.into())
}
}
}
async fn advance(
&self,
run_id: &str,
next_step: u32,
payload: Vec<u8>,
user_headers: &HashMap<String, String>,
opts: StepEnqueueOpts,
) -> std::result::Result<(), WorkerError> {
match self
.enqueue_step(run_id, next_step, payload, user_headers, opts)
.await
{
Ok(new_job_id) => {
// Make sure to preserve `cancel_requested`.
if let Some(entry) = self.registry.lock().await.get_mut(run_id) {
entry.status.state = RunState::Pending;
entry.status.current_step = next_step;
entry.current_job_id = new_job_id;
}
Ok(())
}
// Transient: the runner already executed for this step; failing
// the worker triggers a retry of the same step. The runner must be
// idempotent for `(run_id, step_number)`.
Err(e) => Err(e.to_string().into()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::terminal::NoopTerminalHook;
use std::sync::Mutex as StdMutex;
use std::sync::atomic::{AtomicU32, Ordering};
use taquba::object_store::memory::InMemory;
use taquba::{OpenOptions, QueueConfig};
use tokio::sync::oneshot;
/// Recording terminal hook backed by an mpsc channel.
struct ChannelHook {
tx: tokio::sync::mpsc::UnboundedSender<RunOutcome>,
}
impl TerminalHook for ChannelHook {
async fn on_termination(&self, outcome: &RunOutcome) {
let _ = self.tx.send(outcome.clone());
}
}
/// Runner that executes a fixed list of step outcomes in order.
struct ScriptedRunner {
script: Arc<StdMutex<Vec<StepOutcome>>>,
}
impl ScriptedRunner {
fn new(steps: Vec<StepOutcome>) -> Self {
Self {
script: Arc::new(StdMutex::new(steps)),
}
}
}
impl StepRunner for ScriptedRunner {
async fn run_step(&self, _step: &Step) -> std::result::Result<StepOutcome, StepError> {
let next = self.script.lock().unwrap().remove(0);
Ok(next)
}
}
async fn fresh_queue() -> Arc<Queue> {
Arc::new(
Queue::open(Arc::new(InMemory::new()), "test")
.await
.unwrap(),
)
}
/// Queue with zero retry backoff and a tight reaper, so multi-attempt
/// tests run in well under a second.
async fn fresh_queue_fast_retry() -> Arc<Queue> {
let opts = OpenOptions {
default_queue_config: QueueConfig {
retry_backoff_base: Duration::ZERO,
..QueueConfig::default()
},
reaper_interval: Duration::from_millis(50),
scheduler_interval: Duration::from_millis(50),
..OpenOptions::default()
};
Arc::new(
Queue::open_with_options(Arc::new(InMemory::new()), "test", opts)
.await
.unwrap(),
)
}
fn spawn_runtime<R, H>(runtime: WorkflowRuntime<R, H>) -> oneshot::Sender<()>
where
R: StepRunner + 'static,
H: TerminalHook + 'static,
{
let (tx, rx) = oneshot::channel::<()>();
tokio::spawn(async move {
let _ = runtime
.run(async move {
let _ = rx.await;
})
.await;
});
tx
}
#[tokio::test]
async fn single_step_succeeds_and_fires_hook() {
let queue = fresh_queue().await;
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let runtime = WorkflowRuntime::builder(
queue,
ScriptedRunner::new(vec![StepOutcome::Succeed {
result: b"done".to_vec(),
}]),
ChannelHook { tx },
)
.build();
let shutdown = spawn_runtime(runtime.clone());
let handle = runtime
.submit(RunSpec {
input: b"in".to_vec(),
..Default::default()
})
.await
.unwrap();
let outcome = tokio::time::timeout(Duration::from_secs(2), rx.recv())
.await
.unwrap()
.unwrap();
assert_eq!(outcome.run_id, handle.run_id);
assert_eq!(outcome.status, TerminalStatus::Succeeded);
assert_eq!(outcome.result.as_deref(), Some(b"done".as_slice()));
assert_eq!(outcome.final_step, 0);
assert!(runtime.status(&handle.run_id).await.is_none());
let _ = shutdown.send(());
}
#[tokio::test]
async fn multi_step_run_advances_through_continue() {
let queue = fresh_queue().await;
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let runtime = WorkflowRuntime::builder(
queue,
ScriptedRunner::new(vec![
StepOutcome::Continue {
payload: b"step1".to_vec(),
},
StepOutcome::Continue {
payload: b"step2".to_vec(),
},
StepOutcome::Succeed {
result: b"final".to_vec(),
},
]),
ChannelHook { tx },
)
.build();
let shutdown = spawn_runtime(runtime.clone());
let handle = runtime
.submit(RunSpec {
input: b"start".to_vec(),
..Default::default()
})
.await
.unwrap();
let outcome = tokio::time::timeout(Duration::from_secs(2), rx.recv())
.await
.unwrap()
.unwrap();
assert_eq!(outcome.run_id, handle.run_id);
assert_eq!(outcome.final_step, 2);
assert_eq!(outcome.status, TerminalStatus::Succeeded);
assert_eq!(outcome.result.as_deref(), Some(b"final".as_slice()));
let _ = shutdown.send(());
}
#[tokio::test]
async fn permanent_failure_dead_letters_and_fires_hook() {
struct FailingRunner;
impl StepRunner for FailingRunner {
async fn run_step(&self, _step: &Step) -> std::result::Result<StepOutcome, StepError> {
Err(StepError::permanent("nope"))
}
}
let queue = fresh_queue().await;
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let runtime =
WorkflowRuntime::builder(queue.clone(), FailingRunner, ChannelHook { tx }).build();
let shutdown = spawn_runtime(runtime.clone());
let handle = runtime
.submit(RunSpec {
input: b"x".to_vec(),
..Default::default()
})
.await
.unwrap();
let outcome = tokio::time::timeout(Duration::from_secs(2), rx.recv())
.await
.unwrap()
.unwrap();
assert_eq!(outcome.run_id, handle.run_id);
assert_eq!(outcome.status, TerminalStatus::Failed);
assert_eq!(outcome.error.as_deref(), Some("nope"));
assert!(runtime.status(&handle.run_id).await.is_none());
// Permanent runner errors *do* dead-letter the step.
let stats = queue.stats("workflow-steps").await.unwrap();
assert_eq!(stats.dead, 1, "permanent error should dead-letter");
let _ = shutdown.send(());
}
#[tokio::test]
async fn fail_outcome_terminates_run_without_dead_letter() {
// StepOutcome::Fail is the runner's *verdict* path, not an
// infrastructure error: the hook fires with Failed, the registry
// entry is cleaned up, but the step is acked normally so no dead
// job is left behind for operators to inspect.
struct VerdictRunner;
impl StepRunner for VerdictRunner {
async fn run_step(&self, _step: &Step) -> std::result::Result<StepOutcome, StepError> {
Ok(StepOutcome::Fail {
reason: "agent declined the task".to_string(),
})
}
}
let queue = fresh_queue().await;
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let runtime =
WorkflowRuntime::builder(queue.clone(), VerdictRunner, ChannelHook { tx }).build();
let shutdown = spawn_runtime(runtime.clone());
let handle = runtime
.submit(RunSpec {
input: b"x".to_vec(),
..Default::default()
})
.await
.unwrap();
let outcome = tokio::time::timeout(Duration::from_secs(2), rx.recv())
.await
.expect("hook fired in time")
.expect("hook channel open");
assert_eq!(outcome.run_id, handle.run_id);
assert_eq!(outcome.status, TerminalStatus::Failed);
assert_eq!(outcome.error.as_deref(), Some("agent declined the task"));
assert!(runtime.status(&handle.run_id).await.is_none());
// Crucially: no dead-letter, distinguishing runner verdict from
// infrastructure failure at the queue level.
let stats = queue.stats("workflow-steps").await.unwrap();
assert_eq!(stats.dead, 0, "Fail verdict must not dead-letter");
let _ = shutdown.send(());
}
#[tokio::test]
async fn duplicate_submit_in_process_is_rejected() {
// Pause forever on the first step so the run stays active in the
// registry while we attempt the duplicate submit.
struct PauseRunner;
impl StepRunner for PauseRunner {
async fn run_step(&self, _step: &Step) -> std::result::Result<StepOutcome, StepError> {
std::future::pending().await
}
}
let queue = fresh_queue().await;
let runtime = WorkflowRuntime::builder(queue, PauseRunner, NoopTerminalHook).build();
let shutdown = spawn_runtime(runtime.clone());
let handle = runtime
.submit(RunSpec {
run_id: Some("fixed-id".to_string()),
input: b"x".to_vec(),
..Default::default()
})
.await
.unwrap();
// Wait for the worker to start the step so the registry observes the
// run as Running (or at least Pending).
for _ in 0..40 {
if runtime.status(&handle.run_id).await.is_some() {
break;
}
tokio::time::sleep(Duration::from_millis(25)).await;
}
assert!(runtime.status(&handle.run_id).await.is_some());
let err = runtime
.submit(RunSpec {
run_id: Some("fixed-id".to_string()),
input: b"y".to_vec(),
..Default::default()
})
.await
.unwrap_err();
assert!(matches!(err, Error::DuplicateRun(id) if id == "fixed-id"));
let _ = shutdown.send(());
}
#[tokio::test]
async fn duplicate_submit_across_runtime_restart_is_rejected() {
// Build a runtime, submit a run, then drop the runtime entirely
// (simulating a process restart of the workflow layer) while
// keeping the underlying Queue alive. The next runtime instance
// sees a fresh in-memory registry but must still reject a
// duplicate `run_id` because the durable run record persists
// through the enqueue_with_kv path.
struct PauseRunner;
impl StepRunner for PauseRunner {
async fn run_step(&self, _step: &Step) -> std::result::Result<StepOutcome, StepError> {
std::future::pending().await
}
}
let queue = fresh_queue().await;
// Submit via the first runtime, drop it without starting its
// worker loop or going terminal.
{
let runtime =
WorkflowRuntime::builder(queue.clone(), PauseRunner, NoopTerminalHook).build();
runtime
.submit(RunSpec {
run_id: Some("durable-id".to_string()),
input: b"x".to_vec(),
..Default::default()
})
.await
.unwrap();
}
// The durable record is queryable independently of any runtime.
assert!(
queue
.kv_get(&run_kv_key("durable-id"))
.await
.unwrap()
.is_some(),
"durable run record must persist past runtime drop"
);
// Fresh runtime, same queue. The registry is empty here, so the
// duplicate verdict can only come from the durable KV record.
let runtime2 =
WorkflowRuntime::builder(queue.clone(), PauseRunner, NoopTerminalHook).build();
let err = runtime2
.submit(RunSpec {
run_id: Some("durable-id".to_string()),
input: b"y".to_vec(),
..Default::default()
})
.await
.unwrap_err();
assert!(matches!(err, Error::DuplicateRun(id) if id == "durable-id"));
}
#[tokio::test]
async fn reserved_header_on_submit_is_rejected() {
let queue = fresh_queue().await;
let runtime: WorkflowRuntime<ScriptedRunner, NoopTerminalHook> =
WorkflowRuntime::builder(queue, ScriptedRunner::new(vec![]), NoopTerminalHook).build();
let mut headers = HashMap::new();
headers.insert("workflow.run_id".to_string(), "evil".to_string());
let err = runtime
.submit(RunSpec {
input: b"x".to_vec(),
headers,
..Default::default()
})
.await
.unwrap_err();
assert!(
matches!(&err, Error::ReservedHeaderInSubmit(k) if k == "workflow.run_id"),
"got: {err:?}"
);
}
#[tokio::test]
async fn user_headers_thread_through_to_terminal_hook() {
let queue = fresh_queue().await;
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let runtime = WorkflowRuntime::builder(
queue,
ScriptedRunner::new(vec![
StepOutcome::Continue { payload: vec![] },
StepOutcome::Succeed { result: vec![] },
]),
ChannelHook { tx },
)
.build();
let shutdown = spawn_runtime(runtime.clone());
let mut headers = HashMap::new();
headers.insert("trace_id".to_string(), "abc-123".to_string());
headers.insert("tenant".to_string(), "acme".to_string());
runtime
.submit(RunSpec {
input: b"x".to_vec(),
headers,
..Default::default()
})
.await
.unwrap();
let outcome = tokio::time::timeout(Duration::from_secs(2), rx.recv())
.await
.unwrap()
.unwrap();
assert_eq!(outcome.headers.get("trace_id").unwrap(), "abc-123");
assert_eq!(outcome.headers.get("tenant").unwrap(), "acme");
// Reserved keys must not leak through.
assert!(!outcome.headers.contains_key(HEADER_RUN_ID));
assert!(!outcome.headers.contains_key(HEADER_STEP));
let _ = shutdown.send(());
}
#[tokio::test]
async fn restart_resumes_at_next_step() {
// Headline durability test: after step 0 has acked and step 1 is in
// the queue, kill runtime A entirely and spawn runtime B on the same
// Queue handle. B should claim and complete step 1 without re-running
// step 0.
//
// To make this race-free we gate step 0's runner: the test holds the
// gate while signalling shutdown to A so A enters drain mode without
// ever claiming step 1. Then the gate is opened, A's spawned step-0
// task finishes (enqueueing step 1 + acking step 0) and A exits.
struct GatedRunner {
gate: tokio::sync::Mutex<Option<oneshot::Receiver<Vec<u8>>>>,
}
impl StepRunner for GatedRunner {
async fn run_step(&self, step: &Step) -> std::result::Result<StepOutcome, StepError> {
match step.step_number {
0 => {
let rx = self.gate.lock().await.take().expect("gate consumed twice");
let payload = rx.await.expect("gate sender dropped");
Ok(StepOutcome::Continue { payload })
}
_ => std::future::pending().await,
}
}
}
struct CompleteOnStep1;
impl StepRunner for CompleteOnStep1 {
async fn run_step(&self, step: &Step) -> std::result::Result<StepOutcome, StepError> {
assert_eq!(step.step_number, 1, "runtime B should only ever see step 1");
assert_eq!(step.payload.as_slice(), b"step1-payload");
Ok(StepOutcome::Succeed {
result: b"resumed".to_vec(),
})
}
}
let queue = fresh_queue().await;
let (gate_tx, gate_rx) = oneshot::channel::<Vec<u8>>();
let runtime_a = WorkflowRuntime::builder(
queue.clone(),
GatedRunner {
gate: tokio::sync::Mutex::new(Some(gate_rx)),
},
NoopTerminalHook,
)
.max_concurrent_steps(1)
.build();
let (shutdown_a_tx, shutdown_a_rx) = oneshot::channel::<()>();
let worker_a = {
let runtime_a = runtime_a.clone();
tokio::spawn(async move {
let _ = runtime_a
.run(async move {
let _ = shutdown_a_rx.await;
})
.await;
})
};
let handle = runtime_a
.submit(RunSpec {
input: b"input".to_vec(),
..Default::default()
})
.await
.unwrap();
// Wait for runtime A to claim step 0 and reach the gate (registry
// shows Running for step 0).
for _ in 0..80 {
if let Some(s) = runtime_a.status(&handle.run_id).await {
if s.state == RunState::Running && s.current_step == 0 {
break;
}
}
tokio::time::sleep(Duration::from_millis(25)).await;
}
let s = runtime_a.status(&handle.run_id).await.expect("status");
assert_eq!(s.state, RunState::Running);
assert_eq!(s.current_step, 0);
// A's worker is in the at-capacity select-loop. Signal shutdown
// first, then open the gate so step 0 finishes processing inside
// drain mode (A will not claim step 1).
let _ = shutdown_a_tx.send(());
let _ = gate_tx.send(b"step1-payload".to_vec());
worker_a.await.expect("runtime A drained cleanly");
// Bring up runtime B on the same Queue handle. It should pick up
// step 1 from where A left off.
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let runtime_b =
WorkflowRuntime::builder(queue, CompleteOnStep1, ChannelHook { tx }).build();
let shutdown_b = spawn_runtime(runtime_b.clone());
let outcome = tokio::time::timeout(Duration::from_secs(2), rx.recv())
.await
.expect("hook fired in time")
.expect("hook channel open");
assert_eq!(outcome.run_id, handle.run_id);
assert_eq!(outcome.status, TerminalStatus::Succeeded);
assert_eq!(outcome.result.as_deref(), Some(b"resumed".as_slice()));
assert_eq!(outcome.final_step, 1);
let _ = shutdown_b.send(());
}
/// Submits a run whose runner always returns
/// [`StepError::transient`], capped at `max_attempts`. Asserts the
/// runner is invoked exactly `max_attempts` times (per-step max-attempts
/// propagation) and that the terminal hook fires Failed exactly once on
/// the final attempt (fire-once-on-last-attempt logic).
async fn assert_transient_retries_until_max(max_attempts: u32) {
struct AlwaysTransient {
calls: Arc<AtomicU32>,
}
impl StepRunner for AlwaysTransient {
async fn run_step(&self, _step: &Step) -> std::result::Result<StepOutcome, StepError> {
self.calls.fetch_add(1, Ordering::SeqCst);
Err(StepError::transient("flaky"))
}
}
let queue = fresh_queue_fast_retry().await;
let calls = Arc::new(AtomicU32::new(0));
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let runtime = WorkflowRuntime::builder(
queue,
AlwaysTransient {
calls: calls.clone(),
},
ChannelHook { tx },
)
.build();
let shutdown = spawn_runtime(runtime.clone());
runtime
.submit(RunSpec {
input: b"x".to_vec(),
max_attempts_per_step: Some(max_attempts),
..Default::default()
})
.await
.unwrap();
let outcome = tokio::time::timeout(Duration::from_secs(3), rx.recv())
.await
.expect("hook fired in time")
.expect("hook channel open");
assert_eq!(outcome.status, TerminalStatus::Failed);
assert_eq!(outcome.error.as_deref(), Some("flaky"));
assert_eq!(
calls.load(Ordering::SeqCst),
max_attempts,
"runner called once per attempt up to max_attempts"
);
// Settle window: assert no duplicate hook fires after the terminal one.
tokio::time::sleep(Duration::from_millis(50)).await;
assert!(rx.try_recv().is_err(), "hook fired more than once");
let _ = shutdown.send(());
}
#[tokio::test]
async fn cancel_outcome_terminates_run_without_dead_letter() {
// `StepOutcome::Cancel` is the runner's cancellation verdict path:
// the hook fires with Cancelled, the registry is cleaned up, the
// step is acked, and no dead job is left behind.
struct CancellingRunner;
impl StepRunner for CancellingRunner {
async fn run_step(&self, _step: &Step) -> std::result::Result<StepOutcome, StepError> {
Ok(StepOutcome::Cancel {
reason: "upstream aborted".to_string(),
})
}
}
let queue = fresh_queue().await;
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let runtime =
WorkflowRuntime::builder(queue.clone(), CancellingRunner, ChannelHook { tx }).build();
let shutdown = spawn_runtime(runtime.clone());
let handle = runtime
.submit(RunSpec {
input: b"x".to_vec(),
..Default::default()
})
.await
.unwrap();
let outcome = tokio::time::timeout(Duration::from_secs(2), rx.recv())
.await
.expect("hook fired in time")
.expect("hook channel open");
assert_eq!(outcome.run_id, handle.run_id);
assert_eq!(outcome.status, TerminalStatus::Cancelled);
assert_eq!(outcome.error.as_deref(), Some("upstream aborted"));
assert!(runtime.status(&handle.run_id).await.is_none());
let stats = queue.stats("workflow-steps").await.unwrap();
assert_eq!(stats.dead, 0, "Cancel verdict must not dead-letter");
let _ = shutdown.send(());
}
#[tokio::test]
async fn cancel_pending_run_fires_cancelled_hook() {
// Pending case: a run sits in the queue, we call `cancel()` before
// any worker claims it. The hook fires from `cancel` itself.
struct UnreachableRunner;
impl StepRunner for UnreachableRunner {
async fn run_step(&self, _step: &Step) -> std::result::Result<StepOutcome, StepError> {
unreachable!("worker must not claim the cancelled step");
}
}
let queue = fresh_queue().await;
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let runtime =
WorkflowRuntime::builder(queue.clone(), UnreachableRunner, ChannelHook { tx }).build();
// Note: deliberately do NOT spawn the worker loop, so the submitted
// step stays Pending in the queue while we cancel it.
let mut headers = HashMap::new();
headers.insert("tenant".to_string(), "acme".to_string());
let handle = runtime
.submit(RunSpec {
input: b"x".to_vec(),
headers,
..Default::default()
})
.await
.unwrap();
let status = runtime.status(&handle.run_id).await.expect("active");
assert_eq!(status.state, RunState::Pending);
let was_cancelled = runtime.cancel(&handle.run_id).await.unwrap();
assert!(was_cancelled);
let outcome = tokio::time::timeout(Duration::from_secs(1), rx.recv())
.await
.expect("hook fired in time")
.expect("hook channel open");
assert_eq!(outcome.run_id, handle.run_id);
assert_eq!(outcome.status, TerminalStatus::Cancelled);
// External cancellation carries no reason: `error` is `None`.
assert!(outcome.error.is_none());
assert_eq!(outcome.headers.get("tenant").unwrap(), "acme");
assert!(runtime.status(&handle.run_id).await.is_none());
let stats = queue.stats("workflow-steps").await.unwrap();
assert_eq!(stats.dead, 0, "cancel must not dead-letter");
assert_eq!(stats.pending, 0, "cancelled job must be removed");
}
#[tokio::test]
async fn cancel_during_running_step_overrides_outcome() {
// Running case: the step is in-flight when cancel is called. The
// runner's eventual outcome is discarded; the worker fires Cancelled.
struct GatedRunner {
claimed: Arc<tokio::sync::Notify>,
gate: tokio::sync::Mutex<Option<oneshot::Receiver<()>>>,
}
impl StepRunner for GatedRunner {
async fn run_step(&self, _step: &Step) -> std::result::Result<StepOutcome, StepError> {
self.claimed.notify_one();
let rx = self.gate.lock().await.take().expect("gate consumed twice");
let _ = rx.await;
// The runner "successfully completes" the step, but cancel
// was requested mid-flight so the outcome should be ignored
// and the hook should fire Cancelled instead.
Ok(StepOutcome::Succeed {
result: b"would-have-succeeded".to_vec(),
})
}
}
let queue = fresh_queue().await;
let claimed = Arc::new(tokio::sync::Notify::new());
let (gate_tx, gate_rx) = oneshot::channel::<()>();
let (hook_tx, mut hook_rx) = tokio::sync::mpsc::unbounded_channel();
let runtime = WorkflowRuntime::builder(
queue.clone(),
GatedRunner {
claimed: claimed.clone(),
gate: tokio::sync::Mutex::new(Some(gate_rx)),
},
ChannelHook { tx: hook_tx },
)
.build();
let shutdown = spawn_runtime(runtime.clone());
let handle = runtime
.submit(RunSpec {
input: b"x".to_vec(),
..Default::default()
})
.await
.unwrap();
tokio::time::timeout(Duration::from_secs(2), claimed.notified())
.await
.expect("runner reached gate");
let was_cancelled = runtime.cancel(&handle.run_id).await.unwrap();
assert!(was_cancelled);
// Let the runner finish. The worker should observe `cancel_requested`
// and fire Cancelled rather than advancing or firing Succeeded.
let _ = gate_tx.send(());
let outcome = tokio::time::timeout(Duration::from_secs(2), hook_rx.recv())
.await
.expect("hook fired")
.expect("hook channel open");
assert_eq!(outcome.status, TerminalStatus::Cancelled);
assert!(
outcome.result.is_none(),
"succeed payload must be discarded"
);
assert!(runtime.status(&handle.run_id).await.is_none());
let stats = queue.stats("workflow-steps").await.unwrap();
assert_eq!(stats.dead, 0);
let _ = shutdown.send(());
}
/// Drive a single step that blocks on a gate, calls `cancel(run_id)`
/// while the step is in-flight, and then has the runner return the
/// supplied error. Asserts that external cancellation suppresses the
/// error path entirely: the hook fires `Cancelled` (not `Failed`),
/// no dead-letter is produced regardless of `permanent`/`transient`,
/// and the worker returns `Ok` (no retry, no PermanentFailure
/// propagation).
async fn assert_cancel_suppresses_runner_error(error: StepError) {
struct GatedErrRunner {
claimed: Arc<tokio::sync::Notify>,
gate: tokio::sync::Mutex<Option<oneshot::Receiver<()>>>,
calls: Arc<AtomicU32>,
error: StdMutex<Option<StepError>>,
}
impl StepRunner for GatedErrRunner {
async fn run_step(&self, _step: &Step) -> std::result::Result<StepOutcome, StepError> {
self.calls.fetch_add(1, Ordering::SeqCst);
self.claimed.notify_one();
let rx = self.gate.lock().await.take().expect("gate consumed twice");
let _ = rx.await;
Err(self
.error
.lock()
.unwrap()
.take()
.expect("error consumed twice"))
}
}
let queue = fresh_queue_fast_retry().await;
let claimed = Arc::new(tokio::sync::Notify::new());
let calls = Arc::new(AtomicU32::new(0));
let (gate_tx, gate_rx) = oneshot::channel::<()>();
let (hook_tx, mut hook_rx) = tokio::sync::mpsc::unbounded_channel();
let runtime = WorkflowRuntime::builder(
queue.clone(),
GatedErrRunner {
claimed: claimed.clone(),
gate: tokio::sync::Mutex::new(Some(gate_rx)),
calls: calls.clone(),
error: StdMutex::new(Some(error)),
},
ChannelHook { tx: hook_tx },
)
.build();
let shutdown = spawn_runtime(runtime.clone());
let handle = runtime
.submit(RunSpec {
input: b"x".to_vec(),
..Default::default()
})
.await
.unwrap();
tokio::time::timeout(Duration::from_secs(2), claimed.notified())
.await
.expect("runner reached gate");
let was_cancelled = runtime.cancel(&handle.run_id).await.unwrap();
assert!(was_cancelled);
// Release the runner. It returns Err; without cancellation this
// would either dead-letter (permanent) or nack for retry
// (transient). Cancellation must suppress both.
let _ = gate_tx.send(());
let outcome = tokio::time::timeout(Duration::from_secs(2), hook_rx.recv())
.await
.expect("hook fired")
.expect("hook channel open");
assert_eq!(outcome.status, TerminalStatus::Cancelled);
assert!(
outcome.error.is_none(),
"external cancel must carry no reason (Some(_) would imply runner-issued StepOutcome::Cancel)",
);
assert!(runtime.status(&handle.run_id).await.is_none());
// Settle window: assert no retry attempt and no dead-letter or
// duplicate hook fires after the terminal one.
tokio::time::sleep(Duration::from_millis(100)).await;
assert_eq!(
calls.load(Ordering::SeqCst),
1,
"cancellation must suppress retries",
);
let stats = queue.stats("workflow-steps").await.unwrap();
assert_eq!(stats.dead, 0, "cancellation must suppress dead-letter");
assert!(
hook_rx.try_recv().is_err(),
"hook must fire exactly once for the cancelled run",
);
let _ = shutdown.send(());
}
#[tokio::test]
async fn cancel_suppresses_permanent_runner_error() {
// Without cancellation, `StepError::permanent` dead-letters the
// step and causes the worker to return `PermanentFailure`. With
// an external cancel in flight, the worker must ack and fire
// `Cancelled` instead.
assert_cancel_suppresses_runner_error(StepError::permanent("would-dead-letter")).await;
}
#[tokio::test]
async fn cancel_suppresses_transient_runner_error() {
// Without cancellation, `StepError::transient` nacks for retry
// (and eventually dead-letters). With an external cancel in
// flight, the worker must ack and fire `Cancelled` without
// re-invoking the runner.
assert_cancel_suppresses_runner_error(StepError::transient("would-retry")).await;
}
#[tokio::test]
async fn cancel_signals_step_token_for_cooperative_short_circuit() {
// A runner that watches `step.cancel_token` should short-circuit
// long after-claim work as soon as `WorkflowRuntime::cancel` is
// called. Without the token, cancellation latency is bounded by
// step duration; with it, the runner returns essentially
// immediately. The test pins this by using a step that would
// otherwise sleep for 30 seconds; if the token didn't fire, the
// test would time out.
struct CooperativeRunner {
claimed: Arc<tokio::sync::Notify>,
}
impl StepRunner for CooperativeRunner {
async fn run_step(&self, step: &Step) -> std::result::Result<StepOutcome, StepError> {
self.claimed.notify_one();
tokio::select! {
_ = tokio::time::sleep(Duration::from_secs(30)) => {
Ok(StepOutcome::Succeed { result: b"slow".to_vec() })
}
_ = step.cancel_token.cancelled() => {
Ok(StepOutcome::Cancel { reason: "cooperative".to_string() })
}
}
}
}
let queue = fresh_queue().await;
let claimed = Arc::new(tokio::sync::Notify::new());
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let runtime = WorkflowRuntime::builder(
queue.clone(),
CooperativeRunner {
claimed: claimed.clone(),
},
ChannelHook { tx },
)
.build();
let shutdown = spawn_runtime(runtime.clone());
let handle = runtime
.submit(RunSpec {
input: b"x".to_vec(),
..Default::default()
})
.await
.unwrap();
tokio::time::timeout(Duration::from_secs(2), claimed.notified())
.await
.expect("runner observed token");
let start = std::time::Instant::now();
let was_cancelled = runtime.cancel(&handle.run_id).await.unwrap();
assert!(was_cancelled);
let outcome = tokio::time::timeout(Duration::from_secs(2), rx.recv())
.await
.expect("hook fired well before the 30s sleep would have")
.expect("hook channel open");
let elapsed = start.elapsed();
assert_eq!(outcome.status, TerminalStatus::Cancelled);
// Runner-issued Cancel wins precedence over external cancel, so
// the runner's reason surfaces.
assert_eq!(outcome.error.as_deref(), Some("cooperative"));
assert!(
elapsed < Duration::from_secs(2),
"cooperative cancel must short-circuit the 30s sleep (took {elapsed:?})",
);
assert!(runtime.status(&handle.run_id).await.is_none());
let stats = queue.stats("workflow-steps").await.unwrap();
assert_eq!(stats.dead, 0);
let _ = shutdown.send(());
}
#[tokio::test]
async fn double_cancel_fires_hook_once_and_second_call_returns_false() {
// Submit a run and cancel twice while it sits pending. The first
// call removes the queued step, fires the hook, and drops the
// registry entry. The second call must see no entry and report
// `Ok(false)`; crucially, the hook must NOT fire a second
// time.
struct UnreachableRunner;
impl StepRunner for UnreachableRunner {
async fn run_step(&self, _step: &Step) -> std::result::Result<StepOutcome, StepError> {
unreachable!("worker must not claim the cancelled step");
}
}
let queue = fresh_queue().await;
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let runtime =
WorkflowRuntime::builder(queue, UnreachableRunner, ChannelHook { tx }).build();
// Deliberately do not spawn the worker loop, so step 0 stays
// Pending while both cancels race.
let handle = runtime
.submit(RunSpec {
input: b"x".to_vec(),
..Default::default()
})
.await
.unwrap();
let first = runtime.cancel(&handle.run_id).await.unwrap();
assert!(first, "first cancel initiates termination");
let second = runtime.cancel(&handle.run_id).await.unwrap();
assert!(
!second,
"second cancel must report Ok(false): registry entry is gone after the first fired the hook",
);
// Hook fires exactly once.
let _ = tokio::time::timeout(Duration::from_secs(1), rx.recv())
.await
.expect("hook fired in time")
.expect("hook channel open");
tokio::time::sleep(Duration::from_millis(50)).await;
assert!(
rx.try_recv().is_err(),
"hook must fire exactly once for a double-cancelled run",
);
}
#[tokio::test]
async fn cancel_after_run_already_terminated_returns_false() {
// Submit a run that succeeds normally, wait for the terminal
// hook, then call `cancel`. The registry entry was removed when
// the success hook fired, so `cancel` must report `Ok(false)`
// and must not fire a second hook.
let queue = fresh_queue().await;
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let runtime = WorkflowRuntime::builder(
queue,
ScriptedRunner::new(vec![StepOutcome::Succeed {
result: b"done".to_vec(),
}]),
ChannelHook { tx },
)
.build();
let shutdown = spawn_runtime(runtime.clone());
let handle = runtime
.submit(RunSpec {
input: b"x".to_vec(),
..Default::default()
})
.await
.unwrap();
let outcome = tokio::time::timeout(Duration::from_secs(2), rx.recv())
.await
.expect("Succeeded hook fired")
.expect("hook channel open");
assert_eq!(outcome.status, TerminalStatus::Succeeded);
assert!(runtime.status(&handle.run_id).await.is_none());
let was_cancelled = runtime.cancel(&handle.run_id).await.unwrap();
assert!(
!was_cancelled,
"cancel on an already-terminated run must report Ok(false)",
);
tokio::time::sleep(Duration::from_millis(50)).await;
assert!(
rx.try_recv().is_err(),
"no Cancelled hook may fire after the run already terminated as Succeeded",
);
let _ = shutdown.send(());
}
#[tokio::test]
async fn status_reports_cancelling_while_termination_in_flight() {
// Once `cancel()` has been called but the terminal hook hasn't
// fired yet, `status()` should report `RunState::Cancelling` so
// external observers can see termination is in progress. A gated
// runner holds the cancellation window open long enough to
// observe it deterministically.
struct GatedRunner {
claimed: Arc<tokio::sync::Notify>,
gate: tokio::sync::Mutex<Option<oneshot::Receiver<()>>>,
}
impl StepRunner for GatedRunner {
async fn run_step(&self, _step: &Step) -> std::result::Result<StepOutcome, StepError> {
self.claimed.notify_one();
let rx = self.gate.lock().await.take().expect("gate consumed twice");
let _ = rx.await;
Ok(StepOutcome::Succeed {
result: b"would-have-succeeded".to_vec(),
})
}
}
let queue = fresh_queue().await;
let claimed = Arc::new(tokio::sync::Notify::new());
let (gate_tx, gate_rx) = oneshot::channel::<()>();
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let runtime = WorkflowRuntime::builder(
queue,
GatedRunner {
claimed: claimed.clone(),
gate: tokio::sync::Mutex::new(Some(gate_rx)),
},
ChannelHook { tx },
)
.build();
let shutdown = spawn_runtime(runtime.clone());
let handle = runtime
.submit(RunSpec {
input: b"x".to_vec(),
..Default::default()
})
.await
.unwrap();
tokio::time::timeout(Duration::from_secs(2), claimed.notified())
.await
.expect("runner reached gate");
// Before cancel: runner is in flight, state is Running.
let before = runtime.status(&handle.run_id).await.expect("active");
assert_eq!(before.state, RunState::Running);
runtime.cancel(&handle.run_id).await.unwrap();
// After cancel but before the gate is released: the step is still
// in flight, but the cancellation overlay must dominate the
// reported state.
let during = runtime
.status(&handle.run_id)
.await
.expect("entry retained while termination is in flight");
assert_eq!(during.state, RunState::Cancelling);
// Release the runner; the worker observes cancel_requested and
// settles the run as Cancelled, removing the entry.
let _ = gate_tx.send(());
let outcome = tokio::time::timeout(Duration::from_secs(2), rx.recv())
.await
.expect("hook fired")
.expect("hook channel open");
assert_eq!(outcome.status, TerminalStatus::Cancelled);
assert!(runtime.status(&handle.run_id).await.is_none());
let _ = shutdown.send(());
}
#[tokio::test]
async fn cancel_unknown_run_returns_false() {
let queue = fresh_queue().await;
let runtime: WorkflowRuntime<ScriptedRunner, NoopTerminalHook> =
WorkflowRuntime::builder(queue, ScriptedRunner::new(vec![]), NoopTerminalHook).build();
let was_cancelled = runtime.cancel("never-submitted").await.unwrap();
assert!(!was_cancelled);
}
#[tokio::test]
async fn transient_fires_once_on_single_attempt() {
assert_transient_retries_until_max(1).await;
}
#[tokio::test]
async fn transient_retries_up_to_max_attempts() {
assert_transient_retries_until_max(3).await;
}
}