fusee-core 1.0.0

Deterministic core of the Fusee durable execution engine: records, segments, journals and shard state with no I/O
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
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
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
//! Journal records.
//!
//! A record is one fact about one workflow run. The same `Record` type is used
//! borrowed ([`RecordRef`], for encoding on the hot path) and owned
//! ([`RecordBuf`], after decoding). Records have two encodings:
//!
//! - **Segment**: inside a log segment. Workflow type and step names are
//!   interned per segment: the first record that uses a name defines it in a
//!   prefix, later records refer to it by a small integer.
//! - **Canonical**: names written inline. Used for journals held in memory,
//!   spilled run blobs, checkpoints, replication and the server protocol, so
//!   a record means the same thing wherever it travels.

use crate::wire::{DecodeError, DecodeResult, Reader, put_bytes, put_varint};
use rustc_hash::FxHashMap;
use std::sync::Arc;

pub type RunId = u64;
pub type Seq = u32;
pub type Lsn = u64;
pub type Name = Arc<str>;
pub type Payload = Arc<[u8]>;

/// How a step, child or workflow ended.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum Status {
    Ok = 0,
    Failed = 1,
    Cancelled = 2,
    TimedOut = 3,
    Terminated = 4,
    /// The run ended by starting a new run of the same workflow ID.
    ContinuedAsNew = 5,
    /// The run was replaced by a reset.
    Reset = 6,
}

impl Status {
    pub fn from_u8(v: u8) -> DecodeResult<Self> {
        Ok(match v {
            0 => Self::Ok,
            1 => Self::Failed,
            2 => Self::Cancelled,
            3 => Self::TimedOut,
            4 => Self::Terminated,
            5 => Self::ContinuedAsNew,
            6 => Self::Reset,
            _ => return Err(DecodeError("unknown status")),
        })
    }

    pub fn as_str(self) -> &'static str {
        match self {
            Self::Ok => "ok",
            Self::Failed => "failed",
            Self::Cancelled => "cancelled",
            Self::TimedOut => "timed_out",
            Self::Terminated => "terminated",
            Self::ContinuedAsNew => "continued_as_new",
            Self::Reset => "reset",
        }
    }

    /// The run handed over to a newer run of the same workflow ID.
    pub fn is_handover(self) -> bool {
        matches!(self, Self::ContinuedAsNew | Self::Reset)
    }
}

/// The parent of a child workflow: the shard and run that started it and the
/// journal position that waits for its result.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ParentRef {
    pub shard: u32,
    pub run: RunId,
    pub seq: Seq,
}

/// Where a run came from, when it is not a fresh start.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Origin {
    pub run: RunId,
    pub cause: u8,
}

pub mod origin {
    /// `ctx.continueAsNew`: the `Start` record also ends the previous run.
    pub const CONTINUED: u8 = 1;
    /// `run.reset`: the previous run is ended separately; records copied from
    /// its journal follow the `Start`.
    pub const RESET: u8 = 2;
}

/// Recorded nondeterministic values (`ctx.now()`, `ctx.random()`, ...).
pub mod value_kind {
    pub const NOW: u8 = 0;
    pub const RANDOM: u8 = 1;
    pub const UUID: u8 = 2;
    pub const CUSTOM: u8 = 3;
}

/// The policy byte of a `Start` record (and the `flags` of a `Child` record,
/// which become the child's start policy). Recorded so replay and recovery
/// apply the same rules.
pub mod policy {
    /// Bits 0–1: starting an ID that is already running.
    pub const RUNNING_MASK: u8 = 0b11;
    pub const RUNNING_FAIL: u8 = 0;
    pub const RUNNING_USE_EXISTING: u8 = 1;
    pub const RUNNING_TERMINATE: u8 = 2;
    /// Bits 2–3: starting an ID whose last run finished within retention.
    pub const FINISHED_MASK: u8 = 0b11 << 2;
    pub const FINISHED_ALLOW: u8 = 0;
    pub const FINISHED_IF_FAILED: u8 = 1 << 2;
    pub const FINISHED_REFUSE: u8 = 2 << 2;
    pub const FINISHED_USE_EXISTING: u8 = 3 << 2;
    /// Bits 4–5: what happens to a child when its parent closes.
    pub const CLOSE_MASK: u8 = 0b11 << 4;
    pub const CLOSE_TERMINATE: u8 = 0;
    pub const CLOSE_CANCEL: u8 = 1 << 4;
    pub const CLOSE_ABANDON: u8 = 2 << 4;
    /// Bit 6: the parent does not wait for the child's result.
    pub const DETACHED: u8 = 1 << 6;
    /// Bit 7: on a `Start`, the run is a keyed object (its workflow ID is
    /// `<type>/<key>`, its handlers are updates and its input is a state
    /// snapshot); on a `Child`, the operation is a call to an object's
    /// handler rather than a child start (its input is the encoded call).
    pub const OBJECT: u8 = 1 << 7;
    /// The policy every start between workflows uses: attach to a run that
    /// exists, so a resent start never creates a second run.
    pub const ATTACH: u8 = RUNNING_USE_EXISTING | FINISHED_USE_EXISTING;
}

/// The outcome of a `Waited` record.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum WaitOutcome {
    /// The wait consumed the run's signal with this ordinal.
    Signal(u64),
    TimedOut,
    /// The code dropped the wait (it lost a race): a signal is no longer
    /// offered to it. Not a delivery.
    Abandoned,
}

/// When a step ran: recorded under the dev profile only (production steps
/// read no clock for it and write no byte), for the UI's timeline.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct StepTime {
    /// When the code asked for the step, Unix ms.
    pub began_ms: u64,
    /// From then until its result, ms (retries and their waits included).
    pub took_ms: u64,
}

/// The rare fields of a `Start` record, boxed so every record stays small.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StartMeta<B> {
    /// Who asked (audit; see [`Record`]), for a reset's start.
    pub by: Option<B>,
    /// The W3C `traceparent` of the request that started the run, when the
    /// starter traces: the run's spans join that trace.
    pub trace: Option<B>,
}

impl<B> StartMeta<B> {
    /// `None` when both are absent, so a plain start carries nothing.
    pub fn of(by: Option<B>, trace: Option<B>) -> Option<Box<Self>> {
        (by.is_some() || trace.is_some()).then(|| Box::new(Self { by, trace }))
    }
}

/// An update carried by a `Start` record (update-with-start).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StartUpdate<S, B> {
    pub name: S,
    pub id: S,
    pub payload: B,
}

