summaryrefslogtreecommitdiff
path: root/test/monkey9p.zig
blob: db0c555e97e95a158430229e872b9f2e0028998e (plain) (blame)
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
//! The 9P monkey at T1 (docs/next-tests.md): random 9P requests against an
//! in-process session (test/session.zig), and after each step the
//! documented rules checked against what the requests answered and what
//! /log said meanwhile. It was test/monkey9p.py, which drove a real
//! `pardes --detach` over its socket.
//!
//!     zig build monkey-session -Dplatform=tui -- [--seed N]... [--steps M] [--only i,j,k] [--no-shrink] [-v]
//!     zig build monkey-session-smoke -Dplatform=tui       (part of behaviour-test)
//!
//! A step's op is drawn from the seed alone: the panes, columns and held
//! files it names are tokens (`{p3}`: /index's 4th serial, mod its length,
//! when the op runs), so `--only` replays any subset of a seed's steps, and
//! a failure shrinks by delta debugging over them. Each run is a child of
//! this program (itself again) in a throwaway root that is its HOME, XDG
//! directories and working directory, so a panic or a hang is a failure
//! the parent reports and shrinks like any other.
//!
//! What T1 leaves out: Session performs no effects (no shell runs, nothing
//! is saved or dumped, a Restore hangs nothing up), and no 9P engine stands
//! in front of the core, so the engine's own refusals (a write on an open
//! for reading, a directory opened to write, a mode bit missing) and
//! OTRUNC's truncation first are made here, as cloud9's fs.zig makes them.
const std = @import("std");
const Session = @import("session").Session;
const libc = std.c;
const Allocator = std.mem.Allocator;
const Random = std.Random;

extern "c" fn mkdtemp(template: [*:0]u8) ?[*:0]u8;
extern "c" fn execve(path: [*:0]const u8, argv: [*:null]const ?[*:0]const u8, envp: [*:null]const ?[*:0]const u8) c_int;
extern "c" fn system(cmd: [*:0]const u8) c_int;
extern "c" fn _exit(status: c_int) noreturn;
extern "c" var environ: [*:null]?[*:0]u8;

const OREAD: u8 = 0;
const OWRITE: u8 = 1;
const ORDWR: u8 = 2;
const OTRUNC: u8 = 16;
const DMDIR: u32 = 0x80000000;
// As the Python client asked: a Twrite carries msize less 23, a Tread msize less 11.
const msize = 65536 + 24;
const read_max = msize - 11;
const write_max = msize - 23;
const room = msize - 24;
/// A step that has not finished in this long is a hang.
const hang_ms = 30_000;

const fixtures = [_][2][]const u8{
    .{ "alpha.txt", "alpha beta gamma\nsecond line\n\nthird after a blank\n" },
    .{ "utf8.txt", "na\xc3\xafve caf\xc3\xa9 \xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e e\xcc\x81 \xf0\x9f\x98\x80\nline two\n" },
    .{ "crlf.txt", "one\r\ntwo\r\nthree\r\n" },
    .{ "bad.txt", "hello \xff\xfe world\nlatin-1 caf\xe9\ntruncated \xe6\x97 here\n" },
    .{ "empty.txt", "" },
    .{ "noeol.txt", "no newline at the end" },
};
const fixture_names = [_][]const u8{ "alpha.txt", "utf8.txt", "crlf.txt", "bad.txt", "empty.txt", "noeol.txt", "fresh.txt" };

// ---------------------------------------------------------------------------
// Input pools.

const unsafe = "/~$`;|&<>.\\*?![]{}'\"";
const runes = [_][]const u8{ "\xc3\xa9", "e\xcc\x81", "\xe6\x97\xa5\xe6\x9c\xac", "\xf0\x9f\x98\x80", "\xce\x94", "\xc2\xa0", "\xe2\x80\x8b", "\xc2\x80", "\xc2\x9f" };
const odd = [_][]const u8{ "", "\n", "\r\n", "\x00", "\xff", "\xfe\xff", "\xe6\x97", "\xc0\x80", "\x1b[2J", "\x7f", "\t", "\n\n\n", " ", "  \n" };
const words = [_][]const u8{
    "alpha",      "beta",      "foo",            "bar",           "x",           "hello",   "zz",       "0",       "1",        "-1",        "99999999999",
    "on",         "off",       "follow",         "follow new",    "lock",        "unlock",  "clean",    "dirty",   "show",     "dot=addr",  "addr=dot",
    "limit=addr", "nomark",    "mark",           "get",           "put",         "del",     "delete",   "name",    "cleartag", "answer",    "answer -",
    "size 80 24", "size 20 6", "size 5000 5000", "winsize 80 24", "winsize 0 0", "sig INT", "sig TERM", "sig HUP", "sig KILL", "sig BOGUS", "exec",
    "Wrap on",    "Wrap off",
};
const shell_lines = [_][]const u8{ "true", "false", "echo hi", "echo m9p", "sleep 1", "ls", "cat nonexist", "printf abc", "exit 3", "seq 1 2000", "yes | head -n 3000", "date", "pwd", "env | wc" };
const builtins = [_][]const u8{
    "Look",    "Exec",     "Kill",        "Dump",    "Msg",     "NextColor", "Themes", "DumpThemes",      "TreeContext",   "PdfFit", "PdfTint",   "PdfSections", "Petscii",
    "Palette", "Ascii",    "Save",        "New",     "Newcol",  "Del",       "Filter", "Mode",            "Togglettymode", "Edit",   "Undo",      "Redo",        "Collapse",
    "Delcol",  "DelAbove", "DelBelow",    "Term",    "Repl",    "Joincol",   "Config", "LocationsConfig", "Messages",      "Mini",   "Find",      "Grep",        "Left",
    "Down",    "Up",       "Right",       "Back",    "Forward", "Last",      "Recent", "Jumplist",        "Colors",        "Wrap",   "Tagbottom", "Debug",       "FocusTint",
    "Verbose", "SyntaxBold",  "DumpDir", "Theme",   "Get",       "Put",    "Undo",            "Redo",          "Zerox",  "Snarf",     "Paste",       "Cut",
    "Sort",    "Font",     "Local",
};
const pane_files = [_][]const u8{ "name", "body", "tag", "ctl", "addr", "dot", "limit", "data", "xdata", "sel", "dirty", "mark", "scroll", "errors", "event", "look", "exec", "tagexec", "pty", "pty/run", "pty/data", "pty/ctl", "pty/status" };
const root_files = [_][]const u8{ "README", "index", "status", "focus", "ctl", "commands", "recent", "look", "exec", "log", "screen", "listeners", "layout", "tag", "tagexec", "pane", "pane/new", "col", "" };
const long_x: [300]u8 = @splat('x');
const col_files = [_][]const u8{ "tag", "ctl", "exec" };
const writable = [_][]const u8{ "ctl", "exec", "look", "tag", "tagexec", "focus", "body", "data", "xdata", "addr", "dot", "limit", "name", "sel", "dirty", "mark", "scroll", "errors", "event", "pty/run", "pty/data", "pty/ctl", "log" };
// sam's commands without the ones that reach files or shells.
const edit_chars = "abcdghijklmopqstuvxyz0123456789 ACEGHIJKLMNOPQRSTUVWXYZ_-";

fn oom() noreturn {
    @panic("out of memory");
}
fn cat(a: Allocator, parts: []const []const u8) []u8 {
    return std.mem.concat(a, u8, parts) catch oom();
}
fn print(a: Allocator, comptime f: []const u8, args: anytype) []u8 {
    return std.fmt.allocPrint(a, f, args) catch oom();
}
fn chance(r: Random) f64 {
    return r.float(f64);
}
fn below(r: Random, n: usize) usize {
    return r.uintLessThan(usize, n);
}
fn range(r: Random, lo: usize, hi: usize) usize {
    return r.intRangeLessThan(usize, lo, hi);
}
fn pick(r: Random, xs: anytype) @TypeOf(xs[0]) {
    return xs[below(r, xs.len)];
}
fn repeat(a: Allocator, unit: []const u8, n: usize) []u8 {
    const out = a.alloc(u8, unit.len * n) catch oom();
    for (0..n) |i| @memcpy(out[i * unit.len ..][0..unit.len], unit);
    return out;
}
fn base(path: []const u8) []const u8 {
    return path[if (std.mem.lastIndexOfScalar(u8, path, '/')) |i| i + 1 else 0..];
}

/// No path or shell metacharacters, whatever else it holds.
fn shellSafe(a: Allocator, data: []const u8) []u8 {
    var out: std.ArrayList(u8) = .empty;
    for (data) |b| if (std.mem.indexOfScalar(u8, unsafe, b) == null) out.append(a, b) catch oom();
    return out.items;
}

fn oddBytes(r: Random, a: Allocator, n: usize) []u8 {
    var out: std.ArrayList(u8) = .empty;
    while (out.items.len < n) {
        const x = chance(r);
        if (x < 0.5) {
            out.appendSlice(a, pick(r, &words)) catch oom();
        } else if (x < 0.7) {
            out.appendSlice(a, pick(r, &runes)) catch oom();
        } else if (x < 0.9) {
            out.appendSlice(a, pick(r, &odd)) catch oom();
        } else out.append(a, r.int(u8)) catch oom();
        if (chance(r) < 0.5) out.append(a, ' ') catch oom();
    }
    return out.items[0..n];
}

/// Plain or odd bytes, sometimes huge, with or without a newline.
fn payload(r: Random, a: Allocator, maxlen: usize) []u8 {
    const x = chance(r);
    var data: []const u8 = if (x < 0.35)
        pick(r, &words)
    else if (x < 0.55)
        oddBytes(r, a, range(r, 0, maxlen))
    else if (x < 0.62)
        pick(r, &odd)
    else if (x < 0.66)
        repeat(a, cat(a, &.{ pick(r, &words), " " }), range(r, 100, 20000))
    else if (x < 0.7)
        repeat(a, "x", pick(r, &[_]usize{ 255, 256, 1023, 1024, 1025, 65535, 65536, 70000 }))
    else
        cat(a, &.{ pick(r, &words), " ", oddBytes(r, a, range(r, 0, 12)) });
    if (chance(r) < 0.6 and !std.mem.endsWith(u8, data, "\n")) data = cat(a, &.{ data, pick(r, &[_][]const u8{ "\n", "\n", "\r\n", "\n\n" }) });
    return shellSafe(a, data);
}

fn weirdName(r: Random, a: Allocator) []const u8 {
    const x = chance(r);
    if (x < 0.3) return repeat(a, "x", pick(r, &[_]usize{ 255, 256, 300, 1000 }));
    if (x < 0.5) return pick(r, &[_][]const u8{ "..", ".", "", " ", "new", "NEW", "01", "1 ", "\n", "a\x00b" });
    if (x < 0.7) return pick(r, &runes);
    const out = a.alloc(u8, range(r, 1, 20)) catch oom();
    for (out) |*b| b.* = @intCast(range(r, 1, 256));
    std.mem.replaceScalar(u8, out, '/', '_');
    return out;
}

