brrrty-node 0.1.28

Runs an Eleventy site's JavaScript data files in the site's own Node, for Brrrty, an Eleventy-compatible static site generator.
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
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
//! One Node process running the worker, and the requests Brrrty makes of it.

use std::collections::{HashMap, VecDeque};
use std::fmt::Write as _;
use std::io::{BufRead, BufReader, Read, Write};
use std::path::{Path, PathBuf};
use std::process::{Child, ChildStdin, Command, Stdio};
use std::sync::atomic::AtomicBool;
use std::sync::mpsc::{self, Receiver, RecvTimeoutError};
use std::sync::{Arc, Mutex, MutexGuard};
use std::thread::JoinHandle;
use std::time::{Duration, Instant};

use brrrty_js::Value;
use brrrty_js::runtime::{
    Begun, Decoded, FilterCall, Filters, Finished, FnHandle, JsError, JsResult, JsRuntime,
    LoadedTemplate, Op, PageId, PageStep, PathPart, Pending, Refusal, RuntimeFailure, Settle,
    TemplateId, This, Waited,
};
use serde_json::{Value as Json, json};

use crate::decode::decode;
use crate::find::NodeInfo;
use crate::wire::{Added, Table};

/// The worker's source: the encoding, the computed data proxy, then the
/// protocol. Passed with `--eval`; nothing is written to disk.
const WORKER_SOURCE: &str = concat!(
    include_str!("worker/encode.mjs"),
    "\n",
    include_str!("worker/proxy.mjs"),
    "\n",
    include_str!("worker/config.mjs"),
    "\n",
    include_str!("worker/main.mjs"),
);

/// The worker's source as Node is handed it: without its comment lines.
/// Linux takes no single argument longer than 128 KiB (`MAX_ARG_STRLEN`),
/// and the comments are a quarter of the file. Each one leaves its line
/// behind, empty, so a line number in a stack trace is the file's.
fn worker_source() -> &'static str {
    static SOURCE: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
        let mut out = String::with_capacity(WORKER_SOURCE.len());
        for line in WORKER_SOURCE.split('\n') {
            if !line.trim_start().starts_with("//") {
                out.push_str(line);
            }
            out.push('\n');
        }
        out.pop();
        out
    });
    &SOURCE
}

/// The 11ty version the worker tells data files it is (`ELEVENTY_VERSION`).
const ELEVENTY_VERSION: &str = "3.1.8";

/// The stack of the thread that parses replies.
pub(crate) const REPLY_STACK: usize = 32 * 1024 * 1024;

/// How many of Node's last stderr lines a failure shows.
const STDERR_TAIL: usize = 20;

/// How long a stopped worker's stderr may take to reach its end: past this,
/// a process Node started still holds it, and Brrrty stops reading.
const STDERR_GRACE: Duration = Duration::from_millis(200);

/// How often a request is sent again when the worker lost the values it
/// refers to before giving up.
const AGAIN: usize = 16;

/// Whether a request is one the replay check runs again.
fn logged(op: &str) -> bool {
    !matches!(
        op,
        "reads" | "info" | "callsBegin" | "loadsChanged" | "exportsFinal" | "describe"
    )
}

/// How many bytes of requests may be written behind one Node hasn't
/// answered: less than any pipe holds.
const PIPE_SAFE: usize = 4000;

/// How long a wait for a reply looks for it before sleeping.
const SPIN: Duration = Duration::from_micros(60);

/// How often a wait for a reply checks that Node still runs.
const POLL: Duration = Duration::from_millis(100);

/// The first note about a slow request, then the gap between notes.
const FIRST_NOTE: Duration = Duration::from_secs(10);
const NEXT_NOTE: Duration = Duration::from_secs(30);

/// How 11ty was started, as `ELEVENTY_RUN_MODE` shows it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RunMode {
    Build,
    Serve,
}

impl RunMode {
    pub fn as_str(self) -> &'static str {
        match self {
            RunMode::Build => "build",
            RunMode::Serve => "serve",
        }
    }
}

/// Node couldn't be started.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WorkerError(pub String);

impl std::fmt::Display for WorkerError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}

/// One frame from the worker, or why there is none.
type Reply = Result<Frame, String>;

pub(crate) struct Frame {
    /// A question of the worker's own (a filter a JavaScript template
    /// calls on `this`), not a reply.
    pub question: bool,
    /// The request it answers.
    pub id: u64,
    /// The reply without its value.
    pub head: Json,
    /// The decoded `ok` value, when the reply has one.
    ok: Option<Result<Decoded, String>>,
    /// The reply as sent, without its id and clock: what the replay check
    /// compares.
    pub body: Box<str>,
    /// The clock and random values the request read, as JSON text.
    pub clock: Option<Box<str>>,
    /// The reply mentions a function a call made.
    pub made: bool,
    /// Its place among the frames Node sent. Node runs one request at a
    /// time and answers each as it ends, so this is the order it ran them.
    pub seq: u64,
}

impl Frame {
    /// The decoded `ok` value, when it crossed exactly.
    pub fn value(&self) -> Option<&Value> {
        match &self.ok {
            Some(Ok(d)) => Some(&d.value),
            _ => None,
        }
    }
}

/// One request that loaded data or a template, with its reply, for the
/// replay check.
pub(crate) struct LoadRecord {
    /// The request as sent, without its id.
    pub text: Box<str>,
    /// Whether it loads a template (`templateLoad`), a data file (`load`)
    /// or neither.
    pub kind: LoadKind,
    pub path: String,
    pub body: Box<str>,
    pub clock: Option<Box<str>>,
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum LoadKind {
    Data,
    Template,
    Other,
}

/// One request made after the data files loaded, with its reply, for the
/// replay check.
pub(crate) struct Record {
    /// The render that made it ([`brrrty_js::functions::unit`]); a request
    /// made outside one is a unit of its own.
    pub unit: u64,
    pub label: Arc<str>,
    pub op: &'static str,
    /// The function called, when the request calls one.
    pub handle: Option<u32>,
    /// The request as sent, without its id. It refers to the values the
    /// workers keep by id, which are the same in every worker.
    pub text: Box<str>,
    pub body: Box<str>,
    pub clock: Option<Box<str>>,
    /// The reply mentions a function a call made.
    pub made: bool,
    /// The dynamic definitions the request needs (see [`crate::wire`]),
    /// and whether it names a function a call made itself.
    pub dynamic: Vec<u32>,
    pub names_made: bool,
    /// The questions the request asked Brrrty, with the answers it got,
    /// both as sent.
    pub answers: Vec<(Box<str>, Box<str>)>,
    /// Nothing came back from it, and nothing will.
    pub one_way: bool,
    /// It ran after a call sent with it changed a value the worker kept:
    /// what it gave isn't used, and a replay's won't be the same.
    pub unchecked: bool,
    /// When Node ran it: its reply's place among Node's frames, and for a
    /// request with no reply, its place after the one before it. Threads
    /// take their replies in any order, so the log is put in this order
    /// before the replay check reads it.
    pub at: (u64, u32),
}

/// Where notes about a slow request go.
type Notes = Box<dyn Fn(&str) + Send>;

/// The requests of one operation, how its results are shaped, the page it
/// opens, and what to call it in a note.
type Planned = (Vec<Request<'static>>, Shape, Option<PageId>, &'static str);

/// What a worker's threads share: Node itself, and where its frames go.
pub(crate) struct Mux {
    child: Mutex<Child>,
    stdin: Mutex<Option<ChildStdin>>,
    /// Where Brrrty answers the worker's questions (a filter a JavaScript
    /// template calls on `this`).
    answers: Mutex<Option<Box<dyn Write + Send>>>,
    /// The requests whose replies a [`Ticket`] waits for, by id: where
    /// their frames go. Any other frame goes to the worker itself.
    routes: Mutex<HashMap<u64, mpsc::Sender<Reply>>>,
    /// Where slow-request notes go; stderr unless a test replaces it.
    notes: Mutex<Notes>,
    /// When the first note comes, and then how often.
    note_times: Mutex<(Duration, Duration)>,
}

fn locked<T>(m: &Mutex<T>) -> MutexGuard<'_, T> {
    m.lock().unwrap_or_else(|e| e.into_inner())
}

impl Mux {
    /// The next frame on `rx`: a reply, or a question of Node's own. `Err`
    /// is why none will come.
    fn next(
        &self,
        rx: &Receiver<Reply>,
        what: &str,
        wait: &mut Wait,
        deadline: Option<Duration>,
    ) -> Result<Frame, String> {
        // Most replies come within microseconds: look for one a few times
        // before sleeping, which costs more than the wait.
        let spun = Instant::now();
        loop {
            match rx.try_recv() {
                Ok(r) => return r,
                Err(mpsc::TryRecvError::Disconnected) => break,
                Err(mpsc::TryRecvError::Empty) if spun.elapsed() < SPIN => std::hint::spin_loop(),
                Err(mpsc::TryRecvError::Empty) => break,
            }
        }
        loop {
            match rx.recv_timeout(POLL) {
                Ok(r) => return r,
                Err(RecvTimeoutError::Timeout) => {
                    let now = Instant::now();
                    if let Some(d) = deadline
                        && now.duration_since(wait.start) > d
                    {
                        return Err(format!(
                            "{what} took more than {} s, longer than the first run allows",
                            d.as_secs()
                        ));
                    }
                    if now >= wait.next_note {
                        // Node runs one request at a time: this one may be
                        // waiting its turn behind the others.
                        let waiting = locked(&self.routes).len();
                        let behind = if waiting > 1 {
                            format!(
                                " ({waiting} requests are waiting on Node, which runs one at a time)"
                            )
                        } else {
                            String::new()
                        };
                        (locked(&self.notes))(&format!(
                            "note: still waiting on {what}, {} s{behind}",
                            wait.start.elapsed().as_secs()
                        ));
                        wait.next_note = now + locked(&self.note_times).1;
                    }
                    match wait.exited_at {
                        Some(t) if now.duration_since(t) >= POLL => {
                            return Err("Node exited without replying".to_string());
                        }
                        Some(_) => {}
                        None => {
                            if matches!(locked(&self.child).try_wait(), Ok(Some(_))) {
                                wait.exited_at = Some(now);
                            }
                        }
                    }
                }
                Err(RecvTimeoutError::Disconnected) => {
                    return Err("Node closed its output".to_string());
                }
            }
        }
    }

    /// Start waiting for a frame.
    fn wait(&self) -> Wait {
        let start = Instant::now();
        Wait {
            start,
            next_note: start + locked(&self.note_times).0,
            exited_at: None,
        }
    }

    /// Run the filter a question names, and send Node the answer. The
    /// answer as sent, or why it couldn't be.
    fn answer(&self, frame: &Frame, filters: &mut FilterCall<'_>) -> Result<Box<str>, String> {
        let name = frame
            .head
            .get("filter")
            .and_then(Json::as_str)
            .unwrap_or("")
            .to_string();
        let args = match &frame.ok {
            Some(Ok(d)) if d.is_exact() => match &d.value {
                Value::Array(a) => Ok(a.to_vec()),
                _ => Err("passes its filter something Brrrty can't read".to_string()),
            },
            Some(Ok(_)) => Err(format!(
                "passes `this.{name}` a function or a value that can't cross from JavaScript to Brrrty exactly"
            )),
            _ => Err("passes its filter something Brrrty can't read".to_string()),
        };
        let reply: Box<str> = match args.and_then(|a| filters(&name, &a)) {
            Ok(v) => json!({"ok": encode(&v)}),
            Err(why) => json!({"refused": why}),
        }
        .to_string()
        .into();
        self.send_answer(&reply)?;
        Ok(reply)
    }

