diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-18 22:03:14 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-18 22:23:51 -0300 |
| commit | 99f5b10eccd95aa0066ea6fe6bb75f367ab56a47 (patch) | |
| tree | 813596ea84318a7bae4fd7416ba463de8e3b7107 /core/replica.zig | |
| parent | 92d553123673376adf6126ab90d49a22c5b833d2 (diff) | |
| download | pardes-99f5b10eccd95aa0066ea6fe6bb75f367ab56a47.tar.gz pardes-99f5b10eccd95aa0066ea6fe6bb75f367ab56a47.zip | |
core: src/ becomes core/, pardes.zig its root.zig (the reorganisation's tree, step 2d), and every mention of a src/ path follows: the build's paths, the core's imports of its root, the tests that read the repository's files by path (lspbench's, look.zig's spans over core/look.zig), the comments that cite files, the documents and the manual (the typst corpus reads core/tutor.txt and core/plumbing); left as they were: the /src tree's served names (src/pardes.zig and the rest are the 9P tree's, not the repository's), the CHANGELOG's history, fixtures that are only strings; fs-unit-test runs from the core's root (a module of the core rooted at fs.zig had the runtime's and the devices' `core` be fs.zig); nothing else changes: the ledger (pardes.test.* now root.test.*), frames byte-identical, replicas and process-test the base's
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/replica.zig')
| -rw-r--r-- | core/replica.zig | 4931 |
1 files changed, 4931 insertions, 0 deletions
diff --git a/core/replica.zig b/core/replica.zig new file mode 100644 index 00000000..b8d8f436 --- /dev/null +++ b/core/replica.zig @@ -0,0 +1,4931 @@ +//! A session's replicas (docs/next-daemon.md, phase 5 part A): the authority +//! orders every input and records what its core consumed (core/tape.zig); +//! each replica runs a core of its own, applies its own inputs at once, and +//! takes the authority's stream as the truth. +//! +//! A replica's state is the confirmed position plus its own pending inputs, +//! applied on top (a speculation, recorded as the authority would record +//! it). For each unit of the authority's stream, in order: +//! - its own pending head, recorded the same (inputs, host calls and +//! answers): the head goes, nothing is redone (the fast path); +//! - else the rollback copy is put back (core/rollback.zig), the confirmed +//! units since it are replayed, then this one, and the pending inputs +//! are applied again. +//! A speculation that reaches a host call or a syscall (a Host function, +//! a `tape.site`) or an emulator (the authority's) stops: the copy is put +//! back and the input waits for the authority's answer, one RTT late but +//! never wrong. Clock and environment reads (`tape.value`) are made here, +//! and the comparison catches a guess the authority did not share. +//! +//! No transport: messages are bytes in and out. A client's input is its +//! id, a sequence number and the event as wire.zig frames it; a +//! unit is the input's origin (0 for the authority's own: a pty's output, a +//! tick), its sequence number and the authority's tape of it. +const std = @import("std"); +const builtin = @import("builtin"); +const pardes = @import("root.zig"); +const tape = @import("tape.zig"); +const rollback = @import("rollback.zig"); +const walk = @import("walk.zig"); +const wire = @import("wire"); +const daemon = @import("runtime").daemon; +const writeAll = daemon.writeAll; +const c_heap = @import("c_heap"); +const Term = pardes.Term; +const Echo = @import("echo.zig").Echo; +const Allocator = std.mem.Allocator; +const Pardes = pardes.Pardes; + +const checked = builtin.mode == .debug or builtin.mode == .safe; +const head_len = 6; // origin or client: u16le, seq: u32le +/// A `stamp` frame (wire.zig): its header and the two clocks. +const stamp_len = wire.header_len + 16; +/// Confirmed units fast-pathed under one copy before it is taken afresh: +/// a rollback replays them all. +const rebase_after = 32; +/// A client compares its core's digest with the session's at most once in +/// this many confirmed units (a unit that carries one, its state the +/// confirmed one), and once quiet (`Link.quietCheck`): a full digest is a +/// walk of the whole core, an edit's worth of work. +const check_every = 32; + +fn header(buf: []u8, who: u16, seq: u32) void { + std.mem.writeInt(u16, buf[0..2], who, .little); + std.mem.writeInt(u32, buf[2..6], seq, .little); +} + +/// A unit's head: its origin and seq, then the authority's clocks when it +/// was made (realtime and monotonic, ns), which a replica's guesses of the +/// session's clock start from. +const unit_head = head_len + 16; + +pub const Unit = struct { origin: u16, seq: u32, real_ns: i64 = 0, mono_ns: i64 = 0, tape: []const u8 }; + +fn clockNs(id: std.c.clockid_t) i64 { + var ts: std.c.timespec = undefined; + if (std.c.clock_gettime(id, &ts) != 0) return 0; + return @as(i64, ts.sec) * std.time.ns_per_s + ts.nsec; +} + +fn unitHeader(buf: []u8, origin: u16, seq: u32) void { + header(buf, origin, seq); + std.mem.writeInt(i64, buf[6..14], clockNs(.REALTIME), .little); + std.mem.writeInt(i64, buf[14..22], clockNs(.MONOTONIC), .little); +} + +pub const Input = struct { client: u16, seq: u32, event: pardes.Event, stamp: ?[2]i64 = null }; + +/// A client's input message: its head, a `stamp` frame (when its client +/// guessed the session's clocks), and its event's frame, which the event +/// borrows. +pub fn parseInput(msg: []const u8) ?Input { + if (msg.len < head_len) return null; + var rest = msg[head_len..]; + var f = (wire.framed(rest) catch return null) orelse return null; + var stamp: ?[2]i64 = null; + if (f.tag == @backingInt(wire.ClientTag.stamp)) { + stamp = (wire.decodeClient(f.tag, f.payload) catch return null).stamp; + rest = rest[f.total..]; + f = (wire.framed(rest) catch return null) orelse return null; + } + const ev = switch (wire.decodeClient(f.tag, f.payload) catch return null) { + .event => |ev| ev, + else => return null, + }; + return .{ .client = std.mem.readInt(u16, msg[0..2], .little), .seq = std.mem.readInt(u32, msg[2..6], .little), .event = ev, .stamp = stamp }; +} + +/// An input's stamp as the authority takes it: the client's guess of its +/// clocks, so the guess its speculation recorded is the step's own; none +/// from a client a second or more off (a stall, a bad round trip), whose +/// step reads the clocks here. +fn stampOf(in: Input) ?tape.Guess { + const s = in.stamp orelse return null; + if (@abs(s[0] - clockNs(.REALTIME)) >= std.time.ns_per_s or @abs(s[1] - clockNs(.MONOTONIC)) >= std.time.ns_per_s) return null; + return .{ .real_ns = s[0], .mono_ns = s[1], .gmtoff = gmtoff() }; +} + +/// The core whose tape is `t`, stepped: tape.on is process-wide, and so +/// are syntax.zig's held trees, so one process can step several. +const Stepping = struct { + tape: ?*tape.Tape, + trees: ?*pardes.syntax.HeldTrees, + + fn begin(t: *tape.Tape, epoch: ?*rollback.Epoch) Stepping { + const was: Stepping = .{ .tape = tape.on, .trees = pardes.syntax.held_trees }; + tape.on = t; + rollback.Epoch.select(epoch); + return was; + } + + fn end(was: Stepping) void { + tape.on = was.tape; + pardes.syntax.held_trees = was.trees; + } +}; + +const nowNs = pardes.FrameStats.nowNs; + +/// With PARDES_REPLICA_TRACE, a line on stderr, each of `tapes` described +/// after it. +fn trace(gpa: Allocator, comptime fmt: []const u8, args: anytype, tapes: []const []const u8) void { + if (std.c.getenv("PARDES_REPLICA_TRACE") == null) return; + var w: std.Io.Writer.Allocating = .init(gpa); + defer w.deinit(); + w.writer.print(fmt, args) catch {}; + for (tapes) |t| tape.describe(t, &w.writer); + std.debug.print("{s}", .{w.written()}); +} + +/// The `p`th percentile of `samples` (sorted here); 100 is the worst. +fn pctOf(samples: []u64, p: usize) u64 { + if (samples.len == 0) return 0; + std.mem.sort(u64, samples, {}, std.sort.asc(u64)); + return samples[@min(samples.len - 1, samples.len * p / 100)]; +} + +// ---- the authority ---- + +pub const Authority = struct { + gpa: Allocator, + t: tape.Tape, + core: *Pardes, + host: pardes.Host, + out: std.ArrayListUnmanaged(u8) = .empty, + seq: u32 = 0, + /// Each client's view (core/views.zig): its own layout, focus and + /// cursors over the session's text. The first client has the session's. + view_of: [64]u8 = @splat(0), + /// Steps of its own taken since the last unit (`output`): one unit, + /// `pending`, sent before the next input it steps and at a frame. + batched: u32 = 0, + + /// A session booted with `opts`; `unit` is its first, which a replica + /// is made from. + /// `screens`: its terminals cross as screen state (`Screens`), not as + /// their pty's bytes. + pub fn init(gpa: Allocator, options: pardes.Options, host: pardes.Host, screens: bool) !*Authority { + // A replay gives its core every allocator (tape.zig), and a digest + // tells an allocator from none. + var opts = options; + opts.image_allocator = opts.image_allocator orelse gpa; + opts.tree_sitter_allocator = opts.tree_sitter_allocator orelse gpa; + opts.frame_allocator = opts.frame_allocator orelse gpa; + const a = try gpa.create(Authority); + errdefer gpa.destroy(a); + a.* = .{ .gpa = gpa, .t = tape.memory(gpa), .core = undefined, .host = host }; + errdefer tape.release(&a.t); + a.t.screen_state = screens; + // One word a digest: every pty read is a unit. + a.t.whole_digest = screens and std.c.getenv("PARDES_REPLICA_TRACE") == null; + // A digest at a unit's end, as the session's (Peers.unit). + a.t.no_digest = true; + const was = Stepping.begin(&a.t, null); + defer was.end(); + a.core = try Pardes.init(gpa, opts); + a.core.host = host; + a.core.pump(host) catch {}; + return a; + } + + pub fn deinit(a: *Authority) void { + const was = Stepping.begin(&a.t, null); + a.core.deinit(); + was.end(); + tape.release(&a.t); + a.out.deinit(a.gpa); + a.gpa.destroy(a); + } + + /// What was recorded since the last unit, as one (the boot's, a setup's + /// done straight on the core). + pub fn unit(a: *Authority, origin: u16, seq: u32) ![]const u8 { + const was = Stepping.begin(&a.t, null); + tape.recordDigest(&a.t); + was.end(); + a.out.clearRetainingCapacity(); + try a.out.appendNTimes(a.gpa, 0, unit_head); + unitHeader(a.out.items, origin, seq); + try a.out.appendSlice(a.gpa, a.t.buf.items); + a.t.buf.clearRetainingCapacity(); + return a.out.items; + } + + /// An input of the authority's own (a pty's output, a tick, a setup's + /// command), recorded into the unit being made. + pub fn apply(a: *Authority, ev: pardes.Event) void { + const was = Stepping.begin(&a.t, null); + a.core.update(ev); + a.core.pump(a.host) catch {}; + was.end(); + } + + /// One of the authority's own inputs, as its unit. + pub fn local(a: *Authority, ev: pardes.Event) ![]const u8 { + std.debug.assert(a.batched == 0); // `pending` first: units keep the order steps took + a.apply(ev); + a.seq += 1; + return a.unit(0, a.seq); + } + + /// An input of its own frontend's, in the view that frontend shows + /// (the first: tui.zig `Shell.handle` goes into `s.own`), as its unit. + pub fn own(a: *Authority, ev: pardes.Event) ![]const u8 { + std.debug.assert(a.batched == 0); + const was = Stepping.begin(&a.t, null); + a.core.toView(0); + was.end(); + return a.local(ev); + } + + /// A step of its own (a pty's output) into the open batch: under a + /// flood a replica gets a unit a frame, not one a read (each unit of the + /// authority's rolls back a replica's pending inputs), and its digest + /// once a unit. + pub fn output(a: *Authority, ev: pardes.Event) void { + a.apply(ev); + a.batched += 1; + } + + /// A pass of its host's loop with no input (a frame's wake): its pump + /// alone, its clock advanced and what is due ticked, into the open + /// batch. + pub fn pump(a: *Authority) void { + const was = Stepping.begin(&a.t, null); + a.core.pump(a.host) catch {}; + was.end(); + a.batched += 1; + } + + /// The open batch as a unit, or null: what the host sends before it + /// steps any other input, and once a frame. + pub fn pending(a: *Authority) !?[]const u8 { + if (a.batched == 0) return null; + a.batched = 0; + a.seq += 1; + return try a.unit(0, a.seq); + } + + /// A view of its own for `client`, as its unit; which it is. + pub fn openView(a: *Authority, client: u16) !struct { view: u8, unit: []const u8 } { + std.debug.assert(a.batched == 0); + const was = Stepping.begin(&a.t, null); + const k = a.core.openView() orelse { + was.end(); + return error.TooManyViews; + }; + a.core.pump(a.host) catch {}; + was.end(); + a.view_of[client] = k; + a.seq += 1; + return .{ .view = k, .unit = try a.unit(0, a.seq) }; + } + + /// A client joining as `Peers.join` takes one: its view kept for it + /// (`back`, after a divergence) or a new one, null when every view is + /// taken (refused); the unit that says so to the others (valid until the + /// next call), and the dump it loads, its blobs in place (`gpa`'s), with + /// the digest it checks its load by. + pub fn join(a: *Authority, client: u16, back: ?u8) !?struct { view: u8, unit: []const u8, dump: []u8, digest: u64 } { + std.debug.assert(a.batched == 0); + const was = Stepping.begin(&a.t, null); + defer was.end(); + const again = if (back) |v| a.core.rejoinView(v) else false; + const k = (if (again) back else a.core.openView()) orelse return null; + a.core.undrawn |= @as(u8, 1) << @intCast(k); + a.view_of[client] = k; + a.seq += 1; + const u = try a.unit(0, a.seq); + var blobs: std.ArrayListUnmanaged(walk.Deferred) = .empty; + defer blobs.deinit(a.core.gpa); + const state = try pardes.dump.dumpJoin(a.core, &blobs); + defer a.core.gpa.free(state); + tape.resync(&a.t); + var whole: std.ArrayListUnmanaged(u8) = .empty; + errdefer whole.deinit(a.gpa); + var pos: usize = 0; + for (blobs.items) |b| { + try whole.appendSlice(a.gpa, state[pos..b.at]); + try whole.appendSlice(a.gpa, b.bytes); + pos = b.at; + } + try whole.appendSlice(a.gpa, state[pos..]); + return .{ .view = k, .unit = u, .dump = try whole.toOwnedSlice(a.gpa), .digest = tape.digestOf(a.core) }; + } + + /// Client `client` gone as `Peers.leave` lets one go: its view kept for + /// it (`again`: it joins again for it) or closed; the unit. + pub fn leave(a: *Authority, client: u16, again: bool) ![]const u8 { + std.debug.assert(a.batched == 0); + const was = Stepping.begin(&a.t, null); + const k = a.view_of[client]; + a.core.undrawn &= ~(@as(u8, 1) << @intCast(k)); + if (again) a.core.awayView(k) else a.core.closeView(k); + was.end(); + a.seq += 1; + return a.unit(0, a.seq); + } + + /// Client `client`'s view gone (its link ended), as its unit. + pub fn closeView(a: *Authority, client: u16) ![]const u8 { + std.debug.assert(a.batched == 0); + const was = Stepping.begin(&a.t, null); + a.core.closeView(a.view_of[client]); + was.end(); + a.seq += 1; + return a.unit(0, a.seq); + } + + /// One client's input, applied in the order it came, in its view: the + /// unit every replica takes. + pub fn take(a: *Authority, msg: []const u8) ![]const u8 { + std.debug.assert(a.batched == 0); + const in = parseInput(msg) orelse return error.BadInput; + const was = Stepping.begin(&a.t, null); + a.core.toView(a.view_of[in.client]); + a.t.stamp = stampOf(in); + a.core.update(in.event); + a.core.step(); + a.t.stamp = null; + was.end(); + return a.unit(in.client, in.seq); + } +}; + +// ---- terminals as screen state (core/term/screen_state.zig) ---- +// +// A message of either side is u8 pane, u32 the pane's serial (a slot's +// next pane is another), u8 kind, and the kind's bytes: +// to a replica: kind 1, a delta; kind 2, rows (encodeRows): asked for, +// or a step read them; +// to the authority: kind 1, an acknowledgement (u32 seq); kind 2, a +// request for rows (u64 lo, u64 hi, absolute); kind 3, a +// delta refused: start this terminal afresh. +// In the authority's stream to a peer, before it steps any input but a +// pty's output, and once a frame: its own steps since the last unit, as one +// (`Authority.pending`), then what the peer is owed of the terminals +// (`Screens.flush`). After a step, before its unit: the rows it read that +// the peer holds as holes (`Screens.fill`, then `filled`). A replica takes +// them in that order between units (`Replica.screen`), so a step it replays +// reads the terminal its authority's step read. + +const kind_delta = 1; +const kind_rows = 2; +const kind_ack = 1; +const kind_reset = 3; + +const ScreenMsg = struct { pane: u8, serial: u32, kind: u8, payload: []const u8 }; + +fn parseScreen(msg: []const u8) ?ScreenMsg { + if (msg.len < 6) return null; + return .{ .pane = msg[0], .serial = std.mem.readInt(u32, msg[1..5], .little), .kind = msg[5], .payload = msg[6..] }; +} + +fn writeScreenHead(out: *std.ArrayListUnmanaged(u8), gpa: Allocator, pane: u8, serial: u32, kind: u8) !void { + var head: [6]u8 = undefined; + head[0] = pane; + std.mem.writeInt(u32, head[1..5], serial, .little); + head[5] = kind; + try out.appendSlice(gpa, &head); +} + +/// The authority's side of its replicas' terminals: a track each terminal +/// pane, and what each peer was sent of it. +pub const Screens = struct { + gpa: Allocator, + /// Made the first time a terminal is encoded (`encoder`). + enc: ?*Term.Encoder = null, + tracks: [pardes.MAX_PANES]?Tracked = @splat(null), + peers: std.ArrayListUnmanaged(*[pardes.MAX_PANES]PeerOf) = .empty, + free_peers: std.ArrayListUnmanaged(u16) = .empty, + out: std.ArrayListUnmanaged(u8) = .empty, + /// Nanoseconds: what each flush spent encoding, for every peer. + encode_ns: u64 = 0, + + const Tracked = struct { serial: u32, state: *Term.State, track: *Term.Track }; + const PeerOf = struct { serial: u32 = 0, peer: Term.Peer = .{} }; + + pub fn init(gpa: Allocator) Screens { + return .{ .gpa = gpa }; + } + + /// `core` is the session's, its terminals still there. + pub fn deinit(s: *Screens, core: *Pardes) void { + for (&s.tracks, 0..) |*slot, id| if (slot.*) |*tr| Term.untrack(tr.track, s.gpa, if (s.live(core, id, tr)) tr.state else null); + for (s.peers.items) |p| { + for (p) |*pp| pp.peer.deinit(s.gpa); + s.gpa.destroy(p); + } + s.peers.deinit(s.gpa); + s.free_peers.deinit(s.gpa); + s.out.deinit(s.gpa); + if (s.enc) |e| Term.encoderFree(e); + } + + fn live(_: *const Screens, core: *Pardes, id: usize, tr: *const Tracked) bool { + const pane = core.panes[id] orelse return false; + return pane.serial == tr.serial and pane.terminal == tr.state; + } + + /// A new peer, with nothing yet: its first delta of each terminal + /// starts it afresh. + pub fn addPeer(s: *Screens) !u16 { + if (s.free_peers.pop()) |again| return again; + const p = try s.gpa.create([pardes.MAX_PANES]PeerOf); + p.* = @splat(.{}); + try s.peers.append(s.gpa, p); + return @intCast(s.peers.items.len - 1); + } + + /// Peer `peer` gone: what it was sent forgotten, its number free for + /// the next `addPeer` (a socket's clients come and go). + pub fn dropPeer(s: *Screens, peer: u16) void { + for (s.peers.items[peer]) |*pp| pp.peer.deinit(s.gpa); + s.peers.items[peer].* = @splat(.{}); + s.free_peers.append(s.gpa, peer) catch {}; + } + + /// What `peer` is owed of every terminal, each a message onto `out` + /// (owned): nothing for one it has as it is, or with `bound` in flight. + pub fn flush(s: *Screens, core: *Pardes, peer: u16, _: u8, out: *std.ArrayListUnmanaged([]u8)) !void { + const t0 = nowNs(); + defer s.encode_ns += nowNs() - t0; + for (core.panes, 0..) |slot, id| { + const pane = slot orelse continue; + const state = pane.terminal orelse continue; + const tr = try s.track(core, id); + const pp = &s.peers.items[peer][id]; + if (pp.serial != pane.serial) { + pp.peer.reset(); + pp.serial = pane.serial; + } + s.out.clearRetainingCapacity(); + try writeScreenHead(&s.out, s.gpa, @intCast(id), pane.serial, kind_delta); + var w: std.Io.Writer.Allocating = .fromArrayList(s.gpa, &s.out); + defer s.out = w.toArrayList(); + if (!try Term.encode(state, try s.encoder(), tr.track, &pp.peer, &w.writer)) continue; + try out.append(s.gpa, try s.gpa.dupe(u8, w.written())); + } + } + + /// Rows a peer holds as holes of every terminal a step read (its rows, + /// history and all: text mode over a shell), each a rows message onto + /// `out` (owned): sent after the step and before its unit, so the + /// replica's replay reads what the authority's step read. A history row + /// never changes, so each goes once. Then `filled`. + // ponytail: a step that reads a terminal's rows reads all of them (its + // motion surface), so the first after a flood sends its whole history. + pub fn fill(s: *Screens, core: *Pardes, peer: u16, out: *std.ArrayListUnmanaged([]u8)) !void { + for (core.panes, 0..) |slot, id| { + const pane = slot orelse continue; + const state = pane.terminal orelse continue; + if (!Term.wasRead(state)) continue; + const pp = &s.peers.items[peer][id]; + if (pp.serial != pane.serial or pp.peer.holes.items.len == 0) continue; + const tr = try s.track(core, id); + for (pp.peer.holes.items) |hole| { + var lo = hole[0]; + while (lo < hole[1]) { + const hi = @min(hole[1], lo + 512); + s.out.clearRetainingCapacity(); + try writeScreenHead(&s.out, s.gpa, @intCast(id), pane.serial, kind_rows); + var w: std.Io.Writer.Allocating = .fromArrayList(s.gpa, &s.out); + defer s.out = w.toArrayList(); + try Term.encodeRows(state, try s.encoder(), tr.track, lo, hi, &w.writer); + try out.append(s.gpa, try s.gpa.dupe(u8, w.written())); + lo = hi; + } + } + pp.peer.holes.clearRetainingCapacity(); + } + } + + /// Every peer is filled: the steps' reads are forgotten. + pub fn filled(_: *Screens, core: *Pardes) void { + for (core.panes) |slot| if (slot) |pane| if (pane.terminal) |state| { + Term.clearRead(state); + }; + } + + fn track(s: *Screens, core: *Pardes, id: usize) !*Tracked { + const pane = core.panes[id].?; + if (s.tracks[id]) |*tr| { + if (s.live(core, id, tr)) return tr; + // Its pane closed (its emulator, the pin with it, gone). + Term.untrack(tr.track, s.gpa, null); + s.tracks[id] = null; + } + s.tracks[id] = .{ .serial = pane.serial, .state = pane.terminal.?, .track = try Term.track(s.gpa) }; + return &s.tracks[id].?; + } + + /// The encoder, made the first time a terminal is encoded. + fn encoder(s: *Screens) !*Term.Encoder { + if (s.enc) |e| return e; + s.enc = try Term.encoder(s.gpa); + return s.enc.?; + } + + /// A peer's message (`Replica.screen`'s, `Replica.screenRequest`'s): + /// an answer for it (owned), or null. + pub fn take(s: *Screens, core: *Pardes, peer: u16, msg: []const u8) !?[]u8 { + const m = parseScreen(msg) orelse return null; + const pane = core.panes[m.pane] orelse return null; + if (pane.serial != m.serial) return null; + const state = pane.terminal orelse return null; + const pp = &s.peers.items[peer][m.pane]; + if (pp.serial != m.serial) return null; + switch (m.kind) { + kind_ack => if (m.payload.len == 4) pp.peer.ack(std.mem.readInt(u32, m.payload[0..4], .little)), + kind_reset => pp.peer.reset(), + kind_rows => { + if (m.payload.len != 16) return null; + const tr = try s.track(core, m.pane); + s.out.clearRetainingCapacity(); + try writeScreenHead(&s.out, s.gpa, m.pane, m.serial, kind_rows); + var w: std.Io.Writer.Allocating = .fromArrayList(s.gpa, &s.out); + defer s.out = w.toArrayList(); + try Term.encodeRows(state, try s.encoder(), tr.track, std.mem.readInt(u64, m.payload[0..8], .little), std.mem.readInt(u64, m.payload[8..16], .little), &w.writer); + return try s.gpa.dupe(u8, w.written()); + }, + else => {}, + } + return null; + } +}; + +// ---- a replica ---- + +pub const Stats = struct { + inputs: u32 = 0, + /// Applied at once and clean. + speculated: u32 = 0, + /// Stopped at a host call, a syscall or an emulator: waits one RTT. + stopped: u32 = 0, + /// Not tried: an event of the view's (a resize, the pointer), phase 4's. + unspeculated: u32 = 0, + /// The authority's unit was the pending head, recorded the same. + fast: u32 = 0, + /// The copy put back: the head recorded otherwise (a guess of the clock + /// the authority did not share), or another's unit first. + mispredicted: u32 = 0, + foreign: u32 = 0, + rebases: u32 = 0, + units: u32 = 0, + /// Its digest compared with the session's (a unit's, or quiet). + checks: u32 = 0, + /// The samples below kept: an in-process harness's replica, or a + /// client's with PARDES_REPLICA_STATS. Without, nothing is: a client + /// runs for days, and they grew a word an input and a delta. + samples: bool = false, + /// Nanoseconds: one speculative apply, a copy taken, a rollback (the + /// copy put back, the confirmed units replayed, the pending reapplied). + spec_ns: std.ArrayListUnmanaged(u64) = .empty, + copy_ns: std.ArrayListUnmanaged(u64) = .empty, + rollback_ns: std.ArrayListUnmanaged(u64) = .empty, + copy_bytes: u64 = 0, + copies: u32 = 0, + /// Nanoseconds: a terminal's delta applied. + apply_ns: std.ArrayListUnmanaged(u64) = .empty, + /// What stopped them (`Tape.poisoned_by`), a count a name. + stops: [16]Stop = @splat(.{}), + + const Stop = struct { name: []const u8 = "", n: u32 = 0 }; + + fn stop(s: *Stats, name: []const u8) void { + for (&s.stops) |*st| if (st.n == 0 or std.mem.eql(u8, st.name, name)) { + st.name = name; + st.n += 1; + return; + }; + } + + /// `name:n,...` (spaces as dashes), or `-`. + fn writeStops(s: *const Stats, w: *std.Io.Writer) !void { + var any = false; + for (s.stops) |st| if (st.n > 0) { + if (any) try w.writeByte(','); + any = true; + for (st.name) |ch| try w.writeByte(if (ch == ' ') '-' else ch); + try w.print(":{d}", .{st.n}); + }; + if (!any) try w.writeByte('-'); + } + + pub fn deinit(s: *Stats, gpa: Allocator) void { + s.spec_ns.deinit(gpa); + s.copy_ns.deinit(gpa); + s.rollback_ns.deinit(gpa); + s.apply_ns.deinit(gpa); + } +}; + +/// `terminal`: its speculation read a terminal or moved one's view (or a +/// terminal had the keyboard), so what it did depends on the emulator. +const Pending = struct { seq: u32, msg: []u8, spec: ?[]u8 = null, sent_ns: u64 = 0, terminal: bool = false, guess: ?tape.Guess = null }; + +pub fn parseUnit(msg: []const u8) ?Unit { + if (msg.len < unit_head) return null; + return .{ + .origin = std.mem.readInt(u16, msg[0..2], .little), + .seq = std.mem.readInt(u32, msg[2..6], .little), + .real_ns = std.mem.readInt(i64, msg[6..14], .little), + .mono_ns = std.mem.readInt(i64, msg[14..22], .little), + .tape = msg[unit_head..], + }; +} + +pub const Replica = struct { + gpa: Allocator, + id: u16, + /// Its own view of the session (Authority.openView's). + view: u8 = 0, + /// The core's allocator; its tapes and the engine's own lists are not. + epoch: rollback.Epoch = .{}, + t: tape.Tape, + copy: rollback.Copy = .{}, + pending: std.ArrayListUnmanaged(Pending) = .empty, + /// pending[0..applied] are applied on top of the confirmed position. + applied: usize = 0, + /// The pending input that waits for the authority (it stopped at a host + /// call, or is the view's), and those after it with it. + blocked: ?u32 = null, + /// Confirmed units since the copy was taken, replayed over it when it + /// is put back: a unit of another's that came while the client was + /// idle (`ready`), one of its own fast-pathed, one that moved nothing + /// (`neutral`). Each read the terminals as they are now: a delta lets + /// them go first (`screen`). + since: std.ArrayListUnmanaged([]u8) = .empty, + /// Pending inputs to apply again (after a rollback). + dirty: bool = false, + next_seq: u32 = 1, + stats: Stats = .{}, + diverged: ?[]const u8 = null, + out: std.ArrayListUnmanaged(u8) = .empty, + /// Nanoseconds of the rollback being made, until its reapply. + recovering: ?u64 = null, + /// The digest the last confirmed unit ended on (its last record's + /// bytes): a unit of another's that ends on it again (the session's + /// pump that took this replica's input in, and moved nothing) leaves + /// the speculation on top as it was. + last_digest: std.ArrayListUnmanaged(u8) = .empty, + /// Units of another's taken so, no rollback made. + neutral: u32 = 0, + /// Confirmed units since its digest was last compared (`check_every`). + since_check: u32 = 0, + /// What it shows changed since the host last drew it: a unit replayed + /// on it, a rollback (a unit that confirmed what it applied, or moved + /// nothing, changes nothing to draw). + moved: bool = false, + /// A client of a session elsewhere (`initDump`): the session's clock is + /// guessed, never read here -- its last unit's reading, plus the time + /// since it came, plus a round trip (the input reaches the session that + /// much later). In process, both read one clock. + guess: bool = false, + stamp: struct { real_ns: i64 = 0, mono_ns: i64 = 0, at_ns: u64 = 0 } = .{}, + rtt_ns: u64 = 0, + gmtoff: i32 = 0, + + /// Its terminals' screen state, as the authority's deltas made it. + screens: [pardes.MAX_PANES]?ScreenOf = @splat(null), + /// Each terminal's view (`Term.grid`'s view) as the copy was taken, or + /// as the last delta since set it: the copy holds no emulator, and a + /// speculation may move a view, so a rollback puts these back. + views: [pardes.MAX_PANES]?usize = @splat(null), + /// Its panes' serials as the copy was taken, and whether one was being + /// torn down then (`panesMoved`). + serials: [pardes.MAX_PANES]u32 = @splat(0), + retiring: bool = false, + screen_out: std.ArrayListUnmanaged(u8) = .empty, + /// A record's bytes by hash coming in pieces (`blobPiece`), and how + /// many of them came. + incoming: ?tape.Big = null, + incoming_got: usize = 0, + /// A far client's blobs on disk (its Have's), where a file it is sent + /// goes for its next join. + cache: ?Cache = null, + io: std.Io = undefined, + /// Its predicted echo (core/term/echo.zig), drawn over its core's terminals: + /// fed by its keys, its units and its deltas here, and by `echoFrame`. + echo: Echo = .{}, + + const ScreenOf = struct { + serial: u32, + rep: *Term.Replica, + /// A delta was refused, and a reset asked for: those in flight + /// before it are refused unasked. + refused: bool = false, + /// The rows asked for and not yet answered. + asked: ?[2]u64 = null, + }; + + /// A replica of the session whose first unit is `first`; `screens` + /// as the authority's (its terminals fed by `screen`). In process, its + /// timing samples kept for the harness that reads them. + pub fn init(gpa: Allocator, id: u16, first: []const u8, screens: bool) !*Replica { + const r = try gpa.create(Replica); + r.* = .{ .gpa = gpa, .id = id, .t = tape.memory(gpa), .stats = .{ .samples = true } }; + r.t.screen_state = screens; + r.t.core_gpa = r.epoch.allocator(); + r.t.checks = false; + r.confirm(first); + if (r.t.core == null) { + r.deinit(); + return error.NoCore; + } + if (screens) r.core().echo = &r.echo; + return r; + } + + /// A replica of a session elsewhere, as its walker dump (core/dump.zig) + /// is: what a client of a socket joins with. `opts` are this process's + /// (a replica's own allocators and names); its core's allocator is the + /// rollback epoch's, but for images and PDFs, whose workers allocate on + /// threads of their own. ponytail: a picture's pixels freed while a copy + /// is held are not put off; a rollback over one shows an image a frame + /// late at worst... or reads freed pixels, which no test session has. + pub fn initDump(gpa: Allocator, id: u16, view: u8, bytes: []const u8, opts: pardes.Options) !*Replica { + const r = try gpa.create(Replica); + r.* = .{ .gpa = gpa, .id = id, .view = view, .t = tape.memory(gpa), .guess = true }; + r.t.skips_render = true; + r.t.checks = false; + // Its terminals are the session's deltas (Peers sends them). + r.t.screen_state = true; + errdefer { + tape.release(&r.t); + r.epoch.deinit(); + gpa.destroy(r); + } + const epoch = r.epoch.allocator(); + r.t.core_gpa = epoch; + var o = opts; + o.frame_allocator = epoch; + o.tree_sitter_allocator = epoch; + o.image_allocator = o.image_allocator orelse std.heap.c_allocator; + // The session's closed panes and Recent are the authority's. + o.state = .off; + // Its emulators off the epoch, as `terminal.create` makes a screen + // replica's: deltas write them while a copy is held. + pardes.dump.terminal_gpa = pardes.panes.emulator_gpa; + defer pardes.dump.terminal_gpa = null; + const c = try pardes.dump.load(epoch, o, bytes, null); + // The chrome's colours are local, never dumped: the restored theme's. + pardes.colors.finishThemeInitialization(c); + // It runs a plain kind's code of its own (Addon.plainOps), loaded + // at its first pane, each opened here from its record; another's + // panes show a line. + c.kinds.ops = @splat(null); + for (c.panes, 0..) |pslot, at| if (pslot) |pane| if (pane.addon) |s| if (c.kinds.byName(s.nameOf())) |k| { + if (c.kinds.ops[k] == null) c.kinds.ops[k] = pardes.Addon.plainOps(&c.kinds.kinds[k]); + // Refused (its file not here): the line saying it does not run here. + if (c.kinds.ops[k]) |ops| pane.addon = pardes.dump.plainHere(c, at, s, ops) catch s; + }; + // What loading queued (a watch, a theme asked for) is the + // authority's errand, done there already: the session was dumped + // between steps, with nothing queued. + c.effects_len = 0; + c.effects_head = 0; + c.in_len = 0; + c.in_head = 0; + tape.adoptCore(&r.t, c); + // Its keys into a terminal drawn at once (core/term/echo.zig). + c.echo = &r.echo; + return r; + } + + pub fn deinit(r: *Replica) void { + for (&r.screens) |*slot| if (slot.*) |*sc| Term.replicaFree(sc.rep, pardes.panes.emulator_gpa); + if (r.t.core) |c| { + const was = Stepping.begin(&r.t, &r.epoch); + // The state it ends on is whole as it is: the copy let go, its + // put-off frees done. Put back, it would bring back what was + // freed between steps meanwhile (the shell's rows a draw read + // again after a delta), which `toConfirmed` drops and this + // freed again (a held copy across an input of its own, a + // rejoin's old replica). + if (r.copy.held) r.copy.release(); + c.deinit(); + was.end(); + } + if (r.incoming) |b| r.gpa.free(b.bytes); + r.copy.deinit(); + for (r.pending.items) |p| r.freePending(p); + r.pending.deinit(r.gpa); + for (r.since.items) |u| r.gpa.free(u); + r.since.deinit(r.gpa); + r.out.deinit(r.gpa); + r.screen_out.deinit(r.gpa); + r.last_digest.deinit(r.gpa); + r.stats.deinit(r.gpa); + tape.release(&r.t); + if (r.diverged) |why| r.gpa.free(why); + r.epoch.deinit(); + r.gpa.destroy(r); + } + + pub fn core(r: *const Replica) *Pardes { + return r.t.core.?; + } + + fn freePending(r: *Replica, p: Pending) void { + r.gpa.free(p.msg); + if (p.spec) |s| r.gpa.free(s); + } + + /// The replica's own input: applied at once when it can be, and the + /// message for the authority returned (valid until the next call). + pub fn input(r: *Replica, ev: pardes.Event) ![]const u8 { + r.stats.inputs += 1; + const seq = r.next_seq; + r.next_seq += 1; + // Its time, as the session's clocks will read when it steps it (a + // round trip on from the last unit's, the clocks here in process): + // the step's clock reads, here and there (`stampOf`). + const g: tape.Guess = if (r.guess) guess: { + const since: i64 = @intCast(nowNs() -| r.stamp.at_ns + r.rtt_ns); + break :guess .{ .real_ns = r.stamp.real_ns + since, .mono_ns = r.stamp.mono_ns + since, .gmtoff = r.gmtoff }; + } else .{ .real_ns = clockNs(.REALTIME), .mono_ns = clockNs(.MONOTONIC), .gmtoff = gmtoff() }; + const room = try r.gpa.alloc(u8, head_len + stamp_len + wire.clientBound(.{ .event = ev })); + defer r.gpa.free(room); + _ = try wire.encodeClient(room[head_len..][0..stamp_len], .{ .stamp = .{ g.real_ns, g.mono_ns } }); + const n = (try wire.encodeClient(room[head_len + stamp_len ..], .{ .event = ev })).len; + header(room, r.id, seq); + const msg = try r.gpa.dupe(u8, room[0 .. head_len + stamp_len + n]); + try r.pending.append(r.gpa, .{ .seq = seq, .msg = msg, .sent_ns = nowNs(), .guess = g }); + r.dirty = true; + r.settle(); + if (r.t.screen_state and r.diverged == null) r.echo.typed(r.core(), r.view, ev, seq, r.applied == r.pending.items.len); + r.out.clearRetainingCapacity(); + try r.out.appendSlice(r.gpa, msg); + return r.out.items; + } + + /// One unit of the authority's stream, in its order. + pub fn confirm(r: *Replica, msg: []const u8) void { + if (r.diverged != null) return; + const u = parseUnit(msg) orelse return r.fail("a unit too short to read"); + r.stats.units += 1; + r.since_check +|= 1; + if (u.origin == r.id) if (r.t.core) |c| r.echo.confirmed(c, u.seq); + // A copy taken ahead (`ready`), nothing on top: it stays the point a + // rollback goes back to, the unit replayed on it and kept in `since`, + // not let go and taken again (a megabyte walked) for each unit of + // the session's that comes while the client is idle. + const ahead = r.applied == 0 and r.copy.held; + if (r.guess) { + r.stamp = .{ .real_ns = u.real_ns, .mono_ns = u.mono_ns, .at_ns = nowNs() }; + if (u.origin == r.id) for (r.pending.items) |p| if (p.seq == u.seq) { + // Sent to confirmed: what reaching the session takes, and back. + if (p.sent_ns != 0) r.rtt_ns = nowNs() -| p.sent_ns; + break; + }; + } + const was = Stepping.begin(&r.t, &r.epoch); + defer was.end(); + if (u.origin == r.id and r.pending.items.len > 0 and r.pending.items[0].seq == u.seq and r.applied > 0 and + tape.sameRecords(r.pending.items[0].spec.?, u.tape)) + { + r.stats.fast += 1; + // The state it ends on, which a unit of another's that moves + // nothing ends on again (`neutral`); none, not known. + r.last_digest.clearRetainingCapacity(); + if (lastDigest(u.tape)) |d| r.last_digest.appendSlice(r.gpa, d) catch {}; + if (r.pending.items[0].terminal and r.applied > 1) { + // Right, but what it did read a terminal: replayed later (a + // rollback's `since`) it would read the deltas that came + // after it. So it is taken now, over the emulator its step + // read, and the ones on top applied again over it. + r.toConfirmed(); + if (tape.replayUnit(&r.t, u.tape)) |why| return r.fail(why); + r.freePending(r.pending.orderedRemove(0)); + r.dirty = true; + return; + } + const read_terminal = r.pending.items[0].terminal; + r.freePending(r.pending.orderedRemove(0)); + r.applied -= 1; + if (r.applied == 0) { + // Nothing speculative on top any more: the state is the + // confirmed one, which its digest says, when it carries one + // and a check is due. A step of its own that read what the + // session's did not (a frame's, a derived field's) shows at + // the next check, or quiet (`Link.quietCheck`). + if (r.checkDue()) if (lastDigest(u.tape)) |d| { + r.t.checks = true; + r.t.check(d); + r.t.checks = false; + r.since_check = 0; + r.stats.checks += 1; + if (r.t.diverged) |why| { + trace(r.gpa, "replica {d}: its input {d} came to another state than the session's: {s}\n", .{ r.id, u.seq, why }, &.{u.tape}); + return r.fail(why); + } + }; + // The copy stays the point a rollback goes back to, this + // input kept to replay over it as a unit of the session's + // that comes while idle is: a step writes some bytes of the + // megabyte the copy is, walked once in `rebase_after` units, + // not once a key. Not over one that read a terminal (replayed + // later it would read the deltas since), nor once a pane + // opened, closed or was torn down since the copy (what it + // holds outside the copy was made or freed: `ahead`'s rule). + if (!read_terminal and !r.panesMoved()) return r.keepSince(u.tape); + r.copy.release(); + rollback.Epoch.select(null); + r.clearSince(); + return; + } + return r.keepSince(u.tape); + } + const ends = lastDigest(u.tape); + // None: where it ended is not known, and the next unit is not + // taken as moving nothing from it. + defer { + r.last_digest.clearRetainingCapacity(); + if (ends) |d| r.last_digest.appendSlice(r.gpa, d) catch {}; + } + if (r.applied > 0 and u.origin != r.id and ends != null and std.mem.eql(u8, ends.?, r.last_digest.items)) { + // Nothing a digest covers moved: what was applied on top is + // what the session will apply on top of it. Replayed when the + // copy is next put back. + r.neutral += 1; + return r.keepSince(u.tape); + } + if (r.applied > 0) { + if (u.origin == r.id) r.stats.mispredicted += 1 else r.stats.foreign += 1; + if (u.origin == r.id and std.c.getenv("PARDES_REPLICA_TRACE") != null) { + trace(r.gpa, "replica {d}: its input {d} was recorded otherwise; it guessed:\n", .{ r.id, u.seq }, &.{r.pending.items[0].spec.?}); + trace(r.gpa, "and the authority recorded:\n", .{}, &.{u.tape}); + } + r.recovering = nowNs(); + r.toConfirmed(); + if (r.diverged != null) return; + } + // What it shows moved only when what a digest covers did: a unit of + // the session's pump that read its clock and moved nothing draws + // no frame. + if (ends == null or !std.mem.eql(u8, ends.?, r.last_digest.items)) r.moved = true; + // What the unit opens or closes outside the copy (`keepSince` below). + var serials: [pardes.MAX_PANES]u32 = undefined; + var retiring = false; + if (ahead) { + const c = r.core(); + for (c.panes, &serials) |slot, *n| n.* = if (slot) |pane| pane.serial else 0; + for (c.retired_panes.current, c.retired_panes.previous) |x, y| retiring = retiring or x != null or y != null; + } + // Its digest compared when one is due (the units kept since and + // replayed by a rollback were, if at all, as they came). + const check = ends != null and r.checkDue(); + r.t.checks = check; + const replayed = tape.replayUnit(&r.t, u.tape); + r.t.checks = false; + if (replayed) |why| { + trace(r.gpa, "replica {d}: the unit of {d}/{d} diverged: {s}\n", .{ r.id, u.origin, u.seq, why }, &.{u.tape}); + return r.fail(why); + } + if (check) { + r.since_check = 0; + r.stats.checks += 1; + } + if (u.origin == r.id) { + if (r.pending.items.len == 0 or r.pending.items[0].seq != u.seq) return r.fail("the authority confirmed an input this replica did not send next"); + if (r.blocked == u.seq) r.blocked = null; + r.freePending(r.pending.orderedRemove(0)); + } + r.dirty = true; + if (!ahead) return; + // A pane opened, closed, or torn down (two syncs after it closed, + // `reapPanes`): what it holds outside the copy (a terminal's + // emulator, a picture's pixels, the store's record of it) was made + // or freed here, and is not put back with the copy: a rollback + // replaying the unit again would close a terminal freed already (the + // soak, a `Del` of the shell). Let go with the copy. + const c = r.core(); + for (c.retired_panes.current, c.retired_panes.previous) |x, y| retiring = retiring or x != null or y != null; + for (c.panes, serials) |slot, n| retiring = retiring or (if (slot) |pane| pane.serial else 0) != n; + if (retiring) { + r.copy.release(); + rollback.Epoch.select(null); + return r.clearSince(); + } + r.keepSince(u.tape); + } + + /// Its digest is due a comparison (`check_every`; every unit traced). + fn checkDue(r: *const Replica) bool { + return r.since_check >= check_every or std.c.getenv("PARDES_REPLICA_TRACE") != null; + } + + fn fail(r: *Replica, why: []const u8) void { + if (r.diverged == null) r.diverged = r.gpa.dupe(u8, why) catch return; + } + + /// A unit kept to replay when the copy is next put back; past + /// `rebase_after` of them, that is now (`toConfirmed`). + fn keepSince(r: *Replica, unit: []const u8) void { + const kept = r.gpa.dupe(u8, unit) catch return r.fail("out of memory"); + r.since.append(r.gpa, kept) catch return r.fail("out of memory"); + if (r.since.items.len >= rebase_after) { + r.stats.rebases += 1; + // Nothing on top: it is the confirmed state already. + if (r.applied == 0) { + r.copy.release(); + rollback.Epoch.select(null); + return r.clearSince(); + } + r.toConfirmed(); + r.dirty = true; + } + } + + fn clearSince(r: *Replica) void { + for (r.since.items) |u| r.gpa.free(u); + r.since.clearRetainingCapacity(); + } + + /// Back to the confirmed position: the copy put back, the units fast- + /// pathed since replayed. + fn toConfirmed(r: *Replica) void { + const held = r.copy.held; + if (held) { + const whole = r.copy.restore(); + rollback.Epoch.select(null); + if (!whole) return r.fail("out of memory for a rollback"); + // The frame its host drew is of the memory put back: drawn again. + r.moved = true; + // The rows a terminal was read as are no copy's: what the + // speculation built was the epoch's, freed with it, and what + // was there before is of a grid the deltas since moved on + // from (a rollback never brings a terminal's past back). + r.core().shell_rows = .{}; + // A tab's width is the process's (config.tab_now), which a + // step that set it (Tab) wrote and the copy does not hold. + pardes.config.tab_now = r.core().settings.tab; + } + r.applied = 0; + // Its views as the copy was taken, or as the last delta set them + // (which the units since may move again). + if (held) r.putViews(); + for (r.since.items) |u| if (tape.replayUnit(&r.t, u)) |why| { + trace(r.gpa, "replica {d}: a unit kept since the copy came to another state replayed again: {s}\n", .{ r.id, why }, &.{u}); + return r.fail(why); + }; + r.clearSince(); + for (r.pending.items) |*p| if (p.spec) |s| { + r.gpa.free(s); + p.spec = null; + }; + } + + /// The pending inputs applied again, as far as they go. + pub fn settle(r: *Replica) void { + if (!r.dirty or r.diverged != null) return; + r.dirty = false; + const was = Stepping.begin(&r.t, &r.epoch); + defer was.end(); + while (r.applied < r.pending.items.len and r.blocked != r.pending.items[r.applied].seq) if (!r.speculate()) break; + if (r.recovering) |t0| { + if (r.stats.samples) r.stats.rollback_ns.append(r.gpa, nowNs() - t0) catch {}; + r.recovering = null; + } + } + + /// pending[applied] applied on top: whether it was. + fn speculate(r: *Replica) bool { + const p = &r.pending.items[r.applied]; + // Every kind, a view's own (a resize, the pointer) too: each client + // has its own view, and what reaches the authority's (a pty's size, + // the pointer over a sliding pane) stops it as any host call does. + const in = parseInput(p.msg) orelse return false; + const c = r.core(); + if (!r.copy.held) { + const t0 = nowNs(); + // A grid's worth of text the copy would save, and put back stale. + c.shell_rows.reset(c.gpa); + r.copy.take(&r.epoch, c) catch return false; + r.keepViews(); + rollback.Epoch.select(&r.epoch); + if (r.stats.samples) r.stats.copy_ns.append(r.gpa, nowNs() - t0) catch {}; + r.stats.copy_bytes += r.copy.savedBytes(); + r.stats.copies += 1; + } + var terminals: [pardes.MAX_PANES]u64 = undefined; + if (checked) terminalDigests(c, &terminals); + const t0 = nowNs(); + tape.beginSpeculation(&r.t); + // The time it was sent with, every time it is applied. + r.t.guess = p.guess; + c.toView(r.view); + // Input into a terminal: what reaches its program (a key in tty + // mode, a paste, a mouse report) is a pty write, the authority's, + // and stops here (phase 6 predicts its echo). The rest (pardes's + // own keys over it, the wheel) reads its emulator, here the last + // delta's and there what the pty has said since: it is applied, + // and applied again over each delta while it waits (`screen`), so + // what the authority's unit confirms read the emulator its step + // read (the deltas come before it). + const keyboard = if (c.panes[c.view().active]) |pane| pane.terminal != null else false; + c.update(in.event); + c.step(); + p.terminal = r.t.screen_state and (keyboard or r.t.touched); + const clean = tape.endSpeculation(&r.t); + const dt = nowNs() - t0; + // Where a blob was written, should one be: the input that did. + if (checked and std.c.getenv("PARDES_REPLICA_TRACE") != null) r.copy.check(); + if (!clean) { + // Undone, and the earlier ones applied again on the confirmed + // position: this one waits for the authority. + r.stats.stopped += 1; + r.stats.stop(r.t.poisoned_by); + if (std.c.getenv("PARDES_REPLICA_TRACE") != null) std.debug.print("replica {d}: its input {d} ({t}) stopped at a host call ({s}): it waits\n", .{ r.id, p.seq, in.event, r.t.poisoned_by }); + r.blocked = p.seq; + const upto = r.applied; + r.toConfirmed(); + while (r.applied < upto) if (!r.speculate()) break; + if (r.applied == 0 and r.copy.held) { + r.copy.release(); + rollback.Epoch.select(null); + } + return false; + } + if (checked) { + var after: [pardes.MAX_PANES]u64 = undefined; + terminalDigests(c, &after); + if (!std.mem.eql(u64, &terminals, &after)) { + for (terminals, after, 0..) |x, y, id| if (x != y) std.debug.print("replica {d}: its input {d} ({any}) wrote terminal {d}\n", .{ r.id, p.seq, in.event, id }); + @panic("replica: a speculation wrote a terminal's emulator"); + } + } + r.stats.speculated += 1; + if (r.stats.samples) r.stats.spec_ns.append(r.gpa, dt) catch {}; + p.spec = r.gpa.dupe(u8, r.t.buf.items) catch return false; + r.t.buf.clearRetainingCapacity(); + r.applied += 1; + return true; + } + + /// Its client's, once a frame before it draws: a predicted echo the + /// screen has not shown in time goes, and keys deferred behind one not + /// predicted are predicted once its doing is on the screen. Whether what + /// the overlay draws changed since the last call (by a key, a unit, a + /// delta or this: `echo.changed`). + pub fn echoFrame(r: *Replica) bool { + return r.echo.frame(r.core()); + } + + /// Nothing pending and nothing applied: the replica is the authority's + /// confirmed state, which `digest` says (a copy taken ahead, `ready`, + /// has nothing on top of it). + pub fn quiet(r: *const Replica) bool { + return r.pending.items.len == 0 and r.applied == 0; + } + + /// The copy taken now, while nothing is pending (the host is idle, its + /// frame drawn), so that the next input of its own is applied at once + /// without taking it: a copy is a megabyte saved. Let go when a unit + /// comes first. + pub fn ready(r: *Replica) void { + if (r.copy.held or r.pending.items.len > 0 or r.diverged != null) return; + const c = r.core(); + const t0 = nowNs(); + rollback.Epoch.select(&r.epoch); + defer rollback.Epoch.select(null); + r.copy.take(&r.epoch, c) catch return; + r.keepViews(); + if (r.stats.samples) r.stats.copy_ns.append(r.gpa, nowNs() - t0) catch {}; + r.stats.copy_bytes += r.copy.savedBytes(); + r.stats.copies += 1; + } + + fn keepViews(r: *Replica) void { + const c = r.core(); + for (c.panes, &r.serials) |slot, *n| n.* = if (slot) |pane| pane.serial else 0; + r.retiring = for (c.retired_panes.current, c.retired_panes.previous) |x, y| { + if (x != null or y != null) break true; + } else false; + if (!r.t.screen_state) return; + for (r.core().panes, &r.views) |slot, *v| v.* = if (slot) |pane| if (pane.terminal) |state| pardes.Term.grid(state).view else null else null; + } + + /// Whether a pane opened, closed or is torn down since the copy was + /// taken (`keepViews`). + fn panesMoved(r: *const Replica) bool { + const c = r.core(); + if (r.retiring) return true; + for (c.retired_panes.current, c.retired_panes.previous) |x, y| if (x != null or y != null) return true; + for (c.panes, r.serials) |slot, n| if ((if (slot) |pane| pane.serial else 0) != n) return true; + return false; + } + + fn putViews(r: *Replica) void { + if (!r.t.screen_state) return; + for (r.core().panes, r.views) |slot, at| if (slot) |pane| if (pane.terminal) |state| pardes.Term.viewTo(state, at); + } + + /// Whether its core loaded from the dump is the session as it was + /// dumped, by the welcome's `digest`. + pub fn loaded(r: *Replica, want: u64) bool { + return r.digest() == want; + } + + /// Its own view drawn and presented through `present`, between steps + /// (views.drawOne): nothing the digest covers moves. The epoch's trees + /// held while a copy is (a render can make or drop a syntax tree). + pub fn draw(r: *Replica, present: pardes.views.Present) void { + rollback.Epoch.select(&r.epoch); + defer rollback.Epoch.select(null); + pardes.views.drawOne(r.core(), r.view, present) catch {}; + } + + /// `f(ctx, core)` with its own view live, outside any step, as `draw` + /// draws it (views.within): what its frontend reads of its own view + /// (its pointer's cells, its size) while another client's input was the + /// session's last. + pub fn within(r: *Replica, ctx: anytype, comptime f: fn (@TypeOf(ctx), *Pardes) anyerror!void) void { + rollback.Epoch.select(&r.epoch); + defer rollback.Epoch.select(null); + pardes.views.within(r.core(), r.view, ctx, f) catch {}; + } + + /// One of the authority's terminal messages (`Screens.flush`, or an + /// answer to `screenRequests`), taken between units in stream order: a + /// delta written into its pane's emulator, or fetched rows. The + /// message for the authority (an acknowledgement, or a request to start + /// afresh), valid until the next call, or null. + pub fn screen(r: *Replica, msg: []const u8) ?[]const u8 { + const m = parseScreen(msg) orelse return null; + const c = r.t.core orelse return null; + const pane = c.panes[m.pane] orelse return null; + if (pane.serial != m.serial) return null; // a pane gone: its slot's next is another + const state = pane.terminal orelse return null; + const slot = &r.screens[m.pane]; + if (slot.* == null or slot.*.?.serial != m.serial) { + if (slot.*) |*old| Term.replicaFree(old.rep, pardes.panes.emulator_gpa); + slot.* = null; + const rep = Term.replica(pardes.panes.emulator_gpa) catch { + r.fail("no memory for a terminal's replica"); + return null; + }; + slot.* = .{ .serial = m.serial, .rep = rep }; + } + const sc = &slot.*.?; + // The emulator is the authority's: on the C heap, outside the + // rollback epoch, so a copy held now never saves or puts back any + // of what this writes (State.gpa). + switch (m.kind) { + kind_delta => { + // A unit kept to replay (`since`) read the emulator as it is + // now, and its step may have read it (a key over a shell's + // text reads its rows, an output its prompt): replayed over + // this delta it would read another (the soak, a copy taken + // ahead, three clients: another's key that read the rows, + // replayed by a rollback after the full-screen program + // redrew them, came to another state). So the units since go + // before it: let go with the copy when nothing is on top (the + // state is the confirmed one), else replayed now. + if (r.copy.held and r.since.items.len > 0) { + const was = Stepping.begin(&r.t, &r.epoch); + defer was.end(); + if (r.applied == 0) { + r.copy.release(); + rollback.Epoch.select(null); + r.clearSince(); + } else { + r.toConfirmed(); + r.dirty = true; + } + } + const t0 = nowNs(); + const applied = Term.apply(state, sc.rep, m.payload) catch |err| { + if (err != error.BaseMismatch) r.fail("a terminal's delta did not apply"); + if (sc.refused) return null; + sc.refused = true; + return r.screenMessage(m.pane, m.serial, kind_reset, &.{}); + }; + if (r.stats.samples) r.stats.apply_ns.append(r.gpa, nowNs() - t0) catch {}; + sc.refused = false; + r.echo.delta(c, m.pane, Term.replicaTop(sc.rep)); + // Its rows as the core keeps them read are another grid's. + c.shell_rows.markStale(pane); + // Its view is the authority's now: a rollback puts it back + // here, and what was applied over a terminal goes on top. + if (r.copy.held) r.views[m.pane] = pardes.Term.grid(state).view; + r.overTerminals(); + var seq: [4]u8 = undefined; + std.mem.writeInt(u32, &seq, applied.seq, .little); + return r.screenMessage(m.pane, m.serial, kind_ack, &seq); + }, + kind_rows => { + sc.asked = null; + Term.applyRows(state, sc.rep, m.payload) catch r.fail("a terminal's rows did not apply"); + c.shell_rows.markStale(pane); + r.overTerminals(); + return null; + }, + else => return null, + } + } + + /// A terminal was written (a delta, rows): what is applied over the + /// emulator (`Pending.terminal`) read the one before it, and the + /// authority's step reads this one (it comes before that step's unit). + /// Back to the confirmed position, to be applied again over it. + fn overTerminals(r: *Replica) void { + const any = for (r.pending.items[0..r.applied]) |p| { + if (p.terminal) break true; + } else false; + if (!any) return; + const was = Stepping.begin(&r.t, &r.epoch); + defer was.end(); + r.toConfirmed(); + r.dirty = true; + } + + /// A request for the rows a terminal shows here and does not hold (its + /// holes in view, after a scroll back), one a terminal at a time; null + /// when none is owed. Until the answer, a hole shows blank. + pub fn screenRequest(r: *Replica) ?[]const u8 { + const c = r.t.core orelse return null; + for (&r.screens, 0..) |*slot, id| { + const sc: *ScreenOf = if (slot.*) |*x| x else continue; + if (sc.asked != null) continue; + const pane = c.panes[id] orelse continue; + if (pane.serial != sc.serial) continue; + const state = pane.terminal orelse continue; + const shown = Term.shown(state, sc.rep); + var holes: std.ArrayList([2]u64) = .empty; + defer holes.deinit(r.gpa); + Term.missing(sc.rep, shown[0], shown[1], &holes, r.gpa) catch return null; + if (holes.items.len == 0) continue; + const want: [2]u64 = .{ holes.items[0][0], holes.items[holes.items.len - 1][1] }; + Term.blank(state, sc.rep, want[0], want[1]); + sc.asked = want; + var range: [16]u8 = undefined; + std.mem.writeInt(u64, range[0..8], want[0], .little); + std.mem.writeInt(u64, range[8..16], want[1], .little); + return r.screenMessage(@intCast(id), sc.serial, kind_rows, &range); + } + return null; + } + + fn screenMessage(r: *Replica, pane: u8, serial: u32, kind: u8, payload: []const u8) ?[]const u8 { + r.screen_out.clearRetainingCapacity(); + writeScreenHead(&r.screen_out, r.gpa, pane, serial, kind) catch return null; + r.screen_out.appendSlice(r.gpa, payload) catch return null; + return r.screen_out.items; + } + + /// A piece of a record's bytes by hash (Peer.blob): whole and checked + /// against its hash, kept for the unit that names it (tape.keepFile), + /// and a file's, a far client's, on disk (`cache`) for its next join. + pub fn blobPiece(r: *Replica, msg: []const u8) void { + if (msg.len < 49) return r.fail("a short piece of a record by hash"); + const total = std.math.cast(usize, std.mem.readInt(u64, msg[32..40], .little)) orelse return r.fail("a record by hash too long"); + const at = std.mem.readInt(u64, msg[40..48], .little); + if (at == 0) { + if (r.incoming) |b| r.gpa.free(b.bytes); + r.incoming = .{ .hash = msg[0..32].*, .bytes = r.gpa.alloc(u8, total) catch return r.fail("out of memory for a record by hash"), .path = msg[48] != 0 }; + r.incoming_got = 0; + } + const b = r.incoming orelse return r.fail("a piece of a record by hash out of order"); + const piece = msg[49..]; + if (at != r.incoming_got or b.bytes.len != total or piece.len > total - at) return r.fail("a piece of a record by hash out of order"); + @memcpy(b.bytes[at..][0..piece.len], piece); + r.incoming_got += piece.len; + if (r.incoming_got < total) return; + r.incoming = null; + if ((b.path and b.bytes.len < 9) or !std.mem.eql(u8, &tape.Big.hashOf(b.bytes, b.path), &b.hash)) { + r.gpa.free(b.bytes); + return r.fail("a record by hash came other than its hash"); + } + if (b.path) if (r.cache) |c| if (b.bytes.len - 9 <= c.cap) c.put(r.io, &.{.{ .hash = b.hash, .bytes = b.bytes[9..] }}, &.{}); + tape.keepFile(&r.t, b); + } + + pub fn digest(r: *Replica) u64 { + const was = Stepping.begin(&r.t, &r.epoch); + defer was.end(); + return tape.digestOf(r.core()); + } +}; + +/// The bytes of a unit's last digest record: the state it ended on; null +/// for one that says what the host wrote or which host it is (a replica's +/// core takes those only by replaying the unit). +fn lastDigest(bytes: []const u8) ?[]const u8 { + var r: walk.Reader = .{ .bytes = bytes }; + var last: ?[]const u8 = null; + while (true) { + const head = r.take(5) catch return last; + const payload = r.take(std.mem.bytesToValue(u32, head[1..5])) catch return last; + if (head[0] == tape.digest_kind) last = payload; + if (head[0] == tape.set_kind) return null; + } +} + +fn terminalDigests(p: *const Pardes, out: *[pardes.MAX_PANES]u64) void { + for (p.panes, out) |slot, *d| { + d.* = 0; + const pane = slot orelse continue; + const state = pane.terminal orelse continue; + var h = walk.Hasher.init(0); + // Its view aside: a speculation may move it (`views`). + state.digestScreen(&h, false); + d.* = h.final(); + } +} + +// ---- over a socket (docs/next-daemon.md, "Phase 5 part B notes") ---- +// +// The session's half: each client handed over by the daemon (daemon.zig) is +// a Peer, with a view of its own, a thread reading its inputs and one +// writing what it is sent. The recorder is on while any is. At each step +// boundary of the session's loop (`Peers.boundary`, after a pump): +// - the pump's unit goes to every client; +// - a client that came is joined: its view opened, the session dumped +// (core/dump.zig) and sent ahead of every unit after; +// - each input that came is a unit of its own: its view, the input, a +// `step` (what the client's own core applied at once); +// - a client gone takes its view with it. +// A client's link only ever holds bytes queued for it: the session's step +// never waits on one. One that lets `lag_max` pile up is cut off. + +const libc = std.c; +const log = std.log.scoped(.replica); +/// What a client may leave unread, past its join's dump, before it is cut +/// off: minutes of typing, a screenful of a flood's deltas many times over. +const lag_max: usize = 64 << 20; +const dump_piece = 1 << 20; +/// A blob in a far join's list (wire.zig `blobs`): its place in the state +/// (u64le), its length (u64le) and its BLAKE3 hash. +const blob_entry = 48; + +const Tm = extern struct { sec: c_int, min: c_int, hour: c_int, mday: c_int, mon: c_int, year: c_int, wday: c_int, yday: c_int, isdst: c_int, gmtoff: c_long, zone: ?[*:0]const u8 }; +extern "c" fn localtime_r(t: *const libc.time_t, tm: *Tm) ?*Tm; + +/// This zone's offset from UTC now, seconds. +fn gmtoff() i32 { + var ts: libc.timespec = undefined; + _ = libc.clock_gettime(.REALTIME, &ts); + const secs: libc.time_t = ts.sec; + var tm: Tm = undefined; + return if (localtime_r(&secs, &tm) != null) @intCast(tm.gmtoff) else 0; +} + +/// `bytes` as dump pieces (1 MiB at most each). +fn writePieces(fd: c_int, bytes: []const u8) bool { + var at: usize = 0; + while (at < bytes.len) : (at += dump_piece) { + const piece = bytes[at..@min(bytes.len, at + dump_piece)]; + const head = wire.header(@backingInt(wire.ServerTag.dump), piece.len); + if (!writeAll(fd, &head) or !writeAll(fd, piece)) return false; + } + return true; +} + +// ---- zstd (libzstd, vendored: build.zig) ---- + +/// A far client's stream from its welcome on (`Peer.farJoin`): its join in +/// one frame at level 6 over an 8 MiB window, since an undo snapshot is a +/// near copy of its file's text, a file's length before or after it (the +/// join's numbers: Phase 6's content cache and zstd notes); what follows in +/// a frame at level 3 over 1 MiB. Each envelope (wire.zig `zstd`) is a +/// flush of at most `piece` of input, so that a reader unpacks one whole. +const zstd = struct { + const CCtx = opaque {}; + const DCtx = opaque {}; + const In = extern struct { src: ?*const anyopaque, size: usize, pos: usize }; + const Out = extern struct { dst: ?*anyopaque, size: usize, pos: usize }; + const Mem = extern struct { + alloc: *const fn (?*anyopaque, usize) callconv(.c) ?*anyopaque, + free: *const fn (?*anyopaque, ?*anyopaque) callconv(.c) void, + ctx: ?*anyopaque = null, + }; + extern fn ZSTD_createCCtx_advanced(mem: Mem) ?*CCtx; + extern fn ZSTD_freeCCtx(c: ?*CCtx) usize; + extern fn ZSTD_CCtx_setParameter(c: *CCtx, param: c_int, value: c_int) usize; + extern fn ZSTD_compressStream2(c: *CCtx, out: *Out, in: *In, op: c_int) usize; + extern fn ZSTD_createDCtx_advanced(mem: Mem) ?*DCtx; + extern fn ZSTD_freeDCtx(d: ?*DCtx) usize; + extern fn ZSTD_decompressStream(d: *DCtx, out: *Out, in: *In) usize; + extern fn ZSTD_isError(code: usize) c_uint; + const level_param = 100; + const window_param = 101; + const Op = enum(c_int) { more = 0, flush = 1, end = 2 }; + const piece = 1 << 20; + + /// What zstd allocates is a Zig allocator's (c_heap): a test's own. + var gpa: Allocator = std.heap.c_allocator; + const heap = c_heap.Heap(&gpa); + fn alloc(_: ?*anyopaque, n: usize) callconv(.c) ?*anyopaque { + return heap.malloc(n); + } + fn free(_: ?*anyopaque, p: ?*anyopaque) callconv(.c) void { + heap.free(p); + } + const mem: Mem = .{ .alloc = alloc, .free = free }; + + fn compressor(level: c_int, window_log: c_int) ?*CCtx { + const c = ZSTD_createCCtx_advanced(mem) orelse return null; + if (ZSTD_isError(ZSTD_CCtx_setParameter(c, level_param, level)) != 0 or + ZSTD_isError(ZSTD_CCtx_setParameter(c, window_param, window_log)) != 0) + { + _ = ZSTD_freeCCtx(c); + return null; + } + return c; + } +}; + +fn readSome(fd: c_int, buf: []u8) usize { + while (true) { + const n = libc.read(fd, buf.ptr, buf.len); + if (n < 0 and libc.errno(n) == .INTR) continue; + return if (n <= 0) 0 else @intCast(n); + } +} + +/// What a stream's reader holds of it, read and not yet taken: a dump's +/// reader leaves the units read with its last piece to the stream's. +pub const Framer = struct { + buf: std.ArrayListUnmanaged(u8) = .empty, + len: usize = 0, + /// What zstd envelopes (wire.zig `zstd`) unpacked to, its messages not + /// taken yet, and the stream's decompressor: a far client's. + inner: std.ArrayListUnmanaged(u8) = .empty, + z: ?*zstd.DCtx = null, + bad: bool = false, + + pub fn deinit(f: *Framer, gpa: Allocator) void { + f.buf.deinit(gpa); + f.inner.deinit(gpa); + if (f.z) |z| _ = zstd.ZSTD_freeDCtx(z); + f.z = null; + } + + /// The length a message past the wire's bound said it had, when that + /// is where `run` stopped: an error to name, never a hangup (17.3). + pub fn overlong(fr: *const Framer) ?u32 { + for ([_][]const u8{ fr.buf.items[0..fr.len], fr.inner.items }) |b| if (wire.framed(b)) |_| {} else |_| return std.mem.readInt(u32, b[1..5], .little); + return null; + } + + /// Framed messages off `fd` (wire.zig) handed to `take` until + /// the stream ends, `take` says stop, or a message is malformed. An + /// envelope's are handed on as if they had come bare. + fn run(fr: *Framer, gpa: Allocator, fd: c_int, ctx: anytype, comptime take: fn (@TypeOf(ctx), u8, []const u8) bool) void { + if (fr.bad or !fr.drain(ctx, take)) return; + while (true) { + var at: usize = 0; + var stop = false; + while (!stop) { + const f = (wire.framed(fr.buf.items[at..fr.len]) catch break) orelse break; + at += f.total; + if (f.tag == @backingInt(wire.ServerTag.zstd)) { + fr.bad = !fr.unpack(gpa, f.payload); + stop = fr.bad or !fr.drain(ctx, take); + } else stop = !take(ctx, f.tag, f.payload); + } + if (at > 0) { + std.mem.copyForwards(u8, fr.buf.items[0 .. fr.len - at], fr.buf.items[at..fr.len]); + fr.len -= at; + } + if (stop) return; + if (wire.framed(fr.buf.items[0..fr.len])) |_| {} else |_| return; + // Room for what the head says is coming, or 64 KiB more. + const want = if (fr.len >= wire.header_len) wire.header_len + @as(usize, std.mem.readInt(u32, fr.buf.items[1..5], .little)) else 0; + const room = @max(want, fr.len + (64 << 10)); + if (fr.buf.items.len < room) fr.buf.resize(gpa, room) catch return; + const n = readSome(fd, fr.buf.items[fr.len..]); + if (n == 0) return; + fr.len += n; + } + } + + /// An envelope's bytes onto `inner`. Its writer flushed at most + /// `zstd.piece` of input into it, so it unpacks to no more than that. + fn unpack(fr: *Framer, gpa: Allocator, payload: []const u8) bool { + if (fr.z == null) fr.z = zstd.ZSTD_createDCtx_advanced(zstd.mem); + const z = fr.z orelse return false; + var in: zstd.In = .{ .src = payload.ptr, .size = payload.len, .pos = 0 }; + while (true) { + fr.inner.ensureUnusedCapacity(gpa, 256 << 10) catch return false; + const spare = fr.inner.unusedCapacitySlice(); + var out: zstd.Out = .{ .dst = spare.ptr, .size = spare.len, .pos = 0 }; + if (zstd.ZSTD_isError(zstd.ZSTD_decompressStream(z, &out, &in)) != 0) return false; + fr.inner.items.len += out.pos; + if (in.pos == in.size and out.pos < out.size) return true; + } + } + + /// The whole messages in `inner` handed to `take`; false when it says + /// stop (what is left waits for the next run) or one is malformed. + fn drain(fr: *Framer, ctx: anytype, comptime take: fn (@TypeOf(ctx), u8, []const u8) bool) bool { + var at: usize = 0; + var stop = false; + while (!stop) { + const f = (wire.framed(fr.inner.items[at..]) catch { + fr.bad = true; + return false; + }) orelse break; + at += f.total; + stop = !take(ctx, f.tag, f.payload); + } + if (at > 0) { + const rest = fr.inner.items.len - at; + std.mem.copyForwards(u8, fr.inner.items[0..rest], fr.inner.items[at..]); + fr.inner.items.len = rest; + } + return !stop; + } +}; + +pub const Peer = struct { + peers: *Peers, + fd: c_int, + id: u16, + view: u8, + /// Its process (its socket's peer), for `--ps`; 0 not known. + pid: libc.pid_t = 0, + /// Its bye said it joins again for its view (a divergence's rejoin): + /// kept for it (views.away), not closed. Its reader's, read after its end. + again: bool = false, + /// Its number in the session's `Screens`. + sid: u16 = 0, + /// Inputs read (each one's seq, as the client numbered them). + seq: u32 = 0, + /// The `stamp` frame its next input came with (its reader's). + stamp: [stamp_len]u8 = undefined, + stamped: bool = false, + reader: ?std.Thread = null, + writer: ?std.Thread = null, + lock: std.Io.Mutex = .init, + cond: std.Io.Condition = .init, + /// What it is sent, not written yet, and what the writer writes now. + out: std.ArrayListUnmanaged(u8) = .empty, + sending: std.ArrayListUnmanaged(u8) = .empty, + limit: usize = 64 << 20, + closing: bool = false, + cut: bool = false, + /// Its writer has ended (what it was sent, written). + done: std.atomic.Value(bool) = .init(false), + /// Its join, which its writer writes before what it is sent: the + /// welcome, then the dump with the session's blobs put back where they + /// go, pinned until written (memory.zig `pin`). The core's allocator's. + welcome: [wire.hello_max + 64]u8 = undefined, + welcome_len: usize = 0, + dump: []u8 = &.{}, + blobs: std.ArrayListUnmanaged(walk.Deferred) = .empty, + dump_gpa: Allocator = undefined, + /// Its first word after its hello: whether it is far (through ssh), its + /// blobs asked for by hash and its stream in zstd (`farJoin`). + far: ?bool = null, + /// A far client's answer to its join's list: the blobs it lacks. + want: ?[]u8 = null, + /// A far client's stream's compressor, and an envelope's worth of its + /// output; the input since the last envelope. + z: ?*zstd.CCtx = null, + zout: std.ArrayListUnmanaged(u8) = .empty, + zin: usize = 0, + + fn writeJoin(peer: *Peer) bool { + if (!writeAll(peer.fd, peer.welcome[0..peer.welcome_len])) return false; + const io = peer.peers.io; + peer.lock.lockUncancelable(io); + while (peer.far == null and !peer.closing and !peer.cut) peer.cond.waitUncancelable(io, &peer.lock); + const far = peer.far; + peer.lock.unlock(io); + if (far orelse return false) return peer.farJoin(); + var pos: usize = 0; + for (peer.blobs.items) |b| { + if (!writePieces(peer.fd, peer.dump[pos..b.at]) or !writePieces(peer.fd, b.bytes)) return false; + pos = b.at; + } + return writePieces(peer.fd, peer.dump[pos..]); + } + + /// A far client's join, in one zstd frame: the list of the dump's blobs + /// (each one's place in the state, its length and its BLAKE3 hash), the + /// state, then the blobs it says it lacks; what follows the join goes + /// in a cheaper frame. The hashing is this thread's, never the step's. + /// ponytail: a blob is hashed again at every far join, 3.6 GB/s; a + /// session's join after its first could keep them. + fn farJoin(peer: *Peer) bool { + const gpa = peer.peers.gpa; + peer.z = zstd.compressor(6, 23) orelse return false; + var list: std.ArrayListUnmanaged(u8) = .empty; + defer list.deinit(gpa); + for (peer.blobs.items) |b| { + // Gone meanwhile: its leave waits for this thread in the + // session's step, so the hashing stops here. + if (peer.letting()) return false; + var entry: [blob_entry]u8 = undefined; + std.mem.writeInt(u64, entry[0..8], b.at, .little); + std.mem.writeInt(u64, entry[8..16], b.bytes.len, .little); + std.crypto.hash.Blake3.hash(b.bytes, entry[16..48], .{}); + list.appendSlice(gpa, &entry) catch return false; + if (list.items.len + blob_entry > dump_piece) { + if (!peer.zMessage(.blobs, list.items)) return false; + list.clearRetainingCapacity(); + } + } + if (list.items.len > 0 and !peer.zMessage(.blobs, list.items)) return false; + if (!peer.zPieces(peer.dump) or !peer.zWrite(&.{}, .flush)) return false; + if (peer.blobs.items.len > 0) { + const io = peer.peers.io; + peer.lock.lockUncancelable(io); + while (peer.want == null and !peer.closing and !peer.cut) peer.cond.waitUncancelable(io, &peer.lock); + const want = peer.want; + peer.lock.unlock(io); + const bits = want orelse return false; + if (bits.len != (peer.blobs.items.len + 7) / 8) return false; + for (peer.blobs.items, 0..) |b, i| if (bits[i / 8] >> @intCast(i % 8) & 1 != 0) { + if (!peer.zPieces(b.bytes)) return false; + }; + } + if (!peer.zWrite(&.{}, .end)) return false; + _ = zstd.ZSTD_freeCCtx(peer.z); + peer.z = zstd.compressor(3, 20); + return peer.z != null; + } + + /// Whether it is being let go (or was cut off). + fn letting(peer: *Peer) bool { + const io = peer.peers.io; + peer.lock.lockUncancelable(io); + defer peer.lock.unlock(io); + return peer.closing or peer.cut; + } + + /// One message into its stream's compressor. + fn zMessage(peer: *Peer, tag: wire.ServerTag, payload: []const u8) bool { + const head = wire.header(@backingInt(tag), payload.len); + return peer.zWrite(&head, .more) and peer.zWrite(payload, .more); + } + + /// `bytes` as dump pieces (1 MiB at most each), into the compressor. + fn zPieces(peer: *Peer, bytes: []const u8) bool { + var at: usize = 0; + while (at < bytes.len) : (at += dump_piece) { + if (!peer.zMessage(.dump, bytes[at..@min(bytes.len, at + dump_piece)])) return false; + } + return true; + } + + /// `bytes` into its stream's compressor, and what comes out written as + /// envelopes: one at each `zstd.piece` of input, and one now if `op` + /// says flush or end (the frame's). + fn zWrite(peer: *Peer, bytes: []const u8, op: zstd.Op) bool { + const z = peer.z orelse return false; + var rest = bytes; + while (true) { + const n = @min(rest.len, zstd.piece - peer.zin); + const full = peer.zin + n == zstd.piece; + const now: zstd.Op = if (n < rest.len or (full and op == .more)) .flush else op; + var in: zstd.In = .{ .src = rest.ptr, .size = n, .pos = 0 }; + while (true) { + peer.zout.ensureUnusedCapacity(peer.peers.gpa, 128 << 10) catch return false; + const spare = peer.zout.unusedCapacitySlice(); + var out: zstd.Out = .{ .dst = spare.ptr, .size = spare.len, .pos = 0 }; + const left = zstd.ZSTD_compressStream2(z, &out, &in, @backingInt(now)); + if (zstd.ZSTD_isError(left) != 0) return false; + peer.zout.items.len += out.pos; + if (if (now == .more) in.pos == in.size else left == 0) break; + } + peer.zin += n; + if (now != .more) { + const head = wire.header(@backingInt(wire.ServerTag.zstd), peer.zout.items.len); + if (!writeAll(peer.fd, &head) or !writeAll(peer.fd, peer.zout.items)) return false; + peer.zout.clearRetainingCapacity(); + peer.zin = 0; + } + rest = rest[n..]; + if (rest.len == 0) return true; + } + } + + /// `parts` queued for it, in order; never waits. A client whose queue + /// passes its limit is cut off: its link is shut down, and its reader + /// says it is gone. + fn send(peer: *Peer, parts: []const []const u8) void { + const io = peer.peers.io; + peer.lock.lockUncancelable(io); + defer peer.lock.unlock(io); + if (peer.cut or peer.closing) return; + // The wire's bound, as the message goes (17.3): one past it would + // be the link's end at the reader. + const len = std.mem.readInt(u32, parts[0][1..5], .little); + if (len > wire.max_payload) { + log.err("a {d} MB message to client {d}, over the {d} MiB bound: dropped", .{ len >> 20, peer.id, wire.max_payload >> 20 }); + if (checked) std.debug.panic("a {d}-byte message to client {d}, over the wire's bound", .{ len, peer.id }); + return; + } + var n: usize = peer.out.items.len; + for (parts) |p| n += p.len; + if (n > peer.limit) { + log.info("client {d} is {d} MB behind: cut off", .{ peer.id, n >> 20 }); + peer.peers.cut += 1; + peer.cut = true; + _ = libc.shutdown(peer.fd, 2); + peer.cond.signal(io); + return; + } + for (parts) |p| peer.out.appendSlice(peer.peers.gpa, p) catch { + peer.cut = true; + _ = libc.shutdown(peer.fd, 2); + break; + }; + peer.cond.signal(io); + } + + /// A record's bytes by hash (tape.Big) queued for it in pieces, before + /// the unit that names them: for a far client, and for one on this + /// machine unless they are a file it reads itself (tape.zig `byHash`). + /// Never cut off for them: a big file is no lag. + fn blob(peer: *Peer, b: tape.Big) void { + const io = peer.peers.io; + peer.lock.lockUncancelable(io); + defer peer.lock.unlock(io); + if (peer.cut or peer.closing or (b.path and peer.far == false)) return; + var at: usize = 0; + while (at < b.bytes.len) : (at += dump_piece) { + const piece = b.bytes[at..@min(b.bytes.len, at + dump_piece)]; + var meta: [49]u8 = undefined; + meta[0..32].* = b.hash; + std.mem.writeInt(u64, meta[32..40], b.bytes.len, .little); + std.mem.writeInt(u64, meta[40..48], at, .little); + meta[48] = @intFromBool(b.path); + const head = wire.header(@backingInt(wire.ServerTag.blob), meta.len + piece.len); + for ([_][]const u8{ &head, &meta, piece }) |part| peer.out.appendSlice(peer.peers.gpa, part) catch { + peer.cut = true; + _ = libc.shutdown(peer.fd, 2); + return; + }; + } + peer.cond.signal(io); + } + + fn writeLoop(peer: *Peer) void { + const io = peer.peers.io; + defer peer.done.store(true, .release); + if (!peer.writeJoin()) { + _ = libc.shutdown(peer.fd, 2); + return; + } + // Its pins let go at the next boundary. + peer.peers.post(.{ .joined = peer }); + while (true) { + peer.lock.lockUncancelable(io); + while (peer.out.items.len == 0 and !peer.closing and !peer.cut) peer.cond.waitUncancelable(io, &peer.lock); + if (peer.out.items.len == 0 or peer.cut) { + peer.lock.unlock(io); + return; + } + std.mem.swap(std.ArrayListUnmanaged(u8), &peer.out, &peer.sending); + // The limit was for the dump and what is queued behind it. + peer.limit = lag_max; + peer.lock.unlock(io); + const ok = if (peer.z != null) peer.zWrite(peer.sending.items, .flush) else writeAll(peer.fd, peer.sending.items); + peer.sending.clearRetainingCapacity(); + if (!ok) { + _ = libc.shutdown(peer.fd, 2); + return; + } + } + } + + fn take(peer: *Peer, tag: u8, payload: []const u8) bool { + const t = std.enums.fromInt(wire.ClientTag, tag) orelse return false; + switch (t) { + .hello => return false, + .bye => { + peer.again = (wire.decodeClient(tag, payload) catch return false).bye.again; + return false; + }, + .ping => { + var out: [16]u8 = undefined; + const v = (wire.decodeClient(tag, payload) catch return false).ping; + const pong = wire.encodeServer(&out, .{ .pong = v }) catch return false; + peer.send(&.{pong}); + return true; + }, + // Its word about a terminal (an ack, rows asked, a reset), to + // the session's Screens at the next boundary. + .screen_ack => { + const msg = peer.peers.gpa.dupe(u8, payload) catch return false; + peer.peers.post(.{ .screen = .{ .peer = peer, .msg = msg } }); + return true; + }, + .check => { + peer.peers.post(.{ .check = peer }); + return true; + }, + // What its join's writer waits for (`writeJoin`, `farJoin`). + .far, .want => { + const io = peer.peers.io; + const msg = wire.decodeClient(tag, payload) catch return false; + const bits = if (msg == .want) peer.peers.gpa.dupe(u8, msg.want) catch return false else null; + peer.lock.lockUncancelable(io); + defer peer.lock.unlock(io); + if (bits) |b| { + if (peer.want != null) { + peer.peers.gpa.free(b); + return false; + } + peer.want = b; + } else peer.far = msg.far; + peer.cond.signal(io); + return true; + }, + // Its next input's time, kept for it (this thread's alone). + .stamp => { + const s = (wire.decodeClient(tag, payload) catch return false).stamp; + _ = wire.encodeClient(&peer.stamp, .{ .stamp = s }) catch return false; + peer.stamped = true; + return true; + }, + else => {}, + } + peer.seq += 1; + const stamp: []const u8 = if (peer.stamped) &peer.stamp else &.{}; + peer.stamped = false; + const msg = peer.peers.gpa.alloc(u8, head_len + stamp.len + wire.header_len + payload.len) catch return false; + header(msg, peer.id, peer.seq); + @memcpy(msg[head_len..][0..stamp.len], stamp); + const at = head_len + stamp.len; + msg[at] = tag; + std.mem.writeInt(u32, msg[at + 1 ..][0..4], @intCast(payload.len), .little); + @memcpy(msg[at + wire.header_len ..], payload); + if (parseInput(msg) == null) { + peer.peers.gpa.free(msg); + return false; + } + peer.peers.post(.{ .input = .{ .peer = peer, .msg = msg } }); + return true; + } + + fn readLoop(peer: *Peer) void { + // The framer goes before `gone`, after which the peer may be freed. + var fr: Framer = .{}; + fr.run(peer.peers.gpa, peer.fd, peer, take); + if (fr.overlong()) |n| log.err("client {d} sent a {d} MB message, over the {d} MiB bound: let go", .{ peer.id, n >> 20, wire.max_payload >> 20 }); + fr.deinit(peer.peers.gpa); + peer.peers.post(.{ .gone = peer }); + } +}; + +/// The session's clients, for `dying`. +var live_peers: ?*Peers = null; + +/// The process is panicking: each client's link shut first, so that it +/// hears the end now, not when the trace is written (seconds, symbolized). +pub fn dying() void { + const ps = live_peers orelse return; + for (ps.list) |slot| if (slot) |peer| { + _ = libc.shutdown(peer.fd, 2); + }; +} + +/// The process at the other end of unix socket `fd` (Linux's SO_PEERCRED), +/// or 0: a client's through ssh is ssh's. +fn peerPid(fd: c_int) libc.pid_t { + if (comptime builtin.os.tag != .linux) return 0; + const Cred = extern struct { pid: libc.pid_t, uid: libc.uid_t, gid: libc.gid_t }; + var cred: Cred = undefined; + var len: libc.socklen_t = @sizeOf(Cred); + if (libc.getsockopt(fd, libc.SOL.SOCKET, libc.SO.PEERCRED, &cred, &len) != 0) return 0; + return cred.pid; +} + +pub const Peers = struct { + gpa: Allocator = std.heap.c_allocator, + io: std.Io = undefined, + /// This session's name and pid, for a welcome. + name: []const u8 = "", + t: tape.Tape = undefined, + recording: bool = false, + list: [pardes.views.max]?*Peer = @splat(null), + next_id: u16 = 1, + seq: u32 = 0, + lock: std.Io.Mutex = .init, + inbox: std.ArrayListUnmanaged(Note) = .empty, + taken: std.ArrayListUnmanaged(Note) = .empty, + /// A reader posted: the loop's one test a step while nothing is attached. + mail: std.atomic.Value(bool) = .init(false), + /// ...and what it posted was a client's input or a join, which the + /// session steps at once; a terminal's acknowledgement waits for the + /// session's next frame, as its output does (tui.zig `waitInput`). + urgent: std.atomic.Value(bool) = .init(false), + /// Wakes the session's loop (its wait for input) from a reader. + wake_ctx: ?*anyopaque = null, + wake: ?*const fn (?*anyopaque) void = null, + /// No frontend of its own (the host says): with no client either, the + /// session is unattended. + detached: bool = false, + /// The worst a join cost the session's step, and the last one's dump. + join_ns: u64 = 0, + each_join: std.ArrayListUnmanaged(u64) = .empty, + dump_bytes: usize = 0, + /// ...of it the state the step dumped (the rest, blobs, the writer's). + state_bytes: usize = 0, + /// What each client input cost the session (its step, its unit sent), + /// and each boundary (its pump's unit sent, what came taken in), ns: + /// PARDES_REPLICA_STATS writes them at the session's end, and only then + /// are these and `each_join` kept (`samples`): a session runs for days. + samples: bool = false, + input_ns: std.ArrayListUnmanaged(u64) = .empty, + boundary_ns: std.ArrayListUnmanaged(u64) = .empty, + cut: u32 = 0, + head: [unit_head]u8 = undefined, + /// Its clients' terminals as screen state (core/term/screen_state.zig), while + /// it records: what each was sent. + screens: Screens = undefined, + msgs: std.ArrayListUnmanaged([]u8) = .empty, + + pub const Note = union(enum) { join: Joining, input: Msg, screen: Msg, gone: *Peer, check: *Peer, joined: *Peer }; + /// A connection to join, and the view it joins again for, if it does. + pub const Joining = daemon.Joining; + pub const Msg = struct { peer: *Peer, msg: []u8 }; + + pub fn count(ps: *const Peers) usize { + var n: usize = 0; + for (ps.list) |slot| n += @intFromBool(slot != null); + return n; + } + + fn post(ps: *Peers, note: Note) void { + ps.lock.lockUncancelable(ps.io); + ps.inbox.append(ps.gpa, note) catch { + ps.lock.unlock(ps.io); + switch (note) { + .join => |j| _ = libc.close(j.fd), + .input, .screen => |in| ps.gpa.free(in.msg), + .gone, .check, .joined => {}, + } + return; + }; + ps.mail.store(true, .release); + if (note != .screen) ps.urgent.store(true, .release); + ps.lock.unlock(ps.io); + if (ps.wake) |w| w(ps.wake_ctx); + } + + /// A replica's connection the daemon handed over (its hello taken + /// there), and the view its hello says it had: joined at the next step + /// boundary. Any thread. + pub fn handed(ps: *Peers, fd: c_int, view: ?u8) void { + ps.post(.{ .join = .{ .fd = fd, .view = view } }); + } + + /// What was recorded since the last unit, to every client as one, its + /// digest at its end. + fn unit(ps: *Peers, origin: u16, seq: u32) void { + if (ps.t.buf.items.len == 0) return; + tape.recordDigest(&ps.t); + const bytes = ps.t.buf.items; + unitHeader(&ps.head, origin, seq); + trace(ps.gpa, "session: unit {d}/{d}, {d} bytes, to {d} clients:\n", .{ origin, seq, bytes.len, ps.count() }, &.{bytes}); + // Its records by hash first (tape.by_hash_min: a file read), each + // whole before the unit that names it. + for (ps.t.big.items) |b| { + for (ps.list) |slot| if (slot) |peer| peer.blob(b); + ps.gpa.free(b.bytes); + } + ps.t.big.clearRetainingCapacity(); + const head = wire.header(@backingInt(wire.ServerTag.unit), unit_head + bytes.len); + for (ps.list) |slot| if (slot) |peer| peer.send(&.{ &head, &ps.head, bytes }); + ps.t.buf.clearRetainingCapacity(); + // The sizes its steps gave terminals, now: the next flush says so. + if (ps.t.core) |core| pardes.panes.resizeDeferred(core); + } + + /// After each of the session's pumps: its unit sent, the clients that + /// came joined, their inputs taken in, the ones gone let go. + pub fn boundary(ps: *Peers, core: *Pardes) void { + if (!ps.recording and !ps.mail.load(.acquire)) return; + const t0 = nowNs(); + defer if (ps.recording and ps.samples) ps.boundary_ns.append(ps.gpa, nowNs() - t0) catch {}; + if (ps.recording) { + // The rows the pump's steps read, then its unit, then what the + // terminals came to (the next steps read that). + ps.screensOut(core, .fill); + ps.seq += 1; + ps.unit(0, ps.seq); + ps.screensOut(core, .flush); + } + if (!ps.mail.load(.acquire)) return; + ps.lock.lockUncancelable(ps.io); + std.mem.swap(std.ArrayListUnmanaged(Note), &ps.inbox, &ps.taken); + ps.mail.store(false, .release); + ps.lock.unlock(ps.io); + for (ps.taken.items) |note| switch (note) { + .join => |j| ps.join(core, j), + .input => |in| { + ps.input(core, in.peer, in.msg); + ps.gpa.free(in.msg); + }, + .screen => |in| { + defer ps.gpa.free(in.msg); + const reply = (ps.screens.take(core, in.peer.sid, in.msg) catch null) orelse continue; + defer ps.gpa.free(reply); + const head = wire.header(@backingInt(wire.ServerTag.screen), reply.len); + in.peer.send(&.{ &head, reply }); + }, + .gone => |peer| ps.leave(core, peer), + .joined => |peer| released(peer), + .check => |peer| { + var out: [16]u8 = undefined; + peer.send(&.{wire.encodeServer(&out, .{ .check = tape.digestOf(core) }) catch unreachable}); + }, + }; + ps.taken.clearRetainingCapacity(); + } + + fn input(ps: *Peers, core: *Pardes, peer: *Peer, msg: []const u8) void { + const in = parseInput(msg) orelse return; + const t0 = nowNs(); + defer if (ps.samples) ps.input_ns.append(ps.gpa, nowNs() - t0) catch {}; + // The terminals as this step reads them, then the step, the rows + // it read, its unit (part B terminal notes' order). + ps.screensOut(core, .flush); + core.toView(peer.view); + ps.t.stamp = stampOf(in); + core.update(in.event); + core.step(); + ps.t.stamp = null; + ps.screensOut(core, .fill); + ps.unit(peer.id, in.seq); + // What the step made of a kind's pane (the tick after a key or a + // click into it: another layout) goes now, not at the next pump's: + // one may not come before the client looks. + ps.screensOut(core, .flush); + } + + const ScreensOut = enum { flush, fill }; + + /// What each client is owed of the terminals (`Screens.flush`), or the + /// rows a step read that it lacks (`Screens.fill`, then `filled`), each + /// a screen message queued for it. + fn screensOut(ps: *Peers, core: *Pardes, comptime what: ScreensOut) void { + for (ps.list) |slot| if (slot) |peer| ps.screensTo(core, peer, what); + if (what == .fill) ps.screens.filled(core); + } + + fn screensTo(ps: *Peers, core: *Pardes, peer: *Peer, comptime what: ScreensOut) void { + defer { + for (ps.msgs.items) |m| ps.gpa.free(m); + ps.msgs.clearRetainingCapacity(); + } + (switch (what) { + .flush => ps.screens.flush(core, peer.sid, peer.view, &ps.msgs), + .fill => ps.screens.fill(core, peer.sid, &ps.msgs), + }) catch return; + for (ps.msgs.items) |m| { + const head = wire.header(@backingInt(wire.ServerTag.screen), m.len); + peer.send(&.{ &head, m }); + } + } + + fn join(ps: *Peers, core: *Pardes, joining: Joining) void { + const fd = joining.fd; + const t0 = nowNs(); + defer { + ps.join_ns = @max(ps.join_ns, nowNs() - t0); + if (ps.samples) ps.each_join.append(ps.gpa, nowNs() - t0) catch {}; + } + const slot = for (&ps.list) |*s| { + if (s.* == null) break s; + } else return daemon.refuseReplica(fd, "that session has as many clients as it takes"); + live_peers = ps; + if (!ps.recording) { + // A session recording to a file (PARDES_RECORD) has its tape. + if (tape.on != null) return daemon.refuseReplica(fd, "that session records a tape of its own (PARDES_RECORD)"); + ps.t = tape.memory(ps.gpa); + ps.t.unit_digests = true; + // Terminals cross as screen state: a unit carries no pty bytes, + // and its digest is a word (PARDES_REPLICA_TRACE: one a field). + ps.t.screen_state = true; + ps.t.whole_digest = std.c.getenv("PARDES_REPLICA_TRACE") == null; + ps.screens = .init(ps.gpa); + tape.on = &ps.t; + ps.recording = true; + ps.samples = std.c.getenv("PARDES_REPLICA_STATS") != null; + } + // Someone looks at it now: its messages go by keys, not the clock. + core.unattended = false; + // Its own view again, kept for it since it left (views.away), or one + // new. ponytail: a divergence's redial that came before its old + // link's end (posted by that link's reader, microseconds after the + // bye; the redial is a dial and the daemon's hand-over later) finds + // its view not kept yet, and takes a new one. + const back = if (joining.view) |v| core.rejoinView(v) else false; + const k = (if (back) joining.view else core.openView()) orelse { + core.unattended = ps.count() == 0 and ps.detached; + if (ps.count() == 0) ps.stop(core); + return daemon.refuseReplica(fd, "that session has as many views as it takes"); + }; + // Its own process draws it. + core.undrawn |= @as(u8, 1) << @intCast(k); + ps.seq += 1; + ps.unit(0, ps.seq); + // The session's mutable state, dumped now; its blobs (file texts, + // undo, registers) left out, pinned, and streamed by the client's + // writer thread: the step pays for the state, not for the text. + var blobs: std.ArrayListUnmanaged(walk.Deferred) = .empty; + const bytes = pardes.dump.dumpJoin(core, &blobs) catch |err| { + blobs.deinit(core.gpa); + core.undrawn &= ~(@as(u8, 1) << @intCast(k)); + core.closeView(k); + ps.unit(0, ps.seq); + if (ps.count() == 0) ps.stop(core); + var why: [96]u8 = undefined; + return daemon.refuseReplica(fd, std.fmt.bufPrint(&why, "that session could not be dumped: {t}", .{err}) catch "that session could not be dumped"); + }; + var whole = bytes.len; + for (blobs.items) |b| { + pardes.memory.pin(b.bytes); + whole += b.bytes.len; + } + ps.dump_bytes = whole; + ps.state_bytes = bytes.len; + // The joiner's core is the dump's: the host this tape wraps, and + // what the host wrote in the core, are said again at the next input. + tape.resync(&ps.t); + const peer = ps.gpa.create(Peer) catch { + for (blobs.items) |b| pardes.memory.unpin(b.bytes); + blobs.deinit(core.gpa); + core.gpa.free(bytes); + return daemon.refuseReplica(fd, "out of memory"); + }; + peer.* = .{ .peers = ps, .fd = fd, .id = ps.next_id, .view = k, .limit = lag_max, .dump = bytes, .blobs = blobs, .dump_gpa = core.gpa }; + ps.next_id +%= 1; + if (ps.next_id == 0) ps.next_id = 1; + // The session's digest as dumped, for the joiner to check its load by. + const digest = tape.digestOf(core); + peer.welcome_len = (wire.encodeServer(&peer.welcome, .{ .welcome = .{ .pid = @intCast(libc.getpid()), .session = ps.name[0..@min(ps.name.len, wire.max_name)], .client = peer.id, .view = k, .dump_len = whole, .gmtoff = gmtoff(), .digest = digest, .semantics = wire.semantics, .blobs = @intCast(blobs.items.len) } }) catch unreachable).len; + peer.writer = std.Thread.spawn(.{}, Peer.writeLoop, .{peer}) catch null; + peer.reader = std.Thread.spawn(.{}, Peer.readLoop, .{peer}) catch null; + if (peer.writer == null or peer.reader == null) { + ps.letGo(peer); + core.undrawn &= ~(@as(u8, 1) << @intCast(k)); + core.closeView(k); + ps.unit(0, ps.seq); + return; + } + slot.* = peer; + peer.pid = peerPid(fd); + ps.tellClients(); + // Its terminals afresh, after its dump and before any unit. + if (ps.screens.addPeer()) |sid| { + peer.sid = sid; + ps.screensTo(core, peer, .flush); + } else |_| _ = libc.shutdown(fd, 2); + log.info("client {d} joined '{s}' as view {d}: a {d} KB dump ({d} KB of state) in {d} us", .{ peer.id, ps.name, k, whole >> 10, bytes.len >> 10, (nowNs() - t0) / 1000 }); + if (std.c.getenv("PARDES_REPLICA_TRACE") != null) std.debug.print("session: client {d} joined after unit {d}\n", .{ peer.id, ps.seq }); + } + + fn leave(ps: *Peers, core: *Pardes, peer: *Peer) void { + for (&ps.list) |*slot| if (slot.* == peer) { + slot.* = null; + }; + log.info("client {d} left '{s}'", .{ peer.id, ps.name }); + ps.tellClients(); + ps.screens.dropPeer(peer.sid); + core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view)); + ps.closeViewOf(core, peer, peer.again); + if (ps.count() == 0) core.unattended = ps.detached; + ps.seq += 1; + ps.unit(0, ps.seq); + ps.letGo(peer); + if (ps.count() == 0) ps.stop(core); + } + + /// A gone client's view closed (its slot let go already), or kept for + /// it (`again`: it joins again for it, views.away): the last one of a + /// session no one else watches leaves the session its layout, for a + /// client that comes after to find. + fn closeViewOf(ps: *Peers, core: *Pardes, peer: *Peer, again: bool) void { + if (again) core.awayView(peer.view) else if (ps.count() == 0 and ps.detached) core.leaveView(peer.view) else core.closeView(peer.view); + } + + /// Its threads ended and joined, its link closed. + fn letGo(ps: *Peers, peer: *Peer) void { + const io = ps.io; + peer.lock.lockUncancelable(io); + peer.closing = true; + peer.cond.signal(io); + peer.lock.unlock(io); + _ = libc.shutdown(peer.fd, 2); + if (peer.writer) |w| w.join(); + if (peer.reader) |r| r.join(); + released(peer); + _ = libc.close(peer.fd); + peer.out.deinit(ps.gpa); + peer.sending.deinit(ps.gpa); + if (peer.want) |b| ps.gpa.free(b); + if (peer.z) |z| _ = zstd.ZSTD_freeCCtx(z); + peer.zout.deinit(ps.gpa); + // What its reader posted after it was let go (its end) is nobody's. + ps.lock.lockUncancelable(io); + var i: usize = 0; + while (i < ps.inbox.items.len) { + const mine = switch (ps.inbox.items[i]) { + .input, .screen => |in| in.peer == peer, + .gone, .check, .joined => |p| p == peer, + .join => false, + }; + if (!mine) { + i += 1; + continue; + } + switch (ps.inbox.items[i]) { + .input, .screen => |in| ps.gpa.free(in.msg), + else => {}, + } + _ = ps.inbox.orderedRemove(i); + } + ps.lock.unlock(io); + ps.gpa.destroy(peer); + } + + /// Every client told `why` and let go, its view with it: an Upgrade's + /// exec is next (tui.zig `upgradeNow`). What each was sent goes first, + /// for up to 200 ms; a client then dials the session again. + pub fn refuseAll(ps: *Peers, core: *Pardes, why: []const u8) void { + if (ps.count() == 0) return; + var out: [600]u8 = undefined; + const words = wire.encodeServer(&out, .{ .refused = why }) catch return; + const io = ps.io; + for (ps.list) |slot| if (slot) |peer| { + peer.send(&.{words}); + peer.lock.lockUncancelable(io); + peer.closing = true; + peer.cond.signal(io); + peer.lock.unlock(io); + }; + const until = nowNs() + 200 * std.time.ns_per_ms; + while (nowNs() < until) { + const busy = for (ps.list) |slot| { + if (slot) |peer| if (!peer.done.load(.acquire)) break true; + } else false; + if (!busy) break; + const ts: libc.timespec = .{ .sec = 0, .nsec = 2 * std.time.ns_per_ms }; + _ = libc.nanosleep(&ts, null); + } + // Each joins again for its view, kept in the dump (views.away). + ps.drop(core, true); + } + + /// A join's dump and pins let go: written, or its client gone. + fn released(peer: *Peer) void { + for (peer.blobs.items) |b| pardes.memory.unpin(b.bytes); + peer.blobs.deinit(peer.dump_gpa); + peer.blobs = .empty; + if (peer.dump.len > 0) peer.dump_gpa.free(peer.dump); + peer.dump = &.{}; + } + + /// The daemon told the clients there are now (`pardes --ps`). + fn tellClients(ps: *const Peers) void { + var pids: [pardes.views.max]libc.pid_t = undefined; + var n: usize = 0; + for (ps.list) |slot| if (slot) |peer| { + pids[n] = peer.pid; + n += 1; + }; + daemon.clients(pids[0..n]); + } + + /// Detach typed in client view `k`'s: its link ends (its reader says + /// it is gone, and the next boundary lets it go). + pub fn detach(ps: *Peers, k: u8) bool { + for (ps.list) |slot| if (slot) |peer| if (peer.view == k) { + _ = libc.shutdown(peer.fd, 2); + return true; + }; + return false; + } + + fn stop(ps: *Peers, core: *Pardes) void { + if (!ps.recording) return; + pardes.panes.resizeDeferred(core); + ps.screens.deinit(core); + if (core.host.vtable == &ps.t.vtable) core.host = ps.t.real; + tape.on = null; + tape.release(&ps.t); + ps.recording = false; + } + + /// Every client let go (the session's end, a Restore): each sees its + /// link end. + pub fn dropAll(ps: *Peers, core: *Pardes) void { + ps.drop(core, false); + } + + /// ...each view kept for its client if `again` (an Upgrade's). + fn drop(ps: *Peers, core: *Pardes, again: bool) void { + for (&ps.list) |*slot| if (slot.*) |peer| { + slot.* = null; + core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view)); + ps.closeViewOf(core, peer, again); + ps.screens.dropPeer(peer.sid); + ps.letGo(peer); + }; + ps.tellClients(); + ps.stop(core); + ps.lock.lockUncancelable(ps.io); + for (ps.inbox.items) |note| switch (note) { + .join => |j| _ = libc.close(j.fd), + .input, .screen => |in| ps.gpa.free(in.msg), + .gone, .check, .joined => {}, + }; + ps.inbox.clearRetainingCapacity(); + ps.lock.unlock(ps.io); + core.unattended = ps.detached; + } + + pub fn deinit(ps: *Peers) void { + if (std.c.getenv("PARDES_REPLICA_STATS")) |path| if (ps.input_ns.items.len > 0) ps.writeStats(std.mem.span(path)); + ps.inbox.deinit(ps.gpa); + ps.taken.deinit(ps.gpa); + ps.input_ns.deinit(ps.gpa); + ps.boundary_ns.deinit(ps.gpa); + ps.msgs.deinit(ps.gpa); + ps.each_join.deinit(ps.gpa); + } + + fn writeStats(ps: *Peers, path: []const u8) void { + var buf: [400]u8 = undefined; + const line = std.fmt.bufPrint(&buf, "session inputs {d} input_us {d}/{d}/{d} boundaries {d} boundary_us {d}/{d}/{d} join_us {d} dump_kb {d} state_kb {d} cut {d}\n", .{ + ps.input_ns.items.len, pctOf(ps.input_ns.items, 50) / 1000, pctOf(ps.input_ns.items, 99) / 1000, pctOf(ps.input_ns.items, 100) / 1000, + ps.boundary_ns.items.len, pctOf(ps.boundary_ns.items, 50) / 1000, pctOf(ps.boundary_ns.items, 99) / 1000, pctOf(ps.boundary_ns.items, 100) / 1000, + ps.join_ns / 1000, ps.dump_bytes >> 10, ps.state_bytes >> 10, ps.cut, + }) catch return; + var z: [4096]u8 = undefined; + const pz = std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return; + const fd = libc.open(pz, .{ .ACCMODE = .WRONLY, .CREAT = true, .APPEND = true, .CLOEXEC = true }, @as(libc.mode_t, 0o644)); + if (fd < 0) return; + _ = libc.write(fd, line.ptr, line.len); + var each: [400]u8 = undefined; + var w: std.Io.Writer = .fixed(&each); + w.writeAll("session joins_us") catch {}; + for (ps.each_join.items) |ns| w.print(" {d}", .{ns / 1000}) catch {}; + w.writeAll("\n") catch {}; + _ = libc.write(fd, w.buffered().ptr, w.buffered().len); + _ = libc.close(fd); + } +}; + +// ---- the client's half ---- + +pub const Joined = struct { + fd: c_int, + pid: u32, + client: u16, + view: u8, + gmtoff: i32, + dump: []u8, + /// The session's digest as dumped (`Replica.loaded`). + digest: u64 = 0, + /// Its build's (wire.semantics): the daemon judged the hello by what it + /// last heard, which an upgrade under way may not have said yet. + semantics: u64 = 0, + /// What was read past the dump: the stream's (`readLink`). + rest: Framer = .{}, + /// A far join's blobs that came over the link, for `Have.store` (the C + /// heap's), and how it went: its blobs, those it held already (from the + /// core it left, from disk) and the bytes of the ones it was sent. + got: []Got = &.{}, + blobs: u32 = 0, + from_memory: u32 = 0, + from_disk: u32 = 0, + sent_bytes: u64 = 0, + /// How long its dial and load took, ms (`join`). + ms: u64 = 0, +}; + +/// A blob a far join was sent: where it is in the dump, and its hash. +pub const Got = struct { pos: usize, len: usize, hash: [32]u8 }; + +/// Connected to the replica socket at `path`, its hello said for +/// `session`, welcomed, and the dump read whole: what a client joins with. +/// A far client (`have`) is sent its dump's blobs by hash first, and asks +/// for those it holds nowhere. Errors are said in `why`. `view`: the one +/// it had, joining again for it. +pub fn dial(gpa: Allocator, path: []const u8, session: []const u8, why: []u8, have: ?*Have, view: ?u8) ?Joined { + const fd = libc.socket(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0); + if (fd < 0) return said(why, "no socket", .{}); + var addr: libc.sockaddr.un = .{ .path = @splat(0) }; + if (path.len >= addr.path.len) { + _ = libc.close(fd); + return said(why, "the socket's path is too long: {s}", .{path}); + } + @memcpy(addr.path[0..path.len], path); + if (libc.connect(fd, @ptrCast(&addr), @sizeOf(libc.sockaddr.un)) != 0) { + _ = libc.close(fd); + return said(why, "nothing answers at {s}", .{path}); + } + return joinOn(gpa, fd, session, why, have, view); +} + +/// `dial`'s, on a connection made: the hello, then the join. The +/// connection is closed if the join fails. +fn joinOn(gpa: Allocator, fd: c_int, session: []const u8, why: []u8, have: ?*Have, view: ?u8) ?Joined { + var out: [wire.hello_max + 16]u8 = undefined; + const hello = wire.encodeClient(&out, .{ .hello = .{ .session = session, .view = view } }) catch { + _ = libc.close(fd); + return said(why, "that session's name is too long", .{}); + }; + // Its first word, in the same write: whether it is far. + const far = wire.encodeClient(out[hello.len..], .{ .far = have != null }) catch unreachable; + var j: Joined = .{ .fd = fd, .pid = 0, .client = 0, .view = 0, .gmtoff = 0, .dump = &.{} }; + const Read = struct { + j: *Joined, + gpa: Allocator, + why: []u8, + have: ?*Have, + failed: ?[]const u8 = null, + welcomed: bool = false, + done: bool = false, + /// The join's list of blobs (`blob_entry` each), as it comes. + list: std.ArrayListUnmanaged(u8) = .empty, + /// Where what comes fills the dump, in order (each a place and a + /// length): the state's runs between the blobs, then each blob + /// asked for. A blob listed twice is copied once the dump is whole. + runs: std.ArrayListUnmanaged([2]usize) = .empty, + run: usize = 0, + run_at: usize = 0, + dups: std.ArrayListUnmanaged([3]usize) = .empty, + + fn deinit(r: *@This()) void { + r.list.deinit(r.gpa); + r.runs.deinit(r.gpa); + r.dups.deinit(r.gpa); + } + + fn fail(r: *@This(), words: []const u8) bool { + r.failed = words; + return false; + } + + fn take(r: *@This(), tag: u8, payload: []const u8) bool { + const msg = wire.decodeServer(tag, payload) catch return r.fail("the session's answer was not understood"); + switch (msg) { + .welcome => |w| { + r.j.pid = w.pid; + r.j.client = w.client; + r.j.view = w.view; + r.j.gmtoff = w.gmtoff; + r.j.digest = w.digest; + r.j.semantics = w.semantics; + r.j.dump = r.gpa.alloc(u8, @intCast(w.dump_len)) catch return r.fail("out of memory for the session's dump"); + r.welcomed = true; + r.j.blobs = if (r.have != null) w.blobs else 0; + // A near client's dump comes whole, and a far one's + // without blobs is its state. + if (r.j.blobs == 0) r.runs.append(r.gpa, .{ 0, r.j.dump.len }) catch return r.fail("out of memory for the session's dump"); + r.done = w.dump_len == 0; + return !r.done; + }, + .refused => |words| { + r.failed = std.fmt.bufPrint(r.why, "{s}\x00", .{words}) catch "refused"; + return false; + }, + .blobs => |entries| { + const whole = @as(usize, r.j.blobs) * blob_entry; + if (r.runs.items.len > 0 or r.list.items.len + entries.len > whole) return r.fail("the session sent more of its blobs' list than it said"); + r.list.appendSlice(r.gpa, entries) catch return r.fail("out of memory for the session's blobs' list"); + if (r.list.items.len == whole) return r.lacking(); + return true; + }, + .dump => |piece| { + if (r.runs.items.len == 0) return r.fail("the session sent its dump before its blobs' list"); + var p = piece; + while (p.len > 0) { + if (r.run == r.runs.items.len) return r.fail("the session sent more dump than it said"); + const run = r.runs.items[r.run]; + const n = @min(p.len, run[1] - r.run_at); + @memcpy(r.j.dump[run[0] + r.run_at ..][0..n], p[0..n]); + r.run_at += n; + p = p[n..]; + if (r.run_at == run[1]) { + r.run += 1; + r.run_at = 0; + } + } + if (r.run < r.runs.items.len) return true; + for (r.dups.items) |d| @memcpy(r.j.dump[d[0]..][0..d[2]], r.j.dump[d[1]..][0..d[2]]); + r.done = true; + return false; + }, + else => return r.fail("the session sent a unit before its dump"), + } + } + + /// The list whole: each blob put in place from what the client + /// holds (`Have.find`), or asked for, and the runs to fill made. + fn lacking(r: *@This()) bool { + const h = r.have.?; + const dump = r.j.dump; + const n: usize = r.j.blobs; + const bits = r.gpa.alloc(u8, (n + 7) / 8) catch return r.fail("out of memory for the session's blobs' list"); + defer r.gpa.free(bits); + @memset(bits, 0); + var got: std.ArrayListUnmanaged(Got) = .empty; + defer got.deinit(std.heap.c_allocator); + var firsts: std.AutoHashMapUnmanaged([32]u8, [2]usize) = .empty; + defer firsts.deinit(r.gpa); + var shift: usize = 0; + var end: usize = 0; + for (0..n) |i| { + const e = r.list.items[i * blob_entry ..][0..blob_entry]; + const len = std.math.cast(usize, std.mem.readInt(u64, e[8..16], .little)) orelse return r.fail("the session's list of blobs is malformed"); + const pos = std.math.add(usize, std.math.cast(usize, std.mem.readInt(u64, e[0..8], .little)) orelse maxInt, shift) catch maxInt; + if (pos < end or pos > dump.len or len > dump.len - pos) return r.fail("the session's list of blobs is malformed"); + shift += len; + if (pos > end) r.runs.append(r.gpa, .{ end, pos - end }) catch return r.fail("out of memory for the session's blobs' list"); + end = pos + len; + const hash = e[16..48]; + const first = firsts.getOrPut(r.gpa, hash.*) catch return r.fail("out of memory for the session's blobs' list"); + if (first.found_existing and first.value_ptr[1] == len) { + r.dups.append(r.gpa, .{ pos, first.value_ptr[0], len }) catch return r.fail("out of memory for the session's blobs' list"); + continue; + } + first.value_ptr.* = .{ pos, len }; + if (h.find(hash, dump[pos..][0..len])) |where| switch (where) { + .memory => r.j.from_memory += 1, + .disk => r.j.from_disk += 1, + } else { + bits[i / 8] |= @as(u8, 1) << @intCast(i % 8); + got.append(std.heap.c_allocator, .{ .pos = pos, .len = len, .hash = hash.* }) catch return r.fail("out of memory for the session's blobs' list"); + r.j.sent_bytes += len; + } + } + if (dump.len > end) r.runs.append(r.gpa, .{ end, dump.len - end }) catch return r.fail("out of memory for the session's blobs' list"); + for (got.items) |g| r.runs.append(r.gpa, .{ g.pos, g.len }) catch return r.fail("out of memory for the session's blobs' list"); + const head = wire.header(@backingInt(wire.ClientTag.want), bits.len); + if (!writeAll(r.j.fd, &head) or !writeAll(r.j.fd, bits)) return r.fail("the session hung up"); + r.j.got = got.toOwnedSlice(std.heap.c_allocator) catch return r.fail("out of memory for the session's blobs' list"); + return true; + } + const maxInt = std.math.maxInt(usize); + }; + if (!writeAll(fd, out[0 .. hello.len + far.len])) { + _ = libc.close(fd); + return said(why, "the session hung up", .{}); + } + var r: Read = .{ .j = &j, .gpa = gpa, .why = why, .have = have }; + defer r.deinit(); + j.rest.run(gpa, fd, &r, Read.take); + if (r.failed != null or !r.done) { + j.rest.deinit(gpa); + if (j.dump.len > 0) gpa.free(j.dump); + std.heap.c_allocator.free(j.got); + _ = libc.close(fd); + const text = r.failed orelse "the session hung up before the dump was whole"; + if (text.ptr == why.ptr) return null; + return said(why, "{s}", .{text}); + } + return j; +} + +// ---- a far client's blobs ---- + +/// What a far client holds of a join's blobs already, by content (`dial`): +/// the cache on disk, and on a rejoin the blobs of the core it leaves. +pub const Have = struct { + io: std.Io, + cache: ?Cache = null, + /// The blobs a join found on disk: the most recently used once stored. + hits: std.ArrayListUnmanaged([32]u8) = .empty, + /// The core it leaves (`leaving`), each blob hashed once when one of its + /// length is looked for. ponytail: a scan a lookup, fine to some + /// thousands of blobs; a map by length would take over past that. + old: std.ArrayListUnmanaged([]const u8) = .empty, + old_hash: std.ArrayListUnmanaged(?[32]u8) = .empty, + /// The thread writing the last join's blobs to disk (`store`). + storing: ?std.Thread = null, + + pub fn init(io: std.Io) Have { + return .{ .io = io, .cache = Cache.open(io) }; + } + + pub fn deinit(h: *Have) void { + h.wait(); + h.left(); + h.old.deinit(std.heap.c_allocator); + h.old_hash.deinit(std.heap.c_allocator); + h.hits.deinit(std.heap.c_allocator); + } + + fn wait(h: *Have) void { + if (h.storing) |t| t.join(); + h.storing = null; + } + + /// `core`'s blobs, looked at by the next join until `left`: a rejoin + /// takes what it held, by content, before the core goes. + pub fn leaving(h: *Have, core: *const Pardes) void { + h.left(); + walk.blobs(Pardes, core, &h.old); + h.old_hash.appendNTimes(std.heap.c_allocator, null, h.old.items.len) catch h.old.clearRetainingCapacity(); + } + + pub fn left(h: *Have) void { + h.old.clearRetainingCapacity(); + h.old_hash.clearRetainingCapacity(); + } + + pub const Where = enum { memory, disk }; + + /// The blob of `hash` put in `out` (its length), from the core it + /// leaves or the cache; null when it holds it nowhere. + fn find(h: *Have, hash: *const [32]u8, out: []u8) ?Where { + for (h.old.items, h.old_hash.items) |b, *word| { + if (b.len != out.len) continue; + if (word.* == null) { + var d: [32]u8 = undefined; + std.crypto.hash.Blake3.hash(b, &d, .{}); + word.* = d; + } + if (!std.mem.eql(u8, &word.*.?, hash)) continue; + @memcpy(out, b); + return .memory; + } + if (h.cache) |*c| if (c.get(h.io, hash, out)) { + h.hits.append(std.heap.c_allocator, hash.*) catch {}; + return .disk; + }; + return null; + } + + /// The blobs a join was sent, written to the cache on a thread of their + /// own (with those it found there, made the most recently used), which + /// frees the dump (`gpa`'s) and the lists after: the first frame waits + /// for none of it. + pub fn store(h: *Have, gpa: Allocator, j: *Joined) void { + const dump = j.dump; + const got = j.got; + j.dump = &.{}; + j.got = &.{}; + h.wait(); + var hits = h.hits; + h.hits = .empty; + const Store = struct { + fn run(c: Cache, io: std.Io, a: Allocator, bytes: []u8, list: []Got, found: std.ArrayListUnmanaged([32]u8)) void { + var seen = found; + defer { + a.free(bytes); + std.heap.c_allocator.free(list); + seen.deinit(std.heap.c_allocator); + } + var blobs: std.ArrayListUnmanaged(walk.Blob) = .empty; + defer blobs.deinit(std.heap.c_allocator); + var put: u64 = 0; + for (list) |g| { + if (put + g.len > c.cap) continue; + put += g.len; + blobs.append(std.heap.c_allocator, .{ .hash = g.hash, .bytes = bytes[g.pos..][0..g.len] }) catch return; + } + c.put(io, blobs.items, seen.items); + } + }; + const c = h.cache orelse { + gpa.free(dump); + std.heap.c_allocator.free(got); + hits.deinit(std.heap.c_allocator); + return; + }; + h.storing = std.Thread.spawn(.{}, Store.run, .{ c, h.io, gpa, dump, got, hits }) catch { + Store.run(c, h.io, gpa, dump, got, hits); + return; + }; + } + + /// `core`'s blobs the cache lacks, written there: what this client made + /// since it joined (an edit's text, its undo), for its next start; then + /// the cache trimmed under its cap, at this client's end. + pub fn keep(h: *Have, core: *const Pardes) void { + h.wait(); + const c = h.cache orelse return; + var all: std.ArrayListUnmanaged([]const u8) = .empty; + defer all.deinit(std.heap.c_allocator); + walk.blobs(Pardes, core, &all); + var missing: std.ArrayListUnmanaged(walk.Blob) = .empty; + defer missing.deinit(std.heap.c_allocator); + var put: u64 = 0; + for (all.items) |b| { + var d: [32]u8 = undefined; + std.crypto.hash.Blake3.hash(b, &d, .{}); + if (put + b.len > c.cap or c.has(h.io, &d, b.len)) continue; + put += b.len; + missing.append(std.heap.c_allocator, .{ .bytes = b, .hash = d }) catch return; + } + c.put(h.io, missing.items, h.hits.items); + h.hits.clearRetainingCapacity(); + c.trim(h.io); + } +}; + +/// A far client's blobs on disk (Db.zig): cache.xitdb, by BLAKE3 hash. +pub const Cache = pardes.Db.Cache; + +fn said(why: []u8, comptime fmt: []const u8, args: anytype) ?Joined { + _ = std.fmt.bufPrint(why, fmt ++ "\x00", args) catch {}; + return null; +} + +/// A message `Replica.input` made, to the session: the event's frame (its +/// client and seq are the connection's). +pub fn sendInput(fd: c_int, msg: []const u8) bool { + return writeAll(fd, msg[head_len..]); +} + +/// A message `Replica.screen` or `screenRequest` made, to the session. +pub fn sendScreen(fd: c_int, msg: []const u8) bool { + var buf: [64]u8 = undefined; + if (wire.header_len + msg.len > buf.len) { + const head = wire.header(@backingInt(wire.ClientTag.screen_ack), msg.len); + return writeAll(fd, &head) and writeAll(fd, msg); + } + buf[0..wire.header_len].* = wire.header(@backingInt(wire.ClientTag.screen_ack), msg.len); + @memcpy(buf[wire.header_len..][0..msg.len], msg); + return writeAll(fd, buf[0 .. wire.header_len + msg.len]); +} + +/// Asks the session's digest, answered in stream order. +pub fn askCheck(fd: c_int) bool { + var out: [16]u8 = undefined; + return writeAll(fd, wire.encodeClient(&out, .check) catch unreachable); +} + +/// The client's last word: `again`, it joins again for its view at once. +pub fn bye(fd: c_int, again: bool) void { + var out: [16]u8 = undefined; + if (wire.encodeClient(&out, .{ .bye = .{ .again = again } })) |b| _ = writeAll(fd, b) else |_| {} +} + +/// What the session streams a client after its dump, as its frontend's loop +/// takes it: a unit's and a terminal message's bytes and a refusal's words +/// on the C heap, the loop's to free. +pub const Said = union(enum) { unit: []u8, screen: []u8, checked: u64, refused: []u8, blob: []u8 }; + +/// The session's stream off `fd`, each message handed to `post` until the +/// link ends (a refusal ends it too); `post` says whether it took it (one +/// it did not is freed here). +pub fn readLink(gpa: Allocator, fd: c_int, rest: *Framer, ctx: anytype, comptime post: fn (@TypeOf(ctx), Said) bool) void { + const Take = struct { + fn take(x: @TypeOf(ctx), tag: u8, payload: []const u8) bool { + const t = std.enums.fromInt(wire.ServerTag, tag) orelse return false; + const msg: Said = switch (t) { + .unit => .{ .unit = std.heap.c_allocator.dupe(u8, payload) catch return false }, + .screen => .{ .screen = std.heap.c_allocator.dupe(u8, payload) catch return false }, + .blob => .{ .blob = std.heap.c_allocator.dupe(u8, payload) catch return false }, + .check => .{ .checked = (wire.decodeServer(tag, payload) catch return false).check }, + .refused => .{ .refused = std.heap.c_allocator.dupe(u8, (wire.decodeServer(tag, payload) catch return false).refused) catch return false }, + .pong, .welcome, .dump, .blobs, .zstd => return true, + }; + if (!post(x, msg)) switch (msg) { + .unit, .screen, .refused, .blob => |b| std.heap.c_allocator.free(b), + .checked => {}, + }; + return msg != .refused; + } + }; + rest.run(gpa, fd, ctx, Take.take); + // A message past the wire's bound: said, never a hangup. + const n = rest.overlong() orelse return; + const words = std.fmt.allocPrint(std.heap.c_allocator, "the session sent a {d} MB message, over the {d} MiB bound", .{ n >> 20, wire.max_payload >> 20 }) catch return; + if (!post(ctx, .{ .refused = words })) std.heap.c_allocator.free(words); +} + +/// The words a client goes with when its session runs another build now. +const upgrade_words = "session upgraded to a newer pardes: upgrade this client too"; + +/// A replica client's join, the terminal's and the window's alike (their +/// `runClient`): the session at `path` dialled, its build checked to be this +/// one's, and its dump loaded into a replica checked against the session +/// as it was dumped. Or, `old` given, joined again once old's link has +/// ended, for old's view (`redial`): the core it leaves held meanwhile so +/// that its blobs are not sent again (`Have.leaving`), and let go before +/// the new one loads. The dump is let go, a far join's to the thread that +/// keeps the blobs it was sent (`Have.store`), and how long it all took is +/// `j.ms`. Null, and why in `why`. +pub fn join(gpa: Allocator, j: *Joined, path: []const u8, session: []const u8, o: pardes.Options, have: ?*Have, why: *[600]u8, old: ?*Replica) ?*Replica { + if (old) |prev| if (have) |h| h.leaving(prev.core()); + const t0 = nowNs(); + if (old) |prev| { + j.* = redial(gpa, path, session, why, have, prev.diverged == null, j.view) orelse { + var words: [600]u8 = undefined; + const said_ = std.mem.sliceTo(why, 0); + @memcpy(words[0..said_.len], said_); + _ = std.fmt.bufPrint(why, "could not join again: {s}\x00", .{words[0..said_.len]}) catch {}; + return null; + }; + if (have) |h| h.left(); + prev.deinit(); + } else j.* = sameBuild(gpa, dial(gpa, path, session, why, have, null) orelse return null, why) orelse return null; + defer { + if (have) |h| h.store(gpa, j) else gpa.free(j.dump); + j.dump = &.{}; + } + const r = Replica.initDump(gpa, j.client, j.view, j.dump, o) catch |err| { + _ = std.fmt.bufPrint(why, "the session's dump would not load: {t}\x00", .{err}) catch {}; + return null; + }; + // PARDES_NO_ECHO: no predicted echo (the gates' baseline). + if (std.c.getenv("PARDES_NO_ECHO") != null) r.echo.on = false; + // PARDES_REPLICA_STATS: its timing samples kept, for its line. + r.stats.samples = std.c.getenv("PARDES_REPLICA_STATS") != null; + if (!r.loaded(j.digest)) { + r.deinit(); + _ = std.fmt.bufPrint(why, "the session's dump loaded as another session (a build's dump is not complete)\x00", .{}) catch {}; + return null; + } + r.gmtoff = j.gmtoff; + // On the session's machine: a file read sent by hash is read here; + // far, it goes to the disk for the next join. + r.t.local_files = have == null; + if (have) |h| { + r.cache = h.cache; + r.io = h.io; + } + j.ms = (nowNs() - t0) / std.time.ns_per_ms; + return r; +} + +/// `j`, when its session runs this build; else its link let go, and why in +/// `why`: the daemon judged the hello by what the session last said, which +/// an upgrade under way may not have said yet. +fn sameBuild(gpa: Allocator, j: Joined, why: []u8) ?Joined { + if (j.semantics == wire.semantics) return j; + bye(j.fd, false); + _ = libc.close(j.fd); + var jj = j; + jj.rest.deinit(gpa); + gpa.free(j.dump); + _ = std.fmt.bufPrint(why, "{s}\x00", .{upgrade_words}) catch {}; + return null; +} + +/// The session again, after a divergence or an upgrade: an upgrade's +/// takes the time its exec and load take, and its daemon's, so it is +/// dialled every 100 ms for 20 s. Null, and why in `why`. +/// `patient`: after an Upgrade's `refused` alone, while the session is +/// away a moment; after a divergence a session that will not answer is +/// gone, and the client says so at once (it froze 20 s, 2026-10-30). +/// `view`: the one it had, which the session kept for it (views.away). +fn redial(gpa: Allocator, path: []const u8, session: []const u8, why: *[600]u8, have: ?*Have, patient: bool, view: u8) ?Joined { + var tries: u32 = 0; + while (true) : (tries += 1) { + why.* = @splat(0); + if (dial(gpa, path, session, why, have, view)) |j| return sameBuild(gpa, j, why); + if (std.mem.indexOf(u8, std.mem.sliceTo(why, 0), "runs another build") != null) { + _ = std.fmt.bufPrint(why, "{s}\x00", .{upgrade_words}) catch {}; + return null; + } + if (!patient or tries > 200) return null; + const ts: libc.timespec = .{ .sec = 0, .nsec = 100 * std.time.ns_per_ms }; + _ = libc.nanosleep(&ts, null); + } +} + +/// A replica client's link to its session, the terminal's (tui.zig +/// `runClient`) and the window's (gui.zig `runClient`): its replica, its socket, +/// what the session said, and what it checks. Drawing is the frontend's. +pub const Link = struct { + r: *Replica, + fd: c_int, + quit: bool = false, + /// The session hung up. + gone: bool = false, + /// Something to draw: an input applied, a unit taken. + dirty: bool = true, + /// The last frame was held for the frontend's answer (a terminal's + /// FrameAck, a window's skipped image). + owed: bool = false, + /// Once quiet a while after a change, the session's digest is asked + /// (`quietCheck`): another than this replica's is a divergence, and it + /// joins again. PARDES_REPLICA_CHECK=<file>: whether they agree is + /// written there too, a line a check. + check: ?[:0]const u8 = null, + changed: bool = false, + asked: bool = false, + /// When its own last input was, and its last check asked (`quietCheck` + /// waits a while after each). + input_ns: u64 = 0, + asked_ns: u64 = 0, + /// How many of its own inputs the session recorded otherwise when it + /// last drew: a frame after one that draws something is a rollback that + /// may have been seen (counted so: an upper bound, the frame shows the + /// inputs after it too). + mispredicted: u32 = 0, + visible: u32 = 0, + /// What the session said as it let this client go (the C heap's). + refused: ?[]u8 = null, + /// A line over its frame, until its next key (`notify`): why it joined + /// again. The client's own, never the session's core. + notice_buf: [240]u8 = undefined, + notice_len: usize = 0, + /// PARDES_TEST_DIVERGE=<n>: once quiet after its n-th unit, its core + /// is put off the session's (a digested counter moved), the next unit's + /// digest finds it, and it joins again (test/daemon.py). + test_diverge: u32 = 0, + /// Put off so in this join: its checks are not written (they differ). + put_off: bool = false, + /// How its last join went (Joined's counts), and how long its dial and + /// load took (PARDES_REPLICA_STATS). + join: struct { blobs: u32 = 0, from_memory: u32 = 0, from_disk: u32 = 0, sent_bytes: u64 = 0 } = .{}, + join_ms: u64 = 0, + /// Frames it drew whole (`drawn`), and the last one presented again + /// moved on (its own motion), for PARDES_REPLICA_STATS. + draws: u32 = 0, + again: u32 = 0, + + /// The test knobs the environment sets (PARDES_REPLICA_CHECK, + /// PARDES_TEST_DIVERGE). + pub fn fromEnv(c: *Link, gpa: Allocator, env: *const std.process.Environ.Map) !void { + if (env.get("PARDES_REPLICA_CHECK")) |file| c.check = try gpa.dupeSentinel(u8, file, 0); + if (env.get("PARDES_TEST_DIVERGE")) |n| c.test_diverge = std.fmt.parseInt(u32, n, 10) catch 0; + } + + pub fn deinit(c: *Link, gpa: Allocator) void { + if (c.check) |file| gpa.free(file); + if (c.refused) |words| std.heap.c_allocator.free(words); + } + + /// Join `j`'s link, from its start, and how that join went. + pub fn reset(c: *Link, r: *Replica, j: *const Joined) void { + c.r = r; + c.fd = j.fd; + c.join = .{ .blobs = j.blobs, .from_memory = j.from_memory, .from_disk = j.from_disk, .sent_bytes = j.sent_bytes }; + c.join_ms = j.ms; + c.quit = false; + c.gone = false; + c.dirty = true; + c.owed = false; + c.changed = false; + c.asked = false; + c.mispredicted = 0; + c.put_off = false; + } + + pub fn notify(c: *Link, comptime fmt: []const u8, args: anytype) void { + const text = std.fmt.bufPrint(&c.notice_buf, fmt, args) catch c.notice_buf[0..]; + for (text) |*b| if (b.* < 0x20 or b.* > 0x7e) { + b.* = '?'; + }; + c.notice_len = text.len; + } + + pub fn notice(c: *const Link) []const u8 { + return c.notice_buf[0..c.notice_len]; + } + + /// The frontend's input: applied at once, and sent to the session. + pub fn input(c: *Link, ev: pardes.Event) void { + // Its notice read: a key of its own puts it away. + if (c.notice_len > 0 and (ev == .key or ev == .paste)) c.notice_len = 0; + c.input_ns = nowNs(); + const msg = c.r.input(ev) catch return; + if (!sendInput(c.fd, msg)) c.cut(); + c.dirty = true; + } + + /// What the session streamed, in its order; the bytes are let go. + pub fn take(c: *Link, msg: Said) void { + switch (msg) { + .unit => |bytes| { + defer std.heap.c_allocator.free(bytes); + c.r.confirm(bytes); + if (c.test_diverge != 0 and c.r.stats.units >= c.test_diverge and c.r.quiet()) { + c.test_diverge = 0; + c.put_off = true; + c.r.core().next_serial +%= 1000; + } + if (c.r.moved) c.dirty = true; + c.r.moved = false; + c.changed = true; + }, + .screen => |bytes| { + defer std.heap.c_allocator.free(bytes); + if (c.r.screen(bytes)) |reply| if (!sendScreen(c.fd, reply)) c.cut(); + c.dirty = true; + }, + .blob => |bytes| { + defer std.heap.c_allocator.free(bytes); + c.r.blobPiece(bytes); + }, + .checked => |d| c.checked(d), + .refused => |words| { + if (c.refused) |old| std.heap.c_allocator.free(old); + c.refused = words; + c.quit = true; + }, + } + if (c.r.diverged != null) c.quit = true; + } + + /// Its own view drawn, by the frontend (`drew` is whether anything was + /// written to it): a rollback it may have shown counted, and the + /// history rows it shows and does not hold (a scroll back) asked for, + /// blank until they come. + pub fn drawn(c: *Link, drew: bool) void { + if (c.r.stats.mispredicted > c.mispredicted) { + c.mispredicted = c.r.stats.mispredicted; + if (drew) c.visible += 1; + } + c.dirty = false; + c.draws += 1; + // What moved before this frame is in it: not drawn again at the + // next unit (a stopped input's rollback, under its own frame). + c.r.moved = false; + while (c.r.screenRequest()) |req| if (!sendScreen(c.fd, req)) { + c.cut(); + break; + }; + } + + /// A write to the session failed: it hung up (it cut this client off, + /// lagging), as its reader will say. + fn cut(c: *Link) void { + c.quit = true; + c.gone = true; + } + + /// Each pass of the frontend's loop: quiet (nothing of its own + /// pending) 300 ms after its last input and its last check, and + /// changed since, the session's digest is asked once, answered in + /// stream order after the units before it (a session that pumps on, a + /// window's test feed, sends a unit a frame; the check's own wakes it + /// once). The wake it wants, ms. + pub fn quietCheck(c: *Link) ?u32 { + if (!c.changed or c.asked or !c.r.quiet()) return null; + const quiet_ns = 300 * std.time.ns_per_ms; + const since = nowNs() -| @max(c.input_ns, c.asked_ns); + if (since < quiet_ns) return @intCast((quiet_ns - since) / std.time.ns_per_ms + 1); + c.changed = false; + c.asked = askCheck(c.fd); + if (!c.asked) c.cut(); + c.asked_ns = nowNs(); + return null; + } + + /// The session's digest against this replica's, if it is quiet still: + /// another is a divergence (the replica joins again). + fn checked(c: *Link, theirs: u64) void { + c.asked = false; + if (!c.r.quiet() or c.r.diverged != null) return; + const mine = c.r.digest(); + c.r.stats.checks += 1; + c.r.since_check = 0; + if (mine != theirs) { + c.r.fail("its digest, quiet, is not the session's"); + c.quit = true; + } + const path = c.check orelse return; + if (c.put_off) return; + var buf: [96]u8 = undefined; + const line = (if (mine == theirs) std.fmt.bufPrint(&buf, "agree {x:0>16}\n", .{mine}) else std.fmt.bufPrint(&buf, "differ {x:0>16} {x:0>16}\n", .{ mine, theirs })) catch return; + appendTo(path, line); + } + + /// PARDES_REPLICA_STATS=<file>: how its inputs and the session's units + /// went, a line at its end (test/daemon.py, the gates). + pub fn writeStats(c: *Link, path: []const u8) void { + const st = &c.r.stats; + var buf: [1024]u8 = undefined; + const e = &c.r.echo.stats; + var w: std.Io.Writer = .fixed(&buf); + w.print("inputs {d} at_once {d} stopped {d} units {d} fast {d} neutral {d} mispredicted {d} foreign {d} visible {d} rtt_us {d} spec_us {d}/{d} copy_us {d}/{d} rollback_us {d}/{d} join_ms {d} blobs {d} from_memory {d} from_disk {d} sent_kb {d} echo {d}/{d} confirmed {d} missed {d} draws {d} again {d}", .{ + st.inputs, st.speculated, st.stopped, st.units, st.fast, + c.r.neutral, st.mispredicted, st.foreign, c.visible, c.r.rtt_ns / 1000, + pctOf(st.spec_ns.items, 50) / 1000, pctOf(st.spec_ns.items, 99) / 1000, pctOf(st.copy_ns.items, 50) / 1000, pctOf(st.copy_ns.items, 99) / 1000, pctOf(st.rollback_ns.items, 50) / 1000, + pctOf(st.rollback_ns.items, 99) / 1000, c.join_ms, c.join.blobs, c.join.from_memory, c.join.from_disk, + c.join.sent_bytes >> 10, e.predicted, e.predicted + e.unpredicted, e.confirmed, e.mispredicted, + c.draws, c.again, + }) catch return; + // What stopped its inputs, last: a word a pair before it. + w.writeAll(" stops ") catch return; + st.writeStops(&w) catch return; + w.writeByte('\n') catch return; + var z: [4096]u8 = undefined; + appendTo(std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return, w.buffered()); + } + + /// The link ended: whether the client joins again, and why in its + /// notice (it diverged, or the session let it go to upgrade). + pub fn rejoins(c: *Link) bool { + if (c.r.diverged) |reason| { + c.notify("joined again: this client diverged from the session ({s})", .{reason}); + return true; + } + if (c.refused) |words| { + c.notify("joined again: {s}", .{words}); + std.heap.c_allocator.free(words); + c.refused = null; + return true; + } + return false; + } +}; + +fn appendTo(path: [:0]const u8, line: []const u8) void { + const fd = libc.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .APPEND = true, .CLOEXEC = true }, @as(libc.mode_t, 0o644)); + if (fd < 0) return; + _ = libc.write(fd, line.ptr, line.len); + _ = libc.close(fd); +} + +pub fn digestOf(a: *Authority) u64 { + const was = Stepping.begin(&a.t, null); + defer was.end(); + return tape.digestOf(a.core); +} + +const TestHost = struct { + fn watched(_: ?*anyopaque) bool { + return false; + } + const vtable: pardes.Host.VTable = .{ .watched = watched }; + fn host() pardes.Host { + return .{ .vtable = &vtable }; + } +}; + +/// A host with ptys whose pump polls for a frame, as a client's does. +const PtyHost = struct { + fn resized(_: ?*anyopaque, _: u8, _: u16, _: u16) void {} + fn poll(_: ?*anyopaque) void {} + const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .pty_resize = resized, .poll_frame = poll }; + fn host() pardes.Host { + return .{ .vtable = &vtable }; + } +}; + +/// A kind as a library brings one, its state not plain (no PA_PLAIN): a +/// counter its keys move, painted as `count N`. +const Counter = struct { + const pa = pardes.Kind.c; + fn open(_: [*c]const u8, _: [*c]const u8, _: u64) callconv(.c) ?*anyopaque { + return pardes.Kind.api.alloc.?(4); + } + fn paint(st: ?*anyopaque, win: [*c]const pa.pa_window, _: [*c]pa.pa_paint) callconv(.c) void { + const n: *u32 = @ptrCast(@alignCast(st.?)); + var line: [16]u8 = undefined; + const text = std.fmt.bufPrint(&line, "count {d}", .{n.*}) catch unreachable; + for (text, 0..) |ch, i| win.*.cells[i] = std.mem.zeroInit(pa.pa_cell, .{ .text = .{ ch, 0, 0, 0, 0, 0, 0, 0 }, .len = 1, .flags = pa.PA_PAINTED, .fg = pa.PA_DEFAULT, .bg = pa.PA_DEFAULT }); + } + fn press(st: ?*anyopaque, _: [*c]const pa.pa_key) callconv(.c) c_int { + const n: *u32 = @ptrCast(@alignCast(st.?)); + n.* += 1; + return 1; + } + const words = [_]pa.pa_word{std.mem.zeroInit(pa.pa_word, .{ .name = "Counter", .opens = 1, .doc = "a counter" })}; + const ops = std.mem.zeroInit(pardes.Kind.Ops, .{ + .head = std.mem.zeroInit(pa.pa_head, .{ .size = @sizeOf(pa.pa_head), .abi = pardes.Kind.abi, .name = "counter".* ++ @as([9]u8, @splat(0)), .version = 1, .exts = "", .words = &words, .nwords = 1 }), + .open = open, + .paint = paint, + .key = press, + }); +}; + +/// An edit's file time is the clock's second, which a replica guesses: a +/// second's turn between a key and the authority's take of it is a fair +/// misprediction (11 runs in 40 of a loaded Debug window build's test +/// below). The tests that count fast confirmations start early in a second. +fn freshSecond() void { + while (true) { + var ts: std.c.timespec = undefined; + if (std.c.clock_gettime(.REALTIME, &ts) != 0 or ts.nsec < 50 * std.time.ns_per_ms) return; + const rest: std.c.timespec = .{ .sec = 0, .nsec = std.time.ns_per_s - ts.nsec }; + _ = std.c.nanosleep(&rest, null); + } +} + +fn key(r: *Replica, cp: u21, sent: *std.ArrayListUnmanaged([]u8)) !void { + const text = [_]u8{@intCast(cp & 0x7f)}; + const msg = try r.input(.{ .key = .{ .cp = cp, .text = if (cp < 0x80 and cp >= 0x20) &text else "" } }); + try sent.append(std.testing.allocator, try std.testing.allocator.dupe(u8, msg)); +} + +/// What the session sends a peer: its terminals' changes (`.flush`), or, +/// around a step's unit (Peers.input), the rows the step read that the +/// peer lacks (`.fill`), after which the caller gives terminals the sizes +/// the step gave them (`panes.resizeDeferred`), then the unit. +fn deliver(a: *Authority, screens: *Screens, peer: u16, r: *Replica, comptime what: enum { flush, fill }) !void { + const gpa = std.testing.allocator; + var msgs: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (msgs.items) |m| gpa.free(m); + msgs.deinit(gpa); + } + switch (what) { + .flush => try screens.flush(a.core, peer, r.view, &msgs), + .fill => { + try screens.fill(a.core, peer, &msgs); + screens.filled(a.core); + }, + } + for (msgs.items) |m| if (r.screen(m)) |reply| { + _ = try screens.take(a.core, peer, reply); + }; +} + +/// The blob cache of a test, in its own directory (never the user's). +fn testCache(tmp: *std.testing.TmpDir, buf: *[4200]u8, cap: u64) !Cache { + var dir_buf: [std.Io.Dir.max_path_bytes]u8 = undefined; + const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; + return Cache.of(try std.fmt.bufPrint(buf, "{s}/" ++ Cache.name, .{dir}), cap); +} + +/// A far join over a socket pair, the session's writer and reader threads +/// against the client's `joinOn`, as the daemon hands a connection over: +/// its hello read off first. +fn farJoinOnce(ps: *Peers, core: *Pardes, have: ?*Have) !Joined { + var fds: [2]c_int = undefined; + if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &fds) != 0) return error.NoSocket; + var blobs: std.ArrayListUnmanaged(walk.Deferred) = .empty; + const state = pardes.dump.dumpJoin(core, &blobs) catch |err| { + _ = libc.close(fds[0]); + _ = libc.close(fds[1]); + return err; + }; + var whole = state.len; + for (blobs.items) |b| whole += b.bytes.len; + const peer = try ps.gpa.create(Peer); + peer.* = .{ .peers = ps, .fd = fds[0], .id = 1, .view = 0, .dump = state, .blobs = blobs, .dump_gpa = core.gpa }; + peer.welcome_len = (wire.encodeServer(&peer.welcome, .{ .welcome = .{ .pid = 1, .session = "t", .dump_len = whole, .blobs = @intCast(blobs.items.len) } }) catch unreachable).len; + const Serve = struct { + fn run(p: *Peer) void { + var hello: [wire.header_len + 14]u8 = undefined; + var got: usize = 0; + while (got < hello.len) { + const n = libc.read(p.fd, hello[got..].ptr, hello.len - got); + if (n <= 0) return p.peers.post(.{ .gone = p }); + got += @intCast(n); + } + p.readLoop(); + } + }; + peer.writer = try std.Thread.spawn(.{}, Peer.writeLoop, .{peer}); + peer.reader = try std.Thread.spawn(.{}, Serve.run, .{peer}); + defer ps.letGo(peer); + var why: [300]u8 = @splat(0); + return joinOn(std.testing.allocator, fds[1], "t", &why, have, null) orelse { + std.debug.print("the join failed: {s}\n", .{std.mem.sliceTo(&why, 0)}); + return error.JoinFailed; + }; +} + +fn endJoin(j: *Joined, have: ?*Have) void { + _ = libc.close(j.fd); + j.rest.deinit(std.testing.allocator); + if (have) |h| { + h.store(std.testing.allocator, j); + h.wait(); + } else { + std.testing.allocator.free(j.dump); + std.heap.c_allocator.free(j.got); + } +} + +/// A replica joined from the session's dump as it is now, between units, +/// as `Peers.join` sends it: its state, its blobs put back where they go. +fn joinedNow(a: *Authority, id: u16, view: u8) !*Replica { + const gpa = std.testing.allocator; + const was = Stepping.begin(&a.t, null); + var blobs: std.ArrayListUnmanaged(walk.Deferred) = .empty; + defer blobs.deinit(a.core.gpa); + const state = try pardes.dump.dumpJoin(a.core, &blobs); + defer a.core.gpa.free(state); + tape.resync(&a.t); + was.end(); + var whole: std.ArrayListUnmanaged(u8) = .empty; + defer whole.deinit(gpa); + var pos: usize = 0; + for (blobs.items) |b| { + try whole.appendSlice(gpa, state[pos..b.at]); + try whole.appendSlice(gpa, b.bytes); + pos = b.at; + } + try whole.appendSlice(gpa, state[pos..]); + return Replica.initDump(gpa, id, view, whole.items, .{ .cols = 80, .rows = 24 }); +} + +fn agrees(a: *Authority, r: *Replica) !void { + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null); + try std.testing.expect(r.quiet()); + try std.testing.expectEqual(digestOf(a), r.digest()); +} + +fn inputOf(buf: []u8, client: u16, seq: u32, ev: pardes.Event) ![]const u8 { + const n = (try wire.encodeClient(buf[head_len..], .{ .event = ev })).len; + header(buf, client, seq); + return buf[0 .. head_len + n]; +} + +/// An input of a replica's in the session's order, as `Peers.input` takes +/// it: the terminals flushed to every peer, the step, the rows it read that +/// each peer holds as holes (`fill`), then its unit to every replica. +fn takeAll(a: *Authority, screens: *Screens, rs: []const *Replica, ps: []const u16, msg: []const u8) !void { + const gpa = std.testing.allocator; + for (rs, ps) |r, peer| try deliver(a, screens, peer, r, .flush); + const u = try gpa.dupe(u8, try a.take(msg)); + defer gpa.free(u); + var rows: std.ArrayListUnmanaged([]u8) = .empty; + defer rows.deinit(gpa); + for (rs, ps) |r, peer| { + rows.clearRetainingCapacity(); + try screens.fill(a.core, peer, &rows); + for (rows.items) |m| { + _ = r.screen(m); + gpa.free(m); + } + } + screens.filled(a.core); + for (rs) |r| { + r.confirm(u); + r.settle(); + } +} + +fn sendAs(r: *Replica, a: *Authority, screens: *Screens, rs: []const *Replica, ps: []const u16, ev: pardes.Event) !void { + const msg = try std.testing.allocator.dupe(u8, try r.input(ev)); + defer std.testing.allocator.free(msg); + try takeAll(a, screens, rs, ps, msg); +} + +fn shows(p: *Pardes, text: []const u8) !bool { + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const s = try p.render(arena.allocator()); + var row: [512]u8 = undefined; + for (0..s.rows) |y| { + var n: usize = 0; + for (0..s.cols) |x| { + const g = s.at(@intCast(x), @intCast(y)).grapheme(); + if (n + g.len > row.len) break; + @memcpy(row[n..][0..g.len], g); + n += g.len; + } + if (std.mem.indexOf(u8, row[0..n], text) != null) return true; + } + return false; +} + +test "a message past the wire's bound is named, never read as the link's end" { + var fds: [2]c_int = undefined; + try std.testing.expectEqual(0, libc.pipe(&fds)); + const head = wire.header(@backingInt(wire.ServerTag.unit), wire.max_payload + 1); + _ = libc.write(fds[1], &head, head.len); + _ = libc.close(fds[1]); + var fr: Framer = .{}; + defer fr.deinit(std.testing.allocator); + fr.run(std.testing.allocator, fds[0], {}, struct { + fn take(_: void, _: u8, _: []const u8) bool { + return true; + } + }.take); + _ = libc.close(fds[0]); + try std.testing.expectEqual(wire.max_payload + 1, fr.overlong().?); +} + +test "a client joining again after a divergence is told at once that its session is gone" { + var why: [600]u8 = undefined; + const t0 = nowNs(); + try std.testing.expect(redial(std.testing.allocator, "/nonexistent/pardes-replica.sock", "gone", &why, null, false, 1) == null); + try std.testing.expect(nowNs() - t0 < 100 * std.time.ns_per_ms); +} + +test "a replica's own keys apply at once, confirm on the fast path, and end at the authority's digest" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), false); + defer r.deinit(); + try std.testing.expectEqual(digestOf(a), r.digest()); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + freshSecond(); + for ("oabc") |k| try key(r, k, &sent); + try key(r, pardes.Key.escape, &sent); + try std.testing.expectEqual(@as(usize, 5), r.applied); + for (sent.items) |m| { + r.confirm(try a.take(m)); + r.settle(); + } + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null); + try std.testing.expectEqual(@as(u32, 5), r.stats.fast); + try std.testing.expect(r.quiet()); + try std.testing.expectEqual(digestOf(a), r.digest()); +} + +test "an input's time is its client's: an edit's second confirms on the fast path though the authority takes it in the next" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), false); + defer r.deinit(); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + try key(r, 'o', &sent); + for (sent.items) |m| { + r.confirm(try a.take(m)); + r.settle(); + } + // Typed 30 ms before a second's turn, taken 30 ms after it: the edit's + // file time is the clock's second, read in the step. + while (true) { + var ts: std.c.timespec = undefined; + _ = std.c.clock_gettime(.REALTIME, &ts); + if (ts.nsec >= 970 * std.time.ns_per_ms) break; + const rest: std.c.timespec = .{ .sec = 0, .nsec = 970 * std.time.ns_per_ms - ts.nsec }; + _ = std.c.nanosleep(&rest, null); + } + try key(r, 'x', &sent); + try std.testing.expectEqual(@as(usize, 1), r.applied); + const late: std.c.timespec = .{ .sec = 0, .nsec = 60 * std.time.ns_per_ms }; + _ = std.c.nanosleep(&late, null); + r.confirm(try a.take(sent.items[1])); + r.settle(); + try std.testing.expect(r.diverged == null and r.quiet()); + try std.testing.expectEqual(@as(u32, 2), r.stats.fast); + try std.testing.expectEqual(@as(u32, 0), r.stats.mispredicted); + try std.testing.expectEqual(digestOf(a), r.digest()); +} + +test "another client's input first: a replica rolls back, replays it and applies its own again" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const first = try gpa.dupe(u8, try a.unit(0, 0)); + defer gpa.free(first); + const r1 = try Replica.init(gpa, 1, first, false); + defer r1.deinit(); + const r2 = try Replica.init(gpa, 2, first, false); + defer r2.deinit(); + var s1: std.ArrayListUnmanaged([]u8) = .empty; + var s2: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (s1.items) |m| gpa.free(m); + for (s2.items) |m| gpa.free(m); + s1.deinit(gpa); + s2.deinit(gpa); + } + freshSecond(); + for ("oone") |k| try key(r1, k, &s1); + for ("Otwo") |k| try key(r2, k, &s2); + // The authority hears r2 first, then r1; both hear the units in that + // order. + for ([_]*std.ArrayListUnmanaged([]u8){ &s2, &s1 }) |list| for (list.items) |m| { + const u = try gpa.dupe(u8, try a.take(m)); + defer gpa.free(u); + r1.confirm(u); + r1.settle(); + r2.confirm(u); + r2.settle(); + }; + for ([_]*Replica{ r1, r2 }) |r| { + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null); + try std.testing.expect(r.quiet()); + try std.testing.expectEqual(digestOf(a), r.digest()); + } + try std.testing.expect(r1.stats.foreign >= 1); + try std.testing.expectEqual(@as(u32, 4), r2.stats.fast); +} + +test "the copy taken for an input of its own stays once the input is confirmed: the next takes none, and a rollback replays the first over it" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const first = try gpa.dupe(u8, try a.unit(0, 0)); + defer gpa.free(first); + const r = try Replica.init(gpa, 1, first, false); + defer r.deinit(); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + freshSecond(); + try key(r, 'j', &sent); + const copies = r.stats.copies; + r.confirm(try a.take(sent.items[0])); + try std.testing.expect(r.copy.held); + try std.testing.expectEqual(@as(usize, 1), r.since.items.len); + try key(r, 'j', &sent); + try std.testing.expectEqual(copies, r.stats.copies); + // The session's own key first: back to the copy, the first replayed. + r.confirm(try a.local(.{ .key = .{ .cp = 'l', .text = "l" } })); + r.settle(); + r.confirm(try a.take(sent.items[1])); + r.settle(); + try agrees(a, r); + try std.testing.expectEqual(@as(u32, 1), r.stats.foreign); +} + +test "a rollback over a Tab it applied puts the process's tab width back as its core has it" { + // config.tab_now is the process's, written by the step that set the + // width (the soak, five clients: a Tab undone left it, and the next + // replayed step measured the files' tabs otherwise). + const gpa = std.testing.allocator; + defer pardes.config.tab_now = pardes.config.tab_width; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const first = try gpa.dupe(u8, try a.unit(0, 0)); + defer gpa.free(first); + const r = try Replica.init(gpa, 1, first, false); + defer r.deinit(); + pardes.config.tab_now = r.core().settings.tab; + const was = r.core().settings.tab; + const msg = try gpa.dupe(u8, try r.input(.{ .command = "Tab 2" })); + defer gpa.free(msg); + try std.testing.expectEqual(@as(usize, 1), r.applied); + try std.testing.expectEqual(@as(u16, 2), pardes.config.tab_now); + // The session's own key first: back to the confirmed position. + r.confirm(try a.local(.{ .key = .{ .cp = 'l', .text = "l" } })); + try std.testing.expectEqual(was, r.core().settings.tab); + try std.testing.expectEqual(was, pardes.config.tab_now); +} + +test "a delta applied under a speculation outlives its rollback: the terminal stays the authority's newest" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host(), true); + defer a.deinit(); + var screens: Screens = .init(gpa); + defer screens.deinit(a.core); + const peer = try screens.addPeer(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), true); + defer r.deinit(); + const shell: u8 = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + // Typing goes to the text pane under the shell (the host's write of + // `active`, taped). + a.core.view().active = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.file != null) break id; + } else return error.NoTextPane; + r.confirm(try a.local(.{ .focus = true })); + try deliver(a, &screens, peer, r, .flush); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + // Typed into the file, at once: a copy is held. + try key(r, 'o', &sent); + try std.testing.expect(r.copy.held); + var line: [64]u8 = undefined; + for (0..3) |round| { + // The shell prints: its unit rolls the replica back and applies the + // key again; then its delta lands, under the new copy. + for (0..40) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "round {d} line {d}\r\n", .{ round, i }) } }); + r.confirm((try a.pending()).?); + r.settle(); + try std.testing.expect(r.copy.held); + try deliver(a, &screens, peer, r, .flush); + // The rows the core keeps, read once under the copy. + _ = try pardes.edit.shellRows(r.core(), r.core().panes[shell].?); + } + for (sent.items) |m| { + r.confirm(try a.take(m)); + r.settle(); + gpa.free(m); + } + sent.clearRetainingCapacity(); + try deliver(a, &screens, peer, r, .flush); + // Drawn (its rows read, between steps), then typed into: the copy keeps + // those rows. The shell prints, its delta lands under the copy, and then + // another's unit puts the copy back: the rows it kept are of an older + // grid and must not be read. + r.confirm(try a.local(.{ .focus = false })); + _ = try pardes.edit.shellRows(r.core(), r.core().panes[shell].?); + try key(r, 'x', &sent); + for (0..40) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "last line {d}\r\n", .{i}) } }); + r.confirm((try a.pending()).?); + r.settle(); + try deliver(a, &screens, peer, r, .flush); + r.confirm(try a.local(.{ .focus = true })); + r.settle(); + const n = a.core.panes[shell].?.rows; + const back = try pardes.edit.shellRows(r.core(), r.core().panes[shell].?); + const now = try pardes.edit.shellRows(a.core, a.core.panes[shell].?); + for (now[now.len - n ..], back[back.len - n ..]) |x, y| try std.testing.expectEqualStrings(x, y); + for (sent.items) |m| { + r.confirm(try a.take(m)); + r.settle(); + } + try deliver(a, &screens, peer, r, .flush); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null and r.quiet()); + try std.testing.expectEqual(digestOf(a), r.digest()); + var dump_a: std.Io.Writer.Allocating = .init(gpa); + defer dump_a.deinit(); + var dump_r: std.Io.Writer.Allocating = .init(gpa); + defer dump_r.deinit(); + try Term.dumpScreen(a.core.panes[shell].?.terminal.?, &dump_a.writer); + try Term.dumpScreen(r.core().panes[shell].?.terminal.?, &dump_r.writer); + try std.testing.expectEqualStrings(dump_a.written(), dump_r.written()); + const rows_a = try pardes.edit.shellRows(a.core, a.core.panes[shell].?); + const rows_r = try pardes.edit.shellRows(r.core(), r.core().panes[shell].?); + // As many rows (the history is the authority's to the row), the screen's + // the same; above it, rows scrolled past more than a screen at once are + // holes until fetched. + try std.testing.expectEqual(rows_a.len, rows_r.len); + const shown = a.core.panes[shell].?.rows; + for (rows_a[rows_a.len - shown ..], rows_r[rows_r.len - shown ..]) |x, y| try std.testing.expectEqualStrings(x, y); +} + +test "a closed terminal brought back on a screen-state replica: its emulator on the C heap, never the rollback epoch's" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host(), true); + defer a.deinit(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), true); + defer r.deinit(); + const shell: u8 = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + a.core.view().active = shell; + r.confirm(try a.local(.{ .command = "Del" })); + try std.testing.expectEqual(@as(usize, 1), a.core.store.closed.items.len); + var line: [32]u8 = undefined; + r.confirm(try a.local(.{ .command = try std.fmt.bufPrint(&line, "Look @p{d}", .{a.core.store.closed.items[0].serial}) })); + try std.testing.expect(r.diverged == null); + const back = for (r.core().panes) |slot| { + if (slot) |pane| if (pane.terminal) |state| break state; + } else return error.NotBack; + // Its deltas write it while a copy is held, and its screen replica's + // blobs are freed with the C heap's (`deinit`): the epoch's, a rollback + // freed what they held and the exit freed them behind its back (H21). + try std.testing.expect(r.epoch.owns(@intFromPtr(back)) == null); +} + +test "a view that sized a terminal closing leaves nothing queued: the others' replicas copy, type and agree" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 100, .rows = 30, .boot = .tty }, TestHost.host(), true); + defer a.deinit(); + const first = try gpa.dupe(u8, try a.unit(0, 0)); + defer gpa.free(first); + const r1 = try Replica.init(gpa, 1, first, true); + defer r1.deinit(); + const r2 = try Replica.init(gpa, 2, first, true); + defer r2.deinit(); + a.core.view().active = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break id; + } else return error.NoShell; + const opened = try a.openView(2); + r2.view = opened.view; + for ([_]*Replica{ r1, r2 }) |r| r.confirm(opened.unit); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + // The second client, at another size, types into the terminal: its + // view sizes it. Then it goes (H10, 2026-10-30: the first aborted). + try sent.append(gpa, try gpa.dupe(u8, try r2.input(.{ .resize = .{ .cols = 50, .rows = 20 } }))); + try key(r2, 'x', &sent); + for (sent.items) |m| { + const u = try gpa.dupe(u8, try a.take(m)); + defer gpa.free(u); + r1.confirm(u); + r1.settle(); + } + r1.confirm(try a.closeView(2)); + try std.testing.expectEqual(@as(usize, 0), a.core.effects_len); + try std.testing.expectEqual(@as(usize, 0), r1.core().effects_len); + r1.ready(); + try std.testing.expect(r1.copy.held); + for (sent.items) |m| gpa.free(m); + sent.clearRetainingCapacity(); + try key(r1, 'y', &sent); + r1.confirm(try a.take(sent.items[0])); + r1.settle(); + if (r1.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r1.diverged == null and r1.quiet()); + try std.testing.expectEqual(digestOf(a), r1.digest()); +} + +test "a terminal sized again by a client's key: the step reads the grid it had, on both sides" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 50, .rows = 20, .boot = .tty }, TestHost.host(), true); + defer a.deinit(); + var screens: Screens = .init(gpa); + defer screens.deinit(a.core); + const peer = try screens.addPeer(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), true); + defer r.deinit(); + const shell: u8 = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + a.core.view().active = shell; + // History the replica holds as holes, of lines a narrow grid wraps in + // two, which a wider one unwraps. + var line: [96]u8 = undefined; + for (0..60) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "{d:0>4} xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n", .{i}) } }); + r.confirm((try a.pending()).?); + // Out of terminal mode in the session's view, which sizes it; then a + // client's own view (a copy of it), wider: its first key sizes the + // terminal again, in that key's step (the soak's tty and window at two + // sizes, 2026-10-30: the client's replay read holes the session's step + // had read as text, and the fill came in the new grid's numbers). + r.confirm(try a.local(.{ .key = .{ .cp = 'b', .ctrl = true } })); + for ([_]u21{ 'g', 'g' }) |cp| r.confirm(try a.local(.{ .key = .{ .cp = cp, .text = &.{@intCast(cp)} } })); + const opened = try a.openView(1); + r.view = opened.view; + r.confirm(opened.unit); + try deliver(a, &screens, peer, r, .flush); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + const Step = struct { + fn take(au: *Authority, sc: *Screens, pe: u16, rr: *Replica, msg: []const u8) !void { + try deliver(au, sc, pe, rr, .flush); + const u = try std.testing.allocator.dupe(u8, try au.take(msg)); + defer std.testing.allocator.free(u); + try deliver(au, sc, pe, rr, .fill); + pardes.panes.resizeDeferred(au.core); + rr.confirm(u); + rr.settle(); + } + }; + try sent.append(gpa, try gpa.dupe(u8, try r.input(.{ .resize = .{ .cols = 120, .rows = 30 } }))); + try key(r, 'g', &sent); + try key(r, 'g', &sent); + for (sent.items) |m| try Step.take(a, &screens, peer, r, m); + for (sent.items) |m| gpa.free(m); + sent.clearRetainingCapacity(); + // The session's view types into it, which sizes it back (the client's + // copy of its history all holes again, a new numbering); then the + // client's key that reads all of it, and sizes it again. + try deliver(a, &screens, peer, r, .flush); + { + const was = Stepping.begin(&a.t, null); + a.core.toView(0); + a.core.update(.{ .key = .{ .cp = 'k', .text = "k" } }); + a.core.step(); + was.end(); + } + a.seq += 1; + const back = try gpa.dupe(u8, try a.unit(0, a.seq)); + defer gpa.free(back); + try deliver(a, &screens, peer, r, .fill); + pardes.panes.resizeDeferred(a.core); + r.confirm(back); + r.settle(); + try deliver(a, &screens, peer, r, .flush); + try key(r, 'w', &sent); + try key(r, 'w', &sent); + for (sent.items) |m| try Step.take(a, &screens, peer, r, m); + try deliver(a, &screens, peer, r, .flush); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null and r.quiet()); + try std.testing.expectEqual(digestOf(a), r.digest()); +} + +test "pardes's own keys over a terminal's history apply at once: its view moves here, a delta while they wait puts them on top again, the authority's ends the same" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host(), true); + defer a.deinit(); + var screens: Screens = .init(gpa); + defer screens.deinit(a.core); + const peer = try screens.addPeer(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), true); + defer r.deinit(); + const shell: u8 = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + a.core.view().active = shell; + r.confirm(try a.local(.{ .focus = true })); + var line: [64]u8 = undefined; + for (0..200) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "history line {d}\r\n", .{i}) } }); + r.confirm((try a.pending()).?); + try deliver(a, &screens, peer, r, .flush); + r.settle(); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + // Normal mode in the shell, up a few rows, then half a screen and more: + // the cursor leaves the view, and the view follows it back. + const keys = [_]pardes.Key{ + .{ .cp = a.core.opts.tty_toggle, .ctrl = true }, .{ .cp = 'k', .text = "k" }, .{ .cp = 'k', .text = "k" }, + .{ .cp = 'u', .ctrl = true }, .{ .cp = 'u', .ctrl = true }, .{ .cp = 'k', .text = "k" }, + }; + for (keys) |k| try sent.append(gpa, try gpa.dupe(u8, try r.input(.{ .key = k }))); + try std.testing.expectEqual(@as(u32, 0), r.stats.stopped); + try std.testing.expectEqual(sent.items.len, r.applied); + const here = r.core().panes[shell].?; + try std.testing.expect(pardes.Term.grid(here.terminal).view != null); + // The shell prints while they wait: its unit, then its delta, which + // sets the view the authority's (at the bottom still): what was applied + // goes on top of it again. + for (0..5) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "late line {d}\r\n", .{i}) } }); + r.confirm((try a.pending()).?); + r.settle(); + try deliver(a, &screens, peer, r, .flush); + r.settle(); + try std.testing.expectEqual(sent.items.len, r.applied); + try std.testing.expect(pardes.Term.grid(r.core().panes[shell].?.terminal).view != null); + // The authority takes them in order, the terminal sent before each and + // the rows its step read after, as the session's loop does. + for (sent.items) |m| { + try deliver(a, &screens, peer, r, .flush); + r.settle(); + const u = try gpa.dupe(u8, try a.take(m)); + defer gpa.free(u); + try deliver(a, &screens, peer, r, .fill); + r.confirm(u); + r.settle(); + } + try deliver(a, &screens, peer, r, .flush); + r.settle(); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null and r.quiet()); + try std.testing.expectEqual(@as(u32, 0), r.stats.mispredicted); + try std.testing.expectEqual(digestOf(a), r.digest()); + try std.testing.expect(pardes.Term.grid(a.core.panes[shell].?.terminal).view != null); + try std.testing.expectEqual(pardes.Term.grid(a.core.panes[shell].?.terminal).view, pardes.Term.grid(r.core().panes[shell].?.terminal).view); + // A rollback with nothing confirmed on top puts the view back: one more + // key up, and another's unit first. + try sent.append(gpa, try gpa.dupe(u8, try r.input(.{ .key = .{ .cp = 'u', .ctrl = true } }))); + const before = pardes.Term.grid(a.core.panes[shell].?.terminal).view; + try std.testing.expect(pardes.Term.grid(r.core().panes[shell].?.terminal).view.? < before.?); + r.confirm(try a.local(.{ .focus = false })); + r.settle(); + const last = try gpa.dupe(u8, try a.take(sent.items[sent.items.len - 1])); + defer gpa.free(last); + r.toConfirmed(); + try std.testing.expectEqual(before, pardes.Term.grid(r.core().panes[shell].?.terminal).view); + r.dirty = true; + r.settle(); + try deliver(a, &screens, peer, r, .fill); + r.confirm(last); + r.settle(); + try deliver(a, &screens, peer, r, .flush); + r.settle(); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null and r.quiet()); + try std.testing.expectEqual(digestOf(a), r.digest()); + try std.testing.expectEqual(pardes.Term.grid(a.core.panes[shell].?.terminal).view, pardes.Term.grid(r.core().panes[shell].?.terminal).view); +} + +test "a unit kept over a copy taken ahead is never replayed over a delta that came after it: the session's key read a shell's rows, the program redrew them" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host(), true); + defer a.deinit(); + var screens: Screens = .init(gpa); + defer screens.deinit(a.core); + const peer = try screens.addPeer(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), true); + defer r.deinit(); + const shell: u8 = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + a.core.view().active = shell; + r.confirm(try a.local(.{ .focus = true })); + var line: [96]u8 = undefined; + for (0..30) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "line {d} {s}\r\n", .{ i, "words of a length its own"[0 .. i % 20 + 4] }) } }); + r.confirm((try a.pending()).?); + try deliver(a, &screens, peer, r, .flush); + // Out of the shell's tty mode, in the session's view: keys over its text. + r.confirm(try a.local(.{ .key = .{ .cp = a.core.opts.tty_toggle, .ctrl = true } })); + r.settle(); + // The client idle: a copy taken ahead. The session's keys read the rows + // (up, to the line's end): replayed here over the copy, and kept. + r.ready(); + try std.testing.expect(r.copy.held); + for ([_]u21{ 'k', 'k', '$' }) |cp| { + const u = try gpa.dupe(u8, try a.local(.{ .key = .{ .cp = cp, .text = &.{@intCast(cp)} } })); + defer gpa.free(u); + try deliver(a, &screens, peer, r, .fill); + r.confirm(u); + r.settle(); + } + // The program redraws the screen: other rows where those keys read. + a.output(.{ .output = .{ .pane = shell, .bytes = "\x1b[H\x1b[2J" } }); + for (0..30) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "{d}\r\n", .{i}) } }); + r.confirm((try a.pending()).?); + r.settle(); + try deliver(a, &screens, peer, r, .flush); + // The client types, and the session's key comes first: a rollback, which + // must not replay the keys that read the rows over the redrawn ones. + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + try key(r, 'h', &sent); + try std.testing.expectEqual(@as(usize, 1), r.applied); + { + const u = try gpa.dupe(u8, try a.local(.{ .key = .{ .cp = '0', .text = "0" } })); + defer gpa.free(u); + try deliver(a, &screens, peer, r, .fill); + r.confirm(u); + r.settle(); + } + try deliver(a, &screens, peer, r, .flush); + r.confirm(try a.take(sent.items[0])); + r.settle(); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null and r.quiet()); + try std.testing.expectEqual(digestOf(a), r.digest()); +} + +test "a unit that closes or tears down a terminal is never kept over a copy taken ahead: a rollback would close its emulator again" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host(), true); + defer a.deinit(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), true); + defer r.deinit(); + const text = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.file != null) break id; + } else return error.NoTextPane; + a.core.view().active = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break id; + } else return error.NoShell; + // The session closes its shell, and its next steps tear it down, while + // the client is idle (a copy taken ahead each time). + r.ready(); + r.confirm(try a.local(.{ .command = "Del" })); + a.core.view().active = text; + for (0..3) |_| { + r.ready(); + r.confirm(try a.local(.{ .key = .{ .cp = 'j', .text = "j" } })); + } + // The client types, and the session's key comes first: a rollback. + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + r.ready(); + try key(r, 'l', &sent); + r.confirm(try a.local(.{ .key = .{ .cp = 'o', .text = "o" } })); + r.settle(); + r.confirm(try a.take(sent.items[0])); + r.settle(); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null and r.quiet()); + try std.testing.expectEqual(digestOf(a), r.digest()); + // The shell closed once here as there (the store's record of it is made + // as it closes, on the host's heap, which no copy puts back). + try std.testing.expectEqual(a.core.store.closed.items.len, r.core().store.closed.items.len); +} + +test "a replica joined from the session's dump replays the units after it" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + _ = try a.local(.{ .key = .{ .cp = 'j', .text = "j" } }); + const was = Stepping.begin(&a.t, null); + const bytes = try pardes.dump.dumpBytes(a.core); + tape.resync(&a.t); + was.end(); + defer a.core.gpa.free(bytes); + const r = try Replica.initDump(gpa, 1, 0, bytes, .{ .cols = 80, .rows = 24 }); + defer r.deinit(); + try std.testing.expectEqual(digestOf(a), r.digest()); + r.confirm(try a.local(.{ .key = .{ .cp = 'j', .text = "j" } })); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + for ("oab") |k| try key(r, k, &sent); + try key(r, pardes.Key.escape, &sent); + for (sent.items) |m| { + r.confirm(try a.take(m)); + r.settle(); + } + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null); + try std.testing.expect(r.quiet()); + try std.testing.expectEqual(digestOf(a), r.digest()); +} + +test "a replica joined from a dump replays a step that opens a file's pane and says it has no room for it: its /log, empty, is not the session's" { + const gpa = std.testing.allocator; + // No rows for a second pane: the Look's pane of a kind is made, the + // placement says it has no room (a message in the step that made the + // pane, which was not in /log yet), and the pane goes. + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 5, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + var words: [64]pardes.Kind.LoadedWord = undefined; + const zon = pardes.Kind.loadedOf(&Zon.ops, "/x/zon.so", @splat(9), &words); + _ = try a.local(.{ .addon = .{ .op = .load, .pane = 0, .kind = &zon } }); + const r = try joinedNow(a, 1, 0); + defer r.deinit(); + try std.testing.expectEqual(digestOf(a), r.digest()); + var whole: [libc.PATH_MAX]u8 = undefined; + var look: [libc.PATH_MAX + 8]u8 = undefined; + const path = std.mem.span(libc.realpath("build.zig.zon", &whole) orelse return error.SkipZigTest); + r.confirm(try a.local(.{ .command = try std.fmt.bufPrint(&look, "Look {s}", .{path}) })); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null); + try std.testing.expectEqual(digestOf(a), r.digest()); +} + +/// A kind of `.zon` files, for a replica's Look of one (a picture's, +/// as a kind's pane is made). +const Zon = struct { + const pa = pardes.Kind.c; + fn open(_: [*c]const u8, _: [*c]const u8, _: u64) callconv(.c) ?*anyopaque { + return pardes.Kind.api.alloc.?(4); + } + fn paint(_: ?*anyopaque, _: [*c]const pa.pa_window, _: [*c]pa.pa_paint) callconv(.c) void {} + const ops = std.mem.zeroInit(pardes.Kind.Ops, .{ + .head = std.mem.zeroInit(pa.pa_head, .{ .size = @sizeOf(pa.pa_head), .abi = pardes.Kind.abi, .name = "zon".* ++ @as([13]u8, @splat(0)), .version = 1, .exts = ".zon" }), + .open = open, + .paint = paint, + }); +}; + +test "a replica joined from a themed dump paints its taglines in the restored theme" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, TestHost.host(), false); + defer a.deinit(); + _ = try a.local(.{ .command = "Theme rose_pine_moon" }); + const was = Stepping.begin(&a.t, null); + const bytes = try pardes.dump.dumpBytes(a.core); + tape.resync(&a.t); + was.end(); + defer a.core.gpa.free(bytes); + const r = try Replica.initDump(gpa, 1, 0, bytes, .{ .cols = 80, .rows = 24 }); + defer r.deinit(); + const expected = pardes.colors.ChromeTheme.fromTheme(a.core.theme()); + try std.testing.expectEqualStrings("rose_pine_moon", r.core().theme().name); + try std.testing.expectEqual(expected.tag_bg, r.core().chromeTheme().tag_bg); + try std.testing.expectEqual(expected.tag_fg, r.core().chromeTheme().tag_fg); + r.draw(.{ .ctx = null, .f = null }); + try std.testing.expectEqual(expected.tag_bg, r.core().surface.chrome.tag_bg); + try std.testing.expectEqual(expected.tag_fg, r.core().surface.chrome.tag_fg); + try std.testing.expectEqual(digestOf(a), r.digest()); + // A later theme is an ordinary confirmed unit, its chrome set at once. + r.confirm(try a.local(.{ .command = "Theme monokai_pro_classic" })); + r.settle(); + r.draw(.{ .ctx = null, .f = null }); + const changed = pardes.colors.ChromeTheme.fromTheme(a.core.theme()); + try std.testing.expectEqual(changed.tag_bg, r.core().surface.chrome.tag_bg); + try std.testing.expectEqual(changed.tag_fg, r.core().surface.chrome.tag_fg); + try std.testing.expect(r.diverged == null); + try std.testing.expectEqual(digestOf(a), r.digest()); +} + +test "a host's Adopt and a terminal reader's failure are inputs: a replica makes the same pane" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell, opened by a key + const gpa = std.testing.allocator; + // Its terminals as screen state, as a session's for its clients are. + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), true); + defer a.deinit(); + const was = Stepping.begin(&a.t, null); + const bytes = try pardes.dump.dumpBytes(a.core); + tape.resync(&a.t); + was.end(); + defer a.core.gpa.free(bytes); + const r = try Replica.initDump(gpa, 1, 0, bytes, .{ .cols = 80, .rows = 24 }); + defer r.deinit(); + // What tui.zig's and gui.zig's adoptShell and a failed reader do between + // steps: they wrote the core straight, and every client joined again. + const host = Stepping.begin(&a.t, null); + const id = a.core.adoptShell().?; + try std.testing.expect(a.core.panes[id].?.isTerminal() and !a.core.panes[id].?.greet); + a.core.readerFailed(id, error.InputOutput); + a.core.pump(a.host) catch {}; + host.end(); + a.seq += 1; + r.confirm(try a.unit(0, a.seq)); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null); + try std.testing.expectEqual(digestOf(a), r.digest()); +} + +test "a join's dump, its blobs put back where they go, is the session's dump" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const text = try gpa.alloc(u8, 20000); + defer gpa.free(text); + @memset(text, 'x'); + _ = try a.local(.{ .key = .{ .cp = 'i', .text = "i" } }); + _ = try a.local(.{ .paste = text }); + _ = try a.local(.{ .key = .{ .cp = pardes.Key.escape, .text = "" } }); + const was = Stepping.begin(&a.t, null); + defer was.end(); + const whole = try pardes.dump.byteDump(a.core); + defer a.core.gpa.free(whole); + var blobs: std.ArrayListUnmanaged(walk.Deferred) = .empty; + defer blobs.deinit(a.core.gpa); + const state = try pardes.dump.dumpJoin(a.core, &blobs); + defer a.core.gpa.free(state); + try std.testing.expect(blobs.items.len > 0); + try std.testing.expect(state.len + 20000 <= whole.len); + var joined: std.ArrayListUnmanaged(u8) = .empty; + defer joined.deinit(gpa); + var pos: usize = 0; + for (blobs.items) |b| { + try joined.appendSlice(gpa, state[pos..b.at]); + try joined.appendSlice(gpa, b.bytes); + pos = b.at; + } + try joined.appendSlice(gpa, state[pos..]); + try std.testing.expectEqualSlices(u8, whole, joined.items); +} + +test "a cached blob is hashed again as it is read: one kept cut short, corrupted, planted or longer is a miss, and goes" { + const io = std.testing.io; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var path: [4200]u8 = undefined; + const c = try testCache(&tmp, &path, 1 << 20); + var text: [6000]u8 = undefined; + for (&text, 0..) |*b, i| b.* = @truncate(i *% 31 +% i / 7); + var hash: [32]u8 = undefined; + std.crypto.hash.Blake3.hash(&text, &hash, .{}); + var out: [6000]u8 = undefined; + try std.testing.expect(!c.get(io, &hash, &out)); + var corrupted = text; + corrupted[4000] ^= 0x20; + const planted: [6000]u8 = @splat('x'); + var longer: [6001]u8 = undefined; + @memcpy(longer[0..6000], &text); + longer[6000] = '\n'; + // Each kept under the text's hash, as a torn write or another hand + // would leave it: read, it is a miss, and gone. + for ([_][]const u8{ text[0..3000], &corrupted, &planted, &longer }) |bad| { + c.put(io, &.{.{ .hash = hash, .bytes = bad }}, &.{}); + try std.testing.expect(c.has(io, &hash, bad.len)); + @memset(&out, 0); + try std.testing.expect(!c.get(io, &hash, &out)); + try std.testing.expect(!c.has(io, &hash, bad.len)); + } + // ...and put right, it is read again. + c.put(io, &.{.{ .hash = hash, .bytes = &text }}, &.{}); + try std.testing.expect(c.get(io, &hash, &out)); + try std.testing.expectEqualSlices(u8, &text, &out); +} + +test "the blob cache stays under its cap, the least recently used going first and one past the cap alone without them, the file compacted" { + const io = std.testing.io; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var path: [4200]u8 = undefined; + const c = try testCache(&tmp, &path, 3 * 5000); + var blobs: [4][5000]u8 = undefined; + var hashes: [4][32]u8 = undefined; + for (&blobs, &hashes, 0..) |*b, *h, k| { + @memset(b, @intCast('a' + k)); + std.crypto.hash.Blake3.hash(b, h, .{}); + } + for (0..3) |k| c.put(io, &.{.{ .hash = hashes[k], .bytes = &blobs[k] }}, &.{}); + // The oldest read: it is the newest now. + var out: [5000]u8 = undefined; + try std.testing.expect(c.get(io, &hashes[0], &out)); + c.put(io, &.{}, &.{hashes[0]}); + // Under the cap: nothing goes. + c.trim(io); + for (hashes[0..3]) |*h| try std.testing.expect(c.has(io, h, 5000)); + // One more is past it: the least recently used goes. + c.put(io, &.{.{ .hash = hashes[3], .bytes = &blobs[3] }}, &.{}); + const before = (try std.Io.Dir.cwd().statFile(io, c.path(), .{})).size; + c.trim(io); + try std.testing.expect(c.has(io, &hashes[0], 5000)); + try std.testing.expect(!c.has(io, &hashes[1], 5000)); + try std.testing.expect(c.has(io, &hashes[2], 5000) and c.has(io, &hashes[3], 5000)); + try std.testing.expect((try std.Io.Dir.cwd().statFile(io, c.path(), .{})).size < before); + // The newest past the cap alone (an edited big file's text): it goes, + // and the older ones, which fit, stay. + const big: [4 * 5000]u8 = @splat('z'); + var big_hash: [32]u8 = undefined; + std.crypto.hash.Blake3.hash(&big, &big_hash, .{}); + c.put(io, &.{.{ .hash = big_hash, .bytes = &big }}, &.{}); + c.trim(io); + try std.testing.expect(!c.has(io, &big_hash, big.len)); + for ([_]usize{ 0, 2, 3 }) |k| try std.testing.expect(c.has(io, &hashes[k], 5000)); + // Room for nothing: everything goes. + Cache.of(c.path(), 0).trim(io); + for (&hashes) |*h| try std.testing.expect(!c.has(io, h, 5000)); +} + +test "a blob cache that is no database is started afresh" { + const io = std.testing.io; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var path: [4200]u8 = undefined; + const c = try testCache(&tmp, &path, 1 << 20); + try std.Io.Dir.cwd().writeFile(io, .{ .sub_path = c.path(), .data = "xit but not a database, cut short" }); + const text: [5000]u8 = @splat('t'); + var hash: [32]u8 = undefined; + std.crypto.hash.Blake3.hash(&text, &hash, .{}); + var out: [5000]u8 = undefined; + try std.testing.expect(!c.get(io, &hash, &out)); + c.put(io, &.{.{ .hash = hash, .bytes = &text }}, &.{}); + try std.testing.expect(c.get(io, &hash, &out)); +} + +test "a far join is sent each blob once: joined again with them on disk or in the core it leaves, the state alone; a cached file gone bad, that blob again" { + const gpa = std.testing.allocator; + const io = std.testing.io; + zstd.gpa = gpa; + defer zstd.gpa = std.heap.c_allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const text = try gpa.alloc(u8, 30000); + defer gpa.free(text); + for (text, 0..) |*b, i| b.* = if (i % 61 == 60) '\n' else @intCast('a' + (i * 7 + i / 13) % 26); + _ = try a.local(.{ .key = .{ .cp = 'i', .text = "i" } }); + _ = try a.local(.{ .paste = text }); + _ = try a.local(.{ .key = .{ .cp = pardes.Key.escape, .text = "" } }); + _ = try a.local(.{ .key = .{ .cp = 'o', .text = "o" } }); + _ = try a.local(.{ .paste = text[0..9000] }); + _ = try a.local(.{ .key = .{ .cp = pardes.Key.escape, .text = "" } }); + // Two blobs more than the text: 70 lines deleted (the undo's), the 80 + // after them yanked (the register's). + for ("gg70xd80xy") |c| _ = try a.local(.{ .key = .{ .cp = c, .text = &.{c} } }); + const was = Stepping.begin(&a.t, null); + defer was.end(); + const whole = try pardes.dump.byteDump(a.core); + defer a.core.gpa.free(whole); + // What the session's list will say: its blobs, those of one text once. + var listed: std.ArrayListUnmanaged(walk.Deferred) = .empty; + defer listed.deinit(a.core.gpa); + a.core.gpa.free(try pardes.dump.dumpJoin(a.core, &listed)); + var distinct: std.AutoHashMapUnmanaged([32]u8, usize) = .empty; + defer distinct.deinit(gpa); + var distinct_bytes: u64 = 0; + var biggest: [32]u8 = undefined; + var biggest_len: usize = 0; + for (listed.items) |b| { + var d: [32]u8 = undefined; + std.crypto.hash.Blake3.hash(b.bytes, &d, .{}); + if ((try distinct.getOrPut(gpa, d)).found_existing) continue; + distinct_bytes += b.bytes.len; + if (b.bytes.len > biggest_len) { + biggest = d; + biggest_len = b.bytes.len; + } + } + try std.testing.expect(distinct.count() >= 3); + + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var path: [4200]u8 = undefined; + var have: Have = .{ .io = io, .cache = try testCache(&tmp, &path, 64 << 20) }; + defer { + have.cache = null; + have.deinit(); + } + var ps: Peers = .{ .gpa = gpa, .io = io }; + defer ps.deinit(); + + // A near client: the dump whole, as before. + var j = try farJoinOnce(&ps, a.core, null); + try std.testing.expectEqualSlices(u8, whole, j.dump); + try std.testing.expectEqual(@as(u32, 0), j.blobs); + endJoin(&j, null); + // The first far join: every blob sent, once. + j = try farJoinOnce(&ps, a.core, &have); + try std.testing.expectEqualSlices(u8, whole, j.dump); + try std.testing.expectEqual(listed.items.len, j.blobs); + try std.testing.expectEqual(@as(u32, 0), j.from_disk + j.from_memory); + try std.testing.expectEqual(distinct_bytes, j.sent_bytes); + endJoin(&j, &have); + // Again: all of them on disk. + j = try farJoinOnce(&ps, a.core, &have); + try std.testing.expectEqualSlices(u8, whole, j.dump); + try std.testing.expectEqual(distinct.count(), j.from_disk); + try std.testing.expectEqual(@as(u64, 0), j.sent_bytes); + endJoin(&j, &have); + // The biggest kept wrong (read back once as another length, it goes; + // then junk under its hash): it alone is sent again. + const junk = try gpa.alloc(u8, biggest_len); + defer gpa.free(junk); + @memset(junk, 'z'); + try std.testing.expect(!have.cache.?.get(io, &biggest, junk[0 .. biggest_len - 1])); + have.cache.?.put(io, &.{.{ .hash = biggest, .bytes = junk }}, &.{}); + j = try farJoinOnce(&ps, a.core, &have); + try std.testing.expectEqualSlices(u8, whole, j.dump); + try std.testing.expectEqual(distinct.count() - 1, j.from_disk); + try std.testing.expectEqual(@as(u64, biggest_len), j.sent_bytes); + endJoin(&j, &have); + // A rejoin, no disk: what the core it leaves holds. + const cache = have.cache; + have.cache = null; + have.leaving(a.core); + j = try farJoinOnce(&ps, a.core, &have); + have.left(); + have.cache = cache; + try std.testing.expectEqualSlices(u8, whole, j.dump); + try std.testing.expectEqual(distinct.count(), j.from_memory); + try std.testing.expectEqual(@as(u64, 0), j.sent_bytes); + endJoin(&j, &have); +} + +test "a replica joined after a pane closed: the session's jump to it, its Panes and Recent rows, asked by either" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + // A file looked at and closed: a jump to it, Recent's, a closed pane. + var cwd: [std.Io.Dir.max_path_bytes]u8 = undefined; + var line: [std.Io.Dir.max_path_bytes + 32]u8 = undefined; + const dir = std.mem.span(@as([*:0]u8, @ptrCast(std.c.getcwd(&cwd, cwd.len) orelse return error.NoCwd))); + _ = try a.local(.{ .command = try std.fmt.bufPrint(&line, "Look {s}/build.zig.zon", .{dir}) }); + _ = try a.local(.{ .command = "Del" }); + try std.testing.expectEqual(@as(usize, 1), a.core.store.closed.items.len); + try std.testing.expect(a.core.view().jumps.n > 1); + // What another window of the user's wrote to the store, which the + // session read (src/variants_test.zig's case, the variant hunt's). + a.core.recent.opened(a.core.recentGpa(), try std.fmt.bufPrint(&line, "{s}/build.zig", .{dir})); + const r = try joinedNow(a, 1, 0); + defer r.deinit(); + try std.testing.expectEqual(digestOf(a), r.digest()); + r.confirm(try a.local(.{ .key = .{ .cp = 'j', .text = "j" } })); + r.confirm(try a.local(.{ .command = "Search Panes" })); + r.confirm(try a.local(.{ .command = "Search Recent" })); + try agrees(a, r); + // The replica's own: it waits for the session's rows. + for ([_][]const u8{ "Search Panes", "Search Recent" }) |asked| { + const msg = try gpa.dupe(u8, try r.input(.{ .command = asked })); + defer gpa.free(msg); + r.confirm(try a.take(msg)); + r.settle(); + } + try agrees(a, r); + // The closed pane brought back by the replica's Look at it: the + // session's record of it, off the tape. + { + const closed = try std.fmt.bufPrint(&line, "Look @p{d}", .{a.core.store.closed.items[0].serial}); + const msg = try gpa.dupe(u8, try r.input(.{ .command = closed })); + defer gpa.free(msg); + r.confirm(try a.take(msg)); + r.settle(); + try std.testing.expectEqual(@as(usize, 0), a.core.store.closed.items.len); + try agrees(a, r); + r.confirm(try a.local(.{ .command = "Del" })); + } + // A jump back onto the closed file: opened again, from Recent. + for (0..3) |_| { + const msg = try gpa.dupe(u8, try r.input(.{ .key = .{ .cp = 'o', .ctrl = true } })); + defer gpa.free(msg); + r.confirm(try a.take(msg)); + r.settle(); + } + try agrees(a, r); +} + +test "a replica's Del of a file its session's Recent knows asks the host what the session asked" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + var cwd: [std.Io.Dir.max_path_bytes]u8 = undefined; + var line: [std.Io.Dir.max_path_bytes + 32]u8 = undefined; + const dir = std.mem.span(@as([*:0]u8, @ptrCast(std.c.getcwd(&cwd, cwd.len) orelse return error.NoCwd))); + _ = try a.local(.{ .command = try std.fmt.bufPrint(&line, "Look {s}/build.zig.zon", .{dir}) }); + // Joined while it is open: in the session's Recent, in no replica's. + const r = try joinedNow(a, 1, 0); + defer r.deinit(); + const msg = try gpa.dupe(u8, try r.input(.{ .command = "Del" })); + defer gpa.free(msg); + r.confirm(try a.take(msg)); + r.settle(); + try std.testing.expectEqual(@as(usize, 1), a.core.store.closed.items.len); + try agrees(a, r); +} + +test "a client leaves, and one joins: what the leave queued (a terminal's resize) is done in its unit, on every side, before the join's dump" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, PtyHost.host(), true); + defer a.deinit(); + const first = try gpa.dupe(u8, try a.unit(0, 0)); + defer gpa.free(first); + const old = try Replica.init(gpa, 1, first, true); + defer old.deinit(); + // A second client, bigger, types into the terminal: it sizes it. + const opened = try a.openView(2); + old.confirm(opened.unit); + var buf: [256]u8 = undefined; + old.confirm(try a.take(try inputOf(&buf, 2, 1, .{ .resize = .{ .cols = 120, .rows = 40 } }))); + old.confirm(try a.take(try inputOf(&buf, 2, 2, .{ .key = .{ .cp = 'x', .text = "x" } }))); + // It leaves: the terminal is the first view's size again, its pty's + // resize queued for the next pump; and a client joins at that boundary. + var was = Stepping.begin(&a.t, null); + a.core.closeView(opened.view); + was.end(); + old.confirm(try a.unit(0, 100)); + // The client left idle takes its copy ahead (Replica.ready): of a core + // with nothing queued. + old.ready(); + was = Stepping.begin(&a.t, null); + const k = a.core.openView() orelse return error.NoView; + was.end(); + old.confirm(try a.unit(0, 101)); + const joiner = try joinedNow(a, 3, k); + defer joiner.deinit(); + try std.testing.expectEqual(digestOf(a), joiner.digest()); + // The next pump's unit, every client's. + const u = try gpa.dupe(u8, try a.local(.tick)); + defer gpa.free(u); + old.confirm(u); + joiner.confirm(u); + try agrees(a, old); + try agrees(a, joiner); +} + +test "a client that diverged joins again for its view: kept for it meanwhile, its pane in it again; the other client agrees" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const first = try gpa.dupe(u8, try a.unit(0, 0)); + defer gpa.free(first); + const old = try Replica.init(gpa, 1, first, false); + defer old.deinit(); + const opened = try a.openView(2); + old.confirm(opened.unit); + var buf: [256]u8 = undefined; + old.confirm(try a.take(try inputOf(&buf, 2, 1, .{ .command = "New" }))); + const made = a.core.view().active; + // Its bye says it joins again: its view is kept, as every replica keeps it. + var was = Stepping.begin(&a.t, null); + a.core.awayView(opened.view); + was.end(); + old.confirm(try a.unit(0, 100)); + try agrees(a, old); + // Back for it: its own, its pane in it, in the dump it loads. + was = Stepping.begin(&a.t, null); + try std.testing.expect(a.core.rejoinView(opened.view)); + was.end(); + old.confirm(try a.unit(0, 101)); + const back = try joinedNow(a, 3, opened.view); + defer back.deinit(); + try std.testing.expectEqual(digestOf(a), back.digest()); + a.view_of[3] = opened.view; + const u = try gpa.dupe(u8, try a.take(try back.input(.{ .key = .{ .cp = 'l', .text = "l" } }))); + defer gpa.free(u); + old.confirm(u); + back.confirm(u); + try std.testing.expectEqual(made, a.core.view().active); + try std.testing.expect(pardes.layout.findPane(a.core, made) != null); + try agrees(a, old); + try agrees(a, back); +} + +test "a client joined from a dump moves through another view's pane as the session does, its lines not wrapped" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, TestHost.host(), false); + defer a.deinit(); + // The first client's Wrap off (SPC t w), and its cursor down into the + // file's long lines, near the bottom of its view. + var buf: [256]u8 = undefined; + var seq: u32 = 1; + const before = [_]pardes.Event{.{ .command = "Wrap" }} ++ + @as([10]pardes.Event, @splat(.{ .key = .{ .cp = 'd', .ctrl = true } })) ++ + @as([15]pardes.Event, @splat(.{ .key = .{ .cp = 'j', .text = "j" } })); + for (before) |ev| { + _ = try a.take(try inputOf(&buf, 1, seq, ev)); + seq += 1; + } + // Another client joins, its view not live; the first's keys go on. + _ = try a.openView(2); + const joiner = try joinedNow(a, 3, 1); + defer joiner.deinit(); + try std.testing.expectEqual(digestOf(a), joiner.digest()); + for (0..3) |_| { + const u = try gpa.dupe(u8, try a.take(try inputOf(&buf, 1, seq, .{ .key = .{ .cp = 'd', .ctrl = true } }))); + defer gpa.free(u); + seq += 1; + joiner.confirm(u); + try agrees(a, joiner); + } +} + +test "a client joined from a dump makes a `/` query's rows as the session does, though only the session has drawn its owner" { + if (comptime !pardes.syntax.enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, TestHost.host(), false); + defer a.deinit(); + // The session draws: its owner's colours painted for the view (File.refreshHighlights). + { + const was = Stepping.begin(&a.t, null); + defer was.end(); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + _ = try a.core.render(arena.allocator()); + } + _ = try a.openView(2); + const joiner = try joinedNow(a, 3, 1); + defer joiner.deinit(); + try std.testing.expectEqual(digestOf(a), joiner.digest()); + var buf: [256]u8 = undefined; + for ("/co", 1..) |c, seq| { + const u = try gpa.dupe(u8, try a.take(try inputOf(&buf, 1, @intCast(seq), .{ .key = .{ .cp = c, .text = &.{c} } }))); + defer gpa.free(u); + joiner.confirm(u); + try agrees(a, joiner); + } +} + +test "a step that reads a terminal's rows as the core keeps them sends a new client the ones it holds as holes" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, PtyHost.host(), true); + defer a.deinit(); + var screens: Screens = .init(gpa); + defer screens.deinit(a.core); + const first = try gpa.dupe(u8, try a.unit(0, 0)); + defer gpa.free(first); + const r1 = try Replica.init(gpa, 1, first, true); + defer r1.deinit(); + const r2 = try Replica.init(gpa, 2, first, true); + defer r2.deinit(); + const shell: u8 = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + a.core.view().active = shell; + var line: [64]u8 = undefined; + for (0..60) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "line {d} of the shell's history\r\n", .{i}) } }); + const out = try gpa.dupe(u8, (try a.pending()).?); + defer gpa.free(out); + for ([_]*Replica{ r1, r2 }) |r| r.confirm(out); + // The first client alone has the terminal; it reads all of its rows + // (its text, the cursor on the first): the core keeps them. + const p1 = [_]u16{try screens.addPeer()}; + for ([_]pardes.Event{ .{ .key = .{ .cp = 'b', .ctrl = true } }, .{ .key = .{ .cp = 'g', .text = "g" } }, .{ .key = .{ .cp = 'g', .text = "g" } } }) |ev| { + const msg = try gpa.dupe(u8, try r1.input(ev)); + defer gpa.free(msg); + try takeAll(a, &screens, &.{r1}, &p1, msg); + r2.confirm(a.out.items); + } + // A second comes: its history all holes. The first reads the rows the + // core kept, nothing written since. + const ps = [_]u16{ p1[0], try screens.addPeer() }; + try sendAs(r1, a, &screens, &.{ r1, r2 }, &ps, .{ .key = .{ .cp = 'O', .text = "O" } }); + for ([_]*Replica{ r1, r2 }) |r| try agrees(a, r); +} + +test "a replica with no code for a kind whose state is not plain shows its pane as a line saying so, and its keys go to the authority" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), false); + defer r.deinit(); + // What the host read off the library, as the `addon` event: the + // replica's core takes the kind's words, never its code. + var words: [64]pardes.Kind.LoadedWord = undefined; + const counter = pardes.Kind.loadedOf(&Counter.ops, "/x/counter.so", @splat(7), &words); + r.confirm(try a.local(.{ .addon = .{ .op = .load, .pane = 0, .kind = &counter } })); + r.confirm(try a.local(.{ .command = "Counter" })); + try std.testing.expect(r.core().kinds.ops[r.core().kinds.n - 1] == null); + try std.testing.expect(try shows(r.core(), "addon counter 1 does not run in this client")); + // A key into it, typed at the replica: the authority's kind takes it. + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + try key(r, 'x', &sent); + for (sent.items) |m| { + r.confirm(try a.take(m)); + r.settle(); + } + try std.testing.expect(try shows(a.core, "count 1")); + try std.testing.expect(!try shows(r.core(), "count 1")); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null); + try std.testing.expectEqual(digestOf(a), r.digest()); +} + +/// A kind whose place (`where`) is the rows its last view was told: a +/// session's step that tells it moves its place, never a key's. +const Placed = struct { + const pa = pardes.Kind.c; + fn open(_: [*c]const u8, _: [*c]const u8, _: u64) callconv(.c) ?*anyopaque { + const n: *u32 = @ptrCast(@alignCast(pardes.Kind.api.alloc.?(4))); + n.* = 0; + return n; + } + fn paint(_: ?*anyopaque, _: [*c]const pa.pa_window, _: [*c]pa.pa_paint) callconv(.c) void {} + fn view(st: ?*anyopaque, v: [*c]const pa.pa_view) callconv(.c) void { + const n: *u32 = @ptrCast(@alignCast(st.?)); + n.* = v.*.rows; + } + fn where(st: ?*anyopaque, buf: [*c]u8, len: u32) callconv(.c) u32 { + const n: *u32 = @ptrCast(@alignCast(st.?)); + return @intCast((std.fmt.bufPrint(buf[0..len], "1:0 {d}", .{n.*}) catch return 0).len); + } + const words = [_]pa.pa_word{std.mem.zeroInit(pa.pa_word, .{ .name = "Placed", .opens = 1, .doc = "a place" })}; + const ops = std.mem.zeroInit(pardes.Kind.Ops, .{ + .head = std.mem.zeroInit(pa.pa_head, .{ .size = @sizeOf(pa.pa_head), .abi = pardes.Kind.abi, .name = "placed".* ++ @as([10]u8, @splat(0)), .version = 1, .exts = "", .words = &words, .nwords = 1 }), + .open = open, + .paint = paint, + .view = view, + .where = where, + }); +}; + +test "a kind's place moved in a step of another view's: a replica's key that reads it guesses the authority's" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), false); + defer r.deinit(); + var words: [64]pardes.Kind.LoadedWord = undefined; + const placed = pardes.Kind.loadedOf(&Placed.ops, "/x/placed.so", @splat(7), &words); + r.confirm(try a.local(.{ .addon = .{ .op = .load, .pane = 0, .kind = &placed } })); + // The replica's view opens the kind's pane, and an Esc goes back to + // the file (Last): view 0 has the file. + const own = try a.openView(1); + r.view = own.view; + r.confirm(own.unit); + for ([_]pardes.Event{ .{ .command = "Placed" }, .{ .key = .{ .cp = pardes.Key.escape } } }) |ev| { + r.confirm(try a.take(try r.input(ev))); + r.settle(); + } + // View 0 resized, and a step of the session's own tells the kind: its + // place moves in a step whose view has the file, which reads it not. + var buf: [256]u8 = undefined; + r.confirm(try a.take(try inputOf(&buf, 2, 1, .{ .resize = .{ .cols = 80, .rows = 20 } }))); + r.confirm(try a.local(.tick)); + // The replica's Esc back to the kind's pane: its jump reads the place. + const fast = r.stats.fast; + const missed = r.stats.mispredicted; + const msg = try gpa.dupe(u8, try r.input(.{ .key = .{ .cp = pardes.Key.escape } })); + defer gpa.free(msg); + try std.testing.expectEqual(@as(usize, 1), r.applied); + r.confirm(try a.take(msg)); + r.settle(); + try std.testing.expectEqual(missed, r.stats.mispredicted); + try std.testing.expectEqual(fast + 1, r.stats.fast); + try agrees(a, r); +} + +/// A kind whose every key asks a job, for the job tokens' test. +const Jobber = struct { + const pa = pardes.Kind.c; + fn open(_: [*c]const u8, _: [*c]const u8, _: u64) callconv(.c) ?*anyopaque { + return pardes.Kind.api.alloc.?(4); + } + fn paint(_: ?*anyopaque, _: [*c]const pa.pa_window, _: [*c]pa.pa_paint) callconv(.c) void {} + fn press(_: ?*anyopaque, _: [*c]const pa.pa_key) callconv(.c) c_int { + _ = pardes.Kind.api.job.?(null, 0); + return 1; + } + const words = [_]pa.pa_word{std.mem.zeroInit(pa.pa_word, .{ .name = "Jobber", .opens = 1, .doc = "a key a job" })}; + const ops = std.mem.zeroInit(pardes.Kind.Ops, .{ + .head = std.mem.zeroInit(pa.pa_head, .{ .size = @sizeOf(pa.pa_head), .abi = pardes.Kind.abi, .name = "jobber".* ++ @as([10]u8, @splat(0)), .version = 1, .exts = "", .words = &words, .nwords = 1 }), + .open = open, + .paint = paint, + .key = press, + }); +}; + +test "a client joined from a dump numbers a kind's next job as the session does" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + var words: [64]pardes.Kind.LoadedWord = undefined; + const jobber = pardes.Kind.loadedOf(&Jobber.ops, "/x/jobber.so", @splat(7), &words); + _ = try a.local(.{ .addon = .{ .op = .load, .pane = 0, .kind = &jobber } }); + _ = try a.local(.{ .command = "Jobber" }); + for (0..3) |_| _ = try a.local(.{ .key = .{ .cp = 'x', .text = "x" } }); + const id = a.core.view().active; + try std.testing.expectEqual(@as(u32, 3), a.core.panes[id].?.addon.?.tokens); + const joiner = try joinedNow(a, 2, 1); + defer joiner.deinit(); + try std.testing.expectEqual(@as(u32, 3), joiner.core().panes[id].?.addon.?.tokens); + try std.testing.expectEqual(digestOf(a), joiner.digest()); +} + +test "a replica's word that opens a kind's pane with no file waits for the authority's open: never mispredicted" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), false); + defer r.deinit(); + var words: [64]pardes.Kind.LoadedWord = undefined; + const placed = pardes.Kind.loadedOf(&Placed.ops, "/x/placed.so", @splat(7), &words); + r.confirm(try a.local(.{ .addon = .{ .op = .load, .pane = 0, .kind = &placed } })); + // Its replica has no code for the kind: the open's outcome (its place, + // `1:0 0`) is the authority's to say. + const msg = try gpa.dupe(u8, try r.input(.{ .command = "Placed" })); + defer gpa.free(msg); + try std.testing.expectEqual(@as(usize, 0), r.applied); + r.confirm(try a.take(msg)); + r.settle(); + try std.testing.expectEqual(@as(u32, 0), r.stats.mispredicted); + try agrees(a, r); + try std.testing.expect(r.core().panes[r.core().view().active].?.addon != null); +} + +test "a client joined from a dump tells a kind each view's facts as the session does, the live one's and another's" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + var words: [64]pardes.Kind.LoadedWord = undefined; + const placed = pardes.Kind.loadedOf(&Placed.ops, "/x/placed.so", @splat(7), &words); + _ = try a.local(.{ .addon = .{ .op = .load, .pane = 0, .kind = &placed } }); + _ = try a.local(.{ .command = "Placed" }); + // A message in its pane: a band over its body, in each view. + _ = try a.local(.{ .command = "Placed x" }); + // The second client's view laid out by an input of its own, then the + // first's resized: its kind told at the session's next step of its own. + _ = try a.openView(2); + var buf: [256]u8 = undefined; + _ = try a.take(try inputOf(&buf, 2, 1, .{ .resize = .{ .cols = 80, .rows = 24 } })); + _ = try a.take(try inputOf(&buf, 1, 1, .{ .resize = .{ .cols = 80, .rows = 20 } })); + const joiner = try joinedNow(a, 3, 1); + defer joiner.deinit(); + try std.testing.expectEqual(digestOf(a), joiner.digest()); + joiner.confirm(try a.local(.tick)); + try agrees(a, joiner); +} + +/// A plain kind whose key asks a job and whose job's answer moves its +/// block: run by a client as by its session. +const Asker = struct { + const pa = pardes.Kind.c; + fn open(_: [*c]const u8, _: [*c]const u8, _: u64) callconv(.c) ?*anyopaque { + return pardes.Kind.api.alloc.?(4); + } + fn paint(_: ?*anyopaque, _: [*c]const pa.pa_window, _: [*c]pa.pa_paint) callconv(.c) void {} + fn press(_: ?*anyopaque, _: [*c]const pa.pa_key) callconv(.c) c_int { + _ = pardes.Kind.api.job.?(null, 0); + return 1; + } + fn run(_: [*c]const u8, _: u32, out: [*c]pa.pa_out) callconv(.c) u32 { + const b: [*]u8 = @ptrCast(out.*.alloc.?(out, 1) orelse return 0); + b[0] = 1; + return 1; + } + fn done(st: ?*anyopaque, _: u32, _: [*c]const u8, _: u32) callconv(.c) void { + const n: *u32 = @ptrCast(@alignCast(st.?)); + n.* += 1; + } + const words = [_]pa.pa_word{std.mem.zeroInit(pa.pa_word, .{ .name = "Asker", .opens = 1, .doc = "a key a job" })}; + const ops = std.mem.zeroInit(pardes.Kind.Ops, .{ + .head = std.mem.zeroInit(pa.pa_head, .{ .size = @sizeOf(pa.pa_head), .abi = pardes.Kind.abi, .name = "asker".* ++ @as([11]u8, @splat(0)), .version = 1, .exts = "", .flags = pa.PA_PLAIN, .words = &words, .nwords = 1 }), + .open = open, + .paint = paint, + .key = press, + .job_run = run, + .job_done = done, + }); +}; + +/// A host whose workers are all busy (tui.zig's `job` with its 16 tasks +/// out): a kind's job runs at once, inside the host's call. +const BusyHost = struct { + var core: ?*Pardes = null; + var tasks: @import("selection_pipe.zig").Tasks = .{ .len = @import("selection_pipe.zig").Tasks.capacity }; + fn job(_: ?*anyopaque, pane: u8, serial: u32, token: u32) void { + pardes.host_io.KindJob.start(std.testing.io, core.?, &tasks, pane, serial, token, null, never); + } + fn never(_: ?*anyopaque, j: *pardes.host_io.KindJob) void { + j.drop(); + } + const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .job = job }; +}; + +test "a kind's job answered at once inside its host's call (no worker free) is taken by a client as the session took it" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, .{ .vtable = &BusyHost.vtable }, false); + defer a.deinit(); + BusyHost.core = a.core; + const r = try Replica.init(gpa, 1, try a.unit(0, 0), false); + defer r.deinit(); + var words: [64]pardes.Kind.LoadedWord = undefined; + const asker = pardes.Kind.loadedOf(&Asker.ops, "/x/asker.so", @splat(7), &words); + r.confirm(try a.local(.{ .addon = .{ .op = .load, .pane = 0, .kind = &asker } })); + // The client has its code (as a library of its own found by its scan). + r.core().kinds.ops[r.core().kinds.byName("asker").?] = &Asker.ops; + r.confirm(try a.local(.{ .command = "Asker" })); + const id = a.core.view().active; + // Its key asks a job, answered inside the key's unit: an input the + // host made inside its call, on the tape (Effect.perform with no host + // `job` lands it on the core's side, on no tape: a host must have one). + r.confirm(try a.local(.{ .key = .{ .cp = 'x', .text = "x" } })); + try agrees(a, r); + try std.testing.expectEqual(@as(u32, 1), std.mem.bytesToValue(u32, a.core.panes[id].?.addon.?.plain.?[0..4])); + try std.testing.expectEqualSlices(u8, a.core.panes[id].?.addon.?.plain.?, r.core().panes[id].?.addon.?.plain.?); +} |
