datumesh-engine 0.5.0

The Datumesh engine: the value of a cell as of a time, from stored receipts or by evaluating functions; memoised, and signed for computed cells.
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
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
//! One request's evaluation: an explicit stack of frames, never native recursion.
//!
//! A frame evaluates one computed cell as of one asof (the request's, or the transaction time
//! `T` of an `asof(c, T)` read). Its runtime reads inputs through [`Reader`]; a stored input,
//! or one already answered in this request, is answered at once. A computed input not
//! answered yet suspends the frame: the read returns [`EvalError::Suspended`], the engine
//! pushes a frame for the input (for a slice over a computed function, for each missing
//! member in turn), and once they are done it runs the suspended frame's runtime again from
//! the start. Every cell answered is kept in the request, so a re-run reads it at once. New
//! receipts, manifests and versions are written to the store only when the whole request has
//! succeeded.

use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::sync::Arc;

use datumesh_core::{
    inherit_licence, lexical, Arg, Args, Cell, Declarations, Error as CoreError, ErrorCode, Fault, FunctionDescriptor,
    Inheritance, KeyDocument, LicenceReason, Manifest, Receipt, ReceiptBuilder, Scalar, Slice,
};

use crate::cache::Parsed;
use crate::check::{check_args, valid_args};
use crate::engine::{Engine, STORED};
use crate::error::{EngineError, EvalError, StoreError};
use crate::memo::Entry;
use crate::runtime::{Budget, FnBinding, Input, Inputs, Member, SlicePattern};
use crate::sign::receipt_json;
use crate::slice::{cells_slice, extent_cells, spec_of, versions_slice, Spec};
use crate::whatif::{scoped_hash, Scope};

/// The earliest instant: the asof and tx of a computed receipt with no inputs.
pub(crate) const MIN_INSTANT: &str = "0001-01-01T00:00:00.000000Z";
/// The latest instant.
pub(crate) const MAX_INSTANT: &str = "9999-12-31T23:59:59.999999Z";

/// A string just after the instant `a` and before every later instant (instants have a fixed
/// width, so `a` followed by any character is): the exclusive end of an interval holding at
/// `a` only.
pub(crate) fn just_after(a: &str) -> String {
    format!("{a}~")
}

/// The function names an answer relies on: (name, the transaction time it was resolved at,
/// none for the answer's own asof) to the function hash it resolved to.
pub(crate) type Names = BTreeMap<(String, Option<String>), String>;

/// An answer: the receipt that answers a cell, and over which asofs it is known to.
#[derive(Debug)]
pub(crate) struct Ans {
    /// The cell id.
    pub(crate) id: String,
    /// The receipt's hash, canonical bytes, value, tx and asof.
    pub(crate) hash: String,
    pub(crate) bytes: Arc<Vec<u8>>,
    pub(crate) value: Scalar,
    pub(crate) tx: String,
    pub(crate) asof: String,
    /// The receipt's licence (an input's, for licence inheritance: SPEC 8.5).
    pub(crate) licence: String,
    /// The answer holds for every asof in `[lo, hi)` (`hi` none: unbounded), given the store
    /// as it was read and the names in `names` resolving as recorded.
    pub(crate) lo: String,
    pub(crate) hi: Option<String>,
    /// Every function name the answer depends on (the cell's inputs, transitively).
    pub(crate) names: Arc<Names>,
    /// The cell.
    pub(crate) cell: Cell,
    /// The inputs this answer rests on, with the receipt and value each gave (for a version
    /// kept or validated across a change, the inputs as of then, not its receipt's).
    pub(crate) inputs: Vec<Use>,
    /// Whether the memo may keep it: not when its value rests on a fault a runtime caught
    /// (which no receipt lists), or on such an answer.
    pub(crate) memo: bool,
}

/// An input an answer rests on.
#[derive(Clone, Debug)]
pub(crate) enum Use {
    /// A cell, read as of the answer's asof, or as of the transaction time `at`.
    Cell { id: String, cell: Cell, hash: String, value: Scalar, at: Option<String> },
    /// A slice.
    Slice(Arc<SliceRead>),
}

/// The ids of the cells read whose changes reach an answer through the dependents index:
/// single cells and the members of slices over computed functions. (A slice over a stored
/// function is matched by its pattern instead: [`input_patterns`].)
fn input_deps(inputs: &[Use]) -> impl Iterator<Item = &String> {
    inputs.iter().flat_map(|u| -> Box<dyn Iterator<Item = &String> + '_> {
        match u {
            Use::Cell { id, .. } => Box::new(std::iter::once(id)),
            Use::Slice(s) if s.computed => Box::new(s.members.iter().map(|m| &m.id)),
            Use::Slice(_) => Box::new(std::iter::empty()),
        }
    })
}

/// The slices over stored functions (and version slices) read.
fn input_patterns(inputs: &[Use]) -> impl Iterator<Item = &Arc<Slice>> {
    inputs.iter().filter_map(|u| match u {
        Use::Slice(s) if !s.computed => Some(&s.slice),
        _ => None,
    })
}

impl Ans {
    /// See [`input_deps`].
    pub(crate) fn deps(&self) -> impl Iterator<Item = &String> {
        input_deps(&self.inputs)
    }

    /// See [`input_patterns`].
    pub(crate) fn patterns(&self) -> impl Iterator<Item = &Arc<Slice>> {
        input_patterns(&self.inputs)
    }
}

/// A cell with no value because its evaluation faulted (SPEC 15.12). A fault is memoised as an
/// answer is: it holds for the asofs `[lo, hi)`, and is dropped when a stored version it rests
/// on is added. It has no receipt, so it is never stored.
#[derive(Debug)]
pub(crate) struct FaultAns {
    /// The cell id.
    pub(crate) id: String,
    pub(crate) fault: Fault,
    /// The instant the fault holds from: the latest of what the evaluation read and relied
    /// on (as of the asof, for an asof-dependent fault).
    pub(crate) known: String,
    /// The fault holds for every asof in `[lo, hi)`, given the store as it was read and the
    /// names in `names` resolving as recorded.
    pub(crate) lo: String,
    pub(crate) hi: Option<String>,
    /// Every function name the fault depends on, transitively.
    pub(crate) names: Arc<Names>,
    /// The inputs that gave a value, as an answer's are.
    pub(crate) inputs: Vec<Use>,
    /// The inputs that faulted (their cell ids; their own dependencies are theirs).
    pub(crate) faulted: Vec<String>,
    /// Whether the memo may keep it.
    pub(crate) memo: bool,
}

impl FaultAns {
    /// The cell ids whose changes reach this fault through the dependents index (see
    /// [`Ans::deps`]).
    pub(crate) fn deps(&self) -> impl Iterator<Item = &String> {
        self.faulted.iter().chain(input_deps(&self.inputs))
    }

    /// The slices over stored functions it read (see [`Ans::patterns`]).
    pub(crate) fn patterns(&self) -> impl Iterator<Item = &Arc<Slice>> {
        input_patterns(&self.inputs)
    }
}

/// A slice read as of an asof: its entry and manifest, and its members.
#[derive(Debug)]
pub(crate) struct SliceRead {
    pub(crate) entry: String,
    pub(crate) manifest: Arc<Vec<u8>>,
    pub(crate) slice: Arc<Slice>,
    pub(crate) fn_name: String,
    pub(crate) fn_hash: String,
    /// Over a computed function (members evaluated by the engine).
    pub(crate) computed: bool,
    /// The members in key order (ascending tx for a version slice).
    pub(crate) members: Vec<MemberRec>,
    /// The members' answers, for a slice over a computed function.
    member_ans: Vec<Arc<Ans>>,
    /// Where the read holds from (the latest member asof, and `to`), and until.
    lo: Option<String>,
    hi: Option<String>,
    /// The latest member tx.
    tx: Option<String>,
    memo: bool,
}

/// A member of a slice read.
#[derive(Clone, Debug)]
pub(crate) struct MemberRec {
    pub(crate) id: String,
    pub(crate) hash: String,
    pub(crate) value: Scalar,
    args: Args,
    tx: String,
    asof: String,
    /// A member a scenario changed: its hash is a scenario's, not a receipt's.
    scoped: bool,
}

impl MemberRec {
    fn input(&self) -> Input {
        Input::new(self.value.clone(), self.hash.clone(), self.tx.clone(), self.asof.clone())
    }
}

/// How a cell ended in a request.
#[derive(Clone, Debug)]
pub(crate) enum Done {
    Value(Arc<Ans>),
    /// No value as of the asof.
    NotCovered,
    /// No value: the evaluation faulted (SPEC 15.12), as a memoised answer: the fault and the
    /// instant it holds from (the latest asof of what the evaluation read, and of the bindings
    /// it relied on).
    Fault(Arc<FaultAns>),
    /// The top cell evaluated without a receipt (`Resolver::evaluation`), with the entries
    /// it read.
    Unsigned(Scalar, Vec<String>),
}

/// What looking a cell up gives.
enum Lookup {
    Ready(Done),
    /// A computed cell not answered yet: it needs a frame.
    Eval(Arc<FunctionDescriptor>),
}

/// A cell to evaluate as of an asof.
#[derive(Clone, Debug)]
pub(crate) struct Pend {
    cell: Cell,
    id: String,
    desc: Arc<FunctionDescriptor>,
    asof: String,
}

/// What a runtime read: a cell (as of the asof, or of `at`), or a slice.
#[derive(Clone, Debug)]
enum Rd {
    Cell { ans: Arc<Ans>, at: Option<String> },
    Slice(Arc<SliceRead>),
}

impl Rd {
    fn entry(&self) -> &str {
        match self {
            Rd::Cell { ans, .. } => &ans.hash,
            Rd::Slice(s) => &s.entry,
        }
    }
}

struct Fresh {
    cell_id: String,
    tx: String,
    hash: String,
    bytes: Arc<Vec<u8>>,
    /// Kept as a computed version (validated by later requests).
    computed: bool,
}

enum State {
    /// Not looked at yet: an answer the memo dropped (a stored version it rested on changed)
    /// is rechecked first; else the store's computed version answering as of the asof.
    Start,
    /// Rechecking a dropped answer: inputs before `idx` gave the same receipt or value.
    Recheck { ans: Arc<Ans>, idx: usize, read: Vec<Rd>, names: Names },
    /// The store's computed version answering as of the asof, to validate; else evaluate.
    FromStore,
    /// Validating that version: inputs before `idx` gave the same receipt or value.
    Validate { cand: Arc<Parsed>, idx: usize, read: Vec<Rd>, names: Names },
    /// Evaluating the function; `cand` is the version that did not validate, if any.
    Eval { cand: Option<Arc<Parsed>> },
}

struct Frame {
    cell: Cell,
    id: String,
    asof: String,
    desc: Arc<FunctionDescriptor>,
    state: State,
    /// Evaluate without signing (the top cell of `Resolver::evaluation`).
    unsigned: bool,
    /// Cells to evaluate before the frame steps again, last first.
    pending: Vec<Pend>,
    /// The fuel of the frame's last suspended attempt: its next one charges at least as much.
    floor: u64,
}

/// One request: its asof, its clock reading, and what it has answered so far.
pub(crate) struct Req<'e> {
    eng: &'e Engine,
    asof: String,
    issued: String,
    /// Names resolved, by instant then name: the function hash and the start of the binding.
    names: HashMap<String, HashMap<String, Option<(String, String)>>>,
    /// Cells answered, by asof then cell id.
    done: HashMap<String, HashMap<String, Done>>,
    /// Slices read, by (asof, slice id).
    slices: HashMap<(String, String), Arc<SliceRead>>,
    fresh: Vec<Fresh>,
    /// Manifests of the slices runtimes read, by entry.
    manifests: BTreeMap<String, Arc<Vec<u8>>>,
    /// The declarations of each publisher host (SPEC 9.2), read from the store once per
    /// request: `None` when the store has no key document for the host, or one without.
    declarations: HashMap<String, Option<Arc<Declarations>>>,
    /// The answers this request made (not taken from the memo), inputs first: for the memo.
    pub(crate) learned: Vec<Entry>,
    budget: Budget,
    /// The overrides of a scenario ([`Engine::whatif`](crate::Engine::whatif)): the request
    /// evaluates in its scope, and writes nothing.
    scope: Option<Arc<Scope>>,
    /// The function names each function name reaches through declared inputs, itself
    /// included, as of the request's asof (see [`Req::dependent`]).
    closures: HashMap<String, Arc<HashSet<String>>>,
}