    fn send_answer(&self, reply: &str) -> Result<(), String> {
        match locked(&self.answers).as_mut() {
            Some(w) => w
                .write_all(reply.as_bytes())
                .and_then(|_| w.write_all(b"\n"))
                .and_then(|_| w.flush())
                .map_err(|e| format!("cannot answer Node: {e}")),
            None => Err("Node asked a question it can't be answered on".to_string()),
        }
    }
}

/// How the results of requests sent together are handed back.
#[derive(Clone, Copy, PartialEq)]
enum Shape {
    /// As they are: the list ends with the first that isn't a value.
    List,
    /// A page opened, one function called, the page closed: the call's
    /// result.
    Once,
    /// A page opened, then what each function reads: all of them, or the
    /// first that failed (the page is then closed).
    OpenWithVars,
}

/// One request on its way.
struct Sent {
    id: u64,
    op: &'static str,
    handle: Option<u32>,
    text: String,
    path: Option<String>,
    one_way: bool,
    /// From [`Added`]: the request holds a function a call made, and the
    /// dynamic definitions it needs.
    made: bool,
    dynamic: Vec<u32>,
}

/// Requests sent together, their replies still to come. Waiting for them
/// needs no hold on the worker, so other requests go out meanwhile and
/// Node runs them back to back.
pub(crate) struct Ticket {
    mux: Arc<Mux>,
    rx: Receiver<Reply>,
    sent: Vec<Sent>,
    what: String,
    shape: Shape,
    page: Option<PageId>,
    deadline: Option<Duration>,
}

/// The replies to a [`Ticket`]'s requests, for [`Worker::settle`].
pub(crate) struct Done {
    sent: Vec<Sent>,
    /// One per request that has a reply, in order; shorter when Node
    /// stopped.
    frames: Vec<Frame>,
    /// The questions each request asked, with the answers it got.
    asked: Vec<Vec<(Box<str>, Box<str>)>>,
    failure: Option<String>,
    what: String,
    shape: Shape,
    page: Option<PageId>,
    waited: Duration,
}

impl Ticket {
    /// Wait for the replies, answering Node's questions with `filters`.
    fn collect(self, filters: &mut FilterCall<'_>) -> Done {
        let started = Instant::now();
        let mut frames = Vec::with_capacity(self.sent.len());
        let mut asked = Vec::with_capacity(self.sent.len());
        let mut failure = None;
        'all: for sent in self.sent.iter().filter(|s| !s.one_way) {
            let mut wait = self.mux.wait();
            let mut questions = Vec::new();
            loop {
                match self
                    .mux
                    .next(&self.rx, &self.what, &mut wait, self.deadline)
                {
                    Ok(f) if f.question => match self.mux.answer(&f, filters) {
                        Ok(reply) => questions.push((f.body.clone(), reply)),
                        Err(why) => {
                            failure = Some(why);
                            break 'all;
                        }
                    },
                    Ok(f) if f.id != sent.id => {
                        failure = Some(format!("a reply out of order: {}", f.head));
                        break 'all;
                    }
                    Ok(f) => {
                        frames.push(f);
                        asked.push(questions);
                        break;
                    }
                    Err(why) => {
                        failure = Some(why);
                        break 'all;
                    }
                }
            }
        }
        let mut routes = locked(&self.mux.routes);
        for sent in &self.sent {
            routes.remove(&sent.id);
        }
        drop(routes);
        Done {
            sent: self.sent,
            frames,
            asked,
            failure,
            what: self.what,
            shape: self.shape,
            page: self.page,
            waited: started.elapsed(),
        }
    }
}

impl Pending for Ticket {
    fn wait(self: Box<Self>, filters: &mut FilterCall<'_>) -> Waited {
        Waited(Box::new(self.collect(filters)))
    }
}

/// A running worker. Dropping it closes Node's stdin, waits up to 2 s for
/// it to exit, then kills it.
pub struct Worker {
    mux: Arc<Mux>,
    pid: u32,
    /// The replay check's answers, as the first run gave them, in order.
    pub(crate) answers_feed: Option<VecDeque<(Box<str>, Box<str>)>>,
    /// The questions the request in flight asked, with their answers.
    asked: Vec<(Box<str>, Box<str>)>,
    /// Brrrty's filters, for the questions a request asks.
    filters: Option<Filters>,
    replies: Receiver<Reply>,
    stderr_tail: Arc<Mutex<VecDeque<String>>>,
    stderr_thread: Option<JoinHandle<()>>,
    root: PathBuf,
    next_id: u64,
    next_page: u64,
    /// Set once the worker failed: every later request fails the same way.
    failed: Option<RuntimeFailure>,
    /// The value each load (by call mode and file) gave last.
    last_loaded: HashMap<(bool, PathBuf), Decoded>,
    /// The value a `same` reply to the request in flight stands for.
    earlier: Option<Decoded>,
    /// The loads whose value is the same every time, and whether the last
    /// load's reply said so.
    fixed: std::collections::HashSet<(bool, PathBuf)>,
    last_fixed: bool,
    /// The latest of Node's frames a request of this worker's was
    /// answered by (see [`Frame::seq`]).
    last_seq: u64,
    /// The lists and objects the workers keep (see [`crate::wire`]).
    table: Table,
    /// What the worker was started with, for the replay check.
    node: NodeInfo,
    run_mode: RunMode,
    /// Decode each reply's value. The replay check's workers don't: they
    /// compare replies as text.
    pub(crate) decode: Arc<AtomicBool>,
    /// The requests that loaded the data, in order, with their replies.
    load_log: Vec<LoadRecord>,
    /// How many of them loaded global data (the rest are template and
    /// directory data files and templates, which 11ty loads as their
    /// files finish loading).
    global_end: usize,
    /// The requests made since, with their replies.
    call_log: Vec<Record>,
    /// A call changed a value the worker kept.
    dropped: bool,
    /// Set once the data files loaded: see [`JsRuntime::begin_calls`].
    calls_begun: bool,
    /// How many function handles the data files' values made.
    load_handles: u64,
    /// The replay check's two workers, started as soon as there is
    /// something for them to check.
    replays: Option<[crate::replay::Handle; 2]>,
    /// The replay check runs (see [`Worker::start_with`]).
    order_check: bool,
    /// The longest a request took.
    slowest: Duration,
    /// How long a request may take before the worker is given up on (the
    /// replay check's workers only).
    pub(crate) deadline: Option<Duration>,
    /// The template id of the last `templateLoad` reply.
    last_template: Option<u64>,
    next_ticket: u64,
    /// What the requests cost, for `BRRRTY_JS_TRACE`.
    stats: Stats,
    /// The worker froze the data files' values when the calls began.
    frozen: bool,
    /// Why this worker was started walking, when Brrrty asked it to.
    walk: Option<String>,
    /// Why it walks them instead, when it does.
    walks: Option<String>,
}

/// What a worker's requests cost so far.
#[derive(Default)]
struct Stats {
    started: Option<Instant>,
    requests: u64,
    /// Bytes of requests, and of definitions.
    sent: u64,
    defined: u64,
    definitions: u64,
    /// Bytes of replies.
    received: u64,
    /// Time between a request going out and its reply coming in.
    waited: Duration,
}

/// One request of the first run: what to send, and what the replay check
/// needs to know of it.
struct Request<'a> {
    op: &'static str,
    /// The request as JSON text, without its id.
    text: String,
    /// The definitions it needs sent first.
    added: Added,
    /// The function it calls.
    handle: Option<u32>,
    /// The file it loads.
    path: Option<&'a Path>,
    /// Nothing comes back from it (`pageClose`).
    one_way: bool,
}

impl<'a> Request<'a> {
    fn new(op: &'static str, text: String) -> Request<'a> {
        Request {
            op,
            text,
            added: Added::default(),
            handle: None,
            path: None,
            one_way: false,
        }
    }
}

/// A wait for one frame: when it began, and when the next note is due.
pub(crate) struct Wait {
    start: Instant,
    next_note: Instant,
    /// Node exiting ends the wait even when a process it started keeps
    /// the replies' pipe open.
    exited_at: Option<Instant>,
}

impl Worker {
    /// Start Node with the worker, in `root` (the project folder), with
    /// 11ty's environment variables added to Brrrty's own.
    pub fn start(node: &NodeInfo, root: &Path, run_mode: RunMode) -> Result<Worker, WorkerError> {
        Worker::start_as(node, root, run_mode, None)
    }

    /// [`Worker::start`]; `replay` for one of the replay check's workers.
    pub(crate) fn start_as(
        node: &NodeInfo,
        root: &Path,
        run_mode: RunMode,
        replay: Option<&crate::replay::Loads>,
    ) -> Result<Worker, WorkerError> {
        Worker::start_checked(node, root, run_mode, replay, true)
    }

    /// [`Worker::start`], with or without the replay check: without it
    /// the site's JavaScript runs once, in this worker's Node alone, and
    /// nothing is kept of the calls to run them again.
    pub fn start_with(
        node: &NodeInfo,
        root: &Path,
        run_mode: RunMode,
        order_check: bool,
    ) -> Result<Worker, WorkerError> {
        Worker::start_checked(node, root, run_mode, None, order_check)
    }

    fn start_checked(
        node: &NodeInfo,
        root: &Path,
        run_mode: RunMode,
        replay: Option<&crate::replay::Loads>,
        order_check: bool,
    ) -> Result<Worker, WorkerError> {
        // The build's own worker asks whether this folder's builds walk;
        // the replay check's workers do as it did.
        let walk = match replay {
            Some(loads) => loads.walk.clone(),
            None => walk_roots().starting(root),
        };
        let mut cmd = Command::new(&node.path);
        cmd.args(["--input-type=module", "--eval", worker_source()])
            .current_dir(root)
            .env("ELEVENTY_ROOT", root)
            .env("ELEVENTY_SOURCE", "cli")
            .env("ELEVENTY_RUN_MODE", run_mode.as_str())
            .env("ELEVENTY_VERSION", ELEVENTY_VERSION)
            .stdin(Stdio::piped());
        if let Some(why) = &walk {
            cmd.env("BRRRTY_JS_EXPORTS", "walk");
            cmd.env("BRRRTY_JS_WALK_WHY", why);
        } else {
            cmd.env_remove("BRRRTY_JS_WALK_WHY");
        }
        if replay.is_some() {
            cmd.env("BRRRTY_ROLE", "replay");
        }
        let cannot =
            |e: std::io::Error| WorkerError(format!("cannot start {}: {e}", node.path.display()));
        let (mut child, frames, stderr, answers) = spawn(cmd).map_err(cannot)?;
        let pid = child.id();
        live(pid, true);
        let (tx, replies) = mpsc::channel();
        let decode = Arc::new(AtomicBool::new(true));
        let decoding = decode.clone();
        let stdin = child.stdin.take();
        let mux = Arc::new(Mux {
            child: Mutex::new(child),
            stdin: Mutex::new(stdin),
            answers: Mutex::new(answers),
            routes: Mutex::new(HashMap::new()),
            notes: Mutex::new(Box::new(|s| eprintln!("{s}"))),
            note_times: Mutex::new((FIRST_NOTE, NEXT_NOTE)),
        });
        let routing = mux.clone();
        // A reply nests up to three JSON levels per level of the value
        // (`encode.mjs` refuses values deeper than 1000), and parsing it
        // recurses once per level: a roomy stack, not serde's 128 limit.
        std::thread::Builder::new()
            .name("brrrty-node-replies".into())
            .stack_size(REPLY_STACK)
            .spawn(move || read_frames(frames, tx, decoding, &routing))
            .map_err(|e| {
                let mut child = locked(&mux.child);
                let _ = child.kill();
                let _ = child.wait();
                WorkerError(format!("cannot read Node's replies: {e}"))
            })?;
        let stderr_tail = Arc::new(Mutex::new(VecDeque::new()));
        let tail = stderr_tail.clone();
        let stderr_thread = std::thread::spawn(move || drain_stderr(stderr, tail));
        Ok(Worker {
            mux,
            pid,
            answers_feed: None,
            asked: Vec::new(),
            filters: None,
            replies,
            stderr_tail,
            stderr_thread: Some(stderr_thread),
            root: root.to_path_buf(),
            next_id: 0,
            next_page: 0,
            failed: None,
            last_loaded: HashMap::new(),
            earlier: None,
            fixed: std::collections::HashSet::new(),
            last_fixed: false,
            last_seq: 0,
            table: Table::default(),
            node: node.clone(),
            run_mode,
            decode,
            load_log: Vec::new(),
            global_end: 0,
            call_log: Vec::new(),
            dropped: false,
            calls_begun: false,
            load_handles: 0,
            replays: None,
            order_check,
            slowest: Duration::ZERO,
            deadline: None,
            last_template: None,
            next_ticket: 0,
            stats: Stats {
                started: Some(Instant::now()),
                ..Stats::default()
            },
            frozen: false,
            walk,
            walks: None,
        })
    }