/// A journal record.
///
/// Audit: the `by` of a `Signal`, `Cancel`, `Done` (terminate, reset) or a
/// reset's `Start` names who asked, as the API that took the request saw
/// them (a token's scope, a proxy's identity header, a CLI user). It is
/// written after the record's other fields only when present, so records
/// without it carry no extra bytes.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Record<S, B> {
    /// A run was created. Its acknowledgment is released once durable.
    Start {
        run: RunId,
        id: S,
        wtype: S,
        version: u32,
        codec: u8,
        started_ms: u64,
        /// Absolute workflow deadline in Unix milliseconds; 0 means none.
        deadline_ms: u64,
        parent: Option<ParentRef>,
        /// See [`policy`].
        policy: u8,
        /// The run this one continues or replaces.
        from: Option<Origin>,
        /// How many of the run's following records are copies from
        /// `from`'s journal (reset). Their messages are not sent again.
        copies: u32,
        /// An update delivered in the same record (update-with-start).
        /// Boxed: rare, and it keeps every record small.
        update: Option<Box<StartUpdate<S, B>>>,
        input: B,
        /// Who asked (a reset's start) and the trace context: rare, so
        /// boxed.
        meta: Option<Box<StartMeta<B>>>,
    },
    /// A step finished (or was cancelled). Delivered to code in journal order.
    Step {
        run: RunId,
        seq: Seq,
        name: S,
        attempt: u32,
        status: Status,
        payload: B,
        /// When it ran, under the dev profile (a `STEP_TIMED` record).
        time: Option<StepTime>,
    },
    /// A step attempt failed and will be retried at `retry_at_ms`.
    Attempt {
        run: RunId,
        seq: Seq,
        name: S,
        attempt: u32,
        retry_at_ms: u64,
        error: B,
    },
    /// A durable timer was set (`ctx.sleep`).
    Timer {
        run: RunId,
        seq: Seq,
        name: S,
        fire_at_ms: u64,
    },
    /// A durable timer fired and its sleep returned. Delivered.
    Fired { run: RunId, seq: Seq },
    /// A signal arrived. Its acknowledgment is released once durable.
    /// `dedup` (0 = none) suppresses repeated deliveries of the same message.
    Signal {
        run: RunId,
        name: S,
        dedup: u64,
        payload: B,
        /// Who sent it (audit; see [`Record`]).
        by: Option<B>,
    },
    /// The run began waiting for a signal; `deadline_ms` 0 means no timeout.
    Wait {
        run: RunId,
        seq: Seq,
        name: S,
        deadline_ms: u64,
    },
    /// A wait finished. Delivered.
    Waited {
        run: RunId,
        seq: Seq,
        outcome: WaitOutcome,
    },
    /// A recorded nondeterministic value.
    Value {
        run: RunId,
        seq: Seq,
        kind: u8,
        value: B,
    },
    /// The run started a child workflow. Sent once durable. `flags` is the
    /// child's start policy ([`policy`]).
    Child {
        run: RunId,
        seq: Seq,
        wtype: S,
        id: S,
        flags: u8,
        deadline_ms: u64,
        input: B,
    },
    /// A child workflow finished. Delivered.
    ChildDone {
        run: RunId,
        seq: Seq,
        status: Status,
        payload: B,
    },
    /// The run sent a signal to another workflow. Sent once durable.
    Send {
        run: RunId,
        seq: Seq,
        target: S,
        name: S,
        payload: B,
    },
    /// Cancellation was delivered: every operation pending at this point in
    /// the journal ends with `Status::Cancelled`. Delivered.
    Cancel {
        run: RunId,
        reason: B,
        /// Who asked (audit; see [`Record`]).
        by: Option<B>,
    },
    /// The run finished. Its result is released once durable.
    Done {
        run: RunId,
        status: Status,
        finished_ms: u64,
        payload: B,
        /// Who asked, when a request ended the run (terminate, reset);
        /// see [`Record`].
        by: Option<B>,
    },
    /// An update passed its validator and was accepted. Delivered: the
    /// handler starts at this point in the journal. Its ordinal is the
    /// number of updates the run accepted before it.
    Update {
        run: RunId,
        name: S,
        id: S,
        payload: B,
        /// The workflow operation waiting for the result (a call to an
        /// object from workflow code): the result is sent to it as a child
        /// result once the `UpdateDone` is durable.
        caller: Option<ParentRef>,
    },
    /// An update's handler returned. Its result is released once durable.
    UpdateDone {
        run: RunId,
        seq: Seq,
        ordinal: u64,
        status: Status,
        payload: B,
        /// An object's state changes the handler commits with its result
        /// (an encoded JSON object, sealed by the codec; empty for none).
        state: B,
    },
    /// A running step reported progress (only the latest is kept).
    Heartbeat {
        run: RunId,
        seq: Seq,
        attempt: u32,
        payload: B,
    },
    /// An asynchronously completed step handed out its token for `attempt`
    /// and now waits for `engine.complete` or `engine.fail`.
    Awaiting {
        run: RunId,
        seq: Seq,
        name: S,
        attempt: u32,
    },
    /// An asynchronously completed step got its result. Delivered.
    Completed {
        run: RunId,
        seq: Seq,
        attempt: u32,
        status: Status,
        payload: B,
    },
    /// `ctx.patched(id)` took the new branch at journal position `at`. Takes
    /// no position itself, so adding or removing patches never shifts the
    /// journal.
    Patch { run: RunId, at: Seq, id: S },
    /// Search attributes set by the code (a JSON object, never encrypted).
    Attributes { run: RunId, seq: Seq, payload: B },
    /// A chunk appended to one of the run's streams (`ctx.stream`). `seq`
    /// and `attempt` name the step attempt that wrote it, or are
    /// [`stream::WORKFLOW`] and 0 for workflow code; `offset` is its place
    /// in the stream (0, 1, 2, ...). Takes no journal position.
    Chunk {
        run: RunId,
        seq: Seq,
        attempt: u32,
        stream: S,
        offset: u64,
        payload: B,
    },
    /// A stream was completed after `chunks` chunks: closed by workflow
    /// code (`seq` [`stream::WORKFLOW`]) or by a step, or by the final
    /// result of the step that wrote it (`status` is that result's).
    StreamEnd {
        run: RunId,
        seq: Seq,
        stream: S,
        chunks: u64,
        status: Status,
    },
    /// A paused step (an `Attempt` whose `retry_at_ms` is [`PAUSED`]) was
    /// resumed: its next attempt runs at once with a fresh retry budget.
    /// `attempt` is the step's last attempt before it.
    Resume {
        run: RunId,
        seq: Seq,
        attempt: u32,
        /// Who asked (audit; see [`Record`]).
        by: Option<B>,
    },
}

/// The `retry_at_ms` of an `Attempt` whose step is paused: retried only
/// once a [`Record::Resume`] follows.
pub const PAUSED: u64 = u64::MAX;

/// Streams (`ctx.stream`).
pub mod stream {
    use super::Seq;
    /// The `seq` of chunks and ends written by workflow code rather than by
    /// a step.
    pub const WORKFLOW: Seq = Seq::MAX;
}

pub type RecordRef<'a> = Record<&'a str, &'a [u8]>;
pub type RecordBuf = Record<Name, Payload>;

/// A record's payload as a [`Payload`]: shared when it is one, copied
/// when borrowed.
pub trait ToPayload: AsRef<[u8]> {
    fn to_payload(&self) -> Payload;
}

impl ToPayload for Payload {
    #[inline]
    fn to_payload(&self) -> Payload {
        self.clone()
    }
}

impl ToPayload for &[u8] {
    #[inline]
    fn to_payload(&self) -> Payload {
        Arc::from(*self)
    }
}

impl ToPayload for Vec<u8> {
    #[inline]
    fn to_payload(&self) -> Payload {
        Arc::from(&self[..])
    }
}

/// A record's string field as a [`Name`]: shared when it is one already,
/// allocated when borrowed.
pub trait ToName: AsRef<str> {
    fn to_name(&self) -> Name;
}

impl ToName for Name {
    #[inline]
    fn to_name(&self) -> Name {
        self.clone()
    }
}

impl ToName for &str {
    #[inline]
    fn to_name(&self) -> Name {
        Arc::from(*self)
    }
}