/// How an input of a version compares as of the asof.
enum Check {
    /// The same: the read, the name it relied on (none for a pinned read) and its function hash.
    Same(Rd, Option<(String, Option<String>)>, String),
    Pending(Vec<Pend>),
    Differs,
}

/// What reading a slice as of an asof gives.
enum SliceOutcome {
    Ready(Arc<SliceRead>),
    Pending(Vec<Pend>),
    /// A member over a computed function has no value (since the instant given).
    Absent(String),
}

/// What a frame's step leads to.
enum Step {
    /// The frame changed state: step it again.
    Again,
    Done(Done),
    Push(Vec<Pend>),
}

/// The facts an answer derives from what it read.
struct Facts {
    tx: Option<String>,
    /// The latest input asof (members, `T` and `to` included).
    latest: String,
    lo: String,
    hi: Option<String>,
    names: Arc<Names>,
    inputs: Vec<Use>,
    memo: bool,
    entries: BTreeSet<String>,
}

/// How an evaluated answer is kept.
struct How {
    /// Asof-dependent: its receipt's asof is the evaluation asof, and it holds there only.
    dependent: bool,
    /// Kept as a computed version, for later requests to validate.
    computed: bool,
    /// Kept in the memo.
    memo: bool,
    /// A further asof candidate: where a fault a read returned holds from, and the latest
    /// time `asof_compare` found at or before the asof.
    known: Option<String>,
}

fn store_err(op: &'static str) -> impl Fn(datumesh_store::Error) -> EvalError {
    move |e| EvalError::Store(StoreError::new(op, e))
}