    /// The request that opens a page with `data`, and the page's number.
    fn open_request(
        &mut self,
        data: &Value,
        traps: &[(String, String)],
        defaults: &[String],
    ) -> Result<(PageId, Request<'static>), String> {
        self.next_page += 1;
        let page = self.next_page;
        let mut added = Added::default();
        let mut text = String::with_capacity(1024);
        let _ = write!(text, "{{\"op\":\"pageOpen\",\"page\":{page},\"data\":");
        self.table.page_data(data, false, &mut text, &mut added)?;
        text.push_str(",\"traps\":");
        pairs(&mut text, traps);
        text.push_str(",\"defaults\":");
        crate::wire::strings(&mut text, defaults);
        text.push('}');
        Ok((
            PageId(page),
            Request {
                added,
                ..Request::new("pageOpen", text)
            },
        ))
    }

    /// Send slow-request notes to `notes` instead of stderr, the first
    /// after `first` and then every `next` (tests).
    pub fn set_notes(
        &mut self,
        notes: impl Fn(&str) + Send + 'static,
        first: Duration,
        next: Duration,
    ) {
        *locked(&self.mux.notes) = Box::new(notes);
        *locked(&self.mux.note_times) = (first, next);
    }

    /// Whether the worker froze the data files' values when the pages'
    /// calls began (`Ok`), or walks them after every call, and why (`Err`).
    pub fn exports_frozen(&self) -> Result<(), String> {
        if self.frozen {
            Ok(())
        } else {
            Err(self.walks.clone().unwrap_or_default())
        }
    }

    /// One line of what the requests cost so far (`BRRRTY_JS_TRACE`).
    fn trace(&self, at: &str) {
        let s = &self.stats;
        eprintln!(
            "note: {at}, {:.3} s after Node started: {} requests ({:.1} MB) and {} definitions ({:.1} MB) sent, {:.1} MB of replies, {:.3} s waiting on Node",
            s.started.map(|t| t.elapsed().as_secs_f64()).unwrap_or(0.0),
            s.requests,
            s.sent as f64 / 1e6,
            s.definitions,
            s.defined as f64 / 1e6,
            s.received as f64 / 1e6,
            s.waited.as_secs_f64(),
        );
    }

    /// Node's process id.
    pub fn pid(&self) -> u32 {
        self.pid
    }

    /// The project folder the worker runs in.
    pub fn root(&self) -> &Path {
        &self.root
    }

    /// Send one request and wait for its reply. `what` names it in the
    /// notes about a slow reply.
    fn request(&mut self, req: Request<'_>, what: &str) -> JsResult<Decoded> {
        let filters = self.filters.clone();
        self.request_with(
            req,
            what,
            &mut |name: &str, args: &[Value]| match &filters {
                Some(f) => f(name, args),
                None => Err(format!(
                    "calls `this.{name}` where Brrrty has no filters to run it"
                )),
            },
        )
    }

    /// [`Worker::request`], answering the worker's filter questions with
    /// `filters`.
    fn request_with(
        &mut self,
        req: Request<'_>,
        what: &str,
        filters: &mut FilterCall<'_>,
    ) -> JsResult<Decoded> {
        self.requests(vec![req], what, filters)
            .pop()
            .unwrap_or_else(|| JsResult::Failed(self.fail("a request with no reply")))
    }

    /// Several requests sent together, and their replies in order: Node
    /// runs them one after another without Brrrty in between, so they cost
    /// one round trip. The list ends with the first reply that isn't a
    /// value; the requests after it still ran.
    fn requests(
        &mut self,
        reqs: Vec<Request<'_>>,
        what: &str,
        filters: &mut FilterCall<'_>,
    ) -> Vec<JsResult<Decoded>> {
        let mut out = Vec::with_capacity(reqs.len());
        let mut reqs = reqs.into_iter().peekable();
        while reqs.peek().is_some() {
            // As many as can go in one write. Node may stop reading while
            // it waits for an answer to a question of its own, so what is
            // written past the first request must fit in the pipe.
            let mut group = Vec::new();
            let mut ahead = 0;
            while let Some(next) = reqs.peek() {
                if !group.is_empty() && ahead + next.text.len() + 48 > PIPE_SAFE {
                    break;
                }
                if !group.is_empty() {
                    ahead += next.text.len() + 48;
                }
                group.extend(reqs.next());
            }
            let ticket = match self.send(group, what, Shape::List, None) {
                Ok(t) => t,
                Err(f) => {
                    out.push(JsResult::Failed(f));
                    return out;
                }
            };
            let done = ticket.collect(filters);
            match self.settle(done) {
                Finished::Results { results, .. } => {
                    let stop = results.iter().any(|r| !matches!(r, JsResult::Ok(_)));
                    out.extend(results);
                    if stop {
                        return out;
                    }
                }
                Finished::Again => {
                    out.push(JsResult::Failed(
                        self.fail("the worker lost the values a request referred to"),
                    ));
                    return out;
                }
            }
        }
        out
    }