pub mod kind {
    pub const START: u8 = 1;
    pub const STEP: u8 = 2;
    pub const ATTEMPT: u8 = 3;
    pub const TIMER: u8 = 4;
    pub const FIRED: u8 = 5;
    pub const SIGNAL: u8 = 6;
    pub const WAIT: u8 = 7;
    pub const WAITED: u8 = 8;
    pub const VALUE: u8 = 9;
    pub const CHILD: u8 = 10;
    pub const CHILD_DONE: u8 = 11;
    pub const SEND: u8 = 12;
    pub const CANCEL: u8 = 13;
    pub const DONE: u8 = 14;
    pub const UPDATE: u8 = 15;
    pub const UPDATE_DONE: u8 = 16;
    pub const HEARTBEAT: u8 = 17;
    pub const AWAITING: u8 = 18;
    pub const COMPLETED: u8 = 19;
    pub const PATCH: u8 = 20;
    pub const ATTRIBUTES: u8 = 21;
    pub const CHUNK: u8 = 22;
    pub const STREAM_END: u8 = 23;
    /// An `Update` that names its caller (a call to an object from
    /// workflow code). Its own kind, so a truncated record never decodes.
    pub const UPDATE_CALL: u8 = 24;
    /// An `UpdateDone` that carries an object's state changes.
    pub const UPDATE_DONE_STATE: u8 = 25;
    /// A `Step` with its times (the dev profile). Its own kind, so a plain
    /// step's bytes carry no times and a truncated record never decodes.
    pub const STEP_TIMED: u8 = 26;
    /// A paused step was resumed.
    pub const RESUME: u8 = 27;
    /// Set on the kind byte when interned name definitions follow it.
    pub const DEFINES: u8 = 0x80;
}

impl<S, B> Record<S, B> {
    /// The same record with its names and payloads converted.
    pub fn map<'a, S2, B2>(
        &'a self,
        n: impl Fn(&'a S) -> S2,
        p: impl Fn(&'a B) -> B2,
    ) -> Record<S2, B2> {
        match self {
            Self::Start {
                run,
                id,
                wtype,
                version,
                codec,
                started_ms,
                deadline_ms,
                parent,
                policy,
                from,
                copies,
                update,
                input,
                meta,
            } => Record::Start {
                run: *run,
                id: n(id),
                wtype: n(wtype),
                version: *version,
                codec: *codec,
                started_ms: *started_ms,
                deadline_ms: *deadline_ms,
                parent: *parent,
                policy: *policy,
                from: *from,
                copies: *copies,
                update: update.as_ref().map(|u| {
                    Box::new(StartUpdate {
                        name: n(&u.name),
                        id: n(&u.id),
                        payload: p(&u.payload),
                    })
                }),
                input: p(input),
                meta: meta.as_ref().map(|m| {
                    Box::new(StartMeta {
                        by: m.by.as_ref().map(&p),
                        trace: m.trace.as_ref().map(&p),
                    })
                }),
            },
            Self::Step {
                run,
                seq,
                name,
                attempt,
                status,
                payload,
                time,
            } => Record::Step {
                run: *run,
                seq: *seq,
                name: n(name),
                attempt: *attempt,
                status: *status,
                payload: p(payload),
                time: *time,
            },
            Self::Attempt {
                run,
                seq,
                name,
                attempt,
                retry_at_ms,
                error,
            } => Record::Attempt {
                run: *run,
                seq: *seq,
                name: n(name),
                attempt: *attempt,
                retry_at_ms: *retry_at_ms,
                error: p(error),
            },
            Self::Timer {
                run,
                seq,
                name,
                fire_at_ms,
            } => Record::Timer {
                run: *run,
                seq: *seq,
                name: n(name),
                fire_at_ms: *fire_at_ms,
            },
            Self::Fired { run, seq } => Record::Fired {
                run: *run,
                seq: *seq,
            },
            Self::Signal {
                run,
                name,
                dedup,
                payload,
                by,
            } => Record::Signal {
                run: *run,
                name: n(name),
                dedup: *dedup,
                payload: p(payload),
                by: by.as_ref().map(&p),
            },
            Self::Wait {
                run,
                seq,
                name,
                deadline_ms,
            } => Record::Wait {
                run: *run,
                seq: *seq,
                name: n(name),
                deadline_ms: *deadline_ms,
            },
            Self::Waited { run, seq, outcome } => Record::Waited {
                run: *run,
                seq: *seq,
                outcome: *outcome,
            },
            Self::Value {
                run,
                seq,
                kind,
                value,
            } => Record::Value {
                run: *run,
                seq: *seq,
                kind: *kind,
                value: p(value),
            },
            Self::Child {
                run,
                seq,
                wtype,
                id,
                flags,
                deadline_ms,
                input,
            } => Record::Child {
                run: *run,
                seq: *seq,
                wtype: n(wtype),
                id: n(id),
                flags: *flags,
                deadline_ms: *deadline_ms,
                input: p(input),
            },
            Self::ChildDone {
                run,
                seq,
                status,
                payload,
            } => Record::ChildDone {
                run: *run,
                seq: *seq,
                status: *status,
                payload: p(payload),
            },
            Self::Send {
                run,
                seq,
                target,
                name,
                payload,
            } => Record::Send {
                run: *run,
                seq: *seq,
                target: n(target),
                name: n(name),
                payload: p(payload),
            },
            Self::Cancel { run, reason, by } => Record::Cancel {
                run: *run,
                reason: p(reason),
                by: by.as_ref().map(&p),
            },
            Self::Done {
                run,
                status,
                finished_ms,
                payload,
                by,
            } => Record::Done {
                run: *run,
                status: *status,
                finished_ms: *finished_ms,
                payload: p(payload),
                by: by.as_ref().map(&p),
            },
            Self::Update {
                run,
                name,
                id,
                payload,
                caller,
            } => Record::Update {
                run: *run,
                name: n(name),
                id: n(id),
                payload: p(payload),
                caller: *caller,
            },
            Self::UpdateDone {
                run,
                seq,
                ordinal,
                status,
                payload,
                state,
            } => Record::UpdateDone {
                run: *run,
                seq: *seq,
                ordinal: *ordinal,
                status: *status,
                payload: p(payload),
                state: p(state),
            },
            Self::Heartbeat {
                run,
                seq,
                attempt,
                payload,
            } => Record::Heartbeat {
                run: *run,
                seq: *seq,
                attempt: *attempt,
                payload: p(payload),
            },
            Self::Awaiting {
                run,
                seq,
                name,
                attempt,
            } => Record::Awaiting {
                run: *run,
                seq: *seq,
                name: n(name),
                attempt: *attempt,
            },
            Self::Completed {
                run,
                seq,
                attempt,
                status,
                payload,
            } => Record::Completed {
                run: *run,
                seq: *seq,
                attempt: *attempt,
                status: *status,
                payload: p(payload),
            },
            Self::Patch { run, at, id } => Record::Patch {
                run: *run,
                at: *at,
                id: n(id),
            },
            Self::Attributes { run, seq, payload } => Record::Attributes {
                run: *run,
                seq: *seq,
                payload: p(payload),
            },
            Self::Chunk {
                run,
                seq,
                attempt,
                stream,
                offset,
                payload,
            } => Record::Chunk {
                run: *run,
                seq: *seq,
                attempt: *attempt,
                stream: n(stream),
                offset: *offset,
                payload: p(payload),
            },
            Self::StreamEnd {
                run,
                seq,
                stream,
                chunks,
                status,
            } => Record::StreamEnd {
                run: *run,
                seq: *seq,
                stream: n(stream),
                chunks: *chunks,
                status: *status,
            },
            Self::Resume {
                run,
                seq,
                attempt,
                by,
            } => Record::Resume {
                run: *run,
                seq: *seq,
                attempt: *attempt,
                by: by.as_ref().map(&p),
            },
        }
    }

    pub fn run(&self) -> RunId {
        match self {
            Self::Start { run, .. }
            | Self::Step { run, .. }
            | Self::Attempt { run, .. }
            | Self::Timer { run, .. }
            | Self::Fired { run, .. }
            | Self::Signal { run, .. }
            | Self::Wait { run, .. }
            | Self::Waited { run, .. }
            | Self::Value { run, .. }
            | Self::Child { run, .. }
            | Self::ChildDone { run, .. }
            | Self::Send { run, .. }
            | Self::Cancel { run, .. }
            | Self::Done { run, .. }
            | Self::Update { run, .. }
            | Self::UpdateDone { run, .. }
            | Self::Heartbeat { run, .. }
            | Self::Awaiting { run, .. }
            | Self::Completed { run, .. }
            | Self::Patch { run, .. }
            | Self::Attributes { run, .. }
            | Self::Chunk { run, .. }
            | Self::StreamEnd { run, .. }
            | Self::Resume { run, .. } => *run,
        }
    }

    /// The same record for another run (reset copies a journal).
    pub fn with_run(&self, new: RunId) -> Self
    where
        S: Clone,
        B: Clone,
    {
        let mut r = self.map(S::clone, B::clone);
        match &mut r {
            Self::Start { run, .. }
            | Self::Step { run, .. }
            | Self::Attempt { run, .. }
            | Self::Timer { run, .. }
            | Self::Fired { run, .. }
            | Self::Signal { run, .. }
            | Self::Wait { run, .. }
            | Self::Waited { run, .. }
            | Self::Value { run, .. }
            | Self::Child { run, .. }
            | Self::ChildDone { run, .. }
            | Self::Send { run, .. }
            | Self::Cancel { run, .. }
            | Self::Done { run, .. }
            | Self::Update { run, .. }
            | Self::UpdateDone { run, .. }
            | Self::Heartbeat { run, .. }
            | Self::Awaiting { run, .. }
            | Self::Completed { run, .. }
            | Self::Patch { run, .. }
            | Self::Attributes { run, .. }
            | Self::Chunk { run, .. }
            | Self::StreamEnd { run, .. }
            | Self::Resume { run, .. } => *run = new,
        }
        r
    }

    pub fn seq(&self) -> Option<Seq> {
        match self {
            Self::Step { seq, .. }
            | Self::Attempt { seq, .. }
            | Self::Timer { seq, .. }
            | Self::Fired { seq, .. }
            | Self::Wait { seq, .. }
            | Self::Waited { seq, .. }
            | Self::Value { seq, .. }
            | Self::Child { seq, .. }
            | Self::ChildDone { seq, .. }
            | Self::Send { seq, .. }
            | Self::UpdateDone { seq, .. }
            | Self::Heartbeat { seq, .. }
            | Self::Awaiting { seq, .. }
            | Self::Completed { seq, .. }
            | Self::Attributes { seq, .. }
            | Self::Chunk { seq, .. }
            | Self::StreamEnd { seq, .. }
            | Self::Resume { seq, .. } => Some(*seq),
            _ => None,
        }
    }

    pub fn kind(&self) -> u8 {
        match self {
            Self::Start { .. } => kind::START,
            Self::Step { .. } => kind::STEP,
            Self::Attempt { .. } => kind::ATTEMPT,
            Self::Timer { .. } => kind::TIMER,
            Self::Fired { .. } => kind::FIRED,
            Self::Signal { .. } => kind::SIGNAL,
            Self::Wait { .. } => kind::WAIT,
            Self::Waited { .. } => kind::WAITED,
            Self::Value { .. } => kind::VALUE,
            Self::Child { .. } => kind::CHILD,
            Self::ChildDone { .. } => kind::CHILD_DONE,
            Self::Send { .. } => kind::SEND,
            Self::Cancel { .. } => kind::CANCEL,
            Self::Done { .. } => kind::DONE,
            Self::Update {
                caller: Some(_), ..
            } => kind::UPDATE_CALL,
            Self::Update { .. } => kind::UPDATE,
            Self::UpdateDone { .. } => kind::UPDATE_DONE,
            Self::Heartbeat { .. } => kind::HEARTBEAT,
            Self::Awaiting { .. } => kind::AWAITING,
            Self::Completed { .. } => kind::COMPLETED,
            Self::Patch { .. } => kind::PATCH,
            Self::Attributes { .. } => kind::ATTRIBUTES,
            Self::Chunk { .. } => kind::CHUNK,
            Self::StreamEnd { .. } => kind::STREAM_END,
            Self::Resume { .. } => kind::RESUME,
        }
    }
}

impl<S: AsRef<str>, B: AsRef<[u8]>> Record<S, B> {
    /// Calls `f` for each interned name the record carries, in encoding order.
    pub fn for_each_name(&self, mut f: impl FnMut(&str)) {
        match self {
            Self::Start { wtype, update, .. } => {
                f(wtype.as_ref());
                if let Some(u) = update {
                    f(u.name.as_ref());
                }
            }
            Self::Step { name, .. }
            | Self::Attempt { name, .. }
            | Self::Timer { name, .. }
            | Self::Signal { name, .. }
            | Self::Wait { name, .. }
            | Self::Update { name, .. }
            | Self::Awaiting { name, .. } => f(name.as_ref()),
            Self::Child { wtype, .. } => f(wtype.as_ref()),
            Self::Send { name, .. } => f(name.as_ref()),
            Self::Chunk { stream, .. } | Self::StreamEnd { stream, .. } => f(stream.as_ref()),
            _ => {}
        }
    }