/// A live pane as a token (`{pN}`), a stale one (`{gN}`: a serial seen and
/// since closed), or an impossible serial, literally.
fn pickSerial(r: Random, a: Allocator) []const u8 {
    const x = chance(r);
    if (x < 0.85) return print(a, "{{p{d}}}", .{below(r, 64)});
    if (x < 0.95) return print(a, "{{g{d}}}", .{below(r, 64)});
    return pick(r, &[_][]const u8{ "0", "1", "999", "4294967295", "4294967296", "-1" });
}

fn pickCol(r: Random, a: Allocator) []const u8 {
    if (chance(r) < 0.85) return print(a, "{{c{d}}}", .{below(r, 16)});
    return pick(r, &[_][]const u8{ "0", "99", "4294967295" });
}

fn randomPath(r: Random, a: Allocator, write: bool) []const u8 {
    const x = chance(r);
    if (x < 0.6) return print(a, "/pane/{s}/{s}", .{ pickSerial(r, a), if (write) pick(r, &writable) else pick(r, &pane_files) });
    if (x < 0.75) return cat(a, &.{ "/", if (write) pick(r, &[_][]const u8{ "ctl", "exec", "look", "tag", "tagexec", "focus", "log" }) else pick(r, &root_files) });
    if (x < 0.85) return print(a, "/col/{s}/{s}", .{ pickCol(r, a), pick(r, &col_files) });
    if (x < 0.93) return print(a, "/pane/{s}/{s}", .{ pickSerial(r, a), weirdName(r, a) });
    var out: []const u8 = "";
    for (0..range(r, 1, 4)) |_| out = cat(a, &.{ out, "/", weirdName(r, a) });
    return out;
}

/// The payload a file is likely to take, often mutated.
fn dataFor(r: Random, a: Allocator, path: []const u8) []const u8 {
    const b = base(path);
    const is = struct {
        fn any(name: []const u8, set: []const []const u8) bool {
            for (set) |s| if (std.mem.eql(u8, name, s)) return true;
            return false;
        }
    }.any;
    if (chance(r) < 0.25) return payload(r, a, 64);
    if (is(b, &.{ "addr", "dot", "limit", "sel" })) return address(r, a);
    if (std.mem.eql(u8, b, "ctl") and std.mem.endsWith(u8, path, "pty/ctl"))
        return pick(r, &[_][]const u8{ "sig INT\n", "sig TERM\n", "sig HUP\n", "sig QUIT\n", "sig KILL\n", "winsize 80 24\n", "winsize 1 1\n", "winsize 99999 3\n", "exec\n", "sig\n" });
    if (is(b, &.{ "ctl", "exec", "tagexec" })) return builtinLine(r, a);
    if (std.mem.eql(u8, b, "run")) return cat(a, &.{ shellSafe(a, pick(r, &shell_lines)), "\n" });
    if (std.mem.eql(u8, b, "data") and std.mem.indexOf(u8, path, "/pty/") != null)
        return cat(a, &.{ shellSafe(a, pick(r, &shell_lines)), pick(r, &[_][]const u8{ "\r", "\n", "", "\x03", "\x04" }) });
    if (std.mem.eql(u8, b, "event")) return eventRecord(r, a);
    if (std.mem.eql(u8, b, "focus")) return cat(a, &.{ pickSerial(r, a), pick(r, &[_][]const u8{ "\n", "" }) });
    if (std.mem.eql(u8, b, "look"))
        return cat(a, &.{ shellSafe(a, pick(r, &[_][]const u8{ "alpha", "alpha.txt", "alpha.txt:2", "utf8.txt:#3", "nothere", "bad.txt", "crlf.txt:1", "gamma", "beta" })), "\n" });
    if (is(b, &.{ "dirty", "mark", "scroll" })) return pick(r, &[_][]const u8{ "0", "1", "0\n", "1\n", "2\n", "", "yes\n" });
    if (std.mem.eql(u8, b, "log")) return pick(r, &[_][]const u8{ "follow\n", "follow new\n", "follow", "nope\n" });
    return payload(r, a, 200);
}

fn mutate(r: Random, a: Allocator, data: []const u8) []u8 {
    var d: std.ArrayList(u8) = .empty;
    d.appendSlice(a, data) catch oom();
    for (0..range(r, 1, 4)) |_| {
        if (d.items.len == 0) {
            d.appendSlice(a, pick(r, &words)) catch oom();
            continue;
        }
        const i = below(r, d.items.len);
        const x = chance(r);
        if (x < 0.3) {
            _ = d.orderedRemove(i);
        } else if (x < 0.55) {
            const o = pick(r, &odd);
            d.insertSlice(a, i, shellSafe(a, if (o.len == 0) "\x00" else o)) catch oom();
        } else if (x < 0.75) {
            const j = range(r, i, d.items.len + 1);
            d.insertSlice(a, i, a.dupe(u8, d.items[i..j]) catch oom()) catch oom();
        } else d.insertSlice(a, i, pick(r, &runes)) catch oom();
    }
    return d.items;
}

fn builtinLine(r: Random, a: Allocator) []const u8 {
    const word = pick(r, &builtins);
    const x = chance(r);
    var line: []const u8 = if (x < 0.4)
        word
    else if (x < 0.7)
        cat(a, &.{ word, " ", pick(r, &words) })
    else if (x < 0.8)
        return cat(a, &.{ "Edit ", editCommand(r, a), "\n" }) // Edit text keeps its / delimiters
    else
        mutate(r, a, cat(a, &.{ word, " ", pick(r, &words) }));
    if (chance(r) < 0.1) line = shellSafe(a, pick(r, &shell_lines));
    if (chance(r) < 0.8) line = cat(a, &.{ line, "\n" });
    return shellSafe(a, line);
}

fn editText(r: Random, a: Allocator) []const u8 {
    const out = a.alloc(u8, range(r, 0, 8)) catch oom();
    for (out) |*b| b.* = pick(r, edit_chars);
    return if (chance(r) < 0.3) cat(a, &.{ out, pick(r, &runes) }) else out;
}

fn regex(r: Random, a: Allocator) []const u8 {
    const first = editText(r, a);
    var out: []const u8 = if (first.len == 0) "a" else first;
    for (0..range(r, 0, 4)) |_| {
        const alts = [_][]const u8{ "(a|b)", "^", "$", "[a-z]", "[^ ]", ".*", "\\n", "[\xc3\xa9\xe6\x97\xa5]", "x+", "(", "[", "\\", "a**", "(((a)))", "[z-a]", "" };
        const k = below(r, alts.len);
        const alt = if (k == alts.len - 1) cat(a, &.{ repeat(a, "a|", range(r, 1, 300)), "b" }) else alts[k];
        out = cat(a, &.{ out, alt, editText(r, a) });
    }
    return out;
}

/// sam addresses: valid, near-valid and garbage.
fn address(r: Random, a: Allocator) []const u8 {
    const simple = [_][]const u8{ "0", "$", ".", ",", ";", "#0", "#3", "#99999", "1", "2", "3:2", "1:1", "0:0", "#1,#2", "1,$", "2,1", "-", "+", "-1", "+2", "#-1", "99999", "1:99", "?a?" };
    const x = chance(r);
    const addr: []const u8 = if (x < 0.4)
        pick(r, &simple)
    else if (x < 0.7)
        cat(a, &.{ "/", regex(r, a), "/" })
    else if (x < 0.85)
        cat(a, &.{ pick(r, &simple), pick(r, &[_][]const u8{ ",", ";", "+", "-" }), pick(r, &(simple ++ [_][]const u8{"/a/"})) })
    else
        mutate(r, a, cat(a, &.{ pick(r, &simple), "/", regex(r, a), "/" }));
    return cat(a, &.{ addr, pick(r, &[_][]const u8{ "", "\n" }) });
}

fn editCommand(r: Random, a: Allocator) []const u8 {
    var cmd: []const u8 = switch (below(r, 19)) {
        0 => cat(a, &.{ ",x/", regex(r, a), "/c/", editText(r, a), "/" }),
        1 => cat(a, &.{ "s/", regex(r, a), "/", editText(r, a), "/g" }),
        2 => cat(a, &.{ "a/", editText(r, a), "/" }),
        3 => cat(a, &.{ "i/", editText(r, a), "/" }),
        4 => cat(a, &.{ "c/", editText(r, a), "/" }),
        5 => "d",
        6 => ",d",
        7 => cat(a, &.{ ",y/", regex(r, a), "/d" }),
        8 => cat(a, &.{ ",x/", regex(r, a), "/g/", regex(r, a), "/d" }),
        9 => cat(a, &.{ ",x/", regex(r, a), "/v/", regex(r, a), "/c/", editText(r, a), "/" }),
        10 => "1,2m$",
        11 => "1t0",
        12 => "k",
        13 => "p",
        14 => "=",
        15 => "u",
        16 => cat(a, &.{ "{\na/", editText(r, a), "/\ni/", editText(r, a), "/\n}" }),
        17 => cat(a, &.{ ",x/\\n/a/", editText(r, a), "/" }),
        else => cat(a, &.{ "0,$s/", regex(r, a), "/&&/g" }),
    };
    if (chance(r) < 0.4) cmd = cat(a, &.{ std.mem.trimEnd(u8, address(r, a), "\n"), cmd });
    if (chance(r) < 0.2) {
        // Mutation never brings in a banned command letter or shell character.
        var out: std.ArrayList(u8) = .empty;
        for (mutate(r, a, cmd)) |b| if (std.mem.indexOfScalar(u8, "werfbBDn!<>|", b) == null) out.append(a, b) catch oom();
        cmd = out.items;
    }
    return cmd;
}

fn eventRecord(r: Random, a: Allocator) []const u8 {
    const text = shellSafe(a, editText(r, a));
    var rec: []const u8 = print(a, "{c}{c}{d} {d} {d} {d} {s}\n", .{
        pick(r, "EFKMZ"),                pick(r, "xXlLiIdDZ"),
        pick(r, &[_]i64{ 0, 1, 5, 10 }), pick(r, &[_]i64{ 0, 2, 7, 1 << 40, -1 }),
        pick(r, &[_]u8{ 0, 1, 2, 8 }),   text.len,
        text,
    });
    if (chance(r) < 0.3) rec = mutate(r, a, rec);
    return shellSafe(a, rec);
}

// ---------------------------------------------------------------------------
// Ops: what a step does, drawn from the seed alone.

const Chunks = union(enum) { whole, bytes, sizes: [4]usize };
const Op = union(enum) {
    /// open(path, mode); write data in the given chunk sizes; clunk.
    write: struct { path: []const u8, data: []const u8, mode: u8 = OWRITE, chunks: Chunks = .whole, offset: ?u64 = null },
    /// open(path, mode); read from offset in chunks of count until EOF or `reads` reads.
    read: struct { path: []const u8, count: u32 = 8192, offset: u64 = 0, reads: u8 = 4, mode: u8 = OREAD },
    stat: []const u8,
    remove: []const u8,
    create: struct { path: []const u8, name: []const u8, perm: u32, mode: u8 },
    /// An open kept across later ops; `hwrite`, `hread` and `clunk` name
    /// the held ones by index, mod how many are held.
    hold: struct { path: []const u8, mode: u8 },
    /// Its data drawn, when it runs, from `seed` and the held file's name.
    hwrite: struct { h: usize, seed: u64 },
    hread: struct { h: usize, count: u32, offset: ?u64 },
    clunk: usize,
    /// Restore [f] written to /ctl: refused, or (a real session) the server
    /// hangs up and serves the dump's panes again.
    restore: []const u8,
    /// A change on disk in the root: a file written, or deleted (null).
    host: struct { name: []const u8, data: ?[]const u8 },
    seq: []const Op,
    /// Down to the empty window (every pane closed but `keep`, the columns
    /// too), sometimes rebuilt with a new pane.
    clear: struct { keep: ?usize, cols: bool, rebuild: bool },
};

