diff options
Diffstat (limited to 'src/tape.zig')
| -rw-r--r-- | src/tape.zig | 53 |
1 files changed, 49 insertions, 4 deletions
diff --git a/src/tape.zig b/src/tape.zig index 84bdce29..0609ce0b 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -150,6 +150,17 @@ pub const Tape = struct { 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, fn record(t: *Tape, kind: Kind, comptime T: type, v: *const T) void { t.scratch.clearRetainingCapacity(); @@ -236,7 +247,12 @@ pub const Tape = struct { t.noteWrites(); t.wrap(core); } - t.record(.input, Input, input); + // 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 }; } @@ -251,7 +267,8 @@ pub const Tape = struct { t.record(.done, void, &{}); if ((t.depth == 0 or t.every) and !t.no_digest) if (t.core) |core| { var parts: Parts = undefined; - const d = digestParts(core, &parts); + 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(); @@ -376,8 +393,9 @@ pub const Tape = struct { 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()) }); + 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; } @@ -653,6 +671,12 @@ fn Wrap(comptime i: usize) type { 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; @@ -769,6 +793,17 @@ 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 { @@ -823,6 +858,16 @@ pub fn release(t: *Tape) void { 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; |