    pub fn as_ref(&self) -> RecordRef<'_> {
        self.map(|s| s.as_ref(), |b| b.as_ref())
    }

    pub fn to_buf(&self) -> RecordBuf {
        self.map(|s| Arc::from(s.as_ref()), |b| Arc::from(b.as_ref()))
    }
}

/// How names are written into a record body.
pub trait NameEncoder {
    fn put(&mut self, out: &mut Vec<u8>, name: &str);
}

/// How names are read from a record body.
pub trait NameDecoder {
    /// Called when the kind byte carries [`kind::DEFINES`].
    fn define(&mut self, r: &mut Reader<'_>) -> DecodeResult<()>;
    fn get(&mut self, r: &mut Reader<'_>) -> DecodeResult<Name>;
}

/// Canonical encoding: names inline.
pub struct Inline;

impl NameEncoder for Inline {
    #[inline]
    fn put(&mut self, out: &mut Vec<u8>, name: &str) {
        put_bytes(out, name.as_bytes());
    }
}

/// Canonical decoding. Repeated names share one allocation.
#[derive(Default)]
pub struct InlineDecoder {
    cache: FxHashMap<Box<str>, Name>,
}

impl NameDecoder for InlineDecoder {
    fn define(&mut self, _r: &mut Reader<'_>) -> DecodeResult<()> {
        Err(DecodeError("name definitions in canonical record"))
    }

    fn get(&mut self, r: &mut Reader<'_>) -> DecodeResult<Name> {
        let s = r.str()?;
        if let Some(n) = self.cache.get(s) {
            return Ok(n.clone());
        }
        let name: Name = Arc::from(s);
        if self.cache.len() < 4096 {
            self.cache.insert(Box::from(s), name.clone());
        }
        Ok(name)
    }
}

fn put_status(out: &mut Vec<u8>, s: Status) {
    out.push(s as u8);
}

/// Optional fields after a record's others, each as its tag and bytes, in
/// tag order, only when present and not empty, so a record without them
/// carries no extra bytes (see [`Record`]).
mod ext {
    /// Who asked (audit).
    pub const BY: u8 = 1;
    /// A `Start`'s trace context.
    pub const TRACE: u8 = 2;
}

fn put_ext<B: AsRef<[u8]>>(out: &mut Vec<u8>, tag: u8, v: &Option<B>) {
    if let Some(b) = v
        && !b.as_ref().is_empty()
    {
        out.push(tag);
        put_bytes(out, b.as_ref());
    }
}

/// The optional fields after a record's others: those with tags up to
/// `last`, each once, in order and not empty, so a stray byte still fails
/// the record.
fn exts(r: &mut Reader<'_>, last: u8) -> DecodeResult<[Option<Payload>; 2]> {
    let mut out = [None, None];
    let mut prev = 0u8;
    while !r.is_empty() {
        let tag = r.u8()?;
        if tag <= prev || tag > last {
            return Err(DecodeError("trailing bytes"));
        }
        let b = r.bytes()?;
        if b.is_empty() {
            return Err(DecodeError("trailing bytes"));
        }
        out[usize::from(tag - 1)] = Some(Arc::from(b));
        prev = tag;
    }
    Ok(out)
}

/// The `by` of a record that has no other optional field.
fn by(r: &mut Reader<'_>) -> DecodeResult<Option<Payload>> {
    let [by, _] = exts(r, ext::BY)?;
    Ok(by)
}

/// Encodes the record body: the kind byte, then fields. `defines` holds new
/// interned names to define in a prefix (segment encoding only).
pub fn encode_body<S: AsRef<str>, B: AsRef<[u8]>>(
    rec: &Record<S, B>,
    defines: &[&str],
    names: &mut impl NameEncoder,
    out: &mut Vec<u8>,
) {
    let k = match rec {
        Record::UpdateDone { state, .. } if !state.as_ref().is_empty() => kind::UPDATE_DONE_STATE,
        Record::Step { time: Some(_), .. } => kind::STEP_TIMED,
        _ => rec.kind(),
    };
    if defines.is_empty() {
        out.push(k);
    } else {
        out.push(k | kind::DEFINES);
        put_varint(out, defines.len() as u64);
        for d in defines {
            put_bytes(out, d.as_bytes());
        }
    }
    let seqv = |out: &mut Vec<u8>, s: &Seq| put_varint(out, u64::from(*s));
    match rec {
        Record::Start {
            run,
            id,
            wtype,
            version,
            codec,
            started_ms,
            deadline_ms,
            parent,
            policy,
            from,
            copies,
            update,
            input,
            meta,
        } => {
            put_varint(out, *run);
            put_bytes(out, id.as_ref().as_bytes());
            names.put(out, wtype.as_ref());
            put_varint(out, u64::from(*version));
            out.push(*codec);
            put_varint(out, *started_ms);
            put_varint(out, *deadline_ms);
            match parent {
                None => out.push(0),
                Some(p) => {
                    out.push(1);
                    put_varint(out, u64::from(p.shard));
                    put_varint(out, p.run);
                    put_varint(out, u64::from(p.seq));
                }
            }
            out.push(*policy);
            match from {
                None => out.push(0),
                Some(o) => {
                    out.push(1);
                    put_varint(out, o.run);
                    out.push(o.cause);
                }
            }
            put_varint(out, u64::from(*copies));
            match update {
                None => out.push(0),
                Some(u) => {
                    out.push(1);
                    names.put(out, u.name.as_ref());
                    put_bytes(out, u.id.as_ref().as_bytes());
                    put_bytes(out, u.payload.as_ref());
                }
            }
            put_bytes(out, input.as_ref());
            if let Some(m) = meta {
                put_ext(out, ext::BY, &m.by);
                put_ext(out, ext::TRACE, &m.trace);
            }
        }
        Record::Step {
            run,
            seq,
            name,
            attempt,
            status,
            payload,
            time,
        } => {
            put_varint(out, *run);
            seqv(out, seq);
            names.put(out, name.as_ref());
            put_varint(out, u64::from(*attempt));
            put_status(out, *status);
            put_bytes(out, payload.as_ref());
            // Only in a `STEP_TIMED` record.
            if let Some(t) = time {
                put_varint(out, t.began_ms);
                put_varint(out, t.took_ms);
            }
        }
        Record::Attempt {
            run,
            seq,
            name,
            attempt,
            retry_at_ms,
            error,
        } => {
            put_varint(out, *run);
            seqv(out, seq);
            names.put(out, name.as_ref());
            put_varint(out, u64::from(*attempt));
            put_varint(out, *retry_at_ms);
            put_bytes(out, error.as_ref());
        }
        Record::Timer {
            run,
            seq,
            name,
            fire_at_ms,
        } => {
            put_varint(out, *run);
            seqv(out, seq);
            names.put(out, name.as_ref());
            put_varint(out, *fire_at_ms);
        }
        Record::Fired { run, seq } => {
            put_varint(out, *run);
            seqv(out, seq);
        }
        Record::Signal {
            run,
            name,
            dedup,
            payload,
            by,
        } => {
            put_varint(out, *run);
            names.put(out, name.as_ref());
            put_varint(out, *dedup);
            put_bytes(out, payload.as_ref());
            put_ext(out, ext::BY, by);
        }
        Record::Wait {
            run,
            seq,
            name,
            deadline_ms,
        } => {
            put_varint(out, *run);
            seqv(out, seq);
            names.put(out, name.as_ref());
            put_varint(out, *deadline_ms);
        }
        Record::Waited { run, seq, outcome } => {
            put_varint(out, *run);
            seqv(out, seq);
            match outcome {
                WaitOutcome::Signal(ordinal) => {
                    out.push(0);
                    put_varint(out, *ordinal);
                }
                WaitOutcome::TimedOut => out.push(1),
                WaitOutcome::Abandoned => out.push(2),
            }
        }
        Record::Value {
            run,
            seq,
            kind,
            value,
        } => {
            put_varint(out, *run);
            seqv(out, seq);
            out.push(*kind);
            put_bytes(out, value.as_ref());
        }
        Record::Child {
            run,
            seq,
            wtype,
            id,
            flags,
            deadline_ms,
            input,
        } => {
            put_varint(out, *run);
            seqv(out, seq);
            names.put(out, wtype.as_ref());
            put_bytes(out, id.as_ref().as_bytes());
            out.push(*flags);
            put_varint(out, *deadline_ms);
            put_bytes(out, input.as_ref());
        }
        Record::ChildDone {
            run,
            seq,
            status,
            payload,
        } => {
            put_varint(out, *run);
            seqv(out, seq);
            put_status(out, *status);
            put_bytes(out, payload.as_ref());
        }
        Record::Send {
            run,
            seq,
            target,
            name,
            payload,
        } => {
            put_varint(out, *run);
            seqv(out, seq);
            put_bytes(out, target.as_ref().as_bytes());
            names.put(out, name.as_ref());
            put_bytes(out, payload.as_ref());
        }
        Record::Cancel { run, reason, by } => {
            put_varint(out, *run);
            put_bytes(out, reason.as_ref());
            put_ext(out, ext::BY, by);
        }
        Record::Done {
            run,
            status,
            finished_ms,
            payload,
            by,
        } => {
            put_varint(out, *run);
            put_status(out, *status);
            put_varint(out, *finished_ms);
            put_bytes(out, payload.as_ref());
            put_ext(out, ext::BY, by);
        }
        Record::Update {
            run,
            name,
            id,
            payload,
            caller,
        } => {
            put_varint(out, *run);
            names.put(out, name.as_ref());
            put_bytes(out, id.as_ref().as_bytes());
            put_bytes(out, payload.as_ref());
            // Only in an `UPDATE_CALL` record: an update without a caller
            // encodes as it always has.
            if let Some(p) = caller {
                put_varint(out, u64::from(p.shard));
                put_varint(out, p.run);
                put_varint(out, u64::from(p.seq));
            }
        }
        Record::UpdateDone {
            run,
            seq,
            ordinal,
            status,
            payload,
            state,
        } => {
            put_varint(out, *run);
            seqv(out, seq);
            put_varint(out, *ordinal);
            put_status(out, *status);
            put_bytes(out, payload.as_ref());
            // Only in an `UPDATE_DONE_STATE` record.
            if !state.as_ref().is_empty() {
                put_bytes(out, state.as_ref());
            }
        }
        Record::Heartbeat {
            run,
            seq,
            attempt,
            payload,
        } => {
            put_varint(out, *run);
            seqv(out, seq);
            put_varint(out, u64::from(*attempt));
            put_bytes(out, payload.as_ref());
        }
        Record::Awaiting {
            run,
            seq,
            name,
            attempt,
        } => {
            put_varint(out, *run);
            seqv(out, seq);
            names.put(out, name.as_ref());
            put_varint(out, u64::from(*attempt));
        }
        Record::Completed {
            run,
            seq,
            attempt,
            status,
            payload,
        } => {
            put_varint(out, *run);
            seqv(out, seq);
            put_varint(out, u64::from(*attempt));
            put_status(out, *status);
            put_bytes(out, payload.as_ref());
        }
        Record::Patch { run, at, id } => {
            put_varint(out, *run);
            seqv(out, at);
            put_bytes(out, id.as_ref().as_bytes());
        }
        Record::Attributes { run, seq, payload } => {
            put_varint(out, *run);
            seqv(out, seq);
            put_bytes(out, payload.as_ref());
        }
        Record::Chunk {
            run,
            seq,
            attempt,
            stream,
            offset,
            payload,
        } => {
            put_varint(out, *run);
            seqv(out, seq);
            put_varint(out, u64::from(*attempt));
            names.put(out, stream.as_ref());
            put_varint(out, *offset);
            put_bytes(out, payload.as_ref());
        }
        Record::StreamEnd {
            run,
            seq,
            stream,
            chunks,
            status,
        } => {
            put_varint(out, *run);
            seqv(out, seq);
            names.put(out, stream.as_ref());
            put_varint(out, *chunks);
            put_status(out, *status);
        }
        Record::Resume {
            run,
            seq,
            attempt,
            by,
        } => {
            put_varint(out, *run);
            seqv(out, seq);
            put_varint(out, u64::from(*attempt));
            put_ext(out, ext::BY, by);
        }
    }
}

fn payload(r: &mut Reader<'_>) -> DecodeResult<Payload> {
    Ok(Arc::from(r.bytes()?))
}

fn inline_name(r: &mut Reader<'_>) -> DecodeResult<Name> {
    Ok(Arc::from(r.str()?))
}

/// Decodes a record body produced by [`encode_body`]. Fails on trailing bytes.
pub fn decode_body(bytes: &[u8], names: &mut impl NameDecoder) -> DecodeResult<RecordBuf> {
    let mut r = Reader::new(bytes);
    let mut k = r.u8()?;
    if k & kind::DEFINES != 0 {
        k &= !kind::DEFINES;
        names.define(&mut r)?;
    }
    let rec = match k {
        kind::START => {
            let run = r.varint()?;
            let id = inline_name(&mut r)?;
            let wtype = names.get(&mut r)?;
            let version = r.varint_u32()?;
            let codec = r.u8()?;
            let started_ms = r.varint()?;
            let deadline_ms = r.varint()?;
            let parent = match r.u8()? {
                0 => None,
                1 => Some(ParentRef {
                    shard: r.varint_u32()?,
                    run: r.varint()?,
                    seq: r.varint_u32()?,
                }),
                _ => return Err(DecodeError("bad parent flag")),
            };
            let policy = r.u8()?;
            let from = match r.u8()? {
                0 => None,
                1 => Some(Origin {
                    run: r.varint()?,
                    cause: r.u8()?,
                }),
                _ => return Err(DecodeError("bad origin flag")),
            };
            let copies = r.varint_u32()?;
            let update = match r.u8()? {
                0 => None,
                1 => Some(Box::new(StartUpdate {
                    name: names.get(&mut r)?,
                    id: inline_name(&mut r)?,
                    payload: payload(&mut r)?,
                })),
                _ => return Err(DecodeError("bad update flag")),
            };
            Record::Start {
                run,
                id,
                wtype,
                version,
                codec,
                started_ms,
                deadline_ms,
                parent,
                policy,
                from,
                copies,
                update,
                input: payload(&mut r)?,
                meta: {
                    let [by, trace] = exts(&mut r, ext::TRACE)?;
                    StartMeta::of(by, trace)
                },
            }
        }
        kind::STEP | kind::STEP_TIMED => Record::Step {
            run: r.varint()?,
            seq: r.varint_u32()?,
            name: names.get(&mut r)?,
            attempt: r.varint_u32()?,
            status: Status::from_u8(r.u8()?)?,
            payload: payload(&mut r)?,
            time: if k == kind::STEP_TIMED {
                Some(StepTime {
                    began_ms: r.varint()?,
                    took_ms: r.varint()?,
                })
            } else {
                None
            },
        },
        kind::ATTEMPT => Record::Attempt {
            run: r.varint()?,
            seq: r.varint_u32()?,
            name: names.get(&mut r)?,
            attempt: r.varint_u32()?,
            retry_at_ms: r.varint()?,
            error: payload(&mut r)?,
        },
        kind::TIMER => Record::Timer {
            run: r.varint()?,
            seq: r.varint_u32()?,
            name: names.get(&mut r)?,
            fire_at_ms: r.varint()?,
        },
        kind::FIRED => Record::Fired {
            run: r.varint()?,
            seq: r.varint_u32()?,
        },
        kind::SIGNAL => Record::Signal {
            run: r.varint()?,
            name: names.get(&mut r)?,
            dedup: r.varint()?,
            payload: payload(&mut r)?,
            by: by(&mut r)?,
        },
        kind::WAIT => Record::Wait {
            run: r.varint()?,
            seq: r.varint_u32()?,
            name: names.get(&mut r)?,
            deadline_ms: r.varint()?,
        },
        kind::WAITED => {
            let run = r.varint()?;
            let seq = r.varint_u32()?;
            let outcome = match r.u8()? {
                0 => WaitOutcome::Signal(r.varint()?),
                1 => WaitOutcome::TimedOut,
                2 => WaitOutcome::Abandoned,
                _ => return Err(DecodeError("bad wait outcome")),
            };
            Record::Waited { run, seq, outcome }
        }
        kind::VALUE => Record::Value {
            run: r.varint()?,
            seq: r.varint_u32()?,
            kind: r.u8()?,
            value: payload(&mut r)?,
        },
        kind::CHILD => Record::Child {
            run: r.varint()?,
            seq: r.varint_u32()?,
            wtype: names.get(&mut r)?,
            id: inline_name(&mut r)?,
            flags: r.u8()?,
            deadline_ms: r.varint()?,
            input: payload(&mut r)?,
        },
        kind::CHILD_DONE => Record::ChildDone {
            run: r.varint()?,
            seq: r.varint_u32()?,
            status: Status::from_u8(r.u8()?)?,
            payload: payload(&mut r)?,
        },
        kind::SEND => Record::Send {
            run: r.varint()?,
            seq: r.varint_u32()?,
            target: inline_name(&mut r)?,
            name: names.get(&mut r)?,
            payload: payload(&mut r)?,
        },
        kind::CANCEL => Record::Cancel {
            run: r.varint()?,
            reason: payload(&mut r)?,
            by: by(&mut r)?,
        },
        kind::DONE => Record::Done {
            run: r.varint()?,
            status: Status::from_u8(r.u8()?)?,
            finished_ms: r.varint()?,
            payload: payload(&mut r)?,
            by: by(&mut r)?,
        },
        kind::UPDATE | kind::UPDATE_CALL => Record::Update {
            run: r.varint()?,
            name: names.get(&mut r)?,
            id: inline_name(&mut r)?,
            payload: payload(&mut r)?,
            caller: if k == kind::UPDATE {
                None
            } else {
                Some(ParentRef {
                    shard: r.varint_u32()?,
                    run: r.varint()?,
                    seq: r.varint_u32()?,
                })
            },
        },
        kind::UPDATE_DONE | kind::UPDATE_DONE_STATE => Record::UpdateDone {
            run: r.varint()?,
            seq: r.varint_u32()?,
            ordinal: r.varint()?,
            status: Status::from_u8(r.u8()?)?,
            payload: payload(&mut r)?,
            state: if k == kind::UPDATE_DONE {
                Arc::from(&[][..])
            } else {
                payload(&mut r)?
            },
        },
        kind::HEARTBEAT => Record::Heartbeat {
            run: r.varint()?,
            seq: r.varint_u32()?,
            attempt: r.varint_u32()?,
            payload: payload(&mut r)?,
        },
        kind::AWAITING => Record::Awaiting {
            run: r.varint()?,
            seq: r.varint_u32()?,
            name: names.get(&mut r)?,
            attempt: r.varint_u32()?,
        },
        kind::COMPLETED => Record::Completed {
            run: r.varint()?,
            seq: r.varint_u32()?,
            attempt: r.varint_u32()?,
            status: Status::from_u8(r.u8()?)?,
            payload: payload(&mut r)?,
        },
        kind::PATCH => Record::Patch {
            run: r.varint()?,
            at: r.varint_u32()?,
            id: inline_name(&mut r)?,
        },
        kind::ATTRIBUTES => Record::Attributes {
            run: r.varint()?,
            seq: r.varint_u32()?,
            payload: payload(&mut r)?,
        },
        kind::CHUNK => Record::Chunk {
            run: r.varint()?,
            seq: r.varint_u32()?,
            attempt: r.varint_u32()?,
            stream: names.get(&mut r)?,
            offset: r.varint()?,
            payload: payload(&mut r)?,
        },
        kind::STREAM_END => Record::StreamEnd {
            run: r.varint()?,
            seq: r.varint_u32()?,
            stream: names.get(&mut r)?,
            chunks: r.varint()?,
            status: Status::from_u8(r.u8()?)?,
        },
        kind::RESUME => Record::Resume {
            run: r.varint()?,
            seq: r.varint_u32()?,
            attempt: r.varint_u32()?,
            by: by(&mut r)?,
        },
        _ => return Err(DecodeError("unknown record kind")),
    };
    if !r.is_empty() {
        return Err(DecodeError("trailing bytes after record"));
    }
    Ok(rec)
}

/// Canonical encoding of one record, appended to `out` with a varint length
/// prefix so records can be concatenated into journals and blobs.
pub fn encode_canonical<S: AsRef<str>, B: AsRef<[u8]>>(rec: &Record<S, B>, out: &mut Vec<u8>) {
    // The body goes straight into `out` after a one-byte length, which
    // fits bodies under 128 bytes; a longer one moves over for its varint.
    let at = out.len();
    out.push(0);
    encode_body(rec, &[], &mut Inline, out);
    let len = out.len() - at - 1;
    if len < 0x80 {
        out[at] = len as u8;
    } else {
        // The varint on the stack: the body moves once, nothing is
        // allocated for the prefix.
        let mut prefix = [0u8; 10];
        let mut n = 0;
        let mut v = len as u64;
        while v >= 0x80 {
            prefix[n] = (v as u8) | 0x80;
            v >>= 7;
            n += 1;
        }
        prefix[n] = v as u8;
        out.splice(at..at + 1, prefix[..=n].iter().copied());
    }
}

/// Decodes a concatenation of [`encode_canonical`] records.
pub fn decode_canonical_all(bytes: &[u8]) -> DecodeResult<Vec<RecordBuf>> {
    let mut out = Vec::new();
    decode_canonical_into(bytes, &mut InlineDecoder::default(), &mut out)?;
    Ok(out)
}

/// [`decode_canonical_all`] into `out` (cleared first), with a name cache
/// the caller keeps: names repeated across journals (workflow types, step
/// names) are then decoded without allocating.
pub fn decode_canonical_into(
    bytes: &[u8],
    names: &mut InlineDecoder,
    out: &mut Vec<RecordBuf>,
) -> DecodeResult<()> {
    out.clear();
    let mut r = Reader::new(bytes);
    while !r.is_empty() {
        let body = r.bytes()?;
        out.push(decode_body(body, names)?);
    }
    Ok(())
}

/// A plain start record (no origin, copies or update).
#[allow(clippy::too_many_arguments)]
pub fn start_record(
    run: RunId,
    id: Name,
    wtype: Name,
    version: u32,
    started_ms: u64,
    deadline_ms: u64,
    parent: Option<ParentRef>,
    policy: u8,
    input: Payload,
) -> RecordBuf {
    Record::Start {
        run,
        id,
        wtype,
        version,
        codec: 1,
        started_ms,
        deadline_ms,
        parent,
        policy,
        from: None,
        copies: 0,
        update: None,
        input,
        meta: None,
    }
}

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

    pub(crate) fn samples() -> Vec<RecordBuf> {
        let refs: Vec<RecordRef<'static>> = vec![
            Record::Start {
                run: 7,
                id: "order-123",
                wtype: "checkout",
                version: 2,
                codec: 1,
                started_ms: 1_700_000_000_000,
                deadline_ms: 0,
                parent: Some(ParentRef {
                    shard: 3,
                    run: 9,
                    seq: 4,
                }),
                policy: policy::RUNNING_USE_EXISTING | policy::CLOSE_ABANDON,
                from: Some(Origin {
                    run: 6,
                    cause: origin::CONTINUED,
                }),
                copies: 0,
                update: Some(Box::new(StartUpdate {
                    name: "addItem",
                    id: "u-1",
                    payload: b"{}",
                })),
                input: b"{\"a\":1}",
                meta: Some(Box::new(StartMeta {
                    by: Some(b"token:full"),
                    trace: Some(b"00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01"),
                })),
            },
            Record::Step {
                run: 7,
                seq: 0,
                name: "quote",
                attempt: 1,
                status: Status::Ok,
                payload: b"12",
                time: None,
            },
            Record::Step {
                run: 7,
                seq: 2,
                name: "ship",
                attempt: 2,
                status: Status::Ok,
                payload: b"{}",
                time: Some(StepTime {
                    began_ms: 1_762_000_000_123,
                    took_ms: 1_500,
                }),
            },
            Record::Attempt {
                run: 7,
                seq: 1,
                name: "charge",
                attempt: 1,
                retry_at_ms: 5,
                error: b"boom",
            },
            Record::Timer {
                run: 7,
                seq: 2,
                name: "cool-off",
                fire_at_ms: 99,
            },
            Record::Fired { run: 7, seq: 2 },
            Record::Signal {
                run: 7,
                name: "review",
                dedup: 0,
                payload: b"true",
                by: None,
            },
            Record::Signal {
                run: 7,
                name: "review",
                dedup: 3,
                payload: b"false",
                by: Some(b"proxy:ana@example.com"),
            },
            Record::Wait {
                run: 7,
                seq: 3,
                name: "review",
                deadline_ms: 0,
            },
            Record::Waited {
                run: 7,
                seq: 3,
                outcome: WaitOutcome::Signal(0),
            },
            Record::Waited {
                run: 7,
                seq: 9,
                outcome: WaitOutcome::TimedOut,
            },
            Record::Waited {
                run: 7,
                seq: 8,
                outcome: WaitOutcome::Abandoned,
            },
            Record::Value {
                run: 7,
                seq: 4,
                kind: value_kind::RANDOM,
                value: &[1, 2, 3],
            },
            Record::Child {
                run: 7,
                seq: 5,
                wtype: "ship",
                id: "order-123/ship",
                flags: 0,
                deadline_ms: 0,
                input: b"{}",
            },
            Record::ChildDone {
                run: 7,
                seq: 5,
                status: Status::Failed,
                payload: b"x",
            },
            Record::Send {
                run: 7,
                seq: 6,
                target: "other",
                name: "ping",
                payload: b"",
            },
            Record::Cancel {
                run: 7,
                reason: b"user",
                by: Some(b"token:client"),
            },
            Record::Update {
                run: 7,
                name: "addItem",
                id: "u-2",
                payload: b"3",
                caller: None,
            },
            Record::Update {
                run: 7,
                name: "add",
                id: "call-1",
                payload: b"5",
                caller: Some(ParentRef {
                    shard: 3,
                    run: 12,
                    seq: 4,
                }),
            },
            Record::UpdateDone {
                run: 7,
                seq: 10,
                ordinal: 1,
                status: Status::Ok,
                payload: b"4",
                state: b"",
            },
            Record::UpdateDone {
                run: 7,
                seq: 11,
                ordinal: 2,
                status: Status::Ok,
                payload: b"6",
                state: b"{\"n\":1}",
            },
            Record::Heartbeat {
                run: 7,
                seq: 11,
                attempt: 2,
                payload: b"{\"page\":4}",
            },
            Record::Awaiting {
                run: 7,
                seq: 12,
                name: "approval",
                attempt: 1,
            },
            Record::Completed {
                run: 7,
                seq: 12,
                attempt: 1,
                status: Status::Ok,
                payload: b"true",
            },
            Record::Patch {
                run: 7,
                at: 13,
                id: "refund-rounding",
            },
            Record::Attributes {
                run: 7,
                seq: 13,
                payload: b"{\"status\":\"paid\"}",
            },
            Record::Chunk {
                run: 7,
                seq: 14,
                attempt: 2,
                stream: "tokens",
                offset: 3,
                payload: b"\"lo\"",
            },
            Record::Chunk {
                run: 7,
                seq: stream::WORKFLOW,
                attempt: 0,
                stream: "progress",
                offset: 0,
                payload: b"\0raw",
            },
            Record::StreamEnd {
                run: 7,
                seq: 14,
                stream: "tokens",
                chunks: 4,
                status: Status::Failed,
            },
            Record::Attempt {
                run: 7,
                seq: 15,
                name: "charge",
                attempt: 5,
                retry_at_ms: PAUSED,
                error: b"declined",
            },
            Record::Resume {
                run: 7,
                seq: 15,
                attempt: 5,
                by: Some(b"token"),
            },
            Record::Resume {
                run: 7,
                seq: 15,
                attempt: 6,
                by: None,
            },
            Record::Done {
                run: 7,
                status: Status::ContinuedAsNew,
                finished_ms: 1_700_000_000_500,
                payload: b"\"ch_1\"",
                by: None,
            },
        ];
        refs.iter().map(|r| r.to_buf()).collect()
    }