fn seq(a: Allocator, ops: []const Op) Op {
    return .{ .seq = a.dupe(Op, ops) catch oom() };
}

fn gen(r: Random, a: Allocator) Op {
    const weights = [_]u8{ 30, 14, 4, 5, 2, 2, 2, 6, 6, 6, 8, 3, 3, 2, 3, 3 };
    var w = below(r, 99);
    var k: usize = 0;
    while (w >= weights[k]) : (k += 1) w -= weights[k];
    switch (k) {
        0 => {
            const path = randomPath(r, a, true);
            const data = dataFor(r, a, path);
            const mode = pick(r, &[_]u8{ OWRITE, OWRITE, ORDWR, OWRITE | OTRUNC, ORDWR | OTRUNC, OREAD });
            const x = chance(r);
            const chunks: Chunks = if (x < 0.08 and data.len <= 64) .bytes else if (x < 0.15) .{ .sizes = .{ range(r, 1, 16), range(r, 1, 16), range(r, 1, 16), range(r, 1, 16) } } else .whole;
            const offset: ?u64 = if (chance(r) < 0.05) pick(r, &[_]u64{ 1, 100, 1 << 40 }) else null;
            return .{ .write = .{ .path = path, .data = data, .mode = mode, .chunks = chunks, .offset = offset } };
        },
        1 => return .{ .read = .{
            .path = randomPath(r, a, false),
            .count = pick(r, &[_]u32{ 1, 2, 7, 64, 200, 8192, 1 << 20 }),
            .offset = pick(r, &[_]u64{ 0, 0, 0, 1, 3, 100, 1 << 33 }),
            .reads = pick(r, &[_]u8{ 1, 2, 8 }),
        } },
        2 => {
            var path = randomPath(r, a, false);
            if (chance(r) < 0.2) path = cat(a, &.{ "/os/", pick(r, &[_][]const u8{ "etc", "etc/hosts", "nonexistent", &long_x }) });
            return .{ .stat = path };
        },
        3 => {
            if (chance(r) < 0.7) return .{ .read = .{ .path = "/pane/new", .count = 64, .reads = 1 } };
            return .{ .hold = .{ .path = "/pane/new", .mode = pick(r, &[_]u8{ OREAD, ORDWR }) } };
        },
        4 => {
            const x = chance(r);
            return .{ .remove = if (x < 0.6) print(a, "/pane/{s}", .{pickSerial(r, a)}) else if (x < 0.85) print(a, "/col/{s}", .{pickCol(r, a)}) else randomPath(r, a, false) };
        },
        5 => return .{ .create = .{
            .path = pick(r, &[_][]const u8{ "/col", "/pane", "/", print(a, "/pane/{s}", .{pickSerial(r, a)}) }),
            .name = if (chance(r) < 0.5) weirdName(r, a) else "new",
            .perm = pick(r, &[_]u32{ 0o666, DMDIR | 0o777 }),
            .mode = pick(r, &[_]u8{ OREAD, OWRITE }),
        } },
        6 => return .{ .clear = .{ .keep = if (chance(r) < 0.8) below(r, 64) else null, .cols = chance(r) < 0.5, .rebuild = chance(r) < 0.6 } },
        7 => {
            const x = chance(r);
            if (x < 0.35) {
                var path = randomPath(r, a, false);
                if (chance(r) < 0.3) path = pick(r, &[_][]const u8{ "/log", print(a, "/pane/{s}/event", .{pickSerial(r, a)}), "/screen", "/index", print(a, "/pane/{s}/ctl", .{pickSerial(r, a)}) });
                return .{ .hold = .{ .path = path, .mode = pick(r, &[_]u8{ OREAD, ORDWR, OWRITE }) } };
            }
            const h = below(r, 64);
            if (x < 0.6) return .{ .hwrite = .{ .h = h, .seed = r.int(u64) } };
            if (x < 0.85) return .{ .hread = .{ .h = h, .count = pick(r, &[_]u32{ 1, 16, 8192 }), .offset = pick(r, &[_]?u64{ null, null, 0, 5 }) } };
            return .{ .clunk = h };
        },
        8 => {
            const s = pickSerial(r, a);
            const line = shellSafe(a, pick(r, &shell_lines));
            const x = chance(r);
            if (x < 0.25) return .{ .write = .{ .path = print(a, "/pane/{s}/ctl", .{s}), .data = "Term\n" } };
            if (x < 0.45) return .{ .write = .{ .path = print(a, "/pane/{s}/pty/run", .{s}), .data = cat(a, &.{ line, "\n" }), .mode = ORDWR } };
            if (x < 0.6) return .{ .write = .{ .path = print(a, "/pane/{s}/pty/data", .{s}), .data = cat(a, &.{ line, "\r" }) } };
            if (x < 0.75) return .{ .write = .{ .path = print(a, "/pane/{s}/pty/ctl", .{s}), .data = pick(r, &[_][]const u8{ "sig INT\n", "sig TERM\n", "sig KILL\n", "sig HUP\n" }) } };
            return .{ .write = .{ .path = pick(r, &[_][]const u8{ "/exec", print(a, "/pane/{s}/exec", .{s}) }), .data = cat(a, &.{ line, "\n" }) } };
        },
        9 => {
            const s = pickSerial(r, a);
            const path = pick(r, &[_][]const u8{ print(a, "/pane/{s}/ctl", .{s}), print(a, "/pane/{s}/exec", .{s}), "/exec" });
            return .{ .write = .{ .path = path, .data = cat(a, &.{ "Edit ", editCommand(r, a), "\n" }) } };
        },
        10 => {
            const s = pickSerial(r, a);
            const first: Op = .{ .write = .{ .path = print(a, "/pane/{s}/addr", .{s}), .data = address(r, a) } };
            const x = chance(r);
            if (x < 0.4) return seq(a, &.{ first, .{ .read = .{ .path = print(a, "/pane/{s}/{s}", .{ s, pick(r, &[_][]const u8{ "xdata", "data", "addr", "dot" }) }), .reads = 2 } } });
            if (x < 0.7) return seq(a, &.{ first, .{ .write = .{
                .path = print(a, "/pane/{s}/{s}", .{ s, pick(r, &[_][]const u8{ "data", "xdata" }) }),
                .data = payload(r, a, 20),
                .mode = pick(r, &[_]u8{ OWRITE, OWRITE | OTRUNC }),
            } } });
            return seq(a, &.{first});
        },
        11 => return .{ .write = .{
            .path = pick(r, &[_][]const u8{ print(a, "/pane/{s}/tag", .{pickSerial(r, a)}), "/tag", print(a, "/col/{s}/tag", .{pickCol(r, a)}) }),
            .data = payload(r, a, 40),
            .mode = pick(r, &[_]u8{ OWRITE, OWRITE | OTRUNC }),
        } },
        12 => return .{ .hold = .{ .path = print(a, "/pane/{s}/event", .{pickSerial(r, a)}), .mode = ORDWR } },
        13 => {
            if (chance(r) < 0.5) return .{ .write = .{ .path = "/ctl", .data = pick(r, &[_][]const u8{ "Dump\n", "Dump", "Dump m9pdump\n" }) } };
            return .{ .restore = pick(r, &[_][]const u8{ "Restore\n", "Restore\n", "Restore m9pdump\n", "Restore nothere\n" }) };
        },
        14 => {
            const name = pick(r, &fixture_names);
            if (chance(r) < 0.5) return .{ .host = .{ .name = name, .data = null } };
            return .{ .host = .{ .name = name, .data = payload(r, a, 100) } };
        },
        else => {
            const name = pick(r, &fixture_names);
            const suffix = pick(r, &[_][]const u8{ "", ":2", ":#4", ":1:3", ":/beta/" });
            return .{ .write = .{ .path = pick(r, &[_][]const u8{ "/look", print(a, "/pane/{s}/look", .{pickSerial(r, a)}) }), .data = cat(a, &.{ name, suffix, "\n" }) } };
        },
    }
}

// ---------------------------------------------------------------------------
// Running an op: the requests it made, as the rules see them.

const Kind = enum { walk, open, open_new, trunc, write, write_badfid, read, clunk, clunk_write, stat, remove, create, close_runs, allow };
/// `ok` null: held (a read parked, as a read of the log with nothing new is).
const Rec = struct { kind: Kind, path: []const u8, ok: ?bool, why: []const u8 = "", n: usize = 0 };
const Res = struct {
    recs: std.ArrayList(Rec) = .empty,
    /// The op expected the server to hang up (a Restore taken).
    hung_up: bool = false,
    note: []const u8 = "",
    window: ?[]const []const u8 = null,

    fn add(res: *Res, a: Allocator, rec: Rec) void {
        res.recs.append(a, rec) catch oom();
    }

    fn isWrite(rec: Rec) bool {
        return rec.kind == .write or rec.kind == .clunk_write or rec.kind == .open_new or rec.kind == .trunc;
    }
};

/// An open of the fuzzer's: the session's walked fid, its mode, and where
/// a directory's reading has got to (an engine's diroff, in entries).
const Fid = struct { w: Session.Walked, mode: u8 = 0, pos: u64 = 0, index: u64 = 0 };
const Handle = struct { fid: Fid, path: []const u8, off: u64 = 0, last: []const u8 = "" };

const Fail = error{ Fail, Ended };

