summaryrefslogtreecommitdiff
path: root/src/tape.zig
blob: f0766bd1e940722166a55c72c740949851aed000 (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
//! The recorder and its tape (docs/next-daemon.md, phase 1). Recording,
//! everything the core consumes is appended in the order it consumed it,
//! which is the turn's order (a 9P request served while a step is out in a
//! syscall lands inside that syscall's record):
//!
//!   - each call the host makes into the core (`Input`), with its arguments;
//!   - each call the core makes out into the host -- a Host function, or a
//!     syscall of its own (`Site`) -- and what it returned;
//!   - each field the host wrote in the core between calls (`watched`);
//!   - after every call the host made at the top, the session's digest.
//!
//! Replaying (`PARDES_REPLAY=<tape> pardes`, the same build), a core takes
//! its inputs off the tape instead: a host call answers what it answered, a
//! site returns what it returned, nothing is performed and no syscall is
//! made, and each digest is checked against the recorded one -- by two
//! replicas at different addresses and a third that never draws.
//!
//! The local path pays a null check a call: `on` is null unless
//! PARDES_RECORD names where to write, `<prefix><pid>.tape`.
const std = @import("std");
const pardes = @import("pardes.zig");
const walk = @import("walk.zig");
const Pardes = pardes.Pardes;
const VTable = pardes.Host.VTable;
const libc = std.c;

pub var on: ?*Tape = null;

/// Every call into the core a host makes (a 9P connection task's too).
pub const Input = union(enum) {
    init: pardes.Options,
    init_from_dump: struct { opts: pardes.Options, bytes: []const u8 },
    restore: struct { bytes: []const u8, from: []const u8 },
    /// Whether it was the session's own core, or one a Restore replaced.
    deinit: bool,
    update: pardes.Event,
    serve_fs: pardes.ctlfs.Req,
    pump,
    step,
    perform: pardes.Effect,
    next_effect,
    advance: u64,
    render,
    take_restore,
    set_cwd: struct { id: usize, cwd: []const u8 },
    acknowledge_shell: struct { id: usize, executable: []const u8, prompt_marks: bool },
    shell_failed: struct { id: u8, err: anyerror },
    restart_failed: struct { id: u8, err: anyerror },
    save_failed: struct { id: u8, path: []const u8, err: anyerror },
    dump_failed: struct { path: []const u8, err: anyerror },
    set_last_dump: []const u8,
    set_message: struct { id: usize, text: []const u8 },
    set_status: struct { id: usize, text: []const u8 },
    report_error: struct { id: usize, operation: []const u8, err: anyerror },
    reload_watched_file: struct { id: u8, bytes: []const u8 },
    note_late_failure: usize,
    invalidate,
    acknowledge_panel_presentation: []const pardes.animation.Track,
    run_closed_lines,
    file_deleted: u8,
    open_view,
    close_view: u8,
    to_view: u8,
};

/// The core's own syscalls (each `turn.yield()`s around itself), numbered
/// after the Host functions on the tape.
pub const Site = enum(u16) {
    // Out of the turn (`site`).
    os_stat,
    os_listing,
    os_rw,
    make_dirs,
    resolve,
    read_mount,
    read_file,
    dir_listing,
    kind,
    probe,
    preview,
    // Read in place (`value`).
    clock,
    budget,
    wake,
    env,
    interpreter,
    denied,
    shell,
    mtime,
    cwd,
    pointer,
    /// What a terminal's ingest told the positions pardes keeps over it.
    positions,
    /// Whether a terminal is at its prompt, as a step reads it after its
    /// output (a greeting, a command waiting for the prompt): a
    /// screen-state replica's emulator is the last delta's until the next.
    prompt,
};

/// Fields a host writes straight into the core between its calls.
const watched = .{ "native_images", "native_image_patches", "present_skipped", "shell_continuous", "needs_frame", "quit", "windowed", "active", "unattended" };

const Kind = enum(u8) { input, done, host, ret, set, digest };
/// A digest record's kind byte (replica.zig reads a unit's last).
pub const digest_kind: u8 = @backingInt(Kind.digest);
pub const set_kind: u8 = @backingInt(Kind.set);
const host_fns = @typeInfo(VTable).@"struct".field_names;
/// The `set` that says which Host functions the recording host had.
const set_mask = 0xff;

pub const Tape = struct {
    gpa: std.mem.Allocator,
    replay: bool,
    /// The core whose digest is taken (a Restore replaces it).
    core: ?*Pardes = null,
    /// The host has the turn: a call into the core now is an input, and a
    /// call out of it is the host's to answer.
    host_side: bool = true,
    depth: u32 = 0,
    vtable: VTable = .{},
    // Recording.
    fd: c_int = -1,
    buf: std.ArrayList(u8) = .empty,
    scratch: std.Io.Writer.Allocating,
    real: pardes.Host = .{},
    seen: [watched.len]u64 = @splat(0),
    // Replaying.
    in: walk.Reader = .{ .bytes = &.{} },
    arena: std.heap.ArenaAllocator,
    last_ret: walk.Reader = .{ .bytes = &.{} },
    mask: u64 = 0,
    headless: bool = false,
    digests: u64 = 0,
    diverged: ?[]const u8 = null,
    /// The input being applied, for a divergence's report.
    applying: []const u8 = "",
    /// PARDES_RECORD_EVERY: a digest after every input, a nested one's too
    /// (to find the step a divergence starts at), not only the host's last.
    every: bool = false,
    /// A replica applying its own input before the authority has ordered it
    /// (src/replica.zig): recorded as the authority would record it, so the
    /// two can be compared, but nothing is performed. A host call or a
    /// syscall stops it (`poisoned`): what the call returns is the
    /// authority's to say, and the input waits for it. A read of the clock
    /// or the environment (`value`) is made here, a guess the comparison
    /// checks.
    speculating: bool = false,
    poisoned: bool = false,
    /// The allocator of the core a replay makes (a replica's rollback epoch,
    /// src/rollback.zig), when it is not the tape's own.
    core_gpa: ?std.mem.Allocator = null,
    /// Where a replay keeps what a core keeps of an input (its options),
    /// for a tape that is let go; else the tape's allocator, for good.
    kept_arena: ?std.heap.ArenaAllocator = null,
    /// No digests recorded (a speculation's: a replica compares what it
    /// was given and what it asked, from a state the authority shares).
    no_digest: bool = false,
    /// PARDES_REPLAY_DUMP (phase 2's gate): at the session's end, its dump
    /// loaded must be it, digest for digest, and dump again byte for byte.
    dump_check: bool = false,
    dumps: usize = 0,
    dump_bytes: usize = 0,
    /// Screen-state terminals (src/screen_state.zig, docs/next-daemon.md
    /// phase 5): an authority recording for replicas, or a replica. A
    /// terminal's emulator is the authority's alone: its output is recorded
    /// without its bytes (they cross as screen state), its pty writes are
    /// not recorded, and no digest covers it. A replica's core never writes
    /// one but by `apply`.
    screen_state: bool = false,
    /// A digest as one word (screen-state replicas: every unit of a pty's
    /// output carries one, at the pty's read rate), not one a field; what
    /// differs is then not named.
    whole_digest: bool = false,
    /// A digest only when asked (`recordDigest`): the replica stream's,
    /// one a unit, not one after each of its inputs.
    unit_digests: bool = false,
    /// A replica client's guess of the session's clocks for a speculation
    /// (src/replica.zig): its clock reads (a message's stamp, a file's
    /// mtime) answer this instead of the clocks here.
    guess: ?Guess = null,

    fn record(t: *Tape, kind: Kind, comptime T: type, v: *const T) void {
        t.scratch.clearRetainingCapacity();
        walk.encode(T, v, &t.scratch.writer) catch @panic("tape: out of memory");
        const payload = t.scratch.written();
        const len: u32 = @intCast(payload.len);
        t.buf.append(t.gpa, @backingInt(kind)) catch @panic("tape: out of memory");
        t.buf.appendSlice(t.gpa, std.mem.asBytes(&len)) catch @panic("tape: out of memory");
        t.buf.appendSlice(t.gpa, payload) catch @panic("tape: out of memory");
        if (t.buf.items.len > 1 << 20) t.flush();
    }

    fn flush(t: *Tape) void {
        if (comptime !pardes.hosted) return;
        // A tape in memory (a replica's, an authority's stream) is taken
        // by whoever holds it.
        if (t.fd < 0) return;
        var at: usize = 0;
        while (at < t.buf.items.len) {
            const n = libc.write(t.fd, t.buf.items[at..].ptr, t.buf.items.len - at);
            if (n <= 0) break;
            at += @intCast(n);
        }
        t.buf.clearRetainingCapacity();
    }

    /// What the host wrote between calls, said before the core goes on.
    fn noteWrites(t: *Tape) void {
        const p = t.core orelse return;
        inline for (watched, 0..) |name, i| {
            const now = walk.digest(@FieldType(Pardes, name), &@field(p, name));
            if (now != t.seen[i]) {
                t.seen[i] = now;
                const set: struct { u8, @FieldType(Pardes, name) } = .{ i, @field(p, name) };
                t.record(.set, @TypeOf(set), &set);
            }
        }
    }

    fn seeFields(t: *Tape) void {
        const p = t.core orelse return;
        inline for (watched, 0..) |name, i| t.seen[i] = walk.digest(@FieldType(Pardes, name), &@field(p, name));
    }

    /// The host's functions go through the tape while it records, whoever
    /// set `p.host` last.
    fn wrap(t: *Tape, p: *Pardes) void {
        if (p.host.vtable == &t.vtable) return;
        // The same host given again (each pump is given its host): only
        // the wrapper put back.
        if (p.host.vtable == t.real.vtable and p.host.ctx == t.real.ctx) {
            p.host = .{ .ctx = t, .vtable = &t.vtable };
            return;
        }
        t.real = p.host;
        var mask: u64 = 0;
        inline for (host_fns, 0..) |f, i| if (@field(t.real.vtable.*, f) != null) {
            mask |= @as(u64, 1) << i;
        };
        p.host = t.hostOf(mask);
        const set: struct { u8, u64 } = .{ set_mask, mask };
        t.record(.set, @TypeOf(set), &set);
    }

    /// The tape as a host with these functions.
    fn hostOf(t: *Tape, mask: u64) pardes.Host {
        t.mask = mask;
        t.vtable = .{};
        inline for (host_fns, 0..) |f, i| if (mask & (@as(u64, 1) << i) != 0 or
            (t.headless and comptime std.mem.eql(u8, f, "watched")))
        {
            @field(t.vtable, f) = &Wrap(i).f;
        };
        return .{ .ctx = t, .vtable = &t.vtable };
    }

    fn enterInput(t: *Tape, p: ?*Pardes, input: *const Input) Entered {
        if (!t.host_side) return .{};
        t.host_side = false;
        t.depth += 1;
        if (t.replay) return .{ .outer = true };
        if (p) |core| {
            if (t.core == null) t.core = core;
            t.noteWrites();
            t.wrap(core);
        }
        // A terminal's output crosses as screen state, not as its bytes.
        if (t.screen_state and input.* == .update and input.update == .output) {
            var bare = input.*;
            bare.update.output.bytes = &.{};
            t.record(.input, Input, &bare);
        } else t.record(.input, Input, input);
        return .{ .outer = true };
    }

    fn leaveInput(t: *Tape, p: ?*Pardes) void {
        t.depth -= 1;
        t.host_side = true;
        if (t.replay) return;
        if (p) |core| if (t.core == null) {
            t.core = core;
            t.wrap(core);
        };
        t.record(.done, void, &{});
        if ((t.depth == 0 or t.every) and !t.no_digest and !t.unit_digests) if (t.core) |core| {
            var parts: Parts = undefined;
            const whole = [1]u64{digestOf(core)};
            const d: []const u64 = if (t.whole_digest) &whole else digestParts(core, &parts);
            t.record(.digest, []const u64, &d);
        };
        if (t.depth == 0) t.flush();
        t.seeFields();
    }

    // ---- replaying ----

    const Record = struct { kind: Kind, payload: walk.Reader };

    fn next(t: *Tape) ?Record {
        const head = t.in.take(5) catch return null;
        const len = std.mem.bytesToValue(u32, head[1..5]);
        const payload = t.in.take(len) catch return null;
        return .{ .kind = @fromBackingInt(@intCast(head[0])), .payload = .{ .bytes = payload } };
    }

    fn peek(t: *const Tape) ?Kind {
        if (t.in.bytes.len - t.in.at < 5) return null;
        return @fromBackingInt(@intCast(t.in.bytes[t.in.at]));
    }

    fn diverge(t: *Tape, comptime why: []const u8, args: anytype) void {
        if (t.diverged != null) return;
        t.diverged = std.fmt.allocPrint(t.gpa, why ++ " (tape byte {d}, in {s})", args ++ .{ t.in.at, t.applying }) catch why;
    }

    /// A host call the headless replica does not make (it draws no frame,
    /// so presents none): what the host told the core inside it (what it
    /// presented, `acknowledge_panel_presentation`) is applied all the
    /// same, the host's own calls below it stepped over.
    fn skipHost(t: *Tape) void {
        while (t.diverged == null) {
            var rec = t.next() orelse return;
            switch (rec.kind) {
                .ret => return,
                .input => t.apply(&rec.payload),
                .set => t.applySet(&rec.payload),
                .host => t.skipHost(),
                .digest => t.check(&rec.payload),
                .done => return t.diverge("a host call's records are out of order", .{}),
            }
        }
    }

    /// Over a call the replica does not make (the headless one's present),
    /// what was applied inside it too.
    fn skip(t: *Tape) void {
        var depth: usize = 1;
        while (depth > 0) {
            const rec = t.next() orelse return;
            switch (rec.kind) {
                .host, .input => depth += 1,
                .ret, .done => depth -= 1,
                else => {},
            }
        }
    }

    /// The records of an input up to its end: what was applied inside it,
    /// as it was; the headless replica steps over the host calls it did
    /// not make.
    fn finishInput(t: *Tape) void {
        while (t.diverged == null) {
            const kind = t.peek() orelse return t.diverge("the tape ends inside an input", .{});
            var rec = t.next().?;
            switch (kind) {
                .done => return,
                .set => t.applySet(&rec.payload),
                .host => if (t.headless) t.skipHost() else return t.diverge("the core did not make the host call {d} the tape has", .{std.mem.bytesToValue(u16, rec.payload.bytes[0..2])}),
                .input => t.apply(&rec.payload),
                .digest => t.check(&rec.payload),
                .ret => return t.diverge("the core's input ended before the recorded one", .{}),
            }
        }
    }

    /// The records inside a host call up to its return, whose result is
    /// left in `last_ret`.
    fn finishHost(t: *Tape) bool {
        while (t.diverged == null) {
            var rec = t.next() orelse {
                t.diverge("the tape ends inside a host call", .{});
                return false;
            };
            switch (rec.kind) {
                .ret => {
                    t.last_ret = rec.payload;
                    return true;
                },
                .input => t.apply(&rec.payload),
                .set => t.applySet(&rec.payload),
                .digest => t.check(&rec.payload),
                .host, .done => {
                    t.diverge("a host call's records are out of order", .{});
                    return false;
                },
            }
        }
        return false;
    }

    fn expectHost(t: *Tape, id: u16, comptime what: []const u8) bool {
        if (t.diverged != null) return false;
        const rec = t.next() orelse {
            t.diverge("the core called " ++ what ++ " past the tape's end", .{});
            return false;
        };
        if (rec.kind != .host or std.mem.bytesToValue(u16, rec.payload.bytes[0..2]) != id) {
            const other: u16 = if (rec.kind == .host) std.mem.bytesToValue(u16, rec.payload.bytes[0..2]) else 0;
            t.diverge("the core called " ++ what ++ " where the tape has a {t} ({d})", .{ rec.kind, other });
            return false;
        }
        const outer = !t.host_side;
        t.host_side = true;
        defer t.host_side = !outer;
        return t.finishHost();
    }

    /// A recorded digest against the replica's own.
    fn check(t: *Tape, r: *walk.Reader) void {
        const want = walk.decode([]const u64, r, t.arena.allocator()) catch return;
        const p = t.core orelse return;
        var parts: Parts = undefined;
        const whole = [1]u64{digestOf(p)};
        const got: []const u64 = if (want.len == 1) &whole else digestParts(p, &parts);
        if (!std.mem.eql(u64, got, want)) t.diverge("digest {d} differs in {s}", .{ t.digests, if (want.len == 1) "the session (a digest of one word: run the authority with whole_digest off to name the field)" else partsDiffer(got, want, t.arena.allocator()) });
        t.digests += 1;
    }

    /// The session's dump loaded into a core of its own, which must be the
    /// same session (its digest) and dump to the same bytes. The tape is
    /// put aside meanwhile: a dump is no input.
    fn dumpRoundTrip(t: *Tape, p: *Pardes) void {
        on = null;
        defer on = t;
        const bytes = pardes.dump.dumpBytes(p) catch |err| return t.diverge("dump failed: {t}", .{err});
        defer t.gpa.free(bytes);
        var notes: std.ArrayList(u8) = .empty;
        defer notes.deinit(t.gpa);
        const q = pardes.dump.load(t.gpa, p.opts, bytes, &notes) catch |err| return t.diverge("dump load failed: {t}", .{err});
        defer q.deinit();
        if (notes.items.len > 0) return t.diverge("dump of this build had notes: {s}", .{notes.items});
        var want: Parts = undefined;
        var got: Parts = undefined;
        const a = digestParts(p, &want);
        const b = digestParts(q, &got);
        if (!std.mem.eql(u64, a, b)) return t.diverge("dump round trip: the loaded digest differs in {s}", .{partsDiffer(b, a, t.arena.allocator())});
        const again = pardes.dump.dumpBytes(q) catch |err| return t.diverge("dump of the loaded failed: {t}", .{err});
        defer t.gpa.free(again);
        if (!std.mem.eql(u8, bytes, again)) {
            const at = std.mem.indexOfDiff(u8, bytes, again) orelse 0;
            return t.diverge("dump is no fixpoint: {d} bytes then {d}, first differing at {d}", .{ bytes.len, again.len, at });
        }
        t.dumps += 1;
        t.dump_bytes += bytes.len;
    }

    fn applySet(t: *Tape, r: *walk.Reader) void {
        const which = (r.take(1) catch return)[0];
        if (which == set_mask) {
            const host = t.hostOf(std.mem.bytesToValue(u64, r.take(8) catch return));
            if (t.core) |p| p.host = host;
            return;
        }
        const p = t.core orelse return;
        inline for (watched, 0..) |name, i| if (i == which) {
            @field(p, name) = walk.decode(@FieldType(Pardes, name), r, t.arena.allocator()) catch return;
        };
    }

    /// One input, applied to the replica as the host applied it.
    fn apply(t: *Tape, r: *walk.Reader) void {
        // A session's options are kept by its core (`opts`): theirs for good.
        const Tag = std.meta.Tag(Input);
        const tag = std.mem.bytesToValue(Tag, (r.take(@sizeOf(Tag)) catch return t.diverge("a short input", .{})));
        r.at -= @sizeOf(Tag);
        const kept = tag == .init or tag == .init_from_dump or tag == .restore;
        const keep = if (t.kept_arena) |*k| k.allocator() else t.gpa;
        const input = walk.decode(Input, r, if (kept) keep else t.arena.allocator()) catch return t.diverge("a short input", .{});
        const outer = t.applying;
        t.applying = switch (input) {
            .update => |ev| @tagName(ev),
            else => @tagName(input),
        };
        defer t.applying = outer;
        t.host_side = true;
        defer t.host_side = true;
        switch (input) {
            .init, .init_from_dump => {
                var opts = switch (input) {
                    .init => |o| o,
                    .init_from_dump => |d| d.opts,
                    else => unreachable,
                };
                const gpa = t.core_gpa orelse t.gpa;
                opts.image_allocator = gpa;
                opts.pdf_allocator = gpa;
                opts.tree_sitter_allocator = gpa;
                opts.frame_allocator = gpa;
                t.core = switch (input) {
                    .init => Pardes.init(gpa, opts),
                    .init_from_dump => |d| pardes.dump.initFromDump(gpa, opts, d.bytes),
                    else => unreachable,
                } catch |err| return t.diverge("init failed: {t}", .{err});
                t.core.?.host = t.hostOf(t.mask);
                return t.finishInput();
            },
            else => {},
        }
        const p = t.core orelse return t.diverge("an input before the core", .{});
        switch (input) {
            .init, .init_from_dump => unreachable,
            .restore => |rs| {
                const nc = pardes.dump.restore(p, rs.bytes, rs.from) catch |err| return t.diverge("restore failed: {t}", .{err});
                t.core = nc;
                nc.host = t.hostOf(t.mask);
            },
            // The session is over (or the core a Restore replaced is
            // freed): nothing done now is compared.
            .deinit => |own| {
                if (own) {
                    if (t.dump_check) if (t.core) |core| t.dumpRoundTrip(core);
                    t.core = null;
                }
                return t.skip();
            },
            .update => |ev| p.update(ev),
            .serve_fs => |req| _ = p.serveFs(req),
            .pump => p.pump(t.hostOf(t.mask)) catch {},
            .step => p.step(),
            .perform => |e| p.perform(e),
            .next_effect => _ = p.nextEffect(),
            .advance => |now| p.advance(now),
            .render => if (t.headless) return t.skip() else {
                _ = p.frame_arena.reset(.retain_capacity);
                _ = p.render(p.frame_arena.allocator()) catch {};
            },
            .take_restore => _ = p.takeRestore(),
            .set_cwd => |a| p.setCwd(a.id, a.cwd),
            .acknowledge_shell => |a| p.acknowledgeShell(a.id, a.executable, a.prompt_marks),
            .shell_failed => |a| p.shellFailed(a.id, a.err),
            .restart_failed => |a| p.restartFailed(a.id, a.err),
            .save_failed => |a| p.saveFailed(a.id, a.path, a.err),
            .dump_failed => |a| p.dumpFailed(a.path, a.err),
            .set_last_dump => |path| p.setLastDump(path),
            .set_message => |a| p.setMessage(a.id, a.text),
            .set_status => |a| p.setStatus(a.id, a.text),
            .report_error => |a| p.reportError(a.id, a.operation, a.err),
            .reload_watched_file => |a| _ = p.reloadWatchedFile(a.id, a.bytes),
            .note_late_failure => |id| p.noteLateFailure(id),
            .invalidate => p.invalidate(),
            .acknowledge_panel_presentation => |tracks| p.acknowledgePanelPresentation(tracks),
            .run_closed_lines => pardes.ctlfs.runClosedLines(p),
            .file_deleted => |id| pardes.panes.File.deleted(p, id),
            .open_view => _ = p.openView(),
            .close_view => |k| p.closeView(k),
            .to_view => |k| p.toView(k),
        }
        t.finishInput();
    }
};

const pardes_fields = walk.fieldNames(Pardes);
const stream_fields = walk.fieldNames(pardes.Stream);
const pane_fields = walk.fieldNames(pardes.Pane);
const file_fields = walk.fieldNames(pardes.panes.File.State);
const places_fields = walk.fieldNames(pardes.panes.Places);
const Parts = [pardes_fields.len + stream_fields.len + pardes.MAX_PANES * (2 + pane_fields.len + file_fields.len + places_fields.len)]u64;

/// The session's digest as one per field of the core's, its stream's, and
/// each pane's and its file's, so a divergence says where it is.
fn digestParts(p: *const Pardes, parts: *Parts) []const u64 {
    var n = walk.fieldDigests(Pardes, p, parts[0..pardes_fields.len]).len;
    n += walk.fieldDigests(pardes.Stream, &p.stream, parts[n..][0..stream_fields.len]).len;
    for (p.panes, 0..) |slot, id| {
        const pane = slot orelse continue;
        parts[n] = id;
        parts[n + 1] = @intFromBool(pane.file != null);
        n += 2;
        n += walk.fieldDigests(pardes.Pane, pane, parts[n..][0..pane_fields.len]).len;
        if (pane.file) |*f| n += walk.fieldDigests(pardes.panes.File.State, f, parts[n..][0..file_fields.len]).len;
        n += walk.fieldDigests(pardes.panes.Places, &pane.places, parts[n..][0..places_fields.len]).len;
    }
    return parts[0..n];
}

/// The fields whose digests differ, named.
fn partsDiffer(got: []const u64, want: []const u64, a: std.mem.Allocator) []const u8 {
    var said: std.ArrayList(u8) = .empty;
    if (got.len != want.len) said.print(a, "the panes there are ({d} parts, {d} recorded); ", .{ got.len, want.len }) catch {};
    const n = @min(got.len, want.len);
    var i: usize = 0;
    var count: usize = 0;
    while (i < pardes_fields.len + stream_fields.len and i < n) : (i += 1) {
        if (got[i] == want[i]) continue;
        const name = if (i < pardes_fields.len) pardes_fields[i] else stream_fields[i - pardes_fields.len];
        said.print(a, "{s}{s} ", .{ if (i < pardes_fields.len) "" else "stream.", name }) catch {};
        count += 1;
    }
    while (i + 2 <= n and count < 16) {
        const id = got[i];
        const has_file = got[i + 1] != 0;
        i += 2;
        for (pane_fields) |name| {
            if (i >= n) break;
            if (got[i] != want[i]) {
                said.print(a, "pane {d}.{s} ", .{ id, name }) catch {};
                count += 1;
            }
            i += 1;
        }
        if (has_file) for (file_fields) |name| {
            if (i >= n) break;
            if (got[i] != want[i]) {
                said.print(a, "pane {d}.file.{s} ", .{ id, name }) catch {};
                count += 1;
            }
            i += 1;
        };
        for (places_fields) |name| {
            if (i >= n) break;
            if (got[i] != want[i]) {
                said.print(a, "pane {d}.places.{s} ", .{ id, name }) catch {};
                count += 1;
            }
            i += 1;
        }
    }
    return said.items;
}

/// A call into the core: the host's, and recorded, or the core's own.
pub const Entered = struct { outer: bool = false };

pub inline fn enter(p: ?*Pardes, input: Input) Entered {
    const t = on orelse return .{};
    return t.enterInput(p, &input);
}

pub inline fn leave(p: ?*Pardes, e: Entered) void {
    if (e.outer) on.?.leaveInput(p);
}

/// The core a Restore made is the session's from here.
pub fn adopt(p: *Pardes) void {
    const t = on orelse return;
    if (t.replay) return;
    t.core = p;
    t.wrap(p);
}

/// Whether `p` is the core being recorded (a Restore's old one is not).
pub fn current(p: *const Pardes) bool {
    const t = on orelse return true;
    return t.core == null or t.core == p;
}

/// The session's core is freed: no digest of it after.
pub fn forget(p: *Pardes) void {
    const t = on orelse return;
    if (t.core == p) t.core = null;
}

/// The Host function `i`, through the tape.
fn Wrap(comptime i: usize) type {
    const Fn = @typeInfo(@typeInfo(@FieldType(VTable, host_fns[i])).optional.child).pointer.child;
    const info = @typeInfo(Fn).@"fn";
    const R = info.return_type.?;
    const P = info.param_types;
    return switch (P.len) {
        1 => struct {
            fn f(ctx: ?*anyopaque) R {
                return hostCall(i, R, ctx, .{});
            }
        },
        2 => struct {
            fn f(ctx: ?*anyopaque, a: P[1].?) R {
                return hostCall(i, R, ctx, .{a});
            }
        },
        3 => struct {
            fn f(ctx: ?*anyopaque, a: P[1].?, b: P[2].?) R {
                return hostCall(i, R, ctx, .{ a, b });
            }
        },
        4 => struct {
            fn f(ctx: ?*anyopaque, a: P[1].?, b: P[2].?, c: P[3].?) R {
                return hostCall(i, R, ctx, .{ a, b, c });
            }
        },
        5 => struct {
            fn f(ctx: ?*anyopaque, a: P[1].?, b: P[2].?, c: P[3].?, d: P[4].?) R {
                return hostCall(i, R, ctx, .{ a, b, c, d });
            }
        },
        else => @compileError("tape: a host function of more arguments"),
    };
}

fn hostCall(comptime i: usize, comptime R: type, ctx: ?*anyopaque, args: anytype) R {
    const t: *Tape = @ptrCast(@alignCast(ctx.?));
    const name = host_fns[i];
    // A terminal's answers and what is typed at it from its output are the
    // authority's to write, never a replica's: off the tape on both sides.
    if (comptime std.mem.eql(u8, name, "pty_write")) if (t.screen_state and !t.speculating) {
        if (!t.replay) @call(.auto, @field(t.real.vtable.*, name).?, .{t.real.ctx} ++ args);
        return;
    };
    if (t.replay) {
        // A replica that never draws is never watched: it asks no one.
        if (comptime std.mem.eql(u8, name, "watched")) if (t.headless) return false;
        if (!t.expectHost(i, name)) return std.mem.zeroes(R);
        var ret = t.last_ret;
        if (R == ?[]const u8) {
            // fg_name's answer is written into the caller's buffer.
            const text = (walk.decode(R, &ret, t.arena.allocator()) catch return null) orelse return null;
            const buf = args[args.len - 1];
            const n = @min(text.len, buf.len);
            @memcpy(buf[0..n], text[0..n]);
            return buf[0..n];
        }
        return walk.decode(R, &ret, t.arena.allocator()) catch std.mem.zeroes(R);
    }
    const id: u16 = i;
    if (t.speculating) {
        // Nobody watches a replica's step: its frames are its own, drawn
        // outside it (and the authority's host says so too, or the
        // comparison fails).
        if (comptime std.mem.eql(u8, name, "watched")) {
            const r = false;
            t.record(.host, u16, &id);
            t.record(.ret, R, &r);
            return r;
        }
        t.poisoned = true;
        return poisonOf(R);
    }
    const real = @field(t.real.vtable.*, name).?;
    t.record(.host, u16, &id);
    t.seeFields();
    const outer = !t.host_side;
    t.host_side = true;
    const r = @call(.auto, real, .{t.real.ctx} ++ args);
    t.host_side = !outer;
    t.noteWrites();
    t.record(.ret, R, &r);
    return r;
}

/// A syscall of the core's own, `f(args)`, made out of the turn (a 9P
/// request through a mount of this very session is answered meanwhile):
/// recorded with what it returned, or answered from the tape and never
/// made. A result with slices is decoded into `a` on replay, the allocator
/// `f` would have used.
pub fn site(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? {
    return call(s, a, f, args, true);
}

/// A read of the host's that is no syscall out of the turn (the clock, the
/// environment) or yields for itself: as `site`.
pub fn value(comptime s: Site, comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? {
    return call(s, null, f, args, false);
}

fn call(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anytype, comptime yields: bool) @typeInfo(@TypeOf(f)).@"fn".return_type.? {
    const R = @typeInfo(@TypeOf(f)).@"fn".return_type.?;
    // No tape where there is no host to record or replay one.
    if (comptime !pardes.hosted) return if (yields) out(f, args) else @call(.auto, f, args);
    const t = on orelse return if (yields) out(f, args) else @call(.auto, f, args);
    const id: u16 = @as(u16, host_fns.len) + @backingInt(s);
    if (t.replay) {
        if (!t.expectHost(id, @tagName(s))) {
            std.debug.print("replay: {s}\n", .{t.diverged orelse "?"});
            @panic("tape: the replay diverged at a host call of the core's own");
        }
        var ret = t.last_ret;
        return walk.decode(R, &ret, a orelse t.arena.allocator()) catch @panic("tape: a short result");
    }
    // The host's own (a shell's spawn asking the shell), or one inside a
    // site's syscall: the outer call is the record.
    if (t.host_side) return if (yields) out(f, args) else @call(.auto, f, args);
    // A syscall a speculating replica must not make: its input waits for
    // the authority's answer.
    if (yields and t.speculating) {
        t.poisoned = true;
        return poisonOf(R);
    }
    if (comptime s == .clock or s == .mtime) if (t.speculating) if (t.guess) |g| {
        const r = guessed(R, g);
        t.record(.host, u16, &id);
        t.record(.ret, R, &r);
        return r;
    };
    t.record(.host, u16, &id);
    t.seeFields();
    t.host_side = true;
    const r = if (yields) out(f, args) else @call(.auto, f, args);
    t.host_side = false;
    t.noteWrites();
    t.record(.ret, R, &r);
    return r;
}

pub const Guess = struct { real_ns: i64, mono_ns: i64, gmtoff: i32 };

/// A clock read of the session's, as a replica client guesses it: a file's
/// mtime (realtime seconds), a message's clock (monotonic ms), a message's
/// time of day (in the session's zone).
fn guessed(comptime R: type, g: Guess) R {
    if (R == u32) return std.math.cast(u32, @divFloor(g.real_ns, std.time.ns_per_s)) orelse 0;
    if (R == i64) return @divFloor(g.mono_ns, std.time.ns_per_ms);
    if (R == [8]u8) {
        var clock: [8]u8 = "--:--:--".*;
        const notime = if (libc.getenv("PARDES_NOTIME")) |v| std.mem.span(v).len != 0 else false;
        if (notime) return clock;
        const day = @mod(@divFloor(g.real_ns, std.time.ns_per_s) + g.gmtoff, 86400);
        _ = std.fmt.bufPrint(&clock, "{d:0>2}:{d:0>2}:{d:0>2}", .{ @as(u32, @intCast(@divFloor(day, 3600))), @as(u32, @intCast(@mod(@divFloor(day, 60), 60))), @as(u32, @intCast(@mod(day, 60))) }) catch {};
        return clock;
    }
    @compileError("tape: no guess of a clock read as " ++ @typeName(R));
}

/// The session's digest recorded now (a unit's end: the replica stream's
/// one, `unit_digests`).
pub fn recordDigest(t: *Tape) void {
    const core = t.core orelse return;
    var parts: Parts = undefined;
    const whole = [1]u64{digestOf(core)};
    const d: []const u64 = if (t.whole_digest) &whole else digestParts(core, &parts);
    t.record(.digest, []const u64, &d);
}

/// A core made outside the tape (a replica client's, from a dump) is the
/// one it replays into.
pub fn adoptCore(t: *Tape, p: *Pardes) void {
    t.core = p;
    p.host = t.hostOf(t.mask);
}

/// A replica joins the session recorded on `t` (src/replica.zig): what the
/// host wrote in the core and which host it is are said again at the next
/// input, for a core made from a dump, which has neither.
pub fn resync(t: *Tape) void {
    t.seen = @splat(0);
    if (t.core) |p| if (p.host.vtable == &t.vtable) {
        p.host = t.real;
    };
    t.real = .{};
}

/// A budget of `budget_ns` from `since_ns` on the monotonic clock is spent:
/// what the clock said is taped, so a replica stops its work where this did.
pub fn over(since_ns: u64, budget_ns: u64) bool {
    return value(.budget, overNow, .{ since_ns, budget_ns });
}

fn overNow(since_ns: u64, budget_ns: u64) bool {
    return pardes.FrameStats.nowNs() -| since_ns > budget_ns;
}

fn out(comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? {
    pardes.turn.yield();
    defer pardes.turn.back();
    return @call(.auto, f, args);
}

/// Asked by `turn.yield`: a syscall a replica must never make.
pub fn replaying() bool {
    return if (on) |t| t.replay or t.speculating else false;
}

/// A replica speculating (src/replica.zig): state only the authority
/// changes (a terminal's emulator) is left alone, and the input waits.
pub fn speculating() bool {
    return if (on) |t| t.speculating else false;
}

/// Screen-state terminals, on either side (`Tape.screen_state`).
pub fn screenState() bool {
    return if (on) |t| t.screen_state else false;
}

/// A replica's core under screen-state terminals: its emulators are
/// written by `apply` alone, never by its own steps.
pub fn screenReplica() bool {
    return if (on) |t| t.screen_state and (t.replay or t.speculating) else false;
}

/// What a speculation reached is the authority's: the input is undone and
/// waits for the authority's order.
pub fn poison() void {
    if (on) |t| if (t.speculating) {
        t.poisoned = true;
    };
}

/// A harmless stand-in for what a host call or a syscall would have
/// returned to a speculation that is thrown away once it returns: an
/// error where one can be said, else zeroes.
fn poisonOf(comptime R: type) R {
    return switch (@typeInfo(R)) {
        .error_union => |e| if (@typeInfo(e.error_set).error_set.error_names) |errors|
            (if (errors.len > 0) @field(e.error_set, errors[0]) else std.mem.zeroes(R))
        else
            error.Unexpected,
        .optional => null,
        .void => {},
        .@"union" => |u| inline for (u.field_names, u.field_types) |name, F| {
            if (comptime zeroable(F)) break @unionInit(R, name, std.mem.zeroes(F));
        } else @compileError("tape: no harmless value of " ++ @typeName(R)),
        else => std.mem.zeroes(R),
    };
}

fn zeroable(comptime T: type) bool {
    return switch (@typeInfo(T)) {
        .int, .float, .bool, .optional, .void => true,
        .pointer => |p| p.size == .slice,
        .array => |a| zeroable(a.child),
        .@"struct" => |s| blk: {
            for (s.field_types) |F| if (!zeroable(F)) break :blk false;
            break :blk true;
        },
        else => false,
    };
}

// ---- a replica's tapes in memory (src/replica.zig) ----

/// A tape recorded in memory: an authority's stream, taken unit by unit.
pub fn memory(gpa: std.mem.Allocator) Tape {
    return .{ .gpa = gpa, .replay = false, .scratch = .init(gpa), .arena = .init(gpa), .kept_arena = .init(gpa) };
}

/// What a tape in memory holds, given back (its core is gone already).
pub fn release(t: *Tape) void {
    t.buf.deinit(t.gpa);
    t.scratch.deinit();
    t.arena.deinit();
    if (t.kept_arena) |*k| k.deinit();
}

/// The session's digest recorded now: a batch of steps recorded without
/// one each (`no_digest`) checked once, at its end.
pub fn digestHere(t: *Tape) void {
    const core = t.core orelse return;
    var parts: Parts = undefined;
    const whole = [1]u64{digestOf(core)};
    const d: []const u64 = if (t.whole_digest) &whole else digestParts(core, &parts);
    t.record(.digest, []const u64, &d);
}

/// What was recorded since the last take; the tape's to reuse after.
pub fn taken(t: *Tape) []const u8 {
    return t.buf.items;
}

pub fn clearTaken(t: *Tape) void {
    t.buf.clearRetainingCapacity();
}

/// The records of `bytes`, top-level inputs and all, applied to the
/// tape's core as the authority applied them, each digest checked; why
/// they diverged, or null.
pub fn replayUnit(t: *Tape, bytes: []const u8) ?[]const u8 {
    t.replay = true;
    t.speculating = false;
    defer t.replay = false;
    t.in = .{ .bytes = bytes };
    while (t.diverged == null) {
        const kind = t.peek() orelse break;
        var rec = t.next().?;
        switch (kind) {
            .input => {
                t.apply(&rec.payload);
                _ = t.arena.reset(.retain_capacity);
            },
            .set => t.applySet(&rec.payload),
            .digest => t.check(&rec.payload),
            .host, .ret, .done => t.diverge("a {t} record at the top", .{kind}),
        }
    }
    return t.diverged;
}

/// Starts recording `t`'s core as a speculation: the host's watched
/// fields as they are now, nothing recorded yet.
pub fn beginSpeculation(t: *Tape) void {
    t.replay = false;
    t.speculating = true;
    t.poisoned = false;
    t.no_digest = true;
    t.buf.clearRetainingCapacity();
    t.seeFields();
}

/// Whether it stayed clean (no host call, no syscall, no authority state).
pub fn endSpeculation(t: *Tape) bool {
    t.speculating = false;
    t.no_digest = false;
    return !t.poisoned;
}

/// Whether two tapes record the same inputs, host calls and answers,
/// their digests aside.
pub fn sameRecords(a: []const u8, b: []const u8) bool {
    var x: walk.Reader = .{ .bytes = a };
    var y: walk.Reader = .{ .bytes = b };
    while (true) {
        const ra = nextRecord(&x);
        const rb = nextRecord(&y);
        if (ra == null or rb == null) return ra == null and rb == null;
        if (ra.?.kind != rb.?.kind or !std.mem.eql(u8, ra.?.payload, rb.?.payload)) return false;
    }
}

fn nextRecord(r: *walk.Reader) ?struct { kind: Kind, payload: []const u8 } {
    while (true) {
        const head = r.take(5) catch return null;
        const payload = r.take(std.mem.bytesToValue(u32, head[1..5])) catch return null;
        const kind: Kind = @fromBackingInt(@intCast(head[0]));
        if (kind != .digest) return .{ .kind = kind, .payload = payload };
    }
}

/// A tape's records, one a line (kind; an input's or a host call's name),
/// for a divergence's report.
pub fn describe(bytes: []const u8, w: *std.Io.Writer) void {
    var r: walk.Reader = .{ .bytes = bytes };
    var depth: usize = 0;
    while (true) {
        const head = r.take(5) catch return;
        const payload = r.take(std.mem.bytesToValue(u32, head[1..5])) catch return;
        const kind: Kind = @fromBackingInt(@intCast(head[0]));
        if (kind == .done or kind == .ret) depth -|= 1;
        w.splatByteAll(' ', depth * 2) catch {};
        switch (kind) {
            .input => {
                const tag = std.mem.bytesToValue(std.meta.Tag(Input), payload[0..@sizeOf(std.meta.Tag(Input))]);
                w.print("input {t}", .{tag}) catch {};
                if (tag == .update) w.print(" {t}", .{std.mem.bytesToValue(std.meta.Tag(pardes.Event), payload[@sizeOf(std.meta.Tag(Input))..][0..@sizeOf(std.meta.Tag(pardes.Event))])}) catch {};
            },
            .host => {
                const id = std.mem.bytesToValue(u16, payload[0..2]);
                w.print("host {s}", .{if (id < host_fns.len) host_fns[id] else @tagName(@as(Site, @fromBackingInt(@intCast(id - host_fns.len))))}) catch {};
            },
            .ret => if (payload.len <= 8) w.print("ret {x}", .{payload}) catch {} else w.print("ret ({d} bytes)", .{payload.len}) catch {},
            else => w.print("{t} ({d} bytes)", .{ kind, payload.len }) catch {},
        }
        w.writeByte('\n') catch {};
        if (kind == .input or kind == .host) depth += 1;
    }
}

/// The digest a replica and its authority must agree on.
pub fn digestOf(p: *const Pardes) u64 {
    return walk.digest(Pardes, p);
}

/// PARDES_RECORD set: the tape is written from the session's first call.
pub fn startFromEnv() void {
    if (comptime !pardes.hosted) return;
    if (on != null) return;
    const prefix = std.mem.span(libc.getenv("PARDES_RECORD") orelse return);
    if (prefix.len == 0) return;
    const gpa = std.heap.c_allocator;
    var path_buf: [4096]u8 = undefined;
    const path = std.fmt.bufPrintSentinel(&path_buf, "{s}{d}.tape", .{ prefix, libc.getpid() }, 0) catch return;
    const fd = libc.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true, .CLOEXEC = true }, @as(libc.mode_t, 0o644));
    if (fd < 0) return;
    const t = gpa.create(Tape) catch return;
    t.* = .{ .gpa = gpa, .replay = false, .fd = fd, .scratch = .init(gpa), .arena = .init(gpa), .every = libc.getenv("PARDES_RECORD_EVERY") != null };
    on = t;
}

/// `PARDES_REPLAY=<tape>`: the tape replayed into three replicas, each
/// digest checked; 0 when none diverged.
pub fn replayMain(io: std.Io, path: []const u8) u8 {
    const gpa = std.heap.c_allocator;
    // What a shell starts before its core (tty.zig's localSession).
    pardes.image.start(io, gpa);
    if (comptime pardes.pdf_enabled) pardes.pdf.start(gpa);
    pardes.syntax.start(gpa);
    const bytes = std.Io.Dir.cwd().readFileAlloc(std.Io.Threaded.global_single_threaded.io(), path, gpa, .unlimited) catch |err| {
        std.debug.print("replay: cannot read {s}: {t}\n", .{ path, err });
        return 2;
    };
    const runs = [_]struct { name: []const u8, headless: bool }{
        .{ .name = "first", .headless = false },
        // The first replica's memory is still held: this one's lies elsewhere.
        .{ .name = "second", .headless = false },
        .{ .name = "headless", .headless = true },
    };
    var failed = false;
    for (runs) |run| {
        var t: Tape = .{ .gpa = gpa, .replay = true, .in = .{ .bytes = bytes }, .headless = run.headless, .scratch = .init(gpa), .arena = .init(gpa) };
        t.dump_check = std.mem.eql(u8, run.name, "first") and libc.getenv("PARDES_REPLAY_DUMP") != null;
        on = &t;
        defer on = null;
        while (t.diverged == null) {
            const kind = t.peek() orelse break;
            var rec = t.next().?;
            switch (kind) {
                .input => {
                    t.apply(&rec.payload);
                    _ = t.arena.reset(.retain_capacity);
                },
                .set => t.applySet(&rec.payload),
                .digest => t.check(&rec.payload),
                .host, .ret, .done => t.diverge("a {t} record at the top", .{kind}),
            }
        }
        // A session killed mid-step leaves its last input cut short: the
        // end of the tape, not a divergence.
        if (t.diverged) |why| if (std.mem.startsWith(u8, why, "the tape ends")) {
            t.diverged = null;
        };
        // One killed (as the harnesses end theirs) has its end state here.
        if (t.dump_check and t.diverged == null) if (t.core) |core| t.dumpRoundTrip(core);
        if (t.diverged) |why| {
            failed = true;
            std.debug.print("replay {s}: {s} replica diverged after {d} digests: {s}\n", .{ path, run.name, t.digests, why });
        } else std.debug.print("replay {s}: {s} replica: {d} digests agree\n", .{ path, run.name, t.digests });
        if (t.dump_check and t.diverged == null) std.debug.print("replay {s}: {d} dumps loaded to their digests and dumped to their bytes again ({d} bytes)\n", .{ path, t.dumps, t.dump_bytes });
    }
    return if (failed) 1 else 0;
}