    /// Whether the requests can go in one write (see [`PIPE_SAFE`]).
    fn fits(reqs: &[Request<'_>]) -> bool {
        reqs.iter()
            .skip(1)
            .map(|r| r.text.len() + 48)
            .sum::<usize>()
            <= PIPE_SAFE
    }

    /// Send requests together, in one write. Their replies come to the
    /// ticket, which is waited on without the worker.
    fn send(
        &mut self,
        reqs: Vec<Request<'_>>,
        what: &str,
        shape: Shape,
        page: Option<PageId>,
    ) -> Result<Ticket, RuntimeFailure> {
        if let Some(f) = &self.failed {
            return Err(f.clone());
        }
        // Starting the replays hands them every definition so far, these
        // requests' own among them: they get each one once.
        let mut handed = false;
        if self.calls_begun && self.replays.is_none() && reqs.iter().any(|r| logged(r.op)) {
            self.start_replays();
            handed = true;
        }
        // The values the requests refer to for the first time, each in a
        // line of its own, to this worker and to the replay check's.
        for req in &reqs {
            for (line, dynamic) in &req.added.lines {
                self.stats.definitions += 1;
                self.stats.defined += line.len() as u64;
                self.write_line(line)?;
                if !dynamic
                    && !handed
                    && let Some(replays) = &self.replays
                {
                    for r in replays {
                        r.line(line.clone());
                    }
                }
            }
        }
        let (tx, rx) = mpsc::channel();
        // The requests of one ticket say so: when the worker has lost the
        // values one of them refers to, it runs none of the rest.
        self.next_ticket += 1;
        let ticket = self.next_ticket;
        let mut lines = String::new();
        let mut sent = Vec::with_capacity(reqs.len());
        {
            let mut routes = locked(&self.mux.routes);
            for req in reqs {
                self.next_id += 1;
                let id = self.next_id;
                let _ = write!(lines, "{{\"id\":{id},\"t\":{ticket},");
                lines.push_str(req.text.strip_prefix('{').unwrap_or(&req.text));
                lines.push('\n');
                if !req.one_way {
                    routes.insert(id, tx.clone());
                }
                sent.push(Sent {
                    id,
                    op: req.op,
                    handle: req.handle,
                    path: req.path.map(|p| p.to_string_lossy().into_owned()),
                    one_way: req.one_way,
                    made: req.added.made,
                    dynamic: req.added.dynamic,
                    text: req.text,
                });
            }
        }
        self.stats.sent += lines.len() as u64;
        let written = locked(&self.mux.stdin)
            .as_mut()
            .map(|s| s.write_all(lines.as_bytes()).and_then(|_| s.flush()));
        if !matches!(written, Some(Ok(()))) {
            return Err(self.fail("Node stopped reading requests"));
        }
        Ok(Ticket {
            mux: self.mux.clone(),
            rx,
            sent,
            what: what.to_string(),
            shape,
            page,
            deadline: self.deadline,
        })
    }

    /// What the replies to a ticket's requests mean: each is logged for
    /// the replay check, and the results shaped as the ticket asked.
    fn settle(&mut self, done: Done) -> Finished {
        let Done {
            sent,
            frames,
            asked,
            failure,
            what,
            shape,
            page,
            waited,
        } = done;
        self.slowest = self.slowest.max(waited);
        self.stats.waited += waited;
        let mut results: Vec<JsResult<Decoded>> = Vec::with_capacity(frames.len());
        // A call in the group changed a value the worker kept: the ones
        // after it ran on what it left, which a replay doesn't have.
        let mut unchecked = false;
        // When Node ran the last request of these that it answered.
        let mut last_at = (self.last_seq, 0u32);
        let mut frames = frames.into_iter();
        let mut asked = asked.into_iter();
        for req in sent {
            let mut frame = if req.one_way {
                None
            } else {
                match frames.next() {
                    Some(f) => Some(f),
                    // Node stopped before this one.
                    None => break,
                }
            };
            let answers = if req.one_way {
                Vec::new()
            } else {
                asked.next().unwrap_or_default()
            };
            if let Some(f) = &frame {
                self.stats.requests += 1;
                self.stats.received += f.body.len() as u64;
                if f.head.get("stale").is_some() {
                    // The worker forgot the values this refers to (a call
                    // another page made changed one): it ran nothing of
                    // this request or the ones sent with it after it. They
                    // go again, the values with them.
                    if results.iter().all(|r| matches!(r, JsResult::Ok(_))) {
                        self.table.forget();
                        return Finished::Again;
                    }
                    break;
                }
                // A step of the page before this one failed: the worker
                // ran neither this nor the ones after it.
                if f.head.get("after").is_some() {
                    break;
                }
            }
            if req.op == "templateLoad" {
                self.last_template = frame
                    .as_ref()
                    .and_then(|f| f.head.get("template"))
                    .and_then(Json::as_u64);
            }
            if req.op == "load" {
                self.last_fixed = frame
                    .as_ref()
                    .is_some_and(|f| f.head.get("fixed").is_some());
            }
            let body = frame
                .as_mut()
                .map(|f| std::mem::take(&mut f.body))
                .unwrap_or_default();
            let clock = frame.as_mut().and_then(|f| f.clock.take());
            if logged(req.op) && !self.calls_begun {
                self.load_log.push(LoadRecord {
                    text: req.text.into_boxed_str(),
                    kind: match req.op {
                        "load" => LoadKind::Data,
                        "templateLoad" => LoadKind::Template,
                        _ => LoadKind::Other,
                    },
                    path: req.path.unwrap_or_default(),
                    body,
                    clock,
                });
            } else if logged(req.op) && !self.order_check {
                // Nothing runs the calls again: nothing is kept of them.
                let dropped = frame
                    .as_ref()
                    .is_some_and(|f| f.head.get("dropShared").is_some());
                self.dropped |= dropped;
                unchecked |= dropped;
            } else if logged(req.op) {
                let (unit, label) = match brrrty_js::functions::unit() {
                    Some(u) => u,
                    None => (u64::MAX - self.call_log.len() as u64, Arc::from("")),
                };
                let dropped = frame
                    .as_ref()
                    .is_some_and(|f| f.head.get("dropShared").is_some());
                self.dropped |= dropped;
                let at = match &frame {
                    Some(f) => {
                        self.last_seq = self.last_seq.max(f.seq);
                        last_at = (f.seq, 0);
                        last_at
                    }
                    None => {
                        last_at.1 += 1;
                        last_at
                    }
                };
                self.call_log.push(Record {
                    at,
                    unit,
                    label,
                    op: req.op,
                    handle: req.handle,
                    text: req.text.into_boxed_str(),
                    body,
                    clock,
                    made: frame.as_ref().is_some_and(|f| f.made),
                    names_made: req.made || req.handle.is_some_and(|h| self.table.made_by_call(h)),
                    dynamic: req.dynamic,
                    answers,
                    one_way: req.one_way,
                    unchecked,
                });
                unchecked |= dropped;
            }
            let Some(frame) = frame else {
                continue;
            };
            let r = self.interpret(frame, &what);
            let failed = matches!(r, JsResult::Failed(_));
            results.push(r);
            if failed {
                return Finished::Results {
                    results: vec![results.pop().expect("just pushed")],
                    page: None,
                };
            }
        }
        if let Some(why) = failure {
            return Finished::Results {
                results: vec![JsResult::Failed(self.fail(&why))],
                page: None,
            };
        }
        self.shape(shape, page, results)
    }

    /// The results of an operation whose requests went one part at a time.
    fn shaped(&mut self, done: Done, results: Vec<JsResult<Decoded>>) -> Finished {
        self.shape(done.shape, done.page, results)
    }

    fn shape(
        &mut self,
        shape: Shape,
        page: Option<PageId>,
        mut results: Vec<JsResult<Decoded>>,
    ) -> Finished {
        // The list ends with the first reply that isn't a value.
        let bad = results.iter().position(|r| !matches!(r, JsResult::Ok(_)));
        match shape {
            Shape::List => {
                if let Some(bad) = bad {
                    results.truncate(bad + 1);
                }
                Finished::Results { results, page }
            }
            Shape::Once => {
                let result = match bad {
                    Some(bad) => results.swap_remove(bad),
                    // Opened, then called.
                    None if results.len() == 2 => results.swap_remove(1),
                    None => JsResult::Failed(self.fail("a page's call gave no reply")),
                };
                Finished::Results {
                    results: vec![result],
                    page,
                }
            }
            Shape::OpenWithVars => match bad {
                Some(bad) => {
                    if bad > 0
                        && let Some(page) = page
                    {
                        self.page_close(page);
                    }
                    Finished::Results {
                        results: vec![results.swap_remove(bad)],
                        page: None,
                    }
                }
                None => Finished::Results { results, page },
            },
        }
    }

    /// The requests an operation makes, how its results are shaped, and
    /// the page it opens. `Err` when a value can't be handed over.
    fn op_requests(&mut self, op: Op<'_>) -> Result<Planned, String> {
        Ok(match op {
            Op::Call {
                f,
                this,
                args,
                settle,
            } => {
                let mut added = Added::default();
                let mut text = String::with_capacity(128);
                let _ = write!(
                    text,
                    "{{\"op\":\"call\",\"handle\":{},\"await\":{}",
                    f.0,
                    settle == Settle::Await
                );
                match this {
                    This::Value(v) => {
                        text.push_str(",\"this\":");
                        self.table.argument(v, false, &mut text, &mut added)?;
                    }
                    This::Guarded(v, keys) => {
                        text.push_str(",\"this\":");
                        self.table.argument(v, false, &mut text, &mut added)?;
                        text.push_str(",\"guard\":[");
                        for (i, (k, what)) in keys.iter().enumerate() {
                            if i > 0 {
                                text.push(',');
                            }
                            text.push('[');
                            crate::wire::string(&mut text, k);
                            text.push(',');
                            crate::wire::string(&mut text, what);
                            text.push(']');
                        }
                        text.push(']');
                    }
                    This::Opaque(what) => {
                        text.push_str(",\"opaque\":");
                        crate::wire::string(&mut text, what);
                    }
                }
                text.push_str(",\"args\":[");
                for (i, a) in args.iter().enumerate() {
                    if i > 0 {
                        text.push(',');
                    }
                    self.table.argument(a, false, &mut text, &mut added)?;
                }
                text.push(']');
                // Whose template is calling: the worker finds that page's
                // own data among the collections' items by it.
                if let Some((page, number)) = brrrty_js::functions::own_page() {
                    text.push_str(",\"own\":");
                    self.table.argument(&page, false, &mut text, &mut added)?;
                    if let Some(n) = number {
                        text.push_str(",\"ownAt\":");
                        crate::wire::number(&mut text, n);
                    }
                }
                text.push('}');
                let req = Request {
                    added,
                    handle: Some(f.0),
                    ..Request::new("call", text)
                };
                (vec![req], Shape::List, None, "a data function")
            }
            Op::PageOnce {
                data,
                traps,
                defaults,
                f,
            } => {
                let (page, open) = self.open_request(data, traps, defaults)?;
                (
                    vec![open, call_request(page, f, None), close_request(page)],
                    Shape::Once,
                    None,
                    "a permalink function",
                )
            }
            Op::PageOpenWithVars {
                data,
                traps,
                defaults,
                functions,
                keys,
            } => {
                let (page, open) = self.open_request(data, traps, defaults)?;
                let mut reqs = vec![open];
                reqs.extend(functions.iter().map(|f| vars_request(page, *f, keys)));
                (
                    reqs,
                    Shape::OpenWithVars,
                    Some(page),
                    "a computed data function",
                )
            }
            Op::PageRun { page, steps } => {
                let mut reqs = Vec::with_capacity(steps.len());
                for step in steps {
                    match step {
                        PageStep::Call(f, set) => reqs.push(call_request(page, *f, *set)),
                        PageStep::Set(key, value) => {
                            let mut added = Added::default();
                            let mut text = String::with_capacity(128);
                            let _ =
                                write!(text, "{{\"op\":\"pageSet\",\"page\":{},\"key\":", page.0);
                            crate::wire::string(&mut text, key);
                            text.push_str(",\"value\":");
                            self.table.argument(value, false, &mut text, &mut added)?;
                            text.push('}');
                            reqs.push(Request {
                                added,
                                ..Request::new("pageSet", text)
                            });
                        }
                    }
                }
                (reqs, Shape::List, None, "a computed data function")
            }
            Op::Render {
                template,
                data,
                first,
                traps,
            } => {
                let mut added = Added::default();
                let mut text = String::with_capacity(1024);
                let _ = write!(
                    text,
                    "{{\"op\":\"render\",\"template\":{},\"data\":",
                    template.0
                );
                self.table.page_data(data, true, &mut text, &mut added)?;
                text.push_str(",\"traps\":");
                pairs(&mut text, traps);
                if let Some(first) = first {
                    text.push_str(",\"first\":");
                    self.table.argument(first, true, &mut text, &mut added)?;
                }
                text.push('}');
                let req = Request {
                    added,
                    ..Request::new("render", text)
                };
                (vec![req], Shape::List, None, "a JavaScript template")
            }
        })
    }

    /// [`Worker::now`], for an operation with one result.
    fn one(&mut self, op: Op<'_>, filters: &mut FilterCall<'_>) -> JsResult<Decoded> {
        self.now(op, filters)
            .pop()
            .unwrap_or_else(|| JsResult::Failed(self.fail("a request with no reply")))
    }

    /// Run an operation and wait for it here.
    fn now(&mut self, op: Op<'_>, filters: &mut FilterCall<'_>) -> Vec<JsResult<Decoded>> {
        for _ in 0..AGAIN {
            let finished = match self.begin(op, filters) {
                Begun::Done(f) => f,
                Begun::Sent(pending) => {
                    let waited = pending.wait(filters);
                    self.finish(waited)
                }
            };
            if let Finished::Results { results, .. } = finished {
                return results;
            }
        }
        vec![JsResult::Failed(self.fail(
            "the worker kept losing the values a request referred to",
        ))]
    }

    /// Write one line to Node's stdin.
    pub(crate) fn write_line(&mut self, line: &str) -> Result<(), RuntimeFailure> {
        if let Some(f) = &self.failed {
            return Err(f.clone());
        }
        let mut bytes = Vec::with_capacity(line.len() + 1);
        bytes.extend_from_slice(line.as_bytes());
        bytes.push(b'\n');
        let sent = locked(&self.mux.stdin)
            .as_mut()
            .map(|s| s.write_all(&bytes).and_then(|_| s.flush()));
        if !matches!(sent, Some(Ok(()))) {
            return Err(self.fail("Node stopped reading requests"));
        }
        Ok(())
    }

    /// Node's stdin, for a thread that writes requests ahead of their
    /// replies.
    pub(crate) fn take_stdin(&mut self) -> Option<ChildStdin> {
        locked(&self.mux.stdin).take()
    }

    /// The next request id, and the line that sends `text` under it, with
    /// the clock and random values `feed` when the replay check hands the
    /// first run's back.
    pub(crate) fn line_for(&mut self, text: &str, feed: Option<&str>) -> (u64, String) {
        self.next_id += 1;
        let id = self.next_id;
        let mut line = String::with_capacity(text.len() + 32);
        let _ = write!(line, "{{\"id\":{id},");
        if let Some(feed) = feed {
            line.push_str("\"feed\":");
            line.push_str(feed);
            line.push(',');
        }
        line.push_str(text.strip_prefix('{').unwrap_or(text));
        (id, line)
    }

    /// Send one request and wait for its reply frame.
    pub(crate) fn exchange(
        &mut self,
        text: &str,
        feed: Option<&str>,
        what: &str,
    ) -> Result<Frame, RuntimeFailure> {
        self.exchange_with(text, feed, what, &mut |name: &str, _: &[Value]| {
            Err(format!(
                "calls `this.{name}` where Brrrty has no filters to run it"
            ))
        })
    }

    /// Answer one of the worker's questions: run the filter it names, or,
    /// in the replay check, give the first run's answer.
    pub(crate) fn answer(
        &mut self,
        frame: &Frame,
        filters: &mut FilterCall<'_>,
    ) -> Result<(), String> {
        let reply: Box<str> = match self.answers_feed.as_mut() {
            Some(feed) => {
                let reply: Box<str> = match feed.pop_front() {
                    Some((q, a)) if q == frame.body => a,
                    _ => r#"{"refused":"asks for a filter the first run didn't ask for at this point"}"#
                        .into(),
                };
                self.mux.send_answer(&reply)?;
                reply
            }
            None => self.mux.answer(frame, filters)?,
        };
        self.asked.push((frame.body.clone(), reply));
        Ok(())
    }

    /// [`Worker::exchange`], answering the worker's filter questions with
    /// `filters` while it waits.
    pub(crate) fn exchange_with(
        &mut self,
        text: &str,
        feed: Option<&str>,
        what: &str,
        filters: &mut FilterCall<'_>,
    ) -> Result<Frame, RuntimeFailure> {
        let (id, line) = self.line_for(text, feed);
        self.write_line(&line)?;
        let mut wait = self.wait();
        let frame = loop {
            match self.next_frame(what, &mut wait) {
                Ok(f) if f.question => {
                    if let Err(why) = self.answer(&f, filters) {
                        return Err(self.fail(&why));
                    }
                }
                Ok(f) => break f,
                Err(f) => return Err(f),
            }
        };
        let took = wait.start.elapsed();
        self.slowest = self.slowest.max(took);
        self.stats.requests += 1;
        self.stats.sent += line.len() as u64;
        self.stats.received += frame.body.len() as u64;
        self.stats.waited += took;
        if frame.id != id {
            let head = frame.head.clone();
            return Err(self.fail(&format!("a reply out of order: {head}")));
        }
        Ok(frame)
    }

    /// Start waiting for a frame.
    pub(crate) fn wait(&self) -> Wait {
        self.mux.wait()
    }

    /// The next frame from Node that no ticket waits for: a reply, or a
    /// question of its own.
    pub(crate) fn next_frame(
        &mut self,
        what: &str,
        wait: &mut Wait,
    ) -> Result<Frame, RuntimeFailure> {
        if let Some(f) = &self.failed {
            return Err(f.clone());
        }
        let mux = self.mux.clone();
        mux.next(&self.replies, what, wait, self.deadline)
            .map_err(|why| self.fail(&why))
    }

    /// What a reply means.
    fn interpret(&mut self, frame: Frame, what: &str) -> JsResult<Decoded> {
        let Frame {
            head: reply, ok, ..
        } = frame;
        if reply.get("changedArgument").is_some() {
            return JsResult::Failed(self.fail(&format!(
                "{what} changes the data 11ty hands it (the config's global data and `eleventy`), which 11ty then merges into every page's data; Brrrty can't follow that, so return the data instead"
            )));
        }
        if let Some(p) = reply.get("protocol") {
            return JsResult::Failed(self.fail(&format!("the worker refused a request: {p}")));
        }
        if let Some(why) = reply.get("again") {
            // The worker froze the data files' values and can't vouch for
            // what it then saw: this folder's builds walk them from now on.
            walk_roots().sent_back(&self.root, why.as_str().unwrap_or(""));
            let mut f = self.fail(&format!(
                "the JavaScript worker has to check the data files' values after every call ({})",
                why.as_str().unwrap_or("")
            ));
            f.again = true;
            self.failed = Some(f.clone());
            return JsResult::Failed(f);
        }
        if let Some(t) = reply.get("thrown") {
            return JsResult::Thrown(js_error(t, &self.root));
        }
        if let Some(m) = reply.get("invalid") {
            return JsResult::Thrown(JsError {
                message: m.as_str().unwrap_or("").to_string(),
                invalid: true,
                ..JsError::default()
            });
        }
        if reply.get("dropShared").is_some() {
            // The worker forgot them; ids go on rising, so none is reused.
            self.table.forget();
        }
        if let Some(r) = reply.get("refused") {
            return JsResult::Refused(Refusal {
                why: r.as_str().unwrap_or("").to_string(),
                impure: reply.get("impure").and_then(Json::as_bool) == Some(true),
            });
        }
        let earlier = self.earlier.take();
        if reply.get("same").is_some() {
            return match earlier {
                Some(d) => JsResult::Ok(d),
                None => JsResult::Failed(self.fail("a reply that repeats a value never sent")),
            };
        }
        match ok {
            Some(Ok(d)) => JsResult::Ok(d),
            Some(Err(e)) => JsResult::Failed(self.fail(&format!("a value Brrrty can't read: {e}"))),
            None => JsResult::Failed(self.fail(&format!("a reply with no value: {reply}"))),
        }
    }

    /// Start the replay check's two workers: they load the data again, in
    /// their own orders, while this one goes on, and are sent every value
    /// this one defines as it defines it.
    fn start_replays(&mut self) {
        if !self.order_check {
            return;
        }
        let loads = Arc::new(crate::replay::Loads {
            node: self.node.clone(),
            root: self.root.clone(),
            run_mode: self.run_mode,
            loads: std::mem::take(&mut self.load_log),
            global_end: self.global_end,
            load_handles: self.load_handles,
            walk: self.walk.clone(),
            // The replays load every file again, several in one request:
            // none of it should take longer than all of it did here.
            took: self.stats.started.map(|t| t.elapsed()).unwrap_or_default(),
        });
        let replays = [
            crate::replay::Handle::start(loads.clone(), true),
            crate::replay::Handle::start(loads, false),
        ];
        for (line, dynamic) in &self.table.lines {
            if !dynamic {
                for r in &replays {
                    r.line(line.clone());
                }
            }
        }
        self.replays = Some(replays);
    }

    /// The worker can't go on: say why, with Node's exit status when it
    /// exited, and the last lines of its stderr.
    pub(crate) fn fail(&mut self, why: &str) -> RuntimeFailure {
        drop(locked(&self.mux.stdin).take());
        let deadline = Instant::now() + Duration::from_secs(2);
        let status = loop {
            let mut child = locked(&self.mux.child);
            match child.try_wait() {
                Ok(Some(s)) => break Some(s),
                Ok(None) if Instant::now() < deadline => {
                    drop(child);
                    std::thread::sleep(Duration::from_millis(10))
                }
                _ => {
                    let _ = child.kill();
                    break child.wait().ok();
                }
            }
        };
        self.stop_group();
        self.finish_stderr();
        let message = match status {
            Some(s) => match (s.code(), signal_of(&s)) {
                (Some(code), _) => format!("Node exited with status {code} ({why})"),
                (None, Some(sig)) => format!("Node was killed by signal {sig} ({why})"),
                _ => why.to_string(),
            },
            None => why.to_string(),
        };
        let stopped = STOPPING.load(std::sync::atomic::Ordering::SeqCst);
        let message = if stopped {
            "Brrrty was stopped, and stopped Node with it".to_string()
        } else {
            message
        };
        let failure = RuntimeFailure {
            stopped,
            again: false,
            message,
            stderr_tail: self
                .stderr_tail
                .lock()
                .map(|t| t.iter().cloned().collect())
                .unwrap_or_default(),
        };
        self.failed = Some(failure.clone());
        failure
    }

    /// Stop Node at once (the replay check, at a difference).
    pub(crate) fn stop(&mut self) {
        {
            let mut child = locked(&self.mux.child);
            let _ = child.kill();
            let _ = child.wait();
        }
        self.stop_group();
        if self.failed.is_none() {
            self.failed = Some(RuntimeFailure {
                message: "the replay check stopped its Node".into(),
                stderr_tail: Vec::new(),
                stopped: false,
                again: false,
            });
        }
    }

    /// Kill what is left of Node's process group: processes the site's
    /// code started and left running. One started in a session of its
    /// own (`detached`) is out of reach and keeps running, as under 11ty.
    fn stop_group(&mut self) {
        #[cfg(unix)]
        kill_group(self.pid);
        live(self.pid, false);
    }

    /// Wait briefly for the rest of Node's stderr, then stop waiting: a
    /// process Node started may hold it open for as long as it runs.
    fn finish_stderr(&mut self) {
        if let Some(t) = self.stderr_thread.take() {
            let deadline = Instant::now() + STDERR_GRACE;
            while !t.is_finished() && Instant::now() < deadline {
                std::thread::sleep(Duration::from_millis(5));
            }
            if t.is_finished() {
                let _ = t.join();
            }
        }
    }

    fn shown(&self, file: &Path) -> String {
        file.strip_prefix(&self.root)
            .unwrap_or(file)
            .display()
            .to_string()
    }
}

/// The filters for requests that run computed data: the worker's own.
fn no_filters() -> impl FnMut(&str, &[Value]) -> Result<Value, String> {
    |name: &str, _: &[Value]| {
        Err(format!(
            "calls `this.{name}` where Brrrty has no filters to run it"
        ))
    }
}

fn close_request(page: PageId) -> Request<'static> {
    let text = op_text("pageClose", |t| {
        let _ = write!(t, ",\"page\":{}", page.0);
    });
    Request {
        one_way: true,
        ..Request::new("pageClose", text)
    }
}

fn call_request(page: PageId, f: FnHandle, set: Option<&str>) -> Request<'static> {
    let text = op_text("pageCall", |t| {
        let _ = write!(t, ",\"page\":{},\"handle\":{},\"set\":", page.0, f.0);
        match set {
            Some(key) => crate::wire::string(t, key),
            None => t.push_str("null"),
        }
    });
    Request {
        handle: Some(f.0),
        ..Request::new("pageCall", text)
    }
}