    #[test]
    fn canonical_round_trip() {
        let recs = samples();
        let mut out = Vec::new();
        for r in &recs {
            encode_canonical(r, &mut out);
        }
        assert_eq!(decode_canonical_all(&out).unwrap(), recs);
    }

    /// The record with its optional trailing fields dropped one at a time
    /// from the end: what a body cut just before each reads as.
    fn shorter(r: &RecordBuf) -> Vec<RecordBuf> {
        let mut out = Vec::new();
        let mut r = r.clone();
        loop {
            let dropped = match &mut r {
                Record::Start { meta, .. } => match meta.as_deref_mut() {
                    Some(m) if m.trace.is_some() => m.trace.take().is_some(),
                    Some(m) if m.by.is_some() => m.by.take().is_some(),
                    _ => false,
                },
                Record::Signal { by, .. }
                | Record::Cancel { by, .. }
                | Record::Done { by, .. }
                | Record::Resume { by, .. } => by.take().is_some(),
                _ => false,
            };
            if !dropped {
                return out;
            }
            if let Record::Start { meta, .. } = &mut r
                && meta
                    .as_ref()
                    .is_some_and(|m| m.by.is_none() && m.trace.is_none())
            {
                *meta = None;
            }
            out.push(r.clone());
        }
    }

    #[test]
    fn truncated_bodies_fail_cleanly() {
        for r in samples() {
            let mut body = Vec::new();
            encode_body(&r, &[], &mut Inline, &mut body);
            // Optional fields are last: a body cut just before one is the
            // record without it (the framing around bodies catches cuts).
            let prefixes: Vec<(usize, RecordBuf)> = shorter(&r)
                .into_iter()
                .map(|s| {
                    let mut b = Vec::new();
                    encode_body(&s, &[], &mut Inline, &mut b);
                    (b.len(), s)
                })
                .collect();
            for cut in 0..body.len() {
                let decoded = decode_body(&body[..cut], &mut InlineDecoder::default());
                match prefixes.iter().find(|(len, _)| *len == cut) {
                    Some((_, s)) => assert_eq!(&decoded.unwrap(), s),
                    None => assert!(decoded.is_err(), "{r:?} cut at {cut}"),
                }
            }
            let mut extra = body.clone();
            extra.push(0);
            assert!(decode_body(&extra, &mut InlineDecoder::default()).is_err());
        }
    }

    #[test]
    fn with_run_rewrites_every_kind() {
        for r in samples() {
            assert_eq!(r.with_run(42).run(), 42);
        }
    }

    /// The length prefix is the body's varint whatever its width: bodies
    /// around each width's edge, after other records in the same buffer.
    #[test]
    fn canonical_prefixes_match_the_body_length() {
        for size in [
            0usize, 100, 110, 127, 128, 200, 16_300, 16_384, 70_000, 2_100_000,
        ] {
            let rec: RecordBuf = Record::Value {
                run: 1,
                seq: 2,
                kind: value_kind::CUSTOM,
                value: Arc::from(vec![7u8; size]),
            };
            let mut body = Vec::new();
            encode_body(&rec, &[], &mut Inline, &mut body);
            let mut expected = vec![0xAA];
            put_bytes(&mut expected, &body);
            let mut out = vec![0xAA];
            encode_canonical(&rec, &mut out);
            assert_eq!(out, expected, "payload of {size} bytes");
            encode_canonical(&rec, &mut out);
            assert_eq!(
                decode_canonical_all(&out[1..]).unwrap(),
                vec![rec.clone(), rec]
            );
        }
    }
}