const Ctx = struct {
    gpa: Allocator,
    seed: u64,
    root: []const u8,
    verbose: bool,
    sess: ?Session = null,
    /// What lasts as long as a session.
    life: std.heap.ArenaAllocator,
    serials: std.ArrayList(u32) = .empty,
    cols: std.ArrayList(u32) = .empty,
    /// Serials seen and since closed, the last 20.
    gone: std.ArrayList(u32) = .empty,
    handles: std.ArrayList(Handle) = .empty,
    /// A second open holding `follow new` on /log, and the lines it read.
    follower: ?Session.Walked = null,
    lines: std.ArrayList([]const u8) = .empty,
    marker: ?usize = null,
    step: usize = 0,
    fail_kind: []const u8 = "",
    fail_why: []const u8 = "",

    fn s(c: *Ctx) *Session {
        return &c.sess.?;
    }

    fn fail(c: *Ctx, a: Allocator, kind: []const u8, comptime f: []const u8, args: anytype) error{Fail} {
        c.fail_kind = kind;
        c.fail_why = print(a, f, args);
        return error.Fail;
    }

    /// A request answered `.again` that nothing at T1 would answer later
    /// is a request the real server would hold until its timeout.
    fn hang(c: *Ctx, a: Allocator, what: []const u8, path: []const u8) error{Fail} {
        return c.fail(a, "hang", "{s} {s}: held, never answered", .{ what, path });
    }

    /// A new session: the fixtures on disk, three of them looked at (so one
    /// close does not end it), the session's own first pane removed, and
    /// half the time a follower of the log.
    fn fresh(c: *Ctx, at_step: usize) !void {
        if (c.sess) |*old| old.deinit();
        c.sess = null;
        _ = c.life.reset(.free_all);
        c.serials = .empty;
        c.cols = .empty;
        c.gone = .empty;
        c.handles = .empty;
        c.lines = .empty;
        c.follower = null;
        c.marker = null;
        const la = c.life.allocator();
        for (fixtures) |fx| try putFile(la, c.root, fx[0], fx[1]);
        removeFile(la, c.root, "fresh.txt");
        c.sess = try Session.initFile(c.gpa, fixtures[0][1]);
        const s_ = c.s();
        const first = try serialsOf(la, (try readAll(c, la, "/index")) orelse "");
        for (fixtures[0..3]) |fx| try s_.write("/look", print(la, "{s}/{s}\n", .{ c.root, fx[0] }));
        for (first) |serial| if (try s_.walk(print(la, "/pane/{d}", .{serial}))) |w| {
            var walked = w;
            _ = s_.ask(&walked, .remove, .{});
        };
        c.serials = .fromOwnedSlice(try serialsOf(la, (try readAll(c, la, "/index")) orelse ""));
        if (std.hash.Wyhash.hash(c.seed, std.mem.asBytes(&at_step)) & 1 == 1) {
            var w = (try s_.walk("/log")) orelse return error.NoLog;
            if (s_.ask(&w, .open, .{ .omode = ORDWR }).errno != 0) return error.NoLog;
            if (s_.ask(&w, .write, .{ .data = "follow new\n" }).errno != 0) return error.NoFollow;
            c.follower = w;
        }
    }
};

fn putFile(a: Allocator, dir: []const u8, name: []const u8, data: []const u8) !void {
    const path = try std.fmt.allocPrintSentinel(a, "{s}/{s}", .{ dir, name }, 0);
    const fd = libc.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, @as(libc.mode_t, 0o644));
    if (fd < 0) return error.CannotWrite;
    defer _ = libc.close(fd);
    if (data.len > 0 and libc.write(fd, data.ptr, data.len) != @as(isize, @intCast(data.len))) return error.CannotWrite;
}

fn removeFile(a: Allocator, dir: []const u8, name: []const u8) void {
    const path = std.fmt.allocPrintSentinel(a, "{s}/{s}", .{ dir, name }, 0) catch oom();
    _ = libc.unlink(path);
}

fn serialsOf(a: Allocator, index: []const u8) ![]u32 {
    var out: std.ArrayList(u32) = .empty;
    if (index.len == 0) return out.items;
    // A blank line is unparsable too, as Python's splitlines saw it.
    var lines = std.mem.splitScalar(u8, if (index[index.len - 1] == '\n') index[0 .. index.len - 1] else index, '\n');
    while (lines.next()) |line| {
        var it = std.mem.tokenizeAny(u8, line, " \t");
        try out.append(a, std.fmt.parseInt(u32, it.next() orelse return error.Unparsable, 10) catch return error.Unparsable);
    }
    return out.items;
}

fn hostPath(path: []const u8) bool {
    var it = std.mem.tokenizeScalar(u8, path, '/');
    const first = it.next() orelse return false;
    return std.mem.eql(u8, first, "os") or std.mem.eql(u8, first, "src");
}

fn isHeld(path: []const u8) bool {
    return std.mem.eql(u8, std.mem.trimEnd(u8, path, "/"), "/log") or std.mem.endsWith(u8, path, "/event") or
        std.mem.endsWith(u8, path, "/pty/data") or std.mem.endsWith(u8, path, "/pty/run");
}

/// An open the rule names: pane/new's (it makes a pane) and a truncation
/// ('every write or truncation the tree refused'); others are `open`.
fn openKind(path: []const u8, mode: u8) Kind {
    if (std.mem.eql(u8, std.mem.trimEnd(u8, path, "/"), "/pane/new") and mode & 3 == 0) return .open_new;
    if (mode & OTRUNC != 0 and mode & 3 != 0) return .trunc;
    return .open;
}

/// `path` walked, recorded when the walk is refused.
fn walk(c: *Ctx, a: Allocator, res: *Res, path: []const u8) !?Fid {
    const w = (try c.s().walk(path)) orelse {
        res.add(a, .{ .kind = .walk, .path = path, .ok = false, .why = c.s().said });
        return null;
    };
    return .{ .w = w };
}

/// The open the engine would let through, refused as it refuses: a mode
/// bit the file lacks, a directory opened to write; OTRUNC truncates first.
fn open(c: *Ctx, a: Allocator, res: *Res, f: *Fid, path: []const u8, mode: u8) !bool {
    const kind = openKind(path, mode);
    const rw = mode & 3;
    var need: u16 = 0;
    if (rw == OREAD or rw == ORDWR) need |= 0o400;
    if (rw == OWRITE or rw == ORDWR or mode & OTRUNC != 0) need |= 0o200;
    if (rw == 3 or (f.w.dir and (rw != OREAD or mode & OTRUNC != 0)) or f.w.mode & need != need) {
        res.add(a, .{ .kind = kind, .path = path, .ok = false, .why = "permission denied" });
        return false;
    }
    if (mode & OTRUNC != 0) {
        const t = c.s().ask(&f.w, .truncate, .{});
        if (t.held) return c.hang(a, "truncate", path);
        if (t.errno != 0) {
            res.add(a, .{ .kind = kind, .path = path, .ok = false, .why = c.s().said });
            return false;
        }
    }
    const o = c.s().ask(&f.w, .open, .{ .omode = mode });
    if (o.held) return c.hang(a, "open", path);
    res.add(a, .{ .kind = kind, .path = path, .ok = o.errno == 0, .why = if (o.errno != 0) c.s().said else "" });
    f.mode = mode;
    return o.errno == 0;
}

/// One read at `off`: its bytes, or null when refused or held. A directory
/// reads on from where it got to, or from 0, as the engine's diroff does.
fn readOnce(c: *Ctx, a: Allocator, res: *Res, f: *Fid, path: []const u8, off: u64, count: u32) !?[]const u8 {
    if (!f.w.opened or f.mode & 3 == OWRITE) {
        res.add(a, .{ .kind = .read, .path = path, .ok = false, .why = "bad use of fid" });
        return null;
    }
    if (f.w.dir and off != f.pos) {
        if (off != 0) {
            res.add(a, .{ .kind = .read, .path = path, .ok = false, .why = "bad offset in directory read" });
            return null;
        }
        f.pos = 0;
        f.index = 0;
    }
    const ans = c.s().ask(&f.w, if (f.w.dir) .readdir else .read, .{ .off = if (f.w.dir) f.index else off, .size = @min(count, read_max) });
    if (ans.held) {
        if (!isHeld(path)) return c.hang(a, "read", path);
        res.add(a, .{ .kind = .read, .path = path, .ok = null });
        return null;
    }
    res.add(a, .{ .kind = .read, .path = path, .ok = ans.errno == 0, .why = if (ans.errno != 0) c.s().said else "" });
    if (ans.errno != 0) return null;
    if (f.w.dir) {
        f.index += std.mem.count(u8, ans.bytes, "\n");
        f.pos += ans.bytes.len;
    }
    return try a.dupe(u8, ans.bytes);
}

fn writeOnce(c: *Ctx, a: Allocator, res: *Res, f: *Fid, path: []const u8, off: u64, data: []const u8) !bool {
    // A write on a fid opened for reading is 9P misuse, refused by the
    // engine: not a write the tree refused, and the rule does not name it.
    if (f.mode & 3 == OREAD) {
        res.add(a, .{ .kind = .write_badfid, .path = path, .ok = false, .why = "bad use of fid" });
        return false;
    }
    const ans = c.s().ask(&f.w, .write, .{ .off = off, .data = data });
    if (ans.held) return c.hang(a, "write", path);
    res.add(a, .{ .kind = .write, .path = path, .ok = ans.errno == 0, .why = if (ans.errno != 0) c.s().said else "" });
    return ans.errno == 0;
}

fn clunk(c: *Ctx, a: Allocator, res: *Res, f: *Fid, path: []const u8) !bool {
    const kind: Kind = if (f.mode & 3 != 0) .clunk_write else .clunk;
    if (!f.w.opened) {
        res.add(a, .{ .kind = kind, .path = path, .ok = true });
        return true;
    }
    const ans = c.s().ask(&f.w, .clunk, .{});
    if (ans.held) return c.hang(a, "clunk", path);
    res.add(a, .{ .kind = kind, .path = path, .ok = ans.errno == 0, .why = if (ans.errno != 0) c.s().said else "" });
    return ans.errno == 0;
}

/// The lines a lines file runs for `data` written in pieces of `sizes`
/// (docs/typ/reference.typ, tree.zig write): each ended line, and the
/// unended tail of a piece shorter than a Twrite holds, or of one not a
/// multiple of 4096 bytes, as acme takes each write. An upper bound: the
/// tree now waits for the newline whatever a piece's size.
fn servedLines(a: Allocator, data: []const u8, sizes: []const usize, room_: usize) []const []const u8 {
    var lines: std.ArrayList([]const u8) = .empty;
    var pending: []const u8 = "";
    var at: usize = 0;
    for (sizes) |size| {
        const piece = data[at..][0..size];
        at += size;
        pending = cat(a, &.{ pending, piece });
        while (std.mem.indexOfScalar(u8, pending, '\n')) |nl| {
            lines.append(a, pending[0..nl]) catch oom();
            pending = pending[nl + 1 ..];
        }
        if (pending.len > 0 and piece.len < room_ and piece.len % 4096 != 0) {
            lines.append(a, pending) catch oom();
            pending = "";
        }
    }
    if (pending.len > 0) lines.append(a, pending) catch oom();
    return lines.items;
}

fn selfCheck(a: Allocator) void {
    const cases = .{
        .{ "a\nb\n", &[_]usize{4}, &[_][]const u8{ "a", "b" } },
        .{ "ab\n", &[_]usize{ 1, 1, 1 }, &[_][]const u8{ "a", "b", "" } },
        .{ "ab\ncd", &[_]usize{ 3, 2 }, &[_][]const u8{ "ab", "cd" } },
    };
    // A piece that fills its Twrite goes on into the next.
    const full = servedLines(a, "abcd", &.{ 2, 2 }, 2);
    if (full.len != 1 or !std.mem.eql(u8, full[0], "abcd")) @panic("servedLines");
    inline for (cases) |case| {
        const got = servedLines(a, case[0], case[1], room);
        if (got.len != case[2].len) @panic("servedLines");
        for (got, case[2]) |g, w| if (!std.mem.eql(u8, g, w)) @panic("servedLines");
    }
}