fn vars_request(page: PageId, f: FnHandle, keys: &[String]) -> Request<'static> {
    let text = op_text("pageVars", |t| {
        let _ = write!(t, ",\"page\":{},\"handle\":{},\"keys\":", page.0, f.0);
        crate::wire::strings(t, keys);
    });
    Request {
        handle: Some(f.0),
        ..Request::new("pageVars", text)
    }
}

/// `{"op":"<op>"` and the fields `rest` writes, closed.
fn op_text(op: &str, rest: impl FnOnce(&mut String)) -> String {
    let mut t = String::with_capacity(96);
    let _ = write!(t, "{{\"op\":\"{op}\"");
    rest(&mut t);
    t.push('}');
    t
}

/// `[[key, why], ...]` as JSON text.
fn pairs(out: &mut String, traps: &[(String, String)]) {
    out.push('[');
    for (i, (k, why)) in traps.iter().enumerate() {
        if i > 0 {
            out.push(',');
        }
        out.push('[');
        crate::wire::string(out, k);
        out.push(',');
        crate::wire::string(out, why);
        out.push(']');
    }
    out.push(']');
}

fn unit(r: JsResult<Decoded>) -> JsResult<()> {
    match r {
        JsResult::Ok(_) => JsResult::Ok(()),
        JsResult::Thrown(e) => JsResult::Thrown(e),
        JsResult::Refused(r) => JsResult::Refused(r),
        JsResult::Failed(f) => JsResult::Failed(f),
    }
}

impl JsRuntime for Worker {
    fn set_argument(&mut self, arg: &Value) -> JsResult<()> {
        let text = op_text("setArg", |t| {
            let _ = write!(t, ",\"value\":{}", encode(arg));
        });
        unit(self.request(Request::new("setArg", text), "the data argument"))
    }