impl<'e> Req<'e> {
    pub(crate) fn new(eng: &'e Engine, asof: &str, issued: &str) -> Req<'e> {
        Req {
            eng,
            asof: asof.to_string(),
            issued: issued.to_string(),
            names: HashMap::new(),
            done: HashMap::new(),
            slices: HashMap::new(),
            fresh: Vec::new(),
            manifests: BTreeMap::new(),
            declarations: HashMap::new(),
            learned: Vec::new(),
            budget: Budget::with_work(eng.fuel, eng.work_limit),
            scope: None,
            closures: HashMap::new(),
        }
    }

    /// A request in the scope of a scenario, sharing `budget` (a solve's iterations share one).
    pub(crate) fn scoped(eng: &'e Engine, asof: &str, issued: &str, scope: Arc<Scope>, budget: &Budget) -> Req<'e> {
        let mut req = Req::new(eng, asof, issued);
        req.budget = budget.share();
        req.scope = Some(scope);
        req
    }

    /// Whether the cell's value may change under the scenario's overrides: a computed cell
    /// whose function reaches an overridden function through its declared inputs (as of the
    /// request's asof). Such a cell is evaluated in the scope and never answered from the memo
    /// or the store. Always false outside a scenario, and for a stored cell (its own override
    /// is the only change it can have).
    fn dependent(&mut self, cell: &Cell, desc: &FunctionDescriptor) -> Result<bool, EvalError> {
        let Some(scope) = self.scope.clone() else { return Ok(false) };
        if desc.lang == STORED || scope.affected.is_empty() {
            return Ok(false);
        }
        let closure = self.closure(&cell.function_name())?;
        Ok(closure.iter().any(|n| scope.affected.contains(n)))
    }

    /// The function names `fn_name` reaches through declared inputs (itself included), each
    /// resolved as of the request's asof; cached per request.
    fn closure(&mut self, fn_name: &str) -> Result<Arc<HashSet<String>>, EvalError> {
        if let Some(c) = self.closures.get(fn_name) {
            return Ok(c.clone());
        }
        let asof = self.asof.clone();
        let mut seen: HashSet<String> = HashSet::new();
        let mut todo = vec![fn_name.to_string()];
        while let Some(name) = todo.pop() {
            if !seen.insert(name.clone()) {
                continue;
            }
            let Some(hash) = self.resolve(&name, &asof)? else { continue };
            let Some(d) = self.eng.descriptor_of(&hash).map_err(eng_eval)? else { continue };
            todo.extend(d.inputs.iter().cloned());
        }
        let closure = Arc::new(seen);
        self.closures.insert(fn_name.to_string(), closure.clone());
        Ok(closure)
    }

    /// The scenario's override of cell `id`, if any.
    fn overridden(&self, id: &str) -> Option<(Cell, Scalar)> {
        self.scope.as_ref().and_then(|s| s.values.get(id).cloned())
    }

    /// The answer of an overridden cell: its value, unsigned, valid from the asof on.
    fn override_answer(&self, cell: &Cell, value: Scalar, asof: &str) -> Arc<Ans> {
        let id = cell.id();
        Arc::new(Ans {
            hash: scoped_hash(&id, &value),
            id,
            bytes: Arc::new(Vec::new()),
            value,
            tx: MIN_INSTANT.to_string(),
            asof: asof.to_string(),
            licence: self.eng.licence.clone(),
            lo: asof.to_string(),
            hi: None,
            names: Arc::new(Names::new()),
            cell: cell.clone(),
            inputs: Vec::new(),
            memo: false,
        })
    }

    /// The binding `fn_name` has as of `at`, its function hash and when it began (one query
    /// per name and instant per request).
    fn binding(&mut self, fn_name: &str, at: &str) -> Result<Option<(String, String)>, EvalError> {
        Ok(self.binding_ref(fn_name, at)?.clone())
    }

    /// [`binding`](Req::binding), borrowed (no allocation when it is known).
    fn binding_ref(&mut self, fn_name: &str, at: &str) -> Result<&Option<(String, String)>, EvalError> {
        let known = self.names.get(at).is_some_and(|m| m.contains_key(fn_name));
        if !known {
            let b = self
                .eng
                .store
                .resolve_binding(fn_name, at)
                .map_err(store_err("resolve_binding"))?
                .map(|b| (b.fn_hash, b.tx));
            self.names.entry(at.to_string()).or_default().insert(fn_name.to_string(), b);
        }
        Ok(self.names.get(at).and_then(|m| m.get(fn_name)).unwrap_or(&None))
    }

    /// The function hash `fn_name` resolves to as of `at`.
    fn resolve(&mut self, fn_name: &str, at: &str) -> Result<Option<String>, EvalError> {
        Ok(self.binding_ref(fn_name, at)?.as_ref().map(|(h, _)| h.clone()))
    }

    /// Whether `fn_name` resolves to `hash` as of `at`.
    fn resolves_to(&mut self, fn_name: &str, at: &str, hash: &str) -> Result<bool, EvalError> {
        Ok(self.binding_ref(fn_name, at)?.as_ref().is_some_and(|(h, _)| h == hash))
    }

    /// Whether every name of `names` still resolves as recorded, as of `asof` (or its own
    /// transaction time).
    fn names_hold(&mut self, names: &Names, asof: &str) -> Result<bool, EvalError> {
        for ((name, at), hash) in names {
            if !self.resolves_to(name, at.as_deref().unwrap_or(asof), hash)? {
                return Ok(false);
            }
        }
        Ok(true)
    }

    fn set_done(&mut self, asof: &str, id: &str, d: Done) {
        match self.done.get_mut(asof) {
            Some(m) => {
                m.insert(id.to_string(), d);
            }
            None => {
                self.done.entry(asof.to_string()).or_default().insert(id.to_string(), d);
            }
        }
    }

    fn done_at(&self, asof: &str, id: &str) -> Option<&Done> {
        self.done.get(asof).and_then(|m| m.get(id))
    }

    /// A cell's answer as of `asof` if it can be had without evaluating a function.
    fn lookup(
        &mut self,
        cell: &Cell,
        id: &str,
        desc: Arc<FunctionDescriptor>,
        asof: &str,
    ) -> Result<Lookup, EvalError> {
        if let Some(d) = self.done_at(asof, id) {
            return Ok(Lookup::Ready(d.clone()));
        }
        if let Some((c, value)) = self.overridden(id) {
            let d = Done::Value(self.override_answer(&c, value, asof));
            self.set_done(asof, id, d.clone());
            return Ok(Lookup::Ready(d));
        }
        if self.dependent(cell, &desc)? {
            return Ok(Lookup::Eval(desc));
        }
        if let Some(d) = self.memo_done(id, asof)? {
            self.set_done(asof, id, d.clone());
            return Ok(Lookup::Ready(d));
        }
        if desc.lang == STORED {
            let d = self.stored(cell, id, asof)?;
            self.set_done(asof, id, d.clone());
            return Ok(Lookup::Ready(d));
        }
        Ok(Lookup::Eval(desc))
    }

    /// A memoised answer holding at `asof` whose names still resolve as recorded.
    pub(crate) fn memo_hit(&mut self, id: &str, asof: &str) -> Result<Option<Arc<Ans>>, EvalError> {
        let Some(a) = self.eng.memo.get(id, asof) else { return Ok(None) };
        if !self.names_hold(&a.names, asof)? {
            return Ok(None);
        }
        Ok(Some(a))
    }

    /// A memoised fault holding at `asof` whose names still resolve as recorded.
    fn memo_fault_hit(&mut self, id: &str, asof: &str) -> Result<Option<Arc<FaultAns>>, EvalError> {
        let Some(f) = self.eng.memo.get_fault(id, asof) else { return Ok(None) };
        if !self.names_hold(&f.names, asof)? {
            return Ok(None);
        }
        Ok(Some(f))
    }

    /// The memoised answer of a cell as of `asof` (a value or a fault), if any.
    pub(crate) fn memo_done(&mut self, id: &str, asof: &str) -> Result<Option<Done>, EvalError> {
        if let Some(a) = self.memo_hit(id, asof)? {
            return Ok(Some(Done::Value(a)));
        }
        Ok(self.memo_fault_hit(id, asof)?.map(Done::Fault))
    }

    /// The request's asof.
    pub(crate) fn asof(&self) -> String {
        self.asof.clone()
    }

    /// A stored cell's answer: the version answering as of `asof`.
    pub(crate) fn stored(&mut self, cell: &Cell, id: &str, asof: &str) -> Result<Done, EvalError> {
        let found = self.eng.store.stored_asof_with_receipt(id, asof).map_err(store_err("stored_asof"))?;
        let Some((v, bytes)) = found else { return Ok(Done::NotCovered) };
        let p = self.eng.parsed(&v.receipt_hash, &bytes).map_err(EvalError::Store)?;
        // The version answers from its asof (never earlier than its tx) until a version later
        // by tx answers: from that one's asof. Only the memo needs to know.
        let hi = if self.eng.memo.enabled() {
            let later = self.eng.store.versions(id, &v.tx, MAX_INSTANT).map_err(store_err("versions"))?;
            later.into_iter().filter(|w| w.tx > v.tx).map(|w| w.asof).min()
        } else {
            None
        };
        let r = &p.receipt;
        let a = Arc::new(Ans {
            id: id.to_string(),
            hash: v.receipt_hash.clone(),
            bytes: p.bytes.clone(),
            value: r.value().clone(),
            tx: r.tx().to_string(),
            asof: r.asof().to_string(),
            licence: r.licence().to_string(),
            lo: r.asof().to_string(),
            hi,
            names: Arc::new(Names::new()),
            cell: cell.clone(),
            inputs: Vec::new(),
            memo: true,
        });
        self.learned.push(Entry::Value(a.clone()));
        Ok(Done::Value(a))
    }

    /// Answers `cell` (a computed cell with descriptor `desc`) as of the request's asof: its
    /// frames run as one root, and the roots they split off run first (see
    /// [`run_roots`](Req::run_roots)).
    pub(crate) fn run(
        &mut self,
        cell: Cell,
        desc: Arc<FunctionDescriptor>,
        unsigned: bool,
    ) -> Result<Done, EngineError> {
        let id = cell.id();
        let asof = self.asof.clone();
        self.run_roots(vec![Pend { cell, id: id.clone(), desc, asof: asof.clone() }], unsigned)?;
        self.done_at(&asof, &id)
            .cloned()
            .ok_or_else(|| EngineError::Runtime { cell_id: id, message: "no answer".into() })
    }

    /// Runs the roots, the last first. A root whose frames would pass the depth limit is set
    /// aside, and the pending cell that did not fit becomes a root of its own, run first
    /// (trampolining, Build 2 E4). Nothing is lost: the answers the set-aside frames finished
    /// are in the request, and the root runs again and finds them. Fuel and work are charged
    /// throughout, so an unbounded recursion ends in `BudgetExceeded` or `WorkExceeded`.
    fn run_roots(&mut self, mut roots: Vec<Pend>, unsigned: bool) -> Result<(), EngineError> {
        while let Some(root) = roots.last().cloned() {
            if self.done_at(&root.asof, &root.id).is_some() {
                roots.pop();
                continue;
            }
            let top = unsigned && roots.len() == 1;
            match self.run_stack(root, top)? {
                Ended::Done => {
                    roots.pop();
                }
                Ended::Split(p) => roots.push(p),
            }
        }
        Ok(())
    }

    /// One root's frames, from the root to its answer on an explicit stack. Instead of pushing
    /// a frame past the depth limit it returns [`Ended::Split`] with the pending cell: the
    /// frames are dropped, and their suspended attempts' fuel released.
    fn run_stack(&mut self, root: Pend, unsigned: bool) -> Result<Ended, EngineError> {
        let key = |asof: &str, id: &str| format!("{asof} {id}");
        let state = if unsigned { State::Eval { cand: None } } else { State::Start };
        let mut stack = vec![Frame {
            cell: root.cell,
            id: root.id.clone(),
            asof: root.asof.clone(),
            desc: root.desc,
            state,
            unsigned,
            pending: Vec::new(),
            floor: 0,
        }];
        let mut on_stack: HashSet<String> = HashSet::from([key(&root.asof, &root.id)]);
        loop {
            let Some(top) = stack.last_mut() else { return Ok(Ended::Done) };
            if let Some(p) = top.pending.pop() {
                if self.done_at(&p.asof, &p.id).is_some() {
                    continue;
                }
                if on_stack.contains(&key(&p.asof, &p.id)) {
                    // The path: the frames from the cell that reappeared, then it again.
                    let at = stack.iter().position(|f| f.id == p.id && f.asof == p.asof).unwrap_or(0);
                    let mut path: Vec<String> = stack[at..].iter().map(|f| canonical_call(&f.cell)).collect();
                    path.push(canonical_call(&p.cell));
                    return Err(EngineError::Cycle { path });
                }
                if stack.len() >= self.eng.depth_limit {
                    for f in &stack {
                        self.budget.release(f.floor);
                    }
                    return Ok(Ended::Split(p));
                }
                on_stack.insert(key(&p.asof, &p.id));
                stack.push(Frame {
                    cell: p.cell,
                    id: p.id,
                    asof: p.asof,
                    desc: p.desc,
                    state: State::Start,
                    unsigned: false,
                    pending: Vec::new(),
                    floor: 0,
                });
                continue;
            }
            let step = match top.state {
                State::Start => self.start_frame(top)?,
                State::Recheck { .. } => self.recheck_frame(top)?,
                State::FromStore => self.store_frame(top)?,
                State::Validate { .. } => self.validate_frame(top)?,
                State::Eval { .. } => self.eval_frame(top)?,
            };
            match step {
                Step::Again => {}
                Step::Done(d) => {
                    if let Some(f) = stack.pop() {
                        on_stack.remove(&key(&f.asof, &f.id));
                        match &d {
                            Done::Value(a) => self.learned.push(Entry::Value(a.clone())),
                            // A member without a value ends a computed slice's read: the members
                            // after it (in key order) are not evaluated.
                            _ => {
                                if let Done::Fault(fa) = &d {
                                    self.learned.push(Entry::Fault(fa.clone()));
                                }
                                if let Some(parent) = stack.last_mut() {
                                    parent.pending.clear();
                                }
                            }
                        }
                        self.set_done(&f.asof, &f.id, d);
                    }
                }
                Step::Push(mut list) => {
                    list.reverse();
                    if let Some(top) = stack.last_mut() {
                        top.pending = list;
                    }
                }
            }
        }
    }

    /// A scenario's answer for `cell` (its descriptor `desc`) as of the request's asof. Before
    /// the cell runs, the dependent cells below it are evaluated in dependency order (from the
    /// receipts of its latest computed version), so that it reads them without suspending.
    pub(crate) fn scope_run(&mut self, cell: Cell, desc: Arc<FunctionDescriptor>) -> Result<Done, EngineError> {
        let id = cell.id();
        let asof = self.asof.clone();
        for p in self.scope_order(&cell)? {
            if self.done_at(&p.asof, &p.id).is_some() {
                continue;
            }
            // A cycle here is an artifact of the order (the receipts were made at other
            // instants): the cell is then run as it is, and reports the cycle if it has one.
            match self.run_roots(vec![p], false) {
                Ok(()) | Err(EngineError::Cycle { .. }) => {}
                Err(e) => return Err(e),
            }
        }
        match self.lookup(&cell, &id, desc.clone(), &asof).map_err(|e| engine_error(e, &id))? {
            Lookup::Ready(d) => Ok(d),
            Lookup::Eval(desc) => self.run(cell, desc, false),
        }
    }

    /// The dependent computed cells in the lineage of `top`'s latest computed version, inputs
    /// first (a post-order walk of the receipts, which are acyclic).
    fn scope_order(&mut self, top: &Cell) -> Result<Vec<Pend>, EngineError> {
        let asof = self.asof.clone();
        let Some((v, bytes)) = self
            .eng
            .store
            .computed_asof_with_receipt(&top.id(), &asof)
            .map_err(|e| StoreError::new("computed_asof", e))?
        else {
            return Ok(Vec::new());
        };
        let root = self.eng.parsed(&v.receipt_hash, &bytes)?;
        let mut seen: HashSet<String> = HashSet::from([top.id()]);
        let mut out = Vec::new();
        let mut stack: Vec<(Arc<Parsed>, Arc<FunctionDescriptor>, bool)> = Vec::new();
        for c in self.lineage(&root)? {
            if let Some(d) = self.scope_child(&c)? {
                stack.push((c, d, false));
            }
        }
        while let Some((r, d, expanded)) = stack.pop() {
            let cell = r.receipt.cell().clone();
            let id = cell.id();
            if expanded {
                out.push(Pend { cell, id, desc: d, asof: asof.clone() });
                continue;
            }
            if !seen.insert(id) {
                continue;
            }
            stack.push((r.clone(), d, true));
            for c in self.lineage(&r)? {
                if let Some(cd) = self.scope_child(&c)? {
                    stack.push((c, cd, false));
                }
            }
        }
        Ok(out)
    }

    /// The descriptor of `r`'s cell, when that cell is a dependent computed one.
    fn scope_child(&mut self, r: &Parsed) -> Result<Option<Arc<FunctionDescriptor>>, EngineError> {
        let cell = r.receipt.cell();
        let Some(d) = self.eng.descriptor_of(&cell.fn_hash)? else { return Ok(None) };
        let dependent = self.dependent(cell, &d).map_err(|e| engine_error(e, &cell.id()))?;
        Ok(dependent.then_some(d))
    }

    /// The receipts a receipt reads: its inputs, and for a slice entry its members' receipts.
    fn lineage(&mut self, r: &Parsed) -> Result<Vec<Arc<Parsed>>, EngineError> {
        let mut out = Vec::new();
        for entry in r.receipt.inputs() {
            if entry.starts_with("slice:") {
                let Some(bytes) = self.eng.store.manifest(entry) else { continue };
                let m = Manifest::from_json(&bytes).map_err(EngineError::Invalid)?;
                for h in m.members() {
                    if let Some(p) = self.receipt(h)? {
                        out.push(p);
                    }
                }
            } else if let Some(p) = self.receipt(entry)? {
                out.push(p);
            }
        }
        Ok(out)
    }

    /// A dropped answer to recheck, if the memo has one holding at the asof.
    fn start_frame(&mut self, f: &mut Frame) -> Result<Step, EngineError> {
        if self.dependent(&f.cell, &f.desc).map_err(|e| engine_error(e, &f.id))? {
            f.state = State::Eval { cand: None };
            return Ok(Step::Again);
        }
        f.state = match self.eng.memo.stale(&f.id, &f.asof) {
            Some(ans) => State::Recheck { ans, idx: 0, read: Vec::new(), names: Names::new() },
            None => State::FromStore,
        };
        Ok(Step::Again)
    }

    /// Rechecks a dropped answer as a version is validated (same receipt or value from each
    /// input it rested on, names resolving the same), from memory: no store query unless an
    /// input needs one. Differing: the store's version is looked at as usual.
    fn recheck_frame(&mut self, f: &mut Frame) -> Result<Step, EngineError> {
        let State::Recheck { ans, idx, read, names } = &mut f.state else { return Ok(Step::Again) };
        let ans = ans.clone();
        let asof = f.asof.clone();
        while let Some(u) = ans.inputs.get(*idx) {
            let check = match u {
                Use::Cell { id, cell, hash, value, at } => {
                    self.check_use(cell, id, hash, value, at.as_deref(), &asof, &ans.asof)?
                }
                Use::Slice(old) => {
                    let old_members: Vec<(String, Scalar)> =
                        old.members.iter().map(|m| (m.id.clone(), m.value.clone())).collect();
                    self.check_slice(&old.entry, &old.slice, Some(&old_members), &asof)?
                }
            };
            match check {
                Check::Same(rd, name, hash) => {
                    read.push(rd);
                    if let Some(name) = name {
                        names.insert(name, hash);
                    }
                    *idx += 1;
                }
                Check::Pending(list) => return Ok(Step::Push(list)),
                Check::Differs => {
                    f.state = State::FromStore;
                    return Ok(Step::Again);
                }
            }
        }
        let read = std::mem::take(read);
        let names = std::mem::take(names);
        // Licence inheritance (SPEC 8.5): kept only while the rule allows its licence.
        let parsed = self.eng.parsed(&ans.hash, &ans.bytes)?;
        if !self.reusable(f, &read, &parsed.receipt)? {
            f.state = State::FromStore;
            return Ok(Step::Again);
        }
        let facts = self.facts(&read, names);
        Ok(Step::Done(Done::Value(self.kept(
            f,
            facts,
            &ans.hash,
            &ans.bytes,
            &ans.value,
            (&ans.tx, &ans.asof),
            &ans.licence,
        ))))
    }

    /// The answer of a version kept: rechecked or validated.
    #[allow(clippy::too_many_arguments)]
    fn kept(
        &self,
        f: &Frame,
        facts: Facts,
        hash: &str,
        bytes: &Arc<Vec<u8>>,
        value: &Scalar,
        (tx, asof): (&str, &str),
        licence: &str,
    ) -> Arc<Ans> {
        let (lo, hi) = if self.asof_dependent(f) {
            (f.asof.clone(), Some(just_after(&f.asof)))
        } else {
            (facts.lo.as_str().max(asof).to_string(), facts.hi.clone())
        };
        Arc::new(Ans {
            id: f.id.clone(),
            hash: hash.to_string(),
            bytes: bytes.clone(),
            value: value.clone(),
            tx: tx.to_string(),
            asof: asof.to_string(),
            licence: licence.to_string(),
            lo,
            hi,
            names: facts.names,
            cell: f.cell.clone(),
            inputs: facts.inputs,
            memo: facts.memo,
        })
    }

    /// Whether the frame's function is declared asof-dependent by its runtime.
    fn asof_dependent(&self, f: &Frame) -> bool {
        self.eng.asof_dependent(&f.cell.fn_hash, &f.desc)
    }

    /// The store's computed version answering as of the asof, to validate; else evaluate.
    /// An asof-dependent function's version is reused only as of its own asof.
    fn store_frame(&mut self, f: &mut Frame) -> Result<Step, EngineError> {
        let found = self
            .eng
            .store
            .computed_asof_with_receipt(&f.id, &f.asof)
            .map_err(|e| StoreError::new("computed_asof", e))?;
        f.state = match found {
            Some((v, bytes)) => {
                let cand = self.eng.parsed(&v.receipt_hash, &bytes)?;
                if self.asof_dependent(f) && cand.receipt.asof() != f.asof {
                    State::Eval { cand: None }
                } else {
                    State::Validate { cand, idx: 0, read: Vec::new(), names: Names::new() }
                }
            }
            None => State::Eval { cand: None },
        };
        Ok(Step::Again)
    }

    /// Validates the candidate version: valid as of the asof when each input, its name
    /// resolving to the same function, gives the same receipt or the same value (early
    /// cutoff), and each slice entry the same members hash or the same members' values. An
    /// input not answered yet is pushed first.
    fn validate_frame(&mut self, f: &mut Frame) -> Result<Step, EngineError> {
        let State::Validate { cand, idx, read, names } = &mut f.state else { return Ok(Step::Again) };
        let cand = cand.clone();
        let asof = f.asof.clone();
        while let Some(h) = cand.receipt.inputs().get(*idx) {
            let check = if h.starts_with("slice:") {
                self.check_entry(h, &asof)?
            } else {
                self.check_input(h, &asof, cand.receipt.asof())?
            };
            match check {
                Check::Same(rd, name, hash) => {
                    read.push(rd);
                    if let Some(name) = name {
                        names.insert(name, hash);
                    }
                    *idx += 1;
                }
                Check::Pending(list) => return Ok(Step::Push(list)),
                Check::Differs => {
                    f.state = State::Eval { cand: Some(cand) };
                    return Ok(Step::Again);
                }
            }
        }
        let read = std::mem::take(read);
        let names = std::mem::take(names);
        // Licence inheritance (SPEC 8.5): a version whose licence the rule no longer gives
        // (a receipt signed before 0.4.1, an input now under another licence) is evaluated
        // again, then signed again or refused.
        if !self.reusable(f, &read, &cand.receipt)? {
            f.state = State::Eval { cand: Some(cand) };
            return Ok(Step::Again);
        }
        let facts = self.facts(&read, names);
        let r = &cand.receipt;
        Ok(Step::Done(Done::Value(self.kept(
            f,
            facts,
            r.hash(),
            &cand.bytes,
            r.value(),
            (r.tx(), r.asof()),
            r.licence(),
        ))))
    }

    /// Whether input receipt `h` of a version made as of `reader` still answers as of the asof.
    fn check_input(&mut self, h: &str, asof: &str, reader: &str) -> Result<Check, EngineError> {
        let Some(input) = self.receipt(h)? else { return Ok(Check::Differs) };
        let r = &input.receipt;
        self.check_use(r.cell(), &r.cell().id(), h, r.value(), None, asof, reader)
    }

    /// Whether `cell`, its name resolving to its function, gives receipt `h` or value `value`
    /// as of `at` (a transaction-time read), else as of `asof`. `reader` is the asof of the
    /// answer that read it. A read of a name resolved the name as of the reader's asof (the
    /// binding then starts no later than it: see [`Req::binding_asof`]); a pinned read
    /// (`read_pinned`, SPEC 16.3) did not, relies on no binding, and is checked by its hash
    /// alone, with no name recorded.
    #[allow(clippy::too_many_arguments)]
    fn check_use(
        &mut self,
        cell: &Cell,
        id: &str,
        h: &str,
        value: &Scalar,
        at: Option<&str>,
        asof: &str,
        reader: &str,
    ) -> Result<Check, EngineError> {
        let when = at.unwrap_or(asof).to_string();
        let name = cell.function_name();
        let fail = |e: EvalError| engine_error(e, id);
        // Pinned: the name did not resolve to the hash as of the reader's asof. The binding
        // current as of `when` answers for the reader's asof too when it began no later (no
        // binding starts in between): the store is asked only otherwise.
        let pinned = match at {
            Some(_) => false,
            None => {
                let current = self.binding_ref(&name, &when).map_err(fail)?.clone();
                let at_reader = match current {
                    None => None,
                    Some((bound, start)) if start.as_str() <= reader => Some(bound),
                    Some(_) => self.resolve(&name, reader).map_err(fail)?,
                };
                at_reader.as_deref() != Some(cell.fn_hash.as_str())
            }
        };
        if !pinned && !self.resolves_to(&name, &when, &cell.fn_hash).map_err(fail)? {
            return Ok(Check::Differs);
        }
        self.budget.work(1).map_err(|e| engine_error(e, id))?;
        let Some(desc) = self.eng.descriptor_of(&cell.fn_hash)? else { return Ok(Check::Differs) };
        match self.lookup(cell, id, desc, &when).map_err(|e| engine_error(e, id))? {
            Lookup::Ready(Done::Value(a)) if a.hash == h || &a.value == value => Ok(Check::Same(
                Rd::Cell { ans: a, at: at.map(str::to_string) },
                (!pinned).then(|| (name, at.map(str::to_string))),
                cell.fn_hash.clone(),
            )),
            Lookup::Ready(_) => Ok(Check::Differs),
            Lookup::Eval(desc) => {
                Ok(Check::Pending(vec![Pend { cell: cell.clone(), id: id.to_string(), desc, asof: when }]))
            }
        }
    }

    /// Whether the slice entry `entry` of a version (its manifest from the store) has the
    /// same members, or members with the same values, as of `asof`.
    fn check_entry(&mut self, entry: &str, asof: &str) -> Result<Check, EngineError> {
        let Some(bytes) = self.eng.store.manifest(entry) else { return Ok(Check::Differs) };
        let Ok(m) = Manifest::from_json(&bytes) else { return Ok(Check::Differs) };
        let slice = Arc::new(m.slice().clone());
        let check = self.check_slice(entry, &slice, None, asof)?;
        let Check::Same(Rd::Slice(now), name, hash) = check else { return Ok(check) };
        if now.entry == entry {
            return Ok(Check::Same(Rd::Slice(now), name, hash));
        }
        // Early cutoff: the same cells with the same values. The old members not among the
        // new ones are read from their receipts.
        let by_hash: HashMap<&str, &MemberRec> = now.members.iter().map(|x| (x.hash.as_str(), x)).collect();
        let mut old: Vec<(String, Scalar)> = Vec::with_capacity(m.members().len());
        for h in m.members() {
            match by_hash.get(h.as_str()) {
                Some(x) => old.push((x.id.clone(), x.value.clone())),
                None => match self.receipt(h)? {
                    Some(p) => old.push((p.receipt.cell().id(), p.receipt.value().clone())),
                    None => return Ok(Check::Differs),
                },
            }
        }
        Ok(if same_members(&old, &now) { Check::Same(Rd::Slice(now), name, hash) } else { Check::Differs })
    }

    /// Reads `slice` again as of `asof` (its name resolving to its function). With `old`
    /// (the old members' cell ids and values) it is the same when the entry is, or when the
    /// cells and values are; without, the caller compares.
    fn check_slice(
        &mut self,
        entry: &str,
        slice: &Arc<Slice>,
        old: Option<&[(String, Scalar)]>,
        asof: &str,
    ) -> Result<Check, EngineError> {
        let fail = |e: EvalError| engine_error(e, entry);
        let name = slice.function_name();
        if !self.resolves_to(&name, asof, slice.fn_hash()).map_err(fail)? {
            return Ok(Check::Differs);
        }
        let Some(desc) = self.eng.descriptor_of(slice.fn_hash())? else { return Ok(Check::Differs) };
        let Some(spec) = spec_of(slice) else { return Ok(Check::Differs) };
        let now = match self.slice_at(&name, slice.clone(), desc, spec, asof).map_err(fail)? {
            SliceOutcome::Ready(s) => s,
            SliceOutcome::Pending(list) => return Ok(Check::Pending(list)),
            SliceOutcome::Absent(_) => return Ok(Check::Differs),
        };
        self.budget.work(1 + now.members.len() as u64).map_err(fail)?;
        let same = match old {
            None => true,
            Some(o) => now.entry == entry || same_members(o, &now),
        };
        let fn_hash = slice.fn_hash().to_string();
        Ok(if same { Check::Same(Rd::Slice(now), Some((name, None)), fn_hash) } else { Check::Differs })
    }

    /// The slice `slice` of function `fn_name` (descriptor `desc`) as of `asof`: for a
    /// stored function its members from the store; for a computed one the cells of its
    /// extent, each answered (pending when one is not yet); a version slice's versions.
    fn slice_at(
        &mut self,
        fn_name: &str,
        slice: Arc<Slice>,
        desc: Arc<FunctionDescriptor>,
        spec: Spec,
        asof: &str,
    ) -> Result<SliceOutcome, EvalError> {
        let key = (asof.to_string(), slice.id().to_string());
        if let Some(s) = self.slices.get(&key) {
            return Ok(SliceOutcome::Ready(s.clone()));
        }
        let fn_hash = slice.fn_hash().to_string();
        let computed = desc.lang != STORED;
        let mut members = Vec::new();
        let mut member_ans = Vec::new();
        let (lo, hi, memo);
        match (&spec, computed) {
            (Spec::Cells { pattern }, true) => {
                let cells = extent_cells(fn_name, &fn_hash, &desc, pattern).map_err(EvalError::InvalidInput)?;
                let mut pending = Vec::new();
                let mut absent: Option<String> = None;
                // In key order, up to the first member without a value (SPEC 15.13, 15.12):
                // members after it are neither evaluated nor charged. Members not answered yet
                // before it are evaluated first (in key order; the run stops pushing them at
                // the first that has no value).
                for cell in cells {
                    let id = cell.id();
                    match self.lookup(&cell, &id, desc.clone(), asof)? {
                        Lookup::Ready(Done::Value(a)) => member_ans.push(a),
                        Lookup::Ready(Done::Fault(fa)) => {
                            absent = Some(fa.known.clone());
                            break;
                        }
                        Lookup::Ready(_) => {
                            absent = Some(MIN_INSTANT.to_string());
                            break;
                        }
                        Lookup::Eval(d) => pending.push(Pend { cell, id, desc: d, asof: asof.to_string() }),
                    }
                }
                if !pending.is_empty() {
                    return Ok(SliceOutcome::Pending(pending));
                }
                if let Some(known) = absent {
                    return Ok(SliceOutcome::Absent(known));
                }
                for a in &member_ans {
                    members.push(MemberRec {
                        id: a.id.clone(),
                        hash: a.hash.clone(),
                        value: a.value.clone(),
                        args: a.cell.args.clone(),
                        tx: a.tx.clone(),
                        asof: a.asof.clone(),
                        scoped: false,
                    });
                }
                lo = member_ans.iter().map(|a| a.lo.clone()).max();
                hi = member_ans.iter().filter_map(|a| a.hi.clone()).min();
                memo = member_ans.iter().all(|a| a.memo);
            }
            (_, true) => {
                return Err(EvalError::InvalidInput(CoreError::new(
                    ErrorCode::Slice,
                    "$",
                    "versions() of a computed function: versions are of stored cells",
                )))
            }
            (Spec::Cells { .. } | Spec::Versions { .. }, false) => {
                // Values, hashes, tx and asof from the store's columns and its typed-argument
                // index (store 0.3.2): no receipt bytes; the receipts are not needed to read a
                // slice, only to sign what is read.
                let found = self.eng.store.slice_values(&slice, asof).map_err(store_err("slice_values"))?;
                for v in found {
                    members.push(MemberRec {
                        id: v.version.cell_id,
                        hash: v.version.receipt_hash,
                        value: v.value,
                        args: v.cell.args,
                        tx: v.version.tx,
                        asof: v.version.asof,
                        scoped: false,
                    });
                }
                if let Some(scope) = self.scope.clone() {
                    apply_scope(&scope, &slice, asof, &mut members);
                }
                // Holding past the asof: when no version of a matching cell answers later, else
                // at the asof only. Versions stored later are the dirty set's.
                hi = if self.eng.memo.enabled()
                    && self.eng.store.slice_changed_after(&slice, asof).map_err(store_err("slice_changed_after"))?
                {
                    Some(just_after(asof))
                } else {
                    None
                };
                let to = match &spec {
                    Spec::Versions { to, .. } => Some(to.clone()),
                    Spec::Cells { .. } => None,
                };
                lo = members.iter().map(|m| m.asof.clone()).chain(to).max();
                memo = true;
            }
        }
        let mut hashes: Vec<String> = members.iter().map(|m| m.hash.clone()).collect();
        hashes.sort();
        hashes.dedup();
        let manifest = Manifest::new((*slice).clone(), hashes).map_err(EvalError::InvalidInput)?;
        let s = Arc::new(SliceRead {
            entry: manifest.entry(),
            manifest: Arc::new(manifest.canonical()),
            fn_name: fn_name.to_string(),
            fn_hash,
            computed,
            tx: members.iter().map(|m| m.tx.clone()).max(),
            members,
            member_ans,
            lo,
            hi,
            memo,
            slice,
        });
        self.slices.insert(key, s.clone());
        Ok(SliceOutcome::Ready(s))
    }

    /// A stored receipt by hash, parsed (cached).
    fn receipt(&self, hash: &str) -> Result<Option<Arc<Parsed>>, EngineError> {
        if let Some(p) = self.eng.receipts.get(hash) {
            return Ok(Some(p));
        }
        match self.eng.store.receipt(hash) {
            Some(bytes) => Ok(Some(self.eng.parsed(hash, &bytes)?)),
            None => Ok(None),
        }
    }

    /// What answers resting on `read` derive: tx, the latest input asof, the interval, names,
    /// the inputs and whether the memo may keep them.
    fn facts(&self, read: &[Rd], own: Names) -> Facts {
        fn raise(slot: &mut String, v: &str) {
            if v > slot.as_str() {
                *slot = v.to_string();
            }
        }
        fn lower(hi: &mut Option<String>, v: &Option<String>) {
            if let Some(v) = v {
                if hi.as_ref().is_none_or(|h| v < h) {
                    *hi = Some(v.clone());
                }
            }
        }
        let mut tx: Option<String> = None;
        let mut latest = MIN_INSTANT.to_string();
        let mut lo = MIN_INSTANT.to_string();
        let mut hi: Option<String> = None;
        // The names, sharing an input's map when nothing is added to it.
        let mut names: Option<Arc<Names>> = None;
        let add = |names: &mut Option<Arc<Names>>, from: &Arc<Names>| match names {
            None => *names = Some(from.clone()),
            Some(acc) => {
                if from.keys().any(|k| !acc.contains_key(k)) {
                    let m = Arc::make_mut(acc);
                    for (k, v) in from.iter() {
                        m.entry(k.clone()).or_insert_with(|| v.clone());
                    }
                }
            }
        };
        let mut extra = Names::new();
        let mut inputs = Vec::new();
        let mut memo = true;
        let mut entries = BTreeSet::new();
        for rd in read {
            entries.insert(rd.entry().to_string());
            match rd {
                Rd::Cell { ans, at } => {
                    if tx.as_ref().is_none_or(|t| ans.tx > *t) {
                        tx = Some(ans.tx.clone());
                    }
                    memo &= ans.memo;
                    match at {
                        None => {
                            raise(&mut latest, &ans.lo);
                            raise(&mut lo, &ans.lo);
                            lower(&mut hi, &ans.hi);
                            add(&mut names, &ans.names);
                        }
                        Some(t) => {
                            raise(&mut latest, t);
                            raise(&mut latest, &ans.asof);
                            for ((n, a), v) in ans.names.iter() {
                                let at = a.clone().unwrap_or_else(|| t.clone());
                                extra.entry((n.clone(), Some(at))).or_insert_with(|| v.clone());
                            }
                        }
                    }
                    inputs.push(Use::Cell {
                        id: ans.id.clone(),
                        cell: ans.cell.clone(),
                        hash: ans.hash.clone(),
                        value: ans.value.clone(),
                        at: at.clone(),
                    });
                }
                Rd::Slice(s) => {
                    if let Some(t) = &s.tx {
                        if tx.as_ref().is_none_or(|x| t > x) {
                            tx = Some(t.clone());
                        }
                    }
                    if let Some(l) = &s.lo {
                        raise(&mut latest, l);
                        raise(&mut lo, l);
                    }
                    lower(&mut hi, &s.hi);
                    memo &= s.memo;
                    extra.entry((s.fn_name.clone(), None)).or_insert_with(|| s.fn_hash.clone());
                    for a in &s.member_ans {
                        add(&mut names, &a.names);
                    }
                    inputs.push(Use::Slice(s.clone()));
                }
            }
        }
        // Own names first (as before: the first recorded wins), then what the inputs add.
        let mut names = names.unwrap_or_default();
        let missing = |m: &Names, from: &Names| from.keys().any(|k| !m.contains_key(k));
        if missing(&names, &own) || missing(&names, &extra) {
            let m = Arc::make_mut(&mut names);
            for (k, v) in own.into_iter().chain(extra) {
                m.insert(k, v);
            }
        }
        Facts { tx, latest, lo, hi, names, inputs, memo, entries }
    }

    /// Runs a frame's runtime once.
    fn eval_frame(&mut self, f: &mut Frame) -> Result<Step, EngineError> {
        let Some(rt) = self.eng.runtimes.get(&f.desc.lang).cloned() else {
            return Err(EngineError::NoRuntime { lang: f.desc.lang.clone(), fn_hash: f.cell.fn_hash.clone() });
        };
        let declared = self.eng.asof_dependent(&f.cell.fn_hash, &f.desc);
        let mut budget = self.budget.share();
        let floor = std::mem::take(&mut f.floor);
        budget.begin_attempt(floor);
        let mut reader = Reader {
            budget: self.budget.share(),
            asof: f.asof.clone(),
            req: self,
            desc: &f.desc,
            read: BTreeMap::new(),
            names: Names::new(),
            stop: None,
            faulted: false,
            fault_known: MIN_INSTANT.to_string(),
            compared: MIN_INSTANT.to_string(),
            future_seen: false,
            at_read: false,
            asof_read: false,
            fault_deps: Vec::new(),
            unmemo: false,
        };
        let result = rt.evaluate(&f.desc, &f.cell, &mut reader, &mut budget);
        let Reader {
            read,
            names,
            stop,
            faulted,
            fault_known,
            compared,
            future_seen,
            at_read,
            asof_read,
            fault_deps,
            unmemo,
            ..
        } = reader;
        let fail = |e: EvalError| engine_error(e, &f.id);
        // Exhaustion wins over anything the runtime made of it.
        if budget.is_exhausted() {
            return Err(exhausted(&budget));
        }
        match stop {
            Some(Stop::Pending(list)) => {
                f.floor = budget.suspend_attempt();
                return Ok(Step::Push(list));
            }
            Some(Stop::NotCovered) => {
                budget.finish_attempt();
                // The times come before the cells (SPEC 15.13): E_FORMULA_FUTURE the runtime
                // established (a time found after the asof) stands over an input without a
                // value. Either way the cell has no value; this is which code it reports. Not
                // memoised: the input without a value has no memo entry to follow.
                if matches!(result, Err(EvalError::Fault(Fault::Future))) && (future_seen || asof_read) {
                    let read: Vec<Rd> = read.into_values().collect();
                    let facts = self.facts(&read, names);
                    let fa = self.fault_answer(f, facts, &fault_deps, Fault::Future, None, false)?;
                    return Ok(Step::Done(Done::Fault(fa)));
                }
                return Ok(Step::Done(Done::NotCovered));
            }
            Some(Stop::Fatal(e)) => return Err(fail(e)),
            None => {}
        }
        budget.finish_attempt();
        let read: Vec<Rd> = read.into_values().collect();
        let value = match result {
            Ok(v) => v,
            Err(EvalError::Fault(code)) => {
                // Where the fault holds from: as a receipt's asof would be, had it a value. A
                // fault a read returned (caught or not) is a cell's: memoised as its own.
                let facts = self.facts(&read, names);
                let latest = facts.latest.clone().max(fault_known).max(compared);
                let memo = !unmemo && !at_read;
                let latest = (!(declared || asof_read)).then_some(latest);
                let fa = self.fault_answer(f, facts, &fault_deps, code, latest.as_deref(), memo)?;
                return Ok(Step::Done(Done::Fault(fa)));
            }
            Err(EvalError::Suspended | EvalError::InputNotCovered { .. }) => {
                return Err(EngineError::Runtime {
                    cell_id: f.id.clone(),
                    message: "the runtime returned an input's error for an input it did not read".into(),
                })
            }
            Err(e) => return Err(fail(e)),
        };
        if value.ty() != f.desc.signature.returns.ty {
            return Err(EngineError::Runtime {
                cell_id: f.id.clone(),
                message: format!(
                    "returned a {} where the function declares {}",
                    value.ty().as_str(),
                    f.desc.signature.returns.ty.as_str()
                ),
            });
        }
        if f.unsigned {
            for rd in &read {
                if let Rd::Slice(s) = rd {
                    self.manifests.insert(s.entry.clone(), s.manifest.clone());
                }
            }
            let entries: BTreeSet<String> = read.iter().map(|r| r.entry().to_string()).collect();
            return Ok(Step::Done(Done::Unsigned(value, entries.into_iter().collect())));
        }
        let cand = match &f.state {
            State::Eval { cand } => cand.clone(),
            _ => None,
        };
        let how = How {
            dependent: declared || asof_read,
            // A version is kept for later requests to validate only when its receipt says
            // everything validation needs: not when the value rests on a caught fault, on a
            // read at a transaction time, or on the asof read undeclared.
            computed: !faulted && !at_read && !(asof_read && !declared),
            memo: !faulted,
            known: Some(if faulted { fault_known.max(compared) } else { compared }),
        };
        let ans = self.answer_with(f, cand, value, &read, names, how)?;
        Ok(Step::Done(Done::Value(ans)))
    }

    /// The answer of a faulted cell (SPEC 15.12) for frame `f`: the fault, which holds from
    /// the instant its receipt's asof would have, and rests on what the evaluation read
    /// (`facts`) and on the faulted cells `deps` and their names. `latest` is the latest
    /// instant of what it read and relied on, from which the instant is found; `None` for a
    /// fault asof-dependent (16.7), which holds as of the frame's asof only. Kept by the memo
    /// when `memo` and nothing it rests on says otherwise.
    fn fault_answer(
        &mut self,
        f: &Frame,
        mut facts: Facts,
        deps: &[Arc<FaultAns>],
        fault: Fault,
        latest: Option<&str>,
        memo: bool,
    ) -> Result<Arc<FaultAns>, EngineError> {
        let mut faulted = Vec::with_capacity(deps.len());
        for d in deps {
            facts.memo &= d.memo;
            if let Some(h) = &d.hi {
                if facts.hi.as_ref().is_none_or(|x| h < x) {
                    facts.hi = Some(h.clone());
                }
            }
            if d.names.keys().any(|k| !facts.names.contains_key(k)) {
                let m = Arc::make_mut(&mut facts.names);
                for (k, v) in d.names.iter() {
                    m.entry(k.clone()).or_insert_with(|| v.clone());
                }
            }
            faulted.push(d.id.clone());
        }
        let (known, lo, hi) = match latest {
            None => (f.asof.clone(), f.asof.clone(), Some(just_after(&f.asof))),
            Some(latest) => {
                let tx = facts.tx.clone().unwrap_or_else(|| MIN_INSTANT.to_string());
                let known = self
                    .binding_asof(latest.to_string(), &facts.names, &f.asof)
                    .map_err(|e| engine_error(e, &f.id))?
                    .max(tx);
                (known.clone(), known, facts.hi.clone())
            }
        };
        Ok(Arc::new(FaultAns {
            id: f.id.clone(),
            fault,
            known,
            lo,
            hi,
            names: facts.names,
            inputs: facts.inputs,
            faulted,
            memo: memo && facts.memo,
        }))
    }

    /// The answer for frame `f`'s cell, evaluated to `value` from `read`: the version that
    /// did not validate, `cand`, when its value is the same (early cutoff: keep the current
    /// version); a receipt already signed for exactly these inputs; else a new receipt.
    fn answer_with(
        &mut self,
        f: &Frame,
        cand: Option<Arc<Parsed>>,
        value: Scalar,
        read: &[Rd],
        own: Names,
        how: How,
    ) -> Result<Arc<Ans>, EngineError> {
        let mut facts = self.facts(read, own);
        if let Some(k) = &how.known {
            if *k > facts.latest {
                facts.latest = k.clone();
            }
        }
        let tx = facts.tx.clone().unwrap_or_else(|| MIN_INSTANT.to_string());
        // Each input's answer holds from its `lo` on (its receipt's asof, or later when the
        // answer is a version validated or kept across a change of its own inputs). The latest
        // of these, of the members' asofs, of the transaction times read, and of the start of
        // every name binding relied on, is where this value is known (SPEC 8.2 rule 2); for an
        // asof-dependent function, the evaluation asof.
        let asof = if how.dependent {
            f.asof.clone()
        } else {
            let a =
                self.binding_asof(facts.latest.clone(), &facts.names, &f.asof).map_err(|e| engine_error(e, &f.id))?;
            a.max(tx.clone())
        };
        let (lo, hi) = if how.dependent {
            (f.asof.clone(), Some(just_after(&f.asof)))
        } else {
            (facts.lo.as_str().max(asof.as_str()).to_string(), facts.hi.clone())
        };
        let memo = how.memo && facts.memo;
        let licences = self.licences(read)?;
        let mut existing = None;
        if let Some(c) = &cand {
            if c.receipt.value() == &value
                && (!how.dependent || c.receipt.asof() == f.asof)
                && self.entitled_with(f, &licences, &c.receipt)?
            {
                existing = Some(c.clone());
            } else if self.scope.is_none() && c.receipt.tx() == tx && c.receipt.asof() == asof {
                // The new version would share the old one's (tx, asof), which the store's key
                // holds once (an input corrected with a tx earlier than another input's; a
                // version signed again for its licence): a receipt signed before for exactly
                // these inputs, with a licence the rule allows, is reused rather than signing
                // another.
                existing = self.signed_before(f, &facts.entries, &value, &licences)?;
            }
        }
        if let Some(p) = existing {
            let r = &p.receipt;
            return Ok(Arc::new(Ans {
                id: f.id.clone(),
                hash: r.hash().to_string(),
                bytes: p.bytes.clone(),
                value,
                tx: r.tx().to_string(),
                asof: r.asof().to_string(),
                licence: r.licence().to_string(),
                lo: lo.as_str().max(r.asof()).to_string(),
                hi,
                names: facts.names,
                cell: f.cell.clone(),
                inputs: facts.inputs,
                memo,
            }));
        }
        // Licence inheritance (SPEC 8.5): the licence of the receipt to be issued now, or a
        // refusal (nothing is signed; the request fails, and with it every reader).
        let issued = self.issued.clone();
        let rule = self.inherit(f, &licences, &issued, &self.eng.licence.clone())?;
        let Some(licence) = rule.licence else {
            return Err(EngineError::Unlicensed { cell_id: f.id.clone(), reason: rule.reason });
        };
        if self.scope.is_some() {
            // A scenario writes nothing: its answer is unsigned, and nothing is kept.
            return Ok(Arc::new(Ans {
                id: f.id.clone(),
                hash: scoped_hash(&f.id, &value),
                bytes: Arc::new(Vec::new()),
                value,
                tx,
                asof,
                licence,
                lo,
                hi,
                names: facts.names,
                cell: f.cell.clone(),
                inputs: facts.inputs,
                memo: false,
            }));
        }
        let summary = match &self.eng.summary {
            Some(s) => s(&f.cell),
            None => format!("{} computed by {}.", f.cell.function_name(), self.eng.host),
        };
        let receipt = ReceiptBuilder::new()
            .cell(f.cell.clone())
            .value(value.clone())
            .valid(valid_args(&f.cell, &f.desc))
            .asof(asof.clone())
            .tx(tx.clone())
            .issued(self.issued.clone())
            .publisher(f.cell.ns.clone())
            .signer_host(self.eng.host.clone())
            .inputs(facts.entries.iter().cloned())
            .licence(licence.clone())
            .summary(summary)
            .sign(&self.eng.key)
            .map_err(EngineError::Sign)?;
        let bytes = Arc::new(receipt_json(&receipt));
        let hash = receipt.hash().to_string();
        for rd in read {
            if let Rd::Slice(s) = rd {
                self.manifests.insert(s.entry.clone(), s.manifest.clone());
            }
        }
        self.fresh.push(Fresh {
            cell_id: f.id.clone(),
            tx: tx.clone(),
            hash: hash.clone(),
            bytes: bytes.clone(),
            computed: how.computed,
        });
        Ok(Arc::new(Ans {
            id: f.id.clone(),
            hash,
            bytes,
            value,
            tx,
            asof,
            licence,
            lo,
            hi,
            names: facts.names,
            cell: f.cell.clone(),
            inputs: facts.inputs,
            memo,
        }))
    }

    /// The asof of a new receipt: the latest of `latest` and the start of every name binding
    /// the value relied on (`names`: the names read, transitively, as of `asof` or of their
    /// own transaction time). From then on, up to the asof, the inputs and the names are as
    /// they were read, so the receipt's value is the cell's value as of its own asof.
    fn binding_asof(&mut self, latest: String, names: &Names, asof: &str) -> Result<String, EvalError> {
        let mut out = latest;
        for ((name, at), hash) in names {
            match self.binding(name, at.as_deref().unwrap_or(asof))? {
                Some((h, start)) if h == *hash => {
                    if start > out {
                        out = start;
                    }
                }
                // Not as recorded (cannot happen: every name was resolved in this request):
                // the asof, at which the names are known.
                _ => return Ok(asof.to_string()),
            }
        }
        Ok(out)
    }

    /// A stored receipt of frame `f`'s cell by this mesh with exactly the inputs `entries`
    /// and `value`, and a licence the rule allows for the input licences `licences`
    /// ([`entitled_with`](Req::entitled_with)).
    fn signed_before(
        &mut self,
        f: &Frame,
        entries: &BTreeSet<String>,
        value: &Scalar,
        licences: &[String],
    ) -> Result<Option<Arc<Parsed>>, EngineError> {
        let id = f.id.as_str();
        let Some(first) = entries.iter().next() else { return Ok(None) };
        let candidates = if first.starts_with("slice:") {
            self.eng.store.slice_dependents(first).map_err(|e| StoreError::new("slice_dependents", e))?
        } else {
            self.eng.store.dependents(first).map_err(|e| StoreError::new("dependents", e))?
        };
        for h in candidates {
            let Some(p) = self.receipt(&h)? else { continue };
            let r = &p.receipt;
            if r.signer_host() == self.eng.host
                && r.value() == value
                && r.inputs().len() == entries.len()
                && r.inputs().iter().zip(entries.iter()).all(|(a, b)| a == b)
                && r.cell().id() == id
                && self.entitled_with(f, licences, r)?
            {
                return Ok(Some(p));
            }
        }
        Ok(None)
    }

    /// The licences of the input receipts of `read` (SPEC 8.5): one per receipt read, every
    /// member of a slice included (a stored slice's members from their receipts in the store).
    fn licences(&mut self, read: &[Rd]) -> Result<Vec<String>, EngineError> {
        let mut out = Vec::new();
        for rd in read {
            match rd {
                Rd::Cell { ans, .. } => out.push(ans.licence.clone()),
                Rd::Slice(s) if s.computed => out.extend(s.member_ans.iter().map(|a| a.licence.clone())),
                Rd::Slice(s) => {
                    for m in &s.members {
                        if m.scoped {
                            out.push(self.eng.licence.clone());
                            continue;
                        }
                        let Some(p) = self.receipt(&m.hash)? else {
                            return Err(StoreError::new(
                                "receipt",
                                datumesh_store::Error::NotFound { what: "receipt", key: m.hash.clone() },
                            )
                            .into());
                        };
                        out.push(p.receipt.licence().to_string());
                    }
                }
            }
        }
        Ok(out)
    }

    /// The declarations of the publisher of namespace `ns` (the host of `ns`, SPEC 8.5), from
    /// its key document in the store; none when the store has no document for the host, or
    /// one without declarations.
    fn declarations(&mut self, ns: &str) -> Result<Option<Arc<Declarations>>, EngineError> {
        let host = ns.split('/').next().unwrap_or(ns);
        if let Some(d) = self.declarations.get(host) {
            return Ok(d.clone());
        }
        let d = match self.eng.store.namespace(host).map_err(|e| StoreError::new("namespace", e))? {
            // The store checked the document when it was recorded (SPEC 9.2).
            Some(n) => KeyDocument::from_json(host, &n.keydoc).ok().and_then(|k| k.declarations).map(Arc::new),
            None => None,
        };
        self.declarations.insert(host.to_string(), d.clone());
        Ok(d)
    }

    /// Licence inheritance (SPEC 8.5, core's `inherit_licence`) for frame `f`'s cell over the
    /// input licences `licences`, for a receipt issued at `issued`; `mesh_licence` for a cell
    /// with no input. The publisher's declarations are read only when the rule needs them (a
    /// restricted input).
    fn inherit(
        &mut self,
        f: &Frame,
        licences: &[String],
        issued: &str,
        mesh_licence: &str,
    ) -> Result<Inheritance, EngineError> {
        let function = f.cell.function_name();
        let without = inherit_licence(licences, None, &function, issued, mesh_licence);
        if !matches!(without.reason, LicenceReason::NotDeclared | LicenceReason::DeclarationNotOpen) {
            return Ok(without);
        }
        let d = self.declarations(&f.cell.ns)?;
        Ok(inherit_licence(licences, d.as_deref(), &function, issued, mesh_licence))
    }

    /// Whether receipt `r` of frame `f`'s cell may be reused over inputs with licences
    /// `licences`: entitled at its own `issued` (its licence is the rule's then, as the
    /// entitlement check judges it, SPEC 10.3; a declaration closed since does not matter),
    /// or its licence is the one the rule gives now.
    fn entitled_with(&mut self, f: &Frame, licences: &[String], r: &Receipt) -> Result<bool, EngineError> {
        let then = self.inherit(f, licences, r.issued(), r.licence())?;
        if then.licence.as_deref() == Some(r.licence()) {
            return Ok(true);
        }
        let issued = self.issued.clone();
        let now = self.inherit(f, licences, &issued, &self.eng.licence.clone())?;
        Ok(now.licence.as_deref() == Some(r.licence()))
    }

    /// [`entitled_with`](Req::entitled_with) over the licences of `read`.
    fn reusable(&mut self, f: &Frame, read: &[Rd], r: &Receipt) -> Result<bool, EngineError> {
        if r.inputs().is_empty() {
            return Ok(true);
        }
        let licences = self.licences(read)?;
        self.entitled_with(f, &licences, r)
    }

    /// Writes the request's new manifests, receipts and computed versions: manifests and
    /// receipts first (inputs before the receipts reading them), then the versions.
    pub(crate) fn flush(&mut self) -> Result<(), EngineError> {
        if self.scope.is_some() {
            self.fresh.clear();
            self.manifests.clear();
            return Ok(());
        }
        // Nothing is written when a receipt would be (SPEC 9.9): the store refuses the receipts
        // under a suspended namespace, and the manifests (not refused) are not written before.
        if !self.fresh.is_empty() {
            if let Some(ns) = self.eng.store.namespace(&self.eng.host).map_err(|e| StoreError::new("namespace", e))? {
                if let Some(since) = ns.suspended_at {
                    return Err(EngineError::NamespaceSuspended { host: self.eng.host.clone(), suspended_at: since });
                }
            }
        }
        for m in self.manifests.values() {
            self.eng.store.put_manifest(m).map_err(|e| StoreError::new("put_manifest", e))?;
        }
        for f in &self.fresh {
            self.eng.store.put_receipt(&f.bytes).map_err(|e| StoreError::new("put_receipt", e))?;
        }
        for f in self.fresh.iter().filter(|f| f.computed) {
            match self.eng.store.put_computed(&f.cell_id, &f.tx, &f.hash) {
                Ok(()) => {}
                // Another receipt holds this (cell, tx, asof): the answer stands, as a receipt in
                // the store that is not a computed version.
                Err(datumesh_store::Error::Conflict { .. }) => {
                    tracing::warn!(target: "datumesh_engine", cell_id = %f.cell_id, receipt = %f.hash,
                        "another computed receipt is recorded for this cell at the same tx and asof; kept that one");
                }
                Err(e) => return Err(StoreError::new("put_computed", e).into()),
            }
        }
        self.fresh.clear();
        self.manifests.clear();
        Ok(())
    }
}

/// Applies a scenario to the members of a stored slice read as of `asof`: the removed members
/// go, the value overrides of members apply, and the added members come (sorted by cell id).
fn apply_scope(scope: &Scope, slice: &Slice, asof: &str, members: &mut Vec<MemberRec>) {
    let ov = scope.slices.get(slice.id());
    if let Some(o) = ov {
        members.retain(|m| !o.remove.contains(&m.id));
    }
    for m in members.iter_mut() {
        if let Some((_, v)) = scope.values.get(&m.id) {
            m.value = v.clone();
            m.hash = scoped_hash(&m.id, v);
            m.scoped = true;
        }
    }
    if let Some(o) = ov {
        for (c, v) in &o.add {
            let id = c.id();
            let value = scope.values.get(&id).map_or_else(|| v.clone(), |x| x.1.clone());
            members.push(MemberRec {
                hash: scoped_hash(&id, &value),
                id,
                value,
                args: c.args.clone(),
                tx: asof.to_string(),
                asof: asof.to_string(),
                scoped: true,
            });
        }
    }
    members.sort_by(|a, b| a.id.cmp(&b.id));
}

/// A cell's canonical call, as a cycle's path names it (SPEC 5.8): `<ns>/<fn>(<label>: <literal>,
/// ...)`, labels ascending, strings quoted with `"` doubled; no arguments: `()`.
pub(crate) fn canonical_call(cell: &Cell) -> String {
    let args: Vec<String> = cell
        .args
        .iter()
        .map(|(k, a)| {
            let literal = match a {
                Arg::Value(Scalar::String(s)) => format!("\"{}\"", s.replace('"', "\"\"")),
                Arg::Value(v) => v.lexical().to_string(),
                Arg::Slice => "*".to_string(),
            };
            format!("{k}: {literal}")
        })
        .collect();
    format!("{}/{}({})", cell.ns, cell.fn_name, args.join(", "))
}

/// An evaluation error from the engine's own lookups, as an evaluation sees it.
fn eng_eval(e: EngineError) -> EvalError {
    match e {
        EngineError::Store(e) => EvalError::Store(e),
        EngineError::Invalid(e) => EvalError::InvalidInput(e),
        other => EvalError::Runtime(other.to_string()),
    }
}

/// What running a root's frames ended with.
enum Ended {
    /// The root has its answer.
    Done,
    /// The root's stack would pass the depth limit at this pending cell: it runs first.
    Split(Pend),
}

/// Whether the members `old` (cell id, value) are the cells of `now` with the same values.
fn same_members(old: &[(String, Scalar)], now: &SliceRead) -> bool {
    if old.len() != now.members.len() {
        return false;
    }
    let mut a: Vec<(&str, &Scalar)> = old.iter().map(|(i, v)| (i.as_str(), v)).collect();
    let mut b: Vec<(&str, &Scalar)> = now.members.iter().map(|m| (m.id.as_str(), &m.value)).collect();
    a.sort_by(|x, y| x.0.cmp(y.0));
    b.sort_by(|x, y| x.0.cmp(y.0));
    a == b
}

/// Why a frame's runtime cannot finish this run, recorded at the first read that says so; it
/// wins over whatever the runtime returns.
enum Stop {
    /// Inputs to evaluate first.
    Pending(Vec<Pend>),
    /// An input with no value: neither has this cell.
    NotCovered,
    Fatal(EvalError),
}

/// The [`Inputs`] of one run of a frame's runtime.
struct Reader<'r, 'e> {
    req: &'r mut Req<'e>,
    asof: String,
    desc: &'r FunctionDescriptor,
    budget: Budget,
    /// What was read, by entry (and the transaction time of a cell read at one).
    read: BTreeMap<(String, Option<String>), Rd>,
    /// The names relied on, with their hashes.
    names: Names,
    stop: Option<Stop>,
    /// A read returned a fault (the runtime may have caught it), and since when it holds.
    faulted: bool,
    fault_known: String,
    /// The latest instant `asof_compare` found at or before the asof: an asof candidate.
    compared: String,
    /// A time after the asof was established (`E_FORMULA_FUTURE` may be reported).
    future_seen: bool,
    /// A cell was read at a transaction time.
    at_read: bool,
    /// The asof was read.
    asof_read: bool,
    /// The faulted cells read, whose faults the answer rests on.
    fault_deps: Vec<Arc<FaultAns>>,
    /// A fault rests on something that is not a cell's fault (an absent slice member, which
    /// has no memo entry of its own): not memoised.
    unmemo: bool,
}

impl Reader<'_, '_> {
    /// A read returned a fault that holds from `known` on: the faulted cell `dep`, or none
    /// when the fault is not a cell's (then the answer is not memoised).
    fn fault(&mut self, known: &str, dep: Option<Arc<FaultAns>>) {
        self.faulted = true;
        if known > self.fault_known.as_str() {
            self.fault_known = known.to_string();
        }
        match dep {
            Some(d) => self.fault_deps.push(d),
            None => self.unmemo = true,
        }
    }

    /// A read returned `E_FORMULA_FUTURE`: a fault that depends on the asof itself.
    fn future(&mut self) -> EvalError {
        self.faulted = true;
        self.asof_read = true;
        self.future_seen = true;
        EvalError::Fault(Fault::Future)
    }

    fn declared(&self, fn_name: &str) -> Result<(), EvalError> {
        if !self.desc.inputs.iter().any(|i| i == fn_name) || !fn_name.contains('/') {
            return Err(EvalError::UndeclaredInput { fn_name: fn_name.to_string() });
        }
        Ok(())
    }

    /// The descriptor stored under `fn_hash`: a hash with none is a store failure.
    fn descriptor_for(&self, fn_hash: &str) -> Result<Arc<FunctionDescriptor>, EvalError> {
        match self.req.eng.descriptor_of(fn_hash) {
            Ok(Some(d)) => Ok(d),
            Ok(None) => Err(EvalError::Store(StoreError::new(
                "descriptor",
                datumesh_store::Error::NotFound { what: "descriptor", key: fn_hash.to_string() },
            ))),
            Err(EngineError::Store(e)) => Err(EvalError::Store(e)),
            Err(EngineError::Invalid(e)) => Err(EvalError::InvalidInput(e)),
            Err(e) => Err(EvalError::Runtime(e.to_string())),
        }
    }

    /// The function `fn_name` means as of `at`, recorded as relied on (`key_at`: the
    /// transaction time it is read at, none for the evaluation's asof).
    fn function(
        &mut self,
        fn_name: &str,
        at: &str,
        key_at: Option<&str>,
    ) -> Result<(String, Arc<FunctionDescriptor>), EvalError> {
        let Some(fn_hash) = self.req.resolve(fn_name, at)? else {
            return Err(EvalError::NameUnresolved { fn_name: fn_name.to_string(), asof: at.to_string() });
        };
        self.names.insert((fn_name.to_string(), key_at.map(str::to_string)), fn_hash.clone());
        let desc = self.descriptor_for(&fn_hash)?;
        Ok((fn_hash, desc))
    }

    /// A single-cell read as of the evaluation's asof, or as of `at`.
    fn read_at(&mut self, fn_name: &str, args: &Args, at: Option<&str>) -> Result<Input, EvalError> {
        self.budget.charge_shared(1)?;
        self.declared(fn_name)?;
        let when = at.unwrap_or(&self.asof).to_string();
        let (fn_hash, desc) = self.function(fn_name, &when, at)?;
        let cell = cell_named(fn_name, fn_hash, args);
        self.cell_read(cell, desc, at)
    }

    /// A pinned read (SPEC 16.3 `super`): the cell of `fn_name` at `fn_hash`, with the
    /// descriptor stored under that hash, as of the evaluation's asof. The name is not resolved
    /// and no binding is recorded: the receipt is an input as any read's is.
    fn read_pinned_at(&mut self, fn_name: &str, fn_hash: &str, args: &Args) -> Result<Input, EvalError> {
        self.budget.charge_shared(1)?;
        self.declared(fn_name)?;
        let desc = self.descriptor_for(fn_hash)?;
        let cell = cell_named(fn_name, fn_hash.to_string(), args);
        self.cell_read(cell, desc, None)
    }

    /// Reads `cell` (its descriptor `desc`) as of the evaluation's asof, or as of `at`: a value
    /// is an input, a fault is returned (and recorded), a cell with no value or one to evaluate
    /// stops the run.
    fn cell_read(&mut self, cell: Cell, desc: Arc<FunctionDescriptor>, at: Option<&str>) -> Result<Input, EvalError> {
        let when = at.unwrap_or(&self.asof).to_string();
        if let Some((k, _)) = cell.args.iter().find(|(_, a)| a.scalar().is_none()) {
            return Err(EvalError::InvalidInput(CoreError::new(
                ErrorCode::Slice,
                format!("$.args.{k}"),
                "an input is a cell, not a slice",
            )));
        }
        check_args(&cell, &desc).map_err(EvalError::InvalidInput)?;
        let id = cell.id();
        if at.is_some() {
            self.at_read = true;
        }
        match self.req.lookup(&cell, &id, desc, &when)? {
            Lookup::Ready(Done::Value(a)) => {
                let input = Input::new(a.value.clone(), a.hash.clone(), a.tx.clone(), a.asof.clone());
                let at = at.map(str::to_string);
                self.read.insert((a.hash.clone(), at.clone()), Rd::Cell { ans: a, at });
                Ok(input)
            }
            Lookup::Ready(Done::Fault(fa)) => {
                let known = fa.known.clone();
                self.fault(&known, Some(fa.clone()));
                Err(EvalError::Fault(fa.fault))
            }
            Lookup::Ready(Done::NotCovered | Done::Unsigned(..)) => {
                self.stop = Some(Stop::NotCovered);
                Err(EvalError::InputNotCovered { cell_id: id })
            }
            Lookup::Eval(desc) => {
                self.stop = Some(Stop::Pending(vec![Pend { cell, id, desc, asof: when }]));
                Err(EvalError::Suspended)
            }
        }
    }

    fn slice_read(
        &mut self,
        fn_name: &str,
        slice: Arc<Slice>,
        desc: Arc<FunctionDescriptor>,
        spec: Spec,
    ) -> Result<Arc<SliceRead>, EvalError> {
        let asof = self.asof.clone();
        match self.req.slice_at(fn_name, slice, desc, spec, &asof)? {
            SliceOutcome::Ready(s) => {
                // 1 + members, once the members are obtained.
                self.budget.charge_shared(1 + s.members.len() as u64)?;
                self.read.insert((s.entry.clone(), None), Rd::Slice(s.clone()));
                Ok(s)
            }
            SliceOutcome::Pending(list) => {
                self.stop = Some(Stop::Pending(list));
                Err(EvalError::Suspended)
            }
            SliceOutcome::Absent(known) => {
                self.fault(&known, None);
                Err(EvalError::Fault(Fault::Absent))
            }
        }
    }

    /// Records the first error that must stop the run.
    fn guard<T>(&mut self, r: Result<T, EvalError>) -> Result<T, EvalError> {
        if let Err(e) = &r {
            if !matches!(e, EvalError::Suspended | EvalError::InputNotCovered { .. } | EvalError::Fault(_)) {
                self.stop = Some(Stop::Fatal(e.clone()));
            }
        }
        r
    }

    /// The error of a read made after the run had to stop.
    fn stopped(&self) -> Option<EvalError> {
        match &self.stop {
            Some(Stop::Pending(..)) => Some(EvalError::Suspended),
            Some(Stop::NotCovered) => Some(EvalError::InputNotCovered { cell_id: String::new() }),
            Some(Stop::Fatal(e)) => Some(e.clone()),
            None => None,
        }
    }
}

fn instant_arg(s: &str, what: &str) -> Result<(), EvalError> {
    lexical::check_instant(s, what).map_err(EvalError::InvalidInput)
}

impl Inputs for Reader<'_, '_> {
    fn read(&mut self, fn_name: &str, args: &Args) -> Result<Scalar, EvalError> {
        self.read_cell(fn_name, args).map(|i| i.value)
    }

    fn read_cell(&mut self, fn_name: &str, args: &Args) -> Result<Input, EvalError> {
        if let Some(e) = self.stopped() {
            return Err(e);
        }
        let r = self.read_at(fn_name, args, None);
        self.guard(r)
    }

    fn read_asof(&mut self, fn_name: &str, args: &Args, t: &str) -> Result<Input, EvalError> {
        // The time first, even after the run had to stop (SPEC 15.13).
        if let Some(e) = self.stopped().filter(|_| is_instant(t) && t <= self.asof.as_str()) {
            return Err(e);
        }
        let r = (|| {
            instant_arg(t, "$.t")?;
            if t > self.asof.as_str() {
                return Err(self.future());
            }
            self.read_at(fn_name, args, Some(t))
        })();
        self.guard(r)
    }

    fn read_pinned(&mut self, fn_name: &str, fn_hash: &str, args: &Args) -> Result<Input, EvalError> {
        if let Some(e) = self.stopped() {
            return Err(e);
        }
        let r = self.read_pinned_at(fn_name, fn_hash, args);
        self.guard(r)
    }

    fn read_slice(&mut self, fn_name: &str, pattern: &SlicePattern) -> Result<Vec<Member>, EvalError> {
        if let Some(e) = self.stopped() {
            return Err(e);
        }
        let r = (|| {
            self.declared(fn_name)?;
            let asof = self.asof.clone();
            let (fn_hash, desc) = self.function(fn_name, &asof, None)?;
            let slice = cells_slice(fn_name, &fn_hash, &desc, pattern).map_err(EvalError::InvalidInput)?;
            let s = self.slice_read(fn_name, Arc::new(slice), desc, Spec::Cells { pattern: pattern.clone() })?;
            Ok(s.members.iter().map(|m| Member::new(m.args.clone(), m.input())).collect())
        })();
        self.guard(r)
    }

    fn read_versions(&mut self, fn_name: &str, args: &Args, from: &str, to: &str) -> Result<Vec<Input>, EvalError> {
        // The times first, even after the run had to stop (SPEC 15.13).
        let times_ok = is_instant(from) && is_instant(to) && to <= self.asof.as_str();
        if let Some(e) = self.stopped().filter(|_| times_ok) {
            return Err(e);
        }
        let r = (|| {
            instant_arg(from, "$.from")?;
            instant_arg(to, "$.to")?;
            if to > self.asof.as_str() {
                return Err(self.future());
            }
            self.declared(fn_name)?;
            let asof = self.asof.clone();
            let (fn_hash, desc) = self.function(fn_name, &asof, None)?;
            let (ns, f) = fn_name.rsplit_once('/').unwrap_or(("", fn_name));
            let cell = Cell { ns: ns.to_string(), fn_name: f.to_string(), fn_hash, args: args.clone() };
            check_args(&cell, &desc).map_err(EvalError::InvalidInput)?;
            if desc.lang != STORED {
                return Err(EvalError::InvalidInput(CoreError::new(
                    ErrorCode::Slice,
                    "$",
                    "versions() of a computed function: versions are of stored cells",
                )));
            }
            let slice = versions_slice(&cell, from, to).map_err(EvalError::InvalidInput)?;
            let spec = Spec::Versions { to: to.to_string() };
            let s = self.slice_read(fn_name, Arc::new(slice), desc, spec)?;
            Ok(s.members.iter().map(MemberRec::input).collect())
        })();
        self.guard(r)
    }

    fn binding(&mut self, fn_name: &str, at: Option<&str>) -> Result<Option<FnBinding>, EvalError> {
        if let Some(e) = self.stopped() {
            return Err(e);
        }
        let r = (|| {
            self.declared(fn_name)?;
            if let Some(t) = at {
                instant_arg(t, "$.at")?;
            }
            let when = at.unwrap_or(&self.asof).to_string();
            let Some((fn_hash, start)) = self.req.binding(fn_name, &when)? else { return Ok(None) };
            let (_, descriptor) = self.function(fn_name, &when, at)?;
            Ok(Some(FnBinding { fn_hash, start, descriptor }))
        })();
        self.guard(r)
    }

    fn asof(&mut self) -> String {
        self.asof_read = true;
        self.asof.clone()
    }

    fn binding_peek(&mut self, fn_name: &str, at: Option<&str>) -> Result<Option<FnBinding>, EvalError> {
        if let Some(e) = self.stopped() {
            return Err(e);
        }
        let r = (|| {
            self.declared(fn_name)?;
            if let Some(t) = at {
                instant_arg(t, "$.at")?;
            }
            let when = at.unwrap_or(&self.asof).to_string();
            let Some((fn_hash, start)) = self.req.binding(fn_name, &when)? else { return Ok(None) };
            let descriptor = match self.req.eng.descriptor_of(&fn_hash) {
                Ok(Some(d)) => d,
                Ok(None) => {
                    return Err(EvalError::Store(StoreError::new(
                        "descriptor",
                        datumesh_store::Error::NotFound { what: "descriptor", key: fn_hash },
                    )))
                }
                Err(EngineError::Store(e)) => return Err(EvalError::Store(e)),
                Err(EngineError::Invalid(e)) => return Err(EvalError::InvalidInput(e)),
                Err(e) => return Err(EvalError::Runtime(e.to_string())),
            };
            Ok(Some(FnBinding::new(fn_hash, start, descriptor)))
        })();
        self.guard(r)
    }

    fn read_blob(&mut self, sha256: &str, length: u64) -> Result<Vec<u8>, EvalError> {
        if let Some(e) = self.stopped() {
            return Err(e);
        }
        let r = (|| {
            lexical::check_hex(sha256, 64, "$.sha256").map_err(EvalError::InvalidInput)?;
            let unavailable = |reason: String| EvalError::BlobUnavailable { sha256: sha256.to_string(), reason };
            let Some(bytes) = self.req.eng.store.blob(sha256).map_err(store_err("blob"))? else {
                return Err(unavailable("the store has no content for it".into()));
            };
            if bytes.len() as u64 != length {
                return Err(unavailable(format!("the content has {} bytes, the blob {length}", bytes.len())));
            }
            if datumesh_core::sha256_hex(&bytes) != sha256 {
                return Err(unavailable("the content has another SHA-256".into()));
            }
            Ok(bytes.into_owned())
        })();
        self.guard(r)
    }

    fn asof_compare(&mut self, t: &str) -> Result<std::cmp::Ordering, EvalError> {
        instant_arg(t, "$.t")?;
        let ord = t.cmp(self.asof.as_str());
        if ord == std::cmp::Ordering::Greater {
            // Changes once the asof reaches t: asof-dependent.
            self.asof_read = true;
            self.future_seen = true;
        } else if t > self.compared.as_str() {
            self.compared = t.to_string();
        }
        Ok(ord)
    }
}

/// The cell `fn_name` (`ns/name`) at `fn_hash`, with `args`.
fn cell_named(fn_name: &str, fn_hash: String, args: &Args) -> Cell {
    let (ns, f) = fn_name.rsplit_once('/').unwrap_or(("", fn_name));
    Cell { ns: ns.to_string(), fn_name: f.to_string(), fn_hash, args: args.clone() }
}

/// The request's error for an exhausted budget: its fuel, or its work.
pub(crate) fn exhausted(b: &Budget) -> EngineError {
    match b.exhausted() {
        crate::runtime::Exhausted::Work => EngineError::WorkExceeded { limit: b.work_limit() },
        _ => EngineError::BudgetExceeded { fuel: b.limit() },
    }
}

/// An evaluation error as the request's error, for the cell being evaluated.
pub(crate) fn engine_error(e: EvalError, cell_id: &str) -> EngineError {
    match e {
        EvalError::NoRuntime { lang } => EngineError::NoRuntime { lang, fn_hash: String::new() },
        EvalError::UndeclaredInput { fn_name } => EngineError::UndeclaredInput { fn_name },
        EvalError::Cycle { path } => EngineError::Cycle { path },
        EvalError::DepthExceeded { limit } => EngineError::DepthExceeded { limit },
        EvalError::BudgetExceeded { fuel } => EngineError::BudgetExceeded { fuel },
        EvalError::NameUnresolved { fn_name, asof } => EngineError::NameUnresolved { fn_name, asof },
        EvalError::Store(e) => e.into(),
        EvalError::InvalidInput(e) => EngineError::Invalid(e),
        EvalError::BlobUnavailable { sha256, reason } => EngineError::BlobUnavailable { sha256, reason },
        other => EngineError::Runtime { cell_id: cell_id.to_string(), message: other.to_string() },
    }
}

/// Parses a receipt the store returned (its canonical bytes).
pub(crate) fn parse_stored(hash: &str, bytes: &[u8]) -> Result<Parsed, StoreError> {
    match Receipt::from_json(bytes) {
        Ok(r) if r.hash() == hash => Ok(Parsed { bytes: Arc::new(bytes.to_vec()), receipt: r }),
        Ok(_) | Err(_) => Err(StoreError::new(
            "receipt",
            datumesh_store::Error::Invalid(CoreError::new(
                ErrorCode::InputInvalid,
                "$",
                format!("the store's receipt {hash} does not parse to that hash"),
            )),
        )),
    }
}

/// Whether `s` is an instant (for the asof of `Resolver::evaluate`).
pub(crate) fn is_instant(s: &str) -> bool {
    lexical::check_instant(s, "$").is_ok()
}