/// Errs a successful write may log (docs/typ/reference.typ): a look that
/// finds nothing logs one, per line, whether written to look, written back
/// as an event record (acme's xfid.c:842) or run as the Look builtin; an
/// Edit block whose text never ended runs, and fails, at the close.
fn allowances(a: Allocator, res: *Res, path: []const u8, data: []const u8, sizes: []const usize) void {
    const b = base(path);
    const served = servedLines(a, data, sizes, room);
    var n: usize = 0;
    var lines: usize = 0;
    for (served) |line| {
        if (std.mem.trim(u8, line, " \t\n\r\x0b\x0c").len > 0) lines += 1;
    }
    if (std.mem.eql(u8, b, "look")) {
        // A look's line is its text, blanks and all: one of blanks alone is
        // a look too; only an empty one (or a lone \r) is none.
        for (served) |line| if (std.mem.trimEnd(u8, line, "\r").len > 0) {
            n += 1;
        };
    } else if (std.mem.eql(u8, b, "event")) {
        n = lines;
    } else if (std.mem.eql(u8, b, "exec") or std.mem.eql(u8, b, "ctl") or std.mem.eql(u8, b, "tagexec")) {
        for (served) |line| {
            var it = std.mem.tokenizeAny(u8, line, " \t\n\r\x0b\x0c");
            if (it.next()) |w| if (std.mem.eql(u8, w, "Look")) {
                n += 1;
            };
        }
        if (std.mem.indexOf(u8, data, "Edit") != null and lines > 1) n += 1;
    } else return;
    if (n > 0) res.add(a, .{ .kind = .allow, .path = path, .ok = true, .n = n });
}

fn chunking(a: Allocator, spec: Chunks, n: usize, cap: usize) []const usize {
    if (n == 0) return &.{0};
    var out: std.ArrayList(usize) = .empty;
    var total: usize = 0;
    switch (spec) {
        .bytes => for (0..n) |_| out.append(a, 1) catch oom(),
        .sizes => |sizes| for (sizes) |want| {
            if (total >= n) break;
            const size = @max(1, @min(want, cap, n - total));
            out.append(a, size) catch oom();
            total += size;
        },
        .whole => {},
    }
    if (spec != .bytes) while (total < n) {
        const size = @min(cap, n - total);
        out.append(a, size) catch oom();
        total += size;
    };
    return out.items;
}

fn exec(c: *Ctx, a: Allocator, res: *Res, op: Op) anyerror!void {
    switch (op) {
        .write => |o| {
            if (hostPath(o.path)) {
                res.note = "refused by guard: host tree";
                return;
            }
            var f = (try walk(c, a, res, o.path)) orelse return;
            const opened = try open(c, a, res, &f, o.path, o.mode);
            var sent: []const u8 = "";
            if (opened) {
                var offset: usize = 0;
                var took: std.ArrayList(usize) = .empty;
                for (chunking(a, o.chunks, o.data.len, write_max)) |size| {
                    const piece = o.data[offset..][0..size];
                    const ok = try writeOnce(c, a, res, &f, o.path, o.offset orelse offset, piece);
                    offset += size;
                    sent = o.data[0..offset];
                    if (ok) try took.append(a, size);
                    if (!ok) break;
                }
                // Misses are allowed the Twrites that succeeded: a line
                // written a byte a Twrite is a look a byte, each its own write.
                var sum: usize = 0;
                for (took.items) |t| sum += t;
                allowances(a, res, o.path, o.data[0..sum], took.items);
            }
            const closed = try clunk(c, a, res, &f, o.path);
            // What the open holds when it closes: the last line written, if unended.
            if (opened and o.mode & 3 != 0 and sent.len > 0 and !std.mem.endsWith(u8, sent, "\n"))
                res.add(a, .{ .kind = .close_runs, .path = o.path, .ok = closed });
        },
        .read => |o| {
            var f = (try walk(c, a, res, o.path)) orelse return;
            if (try open(c, a, res, &f, o.path, o.mode)) {
                var off = o.offset;
                for (0..o.reads) |_| {
                    const got = (try readOnce(c, a, res, &f, o.path, off, o.count)) orelse break;
                    if (got.len == 0) break;
                    off += got.len;
                }
            }
            _ = try clunk(c, a, res, &f, o.path);
        },
        .stat => |path| {
            var f = (try walk(c, a, res, path)) orelse return;
            res.add(a, .{ .kind = .walk, .path = path, .ok = true });
            const ans = c.s().ask(&f.w, .stat, .{});
            res.add(a, .{ .kind = .stat, .path = path, .ok = ans.errno == 0 });
        },
        .remove => |path| {
            if (hostPath(path)) {
                res.note = "refused by guard: host tree";
                return;
            }
            var f = (try walk(c, a, res, path)) orelse return;
            res.add(a, .{ .kind = .walk, .path = path, .ok = true });
            const ans = c.s().ask(&f.w, .remove, .{});
            if (ans.held) return c.hang(a, "remove", path);
            res.add(a, .{ .kind = .remove, .path = path, .ok = ans.errno == 0, .why = if (ans.errno != 0) c.s().said else "" });
        },
        .create => |o| {
            if (hostPath(o.path)) {
                res.note = "refused by guard: host tree";
                return;
            }
            var f = (try walk(c, a, res, o.path)) orelse return;
            res.add(a, .{ .kind = .walk, .path = o.path, .ok = true });
            const ans = c.s().ask(&f.w, .create, .{ .data = o.name, .perm = o.perm, .omode = o.mode });
            res.add(a, .{ .kind = .create, .path = cat(a, &.{ o.path, "/", o.name }), .ok = ans.errno == 0 });
            if (f.w.opened) _ = c.s().ask(&f.w, .clunk, .{});
        },
        .hold => |o| {
            if (hostPath(o.path) and o.mode & 3 != 0) {
                res.note = "refused by guard: host tree";
                return;
            }
            var f = (try walk(c, a, res, o.path)) orelse return;
            if (try open(c, a, res, &f, o.path, o.mode))
                try c.handles.append(c.life.allocator(), .{ .fid = f, .path = try c.life.allocator().dupe(u8, o.path) });
        },
        .hwrite => |o| {
            if (c.handles.items.len == 0) {
                res.note = "no such handle";
                return;
            }
            const h = &c.handles.items[o.h % c.handles.items.len];
            var prng = Random.DefaultPrng.init(o.seed);
            const want = resolveData(c, a, h.path, dataFor(prng.random(), a, h.path));
            const data = want[0..@min(want.len, write_max)]; // one Twrite
            h.last = try c.life.allocator().dupe(u8, data);
            if (try writeOnce(c, a, res, &h.fid, h.path, h.off, data)) {
                allowances(a, res, h.path, data, &.{data.len});
                h.off += data.len;
            }
        },
        .hread => |o| {
            if (c.handles.items.len == 0) {
                res.note = "no such handle";
                return;
            }
            const h = &c.handles.items[o.h % c.handles.items.len];
            const offset = o.offset orelse h.off;
            if (try readOnce(c, a, res, &h.fid, h.path, offset, o.count)) |got| h.off = offset + got.len;
        },
        .clunk => |i| {
            if (c.handles.items.len == 0) {
                res.note = "no such handle";
                return;
            }
            var h = c.handles.orderedRemove(i % c.handles.items.len);
            const ok = try clunk(c, a, res, &h.fid, h.path);
            if (h.fid.mode & 3 != 0 and h.last.len > 0 and !std.mem.endsWith(u8, h.last, "\n"))
                res.add(a, .{ .kind = .close_runs, .path = h.path, .ok = ok });
        },
        .restore => |data| {
            var f = (try walk(c, a, res, "/ctl")) orelse return;
            if (!try open(c, a, res, &f, "/ctl", OWRITE)) return;
            const ok = try writeOnce(c, a, res, &f, "/ctl", 0, data);
            _ = try clunk(c, a, res, &f, "/ctl");
            if (!ok) return;
            // Taken: a real server hangs every connection up and serves the
            // dump's panes again. What the connections held is let go.
            res.hung_up = true;
            for (c.handles.items) |*h| _ = c.s().ask(&h.fid.w, .clunk, .{});
            c.handles.clearRetainingCapacity();
            c.marker = null;
        },
        .host => |o| if (o.data) |data| try putFile(a, c.root, o.name, data) else removeFile(a, c.root, o.name),
        .seq => |ops| for (ops) |sub| try exec(c, a, res, sub),
        .clear => unreachable, // resolved into a seq
    }
}

/// Tokens replaced with what is live when the op runs: `{pN}` the Nth
/// serial of /index, mod its length (0 when there is none), `{cN}` a
/// column's, `{gN}` a stale serial's (999 when there is none).
fn resolveStr(c: *Ctx, a: Allocator, text: []const u8) []const u8 {
    if (std.mem.indexOfScalar(u8, text, '{') == null) return text;
    var out: std.ArrayList(u8) = .empty;
    var i: usize = 0;
    while (i < text.len) {
        if (text[i] == '{' and i + 2 < text.len and std.mem.indexOfScalar(u8, "pcg", text[i + 1]) != null) {
            var j = i + 2;
            while (j < text.len and std.ascii.isDigit(text[j])) j += 1;
            if (j > i + 2 and j < text.len and text[j] == '}') {
                const n = std.fmt.parseInt(usize, text[i + 2 .. j], 10) catch 0;
                const live = switch (text[i + 1]) {
                    'p' => c.serials.items,
                    'c' => c.cols.items,
                    else => c.gone.items,
                };
                const none: u32 = if (text[i + 1] == 'g') 999 else 0;
                out.print(a, "{d}", .{if (live.len > 0) live[n % live.len] else none}) catch oom();
                i = j + 1;
                continue;
            }
        }
        out.append(a, text[i]) catch oom();
        i += 1;
    }
    return out.items;
}

fn resolveData(c: *Ctx, a: Allocator, path: []const u8, data: []const u8) []const u8 {
    return if (std.mem.indexOf(u8, path, "/focus") != null) resolveStr(c, a, data) else data;
}

fn resolve(c: *Ctx, a: Allocator, op: Op) Op {
    return switch (op) {
        .write => |o| blk: {
            var w = o;
            w.path = resolveStr(c, a, o.path);
            w.data = resolveData(c, a, w.path, o.data);
            break :blk .{ .write = w };
        },
        .read => |o| blk: {
            var r = o;
            r.path = resolveStr(c, a, o.path);
            break :blk .{ .read = r };
        },
        .stat => |p| .{ .stat = resolveStr(c, a, p) },
        .remove => |p| .{ .remove = resolveStr(c, a, p) },
        .create => |o| blk: {
            var r = o;
            r.path = resolveStr(c, a, o.path);
            break :blk .{ .create = r };
        },
        .hold => |o| .{ .hold = .{ .path = resolveStr(c, a, o.path), .mode = o.mode } },
        .seq => |ops| blk: {
            const out = a.alloc(Op, ops.len) catch oom();
            for (ops, out) |o, *r| r.* = resolve(c, a, o);
            break :blk .{ .seq = out };
        },
        .clear => |o| blk: {
            var ops: std.ArrayList(Op) = .empty;
            const keep: ?u32 = if (o.keep) |k| (if (c.serials.items.len > 0) c.serials.items[k % c.serials.items.len] else null) else null;
            for (c.serials.items) |serial| if (serial != keep) ops.append(a, .{ .remove = print(a, "/pane/{d}", .{serial}) }) catch oom();
            if (o.cols) for (c.cols.items) |col| ops.append(a, .{ .remove = print(a, "/col/{d}", .{col}) }) catch oom();
            if (o.rebuild) ops.append(a, .{ .read = .{ .path = "/pane/new", .count = 64, .reads = 1 } }) catch oom();
            break :blk .{ .seq = ops.items };
        },
        else => op,
    };
}