    fn load(&mut self, file: &Path, call: bool) -> JsResult<Decoded> {
        let what = self.shown(file);
        let key = (call, file.to_path_buf());
        // The worker says `same` when the value is the one it sent last
        // time for this file, and the decoded value is used again: equal
        // data, and the same objects, which Brrrty compares cheaply.
        self.earlier = self.last_loaded.get(&key).cloned();
        // What a module exports as it is (no function to call, no getter)
        // is the same every time: one request, however many templates the
        // file applies to.
        if self.fixed.contains(&key)
            && let Some(d) = self.earlier.take()
        {
            return JsResult::Ok(d);
        }
        let text = op_text("load", |t| {
            t.push_str(",\"path\":");
            crate::wire::string(t, &file.to_string_lossy());
            let _ = write!(t, ",\"call\":{call}");
        });
        let r = self.request(
            Request {
                path: Some(file),
                ..Request::new("load", text)
            },
            &what,
        );
        if let JsResult::Ok(d) = &r {
            if self.last_fixed {
                self.fixed.insert(key.clone());
            }
            self.last_loaded.insert(key, d.clone());
        }
        r
    }

    fn load_config(&mut self, file: &Path) -> JsResult<Decoded> {
        let what = self.shown(file);
        let text = op_text("configLoad", |t| {
            t.push_str(",\"path\":");
            crate::wire::string(t, &file.to_string_lossy());
        });
        self.earlier = None;
        self.request(
            Request {
                path: Some(file),
                ..Request::new("configLoad", text)
            },
            &what,
        )
    }

    fn call(
        &mut self,
        f: FnHandle,
        this: This<'_>,
        args: &[Value],
        settle: Settle,
    ) -> JsResult<Decoded> {
        let op = Op::Call {
            f,
            this,
            args,
            settle,
        };
        self.one(op, &mut no_filters())
    }

    fn begin(&mut self, op: Op<'_>, filters: &mut FilterCall<'_>) -> Begun {
        let (reqs, shape, page, what) = match self.op_requests(op) {
            Ok(r) => r,
            Err(why) => {
                return Begun::Done(Finished::Results {
                    results: vec![JsResult::Refused(Refusal::new(why))],
                    page: None,
                });
            }
        };
        if !Worker::fits(&reqs) {
            // Too much to send ahead of the first reply: one part at a
            // time, here.
            let results = self.requests(reqs, what, filters);
            let done = Done {
                sent: Vec::new(),
                frames: Vec::new(),
                asked: Vec::new(),
                failure: None,
                what: what.to_string(),
                shape,
                page,
                waited: Duration::ZERO,
            };
            return Begun::Done(self.shaped(done, results));
        }
        match self.send(reqs, what, shape, page) {
            Ok(ticket) => Begun::Sent(Box::new(ticket)),
            Err(f) => Begun::Done(Finished::Results {
                results: vec![JsResult::Failed(f)],
                page: None,
            }),
        }
    }

    fn finish(&mut self, waited: Waited) -> Finished {
        match waited.0.downcast::<Done>() {
            Ok(done) => self.settle(*done),
            Err(_) => Finished::Results {
                results: vec![JsResult::Failed(
                    self.fail("replies that aren't this worker's"),
                )],
                page: None,
            },
        }
    }

    fn page_open(
        &mut self,
        data: &Value,
        traps: &[(String, String)],
        defaults: &[String],
    ) -> JsResult<PageId> {
        let (page, open) = match self.open_request(data, traps, defaults) {
            Ok(r) => r,
            Err(why) => return JsResult::Refused(Refusal::new(why)),
        };
        match self.request(open, "a page's data") {
            JsResult::Ok(_) => JsResult::Ok(PageId(page.0)),
            JsResult::Thrown(e) => JsResult::Thrown(e),
            JsResult::Refused(r) => JsResult::Refused(r),
            JsResult::Failed(f) => JsResult::Failed(f),
        }
    }

    fn page_vars(&mut self, page: PageId, f: FnHandle, keys: &[String]) -> JsResult<Vec<String>> {
        let r = self.request(vars_request(page, f, keys), "a computed data function");
        match r {
            JsResult::Ok(d) => match strings(&d.value) {
                Some(list) => JsResult::Ok(list),
                None => JsResult::Failed(self.fail(&format!("pageVars gave {:?}", d.value))),
            },
            JsResult::Thrown(e) => JsResult::Thrown(e),
            JsResult::Refused(r) => JsResult::Refused(r),
            JsResult::Failed(f) => JsResult::Failed(f),
        }
    }

    fn page_call(&mut self, page: PageId, f: FnHandle, set: Option<&str>) -> JsResult<Decoded> {
        self.request(call_request(page, f, set), "a computed data function")
    }

    fn page_set(&mut self, page: PageId, key: &str, value: &Value) -> JsResult<()> {
        let mut added = Added::default();
        let mut text = String::with_capacity(128);
        let _ = write!(text, "{{\"op\":\"pageSet\",\"page\":{},\"key\":", page.0);
        crate::wire::string(&mut text, key);
        text.push_str(",\"value\":");
        if let Err(why) = self.table.argument(value, false, &mut text, &mut added) {
            return JsResult::Refused(Refusal::new(why));
        }
        text.push('}');
        unit(self.request(
            Request {
                added,
                ..Request::new("pageSet", text)
            },
            "a page's data",
        ))
    }

    fn page_close(&mut self, page: PageId) {
        // Nothing comes back from it.
        let _ = self.requests(
            vec![close_request(page)],
            "a page's data",
            &mut no_filters(),
        );
    }

    fn page_once(
        &mut self,
        data: &Value,
        traps: &[(String, String)],
        defaults: &[String],
        f: FnHandle,
    ) -> JsResult<Decoded> {
        let op = Op::PageOnce {
            data,
            traps,
            defaults,
            f,
        };
        self.one(op, &mut no_filters())
    }

    fn page_run(&mut self, page: PageId, steps: &[PageStep<'_>]) -> Vec<JsResult<Decoded>> {
        let filters = self.filters.clone();
        self.now(
            Op::PageRun { page, steps },
            &mut |name: &str, args: &[Value]| match &filters {
                Some(f) => f(name, args),
                None => Err(format!(
                    "calls `this.{name}` where Brrrty has no filters to run it"
                )),
            },
        )
    }

    fn loads_changed(&mut self) -> JsResult<Vec<(PathBuf, Decoded)>> {
        self.global_end = self.load_log.len();
        let d = match self.request(
            Request::new("loadsChanged", op_text("loadsChanged", |_| {})),
            "the data files",
        ) {
            JsResult::Ok(d) => d,
            JsResult::Thrown(e) => return JsResult::Thrown(e),
            JsResult::Refused(r) => return JsResult::Refused(r),
            JsResult::Failed(f) => return JsResult::Failed(f),
        };
        let Value::Array(items) = &d.value else {
            return JsResult::Failed(self.fail("loadsChanged gave something other than a list"));
        };
        let mut out = Vec::new();
        for (i, item) in items.iter().enumerate() {
            let (Value::String(p), value) = (item.get("path"), item.get("value")) else {
                return JsResult::Failed(self.fail("loadsChanged gave an item without a path"));
            };
            let under = |at: &[PathPart]| -> Option<Vec<PathPart>> {
                match at {
                    [PathPart::Index(j), PathPart::Key(k), rest @ ..]
                        if *j == i && k == "value" =>
                    {
                        Some(rest.to_vec())
                    }
                    _ => None,
                }
            };
            out.push((
                PathBuf::from(&*p),
                Decoded {
                    value,
                    functions: d
                        .functions
                        .iter()
                        .filter_map(|(at, h)| under(at).map(|a| (a, *h)))
                        .collect(),
                    refused: d
                        .refused
                        .iter()
                        .filter_map(|(at, w)| under(at).map(|a| (a, w.clone())))
                        .collect(),
                },
            ));
        }
        JsResult::Ok(out)
    }

    fn begin_calls(&mut self) -> JsResult<()> {
        let frame = match self.exchange(&op_text("callsBegin", |_| {}), None, "the data files") {
            Ok(f) => f,
            Err(f) => return JsResult::Failed(f),
        };
        let head = &frame.head;
        self.load_handles = head.get("handles").and_then(Json::as_u64).unwrap_or(0);
        self.frozen = head.get("exports").and_then(Json::as_str) == Some("frozen");
        self.walks = head.get("walks").and_then(Json::as_str).map(str::to_string);
        self.table.load_handles = Some(u32::try_from(self.load_handles).unwrap_or(u32::MAX));
        self.calls_begun = true;
        if trace() {
            self.trace("the data files loaded");
            match self.exports_frozen() {
                Ok(()) => {
                    eprintln!("note: the data files' values are frozen while pages call functions")
                }
                Err(why) => {
                    eprintln!("note: the data files' values are walked after every call ({why})")
                }
            }
        }
        let changed: Vec<&str> = head
            .get("changed")
            .and_then(Json::as_array)
            .map(|a| a.iter().filter_map(Json::as_str).collect())
            .unwrap_or_default();
        if !changed.is_empty() {
            return JsResult::Refused(Refusal {
                why: format!(
                    "the data {} {} gave changed while template and directory data files loaded; 11ty's pages see it as it was when each page was placed, which depends on the order 11ty loads them",
                    if changed.len() == 1 { "file" } else { "files" },
                    changed.join(", ")
                ),
                impure: true,
            });
        }
        if head.get("protos").and_then(Json::as_bool) == Some(true) {
            return JsResult::Refused(Refusal {
                why: "a JavaScript data file changes one of JavaScript's built-in prototypes (`Array.prototype` and the like), which 11ty's templates use and Brrrty's don't".into(),
                impure: true,
            });
        }
        // Template and directory data files and templates load in an order
        // of 11ty's own: with any, the check runs even when no page calls
        // a function, so it starts now.
        if self.load_log.len() > self.global_end {
            self.start_replays();
        }
        JsResult::Ok(())
    }

    fn set_filters(&mut self, filters: Filters) {
        self.filters = Some(filters);
    }

    fn template_setup(&mut self, filters: &[String], node: &[(String, u32)]) -> JsResult<()> {
        let text = op_text("templateSetup", |t| {
            t.push_str(",\"filters\":");
            crate::wire::strings(t, filters);
            // The config's own filters, which Node runs itself.
            t.push_str(",\"node\":[");
            for (i, (name, handle)) in node.iter().enumerate() {
                if i > 0 {
                    t.push(',');
                }
                t.push('[');
                crate::wire::string(t, name);
                let _ = write!(t, ",{handle}]");
            }
            t.push(']');
        });
        unit(self.request(
            Request::new("templateSetup", text),
            "the JavaScript templates' setup",
        ))
    }

    fn load_template(&mut self, file: &Path, layout: bool) -> JsResult<LoadedTemplate> {
        let what = self.shown(file);
        let text = op_text("templateLoad", |t| {
            t.push_str(",\"path\":");
            crate::wire::string(t, &file.to_string_lossy());
            let _ = write!(t, ",\"layout\":{layout}");
        });
        self.last_template = None;
        let r = self.request(
            Request {
                path: Some(file),
                ..Request::new("templateLoad", text)
            },
            &what,
        );
        match r {
            JsResult::Ok(data) => match self.last_template {
                Some(id) => JsResult::Ok(LoadedTemplate {
                    id: TemplateId(id),
                    data,
                }),
                None => JsResult::Failed(self.fail("templateLoad gave no template id")),
            },
            JsResult::Thrown(e) => JsResult::Thrown(e),
            JsResult::Refused(r) => JsResult::Refused(r),
            JsResult::Failed(f) => JsResult::Failed(f),
        }
    }

    fn render(
        &mut self,
        template: TemplateId,
        data: &Value,
        first: Option<&Value>,
        traps: &[(String, String)],
        filters: &mut FilterCall<'_>,
    ) -> JsResult<Decoded> {
        let op = Op::Render {
            template,
            data,
            first,
            traps,
        };
        self.one(op, filters)
    }

    fn verify(&mut self) -> JsResult<()> {
        // What the calls changed of the data files' values comes first: it
        // explains whatever the replay would find.
        match self.request(
            Request::new("exportsFinal", op_text("exportsFinal", |_| {})),
            "the data files' values",
        ) {
            JsResult::Ok(_) => {}
            other @ (JsResult::Refused(_) | JsResult::Failed(_)) => {
                return match other {
                    JsResult::Refused(r) => JsResult::Refused(r),
                    JsResult::Failed(f) => JsResult::Failed(f),
                    _ => JsResult::Ok(()),
                };
            }
            JsResult::Thrown(e) => return JsResult::Thrown(e),
        }
        // Nothing to check: no page called a function, and no template or
        // template or directory data file loaded.
        if self.frozen {
            walk_roots().froze(&self.root);
        }
        let Some(replays) = self.replays.take() else {
            return JsResult::Ok(());
        };
        let calls = Arc::new(crate::replay::Calls {
            records: {
                // The order Node ran them in, not the order the render
                // threads took their replies: the replay check's orders are
                // made from this one, and must differ from it.
                let mut records = std::mem::take(&mut self.call_log);
                records.sort_by_key(|r| r.at);
                records
            },
            load_handles: self.load_handles,
            slowest: self.slowest,
            dynamic: std::mem::take(&mut self.table.dynamic),
            lines: self.table.lines.clone(),
            dropped: self.dropped,
        });
        if trace() {
            self.trace("the pages' calls ended");
        }
        let checking = Instant::now();
        let [a, b] = replays;
        a.run(calls.clone());
        b.run(calls);
        let (a, b) = (a.finish(), b.finish());
        if trace() {
            eprintln!(
                "note: the replay check took {:.3} s after the build's own calls",
                checking.elapsed().as_secs_f64()
            );
        }
        match a.and(b) {
            Ok(()) => JsResult::Ok(()),
            Err(crate::replay::Failure::Differs(why, handle)) => {
                let named = handle
                    .and_then(|h| {
                        let text = op_text("describe", |t| {
                            let _ = write!(t, ",\"handle\":{h}");
                        });
                        self.exchange(&text, None, "a function's name").ok()
                    })
                    .and_then(|f| match f.value() {
                        Some(Value::String(s)) => Some(s.to_string()),
                        _ => None,
                    })
                    .unwrap_or_else(|| "a function from JavaScript data".to_string());
                JsResult::Refused(Refusal {
                    why: why.replace("{fn}", &named),
                    impure: true,
                })
            }
            Err(crate::replay::Failure::Runtime(f)) => JsResult::Failed(f),
            Err(crate::replay::Failure::Again(why)) => {
                walk_roots().sent_back(&self.root, &why);
                let mut f = self.fail(&format!(
                    "the JavaScript worker has to check the data files' values after every call ({why})"
                ));
                f.again = true;
                self.failed = Some(f.clone());
                JsResult::Failed(f)
            }
        }
    }

    fn reads(&mut self) -> JsResult<Vec<PathBuf>> {
        match self.request(
            Request::new("reads", op_text("reads", |_| {})),
            "the list of files read",
        ) {
            JsResult::Ok(d) => match strings(&d.value) {
                Some(list) => JsResult::Ok(list.into_iter().map(PathBuf::from).collect()),
                None => JsResult::Failed(self.fail(&format!("reads gave {:?}", d.value))),
            },
            JsResult::Thrown(e) => JsResult::Thrown(e),
            JsResult::Refused(r) => JsResult::Refused(r),
            JsResult::Failed(f) => JsResult::Failed(f),
        }
    }
}

impl Drop for Worker {
    fn drop(&mut self) {
        if let Some(mut stdin) = locked(&self.mux.stdin).take() {
            let _ = stdin.write_all(b"{\"op\":\"bye\"}\n");
        }
        let deadline = Instant::now() + Duration::from_secs(2);
        loop {
            let mut child = locked(&self.mux.child);
            match child.try_wait() {
                Ok(Some(_)) => break,
                Ok(None) if Instant::now() < deadline => {
                    drop(child);
                    std::thread::sleep(Duration::from_millis(5))
                }
                _ => {
                    let _ = child.kill();
                    let _ = child.wait();
                    break;
                }
            }
        }
        self.stop_group();
        self.finish_stderr();
    }
}

/// The project folders whose builds found they must walk the data files'
/// values after each call rather than freeze them (see the worker's
/// "Freezing"): a build there starts that way.
fn walk_roots() -> MutexGuard<'static, WalkMemo> {
    static ROOTS: std::sync::LazyLock<Mutex<WalkMemo>> =
        std::sync::LazyLock::new(|| Mutex::new(WalkMemo::default()));
    ROOTS.lock().unwrap_or_else(|e| e.into_inner())
}

