summaryrefslogtreecommitdiff
path: root/src/tape.zig
blob: ec38deacfa883ab1c542be327ccaa0e382749ae9 (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
//! 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,
    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,
};

/// 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" };

const Kind = enum(u8) { input, done, host, ret, set, digest };
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,

    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, @intFromEnum(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 {
        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);
        }
        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) if (t.core) |core| {
            var parts: Parts = undefined;
            const d = 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 = @enumFromInt(head[0]), .payload = .{ .bytes = payload } };
    }

    fn peek(t: *const Tape) ?Kind {
        if (t.in.bytes.len - t.in.at < 5) return null;
        return @enumFromInt(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 got = digestParts(p, &parts);
        if (!std.mem.eql(u64, got, want)) t.diverge("digest {d} differs in {s}", .{ t.digests, partsDiffer(got, want, t.arena.allocator()) });
        t.digests += 1;
    }

    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 input = walk.decode(Input, r, if (kept) t.gpa 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,
                };
                opts.image_allocator = t.gpa;
                opts.pdf_allocator = t.gpa;
                opts.tree_sitter_allocator = t.gpa;
                opts.frame_allocator = t.gpa;
                t.core = switch (input) {
                    .init => Pardes.init(t.gpa, opts),
                    .init_from_dump => |d| pardes.dump.initFromDump(t.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) 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 {},
            .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];
    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 real = @field(t.real.vtable.*, name).?;
    const id: u16 = i;
    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.?;
    const t = on orelse return if (yields) out(f, args) else @call(.auto, f, args);
    const id: u16 = @as(u16, host_fns.len) + @intFromEnum(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);
    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;
}

/// 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 else false;
}

/// PARDES_RECORD set: the tape is written from the session's first call.
pub fn startFromEnv() void {
    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) };
        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;
        };
        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 });
    }
    return if (failed) 1 else 0;
}