fn opData(op: Op) []const u8 {
    const data = switch (op) {
        .write => |o| o.data,
        .restore => |d| d,
        .host => |o| o.data orelse "",
        else => "",
    };
    return data[0..@min(data.len, 200)];
}

fn opPath(op: Op) []const u8 {
    return switch (op) {
        .write => |o| o.path,
        .read => |o| o.path,
        .stat, .remove, .restore => |p| p,
        .create => |o| o.path,
        .hold => |o| o.path,
        .host => |o| o.name,
        else => "",
    };
}

// ---------------------------------------------------------------------------
// Reading the session back.

fn readAll(c: *Ctx, a: Allocator, path: []const u8) !?[]const u8 {
    const s = c.s();
    var w = (try s.walk(path)) orelse return null;
    if (s.ask(&w, .open, .{}).errno != 0) return null;
    defer _ = s.ask(&w, .clunk, .{});
    return try readFid(c, a, &w, read_max);
}

/// A whole file on an open, read from 0 in `count`-byte reads; a
/// directory's names, a line each (one entry a read when `count` is not a
/// whole read's).
fn readFid(c: *Ctx, a: Allocator, w: *Session.Walked, count: u32) ![]const u8 {
    var out: std.ArrayList(u8) = .empty;
    if (w.dir) {
        var index: u64 = 0;
        while (true) {
            const ans = c.s().ask(w, .readdir, .{ .off = index, .size = count });
            if (ans.errno != 0) return error.ReadRefused;
            if (ans.bytes.len == 0) break;
            if (count == read_max) {
                try out.appendSlice(a, ans.bytes);
                break;
            }
            try out.appendSlice(a, ans.bytes[0 .. std.mem.indexOfScalar(u8, ans.bytes, '\n').? + 1]);
            index += 1;
        }
        return out.items;
    }
    while (out.items.len < 1 << 22) {
        const ans = c.s().ask(w, .read, .{ .off = out.items.len, .size = count });
        if (ans.held) break;
        if (ans.errno != 0) return error.ReadRefused;
        if (ans.bytes.len == 0) break;
        try out.appendSlice(a, ans.bytes);
    }
    return out.items;
}

fn names(c: *Ctx, a: Allocator, path: []const u8) !?[]const []const u8 {
    var w = (try c.s().walk(path)) orelse return null;
    if (c.s().ask(&w, .open, .{}).errno != 0) return null;
    defer _ = c.s().ask(&w, .clunk, .{});
    return try splitLines(a, try readFid(c, a, &w, read_max));
}

fn splitLines(a: Allocator, text: []const u8) ![]const []const u8 {
    var out: std.ArrayList([]const u8) = .empty;
    var it = std.mem.splitScalar(u8, text, '\n');
    while (it.next()) |line| try out.append(a, line);
    if (out.items.len > 0 and out.items[out.items.len - 1].len == 0) _ = out.pop();
    return out.items;
}

fn isDigits(text: []const u8) bool {
    if (text.len == 0) return false;
    for (text) |b| if (!std.ascii.isDigit(b)) return false;
    return true;
}

// ---------------------------------------------------------------------------
// The log window: the records one step caused, between two unique markers.

/// An unknown ctl word logs `err - ctl: unknown control message "<word>"`.
fn marker(c: *Ctx, a: Allocator) !?[]const []const u8 {
    const s = c.s();
    const word = print(a, "m9p-mark-{d}", .{c.step});
    const quoted = print(a, "\"{s}\"", .{word});
    if (try s.refused("/ctl", print(a, "{s}\n", .{word})) == 0) return c.fail(a, "log", "marker write was taken", .{});
    const prev = c.marker;
    c.marker = c.step;
    const prev_quoted: ?[]const u8 = if (prev) |p| print(a, "\"m9p-mark-{d}\"", .{p}) else null;
    if (c.follower) |*w| {
        const la = c.life.allocator();
        while (true) {
            const ans = s.ask(w, .read, .{ .size = 8192 });
            if (ans.held or ans.errno != 0 or ans.bytes.len == 0) break;
            for (try splitLines(a, ans.bytes)) |line| try c.lines.append(la, try la.dupe(u8, line));
        }
        const lines = c.lines.items;
        const end = lastWith(lines, quoted) orelse return c.fail(a, "log", "marker record never reached the follower", .{});
        var start: usize = 0;
        if (prev_quoted) |pq| start = (lastWith(lines[0..end], pq) orelse return null) + 1;
        const window = try a.dupe([]const u8, lines[start..end]);
        c.lines.replaceRangeAssumeCapacity(0, end, &.{});
        return window;
    }
    const lines = try splitLines(a, (try readAll(c, a, "/log")) orelse "");
    const end = lastWith(lines, quoted) orelse return c.fail(a, "log", "marker record missing from /log", .{});
    const pq = prev_quoted orelse return null;
    const start = lastWith(lines[0..end], pq) orelse return null; // the ring moved past it
    return lines[start + 1 .. end];
}

fn lastWith(lines: []const []const u8, needle: []const u8) ?usize {
    var i = lines.len;
    while (i > 0) {
        i -= 1;
        if (std.mem.indexOf(u8, lines[i], needle) != null) return i;
    }
    return null;
}

/// Records of `kind` (err|msg), counting `(xN)` repeats (docs/typ/
/// reference.typ: a record said again is counted; a follower sees each
/// count as a line of its own).
fn occurrences(a: Allocator, lines: []const []const u8, kind: []const u8) []const []const u8 {
    var out: std.ArrayList([]const u8) = .empty;
    var last_text: ?[]const u8 = null;
    var last_n: usize = 0;
    for (lines) |line| {
        if (!(std.mem.startsWith(u8, line, kind) and line.len > kind.len and line[kind.len] == ' ')) {
            last_text = null;
            continue;
        }
        var text = line;
        var n: usize = 1;
        if (std.mem.endsWith(u8, line, ")")) if (std.mem.lastIndexOf(u8, line, " (x")) |i| {
            if (isDigits(line[i + 3 .. line.len - 1])) {
                text = line[0..i];
                n = std.fmt.parseInt(usize, line[i + 3 .. line.len - 1], 10) catch 1;
            }
        };
        const new = if (last_text != null and std.mem.eql(u8, text, last_text.?)) n -| last_n else n;
        for (0..new) |_| out.append(a, text) catch oom();
        last_text = text;
        last_n = n;
    }
    return out.items;
}

// ---------------------------------------------------------------------------
// The rules, each with its citation. Each answers why it failed, or null.

/// docs/typ/reference.typ 'One rule for what fails': a write fails whenever
/// what it asked for fails ... and logs its reason exactly once, as `err
/// <serial|-> <file>: <why>`, with no `msg` for it. What is not a failure:
/// a look that finds nothing answers nothing and logs one `err`, the write
/// succeeding.
fn oneFailureRule(c: *Ctx, a: Allocator, res: *Res) ?[]const u8 {
    _ = c;
    const window = res.window orelse return null;
    if (res.hung_up) return null;
    // The rule is a write's: a refused open (pane/new's and an OTRUNC one
    // too), create or remove logs no err, nor does a write to pane/new
    // (`permission denied`: it is only read).
    var failed: std.ArrayList(Rec) = .empty;
    var succeeded = false;
    var allowed: usize = 0;
    var closes: usize = 0;
    for (res.recs.items) |r| {
        if (r.kind == .allow) allowed += r.n;
        if (r.kind == .close_runs) closes += 1;
        if (r.kind == .write and r.ok == true) succeeded = true;
        if (Res.isWrite(r) and r.ok == false and (r.kind == .write or r.kind == .clunk_write) and
            !std.mem.eql(u8, std.mem.trimEnd(u8, r.path, "/"), "/pane/new")) failed.append(a, r) catch oom();
    }
    const errs = occurrences(a, window, "err");
    // docs/typ/reference.typ 'A write of command lines ... what is left when
    // it closes runs at the close ... and its failure is in the log alone'.
    const lo = failed.items.len;
    const hi = lo + allowed + closes;
    if (errs.len < lo or errs.len > hi) {
        var why: []const u8 = "";
        for (failed.items) |f| why = print(a, "{s} ({t} {s}: {s})", .{ why, f.kind, f.path, f.why });
        return print(a, "{d} failed write(s) logged {d} err record(s) (allowed {d}..{d}); failed={s} window={s}", .{ lo, errs.len, lo, hi, why, joined(a, window) });
    }
    // A msg is a failed write's only when no Twrite of the op succeeded: a
    // line cut across Twrites runs a piece a Twrite, and a piece that ran
    // (a Save) says so while a later one is refused. A file watch's notice
    // is the host's news, said whenever it arrives: no write's.
    var msgs: std.ArrayList([]const u8) = .empty;
    for (occurrences(a, window, "msg")) |m| {
        if (std.mem.indexOf(u8, m, " deleted on disk (") != null or std.mem.indexOf(u8, m, " changed on disk (") != null or
            std.mem.indexOf(u8, m, "  reloaded ") != null) continue;
        msgs.append(a, m) catch oom();
    }
    if (failed.items.len > 0 and msgs.items.len > 0 and !succeeded)
        return print(a, "a failed write also logged msg: {s}; failed {s}", .{ joined(a, msgs.items), failed.items[0].path });
    return null;
}

const line_files = [_][]const u8{ "look", "exec", "tagexec", "ctl" };

/// docs/typ/reference.typ: 'A write of command lines -- to look, exec,
/// tagexec, a ctl of the root, a pane or a column, or a column's exec --
/// runs each line once it is whole ... what is left when it closes runs at
/// the close'. Another file that fails only at its close breaks 'a write
/// fails whenever what it asked for fails' (or the list is short).
fn closeRunsOnlyLineFiles(c: *Ctx, a: Allocator, res: *Res) ?[]const u8 {
    _ = c;
    const window = res.window orelse return null;
    for (occurrences(a, window, "err")) |line| {
        // `err <serial|-> <file>: <why>`
        const rest = line["err ".len..];
        const sp = std.mem.indexOfScalar(u8, rest, ' ') orelse continue;
        if (sp == 0) continue;
        const colon = std.mem.indexOfScalar(u8, rest[sp + 1 ..], ':') orelse continue;
        if (colon == 0) continue;
        const name = base(rest[sp + 1 ..][0..colon]);
        var skip = false;
        for (line_files) |l| skip = skip or std.mem.eql(u8, name, l);
        for (res.recs.items) |r| skip = skip or (Res.isWrite(r) and r.ok == false and std.mem.eql(u8, base(r.path), name));
        if (skip) continue;
        for (res.recs.items) |r| if (r.kind == .close_runs and std.mem.eql(u8, base(r.path), name))
            return print(a, "a write to {s} with no newline was answered, then failed at the close: {s}", .{ name, line });
    }
    return null;
}

