From 3b57a72becd6dd6700d996c9c5c000e1cb53e076 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 18:19:13 -0300 Subject: phase 5 part A: the speculation and rollback engine, in one process: a walker copy put back where it was, replicas that apply their own input at once and take the authority's tape as the truth, and a harness of typists over delayed links src/rollback.zig: the replica core's allocator (`Epoch`) knows every allocation, and while a copy is held puts off frees of what was there before and refuses to grow it in place, so what the core held stays where it was. The copy (`Copy`) is the digest's walk saving the bytes of every allocation it reaches, field by field (the derived caches and this process's own too: a safe build's free writes over what it frees, and the frame arena points at what the epoch then frees), but the queues a step drains and the frame statistics; a file's text, its line index and its undo snapshots are pinned instead (made anew at each edit, never written; now freed raw everywhere), checked unchanged in a safe build. Put back, each allocation's bytes go back over it, the epoch's allocations are freed and its put-off frees forgotten; let go, they are made. Tree-sitter's trees, the C heap's, are held the same way (syntax.held_trees). src/replica.zig: an `Authority` records its core's every input to a tape in memory and hands each client's input back as a unit (origin, sequence number, its tape); a `Replica` applies its own input at once, as the authority will (update, then pump), recorded by its own tape (tape.zig's speculation mode), and for each unit in order drops its pending head when the authority recorded the same (inputs, host calls and answers: the fast path), else puts the copy back, replays the units since and this one (every digest checked) and applies its pending inputs again. A host call or a syscall stops a speculation: it is answered harmlessly, undone when it returns, and the input waits for the authority's answer; a clock or environment read is a guess the comparison checks; an emulator is the authority's (terminal.zig's writers stop a speculation; a safe build asserts none changed). The view's events (a resize, the pointer) wait. Messages are bytes: no transport yet. Each client has a view of its own (src/views.zig): the authority goes into it before the client's input (`toView`, taped), the replica's speculation the same, and the copy walks the views not live too (`parked`, each pane's `pv`). test/replicas.zig (`zig build replicas`): one authority and N replicas, a view each (`--shared` puts them in one), each client typing monkey.zig's mix at 50-200 ms over its own link (half the RTT plus 10% jitter, in order), the authority's pty echoing 2 ms later; gates: no replica diverges, every replica's digest is the authority's at quiescence; reported per RTT: inputs applied at once, stopped, waiting, fast paths, rollbacks (mispredicted, foreign), and what a speculative apply, a copy and a rollback cost. 3 clients x 100 inputs at 30, 80 and 207 ms: no divergence over 12 Debug seeds and 20 ReleaseFast ones with a filled heap; ReleaseFast p99 a speculative apply under 0.2 ms, a rollback under 1.6 ms. `zig build rollback-bench` times the real copy now (take, release, an edit, restore). What the replicas found, fixed: a release build reads what the heap had where Debug's fill made two cores agree: `col_panes` and `restore_buf` digested undefined (zeroed now), `terminal.State.reply_dropped` never set at create, `Text.sels` and `PendingPipe.ranges` digested past their length (a walker `len` cuts a slice too). PARDES_REPLICA_FILL fills a replica's allocations to show what is left. Co-Authored-By: Claude Opus 5.5 --- src/rollback.zig | 496 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 496 insertions(+) create mode 100644 src/rollback.zig (limited to 'src/rollback.zig') diff --git a/src/rollback.zig b/src/rollback.zig new file mode 100644 index 00000000..9137b6f6 --- /dev/null +++ b/src/rollback.zig @@ -0,0 +1,496 @@ +//! A replica's rollback copy (docs/next-daemon.md, phase 5): the state a +//! speculation can write, saved before it writes, and put back where it was +//! when the authority's order differs. +//! +//! Two halves: +//! - `Epoch`, the replica core's allocator. It knows every allocation, and +//! while a copy is held it frees nothing that was there before (a free +//! is put off) and grows nothing of then in place. What is allocated +//! meanwhile is the epoch's. So whatever the core held when the copy was +//! taken is still where it was when the copy is put back. +//! - `Copy`, the walker's walk of the core's state (src/walk.zig: what a +//! digest covers, so views, buffers, registers, counters and terminal +//! metadata), saving the bytes of every allocation it reaches, the core +//! struct's own included. A file's text, its line index and its undo +//! snapshots are pinned instead: an edit puts a new buffer in their +//! place (File.setContentSpan) and never writes one, so the copy keeps +//! a reference and the put-off free keeps it alive. +//! +//! Putting the copy back writes each saved allocation's bytes over it, so +//! every pointer in them points where it did, at what was there; then the +//! epoch's allocations are freed and its put-off frees forgotten. Letting +//! the copy go (the speculation was right) makes the put-off frees. +//! +//! ponytail: an allocation reached only through a field the walk leaves +//! out (a derived cache's own heap, say) is not saved; that cache is the +//! epoch's as it was written, which is right for one remade from the text +//! it was made of and wrong for one changed in place. The replica harness +//! (test/replicas.zig) checks a digest at every confirmed step. +const std = @import("std"); +const builtin = @import("builtin"); +const pardes = @import("pardes.zig"); +const walk = @import("walk.zig"); +const Allocator = std.mem.Allocator; +const Alignment = std.mem.Alignment; +const Pardes = pardes.Pardes; + +/// Blobs this long or longer (`pinned`) are pinned, not copied. Checked +/// unchanged in a safe build when the copy goes. +const blob_min = 4096; +const checked = builtin.mode == .debug or builtin.mode == .safe; + +pub const Epoch = struct { + child: Allocator = std.heap.c_allocator, + /// Its own bookkeeping, never the epoch's. + meta: Allocator = std.heap.c_allocator, + live: std.AutoHashMapUnmanaged(usize, Entry) = .empty, + on: bool = false, + id: u32 = 0, + born: std.ArrayListUnmanaged(usize) = .empty, + deferred: std.ArrayListUnmanaged(usize) = .empty, + trees: pardes.syntax.HeldTrees = .{ .meta = std.heap.c_allocator }, + + const Entry = struct { len: usize, alignment: Alignment, epoch: u32, freed: bool = false }; + + pub fn allocator(e: *Epoch) Allocator { + return .{ .ptr = e, .vtable = &.{ .alloc = alloc, .resize = resize, .remap = remap, .free = free } }; + } + + pub fn deinit(e: *Epoch) void { + var it = e.live.iterator(); + while (it.next()) |kv| e.child.rawFree(@as([*]u8, @ptrFromInt(kv.key_ptr.*))[0..kv.value_ptr.len], kv.value_ptr.alignment, @returnAddress()); + e.live.deinit(e.meta); + e.born.deinit(e.meta); + e.deferred.deinit(e.meta); + e.trees.born.deinit(e.meta); + e.trees.freed.deinit(e.meta); + } + + fn mine(e: *const Epoch, entry: Entry) bool { + return e.on and entry.epoch == e.id; + } + + /// The live allocation that starts at `addr`. + pub fn owns(e: *const Epoch, addr: usize) ?Entry { + const entry = e.live.get(addr) orelse return null; + return if (entry.freed) null else entry; + } + + fn note(e: *Epoch, addr: usize, len: usize, alignment: Alignment) bool { + e.live.put(e.meta, addr, .{ .len = len, .alignment = alignment, .epoch = if (e.on) e.id else 0 }) catch return false; + if (e.on) e.born.append(e.meta, addr) catch {}; + return true; + } + + /// Every new allocation filled with this: a core reading memory it never + /// wrote shows as a replica's divergence (test/replicas.zig, a release + /// build's heap is otherwise whatever it was). + pub var fill: ?u8 = null; + + fn alloc(ctx: *anyopaque, len: usize, alignment: Alignment, ra: usize) ?[*]u8 { + const e: *Epoch = @ptrCast(@alignCast(ctx)); + const ptr = e.child.rawAlloc(len, alignment, ra) orelse return null; + if (fill) |b| @memset(ptr[0..len], b); + if (!e.note(@intFromPtr(ptr), len, alignment)) { + e.child.rawFree(ptr[0..len], alignment, ra); + return null; + } + return ptr; + } + + fn resize(ctx: *anyopaque, memory: []u8, alignment: Alignment, new_len: usize, ra: usize) bool { + const e: *Epoch = @ptrCast(@alignCast(ctx)); + const entry = e.live.getPtr(@intFromPtr(memory.ptr)) orelse return false; + // What the copy saved stays its size: the core moves instead. + if (e.on and !e.mine(entry.*)) return false; + if (!e.child.rawResize(memory, alignment, new_len, ra)) return false; + entry.len = new_len; + return true; + } + + fn remap(ctx: *anyopaque, memory: []u8, alignment: Alignment, new_len: usize, ra: usize) ?[*]u8 { + const e: *Epoch = @ptrCast(@alignCast(ctx)); + const old = @intFromPtr(memory.ptr); + const entry = e.live.get(old) orelse return null; + if (e.on and !e.mine(entry)) return null; + const ptr = e.child.rawRemap(memory, alignment, new_len, ra) orelse return null; + _ = e.live.remove(old); + e.live.put(e.meta, @intFromPtr(ptr), .{ .len = new_len, .alignment = alignment, .epoch = entry.epoch }) catch {}; + if (e.mine(entry) and @intFromPtr(ptr) != old) e.born.append(e.meta, @intFromPtr(ptr)) catch {}; + return ptr; + } + + fn free(ctx: *anyopaque, memory: []u8, alignment: Alignment, ra: usize) void { + const e: *Epoch = @ptrCast(@alignCast(ctx)); + const addr = @intFromPtr(memory.ptr); + const entry = e.live.getPtr(addr) orelse return e.child.rawFree(memory, alignment, ra); + if (e.on and !e.mine(entry.*)) { + // Held by the copy until it is put back or let go. + if (!entry.freed) e.deferred.append(e.meta, addr) catch {}; + entry.freed = true; + return; + } + e.child.rawFree(memory.ptr[0..entry.len], entry.alignment, ra); + _ = e.live.remove(addr); + } + + fn reallyFree(e: *Epoch, addr: usize) void { + const entry = e.live.get(addr) orelse return; + e.child.rawFree(@as([*]u8, @ptrFromInt(addr))[0..entry.len], entry.alignment, @returnAddress()); + _ = e.live.remove(addr); + } + + fn begin(e: *Epoch) void { + std.debug.assert(!e.on); + e.id +%= 1; + if (e.id == 0) e.id = 1; + e.on = true; + e.born.clearRetainingCapacity(); + e.deferred.clearRetainingCapacity(); + e.trees.born.clearRetainingCapacity(); + e.trees.freed.clearRetainingCapacity(); + pardes.syntax.held_trees = &e.trees; + } + + /// The copy put back: what the epoch made goes, what it freed stays. + fn discard(e: *Epoch) void { + for (e.born.items) |addr| if (e.live.get(addr)) |entry| if (e.mine(entry)) e.reallyFree(addr); + for (e.deferred.items) |addr| if (e.live.getPtr(addr)) |entry| { + entry.freed = false; + }; + for (e.trees.born.items) |t| pardes.syntax.destroyTree(t); + e.end(); + } + + /// The copy let go: what the epoch freed goes now. + fn keep(e: *Epoch) void { + for (e.deferred.items) |addr| e.reallyFree(addr); + for (e.trees.freed.items) |t| pardes.syntax.destroyTree(t); + e.end(); + } + + fn end(e: *Epoch) void { + e.on = false; + e.born.clearRetainingCapacity(); + e.deferred.clearRetainingCapacity(); + e.trees.born.clearRetainingCapacity(); + e.trees.freed.clearRetainingCapacity(); + if (pardes.syntax.held_trees == &e.trees) pardes.syntax.held_trees = null; + } + + /// The trees of whichever core is stepped next: one in-process harness + /// steps several (syntax.held_trees is process-wide). + pub fn select(e: ?*Epoch) void { + pardes.syntax.held_trees = if (e) |x| (if (x.on) &x.trees else null) else null; + } +}; + +pub const Copy = struct { + meta: Allocator = std.heap.c_allocator, + regions: std.ArrayListUnmanaged(Region) = .empty, + bytes: std.ArrayListUnmanaged(u8) = .empty, + seen: std.AutoHashMapUnmanaged(Seen, void) = .empty, + pins: std.ArrayListUnmanaged(Pin) = .empty, + held: bool = false, + epoch: ?*Epoch = null, + /// The field being walked, for a pin's report, and whether it is one + /// of the blobs: a file's text or an undo snapshot's. + where: []const u8 = "", + pinnable: bool = false, + + const Region = struct { addr: usize, off: usize, len: usize }; + const Pin = struct { addr: usize, len: usize, hash: u64, where: []const u8 }; + const Seen = struct { addr: usize, ty: usize }; + pub const Error = Allocator.Error; + + pub fn deinit(c: *Copy) void { + c.regions.deinit(c.meta); + c.bytes.deinit(c.meta); + c.seen.deinit(c.meta); + c.pins.deinit(c.meta); + } + + pub fn savedBytes(c: *const Copy) usize { + return c.bytes.items.len; + } + + pub fn pinnedBytes(c: *const Copy) usize { + var n: usize = 0; + for (c.pins.items) |pin| n += pin.len; + return n; + } + + /// The copy of `p`, whose allocator is `e`'s, taken: from now on what + /// it held stays where it is until the copy is put back or let go. + pub fn take(c: *Copy, e: *Epoch, p: *const Pardes) Error!void { + std.debug.assert(!c.held); + c.regions.clearRetainingCapacity(); + c.bytes.clearRetainingCapacity(); + c.seen.clearRetainingCapacity(); + c.pins.clearRetainingCapacity(); + c.epoch = e; + // A step drains these before it ends: between steps they hold + // nothing, and a copy is taken between steps. + std.debug.assert(p.effects_len == 0 and p.in_len == 0); + if (c.epoch.?.owns(@intFromPtr(p)) == null) @panic("rollback: the core is not its epoch's"); + try c.object(Pardes, p); + e.begin(); + c.held = true; + } + + /// The core as it was when the copy was taken. + pub fn restore(c: *Copy) void { + std.debug.assert(c.held); + c.check(); + for (c.regions.items) |r| @memcpy(@as([*]u8, @ptrFromInt(r.addr))[0..r.len], c.bytes.items[r.off..][0..r.len]); + c.epoch.?.discard(); + c.held = false; + } + + /// The speculation was right: the copy is not wanted. + pub fn release(c: *Copy) void { + std.debug.assert(c.held); + c.check(); + c.epoch.?.keep(); + c.held = false; + } + + pub fn check(c: *const Copy) void { + if (comptime !checked) return; + for (c.pins.items) |pin| if (std.hash.Wyhash.hash(0, @as([*]const u8, @ptrFromInt(pin.addr))[0..pin.len]) != pin.hash) { + const now = @as([*]const u8, @ptrFromInt(pin.addr))[0..pin.len]; + const entry = c.epoch.?.live.get(pin.addr); + std.debug.print("rollback: {s}, {d} bytes at {x}, pinned, was written in place: now {any}; entry {any}, epoch {d}\n", .{ pin.where, pin.len, pin.addr, now[0..32], entry, c.epoch.?.id }); + @panic("rollback: a pinned blob was written in place"); + }; + } + + fn save(c: *Copy, addr: usize, len: usize) Error!void { + if (len == 0) return; + const off = c.bytes.items.len; + try c.bytes.appendSlice(c.meta, @as([*]const u8, @ptrFromInt(addr))[0..len]); + try c.regions.append(c.meta, .{ .addr = addr, .off = off, .len = len }); + } + + /// Whether the value at `addr` of type `T` was reached before. + fn reached(c: *Copy, comptime T: type, addr: usize) Error!bool { + const key: Seen = .{ .addr = addr, .ty = @intFromPtr(@typeName(T).ptr) }; + const got = try c.seen.getOrPut(c.meta, key); + return got.found_existing; + } + + /// The allocation at `addr`, saved once, whatever reaches it. + fn allocation(c: *Copy, addr: usize) Error!?usize { + const entry = c.epoch.?.owns(addr) orelse return null; + if (try c.reached(void, addr)) return entry.len; + try c.save(addr, entry.len); + return entry.len; + } + + fn visit(c: *Copy, comptime T: type, v: *const T) Error!void { + if (comptime !heapIn(T, 0)) return; + switch (@typeInfo(T)) { + .optional => |o| if (v.*) |*payload| try c.visit(o.child, payload), + .array => |a| if (comptime heapIn(a.child, 0)) for (v) |*e| try c.visit(a.child, e), + .pointer => |p| switch (p.size) { + .one => try c.object(p.child, v.*), + .slice => try c.slice(p.child, v.*), + // No length to walk by: its keeper's (a C handle's buffer). + else => {}, + }, + .@"struct" => |s| { + if (comptime walk.machinery(T)) return; + // A terminal: the authority's (its emulator is never written + // by a speculation: terminal.zig's guards). + if (comptime @hasDecl(T, "digest") and @TypeOf(T.digest) == fn (*const T, *walk.Hasher) void) return; + if (s.layout == .@"packed") return; + if (comptime @hasField(T, "items") and @hasField(T, "capacity") and s.field_names.len == 2) { + if (v.capacity == 0) return; + _ = try c.allocation(@intFromPtr(v.items.ptr)); + const E = @typeInfo(@FieldType(T, "items")).pointer.child; + if (comptime heapIn(E, 0)) for (v.items) |*e| try c.visit(E, e); + return; + } + if (comptime @hasDecl(T, "KV") and @hasDecl(T, "Hash") and @hasField(T, "metadata")) { + const m = v.metadata orelse return; + // std's header sits before the metadata, at the start + // of the one allocation. + const header = std.mem.alignForward(usize, 2 * @sizeOf(usize) + @sizeOf(u32), @alignOf(usize)); + _ = try c.allocation(@intFromPtr(m) - header); + var it = v.iterator(); + while (it.next()) |entry| { + try c.visit(@TypeOf(entry.key_ptr.*), entry.key_ptr); + try c.visit(@TypeOf(entry.value_ptr.*), entry.value_ptr); + } + return; + } + if (comptime @hasField(T, "raw") and s.field_names.len == 1 and @hasDecl(T, "load")) return; + if (comptime @hasField(T, "any_future")) return; + if (comptime @hasField(T, "bit_length") and @hasField(T, "masks") and s.field_names.len == 2) { + if (v.bit_length == 0) return; + _ = try c.allocation(@intFromPtr(v.masks) - @sizeOf(@typeInfo(@FieldType(T, "masks")).pointer.child)); + return; + } + inline for (s.field_names, s.field_types, s.field_attrs) |name, F, attr| { + // What a digest covers, the caches made from it and this + // process's own (the views not live are among it): a + // safe build's free writes over what it frees, so what + // the speculation freed would come back as that. Not the + // queues a step drains (undefined between steps), nor the + // authority's. + if (attr.@"comptime" or comptime (annotated(T, "authority", name) or (annotated(T, "local", name) and unsaved(name)))) continue; + if (comptime opaqueTo(F)) continue; + c.where = @typeName(T) ++ "." ++ name; + c.pinnable = comptime pinned(T, name); + if (comptime walk.lengthOf(T, name) != null and @typeInfo(F) == .array) { + const len = comptime walk.lengthOf(T, name).?; + if (comptime heapIn(@typeInfo(F).array.child, 0)) for (@field(v, name)[0..walk.lengthIn(v, len)]) |*e| try c.visit(@typeInfo(F).array.child, e); + } else try c.visit(F, &@field(v, name)); + } + }, + .@"union" => switch (v.*) { + inline else => |*payload| try c.visit(@TypeOf(payload.*), payload), + }, + .error_union => |e| if (v.*) |*payload| try c.visit(e.payload, payload) else |_| {}, + else => {}, + } + } + + fn object(c: *Copy, comptime P: type, ptr: *const P) Error!void { + if (comptime walk.machinery(P)) return; + const addr = @intFromPtr(ptr); + if (try c.reached(P, addr)) return; + if (comptime @hasDecl(P, "digest") and @TypeOf(P.digest) == fn (*const P, *walk.Hasher) void) return; + if (c.epoch.?.owns(addr) != null) { + _ = try c.reached(void, addr); + try c.fields(P, ptr); + } + try c.visit(P, ptr); + } + + /// A struct's bytes, field by field where it says what its fields are: + /// all of them (this process's own too, which the epoch's frees would + /// leave pointing at nothing), but the queues a step drains and the + /// frame statistics (numbers, megabytes of them), and an array only as + /// far as its length says. + fn fields(c: *Copy, comptime T: type, v: *const T) Error!void { + if (comptime @typeInfo(T) != .@"struct" or !@hasDecl(T, "replica")) return c.save(@intFromPtr(v), @sizeOf(T)); + const s = @typeInfo(T).@"struct"; + inline for (s.field_names, s.field_types, s.field_attrs) |name, F, attr| { + if (attr.@"comptime") continue; + if (comptime annotated(T, "local", name) and unsaved(name)) continue; + const at = @intFromPtr(&@field(v, name)); + if (comptime walk.lengthOf(T, name) != null and @typeInfo(F) == .array) { + try c.save(at, walk.lengthIn(v, comptime walk.lengthOf(T, name).?) * @sizeOf(@typeInfo(F).array.child)); + } else if (comptime annotatedStruct(F)) { + try c.fields(F, &@field(v, name)); + } else if (comptime @typeInfo(F) == .array and annotatedStruct(@typeInfo(F).array.child)) { + for (&@field(v, name)) |*e| try c.fields(@typeInfo(F).array.child, e); + } else try c.save(at, @sizeOf(F)); + } + } + + fn slice(c: *Copy, comptime E: type, s: []const E) Error!void { + if (s.len == 0) return; + const addr = @intFromPtr(s.ptr); + if (try c.reached([]const E, addr)) return; + if (comptime !heapIn(E, 0)) if (c.pinnable and s.len * @sizeOf(E) >= blob_min and c.epoch.?.owns(addr) != null) { + const bytes = std.mem.sliceAsBytes(s); + try c.pins.append(c.meta, .{ .addr = addr, .len = bytes.len, .hash = if (checked) std.hash.Wyhash.hash(0, bytes) else 0, .where = c.where }); + return; + }; + _ = try c.allocation(addr); + if (comptime heapIn(E, 0)) for (s) |*e| try c.visit(E, e); + } +}; + +/// The blobs: what an edit puts in place of its predecessor, never writes +/// (File.setContentSpan), and frees raw (memory.freeUnfilled, freeRaw). +fn pinned(comptime T: type, comptime name: []const u8) bool { + const File = pardes.panes.File; + return (T == File.State and (std.mem.eql(u8, name, "content") or std.mem.eql(u8, name, "line_starts"))) or + (T == File.Snapshot and std.mem.eql(u8, name, "content")); +} + +fn annotatedStruct(comptime T: type) bool { + return @typeInfo(T) == .@"struct" and @hasDecl(T, "replica"); +} + +/// The queues every step drains (empty between steps, asserted when a copy +/// is taken: what is in their slots is nothing), and the frame statistics +/// (numbers, and a static name). +fn unsaved(comptime name: []const u8) bool { + return std.mem.eql(u8, name, "effects") or std.mem.eql(u8, name, "in_q") or std.mem.eql(u8, name, "frames"); +} + +fn annotated(comptime T: type, comptime kind: []const u8, comptime name: []const u8) bool { + @setEvalBranchQuota(100_000); + if (!@hasDecl(T, "replica") or !@hasField(@TypeOf(T.replica), kind)) return false; + inline for (@field(T.replica, kind)) |n| if (comptime std.mem.eql(u8, n, name)) return true; + return false; +} + +/// A field no walk sees into (a C handle, a function): its own keeper's. +fn opaqueTo(comptime F: type) bool { + return switch (@typeInfo(F)) { + .optional => |o| opaqueTo(o.child), + .pointer => |p| p.size == .many or p.size == .c or switch (@typeInfo(p.child)) { + .@"opaque", .@"fn" => true, + else => false, + }, + else => false, + }; +} + +/// Whether a `T` can hold a pointer to the heap at all (else a walk of it +/// finds nothing to save). Deep types count as yes. +fn heapIn(comptime T: type, comptime depth: u32) bool { + if (depth > 12) return true; + return switch (@typeInfo(T)) { + .void, .null, .undefined, .bool, .int, .float, .@"enum", .error_set, .comptime_int, .comptime_float, .enum_literal, .type => false, + .optional => |o| heapIn(o.child, depth + 1), + .array => |a| a.len > 0 and heapIn(a.child, depth + 1), + .pointer => true, + .error_union => |e| heapIn(e.payload, depth + 1), + .@"struct" => |s| blk: { + if (s.layout == .@"packed") break :blk false; + for (s.field_types, s.field_attrs) |F, attr| if (!attr.@"comptime" and heapIn(F, depth + 1)) break :blk true; + break :blk false; + }, + .@"union" => |u| blk: { + for (u.field_types) |F| if (heapIn(F, depth + 1)) break :blk true; + break :blk false; + }, + else => true, + }; +} + +test "a copy put back is the core as it was; one let go keeps what was done" { + var e: Epoch = .{ .child = std.testing.allocator }; + defer e.deinit(); + const gpa = e.allocator(); + const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml", .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa }); + p.pump(.{}) catch {}; + const before = walk.digest(Pardes, p); + var c: Copy = .{}; + defer c.deinit(); + const typed = "ox y z"; + try c.take(&e, p); + for (typed) |k| p.update(.{ .key = .{ .cp = k, .text = &.{k} } }); + p.update(.{ .key = .{ .cp = pardes.Key.escape } }); + p.pump(.{}) catch {}; + const edited = walk.digest(Pardes, p); + try std.testing.expect(edited != before); + c.restore(); + try std.testing.expectEqual(before, walk.digest(Pardes, p)); + // The same again, let go this time: what was typed stays. + try c.take(&e, p); + for (typed) |k| p.update(.{ .key = .{ .cp = k, .text = &.{k} } }); + p.update(.{ .key = .{ .cp = pardes.Key.escape } }); + p.pump(.{}) catch {}; + try std.testing.expectEqual(edited, walk.digest(Pardes, p)); + c.release(); + try std.testing.expectEqual(edited, walk.digest(Pardes, p)); + p.deinit(); + try std.testing.expectEqual(@as(usize, 0), e.live.count()); +} -- cgit v1.3 From e89ee60270267cb268ce642d79fb3fb277e84463 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 21:03:38 -0300 Subject: phase 5 part B, terminals: a replica's terminal fed by screen-state deltas, several in flight against the version last sent, its history the authority's to the row, and the authority's own steps batched into a unit a frame screen_state.zig: each state the authority sends is a version of its terminal (Track), made once for every peer; a peer's delta is against the version it was last sent (the stream is ordered and loses nothing), up to 32 in flight, a peer at the bound waiting for an acknowledgement and then getting the newest; peers at one base are sent the same bytes. A replica refuses a delta against another base and is started afresh. It keeps the authority's history extent to the row (no byte limit of its own, trimmed to the authority's count), takes its viewport and its cursor's semantic content, and grows a flood's rows in bulk. The soak now keeps several in flight, lands them late in order, loses one now and then, and checks the history extent and the viewport too. replica.zig: Screens (the authority's tracks and peers) and Replica.screen/screenRequest, messages of u8 pane, u32 serial, u8 kind (a delta, rows; an ack, a row request, a reset). Deltas are applied with the replica's epoch paused, so an emulator outlives a rollback. Authority.output/pending: its own steps (a pty's output) go into one unit, sent before any other input it steps and at a frame, with one digest; under screen state a digest is one word. tape.zig, terminal.zig: under screen state an output is recorded without its bytes, pty writes are off the tape on both sides, no digest covers an emulator, and a replica's core never ingests or resizes one (a resize's own core work, the pointer selections cleared, still runs). A terminal counts its writes (State.changes); its MarkedHandler cycle rides in each delta. test/replicas.zig: the session's shell runs a TUI, then 8 MB of yes read 4 KB at a time, while three clients type; deltas go out before each input the authority steps and once a frame, acknowledgements and row requests come back over the links, every applied delta is checked against the authority's screen at that version and every terminal at quiescence. Co-Authored-By: Claude Opus 5.5 --- src/replica.zig | 291 +++++++++++++++++++++- src/rollback.zig | 12 + src/screen_state.zig | 682 ++++++++++++++++++++++++++++++++++----------------- src/tape.zig | 53 +++- src/terminal.zig | 57 +++++ test/replicas.zig | 320 ++++++++++++++++++++---- 6 files changed, 1130 insertions(+), 285 deletions(-) (limited to 'src/rollback.zig') diff --git a/src/replica.zig b/src/replica.zig index 42d94503..e229bad5 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -28,6 +28,8 @@ const tape = @import("tape.zig"); const rollback = @import("rollback.zig"); const walk = @import("walk.zig"); const wire = @import("detached/wire.zig"); +const screen_state = @import("screen_state.zig"); +const terminal = pardes.panes.terminal; const Allocator = std.mem.Allocator; const Pardes = pardes.Pardes; @@ -97,10 +99,15 @@ pub const Authority = struct { /// Each client's view (src/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. - pub fn init(gpa: Allocator, options: pardes.Options, host: pardes.Host) !*Authority { + /// `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; @@ -111,6 +118,9 @@ pub const Authority = struct { const a = try gpa.create(Authority); errdefer gpa.destroy(a); a.* = .{ .gpa = gpa, .t = tape.memory(gpa), .core = undefined, .host = host }; + 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; const was = Stepping.begin(&a.t, null); defer was.end(); a.core = try Pardes.init(gpa, opts); @@ -150,13 +160,38 @@ pub const Authority = struct { /// 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); } + /// 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.t.no_digest = true; + defer a.t.no_digest = false; + a.apply(ev); + 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; + const was = Stepping.begin(&a.t, null); + tape.digestHere(&a.t); + was.end(); + 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(); @@ -172,6 +207,7 @@ pub const Authority = struct { /// 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]); @@ -182,6 +218,140 @@ pub const Authority = struct { } }; +// ---- terminals as screen state (src/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 it asked for (encodeRows); +// 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. +// The authority sends a peer what it is owed (`Screens.flush`) before it +// steps any input but a pty's output, and once a frame; a replica takes +// them in that order between units (`Replica.screen`). So a step a replica +// replays reads the terminal its authority's 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, + enc: screen_state.Encoder, + tracks: [pardes.MAX_PANES]?Tracked = @splat(null), + peers: std.ArrayListUnmanaged(*[pardes.MAX_PANES]PeerOf) = .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: *terminal.State, track: screen_state.Track }; + const PeerOf = struct { serial: u32 = 0, peer: screen_state.Peer = .{} }; + + pub fn init(gpa: Allocator) Screens { + return .{ .gpa = gpa, .enc = .init(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| tr.track.deinit(s.gpa, if (s.live(core, id, tr)) &tr.state.vt else null); + for (s.peers.items) |p| s.gpa.destroy(p); + s.peers.deinit(s.gpa); + s.out.deinit(s.gpa); + s.enc.deinit(); + } + + 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 { + 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); + } + + /// 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, 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.* = .{ .serial = pane.serial }; + var marks: [terminal.marks_len]u8 = undefined; + 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 screen_state.encode(&s.enc, &tr.track, &pp.peer, &state.vt, state.changes, terminal.marksOf(state, &marks), &w.writer)) continue; + try out.append(s.gpa, try s.gpa.dupe(u8, w.written())); + } + } + + 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). + tr.track.deinit(s.gpa, null); + } + s.tracks[id] = .{ .serial = pane.serial, .state = pane.terminal.?, .track = .init(s.gpa) }; + return &s.tracks[id].?; + } + + /// 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 screen_state.encodeRows(&s.enc, &tr.track, &state.vt, 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 { @@ -207,11 +377,14 @@ pub const Stats = struct { 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, 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); } }; @@ -242,11 +415,26 @@ pub const Replica = struct { out: std.ArrayListUnmanaged(u8) = .empty, /// Nanoseconds of the rollback being made, until its reapply. recovering: ?u64 = null, + /// Its terminals' screen state, as the authority's deltas made it. + screens: [pardes.MAX_PANES]?ScreenOf = @splat(null), + screen_out: std.ArrayListUnmanaged(u8) = .empty, + + const ScreenOf = struct { + serial: u32, + rep: screen_state.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`. - pub fn init(gpa: Allocator, id: u16, first: []const u8) !*Replica { + /// A replica of the session whose first unit is `first`; `screens` + /// as the authority's (its terminals fed by `screen`). + 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) }; + r.t.screen_state = screens; r.t.core_gpa = r.epoch.allocator(); r.confirm(first); if (r.t.core == null) { @@ -257,6 +445,7 @@ pub const Replica = struct { } pub fn deinit(r: *Replica) void { + for (&r.screens) |*slot| if (slot.*) |*sc| sc.rep.deinit(r.coreGpa()); if (r.t.core) |c| { const was = Stepping.begin(&r.t, &r.epoch); if (r.copy.held) r.copy.restore(); @@ -269,6 +458,7 @@ pub const Replica = struct { 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.stats.deinit(r.gpa); tape.release(&r.t); if (r.diverged) |why| r.gpa.free(why); @@ -471,6 +661,89 @@ pub const Replica = struct { return r.pending.items.len == 0 and r.applied == 0 and !r.copy.held; } + fn coreGpa(r: *Replica) Allocator { + return r.epoch.allocator(); + } + + /// 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| old.rep.deinit(r.coreGpa()); + slot.* = .{ .serial = m.serial }; + } + const sc = &slot.*.?; + // The emulator is the authority's, outside every rollback copy: + // what it allocates now must outlive one. + const was = r.epoch.pause(); + defer r.epoch.unpause(was); + switch (m.kind) { + kind_delta => { + const t0 = nowNs(); + const applied = sc.rep.apply(r.coreGpa(), terminal.terminalIo(), &state.vt, 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, &.{}); + }; + r.stats.apply_ns.append(r.gpa, nowNs() - t0) catch {}; + sc.refused = false; + terminal.takeMarks(state, applied.extra); + 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; + sc.rep.applyRows(r.coreGpa(), &state.vt, m.payload) catch r.fail("a terminal's rows did not apply"); + return null; + }, + else => return null, + } + } + + /// 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 = sc.rep.shown(&state.vt); + var holes: std.ArrayList([2]u64) = .empty; + defer holes.deinit(r.gpa); + sc.rep.missing(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] }; + 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; + } + pub fn digest(r: *Replica) u64 { const was = Stepping.begin(&r.t, &r.epoch); defer was.end(); @@ -484,7 +757,7 @@ fn terminalDigests(p: *const Pardes, out: *[pardes.MAX_PANES]u64) void { const pane = slot orelse continue; const state = pane.terminal orelse continue; var h = walk.Hasher.init(0); - state.digest(&h); + state.digestScreen(&h); d.* = h.final(); } } @@ -513,9 +786,9 @@ fn key(r: *Replica, cp: u21, sent: *std.ArrayListUnmanaged([]u8)) !void { 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()); + 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)); + 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; @@ -539,13 +812,13 @@ test "a replica's own keys apply at once, confirm on the fast path, and end at t 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()); + 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); + const r1 = try Replica.init(gpa, 1, first, false); defer r1.deinit(); - const r2 = try Replica.init(gpa, 2, first); + const r2 = try Replica.init(gpa, 2, first, false); defer r2.deinit(); var s1: std.ArrayListUnmanaged([]u8) = .empty; var s2: std.ArrayListUnmanaged([]u8) = .empty; diff --git a/src/rollback.zig b/src/rollback.zig index 9137b6f6..b5f4e966 100644 --- a/src/rollback.zig +++ b/src/rollback.zig @@ -140,6 +140,18 @@ pub const Epoch = struct { _ = e.live.remove(addr); } + /// What is allocated and freed until `unpause` is no copy's: a delta + /// written into a replica's terminal while a copy is held (an emulator + /// is the authority's, outside every copy, and must outlive a rollback). + pub fn pause(e: *Epoch) bool { + defer e.on = false; + return e.on; + } + + pub fn unpause(e: *Epoch, was: bool) void { + e.on = was; + } + fn begin(e: *Epoch) void { std.debug.assert(!e.on); e.id +%= 1; diff --git a/src/screen_state.zig b/src/screen_state.zig index 51c2ec30..20ed7ad7 100644 --- a/src/screen_state.zig +++ b/src/screen_state.zig @@ -13,10 +13,15 @@ //! marks). A row's address is a hash of what the row shows, not of its blob, //! whose style ids are its source page's. An unchanged row costs 8 bytes. //! -//! One delta per peer is in flight, as FrameAck keeps one frame in flight -//! for the tty. The next delta is encoded against the state the peer -//! acknowledged, once it has, so a slow peer skips the states in between. -//! The peer holds the blobs of the state it acknowledged and no others. +//! Each state the authority sends is a version of its terminal (`Track`), +//! made once whatever the number of peers. A peer's next delta is against +//! the version it was last sent: the stream is ordered and loses nothing, +//! so the peer has that one by the time it applies the next. Several are +//! in flight (`bound`); acknowledgements free their room, and a peer at the +//! bound gets nothing until one comes, then the newest version, the ones +//! between skipped. Peers at the same base are sent the same bytes. A +//! replica holds the blobs of the state it applied last and no others; a +//! delta against another (one lost) is refused, and the peer started afresh. //! //! The replica writes each delta into a ghostty Terminal, which a replica's //! terminal pane draws as a local one (src/terminal.zig reads only that). @@ -120,23 +125,26 @@ fn pinOf(pages: anytype, top: u64, abs: u64) ?Pin { pub const Encoder = struct { gpa: std.mem.Allocator, scratch: ?Page = null, - meta: std.Io.Writer.Allocating, - pins: std.ArrayList(Pin) = .empty, + blob: std.Io.Writer.Allocating, + /// The rows a delta sends a blob for, and the addresses it lists once. needed: std.ArrayList(struct { pin: Pin, hash: Hash }) = .empty, + listed: std.AutoHashMapUnmanaged(Hash, void) = .empty, + tail: std.ArrayList(Pin) = .empty, pub fn init(gpa: std.mem.Allocator) Encoder { - return .{ .gpa = gpa, .meta = .init(gpa) }; + return .{ .gpa = gpa, .blob = .init(gpa) }; } pub fn deinit(e: *Encoder) void { if (e.scratch) |*s| s.deinit(); - e.meta.deinit(); - e.pins.deinit(e.gpa); + e.blob.deinit(); e.needed.deinit(e.gpa); + e.listed.deinit(e.gpa); + e.tail.deinit(e.gpa); } /// The PAGE record of `row` alone, its first `cols` cells. - fn blob(e: *Encoder, page: *Page, row: *Row, cols: u16, w: *std.Io.Writer) !void { + fn rowBlob(e: *Encoder, page: *Page, row: *Row, cols: u16, w: *std.Io.Writer) !void { var cap: vt.page.Capacity = if (e.scratch) |s| s.capacity else .{ .cols = cols, .rows = 1, .styles = 64 }; if (cap.cols != cols) cap = .{ .cols = cols, .rows = 1, .styles = 64 }; for (0..16) |_| { @@ -164,148 +172,245 @@ pub const Encoder = struct { } }; -/// What the authority knows of one peer. -pub const Peer = struct { - seq: u32 = 0, - in_flight: bool = false, - /// Never sent anything, or sent before a renumbering: the next delta - /// starts the replica afresh. - fresh: bool = true, - epoch: u32 = 0, - /// The active area's first row in the state it acknowledged. - top: u64 = 0, - /// The blobs the peer holds: those of the state it acknowledged. - known: std.AutoHashMapUnmanaged(Hash, void) = .empty, - /// Those of the delta in flight, which become `known` on its ack. - sent: std.AutoHashMapUnmanaged(Hash, void) = .empty, - - pub fn deinit(peer: *Peer, gpa: std.mem.Allocator) void { - peer.known.deinit(gpa); - peer.sent.deinit(gpa); +/// Deltas a peer may have in flight. At 207 ms and 60 frames a second about +/// 13 are; past the bound a peer gets nothing until an acknowledgement, and +/// then the newest state, the ones between skipped. +pub const bound = 32; + +/// What a peer holds once it has a version: the addresses of its active +/// rows and its meta. A delta against it sends no blob for these. +const Known = struct { + version: u32, + epoch: u32, + top: u64, + addrs: std.AutoHashMapUnmanaged(Hash, void) = .empty, +}; + +/// One terminal as the authority numbers it, for all of its peers: each +/// state it sends is a version, made once whatever the number of peers. +pub const Track = struct { + rows: Rows = .{}, + version: u32 = 0, + /// The emulator's change count the newest version was made at. + changes: u32 = 0, + /// The newest version: its active rows (pins, good while the emulator + /// is unchanged) and their addresses, and its meta. + pins: std.ArrayList(Pin) = .empty, + hashes: std.ArrayList(Hash) = .empty, + meta: std.Io.Writer.Allocating, + meta_hash: Hash = 0, + /// The newest versions, oldest first: the bases a peer's next delta may + /// have. A peer whose base has gone starts afresh. + known: std.ArrayList(Known) = .empty, + /// The last delta encoded, for the next peer at the same base. + cache: std.Io.Writer.Allocating, + cache_base: u32 = 0, + cache_version: u32 = 0, + + const kept = bound + 8; + + pub fn init(gpa: std.mem.Allocator) Track { + return .{ .meta = .init(gpa), .cache = .init(gpa) }; + } + + /// `t` is the terminal, or null when it is gone already (with its pins). + pub fn deinit(tr: *Track, gpa: std.mem.Allocator, t: ?*Terminal) void { + if (t) |term| tr.rows.deinit(term); + tr.pins.deinit(gpa); + tr.hashes.deinit(gpa); + tr.meta.deinit(); + for (tr.known.items) |*k| k.addrs.deinit(gpa); + tr.known.deinit(gpa); + tr.cache.deinit(); + } + + fn find(tr: *const Track, version: u32) ?*const Known { + if (version == 0) return null; + for (tr.known.items) |*k| if (k.version == version) return if (k.epoch == tr.rows.epoch) k else null; + return null; + } + + /// A version of `t` as it is now. + fn advance(tr: *Track, gpa: std.mem.Allocator, t: *Terminal, changes: u32) !void { + try tr.rows.update(t); + tr.version +%= 1; + if (tr.version == 0) tr.version = 1; + tr.changes = changes; + tr.meta.clearRetainingCapacity(); + { + var rw: snapshot.record.Writer = .init(gpa, &tr.meta.writer); + defer rw.deinit(); + try snapshot.terminal.encode(t, &rw); + } + tr.meta_hash = std.hash.Wyhash.hash(0, tr.meta.written()); + tr.pins.clearRetainingCapacity(); + tr.hashes.clearRetainingCapacity(); + var it = t.screens.active.pages.rowIterator(.right_down, .{ .active = .{} }, null); + while (it.next()) |p| { + try tr.pins.append(gpa, p); + try tr.hashes.append(gpa, rowHash(p.node.page(), p.rowAndCell().row, t.cols)); + } + if (tr.known.items.len == kept) { + tr.known.items[0].addrs.deinit(gpa); + _ = tr.known.orderedRemove(0); + } + var k: Known = .{ .version = tr.version, .epoch = tr.rows.epoch, .top = tr.rows.top }; + errdefer k.addrs.deinit(gpa); + try k.addrs.ensureTotalCapacity(gpa, @intCast(tr.hashes.items.len + 1)); + for (tr.hashes.items) |h| k.addrs.putAssumeCapacity(h, {}); + k.addrs.putAssumeCapacity(tr.meta_hash, {}); + try tr.known.append(gpa, k); } +}; + +/// What the authority knows of one peer of one terminal. +pub const Peer = struct { + /// The version last sent, which the next delta is against; 0: none, so + /// the next starts the replica afresh. + sent: u32 = 0, + /// The versions in flight, oldest first. + flight: [bound]u32 = undefined, + n: u8 = 0, + /// Every version up to `seq` has landed. pub fn ack(peer: *Peer, seq: u32) void { - if (!peer.in_flight or seq != peer.seq) return; - std.mem.swap(std.AutoHashMapUnmanaged(Hash, void), &peer.known, &peer.sent); - peer.in_flight = false; + var i: u8 = 0; + while (i < peer.n and peer.flight[i] <= seq) i += 1; + std.mem.copyForwards(u32, peer.flight[0 .. peer.n - i], peer.flight[i..peer.n]); + peer.n -= i; } - /// Start the peer afresh (it joined again, or lost its state). + /// Start the peer afresh: it lost its state, or never had one. pub fn reset(peer: *Peer) void { - peer.fresh = true; - peer.in_flight = false; - peer.known.clearRetainingCapacity(); + peer.* = .{}; } }; -/// The delta's fixed header: u32 seq, u8 flags, u32 epoch, u64 meta, u16 -/// cursor x and y, u8 cursor style, u8 pending wrap, the kitty keyboard -/// flags, u64 top, u64 history rows; then u16 rows and a u64 per active -/// row, u16 and a u64 per row of the tail (those that scrolled off the -/// active area since the acknowledged state, up to a screen of them, oldest -/// first), and a u16 blob count and each blob as u64 hash, u32 length, bytes. +/// The delta's fixed header: u32 seq, u32 base (0: none, the replica +/// starts afresh), u8 flags, u32 epoch, u64 meta, u16 cursor x and y, u8 +/// cursor style, u8 pending wrap, u8 the cursor's semantic content, the +/// kitty keyboard flags, u64 top, u64 history rows, u64 the viewport's top +/// as an absolute row (all ones: following the active area), u16 and the +/// caller's extra bytes; then u16 rows and a u64 per active row, u16 and a +/// u64 per row of the tail (those that scrolled off the active area since +/// the base, up to a screen of them, oldest first), and a u16 blob count +/// and each blob as u64 hash, u32 length, bytes. const flag_reset: u8 = 1; const KittyFlags = @FieldType(Screen, "kitty_keyboard"); +const following = std.math.maxInt(u64); -/// The delta for `peer` against what it acknowledged, onto `out`, or false -/// (nothing written) while its last delta is in flight. -pub fn encode(e: *Encoder, peer: *Peer, n: *Rows, t: *Terminal, out: *std.Io.Writer) !bool { - if (peer.in_flight) return false; - const gpa = e.gpa; - try n.update(t); - if (peer.epoch != n.epoch) peer.reset(); - const reset = peer.fresh; - peer.sent.clearRetainingCapacity(); - - e.meta.clearRetainingCapacity(); - { - var rw: snapshot.record.Writer = .init(gpa, &e.meta.writer); - defer rw.deinit(); - try snapshot.terminal.encode(t, &rw); +/// The delta that takes `peer` from what it was last sent to `t` as it is, +/// onto `out`; false (nothing written) when it has that already, or has +/// `bound` in flight. `changes` counts the emulator's writes (a new count +/// is a new version); `extra` rides along (the caller's own state of the +/// terminal), the same for every peer of one version. A peer at the same +/// base as the last one encoded is sent the same bytes. +pub fn encode(e: *Encoder, tr: *Track, peer: *Peer, t: *Terminal, changes: u32, extra: []const u8, out: *std.Io.Writer) !bool { + if (tr.version == 0 or changes != tr.changes) try tr.advance(e.gpa, t, changes); + if (peer.sent == tr.version or peer.n == bound) return false; + const base = tr.find(peer.sent); + const base_version = if (base) |b| b.version else 0; + if (tr.cache_version != tr.version or tr.cache_base != base_version) { + tr.cache.clearRetainingCapacity(); + try write(e, tr, base, t, extra, &tr.cache.writer); + tr.cache_version = tr.version; + tr.cache_base = base_version; } - const meta = std.hash.Wyhash.hash(0, e.meta.written()); - try peer.sent.put(gpa, meta, {}); + try out.writeAll(tr.cache.written()); + peer.sent = tr.version; + peer.flight[peer.n] = tr.version; + peer.n += 1; + return true; +} +fn write(e: *Encoder, tr: *Track, base: ?*const Known, t: *Terminal, extra: []const u8, out: *std.Io.Writer) !void { + const gpa = e.gpa; + const reset = base == null; const screen = t.screens.active; const pages = &t.screens.get(.primary).?.pages; - try out.writeInt(u32, peer.seq +% 1, .little); + const history = pages.total_rows - pages.rows; + const top = tr.rows.top; + try out.writeInt(u32, tr.version, .little); + try out.writeInt(u32, if (base) |b| b.version else 0, .little); try out.writeByte(if (reset) flag_reset else 0); - try out.writeInt(u32, n.epoch, .little); - try out.writeInt(u64, meta, .little); + try out.writeInt(u32, tr.rows.epoch, .little); + try out.writeInt(u64, tr.meta_hash, .little); try out.writeInt(u16, screen.cursor.x, .little); try out.writeInt(u16, screen.cursor.y, .little); try out.writeByte(@intFromEnum(screen.cursor.cursor_style)); try out.writeByte(@intFromBool(screen.cursor.pending_wrap)); + try out.writeByte(@intFromEnum(screen.cursor.semantic_content)); // ponytail: raw bytes, as the hello refuses a peer of another build. try out.writeAll(std.mem.asBytes(&screen.kitty_keyboard)); - try out.writeInt(u64, n.top, .little); - const history = pages.total_rows - pages.rows; + try out.writeInt(u64, top, .little); try out.writeInt(u64, history, .little); - try out.writeInt(u16, t.rows, .little); + try out.writeInt(u64, if (pages.viewport == .active) following else top - history + pages.scrollbar().offset, .little); + try out.writeInt(u16, @intCast(extra.len), .little); + try out.writeAll(extra); - // The active rows, then the tail: what scrolled off since the state - // acknowledged, which may have changed after it was sent. - const tail: u16 = if (reset) 0 else @intCast(@min(n.top -| peer.top, t.rows, history)); - e.pins.clearRetainingCapacity(); - var it = screen.pages.rowIterator(.right_down, .{ .active = .{} }, null); - while (it.next()) |p| try e.pins.append(gpa, p); - for (0..tail) |j| try e.pins.append(gpa, pages.getTopLeft(.active).up(tail - j).?); + // The active rows, then the tail: what scrolled off since the base, + // which may have changed after it was sent. + e.listed.clearRetainingCapacity(); e.needed.clearRetainingCapacity(); - for (e.pins.items, 0..) |p, i| { - if (i == t.rows) try out.writeInt(u16, tail, .little); + const tail: u16 = if (base) |b| @intCast(@min(top -| b.top, t.rows, history)) else 0; + e.tail.clearRetainingCapacity(); + for (0..tail) |j| try e.tail.append(gpa, pages.getTopLeft(.active).up(tail - j).?); + try out.writeInt(u16, @intCast(tr.pins.items.len), .little); + for (tr.pins.items, tr.hashes.items) |p, h| { + try out.writeInt(u64, h, .little); + try needBlob(e, base, p, h); + } + try out.writeInt(u16, tail, .little); + for (e.tail.items) |p| { const h = rowHash(p.node.page(), p.rowAndCell().row, t.cols); try out.writeInt(u64, h, .little); - // Every address listed is held once this is acknowledged; a blob - // goes only for one not held already, once. - if ((try peer.sent.getOrPut(gpa, h)).found_existing or peer.known.contains(h)) continue; - try e.needed.append(gpa, .{ .pin = p, .hash = h }); + try needBlob(e, base, p, h); } - if (tail == 0) try out.writeInt(u16, 0, .little); - const meta_new = !peer.known.contains(meta); + const meta_new = if (base) |b| !b.addrs.contains(tr.meta_hash) else true; try out.writeInt(u16, @intCast(e.needed.items.len + @intFromBool(meta_new)), .little); if (meta_new) { - try out.writeInt(u64, meta, .little); - try out.writeInt(u32, @intCast(e.meta.written().len), .little); - try out.writeAll(e.meta.written()); + try out.writeInt(u64, tr.meta_hash, .little); + try out.writeInt(u32, @intCast(tr.meta.written().len), .little); + try out.writeAll(tr.meta.written()); } - var blob: std.Io.Writer.Allocating = .init(gpa); - defer blob.deinit(); for (e.needed.items) |need| { - blob.clearRetainingCapacity(); - try e.blob(need.pin.node.page(), need.pin.rowAndCell().row, t.cols, &blob.writer); + e.blob.clearRetainingCapacity(); + try e.rowBlob(need.pin.node.page(), need.pin.rowAndCell().row, t.cols, &e.blob.writer); try out.writeInt(u64, need.hash, .little); - try out.writeInt(u32, @intCast(blob.written().len), .little); - try out.writeAll(blob.written()); + try out.writeInt(u32, @intCast(e.blob.written().len), .little); + try out.writeAll(e.blob.written()); } - peer.top = n.top; - peer.seq +%= 1; - peer.in_flight = true; - peer.fresh = false; - peer.epoch = n.epoch; - return true; +} + +/// A blob goes for an address the base does not hold, once. +fn needBlob(e: *Encoder, base: ?*const Known, p: Pin, h: Hash) !void { + if ((try e.listed.getOrPut(e.gpa, h)).found_existing) return; + if (base) |b| if (b.addrs.contains(h)) return; + try e.needed.append(e.gpa, .{ .pin = p, .hash = h }); } /// The primary screen's rows `[lo, hi)` the authority still has, as u32 /// epoch, u32 count, then each as u64 row, u32 length, blob: a replica's /// fetch of history it holds as holes. // ponytail: every row its blob, none by address: phase 6's content cache. -pub fn encodeRows(e: *Encoder, n: *Rows, t: *Terminal, lo: u64, hi: u64, out: *std.Io.Writer) !void { - try n.update(t); +pub fn encodeRows(e: *Encoder, tr: *Track, t: *Terminal, lo: u64, hi: u64, out: *std.Io.Writer) !void { + try tr.rows.update(t); var blobs: std.Io.Writer.Allocating = .init(e.gpa); defer blobs.deinit(); var count: u32 = 0; var abs = lo; - while (abs < hi) : (abs += 1) { - const p = n.pinAt(t, abs) orelse continue; + // The active area is the deltas'. + while (abs < @min(hi, tr.rows.top)) : (abs += 1) { + const p = tr.rows.pinAt(t, abs) orelse continue; const start = blobs.written().len; try blobs.writer.writeInt(u64, abs, .little); try blobs.writer.writeInt(u32, 0, .little); - try e.blob(p.node.page(), p.rowAndCell().row, t.cols, &blobs.writer); + try e.rowBlob(p.node.page(), p.rowAndCell().row, t.cols, &blobs.writer); const len: u32 = @intCast(blobs.written().len - start - 12); std.mem.writeInt(u32, blobs.written()[start + 8 ..][0..4], len, .little); count += 1; } - try out.writeInt(u32, n.epoch, .little); + try out.writeInt(u32, tr.rows.epoch, .little); try out.writeInt(u32, count, .little); try out.writeAll(blobs.written()); } @@ -337,21 +442,32 @@ pub const Replica = struct { if (r.scratch) |*s| s.deinit(); } - /// Write one delta into `t`; the seq to acknowledge. - pub fn apply(r: *Replica, gpa: std.mem.Allocator, io: std.Io, t: *Terminal, bytes: []const u8) !u32 { + pub const Applied = struct { seq: u32, extra: []const u8 }; + + /// Write one delta into `t`: the seq to acknowledge, and the caller's + /// extra bytes (borrowed from `bytes`). error.BaseMismatch: the delta + /// is against a state this replica does not have (one was lost, or it + /// started afresh), so `t` is as it was and it must be sent a reset. + pub fn apply(r: *Replica, gpa: std.mem.Allocator, io: std.Io, t: *Terminal, bytes: []const u8) !Applied { var in: std.Io.Reader = .fixed(bytes); const seq = try in.takeInt(u32, .little); + const base = try in.takeInt(u32, .little); const flags = try in.takeByte(); + const reset = flags & flag_reset != 0; + if (!reset and base != r.seq) return error.BaseMismatch; const epoch = try in.takeInt(u32, .little); const meta = try in.takeInt(u64, .little); const cx = try in.takeInt(u16, .little); const cy = try in.takeInt(u16, .little); const style = try in.takeByte(); const wrap = try in.takeByte(); + const semantic = try in.takeByte(); var kitty: KittyFlags = undefined; @memcpy(std.mem.asBytes(&kitty), try in.take(@sizeOf(KittyFlags))); const top = try in.takeInt(u64, .little); const history = try in.takeInt(u64, .little); + const viewport = try in.takeInt(u64, .little); + const extra = try in.take(try in.takeInt(u16, .little)); const nrows = try in.takeInt(u16, .little); const hashes = try in.take(@as(usize, nrows) * 8); const tail = try in.takeInt(u16, .little); @@ -371,18 +487,21 @@ pub const Replica = struct { } }.of; - if (flags & flag_reset != 0 or meta != r.meta) { + if (reset or meta != r.meta) { var reader: std.Io.Reader = .fixed(try blobOf(r, &fresh, meta)); var next = try snapshot.terminal.decode(&reader, try snapshot.record.nextHeader(&reader), io, gpa); - if (flags & flag_reset != 0) { + if (reset) { std.mem.swap(Terminal, t, &next); next.deinit(gpa); for (&r.held) |*h| h.clearRetainingCapacity(); r.holes.clearRetainingCapacity(); r.top = top; r.epoch = epoch; - // The authority's history, blank, to be fetched. + // The authority's history, blank, to be fetched. Its own + // limit is the authority's row count (below), not bytes: + // its pages may pack rows otherwise. const primary = t.screens.get(.primary).?; + primary.pages.setMaxBytes(null); try scrollUp(primary, history); if (history > 0) try r.holes.append(gpa, .{ top -| history, top }); } else { @@ -394,16 +513,12 @@ pub const Replica = struct { if (epoch != r.epoch) return error.Renumbered; // The primary screen scrolled `top - r.top` rows since the last - // delta: as many go up into the history here (no more than it and - // the screen hold). - // ponytail: the history's extent then follows this terminal's own - // pruning, under the authority's limit: a page more or less than the - // authority's, and the oldest rows may be some it has dropped. The tail is what they are now; any older than - // the tail are holes, never seen or seen before they last changed. + // delta: as many go up into the history here (no more than its + // history holds and the screen), each beyond the tail a hole. const primary = t.screens.get(.primary).?; if (top > r.top) { const k = top - r.top; - try scrollUp(primary, @min(k, @as(u64, t.rows) + history + 1)); + try scrollUp(primary, @min(k, @as(u64, t.rows) + history)); const held = &r.held[0]; if (k >= held.items.len) held.clearRetainingCapacity() else { std.mem.copyForwards(Hash, held.items, held.items[@intCast(k)..]); @@ -412,17 +527,18 @@ pub const Replica = struct { if (top - tail > r.top) try r.holes.append(gpa, .{ r.top, top - tail }); r.top = top; } + // The history as long as the authority's (it prunes a page at a + // time, and an ED 3 takes it all): row numbers and the scrollbar + // say the same on both. ponytail: one pruned shorter than that + // (its pages packed otherwise) stays shorter. + const have = primary.pages.total_rows - primary.pages.rows; + if (have > history) primary.pages.eraseHistory(.{ .history = .{ .y = @intCast(have - history - 1) } }); + r.trimHoles(primary); for (0..tail) |j| { const p = pinOf(&primary.pages, top, top - tail + j) orelse continue; const h = std.mem.readInt(u64, tails[j * 8 ..][0..8], .little); if (rowHash(p.node.page(), p.rowAndCell().row, t.cols) != h) try r.writeRow(gpa, primary, p, try blobOf(r, &fresh, h)); } - // The authority's history went (an ED3): so does this one's. - if (history == 0 and primary.pages.total_rows > primary.pages.rows) { - primary.pages.eraseHistory(null); - r.holes.clearRetainingCapacity(); - } - r.trimHoles(primary); const screen = t.screens.active; const held = &r.held[@intFromEnum(t.screens.active_key)]; @@ -439,7 +555,10 @@ pub const Replica = struct { screen.cursorAbsolute(@min(cx, t.cols - 1), @min(cy, t.rows - 1)); screen.cursor.cursor_style = @enumFromInt(style); screen.cursor.pending_wrap = wrap != 0; + screen.cursor.semantic_content = @enumFromInt(semantic); screen.kitty_keyboard = kitty; + const oldest = r.top -| (primary.pages.total_rows - primary.pages.rows); + primary.pages.scroll(if (viewport == following) .active else .{ .row = @intCast(viewport -| oldest) }); // Hold the blobs of this state, and no others. var keep: std.AutoHashMapUnmanaged(Hash, []u8) = .empty; @@ -459,7 +578,7 @@ pub const Replica = struct { r.blobs.deinit(gpa); r.blobs = keep; r.seq = seq; - return seq; + return .{ .seq = seq, .extra = extra }; } /// The holes in `[lo, hi)`: the rows to fetch before showing them. @@ -471,6 +590,15 @@ pub const Replica = struct { } } + /// The absolute row the viewport of `t`'s primary screen starts at, and + /// how many it shows. + pub fn shown(r: *const Replica, t: *Terminal) [2]u64 { + const pages = &t.screens.get(.primary).?.pages; + const sb = pages.scrollbar(); + const oldest = r.top -| (pages.total_rows - pages.rows); + return .{ oldest + sb.offset, oldest + sb.offset + sb.len }; + } + /// Write fetched history rows (`encodeRows`) into `t`. pub fn applyRows(r: *Replica, gpa: std.mem.Allocator, t: *Terminal, bytes: []const u8) !void { var in: std.Io.Reader = .fixed(bytes); @@ -479,7 +607,9 @@ pub const Replica = struct { for (0..try in.takeInt(u32, .little)) |_| { const abs = try in.takeInt(u64, .little); const blob = try in.take(try in.takeInt(u32, .little)); - if (abs >= r.top) continue; + // Only a hole is written: a row the replica holds is the + // authority's already, and newer than what this fetch saw. + if (abs >= r.top or !r.inHole(abs)) continue; const p = pinOf(&primary.pages, r.top, abs) orelse continue; try r.writeRow(gpa, primary, p, blob); try r.fill(gpa, abs); @@ -535,13 +665,18 @@ pub const Replica = struct { }; /// `k` blank rows onto the bottom of a screen's active area, its top `k` -/// going up into the history, as `k` linefeeds at the bottom would. -// ponytail: a row at a time, so a flood costs the replica up to its whole -// history's rows a delta (measured in `bench`); a bulk scroll if that shows. +/// going up into the history, as `k` linefeeds at the bottom would. A +/// flood's thousands are grown a row at a time without the cursor (the +/// page list's fast path, a counter), which is put back after. fn scrollUp(screen: *Screen, k: u64) !void { if (k == 0) return; screen.cursorAbsolute(0, screen.pages.rows - 1); - for (0..@intCast(k)) |_| try screen.cursorDownScroll(); + if (k <= screen.pages.rows or screen.no_scrollback or screen.cursor.style_id != 0) { + for (0..@intCast(k)) |_| try screen.cursorDownScroll(); + return; + } + for (0..@intCast(k)) |_| _ = try screen.pages.grow(); + screen.cursorReload(); } /// `rowHash` of a row never written. @@ -601,13 +736,16 @@ fn dumpRow(page: *Page, row: *Row, cols: u16, w: *std.Io.Writer) !void { } /// The state a replica must match: the terminal-wide state, the cursor, -/// the active screen's rows. -fn dumpScreen(t: *Terminal, w: *std.Io.Writer) !void { +/// the history's extent and the viewport, the active screen's rows. +pub fn dumpScreen(t: *Terminal, w: *std.Io.Writer) !void { const screen = t.screens.active; - try w.print("{d}x{d} {s} cursor {d},{d} {s} {} title {s} pwd {s}\n", .{ - t.cols, t.rows, @tagName(t.screens.active_key), - screen.cursor.x, screen.cursor.y, @tagName(screen.cursor.cursor_style), - screen.cursor.pending_wrap, t.getTitle() orelse "", t.getPwd() orelse "", + const sb = t.screens.get(.primary).?.pages.scrollbar(); + try w.print("{d}x{d} {s} cursor {d},{d} {s} {} {s} title {s} pwd {s} scrollbar {d} {d} {d}\n", .{ + t.cols, t.rows, @tagName(t.screens.active_key), + screen.cursor.x, screen.cursor.y, @tagName(screen.cursor.cursor_style), + screen.cursor.pending_wrap, @tagName(screen.cursor.semantic_content), t.getTitle() orelse "", + t.getPwd() orelse "", sb.total, sb.offset, + sb.len, }); try w.writeAll(std.mem.asBytes(&t.modes.values)); try w.writeAll(std.mem.asBytes(&screen.kitty_keyboard)); @@ -619,31 +757,38 @@ fn dumpScreen(t: *Terminal, w: *std.Io.Writer) !void { } /// An authority and its replica, fed one stream of pty bytes, the deltas -/// and fetches between them at random: a delta encoded at a random step and -/// applied a random number of steps later (so the states between are -/// skipped), random resizes of the authority, random fetches of holes. -/// After each delta, the replica's screen is the authority's as it was -/// encoded, and every history row it holds outside its holes is what the -/// authority holds at that absolute row. +/// and fetches between them at random: a delta encoded at a random step +/// (so the states between are skipped), several in flight and each landing +/// a random number of steps later, in order, one lost now and then (the +/// replica refuses the next and is started afresh), random resizes of the +/// authority, random fetches of holes. After each delta, the replica's +/// screen is the authority's as it was encoded, and every history row it +/// holds outside its holes is what the authority holds at that absolute +/// row. pub const Soak = struct { gpa: std.mem.Allocator, io: std.Io, a: *Terminal, stream: vt.TerminalStream, - rows: Rows = .{}, + track: Track, enc: Encoder, peer: Peer = .{}, + /// The authority's writes, as terminal.State counts them. + changes: u32 = 0, b: *Terminal, rep: Replica = .{}, prng: std.Random.DefaultPrng, - /// The delta in flight, the authority's dump when it was encoded, and - /// how many steps it waits before it lands. - delta: std.Io.Writer.Allocating, - expect: std.Io.Writer.Allocating, + /// The deltas in flight, oldest first, each with the authority's dump + /// when it was encoded and the step it lands at. + flight: std.ArrayList(struct { delta: []u8, expect: []u8, at: u64 }) = .empty, got: std.Io.Writer.Allocating, - wait: u32 = 0, + now: u64 = 0, resizes: bool = true, - stats: struct { deltas: u64 = 0, bytes: u64 = 0, fetched: u64 = 0, history_checked: u64 = 0 } = .{}, + drops: bool = true, + /// The replica refused a delta and the authority started it afresh: + /// what was in flight before is refused too. + refused: bool = false, + stats: struct { deltas: u64 = 0, bytes: u64 = 0, fetched: u64 = 0, history_checked: u64 = 0, lost: u64 = 0, refused: u64 = 0, most_in_flight: u64 = 0 } = .{}, pub fn init(gpa: std.mem.Allocator, io: std.Io, cols: u16, rows: u16, scrollback: usize, seed: u64) !*Soak { const s = try gpa.create(Soak); @@ -651,17 +796,18 @@ pub const Soak = struct { const a = try gpa.create(Terminal); a.* = try Terminal.init(io, gpa, .{ .cols = cols, .rows = rows, .max_scrollback_bytes = scrollback }); const b = try gpa.create(Terminal); - b.* = try Terminal.init(io, gpa, .{ .cols = 2, .rows = 2, .max_scrollback_bytes = scrollback }); + // Room to spare: its pages may pack rows otherwise, and it keeps the + // authority's history to the row. + b.* = try Terminal.init(io, gpa, .{ .cols = 2, .rows = 2, .max_scrollback_bytes = 2 * scrollback }); s.* = .{ .gpa = gpa, .io = io, .a = a, .stream = a.vtStream(), + .track = .init(gpa), .enc = .init(gpa), .b = b, .prng = .init(seed), - .delta = .init(gpa), - .expect = .init(gpa), .got = .init(gpa), }; return s; @@ -670,16 +816,18 @@ pub const Soak = struct { pub fn deinit(s: *Soak) void { const gpa = s.gpa; s.stream.deinit(); - s.rows.deinit(s.a); + s.track.deinit(gpa, s.a); s.a.deinit(gpa); gpa.destroy(s.a); s.b.deinit(gpa); gpa.destroy(s.b); s.enc.deinit(); - s.peer.deinit(gpa); s.rep.deinit(gpa); - s.delta.deinit(); - s.expect.deinit(); + for (s.flight.items) |f| { + gpa.free(f.delta); + gpa.free(f.expect); + } + s.flight.deinit(gpa); s.got.deinit(); gpa.destroy(s); } @@ -691,6 +839,7 @@ pub const Soak = struct { while (at < bytes.len) { const n = @min(bytes.len - at, 1 + random.uintLessThan(usize, 512)); s.stream.nextSlice(bytes[at..][0..n]); + s.changes +%= 1; at += n; try s.step(); } @@ -698,48 +847,82 @@ pub const Soak = struct { pub fn step(s: *Soak) !void { const random = s.prng.random(); + s.now += 1; if (s.resizes and random.uintLessThan(u32, 200) == 0) { try s.a.resize(s.gpa, .{ .cols = 10 + random.uintLessThan(u16, 200), .rows = 3 + random.uintLessThan(u16, 60) }); + s.changes +%= 1; } - if (!s.peer.in_flight) { - if (random.boolean()) return; // a state skipped - s.delta.clearRetainingCapacity(); - if (!try encode(&s.enc, &s.peer, &s.rows, s.a, &s.delta.writer)) unreachable; - s.expect.clearRetainingCapacity(); - try dumpScreen(s.a, &s.expect.writer); - s.wait = random.uintLessThan(u32, 4); - s.stats.deltas += 1; - s.stats.bytes += s.delta.written().len; - return; - } - if (s.wait > 0) { - s.wait -= 1; - return; - } - try s.land(); + // A frame now and then; the states between are skipped. + if (random.boolean()) try s.send(s.now + random.uintLessThan(u64, 12)); + while (s.flight.items.len > 0 and s.flight.items[0].at <= s.now) try s.land(); if (random.uintLessThan(u32, 8) == 0) try s.fetch(); } - /// The delta in flight applied and acknowledged, and checked. - pub fn land(s: *Soak) !void { - if (!s.peer.in_flight) return; - s.peer.ack(try s.rep.apply(s.gpa, s.io, s.b, s.delta.written())); + /// A delta, if the peer is owed one, sent to land at step `at`. + fn send(s: *Soak, at: u64) !void { + var d: std.Io.Writer.Allocating = .init(s.gpa); + defer d.deinit(); + if (!try encode(&s.enc, &s.track, &s.peer, s.a, s.changes, "extra", &d.writer)) return; + var expect: std.Io.Writer.Allocating = .init(s.gpa); + defer expect.deinit(); + try dumpScreen(s.a, &expect.writer); + const last = if (s.flight.items.len > 0) s.flight.items[s.flight.items.len - 1].at else 0; + try s.flight.append(s.gpa, .{ .delta = try d.toOwnedSlice(), .expect = try expect.toOwnedSlice(), .at = @max(at, last) }); + s.stats.deltas += 1; + s.stats.bytes += s.flight.items[s.flight.items.len - 1].delta.len; + s.stats.most_in_flight = @max(s.stats.most_in_flight, s.flight.items.len); + } + + /// The oldest delta in flight applied (or lost), acknowledged, checked. + fn land(s: *Soak) !void { + const f = s.flight.orderedRemove(0); + defer s.gpa.free(f.delta); + defer s.gpa.free(f.expect); + if (s.drops and s.prng.random().uintLessThan(u32, 50) == 0) { + s.stats.lost += 1; + return; + } + const applied = s.rep.apply(s.gpa, s.io, s.b, f.delta) catch |err| switch (err) { + error.BaseMismatch => { + // Asked once: the authority starts it afresh. + if (!s.refused) s.peer.reset(); + s.refused = true; + s.stats.refused += 1; + return; + }, + else => return err, + }; + s.refused = false; + if (!std.mem.eql(u8, applied.extra, "extra")) return error.ExtraLost; + s.peer.ack(applied.seq); s.got.clearRetainingCapacity(); try dumpScreen(s.b, &s.got.writer); - if (!std.mem.eql(u8, s.expect.written(), s.got.written())) { - reportDiff(s.expect.written(), s.got.written()); + if (!std.mem.eql(u8, f.expect, s.got.written())) { + reportDiff(f.expect, s.got.written()); return error.ReplicaDiffers; } try s.checkHistory(); } + /// Everything in flight landed, then a last delta of the end, until the + /// replica holds it: what the replica shows at quiescence. + fn settle(s: *Soak) !void { + s.drops = false; + for (0..4) |_| { + while (s.flight.items.len > 0) try s.land(); + try s.send(0); + if (s.flight.items.len == 0) return; + } + return error.NeverSettled; + } + /// Every history row the replica holds, outside its holes and inside /// what the authority still has, is the authority's (rows in history /// never change; a resize renumbers and starts the replica afresh). fn checkHistory(s: *Soak) !void { // Numbered as of now: the authority may have scrolled on since. - try s.rows.update(s.a); - if (s.rep.epoch != s.rows.epoch) return; + try s.track.rows.update(s.a); + if (s.rep.epoch != s.track.rows.epoch) return; const pages = &s.b.screens.get(.primary).?.pages; const have = pages.total_rows - pages.rows; var ra: std.Io.Writer.Allocating = .init(s.gpa); @@ -751,7 +934,7 @@ pub const Soak = struct { const abs = s.rep.top - d; if (s.rep.inHole(abs)) continue; const pb = pinOf(pages, s.rep.top, abs) orelse continue; - const pa = s.rows.pinAt(s.a, abs) orelse continue; + const pa = s.track.rows.pinAt(s.a, abs) orelse continue; ra.clearRetainingCapacity(); rb.clearRetainingCapacity(); try dumpRow(pa.node.page(), pa.rowAndCell().row, s.a.cols, &ra.writer); @@ -774,7 +957,7 @@ pub const Soak = struct { const hi = @min(hole[1], lo + 1 + random.uintLessThan(u64, 80)); var out: std.Io.Writer.Allocating = .init(s.gpa); defer out.deinit(); - try encodeRows(&s.enc, &s.rows, s.a, lo, hi, &out.writer); + try encodeRows(&s.enc, &s.track, s.a, lo, hi, &out.writer); try s.rep.applyRows(s.gpa, s.b, out.written()); s.stats.fetched += hi - lo; try s.checkHistory(); @@ -795,18 +978,14 @@ fn reportDiff(want: []const u8, got: []const u8) void { } } -/// Drive one stream through a soak and land a last delta of its end. +/// Drive one stream through a soak, then settle: the replica's screen at +/// quiescence is the authority's. pub fn soak(gpa: std.mem.Allocator, io: std.Io, bytes: []const u8, cols: u16, rows: u16, scrollback: usize, seed: u64, resizes: bool) !@FieldType(Soak, "stats") { const s = try Soak.init(gpa, io, cols, rows, scrollback, seed); defer s.deinit(); s.resizes = resizes; try s.feed(bytes); - try s.land(); - s.delta.clearRetainingCapacity(); - _ = try encode(&s.enc, &s.peer, &s.rows, s.a, &s.delta.writer); - s.expect.clearRetainingCapacity(); - try dumpScreen(s.a, &s.expect.writer); - try s.land(); + try s.settle(); return s.stats; } @@ -841,7 +1020,7 @@ fn perf(gpa: std.mem.Allocator, io: std.Io, dir_path: []const u8, w: *std.Io.Wri failed += 1; continue; }; - if (seed == 0) try w.print("screen-state {s} {d}x{d}: {d} bytes, {d} deltas, {d} wire bytes, {d} history rows checked, {d} fetched\n", .{ entry.name, cols, rows, bytes.len, st.deltas, st.bytes, st.history_checked, st.fetched }); + if (seed == 0) try w.print("screen-state {s} {d}x{d}: {d} bytes, {d} deltas ({d} lost, {d} refused, {d} in flight at most), {d} wire bytes, {d} history rows checked, {d} fetched\n", .{ entry.name, cols, rows, bytes.len, st.deltas, st.lost, st.refused, st.most_in_flight, st.bytes, st.history_checked, st.fetched }); } } try w.print("screen-state corpus: {d} streams x 3 seeds, {d} failed\n", .{ files, failed }); @@ -858,8 +1037,6 @@ fn bench(gpa: std.mem.Allocator, io: std.Io, w: *std.Io.Writer) !void { defer stream.deinit(); var enc: Encoder = .init(gpa); defer enc.deinit(); - var n: Rows = .{}; - defer n.deinit(t); var out: std.Io.Writer.Allocating = .init(gpa); defer out.deinit(); var line: [1024]u8 = undefined; @@ -868,6 +1045,7 @@ fn bench(gpa: std.mem.Allocator, io: std.Io, w: *std.Io.Writer) !void { var same: [200]u64 = undefined; var full_bytes: usize = 0; var same_bytes: usize = 0; + var changes: u32 = 0; for (&full, &same, 0..) |*f, *u, rep| { for (0..60) |y| { var lw: std.Io.Writer = .fixed(&line); @@ -875,17 +1053,19 @@ fn bench(gpa: std.mem.Allocator, io: std.Io, w: *std.Io.Writer) !void { while (lw.end < 190) try lw.writeAll("\x1b[1mbold\x1b[22m text "); stream.nextSlice(lw.buffered()); } + changes += 1; + var tr: Track = .init(gpa); + defer tr.deinit(gpa, t); var peer: Peer = .{}; - defer peer.deinit(gpa); out.clearRetainingCapacity(); var t0 = std.Io.Clock.awake.now(io).nanoseconds; - _ = try encode(&enc, &peer, &n, t, &out.writer); + _ = try encode(&enc, &tr, &peer, t, changes, "", &out.writer); f.* = @intCast(std.Io.Clock.awake.now(io).nanoseconds - t0); full_bytes = out.written().len; - peer.ack(peer.seq); + // The same screen again as a new version: every row held. out.clearRetainingCapacity(); t0 = std.Io.Clock.awake.now(io).nanoseconds; - _ = try encode(&enc, &peer, &n, t, &out.writer); + _ = try encode(&enc, &tr, &peer, t, changes + 1, "", &out.writer); u.* = @intCast(std.Io.Clock.awake.now(io).nanoseconds - t0); same_bytes = out.written().len; } @@ -893,39 +1073,77 @@ fn bench(gpa: std.mem.Allocator, io: std.Io, w: *std.Io.Writer) !void { std.mem.sort(u64, &same, {}, std.sort.asc(u64)); try w.print("screen-state encode 200x60, every row new: p50 {d} us p99 {d} us, {d} bytes\n", .{ full[100] / 1000, full[198] / 1000, full_bytes }); try w.print("screen-state encode 200x60, nothing new: p50 {d} us p99 {d} us, {d} bytes\n", .{ same[100] / 1000, same[198] / 1000, same_bytes }); + try w.flush(); - // Floods: a second of output as fast as ghostty takes it, a delta every - // 16 ms when the last is acknowledged (by a replica applying it at once). + // Floods: a second of output as fast as ghostty takes it, a frame every + // 16 ms: one version, a delta for each peer (the same bytes for peers + // at the same base), each replica applying its own at once. const yes = try yesStream(gpa, 1 << 16); defer gpa.free(yes); const shell = try shellStream(gpa); defer gpa.free(shell); - for ([_]struct { name: []const u8, bytes: []const u8 }{ .{ .name = "yes", .bytes = yes }, .{ .name = "varied text", .bytes = shell } }) |flood| { - const s = try Soak.init(gpa, io, 200, 60, 16 << 20, 0); - defer s.deinit(); + for ([_]struct { name: []const u8, bytes: []const u8 }{ .{ .name = "yes", .bytes = yes }, .{ .name = "varied text", .bytes = shell } }) |flood| for ([_]usize{ 1, 3 }) |npeers| { + const a = try gpa.create(Terminal); + defer gpa.destroy(a); + a.* = try Terminal.init(io, gpa, .{ .cols = 200, .rows = 60, .max_scrollback_bytes = 16 << 20 }); + defer a.deinit(gpa); + var as = a.vtStream(); + defer as.deinit(); + var tr: Track = .init(gpa); + defer tr.deinit(gpa, a); + var peers: [3]Peer = @splat(.{}); + var reps: [3]Replica = @splat(.{}); + var bs: [3]Terminal = undefined; + for (bs[0..npeers]) |*b| b.* = try Terminal.init(io, gpa, .{ .cols = 2, .rows = 2, .max_scrollback_bytes = 32 << 20 }); + defer for (bs[0..npeers], reps[0..npeers]) |*b, *r| { + b.deinit(gpa); + r.deinit(gpa); + }; + var applies: std.ArrayList(u64) = .empty; + defer applies.deinit(gpa); + var frames: std.ArrayList(u64) = .empty; + defer frames.deinit(gpa); const start = std.Io.Clock.awake.now(io).nanoseconds; var next = start; var fed: u64 = 0; var deltas: u64 = 0; var wire: u64 = 0; - var apply_ns: u64 = 0; + var version: u32 = 0; while (true) { const now = std.Io.Clock.awake.now(io).nanoseconds; if (now - start >= std.time.ns_per_s) break; - s.stream.nextSlice(flood.bytes); + as.nextSlice(flood.bytes); + version += 1; fed += flood.bytes.len; if (now < next) continue; next += 16 * std.time.ns_per_ms; - s.delta.clearRetainingCapacity(); - _ = try encode(&s.enc, &s.peer, &s.rows, s.a, &s.delta.writer); - const a0 = std.Io.Clock.awake.now(io).nanoseconds; - s.peer.ack(try s.rep.apply(gpa, io, s.b, s.delta.written())); - apply_ns += @intCast(std.Io.Clock.awake.now(io).nanoseconds - a0); - deltas += 1; - wire += s.delta.written().len; + var frame_ns: u64 = 0; + for (peers[0..npeers], reps[0..npeers], bs[0..npeers]) |*peer, *rep, *b| { + out.clearRetainingCapacity(); + const e0 = std.Io.Clock.awake.now(io).nanoseconds; + if (!try encode(&enc, &tr, peer, a, version, "", &out.writer)) continue; + frame_ns += @intCast(std.Io.Clock.awake.now(io).nanoseconds - e0); + const a0 = std.Io.Clock.awake.now(io).nanoseconds; + peer.ack((try rep.apply(gpa, io, b, out.written())).seq); + try applies.append(gpa, @intCast(std.Io.Clock.awake.now(io).nanoseconds - a0)); + deltas += 1; + wire += out.written().len; + } + try frames.append(gpa, frame_ns); } - try w.print("screen-state flood of {s}: {d} MB/s of output, {d} deltas/s, {d} wire bytes/s ({d} a delta), replica apply {d} us a delta\n", .{ flood.name, fed / 1_000_000, deltas, wire, wire / @max(1, deltas), apply_ns / @max(1, deltas) / 1000 }); - } + std.mem.sort(u64, applies.items, {}, std.sort.asc(u64)); + std.mem.sort(u64, frames.items, {}, std.sort.asc(u64)); + const p = struct { + fn at(s: []const u64, q: usize) u64 { + return if (s.len == 0) 0 else s[@min(s.len - 1, s.len * q / 100)] / 1000; + } + }.at; + try w.print("screen-state flood of {s}, {d} peer(s): {d} MB/s of output, {d} deltas/s a peer, {d} wire bytes/s a peer ({d} a delta); encode a frame, all peers: p50 {d} us p99 {d} us; apply a delta p50 {d} us p99 {d} us\n", .{ + flood.name, npeers, fed / 1_000_000, deltas / npeers, wire / npeers, + wire / @max(1, deltas), p(frames.items, 50), p(frames.items, 99), p(applies.items, 50), + p(applies.items, 99), + }); + }; try w.flush(); } @@ -942,7 +1160,7 @@ test "recorded streams through replicas, and the encode's cost (PARDES_SCREEN_ST /// A shell's: prompts marked (OSC 133), colours of the three kinds, wide /// and combined graphemes, hyperlinks, tabs, a title and a directory, long /// lines that wrap, and a clear (ED2, ED3) now and then. -fn shellStream(gpa: std.mem.Allocator) ![]u8 { +pub fn shellStream(gpa: std.mem.Allocator) ![]u8 { var out: std.Io.Writer.Allocating = .init(gpa); errdefer out.deinit(); const w = &out.writer; @@ -965,7 +1183,7 @@ fn shellStream(gpa: std.mem.Allocator) ![]u8 { /// A full-screen program's: the alternate screen and its modes, a status /// line, a scrolling region scrolled both ways, lines inserted and /// deleted, cursor shapes, the kitty keyboard pushed and popped. -fn tuiStream(gpa: std.mem.Allocator) ![]u8 { +pub fn tuiStream(gpa: std.mem.Allocator) ![]u8 { var out: std.Io.Writer.Allocating = .init(gpa); errdefer out.deinit(); const w = &out.writer; @@ -983,7 +1201,7 @@ fn tuiStream(gpa: std.mem.Allocator) ![]u8 { return out.toOwnedSlice(); } -fn yesStream(gpa: std.mem.Allocator, lines: usize) ![]u8 { +pub fn yesStream(gpa: std.mem.Allocator, lines: usize) ![]u8 { const out = try gpa.alloc(u8, lines * 3); for (0..lines) |i| @memcpy(out[i * 3 ..][0..3], "y\r\n"); return out; @@ -1021,20 +1239,24 @@ test "under a flood a delta is a screen of hashes: an unchanged row costs its ad const s = try Soak.init(gpa, testing.io, 200, 60, 1 << 20, 1); defer s.deinit(); s.resizes = false; + s.drops = false; const yes = try yesStream(gpa, 10_000); defer gpa.free(yes); + var last: []u8 = &.{}; + defer gpa.free(last); for (0..3) |_| { s.stream.nextSlice(yes); - s.delta.clearRetainingCapacity(); - try testing.expect(try encode(&s.enc, &s.peer, &s.rows, s.a, &s.delta.writer)); - s.expect.clearRetainingCapacity(); - try dumpScreen(s.a, &s.expect.writer); + s.changes += 1; + try s.send(0); + try testing.expectEqual(@as(usize, 1), s.flight.items.len); + gpa.free(last); + last = try gpa.dupe(u8, s.flight.items[0].delta); try s.land(); } // The last: 60 rows and the tail's 60, of hashes the replica holds, no // blob; the header and counts around them. - const d = s.delta.written(); - const at = 4 + 1 + 4 + 8 + 2 + 2 + 1 + 1 + @sizeOf(KittyFlags) + 8 + 8; + const d = last; + const at = 4 + 4 + 1 + 4 + 8 + 2 + 2 + 1 + 1 + 1 + @sizeOf(KittyFlags) + 8 + 8 + 8 + 2 + "extra".len; const nrows = std.mem.readInt(u16, d[at..][0..2], .little); const tail = std.mem.readInt(u16, d[at + 2 + nrows * 8 ..][0..2], .little); const blobs = std.mem.readInt(u16, d[at + 4 + (nrows + tail) * 8 ..][0..2], .little); @@ -1044,6 +1266,27 @@ test "under a flood a delta is a screen of hashes: an unchanged row costs its ad try testing.expect(s.rep.holes.items.len > 0); } +test "several deltas in flight land in order, and a peer at the bound waits for an acknowledgement" { + const gpa = testing.allocator; + const s = try Soak.init(gpa, testing.io, 80, 24, 1 << 20, 2); + defer s.deinit(); + s.resizes = false; + s.drops = false; + var line: [64]u8 = undefined; + for (0..bound + 5) |i| { + s.stream.nextSlice(try std.fmt.bufPrint(&line, "line {d}\r\n", .{i})); + s.changes += 1; + try s.send(1000); + } + // The bound's worth went; the rest wait, and are skipped. + try testing.expectEqual(@as(usize, bound), s.flight.items.len); + try s.land(); + try s.send(1000); + try testing.expectEqual(@as(usize, bound), s.flight.items.len); + try s.settle(); + try testing.expectEqual(@as(u64, 0), s.stats.refused); +} + test "a replica's terminal pane draws as its authority's does" { const pardes = @import("pardes.zig"); const gpa = testing.allocator; @@ -1065,9 +1308,8 @@ test "a replica's terminal pane draws as its authority's does" { var enc: Encoder = .init(gpa); defer enc.deinit(); var peer: Peer = .{}; - defer peer.deinit(gpa); - var rows: Rows = .{}; - defer rows.deinit(ta); + var tr: Track = .init(gpa); + defer tr.deinit(gpa, ta); var rep: Replica = .{}; defer rep.deinit(gpa); var delta: std.Io.Writer.Allocating = .init(gpa); @@ -1077,8 +1319,8 @@ test "a replica's terminal pane draws as its authority's does" { const bytes = try std.fmt.bufPrint(&line, "\x1b[1;3{d}m{d}\x1b[0m 漢字 e\u{301} \x1b[48;2;{d};40;60mbg\x1b[0m and then some text that wraps past the width of the pane\r\n", .{ i % 8, i, i * 4 % 256 }); a.update(.{ .output = .{ .pane = 0, .bytes = bytes } }); delta.clearRetainingCapacity(); - try testing.expect(try encode(&enc, &peer, &rows, ta, &delta.writer)); - peer.ack(try rep.apply(gpa, terminalIoForTest(), tb, delta.written())); + try testing.expect(try encode(&enc, &tr, &peer, ta, @intCast(i + 1), "", &delta.writer)); + peer.ack((try rep.apply(gpa, terminalIoForTest(), tb, delta.written())).seq); b.needs_frame = true; _ = fa.reset(.retain_capacity); _ = fb.reset(.retain_capacity); diff --git a/src/tape.zig b/src/tape.zig index 84bdce29..0609ce0b 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -150,6 +150,17 @@ pub const Tape = struct { dump_check: bool = false, dumps: usize = 0, dump_bytes: usize = 0, + /// Screen-state terminals (src/screen_state.zig, docs/next-daemon.md + /// phase 5): an authority recording for replicas, or a replica. A + /// terminal's emulator is the authority's alone: its output is recorded + /// without its bytes (they cross as screen state), its pty writes are + /// not recorded, and no digest covers it. A replica's core never writes + /// one but by `apply`. + screen_state: bool = false, + /// A digest as one word (screen-state replicas: every unit of a pty's + /// output carries one, at the pty's read rate), not one a field; what + /// differs is then not named. + whole_digest: bool = false, fn record(t: *Tape, kind: Kind, comptime T: type, v: *const T) void { t.scratch.clearRetainingCapacity(); @@ -236,7 +247,12 @@ pub const Tape = struct { t.noteWrites(); t.wrap(core); } - t.record(.input, Input, input); + // A terminal's output crosses as screen state, not as its bytes. + if (t.screen_state and input.* == .update and input.update == .output) { + var bare = input.*; + bare.update.output.bytes = &.{}; + t.record(.input, Input, &bare); + } else t.record(.input, Input, input); return .{ .outer = true }; } @@ -251,7 +267,8 @@ pub const Tape = struct { t.record(.done, void, &{}); if ((t.depth == 0 or t.every) and !t.no_digest) if (t.core) |core| { var parts: Parts = undefined; - const d = digestParts(core, &parts); + const whole = [1]u64{digestOf(core)}; + const d: []const u64 = if (t.whole_digest) &whole else digestParts(core, &parts); t.record(.digest, []const u64, &d); }; if (t.depth == 0) t.flush(); @@ -376,8 +393,9 @@ pub const Tape = struct { const want = walk.decode([]const u64, r, t.arena.allocator()) catch return; const p = t.core orelse return; var parts: Parts = undefined; - const got = digestParts(p, &parts); - if (!std.mem.eql(u64, got, want)) t.diverge("digest {d} differs in {s}", .{ t.digests, partsDiffer(got, want, t.arena.allocator()) }); + const whole = [1]u64{digestOf(p)}; + const got: []const u64 = if (want.len == 1) &whole else digestParts(p, &parts); + if (!std.mem.eql(u64, got, want)) t.diverge("digest {d} differs in {s}", .{ t.digests, if (want.len == 1) "the session (a digest of one word: run the authority with whole_digest off to name the field)" else partsDiffer(got, want, t.arena.allocator()) }); t.digests += 1; } @@ -653,6 +671,12 @@ fn Wrap(comptime i: usize) type { fn hostCall(comptime i: usize, comptime R: type, ctx: ?*anyopaque, args: anytype) R { const t: *Tape = @ptrCast(@alignCast(ctx.?)); const name = host_fns[i]; + // A terminal's answers and what is typed at it from its output are the + // authority's to write, never a replica's: off the tape on both sides. + if (comptime std.mem.eql(u8, name, "pty_write")) if (t.screen_state and !t.speculating) { + if (!t.replay) @call(.auto, @field(t.real.vtable.*, name).?, .{t.real.ctx} ++ args); + return; + }; if (t.replay) { // A replica that never draws is never watched: it asks no one. if (comptime std.mem.eql(u8, name, "watched")) if (t.headless) return false; @@ -769,6 +793,17 @@ pub fn speculating() bool { return if (on) |t| t.speculating else false; } +/// Screen-state terminals, on either side (`Tape.screen_state`). +pub fn screenState() bool { + return if (on) |t| t.screen_state else false; +} + +/// A replica's core under screen-state terminals: its emulators are +/// written by `apply` alone, never by its own steps. +pub fn screenReplica() bool { + return if (on) |t| t.screen_state and (t.replay or t.speculating) else false; +} + /// What a speculation reached is the authority's: the input is undone and /// waits for the authority's order. pub fn poison() void { @@ -823,6 +858,16 @@ pub fn release(t: *Tape) void { if (t.kept_arena) |*k| k.deinit(); } +/// The session's digest recorded now: a batch of steps recorded without +/// one each (`no_digest`) checked once, at its end. +pub fn digestHere(t: *Tape) void { + const core = t.core orelse return; + var parts: Parts = undefined; + const whole = [1]u64{digestOf(core)}; + const d: []const u64 = if (t.whole_digest) &whole else digestParts(core, &parts); + t.record(.digest, []const u64, &d); +} + /// What was recorded since the last take; the tape's to reuse after. pub fn taken(t: *Tape) []const u8 { return t.buf.items; diff --git a/src/terminal.zig b/src/terminal.zig index bcb70110..6c3eeda7 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -158,6 +158,9 @@ pub const State = struct { mouse_cell: if (enabled) ?ghostty_vt.Coordinate else void = if (enabled) null else {}, /// The kitty graphics its program placed, as drawn (TerminalImages.zig). images: TerminalImages = if (images_enabled) .{} else {}, + /// Counts the emulator's writes (output, a resize, its view scrolled): + /// a new count is a new state for its replicas (screen_state.Track). + changes: u32 = 0, /// The emulator as its program has made it, for the replay digest /// (src/walk.zig): the active screen's text, how much history is above @@ -168,6 +171,15 @@ pub const State = struct { /// a replica's terminal is the authority's screen state from phase 5 /// on (docs/next-daemon.md). pub fn digest(s: *const State, h: *pardes.walk.Hasher) void { + if (comptime !enabled) return; + // Under screen-state terminals the emulator is the authority's, and + // a replica's is what the last delta made it: compared as screens + // (test/replicas.zig), not step by step. + if (pardes.tape.screenState()) return; + s.digestScreen(h); + } + + pub fn digestScreen(s: *const State, h: *pardes.walk.Hasher) void { if (comptime !enabled) return; const screen = s.vt.screens.active; const sb = screen.pages.scrollbar(); @@ -531,6 +543,41 @@ pub fn panFor(p: *const Pardes, pane: *const Pane, r: pardes.Rect) u16 { return @intCast(@min(pane.rows - shown, (at + 1) -| shown)); } +/// What pardes made of a terminal's stream that its emulator does not hold +/// (the OSC 133 cycle, MarkedHandler), for a replica's delta: phase u8, +/// started, prompts, declined u32, continuation and lost u8, a finished +/// command's seq u32 and status i32 (all ones: none; i32 min: no status). +// ponytail: the output pins stay the authority's (its 9P pty/run reads them). +pub const marks_len = 1 + 4 * 3 + 2 + 4 + 4; + +pub fn marksOf(state: *const State, buf: *[marks_len]u8) []const u8 { + if (comptime !enabled) return &.{}; + const m = &state.stream.handler; + var w: std.Io.Writer = .fixed(buf); + w.writeByte(@intFromEnum(m.phase)) catch unreachable; + inline for (.{ m.started, m.prompts, m.declined }) |n| w.writeInt(u32, n, .little) catch unreachable; + w.writeAll(&.{ @intFromBool(m.continuation), @intFromBool(m.lost) }) catch unreachable; + w.writeInt(u32, if (m.finished) |f| f.seq else std.math.maxInt(u32), .little) catch unreachable; + w.writeInt(i32, if (m.finished) |f| f.status orelse std.math.minInt(i32) else 0, .little) catch unreachable; + return w.buffered(); +} + +/// A replica's terminal takes its authority's marks (`marksOf`). +pub fn takeMarks(state: *State, bytes: []const u8) void { + if (comptime !enabled) return; + if (bytes.len != marks_len) return; + const m = &state.stream.handler; + m.phase = @enumFromInt(bytes[0]); + m.started = std.mem.readInt(u32, bytes[1..5], .little); + m.prompts = std.mem.readInt(u32, bytes[5..9], .little); + m.declined = std.mem.readInt(u32, bytes[9..13], .little); + m.continuation = bytes[13] != 0; + m.lost = bytes[14] != 0; + const seq = std.mem.readInt(u32, bytes[15..19], .little); + const status = std.mem.readInt(i32, bytes[19..23], .little); + m.finished = if (seq == std.math.maxInt(u32)) null else .{ .seq = seq, .status = if (status == std.math.minInt(i32)) null else status }; +} + /// A replica's speculation leaves an emulator be: it is the authority's /// (docs/next-daemon.md, phase 5), so the input waits for its order. fn authorityOnly() bool { @@ -545,6 +592,7 @@ pub fn scrollGrid(pane: *Pane, delta: i32) void { if (authorityOnly()) return; const state = pane.terminal orelse return; state.vt.screens.active.scroll(.{ .delta_row = delta }); + state.changes +%= 1; } /// Is the viewport already showing the last row of output? @@ -559,7 +607,9 @@ pub fn followOutput(pane: *Pane) void { if (comptime !enabled) return; if (authorityOnly()) return; const state = pane.terminal orelse return; + const moved = state.vt.screens.active.pages.viewport != .active; state.vt.screens.active.scroll(.active); + state.changes +%= @intFromBool(moved); } /// Reflow the grid. A failed reflow keeps the grid it had rather than dropping @@ -572,6 +622,9 @@ pub fn resizeGrid(pane: *Pane, gpa: std.mem.Allocator, cols: u16, rows: u16) voi pane.clearPointerSelection(button); pane.sel[button].state = .none; }; + // A replica's emulator is sized by its authority: the next delta says. + if (pardes.tape.screenReplica()) return; + state.changes +%= 1; state.vt.resize(gpa, .{ .cols = cols, .rows = rows }) catch {}; } @@ -801,6 +854,7 @@ pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { }); state.stream = .init(.{ .handler = .{ .inner = state.vt.vtHandler() }, .allocator = gpa }); state.reply_len = 0; + state.changes = 0; // Every field its default (create leaves them what the heap had, which // a digest then reads: two replicas must agree, src/replica.zig). state.reply_dropped = 0; @@ -856,6 +910,7 @@ pub fn reviveShell(pane: *Pane) void { /// then apply different views. fn ingest(pane: *Pane, bytes: []const u8) void { pane.terminal.?.stream.nextSlice(bytes); + pane.terminal.?.changes +%= 1; } test "replay ownership is terminal-only and construction rolls back on allocation failure" { @@ -1017,6 +1072,8 @@ pub fn feedOutput(p: *Pardes, pane: *Pane, bytes: []const u8) void { if (comptime !enabled) return; if (pane.terminal == null) return; if (authorityOnly()) return; + // A replica's emulator is what the authority's deltas make it. + if (pardes.tape.screenReplica()) return; const has_positions = pane.ovl != null or pane.body.cur_pinned or pane.body.vsel.active or pane.body.msel.active or pane.body.nsel > 0 or pane.body.last_edit != null or pane.look_at != null or pane.body.ed_undo_len > 0 or pane.body.ed_redo_len > 0 or pointerRowCount(pane) > 0; diff --git a/test/replicas.zig b/test/replicas.zig index cdb161e8..f3618cf1 100644 --- a/test/replicas.zig +++ b/test/replicas.zig @@ -5,18 +5,30 @@ //! puts them all in one, where they clash at every key: a stress of //! correctness, not a picture of a session. //! -//! zig build replicas -Dplatform=tty -- [--seeds N] [--steps M] [--clients C] [--rtt 30,80,207] [--first K] [--shared] +//! Terminals cross as screen state (src/screen_state.zig): the session's +//! shell pane runs a full-screen program and then a `yes` flood (`--flood` +//! MB of it) while the clients type, its output stepped by the authority +//! and sent to each replica as deltas, once a frame and before any input +//! the authority steps, each replica acknowledging them over its link and +//! asking for the rows it scrolls back into and does not hold. +//! +//! zig build replicas -Dplatform=tty -- [--seeds N] [--steps M] [--clients C] [--rtt 30,80,207] [--first K] [--flood MB] [--shared] //! //! Gates, per seed and RTT: no replica diverges (every confirmed unit's //! digests agree as it is replayed), and at quiescence every replica's -//! digest is the authority's. Reported per RTT: how inputs went (applied at -//! once, stopped at a host call, left to the authority), how units were -//! taken (fast path, rollback for a misprediction or another's input), and -//! what a speculative apply, a copy and a rollback cost against a frame. +//! digest is the authority's; every delta a replica applies leaves its +//! terminal what the authority's was at that version, and at quiescence it +//! is the authority's, cell for cell. Reported per RTT: how inputs went +//! (applied at once, stopped at a host call, left to the authority), how +//! units were taken (fast path, rollback for a misprediction or another's +//! input), what a speculative apply, a copy and a rollback cost against a +//! frame, and the terminals': deltas a second, wire bytes a second under +//! the flood, encode a frame (every peer), apply a delta. const std = @import("std"); const libc = std.c; const pardes = @import("pardes"); const replica = pardes.replica; +const screen_state = pardes.screen_state; const Key = pardes.Key; const gpa = std.heap.c_allocator; @@ -46,7 +58,7 @@ const Host = struct { } }; -const Kind = enum { input, to_authority, to_replica, echo }; +const Kind = enum { input, to_authority, to_replica, echo, frame, output, screen_down, screen_up }; const Msg = struct { at: u64, @@ -89,6 +101,21 @@ const Totals = struct { spec_ns: std.ArrayListUnmanaged(u64) = .empty, copy_ns: std.ArrayListUnmanaged(u64) = .empty, rollback_ns: std.ArrayListUnmanaged(u64) = .empty, + // Terminals. + apply_ns: std.ArrayListUnmanaged(u64) = .empty, + frame_ns: std.ArrayListUnmanaged(u64) = .empty, + deltas: u64 = 0, + delta_bytes: u64 = 0, + unit_bytes: u64 = 0, + /// Deltas the replicas applied while the flood ran. + flood_deltas: u64 = 0, + flood_us: u64 = 0, + flood_delta_bytes: u64 = 0, + flood_unit_bytes: u64 = 0, + fetched: u64 = 0, + resets: u64 = 0, + screens_wrong: u64 = 0, + screens_checked: u64 = 0, fn add(t: *Totals, s: *const replica.Stats) !void { t.inputs += s.inputs; @@ -105,14 +132,16 @@ const Totals = struct { try t.spec_ns.appendSlice(gpa, s.spec_ns.items); try t.copy_ns.appendSlice(gpa, s.copy_ns.items); try t.rollback_ns.appendSlice(gpa, s.rollback_ns.items); + try t.apply_ns.appendSlice(gpa, s.apply_ns.items); } }; -/// One client's next input: monkey.zig's mix, as events. -fn nextEvent(random: std.Random, buf: *[64]u8) pardes.Event { +/// One client's next input: monkey.zig's mix, as events, and now and then +/// a wheel over the shell pane, which scrolls its history into view. +fn nextEvent(random: std.Random, buf: *[64]u8, wheel_at: ?[2]u16) pardes.Event { const typed = "abcdefghijklmnopqrstuvwxyz0123456789 -_=+,:;()"; const normal = "hjklwbe0$xpuyoOiaAIdcv.nN%sC"; - switch (random.uintLessThan(u8, 40)) { + switch (random.uintLessThan(u8, 42)) { 0...15 => { buf[0] = typed[random.uintLessThan(usize, typed.len)]; return .{ .key = .{ .cp = buf[0], .text = buf[0..1] } }; @@ -143,7 +172,11 @@ fn nextEvent(random: std.Random, buf: *[64]u8) pardes.Event { } }, 37 => return .{ .resize = .{ .cols = @intCast(random.intRangeAtMost(u16, 60, 120)), .rows = @intCast(random.intRangeAtMost(u16, 16, 40)) } }, 38 => return .{ .mouse = .{ .button = .left, .kind = .press, .col = random.uintLessThan(u16, 80), .row = random.uintLessThan(u16, 24) } }, - else => return .{ .mouse = .{ .button = .left, .kind = .release, .col = random.uintLessThan(u16, 80), .row = random.uintLessThan(u16, 24) } }, + 39 => return .{ .mouse = .{ .button = .left, .kind = .release, .col = random.uintLessThan(u16, 80), .row = random.uintLessThan(u16, 24) } }, + else => { + const at = wheel_at orelse return .{ .key = .{ .cp = Key.escape } }; + return .{ .mouse = .{ .button = if (random.uintLessThan(u8, 3) == 0) .wheel_down else .wheel_up, .kind = .press, .col = at[0], .row = at[1] } }; + }, } } @@ -158,7 +191,16 @@ fn writeFile(path: [:0]const u8, lines: usize) !void { } } -const Run = struct { seed: u64, rtt_ms: u64, steps: usize, clients: u16, shared: bool }; +const Run = struct { seed: u64, rtt_ms: u64, steps: usize, clients: u16, shared: bool, flood_mb: u64 }; + +/// The terminal pane's screen as a hash of screen_state's dump of it. +fn screenHash(t: *pardes.panes.terminal.VtSlot) u64 { + var buf: [8192]u8 = undefined; + var h: std.Io.Writer.Hashing(std.hash.Wyhash) = .initHasher(.init(0), &buf); + screen_state.dumpScreen(t, &h.writer) catch {}; + h.writer.flush() catch {}; + return h.hasher.final(); +} fn run(r: Run, dir: []const u8, totals: *Totals) !void { if (trace) std.debug.print("seed {d} rtt {d} ms\n", .{ r.seed, r.rtt_ms }); @@ -176,13 +218,22 @@ fn run(r: Run, dir: []const u8, totals: *Totals) !void { } // The tty's own boot (a raw shell, a text pane under it), then three // files looked at: keys go to the last, a pane's shell echoes. - const a = try replica.Authority.init(gpa, .{ .cols = 100, .rows = 30, .boot = .tty }, host.host()); + const a = try replica.Authority.init(gpa, .{ .cols = 100, .rows = 30, .boot = .tty }, host.host(), true); defer a.deinit(); + var screens: replica.Screens = .init(gpa); + defer screens.deinit(a.core); const first = try gpa.dupe(u8, try a.unit(0, 0)); defer gpa.free(first); var reps: [8]*replica.Replica = undefined; - for (reps[0..r.clients], 1..) |*rp, id| rp.* = try replica.Replica.init(gpa, @intCast(id), first); + var peers: [8]u16 = undefined; + for (reps[0..r.clients], peers[0..r.clients], 1..) |*rp, *peer, id| { + rp.* = try replica.Replica.init(gpa, @intCast(id), first, true); + peer.* = try screens.addPeer(); + } defer for (reps[0..r.clients]) |rp| rp.deinit(); + const shell: u8 = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; for (&paths) |*path| { var line: [300]u8 = undefined; const unit = try a.local(.{ .command = try std.fmt.bufPrint(&line, "Look {s}", .{std.mem.sliceTo(path, 0)}) }); @@ -195,26 +246,87 @@ fn run(r: Run, dir: []const u8, totals: *Totals) !void { rp.view = opened.view; for (reps[0..r.clients]) |each| each.confirm(opened.unit); }; + // Where a wheel over the shell lands in the session's view (the first + // client's); the others' views put it elsewhere, and their wheels hit + // whatever is there. + const sr = a.core.rects[shell]; + const wheel_at: ?[2]u16 = if (sr.h > 2) .{ sr.x + sr.w / 2, sr.y + sr.h / 2 } else null; + + // What the shell's program prints: a full-screen program, then a flood + // of `yes`, read 4 KB at a time as a pty gives it. + const tui = try screen_state.tuiStream(gpa); + defer gpa.free(tui); + const yes = try screen_state.yesStream(gpa, @intCast(r.flood_mb * (1 << 20) / 3)); + defer gpa.free(yes); + const tui_at: u64 = 400_000; + const flood_at: u64 = 1_500_000; + var flood_end: u64 = 0; + var program: struct { bytes: []const u8 = &.{}, at: usize = 0 } = .{}; + // The authority's version of each terminal, as its screen showed then. + var versions: std.AutoHashMapUnmanaged(u64, u64) = .empty; + defer versions.deinit(gpa); + var flood_deltas: [8]u64 = @splat(0); + var queue: std.PriorityQueue(Msg, void, Msg.order) = .empty; - defer queue.deinit(gpa); + defer { + while (queue.pop()) |m| if (m.bytes.len > 0) gpa.free(m.bytes); + queue.deinit(gpa); + } var n: u64 = 0; const one_way = r.rtt_ms * 1000 / 2; const jitter = one_way / 10; var up: [8]Link = @splat(.{}); var down: [8]Link = @splat(.{}); var left: [8]usize = @splat(r.steps); - // Each client's first input, soon. - for (0..r.clients) |c| { - try queue.push(gpa, .{ .at = random.uintLessThan(u64, 200_000), .n = n, .kind = .input, .who = @intCast(c + 1), .bytes = &.{} }); - n += 1; - } + var typing: usize = r.clients; + const push = struct { + fn f(q: *std.PriorityQueue(Msg, void, Msg.order), nn: *u64, m: Msg) !void { + var mm = m; + mm.n = nn.*; + nn.* += 1; + try q.push(gpa, mm); + } + }.f; + // Each client's first input, soon; the programs; the first frame. + for (0..r.clients) |c| try push(&queue, &n, .{ .at = random.uintLessThan(u64, 200_000), .n = 0, .kind = .input, .who = @intCast(c + 1), .bytes = &.{} }); + try push(&queue, &n, .{ .at = tui_at, .n = 0, .kind = .output, .who = 0, .bytes = &.{} }); + try push(&queue, &n, .{ .at = flood_at, .n = 0, .kind = .output, .who = 1, .bytes = &.{} }); + try push(&queue, &n, .{ .at = frame_us, .n = 0, .kind = .frame, .who = 0, .bytes = &.{} }); + var frames_left: u32 = 0; + var flood_units: u64 = 0; + + // What each peer is owed of the terminals, onto its link: before the + // authority steps an input, and once a frame. + var owed: std.ArrayListUnmanaged([]u8) = .empty; + defer owed.deinit(gpa); + const flushAll = struct { + fn f(sc: *replica.Screens, core: *pardes.Pardes, ps: []const u16, list: *std.ArrayListUnmanaged([]u8), q: *std.PriorityQueue(Msg, void, Msg.order), nn: *u64, now: u64, dl: []Link, rnd: std.Random, ow: u64, jt: u64, vs: *std.AutoHashMapUnmanaged(u64, u64), tot: *Totals) !void { + for (ps, 0..) |peer, c| { + list.clearRetainingCapacity(); + try sc.flush(core, peer, list); + for (list.items) |bytes| { + tot.deltas += 1; + tot.delta_bytes += bytes.len; + try push(q, nn, .{ .at = dl[c].arrive(rnd, now, ow, jt), .n = 0, .kind = .screen_down, .who = @intCast(c + 1), .bytes = bytes }); + } + } + // Each version as the authority's screen showed it. + for (&sc.tracks, 0..) |*slot, id| if (slot.*) |*tr| { + const key = @as(u64, tr.serial) << 32 | tr.track.version; + if (tr.track.version == 0 or vs.contains(key)) continue; + _ = core.panes[id] orelse continue; + try vs.put(gpa, key, screenHash(&tr.state.vt)); + }; + } + }.f; + while (queue.pop()) |m| { defer if (m.bytes.len > 0) gpa.free(m.bytes); switch (m.kind) { .input => { const c = m.who - 1; var buf: [64]u8 = undefined; - const ev = nextEvent(random, &buf); + const ev = nextEvent(random, &buf, if (c == 0) wheel_at else null); const before = reps[c].stats; const msg = try gpa.dupe(u8, try reps[c].input(ev)); if (trace) std.debug.print("{d} ms client {d}: {t} {s} -> {s}\n", .{ m.at / 1000, m.who, ev, switch (ev) { @@ -223,39 +335,96 @@ fn run(r: Run, dir: []const u8, totals: *Totals) !void { .command => |t| t, else => "", }, if (reps[c].stats.speculated > before.speculated) "applied" else if (reps[c].stats.stopped > before.stopped) "STOPPED" else if (reps[c].stats.unspeculated > before.unspeculated) "view" else "waits" }); - try queue.push(gpa, .{ .at = up[c].arrive(random, m.at, one_way, jitter), .n = n, .kind = .to_authority, .who = m.who, .bytes = msg }); - n += 1; + try push(&queue, &n, .{ .at = up[c].arrive(random, m.at, one_way, jitter), .n = 0, .kind = .to_authority, .who = m.who, .bytes = msg }); left[c] -= 1; if (left[c] > 0) { // A typist: keys 50-200 ms apart, a pause now and then. const gap: u64 = if (random.uintLessThan(u8, 12) == 0) random.intRangeAtMost(u64, 300_000, 1_500_000) else random.intRangeAtMost(u64, 50_000, 200_000); - try queue.push(gpa, .{ .at = m.at + gap, .n = n, .kind = .input, .who = m.who, .bytes = &.{} }); - n += 1; - } + try push(&queue, &n, .{ .at = m.at + gap, .n = 0, .kind = .input, .who = m.who, .bytes = &.{} }); + } else typing -= 1; }, - .to_authority, .echo => { - const unit = if (m.kind == .echo) - try a.local(.{ .output = .{ .pane = @intCast(m.who), .bytes = m.bytes } }) - else - try a.take(m.bytes); - for (0..r.clients) |c| { - const copy = try gpa.dupe(u8, unit); - try queue.push(gpa, .{ .at = down[c].arrive(random, m.at, one_way, jitter), .n = n, .kind = .to_replica, .who = @intCast(c + 1), .bytes = copy }); - n += 1; - } - for (host.echo.items) |e| { - try queue.push(gpa, .{ .at = m.at + 2_000, .n = n, .kind = .echo, .who = e.pane, .bytes = e.bytes }); - n += 1; - } + .to_authority, .echo, .output => { + const unit = switch (m.kind) { + .echo => echo: { + a.output(.{ .output = .{ .pane = @intCast(m.who), .bytes = m.bytes } }); + break :echo null; + }, + .output => out: { + // The program's next read, 4 KB, then the next a + // fraction of a millisecond on (ghostty's pace). + if (program.at == program.bytes.len) { + program = .{ .bytes = if (m.who == 0) tui else yes }; + } + const k = @min(program.bytes.len - program.at, if (m.who == 0) @as(usize, 700) else 4096); + const chunk = program.bytes[program.at..][0..k]; + program.at += k; + if (program.at < program.bytes.len) + try push(&queue, &n, .{ .at = m.at + if (m.who == 0) @as(u64, 3_000) else 450, .n = 0, .kind = .output, .who = m.who, .bytes = &.{} }) + else if (m.who == 1) flood_end = m.at; + if (m.who == 1) flood_units += 1; + a.output(.{ .output = .{ .pane = shell, .bytes = chunk } }); + break :out null; + }, + else => in: { + // The authority's own steps since the last unit, and + // the terminals as they are: what the input's step + // reads, before it is stepped. + if (try a.pending()) |batch| try sendUnit(&queue, &n, batch, m.at, down[0..r.clients], random, one_way, jitter, totals, m.at >= flood_at and flood_end == 0); + try flushAll(&screens, a.core, peers[0..r.clients], &owed, &queue, &n, m.at, &down, random, one_way, jitter, &versions, totals); + break :in try a.take(m.bytes); + }, + }; + if (unit) |u| try sendUnit(&queue, &n, u, m.at, down[0..r.clients], random, one_way, jitter, totals, false); + for (host.echo.items) |e| try push(&queue, &n, .{ .at = m.at + 2_000, .n = 0, .kind = .echo, .who = e.pane, .bytes = e.bytes }); host.echo.clearRetainingCapacity(); }, - .to_replica => { - const rp = reps[m.who - 1]; - rp.confirm(m.bytes); - rp.settle(); + .frame => { + if (try a.pending()) |batch| try sendUnit(&queue, &n, batch, m.at, down[0..r.clients], random, one_way, jitter, totals, m.at >= flood_at and flood_end == 0); + const t0 = screens.encode_ns; + const before = totals.delta_bytes; + try flushAll(&screens, a.core, peers[0..r.clients], &owed, &queue, &n, m.at, &down, random, one_way, jitter, &versions, totals); + if (screens.encode_ns > t0) try totals.frame_ns.append(gpa, screens.encode_ns - t0); + if (m.at >= flood_at and (flood_end == 0 or m.at <= flood_end)) totals.flood_delta_bytes += totals.delta_bytes - before; + // Frames go on while anything moves, and a second after. + const busy = typing > 0 or flood_end == 0 or queue.count() > 0; + frames_left = if (busy) 60 else frames_left -| 1; + if (frames_left > 0) try push(&queue, &n, .{ .at = m.at + frame_us, .n = 0, .kind = .frame, .who = 0, .bytes = &.{} }); + }, + .to_replica, .screen_down => { + const c = m.who - 1; + const rp = reps[c]; + if (m.kind == .to_replica) { + rp.confirm(m.bytes); + rp.settle(); + } else if (rp.screen(m.bytes)) |reply| { + const pane = m.bytes[0]; + if (reply[5] == 1) { + // Applied: the screen is the authority's at that version. + const sc = rp.screens[pane].?; + const vt = &rp.core().panes[pane].?.terminal.?.vt; + totals.screens_checked += 1; + if (versions.get(@as(u64, sc.serial) << 32 | sc.rep.seq)) |want| { + if (want != screenHash(vt)) { + totals.screens_wrong += 1; + std.debug.print("seed {d} rtt {d} ms: replica {d}'s terminal {d} at version {d} is not the authority's\n", .{ r.seed, r.rtt_ms, m.who, pane, sc.rep.seq }); + } + } else totals.screens_wrong += 1; + // Deltas while the flood runs (an RTT/2 behind it). + if (m.at >= flood_at + one_way and (flood_end == 0 or m.at <= flood_end + one_way)) flood_deltas[c] += 1; + } else totals.resets += 1; + try push(&queue, &n, .{ .at = up[c].arrive(random, m.at, one_way, jitter), .n = 0, .kind = .screen_up, .who = m.who, .bytes = try gpa.dupe(u8, reply) }); + } + if (rp.screenRequest()) |req| try push(&queue, &n, .{ .at = up[c].arrive(random, m.at, one_way, jitter), .n = 0, .kind = .screen_up, .who = m.who, .bytes = try gpa.dupe(u8, req) }); + }, + .screen_up => { + const c = m.who - 1; + if (m.bytes[5] == 2) totals.fetched += std.mem.readInt(u64, m.bytes[14..22], .little) - std.mem.readInt(u64, m.bytes[6..14], .little); + if (try screens.take(a.core, peers[c], m.bytes)) |answer| try push(&queue, &n, .{ .at = down[c].arrive(random, m.at, one_way, jitter), .n = 0, .kind = .screen_down, .who = m.who, .bytes = answer }); }, } } + totals.flood_us += flood_end -| flood_at; + for (flood_deltas[0..r.clients]) |d| totals.flood_deltas += d; const want = replica.digestOf(a); for (reps[0..r.clients], 1..) |rp, id| { try totals.add(&rp.stats); @@ -266,6 +435,29 @@ fn run(r: Run, dir: []const u8, totals: *Totals) !void { totals.unequal += 1; std.debug.print("seed {d} rtt {d} ms: replica {d} at quiescence: {s}\n", .{ r.seed, r.rtt_ms, id, if (rp.quiet()) "another digest" else "inputs still pending" }); } + // At quiescence, every terminal the authority's, cell for cell. + for (a.core.panes, 0..) |slot, pid| { + const pane = slot orelse continue; + const state = pane.terminal orelse continue; + const mine = (rp.core().panes[pid] orelse { + totals.unequal += 1; + continue; + }).terminal orelse continue; + if (screenHash(&state.vt) != screenHash(&mine.vt)) { + totals.unequal += 1; + std.debug.print("seed {d} rtt {d} ms: replica {d}'s terminal {d} at quiescence is not the authority's\n", .{ r.seed, r.rtt_ms, id, pid }); + } + } + } +} + +/// A unit of the authority's to every replica, down its link. +fn sendUnit(q: *std.PriorityQueue(Msg, void, Msg.order), n: *u64, unit: []const u8, now: u64, down: []Link, random: std.Random, one_way: u64, jitter: u64, totals: *Totals, flooding: bool) !void { + totals.unit_bytes += unit.len * down.len; + if (flooding) totals.flood_unit_bytes += unit.len * down.len; + for (down, 1..) |*l, who| { + try q.push(gpa, .{ .at = l.arrive(random, now, one_way, jitter), .n = n.*, .kind = .to_replica, .who = @intCast(who), .bytes = try gpa.dupe(u8, unit) }); + n.* += 1; } } @@ -283,15 +475,19 @@ pub fn main(init: std.process.Init) !void { var clients: u16 = 3; var rtts: []const u64 = &.{ 30, 80, 207 }; var shared = false; + var flood_mb: u64 = 8; var rtt_buf: [8]u64 = undefined; var i: usize = 1; - while (i < args.len and std.mem.eql(u8, args[i], "--shared")) : (i += 1) shared = true; - while (i + 1 < args.len) : (i += 2) { - while (i < args.len and std.mem.eql(u8, args[i], "--shared")) : (i += 1) shared = true; - if (i + 1 >= args.len) break; + while (i < args.len) : (i += 1) { const name = args[i]; - const value = args[i + 1]; - if (std.mem.eql(u8, name, "--seeds")) seeds = try std.fmt.parseInt(u64, value, 10) else if (std.mem.eql(u8, name, "--first")) first = try std.fmt.parseInt(u64, value, 10) else if (std.mem.eql(u8, name, "--steps")) steps = try std.fmt.parseInt(usize, value, 10) else if (std.mem.eql(u8, name, "--clients")) clients = @min(8, try std.fmt.parseInt(u16, value, 10)) else if (std.mem.eql(u8, name, "--rtt")) { + if (std.mem.eql(u8, name, "--shared")) { + shared = true; + continue; + } + if (i + 1 >= args.len) return error.MissingValue; + i += 1; + const value = args[i]; + if (std.mem.eql(u8, name, "--seeds")) seeds = try std.fmt.parseInt(u64, value, 10) else if (std.mem.eql(u8, name, "--first")) first = try std.fmt.parseInt(u64, value, 10) else if (std.mem.eql(u8, name, "--steps")) steps = try std.fmt.parseInt(usize, value, 10) else if (std.mem.eql(u8, name, "--clients")) clients = @min(8, try std.fmt.parseInt(u16, value, 10)) else if (std.mem.eql(u8, name, "--flood")) flood_mb = try std.fmt.parseInt(u64, value, 10) else if (std.mem.eql(u8, name, "--rtt")) { var it = std.mem.tokenizeScalar(u8, value, ','); var k: usize = 0; while (it.next()) |v| : (k += 1) rtt_buf[k] = try std.fmt.parseInt(u64, v, 10); @@ -306,11 +502,13 @@ pub fn main(init: std.process.Init) !void { try std.Io.Dir.createDirAbsolute(init.io, dir, .default_dir); defer std.Io.Dir.cwd().deleteTree(init.io, dir) catch {}; var failed = false; - std.debug.print("{d} seeds x {d} inputs from each of {d} clients, {s}; costs in us (frame {d})\n", .{ seeds, steps, clients, if (shared) "sharing one view" else "a view each", frame_us }); + std.debug.print("{d} seeds x {d} inputs from each of {d} clients, {s}, a TUI then {d} MB of yes in the shell; costs in us (frame {d})\n", .{ seeds, steps, clients, if (shared) "sharing one view" else "a view each", flood_mb, frame_us }); std.debug.print(" rtt inputs at-once stopped view units fast rollback(mis+foreign) rate rebase spec p50/p99 copy p50/p99 (KB) rollback p50/p99 diverged unequal\n", .{}); + var term_lines: std.ArrayList(u8) = .empty; + defer term_lines.deinit(gpa); for (rtts) |rtt| { var t: Totals = .{}; - for (first..first + seeds) |seed| try run(.{ .seed = seed, .rtt_ms = rtt, .steps = steps, .clients = clients, .shared = shared }, dir, &t); + for (first..first + seeds) |seed| try run(.{ .seed = seed, .rtt_ms = rtt, .steps = steps, .clients = clients, .shared = shared, .flood_mb = flood_mb }, dir, &t); const rollbacks = t.mispredicted + t.foreign; std.debug.print("{d:>4} {d:>6} {d:>7} {d:>7} {d:>4} {d:>6} {d:>6} {d:>6} ({d}+{d}) {d:>5.1}% {d:>6} {d:>5}/{d:<6} {d:>5}/{d:<5} ({d}) {d:>7}/{d:<7} {d:>8} {d:>7}\n", .{ rtt, t.inputs, t.speculated, @@ -321,7 +519,25 @@ pub fn main(init: std.process.Init) !void { pct(t.copy_ns.items, 99) / 1000, t.copy_bytes / @max(t.copies, 1) / 1024, pct(t.rollback_ns.items, 50) / 1000, pct(t.rollback_ns.items, 99) / 1000, t.divergences, t.unequal, }); - if (t.divergences > 0 or t.unequal > 0) failed = true; + const flood_s = @as(f64, @floatFromInt(@max(1, t.flood_us))) / 1e6; + + try term_lines.print(gpa, "{d:>4} {d:>7.1} {d:>9.0} {d:>9.0} {d:>5}/{d:<5} {d:>5}/{d:<5} {d:>7} {d:>6} {d:>6}/{d:<6}\n", .{ + rtt, + @as(f64, @floatFromInt(t.flood_deltas)) / flood_s / @as(f64, @floatFromInt(clients)), + @as(f64, @floatFromInt(t.flood_delta_bytes)) / flood_s / @as(f64, @floatFromInt(clients)), + @as(f64, @floatFromInt(t.flood_unit_bytes)) / flood_s / @as(f64, @floatFromInt(clients)), + pct(t.frame_ns.items, 50) / 1000, + pct(t.frame_ns.items, 99) / 1000, + pct(t.apply_ns.items, 50) / 1000, + pct(t.apply_ns.items, 99) / 1000, + t.fetched, + t.resets, + t.screens_checked, + t.screens_wrong, + }); + if (t.divergences > 0 or t.unequal > 0 or t.screens_wrong > 0) failed = true; } + std.debug.print("terminals: deltas a replica applied a second under the flood; a peer's bytes a second under the flood (deltas, units); encode a frame for every peer, apply a delta (us); rows fetched; resets; deltas checked/wrong\n", .{}); + std.debug.print(" rtt deltas/s delta B/s unit B/s encode p50/p99 apply p50/p99 fetched resets checked/wrong\n{s}", .{term_lines.items}); if (failed) std.process.exit(1); } -- cgit v1.3 From 473bbaa838f290e932992ef625be2fdc05c952dc Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 21:45:19 -0300 Subject: screen-state replicas: what a terminal's ingest tells the positions pardes keeps over it taped, the history a step reads sent before its unit, rows sent a blob once a message, and the rows cache out of the rollback copy terminal.zig, tape.zig: ingestWithPositions tracks its rows only where an ingest happens; what the ingest told them (each tracked row's row after it and whether it was erased, a clear, whether the pages lived, a pin not made) is a taped value (Site.positions), which a screen-state replica, ingesting nothing, takes; the logic after it runs alike on both. On the stack, so the local path allocates nothing new. buildRows marks a terminal read (State.read) outside the render (terminal.drawing). screen_state.zig, replica.zig: the authority keeps each peer's holes as its replica does (Peer.holes, from the deltas it sent), and Screens.fill sends a peer the rows it lacks of every terminal a step read, after the step and before its unit, so a replay reads what the step read (a text-mode cursor moved into history the flood scrolled past diverged). encodeRows sends each distinct blob once a message and the rows as addresses (a fill after yes is a third). The rows cache is dropped when a copy is taken and emptied when it is put back, not saved. test/replicas.zig: Ctrl-b in the mix (a shell out of tty mode: text, cursors and selections over it), and a shell's session (prompts, titles, directories, clears) between the TUI and the flood. 4 seeds x 120 inputs x 3 clients at 30, 80, 207 ms: no divergence, every replica and terminal the authority's, 4,653 deltas checked. Co-Authored-By: Claude Opus 5.5 --- src/draw.zig | 2 + src/replica.zig | 59 +++++++++++++++++++++--- src/rollback.zig | 13 +++--- src/screen_state.zig | 98 +++++++++++++++++++++++++++++++++------- src/tape.zig | 2 + src/terminal.zig | 125 ++++++++++++++++++++++++++++++++++++--------------- test/replicas.zig | 58 +++++++++++++++++------- 7 files changed, 278 insertions(+), 79 deletions(-) (limited to 'src/rollback.zig') diff --git a/src/draw.zig b/src/draw.zig index 0cc5c80d..03bc0cf9 100644 --- a/src/draw.zig +++ b/src/draw.zig @@ -521,6 +521,8 @@ fn resolved(color: Color, default: ?[3]u8, palette: ?[16][3]u8) ?[3]u8 { pub fn render(p: *Pardes, arena: std.mem.Allocator) !*Surface { const taped = pardes.tape.enter(p, .render); defer pardes.tape.leave(p, taped); + panes.terminal.drawing = true; + defer panes.terminal.drawing = false; panes.File.refreshHighlights(p); const s = &p.surface; const ncells = @as(usize, p.screen_w) * p.screen_h; diff --git a/src/replica.zig b/src/replica.zig index 92ee7b37..cac8ed59 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -272,7 +272,10 @@ pub const Screens = struct { /// `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| tr.track.deinit(s.gpa, if (s.live(core, id, tr)) &tr.state.vt else null); - for (s.peers.items) |p| s.gpa.destroy(p); + for (s.peers.items) |p| { + for (p) |*pp| pp.peer.deinit(s.gpa); + s.gpa.destroy(p); + } s.peers.deinit(s.gpa); s.out.deinit(s.gpa); s.enc.deinit(); @@ -302,7 +305,10 @@ pub const Screens = struct { 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.* = .{ .serial = pane.serial }; + if (pp.serial != pane.serial) { + pp.peer.reset(); + pp.serial = pane.serial; + } var marks: [terminal.marks_len]u8 = undefined; s.out.clearRetainingCapacity(); try writeScreenHead(&s.out, s.gpa, @intCast(id), pane.serial, kind_delta); @@ -313,6 +319,45 @@ pub const Screens = struct { } } + /// 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 (!state.read) 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 screen_state.encodeRows(&s.enc, &tr.track, &state.vt, 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| { + state.read = false; + }; + } + fn track(s: *Screens, core: *Pardes, id: usize) !*Tracked { const pane = core.panes[id].?; if (s.tracks[id]) |*tr| { @@ -571,9 +616,11 @@ pub const Replica = struct { if (r.copy.held) { r.copy.restore(); rollback.Epoch.select(null); - // The copy's terminal rows are of a grid the deltas since moved - // on from: a rollback never brings a terminal's past back. - r.core().shell_rows.stale = 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 = .{}; } r.applied = 0; for (r.since.items) |u| if (tape.replayUnit(&r.t, u)) |why| return r.fail(why); @@ -614,6 +661,8 @@ pub const Replica = struct { 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; rollback.Epoch.select(&r.epoch); r.stats.copy_ns.append(r.gpa, nowNs() - t0) catch {}; diff --git a/src/rollback.zig b/src/rollback.zig index b5f4e966..d87671e6 100644 --- a/src/rollback.zig +++ b/src/rollback.zig @@ -350,7 +350,7 @@ pub const Copy = struct { // the speculation freed would come back as that. Not the // queues a step drains (undefined between steps), nor the // authority's. - if (attr.@"comptime" or comptime (annotated(T, "authority", name) or (annotated(T, "local", name) and unsaved(name)))) continue; + if (attr.@"comptime" or comptime (annotated(T, "authority", name) or ((annotated(T, "local", name) or annotated(T, "derived", name)) and unsaved(name)))) continue; if (comptime opaqueTo(F)) continue; c.where = @typeName(T) ++ "." ++ name; c.pinnable = comptime pinned(T, name); @@ -390,7 +390,7 @@ pub const Copy = struct { const s = @typeInfo(T).@"struct"; inline for (s.field_names, s.field_types, s.field_attrs) |name, F, attr| { if (attr.@"comptime") continue; - if (comptime annotated(T, "local", name) and unsaved(name)) continue; + if (comptime (annotated(T, "local", name) or annotated(T, "derived", name)) and unsaved(name)) continue; const at = @intFromPtr(&@field(v, name)); if (comptime walk.lengthOf(T, name) != null and @typeInfo(F) == .array) { try c.save(at, walk.lengthIn(v, comptime walk.lengthOf(T, name).?) * @sizeOf(@typeInfo(F).array.child)); @@ -429,10 +429,13 @@ fn annotatedStruct(comptime T: type) bool { } /// The queues every step drains (empty between steps, asserted when a copy -/// is taken: what is in their slots is nothing), and the frame statistics -/// (numbers, and a static name). +/// is taken: what is in their slots is nothing), the frame statistics +/// (numbers, and a static name), and the rows a terminal was last read as +/// (a grid's worth of text, emptied when a copy is taken and when it is +/// put back: src/replica.zig). fn unsaved(comptime name: []const u8) bool { - return std.mem.eql(u8, name, "effects") or std.mem.eql(u8, name, "in_q") or std.mem.eql(u8, name, "frames"); + return std.mem.eql(u8, name, "effects") or std.mem.eql(u8, name, "in_q") or std.mem.eql(u8, name, "frames") or + std.mem.eql(u8, name, "shell_rows"); } fn annotated(comptime T: type, comptime kind: []const u8, comptime name: []const u8) bool { diff --git a/src/screen_state.zig b/src/screen_state.zig index e91f1e14..27e20c0f 100644 --- a/src/screen_state.zig +++ b/src/screen_state.zig @@ -191,8 +191,10 @@ const Known = struct { pub const Track = struct { rows: Rows = .{}, version: u32 = 0, - /// The emulator's change count the newest version was made at. + /// The emulator's change count the newest version was made at, and its + /// history's rows. changes: u32 = 0, + history: u64 = 0, /// The newest version: its active rows (pins, good while the emulator /// is unchanged) and their addresses, and its meta. pins: std.ArrayList(Pin) = .empty, @@ -236,6 +238,8 @@ pub const Track = struct { tr.version +%= 1; if (tr.version == 0) tr.version = 1; tr.changes = changes; + const primary = &t.screens.get(.primary).?.pages; + tr.history = primary.total_rows - primary.rows; tr.meta.clearRetainingCapacity(); { var rw: snapshot.record.Writer = .init(gpa, &tr.meta.writer); @@ -271,6 +275,15 @@ pub const Peer = struct { /// The versions in flight, oldest first. flight: [bound]u32 = undefined, n: u8 = 0, + /// The history rows its replica holds as holes, absolute, as the + /// replica keeps them (Replica.apply's rule, from what it was sent): + /// what `fill` sends before a step reads them. + top: u64 = 0, + holes: std.ArrayListUnmanaged([2]u64) = .empty, + + pub fn deinit(peer: *Peer, gpa: std.mem.Allocator) void { + peer.holes.deinit(gpa); + } /// Every version up to `seq` has landed. pub fn ack(peer: *Peer, seq: u32) void { @@ -282,7 +295,36 @@ pub const Peer = struct { /// Start the peer afresh: it lost its state, or never had one. pub fn reset(peer: *Peer) void { - peer.* = .{}; + var holes = peer.holes; + holes.clearRetainingCapacity(); + peer.* = .{ .holes = holes }; + } + + /// Its replica's holes once it applies a delta to `tr`'s newest version + /// against `base`, as Replica.apply makes them. + fn noteSent(peer: *Peer, gpa: std.mem.Allocator, tr: *const Track, base: ?*const Known, rows: u16) !void { + const top = tr.rows.top; + const history = tr.history; + if (base) |b| { + if (top > peer.top) { + const tail = @min(top -| b.top, rows, history); + if (top - peer.top >= @as(u64, rows) + history) { + peer.holes.clearRetainingCapacity(); + if (history > tail) try peer.holes.append(gpa, .{ top - history, top - tail }); + } else if (top - tail > peer.top) try peer.holes.append(gpa, .{ peer.top, top - tail }); + } + } else { + peer.holes.clearRetainingCapacity(); + if (history > 0) try peer.holes.append(gpa, .{ top - history, top }); + } + peer.top = top; + // Those older than its history's oldest row are gone with it. + var i: usize = 0; + while (i < peer.holes.items.len) { + const hole = &peer.holes.items[i]; + hole[0] = @max(hole[0], top -| history); + if (hole[0] >= hole[1]) _ = peer.holes.orderedRemove(i) else i += 1; + } } }; @@ -317,6 +359,7 @@ pub fn encode(e: *Encoder, tr: *Track, peer: *Peer, t: *Terminal, changes: u32, tr.cache_base = base_version; } try out.writeAll(tr.cache.written()); + try peer.noteSent(e.gpa, tr, base, t.rows); peer.sent = tr.version; peer.flight[peer.n] = tr.version; peer.n += 1; @@ -389,30 +432,41 @@ fn needBlob(e: *Encoder, base: ?*const Known, p: Pin, h: Hash) !void { try e.needed.append(e.gpa, .{ .pin = p, .hash = h }); } -/// The primary screen's rows `[lo, hi)` the authority still has, as u32 -/// epoch, u32 count, then each as u64 row, u32 length, blob: a replica's -/// fetch of history it holds as holes. -// ponytail: every row its blob, none by address: phase 6's content cache. +/// The primary screen's rows `[lo, hi)` the authority still has, for a +/// replica that holds them as holes: u32 epoch, u32 blobs, each u64 hash, +/// u32 length, bytes, then u32 rows, each u64 row and u64 hash. A blob goes +/// once a message (a flood's rows are one). +// ponytail: blobs once a message, not once a peer: phase 6's content cache. pub fn encodeRows(e: *Encoder, tr: *Track, t: *Terminal, lo: u64, hi: u64, out: *std.Io.Writer) !void { try tr.rows.update(t); var blobs: std.Io.Writer.Allocating = .init(e.gpa); defer blobs.deinit(); - var count: u32 = 0; + var rows: std.Io.Writer.Allocating = .init(e.gpa); + defer rows.deinit(); + e.listed.clearRetainingCapacity(); + var nblobs: u32 = 0; + var nrows: u32 = 0; var abs = lo; // The active area is the deltas'. while (abs < @min(hi, tr.rows.top)) : (abs += 1) { const p = tr.rows.pinAt(t, abs) orelse continue; - const start = blobs.written().len; - try blobs.writer.writeInt(u64, abs, .little); - try blobs.writer.writeInt(u32, 0, .little); - try e.rowBlob(p.node.page(), p.rowAndCell().row, t.cols, &blobs.writer); - const len: u32 = @intCast(blobs.written().len - start - 12); - std.mem.writeInt(u32, blobs.written()[start + 8 ..][0..4], len, .little); - count += 1; + const h = rowHash(p.node.page(), p.rowAndCell().row, t.cols); + try rows.writer.writeInt(u64, abs, .little); + try rows.writer.writeInt(u64, h, .little); + nrows += 1; + if ((try e.listed.getOrPut(e.gpa, h)).found_existing) continue; + e.blob.clearRetainingCapacity(); + try e.rowBlob(p.node.page(), p.rowAndCell().row, t.cols, &e.blob.writer); + try blobs.writer.writeInt(u64, h, .little); + try blobs.writer.writeInt(u32, @intCast(e.blob.written().len), .little); + try blobs.writer.writeAll(e.blob.written()); + nblobs += 1; } try out.writeInt(u32, tr.rows.epoch, .little); - try out.writeInt(u32, count, .little); + try out.writeInt(u32, nblobs, .little); try out.writeAll(blobs.written()); + try out.writeInt(u32, nrows, .little); + try out.writeAll(rows.written()); } /// A replica's terminal: what it was sent, written into a ghostty Terminal. @@ -631,9 +685,15 @@ pub const Replica = struct { var in: std.Io.Reader = .fixed(bytes); if (try in.takeInt(u32, .little) != r.epoch) return; // numbered before a resize const primary = t.screens.get(.primary).?; + var blobs: std.AutoHashMapUnmanaged(Hash, []const u8) = .empty; + defer blobs.deinit(gpa); + for (0..try in.takeInt(u32, .little)) |_| { + const h = try in.takeInt(u64, .little); + try blobs.put(gpa, h, try in.take(try in.takeInt(u32, .little))); + } for (0..try in.takeInt(u32, .little)) |_| { const abs = try in.takeInt(u64, .little); - const blob = try in.take(try in.takeInt(u32, .little)); + const blob = blobs.get(try in.takeInt(u64, .little)) orelse return error.MissingBlob; // Only a hole is written: a row the replica holds is the // authority's already, and newer than what this fetch saw. if (abs >= r.top or !r.inHole(abs)) continue; @@ -852,6 +912,7 @@ pub const Soak = struct { s.b.deinit(gpa); gpa.destroy(s.b); s.enc.deinit(); + s.peer.deinit(gpa); s.rep.deinit(gpa); for (s.flight.items) |f| { gpa.free(f.delta); @@ -1088,6 +1149,7 @@ fn bench(gpa: std.mem.Allocator, io: std.Io, w: *std.Io.Writer) !void { var tr: Track = .init(gpa); defer tr.deinit(gpa, t); var peer: Peer = .{}; + defer peer.deinit(gpa); out.clearRetainingCapacity(); var t0 = std.Io.Clock.awake.now(io).nanoseconds; _ = try encode(&enc, &tr, &peer, t, changes, "", &out.writer); @@ -1135,9 +1197,10 @@ fn floods(gpa: std.mem.Allocator, ca: std.mem.Allocator, io: std.Io, w: *std.Io. var reps: [3]Replica = @splat(.{}); var bs: [3]Terminal = undefined; for (bs[0..npeers]) |*b| b.* = try Terminal.init(io, ca, .{ .cols = 2, .rows = 2, .max_scrollback_bytes = 32 << 20 }); - defer for (bs[0..npeers], reps[0..npeers]) |*b, *r| { + defer for (bs[0..npeers], reps[0..npeers], peers[0..npeers]) |*b, *r, *peer| { b.deinit(ca); r.deinit(ca); + peer.deinit(ca); }; var applies: std.ArrayList(u64) = .empty; defer applies.deinit(gpa); @@ -1348,6 +1411,7 @@ test "a replica's terminal pane draws as its authority's does" { var enc: Encoder = .init(gpa); defer enc.deinit(); var peer: Peer = .{}; + defer peer.deinit(gpa); var tr: Track = .init(gpa); defer tr.deinit(gpa, ta); var rep: Replica = .{}; diff --git a/src/tape.zig b/src/tape.zig index 0609ce0b..b1080149 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -88,6 +88,8 @@ pub const Site = enum(u16) { mtime, cwd, pointer, + /// What a terminal's ingest told the positions pardes keeps over it. + positions, }; /// Fields a host writes straight into the core between its calls. diff --git a/src/terminal.zig b/src/terminal.zig index 6c3eeda7..fcb284a0 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -161,6 +161,10 @@ pub const State = struct { /// Counts the emulator's writes (output, a resize, its view scrolled): /// a new count is a new state for its replicas (screen_state.Track). changes: u32 = 0, + /// A step read its rows (its history too): a replica replaying that + /// step must hold them, so its authority sends the ones it does not + /// before the step's unit (replica.Screens.fill). + read: bool = false, /// The emulator as its program has made it, for the replay digest /// (src/walk.zig): the active screen's text, how much history is above @@ -855,6 +859,7 @@ pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { state.stream = .init(.{ .handler = .{ .inner = state.vt.vtHandler() }, .allocator = gpa }); state.reply_len = 0; state.changes = 0; + state.read = false; // Every field its default (create leaves them what the heap had, which // a digest then reads: two replicas must agree, src/replica.zig). state.reply_dropped = 0; @@ -1072,11 +1077,15 @@ pub fn feedOutput(p: *Pardes, pane: *Pane, bytes: []const u8) void { if (comptime !enabled) return; if (pane.terminal == null) return; if (authorityOnly()) return; - // A replica's emulator is what the authority's deltas make it. - if (pardes.tape.screenReplica()) return; const has_positions = pane.ovl != null or pane.body.cur_pinned or pane.body.vsel.active or pane.body.msel.active or pane.body.nsel > 0 or pane.body.last_edit != null or pane.look_at != null or pane.body.ed_undo_len > 0 or pane.body.ed_redo_len > 0 or pointerRowCount(pane) > 0; + // A replica's emulator is what the authority's deltas make it; what + // pardes keeps over it moves as the authority's ingest told it. + if (pardes.tape.screenReplica()) { + if (has_positions) ingestWithPositions(pane, bytes); + return; + } // Sticky bottom, as every other terminal does it: output drags the // viewport down only when the viewport was already on the last row, so // a reader scrolled back into the scrollback stays where they scrolled. @@ -1095,7 +1104,9 @@ const PositionPin = struct { edit_row: ?i32 = null, }; -fn trackRow(pages: *ghostty_vt.PageList, row: i32, failed: *bool) RowPin { +fn trackRow(maybe_pages: ?*ghostty_vt.PageList, row: i32, failed: *bool) RowPin { + // A screen-state replica's: told by the tape, not its emulator. + const pages = maybe_pages orelse return .{ .offset = row }; if (row < 0) return .{ .offset = row }; const bounded: usize = @min(@as(usize, @intCast(row)), pages.total_rows - 1); var left = pages.total_rows - 1 - bounded; @@ -1125,7 +1136,7 @@ fn pinnedRow(pages: ?*ghostty_vt.PageList, row: RowPin) i32 { return 0; } -fn trackPosition(pages: *ghostty_vt.PageList, target: *i32, overlay: ?*EditBuffer, failed: *bool) PositionPin { +fn trackPosition(pages: ?*ghostty_vt.PageList, target: *i32, overlay: ?*EditBuffer, failed: *bool) PositionPin { var grid = target.*; if (overlay) |edit| { const lines: i32 = @intCast(modal.lineCount(edit.text)); @@ -1144,11 +1155,24 @@ fn pointerRowCount(pane: *const Pane) usize { return count; } +/// What an ingest told the positions over it (`ingestWithPositions`): each +/// tracked row where it is after it (the spans' starts and ends, the +/// positions, the pointer rows, in that order) and whether the program +/// erased it, whether the screen was cleared, whether its pages lived and +/// whether a pin could not be made. Taped: a screen-state replica, which +/// ingests nothing, is told the authority's. +const Told = struct { rows: []const i32, erased: []const bool, cleared: bool, alive: bool, failed: bool }; + +fn told(t: Told) Told { + return t; +} + fn ingestWithPositions(pane: *Pane, bytes: []const u8) void { const screens = &pane.terminal.?.vt.screens; const key = screens.active_key; const generation = screens.generation(key); - const pages = &screens.active.pages; + const replica = pardes.tape.screenReplica(); + const pages: ?*ghostty_vt.PageList = if (replica) null else &screens.active.pages; const Group = struct { overlay: ?*EditBuffer, cursor: *i32, selection: *panes.Text.CharSel }; var groups: [history_max * 2 + 1]Group = undefined; groups[0] = .{ @@ -1230,31 +1254,61 @@ fn ingestWithPositions(pane: *Pane, bytes: []const u8) void { } }; } else failed = true; - // Whether the program cleared the screen (ED 2, a switch of screens); - // pardes reads the flag nowhere else. - pane.terminal.?.vt.flags.dirty.clear = false; - ingest(pane, bytes); - const cleared = pane.terminal.?.vt.flags.dirty.clear; - // RIS can destroy the alternate screen and every pin it owned. - const alive = if (screens.generation(key) == generation) pages else null; - var moved = false; - var pointer_rows_lost = alive == null or pointer_positions == null; - if (pointer_positions) |pins| for (pins) |position| { - if (alive != null) if (position.row.pin) |pin| { - if (pin.garbage) pointer_rows_lost = true; + var cleared = false; + var alive: ?*ghostty_vt.PageList = null; + if (!replica) { + // Whether the program cleared the screen (ED 2, a switch of + // screens); pardes reads the flag nowhere else. + pane.terminal.?.vt.flags.dirty.clear = false; + ingest(pane, bytes); + cleared = pane.terminal.?.vt.flags.dirty.clear; + // RIS can destroy the alternate screen and every pin it owned. + alive = if (screens.generation(key) == generation) pages else null; + } + const pointers: []PositionPin = pointer_positions orelse &.{}; + const n = 2 * nspans + npositions + pointers.len; + // On the stack, as the local path never allocated here but for the + // pointer's rows. + var rows_buf: [512]i32 = undefined; + var erased_buf: [512]bool = undefined; + const rows_after = if (n <= rows_buf.len) rows_buf[0..n] else pane.gpa.alloc(i32, n) catch return; + defer if (n > rows_buf.len) pane.gpa.free(rows_after); + const erased_after = if (n <= erased_buf.len) erased_buf[0..n] else pane.gpa.alloc(bool, n) catch return; + defer if (n > erased_buf.len) pane.gpa.free(erased_after); + @memset(rows_after, 0); + @memset(erased_after, false); + if (!replica) { + var k: usize = 0; + for (spans[0..nspans]) |span| for ([_]RowPin{ span.start, span.end }) |rp| { + rows_after[k] = pinnedRow(alive, rp); + erased_after[k] = alive != null and rp.pin != null and rp.pin.?.garbage; + k += 1; }; + for ([_][]PositionPin{ positions[0..npositions], pointers }) |batch| for (batch) |position| { + rows_after[k] = pinnedRow(alive, position.row); + erased_after[k] = alive != null and position.row.pin != null and position.row.pin.?.garbage; + k += 1; + }; + } + const after = pardes.tape.value(.positions, told, .{Told{ .rows = rows_after, .erased = erased_after, .cleared = cleared, .alive = alive != null, .failed = failed }}); + if (after.rows.len != n or after.erased.len != n) return; // a replica its authority's states apart (diverged) + failed = after.failed; + const at_position = 2 * nspans; + const at_pointer = at_position + npositions; + var moved = false; + var pointer_rows_lost = !after.alive or pointer_positions == null; + for (0..pointers.len) |i| if (after.erased[at_pointer + i]) { + pointer_rows_lost = true; }; // The program cleared the screen, or the rows the live copy stood on are // gone (pruned, a reset): the copy goes with them, as what the program // drew next. Kept, ghostty would move its rows to the top of what is // left, over the program's new text. - var copy_lost = cleared or alive == null; - for (spans[0..nspans]) |span| { - if (span.overlay == overlay) if (span.start.pin) |pin| if (alive != null and pin.garbage) { - copy_lost = true; - }; - const start = pinnedRow(alive, span.start); - const rows = @max(1, pinnedRow(alive, span.end) -| start +| 1); + var copy_lost = after.cleared or !after.alive; + for (spans[0..nspans], 0..) |span, i| { + if (span.overlay == overlay and after.erased[2 * i]) copy_lost = true; + const start = after.rows[2 * i]; + const rows = @max(1, after.rows[2 * i + 1] -| start +| 1); moved = moved or start != span.overlay.row or rows != span.overlay.rows; span.overlay.row = start; span.overlay.rows = rows; @@ -1266,23 +1320,16 @@ fn ingestWithPositions(pane: *Pane, bytes: []const u8) void { // The rule above for the copy holds for the selections: one whose row // is gone (ED 3 erased its history row, which ghostty flags `garbage`) // goes too, rather than being clamped to the top. - const erased = struct { - fn at(owner: ?*ghostty_vt.PageList, all: []const PositionPin, index: usize) bool { - if (owner == null) return false; - const pin = all[index].row.pin orelse return false; - return pin.garbage; - } - }.at; + const erased = after.erased[at_position..]; const vsel_lost = if (vsel_at) |i| - erased(alive, positions[0..npositions], i) or (copy_lost and positions[i].edit_row != null) + erased[i] or (copy_lost and positions[i].edit_row != null) else false; - const msel_lost = if (msel_at) |i| erased(alive, positions[0..npositions], i) or erased(alive, positions[0..npositions], i + 1) else false; + const msel_lost = if (msel_at) |i| erased[i] or erased[i + 1] else false; var sels_keep: [panes.Text.max_selections]bool = undefined; - for (0..pane.body.nsel) |i| sels_keep[i] = !(erased(alive, positions[0..npositions], sels_at + 2 * i) or - erased(alive, positions[0..npositions], sels_at + 2 * i + 1)); - for ([_][]PositionPin{ positions[0..npositions], pointer_positions orelse &.{} }) |batch| for (batch) |position| { - var row = pinnedRow(alive, position.row); + for (0..pane.body.nsel) |i| sels_keep[i] = !(erased[sels_at + 2 * i] or erased[sels_at + 2 * i + 1]); + for ([_][]PositionPin{ positions[0..npositions], pointers }, [_]usize{ at_position, at_pointer }) |batch, first| for (batch, first..) |position, k| { + var row = after.rows[k]; if (position.overlay) |edit| { if (position.edit_row) |relative| { row = edit.row +| relative; @@ -1887,7 +1934,11 @@ pub fn setEditText(p: *Pardes, pane: *Pane, new: []u8) void { } +/// Set while a frame is drawn: what it reads is no step's. +pub var drawing = false; + fn buildRows(alloc: std.mem.Allocator, p: *Pardes, pane: *Pane) !Rows { + if (!drawing) pane.terminal.?.read = true; const full = try pane.terminal.?.vt.screens.active.dumpStringAlloc(p.scratch.allocator(), .{ .screen = .{} }); var pit = pane.terminal.?.vt.screens.active.pages.rowIterator(.right_down, .{ .screen = .{} }, null); // split yields one more item than delimiters; the extra final slot is the diff --git a/test/replicas.zig b/test/replicas.zig index 1acd1c11..9b5c3c3b 100644 --- a/test/replicas.zig +++ b/test/replicas.zig @@ -113,6 +113,8 @@ const Totals = struct { flood_delta_bytes: u64 = 0, flood_unit_bytes: u64 = 0, fetched: u64 = 0, + /// Bytes of rows sent before a step's unit, the step having read them. + filled_bytes: u64 = 0, resets: u64 = 0, screens_wrong: u64 = 0, screens_checked: u64 = 0, @@ -141,7 +143,7 @@ const Totals = struct { fn nextEvent(random: std.Random, buf: *[64]u8, wheel_at: ?[2]u16) pardes.Event { const typed = "abcdefghijklmnopqrstuvwxyz0123456789 -_=+,:;()"; const normal = "hjklwbe0$xpuyoOiaAIdcv.nN%sC"; - switch (random.uintLessThan(u8, 42)) { + switch (random.uintLessThan(u8, 43)) { 0...15 => { buf[0] = typed[random.uintLessThan(usize, typed.len)]; return .{ .key = .{ .cp = buf[0], .text = buf[0..1] } }; @@ -173,6 +175,9 @@ fn nextEvent(random: std.Random, buf: *[64]u8, wheel_at: ?[2]u16) pardes.Event { 37 => return .{ .resize = .{ .cols = @intCast(random.intRangeAtMost(u16, 60, 120)), .rows = @intCast(random.intRangeAtMost(u16, 16, 40)) } }, 38 => return .{ .mouse = .{ .button = .left, .kind = .press, .col = random.uintLessThan(u16, 80), .row = random.uintLessThan(u16, 24) } }, 39 => return .{ .mouse = .{ .button = .left, .kind = .release, .col = random.uintLessThan(u16, 80), .row = random.uintLessThan(u16, 24) } }, + // Out of a shell's tty mode and back: its text, a cursor and + // selections over it. + 40 => return .{ .key = .{ .cp = 'b', .ctrl = true } }, else => { const at = wheel_at orelse return .{ .key = .{ .cp = Key.escape } }; return .{ .mouse = .{ .button = if (random.uintLessThan(u8, 3) == 0) .wheel_down else .wheel_up, .kind = .press, .col = at[0], .row = at[1] } }; @@ -252,14 +257,18 @@ fn run(r: Run, dir: []const u8, totals: *Totals) !void { const sr = a.core.rects[shell]; const wheel_at: ?[2]u16 = if (sr.h > 2) .{ sr.x + sr.w / 2, sr.y + sr.h / 2 } else null; - // What the shell's program prints: a full-screen program, then a flood - // of `yes`, read 4 KB at a time as a pty gives it. + // What the shell's program prints: a full-screen program, a shell's + // session (prompts, titles, directories, a clear now and then), then a + // flood of `yes`, read 4 KB at a time as a pty gives it. const tui = try screen_state.tuiStream(gpa); defer gpa.free(tui); + const session = try screen_state.shellStream(gpa); + defer gpa.free(session); const yes = try screen_state.yesStream(gpa, @intCast(r.flood_mb * (1 << 20) / 3)); defer gpa.free(yes); const tui_at: u64 = 400_000; - const flood_at: u64 = 1_500_000; + const session_at: u64 = 1_000_000; + const flood_at: u64 = 2_500_000; var flood_end: u64 = 0; var program: struct { bytes: []const u8 = &.{}, at: usize = 0 } = .{}; // The authority's version of each terminal, as its screen showed then. @@ -290,6 +299,7 @@ fn run(r: Run, dir: []const u8, totals: *Totals) !void { // Each client's first input, soon; the programs; the first frame. for (0..r.clients) |c| try push(&queue, &n, .{ .at = random.uintLessThan(u64, 200_000), .n = 0, .kind = .input, .who = @intCast(c + 1), .bytes = &.{} }); try push(&queue, &n, .{ .at = tui_at, .n = 0, .kind = .output, .who = 0, .bytes = &.{} }); + try push(&queue, &n, .{ .at = session_at, .n = 0, .kind = .output, .who = 2, .bytes = &.{} }); try push(&queue, &n, .{ .at = flood_at, .n = 0, .kind = .output, .who = 1, .bytes = &.{} }); try push(&queue, &n, .{ .at = frame_us, .n = 0, .kind = .frame, .who = 0, .bytes = &.{} }); var frames_left: u32 = 0; @@ -353,13 +363,17 @@ fn run(r: Run, dir: []const u8, totals: *Totals) !void { // The program's next read, 4 KB, then the next a // fraction of a millisecond on (ghostty's pace). if (program.at == program.bytes.len) { - program = .{ .bytes = if (m.who == 0) tui else yes }; + program = .{ .bytes = switch (m.who) { + 0 => tui, + 1 => yes, + else => session, + } }; } - const k = @min(program.bytes.len - program.at, if (m.who == 0) @as(usize, 700) else 4096); + const k = @min(program.bytes.len - program.at, if (m.who == 1) @as(usize, 4096) else 700); const chunk = program.bytes[program.at..][0..k]; program.at += k; if (program.at < program.bytes.len) - try push(&queue, &n, .{ .at = m.at + if (m.who == 0) @as(u64, 3_000) else 450, .n = 0, .kind = .output, .who = m.who, .bytes = &.{} }) + try push(&queue, &n, .{ .at = m.at + if (m.who == 1) @as(u64, 450) else 3_000, .n = 0, .kind = .output, .who = m.who, .bytes = &.{} }) else if (m.who == 1) flood_end = m.at; if (m.who == 1) flood_units += 1; a.output(.{ .output = .{ .pane = shell, .bytes = chunk } }); @@ -369,17 +383,17 @@ fn run(r: Run, dir: []const u8, totals: *Totals) !void { // The authority's own steps since the last unit, and // the terminals as they are: what the input's step // reads, before it is stepped. - if (try a.pending()) |batch| try sendUnit(&queue, &n, batch, m.at, down[0..r.clients], random, one_way, jitter, totals, m.at >= flood_at and flood_end == 0); + if (try a.pending()) |batch| try sendUnit(&queue, &n, batch, m.at, down[0..r.clients], random, one_way, jitter, totals, m.at >= flood_at and flood_end == 0, &screens, a.core, peers[0..r.clients]); try flushAll(&screens, a.core, peers[0..r.clients], &owed, &queue, &n, m.at, &down, random, one_way, jitter, &versions, totals); break :in try a.take(m.bytes); }, }; - if (unit) |u| try sendUnit(&queue, &n, u, m.at, down[0..r.clients], random, one_way, jitter, totals, false); + if (unit) |u| try sendUnit(&queue, &n, u, m.at, down[0..r.clients], random, one_way, jitter, totals, false, &screens, a.core, peers[0..r.clients]); for (host.echo.items) |e| try push(&queue, &n, .{ .at = m.at + 2_000, .n = 0, .kind = .echo, .who = e.pane, .bytes = e.bytes }); host.echo.clearRetainingCapacity(); }, .frame => { - if (try a.pending()) |batch| try sendUnit(&queue, &n, batch, m.at, down[0..r.clients], random, one_way, jitter, totals, m.at >= flood_at and flood_end == 0); + if (try a.pending()) |batch| try sendUnit(&queue, &n, batch, m.at, down[0..r.clients], random, one_way, jitter, totals, m.at >= flood_at and flood_end == 0, &screens, a.core, peers[0..r.clients]); const t0 = screens.encode_ns; const before = totals.delta_bytes; try flushAll(&screens, a.core, peers[0..r.clients], &owed, &queue, &n, m.at, &down, random, one_way, jitter, &versions, totals); @@ -453,8 +467,21 @@ fn run(r: Run, dir: []const u8, totals: *Totals) !void { } } -/// A unit of the authority's to every replica, down its link. -fn sendUnit(q: *std.PriorityQueue(Msg, void, Msg.order), n: *u64, unit: []const u8, now: u64, down: []Link, random: std.Random, one_way: u64, jitter: u64, totals: *Totals, flooding: bool) !void { +/// A unit of the authority's to every replica, down its link: first the +/// rows of its terminals its step read that a replica holds as holes. +fn sendUnit(q: *std.PriorityQueue(Msg, void, Msg.order), n: *u64, unit: []const u8, now: u64, down: []Link, random: std.Random, one_way: u64, jitter: u64, totals: *Totals, flooding: bool, screens: *replica.Screens, core: *pardes.Pardes, peers: []const u16) !void { + var rows: std.ArrayListUnmanaged([]u8) = .empty; + defer rows.deinit(gpa); + for (down, peers, 1..) |*l, peer, who| { + rows.clearRetainingCapacity(); + try screens.fill(core, peer, &rows); + for (rows.items) |bytes| { + totals.filled_bytes += bytes.len; + try q.push(gpa, .{ .at = l.arrive(random, now, one_way, jitter), .n = n.*, .kind = .screen_down, .who = @intCast(who), .bytes = bytes }); + n.* += 1; + } + } + screens.filled(core); totals.unit_bytes += unit.len * down.len; if (flooding) totals.flood_unit_bytes += unit.len * down.len; for (down, 1..) |*l, who| { @@ -523,7 +550,7 @@ pub fn main(init: std.process.Init) !void { }); const flood_s = @as(f64, @floatFromInt(@max(1, t.flood_us))) / 1e6; - try term_lines.print(gpa, "{d:>4} {d:>7.1} {d:>9.0} {d:>9.0} {d:>5}/{d:<5} {d:>5}/{d:<5} {d:>7} {d:>6} {d:>6}/{d:<6}\n", .{ + try term_lines.print(gpa, "{d:>4} {d:>7.1} {d:>9.0} {d:>9.0} {d:>5}/{d:<5} {d:>5}/{d:<5} {d:>7} {d:>9} {d:>6} {d:>6}/{d:<6}\n", .{ rtt, @as(f64, @floatFromInt(t.flood_deltas)) / flood_s / @as(f64, @floatFromInt(clients)), @as(f64, @floatFromInt(t.flood_delta_bytes)) / flood_s / @as(f64, @floatFromInt(clients)), @@ -533,13 +560,14 @@ pub fn main(init: std.process.Init) !void { pct(t.apply_ns.items, 50) / 1000, pct(t.apply_ns.items, 99) / 1000, t.fetched, + t.filled_bytes / 1024, t.resets, t.screens_checked, t.screens_wrong, }); if (t.divergences > 0 or t.unequal > 0 or t.screens_wrong > 0) failed = true; } - std.debug.print("terminals: deltas a replica applied a second under the flood; a peer's bytes a second under the flood (deltas, units); encode a frame of the flood for every peer, apply a delta (us); rows fetched; resets; deltas checked/wrong\n", .{}); - std.debug.print(" rtt deltas/s delta B/s unit B/s encode p50/p99 apply p50/p99 fetched resets checked/wrong\n{s}", .{term_lines.items}); + std.debug.print("terminals: deltas a replica applied a second under the flood; a peer's bytes a second under the flood (deltas, units); encode a frame of the flood for every peer, apply a delta (us); rows fetched, KB of rows sent before the step that read them; resets; deltas checked/wrong\n", .{}); + std.debug.print(" rtt deltas/s delta B/s unit B/s encode p50/p99 apply p50/p99 fetched filled KB resets checked/wrong\n{s}", .{term_lines.items}); if (failed) std.process.exit(1); } -- cgit v1.3 From 77c4a6e346e08538abc410f85a86bec7e8dbedb3 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 22:01:29 -0300 Subject: screen-state replicas: a replica's emulators on the C heap, outside its rollback epoch, so a delta written under a held copy is no copy's to keep or put back (a ReleaseFast run found the heap corrupted) terminal.zig: State.gpa, what an emulator and its State are allocated with: the core's, but on a screen-state replica the C heap (emulator_gpa). replica.zig applies deltas and rows with it; the epoch's pause is gone (rollback.zig back to part A's but for the rows cache). The harness in ReleaseFast corrupted the heap after 20 seeds: a safe build's check named an allocation the copy saved and a delta freed while the epoch was paused, reached as `Text.own` through a view's stale slot (views.zig keeps the live view's parked share as stale copies, and the copy walks it), the replica's old terminal's pin map at that address. With the emulator outside the epoch, nothing of it is saved, kept or put back, and every free under a held copy is put off again, as in part A. Debug, 20 seeds at 80 ms and the defaults: no divergence, every replica and terminal the authority's. Co-Authored-By: Claude Opus 5.5 --- src/replica.zig | 33 ++++++++++++++++----------------- src/rollback.zig | 12 ------------ src/terminal.zig | 28 ++++++++++++++++++++-------- 3 files changed, 36 insertions(+), 37 deletions(-) (limited to 'src/rollback.zig') diff --git a/src/replica.zig b/src/replica.zig index cac8ed59..a96a056a 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -222,14 +222,18 @@ pub const Authority = struct { // // 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 it asked for (encodeRows); +// 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. -// The authority sends a peer what it is owed (`Screens.flush`) before it -// steps any input but a pty's output, and once a frame; a replica takes -// them in that order between units (`Replica.screen`). So a step a replica -// replays reads the terminal its authority's read. +// 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; @@ -490,7 +494,7 @@ pub const Replica = struct { } pub fn deinit(r: *Replica) void { - for (&r.screens) |*slot| if (slot.*) |*sc| sc.rep.deinit(r.coreGpa()); + for (&r.screens) |*slot| if (slot.*) |*sc| sc.rep.deinit(terminal.emulator_gpa); if (r.t.core) |c| { const was = Stepping.begin(&r.t, &r.epoch); if (r.copy.held) r.copy.restore(); @@ -718,10 +722,6 @@ pub const Replica = struct { return r.pending.items.len == 0 and r.applied == 0 and !r.copy.held; } - fn coreGpa(r: *Replica) Allocator { - return r.epoch.allocator(); - } - /// 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 @@ -735,18 +735,17 @@ pub const Replica = struct { const state = pane.terminal orelse return null; const slot = &r.screens[m.pane]; if (slot.* == null or slot.*.?.serial != m.serial) { - if (slot.*) |*old| old.rep.deinit(r.coreGpa()); + if (slot.*) |*old| old.rep.deinit(terminal.emulator_gpa); slot.* = .{ .serial = m.serial }; } const sc = &slot.*.?; - // The emulator is the authority's, outside every rollback copy: - // what it allocates now must outlive one. - const was = r.epoch.pause(); - defer r.epoch.unpause(was); + // 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 => { const t0 = nowNs(); - const applied = sc.rep.apply(r.coreGpa(), terminal.terminalIo(), &state.vt, m.payload) catch |err| { + const applied = sc.rep.apply(state.gpa, terminal.terminalIo(), &state.vt, 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; @@ -763,7 +762,7 @@ pub const Replica = struct { }, kind_rows => { sc.asked = null; - sc.rep.applyRows(r.coreGpa(), &state.vt, m.payload) catch r.fail("a terminal's rows did not apply"); + sc.rep.applyRows(state.gpa, &state.vt, m.payload) catch r.fail("a terminal's rows did not apply"); c.shell_rows.markStale(pane); return null; }, diff --git a/src/rollback.zig b/src/rollback.zig index d87671e6..79b8202e 100644 --- a/src/rollback.zig +++ b/src/rollback.zig @@ -140,18 +140,6 @@ pub const Epoch = struct { _ = e.live.remove(addr); } - /// What is allocated and freed until `unpause` is no copy's: a delta - /// written into a replica's terminal while a copy is held (an emulator - /// is the authority's, outside every copy, and must outlive a rollback). - pub fn pause(e: *Epoch) bool { - defer e.on = false; - return e.on; - } - - pub fn unpause(e: *Epoch, was: bool) void { - e.on = was; - } - fn begin(e: *Epoch) void { std.debug.assert(!e.on); e.id +%= 1; diff --git a/src/terminal.zig b/src/terminal.zig index fcb284a0..aedc007f 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -143,6 +143,11 @@ pub const MarkedHandler = if (enabled) struct { } else void; pub const State = struct { + /// What the emulator and this struct are allocated with: the core's, + /// but on a screen-state replica the C heap (`emulator_gpa`), outside + /// its rollback epoch: written by deltas while a copy is held, no part + /// of it is ever a copy's to save or put back. + gpa: std.mem.Allocator, vt: VtSlot, stream: StreamSlot, /// The emulator's answers to its program (DSR, DA, kitty graphics, ...) @@ -281,7 +286,7 @@ pub const State = struct { .kitty_image_storage_limit = if (images_enabled) TerminalImages.storage_limit else 0, }); defer decoded.deinit(gpa); - out.* = .{ .vt = decoded.toOwned(), .stream = undefined }; + out.* = .{ .gpa = gpa, .vt = decoded.toOwned(), .stream = undefined }; if (comptime images_enabled) inline for (.{ .primary, .alternate }) |key| { if (out.vt.screens.get(key)) |screen| screen.kitty_images.image_limits = .allWithTempDir(tempDir()); }; @@ -582,6 +587,9 @@ pub fn takeMarks(state: *State, bytes: []const u8) void { m.finished = if (seq == std.math.maxInt(u32)) null else .{ .seq = seq, .status = if (status == std.math.minInt(i32)) null else status }; } +/// A screen-state replica's emulators' allocator (`State.gpa`). +pub const emulator_gpa = std.heap.c_allocator; + /// A replica's speculation leaves an emulator be: it is the authority's /// (docs/next-daemon.md, phase 5), so the input waits for its order. fn authorityOnly() bool { @@ -629,7 +637,8 @@ pub fn resizeGrid(pane: *Pane, gpa: std.mem.Allocator, cols: u16, rows: u16) voi // A replica's emulator is sized by its authority: the next delta says. if (pardes.tape.screenReplica()) return; state.changes +%= 1; - state.vt.resize(gpa, .{ .cols = cols, .rows = rows }) catch {}; + _ = gpa; + state.vt.resize(state.gpa, .{ .cols = cols, .rows = rows }) catch {}; } /// DECSET 2004: the program wants its pastes bracketed. @@ -829,8 +838,9 @@ pub fn deinitEmulator(pane: *Pane, gpa: std.mem.Allocator) void { const state = pane.terminal orelse return; if (comptime images_enabled) state.images.deinit(gpa); state.stream.deinit(); - state.vt.deinit(gpa); - gpa.destroy(state); + const own = state.gpa; + state.vt.deinit(own); + own.destroy(state); pane.terminal = null; } @@ -841,9 +851,11 @@ pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { const pane = try createDoc(gpa, cols, rows); errdefer gpa.destroy(pane); if (comptime !enabled) return pane; - const state = try gpa.create(State); - errdefer gpa.destroy(state); - state.vt = try ghostty_vt.Terminal.init(terminalIo(), gpa, .{ + const own = if (pardes.tape.screenReplica()) emulator_gpa else gpa; + const state = try own.create(State); + errdefer own.destroy(state); + state.gpa = own; + state.vt = try ghostty_vt.Terminal.init(terminalIo(), own, .{ .cols = cols, .rows = rows, .max_scrollback_bytes = 16 * 1024 * 1024, @@ -856,7 +868,7 @@ pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { // it could send itself: it runs as the user, under pardes. .kitty_image_loading_limits = if (images_enabled) .allWithTempDir(tempDir()) else if (@FieldType(ghostty_vt.Terminal.Options, "kitty_image_loading_limits") == void) {} else .direct, }); - state.stream = .init(.{ .handler = .{ .inner = state.vt.vtHandler() }, .allocator = gpa }); + state.stream = .init(.{ .handler = .{ .inner = state.vt.vtHandler() }, .allocator = own }); state.reply_len = 0; state.changes = 0; state.read = false; -- cgit v1.3 From bce88593014a4078dc9259855944f9ddbc19fe54 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 23:14:10 -0300 Subject: views and the rollback copy: a live view's slot keeps no pointer into what the live core owns; a register's texts and a file's location rows are pinned, not copied views.zig: when a view goes live (to, close, drawOthers putting the live one back, only), its parked share is emptied (`emptied`): it was left holding copies of the pointers it handed the live fields (a column tag's text, a chord's argument, the presentation's), stale until it parked again. The rollback copy walks every slot: a stale pointer whose memory the live view had freed and another taken saved that other's bytes and put them back. A pane's share holds no pointer but its selections, which `unpark` already hands over. The test fails without it: a tag's text freed under view 0's stale slot, its memory taken by an allocation the copy has no business with, written under a held copy, and the copy put back. Registers.zig, locations.zig, rollback.zig: a register's values and joined text, and a file's location rows (the dropped ones, a query's saved), are replaced whole and never written, so the copy pins them as it does a file's text, freed raw (a safe build's free would write over one a copy still holds); rows pin whole with what each owns (`pinnedDeep`). In the harness at 30 ms the big copies were a file's location rows, 5-6 MB after the flood, not the register. ReleaseFast, 20 seeds, three clients, p99: copy 5.1 -> 0.22 ms at 30 ms (0.19-0.20 at 80 and 207); rollback 14.9 -> 9.5 ms at 30 ms (1.3-1.4 at 80 and 207), what is left being the replay's digests (half the harness's CPU is Wyhash over the whole core per replayed unit). Tests: a 2 MB register and 500 location rows pinned, not saved, and whole after a speculation drops them and the copy is put back. docs/next-daemon.md: the follow-ups. Co-Authored-By: Claude Opus 5.5 --- docs/next-daemon.md | 44 ++++++++++++++++--- src/Registers.zig | 13 ++++-- src/locations.zig | 9 ++-- src/rollback.zig | 123 +++++++++++++++++++++++++++++++++++++++++++++++++--- src/views.zig | 16 +++++++ 5 files changed, 184 insertions(+), 21 deletions(-) (limited to 'src/rollback.zig') diff --git a/docs/next-daemon.md b/docs/next-daemon.md index 00f66cf8..55921027 100644 --- a/docs/next-daemon.md +++ b/docs/next-daemon.md @@ -1743,7 +1743,7 @@ cores (ReleaseFast). the copy's p99 is 7.8 ms and a rollback's 17 ms: a register holding a terminal's whole grid (`%` then `y` in a shell's text, 2 MB after the flood) is copied whole by every speculation. Registers could be pinned - as file texts are; left as found. + as file texts are. (Done since: the follow-ups below.) - **No local regression.** `pardes-perf-live --live`, A and the new tty interleaved A B A ... A (4 runs of the new one, 5 of A) on the perf cores; the median of the runs' p50s, new/A, present and core: @@ -1782,12 +1782,8 @@ Not done, and why: - Text reads outside `buildRows` (a pointer selection's copy in tty mode, 9P's screen reads) do not ask for the rows; 9P's are the authority's anyway. -- **A hazard for part A, not fixed here.** The rollback copy follows a - view's stale slot (above). If a stale pointer lands on a live allocation - that a speculation then writes in place, the rollback puts the old bytes - back over that allocation's new owner. Emptying a slot's pointers when - its view goes live (views.zig `unpark`) would end it; the copy skipping - the live slot instead diverged, so part of it is still read. +- **A hazard for part A** (fixed since, below): the rollback copy followed + a view's stale slot. - Echo prediction (phase 6): a key into a terminal waits a round trip. - A replica steps every output step of the authority's (bytes-less), about 2000 a second under a flood, each a core step. @@ -1796,6 +1792,40 @@ Not done, and why: - The other agent wires it in: the tty and GUI hosts, `Screens` per attached peer, and a replica frontend drawing after `screen`. +Follow-ups (2026-10-29): +- **A live view's slot keeps no pointer.** When a view goes live (`to`, + `close`, `only`, and `drawOthers` putting the live one back), its parked + share is emptied (views.zig `emptied`): it held the pointers it handed + the live fields (a column tag's text, a chord's argument, the + presentation's) until it parked again, and the copy, walking every + slot, saved and put back whatever memory had since taken their place. + A pane's share holds no pointer but its selections, which `unpark` + already hands over. A test frees a tag's text under the stale slot, + hands its memory to another allocation, writes it under a held copy and + puts the copy back: it fails without the fix. +- **Registers and location rows are pinned, not copied.** A register's + values and joined text, and a file's location rows (with the ones an + edit dropped and a query's saved), are replaced whole and never written, + so the copy pins them as it does a file's text, freed raw. Rows pin + whole with what each owns (its path, its colours: `pinnedDeep`). In the + harness at 30 ms the big copies were not the register but a file's + location rows, 5 to 6 MB after the flood. ReleaseFast, 20 seeds, three + clients, p99: copy 5.1 ms to 0.22 ms at 30 ms (0.19 to 0.20 ms at 80 and + 207); rollback 14.9 to 9.5 ms at 30 ms (1.3 to 1.4 ms at 80 and 207). + What is left of a rollback's tail is the replay's digests: half the + harness's CPU is Wyhash over the whole core at every replayed unit, the + pinned blobs included (part A's digest). Hashing a pinned blob once, + while it lives, would take most of it. +- **The live perf harness ends what it starts.** Every process a live run + starts inherits its private runtime directory: after each cell, and in + the cleanup every path takes, the harness sends TERM, then KILL, to each + whose environment names it (`liveSweep`). With a wrapper that leaves a + daemon-kept detached session (as the remote agent's client-live.sh + does), the old harness left three sessions and their daemon a run; this + one leaves none. The gate's baselines are recorded again from base (main + and the new perf.zig), and .scratch/daemon/base/pardes-perf-live rebuilt + from it. + ## Phase 6: upgrade notes Built 2026-10-29 (`src/upgrade.zig`, the tty and GUI hosts, `daemon.zig`, diff --git a/src/Registers.zig b/src/Registers.zig index 5e23da5b..760aff9a 100644 --- a/src/Registers.zig +++ b/src/Registers.zig @@ -5,12 +5,17 @@ //! computed from the editor rather than stored (`_`, `#`, `.`, `%`) and the //! clipboard's paste side are read in edit.zig, which has the pane at hand. const std = @import("std"); +const freeRaw = @import("memory.zig").freeRaw; const Registers = @This(); list: std.ArrayList(Reg) = .empty, last_search: u21 = '/', +/// A value and the joined text are replaced whole, never written in place, +/// so a replica's rollback copy pins them as it does a file's text +/// (src/rollback.zig `pinned`), and they are freed raw: a safe build's +/// free would write over one the copy still holds. pub const Reg = struct { name: u21, /// gpa-owned, one per range, in document order @@ -30,9 +35,9 @@ pub fn deinit(r: *Registers, gpa: std.mem.Allocator) void { } fn freeReg(gpa: std.mem.Allocator, reg: *Reg) void { - for (reg.values) |v| gpa.free(v); + for (reg.values) |v| freeRaw(gpa, v); gpa.free(reg.values); - if (reg.joined) |j| gpa.free(j); + if (reg.joined) |j| freeRaw(gpa, j); } pub fn get(r: *Registers, name: u21) ?*Reg { @@ -69,9 +74,9 @@ pub fn put(r: *Registers, gpa: std.mem.Allocator, name: u21, bytes: []const u8, freeReg(gpa, reg); reg.* = .{ .name = name, .values = values }; } - gpa.free(reg.values[i]); + freeRaw(gpa, reg.values[i]); reg.values[i] = copy; - if (reg.joined) |j| gpa.free(j); + if (reg.joined) |j| freeRaw(gpa, j); reg.joined = null; } diff --git a/src/locations.zig b/src/locations.zig index cdb228a6..40f22f85 100644 --- a/src/locations.zig +++ b/src/locations.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const freeRaw = @import("memory.zig").freeRaw; const pardes = @import("pardes.zig"); const look = @import("look.zig"); const syntax = @import("syntax.zig"); @@ -25,12 +26,14 @@ pub const Row = struct { colors: []u8 = &.{}, }; +/// Raw (no safe build's overwrite): a replica's rollback copy pins a row +/// blob and what its rows own (src/rollback.zig `pinnedDeep`). pub fn freeRows(gpa: std.mem.Allocator, rows: []Row) void { for (rows) |row| { - gpa.free(row.path); - gpa.free(row.colors); + freeRaw(gpa, row.path); + freeRaw(gpa, row.colors); } - gpa.free(rows); + freeRaw(gpa, rows); } /// A list of rows being built, given up: each row's own bytes, then the list. diff --git a/src/rollback.zig b/src/rollback.zig index 79b8202e..a7027da6 100644 --- a/src/rollback.zig +++ b/src/rollback.zig @@ -11,10 +11,11 @@ //! - `Copy`, the walker's walk of the core's state (src/walk.zig: what a //! digest covers, so views, buffers, registers, counters and terminal //! metadata), saving the bytes of every allocation it reaches, the core -//! struct's own included. A file's text, its line index and its undo -//! snapshots are pinned instead: an edit puts a new buffer in their -//! place (File.setContentSpan) and never writes one, so the copy keeps -//! a reference and the put-off free keeps it alive. +//! struct's own included. A file's text, its line index, its undo +//! snapshots and a register's texts are pinned instead: an edit puts a +//! new buffer in their place (File.setContentSpan, Registers.put) and +//! never writes one, so the copy keeps a reference and the put-off free +//! keeps it alive. //! //! Putting the copy back writes each saved allocation's bytes over it, so //! every pointer in them points where it did, at what was there; then the @@ -197,6 +198,8 @@ pub const Copy = struct { /// of the blobs: a file's text or an undo snapshot's. where: []const u8 = "", pinnable: bool = false, + /// Whether it is a blob of rows that own allocations of their own. + deep: bool = false, const Region = struct { addr: usize, off: usize, len: usize }; const Pin = struct { addr: usize, len: usize, hash: u64, where: []const u8 }; @@ -342,6 +345,7 @@ pub const Copy = struct { if (comptime opaqueTo(F)) continue; c.where = @typeName(T) ++ "." ++ name; c.pinnable = comptime pinned(T, name); + c.deep = comptime pinnedDeep(T, name); if (comptime walk.lengthOf(T, name) != null and @typeInfo(F) == .array) { const len = comptime walk.lengthOf(T, name).?; if (comptime heapIn(@typeInfo(F).array.child, 0)) for (@field(v, name)[0..walk.lengthIn(v, len)]) |*e| try c.visit(@typeInfo(F).array.child, e); @@ -394,11 +398,14 @@ pub const Copy = struct { if (s.len == 0) return; const addr = @intFromPtr(s.ptr); if (try c.reached([]const E, addr)) return; - if (comptime !heapIn(E, 0)) if (c.pinnable and s.len * @sizeOf(E) >= blob_min and c.epoch.?.owns(addr) != null) { + const flat = comptime !heapIn(E, 0); + if ((if (flat) c.pinnable else c.deep) and s.len * @sizeOf(E) >= blob_min and c.epoch.?.owns(addr) != null) { + // A blob of rows with allocations of their own pinned whole: + // they are as immutable as it, and kept alive with it. const bytes = std.mem.sliceAsBytes(s); try c.pins.append(c.meta, .{ .addr = addr, .len = bytes.len, .hash = if (checked) std.hash.Wyhash.hash(0, bytes) else 0, .where = c.where }); return; - }; + } _ = try c.allocation(addr); if (comptime heapIn(E, 0)) for (s) |*e| try c.visit(E, e); } @@ -409,7 +416,21 @@ pub const Copy = struct { fn pinned(comptime T: type, comptime name: []const u8) bool { const File = pardes.panes.File; return (T == File.State and (std.mem.eql(u8, name, "content") or std.mem.eql(u8, name, "line_starts"))) or - (T == File.Snapshot and std.mem.eql(u8, name, "content")); + (T == File.Snapshot and std.mem.eql(u8, name, "content")) or + // A register's values and joined text: a yank of a terminal's whole + // grid is megabytes, which every speculation would otherwise copy. + (T == pardes.Registers.Reg and (std.mem.eql(u8, name, "values") or std.mem.eql(u8, name, "joined"))); +} + +/// Blobs of rows that own allocations of their own (a location's path, its +/// colours), the array and they replaced whole and never written, freed raw +/// (locations.freeRows): a file's location rows (5 MB after a flood of +/// search results), the ones an edit dropped, a query's saved. +fn pinnedDeep(comptime T: type, comptime name: []const u8) bool { + const File = pardes.panes.File; + return (T == File.State and std.mem.eql(u8, name, "location_rows")) or + (T == File.DroppedMarks and std.mem.eql(u8, name, "rows")) or + (T == pardes.panes.Query.Saved and std.mem.eql(u8, name, "rows")); } fn annotatedStruct(comptime T: type) bool { @@ -497,3 +518,91 @@ test "a copy put back is the core as it was; one let go keeps what was done" { p.deinit(); try std.testing.expectEqual(@as(usize, 0), e.live.count()); } + +test "a view's slot while it is live keeps no pointer a rollback could put old bytes back through" { + // The C heap: a freed chunk comes back to the next allocation its size. + var e: Epoch = .{}; + defer e.deinit(); + const gpa = e.allocator(); + const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml", .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa }); + defer p.deinit(); + p.pump(.{}) catch {}; + _ = p.openView() orelse return error.NoView; + // View 0's column tag has a text of its own; out to view 1 and back. + const text = "the column tag of view 0, its own"; + p.col_tags[0].own = try gpa.dupe(u8, text); + p.toView(1); + p.toView(0); + // The live view drops it, and its memory is another's: something the + // copy has no business saving. + const was = p.col_tags[0].own.?; + gpa.free(was); + p.col_tags[0].own = null; + const other = try gpa.alloc(u8, text.len); + defer gpa.free(other); + if (other.ptr != was.ptr) return error.SkipZigTest; // the heap handed out another + @memset(other, 'a'); + var c: Copy = .{}; + defer c.deinit(); + try c.take(&e, p); + // Written in place while the copy is held, then the copy put back. + @memset(other, 'b'); + c.restore(); + try std.testing.expect(std.mem.allEqual(u8, other, 'b')); +} + +test "a register's texts are pinned, not copied, and come back whole" { + var e: Epoch = .{ .child = std.testing.allocator }; + defer e.deinit(); + const gpa = e.allocator(); + const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml", .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa }); + p.pump(.{}) catch {}; + // A terminal's whole grid yanked: megabytes, and joined once read. + const grid = try std.testing.allocator.alloc(u8, 2 << 20); + defer std.testing.allocator.free(grid); + @memset(grid, 'y'); + const reg = pardes.Registers.default; + p.registers.put(gpa, reg, grid, 0, 1, true); + _ = p.registers.text(gpa, reg) orelse return error.NoText; + var c: Copy = .{}; + defer c.deinit(); + try c.take(&e, p); + try std.testing.expect(c.savedBytes() < 1 << 20); + try std.testing.expect(c.pinnedBytes() >= 4 << 20); + // A speculation yanks over it, and is undone. + p.registers.put(gpa, reg, "a word", 0, 1, true); + c.restore(); + const back = p.registers.value(reg, 0) orelse return error.Lost; + try std.testing.expectEqual(grid.len, back.len); + try std.testing.expect(std.mem.allEqual(u8, back, 'y')); + p.deinit(); + try std.testing.expectEqual(@as(usize, 0), e.live.count()); +} + +test "a file's location rows are pinned whole, with what they own, and come back" { + const locations = @import("locations.zig"); + var e: Epoch = .{ .child = std.testing.allocator }; + defer e.deinit(); + const gpa = e.allocator(); + const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml", .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa }); + p.pump(.{}) catch {}; + const f = for (p.panes) |slot| { + if (slot) |pane| if (pane.file) |*file| break file; + } else return error.NoFile; + // A search's rows: each its own path. + const rows = try gpa.alloc(locations.Row, 500); + for (rows) |*row| row.* = .{ .kind = .match, .path = try gpa.dupe(u8, "src/rollback.zig"), .at = .{}, .location_end = 0, .code_start = 0 }; + f.location_rows = rows; + var c: Copy = .{}; + defer c.deinit(); + try c.take(&e, p); + try std.testing.expect(c.pinnedBytes() >= rows.len * @sizeOf(locations.Row)); + // A speculation's new search drops them, and is undone. + locations.freeRows(gpa, f.location_rows); + f.location_rows = &.{}; + c.restore(); + try std.testing.expectEqual(@as(usize, 500), f.location_rows.len); + try std.testing.expectEqualStrings("src/rollback.zig", f.location_rows[499].path); + p.deinit(); + try std.testing.expectEqual(@as(usize, 0), e.live.count()); +} diff --git a/src/views.zig b/src/views.zig index bf9b9147..b9760143 100644 --- a/src/views.zig +++ b/src/views.zig @@ -215,6 +215,7 @@ pub fn undumped(p: *Pardes, others: []const Dumped) !void { /// for one client (a Restore, `-l`) is one view. pub fn only(p: *Pardes) void { for (0..max) |k| if (k != p.view) close(p, @intCast(k)); + emptied(p, p.view); const k = p.view; if (k == 0) return; std.mem.swap(?*Parked, &p.parked[0], &p.parked[k]); @@ -390,6 +391,19 @@ pub fn to(p: *Pardes, k: u8) void { if (k == p.view) return; park(p); unpark(p, k); + emptied(p, k); +} + +/// View `k` is live: its slot, stale until it parks again (written whole +/// then), keeps no pointer to what it handed the live fields. A rollback +/// copy walks every slot (src/rollback.zig): one left there, freed by the +/// live view and the memory another's since, would be saved and its old +/// bytes put back over the other's. (A pane's share holds no pointer but +/// its selections, which `unpark` hands over.) +fn emptied(p: *Pardes, k: u8) void { + const slot = p.parked[k] orelse return; + slot.view = .{ .screen_w = p.screen_w, .screen_h = p.screen_h }; + slot.view.col_tags = @splat(.{ .what = .column, .gpa = p.gpa }); } /// A new view, as the live one is laid out and focused, with its panes @@ -431,6 +445,7 @@ pub fn close(p: *Pardes, k: u8) void { } else unreachable; park(p); unpark(p, other); + emptied(p, other); } const v = &p.parked[k].?.view; for (&v.col_tags) |*t| t.deinit(p.gpa); @@ -478,6 +493,7 @@ pub fn drawOthers(p: *Pardes, h: pardes.Host) !void { park(p); defer { copy(&fields, p, &p.parked[live].?.view); + emptied(p, live); p.view = live; for (p.panes) |s| if (s) |pane| { const pv = &pane.pv[live]; -- cgit v1.3 From f264dbc4a646da695331fe7401fa013cbb720b6a Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 00:25:19 -0300 Subject: the walker's digest: a blob's hash made once and folded in as a word (a 100 MB file was 5 ms a digest, two a unit, on the authority); blob fields annotated `.blob`, which the rollback copy's pins read too walk.zig: a field named in a type's `replica.blob` (a file's text, line index and location rows, an undo snapshot's text, a register's values and joined text, the rows an edit dropped, a query's saved rows) holds immutable blobs. A digest hashes one of 4 KiB or more once, keeps the word by its address and length, and folds the length and word in; `forget` drops the word, from memory.freeRaw and freeUnfilled, through which every such blob is freed raw. Only a Debug build (the tests, the harness, replay) hashes the bytes at every digest all the same and panics where the kept word is not their hash: a blob written in place, or freed past its word. A release build, ReleaseSafe the user's own as well as ReleaseFast, takes the word and never panics on a stale one: at worst a divergence, which a replica records and then stops at (nothing resets or rejoins it yet; the notes say so). File.zig: an edit's free of the old text went to `rawFree` itself and so kept its word; the next text of the same length at the same address folded in the old one (the ReleaseFast harness diverged in 162 of 180 replica runs, in `file.content`), and it goes through freeUnfilled now. Registers.zig: `put` makes a new values array rather than writing the old one. rollback.zig: the copy pins what `.blob` names (`pinned` and `pinnedDeep` gone). Measured, ReleaseFast on the perf cores: with a 100 MB file open (PARDES_DIGEST_BENCH=100, replica.zig's bench), a unit of a key into it went from p50 10.45 ms, p99 41.6 ms to p50 24 us, p99 6.1 ms (the first, which makes the word), a digest from 5.18 ms to 10 us. The harness (20 seeds x 120 inputs x 3 clients): rollback p99 9.5 -> 2.6 ms at 30 ms (1.5 and 1.4 ms at 80 and 207), copy p99 0.27 ms; none diverged, none unequal, no delta wrong. Tests: a blob hashed once, its kept word no longer its hash once written in place, its word gone when freed. docs/next-daemon.md: the follow-up. Co-Authored-By: Claude Opus 5.5 --- docs/next-daemon.md | 44 +++++++++++++++++ src/File.zig | 12 +++-- src/Query.zig | 1 + src/Registers.zig | 38 +++++++------- src/memory.zig | 10 +++- src/replica.zig | 51 +++++++++++++++++++ src/rollback.zig | 36 ++------------ src/walk.zig | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 277 insertions(+), 54 deletions(-) (limited to 'src/rollback.zig') diff --git a/docs/next-daemon.md b/docs/next-daemon.md index 55921027..681a2ed4 100644 --- a/docs/next-daemon.md +++ b/docs/next-daemon.md @@ -1825,6 +1825,50 @@ Follow-ups (2026-10-29): one leaves none. The gate's baselines are recorded again from base (main and the new perf.zig), and .scratch/daemon/base/pardes-perf-live rebuilt from it. +- **A blob's digest is a word, made once.** The walker's digest, taken at + every unit a replica is sent and every unit it replays, hashed every blob + whole each time: a 100 MB file was 5 ms a digest, two digests a unit, on + the user's own session. A blob (a field annotated `.blob`: a file's text + and line index, an undo snapshot, a register's values and joined text, a + file's location rows, the ones an edit dropped, a query's saved) is now + hashed once, its word kept by its address (walk.zig), and the digest + folds that word in. The rollback copy pins what the same annotation + names (`pinned` and `pinnedDeep` are gone). + - **Exact while every free drops the word:** every such blob is freed + raw, through `memory.freeRaw` or `freeUnfilled` (the pins needed raw + frees), and those drop its word, so an address used again is hashed + again. The first ReleaseFast harness run found one that did not: an + edit freed the old text with `rawFree` itself, the next text of the + same length came back at the same address, and the old word was folded + in (162 of 180 replica runs diverged, in `file.content`, named with + PARDES_REPLICA_TRACE); it goes through `freeUnfilled` now. A replica's + blobs born under a copy are never digested (a speculation records + none), so the epoch's own frees leave no word behind. A register's + values array was the one written in place; `put` now makes a new one. + ponytail: the word is kept by address, so a new free path that skips + those two misses it; a word carried in the struct beside its blob if + that bites again. + - **Written in place, or freed past its word:** a Debug build hashes a + blob's bytes at every digest and folds those in, and panics where the + word kept at that address and length is not their hash: a blob written + in place (which the pins forbid too) or freed without dropping its + word. The tests, the replicas harness and replay run Debug and go + through it (and pass); walk.zig's test writes a blob in place and sees + its kept word is no longer its hash. A release build (ReleaseSafe, the + user's own, and ReleaseFast) takes the word and never panics on a stale + one: at worst it shows as a digest divergence. Not recovered yet: a + replica that diverges records why (`Replica.diverged`) and takes no + more units; nothing resets it or joins it again. That is the remote + client's to add (a fresh join from the authority's dump), not a crash. + - Measured (ReleaseFast, alone on the perf cores; PARDES_DIGEST_BENCH=100 + zig build core-test -Dtest-filter="big file open"): with a 100 MB file + open, a unit of a key into it (two digests) went from p50 10.45 ms, + p99 41.6 ms to p50 24 us, p99 6.1 ms (the first, which makes the + file's word), and a digest from 5.18 ms to 10 us. The harness (20 seeds + x 120 inputs x 3 clients, ReleaseFast): rollback p99 at 30 ms went from + 9.5 to 2.6 ms (1.5 and 1.4 ms at 80 and 207 ms, about as before), copy + p99 0.27 ms (0.22 before, within the run's noise); none diverged, none + unequal, no delta wrong. ## Phase 6: upgrade notes diff --git a/src/File.zig b/src/File.zig index 48a996a4..7acf8dd6 100644 --- a/src/File.zig +++ b/src/File.zig @@ -30,6 +30,7 @@ const long_paint: usize = 16 << 10; /// Content and primary selection at one file edit boundary. Keeping only the /// primary avoids putting Text.max_selections ranges in every history entry. pub const Snapshot = struct { + pub const replica = .{ .blob = .{"content"} }; content: []u8, cur_row: i32, cur_col: i32, @@ -139,7 +140,11 @@ pub const History = struct { pub const State = struct { /// What the replay digest leaves out (src/walk.zig): what is made again /// from the text whenever it is wanted. - pub const replica = .{ .derived = .{ "line_starts", "highlights", "highlight_start", "syntax_dirty", "node_tree", "node_tree_revision", "node_history", "tables" } }; + pub const replica = .{ + .derived = .{ "line_starts", "highlights", "highlight_start", "syntax_dirty", "node_tree", "node_tree_revision", "node_history", "tables" }, + // Replaced whole at every edit (setContentSpan), never written. + .blob = .{ "content", "line_starts", "location_rows" }, + }; path: []u8, content: []u8, @@ -840,6 +845,7 @@ fn reportEdit(p: *Pardes, f: *State, new: []const u8) void { } pub const DroppedMarks = struct { + pub const replica = .{ .blob = .{"rows"} }; len: usize, hash: u64, rows: []locations.Row, @@ -908,8 +914,8 @@ pub fn setContentSpan(p: *Pardes, f: *State, new: []u8, span_in: ?Span) void { f.saved_len = f.content.len; } // Raw: `free` overwrites a safe build's freed memory, a whole file's - // worth per keystroke. - if (f.content.len > 0) p.gpa.rawFree(f.content, .of(u8), @returnAddress()); + // worth per keystroke; and its digest word goes (src/walk.zig). + pardes.memory.freeUnfilled(p.gpa, f.content); f.content = new; f.revision +%= 1; f.splice = .{ .revision = f.revision, .head = known.head, .tail = known.tail, .old_len = old_len }; diff --git a/src/Query.zig b/src/Query.zig index 8296aa80..49f58a0e 100644 --- a/src/Query.zig +++ b/src/Query.zig @@ -173,6 +173,7 @@ capped: bool = false, saved: ?Saved = null, pub const Saved = struct { + pub const replica = .{ .blob = .{"rows"} }; content: []u8, rows: []locations.Row, asked: []u8, diff --git a/src/Registers.zig b/src/Registers.zig index 760aff9a..6df7401d 100644 --- a/src/Registers.zig +++ b/src/Registers.zig @@ -12,11 +12,12 @@ const Registers = @This(); list: std.ArrayList(Reg) = .empty, last_search: u21 = '/', -/// A value and the joined text are replaced whole, never written in place, -/// so a replica's rollback copy pins them as it does a file's text -/// (src/rollback.zig `pinned`), and they are freed raw: a safe build's -/// free would write over one the copy still holds. +/// The values (their array too) and the joined text are replaced whole, +/// never written in place (`put` makes a new array), freed raw: blobs +/// (src/walk.zig), which a digest hashes once and a replica's rollback copy +/// pins as it does a file's text. pub const Reg = struct { + pub const replica = .{ .blob = .{ "values", "joined" } }; name: u21, /// gpa-owned, one per range, in document order values: [][]u8, @@ -36,7 +37,7 @@ pub fn deinit(r: *Registers, gpa: std.mem.Allocator) void { fn freeReg(gpa: std.mem.Allocator, reg: *Reg) void { for (reg.values) |v| freeRaw(gpa, v); - gpa.free(reg.values); + freeRaw(gpa, reg.values); if (reg.joined) |j| freeRaw(gpa, j); } @@ -65,19 +66,20 @@ pub fn put(r: *Registers, gpa: std.mem.Allocator, name: u21, bytes: []const u8, }; break :blk &r.list.items[r.list.items.len - 1]; }; - if (fresh or reg.values.len != n) { - const values = gpa.alloc([]u8, n) catch { - gpa.free(copy); - return; - }; - for (values) |*v| v.* = &.{}; - freeReg(gpa, reg); - reg.* = .{ .name = name, .values = values }; - } - freeRaw(gpa, reg.values[i]); - reg.values[i] = copy; - if (reg.joined) |j| freeRaw(gpa, j); - reg.joined = null; + // A new array, the old one's values moved into it but value `i`. + const values = gpa.alloc([]u8, n) catch { + gpa.free(copy); + return; + }; + const keep = !fresh and reg.values.len == n; + for (values, 0..) |*v, j| v.* = if (keep and j != i) reg.values[j] else &.{}; + values[i] = copy; + if (keep) { + freeRaw(gpa, reg.values[i]); + freeRaw(gpa, reg.values); + if (reg.joined) |j| freeRaw(gpa, j); + } else freeReg(gpa, reg); + reg.* = .{ .name = name, .values = values }; } /// The whole register as one text: its values joined by newlines, no diff --git a/src/memory.zig b/src/memory.zig index 1d082964..7adcf914 100644 --- a/src/memory.zig +++ b/src/memory.zig @@ -1,5 +1,6 @@ const std = @import("std"); const builtin = @import("builtin"); +const walk = @import("walk.zig"); const config = @import("pardes_config"); const board = config.platform == .esp32p4; @@ -418,7 +419,10 @@ pub fn dupeUnfilled(allocator: Allocator, bytes: []const u8) Allocator.Error![]u /// Frees without the safe-build overwrite `free` does, for text-sized buffers /// freed on every edit. pub fn freeUnfilled(allocator: Allocator, bytes: []const u8) void { - if (bytes.len > 0) allocator.rawFree(@constCast(bytes), .of(u8), @returnAddress()); + if (bytes.len == 0) return; + // A blob's digest word goes with it (walk.zig). + walk.forget(@intFromPtr(bytes.ptr)); + allocator.rawFree(@constCast(bytes), .of(u8), @returnAddress()); } /// `freeUnfilled` for a slice of any element: a text's line index, made @@ -427,7 +431,9 @@ pub fn freeUnfilled(allocator: Allocator, bytes: []const u8) void { pub fn freeRaw(allocator: Allocator, items: anytype) void { const E = @typeInfo(@TypeOf(items)).pointer.child; const bytes = std.mem.sliceAsBytes(items); - if (bytes.len > 0) allocator.rawFree(@constCast(bytes), .of(E), @returnAddress()); + if (bytes.len == 0) return; + walk.forget(@intFromPtr(bytes.ptr)); + allocator.rawFree(@constCast(bytes), .of(E), @returnAddress()); } /// Gives free memory back to the system, the arenas' and libc's, inside its diff --git a/src/replica.zig b/src/replica.zig index a96a056a..0b55a5bf 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -1011,3 +1011,54 @@ test "a delta applied under a speculation outlives its rollback: the terminal st 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); } + +/// p50 and p99 of `samples`, sorted in place. +fn spread(samples: []u64) [2]u64 { + std.mem.sort(u64, samples, {}, std.sort.asc(u64)); + return .{ samples[samples.len / 2], samples[@min(samples.len - 1, samples.len * 99 / 100)] }; +} + +test "the authority's units with a big file open: its blobs hashed once (PARDES_DIGEST_BENCH=)" { + const arg = std.c.getenv("PARDES_DIGEST_BENCH") orelse return error.SkipZigTest; + const mb = std.fmt.parseInt(usize, std.mem.span(arg), 10) catch 100; + const gpa = std.heap.c_allocator; + const tmp = std.mem.span(std.c.getenv("TMPDIR") orelse "/tmp"); + var path_buf: [256]u8 = undefined; + const path = try std.fmt.bufPrintSentinel(&path_buf, "{s}/pardes-digest-bench.txt", .{tmp}, 0); + { + const fd = std.c.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, @as(std.c.mode_t, 0o644)); + if (fd < 0) return error.OpenFailed; + defer _ = std.c.close(fd); + var line: [80]u8 = undefined; + var n: usize = 0; + var i: usize = 0; + while (n < mb << 20) : (i += 1) { + const text = try std.fmt.bufPrint(&line, "line {d:0>9} of a big file, as a log or a dump is\n", .{i}); + if (std.c.write(fd, text.ptr, text.len) != @as(isize, @intCast(text.len))) return error.WriteFailed; + n += text.len; + } + } + defer _ = std.c.unlink(path); + const a = try Authority.init(gpa, .{ .cols = 120, .rows = 40, .file = path }, TestHost.host(), true); + defer a.deinit(); + _ = try a.unit(0, 0); + var unit_ns: [40]u64 = undefined; + var digest_ns: [40]u64 = undefined; + for ([_]bool{ false, true }) |on| { + walk.blob_words_on = on; + defer walk.blob_words_on = true; + walk.forgetAll(); + for (&unit_ns, &digest_ns, 0..) |*u, *d, k| { + // A key into the file: a unit as a replica is sent it. + const t0 = nowNs(); + _ = try a.local(.{ .key = .{ .cp = if (k % 2 == 0) 'j' else 'k', .text = if (k % 2 == 0) "j" else "k" } }); + u.* = nowNs() - t0; + const t1 = nowNs(); + _ = digestOf(a); + d.* = nowNs() - t1; + } + const us = spread(&unit_ns); + const ds = spread(&digest_ns); + std.debug.print("{d} MB file, blobs {s}: a unit p50 {d} us p99 {d} us; a digest p50 {d} us p99 {d} us\n", .{ mb, if (on) "hashed once" else "hashed whole", us[0] / 1000, us[1] / 1000, ds[0] / 1000, ds[1] / 1000 }); + } +} diff --git a/src/rollback.zig b/src/rollback.zig index a7027da6..37fc481d 100644 --- a/src/rollback.zig +++ b/src/rollback.zig @@ -35,8 +35,8 @@ const Allocator = std.mem.Allocator; const Alignment = std.mem.Alignment; const Pardes = pardes.Pardes; -/// Blobs this long or longer (`pinned`) are pinned, not copied. Checked -/// unchanged in a safe build when the copy goes. +/// Blobs (walk.blob: fields annotated `.blob`) this long or longer are +/// pinned, not copied. Checked unchanged in a safe build when the copy goes. const blob_min = 4096; const checked = builtin.mode == .debug or builtin.mode == .safe; @@ -198,8 +198,6 @@ pub const Copy = struct { /// of the blobs: a file's text or an undo snapshot's. where: []const u8 = "", pinnable: bool = false, - /// Whether it is a blob of rows that own allocations of their own. - deep: bool = false, const Region = struct { addr: usize, off: usize, len: usize }; const Pin = struct { addr: usize, len: usize, hash: u64, where: []const u8 }; @@ -344,8 +342,7 @@ pub const Copy = struct { if (attr.@"comptime" or comptime (annotated(T, "authority", name) or ((annotated(T, "local", name) or annotated(T, "derived", name)) and unsaved(name)))) continue; if (comptime opaqueTo(F)) continue; c.where = @typeName(T) ++ "." ++ name; - c.pinnable = comptime pinned(T, name); - c.deep = comptime pinnedDeep(T, name); + c.pinnable = comptime walk.blob(T, name); if (comptime walk.lengthOf(T, name) != null and @typeInfo(F) == .array) { const len = comptime walk.lengthOf(T, name).?; if (comptime heapIn(@typeInfo(F).array.child, 0)) for (@field(v, name)[0..walk.lengthIn(v, len)]) |*e| try c.visit(@typeInfo(F).array.child, e); @@ -398,9 +395,8 @@ pub const Copy = struct { if (s.len == 0) return; const addr = @intFromPtr(s.ptr); if (try c.reached([]const E, addr)) return; - const flat = comptime !heapIn(E, 0); - if ((if (flat) c.pinnable else c.deep) and s.len * @sizeOf(E) >= blob_min and c.epoch.?.owns(addr) != null) { - // A blob of rows with allocations of their own pinned whole: + if (c.pinnable and s.len * @sizeOf(E) >= blob_min and c.epoch.?.owns(addr) != null) { + // A blob of rows with allocations of their own is pinned whole: // they are as immutable as it, and kept alive with it. const bytes = std.mem.sliceAsBytes(s); try c.pins.append(c.meta, .{ .addr = addr, .len = bytes.len, .hash = if (checked) std.hash.Wyhash.hash(0, bytes) else 0, .where = c.where }); @@ -411,28 +407,6 @@ pub const Copy = struct { } }; -/// The blobs: what an edit puts in place of its predecessor, never writes -/// (File.setContentSpan), and frees raw (memory.freeUnfilled, freeRaw). -fn pinned(comptime T: type, comptime name: []const u8) bool { - const File = pardes.panes.File; - return (T == File.State and (std.mem.eql(u8, name, "content") or std.mem.eql(u8, name, "line_starts"))) or - (T == File.Snapshot and std.mem.eql(u8, name, "content")) or - // A register's values and joined text: a yank of a terminal's whole - // grid is megabytes, which every speculation would otherwise copy. - (T == pardes.Registers.Reg and (std.mem.eql(u8, name, "values") or std.mem.eql(u8, name, "joined"))); -} - -/// Blobs of rows that own allocations of their own (a location's path, its -/// colours), the array and they replaced whole and never written, freed raw -/// (locations.freeRows): a file's location rows (5 MB after a flood of -/// search results), the ones an edit dropped, a query's saved. -fn pinnedDeep(comptime T: type, comptime name: []const u8) bool { - const File = pardes.panes.File; - return (T == File.State and std.mem.eql(u8, name, "location_rows")) or - (T == File.DroppedMarks and std.mem.eql(u8, name, "rows")) or - (T == pardes.panes.Query.Saved and std.mem.eql(u8, name, "rows")); -} - fn annotatedStruct(comptime T: type) bool { return @typeInfo(T) == .@"struct" and @hasDecl(T, "replica"); } diff --git a/src/walk.zig b/src/walk.zig index 5060de3d..33247d42 100644 --- a/src/walk.zig +++ b/src/walk.zig @@ -23,6 +23,10 @@ //! .len = .{ .{ "jumps", "njumps" } }, //! // What a field holds is made with another allocator on a load. //! .alloc = .{ .{ "image", "image" } }, +//! // Immutable blobs: replaced whole, never written, freed raw +//! // (memory.freeRaw, freeUnfilled). A digest hashes each once and +//! // folds that word in; a replica's rollback copy pins them. +//! .blob = .{ "content" }, //! }; //! //! None of the three kinds is in a digest or a dump. A type with a `pub fn @@ -31,6 +35,100 @@ const std = @import("std"); pub const Hasher = std.hash.Wyhash; +const builtin = @import("builtin"); +/// Only a Debug build hashes a blob's bytes at every digest and checks its +/// kept word; a release one, ReleaseSafe the user's own, takes the word. +const rehash = builtin.mode == .debug; + +/// Whether `T.name` holds immutable blobs (`.blob`): a file's text, an undo +/// snapshot, a register's text, a file's location rows. +pub fn blob(comptime T: type, comptime name: []const u8) bool { + @setEvalBranchQuota(100_000); + if (!@hasDecl(T, "replica") or !@hasField(@TypeOf(T.replica), "blob")) return false; + inline for (T.replica.blob) |n| if (comptime std.mem.eql(u8, n, name)) return true; + return false; +} + +// ---- a blob's hash, made once ---- +// +// A digest walks the whole core at every unit a replica is sent (and every +// unit a replica replays): a 100 MB file was hashed in full each time. A +// blob of `blob_min` bytes or more is hashed once instead, its word kept by +// its address, and the digest folds the word in. The word goes when the +// blob is freed (`forget`, from memory.freeRaw and freeUnfilled: every +// such blob is freed raw, for the rollback copy's pins), so an address +// used again is hashed again. A Debug build hashes the bytes all the same, +// folds those in, and stops where a kept word is not their hash: a blob +// written in place (which the pins forbid too), or freed past `forget`. A +// release build never stops there: a stale word shows as a divergence. + +const blob_min = 4096; +var blob_words: std.AutoHashMapUnmanaged(usize, struct { len: usize, word: u64 }) = .empty; +var blob_lock: std.atomic.Value(bool) = .init(false); +var blob_count: std.atomic.Value(usize) = .init(0); +threadlocal var in_blob: bool = false; +/// Off: every blob hashed whole at every digest, as before (the bench's). +pub var blob_words_on = true; + +fn lockBlobs() void { + while (blob_lock.cmpxchgWeak(false, true, .acquire, .monotonic) != null) std.atomic.spinLoopHint(); +} + +fn unlockBlobs() void { + blob_lock.store(false, .release); +} + +/// The blob at `addr` is freed: its word, if it has one, goes with it. +pub fn forget(addr: usize) void { + if (blob_count.load(.acquire) == 0) return; + lockBlobs(); + defer unlockBlobs(); + if (blob_words.remove(addr)) blob_count.store(blob_words.count(), .release); +} + +/// The word kept for the blob at `addr` (a test's). +pub fn wordOf(addr: usize) ?u64 { + lockBlobs(); + defer unlockBlobs(); + return if (blob_words.get(addr)) |b| b.word else null; +} + +/// Every word forgotten (the bench's cold digests). +pub fn forgetAll() void { + lockBlobs(); + defer unlockBlobs(); + blob_words.clearRetainingCapacity(); + blob_count.store(0, .release); +} + +/// A blob as a digest takes it: its length and its word. +fn hashBlob(comptime E: type, items: []const E, h: *Hasher) void { + const addr = @intFromPtr(items.ptr); + const word: u64 = word: { + const kept: ?u64 = if (blob_words_on) kept: { + lockBlobs(); + defer unlockBlobs(); + const b = blob_words.get(addr) orelse break :kept null; + break :kept if (b.len == items.len) b.word else null; + } else null; + if (!rehash) if (kept) |w| break :word w; + in_blob = false; + defer in_blob = true; + var one = Hasher.init(0); + hashSlice(E, items, &one); + const w = one.final(); + if (kept) |k| if (k != w) std.debug.panic("walk: the {d}-byte blob at 0x{x} is not what its kept word was: written in place, or freed without memory.freeRaw", .{ items.len, addr }); + if (blob_words_on and kept == null) { + lockBlobs(); + defer unlockBlobs(); + blob_words.put(std.heap.page_allocator, addr, .{ .len = items.len, .word = w }) catch {}; + blob_count.store(blob_words.count(), .release); + } + break :word w; + }; + h.update(std.mem.asBytes(&items.len)); + h.update(std.mem.asBytes(&word)); +} pub fn digest(comptime T: type, v: *const T) u64 { var h = Hasher.init(0); @@ -46,6 +144,9 @@ pub fn fieldDigests(comptime T: type, v: *const T, out: []u64) []u64 { inline for (s.field_names, s.field_types, s.field_attrs) |name, F, attr| { if (attr.@"comptime" or comptime excluded(T, name)) continue; var h = Hasher.init(0); + const was = in_blob; + in_blob = comptime blob(T, name); + defer in_blob = was; if (comptime lengthOf(T, name)) |len| { hashSlice(Element(F), @field(v, name)[0..lengthIn(v, len)], &h); } else hash(F, &@field(v, name), &h); @@ -180,6 +281,9 @@ pub fn hash(comptime T: type, v: *const T, h: *Hasher) void { inline for (s.field_names, s.field_types, s.field_attrs) |name, F, attr| { if (attr.@"comptime" or comptime excluded(T, name)) continue; if (comptime opaqueTo(F)) @compileError("walk: " ++ @typeName(T) ++ "." ++ name ++ " is " ++ @typeName(F) ++ ", which no walk sees into: say what it is (`replica`)"); + const was = in_blob; + in_blob = comptime blob(T, name); + defer in_blob = was; if (comptime lengthOf(T, name)) |len| { hashSlice(Element(F), @field(v, name)[0..lengthIn(v, len)], h); } else hash(F, &@field(v, name), h); @@ -214,6 +318,7 @@ fn hashInt(comptime I: type, n: I, h: *Hasher) void { } fn hashSlice(comptime E: type, items: []const E, h: *Hasher) void { + if (in_blob and items.len * @sizeOf(E) >= blob_min) return hashBlob(E, items, h); h.update(std.mem.asBytes(&items.len)); if (comptime flat(E)) return h.update(std.mem.sliceAsBytes(items)); for (items) |*e| hash(E, e, h); @@ -1584,3 +1689,37 @@ test "a dump that is not one, or is cut short, is refused" { // What it made before the end is freed again. try std.testing.expectError(error.EndOfDump, load(S, gpa, bytes[0 .. bytes.len - 2], null)); } + +test "a blob is hashed once and folded in as a word; one written in place is not its word, which a Debug build checks; a freed one's word goes" { + const memory = @import("memory.zig"); + const digestOfBlob = struct { + fn of(items: []const u8) u64 { + var h = Hasher.init(0); + hashSlice(u8, items, &h); + return h.final(); + } + }.of; + const gpa = std.testing.allocator; + const File = struct { + pub const replica = .{ .blob = .{"content"} }; + content: []u8, + cursor: u32 = 0, + }; + const text = try gpa.alloc(u8, 64 << 10); + @memset(text, 'a'); + var f: File = .{ .content = text }; + const before = digest(File, &f); + try std.testing.expect(wordOf(@intFromPtr(text.ptr)) != null); + // The same word again: nothing it holds moved. + try std.testing.expectEqual(before, digest(File, &f)); + // Written in place, which a blob never is: its kept word is no longer + // its hash, which a Debug build's digest checks (and panics at) where a + // release build's would fold the old word in. + text[1000] = 'b'; + try std.testing.expect(wordOf(@intFromPtr(text.ptr)).? != digestOfBlob(text)); + text[1000] = 'a'; + try std.testing.expectEqual(digestOfBlob(text), wordOf(@intFromPtr(text.ptr)).?); + // Freed, its word goes: an address used again is hashed again. + memory.freeUnfilled(gpa, text); + try std.testing.expect(wordOf(@intFromPtr(text.ptr)) == null); +} -- cgit v1.3 From 3703c2526fb59708b00e12816bd88ffd8a15fc69 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 01:34:23 -0300 Subject: echo: a replica draws a key typed into a terminal at once, mosh's way (a printable character at the cursor, Backspace, Left and Right, in tty mode on the primary screen), from an overlay of its own that no digest, dump or rollback copy sees, painted over the terminal's body at the end of renderBody (the tty's and the GUI's); judged at each delta once the authority has the key and 50 ms have passed, every prediction taken back on a mismatch; drawn at once at a prompt pardes's marks vouch for, elsewhere only once an echo was confirmed, never after a drawn misprediction until one is, so a password prompt or a program that ignores input shows nothing typed; the keys typed behind one not predicted (Tab, Enter) predicted over the screen that shows its doing; zig build replicas -- --echo types a corpus into a played shell at 30/80/207 ms: a key to its character p50 239 -> 8.9 ms and p90 248 -> 15.8 ms at 207, no wrong character drawn; docs/next-daemon.md: the decisions, the hook for the remote client, the numbers, the gates and what is not done Co-Authored-By: Claude Opus 5.5 --- docs/next-daemon.md | 232 ++++++++++++++++ src/body_layer.zig | 3 + src/echo.zig | 781 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/pardes.zig | 7 + src/replica.zig | 16 ++ src/rollback.zig | 5 +- src/terminal.zig | 2 + test/replicas.zig | 766 +++++++++++++++++++++++++++++++++++++++++++++++++-- 8 files changed, 1781 insertions(+), 31 deletions(-) create mode 100644 src/echo.zig (limited to 'src/rollback.zig') diff --git a/docs/next-daemon.md b/docs/next-daemon.md index 0fe72b76..5039c81e 100644 --- a/docs/next-daemon.md +++ b/docs/next-daemon.md @@ -2312,3 +2312,235 @@ Not done, and why: 9p(1) is still one round trip per message (1 s for a read at 207 ms). A python helper for remote scripting could pipeline the same way; nobody scripts that way yet. + +## Phase 6: predicted echo notes + +Built 2026-10-30 (`src/echo.zig`; hooks in `src/replica.zig`; a field on +the core and one call at the end of `body_layer.renderBody`; `zig build +replicas -- --echo`). Transport-free like part B: proven in part A's +in-process harness, carried by the remote client. A key typed into a +terminal pane goes to the authority, and its echo comes back as a delta a +round trip later: 207 ms a character in a shell on the FreeBSD box. The +replica now draws it at once, mosh's way, and the authority's screen +confirms or takes it back. Decisions at the forks, with the ponytail lens: + +- **Presentation, the replica's own.** The overlay is `Replica.echo`; the + core points at it (`Pardes.echo`, `.local`, and in rollback.zig's + `unsaved`, so the copy neither saves nor puts it back). No digest, dump + or tape sees it, and the core's emulator stays the authority's. Its + only write into what is drawn is `Echo.paint`, at the end of + `renderBody` for a terminal pane: the cells predicted and the cursor + after them, over the body as drawn. A terminal's body is painted + straight onto the grid (a body layer is a file's with context rows), + and the tty and the GUI both draw the grid's cells and cursor, so both + get it. A session's own core has `echo` null: one null test a terminal + pane a frame, and no overlay. +- **What is predicted:** a printable ASCII character at the cursor, + Backspace, Left and Right, typed into the view's terminal in tty mode + (keys going to its program), on the primary screen, at the live bottom, + with the cursor shown. Never for a chord, Enter, Tab, Esc, Up and Down, + a paste, or anything on the alternate screen (vim never sees one). More + narrowly, so as never to draw what the line editor would not: + - a character only over a blank cell or the same character (typing + along a fish suggestion); a line edited in its middle, or a + suggestion left, is the shell's to redraw; + - not in the last column (wrap is the program's); + - Backspace only at the end of the line, over a prompt's input or a + predicted character; Left over the same; Right over typed text, a + prompt's input in the colour of the cell before it (past the text's + end fish's Right takes its suggestion). + Each prediction is a cell (an absolute row, the authority's numbering + that the delta's header carries) and the cursor after it; a newer key's + cell replaces an older one's, as mosh's overlay does. +- **Judged once the authority has the key and its echo has had time.** + Each unit of the replica's own says which of its keys the authority + stepped (`Echo.confirmed`). A stream that loses nothing keeps the order + the authority sent in, and it sends what it owes of the terminals + before it steps an input, so a delta that arrives after that unit was + made after the key was written. 50 ms after the unit (`grace_ns`, + mosh's ECHO_TIMEOUT: the shell's echo, then the authority's next frame) + each applied delta judges it: a cell the screen shows goes (confirmed), + one it does not is a misprediction and every prediction goes. A + predicted space (Backspace's too) is judged neither way, as mosh's + blanks are; the cursor after the newest key is judged with it. With no + delta at all (nothing changed: no echo), a frame judges it 250 ms + later (`stale_ns`). +- **A key that is not predicted** (Enter, Tab, a chord, or one the rules + above refuse) is the next to wait for: once the authority has it, every + prediction made before it goes, judged neither way (the deltas from + then show that key's doing, which nothing predicted). The keys typed + behind it wait (`deferred`) until its grace has passed; then those the + authority does not have yet are predicted, in order, over the screen + that shows its doing (`catchUp`). One the authority has whose grace is + not over puts that off a little. So the characters typed straight on + after Tab are drawn where the completion left the cursor once it shows + (a round trip and 50 ms after Tab), the rest at once, and never where + the cursor was before it. + Found by the harness: the first version predicted nothing until the + wait was over, and every key typed in that time became a new wait, so a + word typed after Tab or Enter at 207 ms was all late (p90 242 ms). An + input that is not a key into this terminal (a click, a command) has no + deferral: where the keys after it go is not known here until the + authority has it. +- **Shown where the echo is vouched for.** At a shell's prompt, pardes's + OSC 133 marks (phase input: a B and no C since) and the cursor's own + semantic content say its line editor echoes: drawn at once. Elsewhere + (`cat`, a shell without pardes's marks) only once an echo was confirmed + there since the last Enter, Tab or chord (`trusted`), so the first + character of each line waits a round trip. A key before the last one + not predicted vouches for nothing after it: the harness found the + characters of `sudo ls`, confirmed after its Enter, vouching for the + password prompt that Enter brought up. After a misprediction that was + drawn, nothing is drawn until an echo is confirmed again (`doubt`). A + password prompt or a program that ignores what is typed confirms none, + so nothing typed there is ever drawn. +- **Underlined off a prompt only.** At a prompt the marks vouch for the + echo, and an underline trailing every character for a round trip at + 207 ms is noise; off one the trust is mosh's inference, and the + underline says so. After a misprediction nothing is drawn at all, which + mosh's adaptive underline approximates. +- **No protocol change.** No echo acknowledgement crosses (mosh's + `echo_ack`): the replica's own units stand for it, and the grace for its + 50 ms. No termios flag crosses either: a program's echo is inferred from + confirmations, as mosh does. + +The hook for the remote client (its `Replica` and stream as they are): +- `Replica.init(..., screens = true)` points its core at its echo + (`core.echo = &r.echo`). A replica made another way (the remote line's + `initDump`) does that itself once its core exists. +- Per key: nothing more. `r.input(ev)` predicts after its settle, in + `r.view`. +- Per unit: nothing more. `r.confirm(unit)` tells it which of its keys the + authority has. +- Per delta: nothing more. `r.screen(msg)` judges after each delta it + applies. +- Per frame, before drawing: `if (r.echoFrame()) dirty = true;` judges + what is stale, predicts the keys deferred whose wait is over, and says + whether the overlay changed since the last call; `r.echo.changed` says + the same between frames (after `r.input` and `r.screen`), for a client + that draws only when its state moved (the remote line's `moved`). +- Draw its own view (`core.view == r.view`): the overlay is drawn in no + other, and over a terminal in tty mode only. +- The clock is the monotonic one (`FrameStats.nowNs`) unless `r.echo.now` + is set (the harness's virtual clock). + +Measured. + +- **The harness** (`zig build replicas -Dplatform=tty -- --echo`): one + authority and one replica over part A's delayed, jittered link, the + authority's pty a shell played in the harness (`FakeShell`): a line + editor at a prompt with pardes's marks and Tab completion, and the + programs the corpus runs in it. Each key's echo comes 2 ms after the + authority writes it. The client types a corpus of 222 keys at a + typist's pace (60 to 180 ms a key) and in bursts (12 to 60 ms): lines at + a prompt, Backspace, Left and Right, Tab typed straight on, `sudo` and + its password, vim, a program that ignores what is typed, `cat` (the + kernel's echo, no marks), and a line typed before the last one's prompt. + Each client frame (60 Hz, its own phase) is what its screen shows: its + core rendered, the overlay where it is drawn. Ground truth is the + authority's screen right after each key's echo. +- **Key to its character on the client's screen** (the simulation's clock; + 20 seeds, 3060 keys the shell echoed at each RTT, ms, without → with): + + | | 30 ms | 80 ms | 207 ms | + |---|---|---|---| + | p50 | 49.8 → 8.7 | 102.3 → 8.7 | 239.0 → 8.9 | + | p90 | 59.0 → 15.2 | 111.8 → 15.4 | 247.9 → 15.8 | + | max | 66.9 → 65.3 | 121.5 → 114.5 | 261.7 → 251.6 | + + With it, a key waits for the client's next frame, 0 to 16.7 ms. The tty + host draws an input's frame at once, so a real client's is the frame's + cost. The tail is by design: at 207 ms the p90 of the characters typed + straight on after Tab is 38 ms, of a line typed ahead of its prompt + 48 ms, and of `cat`'s 179 ms (its first characters of a line wait for an + echo to be confirmed); every other part's is 14 to 15 ms. +- **Accuracy** (the same runs, 4440 keys at each RTT): 3718 to 3720 + predicted (84%); 714 to 722 not, by the rules (each Enter, Tab, Ctrl-C, + Ctrl-D and Esc, everything typed in vim, and the characters behind a + key not predicted that the screen showed by the time it was over). Of + those predicted, 3238 to 3240 were what the authority's screen showed + right after the echo; the other 480, at each RTT, are exactly the + password's and the ignoring program's 24 a seed, and none was drawn. + Wrong characters drawn: none, at every RTT (0 keys, 0 frames). Frames a + password's or the ignoring program's key was drawn: 0. vim's keys + predicted: 0. The overlay judged 97 to 132 cells against the screen as + mispredictions, all of them undrawn ones (the password's, the ignoring + program's), and no cursor. Every frame drawn with a prediction shown + (13,083 to 21,009 a cell) had each predicted cell where it was + predicted in the rendered surface. +- **Costs** (ReleaseFast): `paint` p50 50 to 63 ns, p99 86 to 155 ns; the + replica's render with a prediction shown p50 54 µs; a key's `typed` is + a few array scans. +- **No divergence**: the replicas harness (ReleaseFast, 20 seeds x 120 + inputs x 3 clients): none diverged, none unequal, 23,547 deltas checked + and none wrong; its deltas a second (76.3, 75.7, 71.7) are part B's to + the decimal. +- **No local regression.** A session's own core draws no overlay + (`echo` is null: a unit test says the authority's core has none). + `pardes-perf-live --live` (base's driver), main (vtzpkxwu) and this + line interleaved A B A ... A on the perf cores (3 runs of the new one, 4 + of main), ReleaseFast; the median of the runs' p50s, new/main: + + | | new/main | A/A (main) | + |---|---|---| + | key-down, present / core | 0.99 / 0.99 | 1.00 / 0.99 | + | key-right | 0.99 / 1.00 | 1.00 / 1.01 | + | edit-char | 0.97 / 0.98 | 0.99 / 0.98 | + | page-down | 0.85 / 0.92 | 0.82 / 0.85 | + | wheel | 0.97 / 0.99 | 0.98 / 0.98 | + | `yes \| head -c 32M` wall | 1.11 | 1.04 | + | a 1M-line open | 0.93 | 1.01 | + + Every cell's runs overlap main's. The `yes` cell is the kernel's (Tty + throughput notes): main's own runs read 4.5 to 7.1 s in the A/B and 5.8 + to 7.6 s in the A/A, this line's 5.0 to 7.2 s. What the local path + gained: a null test a terminal pane a frame, and 8 bytes of core. + +The gates: +- Unit tests (`zig build core-test -Dtest-filter=echo`): keys typed at a + prompt drawn at once in the rendered surface, unflagged, the cursor past + them, then confirmed by the echo, Backspace too, and the replica's digest + the authority's; a password prompt draws nothing typed and lets it go + when stale, and vim's alternate screen predicts nothing; a misprediction + takes every prediction back and hides the next until an echo is + confirmed, and after Tab the keys behind it are drawn where the + completion left the cursor, or not at all if the screen already shows + them. +- `zig build replicas -- --echo` fails on any divergence, unequal end, + wrong delta, drawn password or ignored key, predicted vim key, or cell + rendered otherwise than predicted; it passes at 20 seeds, Debug and + ReleaseFast. `zig build replicas` passes at 20 seeds, ReleaseFast (and 4 + in Debug). +- Unit tests tty (`zig build unit-test -Dplatform=tty`, its perf gate + too), the full `core-test`, and the 113 snapshot scripts pass; the GUI + build's echo tests pass (`core-test -Dplatform=gui`). + +Not done, and why: +- ASCII only: a width table would let the rest of Unicode in (`kindOf`). +- A line edited in its middle (insert, Backspace), Delete, Home, End, the + readline chords, word moves and a paste are not predicted: the line + editor redraws what is right of the cursor, which a prediction would + have to guess. Each waits a round trip, and the keys typed straight on + behind it are drawn when it is over. +- A key typed after an input that is not a key into this terminal (a + click, a command) and that the replica could not apply itself waits for + the authority, and so do the keys after it until a pause: where they go + is not known here until the authority has it. +- The echo is inferred, not told. No termios flag and no echo + acknowledgement cross. A password prompt is safe because nothing typed + there is ever confirmed. A shell at its prompt that does not echo a key + (zsh's vi command mode) has it drawn until its grace passes, about a + round trip and 50 ms, and then nothing more until an echo is confirmed; + so does a program that turns echo off mid-line in `cat`'s plain echo. +- An echo later than about 30 ms on the authority's host (a loaded box) + makes a drawn character go when judged and come back with the echo: a + flicker, never a wrong character. The grace could follow the measured + echo delay. +- A shell without pardes's marks (fish's own marks, a shell started + without the integration) is trusted only by confirmation, so each line's + first character waits a round trip, as in `cat`. +- The GUI was not run: it draws the grid's cells and cursor that the + harness checks in the rendered surface, and its build compiles and + passes the echo tests. +- The remote client (the other line) wires it: `initDump`'s core, the + frame hook, its `moved`. diff --git a/src/body_layer.zig b/src/body_layer.zig index 5a8d51e5..7a4103c3 100644 --- a/src/body_layer.zig +++ b/src/body_layer.zig @@ -481,6 +481,9 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane } } + // A replica's predicted echo, over what its authority's screen shows. + if (p.echo) |e| if (pane.isTerminal()) e.paint(p, s, pane, id, tx, tw, body_y, body_h, active); + if (rail) |bar| if (bar.rect.h > 0) { // The rail covers the notice bands too: nothing else paints those // two gutter columns, so a gap there keeps the frame-wide border diff --git a/src/echo.zig b/src/echo.zig new file mode 100644 index 00000000..4acd3d11 --- /dev/null +++ b/src/echo.zig @@ -0,0 +1,781 @@ +//! Predicted echo for a replica's terminal panes, mosh's way +//! (docs/next-daemon.md, "Phase 6: predicted echo notes"). A key typed into +//! a terminal goes to the authority, and its echo comes back as screen state +//! a round trip later: this shows it at once. +//! +//! Presentation only. The overlay is the replica's own (`Pardes.echo`, a +//! `.local` field the rollback copy skips), painted over a terminal's body +//! after it is drawn (`paint`, from body_layer.renderBody, which the tty and +//! the GUI share). The core's emulator stays the authority's, and no digest +//! sees any of this. +//! +//! What is predicted: a printable character at the cursor, backspace, and +//! left and right, in tty mode on the primary screen at the live bottom. Any +//! other input this replica could not apply itself (Enter, Tab, a chord, a +//! paste, a click) stops prediction until the authority has confirmed it and +//! its echo has had `grace` to come back: predictions start again from the +//! screen's own cursor. So a key typed after Tab waits a round trip; it is +//! never drawn where the completion was not. +//! +//! Judged as mosh does, once the authority confirmed the key and `grace` +//! passed: at a delta, a cell the screen shows goes, and one it does not is +//! a misprediction, and every prediction goes. With no delta (nothing +//! changed: no echo), a frame judges it `stale` later. Shown at once at a +//! shell's prompt (pardes's OSC 133 marks say its line editor echoes); +//! elsewhere only once an echo was confirmed there since the last key that +//! was not predicted (`cat`, a shell without marks), and underlined; never +//! after a shown misprediction until an echo is confirmed again. A password +//! prompt or a program that ignores input confirms none, so nothing typed +//! there is ever shown. +const std = @import("std"); +const pardes = @import("pardes.zig"); +const terminal = pardes.panes.terminal; +const Pardes = pardes.Pardes; +const Pane = pardes.Pane; +const Surface = pardes.Surface; +const Key = pardes.Key; + +const ms = std.time.ns_per_ms; +/// After the authority confirmed a key, how long its echo has to come back +/// before a delta that does not show it makes a misprediction (mosh's +/// ECHO_TIMEOUT): the shell's echo, then the authority's next frame. +// ponytail: fixed; a loaded host's later echo makes a drawn character go and +// come back. The echo delay measured per terminal would be the bound. +pub const grace_ns = 50 * ms; +/// With no delta at all, how much longer a frame waits to judge a key. +pub const stale_ns = 250 * ms; +const never = std.math.maxInt(u64); +/// Cells predicted at once: a line typed a round trip ahead. +const max = 64; + +/// A cell predicted: `cp` at column `x` of absolute row `row` (the +/// authority's numbering, screen_state's), a space for a cell backspace +/// cleared; `was` what the screen showed there before; `seq` the key's. +pub const Cell = struct { x: u16, row: u64, cp: u21, was: u21, seq: u32, due: u64 = never }; +pub const Cursor = struct { x: u16, row: u64, seq: u32, due: u64 = never }; + +pub const Stats = struct { + /// Keys into a terminal predicted, and not. + predicted: u32 = 0, + unpredicted: u32 = 0, + /// Cells the screen showed when judged; cells and cursors it did not. + confirmed: u32 = 0, + mispredicted: u32 = 0, + cursor_missed: u32 = 0, +}; + +pub const Echo = struct { + /// Off: nothing predicted (the harness's baseline). + on: bool = true, + /// The harness's clock (ns); the monotonic one when null. + now: ?u64 = null, + /// Its client's view, and the terminal it predicts into. + view: u8 = 0, + pane: u8 = 0, + serial: u32 = 0, + /// Each terminal's last delta: its active area's first row, absolute. + tops: [pardes.MAX_PANES]u64 = @splat(0), + cells: [max]Cell = undefined, + n: u8 = 0, + cursor: ?Cursor = null, + /// The newest of its client's inputs the authority confirmed. + acked: u32 = 0, + /// The newest input not predicted, and when its echo is due: nothing is + /// predicted until then. `wait_key`: it was a key forwarded to this + /// terminal (no other pane's), so the keys typed into it meanwhile wait + /// in `deferred`, to be predicted over the screen that shows its doing. + wait: u32 = 0, + wait_due: u64 = 0, + wait_key: bool = false, + deferred: [max]Deferred = undefined, + nd: u8 = 0, + /// An echo confirmed since the last key not predicted: shown off a + /// prompt too. A shown misprediction since the last confirmed echo: + /// shown nowhere. + trusted: bool = false, + doubt: bool = false, + /// What it draws changed: the client draws a frame. + changed: bool = false, + stats: Stats = .{}, + /// Each key's prediction as it is made, when set (the harness's; on + /// the C heap). + log: ?*std.ArrayListUnmanaged(Predicted) = null, + + pub const Predicted = struct { seq: u32, cell: ?Cell, cursor: Cursor }; + + fn time(e: *const Echo) u64 { + return e.now orelse pardes.FrameStats.nowNs(); + } + + const Deferred = struct { seq: u32, kind: ?Kind, due: u64 = never }; + + fn drop(e: *Echo) void { + if (e.n > 0 or e.cursor != null) e.changed = true; + e.n = 0; + e.cursor = null; + } + + fn waiting(e: *const Echo) bool { + return e.acked < e.wait or e.time() < e.wait_due; + } + + fn waitFor(e: *Echo, seq: u32, key: bool) void { + e.wait = seq; + e.wait_due = never; + e.wait_key = key; + e.nd = 0; + } + + /// Its client's input `seq`, after the replica took it: `applied`, + /// it was applied here at once (so it was no key into a terminal). + pub fn typed(e: *Echo, c: *Pardes, view: u8, ev: pardes.Event, seq: u32, applied: bool) void { + e.view = view; + if (applied or comptime !terminal.enabled) return; + switch (ev) { + // ponytail: a motion or focus report a program asked for could + // move its cursor; the judgement catches that. + .mouse => |m| if (m.kind == .motion) return, + .pointer_leave, .focus => return, + else => {}, + } + const id = keyboardPane(c, view) orelse { + // Another pane's, or not a key: where the keys after it go is + // not known here until the authority has it. ponytail: so they + // wait for it too, each, until a pause. + e.waitFor(seq, false); + return; + }; + if (id != e.pane or c.panes[id].?.serial != e.serial) { + e.drop(); + e.nd = 0; + e.pane = @intCast(id); + e.serial = c.panes[id].?.serial; + } + const kind = kindOf(ev); + const key = forwarded(c, ev); + // A key that is not one of those predicted says nothing of the + // program the keys after it reach (Enter: a password prompt's). + if (kind == null) e.trusted = false; + if (!e.on) { + e.stats.unpredicted += 1; + return e.waitFor(seq, key); + } + // Behind keys still deferred: after them, in order. + e.catchUp(c); + if (e.waiting() or e.nd > 0) { + if (e.wait_key and key and e.nd < max) { + e.deferred[e.nd] = .{ .seq = seq, .kind = kind }; + e.nd += 1; + return; + } + e.stats.unpredicted += 1; + return e.waitFor(seq, false); + } + if (kind) |k| if (e.predict(c.panes[id].?, k, seq)) { + e.stats.predicted += 1; + e.changed = true; + return; + }; + e.stats.unpredicted += 1; + e.waitFor(seq, key); + } + + /// The wait over (its key's doing on the screen), the keys deferred + /// behind it that the screen does not show yet are predicted over it, + /// in order; those it shows are not. One the authority has whose echo + /// may be on its way puts it off until its grace has passed. + fn catchUp(e: *Echo, c: *Pardes) void { + if (e.nd == 0 or e.waiting()) return; + const now = e.time(); + var i: usize = 0; + while (i < e.nd and e.deferred[i].due <= now) i += 1; + if (i < e.nd and e.deferred[i].due != never) return; + e.stats.unpredicted += @intCast(i); + const pane = c.panes[e.pane] orelse return e.waitFor(e.wait, false); + if (pane.serial != e.serial) return e.waitFor(e.wait, false); + while (i < e.nd) : (i += 1) { + const d = e.deferred[i]; + if (d.kind) |k| if (e.predict(pane, k, d.seq)) { + e.stats.predicted += 1; + e.changed = true; + continue; + }; + // The next to wait for, the rest behind it. + e.stats.unpredicted += 1; + const rest = e.nd - i - 1; + std.mem.copyForwards(Deferred, e.deferred[0..rest], e.deferred[i + 1 .. e.nd]); + e.wait = d.seq; + e.wait_due = never; + e.wait_key = true; + e.nd = @intCast(rest); + return; + } + e.nd = 0; + } + + /// Whether a key `kind` into terminal `pane` was predicted. + fn predict(e: *Echo, pane: *Pane, kind: Kind, seq: u32) bool { + const state = pane.terminal orelse return false; + const vt = &state.vt; + if (vt.screens.active_key != .primary or !terminal.atBottom(pane) or !vt.modes.get(.cursor_visible)) return false; + const top = e.tops[e.pane]; + const screen = vt.screens.active; + const cur: Cursor = e.cursor orelse .{ .x = @intCast(screen.cursor.x), .row = top + screen.cursor.y, .seq = 0 }; + if (cur.row < top or cur.row - top >= vt.rows or e.n == max) return false; + var next: Cursor = .{ .x = cur.x, .row = cur.row, .seq = seq }; + var cell: ?Cell = null; + switch (kind) { + .char => |cp| { + // The last column wraps as a program pleases; a cell that + // holds another character is a line edited in its middle, + // or a suggestion (fish's) being left: the shell redraws + // what is right of it. ponytail: mosh's shift of the rest + // would cover bash's middle, and mispredict fish's. + if (cur.x + 1 >= vt.cols) return false; + const under = e.cpAt(state, cur.x, cur.row); + if (!blank(under) and under != cp) return false; + cell = .{ .x = cur.x, .row = cur.row, .cp = cp, .was = under, .seq = seq }; + next.x += 1; + }, + // At the end of a line only: in its middle a line editor moves + // the rest left. + .backspace => { + if (cur.x == 0 or !e.isInput(state, cur.x - 1, cur.row) or !blank(e.cpAt(state, cur.x, cur.row))) return false; + cell = .{ .x = cur.x - 1, .row = cur.row, .cp = ' ', .was = e.cpAt(state, cur.x - 1, cur.row), .seq = seq }; + next.x -= 1; + }, + .left => { + if (cur.x == 0 or !e.isInput(state, cur.x - 1, cur.row)) return false; + next.x -= 1; + }, + // Over typed text only: past its end, fish takes the right + // arrow for its suggestion (another colour). + .right => { + if (cur.x + 1 >= vt.cols or !e.isText(state, cur.x, cur.row)) return false; + next.x += 1; + }, + } + if (cell) |want| e.put(want); + e.cursor = next; + if (e.log) |l| l.append(std.heap.c_allocator, .{ .seq = seq, .cell = cell, .cursor = next }) catch {}; + return true; + } + + fn put(e: *Echo, want: Cell) void { + for (e.cells[0..e.n]) |*have| if (have.x == want.x and have.row == want.row) { + const was = have.was; + have.* = want; + have.was = was; + return; + }; + e.cells[e.n] = want; + e.n += 1; + } + + fn mine(e: *const Echo, x: u16, row: u64) ?*const Cell { + for (e.cells[0..e.n]) |*have| if (have.x == x and have.row == row) return have; + return null; + } + + /// What the screen shows at a cell as predicted so far. + fn cpAt(e: *const Echo, state: *terminal.State, x: u16, row: u64) u21 { + if (e.mine(x, row)) |have| return have.cp; + return e.screenCp(state, x, row); + } + + /// What the screen itself shows there; off its active area, a + /// codepoint no key types. + fn screenCp(e: *const Echo, state: *terminal.State, x: u16, row: u64) u21 { + const ci = e.screenCell(state, x, row) orelse return 0x10ffff; + return ci.cell.codepoint(); + } + + fn screenCell(e: *const Echo, state: *terminal.State, x: u16, row: u64) ?terminal.PageCell { + if (comptime !terminal.enabled) return null; + const top = e.tops[e.pane]; + if (row < top or row - top >= state.vt.rows) return null; + return state.vt.screens.active.pages.getCell(.{ .active = .{ .x = x, .y = @intCast(row - top) } }); + } + + /// A prompt's input there, or a character predicted. + fn isInput(e: *const Echo, state: *terminal.State, x: u16, row: u64) bool { + if (e.mine(x, row)) |have| return !blank(have.cp); + const ci = e.screenCell(state, x, row) orelse return false; + return ci.cell.semantic_content == .input; + } + + /// Typed text there: a character predicted, or a prompt's input in the + /// colour of the cell before it. + fn isText(e: *const Echo, state: *terminal.State, x: u16, row: u64) bool { + if (e.mine(x, row)) |have| return !blank(have.cp); + const ci = e.screenCell(state, x, row) orelse return false; + if (ci.cell.semantic_content != .input or blank(ci.cell.codepoint())) return false; + if (x == 0) return true; + const before = e.screenCell(state, x - 1, row) orelse return false; + return before.cell.style_id == ci.cell.style_id; + } + + /// The authority confirmed its client's input `seq`. + pub fn confirmed(e: *Echo, c: *Pardes, seq: u32) void { + e.acked = seq; + const due = e.time() + grace_ns; + for (e.deferred[0..e.nd]) |*d| if (d.seq <= seq and d.due == never) { + d.due = due; + }; + for (e.cells[0..e.n]) |*cell| if (cell.seq <= seq and cell.due == never) { + cell.due = due; + }; + if (e.cursor) |*cur| if (cur.seq <= seq and cur.due == never) { + cur.due = due; + }; + if (e.wait <= seq and e.wait_due == never) { + // The authority stepped a key this did not predict, after every + // one it did: its deltas from here show that key's doing, which + // no prediction knew of. They go, judged neither way. + e.wait_due = due; + e.drop(); + } + e.catchUp(c); + } + + /// Terminal `id`'s delta was applied, its active area's first row now + /// `top`. + pub fn delta(e: *Echo, c: *Pardes, id: u8, top: u64) void { + if (id == e.pane and top != e.tops[id] and (e.n > 0 or e.cursor != null)) { + // Scrolled: the rows predicted are others now. + e.tops[id] = top; + return e.drop(); + } + e.tops[id] = top; + if (id == e.pane) e.judge(c, 0); + e.catchUp(c); + } + + /// Once a frame: what is stale judged. Whether to draw. + pub fn frame(e: *Echo, c: *Pardes) bool { + if (e.n > 0 or e.cursor != null) e.judge(c, stale_ns); + e.catchUp(c); + defer e.changed = false; + return e.changed; + } + + fn judge(e: *Echo, c: *Pardes, slack: u64) void { + if (comptime !terminal.enabled) return; + if (e.n == 0 and e.cursor == null) return; + const now = e.time(); + const pane = c.panes[e.pane] orelse return e.drop(); + if (pane.serial != e.serial) return e.drop(); + const state = pane.terminal orelse return e.drop(); + if (state.vt.screens.active_key != .primary) return e.drop(); + const shown = e.visible(c); + var i: usize = 0; + while (i < e.n) { + const cell = e.cells[i]; + if (now < cell.due) { + i += 1; + continue; + } + if (!blank(cell.cp) and e.screenCp(state, cell.x, cell.row) != cell.cp) { + if (now < cell.due + slack) { + i += 1; + continue; + } + e.stats.mispredicted += 1; + if (shown) e.doubt = true; + e.trusted = false; + return e.drop(); + } + // Shown, or a space (mosh: too easy to show by chance to vouch). + // A key before the last one not predicted vouches for nothing + // after it (a line's characters for the password prompt its + // Enter brought up). + if (!blank(cell.cp)) { + e.stats.confirmed += 1; + if (cell.was != cell.cp and cell.seq > e.wait) { + e.trusted = true; + e.doubt = false; + } + } + e.cells[i] = e.cells[e.n - 1]; + e.n -= 1; + e.changed = true; + } + const cur = e.cursor orelse return; + if (now < cur.due) return; + const screen = state.vt.screens.active; + if (screen.cursor.x == cur.x and e.tops[e.pane] + screen.cursor.y == cur.row) { + e.cursor = null; + e.changed = true; + } else if (now >= cur.due + slack) { + e.stats.cursor_missed += 1; + e.drop(); + } + } + + /// Whether its predictions are drawn now. + pub fn visible(e: *const Echo, c: *const Pardes) bool { + if (e.doubt) return false; + if (e.trusted) return true; + const pane = c.panes[e.pane] orelse return false; + return atPrompt(pane); + } + + /// Over terminal pane `id`'s body, as renderBody drew it: the + /// characters predicted and the cursor after them. + pub fn paint(e: *const Echo, p: *const Pardes, s: *Surface, pane: *const Pane, id: usize, tx: u16, tw: u16, body_y: u16, body_h: u16, active: bool) void { + if (comptime !terminal.enabled) return; + if (e.n == 0 and e.cursor == null) return; + if (id != e.pane or pane.serial != e.serial or p.view != e.view or pane.body.mode != .tty) return; + if (!e.visible(p)) return; + const state = pane.terminal orelse return; + const sb = state.vt.screens.active.pages.scrollbar(); + const top = e.tops[id]; + // Off a prompt, the trust is inferred: underlined, as mosh does. + const flag = !atPrompt(pane); + const at = struct { + fn f(sb_: @TypeOf(sb), pan: u16, h: u16, top_: u64, row: u64) ?u16 { + if (row < top_ or row - top_ >= sb_.len) return null; + const screen_row = sb_.total - sb_.len + (row - top_); + if (screen_row < sb_.offset + pan) return null; + const y = screen_row - sb_.offset - pan; + return if (y < h) @intCast(y) else null; + } + }.f; + for (e.cells[0..e.n]) |cell| { + const y = at(sb, pane.pan, body_h, top, cell.row) orelse continue; + if (cell.x >= tw) continue; + const dst = s.at(tx + cell.x, body_y + y); + // Typed text continues the text before it (mosh's heuristic). + var style = dst.style; + if (cell.x > 0) { + const before = s.at(tx + cell.x - 1, body_y + y); + if (!before.default and !blank(before.text[0])) style = before.style; + } + if (flag and !blank(cell.cp)) style.ul = .single; + dst.default = false; + dst.text[0] = @intCast(cell.cp); + dst.len = 1; + dst.style = style; + } + if (!active or pane.focus != .body) return; + const cur = e.cursor orelse return; + const y = at(sb, pane.pan, body_h, top, cur.row) orelse return; + if (cur.x < tw) s.cursor = .{ .x = tx + cur.x, .y = body_y + y }; + } +}; + +const Kind = union(enum) { char: u21, backspace, left, right }; + +fn kindOf(ev: pardes.Event) ?Kind { + const k = switch (ev) { + .key => |k| k, + else => return null, + }; + if (k.ctrl or k.alt) return null; + switch (k.cp) { + Key.backspace => return .backspace, + Key.left => return .left, + Key.right => return .right, + else => {}, + } + // As forwardKey sends it. ponytail: ASCII only; a width table would + // let the rest in. + const cp: u21 = if (k.text.len == 1) + k.text[0] + else if (k.text.len == 0 and k.cp >= 'a' and k.cp <= 'z' and k.shift) + k.cp - 'a' + 'A' + else if (k.text.len == 0) + k.cp + else + return null; + return if (cp >= 0x20 and cp < 0x7f) .{ .char = cp } else null; +} + +/// Whether key `ev` goes to the terminal's program as it is (forwardKey's) +/// and not to pardes: Esc backs out of the pane, the tty chord leaves tty +/// mode, Ctrl-V pastes the register. +fn forwarded(c: *const Pardes, ev: pardes.Event) bool { + const k = switch (ev) { + .key => |k| k, + else => return false, + }; + if (k.cp == Key.escape) return false; + return !(k.ctrl and (k.cp == c.opts.tty_toggle or k.cp == 'v' or k.cp == 'V')); +} + +fn blank(cp: u21) bool { + return cp == 0 or cp == ' '; +} + +/// At a shell's prompt, reading its input: pardes's marks (a B and no C +/// since) and the cursor's own, on the primary screen. A shell without +/// pardes's marks is trusted only once its echo is confirmed. +fn atPrompt(pane: *const Pane) bool { + if (comptime !terminal.enabled) return false; + const state = pane.terminal orelse return false; + return state.vt.screens.active_key == .primary and state.stream.handler.phase == .input and terminal.promptInputReady(pane); +} + +/// The terminal view `view`'s keys go to, in tty mode: none when they go +/// elsewhere (a prompt, a header, window mode, another pane). +fn keyboardPane(c: *const Pardes, view: u8) ?usize { + const live = c.view == view; + const part = if (live) null else &(c.parked[view] orelse return null).view; + const id = if (part) |v| v.active else c.active; + if (if (part) |v| v.window_pick != null or v.header_focus else c.window_pick != null or c.header_focus) return null; + const pane = c.panes[id] orelse return null; + if (!pane.isTerminal() or pane.terminal == null or pane.prompt != .none) return null; + const mode = if (live) pane.body.mode else pane.pv[view].body.mode; + const focus = if (live) pane.focus else pane.pv[view].focus; + return if (mode == .tty and focus == .body) id else null; +} + +// ---- tests: one authority and one replica, its shell played by hand ---- + +const testing = std.testing; +const replica = @import("replica.zig"); + +const Rig = struct { + a: *replica.Authority, + r: *replica.Replica, + screens: replica.Screens, + peer: u16, + shell: u8, + /// What the authority wrote its shell's pty. + written: std.ArrayListUnmanaged(u8) = .empty, + now: u64 = 1_000 * ms, + + fn ptyWrite(ctx: ?*anyopaque, _: u8, bytes: []const u8) void { + const g: *Rig = @ptrCast(@alignCast(ctx.?)); + g.written.appendSlice(testing.allocator, bytes) catch {}; + } + fn watched(_: ?*anyopaque) bool { + return false; + } + const vtable: pardes.Host.VTable = .{ .watched = watched, .pty_write = ptyWrite }; + + fn init(g: *Rig) !void { + const gpa = testing.allocator; + g.written = .empty; + g.now = 1_000 * ms; + g.a = try replica.Authority.init(gpa, .{ .cols = 60, .rows = 16, .boot = .tty_shell }, .{ .ctx = g, .vtable = &vtable }, true); + g.screens = .init(gpa); + g.peer = try g.screens.addPeer(); + g.r = try replica.Replica.init(gpa, 1, try g.a.unit(0, 0), true); + g.shell = for (g.a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + g.r.echo.now = g.now; + } + + fn deinit(g: *Rig) void { + g.r.deinit(); + g.screens.deinit(g.a.core); + g.a.deinit(); + g.written.deinit(testing.allocator); + } + + fn later(g: *Rig, dt: u64) void { + g.now += dt; + g.r.echo.now = g.now; + } + + /// The shell prints `bytes`; the replica gets the unit and the delta. + fn prints(g: *Rig, bytes: []const u8) !void { + g.a.output(.{ .output = .{ .pane = g.shell, .bytes = bytes } }); + try g.sync(); + } + + fn sync(g: *Rig) !void { + const gpa = testing.allocator; + if (try g.a.pending()) |u| g.r.confirm(u); + g.r.settle(); + var msgs: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (msgs.items) |m| gpa.free(m); + msgs.deinit(gpa); + } + try g.screens.flush(g.a.core, g.peer, &msgs); + for (msgs.items) |m| if (g.r.screen(m)) |reply| { + _ = try g.screens.take(g.a.core, g.peer, reply); + }; + } + + /// The replica's client types `k`; the message for the authority. + fn key(g: *Rig, k: Key) ![]u8 { + return testing.allocator.dupe(u8, try g.r.input(.{ .key = k })); + } + + /// The authority takes it, and the replica its unit. + fn take(g: *Rig, msg: []u8) !void { + defer testing.allocator.free(msg); + if (try g.a.pending()) |u| g.r.confirm(u); + g.r.confirm(try g.a.take(msg)); + g.r.settle(); + } + + /// What the replica's screen shows at the shell's cursor row, drawn, + /// and whether the cell at `x` is underlined. + fn drawn(g: *Rig, arena: *std.heap.ArenaAllocator, x: u16) !struct { cp: u8, ul: bool, cursor: ?u16 } { + _ = arena.reset(.retain_capacity); + const c = g.r.core(); + c.needs_frame = true; + const s = try c.render(arena.allocator()); + const pane = c.panes[g.shell].?; + const body = s.region(.body, g.shell).?.rect; + const y = body.y + terminal.gridCursor(pane).y; + const cell = s.at(body.x + x, y); + return .{ .cp = cell.text[0], .ul = cell.style.ul != .off, .cursor = if (s.cursor) |cur| if (cur.y == y) cur.x - body.x else null else null }; + } +}; + +const prompt = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07"; + +test "keys typed at a shell's prompt are drawn at once, and the authority's echo takes their place" { + if (comptime !terminal.enabled) return error.SkipZigTest; + var g: Rig = undefined; + try g.init(); + defer g.deinit(); + try g.prints(prompt); + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + const before = try g.drawn(&arena, 2); + try testing.expectEqual(@as(?u16, 2), before.cursor); + // Typed: shown at once, the cursor past it, and nothing underlined at + // a prompt. + const l = try g.key(.{ .cp = 'l', .text = "l" }); + const s = try g.key(.{ .cp = 's', .text = "s" }); + try testing.expectEqual(@as(u32, 2), g.r.echo.stats.predicted); + var shown = try g.drawn(&arena, 2); + try testing.expectEqual(@as(u8, 'l'), shown.cp); + try testing.expect(!shown.ul); + try testing.expectEqual(@as(?u16, 4), shown.cursor); + try testing.expectEqual(@as(u8, 's'), (try g.drawn(&arena, 3)).cp); + // The authority's core draws no overlay of its own. + try testing.expect(g.a.core.echo == null); + // The authority takes them, its shell echoes, the delta lands after the + // grace: confirmed, and the screen's own cells and cursor shown. + try g.take(l); + try g.take(s); + try testing.expectEqualStrings("ls", g.written.items); + g.later(grace_ns); + try g.prints("ls"); + try testing.expectEqual(@as(u8, 0), g.r.echo.n); + try testing.expect(g.r.echo.cursor == null); + try testing.expectEqual(@as(u32, 2), g.r.echo.stats.confirmed); + shown = try g.drawn(&arena, 3); + try testing.expectEqual(@as(u8, 's'), shown.cp); + try testing.expectEqual(@as(?u16, 4), shown.cursor); + // Backspace at the end of the line, shown at once. + const bs = try g.key(.{ .cp = Key.backspace }); + shown = try g.drawn(&arena, 3); + try testing.expectEqual(@as(u8, ' '), shown.cp); + try testing.expectEqual(@as(?u16, 3), shown.cursor); + try g.take(bs); + g.later(grace_ns); + try g.prints("\x08\x1b[K"); + try testing.expectEqual(@as(u32, 0), g.r.echo.stats.mispredicted + g.r.echo.stats.cursor_missed); + try testing.expect(g.r.quiet()); + try testing.expectEqual(replica.digestOf(g.a), g.r.digest()); +} + +test "a password prompt shows nothing typed, and a full-screen program is not predicted" { + if (comptime !terminal.enabled) return error.SkipZigTest; + var g: Rig = undefined; + try g.init(); + defer g.deinit(); + try g.prints(prompt ++ "sudo ls\r\n\x1b]133;C;aid=pardes\x07[sudo] password: "); + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + const x = terminal.gridCursor(g.r.core().panes[g.shell].?).x; + const k = try g.key(.{ .cp = 'h', .text = "h" }); + // Predicted, and not drawn: nothing has confirmed an echo here. + try testing.expectEqual(@as(u32, 1), g.r.echo.stats.predicted); + try testing.expectEqual(@as(u8, ' '), (try g.drawn(&arena, x)).cp); + try g.take(k); + // No echo, so no delta: a frame lets it go once it is stale. + g.later(grace_ns + stale_ns); + _ = g.r.echoFrame(); + try testing.expectEqual(@as(u8, 0), g.r.echo.n); + try testing.expectEqual(@as(u32, 1), g.r.echo.stats.mispredicted); + // The next key still shows nothing. + const k2 = try g.key(.{ .cp = 'i', .text = "i" }); + try testing.expectEqual(@as(u8, ' '), (try g.drawn(&arena, x)).cp); + try g.take(k2); + // vim: the alternate screen, never predicted. + try g.prints("\r\n\x1b]133;D;1;aid=pardes\x07" ++ prompt ++ "vim\r\n\x1b]133;C;aid=pardes\x07\x1b[?1049h\x1b[H~"); + g.later(grace_ns + stale_ns); + _ = g.r.echoFrame(); + const before = g.r.echo.stats.predicted; + try g.take(try g.key(.{ .cp = 'i', .text = "i" })); + try testing.expectEqual(before, g.r.echo.stats.predicted); +} + +test "a misprediction takes every prediction back and shows none until an echo is confirmed; after Tab nothing is predicted until its echo" { + if (comptime !terminal.enabled) return error.SkipZigTest; + var g: Rig = undefined; + try g.init(); + defer g.deinit(); + try g.prints(prompt); + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + // A line editor that echoes something else (vi's command mode): shown, + // then taken back. + const a = try g.key(.{ .cp = 'a', .text = "a" }); + try testing.expectEqual(@as(u8, 'a'), (try g.drawn(&arena, 2)).cp); + try g.take(a); + g.later(grace_ns); + try g.prints("A"); + try testing.expectEqual(@as(u32, 1), g.r.echo.stats.mispredicted); + try testing.expect(g.r.echo.doubt); + try testing.expectEqual(@as(u8, 'A'), (try g.drawn(&arena, 2)).cp); + // Doubted: predicted, not drawn, until one is confirmed. + const b = try g.key(.{ .cp = 'b', .text = "b" }); + try testing.expectEqual(@as(u8, ' '), (try g.drawn(&arena, 3)).cp); + try g.take(b); + g.later(grace_ns); + try g.prints("b"); + try testing.expect(!g.r.echo.doubt); + const c = try g.key(.{ .cp = 'c', .text = "c" }); + try testing.expectEqual(@as(u8, 'c'), (try g.drawn(&arena, 4)).cp); + // Tab: not predicted, and neither is what follows until the authority + // has it and its echo has had its grace. + const tab = try g.key(.{ .cp = Key.tab }); + const d = try g.key(.{ .cp = 'd', .text = "d" }); + try testing.expectEqual(@as(u32, 1), g.r.echo.stats.unpredicted); + try g.take(c); + try testing.expectEqual(@as(u8, 'c'), (try g.drawn(&arena, 4)).cp); + // The authority has Tab (and the key after it): what was predicted + // before it goes, the screen's own shown. + try g.take(tab); + try g.take(d); + try testing.expectEqual(@as(u8, 0), g.r.echo.n); + g.later(grace_ns); + try g.prints("c" ++ "ompleted d"); + // `d`, typed behind Tab, the screen showed by then: not predicted. + try testing.expectEqual(@as(u32, 2), g.r.echo.stats.unpredicted); + const e = try g.key(.{ .cp = 'e', .text = "e" }); + try testing.expectEqual(@as(u8, 'e'), (try g.drawn(&arena, 15)).cp); + try g.take(e); + g.later(grace_ns); + try g.prints("e"); + // A key typed behind Tab that the authority does not have yet is + // predicted once Tab's doing is on the screen, where it now goes. + const tab2 = try g.key(.{ .cp = Key.tab }); + const f = try g.key(.{ .cp = 'f', .text = "f" }); + try g.take(tab2); + try g.prints("ff"); + try testing.expectEqual(@as(u8, 0), g.r.echo.n); + g.later(grace_ns); + _ = g.r.echoFrame(); + try testing.expectEqual(@as(u8, 1), g.r.echo.n); + try testing.expectEqual(@as(u8, 'f'), (try g.drawn(&arena, 18)).cp); + try g.take(f); + g.later(grace_ns); + try g.prints("f"); + try testing.expectEqual(@as(u8, 0), g.r.echo.n); + try testing.expectEqual(@as(u32, 1), g.r.echo.stats.mispredicted); + try testing.expectEqual(@as(u32, 0), g.r.echo.stats.cursor_missed); + try testing.expect(g.r.quiet()); + try testing.expectEqual(replica.digestOf(g.a), g.r.digest()); +} diff --git a/src/pardes.zig b/src/pardes.zig index 081861ac..02983c84 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -67,6 +67,7 @@ pub const screen_state = if (terminal_panes) @import("screen_state.zig") else st pub const views = @import("views.zig"); pub const rollback = @import("rollback.zig"); pub const replica = @import("replica.zig"); +pub const echo = @import("echo.zig"); pub const image = @import("image.zig"); pub const dump = @import("dump.zig"); pub const lsp = @import("lsp/lsp.zig"); @@ -478,6 +479,7 @@ pub const Loc = struct { test { _ = @import("rollback.zig"); _ = @import("replica.zig"); + _ = @import("echo.zig"); _ = @import("modal.zig"); _ = @import("tagline.zig"); _ = @import("hunks.zig"); @@ -4698,6 +4700,10 @@ pub const Pardes = struct { restore_buf: [1024]u8 = @splat(0), surface: Surface = .{}, + /// A replica's predicted echo (src/echo.zig), drawn over its terminals: + /// presentation, its own, in no digest and no rollback copy. Null on a + /// session's own core. + echo: ?*echo.Echo = null, /// The view the fields above are of: whose layout, focus, pointer and /// panes' cursors and scroll (views.zig). `views_open` says which views /// there are, one a window or terminal; each not live is in `parked`. @@ -4727,6 +4733,7 @@ pub const Pardes = struct { "surface", "presentation", "frames", "chrome_animation", "lifts", "lift_serials", "cursor_glide", "cursor_view", "cursor_crossing", "cursor_held", "frame_arena", "scratch", "needs_frame", "present_skipped", "shell_continuous", "native_image_patches", + "echo", // Drained by every pump before its digest; a replica's queues // are its own. "effects", "effects_head", "effects_len", "in_q", "in_head", "in_len", diff --git a/src/replica.zig b/src/replica.zig index 0b55a5bf..032e50af 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -467,6 +467,9 @@ pub const Replica = struct { /// Its terminals' screen state, as the authority's deltas made it. screens: [pardes.MAX_PANES]?ScreenOf = @splat(null), screen_out: std.ArrayListUnmanaged(u8) = .empty, + /// Its predicted echo (src/echo.zig), drawn over its core's terminals: + /// fed by its keys, its units and its deltas here, and by `echoFrame`. + echo: pardes.echo.Echo = .{}, const ScreenOf = struct { serial: u32, @@ -490,6 +493,7 @@ pub const Replica = struct { r.deinit(); return error.NoCore; } + if (screens) r.core().echo = &r.echo; return r; } @@ -538,6 +542,7 @@ pub const Replica = struct { try r.pending.append(r.gpa, .{ .seq = seq, .msg = msg }); 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; @@ -548,6 +553,7 @@ pub const Replica = struct { if (r.diverged != null) return; const u = parseUnit(msg) orelse return r.fail("a unit too short to read"); r.stats.units += 1; + if (u.origin == r.id) if (r.t.core) |c| r.echo.confirmed(c, u.seq); 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 @@ -716,6 +722,15 @@ pub const Replica = struct { 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. pub fn quiet(r: *const Replica) bool { @@ -754,6 +769,7 @@ pub const Replica = struct { r.stats.apply_ns.append(r.gpa, nowNs() - t0) catch {}; sc.refused = false; terminal.takeMarks(state, applied.extra); + r.echo.delta(c, m.pane, sc.rep.top); // Its rows as the core keeps them read are another grid's. c.shell_rows.markStale(pane); var seq: [4]u8 = undefined; diff --git a/src/rollback.zig b/src/rollback.zig index 37fc481d..6496a69f 100644 --- a/src/rollback.zig +++ b/src/rollback.zig @@ -415,10 +415,11 @@ fn annotatedStruct(comptime T: type) bool { /// is taken: what is in their slots is nothing), the frame statistics /// (numbers, and a static name), and the rows a terminal was last read as /// (a grid's worth of text, emptied when a copy is taken and when it is -/// put back: src/replica.zig). +/// put back: src/replica.zig), and a replica's predicted echo (src/echo.zig: +/// the replica's, never its core's). fn unsaved(comptime name: []const u8) bool { return std.mem.eql(u8, name, "effects") or std.mem.eql(u8, name, "in_q") or std.mem.eql(u8, name, "frames") or - std.mem.eql(u8, name, "shell_rows"); + std.mem.eql(u8, name, "shell_rows") or std.mem.eql(u8, name, "echo"); } fn annotated(comptime T: type, comptime kind: []const u8, comptime name: []const u8) bool { diff --git a/src/terminal.zig b/src/terminal.zig index aedc007f..3a589659 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -20,6 +20,8 @@ const ghostty_vt = if (enabled) @import("ghostty-vt") else struct {}; pub const history_max = if (enabled) 64 else 8; pub const VtSlot = if (enabled) ghostty_vt.Terminal else void; +/// A cell of an emulator's pages, as `getCell` reads it (src/echo.zig). +pub const PageCell = if (enabled) ghostty_vt.PageList.Cell else void; /// Kitty graphics are drawn where pixel geometry is (pdf_enabled, as for /// image panes' native placements). pub const images_enabled = enabled and pardes.pdf_enabled; diff --git a/test/replicas.zig b/test/replicas.zig index 9b5c3c3b..baaa669d 100644 --- a/test/replicas.zig +++ b/test/replicas.zig @@ -13,6 +13,11 @@ //! asking for the rows it scrolls back into and does not hold. //! //! zig build replicas -Dplatform=tty -- [--seeds N] [--steps M] [--clients C] [--rtt 30,80,207] [--first K] [--flood MB] [--shared] +//! zig build replicas -Dplatform=tty -- --echo [--seeds N] [--rtt 30,80,207] [--first K] +//! +//! `--echo` types a corpus into a shell instead, with and without the +//! predicted echo (src/echo.zig), and gates what it draws: below, at +//! "predicted echo". //! //! Gates, per seed and RTT: no replica diverges (every confirmed unit's //! digests agree as it is replayed), and at quiescence every replica's @@ -29,6 +34,7 @@ const libc = std.c; const pardes = @import("pardes"); const replica = pardes.replica; const screen_state = pardes.screen_state; +const echo = pardes.echo; const Key = pardes.Key; const gpa = std.heap.c_allocator; @@ -58,7 +64,7 @@ const Host = struct { } }; -const Kind = enum { input, to_authority, to_replica, echo, frame, output, screen_down, screen_up }; +const Kind = enum { input, to_authority, to_replica, echo, frame, output, screen_down, screen_up, client_frame }; const Msg = struct { at: u64, @@ -66,6 +72,8 @@ const Msg = struct { kind: Kind, who: u16, bytes: []u8, + /// An echo's: the client's input that made it (--echo). + seq: u32 = 0, fn order(_: void, a: Msg, b: Msg) std.math.Order { if (a.at != b.at) return std.math.order(a.at, b.at); @@ -288,14 +296,6 @@ fn run(r: Run, dir: []const u8, totals: *Totals) !void { var down: [8]Link = @splat(.{}); var left: [8]usize = @splat(r.steps); var typing: usize = r.clients; - const push = struct { - fn f(q: *std.PriorityQueue(Msg, void, Msg.order), nn: *u64, m: Msg) !void { - var mm = m; - mm.n = nn.*; - nn.* += 1; - try q.push(gpa, mm); - } - }.f; // Each client's first input, soon; the programs; the first frame. for (0..r.clients) |c| try push(&queue, &n, .{ .at = random.uintLessThan(u64, 200_000), .n = 0, .kind = .input, .who = @intCast(c + 1), .bytes = &.{} }); try push(&queue, &n, .{ .at = tui_at, .n = 0, .kind = .output, .who = 0, .bytes = &.{} }); @@ -309,26 +309,6 @@ fn run(r: Run, dir: []const u8, totals: *Totals) !void { // authority steps an input, and once a frame. var owed: std.ArrayListUnmanaged([]u8) = .empty; defer owed.deinit(gpa); - const flushAll = struct { - fn f(sc: *replica.Screens, core: *pardes.Pardes, ps: []const u16, list: *std.ArrayListUnmanaged([]u8), q: *std.PriorityQueue(Msg, void, Msg.order), nn: *u64, now: u64, dl: []Link, rnd: std.Random, ow: u64, jt: u64, vs: *std.AutoHashMapUnmanaged(u64, u64), tot: *Totals) !void { - for (ps, 0..) |peer, c| { - list.clearRetainingCapacity(); - try sc.flush(core, peer, list); - for (list.items) |bytes| { - tot.deltas += 1; - tot.delta_bytes += bytes.len; - try push(q, nn, .{ .at = dl[c].arrive(rnd, now, ow, jt), .n = 0, .kind = .screen_down, .who = @intCast(c + 1), .bytes = bytes }); - } - } - // Each version as the authority's screen showed it. - for (&sc.tracks, 0..) |*slot, id| if (slot.*) |*tr| { - const key = @as(u64, tr.serial) << 32 | tr.track.version; - if (tr.track.version == 0 or vs.contains(key)) continue; - _ = core.panes[id] orelse continue; - try vs.put(gpa, key, screenHash(&tr.state.vt)); - }; - } - }.f; while (queue.pop()) |m| { defer if (m.bytes.len > 0) gpa.free(m.bytes); @@ -432,6 +412,7 @@ fn run(r: Run, dir: []const u8, totals: *Totals) !void { } if (rp.screenRequest()) |req| try push(&queue, &n, .{ .at = up[c].arrive(random, m.at, one_way, jitter), .n = 0, .kind = .screen_up, .who = m.who, .bytes = try gpa.dupe(u8, req) }); }, + .client_frame => {}, .screen_up => { const c = m.who - 1; if (m.bytes[5] == 2) totals.fetched += std.mem.readInt(u64, m.bytes[14..22], .little) - std.mem.readInt(u64, m.bytes[6..14], .little); @@ -467,6 +448,35 @@ fn run(r: Run, dir: []const u8, totals: *Totals) !void { } } +fn push(q: *std.PriorityQueue(Msg, void, Msg.order), nn: *u64, m: Msg) !void { + var mm = m; + mm.n = nn.*; + nn.* += 1; + try q.push(gpa, mm); +} + +/// What each peer is owed of the terminals, onto its link (before the +/// authority steps an input, and once a frame); each version as the +/// authority's screen showed it. +fn flushAll(sc: *replica.Screens, core: *pardes.Pardes, ps: []const u16, list: *std.ArrayListUnmanaged([]u8), q: *std.PriorityQueue(Msg, void, Msg.order), nn: *u64, now: u64, dl: []Link, rnd: std.Random, ow: u64, jt: u64, vs: *std.AutoHashMapUnmanaged(u64, u64), tot: *Totals) !void { + for (ps, 0..) |peer, c| { + list.clearRetainingCapacity(); + try sc.flush(core, peer, list); + for (list.items) |bytes| { + tot.deltas += 1; + tot.delta_bytes += bytes.len; + try push(q, nn, .{ .at = dl[c].arrive(rnd, now, ow, jt), .n = 0, .kind = .screen_down, .who = @intCast(c + 1), .bytes = bytes }); + } + } + // Each version as the authority's screen showed it. + for (&sc.tracks, 0..) |*slot, id| if (slot.*) |*tr| { + const key = @as(u64, tr.serial) << 32 | tr.track.version; + if (tr.track.version == 0 or vs.contains(key)) continue; + _ = core.panes[id] orelse continue; + try vs.put(gpa, key, screenHash(&tr.state.vt)); + }; +} + /// A unit of the authority's to every replica, down its link: first the /// rows of its terminals its step read that a replica holds as holes. fn sendUnit(q: *std.PriorityQueue(Msg, void, Msg.order), n: *u64, unit: []const u8, now: u64, down: []Link, random: std.Random, one_way: u64, jitter: u64, totals: *Totals, flooding: bool, screens: *replica.Screens, core: *pardes.Pardes, peers: []const u16) !void { @@ -490,6 +500,695 @@ fn sendUnit(q: *std.PriorityQueue(Msg, void, Msg.order), n: *u64, unit: []const } } +// ---- predicted echo (--echo): one client typing a corpus into a shell ---- +// +// The authority's pty is a shell played here (`FakeShell`): a line editor at +// a prompt with pardes's OSC 133 marks and Tab completion, and the programs +// the corpus runs in it: a password prompt (no echo), vim (the alternate +// screen), a program that ignores what is typed, and `cat` (the kernel's +// plain echo, no marks). Each key's echo comes back 2 ms after the authority +// writes it, as a shell's. The client types at a typist's pace and in bursts; +// each of its frames (60 Hz, a phase of its own) is what its screen shows: +// the predicted echo where it is drawn (src/echo.zig), the screen state +// under it. Per key the shell echoed at a prompt or in cat: the time from +// the key to its character on that screen, with prediction and without. Per +// key predicted: whether the authority's screen, right after its echo, +// shows what was predicted where it was. Per key drawn wrong: its frames. + +const ascii: [128][1]u8 = blk: { + var t: [128][1]u8 = undefined; + for (&t, 0..) |*c, i| c.* = .{@intCast(i)}; + break :blk t; +}; + +const Step = struct { gap: u64, key: Key, section: u8 }; + +/// The corpus's parts, each a case of the gates. +const sections = [_][]const u8{ "typing", "a burst", "backspace", "left, right", "tab", "password", "vim", "ignores input", "cat", "typed ahead" }; + +const Corpus = struct { + steps: std.ArrayListUnmanaged(Step) = .empty, + random: std.Random, + extra: u64 = 0, + section: u8 = 0, + + fn add(c: *Corpus, gap: u64, key: Key) !void { + try c.steps.append(gpa, .{ .gap = gap + c.extra, .key = key, .section = c.section }); + c.extra = 0; + } + + fn typing(c: *Corpus, text: []const u8, fast: bool) !void { + for (text) |ch| try c.add(if (fast) c.random.intRangeAtMost(u64, 12_000, 60_000) else c.random.intRangeAtMost(u64, 60_000, 180_000), .{ .cp = ch, .text = &ascii[ch] }); + } + + fn press(c: *Corpus, key: Key) !void { + try c.add(c.random.intRangeAtMost(u64, 80_000, 250_000), key); + } + + fn pause(c: *Corpus) void { + c.extra += c.random.intRangeAtMost(u64, 500_000, 1_200_000); + } + + fn build(c: *Corpus) !void { + const enter: Key = .{ .cp = Key.enter }; + const bs: Key = .{ .cp = Key.backspace }; + c.pause(); + // At a prompt, at a typist's pace, then in a burst. + c.section = 0; + try c.typing("echo hello world", false); + try c.press(enter); + c.pause(); + c.section = 1; + try c.typing("echo the quick brown fox jumps over", true); + try c.press(enter); + c.pause(); + // Backspace. + c.section = 2; + try c.typing("ecoh", false); + try c.press(bs); + try c.press(bs); + try c.typing("ho fixed tpyo", false); + for (0..3) |_| try c.press(bs); + try c.typing("ypo", false); + try c.press(enter); + c.pause(); + // Left and right over what was typed. + c.section = 3; + try c.typing("echo abc", false); + for ([_]u21{ Key.left, Key.left, Key.right, Key.right }) |k| try c.press(.{ .cp = k }); + try c.typing("def", false); + try c.press(enter); + c.pause(); + // Tab completion, typed straight on. + c.section = 4; + try c.typing("ec", false); + try c.press(.{ .cp = Key.tab }); + try c.typing("tab completed", false); + try c.press(enter); + c.pause(); + // A password prompt: nothing typed there may show. + c.section = 5; + try c.typing("sudo ls", false); + try c.press(enter); + c.pause(); + try c.typing("hunter2", false); + try c.press(enter); + c.pause(); + // vim: the alternate screen, never predicted. + c.section = 6; + try c.typing("vim", false); + try c.press(enter); + c.pause(); + try c.typing("ihello from vim", false); + try c.press(.{ .cp = Key.escape, .shift = true }); + try c.typing(":q", false); + try c.press(enter); + c.pause(); + // A program that ignores what is typed. + c.section = 7; + try c.typing("sink", false); + try c.press(enter); + c.pause(); + try c.typing("this goes nowhere", false); + try c.press(.{ .cp = 'c', .ctrl = true }); + c.pause(); + // cat: the kernel's plain echo. + c.section = 8; + try c.typing("cat", false); + try c.press(enter); + c.pause(); + try c.typing("plain echo", false); + try c.press(enter); + try c.typing("a second line", false); + try c.press(enter); + try c.press(.{ .cp = 'd', .ctrl = true }); + c.pause(); + // Typed on before the next prompt. + c.section = 9; + try c.typing("echo one", false); + try c.press(enter); + try c.typing("echo two", false); + try c.press(enter); + c.pause(); + } +}; + +const prompt_bytes = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07"; + +/// The shell on the authority's pty, a key a write (as forwardKey writes). +const FakeShell = struct { + mode: Mode = .prompt, + line: [256]u8 = undefined, + len: usize = 0, + cur: usize = 0, + vim_cmd: [8]u8 = undefined, + vim_n: usize = 0, + out: std.ArrayListUnmanaged(u8) = .empty, + + const Mode = enum { prompt, password, vim, vim_insert, sink, cat }; + + fn put(sh: *FakeShell, bytes: []const u8) void { + sh.out.appendSlice(gpa, bytes) catch {}; + } + + fn print(sh: *FakeShell, comptime fmt: []const u8, args: anytype) void { + sh.out.print(gpa, fmt, args) catch {}; + } + + fn done(sh: *FakeShell, status: u8) void { + sh.print("\x1b]133;D;{d};aid=pardes\x07" ++ prompt_bytes, .{status}); + sh.mode = .prompt; + sh.len = 0; + sh.cur = 0; + } + + fn insert(sh: *FakeShell, c: u8) void { + if (sh.len + 1 >= sh.line.len) return; + sh.put(&.{c}); + if (sh.cur < sh.len) { + sh.put(sh.line[sh.cur..sh.len]); + sh.print("\x1b[{d}D", .{sh.len - sh.cur}); + } + std.mem.copyBackwards(u8, sh.line[sh.cur + 1 .. sh.len + 1], sh.line[sh.cur..sh.len]); + sh.line[sh.cur] = c; + sh.len += 1; + sh.cur += 1; + } + + fn key(sh: *FakeShell, b: []const u8) void { + const printable = b.len == 1 and b[0] >= 0x20 and b[0] < 0x7f; + const eql = std.mem.eql; + switch (sh.mode) { + .prompt => if (printable) sh.insert(b[0]) else if (eql(u8, b, "\x7f")) { + if (sh.cur == 0) return; + if (sh.cur == sh.len) sh.put("\x08\x1b[K") else { + sh.put("\x08"); + sh.put(sh.line[sh.cur..sh.len]); + sh.print(" \x1b[{d}D", .{sh.len - sh.cur + 1}); + } + std.mem.copyForwards(u8, sh.line[sh.cur - 1 .. sh.len - 1], sh.line[sh.cur..sh.len]); + sh.len -= 1; + sh.cur -= 1; + } else if (eql(u8, b, "\x1b[D")) { + if (sh.cur == 0) return; + sh.put(b); + sh.cur -= 1; + } else if (eql(u8, b, "\x1b[C")) { + if (sh.cur == sh.len) return; + sh.put(b); + sh.cur += 1; + } else if (eql(u8, b, "\t")) { + if (sh.cur != sh.len or std.mem.indexOfScalar(u8, sh.line[0..sh.len], ' ') != null) return; + for ([_][]const u8{ "echo", "sudo", "sink", "cat", "vim" }) |cmd| { + if (sh.len == 0 or sh.len >= cmd.len or !std.mem.startsWith(u8, cmd, sh.line[0..sh.len])) continue; + for (cmd[sh.len..]) |ch| sh.insert(ch); + sh.insert(' '); + return; + } + } else if (eql(u8, b, "\r")) { + var cmd_buf: [256]u8 = undefined; + const cmd = cmd_buf[0..sh.len]; + @memcpy(cmd, sh.line[0..sh.len]); + sh.len = 0; + sh.cur = 0; + sh.put("\r\n\x1b]133;C;aid=pardes\x07"); + if (cmd.len == 0) return sh.done(0); + if (std.mem.startsWith(u8, cmd, "echo")) { + sh.print("{s}\r\n", .{std.mem.trimStart(u8, cmd[4..], " ")}); + sh.done(0); + } else if (std.mem.startsWith(u8, cmd, "sudo")) { + sh.put("[sudo] password for you: "); + sh.mode = .password; + } else if (eql(u8, cmd, "vim")) { + sh.put("\x1b[?1049h\x1b[2J\x1b[H~\r\n~\r\n~\x1b[H"); + sh.mode = .vim; + } else if (eql(u8, cmd, "sink")) { + sh.mode = .sink; + } else if (eql(u8, cmd, "cat")) { + sh.mode = .cat; + } else { + sh.print("fake: unknown command: {s}\r\n", .{cmd}); + sh.done(127); + } + } else if (eql(u8, b, "\x03")) { + sh.put("^C\r\n"); + sh.done(130); + }, + .password => if (eql(u8, b, "\r")) { + sh.put("\r\nsudo: 1 incorrect password attempt\r\n"); + sh.done(1); + } else if (eql(u8, b, "\x03")) { + sh.put("^C\r\n"); + sh.done(130); + }, + .vim => if (sh.vim_n > 0) { + if (eql(u8, b, "\r")) { + const quit = eql(u8, sh.vim_cmd[0..sh.vim_n], ":q"); + sh.vim_n = 0; + if (quit) { + sh.put("\x1b[?1049l"); + sh.done(0); + } + } else if (printable and sh.vim_n < sh.vim_cmd.len) { + sh.vim_cmd[sh.vim_n] = b[0]; + sh.vim_n += 1; + } + } else if (eql(u8, b, "i")) { + sh.mode = .vim_insert; + } else if (eql(u8, b, ":")) { + sh.vim_cmd[0] = ':'; + sh.vim_n = 1; + }, + .vim_insert => if (eql(u8, b, "\x1b")) { + sh.mode = .vim; + } else if (printable) sh.put(b), + .sink => if (eql(u8, b, "\x03")) { + sh.put("^C\r\n"); + sh.done(130); + }, + .cat => if (printable) { + sh.put(b); + if (sh.len < sh.line.len) { + sh.line[sh.len] = b[0]; + sh.len += 1; + } + } else if (eql(u8, b, "\x7f")) { + if (sh.len == 0) return; + sh.put("\x08 \x08"); + sh.len -= 1; + } else if (eql(u8, b, "\r")) { + sh.print("\r\n{s}\r\n", .{sh.line[0..sh.len]}); + sh.len = 0; + } else if (eql(u8, b, "\x04") and sh.len == 0) { + sh.done(0); + } else if (eql(u8, b, "\x03")) { + sh.put("^C\r\n"); + sh.done(130); + }, + } + } +}; + +/// The authority's host: each pty write tagged with the client input that +/// made it. +const EchoHost = struct { + writes: std.ArrayListUnmanaged(struct { seq: u32, bytes: []u8 }) = .empty, + current: u32 = 0, + + fn watched(_: ?*anyopaque) bool { + return false; + } + + fn ptyWrite(ctx: ?*anyopaque, _: u8, bytes: []const u8) void { + const h: *EchoHost = @ptrCast(@alignCast(ctx.?)); + h.writes.append(gpa, .{ .seq = h.current, .bytes = gpa.dupe(u8, bytes) catch return }) catch {}; + } + + fn ignore(_: ?*anyopaque, _: []const u8) void {} + + const vtable: pardes.Host.VTable = .{ .watched = watched, .pty_write = ptyWrite, .set_clipboard = ignore }; + + fn host(h: *EchoHost) pardes.Host { + return .{ .ctx = h, .vtable = &vtable }; + } +}; + +const Typed = struct { + seq: u32, + section: u8, + at: u64, + printable: bool, + /// What was predicted: the cell (its row in the active area) and the + /// cursor after it. + pred: ?struct { x: u16, y: u16, cp: u21 } = null, + pcur: ?struct { x: u16, y: u16 } = null, + /// The shell's mode as it took the key; whether it printed anything for + /// it; the authority's version of the screen before that. + mode: ?FakeShell.Mode = null, + echoed: bool = false, + version_before: u32 = 0, + /// The authority's screen right after its echo: its cursor and the + /// cursor's row; whether that is what was predicted. + after: ?struct { x: u16, y: u16, row: [128]u21 } = null, + truth: ?bool = null, + /// Its prediction drawn: first, and in how many frames. + first_visible: ?u64 = null, + visible_frames: u32 = 0, + /// The screen state showing its echo, first. + seen: ?u64 = null, +}; + +const EchoTotals = struct { + keys: u64 = 0, + echo_keys: u64 = 0, + latency: std.ArrayListUnmanaged(u64) = .empty, + predicted: u64 = 0, + unpredicted: u64 = 0, + confirmed: u64 = 0, + mispredicted: u64 = 0, + cursor_missed: u64 = 0, + right: u64 = 0, + wrong: u64 = 0, + wrong_shown: u64 = 0, + wrong_frames: u64 = 0, + wrong_max: u64 = 0, + secret_frames: u64 = 0, + alt_predicted: u64 = 0, + divergences: u64 = 0, + unequal: u64 = 0, + screens_wrong: u64 = 0, + /// The replica's frame drawn while it shows a prediction (its core's + /// render, the tty's and the GUI's), and the overlay's part of it (ns); + /// cells drawn otherwise than predicted. + render_ns: std.ArrayListUnmanaged(u64) = .empty, + paint_ns: std.ArrayListUnmanaged(u64) = .empty, + drawn_frames: u64 = 0, + paint_wrong: u64 = 0, + sec: [sections.len]Section = @splat(.{}), + + const Section = struct { keys: u64 = 0, predicted: u64 = 0, wrong_frames: u64 = 0, latency: std.ArrayListUnmanaged(u64) = .empty }; + + fn deinit(et: *EchoTotals) void { + for (&et.sec) |*x| x.latency.deinit(gpa); + et.latency.deinit(gpa); + et.render_ns.deinit(gpa); + et.paint_ns.deinit(gpa); + } +}; + +fn cellCp(state: *pardes.panes.terminal.State, x: u16, y: u16) u21 { + const ci = state.vt.screens.active.pages.getCell(.{ .active = .{ .x = x, .y = y } }) orelse return 0x10ffff; + const cp = ci.cell.codepoint(); + return if (cp == 0) ' ' else cp; +} + +/// A key's prediction against the authority's screen right after its echo, +/// once both are known (a key typed behind one not predicted is predicted +/// after the authority has it). +fn judgeTruth(k: *Typed) void { + if (k.truth != null) return; + const pc = k.pcur orelse return; + const after = k.after orelse return; + var ok = after.x == pc.x and after.y == pc.y; + if (k.pred) |cell| ok = ok and cell.y == after.y and cell.x < after.row.len and after.row[cell.x] == cell.cp; + k.truth = ok; + if (trace and !ok) std.debug.print("key {d} in {t} predicted cursor {d},{d} cell {any}; the authority's cursor {d},{d}\n", .{ k.seq, k.mode.?, pc.x, pc.y, k.pred, after.x, after.y }); +} + +fn runEcho(seed: u64, rtt_ms: u64, on: bool, et: *EchoTotals) !void { + var prng = std.Random.DefaultPrng.init(seed *% 0x9e3779b97f4a7c15 +% rtt_ms); + const random = prng.random(); + var host: EchoHost = .{}; + defer { + for (host.writes.items) |w| gpa.free(w.bytes); + host.writes.deinit(gpa); + } + var sh: FakeShell = .{}; + defer sh.out.deinit(gpa); + const a = try replica.Authority.init(gpa, .{ .cols = 100, .rows = 30, .boot = .tty_shell }, host.host(), true); + defer a.deinit(); + var screens: replica.Screens = .init(gpa); + defer screens.deinit(a.core); + const peer = try screens.addPeer(); + const peers = [_]u16{peer}; + const rp = try replica.Replica.init(gpa, 1, try a.unit(0, 0), true); + defer rp.deinit(); + rp.echo.on = on; + const shell: u8 = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + var corpus: Corpus = .{ .random = random }; + defer corpus.steps.deinit(gpa); + try corpus.build(); + var typed: std.ArrayListUnmanaged(Typed) = .empty; + defer typed.deinit(gpa); + var t: Totals = .{}; + defer { + t.spec_ns.deinit(gpa); + t.copy_ns.deinit(gpa); + t.rollback_ns.deinit(gpa); + t.apply_ns.deinit(gpa); + t.frame_ns.deinit(gpa); + } + var versions: std.AutoHashMapUnmanaged(u64, u64) = .empty; + defer versions.deinit(gpa); + var queue: std.PriorityQueue(Msg, void, Msg.order) = .empty; + defer { + while (queue.pop()) |m| if (m.bytes.len > 0) gpa.free(m.bytes); + queue.deinit(gpa); + } + var n: u64 = 0; + const one_way = rtt_ms * 1000 / 2; + const jitter = one_way / 10; + var up: [1]Link = @splat(.{}); + var down: [1]Link = @splat(.{}); + var owed: std.ArrayListUnmanaged([]u8) = .empty; + defer owed.deinit(gpa); + + // The shell's first prompt. + sh.put(prompt_bytes); + a.output(.{ .output = .{ .pane = shell, .bytes = sh.out.items } }); + sh.out.clearRetainingCapacity(); + var next_step: usize = 0; + try push(&queue, &n, .{ .at = corpus.steps.items[0].gap, .n = 0, .kind = .input, .who = 1, .bytes = &.{} }); + try push(&queue, &n, .{ .at = frame_us, .n = 0, .kind = .frame, .who = 0, .bytes = &.{} }); + try push(&queue, &n, .{ .at = random.uintLessThan(u64, frame_us), .n = 0, .kind = .client_frame, .who = 1, .bytes = &.{} }); + // Two clocks tick while anything else moves, and a second after. + var periodic: usize = 2; + var left: [2]u32 = .{ 60, 60 }; + + var frame_arena: std.heap.ArenaAllocator = .init(gpa); + defer frame_arena.deinit(); + var made: std.ArrayListUnmanaged(echo.Echo.Predicted) = .empty; + defer made.deinit(gpa); + rp.echo.log = &made; + while (queue.pop()) |m| { + defer if (m.bytes.len > 0) gpa.free(m.bytes); + rp.echo.now = m.at * 1000; + // What it predicted in the last event's calls (a key typed, or the + // keys behind one it did not, at a unit, a delta or a frame). + defer { + const top = rp.echo.tops[shell]; + for (made.items) |pr| { + const k = &typed.items[pr.seq - 1]; + if (trace) { + const sc = rp.core().panes[shell].?.terminal.?.vt.screens.active.cursor; + std.debug.print("{d} ms ({t}): key {d} predicted {any} cursor {d}; acked {d} wait {d} nd {d}; the screen's cursor {d},{d}\n", .{ m.at / 1000, m.kind, pr.seq, pr.cell, pr.cursor.x, rp.echo.acked, rp.echo.wait, rp.echo.nd, sc.x, sc.y }); + } + if (pr.cell) |cell| k.pred = .{ .x = cell.x, .y = @intCast(cell.row - top), .cp = cell.cp }; + k.pcur = .{ .x = pr.cursor.x, .y = @intCast(pr.cursor.row - top) }; + judgeTruth(k); + } + made.clearRetainingCapacity(); + } + switch (m.kind) { + .input => { + const step = corpus.steps.items[next_step]; + next_step += 1; + const msg = try gpa.dupe(u8, try rp.input(.{ .key = step.key })); + const seq = rp.next_seq - 1; + try typed.append(gpa, .{ .seq = seq, .section = step.section, .at = m.at, .printable = step.key.text.len == 1 }); + if (trace) std.debug.print("{d} ms: key {d} typed: '{s}' cp {x}\n", .{ m.at / 1000, seq, step.key.text, step.key.cp }); + try push(&queue, &n, .{ .at = up[0].arrive(random, m.at, one_way, jitter), .n = 0, .kind = .to_authority, .who = 1, .bytes = msg }); + if (next_step < corpus.steps.items.len) try push(&queue, &n, .{ .at = m.at + corpus.steps.items[next_step].gap, .n = 0, .kind = .input, .who = 1, .bytes = &.{} }); + }, + .to_authority => { + if (try a.pending()) |batch| try sendUnit(&queue, &n, batch, m.at, &down, random, one_way, jitter, &t, false, &screens, a.core, &peers); + try flushAll(&screens, a.core, &peers, &owed, &queue, &n, m.at, &down, random, one_way, jitter, &versions, &t); + host.current = (replica.parseInput(m.bytes) orelse return error.BadInput).seq; + const unit = try a.take(m.bytes); + host.current = 0; + try sendUnit(&queue, &n, unit, m.at, &down, random, one_way, jitter, &t, false, &screens, a.core, &peers); + for (host.writes.items) |w| try push(&queue, &n, .{ .at = m.at + 2_000, .n = 0, .kind = .echo, .who = shell, .bytes = w.bytes, .seq = w.seq }); + host.writes.clearRetainingCapacity(); + }, + .echo => { + const ty: ?*Typed = if (m.seq > 0) &typed.items[m.seq - 1] else null; + const state = a.core.panes[shell].?.terminal.?; + if (ty) |k| { + k.mode = sh.mode; + k.version_before = if (screens.tracks[shell]) |*tr| tr.track.version else 0; + } + sh.key(m.bytes); + if (sh.out.items.len > 0) { + a.output(.{ .output = .{ .pane = shell, .bytes = sh.out.items } }); + sh.out.clearRetainingCapacity(); + if (ty) |k| k.echoed = true; + } + if (ty) |k| { + const cur = state.vt.screens.active.cursor; + k.after = .{ .x = @intCast(cur.x), .y = @intCast(cur.y), .row = undefined }; + for (&k.after.?.row, 0..) |*cp, x| cp.* = cellCp(state, @intCast(x), @intCast(cur.y)); + judgeTruth(k); + } + }, + .frame => { + if (try a.pending()) |batch| try sendUnit(&queue, &n, batch, m.at, &down, random, one_way, jitter, &t, false, &screens, a.core, &peers); + try flushAll(&screens, a.core, &peers, &owed, &queue, &n, m.at, &down, random, one_way, jitter, &versions, &t); + const busy = next_step < corpus.steps.items.len or queue.count() + 1 > periodic; + left[0] = if (busy) 60 else left[0] -| 1; + if (left[0] > 0) try push(&queue, &n, .{ .at = m.at + frame_us, .n = 0, .kind = .frame, .who = 0, .bytes = &.{} }) else periodic -= 1; + }, + .client_frame => { + _ = rp.echoFrame(); + const core = rp.core(); + const drawn = rp.echo.visible(core) and core.panes[shell].?.body.mode == .tty; + if (drawn and rp.echo.n > 0) { + // Drawn as the client draws it: each cell where it was + // predicted. + _ = frame_arena.reset(.retain_capacity); + core.needs_frame = true; + const t0 = pardes.FrameStats.nowNs(); + const surf = try core.render(frame_arena.allocator()); + try et.render_ns.append(gpa, pardes.FrameStats.nowNs() - t0); + const body = surf.region(.body, shell).?.rect; + const top = rp.echo.tops[shell]; + for (rp.echo.cells[0..rp.echo.n]) |cell| { + if (cell.row < top or surf.at(body.x + cell.x, body.y + @as(u16, @intCast(cell.row - top))).text[0] != cell.cp) et.paint_wrong += 1; + } + const t1 = pardes.FrameStats.nowNs(); + rp.echo.paint(core, surf, core.panes[shell].?, shell, body.x, body.w, body.y, body.h, true); + try et.paint_ns.append(gpa, pardes.FrameStats.nowNs() - t1); + et.drawn_frames += 1; + } + if (drawn) for (rp.echo.cells[0..rp.echo.n]) |cell| { + const k = &typed.items[cell.seq - 1]; + k.visible_frames += 1; + if (k.first_visible == null) { + k.first_visible = m.at; + if (trace) { + const st = core.panes[shell].?.terminal.?; + std.debug.print("{d} ms: key {d} ('{u}') drawn: trusted {} doubt {} phase {t} sem {t}\n", .{ m.at / 1000, cell.seq, cell.cp, rp.echo.trusted, rp.echo.doubt, st.stream.handler.phase, st.vt.screens.active.cursor.semantic_content }); + } + } + }; + const applied = if (rp.screens[shell]) |sc| sc.rep.seq else 0; + for (typed.items) |*k| if (k.echoed and k.seen == null and applied > k.version_before) { + k.seen = m.at; + }; + const busy = next_step < corpus.steps.items.len or queue.count() + 1 > periodic; + left[1] = if (busy) 60 else left[1] -| 1; + if (left[1] > 0) try push(&queue, &n, .{ .at = m.at + frame_us, .n = 0, .kind = .client_frame, .who = 1, .bytes = &.{} }) else periodic -= 1; + }, + .to_replica => { + rp.confirm(m.bytes); + rp.settle(); + }, + .screen_down => { + if (rp.screen(m.bytes)) |reply| { + const pane = m.bytes[0]; + if (reply[5] == 1) { + const sc = rp.screens[pane].?; + if (versions.get(@as(u64, sc.serial) << 32 | sc.rep.seq)) |want| { + if (want != screenHash(&rp.core().panes[pane].?.terminal.?.vt)) t.screens_wrong += 1; + } else t.screens_wrong += 1; + } + try push(&queue, &n, .{ .at = up[0].arrive(random, m.at, one_way, jitter), .n = 0, .kind = .screen_up, .who = 1, .bytes = try gpa.dupe(u8, reply) }); + } + if (rp.screenRequest()) |req| try push(&queue, &n, .{ .at = up[0].arrive(random, m.at, one_way, jitter), .n = 0, .kind = .screen_up, .who = 1, .bytes = try gpa.dupe(u8, req) }); + }, + .screen_up => { + if (try screens.take(a.core, peer, m.bytes)) |answer| try push(&queue, &n, .{ .at = down[0].arrive(random, m.at, one_way, jitter), .n = 0, .kind = .screen_down, .who = 1, .bytes = answer }); + }, + .output => {}, + } + } + const s = rp.echo.stats; + et.predicted += s.predicted; + et.unpredicted += s.unpredicted; + et.confirmed += s.confirmed; + et.mispredicted += s.mispredicted; + et.cursor_missed += s.cursor_missed; + et.screens_wrong += t.screens_wrong; + for (typed.items) |k| { + et.keys += 1; + const sec = &et.sec[k.section]; + sec.keys += 1; + if (k.pcur != null) sec.predicted += 1; + if (k.visible_frames > 0 and k.truth != true) sec.wrong_frames += k.visible_frames; + if (k.truth) |ok| { + if (ok) et.right += 1 else et.wrong += 1; + } + const md = k.mode orelse .prompt; + if (md == .password or md == .sink) et.secret_frames += k.visible_frames; + if ((md == .vim or md == .vim_insert) and k.pcur != null) et.alt_predicted += 1; + if (k.visible_frames > 0 and k.truth != true) { + if (trace) std.debug.print("seed {d}: key {d} at {d} ms in {t} drawn {d} frames, truth {?}, predicted {any} cursor {any}, the authority after it {any}\n", .{ seed, k.seq, k.at / 1000, md, k.visible_frames, k.truth, k.pred, k.pcur, if (k.after) |af| .{ af.x, af.y } else null }); + et.wrong_shown += 1; + et.wrong_frames += k.visible_frames; + et.wrong_max = @max(et.wrong_max, k.visible_frames); + } + if (k.printable and k.echoed and (md == .prompt or md == .cat)) { + const seen = k.seen orelse std.math.maxInt(u64); + const drawn = if (k.first_visible != null and k.truth == true) @min(k.first_visible.?, seen) else seen; + if (drawn == std.math.maxInt(u64)) { + std.debug.print("seed {d} rtt {d} ms: a key's echo was never drawn\n", .{ seed, rtt_ms }); + et.unequal += 1; + continue; + } + et.echo_keys += 1; + try et.latency.append(gpa, drawn - k.at); + try sec.latency.append(gpa, drawn - k.at); + if (trace and drawn - k.at > 2 * frame_us) std.debug.print("key {d} at {d} ms in {t}: drawn after {d} ms, {s}\n", .{ k.seq, k.at / 1000, md, (drawn - k.at) / 1000, if (k.pcur == null) "not predicted" else if (k.first_visible == null) "predicted, not drawn" else if (k.truth != true) "predicted wrong" else "predicted, drawn late" }); + } + } + if (rp.diverged) |why| { + et.divergences += 1; + std.debug.print("seed {d} rtt {d} ms: the replica diverged: {s}\n", .{ seed, rtt_ms, why }); + } else if (!rp.quiet() or rp.digest() != replica.digestOf(a)) { + et.unequal += 1; + std.debug.print("seed {d} rtt {d} ms: the replica at quiescence: {s}\n", .{ seed, rtt_ms, if (rp.quiet()) "another digest" else "inputs still pending" }); + } + if (screenHash(&a.core.panes[shell].?.terminal.?.vt) != screenHash(&rp.core().panes[shell].?.terminal.?.vt)) { + et.unequal += 1; + std.debug.print("seed {d} rtt {d} ms: the shell at quiescence is not the authority's\n", .{ seed, rtt_ms }); + } +} + +fn echoMain(seeds: u64, first: u64, rtts: []const u64) !bool { + var failed = false; + var draw_lines: std.ArrayList(u8) = .empty; + defer draw_lines.deinit(gpa); + std.debug.print("predicted echo: one client typing the corpus into a shell, {d} seeds a cell; from a key to its character on the client's screen (ms, frames at 60 Hz)\n", .{seeds}); + std.debug.print(" rtt echo keys echoed p50 p90 max predicted unpredicted right/wrong confirmed mispredicted cursor-missed drawn-wrong keys/frames/max secret-frames vim-predicted diverged unequal screens-wrong\n", .{}); + var by_section: std.ArrayList(u8) = .empty; + defer by_section.deinit(gpa); + var off: [sections.len][2]u64 = undefined; + for (rtts) |rtt| for ([_]bool{ false, true }) |on| { + var et: EchoTotals = .{}; + defer et.deinit(); + for (first..first + seeds) |seed| try runEcho(seed, rtt, on, &et); + const ms = struct { + fn f(us: u64) f64 { + return @as(f64, @floatFromInt(us)) / 1000; + } + }.f; + std.debug.print("{d:>4} {s:>4} {d:>4} {d:>6} {d:>5.1} {d:>5.1} {d:>5.1} {d:>9} {d:>11} {d:>5}/{d:<5} {d:>9} {d:>12} {d:>13} {d:>5}/{d:>5}/{d:<5} {d:>13} {d:>13} {d:>8} {d:>7} {d:>13}\n", .{ + rtt, if (on) "on" else "off", et.keys, et.echo_keys, + ms(pct(et.latency.items, 50)), ms(pct(et.latency.items, 90)), ms(pct(et.latency.items, 100)), et.predicted, + et.unpredicted, et.right, et.wrong, et.confirmed, + et.mispredicted, et.cursor_missed, et.wrong_shown, et.wrong_frames, + et.wrong_max, et.secret_frames, et.alt_predicted, et.divergences, + et.unequal, et.screens_wrong, + }); + for (&et.sec, &off, sections) |*x, *o, name| { + const p50 = pct(x.latency.items, 50) / 1000; + const p90 = pct(x.latency.items, 90) / 1000; + if (!on) { + o.* = .{ p50, p90 }; + continue; + } + try by_section.print(gpa, "{d:>4} {s:<14} {d:>5} {d:>9} {d:>7} {d:>4} -> {d:<4} {d:>4} -> {d:<4} {d:>12}\n", .{ rtt, name, x.keys, x.predicted, x.latency.items.len, o[0], p50, o[1], p90, x.wrong_frames }); + } + if (on) try draw_lines.print(gpa, "{d:>4} {d:>12} {d:>6}/{d:<6} {d:>5}/{d:<5} {d:>10}\n", .{ rtt, et.drawn_frames, pct(et.render_ns.items, 50) / 1000, pct(et.render_ns.items, 99) / 1000, pct(et.paint_ns.items, 50), pct(et.paint_ns.items, 99), et.paint_wrong }); + if (et.divergences > 0 or et.unequal > 0 or et.screens_wrong > 0 or et.secret_frames > 0 or et.alt_predicted > 0 or et.paint_wrong > 0) failed = true; + }; + std.debug.print("by part of the corpus (all seeds): keys typed, predicted, echoed (the latency sample); key to its character on the screen, ms, without -> with; frames a wrong prediction was drawn\n rtt part keys predicted echoed p50 off/on p90 off/on drawn-wrong\n{s}", .{by_section.items}); + std.debug.print("the client's frames drawn with a prediction shown: its render (us) and the overlay's part (ns); cells drawn otherwise than predicted\n rtt frames-drawn render p50/p99 paint p50/p99 drawn-wrong\n{s}", .{draw_lines.items}); + return !failed; +} + + fn pct(samples: []u64, p: usize) u64 { if (samples.len == 0) return 0; std.mem.sort(u64, samples, {}, std.sort.asc(u64)); @@ -504,6 +1203,7 @@ pub fn main(init: std.process.Init) !void { var clients: u16 = 3; var rtts: []const u64 = &.{ 30, 80, 207 }; var shared = false; + var echo_mode = false; var flood_mb: u64 = 8; var rtt_buf: [8]u64 = undefined; var i: usize = 1; @@ -513,6 +1213,10 @@ pub fn main(init: std.process.Init) !void { shared = true; continue; } + if (std.mem.eql(u8, name, "--echo")) { + echo_mode = true; + continue; + } if (i + 1 >= args.len) return error.MissingValue; i += 1; const value = args[i]; @@ -530,6 +1234,10 @@ pub fn main(init: std.process.Init) !void { const dir = try std.fmt.bufPrint(&dir_buf, "{s}/replicas-{d}", .{ tmp, @as(u32, @intCast(libc.getpid())) }); try std.Io.Dir.createDirAbsolute(init.io, dir, .default_dir); defer std.Io.Dir.cwd().deleteTree(init.io, dir) catch {}; + if (echo_mode) { + if (!try echoMain(seeds, first, rtts)) std.process.exit(1); + return; + } var failed = false; std.debug.print("{d} seeds x {d} inputs from each of {d} clients, {s}, a TUI then {d} MB of yes in the shell; costs in us (frame {d})\n", .{ seeds, steps, clients, if (shared) "sharing one view" else "a view each", flood_mb, frame_us }); std.debug.print(" rtt inputs at-once stopped view units fast rollback(mis+foreign) rate rebase spec p50/p99 copy p50/p99 (KB) rollback p50/p99 diverged unequal\n", .{}); -- cgit v1.3 From 57aa8d2f9ae851fa9806abbd1fd04abf1fd2b57f Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 20:56:03 -0300 Subject: replicas' core for a socket: a client's input settles as a `step` (no clock, no wait, no frame) on both sides, every kind of input speculated, the session's clock guessed, views no frontend here draws left undrawn, and a drawn view's selections given back to its slot pardes.zig: `step`, the middle of a pump, is a client input's own top-level input on the authority and in the replica's speculation, so the two tapes read alike; a pump skips the live view when it is `undrawn` (a replica client's, drawn by its own process); `settle` is the pump's whole middle, the Upgrade's too. tape.zig: a `.step` input; `unattended` is a watched field; a speculation's clock reads answer a guess of the session's clocks (`Guess`); `resync` says the host and its writes again for a joiner; `adoptCore`; a short ret is shown in a trace. views.zig: `drawOne` draws a replica client's own view between steps; `drawParked` gives a drawn view's selections back to its slot, for drawOthers too. rollback.zig: std's lists by walk.isList (a safe build's have a third field). Co-Authored-By: Claude Opus 5.5 --- src/pardes.zig | 66 ++++++++++++++++++++++++++++++++------------------- src/rollback.zig | 3 ++- src/tape.zig | 55 ++++++++++++++++++++++++++++++++++++++++++- src/views.zig | 72 ++++++++++++++++++++++++++++++++++++++++++-------------- 4 files changed, 152 insertions(+), 44 deletions(-) (limited to 'src/rollback.zig') diff --git a/src/pardes.zig b/src/pardes.zig index 02983c84..cc55d695 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -4710,6 +4710,9 @@ pub const Pardes = struct { view: u8 = 0, views_open: u8 = 1, parked: [views.max]?*views.Parked = @splat(null), + /// The views no frontend here draws: a replica client's, which its own + /// process draws (src/replica.zig). A pump skips them. + undrawn: u8 = 0, /// per-update scratch (paneCursorLines, selection text); reset each update scratch: std.heap.ArenaAllocator, /// The arena each frame's Surface text is built in, reset by `pump`. Hosts @@ -4739,7 +4742,7 @@ pub const Pardes = struct { "effects", "effects_head", "effects_len", "in_q", "in_head", "in_len", // The views not live: written whenever one goes out, so the // live one's slot is stale (views.zig). - "parked", + "parked", "undrawn", // How this process was started, and the last dump it made for // its host to write: a session loaded elsewhere has its own. "opts", "dump_out", @@ -6056,13 +6059,6 @@ pub const Pardes = struct { } } - /// What a step leaves queued, its inputs and its effects, taken now: - /// the session at a step boundary (an Upgrade's dump, upgrade.zig). - pub fn settle(p: *Pardes) void { - while (p.nextQueued()) |ev| p.update(ev); - while (p.nextEffect()) |e| p.perform(e); - } - pub fn pump(p: *Pardes, host: Host) !void { p.host = host; const taped = tape.enter(p, .pump); @@ -6087,15 +6083,7 @@ pub const Pardes = struct { } p.frames.mark_start = FrameStats.nowNs(); if (v.now) |clock| p.advance(clock(h.ctx)); - // What a 9P open wrote in a row goes in once the writes pause. - ctlfs.pane.flushPausedBatch(p); - // ...and what a /pager open was written before its close. - ctlfs.ctl.fillClosedPagers(p); - // ...and the last line of an open closed with no newline after it. - ctlfs.runClosedLines(p); - while (p.nextQueued()) |ev| p.update(ev); - while (p.nextEffect()) |e| p.perform(e); - turn.bump(&turn.epoch); + p.settle(); // A quitting frame has already freed what it would draw. if (p.quit) return; if (v.poll_frame) |f| f(h.ctx); @@ -6111,13 +6099,17 @@ pub const Pardes = struct { if (v.watched) |f| if (!f(h.ctx)) return; // The other views first, each into its own window or terminal. if (views.several(p)) try views.drawOthers(p, h); - _ = p.frame_arena.reset(.{ .retain_with_limit = memory.limits.frame_kept }); - p.frames.mark_update = FrameStats.nowNs(); - p.frames.view = if (p.panes[p.active]) |pane| pane.scroll() else 0; - const surface = try p.render(p.frame_arena.allocator()); - p.frames.mark_render = FrameStats.nowNs(); - if (v.present) |f| f(h.ctx, surface); - Stream.askAhead(p); + // A replica client's view live (its input the last): its own + // process draws it. + if (p.undrawn & (@as(u8, 1) << @intCast(p.view)) == 0) { + _ = p.frame_arena.reset(.{ .retain_with_limit = memory.limits.frame_kept }); + p.frames.mark_update = FrameStats.nowNs(); + p.frames.view = if (p.panes[p.active]) |pane| pane.scroll() else 0; + const surface = try p.render(p.frame_arena.allocator()); + p.frames.mark_render = FrameStats.nowNs(); + if (v.present) |f| f(h.ctx, surface); + Stream.askAhead(p); + } // Settled before post_present: a shell ticks there, and what that // tick moves owes the next frame. const skipped = p.present_skipped; @@ -6129,6 +6121,32 @@ pub const Pardes = struct { p.frames.record(FrameStats.nowNs(), owed or p.continuous(), skipped); } + /// What a pump does between its wait and its frame: the queued inputs + /// and the effects. Also the session at a step boundary, taken now (an + /// Upgrade's dump, upgrade.zig). + pub fn settle(p: *Pardes) void { + // What a 9P open wrote in a row goes in once the writes pause. + ctlfs.pane.flushPausedBatch(p); + // ...and what a /pager open was written before its close. + ctlfs.ctl.fillClosedPagers(p); + // ...and the last line of an open closed with no newline after it. + ctlfs.runClosedLines(p); + while (p.nextQueued()) |ev| p.update(ev); + while (p.nextEffect()) |e| p.perform(e); + turn.bump(&turn.epoch); + } + + /// A replica client's input settled on the authority (src/replica.zig): + /// a pump that waits on nothing, reads no clock and draws nothing, so + /// the client's own core can apply the same at once and expect the + /// authority's tape of it to read the same. The session's own pump + /// draws it after. + pub fn step(p: *Pardes) void { + const taped = tape.enter(p, .step); + defer tape.leave(p, taped); + p.settle(); + } + /// One request of the control filesystem, from whichever thread has the /// turn (`pardes.turn`). Not an event: it neither sweeps nor resets /// anything the editor's own step may be in the middle of using, so it diff --git a/src/rollback.zig b/src/rollback.zig index 6496a69f..7216664c 100644 --- a/src/rollback.zig +++ b/src/rollback.zig @@ -305,7 +305,8 @@ pub const Copy = struct { // by a speculation: terminal.zig's guards). if (comptime @hasDecl(T, "digest") and @TypeOf(T.digest) == fn (*const T, *walk.Hasher) void) return; if (s.layout == .@"packed") return; - if (comptime @hasField(T, "items") and @hasField(T, "capacity") and s.field_names.len == 2) { + // std's lists (a safe build's have a third field, a lock). + if (comptime walk.isList(T)) { if (v.capacity == 0) return; _ = try c.allocation(@intFromPtr(v.items.ptr)); const E = @typeInfo(@FieldType(T, "items")).pointer.child; diff --git a/src/tape.zig b/src/tape.zig index b1080149..c2ddbb9d 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -36,6 +36,7 @@ pub const Input = union(enum) { update: pardes.Event, serve_fs: pardes.ctlfs.Req, pump, + step, perform: pardes.Effect, next_effect, advance: u64, @@ -93,9 +94,12 @@ pub const Site = enum(u16) { }; /// Fields a host writes straight into the core between its calls. -const watched = .{ "native_images", "native_image_patches", "present_skipped", "shell_continuous", "needs_frame", "quit", "windowed", "active" }; +const watched = .{ "native_images", "native_image_patches", "present_skipped", "shell_continuous", "needs_frame", "quit", "windowed", "active", "unattended" }; const Kind = enum(u8) { input, done, host, ret, set, digest }; +/// A digest record's kind byte (replica.zig reads a unit's last). +pub const digest_kind: u8 = @backingInt(Kind.digest); +pub const set_kind: u8 = @backingInt(Kind.set); const host_fns = @typeInfo(VTable).@"struct".field_names; /// The `set` that says which Host functions the recording host had. const set_mask = 0xff; @@ -163,6 +167,10 @@ pub const Tape = struct { /// output carries one, at the pty's read rate), not one a field; what /// differs is then not named. whole_digest: bool = false, + /// A replica client's guess of the session's clocks for a speculation + /// (src/replica.zig): its clock reads (a message's stamp, a file's + /// mtime) answer this instead of the clocks here. + guess: ?Guess = null, fn record(t: *Tape, kind: Kind, comptime T: type, v: *const T) void { t.scratch.clearRetainingCapacity(); @@ -501,6 +509,7 @@ pub const Tape = struct { .update => |ev| p.update(ev), .serve_fs => |req| _ = p.serveFs(req), .pump => p.pump(t.hostOf(t.mask)) catch {}, + .step => p.step(), .perform => |e| p.perform(e), .next_effect => _ = p.nextEffect(), .advance => |now| p.advance(now), @@ -758,6 +767,12 @@ fn call(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anyt t.poisoned = true; return poisonOf(R); } + if (comptime s == .clock or s == .mtime) if (t.speculating) if (t.guess) |g| { + const r = guessed(R, g); + t.record(.host, u16, &id); + t.record(.ret, R, &r); + return r; + }; t.record(.host, u16, &id); t.seeFields(); t.host_side = true; @@ -768,6 +783,43 @@ fn call(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anyt return r; } +pub const Guess = struct { real_ns: i64, mono_ns: i64, gmtoff: i32 }; + +/// A clock read of the session's, as a replica client guesses it: a file's +/// mtime (realtime seconds), a message's clock (monotonic ms), a message's +/// time of day (in the session's zone). +fn guessed(comptime R: type, g: Guess) R { + if (R == u32) return std.math.cast(u32, @divFloor(g.real_ns, std.time.ns_per_s)) orelse 0; + if (R == i64) return @divFloor(g.mono_ns, std.time.ns_per_ms); + if (R == [8]u8) { + var clock: [8]u8 = "--:--:--".*; + const notime = if (libc.getenv("PARDES_NOTIME")) |v| std.mem.span(v).len != 0 else false; + if (notime) return clock; + const day = @mod(@divFloor(g.real_ns, std.time.ns_per_s) + g.gmtoff, 86400); + _ = std.fmt.bufPrint(&clock, "{d:0>2}:{d:0>2}:{d:0>2}", .{ @as(u32, @intCast(@divFloor(day, 3600))), @as(u32, @intCast(@mod(@divFloor(day, 60), 60))), @as(u32, @intCast(@mod(day, 60))) }) catch {}; + return clock; + } + @compileError("tape: no guess of a clock read as " ++ @typeName(R)); +} + +/// A core made outside the tape (a replica client's, from a dump) is the +/// one it replays into. +pub fn adoptCore(t: *Tape, p: *Pardes) void { + t.core = p; + p.host = t.hostOf(t.mask); +} + +/// A replica joins the session recorded on `t` (src/replica.zig): what the +/// host wrote in the core and which host it is are said again at the next +/// input, for a core made from a dump, which has neither. +pub fn resync(t: *Tape) void { + t.seen = @splat(0); + if (t.core) |p| if (p.host.vtable == &t.vtable) { + p.host = t.real; + }; + t.real = .{}; +} + /// A budget of `budget_ns` from `since_ns` on the monotonic clock is spent: /// what the clock said is taped, so a replica stops its work where this did. pub fn over(since_ns: u64, budget_ns: u64) bool { @@ -964,6 +1016,7 @@ pub fn describe(bytes: []const u8, w: *std.Io.Writer) void { const id = std.mem.bytesToValue(u16, payload[0..2]); w.print("host {s}", .{if (id < host_fns.len) host_fns[id] else @tagName(@as(Site, @fromBackingInt(@intCast(id - host_fns.len))))}) catch {}; }, + .ret => if (payload.len <= 8) w.print("ret {x}", .{payload}) catch {} else w.print("ret ({d} bytes)", .{payload.len}) catch {}, else => w.print("{t} ({d} bytes)", .{ kind, payload.len }) catch {}, } w.writeByte('\n') catch {}; diff --git a/src/views.zig b/src/views.zig index b9760143..361d14a3 100644 --- a/src/views.zig +++ b/src/views.zig @@ -508,28 +508,64 @@ pub fn drawOthers(p: *Pardes, h: pardes.Host) !void { } for (0..max) |i| { const k: u8 = @intCast(i); - if (k == live or !isOpen(p, k)) continue; - // `unpark` hands each slot's selections to its body; drawn, the - // view is out again, and they are the slot's again (else a frame - // drawn would take them from a view, and a replica that draws - // nothing would keep them). - var sels: [MAX_PANES]struct { s: @TypeOf(@as(Pane, undefined).body.sels), n: @TypeOf(@as(Pane, undefined).body.nsel) } = undefined; - for (p.panes, 0..) |s, id| if (s) |pane| { - sels[id] = .{ .s = pane.pv[k].body.sels, .n = pane.pv[k].body.nsel }; - }; - defer for (p.panes, 0..) |s, id| if (s) |pane| { - pane.pv[k].body.sels = sels[id].s; - pane.pv[k].body.nsel = sels[id].n; - }; - unpark(p, k); - _ = p.frame_arena.reset(.{ .retain_with_limit = pardes.memory.limits.frame_kept }); - const surface = try p.render(p.frame_arena.allocator()); - if (v.present) |f| f(h.ctx, surface); + if (k == live or !isOpen(p, k) or p.undrawn & (@as(u8, 1) << @intCast(k)) != 0) continue; // No post_present: what a shell acknowledges there moves the // session's pointer and hover, which are the live view's to move; // a view's panels are acknowledged when it is live and drawn. - copy(&drawn_fields, &p.parked[k].?.view, p); + try drawParked(p, k, .{ .ctx = h.ctx, .f = v.present }); + } +} + +pub const Present = struct { ctx: ?*anyopaque, f: ?*const fn (?*anyopaque, *const pardes.Surface) void }; + +/// Parked view `k` unparked, drawn and presented. `unpark` hands each +/// slot's selections to its body; drawn, the view is out again, and they +/// are the slot's again (else a frame drawn would take them from a view, +/// and a replica that draws nothing would keep them). +fn drawParked(p: *Pardes, k: u8, present: Present) !void { + var sels: [MAX_PANES]struct { []Text.SelRange, u16 } = undefined; + for (p.panes, 0..) |s, id| if (s) |pane| { + sels[id] = .{ pane.pv[k].body.sels, pane.pv[k].body.nsel }; + }; + unpark(p, k); + defer for (p.panes, 0..) |s, id| if (s) |pane| { + pane.pv[k].body.sels = sels[id][0]; + pane.pv[k].body.nsel = sels[id][1]; + }; + _ = p.frame_arena.reset(.{ .retain_with_limit = pardes.memory.limits.frame_kept }); + const surface = try p.render(p.frame_arena.allocator()); + if (present.f) |f| f(present.ctx, surface); + copy(&drawn_fields, &p.parked[k].?.view, p); +} + +/// View `k` drawn and presented outside any step, by a replica client +/// (src/replica.zig) whose own it is: as `drawOthers` draws one, the live +/// view put back after exactly as it was, so the replica's digest is the +/// authority's still. No post_present, as there. +pub fn drawOne(p: *Pardes, k: u8, present: Present) !void { + if (k == p.view or !isOpen(p, k)) { + _ = p.frame_arena.reset(.{ .retain_with_limit = pardes.memory.limits.frame_kept }); + const surface = try p.render(p.frame_arena.allocator()); + if (present.f) |f| f(present.ctx, surface); + return; + } + const live = p.view; + park(p); + defer { + copy(&fields, p, &p.parked[live].?.view); + p.view = live; + for (p.panes) |s| if (s) |pane| { + const pv = &pane.pv[live]; + copy(&pane_fields, pane, pv); + copy(&body_fields, &pane.body, &pv.body); + if (!pane.isTerminal()) { + pane.cols = pv.cols; + pane.rows = pv.rows; + } + if (pane.file) |*f| f.scroll = pv.scroll; + }; } + try drawParked(p, k, present); } // ---- tests ---- -- cgit v1.3 From fdf74e199de7d0fc648f912875323f3dc920ffc2 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 20:56:03 -0300 Subject: replica clients over a socket: the session streams each its dump and every unit after it (replica.zig `Peers`, a writer and a reader thread a client, units at each step boundary, a client's input stepped there in its own view), and `pardes --attach=:` and `--attach=:` (through `ssh -tt -L`) are a client in the tty host, its keys applied at once by its own replica and drawn by its own process wire.zig version 15: welcome (the client's id and view, the dump's length, the session's UTC offset and digest), dump pieces, units (with the authority's clocks), terminal messages both ways, a check. A unit carries one digest, at its end (`unit_digests`). The client: tty.zig `Shell.remote`, `feed` (a terminal's input to the replica and the session); the session's clock guessed from the last unit's (tape.zig `Guess`); a pump woken only by a client's post reads no new time (`mail_only`), and a unit of another's ending where the last ended leaves the speculation as it is (`neutral`); the rollback copy taken while idle (`Replica.ready`); views a frontend here does not draw left undrawn. A lagging client never holds the session: its queue is cut off past 64 MiB (PARDES_REPLICA_LAG_KB). Detach in a client ends that client; a Restore lets every client go. daemon.zig: a replica the daemon hands over is the session's loop's (a note), not a stub thread's. A window session refuses one in words. test/daemon.py `--clients`: two clients (one through the delay relay) type into a detached session and agree with it, one killed outright, one detached, a third typing into a shell. Co-Authored-By: Claude Opus 5.5 --- src/daemon.zig | 61 +--- src/detached/wire.zig | 94 +++++- src/gui/gui.zig | 3 + src/main.zig | 101 ++++++ src/replica.zig | 883 ++++++++++++++++++++++++++++++++++++++++++++++-- src/rollback.zig | 23 +- src/tape.zig | 15 +- src/tty/tty.zig | 425 +++++++++++++++++++++-- test/daemon.py | 153 ++++++++- test/rollback_bench.zig | 18 + 10 files changed, 1659 insertions(+), 117 deletions(-) (limited to 'src/rollback.zig') diff --git a/src/daemon.zig b/src/daemon.zig index 785f4afd..d2ae012c 100644 --- a/src/daemon.zig +++ b/src/daemon.zig @@ -357,19 +357,10 @@ pub fn adopt() void { } /// This session is about to exec another build (upgrade.zig): the daemon -/// is asked to move too, and every replica told why it is let go. -/// ponytail: a pong its thread writes at the same moment may split the -/// words; the client reconnects either way. +/// is asked to move too. Its replica clients were told why they are let go +/// (replica.zig `Peers.refuseAll`). pub fn upgraded() void { tell("upgrade", &.{}); - var out: [600]u8 = undefined; - const said = wire.encodeServer(&out, .{ .refused = "the session is upgrading to another build of pardes: connect again with that build" }) catch return; - for (&replica_fds) |*slot| { - const fd = slot.load(.acquire); - if (fd < 0) continue; - _ = writeAll(fd, said); - _ = libc.shutdown(fd, 2); - } } /// An upgraded session's link and stub as its old process left them: its @@ -388,6 +379,8 @@ pub const Note = union(enum) { /// stub's pipe; for a window session, both -1: open a window. attach: struct { tty: c_int, stub: c_int }, adopted: struct { pids: [max_fds]libc.pid_t, fds: [max_fds]c_int, n: usize }, + /// A replica's connection, its hello taken (src/replica.zig `Peers`). + replica: c_int, }; /// Hands every note the daemon sends on to `deliver`, on a thread of its @@ -416,9 +409,7 @@ fn readNotes(fd: c_int, ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, no } }); for (got.fds[@min(got.nfds, 2)..got.nfds]) |f| _ = libc.close(f); } else if (std.mem.eql(u8, verb, "replica")) { - if (got.nfds >= 1) { - if (std.Thread.spawn(.{}, serveReplica, .{got.fds[0]})) |t| t.detach() else |_| _ = libc.close(got.fds[0]); - } + if (got.nfds >= 1) deliver(ctx, .{ .replica = got.fds[0] }); for (got.fds[@min(got.nfds, 1)..got.nfds]) |f| _ = libc.close(f); } else if (std.mem.eql(u8, verb, "adopted")) { var note: Note = .{ .adopted = .{ .pids = undefined, .fds = undefined, .n = 0 } }; @@ -450,43 +441,11 @@ fn writeAll(fd: c_int, bytes: []const u8) bool { return true; } -var replica_fds: [max_replicas]std.atomic.Value(c_int) = @splat(.init(-1)); - -/// A replica the daemon handed over, its hello accepted: welcomed, its pings -/// answered, until it says bye or hangs up. ponytail: phase 5 proper makes it -/// a replica (the log, the dump, terminal state); this stub is what times a -/// round trip through the daemon's handoff and `ssh -L`. A message larger -/// than its buffer ends it. -fn serveReplica(fd: c_int) void { - defer _ = libc.close(fd); - // Where an upgrade finds it to say why it ends (`upgraded`). - const slot = for (&replica_fds) |*r| { - if (r.cmpxchgStrong(-1, fd, .acq_rel, .acquire) == null) break r; - } else null; - defer if (slot) |r| r.store(-1, .release); - log.info("a replica connected to '{s}'", .{name()}); - defer log.info("a replica of '{s}' left", .{name()}); - var out: [wire.hello_max]u8 = undefined; - const pid: u32 = @intCast(libc.getpid()); - if (!writeAll(fd, wire.encodeServer(&out, .{ .welcome = .{ .pid = pid, .session = name() } }) catch return)) return; - var buf: [msg_max]u8 = undefined; - var len: usize = 0; - while (true) { - while (wire.framed(buf[0..len]) catch return) |f| { - switch (wire.decodeClient(f.tag, f.payload) catch return) { - .ping => |v| if (!writeAll(fd, wire.encodeServer(&out, .{ .pong = v }) catch return)) return, - .bye, .hello => return, - .event => {}, - } - std.mem.copyForwards(u8, &buf, buf[f.total..len]); - len -= f.total; - } - if (len == buf.len) return; - const n = libc.read(fd, buf[len..].ptr, buf.len - len); - if (n < 0 and libc.errno(n) == .INTR) continue; - if (n <= 0) return; - len += @intCast(n); - } +/// A replica refused by a session that takes none (a window's, so far). +pub fn refuseReplica(fd: c_int, why: []const u8) void { + var out: [600]u8 = undefined; + if (wire.encodeServer(&out, .{ .refused = why })) |bytes| _ = writeAll(fd, bytes) else |_| {} + _ = libc.close(fd); } /// A session ended by a signal (TERM, HUP, INT): its shells go with it, as diff --git a/src/detached/wire.zig b/src/detached/wire.zig index 097cbee7..347d45d1 100644 --- a/src/detached/wire.zig +++ b/src/detached/wire.zig @@ -9,9 +9,11 @@ const builtin = @import("builtin"); const pardes = @import("../pardes.zig"); const config = @import("pardes_config"); -/// 14: the hello names a session and carries the semantics hash, and a -/// ping is answered (phase 5's groundwork); 13: the frames went (phase 3). -pub const version: u16 = 14; +/// 15: the session streams a replica its dump and units, and terminal +/// deltas both ways (phase 5 part B); 14: the hello names a session and +/// carries the semantics hash, and a ping is answered (phase 5's +/// groundwork); 13: the frames went (phase 3). +pub const version: u16 = 15; /// What makes two cores step differently, hashed: a replica's hello carries /// its own, and one of another build is refused. The protocol, the release @@ -52,6 +54,12 @@ pub const ClientTag = enum(u8) { hello = 0x01, bye = 0x02, ping = 0x03, + /// A replica's word about a terminal's state (src/screen_state.zig): the + /// delta it holds, or rows it asks for. Opaque here. + screen_ack = 0x04, + /// What is the session's digest, in stream order (a replica checks + /// itself against it once quiet). + check = 0x05, key = 0x10, mouse = 0x11, @@ -109,14 +117,65 @@ pub const ServerTag = enum(u8) { refused = 0x02, /// A ping's value back. pong = 0x03, + /// A piece of the join's dump (src/dump.zig): the welcome says how long + /// it is whole. + dump = 0x04, + /// A unit of the authority's stream (src/replica.zig): its origin, seq, + /// clocks and tape. + unit = 0x05, + /// A terminal's delta, or rows asked for (src/screen_state.zig), applied + /// before the next unit. Opaque here. + screen = 0x06, + /// The session's digest (u64le), after every unit before it. + check = 0x07, }; pub const ServerMsg = union(ServerTag) { - welcome: struct { pid: u32, session: []const u8 }, + /// Who the client is now (its id and view in the session), how long the + /// dump that follows is, and the session's offset from UTC (a replica + /// guesses the session's time of day). + welcome: struct { pid: u32, session: []const u8, client: u16 = 0, view: u8 = 0, dump_len: u64 = 0, gmtoff: i32 = 0, digest: u64 = 0 }, refused: []const u8, pong: u64, + dump: []const u8, + unit: []const u8, + screen: []const u8, + check: u64, }; +/// A message's header, for a payload sent on its own (a unit, a dump's +/// piece): no copy of it into a message buffer. +pub fn header(tag: u8, len: usize) [header_len]u8 { + var h: [header_len]u8 = undefined; + h[0] = tag; + std.mem.writeInt(u32, h[1..5], @intCast(len), .little); + return h; +} + +/// The other way: what a client is told, its slices borrowing `payload`. +pub fn decodeServer(tag: u8, payload: []const u8) Error!ServerMsg { + var r: std.Io.Reader = .fixed(payload); + const msg: ServerMsg = switch (std.enums.fromInt(ServerTag, tag) orelse return error.BadTag) { + .welcome => .{ .welcome = .{ + .pid = try r.takeInt(u32, .little), + .session = try r.take(try r.takeInt(u16, .little)), + .client = try r.takeInt(u16, .little), + .view = try r.takeByte(), + .dump_len = try r.takeInt(u64, .little), + .gmtoff = try r.takeInt(i32, .little), + .digest = try r.takeInt(u64, .little), + } }, + .refused => .{ .refused = try r.take(try r.takeInt(u16, .little)) }, + .pong => .{ .pong = try r.takeInt(u64, .little) }, + .dump => return .{ .dump = payload }, + .unit => return .{ .unit = payload }, + .screen => return .{ .screen = payload }, + .check => .{ .check = try r.takeInt(u64, .little) }, + }; + try end(&r); + return msg; +} + pub fn encodeServer(out: []u8, msg: ServerMsg) Error![]const u8 { var w: std.Io.Writer = .fixed(out); const at = try beginMessage(&w, @backingInt(std.meta.activeTag(msg))); @@ -124,9 +183,16 @@ pub fn encodeServer(out: []u8, msg: ServerMsg) Error![]const u8 { .welcome => |h| { try w.writeInt(u32, h.pid, .little); try putSlice16(&w, h.session); + try w.writeInt(u16, h.client, .little); + try w.writeByte(h.view); + try w.writeInt(u64, h.dump_len, .little); + try w.writeInt(i32, h.gmtoff, .little); + try w.writeInt(u64, h.digest, .little); }, .refused => |words| try putSlice16(&w, words), .pong => |v| try w.writeInt(u64, v, .little), + .dump, .unit, .screen => |bytes| try w.writeAll(bytes), + .check => |v| try w.writeInt(u64, v, .little), } try finishMessage(&w, at); return w.buffered(); @@ -157,6 +223,8 @@ pub const ClientMsg = union(enum) { bye, /// A round trip's probe, answered `pong` with the same value. ping: u64, + screen_ack: []const u8, + check, event: pardes.Event, }; @@ -366,6 +434,8 @@ pub fn encodeClient(out: []u8, msg: ClientMsg) Error![]const u8 { }, .bye => {}, .ping => |v| try w.writeInt(u64, v, .little), + .screen_ack => |bytes| try w.writeAll(bytes), + .check => {}, .event => |ev| switch (ev) { .key => |k| { try w.writeInt(u32, k.cp, .little); @@ -432,6 +502,8 @@ pub fn decodeClient(tag: u8, payload: []const u8) Error!ClientMsg { } }, .bye => .bye, .ping => .{ .ping = try r.takeInt(u64, .little) }, + .screen_ack => return .{ .screen_ack = payload }, + .check => .check, .key => blk: { const cp = try r.takeInt(u32, .little); // `Key.cp` is a u21, and the specials live in the private-use @@ -492,7 +564,8 @@ const msg_slack = header_len + 64; pub fn clientBound(msg: ClientMsg) usize { return msg_slack + switch (msg) { .hello => |h| h.session.len, - .bye, .ping => 0, + .bye, .ping, .check => 0, + .screen_ack => |bytes| bytes.len, .event => |ev| switch (ev) { .mouse, .resize, .pdf_scroll, .pinch, .touch_scroll, .pointer_leave, .focus => 0, .key => |k| k.text.len, @@ -547,7 +620,10 @@ test "detached wire: every Event variant round-trips" { const bytes = try encodeServer(&buf, .{ .welcome = .{ .pid = 42, .session = "work" } }); const f = (try framed(bytes)).?; try testing.expectEqual(@as(u8, 0x01), f.tag); - try testing.expectEqualSlices(u8, &.{ 42, 0, 0, 0, 4, 0, 'w', 'o', 'r', 'k' }, f.payload); + try testing.expectEqualSlices(u8, &.{ 42, 0, 0, 0, 4, 0, 'w', 'o', 'r', 'k', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, f.payload); + const back = (try decodeServer(f.tag, f.payload)).welcome; + try testing.expectEqualStrings("work", back.session); + try testing.expectEqual(@as(u32, 42), back.pid); try testing.expectEqual(@as(u8, 0x03), (try framed(try encodeServer(&buf, .{ .pong = 7 }))).?.tag); } @@ -622,8 +698,10 @@ test "detached wire: a session is never told to do a frontend's remembering" { // them, and the machine that reports them is the one already holding the // core. const allowed = [_][]const u8{ - // Who this connection is, its round trip, and that it is finished. - "hello", "bye", "ping", + // Who this connection is, its round trip, and that it is finished, + // and which terminal state its replica holds (a protocol's word, + // as a ping is). + "hello", "bye", "ping", "screen_ack", "check", // ...and everything a person can do to a window. "key", "mouse", "resize", "paste", "command", "pdf_scroll", diff --git a/src/gui/gui.zig b/src/gui/gui.zig index e4d69281..a88a7335 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -4884,6 +4884,9 @@ fn deliverNote(ctx: *anyopaque, note: daemon.Note) void { for (a.pids[0..a.n], a.fds[0..a.n]) |pid, fd| q.push(.{ .adopted = .{ .pid = pid, .fd = fd } }); q.push(.{ .adopted = .{ .pid = 0, .fd = -1 } }); }, + // ponytail: a window session serves no replica yet; a terminal's + // does (tty.zig). + .replica => |fd| daemon.refuseReplica(fd, "a window session takes no replica yet: run the session in a terminal, or --detach"), } } diff --git a/src/main.zig b/src/main.zig index a6144475..ab5047ce 100644 --- a/src/main.zig +++ b/src/main.zig @@ -108,6 +108,13 @@ const help_text = \\ the session detaches or ends \\ --attach= ...the session called (its --9p or \\ --detach name, else its pid) + \\ --attach=: a client of session on another machine, + \\ over `ssh -L` to its pardes-replica.sock + \\ ($PARDES_SSH: the ssh command, words; and + \\ $PARDES_REMOTE_RUNTIME: its runtime directory, + \\ if not this one's path). Its keys show at once + \\ --attach=: ...through that socket (a path, with a '/'), + \\ one forwarded already, or this machine's \\ --geometry=WxH[+X+Y] the window's size in points, and where it goes \\ where the display lets a window say (X11) \\ -h, --help show this help and exit @@ -738,6 +745,10 @@ fn nativeMain(std_init: std.process.Init) !void { opts.config_dir = found.dir; opts.store_path = storePath(init.io, arena, init.environ_map); if (found.dir) |d| @import("crash.zig").setDir(d); + if (attach) |name| if (std.mem.lastIndexOfScalar(u8, name, ':')) |colon| { + if (comptime pardes.platform != .tty) badArgs(init.io, "a window is no replica client yet: run `pardes --attach={s}` in a terminal", .{name}); + remoteAttach(init, opts, name[0..colon], name[colon + 1 ..]); + }; if (attach) |name| if (!resuming) attachTo(init.io, name); if (detach) |name| { // Said plainly, never a Zig error trace: a name a live session's @@ -815,6 +826,96 @@ fn nativeMain(std_init: std.process.Init) !void { daemon.tellStubExit(0); } +extern "c" fn execvp(file: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int; + +/// `pardes --attach=:`: a replica client (tty.zig `remote`), +/// through the socket `where` names, or through `ssh -L` of host `where`'s +/// replica socket to one of this process's. +fn remoteAttach(init: std.process.Init, opts: pardes.Options, where: []const u8, name: []const u8) noreturn { + const io = init.io; + if (comptime pardes.platform != .tty) unreachable; + const Say = struct { + fn no(o: std.Io, comptime fmt: []const u8, args: anytype) noreturn { + var buf: [700]u8 = undefined; + std.Io.File.stderr().writeStreamingAll(o, std.fmt.bufPrint(&buf, "pardes: --attach: " ++ fmt ++ "\n", args) catch "pardes: --attach failed\n") catch {}; + std.process.exit(1); + } + }; + if (std.mem.indexOfScalar(u8, where, '/') != null) { + @import("tty/tty.zig").remote(init, opts, where, name) catch |err| Say.no(io, "{t}", .{err}); + std.process.exit(0); + } + // ssh -L : , until the socket here answers. + var here_buf: [ninep_io.sun_path_len:0]u8 = undefined; + const tmp = std.mem.span(std.c.getenv("TMPDIR") orelse "/tmp"); + const here = std.fmt.bufPrintSentinel(&here_buf, "{s}/pardes-ssh-{d}.sock", .{ tmp, std.c.getpid() }, 0) catch Say.no(io, "TMPDIR is too long for a socket's path", .{}); + var there_buf: [ninep_io.sun_path_len + 64:0]u8 = undefined; + var dir_buf: [ninep_io.sun_path_len:0]u8 = undefined; + const dir = if (std.c.getenv("PARDES_REMOTE_RUNTIME")) |d| std.mem.span(d) else ninep_io.socketDir(&dir_buf) orelse Say.no(io, "no runtime directory to name the remote socket by: set PARDES_REMOTE_RUNTIME", .{}); + var forward_buf: [2 * ninep_io.sun_path_len + 64]u8 = undefined; + const there = std.fmt.bufPrintSentinel(&there_buf, "{s}/pardes-replica.sock", .{dir}, 0) catch unreachable; + const forward = std.fmt.bufPrintSentinel(&forward_buf, "{s}:{s}", .{ here, there }, 0) catch unreachable; + var host_buf: [256]u8 = undefined; + const host = std.fmt.bufPrintSentinel(&host_buf, "{s}", .{where}, 0) catch Say.no(io, "that host's name is too long", .{}); + var argv: [48:null]?[*:0]const u8 = @splat(null); + var n: usize = 0; + var words_buf: [1024]u8 = undefined; + var words = std.mem.tokenizeScalar(u8, std.mem.span(std.c.getenv("PARDES_SSH") orelse "ssh"), ' '); + var at: usize = 0; + while (words.next()) |w| { + if (n >= 40 or at + w.len + 1 > words_buf.len) Say.no(io, "PARDES_SSH has too many words", .{}); + @memcpy(words_buf[at..][0..w.len], w); + words_buf[at + w.len] = 0; + argv[n] = @ptrCast(&words_buf[at]); + n += 1; + at += w.len + 1; + } + // A terminal asked for and a command that waits, not -N: only an + // interactive connection gets TCP_NODELAY from ssh and sshd, and with + // Nagle each key in a burst waited for the last one's ACK, a round trip. + for ([_][*:0]const u8{ "-tt", "-q", "-o", "ExitOnForwardFailure=yes", "-L", forward.ptr, host.ptr, "exec sleep 2147483647" }) |a| { + argv[n] = a; + n += 1; + } + _ = std.c.unlink(here); + const pid = std.c.fork(); + if (pid < 0) Say.no(io, "no fork for ssh", .{}); + if (pid == 0) { + const null_fd = std.c.open("/dev/null", .{ .ACCMODE = .RDONLY }, @as(std.c.mode_t, 0)); + if (null_fd >= 0) _ = std.c.dup2(null_fd, 0); + _ = execvp(argv[0].?, &argv); + std.c._exit(127); + } + const Ssh = struct { + var child: std.c.pid_t = 0; + var path: [:0]const u8 = ""; + fn end() void { + _ = std.c.kill(child, .TERM); + _ = std.c.waitpid(child, null, 0); + _ = std.c.unlink(path); + } + }; + Ssh.child = pid; + Ssh.path = here; + var waited: u32 = 0; + while (std.c.access(here, std.c.F_OK) != 0) : (waited += 1) { + var status: c_int = 0; + if (std.c.waitpid(pid, &status, std.c.W.NOHANG) == pid) Say.no(io, "ssh to {s} ended before its forward was up", .{where}); + if (waited > 3000) { + Ssh.end(); + Say.no(io, "ssh to {s} set up no forward in 30 s", .{where}); + } + const ts: std.c.timespec = .{ .sec = 0, .nsec = 10 * std.time.ns_per_ms }; + _ = std.c.nanosleep(&ts, null); + } + @import("tty/tty.zig").remote(init, opts, here, name) catch |err| { + Ssh.end(); + Say.no(io, "{t}", .{err}); + }; + Ssh.end(); + std.process.exit(0); +} + /// `pardes --attach[=name]` in a terminal: this terminal handed (through /// the daemon) to the detached session of that name, or the only one, and /// this process its stub until the session leaves it. `pardes-gui diff --git a/src/replica.zig b/src/replica.zig index 032e50af..5c965b2b 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -44,11 +44,34 @@ fn header(buf: []u8, who: u16, seq: u32) void { std.mem.writeInt(u32, buf[2..6], seq, .little); } -pub const Unit = struct { origin: u16, seq: u32, tape: []const u8 }; +/// 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 }; pub fn parseUnit(msg: []const u8) ?Unit { - if (msg.len < head_len) return null; - return .{ .origin = std.mem.readInt(u16, msg[0..2], .little), .seq = std.mem.readInt(u32, msg[2..6], .little), .tape = msg[head_len..] }; + 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..], + }; +} + +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 }; @@ -87,6 +110,13 @@ fn nowNs() u64 { return pardes.FrameStats.nowNs(); } +/// 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 { @@ -142,8 +172,8 @@ pub const Authority = struct { /// done straight on the core). pub fn unit(a: *Authority, origin: u16, seq: u32) ![]const u8 { a.out.clearRetainingCapacity(); - try a.out.appendNTimes(a.gpa, 0, head_len); - header(a.out.items, origin, seq); + try a.out.appendNTimes(a.gpa, 0, unit_head); + unitHeader(a.out.items, origin, seq); try a.out.appendSlice(a.gpa, tape.taken(&a.t)); tape.clearTaken(&a.t); return a.out.items; @@ -212,7 +242,7 @@ pub const Authority = struct { const was = Stepping.begin(&a.t, null); a.core.toView(a.view_of[in.client]); a.core.update(in.event); - a.core.pump(a.host) catch {}; + a.core.step(); was.end(); return a.unit(in.client, in.seq); } @@ -437,7 +467,7 @@ pub const Stats = struct { } }; -const Pending = struct { seq: u32, msg: []u8, spec: ?[]u8 = null }; +const Pending = struct { seq: u32, msg: []u8, spec: ?[]u8 = null, sent_ns: u64 = 0 }; pub const Replica = struct { gpa: Allocator, @@ -464,6 +494,26 @@ pub const Replica = struct { 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, + /// 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), screen_out: std.ArrayListUnmanaged(u8) = .empty, @@ -497,6 +547,43 @@ pub const Replica = struct { return r; } + /// A replica of a session elsewhere, as its walker dump (src/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.headless = 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; + o.pdf_allocator = o.pdf_allocator orelse std.heap.c_allocator; + // The session's store and Recent are the authority's. + o.store_path = null; + const c = try pardes.dump.load(epoch, o, bytes, null); + // 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); + return r; + } + pub fn deinit(r: *Replica) void { for (&r.screens) |*slot| if (slot.*) |*sc| sc.rep.deinit(terminal.emulator_gpa); if (r.t.core) |c| { @@ -512,6 +599,7 @@ pub const Replica = struct { 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); @@ -539,7 +627,7 @@ pub const Replica = struct { const n = (try wire.encodeClient(room[head_len..], .{ .event = ev })).len; header(room, r.id, seq); const msg = try r.gpa.dupe(u8, room[0 .. head_len + n]); - try r.pending.append(r.gpa, .{ .seq = seq, .msg = msg }); + try r.pending.append(r.gpa, .{ .seq = seq, .msg = msg, .sent_ns = nowNs() }); 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); @@ -554,6 +642,21 @@ pub const Replica = struct { const u = parseUnit(msg) orelse return r.fail("a unit too short to read"); r.stats.units += 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: the unit moves the + // state it was of. + if (r.applied == 0 and r.copy.held) { + r.copy.release(); + rollback.Epoch.select(null); + r.clearSince(); + } + 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 @@ -579,6 +682,25 @@ pub const Replica = struct { } return; } + const ends = lastDigest(u.tape); + defer if (ends) |d| { + r.last_digest.clearRetainingCapacity(); + 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; + const kept = r.gpa.dupe(u8, u.tape) 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; + r.toConfirmed(); + r.dirty = true; + } + return; + } 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) { @@ -593,6 +715,7 @@ pub const Replica = struct { r.recovering = nowNs(); r.toConfirmed(); } + r.moved = true; if (tape.replayUnit(&r.t, u.tape)) |why| { if (std.c.getenv("PARDES_REPLICA_TRACE") != null) { var w: std.Io.Writer.Allocating = .init(r.gpa); @@ -657,17 +780,10 @@ pub const Replica = struct { /// 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; - switch (in.event) { - .key, .paste, .command => {}, - else => { - // The view's (a resize, the pointer): the authority's for - // now, phase 4's views to come. - r.stats.unspeculated += 1; - r.blocked = p.seq; - return false; - }, - } const c = r.core(); if (!r.copy.held) { const t0 = nowNs(); @@ -683,6 +799,10 @@ pub const Replica = struct { if (checked) terminalDigests(c, &terminals); const t0 = nowNs(); tape.beginSpeculation(&r.t); + r.t.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 null; c.toView(r.view); // Input into a terminal is the authority's: its step reads the // emulator (rows, cursor, modes), which here is the last delta's and @@ -690,7 +810,7 @@ pub const Replica = struct { // echo. if (r.t.screen_state) if (c.panes[c.active]) |pane| if (pane.isTerminal()) tape.poison(); c.update(in.event); - c.pump(c.host) catch {}; + c.step(); const clean = tape.endSpeculation(&r.t); const dt = nowNs() - t0; // Where a blob was written, should one be: the input that did. @@ -699,6 +819,7 @@ pub const Replica = struct { // Undone, and the earlier ones applied again on the confirmed // position: this one waits for the authority. r.stats.stopped += 1; + if (std.c.getenv("PARDES_REPLICA_TRACE") != null) std.debug.print("replica {d}: its input {d} ({t}) stopped at a host call: it waits\n", .{ r.id, p.seq, in.event }); r.blocked = p.seq; const upto = r.applied; r.toConfirmed(); @@ -732,9 +853,41 @@ pub const Replica = struct { } /// Nothing pending and nothing applied: the replica is the authority's - /// confirmed state, which `digest` says. + /// 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 and !r.copy.held; + 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.stats.copy_ns.append(r.gpa, nowNs() - t0) catch {}; + r.stats.copy_bytes += r.copy.savedBytes(); + r.stats.copies += 1; + } + + /// 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 tape.digestOf(r.core()) == 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 {}; } /// One of the authority's terminal messages (`Screens.flush`, or an @@ -827,6 +980,20 @@ pub const Replica = struct { } }; +/// 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; @@ -838,6 +1005,647 @@ fn terminalDigests(p: *const Pardes, out: *[pardes.MAX_PANES]u64) void { } } +// ---- 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 +// (src/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); +const nosignal: u32 = if (builtin.os.tag == .linux) libc.MSG.NOSIGNAL else 0; +/// 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. +/// PARDES_REPLICA_LAG_KB says another (a test's). +var lag_max: usize = 64 << 20; +const dump_piece = 1 << 20; + +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; +} + +fn writeAll(fd: c_int, bytes: []const u8) bool { + var at: usize = 0; + while (at < bytes.len) { + const n = libc.send(fd, bytes[at..].ptr, bytes.len - at, nosignal); + if (n < 0 and libc.errno(n) == .INTR) continue; + if (n <= 0) return false; + at += @intCast(n); + } + return true; +} + +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, + + pub fn deinit(f: *Framer, gpa: Allocator) void { + f.buf.deinit(gpa); + } + + /// Framed messages off `fd` (detached/wire.zig) handed to `take` until + /// the stream ends, `take` says stop, or a message is malformed. + fn run(fr: *Framer, gpa: Allocator, fd: c_int, ctx: anytype, comptime take: fn (@TypeOf(ctx), u8, []const u8) bool) void { + 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; + 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; + } + } +}; + +fn readFramed(gpa: Allocator, fd: c_int, ctx: anytype, comptime take: fn (@TypeOf(ctx), u8, []const u8) bool) void { + var fr: Framer = .{}; + defer fr.deinit(gpa); + fr.run(gpa, fd, ctx, take); +} + +pub const Peer = struct { + peers: *Peers, + fd: c_int, + id: u16, + view: u8, + /// Inputs read (each one's seq, as the client numbered them). + seq: u32 = 0, + 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, + + /// `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; + 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); + } + + fn writeLoop(peer: *Peer) void { + const io = peer.peers.io; + 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 = 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, .bye => 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; + }, + // ponytail: rterm's acks (src/screen_state.zig) go to its + // session state once terminals cross as screens. + .screen_ack => return true, + .check => { + peer.peers.post(.{ .check = peer }); + return true; + }, + else => {}, + } + peer.seq += 1; + const msg = peer.peers.gpa.alloc(u8, head_len + wire.header_len + payload.len) catch return false; + header(msg, peer.id, peer.seq); + msg[head_len] = tag; + std.mem.writeInt(u32, msg[head_len + 1 ..][0..4], @intCast(payload.len), .little); + @memcpy(msg[head_len + 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 { + readFramed(peer.peers.gpa, peer.fd, peer, take); + peer.peers.post(.{ .gone = peer }); + } +}; + +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), + /// Wakes the session's loop (its wait for input) from a reader. + wake_ctx: ?*anyopaque = null, + wake: ?*const fn (?*anyopaque) void = null, + /// What the session would be with no client: unattended without a + /// terminal of its own (the host says). + alone_unattended: bool = false, + /// The worst a join cost the session's step, and the last one's dump. + join_ns: u64 = 0, + dump_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. + input_ns: std.ArrayListUnmanaged(u64) = .empty, + boundary_ns: std.ArrayListUnmanaged(u64) = .empty, + cut: u32 = 0, + head: [unit_head]u8 = undefined, + + pub const Note = union(enum) { join: c_int, input: struct { peer: *Peer, msg: []u8 }, gone: *Peer, check: *Peer }; + + 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 => |fd| _ = libc.close(fd), + .input => |in| ps.gpa.free(in.msg), + .gone, .check => {}, + } + return; + }; + ps.mail.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): joined at the next step boundary. Any thread. + pub fn handed(ps: *Peers, fd: c_int) void { + ps.post(.{ .join = fd }); + } + + /// 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 (tape.taken(&ps.t).len == 0) return; + tape.recordDigest(&ps.t); + const bytes = tape.taken(&ps.t); + unitHeader(&ps.head, origin, seq); + if (std.c.getenv("PARDES_REPLICA_TRACE") != null) { + var w: std.Io.Writer.Allocating = .init(ps.gpa); + defer w.deinit(); + w.writer.print("session: unit {d}/{d}, {d} bytes, to {d} clients:\n", .{ origin, seq, bytes.len, ps.count() }) catch {}; + tape.describe(bytes, &w.writer); + std.debug.print("{s}", .{w.written()}); + } + 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 }); + tape.clearTaken(&ps.t); + } + + /// 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) ps.boundary_ns.append(ps.gpa, nowNs() - t0) catch {}; + if (ps.recording) { + ps.seq += 1; + ps.unit(0, ps.seq); + } + 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 => |fd| ps.join(core, fd), + .input => |in| { + ps.input(core, in.peer, in.msg); + ps.gpa.free(in.msg); + }, + .gone => |peer| ps.leave(core, 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 ps.input_ns.append(ps.gpa, nowNs() - t0) catch {}; + core.toView(peer.view); + core.update(in.event); + core.step(); + ps.unit(peer.id, in.seq); + } + + fn refuse(fd: c_int, why: []const u8) void { + var out: [600]u8 = undefined; + if (wire.encodeServer(&out, .{ .refused = why })) |bytes| _ = writeAll(fd, bytes) else |_| {} + _ = libc.close(fd); + } + + fn join(ps: *Peers, core: *Pardes, fd: c_int) void { + const t0 = nowNs(); + defer ps.join_ns = @max(ps.join_ns, nowNs() - t0); + const slot = for (&ps.list) |*s| { + if (s.* == null) break s; + } else return refuse(fd, "that session has as many clients as it takes"); + if (!ps.recording) { + // A session recording to a file (PARDES_RECORD) has its tape. + if (tape.on != null) return refuse(fd, "that session records a tape of its own (PARDES_RECORD)"); + ps.t = tape.memory(ps.gpa); + ps.t.unit_digests = true; + tape.on = &ps.t; + ps.recording = true; + } + // Someone looks at it now: its messages go by keys, not the clock. + core.unattended = false; + const k = core.openView() orelse { + core.unattended = ps.count() == 0 and ps.alone_unattended; + if (ps.count() == 0) ps.stop(core); + return refuse(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); + const bytes = pardes.dump.dumpBytes(core) catch |err| { + 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 refuse(fd, std.fmt.bufPrint(&why, "that session could not be dumped: {t}", .{err}) catch "that session could not be dumped"); + }; + // The core's, as a Dump's is. + defer core.gpa.free(bytes); + ps.dump_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 return refuse(fd, "out of memory"); + if (std.c.getenv("PARDES_REPLICA_LAG_KB")) |kb| lag_max = (std.fmt.parseInt(usize, std.mem.span(kb), 10) catch 64 << 10) << 10; + peer.* = .{ .peers = ps, .fd = fd, .id = ps.next_id, .view = k, .limit = bytes.len + lag_max }; + ps.next_id +%= 1; + if (ps.next_id == 0) ps.next_id = 1; + var out: [wire.hello_max + 64]u8 = undefined; + // The session's digest as dumped, for the joiner to check its load by. + const digest = tape.digestOf(core); + const welcome = wire.encodeServer(&out, .{ .welcome = .{ .pid = @intCast(libc.getpid()), .session = ps.name[0..@min(ps.name.len, wire.max_name)], .client = peer.id, .view = k, .dump_len = bytes.len, .gmtoff = gmtoff(), .digest = digest } }) catch unreachable; + peer.send(&.{welcome}); + 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); + peer.send(&.{ &head, piece }); + } + 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; + log.info("client {d} joined '{s}' as view {d}: a {d} KB dump in {d} us", .{ peer.id, ps.name, k, 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 }); + core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view)); + core.closeView(peer.view); + if (ps.count() == 0) core.unattended = ps.alone_unattended; + ps.seq += 1; + ps.unit(0, ps.seq); + ps.letGo(peer); + if (ps.count() == 0) ps.stop(core); + } + + /// 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(); + _ = libc.close(peer.fd); + peer.out.deinit(ps.gpa); + peer.sending.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 => |in| in.peer == peer, + .gone, .check => |p| p == peer, + .join => false, + }; + if (!mine) { + i += 1; + continue; + } + if (ps.inbox.items[i] == .input) ps.gpa.free(ps.inbox.items[i].input.msg); + _ = ps.inbox.orderedRemove(i); + } + ps.lock.unlock(io); + ps.gpa.destroy(peer); + } + + /// 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; + 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 { + for (&ps.list) |*slot| if (slot.*) |peer| { + slot.* = null; + core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view)); + core.closeView(peer.view); + ps.letGo(peer); + }; + ps.stop(core); + ps.lock.lockUncancelable(ps.io); + for (ps.inbox.items) |note| switch (note) { + .join => |fd| _ = libc.close(fd), + .input => |in| ps.gpa.free(in.msg), + .gone, .check => {}, + }; + ps.inbox.clearRetainingCapacity(); + ps.lock.unlock(ps.io); + core.unattended = ps.alone_unattended; + } + + 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); + } + + 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} 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.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); + _ = 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, + /// What was read past the dump: the stream's (`readStream`). + rest: Framer = .{}, +}; + +/// Connected to the replica socket at `path`, its hello said for +/// `session`, welcomed, and the dump read whole: what a client joins with. +/// Errors are said in `why`. +pub fn dial(gpa: Allocator, path: []const u8, session: []const u8, why: []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}); + } + var out: [wire.hello_max]u8 = undefined; + const hello = wire.encodeClient(&out, .{ .hello = .{ .session = session } }) catch { + _ = libc.close(fd); + return said(why, "that session's name is too long", .{}); + }; + var j: Joined = .{ .fd = fd, .pid = 0, .client = 0, .view = 0, .gmtoff = 0, .dump = &.{} }; + const Read = struct { + j: *Joined, + gpa: Allocator, + why: []u8, + got: usize = 0, + failed: ?[]const u8 = null, + welcomed: bool = false, + fn take(r: *@This(), tag: u8, payload: []const u8) bool { + const msg = wire.decodeServer(tag, payload) catch { + r.failed = "the session's answer was not understood"; + return false; + }; + 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.dump = r.gpa.alloc(u8, @intCast(w.dump_len)) catch { + r.failed = "out of memory for the session's dump"; + return false; + }; + r.welcomed = true; + return w.dump_len > 0; + }, + .refused => |words| { + r.failed = std.fmt.bufPrint(r.why, "{s}\x00", .{words}) catch "refused"; + return false; + }, + .dump => |piece| { + if (!r.welcomed or r.got + piece.len > r.j.dump.len) { + r.failed = "the session sent more dump than it said"; + return false; + } + @memcpy(r.j.dump[r.got..][0..piece.len], piece); + r.got += piece.len; + return r.got < r.j.dump.len; + }, + else => { + r.failed = "the session sent a unit before its dump"; + return false; + }, + } + } + }; + if (!writeAll(fd, hello)) { + _ = libc.close(fd); + return said(why, "the session hung up", .{}); + } + var r: Read = .{ .j = &j, .gpa = gpa, .why = why }; + j.rest.run(gpa, fd, &r, Read.take); + if (r.failed != null or !r.welcomed or r.got < j.dump.len) { + j.rest.deinit(gpa); + if (j.dump.len > 0) gpa.free(j.dump); + _ = 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; +} + +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..]); +} + +/// 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. +pub fn bye(fd: c_int) void { + var out: [16]u8 = undefined; + if (wire.encodeClient(&out, .bye)) |b| _ = writeAll(fd, b) else |_| {} +} + +/// What follows the dump on the client's connection, each message handed +/// to `take` (a unit, a terminal's delta) until the link ends; `rest` is +/// what the dump's reader read past it. +pub fn readStream(gpa: Allocator, fd: c_int, rest: *Framer, ctx: anytype, comptime take: fn (@TypeOf(ctx), u8, []const u8) bool) void { + rest.run(gpa, fd, ctx, take); +} + pub fn digestOf(a: *Authority) u64 { const was = Stepping.begin(&a.t, null); defer was.end(); @@ -1078,3 +1886,36 @@ test "the authority's units with a big file open: its blobs hashed once (PARDES_ std.debug.print("{d} MB file, blobs {s}: a unit p50 {d} us p99 {d} us; a digest p50 {d} us p99 {d} us\n", .{ mb, if (on) "hashed once" else "hashed whole", us[0] / 1000, us[1] / 1000, ds[0] / 1000, ds[1] / 1000 }); } } + +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()); +} diff --git a/src/rollback.zig b/src/rollback.zig index 7216664c..b0394d22 100644 --- a/src/rollback.zig +++ b/src/rollback.zig @@ -40,11 +40,22 @@ const Pardes = pardes.Pardes; const blob_min = 4096; const checked = builtin.mode == .debug or builtin.mode == .safe; +/// An address as a hash map's key: a multiply, not a Wyhash of its bytes +/// (every allocation of a replica's core is a lookup). +const Addr = struct { + pub fn hash(_: Addr, a: usize) u64 { + return @as(u64, a) *% 0x9e3779b97f4a7c15; + } + pub fn eql(_: Addr, a: usize, b: usize) bool { + return a == b; + } +}; + pub const Epoch = struct { child: Allocator = std.heap.c_allocator, /// Its own bookkeeping, never the epoch's. meta: Allocator = std.heap.c_allocator, - live: std.AutoHashMapUnmanaged(usize, Entry) = .empty, + live: std.HashMapUnmanaged(usize, Entry, Addr, std.hash_map.default_max_load_percentage) = .empty, on: bool = false, id: u32 = 0, born: std.ArrayListUnmanaged(usize) = .empty, @@ -190,7 +201,7 @@ pub const Copy = struct { meta: Allocator = std.heap.c_allocator, regions: std.ArrayListUnmanaged(Region) = .empty, bytes: std.ArrayListUnmanaged(u8) = .empty, - seen: std.AutoHashMapUnmanaged(Seen, void) = .empty, + seen: std.HashMapUnmanaged(Seen, void, SeenContext, std.hash_map.default_max_load_percentage) = .empty, pins: std.ArrayListUnmanaged(Pin) = .empty, held: bool = false, epoch: ?*Epoch = null, @@ -202,6 +213,14 @@ pub const Copy = struct { const Region = struct { addr: usize, off: usize, len: usize }; const Pin = struct { addr: usize, len: usize, hash: u64, where: []const u8 }; const Seen = struct { addr: usize, ty: usize }; + const SeenContext = struct { + pub fn hash(_: SeenContext, k: Seen) u64 { + return (@as(u64, k.addr) *% 0x9e3779b97f4a7c15) ^ (@as(u64, k.ty) *% 0xc2b2ae3d27d4eb4f); + } + pub fn eql(_: SeenContext, a: Seen, b: Seen) bool { + return a.addr == b.addr and a.ty == b.ty; + } + }; pub const Error = Allocator.Error; pub fn deinit(c: *Copy) void { diff --git a/src/tape.zig b/src/tape.zig index c2ddbb9d..b30096f5 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -167,6 +167,9 @@ pub const Tape = struct { /// output carries one, at the pty's read rate), not one a field; what /// differs is then not named. whole_digest: bool = false, + /// A digest only when asked (`recordDigest`): the replica stream's, + /// one a unit, not one after each of its inputs. + unit_digests: bool = false, /// A replica client's guess of the session's clocks for a speculation /// (src/replica.zig): its clock reads (a message's stamp, a file's /// mtime) answer this instead of the clocks here. @@ -275,7 +278,7 @@ pub const Tape = struct { t.wrap(core); }; t.record(.done, void, &{}); - if ((t.depth == 0 or t.every) and !t.no_digest) if (t.core) |core| { + if ((t.depth == 0 or t.every) and !t.no_digest and !t.unit_digests) if (t.core) |core| { var parts: Parts = undefined; const whole = [1]u64{digestOf(core)}; const d: []const u64 = if (t.whole_digest) &whole else digestParts(core, &parts); @@ -802,6 +805,16 @@ fn guessed(comptime R: type, g: Guess) R { @compileError("tape: no guess of a clock read as " ++ @typeName(R)); } +/// The session's digest recorded now (a unit's end: the replica stream's +/// one, `unit_digests`). +pub fn recordDigest(t: *Tape) void { + const core = t.core orelse return; + var parts: Parts = undefined; + const whole = [1]u64{digestOf(core)}; + const d: []const u64 = if (t.whole_digest) &whole else digestParts(core, &parts); + t.record(.digest, []const u64, &d); +} + /// A core made outside the tape (a replica client's, from a dump) is the /// one it replays into. pub fn adoptCore(t: *Tape, p: *Pardes) void { diff --git a/src/tty/tty.zig b/src/tty/tty.zig index 14dc8042..43994645 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -17,6 +17,8 @@ const panel_compositor = @import("panel_compositor.zig"); const daemon = @import("../daemon.zig"); const host_io = @import("../host_io.zig"); const upgrade = @import("../upgrade.zig"); +const replica = @import("../replica.zig"); +const wire = @import("../detached/wire.zig"); extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int) c_int; @@ -150,6 +152,15 @@ pub const Command = struct { search_rows: pardes.Stream.Batch, files_changed, fs_ready, + /// A replica's connection, handed over by the daemon (Peers). + replica: posix.fd_t, + /// A replica's reader posted: the step boundary takes it in. + replica_mail, + /// A replica client's: what the session streamed (replica.Client). + unit: []u8, + /// The session's digest, asked by PARDES_REPLICA_CHECK. + checked: u64, + link_gone, } = .nop; }; const Loop = vaxis.Loop(@TypeOf(Command.value)); @@ -838,14 +849,18 @@ fn terminalCellPixels(total_pixels: u16, cells: u16, fallback: u32) u32 { } fn updateCoreTerminalSize(core: *pardes.Pardes, cols: u16, rows: u16, pixel_w: u16, pixel_h: u16) void { - core.update(.{ .resize = .{ + core.update(.{ .resize = terminalSize(cols, rows, pixel_w, pixel_h) }); +} + +fn terminalSize(cols: u16, rows: u16, pixel_w: u16, pixel_h: u16) @FieldType(pardes.Event, "resize") { + return .{ .cols = cols, .rows = rows, .cell_pixels = .{ .w = @intCast(terminalCellPixels(pixel_w, cols, 8)), .h = @intCast(terminalCellPixels(pixel_h, rows, 16)), }, - } }); + }; } /// A terminal pane's image fragment (`native.exact`): its `src` into its @@ -1255,6 +1270,268 @@ pub fn run(init: std.process.Init, opts: pardes.Options, term: Term) !void { try localSession(init, opts, &screen, &loop, &paste_buf); } +/// A replica client's link to its session (`remote`). +const Remote = struct { + r: *replica.Replica, + fd: posix.fd_t, + 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 terminal's answer (FrameAck). + owed: bool = false, + /// PARDES_REPLICA_CHECK=: once quiet a while after a change, the + /// session's digest is asked, and whether this replica's agrees is + /// written there, a line a check. + check: ?[:0]const u8 = null, + changed: bool = false, + asked: bool = false, + /// How many of its own inputs the session recorded otherwise when it + /// last drew: a frame after one that writes to the terminal 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, + + /// PARDES_REPLICA_STATS=: how its inputs and the session's units + /// went, a line at its end (test/daemon.py, the gates). + fn writeStats(c: *Remote, path: []const u8) void { + const st = &c.r.stats; + var buf: [600]u8 = undefined; + const line = std.fmt.bufPrint(&buf, "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}\n", .{ + st.inputs, st.speculated, st.stopped, st.units, st.fast, + c.r.neutral, st.mispredicted, st.foreign, c.visible, c.r.rtt_ns / 1000, pct(st.spec_ns.items, 50) / 1000, + pct(st.spec_ns.items, 99) / 1000, pct(st.copy_ns.items, 50) / 1000, pct(st.copy_ns.items, 99) / 1000, pct(st.rollback_ns.items, 50) / 1000, pct(st.rollback_ns.items, 99) / 1000, + }) 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); + _ = libc.close(fd); + } + + fn pct(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 session's digest against this replica's, if it is quiet still. + fn checked(c: *Remote, theirs: u64) void { + c.asked = false; + const path = c.check orelse return; + if (!c.r.quiet()) return; + const mine = c.r.digest(); + 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; + 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); + } + + /// The client's own view drawn on its terminal, outside any step: the + /// replica's digest stays the session's (views.drawOne). What the host + /// writes in the core as it presents (a frame skipped, one owed) is put + /// back, as the frame statistics are this process's. + fn draw(c: *Remote, s: *Shell) void { + const core = c.r.core(); + const skipped = core.present_skipped; + const needs = core.needs_frame; + core.frames.mark_start = pardes.FrameStats.nowNs(); + core.frames.mark_update = core.frames.mark_start; + c.r.draw(.{ .ctx = s, .f = Shell.present }); + core.frames.mark_render = core.frames.mark_update; + if (c.r.stats.mispredicted > c.mispredicted) { + c.mispredicted = c.r.stats.mispredicted; + if (core.frames.bytes > 0) c.visible += 1; + } + c.owed = core.present_skipped; + core.frames.record(pardes.FrameStats.nowNs(), false, c.owed); + core.present_skipped = skipped; + core.needs_frame = needs; + c.dirty = false; + } +}; + +/// The session's stream, onto the loop: its units, and its end. +fn linkReader(gpa: std.mem.Allocator, fd: posix.fd_t, rest: *replica.Framer, loop: *Loop, done: *std.atomic.Value(bool)) void { + defer done.store(true, .release); + const Take = struct { + fn take(l: *Loop, tag: u8, payload: []const u8) bool { + const t = std.enums.fromInt(wire.ServerTag, tag) orelse return false; + switch (t) { + .unit => { + const bytes = std.heap.c_allocator.dupe(u8, payload) catch return false; + l.postEvent(.{ .unit = bytes }) catch std.heap.c_allocator.free(bytes); + }, + .check => { + const d = (wire.decodeServer(tag, payload) catch return false).check; + l.postEvent(.{ .checked = d }) catch {}; + }, + .refused => return false, + // ponytail: a terminal's delta (rterm's, src/screen_state.zig) + // applies here once terminals cross as screens. + .screen, .pong, .welcome, .dump => {}, + } + return true; + } + }; + replica.readStream(gpa, fd, rest, loop, Take.take); + loop.postEvent(.link_gone) catch {}; +} + +/// `pardes --attach=:` (and `:`, through +/// ssh, main.zig): this terminal a replica client of a session elsewhere +/// (docs/next-daemon.md, "Phase 5 part B notes"). Its own core, loaded +/// from the session's dump, applies its keys at once (src/replica.zig); +/// the session's units are the truth it rolls back to. Its own view of the +/// session is drawn here, as the session draws its terminals'. +pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, session: []const u8) !void { + const io = init.io; + const gpa = init.gpa; + var why: [600]u8 = @splat(0); + var joined = replica.dial(gpa, path, session, &why) orelse { + var said: [700]u8 = undefined; + std.Io.File.stderr().writeStreamingAll(io, std.fmt.bufPrint(&said, "pardes: --attach: {s}\n", .{std.mem.sliceTo(&why, 0)}) catch "pardes: --attach failed\n") catch {}; + std.process.exit(1); + }; + defer _ = libc.close(joined.fd); + defer joined.rest.deinit(gpa); + + const allocs = pardes.memory.init(gpa); + defer pardes.memory.deinit(); + pardes.image.start(io, allocs.image); + if (comptime pardes.pdf_enabled) pardes.pdf.start(allocs.pdf); + pardes.syntax.start(allocs.tree_sitter); + defer { + pardes.image.stop(); + if (comptime pardes.pdf_enabled) pardes.pdf.stop(); + pardes.syntax.stop(); + } + var o = opts; + o.image_allocator = allocs.image; + o.pdf_allocator = allocs.pdf; + const r = replica.Replica.initDump(gpa, joined.client, joined.view, joined.dump, o) catch |err| { + gpa.free(joined.dump); + var said: [200]u8 = undefined; + std.Io.File.stderr().writeStreamingAll(io, std.fmt.bufPrint(&said, "pardes: --attach: the session's dump would not load: {t}\n", .{err}) catch "pardes: --attach: the dump would not load\n") catch {}; + std.process.exit(1); + }; + gpa.free(joined.dump); + defer r.deinit(); + r.gmtoff = joined.gmtoff; + if (!r.loaded(joined.digest)) { + std.Io.File.stderr().writeStreamingAll(io, "pardes: --attach: the session's dump loaded as another session (a build's dump is not complete)\n") catch {}; + std.process.exit(1); + } + + var winch_set = posix.sigemptyset(); + posix.sigaddset(&winch_set, posix.SIG.WINCH); + if (comptime builtin.os.tag == .freebsd) + posix.sigaction(.WINCH, &.{ .handler = .{ .handler = keepSignal }, .mask = posix.sigemptyset(), .flags = 0 }, null); + posix.sigprocmask(posix.SIG.BLOCK, &winch_set, null); + var tty_buf: [0x10000]u8 = undefined; + var screen: Screen = .{ .io = io, .gpa = gpa, .env = init.environ_map, .buf = &tty_buf, .kitty = .init(gpa), .primary = true }; + defer screen.kitty.deinit(); + try screen.enter(try vaxis.Tty.init(io, &tty_buf)); + defer screen.leave(); + defer screen.glide.deinit(gpa); + screen.feed.view = joined.view; + var paste_buf: std.Io.Writer.Allocating = .init(gpa); + defer paste_buf.deinit(); + var loop: Loop = .init(io, &screen.tty, &screen.vx); + var frame_arena: std.heap.ArenaAllocator = .init(allocs.frame); + defer frame_arena.deinit(); + var link: Remote = .{ .r = r, .fd = joined.fd }; + if (init.environ_map.get("PARDES_REPLICA_CHECK")) |file| link.check = try gpa.dupeSentinel(u8, file, 0); + defer if (link.check) |file| gpa.free(file); + var sh: Shell = .{ + .io = io, + .gpa = gpa, + .lsp_gpa = allocs.lsp, + .core = r.core(), + .prompt_rcs = undefined, + .loop = &loop, + .frame = &frame_arena, + .paste_buf = &paste_buf, + .inotify_fd = -1, + .from = joined.view, + .remote = &link, + }; + sh.terms[joined.view] = &screen; + + try startInput(&loop, &screen); + defer stopInput(io, &screen.thread); + (try std.Thread.spawn(.{}, winchWatch, .{&loop})).detach(); + var reader_done: std.atomic.Value(bool) = .init(false); + const reader = try std.Thread.spawn(.{}, linkReader, .{ gpa, joined.fd, &joined.rest, &loop, &reader_done }); + if (link.check != null) (try std.Thread.spawn(.{}, wakeWatch, .{ io, &loop })).detach(); + try screen.vx.queryTerminalSend(screen.tty.writer()); + // Its size is its view's first input. + sh.winched(); + while (!link.quit) { + if (link.dirty) link.draw(&sh); + const first = (loop.tryEvent() catch null) orelse idle: { + // Idle, its frame drawn: the next key needs no copy taken. + r.ready(); + break :idle loop.nextEvent() catch break; + }; + var ev = first; + var batch: usize = 1; + while (true) { + _ = sh.apply(ev); + if (link.quit or (!sh.in_paste and batch >= 64)) break; + ev = (loop.tryEvent() catch null) orelse break; + batch += 1; + } + r.settle(); + if (screen.caps_pending and screen.vx.queries_done.load(.unordered)) { + screen.caps_pending = false; + screen.vx.enableDetectedFeatures(screen.tty.writer()) catch {}; + screen.vx.queueRefresh(); + link.dirty = true; + } + if (link.owed) link.dirty = true; + if (link.check != null) { + // Quiet a while after a change: asked once (a wake, then the + // next quiet one). + if (first == .wake and link.changed and !link.asked and r.quiet()) { + link.changed = false; + link.asked = replica.askCheck(joined.fd); + } else if (link.changed and r.quiet()) requestWake(io, 300); + } + if (r.diverged) |reason| { + screen.leave(); + var said: [700]u8 = undefined; + std.Io.File.stderr().writeStreamingAll(io, std.fmt.bufPrint(&said, "pardes: --attach: this replica diverged from '{s}': {s}\n", .{ session, reason }) catch "pardes: --attach: diverged\n") catch {}; + std.process.exit(1); + } + } + if (init.environ_map.get("PARDES_REPLICA_STATS")) |file| link.writeStats(file); + if (!link.gone) replica.bye(joined.fd); + // The link's reader ends with the link, posting as it goes: what it + // and the terminal posted is let go until it has. + _ = libc.shutdown(joined.fd, 2); + while (true) { + while (loop.tryEvent() catch null) |ev| switch (ev) { + .unit => |b| std.heap.c_allocator.free(b), + .paste => |b| gpa.free(@constCast(b)), + else => {}, + }; + if (reader_done.load(.acquire)) break; + std.Thread.yield() catch {}; + } + reader.join(); + if (link.gone) { + screen.leave(); + var said: [300]u8 = undefined; + std.Io.File.stderr().writeStreamingAll(io, std.fmt.bufPrint(&said, "pardes: --attach: '{s}' let this client go (a Detach in it, or the session's end)\n", .{session}) catch "pardes: --attach: the session let this client go\n") catch {}; + } +} + /// A terminal a session draws on, while it has one: entered (raw, the /// alternate screen, mouse and paste reports) and left (all of it put back, /// the terminal closed) at the session's start and end, and at a detach and @@ -1462,6 +1739,10 @@ fn localSession( .test_clock = host_io.testClock(), }; sh.terms[0] = screen; + sh.peers = .{ .gpa = gpa, .io = io, .name = daemon.name(), .wake_ctx = loop, .wake = wakeForReplica }; + defer sh.peers.deinit(); + // Before the core goes: each client sees its link end. + defer sh.peers.dropAll(sh.core); pardes.lsp.setStatusSink(&sh, lspStatusSink); host_io.setExitWake(loop, wakeForExit); defer screen.glide.deinit(gpa); @@ -1524,7 +1805,7 @@ fn localSession( // An upgraded session's terminals, its own again. if (comptime !builtin.is_test) if (upgrade.resumed) |*rec| sh.resumeTerminals(rec, screen); // Nobody to see it, and nobody to size it: `size` sets it (ctl.zig). - core.unattended = !sh.attached(); + sh.setAttended(sh.attached()); while (core.nextEffect()) |effect| core.perform(effect); // ...and its shells, once a fresh session's own has a directory to // adopt them in. @@ -1564,6 +1845,8 @@ fn localSession( while (!core.quit) { pardes.turn.bump(&pardes.turn.restores); try core.pump(host); + // Its unit to each replica client, and theirs taken in. + sh.peers.boundary(core); upgrade.locate(); // Asked in the step (the word), done between steps: the session whole. if (sh.upgrade_asked) |pane| sh.upgradeNow(pane); @@ -1572,6 +1855,9 @@ fn localSession( // off a startup's way. daemon.watch(loop, deliverNote); if (core.takeRestore()) |rp| blk: { + // ponytail: a Restore replaces the core its clients replicate; + // they are let go, and attach again. + sh.peers.dropAll(core); const bytes = filesystem.readRestore(gpa, rp, core.settings.dump_dir.get()) catch |err| { core.reportError(core.active, "Restore", err); break :blk; @@ -1620,6 +1906,7 @@ fn localSession( if (live.on) clearNativeImages(&live.kitty, &live.vx, &live.tty); nc.native_images = live.on and live.vx.caps.kitty_graphics; nc.unattended = !live.on; + sh.peers.alone_unattended = !live.on; if (fs) |f| f.reset(nc); core.deinit(); core = nc; @@ -1664,9 +1951,21 @@ fn deliverNote(ctx: *anyopaque, note: daemon.Note) void { }; loop.postEvent(.{ .adopted = .{ .pid = 0, .fd = -1 } }) catch {}; }, + .replica => |fd| loop.postEvent(.{ .replica = fd }) catch { + _ = libc.close(fd); + }, } } +/// A replica reader's post: the session's wait ends, and its step +/// boundary takes the post in. +fn wakeForReplica(ctx: ?*anyopaque) void { + const loop: *Loop = @ptrCast(@alignCast(ctx.?)); + // Never waits: a full queue wakes the loop anyway, and a reader the + // loop is joining (a client let go) must not wait on it. + _ = loop.tryPostEvent(.replica_mail) catch {}; +} + const Shell = struct { io: std.Io, gpa: std.mem.Allocator, @@ -1718,9 +2017,42 @@ const Shell = struct { upgrade_asked: ?u8 = null, /// Its pty readers stopped for it: their end of file is not a shell's. upgrading: bool = false, + /// The session's replica clients (src/replica.zig), on sockets. + peers: replica.Peers = .{}, + /// This wait took only a replica's post (its input, its join): the + /// pump after it reads no new time, so it moves nothing the replicas' + /// speculations stand on (a replica takes such a unit as is). + mail_only: bool = false, + waited: bool = false, + /// This process a replica client of a session elsewhere (`remote`): + /// its inputs go to the replica, never straight to the core. + remote: ?*Remote = null, const Leaving = enum { detach, exit }; + /// A terminal's input, to the core, or to a replica client's engine + /// (applied at once there, and sent to the session). + fn feed(s: *Shell, ev: pardes.Event) void { + const c = s.remote orelse return s.core.update(ev); + const msg = c.r.input(ev) catch return; + if (!replica.sendInput(c.fd, msg)) c.quit = true; + c.dirty = true; + } + + /// Whether a terminal of this process's shows the session, or + /// nothing (a test) can. + fn local(s: *const Shell) bool { + for (s.terms) |slot| if (slot != null) return true; + return false; + } + + /// Whether anyone looks at the session: a terminal here, or a replica + /// client (Peers keeps it attended while one is there). + fn setAttended(s: *Shell, on: bool) void { + s.peers.alone_unattended = !on; + s.core.unattended = !on and s.peers.count() == 0; + } + fn of(ctx: ?*anyopaque) *Shell { return @ptrCast(@alignCast(ctx.?)); } @@ -1770,6 +2102,10 @@ const Shell = struct { fn clockNow(ctx: ?*anyopaque) u64 { const s = of(ctx); + if (s.waited) { + s.waited = false; + if (s.mail_only and s.peers.count() > 0 and s.core.clock_started) return s.core.now_ns; + } return s.test_clock orelse @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); } @@ -1783,15 +2119,23 @@ const Shell = struct { return screen.on; } - /// Drawn only while a terminal shows it. + /// Drawn only while a terminal shows it: a replica client's view live + /// (its input the last), whichever terminal here shows another. fn watched(ctx: ?*anyopaque) bool { - return of(ctx).attached(); + const s = of(ctx); + if (s.term() == null and s.local()) { + for (s.terms) |slot| if (slot) |sc| if (sc.on) return true; + return false; + } + return s.attached(); } /// Detach: the terminal put back and let go, the stub told (it ends, /// and the shell has its terminal again), the session left running. fn detach(ctx: ?*anyopaque) void { const s = of(ctx); + // A replica client's: its link ends. + if (s.peers.detach(s.core.view)) return; if (!s.attached()) return s.core.reportFailure(s.core.active, "Detach: no terminal is attached"); // One of several: its view goes, and the session stays on the rest. if (pardes.views.count(s.core) > 1) return s.dropView(s.core.view, .detach); @@ -1835,7 +2179,7 @@ const Shell = struct { stopInput(s.io, &screen.thread); screen.leave(); s.core.native_images = false; - s.core.unattended = true; + s.setAttended(false); // The stub's stdin, stdout and stderr were its terminal too. const null_fd = libc.open("/dev/null", .{ .ACCMODE = .RDWR, .CLOEXEC = true }, @as(libc.mode_t, 0)); if (null_fd >= 0) { @@ -1883,7 +2227,7 @@ const Shell = struct { s.terms[k] = screen; } screen.setStub(stub); - s.core.unattended = false; + s.setAttended(true); startInput(s.loop, screen) catch {}; screen.vx.queryTerminalSend(screen.tty.writer()) catch {}; s.core.needs_frame = true; @@ -2124,6 +2468,8 @@ const Shell = struct { } s.input_seen = false; s.output_seen = false; + s.mail_only = true; + defer s.waited = true; // Paced, what is not an input (a frame's ack, a pty's output) is // taken in and the wait goes on to the tick: the acks alone ran the // loop at the terminal's round trip. @@ -2215,15 +2561,16 @@ const Shell = struct { else => {}, } if (s.input_seen) s.input_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); + if (event != .replica_mail) s.mail_only = false; // A terminal's input goes into its view; a closed one's last is // dropped. switch (event) { .key_press, .mouse, .paste, .paste_start, .paste_end, .winsize, .focus_in, .focus_out, .quit => { - if (s.terms[s.from] == null and s.term() != null) { + if (s.terms[s.from] == null and s.local()) { if (event == .paste) s.gpa.free(@constCast(event.paste)); return false; } - if (s.term() != null) core.toView(s.from); + if (s.local() and s.remote == null) core.toView(s.from); }, else => {}, } @@ -2242,6 +2589,10 @@ const Shell = struct { virtual.* = @max(virtual.*, s.wake_to); }, .quit => { + if (s.remote) |c| { + c.quit = true; + return true; + } // One of several: its view goes with it. if (pardes.views.count(core) > 1) { s.dropView(core.view, .detach); @@ -2261,15 +2612,37 @@ const Shell = struct { }, .attach => |a| s.attachTerminal(a.tty, a.stub, null), .adopted => |a| s.adoptShell(a.pid, a.fd), + .replica => |fd| { + s.peers.handed(fd); + s.input_seen = true; + }, + .replica_mail => s.input_seen = true, + .unit => |bytes| { + if (s.remote) |c| { + c.r.confirm(bytes); + if (c.r.moved) c.dirty = true; + c.r.moved = false; + c.changed = true; + } + std.heap.c_allocator.free(bytes); + }, + .checked => |d| if (s.remote) |c| c.checked(d), + .link_gone => if (s.remote) |c| { + c.quit = true; + c.gone = true; + return true; + }, .focus_in => { - core.update(.{ .focus = true }); - upgrade.noticeNewer(core); + s.feed(.{ .focus = true }); + // A client's core is the session's: the notice is the session's to give. + // ponytail: not while clients replicate it (set outside a step). + if (s.remote == null and !s.peers.recording) upgrade.noticeNewer(core); }, .focus_out => { - core.update(.pointer_leave); - core.update(.{ .focus = false }); + s.feed(.pointer_leave); + s.feed(.{ .focus = false }); }, - .winsize => |ws| if (s.attached()) s.resized(ws), + .winsize => |ws| if (s.attached()) s.resized(s.terms[s.from], ws), .pty_read => |pr| { tookRead(s.io, pr.id); s.output_seen = true; @@ -2306,10 +2679,10 @@ const Shell = struct { const bytes = pasteBytes(key); const room = max_paste_bytes -| s.paste_buf.written().len; s.paste_buf.writer.writeAll(bytes[0..@min(bytes.len, room)]) catch {}; - } else core.update(vaxis_input.keyEvent(key)), + } else s.feed(vaxis_input.keyEvent(key)), .mouse => |m| { const pdf_before = nativePdfWheelTarget(core, m); - if (mouseEvent(m)) |ev| core.update(ev); + if (mouseEvent(m)) |ev| s.feed(ev); if (pdf_before) |before| { if (core.panes[before.pane]) |pane| { if (pane.pdfPage()) |page| { @@ -2319,7 +2692,7 @@ const Shell = struct { } }, .paste => |bytes| { - core.update(.{ .paste = bytes }); + s.feed(.{ .paste = bytes }); s.gpa.free(@constCast(bytes)); }, .paste_start => { @@ -2329,7 +2702,7 @@ const Shell = struct { .paste_end => { s.in_paste = false; const pasted = s.paste_buf.written(); - if (pasted.len > 0) core.update(.{ .paste = pasted }); + if (pasted.len > 0) s.feed(.{ .paste = pasted }); s.paste_buf.clearRetainingCapacity(); }, .files_changed => s.check_files = true, @@ -2366,17 +2739,13 @@ const Shell = struct { return false; } - /// The live view's terminal is `ws` big. - fn resized(s: *Shell, ws: vaxis.Winsize) void { - const core = s.core; - if (s.term()) |sc| { + /// Terminal `screen` (the live view's) is `ws` big. + fn resized(s: *Shell, screen: ?*Screen, ws: vaxis.Winsize) void { + if (screen) |sc| { sc.size = ws; sc.vx.resize(s.gpa, sc.tty.writer(), ws) catch {}; } - if (comptime pardes.pdf_enabled) - updateCoreTerminalSize(core, ws.cols, ws.rows, ws.x_pixel, ws.y_pixel) - else - core.update(.{ .resize = .{ .cols = ws.cols, .rows = ws.rows } }); + s.feed(.{ .resize = if (comptime pardes.pdf_enabled) terminalSize(ws.cols, ws.rows, ws.x_pixel, ws.y_pixel) else .{ .cols = ws.cols, .rows = ws.rows } }); } /// A SIGWINCH: each terminal whose size it changed is resized, in its @@ -2387,8 +2756,8 @@ const Shell = struct { if (!sc.on or sc.vx.state.in_band_resize) continue; const ws = sc.tty.getWinsize() catch continue; if (sc.size) |was| if (std.meta.eql(was, ws)) continue; - s.core.toView(@intCast(k)); - s.resized(ws); + if (s.remote == null) s.core.toView(@intCast(k)); + s.resized(sc, ws); } } diff --git a/test/daemon.py b/test/daemon.py index ce19b502..b54b2b54 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -7,8 +7,12 @@ private HOME and runtime directory: - a session killed outright: its shells live on, held by the daemon, and another session adopts them as terminal panes; - a replica's hello on the daemon's stream socket (phase 5): one of this - build handed to its session, which answers it and its pings; another - build's refused, and a session nobody has; + build handed to its session, which welcomes it, sends its dump and + answers its pings; another build's refused, and a session nobody has; +- replica clients (`pardes --attach=:`, phase 5 part B) of a + detached session, one through the delay relay: each one's keys in the + session's text, every client's digest the session's once quiet, one + killed outright while the other types on; - nothing left behind: no session, shell, daemon or socket. python3 -B test/daemon.py [cycles] @@ -17,6 +21,8 @@ private HOME and runtime directory: python3 -B test/daemon.py --ping [n] a hello of 's build and n pings on a replica socket (one `ssh -L` forwards, say), timed + python3 -B test/daemon.py --clients + the replica clients alone """ import fcntl import os @@ -170,7 +176,7 @@ def semantics(binary): class Replica: """A replica's connection (src/detached/wire.zig): length-prefixed messages, a tag byte and a little-endian u32 length.""" - WIRE = 14 + WIRE = 15 def __init__(self, path, timeout=10): self.s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) @@ -194,22 +200,34 @@ class Replica: return tag, self.take(n) def hello(self, session, build): - """('welcome', pid, name) or ('refused', why).""" + """('welcome', pid, name) or ('refused', why); a welcome's dump + is read, whole.""" name = session.encode() self.send(0x01, struct.pack(' after + + +def clients(binary): + """Replica clients of a detached session: one on the replica socket, + one through the delay relay; each one's keys in the session's text and + every client's digest the session's once quiet; one killed outright + while the other types on; nothing left behind.""" + scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn' + scratch.mkdir(parents=True, exist_ok=True) + root = Path(tempfile.mkdtemp(prefix='rc-', dir=scratch)) + env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_') and 'PAGER' not in k} + env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), XDG_CONFIG_HOME=str(root / 'config'), + TERM='xterm-256color', SHELL=shutil.which('bash') or '/bin/bash', PARDES_NOTIME='1') + (root / 'config' / 'pardes').mkdir(parents=True) + (root / 'config' / 'pardes' / 'init').write_text('Shell bash\nMessageLinger 0\n') + notes = root / 'notes.txt' + notes.write_text(''.join(f'line {i} of the notes\n' for i in range(300))) + started = [] + terms = [] + try: + sess = subprocess.Popen([binary, '--detach=files', str(notes)], env=env, cwd=root, stdin=subprocess.DEVNULL, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True) + started.append(sess.pid) + sock = root / 'pardes-replica.sock' + wait_for('the replica socket', sock.exists) + wait_for('the session\'s 9P socket', (root / 'pardes-9p-files.sock').exists) + nine = Client(root / 'pardes-9p-files.sock') + pane = next(int(row.split()[0]) for row in nine.read('/index').splitlines() if row.split()[1] == b'text') + alone = fds(sess.pid) + relay = subprocess.Popen([sys.executable, '-B', str(Path(__file__).parent / 'delay_relay.py'), + str(root / 'relay.sock'), str(sock), '--delay', '40'], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + started.append(relay.pid) + wait_for('the relay', (root / 'relay.sock').exists) + ta, tb = Term(30, 100), Term(24, 80) + terms += [ta, tb] + a = ta.spawn([binary, f'--attach={sock}:files'], dict(env, PARDES_REPLICA_CHECK=str(root / 'a.check'))) + b = tb.spawn([binary, f'--attach={root / "relay.sock"}:files'], dict(env, PARDES_REPLICA_CHECK=str(root / 'b.check'))) + started += [a.pid, b.pid] + for t in (ta, tb): + wait_for('a client\'s first frame', lambda: b'line 3 of the notes' in t.text(), timeout=20) + os.write(ta.master, b'Ofrom-A\x1b') + os.write(tb.master, b'jjofrom-B\x1b') + wait_for('both clients\' keys in the session\'s text', lambda: b'from-A' in nine.read(f'/pane/{pane}/body') + and b'from-B' in nine.read(f'/pane/{pane}/body')) + wait_for('client A agrees once quiet', lambda: agreed(root / 'a.check', 0)) + wait_for('client B agrees once quiet', lambda: agreed(root / 'b.check', 0)) + # Killed outright: the session and the other client carry on. + os.kill(b.pid, signal.SIGKILL) + b.wait(timeout=10) + seen = len(checks(root / 'a.check')) + os.write(ta.master, b'jjjAagain-A\x1b') + wait_for('client A\'s keys after B went', lambda: b'again-A' in nine.read(f'/pane/{pane}/body')) + wait_for('client A agrees again', lambda: agreed(root / 'a.check', seen)) + assert sess.poll() is None + # Detach typed in it: it alone goes. + os.write(ta.master, b' sD') + wait_for('client A let go', lambda: a.poll() is not None, timeout=10) + assert b'let this client go' in ta.text(), ta.text()[-300:] + # A third client, a shell: its keys wait for the session (a pty + # write), its output comes back in units, the same on both sides. + nine.write(f'/pane/{pane}/exec', b'Tty\n') + shell = wait_for('a shell pane', lambda: next((int(row.split()[0]) for row in nine.read('/index').splitlines() + if row.split()[1] == b'term'), None)) + wait_for('the shell\'s prompt', lambda: b'$' in nine.read(f'/pane/{shell}/body') or b'#' in nine.read(f'/pane/{shell}/body')) + alone = fds(sess.pid) + tc = Term(30, 100) + terms.append(tc) + c = tc.spawn([binary, f'--attach={sock}:files'], dict(env, PARDES_REPLICA_CHECK=str(root / 'c.check'))) + started.append(c.pid) + wait_for('client C\'s first frame', lambda: b'$' in tc.text(), timeout=20) + run(nine, shell, b'echo from-the-session\n') + os.write(tc.master, b'echo from-C\r') + wait_for('client C\'s keys in the shell', lambda: b'from-C' in nine.read(f'/pane/{shell}/body')) + wait_for('client C agrees with a shell\'s output', lambda: agreed(root / 'c.check', 0)) + # Its terminal hung up: it dies, and the session carries on. + c.send_signal(signal.SIGHUP) + c.wait(timeout=10) + relay.kill() + relay.wait() + wait_for('the session\'s fds level', lambda: fds(sess.pid) == alone, timeout=10) + print(f'daemon: two replica clients (one through a 40 ms relay) typed into a detached session; ' + f'digests agreed {len(checks(root / "a.check"))} and {len(checks(root / "b.check"))} times; ' + f'one killed, the other typed on and detached; a third typed into a shell, agreed ' + f'{len(checks(root / "c.check"))} times, and was hung up') + try: + nine.write(f'/pane/{pane}/exec', b'Exit\n') + except OSError as e: + # An edited file asks before it goes. + assert 'Exit again' in str(e), e + nine.write(f'/pane/{pane}/exec', b'Exit\n') + sess.wait(timeout=10) + nine.socket.close() + wait_for('nothing left', lambda: not [p for p in processes(root) if alive(p)], timeout=10) + print('daemon: replica clients\' session ended, nothing left behind') + finally: + for t in terms: + t.close() + for pid in started + processes(root): + try: + os.kill(pid, signal.SIGKILL) + except OSError: + pass + shutil.rmtree(root, ignore_errors=True) + + def terminals(client): return [int(row.split()[0]) for row in client.read('/index').splitlines() if row.split()[1] == b'term'] @@ -290,6 +428,7 @@ def main(binary, cycles): daemon = daemons[0] started.append(daemon) replicas(root, binary, session, daemon) + clients(binary) shell = terminals(client)[0] shell_pid = int(run(client, shell, b'echo $$\n').split(b'\n')[1]) started.append(shell_pid) @@ -856,6 +995,8 @@ if __name__ == '__main__': gui_upgrades(Path(sys.argv[3]).resolve(), Path(sys.argv[4]).resolve()) elif sys.argv[1:2] == ['--upgrade']: upgrades(Path(sys.argv[2]).resolve(), Path(sys.argv[3]).resolve()) + elif sys.argv[1:2] == ['--clients']: + clients(sys.argv[2]) elif sys.argv[1:2] == ['--ping']: ping(sys.argv[2], sys.argv[3], sys.argv[4], int(sys.argv[5]) if len(sys.argv) > 5 else 50) elif '--gui' in sys.argv: diff --git a/test/rollback_bench.zig b/test/rollback_bench.zig index 38fe011d..0f0c77a4 100644 --- a/test/rollback_bench.zig +++ b/test/rollback_bench.zig @@ -158,6 +158,24 @@ pub fn main(init: std.process.Init) !void { x.* = t1 - t0; y.* = nowNs() - t1; } + // A join (docs/next-daemon.md, Phase 5 part B notes): the session + // dumped at a step boundary, which is what the authority pays, and + // the joiner's load of it. + var dump: [5]u64 = undefined; + var load: [5]u64 = undefined; + var dump_len: usize = 0; + for (&dump, &load) |*x, *y| { + const t0 = nowNs(); + const bytes = try pardes.dump.dumpBytes(core); + const t1 = nowNs(); + dump_len = bytes.len; + const loaded = try pardes.dump.load(gpa, .{ .tty_only = true, .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa }, bytes, null); + y.* = nowNs() - t1; + x.* = t1 - t0; + loaded.deinit(); + core.gpa.free(bytes); + } + std.debug.print("{s:<8} join: dump {d:.1} MB in {d:.2} ms (the session's step), load {d:.2} ms (the joiner's)\n", .{ s.name, @as(f64, @floatFromInt(dump_len)) / 1e6, @as(f64, @floatFromInt(median(&dump))) * 1e-6, @as(f64, @floatFromInt(median(&load))) * 1e-6 }); var panes: usize = 0; for (core.panes) |slot| panes += @intFromBool(slot != null); const ms = 1e-6; -- cgit v1.3 From 5af775e9817395af39792cc609b832394d4a4fb8 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 15:15:01 -0300 Subject: replica: what only the authority holds, a step reads off the tape (tape.authority): the store's closed panes and Recent, a script's hold on a pane's events, the 9P socket, the session's config directory and startup file; a replica's own are never read, and never put back by a rollback The Panes picker's closed rows, Recent's rows, a closed pane brought back (a Look at @pN, a Panes row), a jump to a closed file and the +Jumps rows read `p.store` and `p.recent`, `.authority` state a replica keeps on its own: its store has no file, its Recent only what it replayed since it joined. The rows made of them went into digested state, so every client of a session diverged together after a Del (H4), every client that joined after one diverged at its first unit forever (H11: trackJump dropped a closed file's jumps at every step when Recent did not know the file), a client's Del of a file the session's Recent knew made the stat the session skipped (H12: noteClosed looked in Recent first), and a client on another machine read its own store (H13). - tape.authority: poison, then a value read: the authority's answer is taped, a replay takes it, a speculation waits for it. One site, `.authority`. - Readers through it: Query's Recent and closed-pane rows, Store.reopen's record, Pardes.recentByHash (jumps, +Jumps), mouse.reportRange's readers check (a script reading a pane's events takes a look or exec: `pane.fs.readers`, the 9P server's), Tty9p's socket check, Pardes.configDir and startupPath (ThemeFile, DumpThemes, Config, DumpConfig, an init file saved), DumpConfig's dump directory. - trackJump no longer reads Recent at every step: a closed file's jump stays; the jump to it drops that file's entries when Recent has forgotten it, as when the file is gone. - noteClosed asks the host (recentKeeps, a taped stat) before it looks in Recent: the same syscall on every side. - The tty toggle, read on every key, is session state: Pardes.tty_toggle, from the options when the core is made, dumped. - The rollback copy no longer saves `store` and `recent` (rollback.zig `unsaved`): their bytes put back pointed at lists the host's heap (recentGpa) had freed, which the epoch never holds. A speculated Del then a Panes listing read a freed name (H8, the segfault). Tests, each failing before: a rollback leaves the store and Recent as a speculation left them; a replica joined after a pane closed has the session's jump to it, its Panes and Recent rows, asked by either (and the variant hunt's Recent written by another window); a replica's Del of a file its session's Recent knows asks the host what the session asked. Co-Authored-By: Claude Opus 5.5 --- src/Output.zig | 10 ++++-- src/Query.zig | 21 +++++++----- src/Store.zig | 46 ++++++++++++++++++++----- src/builtins.zig | 4 +-- src/colors.zig | 5 +-- src/echo.zig | 2 +- src/exec.zig | 9 +++-- src/look.zig | 2 +- src/mouse.zig | 15 ++++++-- src/pardes.zig | 75 ++++++++++++++++++++++++++++++---------- src/replica.zig | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/rollback.zig | 51 ++++++++++++++++++++++++++-- src/tape.zig | 19 +++++++++++ 13 files changed, 311 insertions(+), 50 deletions(-) (limited to 'src/rollback.zig') diff --git a/src/Output.zig b/src/Output.zig index 359db1cf..7c8b7f59 100644 --- a/src/Output.zig +++ b/src/Output.zig @@ -1005,6 +1005,12 @@ test "full Help renders builtin rows, a call hierarchy guide, then language keys try std.testing.expect(std.mem.indexOf(u8, group, "Callstack: static caller chains") != null); } +/// Where a dump goes by default, as the session's environment says (the +/// authority's, `tape.authority`): in `buf`. +fn dumpDirectory(buf: []u8) []const u8 { + return pardes.dump.defaultDirectory(buf) orelse ""; +} + pub fn openConfig(p: *Pardes, id: usize) !void { var out: std.Io.Writer.Allocating = .init(p.gpa); errdefer out.deinit(); @@ -1015,8 +1021,8 @@ pub fn openConfig(p: *Pardes, id: usize) !void { try p.locations_config.write(&located.writer); try config.Runtime.writeReport(&out.writer, .{ .locations = located.written(), - .default_dump_dir = if (host_fs) (pardes.dump.defaultDirectory(&dir_buf) orelse "") else "", - .startup_config_path = p.opts.startup_config_path, + .default_dump_dir = if (host_fs) pardes.tape.authority(dumpDirectory, .{@as([]u8, &dir_buf)}) else "", + .startup_config_path = p.startupPath(), .platform = builtins.viewPlatform(p), .theme_name = p.theme().name, .theme_file = pardes.colors.loadedThemeFile(p), diff --git a/src/Query.zig b/src/Query.zig index 49f58a0e..2f417a68 100644 --- a/src/Query.zig +++ b/src/Query.zig @@ -48,6 +48,7 @@ const memory = @import("memory.zig"); const Names = @import("Names.zig"); const Places = @import("Places.zig"); const Stream = @import("Stream.zig"); +const Store = @import("Store.zig"); const Surface = @import("surface.zig").Surface; const Pardes = pardes.Pardes; const Pane = panes.Pane; @@ -591,12 +592,9 @@ fn startSource(p: *Pardes, id: usize, q: *Query, source: Source, owner_id: ?usiz if (p.fs.serving) Stream.step(p); }, .recent => { - // Every window's files: what the others wrote since, read. - p.store.refresh(p); var out: std.Io.Writer.Allocating = .init(gpa); defer out.deinit(); - Output.recentText(p, &out.writer, true) catch {}; - var lines = std.mem.splitScalar(u8, out.written(), '\n'); + var lines = std.mem.splitScalar(u8, pardes.tape.authority(recentRows, .{ p, &out }), '\n'); while (lines.next()) |ln| { if (ln.len == 0) continue; const rel = Names.relative(ln, dir); @@ -617,10 +615,9 @@ fn startSource(p: *Pardes, id: usize, q: *Query, source: Source, owner_id: ?usiz } // Then the panes this session closed, the latest first, each // `(closed)` and dimmer (makeNameRows): Enter brings one back - // where it was. Its name alone is matched, not the word. - var i = p.store.closed.items.len; - while (i > 0) : (i -= 1) { - const c = p.store.closed.items[i - 1]; + // where it was. Its name alone is matched, not the word. The + // store's, which a replica has from the authority. + for (pardes.tape.authority(Store.closedNames, .{p})) |c| { var buf: [4096 + 48]u8 = undefined; const row = std.fmt.bufPrint(&buf, config.pane_addr ++ "{d} {s}" ++ closed_word, .{ c.serial, Names.relative(c.name, dir) }) catch continue; const key_at = std.mem.indexOfScalar(u8, row, ' ').? + 1; @@ -652,6 +649,14 @@ fn startSource(p: *Pardes, id: usize, q: *Query, source: Source, owner_id: ?usiz /// What a Panes row of a closed pane ends with. const closed_word = " (closed)"; +/// Every window's files, as a Recent query lists them: what the others +/// wrote since, read first. The store's (`tape.authority`): in `out`. +fn recentRows(p: *Pardes, out: *std.Io.Writer.Allocating) []const u8 { + p.store.refresh(p); + Output.recentText(p, &out.writer, true) catch {}; + return out.written(); +} + /// The owner's declarations, from a parse that already exists: its sticky /// headers' (TreeContext) at this revision, or the locations cache's. None /// is made here, on the loop, unless `small` and the text is small: a diff --git a/src/Store.zig b/src/Store.zig index 36215234..9c726705 100644 --- a/src/Store.zig +++ b/src/Store.zig @@ -468,6 +468,21 @@ fn unlist(s: *Store, gpa: std.mem.Allocator, serial: u32) void { }; } +/// A closed pane as Panes lists it: its serial and what its row shows. +pub const Named = struct { serial: u32, name: []const u8 }; + +/// This session's closed panes, the latest first (scratch; the names the +/// store's): what a step lists of them (`tape.authority`). +pub fn closedNames(p: *Pardes) []const Named { + const items = p.store.closed.items; + const out = p.scratch.allocator().alloc(Named, items.len) catch return &.{}; + for (out, 0..) |*o, i| { + const c = items[items.len - 1 - i]; + o.* = .{ .serial = c.serial, .name = c.name }; + } + return out; +} + /// The closed pane with this serial, if this session has it. pub fn closedBySerial(s: *const Store, serial: u32) ?*Closed { for (s.closed.items) |*c| if (c.serial == serial) return c; @@ -785,12 +800,10 @@ fn compact(s: *Store, p: *Pardes, file: File, st: File.Stat, path: []const u8) v /// from `from`. False when it is not this session's, or its record is gone /// or bad; the keyboard on it otherwise. pub fn reopen(s: *Store, p: *Pardes, serial: u32, from: usize) bool { - s.refresh(p); const gpa = p.recentGpa(); - const place = (s.closedBySerial(serial) orelse return false).*; - const body = s.bodyOf(p, place) orelse return false; - // Read from the file: this call's to free (`unlist` frees one held). - defer if (place.body.len == 0) gpa.free(body); + // The store's: a replica has the authority's (`tape.authority`). + const place = pardes.tape.authority(record, .{ s, p, serial }) orelse return false; + const body = place.body; const m = parseMeta(body) orelse return false; const free = p.freeSlot() orelse { p.reportError(from, "Panes", error.NoPaneSlots); @@ -826,6 +839,21 @@ pub fn reopen(s: *Store, p: *Pardes, serial: u32, from: usize) bool { return true; } +/// Where a closed pane goes back, and its record's body (scratch). +pub const Record = struct { gainer: u32, below: bool, owner: u32, rows: u16, body: []const u8 }; + +/// Closed pane `serial`'s record, the store read on first; null when it is +/// not this session's, or its record is gone or bad. +fn record(s: *Store, p: *Pardes, serial: u32) ?Record { + s.refresh(p); + const c = (s.closedBySerial(serial) orelse return null).*; + const body = s.bodyOf(p, c) orelse return null; + // Read from the file: this call's to free (`unlist` frees one held). + defer if (c.body.len == 0) p.recentGpa().free(body); + const copy = p.scratch.allocator().dupe(u8, body) catch return null; + return .{ .gainer = c.gainer, .below = c.below, .owner = c.owner, .rows = c.rows, .body = copy }; +} + /// A closed pane's record body: held here, or read from the file and /// checked against its CRC (gpa; the caller frees it when it is not /// `c.body`). @@ -855,8 +883,9 @@ fn bodyOf(s: *Store, p: *Pardes, c: Closed) ?[]u8 { return body; } -/// `@p` naming a pane this session closed: its serial. -pub fn closedAddress(s: *const Store, word: []const u8) ?u32 { +/// `@p` alone, the pane and no place in it: its serial, for a pane +/// this session closed to come back by (`reopen` says whether it did). +pub fn closedAddress(word: []const u8) ?u32 { const addr = @import("config.zig").pane_addr; if (!std.mem.startsWith(u8, word, addr)) return null; const digits = word[addr.len..]; @@ -865,8 +894,7 @@ pub fn closedAddress(s: *const Store, word: []const u8) ?u32 { } else digits.len; // `@p7` alone, or `@p7:` with nothing after it: the pane, no place in it. if (end == 0 or (end < digits.len and !(digits[end] == ':' and end + 1 == digits.len))) return null; - const serial = std.fmt.parseInt(u32, digits[0..end], 10) catch return null; - return if (s.closedBySerial(serial) != null) serial else null; + return std.fmt.parseInt(u32, digits[0..end], 10) catch null; } // ---- tests ---- diff --git a/src/builtins.zig b/src/builtins.zig index 69ac3113..97cdc5a0 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -941,7 +941,7 @@ pub const DumpThemes = struct { pub const enabled = pardes.hosted; pub fn run(c: Ctx) void { if (comptime enabled) { - if (c.p.opts.config_dir == null) { + if (c.p.configDir() == null) { c.p.reportError(c.id, "dump themes", error.NoConfigDirectory); return; } @@ -1607,7 +1607,7 @@ pub const Help = struct { pub const Config = struct { pub const scope: Scope = .session; pub fn run(c: Ctx) void { - const path = c.p.opts.startup_config_path orelse + const path = c.p.startupPath() orelse return c.p.reportFailure(c.id, "Config: no startup file here: no configuration directory"); var real_buf: [4096]u8 = undefined; const real = if (pardes.filesystem.resolveOs(path, &real_buf)) |r| r.path else null; diff --git a/src/colors.zig b/src/colors.zig index c016434b..cc417ccb 100644 --- a/src/colors.zig +++ b/src/colors.zig @@ -677,14 +677,15 @@ pub fn requestThemeFile(p: *Pardes, id: usize, argument: []const u8) void { } const absolute = std.fs.path.isAbsolute(input); - if (!absolute and p.opts.config_dir == null) { + const config_dir = if (absolute) null else p.configDir(); + if (!absolute and config_dir == null) { p.reportError(id, "theme file", error.NoConfigDirectory); return; } var joined: [4095]u8 = undefined; const resolved = if (absolute) input else path: { - const dir = p.opts.config_dir.?; + const dir = config_dir.?; const separator = if (dir.len > 0 and dir[dir.len - 1] != std.Io.Dir.path.sep) std.Io.Dir.path.sep_str else ""; break :path std.fmt.bufPrint(&joined, "{s}{s}{s}", .{ dir, separator, input }) catch { p.reportError(id, "theme file", error.PathTooLong); diff --git a/src/echo.zig b/src/echo.zig index 4acd3d11..4560930c 100644 --- a/src/echo.zig +++ b/src/echo.zig @@ -500,7 +500,7 @@ fn forwarded(c: *const Pardes, ev: pardes.Event) bool { else => return false, }; if (k.cp == Key.escape) return false; - return !(k.ctrl and (k.cp == c.opts.tty_toggle or k.cp == 'v' or k.cp == 'V')); + return !(k.ctrl and (k.cp == c.tty_toggle or k.cp == 'v' or k.cp == 'V')); } fn blank(cp: u21) bool { diff --git a/src/exec.zig b/src/exec.zig index e1c13daa..c5218279 100644 --- a/src/exec.zig +++ b/src/exec.zig @@ -411,11 +411,16 @@ pub fn spawnTty(p: *Pardes, from: usize) ?*Pane { pub fn spawnV9fsTty(p: *Pardes, from: usize) void { if (comptime !hosted or @import("builtin").os.tag != .linux) return p.reportError(from, "Tty9p", error.LinuxRequired); - if (p.fs.socket_path.len == 0) + // The 9P server's: a replica has the authority's answer. + if (!pardes.tape.authority(servesSocket, .{p})) return p.reportError(from, "Tty9p", error.SessionSocketRequired); _ = spawnTtyWithMount(p, from, true); } +fn servesSocket(p: *Pardes) bool { + return p.fs.socket_path.len != 0; +} + fn spawnTtyWithMount(p: *Pardes, from: usize, v9fs: bool) ?*Pane { // No pane asking (an empty window's tag): the session's directory, as // New's scratch is. @@ -1009,7 +1014,7 @@ pub fn applyStartupConfig(p: *Pardes) void { /// setting lines: a command in it (`Newcol`) is the session's start, and /// would not be right twice. pub fn reapplyInitFile(p: *Pardes, path: []const u8, text: []const u8) void { - const init_path = p.opts.startup_config_path orelse return; + const init_path = p.startupPath() orelse return; if (!std.mem.eql(u8, init_path, path)) return; applyInitText(p, text, true); } diff --git a/src/look.zig b/src/look.zig index 226c96e3..09a22266 100644 --- a/src/look.zig +++ b/src/look.zig @@ -1816,7 +1816,7 @@ pub fn lookAt(p: *Pardes, id: usize, operand_raw: []const u8) void { // `@p` names no open pane: one this session closed comes // back where it was (Store.reopen), any other is a miss, said. if (t.id >= MAX_PANES or p.panes[t.id] == null) { - if (p.store.closedAddress(pl.path)) |serial| if (p.store.reopen(p, serial, id)) return; + if (@TypeOf(p.store).closedAddress(pl.path)) |serial| if (p.store.reopen(p, serial, id)) return; return missed(p, id, "no pane {s}", .{pl.path[0..@min(pl.path.len, 32)]}); } const target = p.panes[t.id].?; diff --git a/src/mouse.zig b/src/mouse.zig index 67ebf7cf..3281f48a 100644 --- a/src/mouse.zig +++ b/src/mouse.zig @@ -990,10 +990,17 @@ fn reportGesture( operand: exec.PointerOperand, chord_arg: ?[]const u8, ) bool { - if ((p.panes[id] orelse return false).fs.readers == 0) return false; return reportRange(p, id, cmd, text, on_tag, gestureRange(p, id, text, on_tag, operand), chord_arg); } +/// Whether a script reads pane `id`'s events, and so takes its looks and +/// execs: the 9P server's, the authority's alone (a replica has its +/// answer, `tape.authority`). +fn readsEvents(p: *Pardes, id: usize) bool { + const pane = p.panes[id] orelse return false; + return pane.fs.readers != 0; +} + /// An event reader told of a look or an exec of `text` at `range` of the /// tag or body, by a click or a key alike: false when the pane has none. pub fn reportRange( @@ -1006,7 +1013,7 @@ pub fn reportRange( chord_arg: ?[]const u8, ) bool { const scripted = p.panes[id] orelse return false; - if (scripted.fs.readers == 0) return false; + if (!pardes.tape.authority(readsEvents, .{ p, id })) return false; const is_look = cmd == config.look_cmd; const action: ctlfs.events.Action = if (is_look) (if (on_tag) .tag_look else .body_look) @@ -1020,7 +1027,9 @@ pub fn reportRange( if (is_look) { if (!named and std.mem.indexOfAny(u8, text, "/.:") != null) flag |= ctlfs.events.flag_filename; } else if (chord_arg != null) flag |= ctlfs.events.flag_chorded; - if (!ctlfs.events.noteAction(p, id, action, range.q0, range.q1, flag, text)) return false; + // Taken whether or not this process's server has the reader: the + // session's said it has (a replica's has none). + _ = ctlfs.events.noteAction(p, id, action, range.q0, range.q1, flag, text); // A chorded exec's argument follows as two records, its text and where // it came from (`file:#q0,#q1`, empty when that is not known), as acme // sends them (exec.c:182-188); the pane keeps it for the write-back. diff --git a/src/pardes.zig b/src/pardes.zig index f0d2cd0c..36ce6bdb 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -857,7 +857,7 @@ test "Togglettymode builtin and keyboard chord share mode transitions including try std.testing.expectEqual(Text.Mode.tty, pane.body.mode); try std.testing.expect(p.executeBuiltinLine(0, "Togglettymode")); try std.testing.expectEqual(Text.Mode.normal, pane.body.mode); - p.update(.{ .key = .{ .cp = p.opts.tty_toggle, .ctrl = true } }); + p.update(.{ .key = .{ .cp = p.tty_toggle, .ctrl = true } }); try std.testing.expectEqual(Text.Mode.tty, pane.body.mode); // The tag has its own mode: the body keeps tty while the tag is typed into. tagline.enterTag(p, pane); @@ -867,7 +867,7 @@ test "Togglettymode builtin and keyboard chord share mode transitions including try std.testing.expectEqual(Text.Mode.normal, pane.body.mode); try std.testing.expect(pane.focus == .body); tagline.enterTag(p, pane); - p.update(.{ .key = .{ .cp = p.opts.tty_toggle, .ctrl = true } }); + p.update(.{ .key = .{ .cp = p.tty_toggle, .ctrl = true } }); try std.testing.expectEqual(Text.Mode.tty, pane.body.mode); try std.testing.expect(pane.focus == .body); @@ -4732,6 +4732,10 @@ pub const Pardes = struct { /// The views no frontend here draws: a replica client's, which its own /// process draws (src/replica.zig). A pump skips them. undrawn: u8 = 0, + /// The Ctrl key into a terminal's tty mode and out (`--tty-toggle`): the + /// session's, taken from its options when it is made, so a dump carries + /// it to a replica, whose own options are its process's. + tty_toggle: u21 = config.tty_toggle_default, /// per-update scratch (paneCursorLines, selection text); reset each update scratch: std.heap.ArenaAllocator, /// The arena each frame's Surface text is built in, reset by `pump`. Hosts @@ -4797,6 +4801,7 @@ pub const Pardes = struct { .tree_sitter_gpa = opts.tree_sitter_allocator orelse gpa, .opts = opts, .windowed = opts.windowed, + .tty_toggle = opts.tty_toggle, .col_tags = @splat(.{ .what = .column, .gpa = gpa }), .global_tag = .{ .what = .workspace, .gpa = gpa }, .fs = .{}, @@ -4989,7 +4994,10 @@ pub const Pardes = struct { /// Recent keeps it. fn noteClosed(p: *Pardes, pane: *const Pane) void { const d = recentPlace(pane) orelse return; - if (p.recent.get(d.path) == null and !recentKeeps(d.path)) return; + // The host asked first, always: what Recent holds is the + // authority's, and a step that asked only when it did not know the + // file would ask on one side and not the other. + if (!recentKeeps(d.path) and p.recent.get(d.path) == null) return; p.recent.closedSel(p.recentGpa(), d.path, d.line, d.col, d.aline, d.acol); } @@ -5024,9 +5032,38 @@ pub const Pardes = struct { }; } - /// The recent entry a closed jump names, by its path's hash. - pub fn recentByHash(p: *const Pardes, hash: u64) ?recent_files.Entry { - for (p.recent.entries.items) |e| if (std.hash.Wyhash.hash(0, e.path) == hash) return e; + /// The session's configuration directory and startup file, as a step + /// reads them: its process's options, so the authority's + /// (`tape.authority`); a replica's are its own process's. + pub fn configDir(p: *Pardes) ?[]const u8 { + return tape.authority(optsConfigDir, .{p}); + } + + pub fn startupPath(p: *Pardes) ?[]const u8 { + return tape.authority(optsStartupPath, .{p}); + } + + fn optsConfigDir(p: *Pardes) ?[]const u8 { + return p.opts.config_dir; + } + + fn optsStartupPath(p: *Pardes) ?[]const u8 { + return p.opts.startup_config_path; + } + + /// The recent entry a closed jump names, by its path's hash, its path + /// in scratch. Recent is the authority's (every window of its user + /// writes it): a replica has its answer (`tape.authority`). + pub fn recentByHash(p: *Pardes, hash: u64) ?recent_files.Entry { + return tape.authority(recentFound, .{ p, hash }); + } + + fn recentFound(p: *Pardes, hash: u64) ?recent_files.Entry { + for (p.recent.entries.items) |e| if (std.hash.Wyhash.hash(0, e.path) == hash) { + var found = e; + found.path = p.scratch.allocator().dupe(u8, e.path) catch return null; + return found; + }; return null; } @@ -6607,7 +6644,7 @@ pub const Pardes = struct { if (header == null and pane.isTerminal() and pane.body.mode == .tty and pane.focus == .body and pane.prompt == .none) { p.leader_on = false; p.ctrl_w_pending = false; - if (key.ctrl and !key.alt and key.cp == p.opts.tty_toggle) + if (key.ctrl and !key.alt and key.cp == p.tty_toggle) return p.toggleTty(p.active); // With ProgramEsc on, bare Escape leaves only at a shell prompt // (Shift-Esc, above, always). @@ -6672,7 +6709,7 @@ pub const Pardes = struct { } return; } - const tty_toggle = key.ctrl and key.cp == p.opts.tty_toggle; + const tty_toggle = key.ctrl and key.cp == p.tty_toggle; if (pane.isTerminal() and tty_toggle) { return p.toggleTty(p.active); } @@ -7624,10 +7661,12 @@ pub const Pardes = struct { if (i == p.jcur) cur = w; // survives -> lands at w; dies -> the next one does var kept = j; const live = if (j.serial == 0) false else if (p.panes[j.pane]) |pane| pane.serial == j.serial else false; - // A file's pane closed: its entry stays, closed, while the recent - // list knows the file, and a jump to it opens it again. + // A file's pane closed: its entry stays, closed, and a jump to it + // opens it again from Recent, or drops it when Recent has + // forgotten it (`jumpBy`: Recent is the authority's, read when + // it is wanted, not at every step). if (!live) { - if (j.path_hash == 0 or p.recentByHash(j.path_hash) == null) continue; + if (j.path_hash == 0) continue; kept.serial = 0; // Open again in some pane (a look, Recent): the entry is that // pane's, and closed only while no pane has the file. @@ -7727,12 +7766,13 @@ pub const Pardes = struct { // A closed file's entry: opened again at its dot, placed as a look // places it, and the entry is that pane from here on. if (j.serial == 0) { - const e = p.recentByHash(j.path_hash) orelse return; - // Deleted since: its entries go, it is said, and the step goes - // on to the next place, never stuck on one that cannot open. - if (!recentKeeps(e.path)) { + const found = p.recentByHash(j.path_hash); + // Deleted since, or Recent has forgotten it: its entries go (the + // first said), and the step goes on to the next place, never + // stuck on one that cannot open. + if (found == null or !recentKeeps(found.?.path)) { var said: [recent_path_cap + 32]u8 = undefined; - p.setMessage(p.active, std.fmt.bufPrint(&said, "{s}: deleted, skipped", .{e.path}) catch "deleted, skipped"); + if (found) |e| p.setMessage(p.active, std.fmt.bufPrint(&said, "{s}: deleted, skipped", .{e.path}) catch "deleted, skipped"); const hash = j.path_hash; var w: usize = 0; var cur = p.jcur; @@ -7750,6 +7790,7 @@ pub const Pardes = struct { // round takes one file's entries away, so it ends. return p.jumpBy(delta); } + const e = found.?; const from = p.active; look.reopenAt(p, from, e.path, .{ .line = @max(1, j.line), .col = if (j.pdf) 0 else @max(1, j.col) }); const opened = p.panes[p.active] orelse return; @@ -9284,7 +9325,7 @@ test "raw tty forwards Ctrl letters and former global shortcuts to the child" { _ = drainWrites(p, &buf); edit.setYank(p, "register text"); for ('a'..'z' + 1) |letter| { - if (letter == p.opts.tty_toggle) continue; // editor/TTY mode shortcut + if (letter == p.tty_toggle) continue; // editor/TTY mode shortcut if (letter == 'v') continue; // the window's paste, asserted just below p.update(.{ .key = .{ .cp = @intCast(letter), .ctrl = true } }); const expected = [_]u8{@intCast(letter - 'a' + 1)}; diff --git a/src/replica.zig b/src/replica.zig index d6a2e0ef..8b1d84f4 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -3337,3 +3337,105 @@ test "a far join is sent each blob once: joined again with them on disk or in th try std.testing.expectEqual(@as(u64, 0), j.sent_bytes); endJoin(&j, &have); } + +/// 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()); +} + +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.fs.max_path_bytes]u8 = undefined; + var line: [std.fs.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.njumps > 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.fs.max_path_bytes]u8 = undefined; + var line: [std.fs.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); +} + diff --git a/src/rollback.zig b/src/rollback.zig index b0394d22..bb3ac2dc 100644 --- a/src/rollback.zig +++ b/src/rollback.zig @@ -399,7 +399,7 @@ pub const Copy = struct { const s = @typeInfo(T).@"struct"; inline for (s.field_names, s.field_types, s.field_attrs) |name, F, attr| { if (attr.@"comptime") continue; - if (comptime (annotated(T, "local", name) or annotated(T, "derived", name)) and unsaved(name)) continue; + if (comptime (annotated(T, "local", name) or annotated(T, "derived", name) or annotated(T, "authority", name)) and unsaved(name)) continue; const at = @intFromPtr(&@field(v, name)); if (comptime walk.lengthOf(T, name) != null and @typeInfo(F) == .array) { try c.save(at, walk.lengthIn(v, comptime walk.lengthOf(T, name).?) * @sizeOf(@typeInfo(F).array.child)); @@ -436,10 +436,15 @@ fn annotatedStruct(comptime T: type) bool { /// (numbers, and a static name), and the rows a terminal was last read as /// (a grid's worth of text, emptied when a copy is taken and when it is /// put back: src/replica.zig), and a replica's predicted echo (src/echo.zig: -/// the replica's, never its core's). +/// the replica's, never its core's). And the store and Recent, a replica's +/// own: on the host's heap (Pardes.recentGpa), which the epoch never puts a +/// free off for, so their bytes put back would point at what is freed. A +/// step reads what the authority holds of them off the tape +/// (tape.authority), never a replica's. fn unsaved(comptime name: []const u8) bool { return std.mem.eql(u8, name, "effects") or std.mem.eql(u8, name, "in_q") or std.mem.eql(u8, name, "frames") or - std.mem.eql(u8, name, "shell_rows") or std.mem.eql(u8, name, "echo"); + std.mem.eql(u8, name, "shell_rows") or std.mem.eql(u8, name, "echo") or std.mem.eql(u8, name, "store") or + std.mem.eql(u8, name, "recent"); } fn annotated(comptime T: type, comptime kind: []const u8, comptime name: []const u8) bool { @@ -601,3 +606,43 @@ test "a file's location rows are pinned whole, with what they own, and come back p.deinit(); try std.testing.expectEqual(@as(usize, 0), e.live.count()); } + +/// `Look `, the name a file of the repository's, absolute. +fn lookAt(buf: []u8, name: []const u8) ![]const u8 { + var cwd: [std.fs.max_path_bytes]u8 = undefined; + const dir = std.c.getcwd(&cwd, cwd.len) orelse return error.NoCwd; + return std.fmt.bufPrint(buf, "Look {s}/{s}", .{ std.mem.span(@as([*:0]u8, @ptrCast(dir))), name }); +} + +test "a rollback leaves the store and Recent as a speculation left them: they live on the host's heap, which no copy holds" { + var e: Epoch = .{ .child = std.testing.allocator }; + defer e.deinit(); + const gpa = e.allocator(); + const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml", .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa }); + p.pump(.{}) catch {}; + var line: [std.fs.max_path_bytes + 16]u8 = undefined; + p.update(.{ .command = try lookAt(&line, "build.zig.zon") }); + p.update(.{ .command = "Del" }); + p.pump(.{}) catch {}; + try std.testing.expectEqual(@as(usize, 1), p.store.closed.items.len); + // Both lists full to what they hold: the next of each moves them. + p.store.closed.shrinkAndFree(p.recentGpa(), p.store.closed.items.len); + p.recent.entries.shrinkAndFree(p.recentGpa(), p.recent.entries.items.len); + const recents = p.recent.entries.items.len; + var c: Copy = .{}; + defer c.deinit(); + try c.take(&e, p); + // A speculation opens a file and closes it (a Del a replica applies at + // once), and is undone. + p.update(.{ .command = try lookAt(&line, "build.zig") }); + p.update(.{ .command = "Del" }); + p.pump(.{}) catch {}; + c.restore(); + // What the store and Recent hold is where they say it is. + try std.testing.expectEqual(@as(usize, 2), p.store.closed.items.len); + try std.testing.expect(std.mem.endsWith(u8, p.store.closed.items[1].name, "build.zig")); + try std.testing.expectEqual(recents + 1, p.recent.entries.items.len); + p.deinit(); + try std.testing.expectEqual(@as(usize, 0), e.live.count()); +} + diff --git a/src/tape.zig b/src/tape.zig index 3c8dd765..b16f39f9 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -107,6 +107,8 @@ pub const Site = enum(u16) { /// command printed (a pty/run's answer): a screen-state replica's /// emulator is the last delta's until the next. prompt, + /// What the authority alone holds and a step reads (`authority`). + authority, }; /// What the sites return. wire.semantics hashes their shape with the @@ -125,6 +127,10 @@ pub const results = .{ anyerror![]u8, anyerror!void, terminal.Told, ?pardes.ctlfs.pty.Printed, @typeInfo(@TypeOf(terminal.greeting)).@"fn".return_type.?, + // What only the authority holds (`authority`): a closed pane's record, + // Recent's entry for a closed jump, the closed panes' names. + ?@import("Store.zig").Record, ?@import("recent.zig").Entry, + []const @import("Store.zig").Named, }; /// What a host reports to the core as failed (`shell_failed`, `save_failed`, @@ -813,6 +819,19 @@ pub fn value(comptime s: Site, comptime f: anytype, args: anytype) @typeInfo(@Ty return call(s, null, f, args, false); } +/// A read of what the authority alone holds (`.authority` state a step +/// makes digested state from: the store's closed panes and Recent, which +/// every window of its user writes; the 9P server's readers and socket), +/// taped as `value` is; a speculation that reaches it waits for the +/// authority's answer, as at a host call. A replica's own is never read. +/// What it returns is the tape's arena's on a replay: `f` returns what +/// lives to the step's end (scratch, or the authority's own), and the +/// caller copies what it keeps. +pub fn authority(comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? { + poison(); + return value(.authority, f, args); +} + fn call(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anytype, comptime yields: bool) @typeInfo(@TypeOf(f)).@"fn".return_type.? { const R = @typeInfo(@TypeOf(f)).@"fn".return_type.?; comptime { -- cgit v1.3 From ac57e8c3a05cdd8f587b6faf47e7f9d2c4357dee Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 15:38:44 -0300 Subject: views: a view drawn or read outside a step (a replica client's own while another is live) leaves the live view's slot and its panes' shares holding nothing, as unpark and drawOthers leave them withParked parked the live view and copied it back, but left its slot (column tags' texts, a chord's argument, the presentation) and each pane's share (its selections) pointing at what the live fields own. A rollback copy walks every slot (src/rollback.zig): once the live view freed one of them and the heap gave the memory to another, the copy saved that and put its old bytes back over it, which `emptied` exists to prevent (the slot left as H5's hypothesis had it, in a path it did not name: Replica.draw and Replica.within). Test (failing before): a view drawn outside a step leaves the live view's slot holding nothing a rollback could put old bytes back through. Co-Authored-By: Claude Opus 5.5 --- src/rollback.zig | 30 ++++++++++++++++++++++++++++++ src/views.zig | 6 ++++++ 2 files changed, 36 insertions(+) (limited to 'src/rollback.zig') diff --git a/src/rollback.zig b/src/rollback.zig index bb3ac2dc..9c4f6c05 100644 --- a/src/rollback.zig +++ b/src/rollback.zig @@ -646,3 +646,33 @@ test "a rollback leaves the store and Recent as a speculation left them: they li try std.testing.expectEqual(@as(usize, 0), e.live.count()); } +test "a view drawn outside a step (a replica's own, another live) leaves the live view's slot holding nothing a rollback could put old bytes back through" { + var e: Epoch = .{}; + defer e.deinit(); + const gpa = e.allocator(); + const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml", .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa }); + defer p.deinit(); + p.pump(.{}) catch {}; + _ = p.openView() orelse return error.NoView; + const text = "the column tag of view 0, its own"; + p.col_tags[0].own = try gpa.dupe(u8, text); + // View 1 read (as a replica client's frontend reads its own) while view + // 0 is live: 0 parked and put back. + const Read = struct { + fn read(_: void, _: *Pardes) anyerror!void {} + }; + try pardes.views.within(p, 1, {}, Read.read); + const was = p.col_tags[0].own.?; + gpa.free(was); + p.col_tags[0].own = null; + const other = try gpa.alloc(u8, text.len); + defer gpa.free(other); + if (other.ptr != was.ptr) return error.SkipZigTest; // the heap handed out another + @memset(other, 'a'); + var c: Copy = .{}; + defer c.deinit(); + try c.take(&e, p); + @memset(other, 'b'); + c.restore(); + try std.testing.expect(std.mem.allEqual(u8, other, 'b')); +} diff --git a/src/views.zig b/src/views.zig index b9731089..34644a96 100644 --- a/src/views.zig +++ b/src/views.zig @@ -583,6 +583,10 @@ fn withParked(p: *Pardes, k: u8, ctx: anytype, comptime f: fn (@TypeOf(ctx), *Pa park(p); defer { copy(&fields, p, &p.parked[live].?.view); + // Its slot and its panes' shares, stale while it is live, keep no + // pointer to what they handed back, as `unpark` leaves them: a + // replica's rollback copy walks them (`emptied`). + emptied(p, live); p.view = live; for (p.panes) |s| if (s) |pane| { const pv = &pane.pv[live]; @@ -593,6 +597,8 @@ fn withParked(p: *Pardes, k: u8, ctx: anytype, comptime f: fn (@TypeOf(ctx), *Pa pane.rows = pv.rows; } if (pane.file) |*file| file.scroll = pv.scroll; + pv.body.sels = &.{}; + pv.body.nsel = 0; }; } try f(ctx, p, k); -- cgit v1.3 From f2ede98aee0265e3506f5afd6039c70be7812767 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 15:19:25 -0300 Subject: remote clients: a pointer's hit carries the place it was made on (Layer.Place, on the wire, version 17) and a step resolves it from that, never from this process's frame (`surface`, `.local`), which a session never draws for a client: a window's pointer over a tag diverged it at once (the user's `tape byte 119`, 15 rejoins in 25 s) and a click there aborted it; a replay that meets a site its tape lacks, or makes a syscall that is none, is a divergence that goes on harmless (poisonOf), not a panic, and a speculation that makes one waits (Tty's shell lookup, untaped, is a value site now, as Shell's was); a copy over a queue is refused, not asserted; a session's last client, while no one else watches it, leaves it its layout (leaveView, settled as closeView is), which its rejoin copies; a redial waits only after an Upgrade's `refused` (a divergence's redialled a gone session 20 s); a dying session shuts its clients' links before its trace, and an --attach client ends its ssh and socket whatever way it exits; a far join's hashing stops at a blob once its client is let go (its leave waits for it in the step); a confirmed speculation's state is checked against its unit's digest, and the trace gives a key's and a pointer's words; test/daemon.py: 200 far joins level, a window's pointer swept over tags and body, ssh stand-in clients refused and detached, a client of a killed session ending in 2 s; in-process tests for each Co-Authored-By: Claude Opus 5.5 --- src/Layer.zig | 70 ++++++++++++++++++++----- src/builtins.zig | 3 +- src/daemon.zig | 7 +-- src/detached/wire.zig | 26 ++++++++-- src/gui/gui.zig | 2 +- src/main.zig | 39 +++++++++----- src/mouse.zig | 140 +++++++++++++++++++++++++++++++++++++++++++++++--- src/ninep/pty.zig | 2 +- src/pardes.zig | 46 ++++++++++------- src/replica.zig | 124 ++++++++++++++++++++++++++++++++++++++++++-- src/rollback.zig | 11 ++-- src/tape.zig | 83 ++++++++++++++++++++++++++++-- src/tty/tty.zig | 4 +- src/views.zig | 67 +++++++++++++++++++++++- test/daemon.py | 122 ++++++++++++++++++++++++++++++++++++++++++- 15 files changed, 666 insertions(+), 80 deletions(-) (limited to 'src/rollback.zig') diff --git a/src/Layer.zig b/src/Layer.zig index ba45bc88..609ddbc4 100644 --- a/src/Layer.zig +++ b/src/Layer.zig @@ -28,6 +28,18 @@ pub const Metrics = struct { tagline_h: u16 = 1, }; +/// What a hit is resolved against: the layer it landed on, as the frame +/// the frontend hit-tested had it. A hit carries it, so the step resolves +/// the hit (again, or clamped back onto it) from the input, never from a +/// frame: the frame is the process's own (Pardes.replica's `surface`), and +/// a session never draws a replica's view. +pub const Place = struct { + viewport: pardes.Rect = .{}, + cols: u16 = 0, + rows: u16 = 0, + context_rows: u16 = 0, +}; + /// A point on a tag, notice or header layer: which row of it and which /// column at the tagline's pitch. pub const TagHit = struct { @@ -40,6 +52,7 @@ pub const TagHit = struct { pixel_x: f32, pixel_y: f32, metrics: Metrics, + place: Place = .{}, }; kind: Kind = .body, @@ -103,14 +116,22 @@ const Point = struct { index: u16, col: u16, metrics: Metrics }; /// The row and column a window pixel lands on, each row at its own pitch: /// a tag's at the tagline's width, a body's compact rows too. +pub fn place(layer: *const Layer) Place { + return .{ .viewport = layer.viewport, .cols = layer.cols, .rows = layer.rows, .context_rows = layer.context_rows }; +} + fn pointAt(layer: *const Layer, x: f32, y: f32, body_w: f32, body_h: f32, tagline_w: f32, tagline_h: f32) ?Point { + return pointIn(layer.kind, layer.place(), x, y, body_w, body_h, tagline_w, tagline_h); +} + +fn pointIn(kind: Kind, layer: Place, x: f32, y: f32, body_w: f32, body_h: f32, tagline_w: f32, tagline_h: f32) ?Point { if (layer.rows == 0 or !std.math.isFinite(x) or !std.math.isFinite(y)) return null; // A band's hit is refused outright on metrics no host could have; a // body's has always been floored to a pixel instead, and both refuse // what is no number or past a u16 (a host's floats: web's exports). for ([_]f32{ body_w, body_h, tagline_w, tagline_h }) |metric| { if (!std.math.isFinite(metric) or metric > std.math.maxInt(u16)) return null; - if (layer.kind != .body and metric < 1) return null; + if (kind != .body and metric < 1) return null; } const bw = @max(1, body_w); const bh = @max(1, body_h); @@ -122,7 +143,7 @@ fn pointAt(layer: *const Layer, x: f32, y: f32, body_w: f32, body_h: f32, taglin const headers = @as(f32, @floatFromInt(layer.context_rows)) * th; const index: u16 = @intFromFloat(if (dy < headers) @floor(dy / th) else @as(f32, @floatFromInt(layer.context_rows)) + @floor((dy - headers) / bh)); if (index >= layer.rows) return null; - const pitch = if (layer.kind != .body or index < layer.context_rows) tw else bw; + const pitch = if (kind != .body or index < layer.context_rows) tw else bw; return .{ .index = index, .col = @intFromFloat(@min(@as(f32, @floatFromInt(layer.cols -| 1)), @floor(dx / pitch))), @@ -134,15 +155,39 @@ fn pointAt(layer: *const Layer, x: f32, y: f32, body_w: f32, body_h: f32, taglin /// pane's text edge). pub fn bodyHit(layer: *const Layer, x: f32, y: f32, body_w: f32, body_h: f32, tagline_w: f32, tagline_h: f32) ?pardes.Mouse.BodyHit { if (layer.kind != .body) return null; - const point = layer.pointAt(x, y, body_w, body_h, tagline_w, tagline_h) orelse return null; - return .{ .serial = layer.serial, .row = point.index, .col = point.col + config.GUTTER, .pixel_x = x, .pixel_y = y, .metrics = point.metrics }; + return bodyHitIn(layer.place(), layer.serial, x, y, .{ .body_w = body_w, .body_h = body_h, .tagline_w = tagline_w, .tagline_h = tagline_h }); } /// A band's pixel as the tag cell under it. pub fn tagHit(layer: *const Layer, x: f32, y: f32, body_w: f32, body_h: f32, tagline_w: f32, tagline_h: f32) ?TagHit { if (layer.kind == .body) return null; - const point = layer.pointAt(x, y, body_w, body_h, tagline_w, tagline_h) orelse return null; - return .{ .kind = layer.kind, .id = layer.id, .serial = layer.serial, .line = point.index, .col = point.col, .pixel_x = x, .pixel_y = y, .metrics = point.metrics }; + return tagHitIn(layer.kind, layer.id, layer.serial, layer.place(), x, y, .{ .body_w = body_w, .body_h = body_h, .tagline_w = tagline_w, .tagline_h = tagline_h }); +} + +const Pixels = struct { body_w: f32, body_h: f32, tagline_w: f32, tagline_h: f32 }; + +fn pixelsOf(m: Metrics) Pixels { + return .{ .body_w = @floatFromInt(m.body_w), .body_h = @floatFromInt(m.body_h), .tagline_w = @floatFromInt(m.tagline_w), .tagline_h = @floatFromInt(m.tagline_h) }; +} + +fn bodyHitIn(at: Place, serial: u32, x: f32, y: f32, m: Pixels) ?pardes.Mouse.BodyHit { + const point = pointIn(.body, at, x, y, m.body_w, m.body_h, m.tagline_w, m.tagline_h) orelse return null; + return .{ .serial = serial, .row = point.index, .col = point.col + config.GUTTER, .pixel_x = x, .pixel_y = y, .metrics = point.metrics, .place = at }; +} + +fn tagHitIn(kind: Kind, id: u16, serial: u32, at: Place, x: f32, y: f32, m: Pixels) ?TagHit { + const point = pointIn(kind, at, x, y, m.body_w, m.body_h, m.tagline_w, m.tagline_h) orelse return null; + return .{ .kind = kind, .id = id, .serial = serial, .line = point.index, .col = point.col, .pixel_x = x, .pixel_y = y, .metrics = point.metrics, .place = at }; +} + +/// A body hit at its own pixel, on the place it carries. +pub fn bodyAgain(hit: pardes.Mouse.BodyHit) ?pardes.Mouse.BodyHit { + return bodyHitIn(hit.place, hit.serial, hit.pixel_x, hit.pixel_y, pixelsOf(hit.metrics)); +} + +/// A tag hit at its own pixel, on the place it carries. +pub fn tagAgain(hit: TagHit) ?TagHit { + return tagHitIn(hit.kind, hit.id, hit.serial, hit.place, hit.pixel_x, hit.pixel_y, pixelsOf(hit.metrics)); } test "compact body layer rows fill reclaimed height and invert exact boundaries" { @@ -215,15 +260,14 @@ pub fn sameCell(a: ?TagHit, b: ?TagHit) bool { pub fn columnAt(p: *const pardes.Pardes, kind: Kind, id: usize, supplied: ?TagHit, clamp: bool, line: u16) ?u16 { var point = supplied orelse return null; if (point.kind != kind or point.id != id) return null; - if (clamp) for (p.surface.tagLayers()) |*layer| { - if (layer.rows == 0 or layer.kind != kind or layer.id != id or layer.serial != point.serial or line >= layer.rows) continue; + if (clamp and line < point.place.rows) { + const v = point.place.viewport; const bw: f32 = @floatFromInt(point.metrics.body_w); const bh: f32 = @floatFromInt(point.metrics.body_h); - const left = @as(f32, @floatFromInt(layer.viewport.x)) * bw; - const right = @as(f32, @floatFromInt(layer.viewport.x + layer.viewport.w)) * bw; + const left = @as(f32, @floatFromInt(v.x)) * bw; + const right = @as(f32, @floatFromInt(v.x + v.w)) * bw; point.pixel_x = std.math.clamp(point.pixel_x, left, @max(left, right - 0.001)); - point.pixel_y = (@as(f32, @floatFromInt(layer.viewport.y + line)) + 0.5) * bh; - break; - }; + point.pixel_y = (@as(f32, @floatFromInt(v.y + line)) + 0.5) * bh; + } return if (p.reprojectTagHit(point)) |mapped| mapped.col else null; } diff --git a/src/builtins.zig b/src/builtins.zig index 97cdc5a0..b101e34d 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -1475,7 +1475,8 @@ pub const Tty = struct { // start its fallback in its place. if (comptime pardes.hosted) if (arg.len > 0) { var why: [320]u8 = undefined; - if (@import("host_io.zig").Shell.refusal(arg[0..@min(arg.len, 200)], &why)) |refused| { + // The authority's lookup, taped, as Shell's (exec.zig). + if (pardes.tape.value(.shell, @import("host_io.zig").Shell.refusal, .{ arg[0..@min(arg.len, 200)], &why })) |refused| { var said: [340]u8 = undefined; return c.p.reportFailure(c.id, std.fmt.bufPrint(&said, "Tty: {s}", .{refused}) catch "Tty: no such shell"); } diff --git a/src/daemon.zig b/src/daemon.zig index c6cd7f85..467af8b8 100644 --- a/src/daemon.zig +++ b/src/daemon.zig @@ -577,9 +577,10 @@ pub var stub: c_int = -1; extern "c" fn procctl(idtype: c_int, id: i64, cmd: c_int, data: ?*anyopaque) c_int; -/// A HUP when this process's parent dies (a forked session's stub), or no -/// longer. Linux keeps it per thread: set and cleared on the main one. -fn hupOnStubDeath(on: bool) void { +/// A HUP when this process's parent dies (a forked session's stub, an +/// --attach client's ssh), or no longer. Linux keeps it per thread: set and +/// cleared on the main one. +pub fn hupOnStubDeath(on: bool) void { var sig: c_int = if (on) @backingInt(posix.SIG.HUP) else 0; switch (builtin.os.tag) { .linux => _ = std.os.linux.prctl(@backingInt(std.os.linux.PR.SET_PDEATHSIG), @intCast(sig), 0, 0, 0), diff --git a/src/detached/wire.zig b/src/detached/wire.zig index 1c9e554e..a809bebd 100644 --- a/src/detached/wire.zig +++ b/src/detached/wire.zig @@ -9,6 +9,8 @@ const builtin = @import("builtin"); const pardes = @import("../pardes.zig"); const config = @import("pardes_config"); +/// 17: a pointer's hit carries the place it was made on (Layer.Place) and +/// a tag hit its line, which the step resolves it by, not by its frame; /// 16: a far client says so, the session sends it its blobs' hashes before /// the rest and only the blobs it asks for, all of it in zstd (phase 6's /// content cache and zstd notes), and a client says its frontend (a window @@ -18,7 +20,7 @@ const config = @import("pardes_config"); /// part B); 14: the hello names a session and /// carries the semantics hash, and a ping is answered (phase 5's /// groundwork); 13: the frames went (phase 3). -pub const version: u16 = 16; +pub const version: u16 = 17; /// The terminal's build and the window's step alike (`semantics`). const family = switch (config.platform) { @@ -417,6 +419,17 @@ fn getMetrics(r: *std.Io.Reader) Error!?pardes.RowMetrics { return m; } +fn putPlace(w: *std.Io.Writer, at: pardes.Layer.Place) Error!void { + for ([_]u16{ at.viewport.x, at.viewport.y, at.viewport.w, at.viewport.h, at.cols, at.rows, at.context_rows }) |n| try w.writeInt(u16, n, .little); +} + +fn getPlace(r: *std.Io.Reader) Error!pardes.Layer.Place { + var n: [7]u16 = undefined; + for (&n) |*x| x.* = try r.takeInt(u16, .little); + if (n[6] > n[5]) return error.BadValue; + return .{ .viewport = .{ .x = n[0], .y = n[1], .w = n[2], .h = n[3] }, .cols = n[4], .rows = n[5], .context_rows = n[6] }; +} + fn putBodyHit(w: *std.Io.Writer, hit: ?pardes.Mouse.BodyHit) Error!void { try w.writeByte(@intFromBool(hit != null)); if (hit) |h| { @@ -426,12 +439,13 @@ fn putBodyHit(w: *std.Io.Writer, hit: ?pardes.Mouse.BodyHit) Error!void { try w.writeInt(u32, @bitCast(h.pixel_x), .little); try w.writeInt(u32, @bitCast(h.pixel_y), .little); try putMetrics(w, h.metrics); + try putPlace(w, h.place); } } fn getBodyHit(r: *std.Io.Reader) Error!?pardes.Mouse.BodyHit { if (!try getBool(r)) return null; - const hit: pardes.Mouse.BodyHit = .{ .serial = try r.takeInt(u32, .little), .row = try r.takeInt(u16, .little), .col = try r.takeInt(u16, .little), .pixel_x = try getF32(r), .pixel_y = try getF32(r), .metrics = (try getMetrics(r)) orelse return error.BadValue }; + const hit: pardes.Mouse.BodyHit = .{ .serial = try r.takeInt(u32, .little), .row = try r.takeInt(u16, .little), .col = try r.takeInt(u16, .little), .pixel_x = try getF32(r), .pixel_y = try getF32(r), .metrics = (try getMetrics(r)) orelse return error.BadValue, .place = try getPlace(r) }; if (!std.math.isFinite(hit.pixel_x) or !std.math.isFinite(hit.pixel_y)) return error.BadValue; return hit; } @@ -443,9 +457,11 @@ fn putTagHit(w: *std.Io.Writer, hit: ?pardes.TagHit) Error!void { try w.writeInt(u16, h.id, .little); try w.writeInt(u32, h.serial, .little); try w.writeInt(u16, h.col, .little); + try w.writeInt(u16, h.line, .little); try w.writeInt(u32, @bitCast(h.pixel_x), .little); try w.writeInt(u32, @bitCast(h.pixel_y), .little); try putMetrics(w, h.metrics); + try putPlace(w, h.place); } } @@ -456,9 +472,11 @@ fn getTagHit(r: *std.Io.Reader) Error!?pardes.TagHit { .id = try r.takeInt(u16, .little), .serial = try r.takeInt(u32, .little), .col = try r.takeInt(u16, .little), + .line = try r.takeInt(u16, .little), .pixel_x = try getF32(r), .pixel_y = try getF32(r), .metrics = (try getMetrics(r)) orelse return error.BadValue, + .place = try getPlace(r), }; if (!std.math.isFinite(hit.pixel_x) or !std.math.isFinite(hit.pixel_y)) return error.BadValue; return hit; @@ -618,7 +636,9 @@ pub fn clientBound(msg: ClientMsg) usize { .bye, .ping, .check, .far => 0, .screen_ack, .want => |bytes| bytes.len, .event => |ev| switch (ev) { - .mouse, .resize, .pdf_scroll, .pinch, .touch_scroll, .pointer_leave, .focus, .frontend => 0, + // Its hits' places (14 bytes each) past the slack. + .mouse => 28, + .resize, .pdf_scroll, .pinch, .touch_scroll, .pointer_leave, .focus, .frontend => 0, .key => |k| k.text.len, .paste => |b| b.len, .command => |line| line.len, diff --git a/src/gui/gui.zig b/src/gui/gui.zig index ff1c7793..c3fdd63a 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -2889,7 +2889,7 @@ fn remoteSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_ // The session again, the core it leaves held meanwhile. if (have) |h| h.leaving(r.core()); dial_t0 = std.Io.Clock.awake.now(io).nanoseconds; - joined = replica.Link.redial(gpa, to.path, to.session, &why, have) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); + joined = replica.Link.redial(gpa, to.path, to.session, &why, have, r.diverged == null) orelse Said.no(io, "could not join again: {s}", .{std.mem.sliceTo(&why, 0)}); if (have) |h| h.left(); r.deinit(); r = replica.Link.load(gpa, &joined, o, &why, have) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); diff --git a/src/main.zig b/src/main.zig index ada5f3b9..f9118dfc 100644 --- a/src/main.zig +++ b/src/main.zig @@ -44,7 +44,10 @@ fn logFn( pub const panic = if (is_emscripten) std.debug.FullPanic(std.debug.defaultPanic) else std.debug.FullPanic(struct { fn call(msg: []const u8, ret_addr: ?usize) noreturn { - if (!recovering.swap(true, .seq_cst)) @import("vaxis").recover(); + if (!recovering.swap(true, .seq_cst)) { + pardes.replica.dying(); + @import("vaxis").recover(); + } @import("crash.zig").record(msg); std.debug.defaultPanic(msg, ret_addr); } @@ -54,7 +57,10 @@ var recovering: std.atomic.Value(bool) = .init(false); pub const debug = if (is_emscripten) struct {} else struct { pub fn handleSegfault(addr: ?usize, name: []const u8, opt_ctx: anytype) noreturn { - if (!recovering.swap(true, .seq_cst)) @import("vaxis").recover(); + if (!recovering.swap(true, .seq_cst)) { + pardes.replica.dying(); + @import("vaxis").recover(); + } return std.debug.defaultHandleSegfault(addr, name, opt_ctx); } }; @@ -883,6 +889,7 @@ fn nativeMain(std_init: std.process.Init) !void { } extern "c" fn execvp(file: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int; +extern "c" fn atexit(f: *const fn () callconv(.c) void) c_int; /// `pardes --ps`: what the per-user daemon keeps, as a tree; `--kill=` /// and `--kill-all`: those sessions ended with their shells, a TERM and, @@ -1050,38 +1057,42 @@ fn remoteAttach(init: std.process.Init, opts: pardes.Options, where: []const u8, const pid = std.c.fork(); if (pid < 0) Say.no(io, "no fork for ssh", .{}); if (pid == 0) { + // A client killed outright takes its ssh with it. + daemon.hupOnStubDeath(true); const null_fd = std.c.open("/dev/null", .{ .ACCMODE = .RDONLY }, @as(std.c.mode_t, 0)); if (null_fd >= 0) _ = std.c.dup2(null_fd, 0); _ = execvp(argv[0].?, &argv); std.c._exit(127); } + // Ended at the client's exit, whatever path it takes there (its own + // words on a refused redial exit from inside it). ponytail: a client + // that crashes leaves its socket's file behind; its ssh goes. const Ssh = struct { var child: std.c.pid_t = 0; var path: [:0]const u8 = ""; - fn end() void { - _ = std.c.kill(child, .TERM); - _ = std.c.waitpid(child, null, 0); + fn end() callconv(.c) void { + if (child > 0) { + _ = std.c.kill(child, .TERM); + _ = std.c.waitpid(child, null, 0); + } _ = std.c.unlink(path); } }; Ssh.child = pid; Ssh.path = here; + _ = atexit(Ssh.end); var waited: u32 = 0; while (std.c.access(here, std.c.F_OK) != 0) : (waited += 1) { var status: c_int = 0; - if (std.c.waitpid(pid, &status, std.c.W.NOHANG) == pid) Say.no(io, "ssh to {s} ended before its forward was up", .{where}); - if (waited > 3000) { - Ssh.end(); - Say.no(io, "ssh to {s} set up no forward in 30 s", .{where}); + if (std.c.waitpid(pid, &status, std.c.W.NOHANG) == pid) { + Ssh.child = 0; + Say.no(io, "ssh to {s} ended before its forward was up", .{where}); } + if (waited > 3000) Say.no(io, "ssh to {s} set up no forward in 30 s", .{where}); const ts: std.c.timespec = .{ .sec = 0, .nsec = 10 * std.time.ns_per_ms }; _ = std.c.nanosleep(&ts, null); } - clientOf(init, opts, here, name, true) catch |err| { - Ssh.end(); - Say.no(io, "{t}", .{err}); - }; - Ssh.end(); + clientOf(init, opts, here, name, true) catch |err| Say.no(io, "{t}", .{err}); std.process.exit(0); } diff --git a/src/mouse.zig b/src/mouse.zig index 3281f48a..b7307f2d 100644 --- a/src/mouse.zig +++ b/src/mouse.zig @@ -59,6 +59,9 @@ pub const Drag = union(enum) { /// This gesture began over a usable native PDF raster. The payload is /// zero-bit when PDF support is absent. pdf: panes.Pdf.PointerDrag = .{}, + /// Where its body was in the frontend's frame at its last hit there: + /// a sweep that leaves it is clamped back onto it (`bodyHitForPane`). + body: ?Layer.Place = null, }, fn button(drag: Drag) ?Mouse.Button { @@ -288,22 +291,29 @@ pub fn bodyHitForPane(p: *const Pardes, id: usize, supplied: Mouse.BodyHit, clam supplied.metrics.tagline_w == 0 or supplied.metrics.tagline_h == 0) return null; var mapped_hit = supplied; if (clamp) { + // Onto the body the sweep is in: the place of its last hit there + // (the drag's `body`), the hit's own while it is over it. + if (supplied.serial != pane.serial) mapped_hit.place = (if (p.drag == .select) p.drag.select.body else null) orelse return null; mapped_hit.serial = pane.serial; - const layer = for (&p.surface.body_layers) |*layer| { - if (layer.rows > 0 and layer.serial == pane.serial) break layer; - } else return null; + if (mapped_hit.place.rows == 0) return null; + const v = mapped_hit.place.viewport; const bw: f32 = @floatFromInt(mapped_hit.metrics.body_w); const bh: f32 = @floatFromInt(mapped_hit.metrics.body_h); - const x0 = @as(f32, @floatFromInt(layer.viewport.x)) * bw; - const y0 = @as(f32, @floatFromInt(layer.viewport.y)) * bh; - const x1 = x0 + @as(f32, @floatFromInt(layer.viewport.w)) * bw; - const y1 = y0 + @as(f32, @floatFromInt(layer.viewport.h)) * bh; + const x0 = @as(f32, @floatFromInt(v.x)) * bw; + const y0 = @as(f32, @floatFromInt(v.y)) * bh; + const x1 = x0 + @as(f32, @floatFromInt(v.w)) * bw; + const y1 = y0 + @as(f32, @floatFromInt(v.h)) * bh; mapped_hit.pixel_x = std.math.clamp(mapped_hit.pixel_x, x0, @max(x0, x1 - 0.001)); mapped_hit.pixel_y = std.math.clamp(mapped_hit.pixel_y, y0, @max(y0, y1 - 0.001)); } return p.reprojectBodyHit(mapped_hit); } +fn bodyPlace(hit: ?Mouse.BodyHit, pane: *const Pane) ?Layer.Place { + const h = hit orelse return null; + return if (h.serial == pane.serial) h.place else null; +} + pub fn sameBodyCell(a: ?Mouse.BodyHit, b: ?Mouse.BodyHit) bool { const first = a orelse return b == null; const second = b orelse return false; @@ -514,6 +524,8 @@ pub fn handleMouse(p: *Pardes, event: Mouse) void { current.kind = previous.kind; current.id = previous.id; current.serial = previous.serial; + // ...and its place, which a clamp brings the pointer back onto. + current.place = previous.place; }; } // A drag can leave the compact text viewport. Preserve its metrics so @@ -805,6 +817,7 @@ pub fn handleMouse(p: *Pardes, event: Mouse) void { .id = id, .button = config.select_button, .ctrl = m.ctrl, + .body = bodyPlace(m.body_hit, pane), .pdf = panes.Pdf.pointerStart( p, pane, @@ -862,6 +875,7 @@ pub fn handleMouse(p: *Pardes, event: Mouse) void { .select = .{ .id = id, .button = m.button, + .body = bodyPlace(m.body_hit, pane), // A native body gesture stays native even when its // point later misses a letterbox or selection. .pdf = panes.Pdf.pointerStart( @@ -933,6 +947,7 @@ pub fn dragUpdate(p: *Pardes, mcol: u16, mrow: u16, body_hit: ?Mouse.BodyHit) vo const tag_rows: i32 = pane.tag_rows; panes.File.geometry(pane); if (pane.body_rows > 0 and pane.sel[b].r0 >= tag_rows) if (body_hit) |supplied| { + if (supplied.serial == pane.serial) s.body = supplied.place; const mapped_hit = bodyHitForPane(p, s.id, supplied, true) orelse return; pane.sel[b].c1 = @as(i32, mapped_hit.col) - config.GUTTER; pane.sel[b].r1 = @as(i32, mapped_hit.row) + tag_rows; @@ -2224,3 +2239,114 @@ test "a program tracking the mouse gets its clicks, drags and moves over its gri try std.testing.expectEqualStrings("", try send(p, &out, col, row, .{ .button = .left, .kind = .press })); try std.testing.expectEqualStrings("", try send(p, &out, col, row, .{ .button = .left, .kind = .release })); } + +const test_metrics: pardes.RowMetrics = .{ .body_w = 10, .body_h = 20, .tagline_w = 6, .tagline_h = 12 }; + +/// A session and a replica joined to it from its dump: digest for digest +/// the same, and only the session has drawn a frame (its surface is its +/// own, `.local`). What a step does with one input must not depend on that. +fn sessionAndReplica(gpa: std.mem.Allocator, opts: pardes.Options) !struct { a: *Pardes, b: *Pardes } { + const a = try Pardes.init(gpa, opts); + errdefer a.deinit(); + a.update(.{ .resize = .{ .cols = opts.cols, .rows = opts.rows, .row_metrics = test_metrics } }); + a.sync(); + const bytes = try pardes.dump.dumpBytes(a); + defer gpa.free(bytes); + const b = try pardes.dump.load(gpa, a.opts, bytes, null); + errdefer b.deinit(); + var frame: std.heap.ArenaAllocator = .init(gpa); + defer frame.deinit(); + _ = try a.render(frame.allocator()); + try expectSame(a, b, "before the press"); + return .{ .a = a, .b = b }; +} + +/// The two cores' digests equal, else the fields (and pane fields) that differ. +fn expectSame(a: *const Pardes, b: *const Pardes, when: []const u8) !void { + if (pardes.walk.digest(Pardes, a) == pardes.walk.digest(Pardes, b)) return; + const names = comptime pardes.walk.fieldNames(Pardes); + var da: [names.len]u64 = undefined; + var db: [names.len]u64 = undefined; + _ = pardes.walk.fieldDigests(Pardes, a, &da); + _ = pardes.walk.fieldDigests(Pardes, b, &db); + for (names, da, db) |name, x, y| if (x != y) std.debug.print("{s}: {s} differs\n", .{ when, name }); + const pane_names = comptime pardes.walk.fieldNames(pardes.Pane); + for (a.panes, b.panes, 0..) |pa, pb, id| { + const x = pa orelse continue; + const y = pb orelse continue; + var pda: [pane_names.len]u64 = undefined; + var pdb: [pane_names.len]u64 = undefined; + _ = pardes.walk.fieldDigests(pardes.Pane, x, &pda); + _ = pardes.walk.fieldDigests(pardes.Pane, y, &pdb); + for (pane_names, pda, pdb) |name, u, v| if (u != v) std.debug.print("{s}: pane {d}.{s} differs\n", .{ when, id, name }); + } + return error.DigestsDiffer; +} + +fn layerCenter(layer: *const Layer, col: u16, comptime tag: bool) [2]f32 { + const w: f32 = if (tag) test_metrics.tagline_w else test_metrics.body_w; + const h: f32 = if (tag) test_metrics.tagline_h else test_metrics.body_h; + return .{ + @as(f32, @floatFromInt(layer.viewport.x)) * test_metrics.body_w + (@as(f32, @floatFromInt(col)) + 0.5) * w, + @as(f32, @floatFromInt(layer.viewport.y)) * test_metrics.body_h + 0.5 * h, + }; +} + +// A frontend's pointer hit is its own frame's: a step resolves it on the +// place the hit carries (Layer.Place), never on this process's last frame +// (`surface`, `.local`), so a session and its replica take one input alike +// (the variant hunt's, 2026-10-30, after a window client's tag hover). +test "a tag press is the same state whichever frame this process drew last" { + const gpa = std.testing.allocator; + const two = try sessionAndReplica(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer two.a.deinit(); + defer two.b.deinit(); + const layer = for (two.a.surface.tagLayers()) |*l| { + if (l.rows != 0 and l.kind == .pane) break l; + } else return error.NoPaneTag; + const at = layerCenter(layer, 2, true); + const hit = layer.tagHit(at[0], at[1], test_metrics.body_w, test_metrics.body_h, test_metrics.tagline_w, test_metrics.tagline_h) orelse return error.NoHit; + const press: pardes.Event = .{ .mouse = .{ .button = .left, .kind = .press, .col = layer.viewport.x + 2, .row = layer.viewport.y, .tag_hit = hit } }; + two.a.update(press); + two.b.update(press); + try expectSame(two.a, two.b, "after the press"); +} + +// The same through a body hit (edit.pointerTextSelection, a sweep's drag), +// on a compact body layer (TreeContext), which a pixel shell inverts. +test "a body press, drag and release are the same state whichever frame this process drew last" { + if (comptime !pardes.syntax.enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const a = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 16 }); + defer a.deinit(); + const pane = try a.setTestFile("pub const Box = struct {\n pub fn run() void {\n" ++ (std.mem.asBytes(&@as([30][16]u8, @splat(" // body\n".*)))) ++ " }\n};\n"); + a.gpa.free(pane.file.?.path); + pane.file.?.path = try a.gpa.dupe(u8, "/test.zig"); + if (!pardes.panes.File.supportsContext(pane)) return error.SkipZigTest; + a.update(.{ .resize = .{ .cols = 60, .rows = 16, .row_metrics = test_metrics } }); + pane.file.?.tree_context = true; + pane.file.?.scroll = 8; + a.sync(); + const bytes = try pardes.dump.dumpBytes(a); + defer gpa.free(bytes); + const b = try pardes.dump.load(gpa, a.opts, bytes, null); + defer b.deinit(); + var frame: std.heap.ArenaAllocator = .init(gpa); + defer frame.deinit(); + _ = try a.render(frame.allocator()); + // The join agrees, the frame drawn and all. + try expectSame(a, b, "before the press"); + const layer = for (&a.surface.body_layers) |*l| { + if (l.rows > 0 and l.serial == pane.serial) break l; + } else return error.NoBodyLayer; + const at = layerCenter(layer, 8, false); + const hit = layer.bodyHit(at[0], at[1], test_metrics.body_w, test_metrics.body_h, test_metrics.tagline_w, test_metrics.tagline_h) orelse return error.NoHit; + const col: u16 = @intFromFloat(at[0] / test_metrics.body_w); + const row: u16 = @intFromFloat(at[1] / test_metrics.body_h); + for ([_]pardes.Mouse.Kind{ .press, .drag, .release }) |kind| { + const ev: pardes.Event = .{ .mouse = .{ .button = .left, .kind = kind, .col = col, .row = row, .body_hit = hit } }; + a.update(ev); + b.update(ev); + } + try expectSame(a, b, "after the press"); +} diff --git a/src/ninep/pty.zig b/src/ninep/pty.zig index 53fc5c68..ee8c618e 100644 --- a/src/ninep/pty.zig +++ b/src/ninep/pty.zig @@ -86,7 +86,7 @@ pub fn writeCtl(p: *Pardes, req: Req, id: usize) Reply { if (comptime pardes.hosted) if (!apply and std.mem.eql(u8, line, "exec")) if (p.panes[id]) |pane| { const bin = pane.shell orelse p.shellBin(); var why: [320]u8 = undefined; - if (@import("../host_io.zig").Shell.refusal(bin[0..@min(bin.len, 200)], &why)) |refused| + if (pardes.tape.value(.shell, @import("../host_io.zig").Shell.refusal, .{ bin[0..@min(bin.len, 200)], &why })) |refused| return tree.failText(req.tag, E.NOENT, std.fmt.bufPrint(&p.fs.ename, "exec: {s}", .{refused}) catch "exec: no such shell"); }; if (!verb(p, id, line, apply)) return tree.failText(req.tag, E.INVAL, if (tooFewRows(line)) e_winsize_rows else if (outOfRange(line)) e_winsize_range else e_bad_pty_ctl); diff --git a/src/pardes.zig b/src/pardes.zig index 5b37342e..189a5b02 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -172,8 +172,10 @@ pub const Turn = struct { /// a step. Nests: a directory listing that resolves each entry yields /// inside a yield, and only the outermost one lets go. pub fn yield(t: *Turn) void { - // A replica makes no syscall: each is a site of the tape's. - if (tape.replaying()) @panic("tape: a syscall that is no tape site, made replaying"); + // A replica makes no syscall: each is a site of the tape's. One that + // is none stops a speculation and diverges a replay (it joins + // again), never the process (a window client's `Tty fish`, H17). + if (tape.replaying()) tape.stray(); const io = t.io orelse return; yield_depth += 1; if (yield_depth != 1) return; @@ -478,6 +480,7 @@ pub const Loc = struct { test { _ = @import("rollback.zig"); + _ = @import("tape.zig"); _ = @import("replica.zig"); _ = @import("echo.zig"); _ = @import("modal.zig"); @@ -3595,6 +3598,7 @@ pub const Mouse = struct { pixel_x: f32, pixel_y: f32, metrics: RowMetrics, + place: Layer.Place = .{}, }; // sel indexing relies on left/middle/right being 0/1/2 pub const Button = enum { left, middle, right, wheel_up, wheel_down, wheel_left, wheel_right, none, back, forward }; @@ -5692,6 +5696,17 @@ pub const Pardes = struct { p.settle(); } + /// The last client of a session no one else watches gone: its view + /// goes, and the session's own is laid out as it was (views.closeKeeping), + /// which the client's rejoin copies. Settled as `closeView` is. + pub fn leaveView(p: *Pardes, k: u8) void { + const taped = tape.enter(p, .{ .leave_view = k }); + defer tape.leave(p, taped); + views.closeKeeping(p, k); + p.sync(); + p.settle(); + } + /// Into view `k`, before the input its client sent. pub fn toView(p: *Pardes, k: u8) void { if (k == p.view) return; @@ -8176,18 +8191,16 @@ pub const Pardes = struct { return p.haptic; } + /// A frontend's tag hit, if what it names is still there: resolved on + /// the place it carries (Layer.Place), not on this process's frame. pub fn reprojectTagHit(p: *const Pardes, tag_hit: TagHit) ?TagHit { - for (p.surface.tagLayers()) |*layer| { - if (layer.rows == 0 or layer.kind != tag_hit.kind or layer.id != tag_hit.id or layer.serial != tag_hit.serial) continue; - if (layer.kind == .pane) { - if (layer.id >= p.panes.len) return null; - const pane = p.panes[layer.id] orelse return null; - if (pane.serial != layer.serial) return null; - } - if (layer.kind == .column and (layer.id >= p.ncol or p.col_serial[layer.id] != layer.serial)) return null; - return layer.tagHit(tag_hit.pixel_x, tag_hit.pixel_y, @floatFromInt(tag_hit.metrics.body_w), @floatFromInt(tag_hit.metrics.body_h), @floatFromInt(tag_hit.metrics.tagline_w), @floatFromInt(tag_hit.metrics.tagline_h)) orelse continue; + if (tag_hit.kind == .pane) { + if (tag_hit.id >= p.panes.len) return null; + const pane = p.panes[tag_hit.id] orelse return null; + if (pane.serial != tag_hit.serial) return null; } - return null; + if (tag_hit.kind == .column and (tag_hit.id >= p.ncol or p.col_serial[tag_hit.id] != tag_hit.serial)) return null; + return Layer.tagAgain(tag_hit); } /// The pane's first body row: under the tag when it is on top, over it @@ -8207,12 +8220,9 @@ pub const Pardes = struct { return layer.capacity(p.row_metrics); } - pub fn reprojectBodyHit(p: *const Pardes, body_hit: Mouse.BodyHit) ?Mouse.BodyHit { - for (&p.surface.body_layers) |*layer| { - if (layer.rows == 0 or layer.serial != body_hit.serial) continue; - return layer.bodyHit(body_hit.pixel_x, body_hit.pixel_y, @floatFromInt(body_hit.metrics.body_w), @floatFromInt(body_hit.metrics.body_h), @floatFromInt(body_hit.metrics.tagline_w), @floatFromInt(body_hit.metrics.tagline_h)); - } - return null; + /// A frontend's body hit, resolved on the place it carries. + pub fn reprojectBodyHit(_: *const Pardes, body_hit: Mouse.BodyHit) ?Mouse.BodyHit { + return Layer.bodyAgain(body_hit); } // ---- render: build the canonical surface (draw.zig) ---- diff --git a/src/replica.zig b/src/replica.zig index 22de375f..2af32cdb 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -235,6 +235,16 @@ pub const Authority = struct { return .{ .view = k, .unit = try 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 { @@ -687,7 +697,23 @@ pub const Replica = struct { r.applied -= 1; if (r.applied == 0) { // Nothing speculative on top any more: the state is the - // confirmed one. + // confirmed one, which its digest says. A step of its own + // that read what the session's did not (a frame's, a + // derived field's) shows here, at the input that did it, + // not at some later unit replayed. + if (lastDigest(u.tape)) |d| { + tape.checkDigest(&r.t, d); + if (r.t.diverged) |why| { + if (std.c.getenv("PARDES_REPLICA_TRACE") != null) { + var w: std.Io.Writer.Allocating = .init(r.gpa); + defer w.deinit(); + w.writer.print("replica {d}: its input {d} came to another state than the session's: {s}\n", .{ r.id, u.seq, why }) catch {}; + tape.describe(u.tape, &w.writer); + std.debug.print("{s}", .{w.written()}); + } + return r.fail(why); + } + } r.copy.release(); rollback.Epoch.select(null); r.clearSince(); @@ -1320,6 +1346,9 @@ pub const Peer = struct { 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); @@ -1350,6 +1379,14 @@ pub const Peer = struct { 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); @@ -1513,6 +1550,18 @@ pub const Peer = struct { } }; +/// 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); + }; +} + pub const Peers = struct { gpa: Allocator = std.heap.c_allocator, io: std.Io = undefined, @@ -1750,6 +1799,7 @@ pub const Peers = struct { const slot = for (&ps.list) |*s| { if (s.* == null) break s; } else return refuse(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 refuse(fd, "that session records a tape of its own (PARDES_RECORD)"); @@ -1836,7 +1886,7 @@ pub const Peers = struct { log.info("client {d} left '{s}'", .{ peer.id, ps.name }); ps.screens.dropPeer(peer.sid); core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view)); - core.closeView(peer.view); + ps.closeViewOf(core, peer); if (ps.count() == 0) core.unattended = ps.alone_unattended; ps.seq += 1; ps.unit(0, ps.seq); @@ -1844,6 +1894,13 @@ pub const Peers = struct { if (ps.count() == 0) ps.stop(core); } + /// A gone client's view closed (its slot let go already): the last one + /// of a session no one else watches leaves the session its layout, for + /// its rejoin (an Upgrade's, a diverged one's) to find. + fn closeViewOf(ps: *Peers, core: *Pardes, peer: *Peer) void { + if (ps.count() == 0 and ps.alone_unattended) 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; @@ -1949,7 +2006,7 @@ pub const Peers = struct { for (&ps.list) |*slot| if (slot.*) |peer| { slot.* = null; core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view)); - core.closeView(peer.view); + ps.closeViewOf(core, peer); ps.screens.dropPeer(peer.sid); ps.letGo(peer); }; @@ -2770,7 +2827,10 @@ pub const Link = struct { /// dialled every 100 ms for 20 s. A far client's (`have`) holds the /// core it leaves meanwhile (`Have.leaving`): its blobs are not sent /// again. Null, and why in `why`. - pub fn redial(gpa: Allocator, path: []const u8, session: []const u8, why: *[600]u8, have: ?*Have) ?Joined { + /// `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). + pub fn redial(gpa: Allocator, path: []const u8, session: []const u8, why: *[600]u8, have: ?*Have, patient: bool) ?Joined { var tries: u32 = 0; while (true) : (tries += 1) { why.* = @splat(0); @@ -2788,13 +2848,20 @@ pub const Link = struct { _ = std.fmt.bufPrint(why, "{s}\x00", .{upgrade_words}) catch {}; return null; } - if (tries > 200) 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); } } }; +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(Link.redial(std.testing.allocator, "/nonexistent/pardes-replica.sock", "gone", &why, null, false) == null); + try std.testing.expect(nowNs() - t0 < 100 * std.time.ns_per_ms); +} + 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; @@ -3007,6 +3074,53 @@ test "a delta applied under a speculation outlives its rollback: the terminal st for (rows_a[rows_a.len - shown ..], rows_r[rows_r.len - shown ..]) |x, y| try std.testing.expectEqualStrings(x, y); } +test "a view that sized a terminal closing leaves nothing queued: the others' replicas copy, type and agree" { + if (comptime !terminal.enabled) return error.SkipZigTest; + 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.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()); +} + /// p50 and p99 of `samples`, sorted in place. fn spread(samples: []u64) [2]u64 { std.mem.sort(u64, samples, {}, std.sort.asc(u64)); diff --git a/src/rollback.zig b/src/rollback.zig index 9c4f6c05..cc10d2cc 100644 --- a/src/rollback.zig +++ b/src/rollback.zig @@ -221,7 +221,9 @@ pub const Copy = struct { return a.addr == b.addr and a.ty == b.ty; } }; - pub const Error = Allocator.Error; + /// Queued: the core holds effects or inputs not drained yet, which no + /// copy is taken over (the replica's input waits for the authority). + pub const Error = Allocator.Error || error{Queued}; pub fn deinit(c: *Copy) void { c.regions.deinit(c.meta); @@ -244,14 +246,15 @@ pub const Copy = struct { /// it held stays where it is until the copy is put back or let go. pub fn take(c: *Copy, e: *Epoch, p: *const Pardes) Error!void { std.debug.assert(!c.held); + // A step drains these before it ends, and a copy is taken between + // steps; an input of a host's without one (a view closed) may leave + // some until it is drained. + if (p.effects_len != 0 or p.in_len != 0) return error.Queued; c.regions.clearRetainingCapacity(); c.bytes.clearRetainingCapacity(); c.seen.clearRetainingCapacity(); c.pins.clearRetainingCapacity(); c.epoch = e; - // A step drains these before it ends: between steps they hold - // nothing, and a copy is taken between steps. - std.debug.assert(p.effects_len == 0 and p.in_len == 0); if (c.epoch.?.owns(@intFromPtr(p)) == null) @panic("rollback: the core is not its epoch's"); try c.object(Pardes, p); e.begin(); diff --git a/src/tape.zig b/src/tape.zig index b16f39f9..12e3bdec 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -62,6 +62,7 @@ pub const Input = union(enum) { file_deleted: u8, open_view, close_view: u8, + leave_view: u8, to_view: u8, /// What a window says of its font and its opacity (gui.zig). take_font_request, @@ -605,6 +606,7 @@ pub const Tape = struct { .file_deleted => |id| pardes.panes.File.deleted(p, id), .open_view => _ = p.openView(), .close_view => |k| p.closeView(k), + .leave_view => |k| p.leaveView(k), .to_view => |k| p.toView(k), .take_font_request => _ = p.takeFontRequest(), .observe_font => |f| _ = p.observeFont(f.name, f.size, f.unit), @@ -845,12 +847,14 @@ fn call(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anyt const t = on orelse return if (yields) out(f, args) else @call(.auto, f, args); const id: u16 = @as(u16, host_fns.len) + @backingInt(s); if (t.replay) { - if (!t.expectHost(id, @tagName(s))) { - std.debug.print("replay: {s}\n", .{t.diverged orelse "?"}); - @panic("tape: the replay diverged at a host call of the core's own"); - } + // Diverged (said in `diverged`): what a speculation gets, harmless, + // for the rest of the input. A replica joins again; a replay says so. + if (!t.expectHost(id, @tagName(s))) return poisonOf(R); var ret = t.last_ret; - return walk.decode(R, &ret, a orelse t.arena.allocator()) catch @panic("tape: a short result"); + return walk.decode(R, &ret, a orelse t.arena.allocator()) catch { + t.diverge("the tape's answer to {t} is short", .{s}); + return poisonOf(R); + }; } // The host's own (a shell's spawn asking the shell), or one inside a // site's syscall: the outer call is the record. @@ -906,6 +910,13 @@ pub fn recordDigest(t: *Tape) void { t.record(.digest, []const u64, &d); } +/// The core's digest against a digest record's payload (a unit's last): a +/// difference is `diverged`, its fields named when the record has them. +pub fn checkDigest(t: *Tape, payload: []const u8) void { + var r: walk.Reader = .{ .bytes = payload }; + t.check(&r); +} + /// A core made outside the tape (a replica client's, from a dump) is the /// one it replays into. pub fn adoptCore(t: *Tape, p: *Pardes) void { @@ -945,6 +956,14 @@ pub fn replaying() bool { return if (on) |t| t.replay or t.speculating else false; } +/// A syscall of the core's that is no site, made by a replica: what it +/// returns is the authority's to say. A speculation waits for it, and a +/// replay has diverged. +pub fn stray() void { + const t = on orelse return; + if (t.speculating) t.poisoned = true else t.diverge("a syscall that is no tape site, made replaying", .{}); +} + /// A replica speculating (src/replica.zig): state only the authority /// changes (a terminal's emulator) is left alone, and the input waits. pub fn speculating() bool { @@ -1115,6 +1134,16 @@ pub fn describe(bytes: []const u8, w: *std.Io.Writer) void { const tag = std.mem.bytesToValue(std.meta.Tag(Input), payload[0..@sizeOf(std.meta.Tag(Input))]); w.print("input {t}", .{tag}) catch {}; if (tag == .update) w.print(" {t}", .{std.mem.bytesToValue(std.meta.Tag(pardes.Event), payload[@sizeOf(std.meta.Tag(Input))..][0..@sizeOf(std.meta.Tag(pardes.Event))])}) catch {}; + // A key's or a pointer's own words: which one a divergence + // starts at. + var room: [4096]u8 = undefined; + var fixed: std.heap.FixedBufferAllocator = .init(&room); + var in: walk.Reader = .{ .bytes = payload }; + if (tag == .update) if (walk.decode(Input, &in, fixed.allocator())) |input| switch (input.update) { + .key => |k| w.print(" {any}", .{k}) catch {}, + .mouse => |m| w.print(" {t} {t} {d},{d} body {any} tag {any}", .{ m.button, m.kind, m.col, m.row, m.body_hit != null, m.tag_hit != null }) catch {}, + else => {}, + } else |_| {}; }, .host => { const id = std.mem.bytesToValue(u16, payload[0..2]); @@ -1201,3 +1230,47 @@ pub fn replayMain(io: std.Io, path: []const u8) u8 { } return if (failed) 1 else 0; } + +test "a syscall that is no site, made replaying, diverges the replay and poisons a speculation" { + if (comptime !pardes.hosted) return; + const gpa = std.testing.allocator; + var t: Tape = .{ .gpa = gpa, .replay = true, .scratch = .init(gpa), .arena = .init(gpa) }; + defer t.scratch.deinit(); + defer t.arena.deinit(); + defer if (t.diverged) |why| gpa.free(why); + on = &t; + defer on = null; + pardes.turn.yield(); + pardes.turn.back(); + try std.testing.expect(t.diverged != null); + t.replay = false; + t.speculating = true; + pardes.turn.yield(); + pardes.turn.back(); + try std.testing.expect(t.poisoned); +} + +test "a replay that finds no answer where its core reads one diverges, and the read goes on harmless" { + if (comptime !pardes.hosted) return; + const gpa = std.testing.allocator; + var t: Tape = .{ .gpa = gpa, .replay = true, .scratch = .init(gpa), .arena = .init(gpa) }; + defer t.scratch.deinit(); + defer t.arena.deinit(); + defer if (t.diverged) |why| gpa.free(why); + // The input's end where the core reads a value of the host's (a + // window client's step that took a turn the session's had not, H9 of + // 2026-10-30: it aborted the client). + const bytes = [5]u8{ @backingInt(Kind.done), 0, 0, 0, 0 }; + t.in = .{ .bytes = &bytes }; + t.host_side = false; + on = &t; + defer on = null; + const read = struct { + fn f() bool { + return true; + } + }.f; + try std.testing.expect(!value(.denied, read, .{})); + try std.testing.expect(t.diverged != null); + try std.testing.expect(!value(.denied, read, .{})); +} diff --git a/src/tty/tty.zig b/src/tty/tty.zig index a1a28181..39c287f7 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -1482,9 +1482,9 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se // are not sent again. if (have) |h| h.leaving(r.core()); dial_t0 = std.Io.Clock.awake.now(io).nanoseconds; - joined = replica.Link.redial(gpa, path, session, &why, have) orelse { + joined = replica.Link.redial(gpa, path, session, &why, have, r.diverged == null) orelse { screen.leave(); - Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); + Said.no(io, "could not join again: {s}", .{std.mem.sliceTo(&why, 0)}); }; if (have) |h| h.left(); r.deinit(); diff --git a/src/views.zig b/src/views.zig index 34644a96..ac274621 100644 --- a/src/views.zig +++ b/src/views.zig @@ -422,7 +422,7 @@ pub fn open(p: *Pardes) ?u8 { }; const slot = p.parked[k].?; slot.* = .{ .view = .{ .screen_w = p.screen_w, .screen_h = p.screen_h } }; - inline for (.{ "screen_w", "screen_h", "cell_pixels", "row_metrics", "ncol", "col_weight", "col_panes", "col_n", "col_serial", "finder", "active", "active_column", "jumps", "njumps", "jcur", "windowed", "native_images", "native_image_patches" }) |name| + inline for (sized ++ laid_out ++ .{ "windowed", "native_images", "native_image_patches" }) |name| @field(slot.view, name) = @field(p, name); slot.view.col_tags = @splat(.{ .what = .column, .gpa = p.gpa }); for (p.panes, 0..) |s, id| { @@ -435,6 +435,39 @@ pub fn open(p: *Pardes) ?u8 { return k; } +/// How a view is laid out and focused: what a new one takes of the live +/// one (`open`), and what one going leaves the view that stays (`closeKeeping`). +const laid_out = .{ "ncol", "col_weight", "col_panes", "col_n", "col_serial", "finder", "active", "active_column", "jumps", "njumps", "jcur" }; +const sized = .{ "screen_w", "screen_h", "cell_pixels", "row_metrics" }; + +/// View `k` gone, and the view that stays live laid out as `k` was: its +/// columns, its focus, its panes where they were scrolled and its cursors +/// where they were. For a session's last client while no one else watches +/// it (replica.Peers.leave): the client's rejoin (an Upgrade's, a diverged +/// one's) copies the live view (`open`), so it finds its own again. +pub fn closeKeeping(p: *Pardes, k: u8) void { + if (!isOpen(p, k) or count(p) == 1) return; + to(p, k); + const other: u8 = for (0..max) |i| { + if (i != k and isOpen(p, @intCast(i))) break @intCast(i); + } else unreachable; + const slot = p.parked[other].?; + // Its size too: the terminals it sized keep theirs, no shell redrawn for + // a size it is given back at the rejoin. + inline for (sized ++ laid_out) |name| @field(slot.view, name) = @field(p, name); + for (p.panes, 0..) |s, id| { + const pane = s orelse { + slot.serials[id] = 0; + continue; + }; + if (pane.pv_known & (@as(u8, 1) << @intCast(other)) != 0 and pane.pv[other].body.sels.len > 0) p.gpa.free(pane.pv[other].body.sels); + slot.serials[id] = pane.serial; + pane.pv[other] = fresh(pane); + pane.pv_known |= @as(u8, 1) << @intCast(other); + } + close(p, k); +} + /// View `k` gone, and all it held; the live one moves to another first. /// The last view stays: a session without a client keeps its own. pub fn close(p: *Pardes, k: u8) void { @@ -771,6 +804,38 @@ test "a view closing leaves the others as they were" { try testing.expectEqual(@as(i32, 70), pane.body.cur_row); } +test "the last client's view going leaves the session its layout, which a rejoin copies" { + const gpa = testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 }); + defer p.deinit(); + const text = try numbered(gpa, 100); + defer gpa.free(text); + const pane = try p.setTestFile(text); + const k = p.openView().?; + p.toView(k); + p.update(.{ .resize = .{ .cols = 120, .rows = 40 } }); + p.update(.{ .command = "Newcol" }); + p.update(.{ .command = "New" }); + const made = p.active; + const ncol = p.ncol; + p.active = 0; + p.sync(); + cursorTo(p, pane, 60); + p.leaveView(k); + try testing.expectEqual(@as(usize, 1), count(p)); + try testing.expectEqual(ncol, p.ncol); + try testing.expect(layout.findPane(p, made) != null); + try testing.expectEqual(@as(i32, 60), pane.body.cur_row); + try testing.expectEqual(@as(u16, 120), p.screen_w); + // Its client back (a view copied from the live one): all of it again. + const again = p.openView().?; + p.toView(again); + try testing.expectEqual(ncol, p.ncol); + try testing.expect(layout.findPane(p, made) != null); + try testing.expectEqual(@as(usize, 0), p.active); + try testing.expectEqual(@as(i32, 60), pane.body.cur_row); +} + test "drawing the other views leaves the live one as it was, to the digest" { const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); diff --git a/test/daemon.py b/test/daemon.py index 1086dac3..7b0e5343 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -193,7 +193,7 @@ class Replica: """A replica's connection (src/detached/wire.zig): length-prefixed messages, a tag byte and a little-endian u32 length. It is a near client: its first word after the hello says so.""" - WIRE = 16 + WIRE = 17 def __init__(self, path, timeout=10): self.s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) @@ -547,6 +547,11 @@ class Window: self.p.stdin.write(text.replace(b'\x1b', b'\x1b[27u')) self.p.stdin.flush() + def mouse(self, kind, button, x, y): + """A pointer event at window pixels (gui.zig's test feed).""" + self.p.stdin.write(f'\x1b]777;mouse;{kind};{button};{x};{y}\x07'.encode()) + self.p.stdin.flush() + def said(self): return self.err.read_text(errors='replace') if self.err.exists() else '' @@ -632,6 +637,24 @@ def window_clients(binary, gui_binary): n = len(checks(check)) w.keys(b'jjjjjkkk\x06\x06\x02') wait_for(f'the window client agrees again ({rtt} ms)', lambda: agreed(check, n), timeout=30) + if rtt in (0, 207): + # The pointer over the tags (the workspace's, the column's, + # the pane's: the top rows, a test window's cells about 16 by + # 33 pixels) and over the body, a click there: a window's + # hits are its own frame's, which the session never draws, + # so they are its input's, not re-made from a frame. Each + # tag motion made it join again (the user's, 2026-10-30). + n = len(checks(check)) + for y in range(5, 170, 11): + for x in range(4, 1280, 23): + w.mouse('motion', 0, x, y) + for k in range(40): + w.mouse('motion', 0, 100 + 25 * k, 200 + 9 * k) + w.mouse('down', 1, 400, 300) + w.mouse('motion', 1, 600, 360) + w.mouse('up', 1, 600, 360) + wait_for(f'the window client agrees after its pointer ({rtt} ms)', lambda: agreed(check, n), timeout=30) + assert 'joined again' not in w.said(), w.said()[-800:] agreements.append(len(checks(check))) assert w.close() == 0, w.said()[-800:] if relay: @@ -891,6 +914,30 @@ def far(binary): detach('f', tf, f) f_join = joins(root / 'f.stats')[0] assert f_join['from_disk'] > 0 and sum(cached(cache).values()) <= 1 << 20, (f_join, cached(cache)) + far_joins(root / 'pardes-replica.sock', 'files', binary, 200) + # Through ssh (a stand-in forwarding as `ssh -L` does): a client + # refused and one detached each end their ssh and its socket (one + # refused left both, 2026-10-30). + fake = root / 'ssh' + fake.write_text('#!/bin/sh\nwhile [ $# -gt 0 ]; do case $1 in -L) fwd=$2; shift;; esac; shift; done\n' + f'exec {sys.executable} -B {Path(__file__).parent / "delay_relay.py"} "${{fwd%%:*}}" "${{fwd#*:}}" --delay 0\n') + fake.chmod(0o755) + senv = dict(env, PARDES_SSH=str(fake), PARDES_REMOTE_RUNTIME=str(root), TMPDIR=str(root)) + + def forwarded(): + return [q for q in processes(root) if alive(q) and any(b'pardes-ssh-' in a for a in cmdline(q))] or list(root.glob('pardes-ssh-*')) + for name in ('nobody', 'files'): + ts = Term(24, 90) + terms.append(ts) + sc = ts.spawn([binary, f'--attach=box:{name}'], senv) + started.append(sc.pid) + if name == 'files': + wait_for('the client through ssh\'s first frame', lambda: b'line 3 of the notes' in ts.text(), timeout=30) + time.sleep(.3) + os.write(ts.master, b' sD') + wait_for(f'the client through ssh ends ({name})', lambda: sc.poll() is not None, timeout=30) + assert name == 'files' or b"no session called 'nobody'" in ts.text(), ts.text()[-300:] + wait_for(f'its ssh and socket gone ({name})', lambda: not forwarded(), timeout=10) body = texts() for words in (b'from-A', b'from-B', b'again-A', b'from-C', b'from-D', b'from-E', b'from-F'): assert words in body, words @@ -919,6 +966,67 @@ def far(binary): shutil.rmtree(root, ignore_errors=True) +def far_joins(sock, session, binary, n): + """`n` far joins of one session in a row, each read whole (its blobs + asked for by none, its zstd frames unpacked) and answering a ping, then + gone: the session's fds and threads as they were, its RSS within a + bound. Each join made zstd contexts through the C heap, whose release + builds named a new allocator at every malloc and aborted at the 65th + (the box's session, after 15 far joins, 2026-10-30).""" + from compression import zstd + build = semantics(binary) + + def join(): + r = Replica(sock, timeout=30) + name = session.encode() + r.send(0x01, struct.pack('= 5: + t, m = struct.unpack_from(' 2000, timeout=30) os.kill(session, signal.SIGKILL) + killed = time.monotonic() + wait_for('the replica client ended with its session', lambda: rc.poll() is not None, timeout=10) + assert time.monotonic() - killed < 2 and b'let this client go' in tr.text(), (time.monotonic() - killed, tr.text()[-300:]) + tr.close() stub.wait(timeout=10) client.socket.close() term.close() -- cgit v1.3