/// Which folders' builds walk, and for how long. A build that was sent
/// back runs again walking; the build after that tries the freeze again
/// (under `brrrty serve` the code may have been fixed). A folder sent
/// back again and again walks for twice as many builds each time, up to
/// 64, so a site that can't be frozen stops paying for the try.
#[derive(Default)]
pub(crate) struct WalkMemo {
    roots: HashMap<PathBuf, Walks>,
}

struct Walks {
    why: String,
    /// How many builds still start walking.
    left: u32,
    /// How many it was the last time the folder was sent back.
    last: u32,
}

impl WalkMemo {
    const MOST: u32 = 64;

    /// A build of `root` was sent back.
    pub(crate) fn sent_back(&mut self, root: &Path, why: &str) {
        let w = self.roots.entry(root.to_path_buf()).or_insert(Walks {
            why: String::new(),
            left: 0,
            last: 0,
        });
        // Several of one build's requests may say so.
        if w.left > 0 {
            return;
        }
        w.why = why.to_string();
        w.last = (w.last * 2).clamp(1, Self::MOST);
        w.left = w.last;
    }

    /// A build of `root` starts: why it walks, if it does.
    pub(crate) fn starting(&mut self, root: &Path) -> Option<String> {
        let w = self.roots.get_mut(root)?;
        if w.left == 0 {
            return None;
        }
        w.left -= 1;
        Some(w.why.clone())
    }

    /// A build of `root` froze and passed.
    pub(crate) fn froze(&mut self, root: &Path) {
        self.roots.remove(root);
    }
}

/// Whether to say on stderr what the JavaScript worker's requests cost
/// (`BRRRTY_JS_TRACE`, for whoever looks into a slow build).
fn trace() -> bool {
    static ON: std::sync::LazyLock<bool> =
        std::sync::LazyLock::new(|| std::env::var_os("BRRRTY_JS_TRACE").is_some());
    *ON
}

/// The process ids of the workers running now. Each leads its own process
/// group, so a terminal's Ctrl-C doesn't reach it: [`stop_all`] does.
static LIVE: Mutex<Vec<u32>> = Mutex::new(Vec::new());

/// Set once [`stop_all`] ran: a worker failing after that was stopped.
static STOPPING: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

fn live(pid: u32, running: bool) {
    let mut l = LIVE.lock().unwrap_or_else(|e| e.into_inner());
    l.retain(|p| *p != pid);
    if running {
        l.push(pid);
    }
}

/// Kill every running worker and what it started, at once: for Ctrl-C,
/// SIGTERM and a panic. Safe to call from a signal handler thread.
pub fn stop_all() {
    STOPPING.store(true, std::sync::atomic::Ordering::SeqCst);
    let l = LIVE.lock().unwrap_or_else(|e| e.into_inner());
    #[cfg(unix)]
    for pid in l.iter() {
        kill_group(*pid);
    }
}

/// Kill the worker with this process id and what it started.
pub(crate) fn stop_pid(pid: u32) {
    #[cfg(unix)]
    kill_group(pid);
    // Elsewhere there is no group: Windows stops the tree by name.
    #[cfg(windows)]
    {
        let _ = Command::new("taskkill")
            .args(["/PID", &pid.to_string(), "/T", "/F"])
            .stdin(Stdio::null())
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status();
    }
    #[cfg(not(any(unix, windows)))]
    let _ = pid;
}

#[cfg(unix)]
fn kill_group(pid: u32) {
    if let Ok(pid) = i32::try_from(pid) {
        // SAFETY: killpg only sends a signal, to the group Node leads.
        unsafe {
            libc::killpg(pid, libc::SIGKILL);
        }
    }
}

/// Start the worker. Its replies come on a pipe of their own, which Node
/// inherits under the descriptor number it has here, and its stdout joins
/// its stderr: a child process or logger writing to fd 1 is only noise, as
/// it is under 11ty.
#[cfg(unix)]
fn spawn(mut cmd: Command) -> std::io::Result<Spawned> {
    use std::os::fd::AsRawFd;
    use std::os::unix::process::CommandExt;
    let (frames_r, frames_w) = std::io::pipe()?;
    // A copy is numbered 3 or above, clear of the standard three.
    let frames_w = frames_w.try_clone()?;
    let (answers_r, answers_w) = std::io::pipe()?;
    let answers_r = answers_r.try_clone()?;
    let (err_r, err_w) = std::io::pipe()?;
    let fd = frames_w.as_raw_fd();
    let answers_fd = answers_r.as_raw_fd();
    cmd.stdout(err_w.try_clone()?)
        .stderr(err_w)
        .env("BRRRTY_FRAMES_FD", fd.to_string())
        .env("BRRRTY_ANSWERS_FD", answers_fd.to_string())
        // A group of its own, so what Node starts can be stopped with it.
        .process_group(0);
    // SAFETY: fcntl is async-signal-safe, and the descriptor is this
    // process's own, open until after the spawn.
    unsafe {
        cmd.pre_exec(move || {
            // Keep it open across exec in Node only.
            if libc::fcntl(fd, libc::F_SETFD, 0) == -1
                || libc::fcntl(answers_fd, libc::F_SETFD, 0) == -1
            {
                return Err(std::io::Error::last_os_error());
            }
            Ok(())
        });
    }
    let child = cmd.spawn();
    // The parent's write ends close here, so the reads end when Node's do.
    drop(cmd);
    drop(frames_w);
    drop(answers_r);
    Ok((
        child?,
        Box::new(frames_r),
        Box::new(err_r),
        Some(Box::new(answers_w)),
    ))
}

/// A started Node: the process, its replies, its stderr, and where
/// Brrrty answers its questions (none where that pipe can't be passed).
type Spawned = (
    Child,
    Box<dyn Read + Send>,
    Box<dyn Read + Send>,
    Option<Box<dyn Write + Send>>,
);

/// Start the worker, its replies on stdout.
#[cfg(not(unix))]
fn spawn(mut cmd: Command) -> std::io::Result<Spawned> {
    let mut child = cmd
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .env("BRRRTY_FRAMES_FD", "1")
        .spawn()?;
    let stdout = child.stdout.take().expect("piped stdout");
    let stderr = child.stderr.take().expect("piped stderr");
    Ok((child, Box::new(stdout), Box::new(stderr), None))
}

#[cfg(unix)]
fn signal_of(s: &std::process::ExitStatus) -> Option<i32> {
    use std::os::unix::process::ExitStatusExt;
    s.signal()
}

#[cfg(not(unix))]
fn signal_of(_: &std::process::ExitStatus) -> Option<i32> {
    None
}

/// Read `\x1eBRRRTY <kind> <id> <clock length> <body length>\n<clock><body>`
/// frames until Node closes its output. Anything else is a protocol error,
/// sent once.
fn read_frames(stdout: impl Read, tx: mpsc::Sender<Reply>, decoding: Arc<AtomicBool>, mux: &Mux) {
    read_frames_to(stdout, &tx, decoding, mux);
    // No more frames: whoever waits for one learns so.
    locked(&mux.routes).clear();
}