/// docs/typ/reference.typ /index: one line per pane; /pane/<n>/ each
/// pane's directory.
fn indexMatchesPaneDirs(c: *Ctx, a: Allocator, res: *Res) !?[]const u8 {
    _ = res;
    const first = (try readAll(c, a, "/index")) orelse return "/index unreadable";
    const listed = (try names(c, a, "/pane")) orelse return "walking /pane failed";
    const second = (try readAll(c, a, "/index")) orelse return "/index unreadable";
    const serials = serialsOf(a, second) catch return print(a, "/index unparsable: {s}", .{second});
    const la = c.life.allocator();
    for (c.serials.items) |old| {
        if (std.mem.indexOfScalar(u32, serials, old) == null and std.mem.indexOfScalar(u32, c.gone.items, old) == null) try c.gone.append(la, old);
    }
    std.mem.sort(u32, c.gone.items, {}, std.sort.asc(u32));
    if (c.gone.items.len > 20) c.gone.replaceRangeAssumeCapacity(0, c.gone.items.len - 20, &.{});
    c.serials = .fromOwnedSlice(try la.dupe(u32, serials));
    if (!std.mem.eql(u8, first, second)) return null; // it changed between the reads; no claim
    var dirs: std.ArrayList(u32) = .empty;
    for (listed) |n| {
        if (isDigits(n)) {
            try dirs.append(a, std.fmt.parseInt(u32, n, 10) catch return print(a, "/pane lists {s}", .{n}));
        } else if (!std.mem.eql(u8, n, "new")) return print(a, "/pane lists a non-serial entry {s}", .{n});
    }
    const sorted = try a.dupe(u32, serials);
    std.mem.sort(u32, sorted, {}, std.sort.asc(u32));
    std.mem.sort(u32, dirs.items, {}, std.sort.asc(u32));
    if (!std.mem.eql(u32, sorted, dirs.items)) return print(a, "/index serials {any} != /pane dirs {any}", .{ sorted, dirs.items });
    return null;
}

const listings = [_][]const u8{ "/index", "/layout", "/", "/pane", "/col", "/pane/{s}", "/status", "/commands", "/pane/{s}/tag", "/recent", "/README" };

/// 9P read(5): reads at successive offsets see the one file; a listing
/// read in chunks equals one read whole within the same open.
fn chunkedEqualsWhole(c: *Ctx, a: Allocator, res: *Res) !?[]const u8 {
    _ = res;
    var path: []const u8 = listings[c.step % listings.len];
    if (std.mem.indexOf(u8, path, "{s}")) |i| {
        if (c.serials.items.len == 0) return null;
        path = print(a, "{s}{d}{s}", .{ path[0..i], c.serials.items[c.step % c.serials.items.len], path[i + 3 ..] });
    }
    var w = (try c.s().walk(path)) orelse return null; // gone since
    if (c.s().ask(&w, .open, .{}).errno != 0) return null;
    defer _ = c.s().ask(&w, .clunk, .{});
    const whole = try readFid(c, a, &w, read_max);
    const size: u32 = @intCast(if (w.dir) 128 + (c.step * 37) % 900 else 1 + (c.step * 7) % 50);
    const chunked = try readFid(c, a, &w, size);
    if (w.dir) {
        if (!std.mem.eql(u8, whole, chunked)) return print(a, "{s}: whole {s} != chunked({d}) {s}", .{ path, whole, size, chunked });
    } else if (!std.mem.eql(u8, whole, chunked) and !std.mem.eql(u8, try readFid(c, a, &w, read_max), chunked)) {
        // A live file (a tag says `running`, then `exit 0`) may change
        // between the two reads: only chunks no whole read matches, read
        // again, are a chunking fault.
        return print(a, "{s}: whole {s} != chunked({d}) {s}", .{ path, whole[0..@min(200, whole.len)], size, chunked[0..@min(200, chunked.len)] });
    }
    return null;
}

fn joined(a: Allocator, lines: []const []const u8) []const u8 {
    var out: []const u8 = "[";
    for (lines, 0..) |l, i| out = cat(a, &.{ out, if (i > 0) " | " else "", l });
    return cat(a, &.{ out, "]" });
}

/// One op and every rule after it.
fn runStep(c: *Ctx, a: Allocator, raw: Op) Fail!void {
    const op = resolve(c, a, raw);
    const what = print(a, "{t} {s}", .{ op, opPath(op) });
    var res: Res = .{};
    if (c.verbose) std.debug.print("  op {d}: {s} {f}\n", .{ c.step, what, std.zig.fmtString(opData(op)) });
    exec(c, a, &res, op) catch |err| switch (err) {
        error.Fail => return error.Fail,
        else => return c.fail(a, "harness", "{s}: {t}", .{ what, err }),
    };
    // Closing the session's last pane quits it (docs/typ/reference.typ).
    if (c.s().ended()) return error.Ended;
    checks(c, a, &res) catch |err| switch (err) {
        error.Fail => return error.Fail,
        else => return c.fail(a, "checker", "after {s}: {t}", .{ what, err }),
    };
}

fn checks(c: *Ctx, a: Allocator, res: *Res) !void {
    const window = try marker(c, a);
    res.window = if (res.hung_up) null else window;
    if (c.verbose) {
        if (res.note.len > 0) std.debug.print("    -- {s}\n", .{res.note});
        for (res.recs.items) |r| std.debug.print("    {t} {f} -> {s} {s}\n", .{ r.kind, std.zig.fmtString(r.path), if (r.ok) |ok| (if (ok) "ok" else "Rerror") else "held", r.why });
        for (window orelse &.{}) |line| std.debug.print("    log: {s}\n", .{line});
    }
    const rules = .{ .{ "one_failure_rule", oneFailureRule }, .{ "close_runs_only_line_files", closeRunsOnlyLineFiles }, .{ "index_matches_pane_dirs", indexMatchesPaneDirs }, .{ "chunked_equals_whole", chunkedEqualsWhole } };
    inline for (rules) |rule| {
        const got = rule[1](c, a, res);
        const problem = if (@typeInfo(@TypeOf(got)) == .error_union) try got else got;
        if (problem) |why| return c.fail(a, rule[0], "{s}", .{why});
    }
    // The column list, for the next ops.
    if (try names(c, a, "/col")) |cols| {
        c.cols = .empty;
        for (cols) |n| if (isDigits(n)) try c.cols.append(c.life.allocator(), std.fmt.parseInt(u32, n, 10) catch continue);
    }
}

/// What makes two failures the same bug: the kind, and the words with
/// numbers, quoted words and paths blurred.
fn signature(a: Allocator, kind: []const u8, why: []const u8) []const u8 {
    var out: std.ArrayList(u8) = .empty;
    out.print(a, "{s}:", .{kind}) catch oom();
    const singles = std.mem.indexOf(u8, why, "window=") == null;
    var i: usize = 0;
    while (i < why.len and out.items.len < kind.len + 1 + 300) {
        const ch = why[i];
        if (ch == '"' or (ch == '\'' and singles)) {
            if (std.mem.indexOfScalarPos(u8, why, i + 1, ch)) |close| {
                out.appendSlice(a, &.{ ch, ch }) catch oom();
                i = close + 1;
                continue;
            }
        }
        if (std.mem.startsWith(u8, why[i..], "/home/") or std.mem.startsWith(u8, why[i..], "/tmp/pardes-mky-")) {
            out.appendSlice(a, "PATH") catch oom();
            while (i < why.len and !std.ascii.isWhitespace(why[i])) i += 1;
            continue;
        }
        if (std.ascii.isDigit(ch)) {
            out.append(a, 'N') catch oom();
            while (i < why.len and std.ascii.isDigit(why[i])) i += 1;
            continue;
        }
        out.append(a, ch) catch oom();
        i += 1;
    }
    return out.items;
}

/// The child's run of one seed: its failures as `mky fail <step> <sig>`
/// lines, each step announced as `mky step <n>` first, so the parent knows
/// where a panic or a hang was.
fn fuzz(gpa: Allocator, seed: u64, steps: usize, only: ?[]const usize, verbose: bool, root: []const u8) !u8 {
    var c: Ctx = .{ .gpa = gpa, .seed = seed, .root = root, .verbose = verbose, .life = .init(gpa) };
    var prng = Random.DefaultPrng.init(seed);
    const r = prng.random();
    var step_arena: std.heap.ArenaAllocator = .init(gpa);
    var found: std.StringArrayHashMapUnmanaged(struct { n: usize, first: usize }) = .empty;
    try c.fresh(0);
    const t0 = nowMs();
    var ended: usize = 0;
    var ran: usize = 0;
    for (0..steps) |step| {
        _ = step_arena.reset(.retain_capacity);
        const a = step_arena.allocator();
        c.step = step;
        const op = gen(r, a);
        if (only) |list| if (std.mem.indexOfScalar(usize, list, step) == null) continue;
        std.debug.print("mky step {d}\n", .{step});
        ran += 1;
        runStep(&c, a, op) catch |err| switch (err) {
            error.Ended => {
                ended += 1;
                try c.fresh(step + 1);
                continue;
            },
            error.Fail => {
                const sig = try gpa.dupe(u8, signature(a, c.fail_kind, c.fail_why));
                std.debug.print("[seed {d} step {d}] FAIL {s}: {s}\n", .{ seed, step, c.fail_kind, c.fail_why[0..@min(c.fail_why.len, 400)] });
                std.debug.print("mky fail {d} {s}\n", .{ step, sig });
                const got = try found.getOrPut(gpa, sig);
                if (got.found_existing) got.value_ptr.n += 1 else got.value_ptr.* = .{ .n = 1, .first = step };
                if (only != null) break;
                try c.fresh(step + 1);
            },
        };
        if (step > 0 and step % 500 == 0)
            std.debug.print("[seed {d}] step {d}/{d}, {d} distinct failure(s)\n", .{ seed, step, steps, found.count() });
    }
    const dt = @max(1, nowMs() - t0);
    std.debug.print("[seed {d}] {d} steps in {d} ms ({d}/s), {d} clean quit(s)\n", .{ seed, ran, dt, @divTrunc(@as(i64, @intCast(ran)) * 1000, dt), ended });
    for (found.keys(), found.values()) |sig, v| std.debug.print("  {d}x {s}  (first at step {d})\n", .{ v.n, sig[0..@min(sig.len, 200)], v.first });
    if (c.sess) |*s| s.deinit();
    return if (found.count() > 0) 1 else 0;
}

fn nowMs() i64 {
    var ts: libc.timespec = undefined;
    _ = libc.clock_gettime(.MONOTONIC, &ts);
    return @as(i64, ts.sec) * 1000 + @divTrunc(@as(i64, ts.nsec), std.time.ns_per_ms);
}

// ---------------------------------------------------------------------------
// The parent: a child a run, in its own root.

const Found = struct { step: usize, sig: []const u8 };