fn read_frames_to(
    stdout: impl Read,
    tx: &mpsc::Sender<Reply>,
    decoding: Arc<AtomicBool>,
    mux: &Mux,
) {
    let mut r = BufReader::with_capacity(1 << 16, stdout);
    let mut seq = 0u64;
    loop {
        seq += 1;
        let mut header = Vec::new();
        match r.read_until(b'\n', &mut header) {
            Ok(0) => return,
            Ok(_) => {}
            Err(e) => {
                let _ = tx.send(Err(format!("cannot read Node's output: {e}")));
                return;
            }
        }
        let fields = header
            .strip_prefix(b"\x1eBRRRTY ")
            .and_then(|h| h.strip_suffix(b"\n"))
            .and_then(|h| std::str::from_utf8(h).ok())
            .and_then(|h| {
                let mut it = h.split(' ');
                let kind = it.next()?;
                let id = it.next()?.parse::<u64>().ok()?;
                let clock = it.next()?.parse::<usize>().ok()?;
                let body = it.next()?.parse::<usize>().ok()?;
                (it.next().is_none() && matches!(kind, "r" | "f" | "q"))
                    .then_some((kind, id, clock, body))
            });
        let Some((kind, id, clock_len, body_len)) = fields else {
            let shown = String::from_utf8_lossy(&header);
            let _ = tx.send(Err(format!(
                "something other than the worker wrote to its output: {:?}",
                shown.chars().take(80).collect::<String>()
            )));
            return;
        };
        let mut read = |len: usize| -> Result<String, String> {
            let mut bytes = vec![0; len];
            r.read_exact(&mut bytes)
                .map_err(|e| format!("a cut-off reply: {e}"))?;
            String::from_utf8(bytes).map_err(|_| "a reply that isn't UTF-8".to_string())
        };
        let parts = read(clock_len).and_then(|c| read(body_len).map(|b| (c, b)));
        let (clock, body) = match parts {
            Ok(p) => p,
            Err(e) => {
                let _ = tx.send(Err(e));
                return;
            }
        };
        let decode_ok = decoding.load(std::sync::atomic::Ordering::Relaxed);
        let parsed = parse_reply(body.as_bytes()).map(|mut head| {
            let value = head.as_object_mut().and_then(|o| o.remove("ok"));
            let ok = if decode_ok || kind == "q" {
                value.as_ref().map(decode)
            } else {
                None
            };
            Frame {
                question: kind == "q",
                id,
                head,
                ok,
                body: body.into_boxed_str(),
                clock: (!clock.is_empty()).then(|| clock.into_boxed_str()),
                made: kind == "f",
                seq,
            }
        });
        // A frame a ticket waits for goes to it; any other to the worker.
        let route = match &parsed {
            Ok(f) => locked(&mux.routes).get(&f.id).cloned(),
            Err(_) => None,
        };
        let sent = match route {
            Some(to) => to.send(parsed).is_ok(),
            None => tx.send(parsed).is_ok(),
        };
        if !sent && locked(&mux.routes).is_empty() {
            return;
        }
    }
}

/// A list of strings, as `reads` and `varsUsed` reply.
fn strings(v: &Value) -> Option<Vec<String>> {
    match v {
        Value::Array(a) => a
            .iter()
            .map(|s| match s {
                Value::String(s) => Some(s.to_string()),
                _ => None,
            })
            .collect(),
        _ => None,
    }
}

/// One reply body, without serde's nesting limit (see [`REPLY_STACK`]).
pub(crate) fn parse_reply(body: &[u8]) -> Result<Json, String> {
    let mut de = serde_json::Deserializer::from_slice(body);
    de.disable_recursion_limit();
    let v = serde::Deserialize::deserialize(&mut de)
        .and_then(|v: Json| de.end().map(|()| v))
        .map_err(|e| format!("a reply that isn't JSON: {e}"))?;
    Ok(v)
}

/// Pass Node's stderr through to Brrrty's, as it comes, keeping the last
/// lines for a failure report.
fn drain_stderr(stderr: impl Read, tail: Arc<Mutex<VecDeque<String>>>) {
    let mut r = BufReader::new(stderr);
    let mut line = Vec::new();
    let mut err = std::io::stderr();
    loop {
        line.clear();
        match r.read_until(b'\n', &mut line) {
            Ok(0) | Err(_) => return,
            Ok(_) => {
                let _ = err.write_all(&line);
                if let Ok(mut t) = tail.lock() {
                    if t.len() == STDERR_TAIL {
                        t.pop_front();
                    }
                    t.push_back(String::from_utf8_lossy(&line).trim_end().to_string());
                }
            }
        }
    }
}

/// The site's error, placed: a SyntaxError at the header Node prints for
/// it, anything else at the first stack frame inside the project and
/// outside `node_modules`.
fn js_error(t: &Json, root: &Path) -> JsError {
    let s = |k: &str| t.get(k).and_then(Json::as_str).unwrap_or("").to_string();
    let stack = s("stack");
    let place = crate::stack::syntax_header(&s("inspect"))
        .or_else(|| crate::stack::pick(&crate::stack::frames(&stack), root));
    JsError {
        name: s("name"),
        message: s("message"),
        file: place.as_ref().map(|p| p.file.clone()),
        line: place.as_ref().map(|p| p.line),
        column: place.and_then(|p| p.column),
        stack,
        invalid: false,
    }
}

/// A Brrrty value in the worker's wire forms.
pub fn encode(v: &Value) -> Json {
    match v {
        Value::Undefined => json!({"$u": 1}),
        Value::Null => Json::Null,
        Value::Bool(b) => Json::Bool(*b),
        Value::Number(n) => {
            if n.is_nan() {
                json!({"$n": "NaN"})
            } else if *n == f64::INFINITY {
                json!({"$n": "Infinity"})
            } else if *n == f64::NEG_INFINITY {
                json!({"$n": "-Infinity"})
            } else if *n == 0.0 && n.is_sign_negative() {
                json!({"$n": "-0"})
            } else {
                serde_json::Number::from_f64(*n).map_or(Json::Null, Json::Number)
            }
        }
        Value::String(s) => Json::String(s.to_string()),
        Value::Array(a) => Json::Array(a.iter().map(encode).collect()),
        Value::Object(o) => {
            json!({"$o": o.iter().map(|(k, v)| json!([k, encode(v)])).collect::<Vec<_>>()})
        }
        Value::Date(d) => json!({"$d": d.timestamp_millis()}),
        Value::Function(h) => json!({"$f": h.0}),
    }
}

#[cfg(test)]
mod source {
    /// Linux refuses an argument of 128 KiB or more ("Argument list too
    /// long"), on any machine, whatever else is on the command line.
    /// macOS doesn't, so this is where a worker grown too long shows.
    #[test]
    fn the_worker_fits_in_one_argument_with_room_to_grow() {
        let source = super::worker_source();
        assert!(
            source.len() < 120 * 1024,
            "the worker's source is {} bytes without its comments",
            source.len()
        );
        // Line numbers are the file's.
        assert_eq!(source.lines().count(), super::WORKER_SOURCE.lines().count());
        assert!(!source.lines().any(|l| l.trim_start().starts_with("//")));
    }
}

#[cfg(test)]
mod walk_memo {
    use super::WalkMemo;
    use std::path::Path;

    /// A build sent back runs again walking, and the one after tries the
    /// freeze again; a folder sent back every time walks for longer and
    /// longer, and one that freezes is forgotten.
    #[test]
    fn a_folder_sent_back_walks_for_a_while_not_for_good() {
        let root = Path::new("/site");
        let mut m = WalkMemo::default();
        assert_eq!(m.starting(root), None);
        m.sent_back(root, "why");
        // Several requests of the one build may say so.
        m.sent_back(root, "another request");
        assert_eq!(m.starting(root).as_deref(), Some("why"));
        // The build after the rerun tries again.
        assert_eq!(m.starting(root), None);
        m.sent_back(root, "again");
        assert_eq!(m.starting(root).as_deref(), Some("again"));
        assert_eq!(m.starting(root).as_deref(), Some("again"));
        assert_eq!(m.starting(root), None);
        m.sent_back(root, "third");
        let walked = std::iter::from_fn(|| m.starting(root)).count();
        assert_eq!(walked, 4);
        for _ in 0..10 {
            m.sent_back(root, "more");
            while m.starting(root).is_some() {}
        }
        m.sent_back(root, "most");
        assert_eq!(std::iter::from_fn(|| m.starting(root)).count(), 64);
        // A build that froze and passed starts over.
        m.froze(root);
        m.sent_back(root, "new");
        assert_eq!(std::iter::from_fn(|| m.starting(root)).count(), 1);
    }
}

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

    /// A definition that refers to a value the worker forgot is skipped
    /// (Brrrty wrote it before it heard of the change), and so is all of
    /// it: the ids it had handed out before it met the forgotten value
    /// held objects half read, with wire markers still inside. A request
    /// that names one is told it is stale, like any forgotten value, and
    /// the worker goes on.
    #[test]
    fn a_skipped_definition_leaves_none_of_its_values_behind() {
        let node = match crate::find::find_node(None) {
            Ok(n) => n,
            Err(e) => {
                assert!(std::env::var_os("CI").is_none(), "CI needs Node: {e}");
                eprintln!("skipped: {e}");
                return;
            }
        };
        let t = tempfile::tempdir().unwrap();
        let file = t.path().join("h.js");
        std::fs::write(
            &file,
            "module.exports = { poke(o) { o.k = 2; return ''; }, show(v) { return JSON.stringify(v); } };",
        )
        .unwrap();
        let mut w = Worker::start(&node, t.path(), RunMode::Build).expect("starts");
        assert!(matches!(w.load(&file, true), JsResult::Ok(_)));
        w.exchange(r#"{"op":"callsBegin"}"#, None, "the calls' start")
            .expect("begins");
        let call = |w: &mut Worker, handle: u32, arg: u32| {
            w.exchange(
                &format!(
                    r#"{{"op":"call","handle":{handle},"await":false,"this":{{"$u":1}},"args":[{{"$R":{arg}}}]}}"#
                ),
                None,
                "a call",
            )
            .expect("answers")
        };
        // A kept value, which `poke` changes: the worker forgets it.
        w.write_line(r#"{"op":"def","id":50,"v":{"k":1},"n":1}"#)
            .unwrap();
        let poked = call(&mut w, 0, 50);
        assert!(poked.head.get("dropShared").is_some(), "{}", poked.body);
        // Written before Brrrty heard: it refers to the forgotten value,
        // after two values of its own.
        w.write_line(r#"{"op":"def","id":60,"v":{"a":{"x":1},"o":{"$R":50}},"n":2}"#)
            .unwrap();
        for id in [60, 61, 50] {
            let shown = call(&mut w, 1, id);
            assert_eq!(
                shown.head.get("stale"),
                Some(&Json::Bool(true)),
                "value {id}: {}",
                shown.body
            );
        }
        // And the worker is none the worse: the values sent again work.
        w.write_line(r#"{"op":"def","id":70,"v":{"a":{"x":1},"o":{"k":1}},"n":3}"#)
            .unwrap();
        let shown = call(&mut w, 1, 70);
        assert!(
            shown.body.contains(r#"{\"a\":{\"x\":1},\"o\":{\"k\":1}}"#),
            "{}",
            shown.body
        );
    }
}