/// One child run of `seed`; its failures, a panic or a hang among them.
/// Its output is passed on unless `quiet` (a shrink's trials).
fn spawn(gpa: Allocator, seed: u64, steps: usize, only: ?[]const usize, verbose: bool, quiet: bool) ![]Found {
    var arena: std.heap.ArenaAllocator = .init(gpa);
    defer arena.deinit();
    const a = arena.allocator();
    var template = "/tmp/pardes-mky-XXXXXX".*;
    const root = std.mem.span(mkdtemp(&template) orelse return error.NoTempDir);
    defer {
        const rm = std.fmt.allocPrintSentinel(a, "rm -rf -- '{s}'", .{root}, 0) catch oom();
        _ = system(rm);
    }
    var argv: std.ArrayList(?[*:0]const u8) = .empty;
    for ([_][]const u8{ "pardes-monkey-session", "--seed", print(a, "{d}", .{seed}), "--steps", print(a, "{d}", .{steps}) }) |arg| try argv.append(a, try a.dupeSentinel(u8, arg, 0));
    if (verbose) try argv.append(a, "-v");
    if (only) |list| {
        var text: std.ArrayList(u8) = .empty;
        for (list, 0..) |n, i| try text.print(a, "{s}{d}", .{ if (i > 0) "," else "", n });
        try argv.append(a, "--only");
        try argv.append(a, try a.dupeSentinel(u8, text.items, 0));
    }
    try argv.append(a, null);
    // The session's HOME, XDG directories and dump are the root's; what a
    // session of the caller's left in the environment is not passed on.
    var envp: std.ArrayList(?[*:0]const u8) = .empty;
    for ([_][2][]const u8{
        .{ "MKY_ROOT", root },
        .{ "HOME", root },
        .{ "XDG_RUNTIME_DIR", root },
        .{ "XDG_CONFIG_HOME", print(a, "{s}/config", .{root}) },
        .{ "XDG_STATE_HOME", print(a, "{s}/state", .{root}) },
        .{ "XDG_DATA_HOME", print(a, "{s}/data", .{root}) },
        .{ "XDG_CACHE_HOME", print(a, "{s}/cache", .{root}) },
        .{ "PARDES_DUMP", print(a, "{s}/m9p.dump.zon", .{root}) },
        .{ "SHELL", "/bin/sh" },
        .{ "PARDES_NOTIME", "1" },
    }) |kv| try envp.append(a, try std.fmt.allocPrintSentinel(a, "{s}={s}", .{ kv[0], kv[1] }, 0));
    var e: usize = 0;
    while (environ[e]) |entry| : (e += 1) {
        const kv = std.mem.span(entry);
        if (std.mem.startsWith(u8, kv, "PARDES_") or std.mem.startsWith(u8, kv, "NINE_MOUNT=") or std.mem.startsWith(u8, kv, "NAMESPACE=")) continue;
        try envp.append(a, entry);
    }
    try envp.append(a, null);
    var fds: [2]c_int = undefined;
    if (libc.pipe(&fds) != 0) return error.NoPipe;
    const root_z = try a.dupeSentinel(u8, root, 0);
    const pid = libc.fork();
    if (pid < 0) return error.ForkFailed;
    if (pid == 0) {
        _ = libc.dup2(fds[1], 1);
        _ = libc.dup2(fds[1], 2);
        _ = libc.close(fds[0]);
        if (libc.chdir(root_z) != 0) _exit(126);
        _ = execve("/proc/self/exe", @ptrCast(argv.items.ptr), @ptrCast(envp.items.ptr));
        _exit(127);
    }
    _ = libc.close(fds[1]);
    defer _ = libc.close(fds[0]);
    var found: std.ArrayList(Found) = .empty;
    var pending: std.ArrayList(u8) = .empty;
    var last_step: usize = 0;
    var panic_line: ?[]const u8 = null;
    // The core's own output during the current step, shown if it crashes.
    var said: std.ArrayList(u8) = .empty;
    var hung = false;
    var buf: [64 * 1024]u8 = undefined;
    while (true) {
        var pfd = [_]libc.pollfd{.{ .fd = fds[0], .events = libc.POLL.IN, .revents = 0 }};
        if (libc.poll(&pfd, 1, hang_ms) == 0) {
            hung = true;
            _ = libc.kill(pid, libc.SIG.KILL);
            continue;
        }
        const n = libc.read(fds[0], &buf, buf.len);
        if (n <= 0) break;
        try pending.appendSlice(a, buf[0..@intCast(n)]);
        while (std.mem.indexOfScalar(u8, pending.items, '\n')) |nl| {
            const line = try a.dupe(u8, pending.items[0..nl]);
            pending.replaceRangeAssumeCapacity(0, nl + 1, &.{});
            if (std.mem.startsWith(u8, line, "mky step ")) {
                last_step = std.fmt.parseInt(usize, line["mky step ".len..], 10) catch last_step;
                said.clearRetainingCapacity();
            } else if (std.mem.startsWith(u8, line, "mky fail ")) {
                var it = std.mem.splitScalar(u8, line["mky fail ".len..], ' ');
                const at = std.fmt.parseInt(usize, it.next().?, 10) catch 0;
                try found.append(gpa, .{ .step = at, .sig = try gpa.dupe(u8, it.rest()) });
            } else {
                if (panic_line == null and std.mem.indexOf(u8, line, "panic: ") != null) panic_line = line;
                if (!quiet and (std.mem.startsWith(u8, line, "[seed ") or std.mem.startsWith(u8, line, "  "))) {
                    std.debug.print("{s}\n", .{line});
                } else try said.print(a, "{s}\n", .{line});
            }
        }
    }
    var status: c_int = 0;
    _ = libc.waitpid(pid, &status, 0);
    const sig = status & 0x7f;
    const code = (status >> 8) & 0xff;
    const lost: ?[]const u8 = if (hung)
        print(a, "hang:no step finished within {d} ms", .{hang_ms})
    else if (sig != 0)
        signature(a, "crash", print(a, "signal {d} {s}", .{ sig, panic_line orelse "" }))
    else if (code > 1)
        print(a, "harness:exit {d}", .{code})
    else
        null;
    if (lost) |why| {
        if (!quiet) std.debug.print("{s}[seed {d} step {d}] FAIL {s}\n", .{ if (sig != 0 or code > 1) said.items else "", seed, last_step, why });
        try found.append(gpa, .{ .step = last_step, .sig = try gpa.dupe(u8, why) });
    }
    return found.toOwnedSlice(gpa);
}

/// Zeller's delta debugging: a 1-minimal failing subsequence.
fn ddmin(a: Allocator, start: []const usize, ctx: anytype, fails: fn (@TypeOf(ctx), []const usize) bool) ![]const usize {
    var items = start;
    var n: usize = 2;
    while (items.len >= 2) {
        const size = @max(1, items.len / n);
        var reduced = false;
        var lo: usize = 0;
        while (lo < items.len) : (lo += size) {
            const hi = @min(items.len, lo + size);
            const complement = try std.mem.concat(a, usize, &.{ items[0..lo], items[hi..] });
            if (complement.len > 0 and fails(ctx, complement)) {
                items = complement;
                n = @max(n - 1, 2);
                reduced = true;
                break;
            }
        }
        if (!reduced) {
            if (n >= items.len) break;
            n = @min(items.len, n * 2);
        }
    }
    return items;
}

const Trial = struct {
    gpa: Allocator,
    seed: u64,
    steps: usize,
    sig: []const u8,
    budget: *usize,

    fn fails(t: *const Trial, only: []const usize) bool {
        if (t.budget.* == 0) return false;
        t.budget.* -= 1;
        const found = spawn(t.gpa, t.seed, t.steps, only, false, true) catch return false;
        return found.len > 0 and std.mem.eql(u8, found[0].sig, t.sig);
    }
};

fn shrink(gpa: Allocator, seed: u64, f: Found, budget: usize) !void {
    var left = budget;
    const trial: Trial = .{ .gpa = gpa, .seed = seed, .steps = f.step + 1, .sig = f.sig, .budget = &left };
    const body = try gpa.alloc(usize, f.step + 1);
    for (body, 0..) |*b, i| b.* = i;
    if (!trial.fails(body)) {
        std.debug.print("  not reproduced on replay: --seed {d} --steps {d}\n", .{ seed, f.step + 1 });
        return;
    }
    const small = try ddmin(gpa, body, &trial, Trial.fails);
    var text: std.ArrayList(u8) = .empty;
    for (small, 0..) |n, i| try text.print(gpa, "{s}{d}", .{ if (i > 0) "," else "", n });
    std.debug.print("  shrunk {d} -> {d} ops: zig build monkey-session -Dplatform=tui -- --seed {d} --steps {d} --only {s} -v\n", .{ body.len, small.len, seed, f.step + 1, text.items });
}

fn usage() noreturn {
    std.debug.print("usage: pardes-monkey-session [--seed N]... [--steps M] [--only i,j,k] [--no-shrink] [--shrink-budget N] [--smoke] [-v]\n", .{});
    std.process.exit(2);
}

pub fn main(init: std.process.Init) !void {
    const gpa = std.heap.page_allocator;
    const args = try init.minimal.args.toSlice(init.arena.allocator());
    var seeds: std.ArrayList(u64) = .empty;
    var steps: ?usize = null;
    var only: ?[]const usize = null;
    var no_shrink = false;
    var budget: usize = 300;
    var smoke = false;
    var verbose = false;
    var i: usize = 1;
    while (i < args.len) : (i += 1) {
        const arg = args[i];
        const value = if (i + 1 < args.len) args[i + 1] else "";
        if (std.mem.eql(u8, arg, "--seed")) {
            try seeds.append(gpa, std.fmt.parseInt(u64, value, 10) catch usage());
            i += 1;
        } else if (std.mem.eql(u8, arg, "--steps")) {
            steps = std.fmt.parseInt(usize, value, 10) catch usage();
            i += 1;
        } else if (std.mem.eql(u8, arg, "--only")) {
            var list: std.ArrayList(usize) = .empty;
            var it = std.mem.tokenizeScalar(u8, value, ',');
            while (it.next()) |n| try list.append(gpa, std.fmt.parseInt(usize, n, 10) catch usage());
            only = list.items;
            i += 1;
        } else if (std.mem.eql(u8, arg, "--shrink-budget")) {
            budget = std.fmt.parseInt(usize, value, 10) catch usage();
            i += 1;
        } else if (std.mem.eql(u8, arg, "--no-shrink")) {
            no_shrink = true;
        } else if (std.mem.eql(u8, arg, "--smoke")) {
            smoke = true;
        } else if (std.mem.eql(u8, arg, "-v") or std.mem.eql(u8, arg, "--verbose")) {
            verbose = true;
        } else usage();
    }
    if (libc.getenv("MKY_ROOT")) |root| {
        var check: std.heap.ArenaAllocator = .init(gpa);
        selfCheck(check.allocator());
        check.deinit();
        // An error out of the run (a fresh session's /index unparsable, its
        // log refusing `follow new`) is a harness failure, not exit 1 with no
        // failure line, which the parent would take for a clean run.
        std.process.exit(fuzz(gpa, if (seeds.items.len > 0) seeds.items[0] else 1, steps orelse 1000, only, verbose, std.mem.span(root)) catch |err| {
            std.debug.print("mky error {t}\n", .{err});
            std.process.exit(3);
        });
    }
    if (smoke) {
        // A short fixed-seed run that fails on any failure.
        seeds.clearRetainingCapacity();
        try seeds.append(gpa, 20260929);
        steps = steps orelse 800;
        no_shrink = true;
    }
    if (seeds.items.len == 0) try seeds.append(gpa, 1);
    var total: usize = 0;
    for (seeds.items) |seed| {
        const found = try spawn(gpa, seed, steps orelse 1000, only, verbose, false);
        total += found.len;
        if (no_shrink or only != null) continue;
        var seen: std.StringHashMapUnmanaged(void) = .empty;
        for (found) |f| if (!(try seen.getOrPut(gpa, f.sig)).found_existing) try shrink(gpa, seed, f, budget);
    }
    if (total > 0) std.process.exit(1);
}